Skip to content

Commit 6761920

Browse files
committed
fix implementation and add some tests for importScripts
1 parent 937775c commit 6761920

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node/index.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import URL, { fileURLToPath } from 'url';
17+
import { URL, fileURLToPath, pathToFileURL } from 'url';
18+
import path from 'path';
1819
import fs from 'fs';
1920
import VM from 'vm';
2021
import threads from 'worker_threads';
@@ -73,7 +74,7 @@ function Event(type, target) {
7374
// thread boundary, but behaves differently in each context.
7475
export default threads.isMainThread ? mainThread() : workerThread();
7576

76-
const baseUrl = URL.pathToFileURL(process.cwd() + '/');
77+
const baseUrl = pathToFileURL(process.cwd() + '/');
7778

7879
function mainThread() {
7980

@@ -99,7 +100,7 @@ function mainThread() {
99100
mod = url;
100101
}
101102
else {
102-
mod = URL.fileURLToPath(new URL.URL(url, baseUrl));
103+
mod = fileURLToPath(new URL(url, baseUrl));
103104
}
104105
const worker = new threads.Worker(
105106
fileURLToPath(import.meta.url),
@@ -165,6 +166,22 @@ function workerThread() {
165166
close() {
166167
process.exit();
167168
}
169+
importScripts() {
170+
for (let i = 0; i < arguments.length; i++) {
171+
const url = arguments[i];
172+
let code;
173+
if (/^data:/.test(url)) {
174+
code = parseDataUrl(url).data;
175+
}
176+
else {
177+
code = fs.readFileSync(
178+
new URL(path.posix.normalize(url), pathToFileURL(mod)),
179+
'utf-8'
180+
);
181+
}
182+
VM.runInThisContext(code, { filename: url });
183+
}
184+
}
168185
}
169186
let proto = Object.getPrototypeOf(global);
170187
delete proto.constructor;
@@ -177,7 +194,7 @@ function workerThread() {
177194

178195
const isDataUrl = /^data:/.test(mod);
179196
if (type === 'module') {
180-
import(isDataUrl ? mod : URL.pathToFileURL(mod))
197+
import(isDataUrl ? mod : pathToFileURL(mod))
181198
.catch(err => {
182199
if (isDataUrl && err.message === 'Not supported') {
183200
console.warn('Worker(): Importing data: URLs requires Node 12.10+. Falling back to classic worker.');
@@ -193,7 +210,7 @@ function workerThread() {
193210
evaluateDataUrl(mod, name);
194211
}
195212
else {
196-
importScripts(mod);
213+
global.importScripts(mod);
197214
}
198215
}
199216
catch (err) {
@@ -222,17 +239,3 @@ function parseDataUrl(url) {
222239
}
223240
return { type, data };
224241
}
225-
226-
function importScripts() {
227-
for (let i = 0; i < arguments.length; i++) {
228-
const url = arguments[i];
229-
let code;
230-
if (/^data:/.test(url)) {
231-
code = parseDataUrl(url).data;
232-
}
233-
else {
234-
code = fs.readFileSync(url, 'utf-8');
235-
}
236-
VM.runInThisContext(code, { filename: url });
237-
}
238-
}

test/fixtures/importscripts-2.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
postMessage('from importscripts-2.cjs');

test/fixtures/importscripts.cjs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
importScripts('./importscripts-2.cjs');
2+
importScripts('data:application/javascript,postMessage("from data url")');

test/index.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,12 @@ test.serial('data URL - classic', async t => {
9191
t.is(worker.events.length, 1, 'should have received a message event');
9292
t.is(worker.events[0].data, 42);
9393
});
94+
95+
test.serial('importScripts', async t => {
96+
t.teardown(() => worker && worker.terminate());
97+
const worker = createModuleWorker('./test/fixtures/importscripts.cjs', {});
98+
await sleep(500);
99+
t.is(worker.events.length, 2, 'should have received two message events');
100+
t.is(worker.events[0].data, 'from importscripts-2.cjs');
101+
t.is(worker.events[1].data, 'from data url');
102+
});

0 commit comments

Comments
 (0)