7 Commits

Author SHA1 Message Date
Nicolas Lepage
fb62588b31 feat: adds Referer, Host, RequestURI and User-Agent on request 2025-11-21 10:16:13 +01:00
Nicolas Lepage
9ae9f3c443 feat: allows registering multiple wasm instances 2025-11-21 01:04:46 +01:00
Nicolas Lepage
9ff6ec615a docs: updates build tags and adds warning for FOSDEM video 2025-02-13 10:41:03 +01:00
Nicolas Lepage
ac1cfbfe8a chore: v2.2.1 2025-02-07 11:48:06 +01:00
Nicolas Lepage
f0a7bf8f2b docs: updates EliCDavis’ contribution 2025-02-07 11:46:33 +01:00
Nicolas Lepage
d7055fb5ea [BUGFIX] Writes default header when response close and no header already written 2025-02-07 11:45:18 +01:00
Nicolas Lepage
b9921d7d43 fix: writes default header when response closed and no header written 2025-02-07 11:42:55 +01:00
28 changed files with 335 additions and 152 deletions

View File

@@ -27,7 +27,8 @@
"avatar_url": "https://avatars.githubusercontent.com/u/9094977?v=4",
"profile": "https://recolude.com/",
"contributions": [
"code"
"code",
"bug"
]
}
],

5
.prettierrc Normal file
View File

@@ -0,0 +1,5 @@
{
"printWidth": 120,
"singleQuote": true,
"trailingComma": "all"
}

View File

@@ -22,7 +22,11 @@
## How?
Talk given at the Go devroom of FOSDEM 2021 explaining how `go-wasm-http-server` works:
Below is a talk given at the Go devroom of FOSDEM 2021 explaining how `go-wasm-http-server` works.
> [!WARNING]
> `go-wasm-http-server` has suffered major changes since this talk, be aware that it is not accurate anymore on several aspects.
> Please refer to the documentation below for up to date usage of `go-wasm-http-server`.
[![Deploy a Go HTTP server in your browser Youtube link](https://raw.githubusercontent.com/nlepage/go-wasm-http-talk/main/youtube.png)](https://youtu.be/O2RB_8ircdE)
@@ -47,7 +51,7 @@ In your Go code, replace [`http.ListenAndServe()`](https://pkg.go.dev/net/http#L
📄 `server.go`
```go
// +build !js,!wasm
//go:build !js && !wasm
package main
@@ -66,7 +70,7 @@ becomes:
📄 `server_js_wasm.go`
```go
// +build js,wasm
//go:build js && wasm
package main
@@ -116,7 +120,7 @@ importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.4/misc/wasm/wasm_exe
// If you compiled with TinyGo then, similarly, use:
importScripts('https://cdn.jsdelivr.net/gh/tinygo-org/tinygo@0.35.0/targets/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.0/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.1/sw.js')
registerWasmHTTPListener('path/to/server.wasm')
```
@@ -209,7 +213,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://byjp.me/"><img src="https://avatars.githubusercontent.com/u/42999?v=4?s=100" width="100px;" alt="JP Hastings-Edrei"/><br /><sub><b>JP Hastings-Edrei</b></sub></a><br /><a href="https://github.com/nlepage/go-wasm-http-server/commits?author=jphastings" title="Code">💻</a> <a href="https://github.com/nlepage/go-wasm-http-server/commits?author=jphastings" title="Documentation">📖</a> <a href="#example-jphastings" title="Examples">💡</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://recolude.com/"><img src="https://avatars.githubusercontent.com/u/9094977?v=4?s=100" width="100px;" alt="Eli Davis"/><br /><sub><b>Eli Davis</b></sub></a><br /><a href="https://github.com/nlepage/go-wasm-http-server/commits?author=EliCDavis" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://recolude.com/"><img src="https://avatars.githubusercontent.com/u/9094977?v=4?s=100" width="100px;" alt="Eli Davis"/><br /><sub><b>Eli Davis</b></sub></a><br /><a href="https://github.com/nlepage/go-wasm-http-server/commits?author=EliCDavis" title="Code">💻</a> <a href="https://github.com/nlepage/go-wasm-http-server/issues?q=author%3AEliCDavis" title="Bug reports">🐛</a></td>
</tr>
</tbody>
</table>

View File

@@ -0,0 +1,27 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
wasmhttp "github.com/nlepage/go-wasm-http-server/v2"
)
var binaryName = ""
func main() {
http.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
res.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(res).Encode(map[string]string{
"message": fmt.Sprintf("Hello from %s at path %s", binaryName, os.Getenv("WASM_HTTP_PATH")),
}); err != nil {
panic(err)
}
})
wasmhttp.Serve(nil)
select {}
}

BIN
docs/hello-multiple/api1.wasm Executable file

Binary file not shown.

BIN
docs/hello-multiple/api2.wasm Executable file

Binary file not shown.

3
docs/hello-multiple/build.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/sh
GOOS=js GOARCH=wasm go build -o api1.wasm --ldflags="-X 'main.binaryName=api1.wasm'" .
GOOS=js GOARCH=wasm go build -o api2.wasm --ldflags="-X 'main.binaryName=api2.wasm'" .

View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>go-wasm-http-server hello demo</title>
<script>
navigator.serviceWorker.register('sw.js');
async function hello() {
const api = document.querySelector('#api').value;
const res = await fetch(`${api}/hello`);
const { message } = await res.json();
alert(message);
}
</script>
</head>
<body>
<label for="api">API: </label
><select id="api">
<option value="api1/1">api1.wasm first instance</option>
<option value="api1/2">api1.wasm second instance</option>
<option value="api2/1">api2.wasm first instance</option>
<option value="api2/2">api2.wasm second instance</option>
</select>
<button onclick="hello()">Hello</button>
</body>
</html>

15
docs/hello-multiple/sw.js Normal file
View File

@@ -0,0 +1,15 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.25.1/lib/wasm/wasm_exec.js');
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@master/sw.js');
addEventListener('install', (event) => {
event.waitUntil(caches.open('examples').then((cache) => cache.addAll(['api1.wasm', 'api2.wasm'])));
});
addEventListener('activate', (event) => {
event.waitUntil(clients.claim());
});
registerWasmHTTPListener('api1.wasm', { base: 'api1/1/' });
registerWasmHTTPListener('api1.wasm', { base: 'api1/2/' });
registerWasmHTTPListener('api2.wasm', { base: 'api2/1/' });
registerWasmHTTPListener('api2.wasm', { base: 'api2/2/' });

Binary file not shown.

View File

@@ -3,47 +3,46 @@
<head>
<title>go-wasm-http-server hello sse demo</title>
<script>
navigator.serviceWorker.register('sw.js')
.then(registration => {
const serviceWorker = registration.installing ?? registration.waiting ?? registration.active
if (serviceWorker.state === 'activated') {
document.querySelector('#open-button').disabled = false
} else {
serviceWorker.addEventListener('statechange', e => {
if (e.target.state === 'activated') {
document.querySelector('#open-button').disabled = false
}
})
}
})
navigator.serviceWorker.register('sw.js').then((registration) => {
const serviceWorker = registration.installing ?? registration.waiting ?? registration.active;
if (serviceWorker.state === 'activated') {
document.querySelector('#open-button').disabled = false;
} else {
serviceWorker.addEventListener('statechange', (e) => {
if (e.target.state === 'activated') {
document.querySelector('#open-button').disabled = false;
}
});
}
});
window.addEventListener('DOMContentLoaded', () => {
let es;
document.querySelector('#open-button').addEventListener('click', () => {
if (es && es.readyState === es.OPEN) return
es = new EventSource('api/events')
if (es && es.readyState === es.OPEN) return;
es = new EventSource('api/events');
es.addEventListener('ping', (e) => {
const li = document.createElement('li')
li.textContent = `ping: data=${e.data}`
document.querySelector('ul').append(li)
})
})
const li = document.createElement('li');
li.textContent = `ping: data=${e.data}`;
document.querySelector('ul').append(li);
});
});
document.querySelector('#close-button').addEventListener('click', () => {
if (!es) return
es.close()
})
if (!es) return;
es.close();
});
document.querySelector('#clear-button').addEventListener('click', () => {
document.querySelector('ul').innerHTML = ''
})
document.querySelector('ul').innerHTML = '';
});
window.addEventListener('unload', () => {
if (!es) return
es.close()
})
})
if (!es) return;
es.close();
});
});
</script>
</head>
<body>

View File

@@ -1,14 +1,14 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.0/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.25.1/lib/wasm/wasm_exec.js');
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@master/sw.js');
const wasm = 'api.wasm'
const wasm = 'api.wasm';
addEventListener('install', (event) => {
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)))
})
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)));
});
addEventListener('activate', (event) => {
event.waitUntil(clients.claim())
})
event.waitUntil(clients.claim());
});
registerWasmHTTPListener(wasm, { base: 'api' })
registerWasmHTTPListener(wasm, { base: 'api' });

View File

@@ -3,30 +3,30 @@
<head>
<title>go-wasm-http-server hello with state and keepalive demo</title>
<script>
navigator.serviceWorker.register('sw.js').then(registration => {
const sw = registration.active ?? registration.installing ?? registration.waiting
setInterval(() => sw.postMessage(null), 15000)
})
navigator.serviceWorker.register('sw.js').then((registration) => {
const sw = registration.active ?? registration.installing ?? registration.waiting;
setInterval(() => sw.postMessage(null), 15000);
});
async function hello() {
const name = document.querySelector("#name").value
const name = document.querySelector('#name').value;
const res = await fetch('api/hello', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({ name })
})
body: JSON.stringify({ name }),
});
const { message } = await res.json()
const { message } = await res.json();
alert(message)
alert(message);
}
</script>
</head>
<body>
<label for="name">Name: </label><input id="name" value="World">
<label for="name">Name: </label><input id="name" value="World" />
<button onclick="hello()">Hello</button>
</body>
</html>

View File

@@ -1,16 +1,16 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
importScripts('../sw.js')
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.25.1/lib/wasm/wasm_exec.js');
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@master/sw.js');
const wasm = '../hello-state/api.wasm'
const wasm = '../hello-state/api.wasm';
addEventListener('install', event => {
event.waitUntil(caches.open('hello-state').then((cache) => cache.add(wasm)))
})
addEventListener('install', (event) => {
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)));
});
addEventListener('activate', event => {
event.waitUntil(clients.claim())
})
addEventListener('activate', (event) => {
event.waitUntil(clients.claim());
});
addEventListener('message', () => {})
addEventListener('message', () => {});
registerWasmHTTPListener(wasm, { base: 'api' })
registerWasmHTTPListener(wasm, { base: 'api' });

Binary file not shown.

View File

@@ -3,27 +3,27 @@
<head>
<title>go-wasm-http-server hello with state demo</title>
<script>
navigator.serviceWorker.register('sw.js')
navigator.serviceWorker.register('sw.js');
async function hello() {
const name = document.querySelector("#name").value
const name = document.querySelector('#name').value;
const res = await fetch('api/hello', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({ name })
})
body: JSON.stringify({ name }),
});
const { message } = await res.json()
const { message } = await res.json();
alert(message)
alert(message);
}
</script>
</head>
<body>
<label for="name">Name: </label><input id="name" value="World">
<label for="name">Name: </label><input id="name" value="World" />
<button onclick="hello()">Hello</button>
</body>
</html>

View File

@@ -1,14 +1,14 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.0/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.25.1/lib/wasm/wasm_exec.js');
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@master/sw.js');
const wasm = 'api.wasm'
const wasm = 'api.wasm';
addEventListener('install', (event) => {
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)))
})
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)));
});
addEventListener('activate', (event) => {
event.waitUntil(clients.claim())
})
event.waitUntil(clients.claim());
});
registerWasmHTTPListener(wasm, { base: 'api' })
registerWasmHTTPListener(wasm, { base: 'api' });

Binary file not shown.

View File

@@ -3,27 +3,27 @@
<head>
<title>go-wasm-http-server hello demo</title>
<script>
navigator.serviceWorker.register('sw.js')
navigator.serviceWorker.register('sw.js');
async function hello() {
const name = document.querySelector("#name").value
const name = document.querySelector('#name').value;
const res = await fetch('api/hello', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({ name })
})
body: JSON.stringify({ name }),
});
const { message } = await res.json()
const { message } = await res.json();
alert(message)
alert(message);
}
</script>
</head>
<body>
<label for="name">Name: </label><input id="name" value="World">
<label for="name">Name: </label><input id="name" value="World" />
<button onclick="hello()">Hello</button>
</body>
</html>

View File

@@ -1,14 +1,14 @@
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.0/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.25.1/lib/wasm/wasm_exec.js');
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@master/sw.js');
const wasm = 'api.wasm'
const wasm = 'api.wasm';
addEventListener('install', (event) => {
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)))
})
event.waitUntil(caches.open('examples').then((cache) => cache.add(wasm)));
});
addEventListener('activate', (event) => {
event.waitUntil(clients.claim())
})
event.waitUntil(clients.claim());
});
registerWasmHTTPListener(wasm, { base: 'api' })
registerWasmHTTPListener(wasm, { base: 'api' });

View File

@@ -6,11 +6,40 @@
<body>
<h1>go-wasm-http-server examples</h1>
<ul>
<li><a href="hello/">Hello example</a> (<a href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello">Sources</a>)</li>
<li><a href="hello-state/">Hello example with state</a> (<a href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-state">Sources</a>)</li>
<li><a href="hello-state-keepalive/">Hello example with state and keepalive</a> (<a href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-state-keepalive">Sources</a>)</li>
<li><a href="hello-sse/">Hello example with Server Sent Events</a> (<a href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-sse">Sources</a>)</li>
<li>
<a href="hello/">Hello example</a> (<a
href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello"
>Sources</a
>)
</li>
<li>
<a href="hello-multiple/">Hello example with multiple wasm instances</a> (<a
href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-multiple"
>Sources</a
>)
</li>
<li>
<a href="hello-state/">Hello example with state</a> (<a
href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-state"
>Sources</a
>)
</li>
<li>
<a href="hello-state-keepalive/">Hello example with state and keepalive</a> (<a
href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-state-keepalive"
>Sources</a
>)
</li>
<li>
<a href="hello-sse/">Hello example with Server Sent Events</a> (<a
href="https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-sse"
>Sources</a
>)
</li>
</ul>
<p>See <a href="https://github.com/nlepage/go-wasm-http-server?tab=readme-ov-file#readme">README</a> for more information.</p>
<p>
See <a href="https://github.com/nlepage/go-wasm-http-server?tab=readme-ov-file#readme">README</a> for more
information.
</p>
</body>
</html>

View File

@@ -1,5 +1,5 @@
importScripts('https://cdn.jsdelivr.net/gh/tinygo-org/tinygo@0.35.0/targets/wasm_exec.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.0/sw.js')
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.2.1/sw.js')
const wasm = 'api.wasm'

6
go.mod
View File

@@ -1,9 +1,9 @@
module github.com/nlepage/go-wasm-http-server/v2
go 1.18
go 1.22
require (
github.com/hack-pad/safejs v0.1.1
github.com/nlepage/go-js-promise v1.0.0
github.com/tmaxmax/go-sse v0.8.0
github.com/nlepage/go-js-promise v1.1.0
github.com/tmaxmax/go-sse v0.11.0
)

11
go.sum
View File

@@ -1,9 +1,6 @@
github.com/hack-pad/safejs v0.1.1 h1:d5qPO0iQ7h2oVtpzGnLExE+Wn9AtytxIfltcS2b9KD8=
github.com/hack-pad/safejs v0.1.1/go.mod h1:HdS+bKF1NrE72VoXZeWzxFOVQVUSqZJAG0xNCnb+Tio=
github.com/nlepage/go-js-promise v1.0.0 h1:K7OmJ3+0BgWJ2LfXchg2sI6RDr7AW/KWR8182epFwGQ=
github.com/nlepage/go-js-promise v1.0.0/go.mod h1:bdOP0wObXu34euibyK39K1hoBCtlgTKXGc56AGflaRo=
github.com/tmaxmax/go-sse v0.8.0 h1:pPpTgyyi1r7vG2o6icebnpGEh3ebcnBXqDWkb7aTofs=
github.com/tmaxmax/go-sse v0.8.0/go.mod h1:HLoxqxdH+7oSUItjtnpxjzJedfr/+Rrm/dNWBcTxJFM=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
github.com/nlepage/go-js-promise v1.1.0 h1:BfvywsIMo4cpNOKyoReBWkxEW8f9HMwXqGc45wEKPRs=
github.com/nlepage/go-js-promise v1.1.0/go.mod h1:bdOP0wObXu34euibyK39K1hoBCtlgTKXGc56AGflaRo=
github.com/tmaxmax/go-sse v0.11.0 h1:nogmJM6rJUoOLoAwEKeQe5XlVpt9l7N82SS1jI7lWFg=
github.com/tmaxmax/go-sse v0.11.0/go.mod h1:u/2kZQR1tyngo1lKaNCj1mJmhXGZWS1Zs5yiSOD+Eg8=

View File

@@ -11,6 +11,10 @@ import (
"github.com/nlepage/go-wasm-http-server/v2/internal/safejs"
)
var (
navigator = safejs.Safe(js.Global().Get("navigator"))
)
// Request builds and returns the equivalent http.Request
func Request(uvalue js.Value) (*http.Request, error) {
value := safejs.Safe(uvalue)
@@ -66,13 +70,21 @@ func Request(uvalue js.Value) (*http.Request, error) {
req := &http.Request{
Method: method,
URL: u,
Host: u.Host,
Body: bodyReader,
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
RequestURI: rawURL,
}
referer, err := value.GetString("referrer")
if err != nil {
return nil, err
}
req.Header.Set("Referer", referer)
headers, err := value.Get("headers")
if err != nil {
return nil, err
@@ -115,5 +127,13 @@ func Request(uvalue js.Value) (*http.Request, error) {
req.Header.Set(key, value)
}
if req.UserAgent() == "" {
userAgent, err := navigator.GetString("userAgent")
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
}
return req, nil
}

View File

@@ -123,6 +123,9 @@ func (r *response) Flush() {
// Close implements [io.Closer]
func (r *response) Close() error {
if !r.wroteHeader {
r.WriteHeader(200)
}
if err := r.body.Flush(); err != nil {
return err
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"os"
"strings"
"syscall/js"
@@ -21,10 +22,8 @@ func Serve(handler http.Handler) (func(), error) {
h = http.DefaultServeMux
}
prefix, err := wasmhttp.GetString("path")
if err != nil {
return nil, err
}
path := os.Getenv("WASM_HTTP_PATH")
prefix := path
for strings.HasSuffix(prefix, "/") {
prefix = strings.TrimSuffix(prefix, "/")
@@ -78,7 +77,7 @@ func Serve(handler http.Handler) (func(), error) {
return nil, err
}
if _, err = wasmhttp.Call("setHandler", handlerValue); err != nil {
if _, err = wasmhttp.Call("setHandler", path, handlerValue); err != nil {
return nil, err
}

112
sw.js
View File

@@ -1,42 +1,94 @@
function registerWasmHTTPListener(wasm, { base, cacheName, passthrough, args = [] } = {}) {
let path = new URL(registration.scope).pathname
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
class WasmHTTP {
#listeners = new Map();
const handlerPromise = new Promise(setHandler => {
self.wasmhttp = {
path,
setHandler,
setHandler(path, handler) {
const listener = this.#listeners.get(path);
if (!listener) {
throw new Error(`no listener for path "${path}"`);
}
})
const go = new Go()
go.argv = [wasm, ...args]
const source = cacheName
? caches.open(cacheName).then((cache) => cache.match(wasm)).then((response) => response ?? fetch(wasm))
: caches.match(wasm).then(response => (response) ?? fetch(wasm))
WebAssembly.instantiateStreaming(source, go.importObject).then(({ instance }) => go.run(instance))
listener.setHandler(handler);
}
addEventListener('fetch', e => {
if (passthrough?.(e.request)) {
e.respondWith(fetch(e.request))
handle(e) {
const { pathname } = new URL(e.request.url);
for (const [path, listener] of this.#listeners) {
if (pathname.startsWith(path)) {
listener.handle(e);
return;
}
}
}
addListener({ wasm, base = '', cacheName, passthrough, args = [], env = {} }) {
const path = new URL(trimStart(base, '/'), registration.scope).pathname;
if (this.#listeners.has(path)) {
throw new Error(`a listener is already registered for path "${path}"`);
}
const { pathname } = new URL(e.request.url)
if (!pathname.startsWith(path)) return
this.#listeners.set(path, new WasmHTTPListener({ wasm, path, cacheName, passthrough, args, env }));
}
}
e.respondWith(handlerPromise.then(handler => handler(e.request)))
})
class WasmHTTPListener {
#handlerPromise;
#resolveHandler;
#passthrough;
constructor({ wasm, path, cacheName, passthrough, args, env }) {
this.#handlerPromise = new Promise((resolve) => {
this.#resolveHandler = resolve;
});
this.#passthrough = passthrough;
this.#run({ wasm, path, cacheName, passthrough, args, env });
}
async #run({ wasm, path, cacheName, args, env }) {
try {
const go = new Go();
go.argv = [wasm, ...args];
go.env = { ...env, WASM_HTTP_PATH: path };
const cache = cacheName ? await caches.open(cacheName) : caches;
const source = (await cache.match(wasm)) ?? (await fetch(wasm));
const { instance } = await WebAssembly.instantiateStreaming(source, go.importObject);
await go.run(instance);
} catch (err) {
console.error(`error while running ${wasm} for path "${path}"`, err);
}
}
setHandler(handler) {
this.#resolveHandler(handler);
}
handle(e) {
if (this.#passthrough?.(e.request)) return;
// FIXME return 500 if run has thrown
e.respondWith(this.#handlerPromise.then((handler) => handler(e.request)));
}
}
self.wasmhttp = new WasmHTTP();
addEventListener('fetch', (e) => {
self.wasmhttp.handle(e);
});
function registerWasmHTTPListener(wasm, { base, cacheName, passthrough, args, env } = {}) {
self.wasmhttp.addListener({ wasm, base, cacheName, passthrough, args, env });
}
function trimStart(s, c) {
let r = s
while (r.startsWith(c)) r = r.slice(c.length)
return r
}
function trimEnd(s, c) {
let r = s
while (r.endsWith(c)) r = r.slice(0, -c.length)
return r
let r = s;
while (r.startsWith(c)) r = r.slice(c.length);
return r;
}