Skip to content

fix(client, server): preserve message ordering for blob messages in peer adapters#1621

Merged
dinwwwh merged 3 commits into
middleapi:mainfrom
dinwwwh:fix/client-server/preserve-message-ordering-for-blob-messages-in-peer-adapters
Jul 3, 2026
Merged

fix(client, server): preserve message ordering for blob messages in peer adapters#1621
dinwwwh merged 3 commits into
middleapi:mainfrom
dinwwwh:fix/client-server/preserve-message-ordering-for-blob-messages-in-peer-adapters

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Jul 3, 2026

Copy link
Copy Markdown
Member

Blob messages require an async step to load bytes before handling, while text messages are handled synchronously. This meant that when a blob message was queued before a text message, the text message could reach the peer first, breaking expected ordering.

dinwwwh added 2 commits July 3, 2026 14:01
…eer adapters

Blob messages require an async step to load bytes before sending,
while text messages are sent synchronously. This meant that when a
blob message was queued before a text message, the text message
could reach the peer first, breaking expected ordering.
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 3, 2026
@vercel

vercel Bot commented Jul 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
orpc Ready Ready Preview, Comment Jul 3, 2026 7:41am

@dosubot dosubot Bot added bug Something isn't working javascript Pull requests that update javascript code labels Jul 3, 2026
@middleapi middleapi deleted a comment from coderabbitai Bot Jul 3, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 3, 2026

Copy link
Copy Markdown
More templates

@orpc/arktype

npm i https://pkg.pr.new/middleapi/orpc/@orpc/arktype@1621

@orpc/bun

npm i https://pkg.pr.new/middleapi/orpc/@orpc/bun@1621

@orpc/client

npm i https://pkg.pr.new/middleapi/orpc/@orpc/client@1621

@orpc/cloudflare

npm i https://pkg.pr.new/middleapi/orpc/@orpc/cloudflare@1621

@orpc/contract

npm i https://pkg.pr.new/middleapi/orpc/@orpc/contract@1621

@orpc/experimental-effect

npm i https://pkg.pr.new/middleapi/orpc/@orpc/experimental-effect@1621

@orpc/evlog

npm i https://pkg.pr.new/middleapi/orpc/@orpc/evlog@1621

@orpc/interop

npm i https://pkg.pr.new/middleapi/orpc/@orpc/interop@1621

@orpc/json-schema

npm i https://pkg.pr.new/middleapi/orpc/@orpc/json-schema@1621

@orpc/nest

npm i https://pkg.pr.new/middleapi/orpc/@orpc/nest@1621

@orpc/next

npm i https://pkg.pr.new/middleapi/orpc/@orpc/next@1621

@orpc/openapi

npm i https://pkg.pr.new/middleapi/orpc/@orpc/openapi@1621

@orpc/opentelemetry

npm i https://pkg.pr.new/middleapi/orpc/@orpc/opentelemetry@1621

@orpc/pino

npm i https://pkg.pr.new/middleapi/orpc/@orpc/pino@1621

@orpc/publisher

npm i https://pkg.pr.new/middleapi/orpc/@orpc/publisher@1621

@orpc/ratelimit

npm i https://pkg.pr.new/middleapi/orpc/@orpc/ratelimit@1621

@orpc/server

npm i https://pkg.pr.new/middleapi/orpc/@orpc/server@1621

@orpc/shared

npm i https://pkg.pr.new/middleapi/orpc/@orpc/shared@1621

@orpc/tanstack-query

npm i https://pkg.pr.new/middleapi/orpc/@orpc/tanstack-query@1621

@orpc/valibot

npm i https://pkg.pr.new/middleapi/orpc/@orpc/valibot@1621

@orpc/zod

npm i https://pkg.pr.new/middleapi/orpc/@orpc/zod@1621

commit: 9e68990

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

WebSocketHandler.upgrade passes a Promise<Uint8Array> to .message() when the event payload is a Blob, because loadBytes is not awaited. Awaiting the Blob load is required for the sequential ordering fix to work.

Reviewed changes — the PR removes async Blob loading from the text-or-bytes normalization path and wraps WebSocket/MessagePort message handlers in sequential queues so that blob-to-binary conversion doesn't let later text/bytes messages overtake earlier blob messages.

  • Synchronous toStringOrBytes in @orpc/shared now concatenates arrays directly instead of constructing a Blob, and no longer accepts Blob inputs.
  • Client WebSocket transport uses sequential plus an explicit await loadBytes(event.data) for blob payloads, keeping decode/dispatch order.
  • Server WebSocket handler uses sequential in .upgrade() and synchronous toStringOrBytes in .message().
  • MessagePort client/server switch from isPlainObject to isPeerMessage validation and return explicit matched results.
  • Crossws handler and test coverage updated; the previously skipped crossws parallel-stream test is now re-enabled.
  • Docs/JSDoc refined to warn about ordering and consistent peer instances.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

Comment thread packages/server/src/adapters/websocket/handler.ts Outdated

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes — the latest commit addressed the prior Blob-in-upgrade() ordering concern and tightened peer lifecycle handling across adapters.

  • Awaited loadBytes in WebSocketHandler.upgrade so Blob payloads are fully loaded before this.message(...) runs, fixing the Promise<Uint8Array> bug flagged in the previous review.
  • Reordered close cleanup in MessagePortHandler.close and WebSocketHandler.close to delete the peer from the map before awaiting peer.close(), avoiding races where an in-flight message could reach a closing peer.
  • Awaited peer.close() in MessagePortLinkTransport's close listener so the client transport waits for shutdown.
  • Documented the non-blocking dispatch in WebSocketLinkTransport with a comment explaining why peer.message(...) is intentionally not awaited.
  • Added/adjusted tests to cover Blob messages through WebSocketHandler.upgrade and to verify idempotent close handling.

Targeted tests pass, including the re-enabled crossws parallel stream cases.

Pullfrog  | View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

@dinwwwh dinwwwh merged commit 897a751 into middleapi:main Jul 3, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working javascript Pull requests that update javascript code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant