mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-10 17:29:10 +00:00
⚗️
This commit is contained in:
parent
28bf05797e
commit
0bc6fbc825
@ -22,5 +22,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
fmt.Println("Calling ServeOnce")
|
||||||
|
|
||||||
wasmhttp.ServeOnce(nil)
|
wasmhttp.ServeOnce(nil)
|
||||||
}
|
}
|
||||||
|
BIN
docs/api.wasm
BIN
docs/api.wasm
Binary file not shown.
@ -1,3 +1,11 @@
|
|||||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@048825e2c7dd6b9dcfe3462105f4364ffc70663c/sw.js')
|
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@048825e2c7dd6b9dcfe3462105f4364ffc70663c/sw.js')
|
||||||
|
|
||||||
|
addEventListener('install', (event) => {
|
||||||
|
event.waitUntil(skipWaiting())
|
||||||
|
})
|
||||||
|
|
||||||
|
addEventListener('activate', event => {
|
||||||
|
event.waitUntil(clients.claim())
|
||||||
|
})
|
||||||
|
|
||||||
registerWasmHTTPListener('api.wasm')
|
registerWasmHTTPListener('api.wasm')
|
||||||
|
50
sw.js
50
sw.js
@ -1,29 +1,5 @@
|
|||||||
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')
|
||||||
|
|
||||||
async function startWasm(wasm, { env, args = [] }) {
|
|
||||||
const go = new Go()
|
|
||||||
go.env = env
|
|
||||||
go.argv = [wasm, ...args]
|
|
||||||
const { instance } = await WebAssembly.instantiateStreaming(fetch(wasm), go.importObject)
|
|
||||||
return go.run(instance)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// addEventListener('install', (event) => {
|
|
||||||
// event.waitUntil(skipWaiting())
|
|
||||||
// })
|
|
||||||
|
|
||||||
let nextHandlerId = 1
|
let nextHandlerId = 1
|
||||||
const handlerResolvers = {}
|
const handlerResolvers = {}
|
||||||
const handlers = []
|
const handlers = []
|
||||||
@ -35,10 +11,6 @@ self.wasmhttp = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
addEventListener('activate', event => {
|
|
||||||
event.waitUntil(clients.claim())
|
|
||||||
})
|
|
||||||
|
|
||||||
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, '/')}`
|
||||||
@ -59,6 +31,28 @@ function registerWasmHTTPListener(wasm, base, args) {
|
|||||||
|
|
||||||
const handler = await handlerPromise
|
const handler = await handlerPromise
|
||||||
|
|
||||||
|
console.log({ handler })
|
||||||
|
|
||||||
e.respondWith(handler(e.request))
|
e.respondWith(handler(e.request))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function startWasm(wasm, { env, args = [] }) {
|
||||||
|
const go = new Go()
|
||||||
|
go.env = env
|
||||||
|
go.argv = [wasm, ...args]
|
||||||
|
const { instance } = await WebAssembly.instantiateStreaming(fetch(wasm), go.importObject)
|
||||||
|
return go.run(instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user