fix: serialize @Persist state for RPC returns#100
Open
dmmulroy wants to merge 2 commits intocloudflare:mainfrom
Open
fix: serialize @Persist state for RPC returns#100dmmulroy wants to merge 2 commits intocloudflare:mainfrom
dmmulroy wants to merge 2 commits intocloudflare:mainfrom
Conversation
798db8d to
da0f7d8
Compare
Fixes cloudflare#99 - @persist state returned RpcStub objects instead of data Problem: Cloudflare RPC uses structured clone which can't serialize Proxy objects. @persist wraps values in Proxy for mutation tracking. Solution: Unwrap proxies before RPC serialization via _wrapMethodsForRpc(). Optimized to skip non-proxied values (most RPC returns) for performance. Key changes in persist.ts: - Add containsProxy() to detect IS_PROXIED symbol recursively - unwrapProxy() returns original when no proxy detected (fast path) - Only recreate objects with null prototype when proxy present ```typescript // Before: always recreated all objects export function unwrapProxy<T>(value: T): T { // ... always traverse and recreate with Object.create(null) } // After: fast path for non-proxied values export function unwrapProxy<T>(value: T): T { if (!containsProxy(value)) return value; // identity return // ... only unwrap when proxy detected } ```
da0f7d8 to
a624cd4
Compare
Changed Object.create(null) to {} so returned objects retain
standard prototype methods (hasOwnProperty, toString, etc).
Updated tests to verify malicious keys aren't copied as own
properties while allowing inherited prototype methods.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Brayden
approved these changes
Dec 8, 2025
|
What would it take to merge this fix and update the library? I've run into the same issue and now porting the fix into my fork of the library with other fixes. I am in no rush, just curious. Thank you |
apankov1
added a commit
to apankov1/actors
that referenced
this pull request
Dec 10, 2025
Allows consumers to manually unwrap @persist proxies for structuredClone and other non-RPC serialization use cases. Related to cloudflare#100
|
While testing this fix in my project, I ran into cases where auto-unwrapping doesn't apply — structuredClone() on @persist fields and manual caching to DO storage still fail with DataCloneError. Exporting unwrapProxy from the public API covers these: apankov1/actors@c8b730c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #99 -
@Persiststate returnedRpcStubobjects instead of serialized data.Problem: Cloudflare RPC uses structured clone which can't serialize Proxy objects.
@Persistwraps values in Proxy for mutation tracking, causing RPC callers to receiveRpcStubobjects.Solution: Unwrap proxies before RPC serialization. Optimized to skip non-proxied values for performance.
Key changes
Test plan
unwrapProxybehavior