@vitest/web-worker 2.1.2 → 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 +138 -136
- package/package.json +3 -3
package/dist/index.js
CHANGED
package/dist/pure.js
CHANGED
@@ -1,12 +1,14 @@
|
|
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) {
|
8
|
+
const mocker = globalThis.__vitest_mocker__;
|
8
9
|
super(options);
|
9
10
|
this.context = context;
|
11
|
+
this.mocker = globalThis.__vitest_mocker__ = mocker;
|
10
12
|
}
|
11
13
|
prepareContext(context) {
|
12
14
|
const ctx = super.prepareContext(context);
|
@@ -371,6 +373,141 @@ function getFileIdFromUrl(url) {
|
|
371
373
|
return stripProtocol(url);
|
372
374
|
}
|
373
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
|
+
|
374
511
|
function createWorkerConstructor(options) {
|
375
512
|
const runnerOptions = getRunnerOptions();
|
376
513
|
const cloneType = () => options?.clone ?? process.env.VITEST_WEB_WORKER_CLONE ?? "native";
|
@@ -528,141 +665,6 @@ function createWorkerConstructor(options) {
|
|
528
665
|
};
|
529
666
|
}
|
530
667
|
|
531
|
-
function convertNodePortToWebPort(port) {
|
532
|
-
if (!("addEventListener" in port)) {
|
533
|
-
Object.defineProperty(port, "addEventListener", {
|
534
|
-
value(...args) {
|
535
|
-
return this.addListener(...args);
|
536
|
-
},
|
537
|
-
configurable: true,
|
538
|
-
enumerable: true
|
539
|
-
});
|
540
|
-
}
|
541
|
-
if (!("removeEventListener" in port)) {
|
542
|
-
Object.defineProperty(port, "removeEventListener", {
|
543
|
-
value(...args) {
|
544
|
-
return this.removeListener(...args);
|
545
|
-
},
|
546
|
-
configurable: true,
|
547
|
-
enumerable: true
|
548
|
-
});
|
549
|
-
}
|
550
|
-
if (!("dispatchEvent" in port)) {
|
551
|
-
const emit = port.emit.bind(port);
|
552
|
-
Object.defineProperty(port, "emit", {
|
553
|
-
value(event) {
|
554
|
-
if (event.name === "message") {
|
555
|
-
port.onmessage?.(event);
|
556
|
-
}
|
557
|
-
if (event.name === "messageerror") {
|
558
|
-
port.onmessageerror?.(event);
|
559
|
-
}
|
560
|
-
return emit(event);
|
561
|
-
},
|
562
|
-
configurable: true,
|
563
|
-
enumerable: true
|
564
|
-
});
|
565
|
-
Object.defineProperty(port, "dispatchEvent", {
|
566
|
-
value(event) {
|
567
|
-
return this.emit(event);
|
568
|
-
},
|
569
|
-
configurable: true,
|
570
|
-
enumerable: true
|
571
|
-
});
|
572
|
-
}
|
573
|
-
return port;
|
574
|
-
}
|
575
|
-
function createSharedWorkerConstructor() {
|
576
|
-
const runnerOptions = getRunnerOptions();
|
577
|
-
return class SharedWorker extends EventTarget {
|
578
|
-
static __VITEST_WEB_WORKER__ = true;
|
579
|
-
_vw_workerTarget = new EventTarget();
|
580
|
-
_vw_name;
|
581
|
-
_vw_workerPort;
|
582
|
-
onerror = null;
|
583
|
-
port;
|
584
|
-
constructor(url, options) {
|
585
|
-
super();
|
586
|
-
const name = typeof options === "string" ? options : options?.name;
|
587
|
-
let selfProxy;
|
588
|
-
const context = {
|
589
|
-
onmessage: null,
|
590
|
-
onmessageerror: null,
|
591
|
-
onerror: null,
|
592
|
-
onlanguagechange: null,
|
593
|
-
onoffline: null,
|
594
|
-
ononline: null,
|
595
|
-
onrejectionhandled: null,
|
596
|
-
onrtctransform: null,
|
597
|
-
onunhandledrejection: null,
|
598
|
-
origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
|
599
|
-
importScripts: () => {
|
600
|
-
throw new Error(
|
601
|
-
"[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead."
|
602
|
-
);
|
603
|
-
},
|
604
|
-
crossOriginIsolated: false,
|
605
|
-
onconnect: null,
|
606
|
-
name: name || "",
|
607
|
-
close: () => this.port.close(),
|
608
|
-
dispatchEvent: (event) => {
|
609
|
-
return this._vw_workerTarget.dispatchEvent(event);
|
610
|
-
},
|
611
|
-
addEventListener: (...args) => {
|
612
|
-
return this._vw_workerTarget.addEventListener(...args);
|
613
|
-
},
|
614
|
-
removeEventListener: this._vw_workerTarget.removeEventListener,
|
615
|
-
get self() {
|
616
|
-
return selfProxy;
|
617
|
-
}
|
618
|
-
};
|
619
|
-
selfProxy = new Proxy(context, {
|
620
|
-
get(target, prop, receiver) {
|
621
|
-
if (Reflect.has(target, prop)) {
|
622
|
-
return Reflect.get(target, prop, receiver);
|
623
|
-
}
|
624
|
-
return Reflect.get(globalThis, prop, receiver);
|
625
|
-
}
|
626
|
-
});
|
627
|
-
const channel = new MessageChannel();
|
628
|
-
this.port = convertNodePortToWebPort(channel.port1);
|
629
|
-
this._vw_workerPort = convertNodePortToWebPort(channel.port2);
|
630
|
-
this._vw_workerTarget.addEventListener("connect", (e) => {
|
631
|
-
context.onconnect?.(e);
|
632
|
-
});
|
633
|
-
const runner = new InlineWorkerRunner(runnerOptions, context);
|
634
|
-
const id = getFileIdFromUrl(url);
|
635
|
-
this._vw_name = id;
|
636
|
-
runner.resolveUrl(id).then(([, fsPath]) => {
|
637
|
-
this._vw_name = name ?? fsPath;
|
638
|
-
debug("initialize shared worker %s", this._vw_name);
|
639
|
-
return runner.executeFile(fsPath).then(() => {
|
640
|
-
runnerOptions.moduleCache.invalidateSubDepTree([
|
641
|
-
fsPath,
|
642
|
-
runner.mocker.getMockPath(fsPath)
|
643
|
-
]);
|
644
|
-
this._vw_workerTarget.dispatchEvent(
|
645
|
-
new MessageEvent("connect", {
|
646
|
-
ports: [this._vw_workerPort]
|
647
|
-
})
|
648
|
-
);
|
649
|
-
debug("shared worker %s successfully initialized", this._vw_name);
|
650
|
-
});
|
651
|
-
}).catch((e) => {
|
652
|
-
debug("shared worker %s failed to initialize: %o", this._vw_name, e);
|
653
|
-
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
654
|
-
const error = new EventConstructor("error", {
|
655
|
-
error: e,
|
656
|
-
message: e.message
|
657
|
-
});
|
658
|
-
this.dispatchEvent(error);
|
659
|
-
this.onerror?.(error);
|
660
|
-
console.error(e);
|
661
|
-
});
|
662
|
-
}
|
663
|
-
};
|
664
|
-
}
|
665
|
-
|
666
668
|
function defineWebWorkers(options) {
|
667
669
|
if (typeof Worker === "undefined" || !("__VITEST_WEB_WORKER__" in globalThis.Worker)) {
|
668
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",
|