@vitest/web-worker 1.0.0-beta.1 → 1.0.0-beta.3
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/dist/index.js +1 -1
- package/dist/pure.js +15 -22
- package/package.json +2 -2
package/dist/index.js
CHANGED
package/dist/pure.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { VitestExecutor } from 'vitest/execute';
|
2
2
|
import createDebug from 'debug';
|
3
|
-
import { MessageChannel } from 'worker_threads';
|
3
|
+
import { MessageChannel } from 'node:worker_threads';
|
4
4
|
|
5
5
|
class InlineWorkerRunner extends VitestExecutor {
|
6
6
|
constructor(options, context) {
|
@@ -287,7 +287,7 @@ function assertGlobalExists(name) {
|
|
287
287
|
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.`);
|
288
288
|
}
|
289
289
|
function createClonedMessageEvent(data, transferOrOptions, clone) {
|
290
|
-
const transfer = Array.isArray(transferOrOptions) ? transferOrOptions : transferOrOptions
|
290
|
+
const transfer = Array.isArray(transferOrOptions) ? transferOrOptions : transferOrOptions?.transfer;
|
291
291
|
debug("clone worker message %o", data);
|
292
292
|
const origin = typeof location === "undefined" ? void 0 : location.origin;
|
293
293
|
if (typeof structuredClone === "function" && clone === "native") {
|
@@ -299,7 +299,7 @@ function createClonedMessageEvent(data, transferOrOptions, clone) {
|
|
299
299
|
}
|
300
300
|
if (clone !== "none") {
|
301
301
|
debug("create message event, using polyfilled structured clone");
|
302
|
-
|
302
|
+
transfer?.length && console.warn(
|
303
303
|
'[@vitest/web-worker] `structuredClone` is not supported in this environment. Falling back to polyfill, your transferable options will be lost. Set `VITEST_WEB_WORKER_CLONE` environmental variable to "none", if you don\'t want to loose it,or update to Node 17+.'
|
304
304
|
);
|
305
305
|
return new MessageEvent("message", {
|
@@ -357,7 +357,7 @@ function getFileIdFromUrl(url) {
|
|
357
357
|
|
358
358
|
function createWorkerConstructor(options) {
|
359
359
|
const runnerOptions = getRunnerOptions();
|
360
|
-
const cloneType = () =>
|
360
|
+
const cloneType = () => options?.clone ?? process.env.VITEST_WEB_WORKER_CLONE ?? "native";
|
361
361
|
return class Worker extends EventTarget {
|
362
362
|
static __VITEST_WEB_WORKER__ = true;
|
363
363
|
_vw_workerTarget = new EventTarget();
|
@@ -372,7 +372,7 @@ function createWorkerConstructor(options) {
|
|
372
372
|
super();
|
373
373
|
const context = {
|
374
374
|
onmessage: null,
|
375
|
-
name: options2
|
375
|
+
name: options2?.name,
|
376
376
|
close: () => this.terminate(),
|
377
377
|
dispatchEvent: (event) => {
|
378
378
|
return this._vw_workerTarget.dispatchEvent(event);
|
@@ -398,22 +398,19 @@ function createWorkerConstructor(options) {
|
|
398
398
|
}
|
399
399
|
};
|
400
400
|
this._vw_workerTarget.addEventListener("message", (e) => {
|
401
|
-
|
402
|
-
(_a = context.onmessage) == null ? void 0 : _a.call(context, e);
|
401
|
+
context.onmessage?.(e);
|
403
402
|
});
|
404
403
|
this.addEventListener("message", (e) => {
|
405
|
-
|
406
|
-
(_a = this.onmessage) == null ? void 0 : _a.call(this, e);
|
404
|
+
this.onmessage?.(e);
|
407
405
|
});
|
408
406
|
this.addEventListener("messageerror", (e) => {
|
409
|
-
|
410
|
-
(_a = this.onmessageerror) == null ? void 0 : _a.call(this, e);
|
407
|
+
this.onmessageerror?.(e);
|
411
408
|
});
|
412
409
|
const runner = new InlineWorkerRunner(runnerOptions, context);
|
413
410
|
const id = getFileIdFromUrl(url);
|
414
411
|
this._vw_name = id;
|
415
412
|
runner.resolveUrl(id).then(([, fsPath]) => {
|
416
|
-
this._vw_name =
|
413
|
+
this._vw_name = options2?.name ?? fsPath;
|
417
414
|
debug("initialize worker %s", this._vw_name);
|
418
415
|
return runner.executeFile(fsPath).then(() => {
|
419
416
|
runnerOptions.moduleCache.invalidateSubDepTree([fsPath, runner.mocker.getMockPath(fsPath)]);
|
@@ -424,7 +421,6 @@ function createWorkerConstructor(options) {
|
|
424
421
|
debug("worker %s successfully initialized", this._vw_name);
|
425
422
|
});
|
426
423
|
}).catch((e) => {
|
427
|
-
var _a;
|
428
424
|
debug("worker %s failed to initialize: %o", this._vw_name, e);
|
429
425
|
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
430
426
|
const error = new EventConstructor("error", {
|
@@ -432,7 +428,7 @@ function createWorkerConstructor(options) {
|
|
432
428
|
message: e.message
|
433
429
|
});
|
434
430
|
this.dispatchEvent(error);
|
435
|
-
|
431
|
+
this.onerror?.(error);
|
436
432
|
console.error(e);
|
437
433
|
});
|
438
434
|
}
|
@@ -492,11 +488,10 @@ function convertNodePortToWebPort(port) {
|
|
492
488
|
const emit = port.emit.bind(port);
|
493
489
|
Object.defineProperty(port, "emit", {
|
494
490
|
value(event) {
|
495
|
-
var _a, _b;
|
496
491
|
if (event.name === "message")
|
497
|
-
|
492
|
+
port.onmessage?.(event);
|
498
493
|
if (event.name === "messageerror")
|
499
|
-
|
494
|
+
port.onmessageerror?.(event);
|
500
495
|
return emit(event);
|
501
496
|
},
|
502
497
|
configurable: true,
|
@@ -523,7 +518,7 @@ function createSharedWorkerConstructor() {
|
|
523
518
|
port;
|
524
519
|
constructor(url, options) {
|
525
520
|
super();
|
526
|
-
const name = typeof options === "string" ? options : options
|
521
|
+
const name = typeof options === "string" ? options : options?.name;
|
527
522
|
const context = {
|
528
523
|
onconnect: null,
|
529
524
|
name,
|
@@ -546,8 +541,7 @@ function createSharedWorkerConstructor() {
|
|
546
541
|
this.port = convertNodePortToWebPort(channel.port1);
|
547
542
|
this._vw_workerPort = convertNodePortToWebPort(channel.port2);
|
548
543
|
this._vw_workerTarget.addEventListener("connect", (e) => {
|
549
|
-
|
550
|
-
(_a = context.onconnect) == null ? void 0 : _a.call(context, e);
|
544
|
+
context.onconnect?.(e);
|
551
545
|
});
|
552
546
|
const runner = new InlineWorkerRunner(runnerOptions, context);
|
553
547
|
const id = getFileIdFromUrl(url);
|
@@ -565,7 +559,6 @@ function createSharedWorkerConstructor() {
|
|
565
559
|
debug("shared worker %s successfully initialized", this._vw_name);
|
566
560
|
});
|
567
561
|
}).catch((e) => {
|
568
|
-
var _a;
|
569
562
|
debug("shared worker %s failed to initialize: %o", this._vw_name, e);
|
570
563
|
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
571
564
|
const error = new EventConstructor("error", {
|
@@ -573,7 +566,7 @@ function createSharedWorkerConstructor() {
|
|
573
566
|
message: e.message
|
574
567
|
});
|
575
568
|
this.dispatchEvent(error);
|
576
|
-
|
569
|
+
this.onerror?.(error);
|
577
570
|
console.error(e);
|
578
571
|
});
|
579
572
|
}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/web-worker",
|
3
3
|
"type": "module",
|
4
|
-
"version": "1.0.0-beta.
|
4
|
+
"version": "1.0.0-beta.3",
|
5
5
|
"description": "Web Worker support for testing in Vitest",
|
6
6
|
"license": "MIT",
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
@@ -35,7 +35,7 @@
|
|
35
35
|
"*.d.ts"
|
36
36
|
],
|
37
37
|
"peerDependencies": {
|
38
|
-
"vitest": "
|
38
|
+
"vitest": "^1.0.0-0"
|
39
39
|
},
|
40
40
|
"dependencies": {
|
41
41
|
"debug": "^4.3.4"
|