-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
35 lines (33 loc) · 1001 Bytes
/
worker.js
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
require('./polyfill.js');
require('./wasm_exec.js');
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request));
});
function handleRequest(req) {
return new Promise((async (resolve, reject) => {
try {
const url = new URL(req.url);
const go = new Go();
const instance = await WebAssembly.instantiate(WASM, go.importObject);
go.run(instance);
switch (url.pathname) {
case '/sign':
sign(url.searchParams.get('message'), (answer) => {
resolve(new Response(answer, { status: 200 }));
});
break;
case '/verify':
verify(url.searchParams.get('token'), (answer) => {
resolve(new Response(answer, { status: 200 }));
});
break;
default:
resolve(new Response('', { status: 404 }));
break;
}
} catch (e) {
console.log(e);
reject(new Response(JSON.stringify(e), { status: 500 }));
}
}));
}