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. * * The entry says how to read the track, so a consumer needs the track name and nothing else about * the application. * * A *loose* object: fields this build doesn't recognize pass through untouched, so an entry using * a future mode round-trips rather than losing the fields that describe it. */ export const JsonConfigSchema = z.looseObject({ // Optional reference to another broadcast that publishes this track, expressed // relative to the broadcast that served this catalog (e.g. "./source"). // If unset, the track lives in the same broadcast as the catalog. broadcast: z.optional(RelativeBroadcastSchema), // The instance of the stream the reference names, when known. Absent accepts whichever stream // resolves first. A route of another instance fails the entry; a route over a wire that // carries no instance has one its session minted, which proves nothing, so there it is best // effort. Without `broadcast`, it names the catalog's own stream. instance: z.optional(InstanceSchema), // The track's name in its broadcast, when it differs from this entry's ID (its key in the // catalog). Absent means the ID is the track name. Resolve it with `track`. track: z.optional(z.string()), // Whether the track is a latest-value document or an append log. Always stated. mode: ModeSchema, // An optional identifier for the shape of each value, typically a JSON Schema URL. // Purely descriptive: a consumer that doesn't recognize it can still read the track. schema: z.optional(z.string()), // The maximum bitrate of the track in bits per second, if known. bitrate: z.optional(u53Schema), // The largest media span the publisher hands to the transport behind a payload's timestamp, in // whole milliseconds rounded up, with the same meaning as a video rendition's `jitter`. jitter: z.optional( z.pipe( u53Schema, z.transform((value) => (value === 0 ? undefined : value)), ), ), // How far this track's payloads persistently trail what the broadcast's other tracks already // wrote, with the same meaning and encoding as a video rendition's `delay`. Only measured for // payloads that carry a capture time. delay: z.optional( z.pipe( u53Schema, z.transform((value) => (value === 0 ? undefined : value)), ), ), }); /** * Schema for the catalog `json` section: a map of track name to config. * * Not a rendition set: entries are distinct tracks, not alternatives to choose between. The map * key is the track name to subscribe to. */ export const JsonSchema = z.object({ tracks: renditions(JsonConfigSchema), }); /** The catalog JSON section: data tracks keyed by track name. */ export type Json = z.infer; /** How to read one JSON track. */ export type JsonConfig = z.infer;