Skip to content
Merged
21 changes: 1 addition & 20 deletions js/net/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { type GetPromise, Once, Signal } from "@moq/signals";
import { NotFound } from "./error.ts";
import type { Consumer as GroupConsumer } from "./group.ts";
import { type Hop, Route, randomHop } from "./hop.ts";
import { Route } from "./hop.ts";
import { hooks, type TrackSequence } from "./internal.ts";
import * as Path from "./path.ts";
import * as track from "./track.ts";
Expand Down Expand Up @@ -34,17 +34,6 @@ class BroadcastState {
// Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast
// closes once the last one closes, so a shared consumer can be handed to several callers.
consumers = 0;
// The origin serving this broadcast (see the wire's `origin`): named by an upstream
// reply, or generated on first use for content nobody named.
origin?: Hop;
}

// The origin a peer is told serves this broadcast: proxied from upstream, or a random
// one for content originating here, stable for the broadcast's life and shared by every
// session serving it.
function origin(state: BroadcastState): Hop {
state.origin ??= randomHop();
return state.origin;
}

function dequeueRequest(state: BroadcastState): track.Request | undefined {
Expand Down Expand Up @@ -258,10 +247,6 @@ export class Producer {
resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),
fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),
requested: () => this.#requested(),
origin: () => origin(this.#state),
name: (named) => {
this.#state.origin = named;
},
};
}

Expand Down Expand Up @@ -335,10 +320,6 @@ export class Consumer {
resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),
fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),
requested: () => this.#requested(),
origin: () => origin(this.#state),
name: (named) => {
this.#state.origin = named;
},
});
}

Expand Down
3 changes: 2 additions & 1 deletion js/net/src/lite/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ test("FetchOk names the origin on draft-07 only", async () => {
const written: Uint8Array[] = [];
const writer = new Writer(
new WritableStream<Uint8Array>({ write: (chunk) => void written.push(new Uint8Array(chunk)) }),
Version.DRAFT_07,
);
await new FetchOk(HopSchema.parse(42n)).encode(writer, Version.DRAFT_07);
writer.close();
await writer.closed;
const buf = concat(written);
expect(buf).toEqual(new Uint8Array([1, 42]));
const got = await FetchOk.decode(new Reader(undefined, buf), Version.DRAFT_07);
const got = await FetchOk.decode(new Reader(undefined, buf, Version.DRAFT_07), Version.DRAFT_07);
expect(got.origin).toBe(HopSchema.parse(42n));

await expect(new FetchOk(HopSchema.parse(42n)).encode(writer, Version.DRAFT_06)).rejects.toThrow();
Expand Down
161 changes: 141 additions & 20 deletions js/net/src/lite/origin.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { expect, test } from "bun:test";
import * as broadcast from "../broadcast.ts";
import { HopSchema, randomHop, UNKNOWN_HOP } from "../hop.ts";
import { Producer as GroupProducer } from "../group.ts";
import { type Hop, HopSchema, randomHop } from "../hop.ts";
import { createMockTransportPair } from "../mock.ts";
import { Producer as OriginProducer } from "../origin.ts";
import * as Path from "../path.ts";
import { Reader, Stream } from "../stream.ts";
import { wireOf } from "../wire.ts";
import { Fetch, FetchOk } from "./fetch.ts";
import { Group as GroupMessage } from "./group.ts";
import { Publisher } from "./publisher.ts";
import { StreamId } from "./stream.ts";
import { encodeSubscribeResponse, Subscribe, SubscribeStart } from "./subscribe.ts";
import {
decodeSubscribeResponse,
encodeSubscribeResponse,
Subscribe,
SubscribeEnd,
SubscribeStart,
} from "./subscribe.ts";
import { Subscriber } from "./subscriber.ts";
import { TrackInfo, Track as TrackMessage } from "./track.ts";
import { ALPN_07_WIP, Version } from "./version.ts";
Expand All @@ -27,50 +35,163 @@ async function settlesWithin(promise: Promise<unknown>, ms: number): Promise<boo
}
}

test("a broadcast originating here names one random origin for its life", () => {
const producer = new broadcast.Producer();
const origin = wireOf(producer).origin();
expect(origin).not.toBe(UNKNOWN_HOP);
expect(wireOf(producer).origin()).toBe(origin);
// Every handle, and so every session serving it, names the same one.
expect(wireOf(producer.consume()).origin()).toBe(origin);
expect(wireOf(new broadcast.Producer()).origin()).not.toBe(origin);
// Serve one SUBSCRIBE and one FETCH of `track` over a fresh draft-07 session publishing `origin`,
// returning the origins its SUBSCRIBE_START and FETCH_OK name.
async function servedOrigins(origin: OriginProducer, track: string): Promise<[Hop, Hop]> {
const pair = createMockTransportPair(ALPN_07_WIP);
const publisher = new Publisher(pair.server, VERSION, randomHop(), origin.consume());

const sub = await Stream.open(pair.client, { version: VERSION });
const serving = await Stream.accept(pair.server, VERSION);
if (!serving) throw new Error("the publisher never accepted the subscribe stream");
void publisher.runSubscribe(new Subscribe({ id: 0n, broadcast: Path.from("room"), track, priority: 0 }), serving);
const start = await decodeSubscribeResponse(sub.reader, VERSION);
if (!("start" in start)) throw new Error("expected SUBSCRIBE_START");

const fetch = await Stream.open(pair.client, { version: VERSION });
const fetching = await Stream.accept(pair.server, VERSION);
if (!fetching) throw new Error("the publisher never accepted the fetch stream");
void publisher.runFetch(new Fetch({ broadcast: Path.from("room"), track, priority: 0, group: 0 }), fetching);
const ok = await FetchOk.decode(fetch.reader, VERSION);

publisher.close();
sub.close();
fetch.close();
return [start.start.origin, ok.origin];
}

// JS publishes only what it produces, so the origin it names is its own: one identity per
// origin, shared by every session serving it (Rust's `origin.hop()`). A relay failing over
// from one session to another sees the same origin and resumes rather than ending the broadcast.
test("every draft-07 session serving an origin names the same origin", async () => {
const origin = new OriginProducer();
const produced = origin.createBroadcast(Path.from("room"));
produced.announce();
const track = produced.createTrack("video");
const group = new GroupProducer(0);
group.writeString("x");
group.close();
track.writeGroup(group);

const [start, fetched] = await servedOrigins(origin, "video");
expect(fetched).toBe(start);
expect(await servedOrigins(origin, "video")).toEqual([start, start]);

const other = new OriginProducer();
const elsewhere = other.createBroadcast(Path.from("room"));
elsewhere.announce();
elsewhere.createTrack("video").writeGroup(group);
expect((await servedOrigins(other, "video"))[0]).not.toBe(start);
});

// Plays the upstream publisher on a mock draft-07 session, answering the subscriber's streams.
class Upstream {
readonly pair = createMockTransportPair(ALPN_07_WIP);
readonly subscriber = new Subscriber(this.pair.client, VERSION, randomHop());

// Answer a TRACK stream, then accept and start the SUBSCRIBE that follows it at group 0,
// naming `origin`.
async subscribe(origin: Hop): Promise<Stream> {
const info = await Stream.accept(this.pair.server, VERSION);
if (!info) throw new Error("the subscriber never asked for TRACK_INFO");
expect(await info.reader.u53()).toBe(StreamId.Track);
await TrackMessage.decode(info.reader, VERSION);
await new TrackInfo({ maxAge: 60_000 }).encode(info.writer, VERSION);
info.close();

const sub = await Stream.accept(this.pair.server, VERSION);
if (!sub) throw new Error("the subscriber never subscribed");
expect(await sub.reader.u53()).toBe(StreamId.Subscribe);
await Subscribe.decode(sub.reader, VERSION);
await this.start(sub, origin);
return sub;
}

async start(sub: Stream, origin: Hop): Promise<void> {
await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, origin) }, VERSION);
}

// End a started subscription cleanly with no groups served.
async end(sub: Stream): Promise<void> {
await encodeSubscribeResponse(sub.writer, { end: new SubscribeEnd(0, 0) }, VERSION);
sub.close();
}
}

// JS used to keep one origin per broadcast, which the latest reply overwrote. Each track copy
// now keeps its own, so two tracks served by different origins are both fine.
test("tracks of one broadcast keep the origins their own replies named", async () => {
const upstream = new Upstream();
const consumer = upstream.subscriber.consume(Path.from("room"));

const video = consumer.track("video").subscribe({});
const videoSub = await upstream.subscribe(HopSchema.parse(42n));
const audio = consumer.track("audio").subscribe({});
const audioSub = await upstream.subscribe(HopSchema.parse(43n));
// Repeating a copy's own origin is not a change.
await upstream.start(videoSub, HopSchema.parse(42n));

// Both end cleanly, after every START was read, rather than with a mismatch.
await upstream.end(videoSub);
await upstream.end(audioSub);
expect(await video.closed).toBeNull();
expect(await audio.closed).toBeNull();

upstream.subscriber.close();
});

// Mirrors Rust's track::Provenance: a copy has one origin, so a reply naming another means
// upstream is serving a different track. The copy is dropped rather than relabeled.
test("a SUBSCRIBE_START naming a second origin drops the copy", async () => {
const upstream = new Upstream();
const consumer = upstream.subscriber.consume(Path.from("room"));

const reader = consumer.track("video").subscribe({});
const sub = await upstream.subscribe(HopSchema.parse(42n));
await upstream.start(sub, HopSchema.parse(43n));

const closed = await reader.closed;
expect(closed).toBeInstanceOf(Error);
expect((closed as Error).message).toContain("origin changed");

upstream.subscriber.close();
});

test("a draft-07 group waits for SUBSCRIBE_START, whose origin the broadcast then names", async () => {
test("a draft-07 group waits for SUBSCRIBE_START", async () => {
const pair = createMockTransportPair(ALPN_07_WIP);
const subscriber = new Subscriber(pair.client, VERSION, randomHop());
const consumer = subscriber.consume(Path.from("room"));
const reader = consumer.track("video").subscribe({});

const info = await Stream.accept(pair.server);
const info = await Stream.accept(pair.server, VERSION);
if (!info) throw new Error("the subscriber never asked for TRACK_INFO");
expect(await info.reader.u53()).toBe(StreamId.Track);
await TrackMessage.decode(info.reader, VERSION);
await new TrackInfo({ maxAge: 60_000 }).encode(info.writer, VERSION);
info.close();

const sub = await Stream.accept(pair.server);
const sub = await Stream.accept(pair.server, VERSION);
if (!sub) throw new Error("the subscriber never subscribed");
expect(await sub.reader.u53()).toBe(StreamId.Subscribe);
await Subscribe.decode(sub.reader, VERSION);

// The group's stream races ahead of the subscribe stream.
let controller!: ReadableStreamDefaultController<Uint8Array>;
const readable = new ReadableStream<Uint8Array>({ start: (c) => (controller = c) });
void subscriber.runGroup(new GroupMessage({ subscribe: 0n, sequence: 0 }), new Reader(readable));
void subscriber.runGroup(
new GroupMessage({ subscribe: 0n, sequence: 0 }),
new Reader(readable, undefined, VERSION),
);
controller.enqueue(new Uint8Array([0, 1, 120]));
controller.close();

const next = reader.recvGroup();
expect(await settlesWithin(next, 50)).toBe(false);

const origin = HopSchema.parse(42n);
await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, origin) }, VERSION);
await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, HopSchema.parse(42n)) }, VERSION);
const group = await next;
expect(group?.sequence).toBe(0);
expect(await group?.readString()).toBe("x");

// Republishing the broadcast proxies the origin upstream named.
expect(wireOf(consumer).origin()).toBe(origin);
subscriber.close();
});
12 changes: 8 additions & 4 deletions js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ export class Publisher {

#publish?: OriginConsumer;

// The origin named in SUBSCRIBE_START and FETCH_OK: the published origin's identity, shared by
// every session serving it. Unused without one, since nothing is served.
#origin: Hop;

// TRACK_INFO is immutable per track, so resolve it from the application once
// (via a throwaway subscribe whose info() resolves when the app calls accept)
// and reuse it for every later TRACK request of the same track. Keyed by the
Expand All @@ -404,6 +408,7 @@ export class Publisher {
const origin = publish && wireOf(publish);
this.#advertised = origin?.advertised ?? new Signal(new Map());
this.#publish = publish;
this.#origin = origin?.hop ?? hop;

// Grab the datagram writer up front when the transport carries datagrams (no group
// fallback, so it stays undefined otherwise). One writer for all subscriptions.
Expand Down Expand Up @@ -670,9 +675,8 @@ export class Publisher {
start: { included: sequence },
end: bounds.endGroup === undefined ? undefined : { included: bounds.endGroup },
});
// Read once content flowed: an upstream's SUBSCRIBE_START named the origin
// before any of its content did.
const start = new SubscribeStart(sequence, wireOf(front).origin());
// JS publishes only what it produces, so the origin serving it is ours.
const start = new SubscribeStart(sequence, this.#origin);

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 Preserve the demanded front's origin

The follow-up change from the session hop to origin?.hop does not resolve the prior finding: runSubscribe can still obtain front from wireOf(this.#publish).demand(...), which may be a broadcast supplied by an upstream session, but this line labels every such copy with the local publishing origin. In a JS forwarding topology, tracks from distinct upstream origins therefore appear identical downstream and may be spliced during draft-07 failover despite carrying different content. Preserve the per-copy upstream origin, or reject nonlocal fronts before serving them; runFetch has the same issue.

AGENTS.md reference: AGENTS.md:L63-L63

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.

Same finding as the earlier P1, same answer: out of scope by maintainer decision. @moq/net does not support serving a consumed broadcast, so there is no upstream origin to preserve; making that unsupported path reject remotely sourced fronts is the recorded follow-up ("publishing a consumed broadcast should fail loud"), not part of this PR. See the decisions comment and the reply above.

(Written by Claude Opus 5.5)

await encodeSubscribeResponse(stream.writer, { start }, this.version);
});

Expand Down Expand Up @@ -742,7 +746,7 @@ export class Publisher {
const info = await this.#resolveTrackInfo(front, msg.track);
group = await wireOf(front).fetchGroup(msg.track, msg.group, { priority: msg.priority });
if (hasOrigin(this.version)) {
await new FetchOk(wireOf(front).origin()).encode(stream.writer, this.version);
await new FetchOk(this.#origin).encode(stream.writer, this.version);
}
await this.#runFetchGroup(group, stream.writer, {
timescale: Timescale(info.timescale),
Expand Down
17 changes: 7 additions & 10 deletions js/net/src/lite/subscriber.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect, spyOn, test } from "bun:test";
import { Signal } from "@moq/signals";
import { Producer as BroadcastProducer } from "../broadcast.ts";
import type { Probe as ProbeStats } from "../connection/stats.ts";
import { error, fromTransport, reason, StreamCode, StreamError } from "../error.ts";
import { HopSchema, isAnonymous, MAX_HOPS, Route, UNKNOWN_HOP } from "../hop.ts";
Expand Down Expand Up @@ -806,7 +805,7 @@ test.each([
const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n));

let settled = false;
const fetch = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0).then(
const fetch = subscriber.fetchGroup(Path.from("room"), "video", 0).then(
() => {
settled = true;
return undefined;
Expand Down Expand Up @@ -840,9 +839,7 @@ test("a fetch started after the subscriber closes rejects without opening a stre
const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n));
subscriber.close();

const err = await subscriber
.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0)
.catch((err: unknown) => err);
const err = await subscriber.fetchGroup(Path.from("room"), "video", 0).catch((err: unknown) => err);
expectCut(err, undefined);
expect(streams.length).toBe(0);
});
Expand All @@ -853,7 +850,7 @@ test("an already-aborted fetch rejects without opening a stream", async () => {
const cause = new Error("gone");

const err = await subscriber
.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, {
.fetchGroup(Path.from("room"), "video", 0, {
signal: AbortSignal.abort(cause),
})
.catch((err: unknown) => err);
Expand All @@ -868,10 +865,10 @@ test("one of two fetch sharers aborting leaves the other's fetch", async () => {
const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n));

const controller = new AbortController();
const a = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, {
const a = subscriber.fetchGroup(Path.from("room"), "video", 0, {
signal: controller.signal,
});
const b = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0);
const b = subscriber.fetchGroup(Path.from("room"), "video", 0);

await drainUntil(() => streams.length === 1);
await answerTrackInfo(streams[0]);
Expand Down Expand Up @@ -904,10 +901,10 @@ test.each([

const first = new AbortController();
const second = new AbortController();
const a = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, {
const a = subscriber.fetchGroup(Path.from("room"), "video", 0, {
signal: first.signal,
});
const b = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, {
const b = subscriber.fetchGroup(Path.from("room"), "video", 0, {
signal: second.signal,
});

Expand Down
Loading
Loading