@vitest/web-worker 4.1.4 → 5.0.0-beta.1

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.
Files changed (2) hide show
  1. package/dist/pure.js +24 -6
  2. package/package.json +2 -2
package/dist/pure.js CHANGED
@@ -316,16 +316,34 @@ function createClonedMessageEvent(data, transferOrOptions, clone) {
316
316
  debug("clone worker message %o", data);
317
317
  const origin = typeof location === "undefined" ? undefined : location.origin;
318
318
  const ports = transfer?.filter((t) => t instanceof MessagePort);
319
- const transferWithoutPorts = transfer?.filter(
320
- // `ports` must be excluded from the `transfer` option passed to `structuredClone` to keep the MessagePort objects working correctly in the same thread.
321
- (t) => !(t instanceof MessagePort)
322
- );
323
319
  if (typeof structuredClone === "function" && clone === "native") {
324
320
  debug("create message event, using native structured clone");
321
+ // A real Worker serializes `data` across realms and exposes the
322
+ // MessagePorts from `transfer` as `event.ports` on the receiving side.
323
+ // @vitest/web-worker runs both sides in a single realm, so we use
324
+ // `structuredClone` to emulate that transfer boundary.
325
+ //
326
+ // `MessageEvent.ports` must be the *cloned* ports returned by
327
+ // `structuredClone`, not the originals from `transfer`: once transferred,
328
+ // the originals are detached and can no longer communicate — e.g.
329
+ // `port1.postMessage(...)` on the caller side would not trigger
330
+ // `port2.onmessage` on a detached `port2`.
331
+ //
332
+ // `data` and `ports` must also be cloned in the *same* `structuredClone`
333
+ // call. A transferred object is detached immediately, so we cannot clone
334
+ // `data` first and then clone `ports` (or vice versa) — the second call
335
+ // would see already-detached ports. Passing them together as a single
336
+ // input also makes `structuredClone` deduplicate by identity, so a port
337
+ // referenced from inside `data` and from `transfer` resolves to the same
338
+ // transferred instance in the cloned graph.
339
+ const { data: clonedData, ports: clonedPorts } = structuredClone({
340
+ data,
341
+ ports
342
+ }, { transfer });
325
343
  return new MessageEvent("message", {
326
- data: structuredClone(data, { transfer: transferWithoutPorts }),
344
+ data: clonedData,
327
345
  origin,
328
- ports
346
+ ports: clonedPorts
329
347
  });
330
348
  }
331
349
  if (clone !== "none") {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/web-worker",
3
3
  "type": "module",
4
- "version": "4.1.4",
4
+ "version": "5.0.0-beta.1",
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.1.4"
36
+ "vitest": "5.0.0-beta.1"
37
37
  },
38
38
  "dependencies": {
39
39
  "obug": "^2.1.1"