@caprid/moq-core 0.0.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/LICENSE +20 -0
  2. package/NOTICE +5 -0
  3. package/README.md +37 -2
  4. package/UPSTREAM.json +38 -0
  5. package/dist/index.d.ts +18 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +10 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/protocol/bytes.d.ts +59 -0
  10. package/dist/protocol/bytes.d.ts.map +1 -0
  11. package/dist/protocol/bytes.js +211 -0
  12. package/dist/protocol/bytes.js.map +1 -0
  13. package/dist/protocol/control-messages.d.ts +560 -0
  14. package/dist/protocol/control-messages.d.ts.map +1 -0
  15. package/dist/protocol/control-messages.js +1146 -0
  16. package/dist/protocol/control-messages.js.map +1 -0
  17. package/dist/protocol/errors.d.ts +64 -0
  18. package/dist/protocol/errors.d.ts.map +1 -0
  19. package/dist/protocol/errors.js +67 -0
  20. package/dist/protocol/errors.js.map +1 -0
  21. package/dist/protocol/object-stream.d.ts +133 -0
  22. package/dist/protocol/object-stream.d.ts.map +1 -0
  23. package/dist/protocol/object-stream.js +265 -0
  24. package/dist/protocol/object-stream.js.map +1 -0
  25. package/dist/protocol/publish-session.d.ts +196 -0
  26. package/dist/protocol/publish-session.d.ts.map +1 -0
  27. package/dist/protocol/publish-session.js +1174 -0
  28. package/dist/protocol/publish-session.js.map +1 -0
  29. package/dist/protocol/request-stream.d.ts +51 -0
  30. package/dist/protocol/request-stream.d.ts.map +1 -0
  31. package/dist/protocol/request-stream.js +94 -0
  32. package/dist/protocol/request-stream.js.map +1 -0
  33. package/dist/protocol/session.d.ts +340 -0
  34. package/dist/protocol/session.d.ts.map +1 -0
  35. package/dist/protocol/session.js +908 -0
  36. package/dist/protocol/session.js.map +1 -0
  37. package/dist/protocol/stream-reset.d.ts +10 -0
  38. package/dist/protocol/stream-reset.d.ts.map +1 -0
  39. package/dist/protocol/stream-reset.js +24 -0
  40. package/dist/protocol/stream-reset.js.map +1 -0
  41. package/dist/protocol/subgroup-writer.d.ts +67 -0
  42. package/dist/protocol/subgroup-writer.d.ts.map +1 -0
  43. package/dist/protocol/subgroup-writer.js +172 -0
  44. package/dist/protocol/subgroup-writer.js.map +1 -0
  45. package/dist/protocol/transport-error.d.ts +29 -0
  46. package/dist/protocol/transport-error.d.ts.map +1 -0
  47. package/dist/protocol/transport-error.js +39 -0
  48. package/dist/protocol/transport-error.js.map +1 -0
  49. package/dist/protocol/varint.d.ts +20 -0
  50. package/dist/protocol/varint.d.ts.map +1 -0
  51. package/dist/protocol/varint.js +99 -0
  52. package/dist/protocol/varint.js.map +1 -0
  53. package/dist/track-source.d.ts +13 -0
  54. package/dist/track-source.d.ts.map +1 -0
  55. package/dist/track-source.js +2 -0
  56. package/dist/track-source.js.map +1 -0
  57. package/package.json +31 -4
@@ -0,0 +1,196 @@
1
+ import { type KeyValuePair, type Location, type MessageParameters, type TrackNamespace } from './control-messages.js';
2
+ import type { Goaway, MoqtSendStreamOptions, MoqtTransport } from './session.js';
3
+ /** A SUBSCRIBE from the peer, as passed to `onSubscribe` once it has been answered with SUBSCRIBE_OK. */
4
+ export interface IncomingSubscribe {
5
+ /** The SUBSCRIBE's Request ID; the session also uses it as the subscription's Track Alias. */
6
+ requestId: number;
7
+ /** Track namespace requested. */
8
+ trackNamespace: TrackNamespace;
9
+ /** Track name requested. */
10
+ trackName: string;
11
+ /** Message parameters the SUBSCRIBE carried. */
12
+ parameters: MessageParameters;
13
+ }
14
+ /** Event callbacks for {@link createMoqtPublishSession}. All are optional. */
15
+ export interface MoqtPublishSessionCallbacks {
16
+ /** The server's SETUP arrived; the session is fully established. */
17
+ onReady?(serverOptions: KeyValuePair[]): void;
18
+ /** GOAWAY (control stream or a request stream): stop initiating, migrate. */
19
+ onGoaway?(goaway: Goaway): void;
20
+ /**
21
+ * The announced namespace reached the wire on an accepted SUBSCRIBE_NAMESPACE solicitation — the peer can now route
22
+ * SUBSCRIBEs to it. NAMESPACE entries have no acknowledgement of their own; this is the strongest "announced" fact
23
+ * the transport draft offers.
24
+ */
25
+ onAnnounced?(info: {
26
+ namespace: TrackNamespace;
27
+ }): void;
28
+ /**
29
+ * The solicitation stream carrying the announce ended while the session was still live — the peer rejected the
30
+ * announce (unauthorized suffix) or withdrew its namespace interest. Either way the ingest path is gone.
31
+ */
32
+ onAnnounceEnded?(info: {
33
+ error?: unknown;
34
+ }): void;
35
+ /** The peer subscribed to a registered track (already answered SUBSCRIBE_OK). */
36
+ onSubscribe?(subscribe: IncomingSubscribe): void;
37
+ /** A subscription ended (the peer's FIN or reset, or our PUBLISH_DONE). */
38
+ onSubscribeEnd?(info: {
39
+ requestId: number;
40
+ }): void;
41
+ /**
42
+ * The session could not build its SUBSCRIBE_OK for a SUBSCRIBE to a registered track, and answered REQUEST_ERROR
43
+ * `INTERNAL_ERROR` instead. The track stays registered and the session stays up, but a cause that lies in the track
44
+ * (its properties) fails every subscription to it. `error` is the encoder's failure.
45
+ */
46
+ onSubscribeFailed?(info: {
47
+ requestId: number;
48
+ trackName: string;
49
+ error: unknown;
50
+ }): void;
51
+ /**
52
+ * A track's effective alias binding changed. The most recent live subscription wins the binding (a replaced one ends
53
+ * with PUBLISH_DONE `TRACK_ENDED` — a clean end for that request, covering the peer's resubscribe races), and only
54
+ * while its Forward State
55
+ * permits data (`forward: 0` subscribes without authorizing it; REQUEST_UPDATE toggles it). `undefined` means no live
56
+ * forwarding subscription, so no data streams may be opened for the track.
57
+ */
58
+ onTrackBinding?(info: {
59
+ trackName: string;
60
+ trackAlias: number | undefined;
61
+ }): void;
62
+ /**
63
+ * REQUEST_UPDATE on an inbound request stream. `requestId` is the request being updated (the stream's SUBSCRIBE or
64
+ * SUBSCRIBE_NAMESPACE); `updateRequestId` is the update's own, freshly consumed Request ID (§6.4.2.1). A subscription
65
+ * update may carry Forward State, a subscriber priority and the two delivery timeouts (§5.2), which the session leaves
66
+ * to its caller, and FILL_PARAMETERS, which the session answers itself (§3.4.1); one carrying anything else is
67
+ * answered with REQUEST_ERROR and ends the subscription.
68
+ */
69
+ onRequestUpdate?(info: {
70
+ requestId: number;
71
+ updateRequestId: number;
72
+ parameters: MessageParameters;
73
+ }): void;
74
+ /** The session ended — transport closed, or a fatal protocol error. */
75
+ onClosed?(info: {
76
+ error?: unknown;
77
+ }): void;
78
+ }
79
+ /** Options for {@link createMoqtPublishSession}. */
80
+ export interface MoqtPublishSessionConfig {
81
+ /** Extra Setup Options to send (an implementation identifier is added automatically). */
82
+ setupOptions?: KeyValuePair[];
83
+ /** Identifies this implementation in SETUP (§9.1.5). */
84
+ implementationName?: string;
85
+ /** Session event callbacks. */
86
+ callbacks?: MoqtPublishSessionCallbacks;
87
+ }
88
+ /**
89
+ * The largest encoded track property block every SUBSCRIBE_OK this session sends can carry. The
90
+ * properties ride in SUBSCRIBE_OK, a control message whose length field is 16 bits (§9). The rest
91
+ * of its body is the track alias, a varint of up to 9 bytes on the wire (it is the peer's request
92
+ * ID, so it is unknown until a subscription arrives), and the count of message parameters, of which
93
+ * this session sends none (1 byte) unless the track reports a Largest Object (see
94
+ * {@link RegisterTrackOptions.getLargestObject}). A longer block registers but can never answer a subscription.
95
+ */
96
+ export declare const MAX_TRACK_PROPERTIES_LENGTH: number;
97
+ /** A track for {@link MoqtPublishSession.registerTrack}. */
98
+ export interface RegisterTrackOptions {
99
+ /** Track metadata is explicit; raw tracks have no implied timestamp unit. */
100
+ trackProperties?: KeyValuePair[];
101
+ /** Track namespace the peer's SUBSCRIBE must name. */
102
+ trackNamespace: TrackNamespace;
103
+ /** Track name the peer's SUBSCRIBE must name. */
104
+ trackName: string;
105
+ /**
106
+ * The track's Largest Object (§3.1.4), read each time the session answers a SUBSCRIBE (SUBSCRIBE_OK) or a
107
+ * REQUEST_UPDATE (REQUEST_OK), which then carry it as LARGEST_OBJECT (§9.20.17: a publisher that has published
108
+ * Objects MUST include it). `undefined`, or omitting this, means nothing has been published, and the parameter is left
109
+ * off. It also bounds every fill range (§3.4), so only a track that reports one can be asked for a nonempty fill.
110
+ * LARGEST_OBJECT takes up to 19 bytes of the SUBSCRIBE_OK that {@link MAX_TRACK_PROPERTIES_LENGTH} leaves to the track
111
+ * properties; a SUBSCRIBE_OK that no longer fits is refused like any other (`onSubscribeFailed`).
112
+ */
113
+ getLargestObject?: () => Location | undefined;
114
+ }
115
+ /** A track registered with {@link MoqtPublishSession.registerTrack}. */
116
+ export interface RegisteredTrack {
117
+ /** The registered track name. */
118
+ readonly trackName: string;
119
+ /**
120
+ * End the track: refuse future SUBSCRIBEs with DOES_NOT_EXIST at once, and end every live subscription (its data
121
+ * streams closed, then PUBLISH_DONE `TRACK_ENDED` and a FIN on its request stream). Idempotent; only the first call's
122
+ * options apply.
123
+ *
124
+ * With `options.after`, the subscriptions end once `after` settles (fulfilled or rejected) instead of now, so a last
125
+ * group still being written, such as the MSF end-of-broadcast catalog, reaches them first. Until then they keep their
126
+ * binding and `openUniStream()` serves them, even while the session is closing. Close the last group's stream before
127
+ * `after` settles: a stream still open then is reset like any other. An orderly `close()` waits for `after` within
128
+ * its `CLOSE_FLUSH_TIMEOUT_MS` drain window and then ends the subscriptions anyway.
129
+ */
130
+ end(options?: {
131
+ after?: PromiseLike<unknown>;
132
+ }): void;
133
+ }
134
+ /** A publish-capable MOQT session over an established transport. Created by {@link createMoqtPublishSession}. */
135
+ export interface MoqtPublishSession {
136
+ /** Resolves when the server's SETUP has been received. */
137
+ readonly ready: Promise<void>;
138
+ /** Whether a current solicitation covers the desired namespace; undefined before any is accepted. */
139
+ readonly namespaceSolicited: boolean | undefined;
140
+ /**
141
+ * Announce the namespace on every matching solicitation — current and future. The wire write additionally waits for
142
+ * the first `registerTrack()`: an announce invites immediate SUBSCRIBEs, and an empty registry would answer them with
143
+ * a terminal DOES_NOT_EXIST. Result via `onAnnounced` / `onAnnounceEnded`.
144
+ */
145
+ announce(trackNamespace: TrackNamespace): void;
146
+ /** Register one servable track; the peer's SUBSCRIBEs are answered from this registry. */
147
+ registerTrack(options: RegisterTrackOptions): RegisteredTrack;
148
+ /**
149
+ * Open a unidirectional data stream for the live subscription bound to `trackAlias`. The stream counts toward that
150
+ * subscription's PUBLISH_DONE Stream Count, and a stream still open when the subscription ends is reset before
151
+ * PUBLISH_DONE is written (§9.9). Rejects when no live subscription holds the alias, when the subscription ends while
152
+ * the stream is opening (the stream is reset first), and once the session is closing, except for a track whose
153
+ * `end({ after })` is still waiting: its last group may open a stream inside the close drain. `options` go to the
154
+ * transport as they are.
155
+ */
156
+ openUniStream(trackAlias: number, options?: MoqtSendStreamOptions): Promise<WritableStream<Uint8Array>>;
157
+ /**
158
+ * Orderly stop: end every live subscription (PUBLISH_DONE `TRACK_ENDED`, then FIN) and retract the announce
159
+ * (NAMESPACE_DONE), give those writes a bounded window to reach the wire (closing a WebTransport discards queued
160
+ * data; a track ended with `end({ after })` gets the same window for `after`, then ends regardless), then give the peer a bounded window to close each ended subscription's request stream, which it does once it
161
+ * has consumed the PUBLISH_DONE, and close the transport. Synchronous callers return immediately; the transport closes
162
+ * within `CLOSE_FLUSH_TIMEOUT_MS` (250 ms) plus `CLOSE_END_ACK_TIMEOUT_MS` (2 s), and sooner against a peer that
163
+ * answers promptly.
164
+ */
165
+ close(closeCode?: number, reason?: string): void;
166
+ /** Drain outgoing streams, preserving an optional local closure error. */
167
+ destroy(error?: unknown): void;
168
+ }
169
+ /**
170
+ * How long `close()` waits for the PUBLISH_DONE + FIN / NAMESPACE_DONE writes to drain before closing the transport out
171
+ * from under them.
172
+ */
173
+ export declare const CLOSE_FLUSH_TIMEOUT_MS = 250;
174
+ /**
175
+ * How long `close()` then waits for the peer to close each ended subscription's request stream. A write reaching the
176
+ * wire does not mean the peer has read it: moq-relay reads PUBLISH_DONE, waits for Stream Count data streams (or a 1 s
177
+ * grace when it cannot count them), finishes the track and only then FINs its side. A transport closed before that
178
+ * aborts the track, which viewers see as PUBLISH_DONE status 0 `internal error`. The bound covers the relay's grace
179
+ * plus a round trip, so a peer that never closes its side costs at most this much.
180
+ */
181
+ export declare const CLOSE_END_ACK_TIMEOUT_MS = 2000;
182
+ /**
183
+ * Create a publish-capable MOQT session driver over an established transport.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * const session = createMoqtPublishSession(transport, {
188
+ * callbacks: { onSubscribe: (s) => console.log('serving', s.trackName) },
189
+ * });
190
+ * await session.ready;
191
+ * session.announce(['live', 'abc123']);
192
+ * const track = session.registerTrack({ trackNamespace: ['live', 'abc123'], trackName: 'video' });
193
+ * ```;
194
+ */
195
+ export declare function createMoqtPublishSession(transport: MoqtTransport, config?: MoqtPublishSessionConfig): MoqtPublishSession;
196
+ //# sourceMappingURL=publish-session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publish-session.d.ts","sourceRoot":"","sources":["../../src/protocol/publish-session.ts"],"names":[],"mappings":"AAqCA,OAAO,EAYL,KAAK,YAAY,EACjB,KAAK,QAAQ,EAGb,KAAK,iBAAiB,EAOtB,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AAI/B,OAAO,KAAK,EAAE,MAAM,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAQjF,yGAAyG;AACzG,MAAM,WAAW,iBAAiB;IAChC,8FAA8F;IAC9F,SAAS,EAAE,MAAM,CAAC;IAClB,iCAAiC;IACjC,cAAc,EAAE,cAAc,CAAC;IAC/B,4BAA4B;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,UAAU,EAAE,iBAAiB,CAAC;CAC/B;AAED,8EAA8E;AAC9E,MAAM,WAAW,2BAA2B;IAC1C,oEAAoE;IACpE,OAAO,CAAC,CAAC,aAAa,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC;IAC9C,6EAA6E;IAC7E,QAAQ,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC;;;;OAIG;IACH,WAAW,CAAC,CA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