From c207b2da2a76b8b5eea100e4c6630b8f6458aea7 Mon Sep 17 00:00:00 2001 From: Leon van Kammen Date: Mon, 23 Jun 2025 22:10:47 +0200 Subject: [PATCH] added 9pfs.create_file_from_url() --- com/isoterminal.js | 7 ++++--- com/isoterminal/ISOTerminal.js | 15 +++++++-------- com/isoterminal/PromiseWorker.js | 4 ++-- com/isoterminal/feat/9pfs_utils.js | 11 +++++++++++ com/isoterminal/worker.js | 13 +++++++------ 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/com/isoterminal.js b/com/isoterminal.js index 7d32a1e..403ddd5 100644 --- a/com/isoterminal.js +++ b/com/isoterminal.js @@ -439,9 +439,10 @@ if( typeof AFRAME != 'undefined '){ this.el.addEventListener('exec', (e) => this.term.exec( e.detail ) ) this.el.addEventListener('hook', (e) => this.term.hook( e.detail[0], e.detail[1] ) ) this.el.addEventListener('send', (e) => this.term.send( e.detail[0], e.detail[1] || 0 ) ) - this.el.addEventListener('create_file', async (e) => await this.term.worker.create_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) ) - this.el.addEventListener('update_file', async (e) => await this.term.worker.update_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) ) - this.el.addEventListener('append_file', async (e) => await this.term.worker.append_file( e.detail[0], this.term.convert.toUint8Array(e.detail[1]) ) ) + this.el.addEventListener('create_file', async (e) => await this.term.worker.create_file( e.detail[0], e.detail[1] ) ) + this.el.addEventListener('create_file_from_url', async (e) => await this.term.worker.create_file_from_url( e.detail[0], e.detail[1] ) ) + this.el.addEventListener('update_file', async (e) => await this.term.worker.update_file( e.detail[0], e.detail[1] ) ) + this.el.addEventListener('append_file', async (e) => await this.term.worker.append_file( e.detail[0], e.detail[1] ) ) this.el.addEventListener('read_file', async (e) => { const buf = await this.term.worker.read_file( e.detail[0] ) const str = new TextDecoder().decode(buf) diff --git a/com/isoterminal/ISOTerminal.js b/com/isoterminal/ISOTerminal.js index bcc106a..2b73eff 100644 --- a/com/isoterminal/ISOTerminal.js +++ b/com/isoterminal/ISOTerminal.js @@ -17,15 +17,14 @@ function ISOTerminal(instance,opts){ ISOTerminal.prototype.emit = function(event,data,sender){ data = data || false + // *TODO* wrap certain events into this.preventFrameDrop( () => { .. }) to boost performance const evObj = new CustomEvent(event, {detail: data} ) - this.preventFrameDrop( () => { - // forward event to worker/instance/AFRAME element or component-function - // this feels complex, but actually keeps event- and function-names more concise in codebase - this.dispatchEvent( evObj ) - if( sender != "instance" && this.instance ) this.instance.dispatchEvent(evObj) - if( sender != "worker" && this.worker ) this.worker.postMessage({event,data}, PromiseWorker.prototype.getTransferable(data) ) - if( sender !== undefined && typeof this[event] == 'function' ) this[event].apply(this, data && data.push ? data : [data] ) - }) + // forward event to worker/instance/AFRAME element or component-function + // this feels complex, but actually keeps event- and function-names more concise in codebase + this.dispatchEvent( evObj ) + if( sender != "instance" && this.instance ) this.instance.dispatchEvent(evObj) + if( sender != "worker" && this.worker ) this.worker.postMessage({event,data}, PromiseWorker.prototype.getTransferable(data) ) + if( sender !== undefined && typeof this[event] == 'function' ) this[event].apply(this, data && data.push ? data : [data] ) } ISOTerminal.addEventListener = (event,cb) => { diff --git a/com/isoterminal/PromiseWorker.js b/com/isoterminal/PromiseWorker.js index 59447a1..a98dbfb 100644 --- a/com/isoterminal/PromiseWorker.js +++ b/com/isoterminal/PromiseWorker.js @@ -44,7 +44,8 @@ function PromiseWorker(file, onmessage){ this.resolvers = this.resolvers || {last:1,pending:{}} msg.data.promiseId = this.resolvers.last++ // Send id and task to WebWorker - worker.postMessage(msg, PromiseWorker.prototype.getTransferable(msg.data) ) + let dataTransferable = PromiseWorker.prototype.getTransferable(msg.data) + worker.postMessage(msg, dataTransferable ) return new Promise( resolve => this.resolvers.pending[ msg.data.promiseId ] = resolve ); }, @@ -72,6 +73,5 @@ PromiseWorker.prototype.getTransferable = function(data){ for( var i in data ){ if( isTransferable(data[i]) ) objs.push(data[i]) } - if( objs.length ) debugger return objs.length ? objs : undefined } diff --git a/com/isoterminal/feat/9pfs_utils.js b/com/isoterminal/feat/9pfs_utils.js index e5c7e2a..cdb49c2 100644 --- a/com/isoterminal/feat/9pfs_utils.js +++ b/com/isoterminal/feat/9pfs_utils.js @@ -28,6 +28,17 @@ emulator.fs9p.update_file = async function(file,data){ } } +emulator.fs9p.create_file_from_url = async function(file,url){ + const convert = ISOTerminal.prototype.convert + return fetch(url) + .then( (res) => res.arrayBuffer() ) + .then( (buf) => { + let arr = new Uint8Array(buf) + return emulator.create_file(file, arr ) + }) + .catch( console.error ) +} + emulator.fs9p.append_file = async function(file,data){ const convert = ISOTerminal.prototype.convert diff --git a/com/isoterminal/worker.js b/com/isoterminal/worker.js index df5279b..179a891 100644 --- a/com/isoterminal/worker.js +++ b/com/isoterminal/worker.js @@ -3,7 +3,7 @@ importScripts("ISOTerminal.js") // we don't instance it again here (just use it this.runISO = async function(opts){ this.opts = opts - if( opts.debug ) console.dir(opts) + if( opts.debug ) console.dir(opts) if( opts.cdrom && !opts.cdrom.url.match(/^http/) ) opts.cdrom.url = "../../"+opts.cdrom.url if( opts.bzimage && !opts.cdrom.url.match(/^http/) ) opts.bzimage.url = "../../"+opts.bzimage.url @@ -52,11 +52,12 @@ this.runISO = async function(opts){ arr[0] = String(arr[0]).replace(/^\/mnt/,'') return arr } - this.create_file = async function(){ return emulator.create_file.apply(emulator, stripMountDir(arguments[0]) ) } - this.read_file = async function(){ return emulator.read_file.apply(emulator, stripMountDir(arguments[0]) ) } - this.read_file_world = async function(){ return emulator.fs9p.read_file_world.apply(emulator.fs9p, stripMountDir(arguments[0]) ) } - this.append_file = async function(){ emulator.fs9p.append_file.apply(emulator.fs9p, stripMountDir(arguments[0])) } - this.update_file = async function(){ emulator.fs9p.update_file.apply(emulator.fs9p, stripMountDir(arguments[0])) } + this.create_file = async function(){ return emulator.create_file.apply(emulator, stripMountDir(arguments[0]) ) } + this.create_file_from_url = async function(){ return emulator.fs9p.create_file_from_url.apply(emulator, stripMountDir(arguments[0]) ) } + this.read_file = async function(){ return emulator.read_file.apply(emulator, stripMountDir(arguments[0]) ) } + this.read_file_world = async function(){ return emulator.fs9p.read_file_world.apply(emulator.fs9p, stripMountDir(arguments[0]) ) } + this.append_file = async function(){ emulator.fs9p.append_file.apply(emulator.fs9p, stripMountDir(arguments[0])) } + this.update_file = async function(){ emulator.fs9p.update_file.apply(emulator.fs9p, stripMountDir(arguments[0])) } // filename will be read from 9pfs: "/mnt/"+filename emulator.readFromPipe = function(filename,cb){