Skip to content

Commit

Permalink
support write arraybuffer to api.fs
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Apr 25, 2019
1 parent b9b9dab commit d4d7415
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
37 changes: 36 additions & 1 deletion web/src/fileSystemManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import * as BrowserFS from 'browserfs'

const ArrayBufferView = Object.getPrototypeOf(Object.getPrototypeOf(new Uint8Array)).constructor;

export class FileSystemManager {
constructor(){
this.fs = null
this.path = null
this.buffer = null
this.api = {}
}
init(){
return new Promise((resolve, reject)=>{
Expand All @@ -18,7 +23,37 @@ export class FileSystemManager {
reject(e)
return
}
this.fs = BrowserFS.BFSRequire('fs')
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')
this.api = {fs: this.fs, path: this.path}

resolve(this.fs)
});
})
Expand Down
8 changes: 4 additions & 4 deletions web/src/jailed/jailed.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ Connection.prototype.disconnect = function() {
* @param {String} url of a plugin source
* @param {Object} _interface to provide for the plugin
*/
var Plugin = function( config, _interface, _fs, is_proxy) {
var Plugin = function( config, _interface, _fs_api, is_proxy) {
this.config = config
this.id = config.id || randId();
this._id = config._id;
Expand All @@ -799,7 +799,7 @@ var Plugin = function( config, _interface, _fs, is_proxy) {
else{
this._disconnected = true;
this._bindInterface(_interface);
this._initialInterface.fs = _fs;
for(let k in _fs_api) this._initialInterface[k] = _fs_api[k];
this._connect();
}
this._updateUI()
Expand All @@ -813,7 +813,7 @@ var Plugin = function( config, _interface, _fs, is_proxy) {
* @param {String} code of the plugin
* @param {Object} _interface to provide to the plugin
*/
var DynamicPlugin = function(config, _interface, _fs, is_proxy) {
var DynamicPlugin = function(config, _interface, _fs_api, is_proxy) {
this.config = config
if(!this.config.script){
throw "you must specify the script for the plugin to run."
Expand All @@ -837,7 +837,7 @@ var DynamicPlugin = function(config, _interface, _fs, is_proxy) {
else{
this._disconnected = true;
this._bindInterface(_interface);
this._initialInterface.fs = _fs;
for(let k in _fs_api) this._initialInterface[k] = _fs_api[k];
this._connect();
}
this._updateUI()
Expand Down
6 changes: 3 additions & 3 deletions web/src/pluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ export class PluginManager {
const _interface = _.assign({TAG: tconfig.tag, WORKSPACE: this.selected_workspace}, this.imjoy_api)

// create a proxy plugin
const plugin = new DynamicPlugin(tconfig, _interface, this.fm.fs, true)
const plugin = new DynamicPlugin(tconfig, _interface, this.fm.api, true)
plugin.api = {
__jailed_type__: 'plugin_api',
__id__: plugin.id,
Expand Down Expand Up @@ -1054,7 +1054,7 @@ export class PluginManager {
const tconfig = _.assign({}, template, config)
tconfig.workspace = this.selected_workspace
const _interface = _.assign({TAG: tconfig.tag, WORKSPACE: this.selected_workspace}, this.imjoy_api)
const plugin = new DynamicPlugin(tconfig, _interface, this.fm.fs)
const plugin = new DynamicPlugin(tconfig, _interface, this.fm.api)
plugin.whenConnected(() => {
if (!plugin.api) {
console.error('Error occured when loading plugin.')
Expand Down Expand Up @@ -1143,7 +1143,7 @@ export class PluginManager {
const tconfig = _.assign({}, pconfig.plugin, pconfig)
tconfig.workspace = this.selected_workspace
const _interface = _.assign({TAG: tconfig.tag, WORKSPACE: this.selected_workspace}, this.imjoy_api)
const plugin = new DynamicPlugin(tconfig, _interface, this.fm.fs)
const plugin = new DynamicPlugin(tconfig, _interface, this.fm.api)
plugin.whenConnected(() => {
if (!plugin.api) {
console.error('the window plugin seems not ready.')
Expand Down

0 comments on commit d4d7415

Please sign in to comment.