From 268c971467eaff33e0ccad5554e70e3a1cb2017f Mon Sep 17 00:00:00 2001 From: Eli Davis Date: Wed, 5 Feb 2025 17:48:27 -0800 Subject: [PATCH 1/3] Provide ability to passthrough and hit network --- sw.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sw.js b/sw.js index 7348a6d..f12bb86 100644 --- a/sw.js +++ b/sw.js @@ -1,4 +1,4 @@ -function registerWasmHTTPListener(wasm, { base, cacheName, args = [] } = {}) { +function registerWasmHTTPListener(wasm, { base, cacheName, passthroughFunc, args = [] } = {}) { let path = new URL(registration.scope).pathname if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}` @@ -17,6 +17,10 @@ function registerWasmHTTPListener(wasm, { base, cacheName, args = [] } = {}) { WebAssembly.instantiateStreaming(source, go.importObject).then(({ instance }) => go.run(instance)) addEventListener('fetch', e => { + if (passthroughFunc && passthroughFunc(e.request)) { + e.respondWith(fetch(e.request)) + return; + } const { pathname } = new URL(e.request.url) if (!pathname.startsWith(path)) return From 38c1814f66cec7dc62c3aa79ff5dacec54d32e46 Mon Sep 17 00:00:00 2001 From: Nicolas Lepage <19571875+nlepage@users.noreply.github.com> Date: Thu, 6 Feb 2025 09:42:59 +0100 Subject: [PATCH 2/3] refactor: change signature for passthrough function --- sw.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sw.js b/sw.js index f12bb86..11e0ae0 100644 --- a/sw.js +++ b/sw.js @@ -1,4 +1,4 @@ -function registerWasmHTTPListener(wasm, { base, cacheName, passthroughFunc, args = [] } = {}) { +function registerWasmHTTPListener(wasm, { base, cacheName, passthrough, args = [] } = {}) { let path = new URL(registration.scope).pathname if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}` @@ -17,10 +17,11 @@ function registerWasmHTTPListener(wasm, { base, cacheName, passthroughFunc, args WebAssembly.instantiateStreaming(source, go.importObject).then(({ instance }) => go.run(instance)) addEventListener('fetch', e => { - if (passthroughFunc && passthroughFunc(e.request)) { + if (passthrough?.(e.request)) { e.respondWith(fetch(e.request)) return; } + const { pathname } = new URL(e.request.url) if (!pathname.startsWith(path)) return From 5dc06a3dbbd50a9cc72ede6b4bbf6f778dfb3075 Mon Sep 17 00:00:00 2001 From: Nicolas Lepage <19571875+nlepage@users.noreply.github.com> Date: Thu, 6 Feb 2025 09:55:58 +0100 Subject: [PATCH 3/3] docs: updates documentation for registerWasmHTTPListener --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0f92940..fe00359 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,9 @@ URL string of the WebAssembly module, example: `"path/to/my-module.wasm"`. An optional object containing: - `base` (`string`): Base path of the server, relative to the ServiceWorker's scope. +- `cacheName` (`string`): Name of the [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to store the WebAssembly binary. - `args` (`string[]`): Arguments for the WebAssembly module. +- `passthrough` (`(request: Request): boolean`): Optional callback to allow passing the request through to network. ## Contributors ✨