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
3 changes: 3 additions & 0 deletions bench/browser/page/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async function allocator(connection: Moq.Connection): Promise<Moq.Bandwidth.Hand
function reserve(handle: Moq.Bandwidth.Handle, origin: Moq.Origin.Producer, name: string) {
const publication = origin.route(Moq.Path.from(name), { hidden: true });
const track = publication.broadcast.createTrack("video", { priority: 1 });
if (!track) throw new Error("the track already exists");
const reservation = handle.reserve(track.demand(), 2_500_000);
cleanup.push(() => {
reservation.close();
Expand Down Expand Up @@ -151,6 +152,7 @@ async function receive(config: Sampling): Promise<void> {
const publishing = new Moq.Origin.Producer();
const publication = publishing.route(Moq.Path.from(`${config.path}/live`), { hidden: true });
const track = publication.broadcast.createTrack("data", { timescale: Moq.Time.Timescale.MILLI });
if (!track) throw new Error("the track already exists");
publication.announce();
const publisher = await Moq.Connection.connect({
url: new URL(config.url),
Expand Down Expand Up @@ -252,6 +254,7 @@ async function resume(config: Sampling & { cycles: number; idle_ms: number; limi
await new Promise((resolve) => setTimeout(resolve, config.idle_ms));
const publication = origin.route(Moq.Path.from(`${config.path}/resume/${cycle}`), { hidden: true });
const track = publication.broadcast.createTrack("video", { priority: 1 });
if (!track) throw new Error("the track already exists");
const start = performance.now();
const reservation = handle.reserve(track.demand(), 2_500_000);
const deadline = start + config.limit_ms;
Expand Down
1 change: 1 addition & 0 deletions bench/browser/page/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function publish(config: Transport): Promise<void> {
timescale: Moq.Time.Timescale.MILLI,
maxAge: Duration.millis(600_000),
});
if (!track) throw new Error("the track already exists");
const demand = track.demand().wanted;
publication.announce();
const connection = await Moq.Connection.connect({
Expand Down
1 change: 1 addition & 0 deletions demo/boy/src/game.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ test("status skips values that fail the schema and ends when the track errors",
const origin = new Moq.Origin.Producer();
const publication = origin.route(Moq.Path.from("game", "test"));
const track = publication.broadcast.createTrack("status");
if (!track) throw new Error("the track already exists");

// One finished group per status, so between statuses the loop waits on the next group.
const publish = (value: unknown) => {
Expand Down
1 change: 1 addition & 0 deletions demo/boy/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ export class Game {
});

const track = route.broadcast.createTrack("command", { timescale: Moq.Time.Timescale.MILLI });
if (!track) throw new Error("the track already exists");
const producer = new Json.Snapshot.Producer<Record<string, unknown>>({ track });
effect.cleanup(() => producer.finish());
effect.run(this.#runCommandTrack.bind(this, track, producer));
Expand Down
6 changes: 3 additions & 3 deletions demo/carla/web/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export function populatePreview(add: (path: Moq.Path.Valid) => Truck) {
].entries()) {
const truck = add(Moq.Path.from(`carla/truck${index + 1}/front.hang`));
const origin = new Moq.Broadcast.Producer();
const producer = new Json.Snapshot.Producer<TruckStatus>({
track: origin.createTrack("status", { compression: "deflate" }),
});
const track = origin.createTrack("status", { compression: "deflate" });
if (!track) throw new Error("the status track already exists");
const producer = new Json.Snapshot.Producer<TruckStatus>({ track });
(truck.broadcast.out.active as Moq.Signals.Signal<Moq.Broadcast.Consumer | undefined>).set(origin.consume());
producer.update(
{
Expand Down
7 changes: 4 additions & 3 deletions demo/carla/web/src/truck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe("Truck status", () => {
function statusBroadcast() {
const broadcast = new Moq.Broadcast.Producer();
const track = broadcast.createTrack("status", { compression: "deflate" });
if (!track) throw new Error("the track already exists");
const producer = new Json.Snapshot.Producer<TruckStatus>({ track });
return { broadcast, producer };
}
Expand Down Expand Up @@ -408,9 +409,9 @@ describe("Truck control", () => {
const driving = new Moq.Signals.Signal<string | undefined>(undefined);
const focused = new Moq.Signals.Signal<string | undefined>(undefined);
const camera = origin.route(Moq.Path.from("carla/truck1/front.hang"));
const producer = new Json.Snapshot.Producer<TruckStatus>({
track: camera.broadcast.createTrack("status", { compression: "deflate" }),
});
const track = camera.broadcast.createTrack("status", { compression: "deflate" });
if (!track) throw new Error("the status track already exists");
const producer = new Json.Snapshot.Producer<TruckStatus>({ track });
const truck = new Truck({
broadcastId: Moq.Path.from("carla/truck1/front.hang"),
connection,
Expand Down
1 change: 1 addition & 0 deletions demo/carla/web/src/truck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function publishControl(
const route = origin.route(path, { hidden: true });
effect.cleanup(() => route.close());
const track = route.broadcast.createTrack("control", { maxAge: CONTROL_HEARTBEAT });
if (!track) throw new Error("the track already exists");
// Announced once the track exists, under a fresh instance: the truck binds its
// single driver to this path and instance.
route.announce();
Expand Down
1 change: 1 addition & 0 deletions demo/web/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ meta.run((effect) => {
timescale: Net.Time.Timescale.MILLI,
maxAge: Duration.millis(86_400_000),
});
if (!track) throw new Error("the track already exists");
effect.cleanup(() => {
track.finish();
track.close();
Expand Down
2 changes: 1 addition & 1 deletion doc/concept/identity.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ while let Some(request) = claim.requested().await {
// Takes the claim's instance, so readers resolved through the claim continue.
// Hidden until its tracks exist.
let job = request.route(RouteOptions::hidden())?;
let video = job.broadcast().create_track("video", info.clone())?;
let video = job.broadcast().create_track("video", info.clone()).context("video exists")?;
// Announces the exact path under that instance and answers the waiting request.
job.announce()?;
spawn(run_job(video, job));
Expand Down
2 changes: 2 additions & 0 deletions doc/lib/js/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const connection = await Moq.Connection.connect({
// Hidden until its first track exists, then announced.
const route = origin.route(Moq.Path.from("chat.room"), { hidden: true });
const track = route.broadcast.createTrack("messages", { timescale: Moq.Time.Timescale.MILLI });
// Undefined while a live track already holds the name.
if (!track) throw new Error("track name taken");
const now = Moq.Time.Timestamp.now();
const group = track.appendGroup(now);
group.writeFrame({ payload: new TextEncoder().encode("hello"), timestamp: now });
Expand Down
2 changes: 2 additions & 0 deletions doc/lib/js/publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ signals.run((effect) => {
// still replays the last value. Each value is stamped with when it was
// written, so the track declares a timescale.
const track = net.createTrack("meta.json", { timescale: Moq.Time.Timescale.MILLI });
// Undefined while a live track already holds the name.
if (!track) return;
effect.cleanup(() => {
track.finish();
track.close();
Expand Down
2 changes: 2 additions & 0 deletions js/e2ee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const route = origin.route(await credential.path("meeting.hang"), {
const video = await new Broadcast.Producer(route.broadcast, generation).createTrack("video", {
timescale: Time.Timescale.MILLI,
});
// Undefined while a live track already holds the name.
if (!video) throw new Error("track name taken");
route.announce();

const now = Time.Timestamp.now();
Expand Down
11 changes: 7 additions & 4 deletions js/e2ee/bench/pump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Bun's WebCrypto stands in for the browser's, so compare runs on the same runtime.
import { Duration } from "@moq/clock";
import { Broadcast, Time } from "@moq/net";
import { defined } from "../src/defined.test-util.ts";
import { Credential } from "../src/index.ts";

const GROUP_FRAMES = 64;
Expand All @@ -25,10 +26,12 @@ for (const bytes of [160, 1_160, 16_384, 262_144]) {
for (let round = 0; round < 6; round++) {
const generation = credential.produce();
// No publisher limit: every group is sealed before the first is read.
const net = new Broadcast.Producer().createTrack(await generation.name("video"), {
timescale: Time.Timescale.MILLI,
maxAge: null,
});
const net = defined(
new Broadcast.Producer().createTrack(await generation.name("video"), {
timescale: Time.Timescale.MILLI,
maxAge: null,
}),
);
const consumer = credential
.consume(generation.epoch)
.consume(net.subscribe({ delay: { max: Duration.millis(60_000) } }), "video");
Expand Down
24 changes: 16 additions & 8 deletions js/e2ee/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,27 @@ export class Producer {
/**
* Create the track for `semantic` under its physical name.
*
* Rejects with {@link Failure} `identity` past 65535 bytes, `reuse` if this generation already
* produced the name (even on another broadcast), or the net error if the broadcast already
* holds it.
* Resolves `undefined` while the broadcast already holds it. Rejects with {@link Failure}
* `identity` past 65535 bytes, or `reuse` if this generation already produced the name (even
* on another broadcast).
*/
async createTrack(semantic: string, info: NetTrack.InfoInit = {}): Promise<Track.Producer> {
async createTrack(semantic: string, info: NetTrack.InfoInit = {}): Promise<Track.Producer | undefined> {
const name = await this.generation.name(semantic);
return this.generation.produce(this.#inner.createTrack(name, info), semantic);
const track = this.#inner.createTrack(name, info);
return track && this.generation.produce(track, semantic);
}

/** Create a protected datagram track in its own namespace and sequence space. */
async createDatagrams(semantic: string, info: Partial<NetDatagram.Info> = {}): Promise<Datagram.Producer> {
/**
* Create a protected datagram track in its own namespace and sequence space, or `undefined`
* while the broadcast already holds a live one of that name.
*/
async createDatagrams(
semantic: string,
info: Partial<NetDatagram.Info> = {},
): Promise<Datagram.Producer | undefined> {
const name = await this.generation.name(semantic);
return this.generation.produceDatagrams(this.#inner.createDatagrams(name, info), semantic);
const track = this.#inner.createDatagrams(name, info);
return track && this.generation.produceDatagrams(track, semantic);
}

/** End the wrapped broadcast, as `@moq/net`'s `close` does. Idempotent. */
Expand Down
5 changes: 5 additions & 0 deletions js/e2ee/src/defined.test-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** `value`, failing the test when it is `undefined`, as a name or group already taken. @internal */
export function defined<T>(value: T | undefined): T {
if (value === undefined) throw new Error("expected a value");
return value;
}
13 changes: 8 additions & 5 deletions js/e2ee/src/e2ee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import * as Protected from "./broadcast.ts";
import { Credential } from "./credential.ts";
import type * as Datagram from "./datagram.ts";
import { defined } from "./defined.test-util.ts";
import { DATAGRAM, GROUP } from "./derive.ts";
import { Failure } from "./failure.ts";
import type * as Generation from "./generation.ts";
Expand Down Expand Up @@ -46,7 +47,7 @@ const text = (value: string) => encoder.encode(value);
// No publisher limit, since the tests read groups a later one has superseded.
function netTrack(name: string, timed = true): NetTrack.Producer {
const timescale = timed ? Time.Timescale.MILLI : undefined;
return new Broadcast.Producer().createTrack(name, { timescale, maxAge: null });
return defined(new Broadcast.Producer().createTrack(name, { timescale, maxAge: null }));
}

function subscribe(net: NetTrack.Producer): NetTrack.Subscriber {
Expand Down Expand Up @@ -247,7 +248,7 @@ test("a broadcast speaks semantic names and puts physical names on the wire", as
const generation = credential().produce();
const net = new Broadcast.Producer();
const producer = new Protected.Producer(net, generation);
const video = await producer.createTrack("video", { timescale: Time.Timescale.MILLI });
const video = defined(await producer.createTrack("video", { timescale: Time.Timescale.MILLI }));
expect(video.name).toBe(await generation.name("video"));

const consumer = await new Protected.Consumer(
Expand Down Expand Up @@ -877,9 +878,11 @@ test("produce waits for an unaccepted track's timescale", async () => {

test("a timestamp out of range at the track's timescale spends nothing", async () => {
const generation = credential().produce();
const net = new Broadcast.Producer().createTrack(await generation.name("video"), {
timescale: Time.Timescale.NANO,
});
const net = defined(
new Broadcast.Producer().createTrack(await generation.name("video"), {
timescale: Time.Timescale.NANO,
}),
);
const consumer = generation.credential.consume(generation.epoch).consume(subscribe(net), "video");
const producer = await generation.produce(net, "video");
const huge = new Time.Timestamp(Number.MAX_SAFE_INTEGER, Time.Timescale.MILLI);
Expand Down
17 changes: 11 additions & 6 deletions js/e2ee/src/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type Broadcast, Connection, Origin, type Path, Time } from "@moq/net";
import { createMockTransportPair } from "../../net/src/mock.ts";
import * as Protected from "./broadcast.ts";
import { Credential } from "./credential.ts";
import { defined } from "./defined.test-util.ts";
import * as Epoch from "./epoch.ts";

const encoder = new TextEncoder();
Expand Down Expand Up @@ -68,9 +69,11 @@ for (const [name, protocol, routed, keyed] of transports) {
instance: { id: Epoch.instance(generation.epoch), epoch: 0n },
});
const broadcast = broadcastRoute.broadcast;
const video = await new Protected.Producer(broadcast, generation).createTrack("video", {
timescale: Time.Timescale.MILLI,
});
const video = defined(
await new Protected.Producer(broadcast, generation).createTrack("video", {
timescale: Time.Timescale.MILLI,
}),
);
broadcastRoute.announce();

// Starts before its first frame, so a keyed frame's time (its group's key) tells apart.
Expand Down Expand Up @@ -129,9 +132,11 @@ for (const [name, protocol] of [
instance: { id: Epoch.instance(generation.epoch), epoch: 0n },
});
const broadcast = broadcastRoute.broadcast;
const audio = await new Protected.Producer(broadcast, generation).createDatagrams("audio", {
timescale: Time.Timescale.MILLI,
});
const audio = defined(
await new Protected.Producer(broadcast, generation).createDatagrams("audio", {
timescale: Time.Timescale.MILLI,
}),
);
broadcastRoute.announce();

const remote = await active(subscriber, path);
Expand Down
15 changes: 9 additions & 6 deletions js/hang/src/catalog/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test } from "bun:test";
import { Duration } from "@moq/clock";
import * as Json from "@moq/json";
import * as Moq from "@moq/net";
import { defined } from "../defined.test-util.ts";
import { TRACK } from "./format";
import {
checkRenditions,
Expand Down Expand Up @@ -56,7 +57,7 @@ test("the shared containment check covers every section carrying a broadcast ref

test("watch yields a catalog with many renditions", async () => {
const broadcast = new Moq.Broadcast.Producer();
const track = broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI });
const track = defined(broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI }));
const producer = new Json.Snapshot.Producer<Root>({ track, deltas: false });
const consumer = watch(broadcast.consume())[Symbol.asyncIterator]();
producer.update(catalog(1000), Moq.Time.Timestamp.now());
Expand All @@ -68,7 +69,7 @@ test("watch yields a catalog with many renditions", async () => {

test("watch refuses a catalog needing a newer reader", async () => {
const broadcast = new Moq.Broadcast.Producer();
const track = broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI });
const track = defined(broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI }));
const producer = new Json.Snapshot.Producer<Root>({ track, deltas: false });
const consumer = watch(broadcast.consume())[Symbol.asyncIterator]();
producer.update({ version: VERSION + 1 }, Moq.Time.Timestamp.now());
Expand All @@ -90,7 +91,7 @@ async function watchRequested(root: Root) {
const origin = new Moq.Origin.Producer();
const broadcastRoute = origin.route(Moq.Path.from("room/alice"), { hidden: true });
const broadcast = broadcastRoute.broadcast;
const track = broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI });
const track = defined(broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI }));
const producer = new Json.Snapshot.Producer<Root>({ track, deltas: false });
broadcastRoute.announce();
const request = origin.request(Moq.Path.from("room/alice"));
Expand Down Expand Up @@ -119,7 +120,7 @@ test("watch accepts a sibling reference under the same root and yields it unreso

test("watch on a standalone broadcast refuses any parent reference", async () => {
const broadcast = new Moq.Broadcast.Producer();
const track = broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI });
const track = defined(broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI }));
const producer = new Json.Snapshot.Producer<Root>({ track, deltas: false });
const consumer = watch(broadcast.consume())[Symbol.asyncIterator]();
producer.update(referencing("../bob"), Moq.Time.Timestamp.now());
Expand All @@ -130,7 +131,7 @@ test("watch on a standalone broadcast refuses any parent reference", async () =>

test("watch yields each catalog update with the timestamp it takes effect at", async () => {
const broadcast = new Moq.Broadcast.Producer();
const track = broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI });
const track = defined(broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI }));
const producer = new Json.Snapshot.Producer<Root>({ track, deltas: false });
const consumer = watch(broadcast.consume())[Symbol.asyncIterator]();
for (const [count, ms] of [
Expand All @@ -156,7 +157,9 @@ test("a holding watcher reads every update it may still play, in order", async (
[Duration.millis(1001), [1000, 2000, 3000]],
] as const) {
const broadcast = new Moq.Broadcast.Producer();
const track = broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI, maxAge: Duration.secs(30) });
const track = defined(
broadcast.createTrack(TRACK, { timescale: Moq.Time.Timescale.MILLI, maxAge: Duration.secs(30) }),
);
const producer = new Json.Snapshot.Producer<Root>({ track, deltas: false });
// Subscribed before the updates land, as a live reader is; all three arrive before it reads.
const updates = watch(broadcast.consume(), { delay })[Symbol.asyncIterator]();
Expand Down
5 changes: 5 additions & 0 deletions js/hang/src/defined.test-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** `value`, failing the test when it is `undefined`, as a name or group already taken. @internal */
export function defined<T>(value: T | undefined): T {
if (value === undefined) throw new Error("expected a value");
return value;
}
4 changes: 3 additions & 1 deletion js/hang/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export class Writer<T, I = T> {
* publisher passes the schema's input (plain numbers) and readers get its output.
*/
constructor(broadcast: Moq.Broadcast.Producer, name: string, schema: z.ZodMiniType<T, I>) {
this.track = broadcast.createTrack(name, { priority: PRIORITY, compression: "deflate", refresh: REFRESH });
const track = broadcast.createTrack(name, { priority: PRIORITY, compression: "deflate", refresh: REFRESH });
if (!track) throw new Error(`the track ${name} already exists`);
this.track = track;
this.#producer = new Json.Snapshot.Producer<T>({ track: this.track });
this.#schema = schema;
}
Expand Down
8 changes: 4 additions & 4 deletions js/net/api/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ export namespace Broadcast {
close(): void;
get closed(): Signals.GetPromise<null>;
consume(): Broadcast.Consumer;
createDatagrams(name: string, info?: Partial<Datagram.Info>): Datagram.Producer;
createTrack(name: string, info?: Track.InfoInit): Track.Producer;
createDatagrams(name: string, info?: Partial<Datagram.Info>): Datagram.Producer | undefined;
createTrack(name: string, info?: Track.InfoInit): Track.Producer | undefined;
removeTrack(name: string): void;
track(name: string): Track.Consumer;
}
Expand Down Expand Up @@ -698,7 +698,7 @@ export namespace Track {
export type Fill = Track.FillGroups | Track.FillLookup;
export class FillGroups {
[Symbol.dispose](): void;
accept(start: bigint, info?: Track.Info): Group.Producer;
accept(start: bigint, info?: Track.Info): Group.Producer | undefined;
close(): void;
dropGroups(start: bigint, end: bigint): void;
readonly end: bigint;
Expand All @@ -715,7 +715,7 @@ export namespace Track {
}
export class FillLookup {
[Symbol.dispose](): void;
accept(start: bigint, info?: Track.Info): Group.Producer;
accept(start: bigint, info?: Track.Info): Group.Producer | undefined;
close(): void;
readonly kind: "lookup";
get maxDelay(): Clock.Duration | null;
Expand Down
Loading