-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
36 lines (35 loc) · 1.2 KB
/
worker.js
File metadata and controls
36 lines (35 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Adapter from './Adapter.js';
import { CfKvMock } from './Mock.js';
import WasmModule from './gen/WasmDemo.js';
import WasmBinary from './gen/WasmDemo.wasm';
export default {
/**
* @param {Request} request
* @param {*} env
* @param {*} ctx
* @returns {Response}
*/
async fetch(request, env, ctx) {
// Bootstrap the WASM instance:
const adapterOptions = {
async instantiateWasm (imports, onSuccess) {
const instance = await WebAssembly.instantiate(WasmBinary, imports);
onSuccess(instance);
return instance.exports;
},
// You can replace this with a real Cloudflare KV binding from `env`:
cfkv: new CfKvMock(console.log),
};
// Await WASM initialisation and request body:
const [adapter, body] = await Promise.all([
Adapter.make(WasmModule, adapterOptions),
request.text(),
]);
// Handle request:
const res = await adapter.handleRequest(request.url, body);
return new Response(new Uint8Array(res.content), {
status: res.statusCode,
headers: res.headers,
});
},
};