perf(standard-server): reduce payload size for batch responses#354
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThe changes adjust how batch responses are structured and validated in both test and production code. In the tests, the expected batch response items have been updated—headers have been set to an empty object and a response status has been modified. In the production file, the asynchronous generator in the response creator now yields items with explicit index, status, headers, and body, including a cleanup call. The parse function has been refined to validate that each item contains a numeric index and to default missing values according to the new structure, with an import update for headers. Changes
Sequence Diagram(s)sequenceDiagram
participant C as Client
participant TB as toBatchResponse
participant AG as Async Generator
participant PR as parseBatchResponse
C->>TB: Call toBatchResponse(options)
TB->>AG: Initialize generator for response body
loop Process each item in options.body
AG->>C: Yield BatchResponseBodyItem {index, status, headers, body}
end
AG->>TB: Execute cleanup via options.body.return()
C->>PR: Pass response for parsing
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
More templates
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/openapi
@orpc/openapi-client
@orpc/react
@orpc/react-query
@orpc/server
@orpc/solid-query
@orpc/shared
@orpc/standard-server
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/svelte-query
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/standard-server/src/batch/response.ts (1)
19-22: Consider adding unit test for edge casesWhile the optimization is sound, consider adding tests for edge cases such as:
- When status is undefined or matches the parent status
- When headers are empty or undefined
- When dealing with null or undefined bodies
This would ensure robustness when handling various input patterns.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/standard-server/src/batch/response.test.ts(2 hunks)packages/standard-server/src/batch/response.ts(3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/standard-server/src/batch/response.test.ts (1)
packages/standard-server/src/batch/response.ts (1)
BatchResponseBodyItem(4-6)
packages/standard-server/src/batch/response.ts (2)
packages/shared/src/object.ts (1)
isObject(31-39)packages/standard-server/src/types.ts (1)
StandardHeaders(1-3)
🔇 Additional comments (6)
packages/standard-server/src/batch/response.test.ts (2)
6-7: Test data updated to match new response formatThe test data has been updated to reflect the optimized batch response format:
r1now has an empty headers object instead of containing a custom headerr2has its status changed from 205 to 207These changes align with the implementation optimizations in the response.ts file, where empty headers and duplicate status codes are being omitted to reduce payload size.
48-48: Test case correctly updated for new validation logicThe test case for invalid batch item has been updated to yield an object with empty headers instead of a string. This aligns with the new validation in
parseBatchResponse, which now checks for a numeric index rather than just verifying the presence of status and headers.packages/standard-server/src/batch/response.ts (4)
1-1: Import updated to include StandardHeaders typeThe import now includes
StandardHeaderswhich is used for type validation and defaults in the updated implementation.
13-30: Good optimization to reduce payload sizeThe
toBatchResponsefunction has been optimized to reduce payload size by:
- Omitting the status code when it matches the parent response status
- Omitting headers when they're empty
- Ensuring proper cleanup with
options.body.return?.()in a finally blockThis is a well-designed optimization that will benefit performance, especially with large batches.
45-45: Improved validation for batch response itemsThe validation logic now checks that
item.indexis a number, which is more precise and aligns with theBatchResponseBodyIteminterface requirements.
51-56: Enhanced type safety and defaults for parsed response itemsThe yielded object now:
- Explicitly casts types for better type safety
- Uses null coalescing operators to provide sensible defaults
- Only includes necessary fields to reduce payload size
- Uses the
satisfiesoperator to ensure type compatibilityThis approach maintains type safety while optimizing the response format.
Summary by CodeRabbit
Refactor
Tests