8000 fix data urls by developit · Pull Request #53 · developit/web-worker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix data urls #53

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

Merged
merged 7 commits into from
Jan 31, 2025
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 src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function workerThread() {

const isDataUrl = /^data:/.test(mod);
if (type === 'module') {
import(URL.pathToFileURL(mod))
import(isDataUrl ? mod : URL.pathToFileURL(mod))
.catch(err => {
if (isDataUrl && err.message === 'Not supported') {
console.warn('Worker(): Importing data: URLs requires Node 12.10+. Falling back to classic worker.');
Expand Down
24 changes: 20 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import Worker from '../dist/node/index.cjs';

let worker;

function createModuleWorker(url) {
const worker = new Worker(url, { type: 'module' });
function createModuleWorker(url, opts) {
const worker = new Worker(url, opts || { type: 'module' });
worker.events = [];
worker.addEventListener('message', e => {
worker.events.push(e);
Expand All @@ -36,7 +36,7 @@ test.after.always(t => {

test.serial('instantiation', async t => {
worker = createModuleWorker('./test/fixtures/worker.mjs');
await sleep(500);
await sleep(50);
t.is(worker.events.length, 1, 'should have received a message event');
t.is(worker.events[0].data, 42);
});
Expand All @@ -49,7 +49,7 @@ test.serial('postMessage', async t => {
worker.postMessage(msg);
const timestamp = Date.now();

await sleep(500);
await sleep(50);

t.is(worker.events.length, 2, 'should have received two message responses');

Expand All @@ -75,3 +75,19 @@ test.serial('close', async t => {
});
t.is(closed, true, 'should have closed itself');
});

test.serial('data URL - module', async t => {
t.teardown(() => worker && worker.terminate());
const worker = createModuleWorker('data:application/javascript,postMessage(42)');
await sleep(50);
t.is(worker.events.length, 1, 'should have received a message event');
t.is(worker.events[0].data, 42);
});

test.serial('data URL - classic', async t => {
t.teardown(() => worker && worker.terminate());
const worker = createModuleWorker('data:application/javascript,postMessage(42)', {});
await sleep(50);
t.is(worker.events.length, 1, 'should have received a message event');
t.is(worker.events[0].data, 42);
});
0