Skip to content

fix(http): proxies, null-prototype buffers and overridden byte reads in the JSON fallback - #95

Open
eliahilse wants to merge 1 commit into
mainfrom
fix/json-fallback-edges
Open

eliahilse wants to merge 1 commit into
mainfrom
fix/json-fallback-edges

Conversation

@eliahilse

Copy link
Copy Markdown
Owner

What

Closes the JSON fallback gaps that #92 deferred.

  • Proxies:
    • Where the runtime can detect a Proxy (node:util types.isProxy on Node, Bun and Deno), a Proxy in a JSON fallback body goes through a single-read serializer. A toJSON supplied by a trap is then honoured, as JSON.stringify would do.
    • A Proxy around a typed array, ArrayBuffer, SharedArrayBuffer or DataView fails with EncodeError code json at its path ("proxied byte storage or view"), in every runtime.
    • A revoked Proxy fails with code json.
  • Raw storage with a null prototype: an ArrayBuffer whose prototype was set to null is rejected like any other raw storage. Plain objects keep the cheap check.
  • Byte reads ignore overrides:
    • Base64 encoding reads bytes through the intrinsic Uint8Array.prototype.toBase64 where the runtime has it, and through a slot-safe copy otherwise. A subclass overriding length or subarray therefore encodes the bytes it contains.
    • A detached or out-of-bounds buffer fails with code json and its path.
  • Documented, not changed: on the JSON fallback path an accessor property may be read twice, and a getter that returns a different value per read serializes the later one. Making every read single costs about 3× on ordinary payloads. In a runtime without Proxy detection, an empty-target Proxy with a toJSON trap serializes as JSON.stringify sees it.

Performance

Bun 1.4.2, 10k rows ({id, name, active, score}, plus 4 bytes per row in the bytes case), medians of 31 interleaved runs in one process. The machine was heavily loaded, so the ratios are the meaningful part. The benchmark is in packages/hyperfly/benches/json-fallback.ts.

encode main this PR ratio
no bytes 0.989 ms 0.936 ms 0.95×
bytes 32.08 ms 33.48 ms 1.04×

Decoding is unchanged.

Tests

  • Cases for every rule above, at the root and nested, on both the fast path and the single-read path, with and without toBase64.
  • A 5,000-graph seeded differential checks that the single-read serializer matches JSON.stringify exactly on values without bytes.
  • No existing assertion changed.
  • Gates: lint, check-types, http.json vectors, and bun test with 1474 passing and 0 failing in an isolated run. Under heavy load, the unchanged inflate truncation tests and one wall-clock assertion in http.test.ts can exceed their limits; CI is the arbiter.

🤖 Generated with Claude Code

Co-Authored-By: GPT 6.1 Sol <noreply@openai.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant