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
9ffd514abc
commit
3c63741d6a
47
sw.js
47
sw.js
@ -1,6 +1,6 @@
|
|||||||
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.15.7/misc/wasm/wasm_exec.js')
|
importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.15.7/misc/wasm/wasm_exec.js')
|
||||||
|
|
||||||
const startWasm = async (wasm, { env, args = [] }) => {
|
async function startWasm(wasm, { env, args = [] }) => {
|
||||||
const go = new Go()
|
const go = new Go()
|
||||||
go.env = env
|
go.env = env
|
||||||
go.argv = [wasm, ...args]
|
go.argv = [wasm, ...args]
|
||||||
@ -8,13 +8,13 @@ const startWasm = async (wasm, { env, args = [] }) => {
|
|||||||
return go.run(instance)
|
return go.run(instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
const trimStart = (s, c) => {
|
function trimStart(s, c) {
|
||||||
let r = s
|
let r = s
|
||||||
while (r.startsWith(c)) r = r.slice(c.length)
|
while (r.startsWith(c)) r = r.slice(c.length)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
const trimEnd = (s, c) => {
|
function trimEnd(s, c) {
|
||||||
let r = s
|
let r = s
|
||||||
while (r.endsWith(c)) r = r.slice(0, -c.length)
|
while (r.endsWith(c)) r = r.slice(0, -c.length)
|
||||||
return r
|
return r
|
||||||
@ -24,7 +24,6 @@ const trimEnd = (s, c) => {
|
|||||||
// event.waitUntil(skipWaiting())
|
// event.waitUntil(skipWaiting())
|
||||||
// })
|
// })
|
||||||
|
|
||||||
const running = new Set()
|
|
||||||
let nextHandlerId = 1
|
let nextHandlerId = 1
|
||||||
const handlerResolvers = {}
|
const handlerResolvers = {}
|
||||||
const handlers = []
|
const handlers = []
|
||||||
@ -36,55 +35,23 @@ self.wasmhttp = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('aha!')
|
|
||||||
|
|
||||||
addEventListener('activate', event => {
|
addEventListener('activate', event => {
|
||||||
console.log('activate!')
|
console.log('activate!')
|
||||||
event.waitUntil(clients.claim())
|
event.waitUntil(clients.claim())
|
||||||
})
|
})
|
||||||
|
|
||||||
addEventListener('message', async ({ data }) => {
|
|
||||||
console.log('message!', data)
|
|
||||||
|
|
||||||
if (data.type !== 'wasmhttp.register') return
|
|
||||||
|
|
||||||
const { wasm, base, args } = 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, { env: { WASMHTTP_HANDLER_ID: handlerId, WASMHTTP_PATH: path }, args })
|
|
||||||
running.add(key)
|
|
||||||
|
|
||||||
// FIXME try catch
|
|
||||||
handlers.push([path, await handler])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
addEventListener('fetch', e => {
|
|
||||||
console.log('fetch')
|
|
||||||
|
|
||||||
const { pathname } = new URL(e.request.url)
|
|
||||||
const [, handler] = handlers.find(([path]) => pathname.startsWith(path)) || []
|
|
||||||
if (!handler) return
|
|
||||||
|
|
||||||
e.respondWith(handler(e.request))
|
|
||||||
})
|
|
||||||
|
|
||||||
function registerWasmHTTPListener(wasm, base, args) {
|
function registerWasmHTTPListener(wasm, base, args) {
|
||||||
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, '/')}`
|
||||||
|
|
||||||
addEventListener('fetch', async e => {
|
addEventListener('fetch', async e => {
|
||||||
|
console.log("new fetch !")
|
||||||
|
|
||||||
const { pathname } = new URL(e.request.url)
|
const { pathname } = new URL(e.request.url)
|
||||||
if (!pathname.startsWith(path)) return
|
if (!pathname.startsWith(path)) return
|
||||||
|
|
||||||
|
console.log("path OK!")
|
||||||
|
|
||||||
const handlerId = `${nextHandlerId++}`
|
const handlerId = `${nextHandlerId++}`
|
||||||
const handlerPromise = new Promise(resolve => handlerResolvers[handlerId] = resolve)
|
const handlerPromise = new Promise(resolve => handlerResolvers[handlerId] = resolve)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user