@caprid/moq-core 0.0.0 → 0.1.0

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 +26 -2
  4. package/UPSTREAM.json +34 -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 +58 -0
  10. package/dist/protocol/bytes.d.ts.map +1 -0
  11. package/dist/protocol/bytes.js +210 -0
  12. package/dist/protocol/bytes.js.map +1 -0
  13. package/dist/protocol/control-messages.d.ts +544 -0
  14. package/dist/protocol/control-messages.d.ts.map +1 -0
  15. package/dist/protocol/control-messages.js +1110 -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 +128 -0
  22. package/dist/protocol/object-stream.d.ts.map +1 -0
  23. package/dist/protocol/object-stream.js +255 -0
  24. package/dist/protocol/object-stream.js.map +1 -0
  25. package/dist/protocol/publish-session.d.ts +175 -0
  26. package/dist/protocol/publish-session.d.ts.map +1 -0
  27. package/dist/protocol/publish-session.js +1026 -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 +334 -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,175 @@
1
+ import { type KeyValuePair, 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; one carrying anything else is answered with REQUEST_ERROR and ends the subscription.
67
+ */
68
+ onRequestUpdate?(info: {
69
+ requestId: number;
70
+ updateRequestId: number;
71
+ parameters: MessageParameters;
72
+ }): void;
73
+ /** The session ended — transport closed, or a fatal protocol error. */
74
+ onClosed?(info: {
75
+ error?: unknown;
76
+ }): void;
77
+ }
78
+ /** Options for {@link createMoqtPublishSession}. */
79
+ export interface MoqtPublishSessionConfig {
80
+ /** Extra Setup Options to send (an implementation identifier is added automatically). */
81
+ setupOptions?: KeyValuePair[];
82
+ /** Identifies this implementation in SETUP (§9.1.5). */
83
+ implementationName?: string;
84
+ /** Session event callbacks. */
85
+ callbacks?: MoqtPublishSessionCallbacks;
86
+ }
87
+ /**
88
+ * The largest encoded track property block every SUBSCRIBE_OK this session sends can carry. The
89
+ * properties ride in SUBSCRIBE_OK, a control message whose length field is 16 bits (§9). The rest
90
+ * of its body is the track alias, a varint of up to 9 bytes on the wire (it is the peer's request
91
+ * ID, so it is unknown until a subscription arrives), and the count of message parameters, of which
92
+ * this session sends none (1 byte). A longer block registers but can never answer a subscription.
93
+ */
94
+ export declare const MAX_TRACK_PROPERTIES_LENGTH: number;
95
+ /** A track for {@link MoqtPublishSession.registerTrack}. */
96
+ export interface RegisterTrackOptions {
97
+ /** Track metadata is explicit; raw tracks have no implied timestamp unit. */
98
+ trackProperties?: KeyValuePair[];
99
+ /** Track namespace the peer's SUBSCRIBE must name. */
100
+ trackNamespace: TrackNamespace;
101
+ /** Track name the peer's SUBSCRIBE must name. */
102
+ trackName: string;
103
+ }
104
+ /** A track registered with {@link MoqtPublishSession.registerTrack}. */
105
+ export interface RegisteredTrack {
106
+ /** The registered track name. */
107
+ readonly trackName: string;
108
+ /**
109
+ * End the track: end every live subscription (its data streams closed, then PUBLISH_DONE `TRACK_ENDED` and a FIN on
110
+ * its request stream) and refuse future SUBSCRIBEs with DOES_NOT_EXIST. Idempotent.
111
+ */
112
+ end(): void;
113
+ }
114
+ /** A publish-capable MOQT session over an established transport. Created by {@link createMoqtPublishSession}. */
115
+ export interface MoqtPublishSession {
116
+ /** Resolves when the server's SETUP has been received. */
117
+ readonly ready: Promise<void>;
118
+ /** Whether a current solicitation covers the desired namespace; undefined before any is accepted. */
119
+ readonly namespaceSolicited: boolean | undefined;
120
+ /**
121
+ * Announce the namespace on every matching solicitation — current and future. The wire write additionally waits for
122
+ * the first `registerTrack()`: an announce invites immediate SUBSCRIBEs, and an empty registry would answer them with
123
+ * a terminal DOES_NOT_EXIST. Result via `onAnnounced` / `onAnnounceEnded`.
124
+ */
125
+ announce(trackNamespace: TrackNamespace): void;
126
+ /** Register one servable track; the peer's SUBSCRIBEs are answered from this registry. */
127
+ registerTrack(options: RegisterTrackOptions): RegisteredTrack;
128
+ /**
129
+ * Open a unidirectional data stream for the live subscription bound to `trackAlias`. The stream counts toward that
130
+ * subscription's PUBLISH_DONE Stream Count, and a stream still open when the subscription ends is reset before
131
+ * PUBLISH_DONE is written (§9.9). Rejects once the session is closing, when no live subscription holds the alias,
132
+ * and when the subscription ends while the stream is opening (the stream is reset first). `options` go to the
133
+ * transport as they are.
134
+ */
135
+ openUniStream(trackAlias: number, options?: MoqtSendStreamOptions): Promise<WritableStream<Uint8Array>>;
136
+ /**
137
+ * Orderly stop: end every live subscription (PUBLISH_DONE `TRACK_ENDED`, then FIN) and retract the announce
138
+ * (NAMESPACE_DONE), give those writes a bounded window to reach the wire (closing a WebTransport discards queued
139
+ * data), then give the peer a bounded window to close each ended subscription's request stream, which it does once it
140
+ * has consumed the PUBLISH_DONE, and close the transport. Synchronous callers return immediately; the transport closes
141
+ * within `CLOSE_FLUSH_TIMEOUT_MS` (250 ms) plus `CLOSE_END_ACK_TIMEOUT_MS` (2 s), and sooner against a peer that
142
+ * answers promptly.
143
+ */
144
+ close(closeCode?: number, reason?: string): void;
145
+ /** Drain outgoing streams, preserving an optional local closure error. */
146
+ destroy(error?: unknown): void;
147
+ }
148
+ /**
149
+ * How long `close()` waits for the PUBLISH_DONE + FIN / NAMESPACE_DONE writes to drain before closing the transport out
150
+ * from under them.
151
+ */
152
+ export declare const CLOSE_FLUSH_TIMEOUT_MS = 250;
153
+ /**
154
+ * How long `close()` then waits for the peer to close each ended subscription's request stream. A write reaching the
155
+ * wire does not mean the peer has read it: moq-relay reads PUBLISH_DONE, waits for Stream Count data streams (or a 1 s
156
+ * grace when it cannot count them), finishes the track and only then FINs its side. A transport closed before that
157
+ * aborts the track, which viewers see as PUBLISH_DONE status 0 `internal error`. The bound covers the relay's grace
158
+ * plus a round trip, so a peer that never closes its side costs at most this much.
159
+ */
160
+ export declare const CLOSE_END_ACK_TIMEOUT_MS = 2000;
161
+ /**
162
+ * Create a publish-capable MOQT session driver over an established transport.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * const session = createMoqtPublishSession(transport, {
167
+ * callbacks: { onSubscribe: (s) => console.log('serving', s.trackName) },
168
+ * });
169
+ * await session.ready;
170
+ * session.announce(['live', 'abc123']);
171
+ * const track = session.registerTrack({ trackNamespace: ['live', 'abc123'], trackName: 'video' });
172
+ * ```;
173
+ */
174
+ export declare function createMoqtPublishSession(transport: MoqtTransport, config?: MoqtPublishSessionConfig): MoqtPublishSession;
175
+ //# 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":"AAgCA,OAAO,EAUL,KAAK,YAAY,EAGjB,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;IACl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