fix(@Persist): add unwrapProxy for RPC serialization#5
Merged
Conversation
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 } ```
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>
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
Cherry-pick of cloudflare#100 by @dmmulroy
Adds
unwrapProxy()andcontainsProxy()functions to properly strip @persist proxy wrappers before RPC serialization.Problem
@persist decorator creates Proxy objects for change tracking. These proxies:
DataCloneErrorwhen passed tostructuredClone()RpcStubobjects when returned via RPC instead of plain dataSolution
unwrapProxy(value)function that recursively strips proxy markerscontainsProxy(value)helper to detect proxied objectsIS_PROXIEDsymbol for manual detectionTest Results
All 26 persist tests pass including:
@Persist): prevent null auto-vivification on read operations cloudflare/actors#93)@Persist): prevent heap overflow in error handler cloudflare/actors#94)References