Skip to content

Commit c2c5f4c

Browse files
panvaaduh95
authored andcommitted
worker: preserve blob module URLs
Evaluate blob module sources under their original URL instead of rewrapping them in a data URL. This preserves import.meta.url and module identity while retaining the captured source after URL revocation. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66354 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 05d533e commit c2c5f4c

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

‎lib/internal/webworker.js‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ const {
2020
SymbolFor,
2121
SymbolIterator,
2222
SymbolToStringTag,
23-
TypedArrayPrototypeGetLength,
24-
Uint8Array,
2523
globalThis,
2624
} = primordials;
2725

@@ -77,10 +75,6 @@ const {
7775
vm_dynamic_import_default_internal,
7876
} = internalBinding('symbols');
7977

80-
const {
81-
base64Slice,
82-
} = internalBinding('buffer');
83-
8478
const {
8579
hasOpenSSL,
8680
} = internalBinding('config');
@@ -757,10 +751,9 @@ const convertWorkerOptions = createDictionaryConverter(
757751

758752
/**
759753
* @param {URL} workerURL The parsed URL of the worker script.
760-
* @param {'classic'|'module'} type The worker's type.
761754
* @returns {{ value?: URL, source?: string }|null}
762755
*/
763-
function resolveWorkerEntry(workerURL, type) {
756+
function resolveWorkerEntry(workerURL) {
764757
switch (workerURL.protocol) {
765758
case 'file:':
766759
return { value: workerURL };
@@ -779,17 +772,6 @@ function resolveWorkerEntry(workerURL, type) {
779772
if (data === undefined) {
780773
return null;
781774
}
782-
if (type === 'module') {
783-
// The blob's contents are re-wrapped as a data: URL so that the
784-
// module loader evaluates them as a module script.
785-
// TODO(@avivkeller): What if we update the ESM loader to accept blob:
786-
// urls?
787-
const bytes = new Uint8Array(data);
788-
return {
789-
value: new URL('data:text/javascript;base64,' + base64Slice(
790-
bytes, 0, TypedArrayPrototypeGetLength(bytes))),
791-
};
792-
}
793775
return { source: utf8Decode(data) };
794776
}
795777
default:
@@ -824,7 +806,7 @@ class Worker extends EventTarget {
824806
}
825807

826808
this[kWorker] = null;
827-
const entry = resolveWorkerEntry(workerURL, options.type);
809+
const entry = resolveWorkerEntry(workerURL);
828810
if (entry === null) {
829811
// "If the algorithm asynchronously completes with null or with a
830812
// script whose error to rethrow is non-null, then: Queue a global
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --experimental-web-worker
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasCrypto) common.skip('missing crypto');
6+
7+
const assert = require('node:assert');
8+
const source = `
9+
import value from 'data:text/javascript,export default 42';
10+
postMessage([import.meta.url, location.href, value]);
11+
`;
12+
const url = URL.createObjectURL(new Blob([source], { type: 'text/javascript' }));
13+
const worker = new Worker(url, { type: 'module' });
14+
URL.revokeObjectURL(url);
15+
worker.onerror = common.mustNotCall('worker failed');
16+
worker.onmessage = common.mustCall(({ data }) => {
17+
worker.terminate();
18+
assert.deepStrictEqual(data, [url, url, 42]);
19+
});

0 commit comments

Comments
 (0)