From d4d741502a90ac109035af5d989d242e432f8d45 Mon Sep 17 00:00:00 2001 From: oeway Date: Thu, 25 Apr 2019 21:02:10 +0200 Subject: [PATCH] support write arraybuffer to api.fs --- web/src/fileSystemManager.js | 37 +++++++++++++++++++++++++++++++++++- web/src/jailed/jailed.js | 8 ++++---- web/src/pluginManager.js | 6 +++--- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/web/src/fileSystemManager.js b/web/src/fileSystemManager.js index 54a4c5897..81c0d5f9d 100644 --- a/web/src/fileSystemManager.js +++ b/web/src/fileSystemManager.js @@ -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)=>{ @@ -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) }); }) diff --git a/web/src/jailed/jailed.js b/web/src/jailed/jailed.js index 967985920..8aa5d5220 100644 --- a/web/src/jailed/jailed.js +++ b/web/src/jailed/jailed.js @@ -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; @@ -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() @@ -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." @@ -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() diff --git a/web/src/pluginManager.js b/web/src/pluginManager.js index 5911192a4..0a3eddaa4 100644 --- a/web/src/pluginManager.js +++ b/web/src/pluginManager.js @@ -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, @@ -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.') @@ -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.')