From b25c20e8f52c91754ad7294f7acc6b9cea149b14 Mon Sep 17 00:00:00 2001 From: Nicolas Lepage <19571875+nlepage@users.noreply.github.com> Date: Sun, 14 Jun 2020 15:26:42 +0200 Subject: [PATCH] :recycle: --- index.js | 5 +++-- sw.js | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index c2464fa..03b9362 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,15 @@ window.wasmhttp = { - register: async (wasm, { scope, base = '', swUrl = 'sw.js' } = {}) => { + register: async (wasm, { scope, base = '', swUrl = 'sw.js', argv = [] } = {}) => { const options = {} if (scope) options.scope = scope - //FIXME register once + //FIXME register once (beware of changing scope ?) const registration = await navigator.serviceWorker.register(swUrl, options) await navigator.serviceWorker.ready registration.active.postMessage({ type: 'wasmhttp.register', wasm, base, + argv, }) } } diff --git a/sw.js b/sw.js index 9506ab2..2c8aa3e 100644 --- a/sw.js +++ b/sw.js @@ -1,11 +1,13 @@ importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.14.3/misc/wasm/wasm_exec.js') -const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH) => { +// FIXME use an options object with { env, argv } +const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH, argv) => { const go = new Go() go.env = { WASMHTTP_HANDLER_ID, WASMHTTP_PATH, } + go.argv = argv const { instance } = await WebAssembly.instantiateStreaming(fetch(wasm), go.importObject) return go.run(instance) } @@ -50,7 +52,7 @@ addEventListener('message', async ({ data }) => { if (data.type !== 'wasmhttp.register') return - const { wasm, base } = data + const { wasm, base, argv } = data let path = new URL(registration.scope).pathname if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}` @@ -61,7 +63,7 @@ addEventListener('message', async ({ data }) => { const handlerId = `${nextHandlerId++}` const handler = new Promise(resolve => handlerResolvers[handlerId] = resolve) - startWasm(wasm, handlerId, path) + startWasm(wasm, handlerId, path, argv) running.add(key) // FIXME try catch