Skip to content
Merged
5 changes: 5 additions & 0 deletions js/msf/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ How to update your code for each breaking change, newest first.

## Unreleased

### `CatalogSchema` keeps prototype-named extension members

`CatalogSchema.parse` preserves own `__proto__`, `constructor`, and `toString` members in `ext`, matching `decode`.
The parsed extension map now has a null prototype; inspect it with `Object.hasOwn` and `Object.entries`.

### `Track.stalled` is removed (#4915)

The non-standard `stalled` track field is gone and no longer read or written.
Expand Down
7 changes: 4 additions & 3 deletions js/msf/api/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
import { $strip } from "zod/v4/core";
import { ZodMiniArray } from "zod/mini";
import { ZodMiniBoolean } from "zod/mini";
import { ZodMiniCustom } from "zod/mini";
import { ZodMiniEnum } from "zod/mini";
import { ZodMiniNumber } from "zod/mini";
import { ZodMiniObject } from "zod/mini";
import { ZodMiniOptional } from "zod/mini";
import { ZodMiniRecord } from "zod/mini";
import { ZodMiniPipe } from "zod/mini";
import { ZodMiniString } from "zod/mini";
import { ZodMiniTransform } from "zod/mini";
import { ZodMiniUnion } from "zod/mini";
import { ZodMiniUnknown } from "zod/mini";
import { infer } from "zod/mini";

export type Catalog = infer<typeof CatalogSchema>;

export const CatalogSchema: ZodMiniObject<{
ext: ZodMiniOptional<ZodMiniRecord<ZodMiniString<string>, ZodMiniUnknown>>;
ext: ZodMiniOptional<ZodMiniPipe<ZodMiniCustom<Record<string, unknown>, Record<string, unknown>>, ZodMiniTransform<Record<string, unknown>, Record<string, unknown>>>>;
tracks: ZodMiniArray<ZodMiniObject<{
altGroup: ZodMiniOptional<ZodMiniNumber<number>>;
bitrate: ZodMiniOptional<ZodMiniNumber<number>>;
Expand Down
38 changes: 23 additions & 15 deletions js/msf/src/catalog.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, test } from "bun:test";
import { decode, encode } from "./catalog.ts";
import { runInNewContext } from "node:vm";
import { CatalogSchema, decode, encode } from "./catalog.ts";

function encodeJson(value: unknown): Uint8Array {
return new TextEncoder().encode(JSON.stringify(value));
Expand Down Expand Up @@ -264,20 +265,27 @@ test("refuses an extension section named after a reserved root field", () => {
expect(() => encode({ tracks: [], ext: { tracks: "nope" } })).toThrow(/reserved root field/);
});

test("round-trips a __proto__ extension member as data", () => {
// Assigning `__proto__` to an ordinary object invokes the prototype setter: the member
// vanishes from the catalog and its value becomes the map's prototype. Built as raw JSON,
// since `__proto__:` in an object literal is that same setter syntax and never lands as a key.
const raw = '{"version":"draft-01","tracks":[],"__proto__":{"polluted":1},"ok":2}';
const catalog = decode(new TextEncoder().encode(raw));

const ext = catalog.ext ?? {};
expect(Object.keys(ext).sort()).toEqual(["__proto__", "ok"]);
expect(Object.getPrototypeOf(ext)).toBeNull();

const wire = decodeJson(encode(catalog));
expect(Object.keys(wire)).toContain("__proto__");
expect(wire.ok).toBe(2);
test("shared Rust extension names survive decoding and the public schema", async () => {
const raw = await Bun.file(new URL("../../../rs/moq-msf/fixtures/catalog-names.json", import.meta.url)).bytes();
const { version: _, tracks, ...ext } = decodeJson(raw);
const catalog = decode(raw);
for (const parsed of [catalog, CatalogSchema.parse({ tracks, ext }), CatalogSchema.parse(catalog)]) {
expect(Object.keys(parsed.ext ?? {}).sort()).toEqual(Object.keys(ext).sort());
expect(Object.getPrototypeOf(parsed.ext)).toBeNull();
expect(decodeJson(encode(parsed))).toEqual(decodeJson(raw));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
// Like a record, the extension accepts only plain objects: these have no own members to keep.
for (const bad of [new Map([["mpegts", 1]]), new Set(["mpegts"]), [1, 2], "mpegts"]) {
expect(() => CatalogSchema.parse({ tracks: [], ext: bad })).toThrow();
}
// A plain object from another realm has a different `Object.prototype`, yet is still plain.
const foreign = runInNewContext(`JSON.parse('{"mpegts":1,"constructor":2,"__proto__":3}')`);
expect(Object.getPrototypeOf(foreign)).not.toBe(Object.prototype);
expect(Object.keys(CatalogSchema.parse({ tracks: [], ext: foreign }).ext ?? {}).sort()).toEqual([
"__proto__",
"constructor",
"mpegts",
]);
});

test("caption role, label, language and codec survive the wire", () => {
Expand Down
18 changes: 17 additions & 1 deletion js/msf/src/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,23 @@ export const CatalogSchema = z.object({
// draft-lcurley-moq-mpegts. Mirrors `Catalog::ext` in `moq-msf`, which is typed;
// here it stays an untyped map, so members this build has never seen survive a
// decode/encode round trip unchanged.
ext: z.optional(z.record(z.string(), z.unknown())),
ext: z.optional(
z.pipe(
// A record parser drops `__proto__`; every own extension member is application data.
// Only plain objects qualify, as with a record: a `Map` or class instance has no
// own members to copy, so accepting one would silently empty the extension. A plain
// object's prototype is null or some realm's `Object.prototype`, recognized by shape
// rather than identity so an object parsed in another realm (an iframe, `vm`) passes.
z.custom<Record<string, unknown>>((value) => {
if (typeof value !== "object" || value === null) return false;
const proto = Object.getPrototypeOf(value);
return (
proto === null || (Object.getPrototypeOf(proto) === null && Object.hasOwn(proto, "isPrototypeOf"))
);
}),
Comment thread
kixelated marked this conversation as resolved.
Resolved
z.transform((value) => Object.assign(Object.create(null), value) as Record<string, unknown>),
),
),
});

/** The MSF catalog: a snapshot of the available tracks. */
Expand Down
105 changes: 105 additions & 0 deletions js/publish/src/broadcast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,111 @@ const audioConfig: Catalog.AudioConfig = {
container: { kind: "legacy" },
};

const parity = (await Bun.file(new URL("../../../rs/hang/fixtures/catalog-parity.json", import.meta.url)).json()) as {
catalog: unknown;
refused?: boolean;
}[];

for (const kind of ["video", "audio", "text"] as const) {
const fixture = parity.find((c) => {
if (c.refused) return false;
const entries = Catalog.RootSchema.parse(c.catalog)[kind]?.renditions;
return entries && ["__proto__", "constructor", "toString"].every((name) => Object.hasOwn(entries, name));
});
if (!fixture) throw new Error(`missing shared ${kind} name fixture`);
const configs = Catalog.RootSchema.parse(fixture.catalog)[kind]?.renditions ?? {};

test(`shared Rust ${kind} names survive registration and the published catalog`, async () => {
const origin = new Origin.Producer();
const broadcast = new Broadcast({ origin, name: Path.from("names.hang") });
try {
const register = () =>
Object.entries(configs).map(([name, config]) => {
const rendition = broadcast[kind](name);
rendition.config.set(config);
expect(() => broadcast[kind](name)).toThrow();
return rendition;
});
const registered = register();
await settle();
const entries = (await readCatalog(broadcast))?.[kind]?.renditions ?? {};
expect(Object.keys(entries).sort()).toEqual(Object.keys(configs).sort());
expect(entries).toEqual(configs);
for (const rendition of registered) {
expect(rendition.track.peek()?.name).toBe(rendition.name);
rendition.close();
}
await settle();
expect((await readCatalog(broadcast))?.[kind]).toBeUndefined();
const again = register();
await settle();
const restored = (await readCatalog(broadcast))?.[kind]?.renditions ?? {};
expect(Object.keys(restored).sort()).toEqual(Object.keys(configs).sort());
expect(restored).toEqual(configs);
for (const rendition of again) expect(rendition.track.peek()?.name).toBe(rendition.name);

// A codec change replaces each prototype-named track under a fresh name. Text has no codec identity.
if (kind === "text") return;
const codec = kind === "video" ? "vp8" : "mp4a.40.2";
const byName = new Map(again.map((rendition) => [rendition.name, rendition]));
for (const [name, config] of Object.entries(configs)) {
expect(config.codec).not.toBe(codec);
byName.get(name)?.config.set({ ...config, codec });
}
await settle();
const replaced = (await readCatalog(broadcast))?.[kind]?.renditions ?? {};
expect(Object.keys(replaced).sort()).toEqual(Object.keys(configs).sort());
for (const rendition of again) {
const track = `${rendition.name}/1`;
expect(rendition.track.peek()?.name).toBe(track);
expect(replaced[rendition.name]?.codec).toBe(codec);
expect(replaced[rendition.name]?.track).toBe(track);
}
} finally {
broadcast.close();
origin.close();
}
});
}

test("shared Rust stats names survive the publisher's report builder", async () => {
const fixture = Stats.SnapshotSchema.parse(
await Bun.file(new URL("../../../rs/hang/fixtures/stats-snapshot.json", import.meta.url)).json(),
);
const clock = new Manual();
const installed = clock.install();
const origin = new Origin.Producer();
const broadcast = new Broadcast({ origin, name: Path.from("stats-names.hang"), stats: true });
try {
for (const [name, stats] of Object.entries(fixture.renditions ?? {})) {
const rendition = broadcast.video(name);
rendition.config.set(videoConfig);
rendition.stats.set(stats);
}
await settle();
const net = broadcast.net.peek();
if (!net) throw new Error("expected a network producer");
const subscriber = net.track(Broadcast.STATS_TRACK).subscribe();
try {
const consumer = new Json.Snapshot.Consumer<Stats.Snapshot>({
track: subscriber,
schema: Stats.SnapshotSchema,
});
await settle();
await clock.advance(Nano.fromMilli(Milli(1000)));
const reported = (await consumer.latest())?.value.renditions ?? {};
expect(Object.keys(reported).sort()).toEqual(Object.keys(fixture.renditions ?? {}).sort());
expect(reported).toEqual(fixture.renditions ?? {});
} finally {
subscriber.close();
}
} finally {
broadcast.close();
origin.close();
installed.close();
}
});

test("sibling broadcasts on one page advertise the same capture clock", async () => {
const origin = new Origin.Producer();
const camera = new Broadcast({ enabled: true, origin, name: Path.from("participant/camera.hang") });
Expand Down
35 changes: 17 additions & 18 deletions js/publish/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export type BroadcastInput = {
// The longest group among the video and audio renditions, the catalog's `media.join.interval`: the
// promise that every one starts a group at least that often. Undefined while none declares one, so a
// later listing tries again; it reads every hint until then, since none is fixed yet.
function longestGroup(effect: Effect, renditions: Record<string, Rendition<unknown>>): Catalog.U53 | undefined {
function longestGroup(effect: Effect, renditions: ReadonlyMap<string, Rendition<unknown>>): Catalog.U53 | undefined {
let longest: number | undefined;
for (const rendition of Object.values(renditions)) {
for (const rendition of renditions.values()) {
if (rendition.kind === "text") continue;
const hint = effect.get(rendition.joinInterval);
if (hint === undefined || !Number.isFinite(hint) || hint <= 0) continue;
Expand Down Expand Up @@ -149,9 +149,8 @@ export class Broadcast {
*/
readonly order = new Order();

// The registered renditions keyed by full track name. A plain object so deep-equality detects a
// key add/remove; the Rendition values compare by identity, which is stable.
readonly #renditions = new Signal<Record<string, Rendition<unknown>>>({});
// Replaced on add/remove so signal subscribers see every registry change.
readonly #renditions = new Signal<ReadonlyMap<string, Rendition<unknown>>>(new Map());

// The writable signals backing each Rendition's read-only `track` and `slot`, keyed by name.
// A static network track is exposed here while it is open. On-demand gates non-audio tracks
Expand Down Expand Up @@ -227,7 +226,7 @@ export class Broadcast {
}

#register<C>(name: string, kind: Kind): Rendition<C> {
if (this.#renditions.peek()[name]) {
if (this.#renditions.peek().has(name)) {
throw new Error(`rendition already registered: ${name}`);
}
this.#publication.peek()?.check(name, kind);
Expand All @@ -241,7 +240,7 @@ export class Broadcast {
// The slot is the one its producer fills, so it holds this rendition's config.
const slot = handle.slot as Getter<Slot<C> | undefined>;
const rendition = makeRendition<C>(name, kind, handle.track, slot, () => this.#unregister(name));
this.#renditions.update((renditions) => ({ ...renditions, [name]: rendition as Rendition<unknown> }));
this.#renditions.update((renditions) => new Map(renditions).set(name, rendition as Rendition<unknown>));

return rendition;
}
Expand All @@ -256,9 +255,9 @@ export class Broadcast {
}

this.#renditions.update((renditions) => {
if (!(name in renditions)) return renditions;
const next = { ...renditions };
delete next[name];
if (!renditions.has(name)) return renditions;
const next = new Map(renditions);
next.delete(name);
return next;
});
}
Expand All @@ -271,13 +270,13 @@ export class Broadcast {
const publication = effect.get(this.#publication);
if (publication) effect.get(publication.renames);

const video: Record<string, Catalog.VideoConfig> = {};
const audio: Record<string, Catalog.AudioConfig> = {};
const text: Record<string, Catalog.TextConfig> = {};
const video: Record<string, Catalog.VideoConfig> = Object.create(null);
const audio: Record<string, Catalog.AudioConfig> = Object.create(null);
const text: Record<string, Catalog.TextConfig> = Object.create(null);
const listed = new Map<string, Listing>();
const served: [Rendition<unknown>, string, Identity][] = [];

for (const rendition of Object.values(renditions)) {
for (const rendition of renditions.values()) {
const source = enabled ? effect.get(rendition.config) : undefined;
if (source === undefined) continue;
// The rendition's media is this broadcast's own track, so its config can't point elsewhere.
Expand Down Expand Up @@ -402,8 +401,8 @@ export class Broadcast {

// What each listed rendition has sent, keyed by its catalog ID, and the connection's round-trip time.
#stats(): { transport?: { rtt: number }; renditions: Record<string, RenditionStats> } {
const renditions: Record<string, RenditionStats> = {};
for (const rendition of Object.values(this.#renditions.peek())) {
const renditions: Record<string, RenditionStats> = Object.create(null);
for (const rendition of this.#renditions.peek().values()) {
const stats = rendition.stats.peek();
if (stats && rendition.config.peek() !== undefined) renditions[rendition.name] = stats;
}
Expand All @@ -414,7 +413,7 @@ export class Broadcast {
// The names outside a publication's own that a replacement must avoid: every registered ID, and
// every ID and track name in the catalog, including an application's own sections.
#reserved(): Set<string> {
const taken = new Set(Object.keys(this.#renditions.peek()));
const taken = new Set(this.#renditions.peek().keys());
// A no-op edit is the catalog's only read; it publishes nothing.
this.catalog.mutate((catalog) => {
for (const entries of [
Expand Down Expand Up @@ -520,7 +519,7 @@ export class Broadcast {
const renditions = inner.get(this.#renditions);
inner.get(publication.renames);

for (const rendition of Object.values(renditions)) {
for (const rendition of renditions.values()) {
const handle = this.#handles.get(rendition.name);
if (!handle) continue;

Expand Down
1 change: 0 additions & 1 deletion quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ their end report, so an embedder's drain alert pages on real cuts.
- [The relay reports native sessions it force-closes at the drain deadline](/quest/m1/relay-drain-cuts.md) - the drain's public outcome and live tally separate sessions cut while open from sessions only finishing their end report, so an embedder's drain alert pages on real cuts
- [A Rust relay never replays from group 0 to a lite-03/04 subscriber](/quest/m1/relay-inflight-replay.md) - A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track copy that is still resolving starts at the newest group once the copy resolves
- [The JS resume model passes seed 12345](/quest/m1/js-resume-model-12345.md) - a reader updated to no ranges keeps reading instead of ending cleanly
- [Prototype-safe rendition names in JS](/quest/m1/proto-safe-names.md) - `js/msf`'s `ext` and `js/publish`'s rendition maps keep `__proto__` and `constructor` names, like Rust
- [Every moq-mux consumer gets the catalog warmup from one entry point](/quest/m1/mux-warmup.md) - one moq-mux call widens the subscription and configures the consumer, adopted by moq-audio, moq-c, moq-ffi's raw consumer, and moqsrc
- [Codex shells never load another checkout's dev environment](/quest/m1/codex-env-snapshot.md) - the Codex dev-shell snapshot is skipped when stale or inside `nix develop`
- [Rename moq-rtc to moq-webrtc](/quest/m1/webrtc-rename.md) - one published `moq-webrtc` crate holds the gateway, the ICE driver, and the data-channel transport
Expand Down
46 changes: 0 additions & 46 deletions quest/m1/proto-safe-names.md

This file was deleted.

Loading
Loading