@vitest/web-worker 3.1.0-beta.1 → 3.1.0
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/pure.js +341 -386
- package/package.json +2 -2
package/dist/pure.js
CHANGED
@@ -4,23 +4,19 @@ import { readFileSync as readFileSync$1 } from 'node:fs';
|
|
4
4
|
import createDebug from 'debug';
|
5
5
|
|
6
6
|
class InlineWorkerRunner extends VitestExecutor {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
});
|
18
|
-
}
|
7
|
+
constructor(options, context) {
|
8
|
+
const mocker = globalThis.__vitest_mocker__;
|
9
|
+
super(options);
|
10
|
+
this.context = context;
|
11
|
+
this.mocker = globalThis.__vitest_mocker__ = mocker;
|
12
|
+
}
|
13
|
+
prepareContext(context) {
|
14
|
+
const ctx = super.prepareContext(context);
|
15
|
+
return Object.assign(ctx, this.context, { importScripts });
|
16
|
+
}
|
19
17
|
}
|
20
18
|
function importScripts() {
|
21
|
-
|
22
|
-
"[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead."
|
23
|
-
);
|
19
|
+
throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
|
24
20
|
}
|
25
21
|
|
26
22
|
const VOID = -1;
|
@@ -302,400 +298,359 @@ var ponyfillStructuredClone = typeof structuredClone === "function" ?
|
|
302
298
|
const readFileSync = readFileSync$1;
|
303
299
|
const debug = createDebug("vitest:web-worker");
|
304
300
|
function getWorkerState() {
|
305
|
-
|
301
|
+
return globalThis.__vitest_worker__;
|
306
302
|
}
|
307
303
|
function assertGlobalExists(name) {
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
);
|
312
|
-
}
|
304
|
+
if (!(name in globalThis)) {
|
305
|
+
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.`);
|
306
|
+
}
|
313
307
|
}
|
314
308
|
function createClonedMessageEvent(data, transferOrOptions, clone) {
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
origin
|
341
|
-
});
|
309
|
+
const transfer = Array.isArray(transferOrOptions) ? transferOrOptions : transferOrOptions?.transfer;
|
310
|
+
debug("clone worker message %o", data);
|
311
|
+
const origin = typeof location === "undefined" ? undefined : location.origin;
|
312
|
+
if (typeof structuredClone === "function" && clone === "native") {
|
313
|
+
debug("create message event, using native structured clone");
|
314
|
+
return new MessageEvent("message", {
|
315
|
+
data: structuredClone(data, { transfer }),
|
316
|
+
origin
|
317
|
+
});
|
318
|
+
}
|
319
|
+
if (clone !== "none") {
|
320
|
+
debug("create message event, using polyfilled structured clone");
|
321
|
+
if (transfer?.length) {
|
322
|
+
console.warn("[@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+.");
|
323
|
+
}
|
324
|
+
return new MessageEvent("message", {
|
325
|
+
data: ponyfillStructuredClone(data, { lossy: true }),
|
326
|
+
origin
|
327
|
+
});
|
328
|
+
}
|
329
|
+
debug("create message event without cloning an object");
|
330
|
+
return new MessageEvent("message", {
|
331
|
+
data,
|
332
|
+
origin
|
333
|
+
});
|
342
334
|
}
|
343
335
|
function createMessageEvent(data, transferOrOptions, clone) {
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
});
|
351
|
-
}
|
336
|
+
try {
|
337
|
+
return createClonedMessageEvent(data, transferOrOptions, clone);
|
338
|
+
} catch (error) {
|
339
|
+
debug("failed to clone message, dispatch \"messageerror\" event: %o", error);
|
340
|
+
return new MessageEvent("messageerror", { data: error });
|
341
|
+
}
|
352
342
|
}
|
353
343
|
function getRunnerOptions() {
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
344
|
+
const state = getWorkerState();
|
345
|
+
const { config, rpc, moduleCache, moduleExecutionInfo } = state;
|
346
|
+
return {
|
347
|
+
async fetchModule(id) {
|
348
|
+
const result = await rpc.fetch(id, "web");
|
349
|
+
if (result.id && !result.externalize) {
|
350
|
+
const code = readFileSync(result.id, "utf-8");
|
351
|
+
return { code };
|
352
|
+
}
|
353
|
+
return result;
|
354
|
+
},
|
355
|
+
resolveId(id, importer) {
|
356
|
+
return rpc.resolveId(id, importer, "web");
|
357
|
+
},
|
358
|
+
moduleCache,
|
359
|
+
moduleExecutionInfo,
|
360
|
+
interopDefault: config.deps.interopDefault ?? true,
|
361
|
+
moduleDirectories: config.deps.moduleDirectories,
|
362
|
+
root: config.root,
|
363
|
+
base: config.base,
|
364
|
+
state
|
365
|
+
};
|
376
366
|
}
|
377
367
|
function stripProtocol(url) {
|
378
|
-
|
368
|
+
return url.toString().replace(/^file:\/+/, "/");
|
379
369
|
}
|
380
370
|
function getFileIdFromUrl(url) {
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
371
|
+
if (typeof self === "undefined") {
|
372
|
+
return stripProtocol(url);
|
373
|
+
}
|
374
|
+
if (!(url instanceof URL)) {
|
375
|
+
url = new URL(url, self.location.origin);
|
376
|
+
}
|
377
|
+
if (url.protocol === "http:" || url.protocol === "https:") {
|
378
|
+
return url.pathname;
|
379
|
+
}
|
380
|
+
return stripProtocol(url);
|
391
381
|
}
|
392
382
|
|
393
383
|
function convertNodePortToWebPort(port) {
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
384
|
+
if (!("addEventListener" in port)) {
|
385
|
+
Object.defineProperty(port, "addEventListener", {
|
386
|
+
value(...args) {
|
387
|
+
return this.addListener(...args);
|
388
|
+
},
|
389
|
+
configurable: true,
|
390
|
+
enumerable: true
|
391
|
+
});
|
392
|
+
}
|
393
|
+
if (!("removeEventListener" in port)) {
|
394
|
+
Object.defineProperty(port, "removeEventListener", {
|
395
|
+
value(...args) {
|
396
|
+
return this.removeListener(...args);
|
397
|
+
},
|
398
|
+
configurable: true,
|
399
|
+
enumerable: true
|
400
|
+
});
|
401
|
+
}
|
402
|
+
if (!("dispatchEvent" in port)) {
|
403
|
+
const emit = port.emit.bind(port);
|
404
|
+
Object.defineProperty(port, "emit", {
|
405
|
+
value(event) {
|
406
|
+
if (event.name === "message") {
|
407
|
+
port.onmessage?.(event);
|
408
|
+
}
|
409
|
+
if (event.name === "messageerror") {
|
410
|
+
port.onmessageerror?.(event);
|
411
|
+
}
|
412
|
+
return emit(event);
|
413
|
+
},
|
414
|
+
configurable: true,
|
415
|
+
enumerable: true
|
416
|
+
});
|
417
|
+
Object.defineProperty(port, "dispatchEvent", {
|
418
|
+
value(event) {
|
419
|
+
return this.emit(event);
|
420
|
+
},
|
421
|
+
configurable: true,
|
422
|
+
enumerable: true
|
423
|
+
});
|
424
|
+
}
|
425
|
+
return port;
|
436
426
|
}
|
437
427
|
function createSharedWorkerConstructor() {
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
518
|
-
const error = new EventConstructor("error", {
|
519
|
-
error: e,
|
520
|
-
message: e.message
|
521
|
-
});
|
522
|
-
this.dispatchEvent(error);
|
523
|
-
this.onerror?.(error);
|
524
|
-
console.error(e);
|
525
|
-
});
|
526
|
-
}
|
527
|
-
};
|
428
|
+
const runnerOptions = getRunnerOptions();
|
429
|
+
return class SharedWorker extends EventTarget {
|
430
|
+
static __VITEST_WEB_WORKER__ = true;
|
431
|
+
_vw_workerTarget = new EventTarget();
|
432
|
+
_vw_name;
|
433
|
+
_vw_workerPort;
|
434
|
+
onerror = null;
|
435
|
+
port;
|
436
|
+
constructor(url, options) {
|
437
|
+
super();
|
438
|
+
const name = typeof options === "string" ? options : options?.name;
|
439
|
+
let selfProxy;
|
440
|
+
const context = {
|
441
|
+
onmessage: null,
|
442
|
+
onmessageerror: null,
|
443
|
+
onerror: null,
|
444
|
+
onlanguagechange: null,
|
445
|
+
onoffline: null,
|
446
|
+
ononline: null,
|
447
|
+
onrejectionhandled: null,
|
448
|
+
onrtctransform: null,
|
449
|
+
onunhandledrejection: null,
|
450
|
+
origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
|
451
|
+
importScripts: () => {
|
452
|
+
throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
|
453
|
+
},
|
454
|
+
crossOriginIsolated: false,
|
455
|
+
onconnect: null,
|
456
|
+
name: name || "",
|
457
|
+
close: () => this.port.close(),
|
458
|
+
dispatchEvent: (event) => {
|
459
|
+
return this._vw_workerTarget.dispatchEvent(event);
|
460
|
+
},
|
461
|
+
addEventListener: (...args) => {
|
462
|
+
return this._vw_workerTarget.addEventListener(...args);
|
463
|
+
},
|
464
|
+
removeEventListener: (...args) => {
|
465
|
+
return this._vw_workerTarget.removeEventListener(...args);
|
466
|
+
},
|
467
|
+
get self() {
|
468
|
+
return selfProxy;
|
469
|
+
}
|
470
|
+
};
|
471
|
+
selfProxy = new Proxy(context, { get(target, prop, receiver) {
|
472
|
+
if (Reflect.has(target, prop)) {
|
473
|
+
return Reflect.get(target, prop, receiver);
|
474
|
+
}
|
475
|
+
return Reflect.get(globalThis, prop, receiver);
|
476
|
+
} });
|
477
|
+
const channel = new MessageChannel();
|
478
|
+
this.port = convertNodePortToWebPort(channel.port1);
|
479
|
+
this._vw_workerPort = convertNodePortToWebPort(channel.port2);
|
480
|
+
this._vw_workerTarget.addEventListener("connect", (e) => {
|
481
|
+
context.onconnect?.(e);
|
482
|
+
});
|
483
|
+
const runner = new InlineWorkerRunner(runnerOptions, context);
|
484
|
+
const id = getFileIdFromUrl(url);
|
485
|
+
this._vw_name = id;
|
486
|
+
runner.resolveUrl(id).then(([, fsPath]) => {
|
487
|
+
this._vw_name = name ?? fsPath;
|
488
|
+
debug("initialize shared worker %s", this._vw_name);
|
489
|
+
return runner.executeFile(fsPath).then(() => {
|
490
|
+
runnerOptions.moduleCache.invalidateSubDepTree([fsPath, runner.mocker.getMockPath(fsPath)]);
|
491
|
+
this._vw_workerTarget.dispatchEvent(new MessageEvent("connect", { ports: [this._vw_workerPort] }));
|
492
|
+
debug("shared worker %s successfully initialized", this._vw_name);
|
493
|
+
});
|
494
|
+
}).catch((e) => {
|
495
|
+
debug("shared worker %s failed to initialize: %o", this._vw_name, e);
|
496
|
+
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
497
|
+
const error = new EventConstructor("error", {
|
498
|
+
error: e,
|
499
|
+
message: e.message
|
500
|
+
});
|
501
|
+
this.dispatchEvent(error);
|
502
|
+
this.onerror?.(error);
|
503
|
+
console.error(e);
|
504
|
+
});
|
505
|
+
}
|
506
|
+
};
|
528
507
|
}
|
529
508
|
|
530
509
|
function createWorkerConstructor(options) {
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
return;
|
664
|
-
}
|
665
|
-
debug(
|
666
|
-
"posting message %o from the main thread to the worker %s",
|
667
|
-
data,
|
668
|
-
this._vw_name
|
669
|
-
);
|
670
|
-
const event = createMessageEvent(data, transferOrOptions, cloneType());
|
671
|
-
if (event.type === "messageerror") {
|
672
|
-
this.dispatchEvent(event);
|
673
|
-
} else {
|
674
|
-
this._vw_workerTarget.dispatchEvent(event);
|
675
|
-
}
|
676
|
-
}
|
677
|
-
terminate() {
|
678
|
-
debug("terminating worker %s", this._vw_name);
|
679
|
-
this._vw_outsideListeners.forEach((fn, type) => {
|
680
|
-
this.removeEventListener(type, fn);
|
681
|
-
});
|
682
|
-
this._vw_insideListeners.forEach((fn, type) => {
|
683
|
-
this._vw_workerTarget.removeEventListener(type, fn);
|
684
|
-
});
|
685
|
-
}
|
686
|
-
};
|
510
|
+
const runnerOptions = getRunnerOptions();
|
511
|
+
const cloneType = () => options?.clone ?? process.env.VITEST_WEB_WORKER_CLONE ?? "native";
|
512
|
+
return class Worker extends EventTarget {
|
513
|
+
static __VITEST_WEB_WORKER__ = true;
|
514
|
+
_vw_workerTarget = new EventTarget();
|
515
|
+
_vw_insideListeners = new Map();
|
516
|
+
_vw_outsideListeners = new Map();
|
517
|
+
_vw_name;
|
518
|
+
_vw_messageQueue = [];
|
519
|
+
onmessage = null;
|
520
|
+
onmessageerror = null;
|
521
|
+
onerror = null;
|
522
|
+
constructor(url, options) {
|
523
|
+
super();
|
524
|
+
let selfProxy;
|
525
|
+
const context = {
|
526
|
+
onmessage: null,
|
527
|
+
onmessageerror: null,
|
528
|
+
onerror: null,
|
529
|
+
onlanguagechange: null,
|
530
|
+
onoffline: null,
|
531
|
+
ononline: null,
|
532
|
+
onrejectionhandled: null,
|
533
|
+
onrtctransform: null,
|
534
|
+
onunhandledrejection: null,
|
535
|
+
origin: typeof location !== "undefined" ? location.origin : "http://localhost:3000",
|
536
|
+
importScripts: () => {
|
537
|
+
throw new Error("[vitest] `importScripts` is not supported in Vite workers. Please, consider using `import` instead.");
|
538
|
+
},
|
539
|
+
crossOriginIsolated: false,
|
540
|
+
name: options?.name || "",
|
541
|
+
close: () => this.terminate(),
|
542
|
+
dispatchEvent: (event) => {
|
543
|
+
return this._vw_workerTarget.dispatchEvent(event);
|
544
|
+
},
|
545
|
+
addEventListener: (...args) => {
|
546
|
+
if (args[1]) {
|
547
|
+
this._vw_insideListeners.set(args[0], args[1]);
|
548
|
+
}
|
549
|
+
return this._vw_workerTarget.addEventListener(...args);
|
550
|
+
},
|
551
|
+
removeEventListener: (...args) => {
|
552
|
+
return this._vw_workerTarget.removeEventListener(...args);
|
553
|
+
},
|
554
|
+
postMessage: (...args) => {
|
555
|
+
if (!args.length) {
|
556
|
+
throw new SyntaxError("\"postMessage\" requires at least one argument.");
|
557
|
+
}
|
558
|
+
debug("posting message %o from the worker %s to the main thread", args[0], this._vw_name);
|
559
|
+
const event = createMessageEvent(args[0], args[1], cloneType());
|
560
|
+
this.dispatchEvent(event);
|
561
|
+
},
|
562
|
+
get self() {
|
563
|
+
return selfProxy;
|
564
|
+
}
|
565
|
+
};
|
566
|
+
selfProxy = new Proxy(context, { get(target, prop, receiver) {
|
567
|
+
if (Reflect.has(target, prop)) {
|
568
|
+
return Reflect.get(target, prop, receiver);
|
569
|
+
}
|
570
|
+
return globalThis[prop];
|
571
|
+
} });
|
572
|
+
this._vw_workerTarget.addEventListener("message", (e) => {
|
573
|
+
context.onmessage?.(e);
|
574
|
+
});
|
575
|
+
this.addEventListener("message", (e) => {
|
576
|
+
this.onmessage?.(e);
|
577
|
+
});
|
578
|
+
this.addEventListener("messageerror", (e) => {
|
579
|
+
this.onmessageerror?.(e);
|
580
|
+
});
|
581
|
+
const runner = new InlineWorkerRunner(runnerOptions, context);
|
582
|
+
const id = getFileIdFromUrl(url);
|
583
|
+
this._vw_name = id;
|
584
|
+
runner.resolveUrl(id).then(([, fsPath]) => {
|
585
|
+
this._vw_name = options?.name ?? fsPath;
|
586
|
+
debug("initialize worker %s", this._vw_name);
|
587
|
+
return runner.executeFile(fsPath).then(() => {
|
588
|
+
runnerOptions.moduleCache.invalidateSubDepTree([fsPath, runner.mocker.getMockPath(fsPath)]);
|
589
|
+
const q = this._vw_messageQueue;
|
590
|
+
this._vw_messageQueue = null;
|
591
|
+
if (q) {
|
592
|
+
q.forEach(([data, transfer]) => this.postMessage(data, transfer), this);
|
593
|
+
}
|
594
|
+
debug("worker %s successfully initialized", this._vw_name);
|
595
|
+
});
|
596
|
+
}).catch((e) => {
|
597
|
+
debug("worker %s failed to initialize: %o", this._vw_name, e);
|
598
|
+
const EventConstructor = globalThis.ErrorEvent || globalThis.Event;
|
599
|
+
const error = new EventConstructor("error", {
|
600
|
+
error: e,
|
601
|
+
message: e.message
|
602
|
+
});
|
603
|
+
this.dispatchEvent(error);
|
604
|
+
this.onerror?.(error);
|
605
|
+
console.error(e);
|
606
|
+
});
|
607
|
+
}
|
608
|
+
addEventListener(type, callback, options) {
|
609
|
+
if (callback) {
|
610
|
+
this._vw_outsideListeners.set(type, callback);
|
611
|
+
}
|
612
|
+
return super.addEventListener(type, callback, options);
|
613
|
+
}
|
614
|
+
postMessage(...args) {
|
615
|
+
if (!args.length) {
|
616
|
+
throw new SyntaxError("\"postMessage\" requires at least one argument.");
|
617
|
+
}
|
618
|
+
const [data, transferOrOptions] = args;
|
619
|
+
if (this._vw_messageQueue != null) {
|
620
|
+
debug("worker %s is not yet initialized, queue message %s", this._vw_name, data);
|
621
|
+
this._vw_messageQueue.push([data, transferOrOptions]);
|
622
|
+
return;
|
623
|
+
}
|
624
|
+
debug("posting message %o from the main thread to the worker %s", data, this._vw_name);
|
625
|
+
const event = createMessageEvent(data, transferOrOptions, cloneType());
|
626
|
+
if (event.type === "messageerror") {
|
627
|
+
this.dispatchEvent(event);
|
628
|
+
} else {
|
629
|
+
this._vw_workerTarget.dispatchEvent(event);
|
630
|
+
}
|
631
|
+
}
|
632
|
+
terminate() {
|
633
|
+
debug("terminating worker %s", this._vw_name);
|
634
|
+
this._vw_outsideListeners.forEach((fn, type) => {
|
635
|
+
this.removeEventListener(type, fn);
|
636
|
+
});
|
637
|
+
this._vw_insideListeners.forEach((fn, type) => {
|
638
|
+
this._vw_workerTarget.removeEventListener(type, fn);
|
639
|
+
});
|
640
|
+
}
|
641
|
+
};
|
687
642
|
}
|
688
643
|
|
689
644
|
function defineWebWorkers(options) {
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
645
|
+
if (typeof Worker === "undefined" || !("__VITEST_WEB_WORKER__" in globalThis.Worker)) {
|
646
|
+
assertGlobalExists("EventTarget");
|
647
|
+
assertGlobalExists("MessageEvent");
|
648
|
+
globalThis.Worker = createWorkerConstructor(options);
|
649
|
+
}
|
650
|
+
if (typeof SharedWorker === "undefined" || !("__VITEST_WEB_WORKER__" in globalThis.SharedWorker)) {
|
651
|
+
assertGlobalExists("EventTarget");
|
652
|
+
globalThis.SharedWorker = createSharedWorkerConstructor();
|
653
|
+
}
|
699
654
|
}
|
700
655
|
|
701
656
|
export { defineWebWorkers };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/web-worker",
|
3
3
|
"type": "module",
|
4
|
-
"version": "3.1.0
|
4
|
+
"version": "3.1.0",
|
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": "3.1.0
|
36
|
+
"vitest": "3.1.0"
|
37
37
|
},
|
38
38
|
"dependencies": {
|
39
39
|
"debug": "^4.4.0"
|