mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-11 09:39:10 +00:00
⚗️
This commit is contained in:
parent
d983ee2221
commit
b4be701615
@ -20,22 +20,7 @@
|
|||||||
<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>
|
<button onclick="hello()">Hello</button>
|
||||||
<script>
|
<script>
|
||||||
const startWasm = async (wasm, { scope, base = '' } = {}) => {
|
wasmhttp.register('api.wasm', { base: 'api' })
|
||||||
try {
|
|
||||||
const options = {}
|
|
||||||
if (scope) options.scope = scope
|
|
||||||
const registration = await navigator.serviceWorker.register('sw.js', options)
|
|
||||||
await navigator.serviceWorker.ready
|
|
||||||
registration.active.postMessage({
|
|
||||||
type: 'startWasm',
|
|
||||||
wasm,
|
|
||||||
base,
|
|
||||||
})
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
startWasm('api.wasm')
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
23
docs/sw.js
23
docs/sw.js
@ -1,23 +0,0 @@
|
|||||||
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.13.4/misc/wasm/wasm_exec.js')
|
|
||||||
|
|
||||||
addEventListener('install', (event) => {
|
|
||||||
console.log('install!')
|
|
||||||
// wasmhttp.serve({
|
|
||||||
// wasm: 'api.wasm',
|
|
||||||
// base: '/api',
|
|
||||||
// })
|
|
||||||
event.waitUntil(skipWaiting())
|
|
||||||
})
|
|
||||||
|
|
||||||
addEventListener('activate', event => {
|
|
||||||
console.log('activate!')
|
|
||||||
event.waitUntil(clients.claim())
|
|
||||||
})
|
|
||||||
|
|
||||||
addEventListener('fetch', () => {
|
|
||||||
console.log('fetch!')
|
|
||||||
})
|
|
||||||
|
|
||||||
addEventListener('message', ({ data }) => {
|
|
||||||
console.log('message', data)
|
|
||||||
})
|
|
14
index.js
Normal file
14
index.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
window.wasmhttp = {
|
||||||
|
register: async (wasm, { scope, base = '' } = {}) => {
|
||||||
|
const options = {}
|
||||||
|
if (scope) options.scope = scope
|
||||||
|
//FIXME register once
|
||||||
|
const registration = await navigator.serviceWorker.register('sw.js', options)
|
||||||
|
await navigator.serviceWorker.ready
|
||||||
|
registration.active.postMessage({
|
||||||
|
type: 'wasmhttp.register',
|
||||||
|
wasm,
|
||||||
|
base,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
68
sw.js
68
sw.js
@ -1,5 +1,4 @@
|
|||||||
let nextHandlerId = 1
|
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.13.4/misc/wasm/wasm_exec.js')
|
||||||
const handlerResolvers = {}
|
|
||||||
|
|
||||||
const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH) => {
|
const startWasm = async (wasm, WASMHTTP_HANDLER_ID, WASMHTTP_PATH) => {
|
||||||
const go = new Go()
|
const go = new Go()
|
||||||
@ -23,33 +22,50 @@ const trimEnd = (s, c) => {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addEventListener('install', (event) => {
|
||||||
|
// event.waitUntil(skipWaiting())
|
||||||
|
// })
|
||||||
|
|
||||||
|
const running = new Set()
|
||||||
|
let nextHandlerId = 1
|
||||||
|
const handlerResolvers = {}
|
||||||
|
const handlers = []
|
||||||
|
|
||||||
self.wasmhttp = {
|
self.wasmhttp = {
|
||||||
serve: ({ wasm, base } = {}) => {
|
|
||||||
try {
|
|
||||||
if (!wasm) throw TypeError('options.wasm must be defined')
|
|
||||||
|
|
||||||
const handlerId = `${nextHandlerId++}`
|
|
||||||
const handler = new Promise(resolve => handlerResolvers[handlerId] = resolve)
|
|
||||||
|
|
||||||
let path = new URL(registration.scope).pathname
|
|
||||||
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
|
|
||||||
|
|
||||||
startWasm(wasm, handlerId, path)
|
|
||||||
|
|
||||||
addEventListener('fetch', async e => {
|
|
||||||
if (!new URL(e.request.url).pathname.startsWith(path)) return
|
|
||||||
|
|
||||||
// FIXME try catch
|
|
||||||
e.respondWith((await handler)(e.request))
|
|
||||||
})
|
|
||||||
} catch (e) {
|
|
||||||
console.error('wasmhttp: error:', e)
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
registerHandler: (handlerId, handler) => {
|
registerHandler: (handlerId, handler) => {
|
||||||
handlerResolvers[handlerId](handler)
|
handlerResolvers[handlerId](handler)
|
||||||
delete handlerResolvers[handlerId]
|
delete handlerResolvers[handlerId]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addEventListener('activate', event => event.waitUntil(clients.claim()))
|
||||||
|
|
||||||
|
addEventListener('message', async ({ data }) => {
|
||||||
|
if (data.type !== 'wasmhttp.register') return
|
||||||
|
|
||||||
|
const { wasm, base } = data
|
||||||
|
|
||||||
|
let path = new URL(registration.scope).pathname
|
||||||
|
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
|
||||||
|
|
||||||
|
const key = `${wasm}:${path}`
|
||||||
|
|
||||||
|
if (!running.has(key)) {
|
||||||
|
const handlerId = `${nextHandlerId++}`
|
||||||
|
const handler = new Promise(resolve => handlerResolvers[handlerId] = resolve)
|
||||||
|
|
||||||
|
startWasm(wasm, handlerId, path)
|
||||||
|
running.add(key)
|
||||||
|
|
||||||
|
// FIXME try catch
|
||||||
|
handlers.push([path, await handler])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
addEventListener('fetch', e => {
|
||||||
|
const { pathname } = new URL(e.request.url)
|
||||||
|
const [, handler] = handlers.find(([path]) => pathname.startsWith(path)) || []
|
||||||
|
if (!handler) return
|
||||||
|
|
||||||
|
e.respondWith((handler)(e.request))
|
||||||
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user