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 js/json/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Diff {

type Obj = Record<string, unknown>;

function isObject(value: unknown): value is Obj {
export function isObject(value: unknown): value is Obj {
return typeof value === "object" && value !== null && !Array.isArray(value);
}

Expand Down
28 changes: 28 additions & 0 deletions js/json/src/snapshot/codec.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, test } from "bun:test";
import { DEFAULT_MAX_FRAME_SIZE, Encoder as Flate } from "@moq/flate";
import { MissingSnapshot } from "../error.ts";
import { Decoder } from "./decoder.ts";
import { type Config, Encoder } from "./encoder.ts";
Expand Down Expand Up @@ -151,6 +152,33 @@ test("a delta before a snapshot throws", () => {
});

// A backlog is applied in full but materialized once: the intermediate reconstructions are stale.
test("compressed patches cannot grow the value past the frame cap", () => {
// Each frame inflates under the per-frame cap, but the patches together would not.
const decoder = new Decoder<Doc>({ compression: "deflate" });
const flate = new Flate();
const frame = (value: Doc) => flate.frame(new TextEncoder().encode(JSON.stringify(value)));
const big = "x".repeat(DEFAULT_MAX_FRAME_SIZE / 2);

decoder.snapshot(frame({ a: big }));
// Past the running bound, but the deletion leaves the value itself small enough.
decoder.delta(frame({ a: null, b: big }));
expect(Object.keys(decoder.decode() ?? {})).toEqual(["b"]);

expect(() => decoder.delta(frame({ c: big }))).toThrow(`${DEFAULT_MAX_FRAME_SIZE}`);
expect(decoder.decode()).toBeUndefined();
});

test("a __proto__ patch key stays a member", () => {
const decoder = new Decoder<Doc>();
const json = (text: string) => new TextEncoder().encode(text);
decoder.snapshot(json(`{"a":{}}`));
decoder.delta(json(`{"__proto__":{"polluted":true},"a":{"__proto__":{"polluted":true}}}`));
expect(({} as Doc).polluted).toBeUndefined();
expect(JSON.stringify(decoder.decode())).toBe(
`{"a":{"__proto__":{"polluted":true}},"__proto__":{"polluted":true}}`,
);
});

test("frames apply without materializing", () => {
const encoder = new Encoder<Doc>({ deltaRatio: 100 });
const decoder = new Decoder<Doc>({});
Expand Down
80 changes: 72 additions & 8 deletions js/json/src/snapshot/decoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Decoder as Flate } from "@moq/flate";
import { DEFAULT_MAX_FRAME_SIZE, Decoder as Flate } from "@moq/flate";

import { isDeflate } from "../compression.ts";
import { merge } from "../diff.ts";
import { isObject } from "../diff.ts";
import { MissingSnapshot } from "../error.ts";
import type { Config } from "./encoder.ts";

Expand All @@ -26,6 +26,11 @@ export class Decoder<T> {
#flate?: Flate;
// The reconstructed value, `undefined` until the first snapshot.
#current?: unknown;
// The reconstructed value's size in JSON bytes: the snapshot's own bytes, then moved by what each
// patch adds, replaces, or removes (see {@link apply}). The snapshot is not re-serialized, so a
// number printed longer than its source (`9e9`) goes uncounted, but the value's memory follows
// its nodes, which its bytes bound all the same.
#size = 0;

constructor(config: Pick<Config<T>, "schema" | "compression"> = {}) {
this.#schema = config.schema;
Expand All @@ -41,17 +46,27 @@ export class Decoder<T> {
snapshot(payload: Uint8Array): void {
// Each group is its own compressed stream, so the window starts cold here.
this.#flate = this.#decompress ? new Flate() : undefined;
this.#current = this.#parse(payload);
const plain = this.#inflate(payload);
this.#current = this.#parse(plain);
this.#size = plain.byteLength;
Comment on lines +50 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Measure normalized JavaScript snapshots before accepting them

When a snapshot contains compact exponent-form numbers such as repeated 9e9, JSON.parse normalizes them and JSON.stringify emits the much longer 9000000000. Recording only the wire length therefore accepts a snapshot whose reconstructed compact JSON exceeds DEFAULT_MAX_FRAME_SIZE; if no delta follows, the fallback measurement never runs, and decode() can additionally clone this oversized state. Measure the parsed value here and reject it when it exceeds the cap.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declining; f966ce3 documents what the bound counts instead. The bound exists to cap the memory and work a peer can make the decoder hold, and that follows the parsed value's nodes, not how many digits re-serializing prints for each number. A merge patch adds no more nodes than its own bytes can hold, so the source bytes bound the value's memory as well as its compact JSON does: 9e9 and 9000000000 are the same single number node. Measuring every snapshot would add a full walk per group (per update with deltas off) to catch a cosmetic ~5x on exponent-form numbers that never reaches memory. The value is still measured as compact JSON once the running count passes the limit.

(Written by Claude Opus 5.5)

}

/**
* Apply one of a group's later frames: an RFC 7396 merge patch against the current value.
*
* Throws when no snapshot has been applied yet, since a patch has nothing to apply to.
* Throws when no snapshot has been applied yet, since a patch has nothing to apply to, or when
* the result would outgrow the largest snapshot a producer publishes. A refused patch drops the
* value until the next snapshot.
*/
delta(payload: Uint8Array): void {
if (this.#current === undefined) throw new MissingSnapshot();
this.#current = merge(this.#current, this.#parse(payload));
const [value, growth] = apply(this.#current, this.#parse(this.#inflate(payload)));
this.#current = value;
this.#size += growth;
if (this.#size > DEFAULT_MAX_FRAME_SIZE) {
this.#current = undefined;
throw new Error(`reconstructed value exceeded ${DEFAULT_MAX_FRAME_SIZE} bytes`);
}
}

/**
Expand All @@ -76,9 +91,58 @@ export class Decoder<T> {
return this.#schema ? this.#schema.parse(this.#current) : (structuredClone(this.#current) as T);
}

// Decompress a frame against the group's window when compressing, then parse it as JSON.
#parse(payload: Uint8Array): unknown {
const plain = this.#flate ? this.#flate.frame(payload) : payload;
// Decompress a frame against the group's window when compressing.
#inflate(payload: Uint8Array): Uint8Array {
return this.#flate ? this.#flate.frame(payload) : payload;
}

#parse(plain: Uint8Array): unknown {
return JSON.parse(new TextDecoder().decode(plain));
}
}

const utf8 = new TextEncoder();

// Bytes in a value's JSON, or none for an absent one.
function bytes(value: unknown): number {
return value === undefined ? 0 : utf8.encode(JSON.stringify(value)).byteLength;
}

// Apply an RFC 7396 merge patch, mutating `target` where it can, and return the result with how many
// JSON bytes it grew. Only the patch and what it replaces or removes are measured, never the
// untouched rest, so the cost follows the frame rather than the value it lands on. A member counts its
// key, value, colon, and comma, without the O(n) scan for whether an object has other members, so
// the count can be off from the compact JSON by a byte per object but never drifts as members come
// and go.
function apply(target: unknown, patch: unknown): [unknown, number] {
if (!isObject(patch)) return [patch, bytes(patch) - bytes(target)];

let growth = 0;
let object: Record<string, unknown>;
if (isObject(target)) {
object = target;
} else {
object = {};
growth += 2 - bytes(target);
}

for (const [key, value] of Object.entries(patch)) {
const present = Object.hasOwn(object, key);
if (value === null) {
if (!present) continue;
growth -= bytes(key) + bytes(object[key]) + 2;
delete object[key];
} else if (present) {
const [next, grown] = apply(object[key], value);
object[key] = next;
growth += grown;
} else {
const [next, grown] = apply(undefined, value);
// Defined rather than assigned, so a `__proto__` key becomes a member instead of the
// object's prototype.
Object.defineProperty(object, key, { value: next, enumerable: true, writable: true, configurable: true });
growth += bytes(key) + 2 + grown;
}
}
return [object, growth];
}
8 changes: 5 additions & 3 deletions rs/moq-json/benches/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,15 @@ fn snapshot_budget(c: &mut Criterion) {
.map(|i| (format!("path-{i}"), json!({"bytes": 123})))
.collect();
let bytes = serde_json::to_vec(&value).unwrap();
for bounded in [false, true] {
// A value right at its limit is the worst case: every patch lands on the boundary, so a
// decoder that measured the whole value there would slope with `entries`.
for at_limit in [false, true] {
group.bench_with_input(
BenchmarkId::new(if bounded { "bounded" } else { "unbounded" }, entries),
BenchmarkId::new(if at_limit { "at_limit" } else { "default" }, entries),
&bytes,
|b, bytes| {
let mut config = moq_json::snapshot::consumer::Config::default();
config.max_size = bounded.then_some(8 * 1024 * 1024);
config.max_size = at_limit.then_some(bytes.len());
let mut decoder = moq_json::snapshot::Decoder::<Value>::new(config);
decoder.snapshot(bytes).unwrap();
b.iter(|| decoder.delta(black_box(br#"{"path-0":{"bytes":124}}"#)).unwrap());
Expand Down
107 changes: 85 additions & 22 deletions rs/moq-json/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ use serde_json::{Map, Value};

/// Validate the complete frame before changing the retained value, then apply its patch in place.
/// Existing object keys and numeric values are reused instead of building a patch tree.
pub(crate) fn apply_bytes(target: &mut Value, bytes: &[u8], scratch: &RefCell<CheckScratch>) -> serde_json::Result<()> {
///
/// Returns how much the target's compact JSON grew (negative when it shrank). Only the patch and
/// whatever it replaces or removes are measured, never the untouched rest of the value, so a peer
/// pays for every byte it makes the caller count.
pub(crate) fn apply_bytes(
target: &mut Value,
bytes: &[u8],
scratch: &RefCell<CheckScratch>,
) -> serde_json::Result<i64> {
check(bytes, scratch)?;
apply_generated_bytes(target, bytes)
let growth = Cell::new(0);
apply(target, bytes, Some(&growth))?;
Ok(growth.get())
}

/// Validate one JSON value's syntax, rejecting any object that repeats a key.
Expand All @@ -19,17 +29,40 @@ pub(crate) fn check(bytes: &[u8], scratch: &RefCell<CheckScratch>) -> serde_json

/// Apply a patch emitted by our serializer, whose keys and syntax were already checked.
pub(crate) fn apply_generated_bytes(target: &mut Value, bytes: &[u8]) -> serde_json::Result<()> {
apply(target, bytes, None)
}

fn apply(target: &mut Value, bytes: &[u8], growth: Option<&Cell<i64>>) -> serde_json::Result<()> {
use serde::de::DeserializeSeed;
let mut decoder = serde_json::Deserializer::from_slice(bytes);
Patch {
target,
deletion: false,
growth,
}
.deserialize(&mut decoder)?;
Ok(())
}

use std::cell::RefCell;
/// Bytes in `value`'s compact JSON.
fn len(value: &(impl serde::Serialize + ?Sized)) -> i64 {
struct Count(i64);
impl std::io::Write for Count {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.0 += bytes.len() as i64;
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let mut count = Count(0);
// Neither a `Value` nor a `str` fails to serialize, and counting never fails to write.
let _ = serde_json::to_writer(&mut count, value);
count.0
}

use std::cell::{Cell, RefCell};

#[derive(Default)]
pub(crate) struct CheckScratch {
Expand Down Expand Up @@ -163,6 +196,24 @@ impl<'de> serde::Deserialize<'de> for Key<'de> {
struct Patch<'a> {
target: &'a mut Value,
deletion: bool,
/// Accumulates the change in the target's compact JSON size, when the caller tracks it.
growth: Option<&'a Cell<i64>>,
}

impl Patch<'_> {
/// Replace the target wholesale, measuring only the old and new values.
fn replace(self, value: Value) -> bool {
grow(self.growth, || len(&value) - len(self.target));
*self.target = value;
false
}
}

/// Add to the running growth, measuring nothing when no caller tracks it.
fn grow(growth: Option<&Cell<i64>>, by: impl FnOnce() -> i64) {
if let Some(growth) = growth {
growth.set(growth.get() + by());
}
}

impl<'de> serde::de::DeserializeSeed<'de> for Patch<'_> {
Expand All @@ -184,8 +235,7 @@ impl<'de> serde::de::Visitor<'de> for Patch<'_> {
if self.deletion {
Ok(true)
} else {
*self.target = Value::Null;
Ok(false)
Ok(self.replace(Value::Null))
}
}

Expand All @@ -194,46 +244,41 @@ impl<'de> serde::de::Visitor<'de> for Patch<'_> {
}

fn visit_bool<E: serde::de::Error>(self, value: bool) -> Result<bool, E> {
*self.target = Value::Bool(value);
Ok(false)
Ok(self.replace(Value::Bool(value)))
}

fn visit_i64<E: serde::de::Error>(self, value: i64) -> Result<bool, E> {
*self.target = Value::from(value);
Ok(false)
Ok(self.replace(Value::from(value)))
}

fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<bool, E> {
*self.target = Value::from(value);
Ok(false)
Ok(self.replace(Value::from(value)))
}

fn visit_f64<E: serde::de::Error>(self, value: f64) -> Result<bool, E> {
*self.target = Value::from(value);
Ok(false)
Ok(self.replace(Value::from(value)))
}

fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<bool, E> {
*self.target = Value::from(value);
Ok(false)
Ok(self.replace(Value::from(value)))
}

fn visit_string<E: serde::de::Error>(self, value: String) -> Result<bool, E> {
*self.target = Value::String(value);
Ok(false)
Ok(self.replace(Value::String(value)))
}

fn visit_seq<A: serde::de::SeqAccess<'de>>(self, mut seq: A) -> Result<bool, A::Error> {
let mut values = Vec::with_capacity(seq.size_hint().unwrap_or(0));
while let Some(value) = seq.next_element()? {
values.push(value);
}
*self.target = Value::Array(values);
Ok(false)
Ok(self.replace(Value::Array(values)))
}

fn visit_map<A: serde::de::MapAccess<'de>>(self, mut map: A) -> Result<bool, A::Error> {
let growth = self.growth;
if !self.target.is_object() {
grow(growth, || 2 - len(self.target));
*self.target = Value::Object(Map::new());
}
let object = self.target.as_object_mut().expect("target is an object");
Expand All @@ -242,16 +287,25 @@ impl<'de> serde::de::Visitor<'de> for Patch<'_> {
let remove = map.next_value_seed(Patch {
target: value,
deletion: true,
growth,
})?;
if remove {
object.remove(key.as_ref());
if remove && let Some(value) = object.remove(key.as_ref()) {
// The member, its colon, and the comma that separated it from a remaining one.
grow(growth, || {
-(len(key.as_ref()) + 1 + len(&value) + i64::from(!object.is_empty()))
});
}
} else {
// Measured whole once inserted, so the placeholder it starts from is never counted.
let mut value = Value::Null;
if !map.next_value_seed(Patch {
target: &mut value,
deletion: true,
growth: None,
})? {
grow(growth, || {
len(key.as_ref()) + 1 + len(&value) + i64::from(!object.is_empty())
});
object.insert(key.into_owned(), value);
}
}
Expand All @@ -275,16 +329,25 @@ mod test {
(json!({"old": 1}), json!({"new": {"absent": null, "kept": 1}})),
(json!(1), json!({"nested": {"absent": null, "kept": 1}})),
(json!({"old": 1}), json!([1, 2, 3])),
(json!({"only": [1, 2]}), json!({"only": null, "absent": null})),
(
json!({"a": "x", "b": 1e15}),
json!({"b": null, "a": {"esc\"aped": 9e9}}),
),
(json!({"a": {"b": 1}}), json!({"a": "flat"})),
] {
let mut expected = target.clone();
json_patch::merge(&mut expected, &patch);
apply_bytes(
let before = len(&target);
let growth = apply_bytes(
&mut target,
serde_json::to_string(&patch).unwrap().as_bytes(),
&RefCell::new(CheckScratch::default()),
)
.unwrap();
assert_eq!(target, expected);
// Counted from the touched members alone, yet exact for the whole value.
assert_eq!(before + growth, len(&target), "{patch}");
}
}

Expand Down
Loading
Loading