5e0106f1fe
WHAT: web worker for compiling and instantiation of web assembly module WHY: Currently webassembly requires unsafe-eval, however we don't want to add it to main site due to CSP. The workaround for this is to instantiate wasm code inside a web worker. Change-Id: I0c3c9cafa3a0c344761cf6dd86bf96248f1103ca
32 lines
1.0 KiB
JavaScript
32 lines
1.0 KiB
JavaScript
// Copyright (C) 2020 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
importScripts('/static/static/wasm/wasm_exec.js')
|
|
|
|
if (!WebAssembly.instantiate) {
|
|
self.postMessage(new Error('web assembly is not supported'));
|
|
}
|
|
|
|
const go = new Go();
|
|
const instantiateStreaming = WebAssembly.instantiateStreaming || async function (resp, importObject) {
|
|
const response = await resp;
|
|
const source = await response.arrayBuffer();
|
|
|
|
return await WebAssembly.instantiate(source, importObject);
|
|
};
|
|
const response = fetch('/static/static/wasm/access.wasm');
|
|
instantiateStreaming(response, go.importObject).then(result => go.run(result.instance)).catch(err => self.postMessage(new Error(err.message)));
|
|
|
|
self.onmessage = function (event) {
|
|
const type = event.data.type;
|
|
switch (type) {
|
|
case 'GenerateAccess':
|
|
const result = self.generateAccessGrant();
|
|
|
|
self.postMessage(result);
|
|
break;
|
|
default:
|
|
self.postMessage(new Error('provided message event type is not supported'));
|
|
}
|
|
};
|