diff --git a/js/hang/api/catalog.api.md b/js/hang/api/catalog.api.md index 51f489cbf7e..46176901ff9 100644 --- a/js/hang/api/catalog.api.md +++ b/js/hang/api/catalog.api.md @@ -20,7 +20,6 @@ import { ZodMiniNumber } from "zod/mini"; import { ZodMiniObject } from "zod/mini"; import { ZodMiniOptional } from "zod/mini"; import { ZodMiniPipe } from "zod/mini"; -import { ZodMiniRecord } from "zod/mini"; import { ZodMiniString } from "zod/mini"; import { ZodMiniTransform } from "zod/mini"; import { ZodMiniType } from "zod/mini"; @@ -132,16 +131,17 @@ export const BinaryConfigSchema: ZodMiniObject<{ }, $loose>; export const BinarySchema: ZodMiniObject<{ - tracks: ZodMiniRecord, ZodMiniObject<{ - bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; - broadcast: ZodMiniOptional>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - instance: ZodMiniOptional>>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - mime: ZodMiniOptional>; - mode: ZodMiniString; - track: ZodMiniOptional>; - }, $loose>>; + tracks: ZodMiniPipe, Record>, ZodMiniTransform | undefined; + broadcast?: RelativeBroadcast | undefined; + delay?: number & $brand<"u53"> | undefined; + instance?: ReferenceInstance | undefined; + jitter?: number & $brand<"u53"> | undefined; + mime?: string | undefined; + mode: string; + track?: string | undefined; + }>, Record>>; }, $strip>; export type Clock = infer; @@ -242,16 +242,17 @@ export const JsonConfigSchema: ZodMiniObject<{ }, $loose>; export const JsonSchema: ZodMiniObject<{ - tracks: ZodMiniRecord, ZodMiniObject<{ - bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; - broadcast: ZodMiniOptional>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - instance: ZodMiniOptional>>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - mode: ZodMiniString; - schema: ZodMiniOptional>; - track: ZodMiniOptional>; - }, $loose>>; + tracks: ZodMiniPipe, Record>, ZodMiniTransform | undefined; + broadcast?: RelativeBroadcast | undefined; + delay?: number & $brand<"u53"> | undefined; + instance?: ReferenceInstance | undefined; + jitter?: number & $brand<"u53"> | undefined; + mode: string; + schema?: string | undefined; + track?: string | undefined; + }>, Record>>; }, $strip>; export type KnownMode = "snapshot" | "stream"; @@ -323,74 +324,71 @@ export const RootSchema: ZodMiniObject<{ }>, Record>>; }, $strip>>; binary: ZodMiniOptional, ZodMiniObject<{ - bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; - broadcast: ZodMiniOptional>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - instance: ZodMiniOptional>>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - mime: ZodMiniOptional>; - mode: ZodMiniString; - track: ZodMiniOptional>; - }, $loose>>; + tracks: ZodMiniPipe, Record>, ZodMiniTransform | undefined; + broadcast?: RelativeBroadcast | undefined; + delay?: number & $brand<"u53"> | undefined; + instance?: ReferenceInstance | undefined; + jitter?: number & $brand<"u53"> | undefined; + mime?: string | undefined; + mode: string; + track?: string | undefined; + }>, Record>>; }, $strip>, ZodMiniPipe, ZodMiniTransform>]>>; clock: ZodMiniOptional>; wall: $ZodBranded, "u53", "out">; }, $strip>>; - contributions: ZodMiniOptional, ZodMiniObject<{ - broadcast: ZodMiniType>; - instance: ZodMiniOptional>>; - }, $strip>>>; + contributions: ZodMiniOptional, Record>, ZodMiniTransform, Record>>>; echo: ZodMiniOptional>; }, $strip>, ZodMiniPipe, ZodMiniTransform>]>>; json: ZodMiniOptional, ZodMiniObject<{ - bitrate: ZodMiniOptional<$ZodBranded, "u53", "out">>; - broadcast: ZodMiniOptional>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - instance: ZodMiniOptional>>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - mode: ZodMiniString; - schema: ZodMiniOptional>; - track: ZodMiniOptional>; - }, $loose>>; + tracks: ZodMiniPipe, Record>, ZodMiniTransform | undefined; + broadcast?: RelativeBroadcast | undefined; + delay?: number & $brand<"u53"> | undefined; + instance?: ReferenceInstance | undefined; + jitter?: number & $brand<"u53"> | undefined; + mode: string; + schema?: string | undefined; + track?: string | undefined; + }>, Record>>; }, $strip>, ZodMiniPipe, ZodMiniTransform>]>>; stats: ZodMiniOptional; }, $strip>, ZodMiniPipe, ZodMiniTransform>]>>; text: ZodMiniOptional, ZodMiniObject<{ - broadcast: ZodMiniOptional>>; - container: ZodMiniDefault; - }, $strip>, ZodMiniObject<{ - init: ZodMiniBase64; - kind: ZodMiniLiteral<"cmaf">; - }, $strip>, ZodMiniObject<{ - kind: ZodMiniLiteral<"loc">; - }, $strip>], "kind">, ZodMiniPipe; - }, $loose>, ZodMiniTransform<{ + renditions: ZodMiniPipe, Record>, ZodMiniTransform>]>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - format: ZodMiniString; - instance: ZodMiniOptional>>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - label: ZodMiniOptional>; - lang: ZodMiniOptional>; - role: ZodMiniDefault>; - track: ZodMiniOptional>; - }, $strip>>; + }; + delay?: number & $brand<"u53"> | undefined; + format: string; + instance?: ReferenceInstance | undefined; + jitter?: number & $brand<"u53"> | undefined; + label?: string | undefined; + lang?: string | undefined; + role: string; + track?: string | undefined; + }>, Record>>; }, $strip>, ZodMiniPipe, ZodMiniTransform>]>>; version: ZodMiniOptional>; video: ZodMiniOptional; export const TextRoleSchema: ZodMiniString; export const TextSchema: ZodMiniObject<{ - renditions: ZodMiniRecord, ZodMiniObject<{ - broadcast: ZodMiniOptional>>; - container: ZodMiniDefault; - }, $strip>, ZodMiniObject<{ - init: ZodMiniBase64; - kind: ZodMiniLiteral<"cmaf">; - }, $strip>, ZodMiniObject<{ - kind: ZodMiniLiteral<"loc">; - }, $strip>], "kind">, ZodMiniPipe; - }, $loose>, ZodMiniTransform<{ + renditions: ZodMiniPipe, Record>, ZodMiniTransform>]>>; - delay: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - format: ZodMiniString; - instance: ZodMiniOptional>>; - jitter: ZodMiniOptional, "u53", "out">, ZodMiniTransform | undefined, number & $brand<"u53">>>>; - label: ZodMiniOptional>; - lang: ZodMiniOptional>; - role: ZodMiniDefault>; - track: ZodMiniOptional>; - }, $strip>>; + }; + delay?: number & $brand<"u53"> | undefined; + format: string; + instance?: ReferenceInstance | undefined; + jitter?: number & $brand<"u53"> | undefined; + label?: string | undefined; + lang?: string | undefined; + role: string; + track?: string | undefined; + }>, Record>>; }, $strip>; export type Track = infer; diff --git a/js/hang/api/index.api.md b/js/hang/api/index.api.md index e04677d3467..302026db3b1 100644 --- a/js/hang/api/index.api.md +++ b/js/hang/api/index.api.md @@ -5,13 +5,12 @@ import { $ZodBranded } from "zod/v4/core"; import { $brand } from "zod/mini"; import { $loose } from "zod/v4/core"; import { $strip } from "zod/v4/core"; +import { ZodMiniCustom } from "zod/mini"; import { ZodMiniNullable } from "zod/mini"; import { ZodMiniNumber } from "zod/mini"; import { ZodMiniObject } from "zod/mini"; import { ZodMiniOptional } from "zod/mini"; import { ZodMiniPipe } from "zod/mini"; -import { ZodMiniRecord } from "zod/mini"; -import { ZodMiniString } from "zod/mini"; import { ZodMiniTransform } from "zod/mini"; import { ZodMiniType } from "zod/mini"; import { infer } from "zod/mini"; @@ -51,27 +50,21 @@ export namespace Echo { export const SUFFIX: ".echo"; export type Snapshot = infer; export const SnapshotSchema: ZodMiniObject<{ - renditions: ZodMiniOptional, ZodMiniObject<{ - arrival: ZodMiniOptional, "u53", "out">; - wall: $ZodBranded, "u53", "out">; - }, $strip>>, ZodMiniTransform<{ + renditions: ZodMiniOptional, Record>, ZodMiniTransform; wall: number & $brand<"u53">; - } | undefined, { - timestamp: number & $brand<"u53">; - wall: number & $brand<"u53">; - } | null>>>; - bytes: ZodMiniOptional<$ZodBranded, "u53", "out">>; - decoded: ZodMiniOptional<$ZodBranded, "u53", "out">>; - errors: ZodMiniOptional<$ZodBranded, "u53", "out">>; - frames: ZodMiniOptional<$ZodBranded, "u53", "out">>; - late: ZodMiniOptional<$ZodBranded, "u53", "out">>; - latency: ZodMiniOptional, "u53", "out">>, ZodMiniTransform | undefined, number & $brand<"u53"> | null>>>; - stalled: ZodMiniOptional<$ZodBranded, "u53", "out">>; - stalls: ZodMiniOptional<$ZodBranded, "u53", "out">>; - underruns: ZodMiniOptional<$ZodBranded, "u53", "out">>; - }, $strip>>>; + } | undefined; + bytes?: number & $brand<"u53"> | undefined; + decoded?: number & $brand<"u53"> | undefined; + errors?: number & $brand<"u53"> | undefined; + frames?: number & $brand<"u53"> | undefined; + late?: number & $brand<"u53"> | undefined; + latency?: number & $brand<"u53"> | undefined; + stalled?: number & $brand<"u53"> | undefined; + stalls?: number & $brand<"u53"> | undefined; + underruns?: number & $brand<"u53"> | undefined; + }>, Record>>>; transport: ZodMiniOptional, "u53", "out">>, ZodMiniTransform | undefined, number & $brand<"u53"> | null>>>; lostBytes: ZodMiniOptional, "u53", "out">>, ZodMiniTransform | undefined, number & $brand<"u53"> | null>>>; @@ -186,13 +179,13 @@ export namespace Stats { }, $strip>; export type Snapshot = infer; export const SnapshotSchema: ZodMiniObject<{ - renditions: ZodMiniOptional, ZodMiniObject<{ - bitrate: ZodMiniOptional, "u53", "out">>, ZodMiniTransform | undefined, number & $brand<"u53"> | null>>>; - bytes: ZodMiniOptional<$ZodBranded, "u53", "out">>; - frames: ZodMiniOptional<$ZodBranded, "u53", "out">>; - keyframes: ZodMiniOptional<$ZodBranded, "u53", "out">>; - skipped: ZodMiniOptional<$ZodBranded, "u53", "out">>; - }, $strip>>>; + renditions: ZodMiniOptional, Record>, ZodMiniTransform | undefined; + bytes?: number & $brand<"u53"> | undefined; + frames?: number & $brand<"u53"> | undefined; + keyframes?: number & $brand<"u53"> | undefined; + skipped?: number & $brand<"u53"> | undefined; + }>, Record>>>; transport: ZodMiniOptional, "u53", "out">>, ZodMiniTransform | undefined, number & $brand<"u53"> | null>>>; lostBytes: ZodMiniOptional, "u53", "out">>, ZodMiniTransform | undefined, number & $brand<"u53"> | null>>>; diff --git a/js/hang/src/catalog/binary.ts b/js/hang/src/catalog/binary.ts index 5b4d8fecc72..92707c230eb 100644 --- a/js/hang/src/catalog/binary.ts +++ b/js/hang/src/catalog/binary.ts @@ -2,6 +2,7 @@ import * as z from "zod/mini"; import { u53Schema } from "./integers"; import { ModeSchema } from "./mode"; import { InstanceSchema, RelativeBroadcastSchema } from "./path"; +import { renditions } from "./section"; /** * Schema for a single binary track: application data published as opaque payloads. @@ -64,7 +65,7 @@ export const BinaryConfigSchema = z.looseObject({ * key is the track name to subscribe to. */ export const BinarySchema = z.object({ - tracks: z.record(z.string(), BinaryConfigSchema), + tracks: renditions(BinaryConfigSchema), }); /** The catalog binary section: data tracks keyed by track name. */ diff --git a/js/hang/src/catalog/json.ts b/js/hang/src/catalog/json.ts index c697111d079..d3fa83b565c 100644 --- a/js/hang/src/catalog/json.ts +++ b/js/hang/src/catalog/json.ts @@ -2,6 +2,7 @@ import * as z from "zod/mini"; import { u53Schema } from "./integers"; import { ModeSchema } from "./mode"; import { InstanceSchema, RelativeBroadcastSchema } from "./path"; +import { renditions } from "./section"; /** * Schema for a single JSON track: application data published as a live JSON document or log. @@ -63,7 +64,7 @@ export const JsonConfigSchema = z.looseObject({ * key is the track name to subscribe to. */ export const JsonSchema = z.object({ - tracks: z.record(z.string(), JsonConfigSchema), + tracks: renditions(JsonConfigSchema), }); /** The catalog JSON section: data tracks keyed by track name. */ diff --git a/js/hang/src/catalog/parity.test.ts b/js/hang/src/catalog/parity.test.ts index 8b712eb51a5..9cb15ec7bfc 100644 --- a/js/hang/src/catalog/parity.test.ts +++ b/js/hang/src/catalog/parity.test.ts @@ -7,10 +7,14 @@ interface Case { refused?: boolean; video?: string[]; audio?: string[]; + text?: string[]; + json?: string[]; + binary?: string[]; + contributions?: string[]; } // The corpus rs/hang also loads: both languages accept the same catalogs and keep the same -// renditions, so a divergence fails CI on whichever side drifts. +// rendition, data track, and contribution IDs, so a divergence fails CI on whichever side drifts. const cases = (await Bun.file( new URL("../../../../rs/hang/fixtures/catalog-parity.json", import.meta.url), ).json()) as Case[]; @@ -28,5 +32,9 @@ for (const c of cases) { const root = parse(); expect(ids(root.video?.renditions)).toEqual(c.video ?? []); expect(ids(root.audio?.renditions)).toEqual(c.audio ?? []); + expect(ids(root.text?.renditions)).toEqual(c.text ?? []); + expect(ids(root.json?.tracks)).toEqual(c.json ?? []); + expect(ids(root.binary?.tracks)).toEqual(c.binary ?? []); + expect(ids(root.contributions)).toEqual(c.contributions ?? []); }); } diff --git a/js/hang/src/catalog/root.test.ts b/js/hang/src/catalog/root.test.ts index e67b0e713de..375975ef8f6 100644 --- a/js/hang/src/catalog/root.test.ts +++ b/js/hang/src/catalog/root.test.ts @@ -112,6 +112,9 @@ test("a contribution parses beside a same-ID rendition and a malformed one is re ]) { expect(RootSchema.safeParse({ contributions }).success).toBe(false); } + // The issue names a contribution, not a rendition. + const refused = RootSchema.safeParse({ contributions: { alice: {} } }); + expect(refused.error?.message).toContain("malformed contribution alice"); }); test("rendition parent broadcast reference stays distinct from empty", () => { diff --git a/js/hang/src/catalog/root.ts b/js/hang/src/catalog/root.ts index d1a241fda82..9dfbba8f5cd 100644 --- a/js/hang/src/catalog/root.ts +++ b/js/hang/src/catalog/root.ts @@ -11,7 +11,7 @@ import { TRACK } from "./format"; import { JsonSchema } from "./json"; import type { RelativeBroadcast } from "./path"; import { PRIORITY } from "./priority"; -import { named, section } from "./section"; +import { named, renditions, section } from "./section"; import { StatsSchema } from "./stats"; import { TextSchema } from "./text"; import { track } from "./track"; @@ -82,7 +82,7 @@ export const RootSchema = z.looseObject({ json: section(JsonSchema, "tracks"), binary: section(BinarySchema, "tracks"), // A new key, so a malformed entry fails the catalog, the same as Rust. - contributions: z.optional(z.record(z.string(), ContributionSchema)), + contributions: z.optional(renditions(ContributionSchema, "contribution")), }); /** The root catalog object: the media sections, the clock, the data track sections, plus any app extensions. */ diff --git a/js/hang/src/catalog/section.ts b/js/hang/src/catalog/section.ts index 90cca8003d1..f89236bd467 100644 --- a/js/hang/src/catalog/section.ts +++ b/js/hang/src/catalog/section.ts @@ -35,15 +35,17 @@ export function section(schema: T, map: string) { } /** - * A media section's renditions, refusing the catalog if any one is malformed. + * A catalog map keyed by ID (renditions, data tracks, contributions), refusing the catalog if any + * entry is malformed. * - * Each rendition must parse with `schema`; the issue names the rendition. An unknown container + * Each entry must parse with `schema`; the issue names the entry. An unknown container * or extension field is not malformed, so it never refuses the catalog. Mirrors the Rust * `deserialize_renditions`. * - * @param schema - one rendition's schema. + * @param schema - one entry's schema. + * @param kind - what an entry is, naming it in the issue (`rendition`, `contribution`). */ -export function renditions(schema: T) { +export function renditions(schema: T, kind = "rendition") { return z.pipe( // Not `z.record`, which would drop an ID like `__proto__` that JSON.parse keeps as its own. z.custom>( @@ -57,7 +59,7 @@ export function renditions(schema: T) { else ctx.issues.push({ code: "custom", - message: `malformed rendition ${id}: ${result.error.message}`, + message: `malformed ${kind} ${id}: ${result.error.message}`, input: value, }); } diff --git a/js/hang/src/catalog/text.ts b/js/hang/src/catalog/text.ts index f31fb81de07..d7dff58fff1 100644 --- a/js/hang/src/catalog/text.ts +++ b/js/hang/src/catalog/text.ts @@ -2,6 +2,7 @@ import * as z from "zod/mini"; import { ContainerSchema } from "./container"; import { u53Schema } from "./integers"; import { InstanceSchema, RelativeBroadcastSchema } from "./path"; +import { renditions } from "./section"; /** * The cue serialization format of a text track, mirroring the Rust `TextFormat`. @@ -84,7 +85,7 @@ export const TextConfigSchema = z.object({ export const TextSchema = z.object({ // A map of track name to rendition configuration. // This is not an array so it will work with JSON Merge Patch. - renditions: z.record(z.string(), TextConfigSchema), + renditions: renditions(TextConfigSchema), }); /** The catalog text section: renditions keyed by track name. */ diff --git a/js/hang/src/echo.ts b/js/hang/src/echo.ts index 8ee300bbf95..c9e16ca7550 100644 --- a/js/hang/src/echo.ts +++ b/js/hang/src/echo.ts @@ -7,6 +7,7 @@ import type { Path } from "@moq/net"; import * as z from "zod/mini"; import { u53Schema } from "./catalog/integers"; +import { renditions } from "./catalog/section"; import { measurementSchema } from "./snapshot"; import { TransportSchema } from "./stats"; @@ -55,7 +56,7 @@ export const SnapshotSchema = z.looseObject({ // Connection measurements; unavailable fields remain absent. transport: z.optional(TransportSchema), // Empty entries retain a rendition ID whose counters are all zero. - renditions: z.optional(z.record(z.string(), RenditionSchema)), + renditions: z.optional(renditions(RenditionSchema)), }); /** One viewer report, with application or container sections flattened beside it. */ export type Snapshot = z.infer; diff --git a/js/hang/src/stats.test.ts b/js/hang/src/stats.test.ts index 3fafa88cedd..c1507acb572 100644 --- a/js/hang/src/stats.test.ts +++ b/js/hang/src/stats.test.ts @@ -36,6 +36,16 @@ test("missing rendition counters mean zero while parsing preserves omission", () expect(Echo.SnapshotSchema.parse({})).toEqual({}); }); +test("rendition IDs are any string, and a malformed one refuses the snapshot", () => { + const kept = JSON.parse('{"renditions":{"":{},"__proto__":{"frames":1},"constructor":{}}}'); + for (const schema of [Stats.SnapshotSchema, Echo.SnapshotSchema]) { + const renditions = schema.parse(kept).renditions ?? {}; + expect(Object.keys(renditions)).toEqual(["", "__proto__", "constructor"]); + expect(Object.hasOwn(renditions, "__proto__")).toBe(true); + expect(() => schema.parse(JSON.parse('{"renditions":{"ok":{},"__proto__":{"frames":-1}}}'))).toThrow(); + } +}); + test("optional null measurements mean unavailable rather than measured zero", () => { expect( JSON.parse( diff --git a/js/hang/src/stats.ts b/js/hang/src/stats.ts index 43d9b054790..34248f784ef 100644 --- a/js/hang/src/stats.ts +++ b/js/hang/src/stats.ts @@ -5,6 +5,7 @@ */ import * as z from "zod/mini"; import { u53Schema } from "./catalog/integers"; +import { renditions } from "./catalog/section"; import { measurementSchema } from "./snapshot"; /** Transport gauges and cumulative loss counters known to the observer. */ export const TransportSchema = z.object({ @@ -41,7 +42,7 @@ export const SnapshotSchema = z.looseObject({ // Connection measurements; unavailable fields remain absent. transport: z.optional(TransportSchema), // Empty entries retain a rendition ID whose counters are all zero. - renditions: z.optional(z.record(z.string(), RenditionSchema)), + renditions: z.optional(renditions(RenditionSchema)), }); /** One publisher report, with application or container sections flattened beside it. */ export type Snapshot = z.infer; diff --git a/js/watch/src/echo.test.ts b/js/watch/src/echo.test.ts index dfb6af5bdb7..9a8b4df402b 100644 --- a/js/watch/src/echo.test.ts +++ b/js/watch/src/echo.test.ts @@ -109,6 +109,35 @@ test("publishes feedback where the catalog solicits it and follows the solicitat } }); +test("a rendition named __proto__ keeps its counters in the published report", async () => { + const clock = new Manual(); + const installed = clock.install(); + const origin = new Origin.Producer(); + const catalog = new Signal({ echo: { path: "viewers" } } as Catalog.Root); + const broadcast = watched(origin, "room/live.hang", catalog); + const echo = new Echo({ + broadcast, + origin, + name: "alice", + sources: [ + // JSON.parse keeps `__proto__` as an own key, as a decoder's report does. + source("room/live.hang", JSON.parse('{"__proto__":{"frames":1}}') as Record), + source("room/live.hang", { video: { frames: 2 } }), + ], + }); + try { + await flush(); + const renditions = (await read(origin, "room/viewers/alice.echo", clock))?.renditions ?? {}; + expect(Object.keys(renditions).sort()).toEqual(["__proto__", "video"]); + expect(Object.hasOwn(renditions, "__proto__")).toBe(true); + } finally { + echo.close(); + broadcast.close(); + origin.close(); + installed.close(); + } +}); + test("a second reporter at a feedback path in use publishes nothing until the holder leaves", async () => { const clock = new Manual(); const installed = clock.install(); diff --git a/js/watch/src/echo.ts b/js/watch/src/echo.ts index f2d177b2da0..32f2deaf016 100644 --- a/js/watch/src/echo.ts +++ b/js/watch/src/echo.ts @@ -160,8 +160,10 @@ export class Echo { // What the decoders received of the renditions in the catalog of the broadcast at `base`. #report(base: Moq.Path.Valid): Snapshot { - const renditions: Record = {}; - for (const source of this.#sources) Object.assign(renditions, source.feedback(base)); + // Not Object.assign, which sets the prototype for an ID like `__proto__` instead of copying it. + const renditions: Record = Object.fromEntries( + this.#sources.flatMap((source) => Object.entries(source.feedback(base))), + ); const rtt = this.in.probe.peek()?.rtt; return rtt === undefined ? { renditions } : { transport: { rtt: Math.floor(rtt) }, renditions }; } diff --git a/js/watch/src/feedback.test.ts b/js/watch/src/feedback.test.ts index 4e484250fd9..a493b72ef92 100644 --- a/js/watch/src/feedback.test.ts +++ b/js/watch/src/feedback.test.ts @@ -16,6 +16,15 @@ test("counts per rendition of each catalog broadcast, omitting zeros", () => { expect(tally.report(Path.from("unknown.hang"))).toEqual({}); }); +test("a rendition named __proto__ is reported as its own ID", () => { + const tally = new Tally(); + tally.count({ ...live, id: "__proto__" }, { frames: 1 }); + tally.count(live, { frames: 2 }); + const report = tally.report(live.path); + expect(Object.keys(report).sort()).toEqual(["__proto__", "video"]); + expect(Object.getOwnPropertyDescriptor(report, "__proto__")?.value).toEqual({ frames: 1 }); +}); + test("a stall reports its time so far and adds its duration once it ends", async () => { const clock = new Manual(); const installed = clock.install(); diff --git a/js/watch/src/feedback.ts b/js/watch/src/feedback.ts index 2c8916f64b0..41e18ccec94 100644 --- a/js/watch/src/feedback.ts +++ b/js/watch/src/feedback.ts @@ -99,15 +99,16 @@ export class Tally { /** What was received of each rendition of the broadcast at `path`, keyed by rendition ID. */ report(path: Moq.Path.Valid): Record { - const report: Record = {}; + // fromEntries defines each ID as its own key, so `__proto__` keeps its counters. + const report: [string, Received][] = []; for (const [id, entry] of this.#broadcasts.get(path) ?? []) { const received = { ...entry.received }; if (entry.stalling !== undefined) received.stalled = (received.stalled ?? 0) + elapsed(entry.stalling); if (received.stalled !== undefined) received.stalled = Math.floor(received.stalled); if (received.arrival) received.arrival = { ...received.arrival }; - report[id] = received; + report.push([id, received]); } - return report; + return Object.fromEntries(report); } } diff --git a/js/watch/src/support-probe.test.ts b/js/watch/src/support-probe.test.ts new file mode 100644 index 00000000000..83a18bf6747 --- /dev/null +++ b/js/watch/src/support-probe.test.ts @@ -0,0 +1,19 @@ +import { expect, test } from "bun:test"; +import { Effect } from "@moq/signals"; +import { SupportProbe } from "./support-probe"; + +test("a supported rendition named __proto__ is published as its own ID", async () => { + const probe = new SupportProbe<{ codec: string }>("video"); + const effect = new Effect(); + // JSON.parse keeps `__proto__` as an own key, as the catalog parser does. + const renditions = JSON.parse('{"__proto__":{"codec":"avc1"},"hd":{"codec":"avc1"}}') as Record< + string, + { codec: string } + >; + const published = Promise.withResolvers>(); + probe.run(effect, async () => true, renditions, published.resolve); + const available = await published.promise; + expect(Object.keys(available).sort()).toEqual(["__proto__", "hd"]); + expect(Object.hasOwn(available, "__proto__")).toBe(true); + effect.close(); +}); diff --git a/js/watch/src/support-probe.ts b/js/watch/src/support-probe.ts index 0af9ffe5b6d..4325ddcc5e4 100644 --- a/js/watch/src/support-probe.ts +++ b/js/watch/src/support-probe.ts @@ -57,7 +57,8 @@ export class SupportProbe { effect.set(this.#pending, true, false); effect.spawn(async () => { - const available: Record = {}; + // A Map, since assigning an ID like `__proto__` into a plain object sets its prototype instead. + const available = new Map(); for (const [name, config] of Object.entries(renditions)) { const key = cacheKey(config); @@ -86,14 +87,14 @@ export class SupportProbe { // Torn down: stop probing and publish nothing, since the rerun redoes this. if (effect.abort.aborted) return; - if (isSupported) available[name] = config; + if (isSupported) available.set(name, config); } - if (Object.keys(available).length === 0 && Object.keys(renditions).length > 0) { + if (available.size === 0 && Object.keys(renditions).length > 0) { console.warn(`[Source] no supported ${this.#kind} renditions found:`, renditions); this.error.set(new DOMException(`no supported ${this.#kind} renditions`, "NotSupportedError")); } - publish(available); + publish(Object.fromEntries(available)); this.#pending.set(false); }); } diff --git a/quest/m1/cut-audit/README.md b/quest/m1/cut-audit/README.md index 7441dd98a4e..ba523a62b6c 100644 --- a/quest/m1/cut-audit/README.md +++ b/quest/m1/cut-audit/README.md @@ -54,7 +54,6 @@ leaving JS-only redesigns of `js/net` to [rs2ts](/quest/m2/rs2ts-js.md). ## Required - [Each export-clock rule kept by the jitter audit has its own test](/quest/m1/cut-audit/jitter-rule-tests.md) - `stale`, `unheard_lag`, and the horizon shift in `rs/moq-mux/src/jitter.rs` each have a test that fails when that rule alone is removed -- [Text and data sections keep a prototype-named ID](/quest/m1/cut-audit/catalog-proto-ids.md) - text and data entries keep the ID JS's `z.record` drops ## Related diff --git a/quest/m1/cut-audit/catalog-proto-ids.md b/quest/m1/cut-audit/catalog-proto-ids.md deleted file mode 100644 index 231966596ba..00000000000 --- a/quest/m1/cut-audit/catalog-proto-ids.md +++ /dev/null @@ -1,23 +0,0 @@ -# [XS] Text and data sections keep a prototype-named ID - -## Goal - -A text rendition or json/binary data track whose ID is `__proto__` is kept -by both Rust and JS, the way video and audio renditions already are. The -shared parity corpus covers each section. - -## Plan - -Follow-up of [moq-dev/moq#7035](https://github.com/moq-dev/moq/pull/7035), -which fixed it for video and audio only. - -A malformed entry in any section refuses the whole catalog in both -languages (maintainer, 2026-10-30), which text and data sections already do -(`a_malformed_text_section_still_fails` and -`a_malformed_data_section_still_fails` in `rs/hang/src/catalog/root.rs`). -What remains is the ID: `text.ts`, `json.ts`, and `binary.ts` parse their -maps with `z.record`, which drops a `__proto__` ID that Rust keeps. - -Reuse `renditions()` in `js/hang/src/catalog/section.ts`, which video and -audio use, and add text and data cases (including `__proto__`) to -`rs/hang/fixtures/catalog-parity.json`. diff --git a/rs/hang/fixtures/catalog-parity.json b/rs/hang/fixtures/catalog-parity.json index 369dfdecc70..f39ae0ae8dc 100644 --- a/rs/hang/fixtures/catalog-parity.json +++ b/rs/hang/fixtures/catalog-parity.json @@ -3529,5 +3529,199 @@ } }, "refused": true + }, + { + "name": "text IDs are any string", + "catalog": { + "text": { + "renditions": { + "": { + "format": "vtt" + }, + "__proto__": { + "format": "vtt" + }, + "constructor": { + "format": "vtt" + }, + "toString": { + "format": "vtt" + } + } + } + }, + "text": ["", "__proto__", "constructor", "toString"] + }, + { + "name": "text malformed __proto__ refuses the catalog", + "catalog": { + "text": { + "renditions": { + "ok": { + "format": "vtt" + }, + "__proto__": {} + } + } + }, + "refused": true + }, + { + "name": "text malformed bad refuses the catalog", + "catalog": { + "text": { + "renditions": { + "__proto__": { + "format": "vtt" + }, + "bad": {} + } + } + }, + "refused": true + }, + { + "name": "json IDs are any string", + "catalog": { + "json": { + "tracks": { + "": { + "mode": "snapshot" + }, + "__proto__": { + "mode": "snapshot" + }, + "constructor": { + "mode": "snapshot" + }, + "toString": { + "mode": "snapshot" + } + } + } + }, + "json": ["", "__proto__", "constructor", "toString"] + }, + { + "name": "json malformed __proto__ refuses the catalog", + "catalog": { + "json": { + "tracks": { + "ok": { + "mode": "snapshot" + }, + "__proto__": {} + } + } + }, + "refused": true + }, + { + "name": "json malformed bad refuses the catalog", + "catalog": { + "json": { + "tracks": { + "__proto__": { + "mode": "snapshot" + }, + "bad": {} + } + } + }, + "refused": true + }, + { + "name": "binary IDs are any string", + "catalog": { + "binary": { + "tracks": { + "": { + "mode": "stream" + }, + "__proto__": { + "mode": "stream" + }, + "constructor": { + "mode": "stream" + }, + "toString": { + "mode": "stream" + } + } + } + }, + "binary": ["", "__proto__", "constructor", "toString"] + }, + { + "name": "binary malformed __proto__ refuses the catalog", + "catalog": { + "binary": { + "tracks": { + "ok": { + "mode": "stream" + }, + "__proto__": {} + } + } + }, + "refused": true + }, + { + "name": "binary malformed bad refuses the catalog", + "catalog": { + "binary": { + "tracks": { + "__proto__": { + "mode": "stream" + }, + "bad": {} + } + } + }, + "refused": true + }, + { + "name": "contribution IDs are any string", + "catalog": { + "contributions": { + "": { + "broadcast": "./a.hang" + }, + "__proto__": { + "broadcast": "./a.hang" + }, + "constructor": { + "broadcast": "./a.hang" + }, + "toString": { + "broadcast": "./a.hang" + } + } + }, + "contributions": ["", "__proto__", "constructor", "toString"] + }, + { + "name": "contribution malformed __proto__ refuses the catalog", + "catalog": { + "contributions": { + "ok": { + "broadcast": "./a.hang" + }, + "__proto__": {} + } + }, + "refused": true + }, + { + "name": "contribution malformed bad refuses the catalog", + "catalog": { + "contributions": { + "__proto__": { + "broadcast": "./a.hang" + }, + "bad": {} + } + }, + "refused": true } ] diff --git a/rs/hang/src/catalog/root.rs b/rs/hang/src/catalog/root.rs index 9b7130f8718..205620600e7 100644 --- a/rs/hang/src/catalog/root.rs +++ b/rs/hang/src/catalog/root.rs @@ -435,10 +435,18 @@ mod test { video: Vec, #[serde(default)] audio: Vec, + #[serde(default)] + text: Vec, + #[serde(default)] + json: Vec, + #[serde(default)] + binary: Vec, + #[serde(default)] + contributions: Vec, } /// The corpus `js/hang` also loads: both languages accept the same catalogs and keep the same - /// renditions, so a divergence fails CI on whichever side drifts. + /// rendition, data track, and contribution IDs, so a divergence fails CI on whichever side drifts. #[test] fn shared_parity_fixtures() { let cases: Vec = serde_json::from_str(include_str!("../../fixtures/catalog-parity.json")).unwrap(); @@ -455,8 +463,23 @@ mod test { (Ok(catalog), false) => { let video: Vec<_> = catalog.video.renditions.keys().cloned().collect(); let audio: Vec<_> = catalog.audio.renditions.keys().cloned().collect(); - if (&video, &audio) != (&case.video, &case.audio) { - failures.push(format!("{}: kept video {video:?} audio {audio:?}", case.name)); + let text: Vec<_> = catalog.text.renditions.keys().cloned().collect(); + let json: Vec<_> = catalog.json.tracks.keys().cloned().collect(); + let binary: Vec<_> = catalog.binary.tracks.keys().cloned().collect(); + let contributions: Vec<_> = catalog.contributions.keys().cloned().collect(); + if (&video, &audio, &text, &json, &binary, &contributions) + != ( + &case.video, + &case.audio, + &case.text, + &case.json, + &case.binary, + &case.contributions, + ) { + failures.push(format!( + "{}: kept video {video:?} audio {audio:?} text {text:?} json {json:?} binary {binary:?} contributions {contributions:?}", + case.name + )); } } }