Created
December 10, 2019 15:56
-
-
Save oeway/68acb3e239c40ab1001c8c1bd70dba33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<docs lang="markdown"> | |
## Browser File System | |
A virtual nodejs-style file system in the browser | |
More information: https://github.com/jvilk/BrowserFS | |
</docs> | |
<config lang="json"> | |
{ | |
"name": "BrowserFS", | |
"type": "iframe", | |
"tags": [], | |
"ui": "", | |
"cover": "", | |
"version": "0.1.0", | |
"description": "A virtual nodejs-style file system in the browser", | |
"icon": "extension", | |
"inputs": null, | |
"outputs": null, | |
"api_version": "0.1.6", | |
"env": "", | |
"permissions": [], | |
"requirements": ["https://cdnjs.cloudflare.com/ajax/libs/BrowserFS/1.4.3/browserfs.min.js"], | |
"dependencies": [] | |
} | |
</config> | |
<script lang="javascript"> | |
const ArrayBufferView = Object.getPrototypeOf( | |
Object.getPrototypeOf(new Uint8Array()) | |
).constructor; | |
class FileSystemManager { | |
constructor() { | |
this.fs = null; | |
this.path = null; | |
this.buffer = null; | |
this.api = {}; | |
} | |
init() { | |
return new Promise((resolve, reject) => { | |
BrowserFS.configure({ | |
fs: "MountableFileSystem", | |
options: { | |
"/tmp": { fs: "InMemory", options: { storeName: "tmp" } }, | |
"/home": { fs: "IndexedDB", options: { storeName: "home" } }, | |
// '/mnt/h5': { fs: "HTML5FS", options: {} } | |
}, | |
}, | |
e => { | |
if (e) { | |
reject(e); | |
return; | |
} | |
const _fs = BrowserFS.BFSRequire("fs"); | |
const buffer = BrowserFS.BFSRequire("buffer"); | |
//convert arraybuffer to Buffer | |
var convert = function(fn) { | |
return function() { | |
const args = Array.prototype.slice.call(arguments); | |
const newargs = []; | |
for (let arg of args) { | |
if (arg instanceof ArrayBuffer) { | |
newargs.push(buffer.Buffer(arg)); | |
} else if (arg instanceof ArrayBufferView) { | |
newargs.push(buffer.Buffer(arg.buffer)); | |
} else { | |
newargs.push(arg); | |
} | |
} | |
return fn.apply(this, newargs); | |
}; | |
}; | |
this.fs = {}; | |
for (let k in _fs) { | |
this.fs[k] = convert(_fs[k]); | |
} | |
this.path = BrowserFS.BFSRequire("path"); | |
resolve({ fs: this.fs, path: this.path }); | |
}); | |
}); | |
} | |
destroy() {} | |
} | |
const fileSystem = new FileSystemManager() | |
fileSystem.init().then((fs_api)=>{ | |
console.log(fs_api) | |
fs_api.setup = ()=>{ | |
api.log('Browser file system initialized') | |
} | |
api.export(fs_api) | |
}).catch((e)=>{ | |
console.error(e) | |
api.alert('Failed to initialize the browser file system.') | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment