mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-10 17:29:10 +00:00
♻️
This commit is contained in:
parent
b25c20e8f5
commit
21d93ddbbb
4
index.js
4
index.js
@ -1,5 +1,5 @@
|
||||
window.wasmhttp = {
|
||||
register: async (wasm, { scope, base = '', swUrl = 'sw.js', argv = [] } = {}) => {
|
||||
register: async (wasm, { scope, base = '', swUrl = 'sw.js', args = [] } = {}) => {
|
||||
const options = {}
|
||||
if (scope) options.scope = scope
|
||||
//FIXME register once (beware of changing scope ?)
|
||||
@ -9,7 +9,7 @@ window.wasmhttp = {
|
||||
type: 'wasmhttp.register',
|
||||
wasm,
|
||||
base,
|
||||
argv,
|
||||
args,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
8
sw.js
8
sw.js
@ -1,13 +1,13 @@
|
||||
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.14.3/misc/wasm/wasm_exec.js')
|
||||
|
||||
// FIXME use an options object with { env, argv }
|
||||
const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH, argv) => {
|
||||
const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH, args) => {
|
||||
const go = new Go()
|
||||
go.env = {
|
||||
WASMHTTP_HANDLER_ID,
|
||||
WASMHTTP_PATH,
|
||||
}
|
||||
go.argv = argv
|
||||
go.argv = [wasm, ...args]
|
||||
const { instance } = await WebAssembly.instantiateStreaming(fetch(wasm), go.importObject)
|
||||
return go.run(instance)
|
||||
}
|
||||
@ -52,7 +52,7 @@ addEventListener('message', async ({ data }) => {
|
||||
|
||||
if (data.type !== 'wasmhttp.register') return
|
||||
|
||||
const { wasm, base, argv } = data
|
||||
const { wasm, base, args } = data
|
||||
|
||||
let path = new URL(registration.scope).pathname
|
||||
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
|
||||
@ -63,7 +63,7 @@ addEventListener('message', async ({ data }) => {
|
||||
const handlerId = `${nextHandlerId++}`
|
||||
const handler = new Promise(resolve => handlerResolvers[handlerId] = resolve)
|
||||
|
||||
startWasm(wasm, handlerId, path, argv)
|
||||
startWasm(wasm, handlerId, path, args)
|
||||
running.add(key)
|
||||
|
||||
// FIXME try catch
|
||||
|
Loading…
x
Reference in New Issue
Block a user