fix(standard-server): optimize withEventMeta to avoid unnecessary proxies event iterator data - #979
Conversation
…xies This prevents structuredClone failures on proxy things Fixes: #977
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughAdds an early-return in withEventMeta to avoid creating a Proxy when meta has no meaningful fields (id, retry, comments empty/undefined), and adds tests verifying when the original object is returned versus a proxied/new object. No public API or exported signatures changed. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller
participant withEventMeta
participant Validations as Validations<br/>(id/retry/comments)
participant ProxyWrap as Proxy Wrapper
Caller->>withEventMeta: withEventMeta(container, meta)
alt meta.id/retry undefined AND meta.comments empty/undefined
withEventMeta-->>Caller: return container (same reference)
note right of withEventMeta #DFF2E1: Skip validations & Proxy
else meta has meaningful fields
withEventMeta->>Validations: assertEventId / assertEventRetry / assertEventComment
Validations-->>withEventMeta: validations pass
withEventMeta->>ProxyWrap: create Proxy and attach EVENT_SOURCE_META_SYMBOL
ProxyWrap-->>Caller: return proxied container
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Pre-merge checks (4 passed, 1 warning)❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Poem
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. 📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
🔇 Additional comments (2)
✨ Finishing Touches
🧪 Generate unit tests
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. Comment |
There was a problem hiding this comment.
Summary of Changes
Hello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces an optimization to the withEventMeta function within the standard-server package. The primary goal is to enhance efficiency and prevent potential issues by ensuring that proxy objects are only created when there are actual metadata changes to apply. This refinement helps avoid structuredClone failures that could arise from attempting to clone objects that were unnecessarily proxied.
Highlights
- Optimization of withEventMeta function: The withEventMeta function now includes a check to prevent the creation of unnecessary proxy objects when no actual event metadata (id, retry, or comments) is being applied. This ensures that the original object reference is returned if no metadata changes are required.
- Prevention of structuredClone failures: By avoiding the creation of superfluous proxy objects, this change directly addresses and prevents potential structuredClone failures that could occur when attempting to deep copy objects that were inadvertently proxied by withEventMeta without any meaningful metadata modifications.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces a valuable optimization to withEventMeta by avoiding the creation of a Proxy when no metadata is provided. This not only improves performance but also resolves a structuredClone failure on proxied objects. The implementation is sound and is accompanied by appropriate tests. I have one suggestion to enhance the code's conciseness.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
More templates
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-event-iterator
@orpc/hey-api
@orpc/interop
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/react
@orpc/react-query
@orpc/experimental-react-swr
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
packages/standard-server/src/event-iterator/meta.ts (1)
10-16: Tighten the “empty meta” check (readability).Use optional chaining + nullish coalescing to express “undefined or empty” succinctly.
Apply:
- if ( - meta.id === undefined - && meta.retry === undefined - && (meta.comments === undefined || meta.comments.length === 0) - ) { + if ( + meta.id === undefined + && meta.retry === undefined + && ((meta.comments?.length ?? 0) === 0) + ) { return container }
🧹 Nitpick comments (3)
packages/standard-server/src/event-iterator/meta.ts (2)
26-30: Guard against non-arraycommentsat runtime.If
commentsis accidentally a string in JS callers, the for-of loop will iterate characters. Add a shape check.Apply:
if (meta.comments !== undefined) { + if (!Array.isArray(meta.comments)) { + throw new TypeError("Event's comments must be an array of strings") + } for (const comment of meta.comments) { assertEventComment(comment) } }
32-40: Consider eliminating Proxy entirely with a WeakMap-backed meta store.If any meaningful meta reaches consumer-visible values, Proxies will still break structuredClone. A WeakMap keeps metadata without wrapping or mutating (GC-safe), and
getEventMetacan read from it.Example (outside the shown diff):
// module scope const META = new WeakMap<object, EventMeta>() export function withEventMeta<T extends object>(container: T, meta: EventMeta): T { if (meta.id === undefined && meta.retry === undefined && ((meta.comments?.length ?? 0) === 0)) { return container } // run validations... META.set(container, meta) return container } export function getEventMeta(container: unknown): EventMeta | undefined { return isTypescriptObject(container) ? META.get(container as object) : undefined }packages/standard-server/src/event-iterator/meta.test.ts (1)
19-29: Strengthen cases + fix test title grammar.Add coverage for
retry: 0and “comments-only” paths; tweak title for grammar.Apply:
-it('withEventMeta only proxy when make sense', () => { +it('withEventMeta only proxies when it makes sense', () => { const data = { value: 123, meta: undefined } expect(withEventMeta(data, { id: '123', retry: 10000, comments: ['hello', 'world'] })).not.toBe(data) expect(withEventMeta(data, { id: '123' })).not.toBe(data) expect(withEventMeta(data, {})).toBe(data) expect(withEventMeta(data, { notExists: true } as any)).toBe(data) expect(withEventMeta(data, { id: undefined })).toBe(data) expect(withEventMeta(data, { comments: [] })).toBe(data) + expect(withEventMeta(data, { retry: 0 })).not.toBe(data) + expect(withEventMeta(data, { comments: ['note'] })).not.toBe(data) })Optional: add a separate assertion that
structuredClone(withEventMeta(data, {}))does not throw, to lock in the fix for empty-meta cases.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/standard-server/src/event-iterator/meta.test.ts(1 hunks)packages/standard-server/src/event-iterator/meta.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/standard-server/src/event-iterator/meta.test.ts (1)
packages/standard-server/src/event-iterator/meta.ts (1)
withEventMeta(9-41)
🔇 Additional comments (1)
packages/standard-server/src/event-iterator/meta.ts (1)
10-16: Approve withEventMeta early-return fix. Verified that allwithEventMetacall sites use non-empty meta when proxying and nostructuredCloneusages exist to rewrap plain events.
This prevents structuredClone failures on proxy things
Fixes: #977
Summary by CodeRabbit