added 9pfs.create_file_from_url()
All checks were successful
/ mirror_to_github (push) Successful in 36s
/ test (push) Successful in 9s

This commit is contained in:
Leon van Kammen 2025-06-23 22:10:47 +02:00
parent c97875e3e5
commit c207b2da2a
5 changed files with 31 additions and 19 deletions

View file

@ -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)

View file

@ -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] )
})
}
ISOTerminal.addEventListener = (event,cb) => {

View file

@ -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
}

View file

@ -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

View file

@ -53,6 +53,7 @@ this.runISO = async function(opts){
return arr
}
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])) }