Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-vans-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hyperfly": patch
---

JSON fallback handles proxy `toJSON` hooks when runtime detection is available and reports invalid byte storage with value paths. Byte encoding ignores overridden typed-array properties so subclasses produce the bytes they contain.
3 changes: 3 additions & 0 deletions packages/hyperfly/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ was created, including `Buffer` and values from other realms. Raw `ArrayBuffer`
views (including `DataView`) raise code `json` with their path, like Python's unsupported `memoryview`;
wrap the desired range in a `Uint8Array` to send it as bytes.

On the JSON fallback path an accessor property may be read twice; a getter that returns a different value per read serializes the later one.
Without runtime proxy detection, an empty-target Proxy that supplies `toJSON` through its `get` trap follows native `JSON.stringify` behavior.

A client that holds nothing gets JSON plus a `Hyperfly-Offer` naming an artifact it could fetch; once it has it, the
same route can answer in binary. Responses carry `Vary: Hyperfly-Accept, Accept`.
`respond` answers HEAD with the headers GET would get and no body, and status 204, 205 or 304 with the negotiated
Expand Down
52 changes: 52 additions & 0 deletions packages/hyperfly/benches/json-fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { encodeFor, negotiate, type JsonNegotiation } from "../src/http.js";
import { CodecRegistry, compileIR, type IRNode } from "../src/index.js";

// Extract origin/main's packages/hyperfly/src into a temporary directory, then
// pass its http.ts path so both versions run on the same Bun process and data.
const baselinePath = Bun.argv[2];
if (!baselinePath) throw new Error("usage: bun benches/json-fallback.ts /path/to/origin-main/src/http.ts");
const baseline = await import(baselinePath) as { encodeFor: typeof encodeFor };
const fields: Extract<IRNode, { kind: "struct" }>["fields"] = [
{ name: "id", type: { kind: "int" } }, { name: "name", type: { kind: "string" } },
{ name: "active", type: { kind: "bool" } }, { name: "score", type: { kind: "float64" } },
];
const registry = new CodecRegistry([compileIR({ kind: "array", element: { kind: "struct", fields } })]);
const byteRegistry = new CodecRegistry([compileIR({ kind: "array", element: { kind: "struct", fields: [
...fields, { name: "data", type: { kind: "bytes" } },
] } })]);
const rows = Array.from({ length: 10_000 }, (_, id) => ({ id, name: `row ${id}`, active: id % 2 === 0, score: id / 2 }));
const bytes = rows.map((row) => ({ ...row, data: new Uint8Array([0, 127, 128, 255]) }));
const samples = 31;
const iterations = 5;
let consumed = 0;
function time(encode: typeof encodeFor, value: unknown, decision: JsonNegotiation) {
const start = performance.now();
for (let i = 0; i < iterations; i++) consumed += (encode(decision, value).body as string).length;
return (performance.now() - start) / iterations;
}
function median(values: number[]) {
return values.sort((a, b) => a - b)[Math.floor(values.length / 2)]!;
}
for (const [name, value, codecs] of [["encode no-bytes", rows, registry], ["encode bytes", bytes, byteRegistry]] as const) {
const decision = negotiate(null, codecs);
if (decision.kind !== "json") throw new Error("expected JSON negotiation");
for (let i = 0; i < 12; i++) {
time(baseline.encodeFor, value, decision);
time(encodeFor, value, decision);
}
const main: number[] = [];
const current: number[] = [];
for (let i = 0; i < samples; i++) {
if (i % 2 === 0) {
main.push(time(baseline.encodeFor, value, decision));
current.push(time(encodeFor, value, decision));
} else {
current.push(time(encodeFor, value, decision));
main.push(time(baseline.encodeFor, value, decision));
}
}
const before = median(main);
const after = median(current);
console.log(`${name}: origin/main=${before.toFixed(3)}ms current=${after.toFixed(3)}ms ratio=${(after / before).toFixed(3)} (${samples} interleaved samples, ${iterations} encodes/sample)`);
}
console.log(`Bun ${Bun.version}; consumed ${consumed} characters`);
193 changes: 174 additions & 19 deletions packages/hyperfly/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,25 @@ function jsonAt(cause: unknown, key: string): never {
throw cause;
}

const jsonTypedArrayToBase64 = Object.getOwnPropertyDescriptor(Uint8Array.prototype, "toBase64")?.value as ((this: Uint8Array) => string) | undefined;
const jsonTypedArrayBuffer = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype) as object, "buffer")!.get!;

function base64(bytes: Uint8Array): string {
// Native encoding reads and validates the typed-array slots directly.
if (typeof jsonTypedArrayToBase64 === "function") return jsonTypedArrayToBase64.call(bytes);
// The copy constructor validates the view and copies slots without reading
// user-provided length, iterator, constructor, or subarray properties.
const copy = new Uint8Array(bytes);
const length = byteLengthOf(copy);
if (length < 32) {
let raw = "";
for (let i = 0; i < length; i++) raw += String.fromCharCode(copy[i]!);
return btoa(raw);
}
const buffer = jsonTypedArrayBuffer.call(copy) as ArrayBuffer;
const chunks: string[] = [];
for (let start = 0; start < bytes.length; start += 0x8000) {
chunks.push(String.fromCharCode(...bytes.subarray(start, start + 0x8000)));
for (let start = 0; start < length; start += 0x8000) {
chunks.push(String.fromCharCode(...new Uint8Array(buffer, start, Math.min(0x8000, length - start))));
}
return btoa(chunks.join(""));
}
Expand Down Expand Up @@ -331,9 +346,29 @@ const bufferByteLengths = [ArrayBuffer, globalThis.SharedArrayBuffer]
.filter((type) => typeof type === "function")
.map((type) => Object.getOwnPropertyDescriptor(type.prototype, "byteLength")!.get!);

/** The intrinsic getters recognize raw storage, shared or not, from other realms without trusting its tag. */
function isRawArrayBuffer(value: object): boolean {
if (!("byteLength" in value)) return false;
interface JsonIntrinsics {
isProxy(value: unknown): boolean;
isBoxedPrimitive?(value: unknown): boolean;
}

/** Runtime slot checks keep the preflight free of observable Proxy traps. */
function jsonIntrinsics(): JsonIntrinsics | undefined {
const get = (globalThis as { process?: { getBuiltinModule?: (id: string) => unknown } }).process?.getBuiltinModule;
try {
const types = (get?.("node:util") as { types?: JsonIntrinsics } | undefined)?.types;
if (typeof types?.isProxy === "function") return types;
} catch {
// Native JSON traversal remains available without runtime Proxy detection.
}
return undefined;
}

const jsonSlots = jsonIntrinsics();
const TypedArray = Object.getPrototypeOf(Uint8Array) as typeof Uint8Array;

/** Intrinsic getters also recognize storage with a null prototype and storage from other realms. */
function isRawArrayBuffer(value: object, prototype: object | null): boolean {
if (prototype === Object.prototype || prototype === Array.prototype) return false;
for (const byteLength of bufferByteLengths) {
try {
byteLength.call(value);
Expand All @@ -345,56 +380,168 @@ function isRawArrayBuffer(value: object): boolean {
return false;
}

/** Slot checks run first; a byte prototype without slots identifies a proxied or fabricated receiver. */
function isProxiedByteStorage(value: object, prototype: object | null): boolean {
return prototype !== Object.prototype && prototype !== Array.prototype && prototype !== null
&& (value instanceof TypedArray || value instanceof ArrayBuffer || value instanceof DataView
|| (typeof SharedArrayBuffer === "function" && value instanceof SharedArrayBuffer));
}

/** A bounded preflight keeps ordinary data on the native JSON.stringify fast path. */
function mayContainBytes(value: object, depth = 0): boolean {
if (depth === 128 || ArrayBuffer.isView(value) || "toJSON" in value || isRawArrayBuffer(value)) return true;
function mayContainBytes(value: object, state: { proxy: boolean }, depth = 0): boolean {
if (jsonSlots?.isProxy(value)) { state.proxy = true; return true; }
if (depth === 128 || ArrayBuffer.isView(value)) return true;
const prototype: object | null = Object.getPrototypeOf(value);
if (isRawArrayBuffer(value, prototype)) return true;
if (isProxiedByteStorage(value, prototype)) { state.proxy = true; return true; }
if ("toJSON" in value) return true;
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
const item: unknown = value[i];
if (typeof item === "object" && item !== null) {
if (mayContainBytes(item, depth + 1)) return true;
if (mayContainBytes(item, state, depth + 1)) return true;
} else if (typeof item === "function" || typeof item === "bigint") return true;
}
} else {
for (const key in value) {
if (!Object.hasOwn(value, key)) continue;
const item: unknown = (value as Record<string, unknown>)[key];
if (typeof item === "object" && item !== null) {
if (mayContainBytes(item, depth + 1)) return true;
if (mayContainBytes(item, state, depth + 1)) return true;
} else if (typeof item === "function" || typeof item === "bigint") return true;
}
}
return false;
}

/** JSON unboxes only these four internal-slot types, even across realms or with altered prototypes. */
function unboxJson(value: object): unknown {
if (jsonSlots?.isBoxedPrimitive?.(value) === false) return value;
let number = false;
try { Number.prototype.valueOf.call(value); number = true; } catch { /* Not a Number wrapper. */ }
if (number) return +(value as unknown as number);
let string = false;
try { String.prototype.valueOf.call(value); string = true; } catch { /* Not a String wrapper. */ }
if (string) return `${value as unknown as string}`;
try { return Boolean.prototype.valueOf.call(value); } catch { /* Not a Boolean wrapper. */ }
try { return BigInt.prototype.valueOf.call(value); } catch { /* Not a BigInt wrapper. */ }
return value;
}

/** SerializeProperty in JSON order, retaining the single Get before toJSON so bytes cannot be hidden. */
function stringifyWithBytes(input: unknown): string | undefined {
const ancestors = new Set<object>();
const path: (string | number)[] = [];
function fail(message: string, cause?: unknown): never {
throw new EncodeError("json", `$${path.map((key) => typeof key === "number" ? `[${key}]` : `.${key}`).join("")}: ${message}`, { cause });
}
function byteValue(value: object): string | undefined {
if (ArrayBuffer.isView(value)) {
if (isUint8Array(value)) {
try { return base64(value); } catch (cause) { fail("detached or out-of-bounds Uint8Array", cause); }
}
fail("unsupported byte storage or view; use Uint8Array");
}
const prototype: object | null = Object.getPrototypeOf(value);
if (isRawArrayBuffer(value, prototype)) fail("unsupported byte storage or view; use Uint8Array");
if (isProxiedByteStorage(value, prototype)) {
fail("proxied byte storage or view; pass the Uint8Array itself");
}
return undefined;
}
function serialize(original: unknown, key: string): unknown {
let value = original;
// Reject slot-less byte receivers before invoking methods such as Buffer.toJSON.
if (typeof original === "object" && original !== null && !ArrayBuffer.isView(original)) {
const prototype: object | null = Object.getPrototypeOf(original);
if (!isRawArrayBuffer(original, prototype) && isProxiedByteStorage(original, prototype)) {
fail("proxied byte storage or view; pass the Uint8Array itself");
}
}
if ((typeof value === "object" && value !== null) || typeof value === "function" || typeof value === "bigint") {
const toJSON: unknown = (value as { toJSON?: unknown }).toJSON;
if (typeof toJSON === "function") value = Reflect.apply(toJSON, value, [key]);
if (typeof original === "object" && original !== null) {
const bytes = byteValue(original);
if (bytes !== undefined) return bytes;
}
}
if (typeof value === "object" && value !== null) {
if (value !== original) {
const bytes = byteValue(value);
if (bytes !== undefined) return bytes;
}
value = unboxJson(value);
}
if (typeof value === "function" || typeof value === "symbol") return undefined;
if (typeof value === "bigint") throw new TypeError("Do not know how to serialize a BigInt");
if (typeof value !== "object" || value === null) return value;
if (ancestors.has(value)) throw new TypeError("Converting circular structure to JSON");
ancestors.add(value);
if (Array.isArray(value)) {
const length = Math.min(Math.max(Math.trunc(+(value.length as number)), 0) || 0, Number.MAX_SAFE_INTEGER);
const output: unknown[] = new Array(length);
Object.setPrototypeOf(output, null);
for (let i = 0; i < length; i++) {
path.push(i);
output[i] = serialize(value[i], String(i));
path.pop();
}
ancestors.delete(value);
return output;
}
const output = Object.create(null) as Record<string, unknown>;
for (const name of Object.keys(value)) {
path.push(name);
const item = serialize((value as Record<string, unknown>)[name], name);
path.pop();
output[name] = item;
}
ancestors.delete(value);
return output;
}
return JSON.stringify(serialize(input, ""));
}

/** JSON.stringify owns traversal and toJSON behavior; bytes convert independently of the schema. */
function jsonReplacer() {
const parents = new WeakMap<object, { holder: object; key: string }>();
function unsupported(holder: object, key: string): never {
function unsupported(holder: object, key: string, proxied = false, cause?: unknown): never {
let path = "";
for (let parent = parents.get(holder); parent; parent = parents.get(holder)) {
path = (Array.isArray(holder) ? `[${key}]` : `.${key}`) + path;
holder = parent.holder;
key = parent.key;
}
throw new EncodeError("json", `$${path}: unsupported byte storage or view; use Uint8Array`);
const message = cause !== undefined ? "detached or out-of-bounds Uint8Array"
: proxied ? "proxied byte storage or view; pass the Uint8Array itself"
: "unsupported byte storage or view; use Uint8Array";
throw new EncodeError("json", `$${path}: ${message}`, { cause });
}
return function (this: object, key: string, value: unknown): unknown {
// toJSON runs first, so inspect the original as well to recognize Buffer and byte subclasses.
const original: unknown = Reflect.get(this, key);
if (typeof original === "object" && original !== null && original !== value) {
if (ArrayBuffer.isView(original)) {
if (isUint8Array(original)) return base64(original);
if (isUint8Array(original)) {
try { return base64(original); } catch (cause) { unsupported(this, key, false, cause); }
}
unsupported(this, key);
}
if (isRawArrayBuffer(original)) unsupported(this, key);
const prototype: object | null = Object.getPrototypeOf(original);
if (isRawArrayBuffer(original, prototype)) unsupported(this, key);
if (isProxiedByteStorage(original, prototype)) unsupported(this, key, true);
}
if (typeof value === "object" && value !== null) {
if (ArrayBuffer.isView(value)) {
if (isUint8Array(value)) return base64(value);
if (isUint8Array(value)) {
try { return base64(value); } catch (cause) { unsupported(this, key, false, cause); }
}
unsupported(this, key);
}
if (isRawArrayBuffer(value)) unsupported(this, key);
const prototype: object | null = Object.getPrototypeOf(value);
if (isRawArrayBuffer(value, prototype)) unsupported(this, key);
if (isProxiedByteStorage(value, prototype)) unsupported(this, key, true);
parents.set(value, { holder: this, key });
}
return value;
Expand All @@ -416,9 +563,11 @@ function parseJson<T>(text: string, registry: CodecRegistry<T>): T {
function jsonBody(value: unknown): string {
let body: string | undefined;
try {
const needsReplacer = typeof value === "object" && value !== null
? mayContainBytes(value) : typeof value === "function" || typeof value === "bigint";
body = needsReplacer ? JSON.stringify(value, jsonReplacer()) : JSON.stringify(value);
const state = { proxy: false };
const needsConversion = typeof value === "object" && value !== null
? mayContainBytes(value, state) : typeof value === "function" || typeof value === "bigint";
body = state.proxy ? stringifyWithBytes(value)
: needsConversion ? JSON.stringify(value, jsonReplacer()) : JSON.stringify(value);
} catch (cause) {
if (isInstance(cause, EncodeError) && cause.code === "json") throw cause;
throw new EncodeError("json", `$: ${messageOf(cause, "value could not be encoded as JSON")}`, { cause });
Expand All @@ -427,7 +576,11 @@ function jsonBody(value: unknown): string {
return body;
}

/** Encode one value according to a negotiation decision; JSON bytes are schema-independent. */
/**
* Encode one value according to a negotiation decision; JSON bytes are schema-independent.
* On the JSON fallback path an accessor property may be read twice; a getter that
* returns a different value per read serializes the later one.
*/
export function encodeFor<T>(decision: Negotiation<T>, value: NoInfer<T>, options: EncodeOptions = {}): Encoded {
if (decision.kind === "hyperfly") {
try {
Expand Down Expand Up @@ -735,6 +888,8 @@ export interface RespondOptions extends NegotiateOptions, EncodeOptions {
* Answer one request with `value`, in binary when the client can read it. Fetch-API
* glue: one function covers Hono, Cloudflare Workers, Bun.serve, Deno and Next route
* handlers, because they all speak Request and Response.
* On the JSON fallback path an accessor property may be read twice; a getter that
* returns a different value per read serializes the later one.
*/
export function respond<T>(
request: Request,
Expand Down
Loading
Loading