Skip to content

[PHP-WASM] Transfer event stdin streams across workers - #4060

Merged
adamziel merged 5 commits into
trunkfrom
codex/readable-stream-transfer
Jul 15, 2026
Merged

adamziel merged 5 commits into
trunkfrom
codex/readable-stream-transfer

Conversation

@adamziel

@adamziel adamziel commented Jul 15, 2026 •

Copy link
Copy Markdown
Collaborator

PHPWorker can now deliver an event whose stdin remains a live ReadableStream across the browser worker boundary. Comlink applies transfer handlers only to the top-level value, so a stream stored in an event's stdin property otherwise reaches postMessage() without a transfer list and structured cloning fails.

The event handler serializes stdin through a dedicated stream handler. It transfers the stream directly when the runtime supports that operation and uses the existing MessagePort bridge otherwise. The bridge transfers owned chunk copies because ReadableStream.tee() branches share chunk objects and detaching one branch's buffer must not invalidate the others. Consumer cancellation travels back over the port and cancels the source reader.

A stream can only be transferred once, so PHPWorker gives each listener its own tee branch. Slow listeners may queue unread chunks because this transport does not coordinate backpressure between branches.

Browser-worker tests cover native stream transfer and the forced MessagePort fallback, including incremental delivery to two listeners and source cancellation after both tee branches cancel.

This is the worker transport required by #3996. It adds no sendmail behavior.

Copilot AI review requested due to automatic review settings July 15, 2026 09:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for transferring ReadableStream instances nested inside worker-dispatched events (e.g., stdin on a PHPWorker event) across the Comlink boundary, including a Safari fallback via MessagePort, and validates the behavior with a Playwright test.

Changes:

  • Register Comlink transfer handlers for ReadableStream<Uint8Array> and for event objects that contain nested streams.
  • Update PHPWorker.dispatchEvent() to tee stdin streams so each listener receives a transferable branch.
  • Add a Playwright worker + spec to verify multi-listener delivery and that the source stream remains live during reads.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/php-wasm/web/src/test/readable-stream-transfer.spec.ts New Playwright spec validating cross-worker stream transfer to multiple listeners.
packages/php-wasm/web/src/test/playwright/readable-stream-worker.ts New worker script that emits an event containing a ReadableStream payload.
packages/php-wasm/web/src/test/playwright/browser-globals.ts Exposes worker-spawn + API-consumption helpers and the worker URL to the browser test page.
packages/php-wasm/web/playwright.config.ts Includes the new stream-transfer spec in the Playwright run.
packages/php-wasm/universal/src/lib/php-worker.ts Tees readable stdin streams per listener to avoid one-time transfer limitations.
packages/php-wasm/universal/src/lib/api.ts Adds Comlink transfer handlers for ReadableStream and nested-stream event payloads (Safari fallback via MessagePort).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/php-wasm/web/src/test/readable-stream-transfer.spec.ts
Comment thread packages/php-wasm/web/src/test/readable-stream-transfer.spec.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/php-worker.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/php-worker.ts
Comment thread packages/php-wasm/web/src/test/playwright/browser-globals.ts Outdated
@adamziel adamziel changed the title [PHP-WASM] Transfer ReadableStream events across workers [PHP-WASM] Transfer event stdin streams across workers Jul 15, 2026
@adamziel
adamziel merged commit ba93b1c into trunk Jul 15, 2026
53 checks passed
@adamziel
adamziel deleted the codex/readable-stream-transfer branch July 15, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants