Skip to content

Commit

Permalink
fix(wasm): work with service workers (#334)
Browse files Browse the repository at this point in the history
BREAKING CHANGES:

This change leverages `globalThis` and will fail in older environments where `globalThis` is not implemented. See https://caniuse.com/#search=WebAssembly and https://caniuse.com/#search=globalThis. If your environment doesn't support `globalThis`, please use an earlier version of this plugin.

* Make wasm plugin work with service workers

The code generated by @rollup/plugin-wasm breaks when executed in a
service worker because window is undefined.

* Add test for worker scenario and simplify the fix

Instead of checking whether window is defined, we can just use
globalThis.

* Only run worker test if worker_threads supported
  • Loading branch information
vedantroy authored Apr 26, 2020
1 parent 440dbc4 commit 59f984d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/wasm/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function wasm(options = {}) {
if (isNode) {
buf = Buffer.from(src, 'base64')
} else {
var raw = window.atob(src)
var raw = globalThis.atob(src)
var rawLength = raw.length
buf = new Uint8Array(new ArrayBuffer(rawLength))
for(var i = 0; i < rawLength; i++) {
Expand Down
11 changes: 11 additions & 0 deletions packages/wasm/test/fixtures/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sample from './sample.wasm';
// atob doesn't exist in node, so we fake it
// in the browser, globalThis.atob will exist
// eslint-disable-next-line no-undef
globalThis.atob = (str) => Buffer.from(str, 'base64').toString('binary');
// trick the wasm loader into thinking we are in the browser
const realProcess = process;
// eslint-disable-next-line no-global-assign, no-undefined
process = undefined;
sample({});
realProcess.exit(0);
34 changes: 32 additions & 2 deletions packages/wasm/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import wasm from '../dist/index';

const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;

const testBundle = async (t, bundle) => {
const generateCode = async (bundle) => {
const { output } = await bundle.generate({ format: 'cjs' });
const [{ code }] = output;
const func = new AsyncFunction('t', `let result;\n\n${code}\n\nreturn result;`);
return code;
};

const testBundle = async (t, bundle) => {
const code = await generateCode(bundle);
const func = new AsyncFunction('t', `let result;\n\n${code}\n\nreturn result;`);
return func(t);
};

Expand Down Expand Up @@ -61,3 +65,29 @@ test('imports', async (t) => {
});
await testBundle(t, bundle);
});

try {
const { Worker } = require('worker_threads');
test('worker', async (t) => {
t.plan(2);

const bundle = await rollup({
input: 'test/fixtures/worker.js',
plugins: [wasm()]
});
const code = await generateCode(bundle);
const executeWorker = () => {
const worker = new Worker(code, { eval: true });
return new Promise((resolve, reject) => {
worker.on('error', (err) => reject(err));
worker.on('exit', (exitCode) => resolve(exitCode));
});
};
await t.notThrowsAsync(async () => {
const result = await executeWorker();
t.true(result === 0);
});
});
} catch (err) {
// worker threads aren't fully supported in Node versions before 11.7.0
}

0 comments on commit 59f984d

Please sign in to comment.