@vitest/web-worker 2.1.3 → 2.1.4
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 +136 -136
- package/package.json +3 -3
package/dist/index.js
CHANGED
package/dist/pure.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
+
import { MessageChannel } from 'node:worker_threads';
|
1
2
|
import { VitestExecutor } from 'vitest/execute';
|
2
3
|
import { readFileSync as readFileSync$1 } from 'node:fs';
|
3
4
|
import createDebug from 'debug';
|
4
|
-
import { MessageChannel } from 'node:worker_threads';
|
5
5
|
|
6
6
|
class InlineWorkerRunner extends VitestExecutor {
|
7
7
|
constructor(options, context) {
|
@@ -373,6 +373,141 @@ function getFileIdFromUrl(url) {
|
|
373
373
|
return stripProtocol(url);
|
374
374
|
}
|
375
375
|
|
376
|
+
function convertNodePortToWebPort(port) {
|
377
|
+
if (!("addEventListener" in port)) {
|
378
|
+
Object.defineProperty(port, "addEventListener", {
|
379
|
+
value(...args) {
|
380
|
+
return this.addListener(...args);
|
381
|
+
},
|
382
|
+
configurable: true,
|
383
|
+
enumerable: true
|
384
|
+
});
|
385
|
+
}
|
386
|
+
if (!("removeEventListener" in port)) {
|
387
|
+
Object.defineProperty(port, "removeEventListener", {
|
388
|
+
value(...args) {
|
389
|
+
return this.removeListener(...args);
|
390
|
+
},
|
391
|
+
configurable: true,
|
392
|
+
enumerable: true
|
393
|
+
});
|
394
|
+
}
|
395
|
+
if (!("dispatchEvent" in port)) {
|
396
|
+
const emit = port.emit.bind(port);
|
397
|
+
Object.defineProperty(port, "emit", {
|
398
|
+
value(event) {
|
399
|
+
if (event.name === "message") {
|
400
|
+
port.onmessage?.(event);
|
401
|
+
}
|
402
|
+
if (event.name === "messageerror") {
|
403
|
+
port.onmessageerror?.(event);
|
404
|
+
}
|
405
|
+
return emit(event);
|
406
|
+
},
|
407
|
+
configurable: true,
|
408
|
+
enumerable: true
|
409
|
+
});
|
410
|
+
Object.defineProperty(port, "dispatchEvent", {
|
411
|
+
value(event) {
|
412
|
+
return this.emit(event);
|
413
|
+
},
|
414
|
+
configurable: true,
|
415
|
+
enumerable: true
|
416
|
+
});
|
417
|
+
}
|
418
|
+
return port;
|
419
|
+
}
|
420
|
+
function createSharedWorkerConstructor() {
|
421
|
+
const runnerOptions = getRunnerOptions();
|
422
|
+
return class SharedWorker extends EventTarget {
|
423
|
+
static __VITEST_WEB_WORKER__ = true;
|
424
|
+
_vw_workerTarget = new EventTarget();
|
425
|
+
_vw_name;
|
426
|
+
_vw_workerPort;
|
427
|
+
onerror = null;
|
428
|
+
port;
|
429
|
+
constructor(url, options) {
|
430
|
+
super();
|
431
|
+
const name = typeof options === "string" ? options : options?.name;
|
432
|
+
let selfProxy;
|
433
|
+
const context = {
|
434
|
+
onmessage: null,
|
435
|
+
onmessageerror: null,
|
436
|
+
onerror: null,
|
437
|
+
onlanguagechange: null,
|
438
|
+
onoffline: null,
|
439
|
+
ononline: null,
|
440
|
+
onrejectionhandled: null,
|
441
|
+
onrtctransform: null,
|
442
|
+
onunhandledrejection: null,
|
443
|
+
origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
|
444
|
+
importScripts: () => {
|
445
|
+
throw new Error(
|
446
|
+
"[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead."
|
447
|
+
);
|
448
|
+
},
|
449
|
+
crossOriginIsolated: false,
|
450
|
+
onconnect: null,
|
451
|
+
name: name || "",
|
452
|
+
close: () => this.port.close(),
|
453
|
+
dispatchEvent: (event) => {
|
454
|
+
return this._vw_workerTarget.dispatchEvent(event);
|
455
|
+
},
|
456
|
+
addEventListener: (...args) => {
|
457
|
+
return this._vw_workerTarget.addEventListener(...args);
|
458
|
+
},
|
459
|
+
removeEventListener: this._vw_workerTarget.removeEventListener,
|
460
|
+
get self() {
|
461
|
+
return selfProxy;
|
462
|
+
}
|
463
|
+
};
|
464
|
+
selfProxy = new Proxy(context, {
|
465
|
+
get(target, prop, receiver) {
|
466
|
+
if (Reflect.has(target, prop)) {
|
467
|
+
return Reflect.get(target, prop, receiver);
|
468
|
+
}
|
469
|
+
return Reflect.get(globalThis, prop, receiver);
|
470
|
+
}
|
471
|
+
});
|
472
|
+
const channel = new MessageChannel();
|
473
|
+
this.port = convertNodePortToWebPort(channel.port1);
|
474
|
+
this._vw_workerPort = convertNodePortToWebPort(channel.port2);
|
475
|
+
this._vw_workerTarget.addEventListener("connect", (e) => {
|
476
|
+
context.onconnect?.(e);
|
477
|
+
});
|
478
|
+
const runner = new InlineWorkerRunner(runnerOptions, context);
|
479
|
+
const id = getFileIdFromUrl(url);
|
480
|
+
this._vw_name = id;
|
481
|
+
runner.resolveUrl(id).then(([, fsPath]) => {
|
482
|
+
this._vw_name = name ?? fsPath;
|
483
|
+
debug("initialize shared worker %s", this._vw_name);
|
484
|
+
return runner.executeFile(fsPath).then(() => {
|
485
|
+
runnerOptions.moduleCache.invalidateSubDepTree([
|
486
|
+
fsPath,
|
487
|
+
runner.mocker.getMockPath(fsPath)
|
488
|
+
]);
|
489
|
+
this._vw_workerTarget.dispatchEvent(
|
490
|
+
new MessageEvent("connect", {
|
491
|
+
ports: [this._vw_workerPort]
|
492
|
+
})
|
493
|
+
);
|
494
|
+
debug("shared worker %s successfully initialized", this._vw_name);
|
495
|
+
});
|
496
|
+
}).catch((e) => {
|
497
|
+
debug("shared worker %s failed to initialize: %o", this._vw_name, e);
|
498
|
+
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
499
|
+
const error = new EventConstructor("error", {
|
500
|
+
error: e,
|
501
|
+
message: e.message
|
502
|
+
});
|
503
|
+
this.dispatchEvent(error);
|
504
|
+
this.onerror?.(error);
|
505
|
+
console.error(e);
|
506
|
+
});
|
507
|
+
}
|
508
|
+
};
|
509
|
+
}
|
510
|
+
|
376
511
|
function createWorkerConstructor(options) {
|
377
512
|
const runnerOptions = getRunnerOptions();
|
378
513
|
const cloneType = () => options?.clone ?? process.env.VITEST_WEB_WORKER_CLONE ?? "native";
|
@@ -530,141 +665,6 @@ function createWorkerConstructor(options) {
|
|
530
665
|
};
|
531
666
|
}
|
532
667
|
|
533
|
-
function convertNodePortToWebPort(port) {
|
534
|
-
if (!("addEventListener" in port)) {
|
535
|
-
Object.defineProperty(port, "addEventListener", {
|
536
|
-
value(...args) {
|
537
|
-
return this.addListener(...args);
|
538
|
-
},
|
539
|
-
configurable: true,
|
540
|
-
enumerable: true
|
541
|
-
});
|
542
|
-
}
|
543
|
-
if (!("removeEventListener" in port)) {
|
544
|
-
Object.defineProperty(port, "removeEventListener", {
|
545
|
-
value(...args) {
|
546
|
-
return this.removeListener(...args);
|
547
|
-
},
|
548
|
-
configurable: true,
|
549
|
-
enumerable: true
|
550
|
-
});
|
551
|
-
}
|
552
|
-
if (!("dispatchEvent" in port)) {
|
553
|
-
const emit = port.emit.bind(port);
|
554
|
-
Object.defineProperty(port, "emit", {
|
555
|
-
value(event) {
|
556
|
-
if (event.name === "message") {
|
557
|
-
port.onmessage?.(event);
|
558
|
-
}
|
559
|
-
if (event.name === "messageerror") {
|
560
|
-
port.onmessageerror?.(event);
|
561
|
-
}
|
562
|
-
return emit(event);
|
563
|
-
},
|
564
|
-
configurable: true,
|
565
|
-
enumerable: true
|
566
|
-
});
|
567
|
-
Object.defineProperty(port, "dispatchEvent", {
|
568
|
-
value(event) {
|
569
|
-
return this.emit(event);
|
570
|
-
},
|
571
|
-
configurable: true,
|
572
|
-
enumerable: true
|
573
|
-
});
|
574
|
-
}
|
575
|
-
return port;
|
576
|
-
}
|
577
|
-
function createSharedWorkerConstructor() {
|
578
|
-
const runnerOptions = getRunnerOptions();
|
579
|
-
return class SharedWorker extends EventTarget {
|
580
|
-
static __VITEST_WEB_WORKER__ = true;
|
581
|
-
_vw_workerTarget = new EventTarget();
|
582
|
-
_vw_name;
|
583
|
-
_vw_workerPort;
|
584
|
-
onerror = null;
|
585
|
-
port;
|
586
|
-
constructor(url, options) {
|
587
|
-
super();
|
588
|
-
const name = typeof options === "string" ? options : options?.name;
|
589
|
-
let selfProxy;
|
590
|
-
const context = {
|
591
|
-
onmessage: null,
|
592
|
-
onmessageerror: null,
|
593
|
-
onerror: null,
|
594
|
-
onlanguagechange: null,
|
595
|
-
onoffline: null,
|
596
|
-
ononline: null,
|
597
|
-
onrejectionhandled: null,
|
598
|
-
onrtctransform: null,
|
599
|
-
onunhandledrejection: null,
|
600
|
-
origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
|
601
|
-
importScripts: () => {
|
602
|
-
throw new Error(
|
603
|
-
"[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead."
|
604
|
-
);
|
605
|
-
},
|
606
|
-
crossOriginIsolated: false,
|
607
|
-
onconnect: null,
|
608
|
-
name: name || "",
|
609
|
-
close: () => this.port.close(),
|
610
|
-
dispatchEvent: (event) => {
|
611
|
-
return this._vw_workerTarget.dispatchEvent(event);
|
612
|
-
},
|
613
|
-
addEventListener: (...args) => {
|
614
|
-
return this._vw_workerTarget.addEventListener(...args);
|
615
|
-
},
|
616
|
-
removeEventListener: this._vw_workerTarget.removeEventListener,
|
617
|
-
get self() {
|
618
|
-
return selfProxy;
|
619
|
-
}
|
620
|
-
};
|
621
|
-
selfProxy = new Proxy(context, {
|
622
|
-
get(target, prop, receiver) {
|
623
|
-
if (Reflect.has(target, prop)) {
|
624
|
-
return Reflect.get(target, prop, receiver);
|
625
|
-
}
|
626
|
-
return Reflect.get(globalThis, prop, receiver);
|
627
|
-
}
|
628
|
-
});
|
629
|
-
const channel = new MessageChannel();
|
630
|
-
this.port = convertNodePortToWebPort(channel.port1);
|
631
|
-
this._vw_workerPort = convertNodePortToWebPort(channel.port2);
|
632
|
-
this._vw_workerTarget.addEventListener("connect", (e) => {
|
633
|
-
context.onconnect?.(e);
|
634
|
-
});
|
635
|
-
const runner = new InlineWorkerRunner(runnerOptions, context);
|
636
|
-
const id = getFileIdFromUrl(url);
|
637
|
-
this._vw_name = id;
|
638
|
-
runner.resolveUrl(id).then(([, fsPath]) => {
|
639
|
-
this._vw_name = name ?? fsPath;
|
640
|
-
debug("initialize shared worker %s", this._vw_name);
|
641
|
-
return runner.executeFile(fsPath).then(() => {
|
642
|
-
runnerOptions.moduleCache.invalidateSubDepTree([
|
643
|
-
fsPath,
|
644
|
-
runner.mocker.getMockPath(fsPath)
|
645
|
-
]);
|
646
|
-
this._vw_workerTarget.dispatchEvent(
|
647
|
-
new MessageEvent("connect", {
|
648
|
-
ports: [this._vw_workerPort]
|
649
|
-
})
|
650
|
-
);
|
651
|
-
debug("shared worker %s successfully initialized", this._vw_name);
|
652
|
-
});
|
653
|
-
}).catch((e) => {
|
654
|
-
debug("shared worker %s failed to initialize: %o", this._vw_name, e);
|
655
|
-
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
656
|
-
const error = new EventConstructor("error", {
|
657
|
-
error: e,
|
658
|
-
message: e.message
|
659
|
-
});
|
660
|
-
this.dispatchEvent(error);
|
661
|
-
this.onerror?.(error);
|
662
|
-
console.error(e);
|
663
|
-
});
|
664
|
-
}
|
665
|
-
};
|
666
|
-
}
|
667
|
-
|
668
668
|
function defineWebWorkers(options) {
|
669
669
|
if (typeof Worker === "undefined" || !("__VITEST_WEB_WORKER__" in globalThis.Worker)) {
|
670
670
|
assertGlobalExists("EventTarget");
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/web-worker",
|
3
3
|
"type": "module",
|
4
|
-
"version": "2.1.
|
4
|
+
"version": "2.1.4",
|
5
5
|
"description": "Web Worker support for testing in Vitest",
|
6
6
|
"license": "MIT",
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
@@ -33,10 +33,10 @@
|
|
33
33
|
"dist"
|
34
34
|
],
|
35
35
|
"peerDependencies": {
|
36
|
-
"vitest": "2.1.
|
36
|
+
"vitest": "2.1.4"
|
37
37
|
},
|
38
38
|
"dependencies": {
|
39
|
-
"debug": "^4.3.
|
39
|
+
"debug": "^4.3.7"
|
40
40
|
},
|
41
41
|
"devDependencies": {
|
42
42
|
"@types/debug": "^4.1.12",
|