cf worker ban the top level of await without an install event. so I couldn’t find a way to set async init.
I want to compare IP in a list of ranges. To make it quicker I map the list to be a tree struct. like
const tree = buildTree()
addEventListener(...)
async function handleRequest() { checkIP(ip, tree)}
Assume the data is not such large, so skip KV(about 300K), but couldn’t bear to handle it in fetch event.
It works. cpu run about 1ms (btw, I meet 100ms startup limit here that i couldn’t find in docs. locally test 200ms failed and 90ms pass, so i guess it’s 100ms)
To optimize it I want to use wasm build by rust, I find the template rely on wasm_bindgen with issue.
(this issue origin is base on cfworker too, interesting)
load wasm will be a async function like
const { buildChecker } = wasm_bindgen;
await wasm_bindgen(wasm);// <= banned on the top level
const checker = buildChecker();// <= init with a tree about 20ms
addEventListener(...)
async function handleRequest() {checker.check(ip);}
Obviously, it was banned by Worker on the top-level. As normal browser/serviceworker it should run on install
event.
but I only find fetch event
for request and scheduled event
for crontrigger. It will be a bit wired to set a cron to trigger once and then delete it to mock a install
or says init
or startup
or ready
event.(I prefer to have a onready
event to solve it, include long init cases.)
Any solution about this case o_o?