Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wasm plugin work with service workers #334

Merged
merged 3 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}