@caprid/relay 0.0.0-stage → 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 (103) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +161 -2
  3. package/dist/connect-session.d.ts +125 -0
  4. package/dist/connect-session.d.ts.map +1 -0
  5. package/dist/connect-session.js +300 -0
  6. package/dist/connect-session.js.map +1 -0
  7. package/dist/connect.d.ts +143 -0
  8. package/dist/connect.d.ts.map +1 -0
  9. package/dist/connect.js +129 -0
  10. package/dist/connect.js.map +1 -0
  11. package/dist/delivery-timeouts.d.ts +37 -0
  12. package/dist/delivery-timeouts.d.ts.map +1 -0
  13. package/dist/delivery-timeouts.js +48 -0
  14. package/dist/delivery-timeouts.js.map +1 -0
  15. package/dist/document-service.d.ts +58 -0
  16. package/dist/document-service.d.ts.map +1 -0
  17. package/dist/document-service.js +166 -0
  18. package/dist/document-service.js.map +1 -0
  19. package/dist/errors.d.ts +71 -0
  20. package/dist/errors.d.ts.map +1 -0
  21. package/dist/errors.js +99 -0
  22. package/dist/errors.js.map +1 -0
  23. package/dist/frame-reader.d.ts +81 -0
  24. package/dist/frame-reader.d.ts.map +1 -0
  25. package/dist/frame-reader.js +110 -0
  26. package/dist/frame-reader.js.map +1 -0
  27. package/dist/frame-writer.d.ts +109 -0
  28. package/dist/frame-writer.d.ts.map +1 -0
  29. package/dist/frame-writer.js +409 -0
  30. package/dist/frame-writer.js.map +1 -0
  31. package/dist/index.d.ts +19 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +9 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/media-track-properties.d.ts +59 -0
  36. package/dist/media-track-properties.d.ts.map +1 -0
  37. package/dist/media-track-properties.js +120 -0
  38. package/dist/media-track-properties.js.map +1 -0
  39. package/dist/media.d.ts +28 -0
  40. package/dist/media.d.ts.map +1 -0
  41. package/dist/media.js +28 -0
  42. package/dist/media.js.map +1 -0
  43. package/dist/namespace-events.d.ts +44 -0
  44. package/dist/namespace-events.d.ts.map +1 -0
  45. package/dist/namespace-events.js +107 -0
  46. package/dist/namespace-events.js.map +1 -0
  47. package/dist/node.d.ts +75 -0
  48. package/dist/node.d.ts.map +1 -0
  49. package/dist/node.js +114 -0
  50. package/dist/node.js.map +1 -0
  51. package/dist/payload.d.ts +8 -0
  52. package/dist/payload.d.ts.map +1 -0
  53. package/dist/payload.js +23 -0
  54. package/dist/payload.js.map +1 -0
  55. package/dist/publish.d.ts +209 -0
  56. package/dist/publish.d.ts.map +1 -0
  57. package/dist/publish.js +123 -0
  58. package/dist/publish.js.map +1 -0
  59. package/dist/room-endpoint.d.ts +21 -0
  60. package/dist/room-endpoint.d.ts.map +1 -0
  61. package/dist/room-endpoint.js +47 -0
  62. package/dist/room-endpoint.js.map +1 -0
  63. package/dist/send-order.d.ts +14 -0
  64. package/dist/send-order.d.ts.map +1 -0
  65. package/dist/send-order.js +11 -0
  66. package/dist/send-order.js.map +1 -0
  67. package/dist/settling-groups.d.ts +35 -0
  68. package/dist/settling-groups.d.ts.map +1 -0
  69. package/dist/settling-groups.js +73 -0
  70. package/dist/settling-groups.js.map +1 -0
  71. package/dist/subscription-events.d.ts +51 -0
  72. package/dist/subscription-events.d.ts.map +1 -0
  73. package/dist/subscription-events.js +125 -0
  74. package/dist/subscription-events.js.map +1 -0
  75. package/dist/subscription.d.ts +207 -0
  76. package/dist/subscription.d.ts.map +1 -0
  77. package/dist/subscription.js +283 -0
  78. package/dist/subscription.js.map +1 -0
  79. package/dist/supervised-publisher.d.ts +92 -0
  80. package/dist/supervised-publisher.d.ts.map +1 -0
  81. package/dist/supervised-publisher.js +839 -0
  82. package/dist/supervised-publisher.js.map +1 -0
  83. package/dist/supervised-subscriber.d.ts +201 -0
  84. package/dist/supervised-subscriber.d.ts.map +1 -0
  85. package/dist/supervised-subscriber.js +1420 -0
  86. package/dist/supervised-subscriber.js.map +1 -0
  87. package/dist/supervision.d.ts +78 -0
  88. package/dist/supervision.d.ts.map +1 -0
  89. package/dist/supervision.js +149 -0
  90. package/dist/supervision.js.map +1 -0
  91. package/dist/switchable.d.ts +164 -0
  92. package/dist/switchable.d.ts.map +1 -0
  93. package/dist/switchable.js +327 -0
  94. package/dist/switchable.js.map +1 -0
  95. package/dist/validation.d.ts +64 -0
  96. package/dist/validation.d.ts.map +1 -0
  97. package/dist/validation.js +137 -0
  98. package/dist/validation.js.map +1 -0
  99. package/dist/write-slot.d.ts +87 -0
  100. package/dist/write-slot.d.ts.map +1 -0
  101. package/dist/write-slot.js +519 -0
  102. package/dist/write-slot.js.map +1 -0
  103. package/package.json +82 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delivery-timeouts.d.ts","sourceRoot":"","sources":["../src/delivery-timeouts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAGxE,sGAAsG;AACtG,MAAM,WAAW,gBAAgB;IAC/B,2FAA2F;IAC3F,QAAQ,EAAE,MAAM,CAAC;IACjB,0GAA0G;IAC1G,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,+EAA+E;AAC/E,eAAO,MAAM,uBAAuB,IAAO,CAAC;AAC5C,iFAAiF;AACjF,eAAO,MAAM,yBAAyB,IAAO,CAAC;AAE9C,iCAAiC;AACjC,eAAO,MAAM,oBAAoB,EAAE,gBAAgE,CAAC;AAUpG;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,UAAU,EAAE,SAAS,YAAY,EAAE,EACnC,QAAQ,GAAE,gBAAuC,GAChD,gBAAgB,CAKlB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,iBAAiB,EAC7B,QAAQ,GAAE,gBAAuC,GAChD,gBAAgB,CAKlB;AAQD,4FAA4F;AAC5F,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,gBAAgB,EAAE,UAAU,EAAE,gBAAgB,GAAG,gBAAgB,CAK7G"}
@@ -0,0 +1,48 @@
1
+ import { MAX_TIMEOUT_MS } from './connect-session.js';
2
+ /** Property and parameter type of OBJECT_DELIVERY_TIMEOUT (§9.20.4, §10.2). */
3
+ export const OBJECT_DELIVERY_TIMEOUT = 0x02;
4
+ /** Property and parameter type of SUBGROUP_DELIVERY_TIMEOUT (§9.20.3, §10.1). */
5
+ export const SUBGROUP_DELIVERY_TIMEOUT = 0x06;
6
+ /** No timeout of either kind. */
7
+ export const NO_DELIVERY_TIMEOUTS = Object.freeze({ objectMs: 0, subgroupMs: 0 });
8
+ /** The first integer value of `type` among `properties`, if any. A byte value is not a timeout. */
9
+ function propertyValue(properties, type) {
10
+ for (const property of properties) {
11
+ if (property.type === type && typeof property.value === 'number')
12
+ return property.value;
13
+ }
14
+ return undefined;
15
+ }
16
+ /**
17
+ * The publisher's values for a group: each timeout from the group's first object's properties when present there,
18
+ * and from the track's otherwise (§5.2). Pass the track's own values as `fallback`.
19
+ */
20
+ export function publisherTimeouts(properties, fallback = NO_DELIVERY_TIMEOUTS) {
21
+ return {
22
+ objectMs: propertyValue(properties, OBJECT_DELIVERY_TIMEOUT) ?? fallback.objectMs,
23
+ subgroupMs: propertyValue(properties, SUBGROUP_DELIVERY_TIMEOUT) ?? fallback.subgroupMs,
24
+ };
25
+ }
26
+ /**
27
+ * A subscription's values after `parameters`: each timeout the message carries replaces the previous one, and an
28
+ * absent one keeps it (§9.5: a parameter not present in REQUEST_UPDATE keeps its value).
29
+ */
30
+ export function subscriberTimeouts(parameters, previous = NO_DELIVERY_TIMEOUTS) {
31
+ return {
32
+ objectMs: parameters.objectDeliveryTimeout ?? previous.objectMs,
33
+ subgroupMs: parameters.subgroupDeliveryTimeout ?? previous.subgroupMs,
34
+ };
35
+ }
36
+ /** The smaller non-zero of two values, clamped to the timer bound; 0 when neither is set. */
37
+ function smallerNonZero(a, b) {
38
+ const set = [a, b].filter((value) => value > 0);
39
+ return set.length === 0 ? 0 : Math.min(MAX_TIMEOUT_MS, ...set);
40
+ }
41
+ /** A group's effective timeouts from the publisher's and the subscriber's values (§5.2). */
42
+ export function effectiveTimeouts(publisher, subscriber) {
43
+ return {
44
+ objectMs: smallerNonZero(publisher.objectMs, subscriber.objectMs),
45
+ subgroupMs: smallerNonZero(publisher.subgroupMs, subscriber.subgroupMs),
46
+ };
47
+ }
48
+ //# sourceMappingURL=delivery-timeouts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delivery-timeouts.js","sourceRoot":"","sources":["../src/delivery-timeouts.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAUtD,+EAA+E;AAC/E,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,CAAC;AAC5C,iFAAiF;AACjF,MAAM,CAAC,MAAM,yBAAyB,GAAG,IAAI,CAAC;AAE9C,iCAAiC;AACjC,MAAM,CAAC,MAAM,oBAAoB,GAAqB,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC;AAEpG,mGAAmG;AACnG,SAAS,aAAa,CAAC,UAAmC,EAAE,IAAY;IACtE,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;QAClC,IAAI,QAAQ,CAAC,IAAI,KAAK,IAAI,IAAI,OAAO,QAAQ,CAAC,KAAK,KAAK,QAAQ;YAAE,OAAO,QAAQ,CAAC,KAAK,CAAC;IAC1F,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,UAAmC,EACnC,WAA6B,oBAAoB;IAEjD,OAAO;QACL,QAAQ,EAAE,aAAa,CAAC,UAAU,EAAE,uBAAuB,CAAC,IAAI,QAAQ,CAAC,QAAQ;QACjF,UAAU,EAAE,aAAa,CAAC,UAAU,EAAE,yBAAyB,CAAC,IAAI,QAAQ,CAAC,UAAU;KACxF,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAChC,UAA6B,EAC7B,WAA6B,oBAAoB;IAEjD,OAAO;QACL,QAAQ,EAAE,UAAU,CAAC,qBAAqB,IAAI,QAAQ,CAAC,QAAQ;QAC/D,UAAU,EAAE,UAAU,CAAC,uBAAuB,IAAI,QAAQ,CAAC,UAAU;KACtE,CAAC;AACJ,CAAC;AAED,6FAA6F;AAC7F,SAAS,cAAc,CAAC,CAAS,EAAE,CAAS;IAC1C,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;IAChD,OAAO,GAAG,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,cAAc,EAAE,GAAG,GAAG,CAAC,CAAC;AACjE,CAAC;AAED,4FAA4F;AAC5F,MAAM,UAAU,iBAAiB,CAAC,SAA2B,EAAE,UAA4B;IACzF,OAAO;QACL,QAAQ,EAAE,cAAc,CAAC,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC,QAAQ,CAAC;QACjE,UAAU,EAAE,cAAc,CAAC,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC,UAAU,CAAC;KACxE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,58 @@
1
+ import type { RelayTrackWriter } from './publish.js';
2
+ /** Options for {@link createCatalogService}. */
3
+ export interface CatalogServiceOptions {
4
+ /** Pause before retrying a write that failed while the track stayed open. Default 1000 ms. */
5
+ retryDelayMs?: number;
6
+ }
7
+ /** Options for {@link createDocumentService}. */
8
+ export type DocumentServiceOptions = CatalogServiceOptions;
9
+ /** Options for {@link CatalogService.serve}. */
10
+ export interface ServeOptions {
11
+ /** Aborting stops the service, cancelling a write in progress; `serve` then rejects with the signal's reason. */
12
+ signal?: AbortSignal;
13
+ }
14
+ /**
15
+ * Serves a state-shaped track: sends its document on every relay subscription. Create one with
16
+ * {@link createCatalogService}.
17
+ */
18
+ export interface CatalogService {
19
+ /**
20
+ * Run until the signal aborts or the track ends. Resolves when the track ends without a write having failed. Rejects
21
+ * with the `RelayError` that ended the track when a write exposed it, so a retired catalog cannot go unnoticed.
22
+ */
23
+ serve(options?: ServeOptions): Promise<void>;
24
+ /** Documents the transport accepted. */
25
+ readonly sends: number;
26
+ /** Writes that failed while the track stayed open, each followed by a retry or by lost demand. */
27
+ readonly failures: number;
28
+ }
29
+ /**
30
+ * Serves a state-shaped track whose document the application replaces, such as the media timeline. Create one with
31
+ * {@link createDocumentService}.
32
+ */
33
+ export interface DocumentService extends CatalogService {
34
+ /** Replace the document, and send it if the relay is subscribed. Returns at once. */
35
+ update(document: ArrayBufferView): void;
36
+ /** The latest document, or `undefined` before the first. */
37
+ readonly document: Uint8Array | undefined;
38
+ }
39
+ /**
40
+ * Serve a state-shaped track, such as an MSF catalog: send `document` as one independent group on the relay's current
41
+ * subscription and again on every later one, including a replacement (spec/behavior.md, "Media publishing"). Viewers
42
+ * who join during one relay subscription get the relay's cached copy. A write that fails while the track stays open is
43
+ * retried after `retryDelayMs` for as long as demand remains; a failure that ends the track ends the service.
44
+ *
45
+ * ```ts
46
+ * const catalog = createCatalogService(publisher.registerTrack({ track: CATALOG_TRACK }), buildMsfCatalog(input));
47
+ * void catalog.serve({ signal });
48
+ * ```
49
+ */
50
+ export declare function createCatalogService(track: RelayTrackWriter, document: ArrayBufferView, options?: CatalogServiceOptions): CatalogService;
51
+ /**
52
+ * Serve a state-shaped track whose document the application replaces, such as the msf-01 media timeline: the latest
53
+ * document goes out as a new group on every relay subscription, as {@link createCatalogService} does, and again on
54
+ * every `update()` while the relay is subscribed. Nothing is sent before the first document, and an update without
55
+ * relay demand only replaces it.
56
+ */
57
+ export declare function createDocumentService(track: RelayTrackWriter, document?: ArrayBufferView, options?: DocumentServiceOptions): DocumentService;
58
+ //# sourceMappingURL=document-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document-service.d.ts","sourceRoot":"","sources":["../src/document-service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGrD,gDAAgD;AAChD,MAAM,WAAW,qBAAqB;IACpC,8FAA8F;IAC9F,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,iDAAiD;AACjD,MAAM,MAAM,sBAAsB,GAAG,qBAAqB,CAAC;AAE3D,gDAAgD;AAChD,MAAM,WAAW,YAAY;IAC3B,iHAAiH;IACjH,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,KAAK,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,wCAAwC;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,kGAAkG;IAClG,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED;;;GAGG;AACH,MAAM,WAAW,eAAgB,SAAQ,cAAc;IACrD,qFAAqF;IACrF,MAAM,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAAC;IACxC,4DAA4D;IAC5D,QAAQ,CAAC,QAAQ,EAAE,UAAU,GAAG,SAAS,CAAC;CAC3C;AAsHD;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,eAAe,EACzB,OAAO,GAAE,qBAA0B,GAClC,cAAc,CAahB;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,CAAC,EAAE,eAAe,EAC1B,OAAO,GAAE,sBAA2B,GACnC,eAAe,CA0BjB"}
@@ -0,0 +1,166 @@
1
+ import { RelayError } from './errors.js';
2
+ import { payloadBytes } from './validation.js';
3
+ function retryDelay(options) {
4
+ if (typeof options !== 'object' || options === null)
5
+ throw new TypeError('options must be an object');
6
+ const delay = options.retryDelayMs ?? 1000;
7
+ if (typeof delay !== 'number' || Number.isNaN(delay))
8
+ throw new TypeError('retryDelayMs must be a number');
9
+ if (delay < 0 || delay > 2147483647)
10
+ throw new RangeError('retryDelayMs must be from 0 to 2147483647');
11
+ return delay;
12
+ }
13
+ /** Wait `ms`, or until `cut` is called; reject with the signal's reason if it aborts. */
14
+ function sleep(ms, signal, onCut) {
15
+ return new Promise((resolve, reject) => {
16
+ const done = () => {
17
+ clearTimeout(timer);
18
+ signal?.removeEventListener('abort', abort);
19
+ onCut(undefined);
20
+ resolve();
21
+ };
22
+ const abort = () => {
23
+ clearTimeout(timer);
24
+ onCut(undefined);
25
+ reject(signal?.reason);
26
+ };
27
+ const timer = setTimeout(done, ms);
28
+ signal?.addEventListener('abort', abort, { once: true });
29
+ onCut(done);
30
+ });
31
+ }
32
+ /** Send the latest document on every relay subscription and on every update while the relay is subscribed. */
33
+ async function serveDocument(track, state, delayMs, { signal }) {
34
+ signal?.throwIfAborted();
35
+ const changes = track.demandChanges();
36
+ let wake;
37
+ /** Ends a retry pause early: set only while one runs. */
38
+ let cutPause;
39
+ let finished = false;
40
+ const notify = () => {
41
+ const woken = wake;
42
+ wake = undefined;
43
+ woken?.();
44
+ };
45
+ state.notify = notify;
46
+ const watching = (async () => {
47
+ try {
48
+ // The current state first, then a repeated `true` for every new or replacement relay subscription.
49
+ for await (const demanded of changes) {
50
+ if (demanded) {
51
+ state.due = true;
52
+ notify();
53
+ }
54
+ }
55
+ }
56
+ finally {
57
+ finished = true;
58
+ notify();
59
+ cutPause?.();
60
+ }
61
+ })();
62
+ watching.catch(() => { });
63
+ const abort = () => {
64
+ void changes.return?.();
65
+ notify();
66
+ };
67
+ signal?.addEventListener('abort', abort, { once: true });
68
+ try {
69
+ for (;;) {
70
+ signal?.throwIfAborted();
71
+ const document = state.document;
72
+ if (!state.due || !document || !track.hasDemand) {
73
+ // The track ended (or the demand iterator failed, which is raised here).
74
+ if (finished)
75
+ return await watching;
76
+ await new Promise((resolve) => {
77
+ wake = resolve;
78
+ });
79
+ continue;
80
+ }
81
+ state.due = false;
82
+ try {
83
+ await track.write(document, signal ? { signal } : {});
84
+ state.sends++;
85
+ }
86
+ catch (error) {
87
+ signal?.throwIfAborted();
88
+ if (!(error instanceof RelayError) || error.code === 'CLOSED' || track.closed)
89
+ throw error;
90
+ state.failures++;
91
+ // Losing demand ends the retries; the next relay subscription makes the document due again.
92
+ if (!track.hasDemand)
93
+ continue;
94
+ state.due = true;
95
+ // The track ending cuts the pause short: the loop then returns rather than wait out the delay.
96
+ await sleep(delayMs, signal, (cut) => {
97
+ cutPause = cut;
98
+ });
99
+ }
100
+ }
101
+ }
102
+ finally {
103
+ signal?.removeEventListener('abort', abort);
104
+ state.notify = undefined;
105
+ void changes.return?.();
106
+ }
107
+ }
108
+ /**
109
+ * Serve a state-shaped track, such as an MSF catalog: send `document` as one independent group on the relay's current
110
+ * subscription and again on every later one, including a replacement (spec/behavior.md, "Media publishing"). Viewers
111
+ * who join during one relay subscription get the relay's cached copy. A write that fails while the track stays open is
112
+ * retried after `retryDelayMs` for as long as demand remains; a failure that ends the track ends the service.
113
+ *
114
+ * ```ts
115
+ * const catalog = createCatalogService(publisher.registerTrack({ track: CATALOG_TRACK }), buildMsfCatalog(input));
116
+ * void catalog.serve({ signal });
117
+ * ```
118
+ */
119
+ export function createCatalogService(track, document, options = {}) {
120
+ // Arguments first, as everywhere in the SDK: a bad document never reaches the track.
121
+ const state = { document: payloadBytes(document), due: false, sends: 0, failures: 0, notify: undefined };
122
+ const delayMs = retryDelay(options);
123
+ return {
124
+ serve: (serveOptions = {}) => serveDocument(track, state, delayMs, serveOptions),
125
+ get sends() {
126
+ return state.sends;
127
+ },
128
+ get failures() {
129
+ return state.failures;
130
+ },
131
+ };
132
+ }
133
+ /**
134
+ * Serve a state-shaped track whose document the application replaces, such as the msf-01 media timeline: the latest
135
+ * document goes out as a new group on every relay subscription, as {@link createCatalogService} does, and again on
136
+ * every `update()` while the relay is subscribed. Nothing is sent before the first document, and an update without
137
+ * relay demand only replaces it.
138
+ */
139
+ export function createDocumentService(track, document, options = {}) {
140
+ const state = {
141
+ document: document === undefined ? undefined : payloadBytes(document),
142
+ due: false,
143
+ sends: 0,
144
+ failures: 0,
145
+ notify: undefined,
146
+ };
147
+ const delayMs = retryDelay(options);
148
+ return {
149
+ serve: (serveOptions = {}) => serveDocument(track, state, delayMs, serveOptions),
150
+ update(next) {
151
+ state.document = payloadBytes(next);
152
+ state.due = true;
153
+ state.notify?.();
154
+ },
155
+ get document() {
156
+ return state.document;
157
+ },
158
+ get sends() {
159
+ return state.sends;
160
+ },
161
+ get failures() {
162
+ return state.failures;
163
+ },
164
+ };
165
+ }
166
+ //# sourceMappingURL=document-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document-service.js","sourceRoot":"","sources":["../src/document-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAoD/C,SAAS,UAAU,CAAC,OAA8B;IAChD,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI;QAAE,MAAM,IAAI,SAAS,CAAC,2BAA2B,CAAC,CAAC;IACtG,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,IAAI,IAAI,CAAC;IAC3C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,+BAA+B,CAAC,CAAC;IAC3G,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,UAAU;QAAE,MAAM,IAAI,UAAU,CAAC,2CAA2C,CAAC,CAAC;IACvG,OAAO,KAAK,CAAC;AACf,CAAC;AAED,yFAAyF;AACzF,SAAS,KAAK,CACZ,EAAU,EAC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,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,CAAC;QAChF,IAAI,KAAK;YACP,OAAO,KAAK,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,IAAI,QAAQ;YACV,OAAO,KAAK,CAAC,QAAQ,CAAC;QACxB,CAAC;KACF,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CACnC,KAAuB,EACvB,QAA0B,EAC1B,UAAkC,EAAE;IAEpC,MAAM,KAAK,GAAU;QACnB,QAAQ,EAAE,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC;QACrE,GAAG,EAAE,KAAK;QACV,KAAK,EAAE,CAAC;QACR,QAAQ,EAAE,CAAC;QACX,MAAM,EAAE,SAAS;KAClB,CAAC;IACF,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IACpC,OAAO;QACL,KAAK,EAAE,CAAC,YAAY,GAAG,EAAE,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,CAAC;QAChF,MAAM,CAAC,IAAI;YACT,KAAK,CAAC,QAAQ,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;YACpC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC;YACjB,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC;QACnB,CAAC;QACD,IAAI,QAAQ;YACV,OAAO,KAAK,CAAC,QAAQ,CAAC;QACxB,CAAC;QACD,IAAI,KAAK;YACP,OAAO,KAAK,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,IAAI,QAAQ;YACV,OAAO,KAAK,CAAC,QAAQ,CAAC;QACxB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,71 @@
1
+ /** Stable categories for SDK lifecycle failures; messages are diagnostic text only. */
2
+ export type RelayErrorCode = 'CONNECTION_TIMEOUT' | 'WRITE_TIMEOUT'
3
+ /**
4
+ * An object outlived its delivery timeout (draft-ietf-moq-transport-22 §5.2) before it reached the transport: the
5
+ * smaller non-zero of the publisher's OBJECT_DELIVERY_TIMEOUT (`0x02`) and the relay subscription's. The data is
6
+ * stale; do not retry it as it is.
7
+ */
8
+ | 'DELIVERY_TIMEOUT' | 'CLOSED' | 'WRITE_PENDING' | 'NAMESPACE_NOT_SOLICITED' | 'NAMESPACE_ENDED' | 'SUBSCRIPTION_CHANGED' | 'SUBSCRIPTION_REQUEST_FAILED' | 'SUBSCRIPTION_OVERFLOW' | 'UPDATE_QUEUE_FULL'
9
+ /**
10
+ * `namespaceEvents` refused a prefix that overlaps (equals, contains or falls under) one this connection is already
11
+ * discovering, in phase `subscription`; nothing was sent. Draft-22 §9.15 allows one SUBSCRIBE_NAMESPACE per prefix
12
+ * tree on a session. Widen one iterator's prefix and filter, or use a second connection.
13
+ */
14
+ | 'PREFIX_OVERLAP'
15
+ /**
16
+ * A supervised publisher's track was written while no generation was up, or the connection under the operation
17
+ * ended, in phase `connection`. Nothing is replayed: write the next object once the publisher is live again. A frame
18
+ * writer treats it as an interruption, not a retirement.
19
+ */
20
+ | 'RECONNECTING' | 'GOAWAY' | 'TRANSPORT_ERROR' | 'TRANSPORT_UNAVAILABLE'
21
+ /**
22
+ * The relay refused the connection's credentials: in phase `connecting`, a refusal to connect; in phase
23
+ * `connection`, the end of a live session whose token expired or whose grant was revoked. Over native QUIC
24
+ * (`@caprid/relay/node`) this is always reported, and so it is in a browser against moq-relay 0.17.0 or later, which
25
+ * delivers its CLOSE_WEBTRANSPORT_SESSION (code 2, `unauthorized`). Against older relays a browser loses that close,
26
+ * and it arrives as `TRANSPORT_ERROR` (muxinc/relay-sdks#14).
27
+ */
28
+ | 'UNAUTHORIZED';
29
+ /** Where the failure originated, not a delivery acknowledgment or a safe-retry flag. */
30
+ export type RelayErrorPhase = 'connecting' | 'connection' | 'subscription' | 'waiting-for-demand' | 'opening-stream' | 'writing' | 'closing';
31
+ /** Caller cancellation reasons and argument errors are not converted to RelayError. */
32
+ export declare class RelayError extends Error {
33
+ /** `'RelayError'`, or the subclass name for the three subscription errors. */
34
+ readonly name: string;
35
+ /** The stable failure category; branch on this, not on `message` or the class. */
36
+ readonly code: RelayErrorCode;
37
+ /** The step that was running when the failure happened. */
38
+ readonly phase: RelayErrorPhase;
39
+ /**
40
+ * @param code The stable failure category.
41
+ * @param message Diagnostic text; not a compatibility contract.
42
+ * @param phase The step that was running when the failure happened.
43
+ * @param options `cause` carries the wrapped transport or session failure.
44
+ */
45
+ constructor(code: RelayErrorCode, message: string, phase: RelayErrorPhase, options?: ErrorOptions);
46
+ }
47
+ /**
48
+ * Whether a send-stream failure is the peer's STOP_SENDING: a stream error (TA-20, TA-22), whatever its class or name.
49
+ * Chromium sometimes reports the stop instead as a `NetworkError` DOMException saying the remote side aborted the
50
+ * stream, with no `source` and no code, which is accepted too (spec/transport-adapter.md, Known deviations). A session
51
+ * failure has `source` `'session'`.
52
+ */
53
+ export declare function isStreamStop(error: unknown): boolean;
54
+ /**
55
+ * Wrap a failure while connecting: a credentials refusal is `UNAUTHORIZED`, anything else a transport error.
56
+ * `peerClose` is what the transport's `closed` resolved with, if it has; the session reports a clean close before
57
+ * server SETUP without its code. A failure in the turn server SETUP arrives is a transport error (see
58
+ * {@link connectedError} for a close after that).
59
+ */
60
+ export declare function connectingError(error: unknown, peerClose?: unknown): RelayError;
61
+ /** Whether the peer closed the session over its credentials: a `closed` resolution or session error with 0x2, 0x16 or 0x18. */
62
+ export declare function isCredentialRefusal(value: unknown): boolean;
63
+ /**
64
+ * Wrap the failure that ended an established connection. A session closed with a credentials code is `UNAUTHORIZED`
65
+ * in phase `connection`: moq-relay ends a session that way when its grant expires or is revoked (muxinc/relay-sdks#261,
66
+ * #227). Anything else is a transport error.
67
+ */
68
+ export declare function connectedError(error: unknown, peerClose?: unknown): RelayError;
69
+ /** Wrap only transport/session failures, preserving their original cause. */
70
+ export declare function transportError(error: unknown, phase: RelayErrorPhase): RelayError;
71
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAEA,uFAAuF;AACvF,MAAM,MAAM,cAAc,GACtB,oBAAoB,GACpB,eAAe;AACjB;;;;GAIG;GACD,kBAAkB,GAClB,QAAQ,GACR,eAAe,GACf,yBAAyB,GACzB,iBAAiB,GACjB,sBAAsB,GACtB,6BAA6B,GAC7B,uBAAuB,GACvB,mBAAmB;AACrB;;;;GAIG;GACD,gBAAgB;AAClB;;;;GAIG;GACD,cAAc,GACd,QAAQ,GACR,iBAAiB,GACjB,uBAAuB;AACzB;;;;;;GAMG;GACD,cAAc,CAAC;AAEnB,wFAAwF;AACxF,MAAM,MAAM,eAAe,GACvB,YAAY,GACZ,YAAY,GACZ,cAAc,GACd,oBAAoB,GACpB,gBAAgB,GAChB,SAAS,GACT,SAAS,CAAC;AAEd,uFAAuF;AACvF,qBAAa,UAAW,SAAQ,KAAK;IACnC,8EAA8E;IAC9E,SAAkB,IAAI,EAAE,MAAM,CAAgB;IAC9C,kFAAkF;IAClF,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,2DAA2D;IAC3D,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC;IAEhC;;;;;OAKG;gBACS,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,YAAY;CAKlG;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAOpD;AA0BD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,UAAU,CAU/E;AAED,+HAA+H;AAC/H,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE3D;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,OAAO,GAAG,UAAU,CAU9E;AAED,6EAA6E;AAC7E,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,eAAe,GAAG,UAAU,CAKjF"}
package/dist/errors.js ADDED
@@ -0,0 +1,99 @@
1
+ import { isStreamError, SESSION_ERROR } from '@caprid/moq-core';
2
+ /** Caller cancellation reasons and argument errors are not converted to RelayError. */
3
+ export class RelayError extends Error {
4
+ /** `'RelayError'`, or the subclass name for the three subscription errors. */
5
+ name = 'RelayError';
6
+ /** The stable failure category; branch on this, not on `message` or the class. */
7
+ code;
8
+ /** The step that was running when the failure happened. */
9
+ phase;
10
+ /**
11
+ * @param code The stable failure category.
12
+ * @param message Diagnostic text; not a compatibility contract.
13
+ * @param phase The step that was running when the failure happened.
14
+ * @param options `cause` carries the wrapped transport or session failure.
15
+ */
16
+ constructor(code, message, phase, options) {
17
+ super(message, options);
18
+ this.code = code;
19
+ this.phase = phase;
20
+ }
21
+ }
22
+ /**
23
+ * Whether a send-stream failure is the peer's STOP_SENDING: a stream error (TA-20, TA-22), whatever its class or name.
24
+ * Chromium sometimes reports the stop instead as a `NetworkError` DOMException saying the remote side aborted the
25
+ * stream, with no `source` and no code, which is accepted too (spec/transport-adapter.md, Known deviations). A session
26
+ * failure has `source` `'session'`.
27
+ */
28
+ export function isStreamStop(error) {
29
+ if (isStreamError(error))
30
+ return true;
31
+ return (error instanceof Error &&
32
+ error.name === 'NetworkError' &&
33
+ error.message === 'The stream was aborted by the remote server');
34
+ }
35
+ /**
36
+ * MOQT session termination codes (draft-22 §12.2) that mean the relay refused the connection's credentials. moq-relay
37
+ * sends `UNAUTHORIZED` (0x2) for every refusal: no token, a bad signature, a malformed or expired token, a path outside
38
+ * the token's root. The core never closes with any of them itself.
39
+ */
40
+ const CREDENTIAL_REFUSALS = new Set([
41
+ SESSION_ERROR.UNAUTHORIZED,
42
+ SESSION_ERROR.MALFORMED_AUTH_TOKEN,
43
+ SESSION_ERROR.EXPIRED_AUTH_TOKEN,
44
+ ]);
45
+ /**
46
+ * The peer's close, when it closed the session because of the credentials: `closed` resolving with the code (TA-9), or
47
+ * a session error carrying it (TA-24).
48
+ */
49
+ function credentialRefusal(value) {
50
+ if (typeof value !== 'object' || value === null)
51
+ return undefined;
52
+ const { source, closeCode, reason } = value;
53
+ // A `closed` resolution has no source; an error that has one must be a session error (TA-22).
54
+ if (source !== undefined && source !== 'session')
55
+ return undefined;
56
+ if (typeof closeCode !== 'number' || !CREDENTIAL_REFUSALS.has(closeCode))
57
+ return undefined;
58
+ return { closeCode, reason: typeof reason === 'string' ? reason : '' };
59
+ }
60
+ /**
61
+ * Wrap a failure while connecting: a credentials refusal is `UNAUTHORIZED`, anything else a transport error.
62
+ * `peerClose` is what the transport's `closed` resolved with, if it has; the session reports a clean close before
63
+ * server SETUP without its code. A failure in the turn server SETUP arrives is a transport error (see
64
+ * {@link connectedError} for a close after that).
65
+ */
66
+ export function connectingError(error, peerClose) {
67
+ if (error instanceof RelayError)
68
+ return error;
69
+ const refusal = credentialRefusal(error) ?? credentialRefusal(peerClose);
70
+ if (refusal === undefined)
71
+ return transportError(error, 'connecting');
72
+ return new RelayError('UNAUTHORIZED', `Relay refused the connection's credentials: ${refusal.reason || 'no reason given'} (session closed with code 0x${refusal.closeCode.toString(16)}); check the token and that it grants this endpoint's path`, 'connecting', { cause: error });
73
+ }
74
+ /** Whether the peer closed the session over its credentials: a `closed` resolution or session error with 0x2, 0x16 or 0x18. */
75
+ export function isCredentialRefusal(value) {
76
+ return credentialRefusal(value) !== undefined;
77
+ }
78
+ /**
79
+ * Wrap the failure that ended an established connection. A session closed with a credentials code is `UNAUTHORIZED`
80
+ * in phase `connection`: moq-relay ends a session that way when its grant expires or is revoked (muxinc/relay-sdks#261,
81
+ * #227). Anything else is a transport error.
82
+ */
83
+ export function connectedError(error, peerClose) {
84
+ if (error instanceof RelayError)
85
+ return error;
86
+ const refusal = credentialRefusal(error) ?? credentialRefusal(peerClose);
87
+ if (refusal === undefined)
88
+ return transportError(error, 'connection');
89
+ return new RelayError('UNAUTHORIZED', `Relay ended the session over its credentials: ${refusal.reason || 'no reason given'} (session closed with code 0x${refusal.closeCode.toString(16)}); the token expired or its grant was revoked. Connect again with a fresh token`, 'connection', { cause: error });
90
+ }
91
+ /** Wrap only transport/session failures, preserving their original cause. */
92
+ export function transportError(error, phase) {
93
+ if (error instanceof RelayError)
94
+ return error;
95
+ return new RelayError('TRANSPORT_ERROR', error instanceof Error ? error.message : 'Relay transport failed', phase, {
96
+ cause: error,
97
+ });
98
+ }
99
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAsDhE,uFAAuF;AACvF,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC,8EAA8E;IAC5D,IAAI,GAAW,YAAY,CAAC;IAC9C,kFAAkF;IACzE,IAAI,CAAiB;IAC9B,2DAA2D;IAClD,KAAK,CAAkB;IAEhC;;;;;OAKG;IACH,YAAY,IAAoB,EAAE,OAAe,EAAE,KAAsB,EAAE,OAAsB;QAC/F,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,IAAI,aAAa,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACtC,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,KAAK,CAAC,IAAI,KAAK,cAAc;QAC7B,KAAK,CAAC,OAAO,KAAK,6CAA6C,CAChE,CAAC;AACJ,CAAC;AAED;;;;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,MAAM,UAAU,mBAAmB,CAAC,KAAc;IAChD,OAAO,iBAAiB,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC;AAChD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc,EAAE,SAAmB;IAChE,IAAI,KAAK,YAAY,UAAU;QAAE,OAAO,KAAK,CAAC;IAC9C,MAAM,OAAO,GAAG,iBAAiB,CAAC,KAAK,CAAC,IAAI,iBAAiB,CAAC,SAAS,CAAC,CAAC;IACzE,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,cAAc,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;IACtE,OAAO,IAAI,UAAU,CACnB,cAAc,EACd,iDAAiD,OAAO,CAAC,MAAM,IAAI,iBAAiB,gCAAgC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC,iFAAiF,EACnO,YAAY,EACZ,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAC;AACJ,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,cAAc,CAAC,KAAc,EAAE,KAAsB;IACnE,IAAI,KAAK,YAAY,UAAU;QAAE,OAAO,KAAK,CAAC;IAC9C,OAAO,IAAI,UAAU,CAAC,iBAAiB,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,wBAAwB,EAAE,KAAK,EAAE;QACjH,KAAK,EAAE,KAAK;KACb,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,81 @@
1
+ import type { KeyValuePair, MoqtObject, SubscribeOk } from '@caprid/moq-core';
2
+ import type { WantSwitchedInfo } from './supervised-subscriber.js';
3
+ /**
4
+ * What a {@link FrameReader} drives: `@caprid/moq-media`'s `FrameSequencer` qualifies. The reader feeds it a
5
+ * subscription's events and polls it; the sequencer owns the ordering, the deadlines and the LOC format.
6
+ */
7
+ export interface FrameReaderSequencer<F> {
8
+ /** Set from each SUBSCRIBE_OK. */
9
+ trackProperties: readonly KeyValuePair[];
10
+ /** An object arrived. */
11
+ object(object: MoqtObject): void;
12
+ /** A group's stream ended cleanly. */
13
+ groupEnded(groupId: number): void;
14
+ /** A group's stream was reset. */
15
+ groupReset(groupId: number): void;
16
+ /** The subscription was replaced: forget everything. */
17
+ restart(): void;
18
+ /** Take the frames ready now, and when to poll again. */
19
+ poll(): {
20
+ /** The frames ready now, in decode order. */
21
+ frames: F[];
22
+ /** Poll again after this many microseconds, 0 at once; `undefined` when only new events can change it. */
23
+ waitUs: number | undefined;
24
+ /** The sequencer asks for the subscription to be replaced at the live edge: see `onCatchUp`. */
25
+ catchUp?: boolean;
26
+ };
27
+ }
28
+ /** Options for {@link createFrameReader}. */
29
+ export interface FrameReaderOptions<F> {
30
+ /**
31
+ * Called synchronously with each frame, in decode order. An exception from it goes to `console.error` and the reader
32
+ * carries on.
33
+ */
34
+ onFrame(frame: F): void;
35
+ /**
36
+ * The sequencer signalled a catch-up (`@caprid/moq-media`'s `catchUpAfterUs`): frames keep arriving too late to use.
37
+ * Replace the subscription at the live edge, which drops what the relay still holds queued for it: a supervised
38
+ * subscriber's `catchUp(key)` does, and its `onRestarted` restarts this reader. Called synchronously, after the
39
+ * frames of the same poll; an exception from it goes to `console.error`.
40
+ */
41
+ onCatchUp?(): void;
42
+ }
43
+ /**
44
+ * A subscription's or a want's handlers that read its track as frames in decode order. Pass it, or spread it with
45
+ * handlers of your own, to `subscribe` or `want`. Each group is one subgroup, as the frame writer sends it, so a
46
+ * subgroup's end is its group's. A want read through a reader keeps `drainMs` at 0, the default: a drained old
47
+ * subscription's groups would arrive after the Move, indistinguishable from the new one's.
48
+ */
49
+ export interface FrameReader {
50
+ /** Takes the track properties, which may declare the Timescale. */
51
+ onSubscribed(info: SubscribeOk): void;
52
+ /** Feeds the object to the sequencer and delivers what is ready. */
53
+ onObject(object: MoqtObject): void;
54
+ /** Ends the object's group. */
55
+ onSubgroupEnd(info: {
56
+ groupId: number;
57
+ }): void;
58
+ /** Resets the object's group: its remaining objects will not come. */
59
+ onSubgroupReset(info: {
60
+ groupId: number;
61
+ }): void;
62
+ /** A want's Restart: the sequencer starts over and marks the next frame. */
63
+ onRestarted(): void;
64
+ /** A want's Move: the same, with the new subscription's track properties. */
65
+ onSwitched(info: WantSwitchedInfo): void;
66
+ /** Stop delivering and cancel the pending poll. Later events are ignored. */
67
+ close(): void;
68
+ }
69
+ /**
70
+ * Read a track as frames: feed the sequencer every event, poll it after each, and again when a wait it reports ends
71
+ * (at most 50 ms on, since another track may move a shared clock).
72
+ * See spec/behavior.md, "Media subscribing".
73
+ *
74
+ * ```ts
75
+ * const clock = createPlayoutClock();
76
+ * const video = createFrameReader(createFrameSequencer({ clock }), { onFrame: decode });
77
+ * subscriber.subscribe({ namespace, track: 'video' }, { ...video, onError });
78
+ * ```
79
+ */
80
+ export declare function createFrameReader<F>(sequencer: FrameReaderSequencer<F>, options: FrameReaderOptions<F>): FrameReader;
81
+ //# sourceMappingURL=frame-reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame-reader.d.ts","sourceRoot":"","sources":["../src/frame-reader.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAEnE;;;GAGG;AACH,MAAM,WAAW,oBAAoB,CAAC,CAAC;IACrC,kCAAkC;IAClC,eAAe,EAAE,SAAS,YAAY,EAAE,CAAC;IACzC,yBAAyB;IACzB,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC;IACjC,sCAAsC;IACtC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,kCAAkC;IAClC,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,wDAAwD;IACxD,OAAO,IAAI,IAAI,CAAC;IAChB,yDAAyD;IACzD,IA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