Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ jobs:
# viewer: diff-independent, since a break can arrive through the relay,
# js/net, or the wire between them.
# `gh test` exercises workflow and release tooling, which no pull request runs.
recipe: ["rs doctest --workspace", "rs loom", "test drill-sensitivity", "rs uring", "test drain", "gh test"]
recipe: ["rs doctest --workspace", "rs loom", "test drill-sensitivity", "rs uring", "test drain", "gh test", "test json-properties"]
steps:
- name: Free disk space
uses: jlumbroso/free-disk-space@ceedf095f4ec1a097402bc6bd80831f2e1a6fde6 # main
Expand Down
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions js/json/bench/codec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** Sweep snapshot codec cost over document size and changed keys. */
import { Decoder } from "../src/snapshot/decoder.ts";
import { Encoder } from "../src/snapshot/encoder.ts";

const updates = 256;
console.log("keys\tchanged\tcompression\tms/update");
for (const keys of [1, 10, 100, 1000]) {
for (const changed of new Set([1, Math.min(10, keys)])) {
for (const compression of ["none", "deflate"] as const) {
const samples: number[] = [];
for (let sample = 0; sample < 3; sample++) {
const config = { deltaRatio: 100, compression };
const encoder = new Encoder<Record<string, number>>(config);
const decoder = new Decoder<Record<string, number>>(config);
let value = Object.fromEntries(Array.from({ length: keys }, (_, n) => [`k${n}`, 0]));
const start = performance.now();
for (let n = 1; n <= updates; n++) {
value = { ...value };
for (let key = 0; key < changed; key++) value[`k${key}`] = n;
const frame = encoder.update(value);
if (frame) {
if (frame.keyframe) decoder.snapshot(frame.payload);
else decoder.delta(frame.payload);
frame.commit();
}
if (decoder.decode()?.k0 !== n) throw new Error("codec benchmark lost an update");
}
samples.push((performance.now() - start) / updates);
}
samples.sort((a, b) => a - b);
console.log(`${keys}\t${changed}\t${compression}\t${samples[1]?.toFixed(6)}`);
}
}
}
5 changes: 5 additions & 0 deletions js/json/bench/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../tsconfig.json",
"compilerOptions": { "rootDir": "..", "noEmit": true },
"include": ["./*.ts"]
}
1 change: 1 addition & 0 deletions js/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"devDependencies": {
"@types/bun": "^1.4.2",
"fast-check": "^4.10.2",
"rimraf": "^6.1.3",
"typescript": "7.0.2"
}
Expand Down
10 changes: 10 additions & 0 deletions js/json/src/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ test("deepEqual", () => {
expect(deepEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false);
expect(deepEqual([1, 2], [1, 2, 3])).toBe(false);
});

// JSON.parse creates an own data field; merging must preserve that representation.
test("prototype-named JSON keys stay own data fields", () => {
const patch = JSON.parse('{"__proto__":{"x":1}}');
const result = merge({}, patch) as Record<string, unknown>;
expect(Object.getPrototypeOf(result)).toBe(Object.prototype);
expect(Object.hasOwn(result, "__proto__")).toBe(true);
expect(Object.getOwnPropertyDescriptor(result, "__proto__")?.value).toEqual({ x: 1 });
expect(deepEqual(JSON.parse('{"__proto__":{}}'), { a: {} })).toBe(false);
});
23 changes: 16 additions & 7 deletions js/json/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ function isObject(value: unknown): value is Obj {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

// Assignment to __proto__ invokes an inherited setter; JSON requires an own data field.
function set(object: Obj, key: string, value: unknown): void {
if (key === "__proto__") {
Object.defineProperty(object, key, { value, enumerable: true, configurable: true, writable: true });
} else {
object[key] = value;
}
}

/**
* Generate an RFC 7396 merge patch transforming `oldVal` into `newVal`.
*
Expand All @@ -36,21 +45,21 @@ export function diff(oldVal: unknown, newVal: unknown): Diff {
function diffObjects(oldObj: Obj, newObj: Obj, patch: Obj, forced: { value: boolean }): void {
// Keys present in old but missing from new become explicit null deletions.
for (const key of Object.keys(oldObj)) {
if (!(key in newObj)) patch[key] = null;
if (!Object.hasOwn(newObj, key)) set(patch, key, null);
}

for (const key of Object.keys(newObj)) {
const newV = newObj[key];
const oldV = oldObj[key];
const inOld = key in oldObj;
const inOld = Object.hasOwn(oldObj, key);
const oldV = inOld ? oldObj[key] : undefined;

if (inOld && deepEqual(oldV, newV)) continue;

// Recurse into nested objects so unchanged sibling keys stay out of the patch.
if (isObject(oldV) && isObject(newV)) {
const sub: Obj = {};
diffObjects(oldV, newV, sub, forced);
if (Object.keys(sub).length > 0) patch[key] = sub;
if (Object.keys(sub).length > 0) set(patch, key, sub);
continue;
}

Expand All @@ -60,7 +69,7 @@ function diffObjects(oldObj: Obj, newObj: Obj, patch: Obj, forced: { value: bool
if (newV === null || hasNullMember(newV)) {
forced.value = true;
}
patch[key] = newV;
set(patch, key, newV);
}
}

Expand All @@ -84,7 +93,7 @@ export function merge(target: unknown, patch: unknown): unknown {
if (value === null) {
delete base[key];
} else {
base[key] = merge(base[key], value);
set(base, key, merge(Object.hasOwn(base, key) ? base[key] : undefined, value));
}
}
return base;
Expand All @@ -101,7 +110,7 @@ export function deepEqual(a: unknown, b: unknown): boolean {
if (isObject(a) && isObject(b)) {
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) return false;
return keys.every((key) => key in b && deepEqual(a[key], b[key]));
return keys.every((key) => Object.hasOwn(b, key) && deepEqual(a[key], b[key]));
}

return false;
Expand Down
81 changes: 81 additions & 0 deletions js/json/src/snapshot/properties.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { expect, test } from "bun:test";
import fc from "fast-check";
import { Decoder } from "./decoder.ts";
import { Encoder } from "./encoder.ts";

type Json = null | boolean | number | string | Json[] | { [key: string]: Json };
type Step = { value: Json; reset: boolean };

// Mirror the Rust generator: bounded JSON trees plus edits to a persistent
// object, so sequences exercise deltas as well as unrelated snapshot roots.
const key = fc.oneof(
fc.constantFrom("a", "b", "c", "", "__proto__", "constructor", "prototype", "\0", "雪"),
fc.string({ maxLength: 8 }),
);
const leaf: fc.Arbitrary<Json> = fc.oneof(
fc.constant(null),
fc.boolean(),
fc.integer({ min: -1_000_000, max: 1_000_000 }).map((n) => n / 4),
fc.oneof(fc.string({ maxLength: 16 }), fc.constant("雪\0")),
);
const value: fc.Arbitrary<Json> = fc.letrec<{ value: Json }>((tie) => ({
value: fc.oneof(
{ maxDepth: 4, depthSize: "small", withCrossShrink: true },
leaf,
fc.array(tie("value"), { maxLength: 4 }),
fc.dictionary(key, tie("value"), { maxKeys: 4 }),
),
})).value;
const reset = fc.integer({ min: 0, max: 15 }).map((n) => n === 0);
const roots = fc.array(fc.record({ value, reset }), { minLength: 1, maxLength: 32 });
const edits = fc
.array(fc.record({ key, value, remove: fc.boolean(), reset }), { minLength: 1, maxLength: 32 })
.map((edits): Step[] => {
let current: Record<string, Json> = {};
return edits.map(({ key, value, remove, reset }) => {
const next = { ...current };
if (remove) delete next[key];
else Object.defineProperty(next, key, { value, enumerable: true, configurable: true, writable: true });
current = next;
return { value: next, reset };
});
});

function setting(name: string, fallback: number, min: number): number {
const raw = process.env[name];
if (raw !== undefined && !/^\d+$/.test(raw)) throw new Error(`${name} must be an integer`);
const value = Number(raw ?? fallback);
if (!Number.isInteger(value) || value < min || value > 0x7fffffff) {
throw new Error(`${name} must be an integer between ${min} and 2147483647`);
}
return value;
}

test("snapshot codec property reconstructs each JSON value", () => {
const seed = setting("MOQ_JSON_SEED", 0x6d6f71, 0);
const numRuns = setting("MOQ_JSON_CASES", 256, 1);
fc.assert(
fc.property(
fc.constantFrom(0, 1, 2, 8, 100),
fc.boolean(),
fc.oneof(roots, edits),
(deltaRatio, deflate, steps) => {
const config = { deltaRatio, compression: deflate ? ("deflate" as const) : ("none" as const) };
const encoder = new Encoder<Json>(config);
const decoder = new Decoder<Json>(config);
for (const { value, reset } of steps) {
if (reset) encoder.reset();
const frame = encoder.update(value);
if (frame) {
if (frame.keyframe) decoder.snapshot(frame.payload);
else decoder.delta(frame.payload);
frame.commit();
}
// JSON serialization defines the domain, including its normalization.
expect(decoder.decode()).toEqual(JSON.parse(JSON.stringify(value)));
}
},
),
{ seed, numRuns },
);
});
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ ranked just below deterministic TS legs, which it requires.

- [Binding audio delay](/quest/m2/binding-surface.md) - moq-ffi and every wrapper configure and observe audio playout delay
- [Drill mutation gate](/quest/m2/drill-mutation-gate.md) - a PR touching a drill mutation's target runs that mutation's sensitivity check, advisory until the drills run on controlled time
- [Snapshot properties](/quest/m2/json-snapshot-properties.md) - random value sequences through the JS and Rust Snapshot codecs always decode to the published value
- [Window properties](/quest/m2/json-window-properties.md) - random push/pop sequences through the JS and Rust Window codecs decode to the producer's window, for readers that keep up or join late
- [Pipelined requests](/quest/m2/pipeline-requests/README.md) - SUBSCRIBE and the first FETCH go out with the track-info request at every hop, so first data arrives a round trip sooner per hop
- [OBS native codecs](/quest/m2/obs-moq-video/README.md) - replace FFmpeg video and audio decoding with moq-video and moq-audio, deliver GPU frames, and use native audio/video encoders
Expand Down
48 changes: 0 additions & 48 deletions quest/m2/json-snapshot-properties.md

This file was deleted.

4 changes: 2 additions & 2 deletions quest/m2/json-window-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ to the codec itself unless the test finds a bug.
## Plan

Decided 2026-10-14, alongside
[Snapshot codec property tests](/quest/m2/json-snapshot-properties.md),
[Snapshot codec property tests](/test/json-properties.md),
which carries the reasoning for each choice:

- **Codec sequences** through `Encoder` -> `Decoder`, per implementation, at
Expand All @@ -37,4 +37,4 @@ which carries the reasoning for each choice:

## Related

- [Snapshot codec property tests](/quest/m2/json-snapshot-properties.md) - the same treatment for the Snapshot codec
- [Snapshot codec property tests](/test/json-properties.md) - the same treatment for the Snapshot codec
Loading
Loading