@vitest/web-worker 4.0.0-beta.4 → 4.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/index.js +1 -2
- package/dist/pure.js +48 -79
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2021-Present Vitest
|
|
3
|
+
Copyright (c) 2021-Present VoidZero Inc. and Vitest contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/dist/index.js
CHANGED
package/dist/pure.js
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
import { MessageChannel } from 'node:worker_threads';
|
|
2
|
-
import {
|
|
3
|
-
import { readFileSync as readFileSync$1 } from 'node:fs';
|
|
2
|
+
import { getWorkerState, VitestModuleEvaluator, startVitestModuleRunner, VITEST_VM_CONTEXT_SYMBOL } from 'vitest/internal/module-runner';
|
|
4
3
|
import createDebug from 'debug';
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
function startWebWorkerModuleRunner(context) {
|
|
6
|
+
const state = getWorkerState();
|
|
7
|
+
const mocker = globalThis.__vitest_mocker__;
|
|
8
|
+
const compiledFunctionArgumentsNames = Object.keys(context);
|
|
9
|
+
const compiledFunctionArgumentsValues = Object.values(context);
|
|
10
|
+
compiledFunctionArgumentsNames.push("importScripts");
|
|
11
|
+
compiledFunctionArgumentsValues.push(importScripts);
|
|
12
|
+
const vm = globalThis[VITEST_VM_CONTEXT_SYMBOL];
|
|
13
|
+
const evaluator = new VitestModuleEvaluator(vm, {
|
|
14
|
+
interopDefault: state.config.deps.interopDefault,
|
|
15
|
+
moduleExecutionInfo: state.moduleExecutionInfo,
|
|
16
|
+
getCurrentTestFilepath: () => state.filepath,
|
|
17
|
+
compiledFunctionArgumentsNames,
|
|
18
|
+
compiledFunctionArgumentsValues
|
|
19
|
+
});
|
|
20
|
+
return startVitestModuleRunner({
|
|
21
|
+
evaluator,
|
|
22
|
+
evaluatedModules: state.evaluatedModules,
|
|
23
|
+
mocker,
|
|
24
|
+
state
|
|
25
|
+
});
|
|
19
26
|
}
|
|
20
27
|
function importScripts() {
|
|
21
28
|
throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
|
|
@@ -297,13 +304,7 @@ var ponyfillStructuredClone = typeof structuredClone === "function" ?
|
|
|
297
304
|
) :
|
|
298
305
|
(any, options) => deserialize(serialize(any, options));
|
|
299
306
|
|
|
300
|
-
// keep the reference in case it was mocked
|
|
301
|
-
const readFileSync = readFileSync$1;
|
|
302
307
|
const debug = createDebug("vitest:web-worker");
|
|
303
|
-
function getWorkerState() {
|
|
304
|
-
// @ts-expect-error untyped global
|
|
305
|
-
return globalThis.__vitest_worker__;
|
|
306
|
-
}
|
|
307
308
|
function assertGlobalExists(name) {
|
|
308
309
|
if (!(name in globalThis)) {
|
|
309
310
|
throw new Error(`[@vitest/web-worker] Cannot initiate a custom Web Worker. "${name}" is not supported in this environment. Please, consider using jsdom or happy-dom environment.`);
|
|
@@ -344,30 +345,6 @@ function createMessageEvent(data, transferOrOptions, clone) {
|
|
|
344
345
|
return new MessageEvent("messageerror", { data: error });
|
|
345
346
|
}
|
|
346
347
|
}
|
|
347
|
-
function getRunnerOptions() {
|
|
348
|
-
const state = getWorkerState();
|
|
349
|
-
const { config, rpc, moduleCache, moduleExecutionInfo } = state;
|
|
350
|
-
return {
|
|
351
|
-
async fetchModule(id) {
|
|
352
|
-
const result = await rpc.fetch(id, "web");
|
|
353
|
-
if (result.id && !result.externalize) {
|
|
354
|
-
const code = readFileSync(result.id, "utf-8");
|
|
355
|
-
return { code };
|
|
356
|
-
}
|
|
357
|
-
return result;
|
|
358
|
-
},
|
|
359
|
-
resolveId(id, importer) {
|
|
360
|
-
return rpc.resolveId(id, importer, "web");
|
|
361
|
-
},
|
|
362
|
-
moduleCache,
|
|
363
|
-
moduleExecutionInfo,
|
|
364
|
-
interopDefault: config.deps.interopDefault ?? true,
|
|
365
|
-
moduleDirectories: config.deps.moduleDirectories,
|
|
366
|
-
root: config.root,
|
|
367
|
-
base: config.base,
|
|
368
|
-
state
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
348
|
function stripProtocol(url) {
|
|
372
349
|
return url.toString().replace(/^file:\/+/, "/");
|
|
373
350
|
}
|
|
@@ -429,7 +406,6 @@ function convertNodePortToWebPort(port) {
|
|
|
429
406
|
return port;
|
|
430
407
|
}
|
|
431
408
|
function createSharedWorkerConstructor() {
|
|
432
|
-
const runnerOptions = getRunnerOptions();
|
|
433
409
|
return class SharedWorker extends EventTarget {
|
|
434
410
|
static __VITEST_WEB_WORKER__ = true;
|
|
435
411
|
_vw_workerTarget = new EventTarget();
|
|
@@ -452,9 +428,6 @@ function createSharedWorkerConstructor() {
|
|
|
452
428
|
onrtctransform: null,
|
|
453
429
|
onunhandledrejection: null,
|
|
454
430
|
origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
|
|
455
|
-
importScripts: () => {
|
|
456
|
-
throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
|
|
457
|
-
},
|
|
458
431
|
crossOriginIsolated: false,
|
|
459
432
|
onconnect: null,
|
|
460
433
|
name: name || "",
|
|
@@ -484,17 +457,17 @@ function createSharedWorkerConstructor() {
|
|
|
484
457
|
this._vw_workerTarget.addEventListener("connect", (e) => {
|
|
485
458
|
context.onconnect?.(e);
|
|
486
459
|
});
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
460
|
+
const fileId = getFileIdFromUrl(url);
|
|
461
|
+
this._vw_name = fileId;
|
|
462
|
+
startWebWorkerModuleRunner(context).then((runner) => {
|
|
463
|
+
return runner.mocker.resolveId(fileId).then(({ url, id: resolvedId }) => {
|
|
464
|
+
this._vw_name = name ?? url;
|
|
465
|
+
debug("initialize shared worker %s", this._vw_name);
|
|
466
|
+
return runner.import(url).then(() => {
|
|
467
|
+
runner._invalidateSubTreeById([resolvedId, runner.mocker.getMockPath(resolvedId)]);
|
|
468
|
+
this._vw_workerTarget.dispatchEvent(new MessageEvent("connect", { ports: [this._vw_workerPort] }));
|
|
469
|
+
debug("shared worker %s successfully initialized", this._vw_name);
|
|
470
|
+
});
|
|
498
471
|
});
|
|
499
472
|
}).catch((e) => {
|
|
500
473
|
debug("shared worker %s failed to initialize: %o", this._vw_name, e);
|
|
@@ -512,7 +485,6 @@ function createSharedWorkerConstructor() {
|
|
|
512
485
|
}
|
|
513
486
|
|
|
514
487
|
function createWorkerConstructor(options) {
|
|
515
|
-
const runnerOptions = getRunnerOptions();
|
|
516
488
|
const cloneType = () => options?.clone ?? process.env.VITEST_WEB_WORKER_CLONE ?? "native";
|
|
517
489
|
return class Worker extends EventTarget {
|
|
518
490
|
static __VITEST_WEB_WORKER__ = true;
|
|
@@ -539,9 +511,6 @@ function createWorkerConstructor(options) {
|
|
|
539
511
|
onrtctransform: null,
|
|
540
512
|
onunhandledrejection: null,
|
|
541
513
|
origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
|
|
542
|
-
importScripts: () => {
|
|
543
|
-
throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
|
|
544
|
-
},
|
|
545
514
|
crossOriginIsolated: false,
|
|
546
515
|
name: options?.name || "",
|
|
547
516
|
close: () => this.terminate(),
|
|
@@ -584,21 +553,21 @@ function createWorkerConstructor(options) {
|
|
|
584
553
|
this.addEventListener("messageerror", (e) => {
|
|
585
554
|
this.onmessageerror?.(e);
|
|
586
555
|
});
|
|
587
|
-
const
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
556
|
+
const fileId = getFileIdFromUrl(url);
|
|
557
|
+
this._vw_name = fileId;
|
|
558
|
+
startWebWorkerModuleRunner(context).then((runner) => {
|
|
559
|
+
return runner.mocker.resolveId(fileId).then(({ url, id: resolvedId }) => {
|
|
560
|
+
this._vw_name = options?.name ?? url;
|
|
561
|
+
debug("initialize worker %s", this._vw_name);
|
|
562
|
+
return runner.import(url).then(() => {
|
|
563
|
+
runner._invalidateSubTreeById([resolvedId, runner.mocker.getMockPath(resolvedId)]);
|
|
564
|
+
const q = this._vw_messageQueue;
|
|
565
|
+
this._vw_messageQueue = null;
|
|
566
|
+
if (q) {
|
|
567
|
+
q.forEach(([data, transfer]) => this.postMessage(data, transfer), this);
|
|
568
|
+
}
|
|
569
|
+
debug("worker %s successfully initialized", this._vw_name);
|
|
570
|
+
});
|
|
602
571
|
});
|
|
603
572
|
}).catch((e) => {
|
|
604
573
|
debug("worker %s failed to initialize: %o", this._vw_name, e);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitest/web-worker",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.0.0-beta.
|
|
4
|
+
"version": "4.0.0-beta.6",
|
|
5
5
|
"description": "Web Worker support for testing in Vitest",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"vitest": "4.0.0-beta.
|
|
36
|
+
"vitest": "4.0.0-beta.6"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"debug": "^4.4.1"
|