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