@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,19 @@
1
+ export type { Goaway, KeyValuePair, LocationFilter, MessageParameters, MoqTrackSource, MoqtObject, MoqtTransport, PublishDone, RequestError, SubscribeOk, Subscription, SubscriptionHandlers, } from '@caprid/moq-core';
2
+ export type { ConnectRelayOptions, ConnectRelaySubscriberOptions, RelayCredential, RelaySubscriber, RelaySubscriptionHandlers, RelayTrackOptions, RelayTransportFactory, } from './connect.js';
3
+ export { connectRelaySubscriber } from './connect.js';
4
+ export { RelayError, type RelayErrorCode, type RelayErrorPhase } from './errors.js';
5
+ export type { NamespaceEvent, NamespaceEventOptions, NamespaceEvents } from './namespace-events.js';
6
+ export { readJSON, readText } from './payload.js';
7
+ export type { ConnectRelayPublisherOptions, PublishProperty, RegisterTrackOptions, RelayGroupWriter, RelayPublisher, RelayTrackWriter, } from './publish.js';
8
+ export { connectRelayPublisher } from './publish.js';
9
+ export { roomEndpoint } from './room-endpoint.js';
10
+ export type { RelaySubscription, SubscriptionEnd, SubscriptionEndKind } from './subscription.js';
11
+ export type { SubscriptionEvent, SubscriptionEventOptions, SubscriptionEvents } from './subscription-events.js';
12
+ export { SubscriptionConnectionClosedError, SubscriptionOverflowError, SubscriptionRequestError, } from './subscription-events.js';
13
+ export type { ConnectSupervisedPublisherOptions, PublisherGeneration, SupervisedPublisher, } from './supervised-publisher.js';
14
+ export { connectSupervisedPublisher } from './supervised-publisher.js';
15
+ export type { ConnectSupervisedSubscriberOptions, RestartReason, SupervisedSubscriber, WantHandlers, WantOptions, WantSwitchedInfo, WantTarget, } from './supervised-subscriber.js';
16
+ export { connectSupervisedSubscriber } from './supervised-subscriber.js';
17
+ export type { SupervisedStatus, SupervisionBackoff } from './supervision.js';
18
+ export type { SwitchableHandlers, SwitchableSubscription, SwitchedInfo, SwitchOptions, } from './switchable.js';
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,MAAM,EACN,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,cAAc,EACd,UAAU,EACV,aAAa,EACb,WAAW,EACX,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,oBAAoB,GACrB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EACV,mBAAmB,EACnB,6BAA6B,EAC7B,eAAe,EACf,eAAe,EACf,yBAAyB,EACzB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,KAAK,cAAc,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AACpF,YAAY,EAAE,cAAc,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACpG,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EACV,4BAA4B,EAC5B,eAAe,EACf,oBAAoB,EACpB,gBAAgB,EAChB,cAAc,EACd,gBAAgB,GACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,YAAY,EAAE,iBAAiB,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACjG,YAAY,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAChH,OAAO,EACL,iCAAiC,EACjC,yBAAyB,EACzB,wBAAwB,GACzB,MAAM,0BAA0B,CAAC;AAClC,YAAY,EACV,iCAAiC,EACjC,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACvE,YAAY,EACV,kCAAkC,EAClC,aAAa,EACb,oBAAoB,EACpB,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,UAAU,GACX,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC;AACzE,YAAY,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC7E,YAAY,EACV,kBAAkB,EAClB,sBAAsB,EACtB,YAAY,EACZ,aAAa,GACd,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export { connectRelaySubscriber } from './connect.js';
2
+ export { RelayError } from './errors.js';
3
+ export { readJSON, readText } from './payload.js';
4
+ export { connectRelayPublisher } from './publish.js';
5
+ export { roomEndpoint } from './room-endpoint.js';
6
+ export { SubscriptionConnectionClosedError, SubscriptionOverflowError, SubscriptionRequestError, } from './subscription-events.js';
7
+ export { connectSupervisedPublisher } from './supervised-publisher.js';
8
+ export { connectSupervisedSubscriber } from './supervised-subscriber.js';
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,UAAU,EAA6C,MAAM,aAAa,CAAC;AAEpF,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AASlD,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD,OAAO,EACL,iCAAiC,EACjC,yBAAyB,EACzB,wBAAwB,GACzB,MAAM,0BAA0B,CAAC;AAMlC,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AAUvE,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,59 @@
1
+ import type { MessageParameters } from '@caprid/moq-core';
2
+ import type { PublishProperty } from './publish.js';
3
+ /** What a media track carries, which decides its default delivery timeout and priorities. */
4
+ export type MediaKind = 'audio' | 'video';
5
+ /** Options for {@link mediaTrackProperties}. Every duration is a positive integer number of microseconds. */
6
+ export interface MediaTrackPropertiesOptions {
7
+ /** The encoder's keyframe interval. Required for video; audio ignores it. */
8
+ keyframeIntervalUs?: number;
9
+ /**
10
+ * How far behind live a viewer plays (see `CONTEXT.md`); at least the keyframe interval. Default twice the keyframe
11
+ * interval, the default the frame reader takes too (#303). Audio ignores it.
12
+ */
13
+ targetLatencyUs?: number;
14
+ /** The subgroup timeout itself, replacing the default for either kind. */
15
+ subgroupTimeoutUs?: number;
16
+ /**
17
+ * The track's Default Publisher Priority (0x0E), an integer from 0 to 255 where lower is more urgent, replacing the
18
+ * default for either kind: 16 for audio, 96 for video.
19
+ */
20
+ publisherPriority?: number;
21
+ }
22
+ /**
23
+ * The track properties that give a media track its default delivery timeout and publisher priority (spec/behavior.md,
24
+ * "Media publishing"), so a stall costs stale frames rather than lasting latency. Pass them to `registerTrack` with the track's format
25
+ * properties, such as `@caprid/moq-media`'s `locTrackProperties()`:
26
+ *
27
+ * ```ts
28
+ * const video = publisher.registerTrack({
29
+ * track: 'video',
30
+ * trackProperties: [...locTrackProperties(), ...mediaTrackProperties('video', { keyframeIntervalUs: 1_000_000 })],
31
+ * });
32
+ * ```
33
+ *
34
+ * The result is a SUBGROUP_DELIVERY_TIMEOUT (0x06) in whole milliseconds, rounded up: 500 ms for audio, and for
35
+ * video the target latency less the keyframe interval, but never less than the interval. Then a Default Publisher
36
+ * Priority (0x0E) that puts audio ahead of video: 16 for audio, 96 for video. moq-relay 0.17 passes it on to viewers
37
+ * in SUBSCRIBE_OK but orders delivery by subscriber priority alone, so on that relay it changes nothing yet. No object
38
+ * timeout is set:
39
+ * through a frame writer it would never fire, because a group's wait for settling room is not counted and, once its
40
+ * stream is open, a browser's transport takes each object at once even during a stall.
41
+ */
42
+ export declare function mediaTrackProperties(kind: MediaKind, options?: MediaTrackPropertiesOptions): PublishProperty[];
43
+ /**
44
+ * The subscription parameters a viewer gives a media track, so audio goes ahead of video when the downlink is congested
45
+ * (spec/behavior.md, "Media subscribing"). Pass them to `subscribe`, `want` or `switchTo`:
46
+ *
47
+ * ```ts
48
+ * subscriber.subscribe({ namespace, track: 'audio', parameters: mediaSubscribeParameters('audio') }, handlers);
49
+ * subscriber.subscribe({ namespace, track: 'video', parameters: mediaSubscribeParameters('video') }, handlers);
50
+ * ```
51
+ *
52
+ * The result is a SUBSCRIBER_PRIORITY of 16 for audio and 96 for video, lower being more urgent: the same values
53
+ * `mediaTrackProperties` gives as publisher priorities. moq-relay 0.17 orders what it sends a viewer by subscriber
54
+ * priority alone and ignores the publisher's, so this is the priority that decides delivery there. It cannot be
55
+ * changed later: on that relay any `update()` ends the subscription. `overrides` are copied onto the result, such as a
56
+ * `locationFilter`; an explicit `subscriberPriority`, an integer from 0 to 255, replaces the default.
57
+ */
58
+ export declare function mediaSubscribeParameters(kind: MediaKind, overrides?: MessageParameters): MessageParameters;
59
+ //# sourceMappingURL=media-track-properties.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media-track-properties.d.ts","sourceRoot":"","sources":["../src/media-track-properties.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAG1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAGpD,6FAA6F;AAC7F,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,OAAO,CAAC;AAE1C,6GAA6G;AAC7G,MAAM,WAAW,2BAA2B;IAC1C,6EAA6E;IAC7E,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,0EAA0E;IAC1E,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAaD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,GAAE,2BAAgC,GAAG,eAAe,EAAE,CAwBlH;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,SAAS,EAAE,SAAS,GAAE,iBAAsB,GAAG,iBAAiB,CAM9G"}
@@ -0,0 +1,120 @@
1
+ import { MAX_TIMEOUT_MS } from './connect-session.js';
2
+ import { SUBGROUP_DELIVERY_TIMEOUT } from './delivery-timeouts.js';
3
+ import { DEFAULT_PUBLISHER_PRIORITY, MAX_DEFAULT_PUBLISHER_PRIORITY } from './validation.js';
4
+ /** Audio's default subgroup timeout: measured under a 2 s uplink stall to sjc ([#302](https://github.com/muxinc/relay-sdks/issues/302)). */
5
+ const AUDIO_SUBGROUP_TIMEOUT_US = 500_000;
6
+ /** Default Publisher Priority per kind, lower being more urgent: audio ahead of video, as Huddle sets them by hand. */
7
+ const PUBLISHER_PRIORITY = { audio: 16, video: 96 };
8
+ /** SUBSCRIBER_PRIORITY per kind, lower being more urgent: the same values as the publisher priorities. */
9
+ const SUBSCRIBER_PRIORITY = { audio: 16, video: 96 };
10
+ /** The largest SUBSCRIBER_PRIORITY, a single byte. */
11
+ const MAX_SUBSCRIBER_PRIORITY = 255;
12
+ /** The largest value whose whole milliseconds the write slot's timers hold. */
13
+ const MAX_TIMEOUT_US = MAX_TIMEOUT_MS * 1000;
14
+ /**
15
+ * The track properties that give a media track its default delivery timeout and publisher priority (spec/behavior.md,
16
+ * "Media publishing"), so a stall costs stale frames rather than lasting latency. Pass them to `registerTrack` with the track's format
17
+ * properties, such as `@caprid/moq-media`'s `locTrackProperties()`:
18
+ *
19
+ * ```ts
20
+ * const video = publisher.registerTrack({
21
+ * track: 'video',
22
+ * trackProperties: [...locTrackProperties(), ...mediaTrackProperties('video', { keyframeIntervalUs: 1_000_000 })],
23
+ * });
24
+ * ```
25
+ *
26
+ * The result is a SUBGROUP_DELIVERY_TIMEOUT (0x06) in whole milliseconds, rounded up: 500 ms for audio, and for
27
+ * video the target latency less the keyframe interval, but never less than the interval. Then a Default Publisher
28
+ * Priority (0x0E) that puts audio ahead of video: 16 for audio, 96 for video. moq-relay 0.17 passes it on to viewers
29
+ * in SUBSCRIBE_OK but orders delivery by subscriber priority alone, so on that relay it changes nothing yet. No object
30
+ * timeout is set:
31
+ * through a frame writer it would never fire, because a group's wait for settling room is not counted and, once its
32
+ * stream is open, a browser's transport takes each object at once even during a stall.
33
+ */
34
+ export function mediaTrackProperties(kind, options = {}) {
35
+ if (typeof kind !== 'string')
36
+ throw new TypeError("kind must be 'audio' or 'video'");
37
+ if (kind !== 'audio' && kind !== 'video')
38
+ throw new RangeError("kind must be 'audio' or 'video'");
39
+ const keyframeIntervalUs = optionalMicroseconds(options.keyframeIntervalUs, 'keyframeIntervalUs');
40
+ const targetLatencyUs = optionalMicroseconds(options.targetLatencyUs, 'targetLatencyUs');
41
+ const subgroupTimeoutUs = optionalMicroseconds(options.subgroupTimeoutUs, 'subgroupTimeoutUs');
42
+ const publisherPriority = optionalPriority(options.publisherPriority);
43
+ if (kind === 'video' && keyframeIntervalUs === undefined) {
44
+ throw new TypeError('keyframeIntervalUs is required for video');
45
+ }
46
+ if (keyframeIntervalUs !== undefined && targetLatencyUs !== undefined && targetLatencyUs < keyframeIntervalUs) {
47
+ throw new RangeError('targetLatencyUs must be at least keyframeIntervalUs');
48
+ }
49
+ return [
50
+ { type: SUBGROUP_DELIVERY_TIMEOUT, value: Math.ceil(timeoutUs() / 1000) },
51
+ { type: DEFAULT_PUBLISHER_PRIORITY, value: publisherPriority ?? PUBLISHER_PRIORITY[kind] },
52
+ ];
53
+ function timeoutUs() {
54
+ if (subgroupTimeoutUs !== undefined)
55
+ return subgroupTimeoutUs;
56
+ if (kind === 'audio')
57
+ return AUDIO_SUBGROUP_TIMEOUT_US;
58
+ const interval = keyframeIntervalUs;
59
+ return Math.max(interval, (targetLatencyUs ?? 2 * interval) - interval);
60
+ }
61
+ }
62
+ /**
63
+ * The subscription parameters a viewer gives a media track, so audio goes ahead of video when the downlink is congested
64
+ * (spec/behavior.md, "Media subscribing"). Pass them to `subscribe`, `want` or `switchTo`:
65
+ *
66
+ * ```ts
67
+ * subscriber.subscribe({ namespace, track: 'audio', parameters: mediaSubscribeParameters('audio') }, handlers);
68
+ * subscriber.subscribe({ namespace, track: 'video', parameters: mediaSubscribeParameters('video') }, handlers);
69
+ * ```
70
+ *
71
+ * The result is a SUBSCRIBER_PRIORITY of 16 for audio and 96 for video, lower being more urgent: the same values
72
+ * `mediaTrackProperties` gives as publisher priorities. moq-relay 0.17 orders what it sends a viewer by subscriber
73
+ * priority alone and ignores the publisher's, so this is the priority that decides delivery there. It cannot be
74
+ * changed later: on that relay any `update()` ends the subscription. `overrides` are copied onto the result, such as a
75
+ * `locationFilter`; an explicit `subscriberPriority`, an integer from 0 to 255, replaces the default.
76
+ */
77
+ export function mediaSubscribeParameters(kind, overrides = {}) {
78
+ if (typeof kind !== 'string')
79
+ throw new TypeError("kind must be 'audio' or 'video'");
80
+ if (kind !== 'audio' && kind !== 'video')
81
+ throw new RangeError("kind must be 'audio' or 'video'");
82
+ if (typeof overrides !== 'object' || overrides === null)
83
+ throw new TypeError('overrides must be an object');
84
+ const subscriberPriority = optionalSubscriberPriority(overrides.subscriberPriority);
85
+ return { ...overrides, subscriberPriority: subscriberPriority ?? SUBSCRIBER_PRIORITY[kind] };
86
+ }
87
+ /** A positive safe integer of microseconds whose whole milliseconds fit the timer bound, or `undefined`. */
88
+ function optionalMicroseconds(value, name) {
89
+ if (value === undefined)
90
+ return undefined;
91
+ if (typeof value !== 'number')
92
+ throw new TypeError(`${name} must be a number of microseconds`);
93
+ if (!Number.isSafeInteger(value) || value <= 0 || value > MAX_TIMEOUT_US) {
94
+ throw new RangeError(`${name} must be a positive integer of at most ${MAX_TIMEOUT_US} microseconds`);
95
+ }
96
+ return value;
97
+ }
98
+ /** An integer Default Publisher Priority from 0 to 255, or `undefined`. */
99
+ function optionalPriority(value) {
100
+ if (value === undefined)
101
+ return undefined;
102
+ if (typeof value !== 'number')
103
+ throw new TypeError('publisherPriority must be a number');
104
+ if (!Number.isInteger(value) || value < 0 || value > MAX_DEFAULT_PUBLISHER_PRIORITY) {
105
+ throw new RangeError(`publisherPriority must be an integer from 0 to ${MAX_DEFAULT_PUBLISHER_PRIORITY}`);
106
+ }
107
+ return value;
108
+ }
109
+ /** An integer SUBSCRIBER_PRIORITY from 0 to 255, or `undefined`. */
110
+ function optionalSubscriberPriority(value) {
111
+ if (value === undefined)
112
+ return undefined;
113
+ if (typeof value !== 'number')
114
+ throw new TypeError('subscriberPriority must be a number');
115
+ if (!Number.isInteger(value) || value < 0 || value > MAX_SUBSCRIBER_PRIORITY) {
116
+ throw new RangeError(`subscriberPriority must be an integer from 0 to ${MAX_SUBSCRIBER_PRIORITY}`);
117
+ }
118
+ return value;
119
+ }
120
+ //# sourceMappingURL=media-track-properties.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media-track-properties.js","sourceRoot":"","sources":["../src/media-track-properties.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AAEnE,OAAO,EAAE,0BA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AC,CAAC;IACrG,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * `@caprid/relay/media`: the media policy over registered tracks and subscriptions (spec/behavior.md, "Media publishing"
3
+ * and "Media subscribing"). A frame writer maps encoded frames onto a track's groups; a catalog service and a document
4
+ * service serve state-shaped tracks, and `mediaTrackProperties` gives a media track its default delivery timeout and publisher
5
+ * priority; `mediaSubscribeParameters` gives a viewer's subscription to it the matching subscriber priority, and a
6
+ * frame reader reads a subscription as frames in decode order. They know no format: a frame carries its own object
7
+ * properties, a document is bytes, and the reader drives a sequencer it is given, so they pair with `@caprid/moq-media`'s
8
+ * LOC and MSF helpers and its `FrameSequencer` without depending on them.
9
+ *
10
+ * ```ts
11
+ * import { createCatalogService, createFrameWriter, mediaTrackProperties } from '@caprid/relay/media';
12
+ *
13
+ * const video = createFrameWriter(
14
+ * publisher.registerTrack({
15
+ * track: 'video',
16
+ * trackProperties: [...locTrackProperties(), ...mediaTrackProperties('video', { keyframeIntervalUs: 1_000_000 })],
17
+ * }),
18
+ * );
19
+ * const outcome = await video.write(chunkToLocFrame(chunk, { videoConfig }));
20
+ * ```
21
+ *
22
+ * @packageDocumentation
23
+ */
24
+ export { type CatalogService, type CatalogServiceOptions, createCatalogService, createDocumentService, type DocumentService, type DocumentServiceOptions, type ServeOptions, } from './document-service.js';
25
+ export { createFrameReader, type FrameReader, type FrameReaderOptions, type FrameReaderSequencer, } from './frame-reader.js';
26
+ export { createFrameWriter, type Frame, type FrameGrouping, type FrameOutcome, type FrameOutcomeKind, type FrameWriter, type FrameWriterOptions, } from './frame-writer.js';
27
+ export { type MediaKind, type MediaTrackPropertiesOptions, mediaSubscribeParameters, mediaTrackProperties, } from './media-track-properties.js';
28
+ //# sourceMappingURL=media.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media.d.ts","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC1B,oBAAoB,EACpB,qBAAqB,EACrB,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC3B,KAAK,YAAY,GAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,iBAAiB,EACjB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,GAC1B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,iBAAiB,EACjB,KAAK,KAAK,EACV,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,kBAAkB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,KAAK,SAAS,EACd,KAAK,2BAA2B,EAChC,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,6BAA6B,CAAC"}
package/dist/media.js ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * `@caprid/relay/media`: the media policy over registered tracks and subscriptions (spec/behavior.md, "Media publishing"
3
+ * and "Media subscribing"). A frame writer maps encoded frames onto a track's groups; a catalog service and a document
4
+ * service serve state-shaped tracks, and `mediaTrackProperties` gives a media track its default delivery timeout and publisher
5
+ * priority; `mediaSubscribeParameters` gives a viewer's subscription to it the matching subscriber priority, and a
6
+ * frame reader reads a subscription as frames in decode order. They know no format: a frame carries its own object
7
+ * properties, a document is bytes, and the reader drives a sequencer it is given, so they pair with `@caprid/moq-media`'s
8
+ * LOC and MSF helpers and its `FrameSequencer` without depending on them.
9
+ *
10
+ * ```ts
11
+ * import { createCatalogService, createFrameWriter, mediaTrackProperties } from '@caprid/relay/media';
12
+ *
13
+ * const video = createFrameWriter(
14
+ * publisher.registerTrack({
15
+ * track: 'video',
16
+ * trackProperties: [...locTrackProperties(), ...mediaTrackProperties('video', { keyframeIntervalUs: 1_000_000 })],
17
+ * }),
18
+ * );
19
+ * const outcome = await video.write(chunkToLocFrame(chunk, { videoConfig }));
20
+ * ```
21
+ *
22
+ * @packageDocumentation
23
+ */
24
+ export { createCatalogService, createDocumentService, } from './document-service.js';
25
+ export { createFrameReader, } from './frame-reader.js';
26
+ export { createFrameWriter, } from './frame-writer.js';
27
+ export { mediaSubscribeParameters, mediaTrackProperties, } from './media-track-properties.js';
28
+ //# sourceMappingURL=media.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media.js","sourceRoot":"","sources":["../src/media.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAGL,oBAAoB,EACpB,qBAAqB,GAItB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,iBAAiB,GAIlB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,iBAAiB,GAOlB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAGL,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,6BAA6B,CAAC"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Namespace discovery: one SUBSCRIBE_NAMESPACE request from creation to its end, read through the
3
+ * bounded events iterator `subscribeEvents` uses. The relay answers with the namespaces under the
4
+ * prefix that are announced now, then each later announce and withdrawal; this module turns the
5
+ * wire's prefix-relative entries into full namespaces, keeps the events edge-triggered, and maps the
6
+ * request's ending onto the subscription error classes.
7
+ */
8
+ import { type MoqtSession } from '@caprid/moq-core';
9
+ import { type SubscriptionEventOptions } from './subscription-events.js';
10
+ /** One change in which namespaces under the prefix are announced. */
11
+ export interface NamespaceEvent {
12
+ /**
13
+ * `announced`: a publisher announced the namespace, or it was already announced when the request
14
+ * was accepted. `unannounced`: its announce was withdrawn. Each namespace alternates between the
15
+ * two; the relay's repeats are not passed on.
16
+ */
17
+ type: 'announced' | 'unannounced';
18
+ /** The full namespace: the prefix's fields followed by the rest, without splitting or joining. */
19
+ namespace: string[];
20
+ }
21
+ /** Options for `namespaceEvents`: the prefix, the iterator's queue bound and its own cancellation. */
22
+ export interface NamespaceEventOptions extends SubscriptionEventOptions {
23
+ /** Namespace prefix to discover, matched field by field. Same rules as a namespace. */
24
+ prefix: readonly string[];
25
+ }
26
+ /** Single-consumer iterator owning one namespace request, never the shared connection. */
27
+ export interface NamespaceEvents extends AsyncIterableIterator<NamespaceEvent> {
28
+ /** Discard queued events and settle pending reads as EOF. Idempotent. */
29
+ close(): void;
30
+ /** Called by `for await` on `break` or a throw: discards queued events, cancels the request, resolves done. */
31
+ return(): Promise<IteratorResult<NamespaceEvent>>;
32
+ }
33
+ /** What a namespace request borrows from the connection. */
34
+ export interface NamespaceRequestDependencies {
35
+ /** The core session's SUBSCRIBE_NAMESPACE. */
36
+ subscribeNamespace: MoqtSession['subscribeNamespace'];
37
+ /** Observe the connection's closure in the turn it happens; returns the removal. */
38
+ observeClosed(observer: (error?: unknown) => void): () => void;
39
+ /** The connection's prefixes with a namespace request open; see {@link claimPrefix}. */
40
+ prefixes: Set<readonly string[]>;
41
+ }
42
+ /** Open the iterator over `prefix`, whose fields the caller has already admitted. */
43
+ export declare function createNamespaceEvents(deps: NamespaceRequestDependencies, prefix: string[], options: SubscriptionEventOptions): NamespaceEvents;
44
+ //# sourceMappingURL=namespace-events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"namespace-events.d.ts","sourceRoot":"","sources":["../src/namespace-events.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAmB,KAAK,WAAW,EAAyC,MAAM,kBAAkB,CAAC;AAG5G,OAAO,EAA8C,KAAK,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AAErH,qEAAqE;AACrE,MAAM,WAAW,cAAc;IAC7B;;;;OAIG;IACH,IAAI,EAAE,WAAW,GAAG,aAAa,CAAC;IAClC,kGAAkG;IAClG,SAAS,EAAE,MAAM,EAAE,CAAC;CACrB;AAED,sGAAsG;AACtG,MAAM,WAAW,qBAAsB,SAAQ,wBAAwB;IACrE,uFAAuF;IACvF,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;CAC3B;AAED,0FAA0F;AAC1F,MAAM,WAAW,eAAgB,SAAQ,qBAAqB,CAAC,cAAc,CAAC;IAC5E,yEAAyE;IACzE,KAAK,IAAI,IAAI,CAAC;IACd,+GAA+G;IAC/G,MAAM,IAAI,OAAO,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC;CACnD;AAED,4DAA4D;AAC5D,MAAM,WAAW,4BAA4B;IAC3C,8CAA8C;IAC9C,kBAAkB,EAAE,WAAW,CAAC,oBAAoB,CAAC,CAAC;IACtD,oFAAoF;IACpF,aAAa,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,OAAO,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IAC/D,wFAAwF;IACxF,QAAQ,EAAE,GAAG,CAAC,SAAS,MAAM,EAAE,CAAC,CAAC;CAClC;AAsBD,qFAAqF;AACrF,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,4BAA4B,EAClC,MAAM,EAAE,MAAM,EAAE,EAChB,OAAO,EAAE,wBAAwB,GAChC,eAAe,CAEjB"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Namespace discovery: one SUBSCRIBE_NAMESPACE request from creation to its end, read through the
3
+ * bounded events iterator `subscribeEvents` uses. The relay answers with the namespaces under the
4
+ * prefix that are announced now, then each later announce and withdrawal; this module turns the
5
+ * wire's prefix-relative entries into full namespaces, keeps the events edge-triggered, and maps the
6
+ * request's ending onto the subscription error classes.
7
+ */
8
+ import { isStreamFailure, REQUEST_ERROR_CODE } from '@caprid/moq-core';
9
+ import { RelayError } from './errors.js';
10
+ import { CLOSURE_GRACE_MS, SubscriptionConnectionClosedError, SubscriptionRequestError } from './subscription.js';
11
+ import { createBoundedEvents } from './subscription-events.js';
12
+ /**
13
+ * Hold `prefix` for one request, or refuse it when it overlaps one already held: draft-22 §9.15 has a relay answer an
14
+ * overlapping SUBSCRIBE_NAMESPACE on the same session with PREFIX_OVERLAP. moq-relay 0.17.0 does not, so the SDK
15
+ * refuses it itself, before anything is sent, and behaves the same against every relay. Returns the release.
16
+ */
17
+ function claimPrefix(prefixes, prefix) {
18
+ for (const held of prefixes) {
19
+ const [shorter, longer] = held.length <= prefix.length ? [held, prefix] : [prefix, held];
20
+ if (shorter.every((field, i) => field === longer[i])) {
21
+ throw new RelayError('PREFIX_OVERLAP', `This connection already discovers ${JSON.stringify(held)}, which overlaps ${JSON.stringify(prefix)}; one request per prefix tree`, 'subscription');
22
+ }
23
+ }
24
+ prefixes.add(prefix);
25
+ return () => prefixes.delete(prefix);
26
+ }
27
+ /** Open the iterator over `prefix`, whose fields the caller has already admitted. */
28
+ export function createNamespaceEvents(deps, prefix, options) {
29
+ return createBoundedEvents((sink) => openNamespaceRequest(deps, prefix, sink), options);
30
+ }
31
+ function openNamespaceRequest(deps, prefix, sink) {
32
+ let ended = false;
33
+ let core;
34
+ let removeClosed = () => { };
35
+ let cancelGrace = () => { };
36
+ const releasePrefix = claimPrefix(deps.prefixes, prefix);
37
+ // Keyed by the suffix's JSON: a field may hold any character, a separator could collide.
38
+ const announced = new Set();
39
+ const finish = (error) => {
40
+ if (ended)
41
+ return;
42
+ ended = true;
43
+ releasePrefix();
44
+ removeClosed();
45
+ cancelGrace();
46
+ core?.cancel(error);
47
+ // A clean end keeps what is queued: the withdrawals the core reports for it come just before.
48
+ if (error === undefined)
49
+ sink.complete();
50
+ else
51
+ sink.ended(error);
52
+ };
53
+ /**
54
+ * The same rule a subscription follows: a REQUEST_ERROR ends the request one microtask later, so a
55
+ * closure reported in the same turn wins; a stream that failed without one may be the first sign of
56
+ * a lost transport, so it waits for the closure up to the grace.
57
+ */
58
+ const fail = (error, viaStream) => {
59
+ const reject = () => finish(new SubscriptionRequestError(error));
60
+ if (!viaStream) {
61
+ queueMicrotask(reject);
62
+ return;
63
+ }
64
+ const grace = setTimeout(reject, CLOSURE_GRACE_MS);
65
+ cancelGrace = () => clearTimeout(grace);
66
+ };
67
+ const change = (type, suffix) => {
68
+ if (ended)
69
+ return;
70
+ const key = JSON.stringify(suffix);
71
+ if (type === 'announced' ? announced.has(key) : !announced.delete(key))
72
+ return;
73
+ if (type === 'announced')
74
+ announced.add(key);
75
+ sink.event({ type, namespace: [...prefix, ...suffix] });
76
+ };
77
+ removeClosed = deps.observeClosed((error) => finish(new SubscriptionConnectionClosedError(error)));
78
+ if (!ended) {
79
+ try {
80
+ core = deps.subscribeNamespace({ trackNamespacePrefix: prefix }, {
81
+ onNamespace: (suffix) => change('announced', suffix),
82
+ onNamespaceDone: (suffix) => change('unannounced', suffix),
83
+ onError: (error) => fail(error, isStreamFailure(error) && error.errorCode !== REQUEST_ERROR_CODE.TIMEOUT),
84
+ onEnd: ({ error }) => {
85
+ // A finished stream is the relay ending the request on purpose; a failed one may be a lost connection.
86
+ if (error === undefined)
87
+ finish();
88
+ else {
89
+ const reason = error instanceof Error ? error.message : 'namespace request stream failed';
90
+ fail({ errorCode: REQUEST_ERROR_CODE.INTERNAL_ERROR, retryInterval: 0, reason }, true);
91
+ }
92
+ },
93
+ });
94
+ }
95
+ catch (error) {
96
+ // Encoding can refuse the request (a field too long for the wire); leave nothing observing.
97
+ ended = true;
98
+ releasePrefix();
99
+ removeClosed();
100
+ throw error;
101
+ }
102
+ if (ended)
103
+ core.cancel();
104
+ }
105
+ return { cancel: (reason) => finish(reason) };
106
+ }
107
+ //# sourceMappingURL=namespace-events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"namespace-events.js","sourceRoot":"","sources":["../src/namespace-events.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,eAAe,EAAoB,kBAAkB,EAAqB,MAAM,kBAAkB,CAAC;AAC5G,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,iCAAiC,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAClH,OAAO,EAAyB,mBAAmB,EAAiC,MAAM,0BAA0B,CAAC;AAsCrH;;;;GAIG;AACH,S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AC,OAAO,KAAK,EAAE,CAAC;YACf,4FAA4F;YAC5F,KAAK,GAAG,IAAI,CAAC;YACb,aAAa,EAAE,CAAC;YAChB,YAAY,EAAE,CAAC;YACf,MAAM,KAAK,CAAC;QACd,CAAC;QACD,IAAI,KAAK;YAAE,IAAI,CAAC,MAAM,EAAE,CAAC;IAC3B,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC;AAChD,CAAC"}
package/dist/node.d.ts ADDED
@@ -0,0 +1,75 @@
1
+ /**
2
+ * `@caprid/relay/node`: the SDK for Node. It exports everything `@caprid/relay` does, and its connect functions default
3
+ * `createTransport` to `@caprid/relay-quic`, the native QUIC transport adapter, an optional peer dependency installed next
4
+ * to this package. It uses that adapter even when a global `WebTransport` exists, and warns once when the global
5
+ * could replace it.
6
+ *
7
+ * ```ts
8
+ * import { connectRelayPublisher } from '@caprid/relay/node';
9
+ *
10
+ * const publisher = await connectRelayPublisher({ url, namespace, getToken });
11
+ * ```
12
+ *
13
+ * @packageDocumentation
14
+ */
15
+ import { type ConnectRelayOptions, type ConnectRelaySubscriberOptions, type RelaySubscriber, type RelayTransportFactory } from './connect.js';
16
+ import { type ConnectRelayPublisherOptions, type RelayPublisher } from './publish.js';
17
+ import { type ConnectSupervisedPublisherOptions, type SupervisedPublisher } from './supervised-publisher.js';
18
+ import { type ConnectSupervisedSubscriberOptions, type SupervisedSubscriber } from './supervised-subscriber.js';
19
+ export * from './index.js';
20
+ /** A certificate hash the server's end-entity certificate must match, in the W3C WebTransport shape. */
21
+ export interface RelayCertificateHash {
22
+ /** The hash algorithm; only `'sha-256'` is supported. */
23
+ algorithm: 'sha-256';
24
+ /** The SHA-256 of the certificate's DER encoding. */
25
+ value: BufferSource;
26
+ }
27
+ /**
28
+ * What a Node connection trusts, passed to `@caprid/relay-quic`'s `nativeQuicTransport`. With neither option, the
29
+ * platform's root certificates. Declared here, not imported, so these declarations need no `@caprid/relay-quic` installed.
30
+ */
31
+ export interface RelayNodeTrustOptions {
32
+ /** Extra trust anchors, PEM encoded, added to the platform's roots: one string or buffer may hold several. */
33
+ ca?: string | Uint8Array | readonly (string | Uint8Array)[];
34
+ /** Accept exactly the end-entity certificates with these hashes instead of validating a chain. */
35
+ serverCertificateHashes?: readonly RelayCertificateHash[];
36
+ }
37
+ /**
38
+ * A role's connection options in Node: either the native QUIC adapter's trust options (`ca`,
39
+ * `serverCertificateHashes`), or a `createTransport` of the caller's own, which replaces the adapter. Both at once is a
40
+ * TypeError, since the trust options would go unused.
41
+ */
42
+ export type RelayNodeOptions<T extends ConnectRelayOptions> = (Omit<T, 'createTransport'> & RelayNodeTrustOptions & {
43
+ /** Absent: the connection dials through `@caprid/relay-quic`. */
44
+ createTransport?: undefined;
45
+ }) | (T & {
46
+ /** The caller's own transport, used instead of `@caprid/relay-quic`. */
47
+ createTransport: RelayTransportFactory;
48
+ /** Not taken beside `createTransport`: pass it to the factory instead. */
49
+ ca?: undefined;
50
+ /** Not taken beside `createTransport`: pass it to the factory instead. */
51
+ serverCertificateHashes?: undefined;
52
+ });
53
+ /**
54
+ * Open a Relay connection for subscribing to tracks, as `@caprid/relay`'s `connectRelaySubscriber` does, over native QUIC
55
+ * through `@caprid/relay-quic` unless `createTransport` is given. The caller owns connection shutdown; an open connection
56
+ * keeps the process alive until it closes.
57
+ */
58
+ export declare function connectRelaySubscriber(options: RelayNodeOptions<ConnectRelaySubscriberOptions>): Promise<RelaySubscriber>;
59
+ /**
60
+ * Open a Relay connection that publishes one namespace, as `@caprid/relay`'s `connectRelayPublisher` does, over native
61
+ * QUIC through `@caprid/relay-quic` unless `createTransport` is given. An open connection keeps the process alive until
62
+ * it closes.
63
+ */
64
+ export declare function connectRelayPublisher(options: RelayNodeOptions<ConnectRelayPublisherOptions>): Promise<RelayPublisher>;
65
+ /**
66
+ * Open a subscriber that reconnects, as `@caprid/relay`'s `connectSupervisedSubscriber` does, with every connection over
67
+ * native QUIC through `@caprid/relay-quic` unless `createTransport` is given.
68
+ */
69
+ export declare function connectSupervisedSubscriber(options: RelayNodeOptions<ConnectSupervisedSubscriberOptions>): Promise<SupervisedSubscriber>;
70
+ /**
71
+ * Open a publisher that reconnects, as `@caprid/relay`'s `connectSupervisedPublisher` does, with every generation over
72
+ * native QUIC through `@caprid/relay-quic` unless `createTransport` is given.
73
+ */
74
+ export declare function connectSupervisedPublisher(options: RelayNodeOptions<ConnectSupervisedPublisherOptions>): Promise<SupervisedPublisher>;
75
+ //# sourceMappingURL=node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,6BAA6B,EAElC,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC3B,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,KAAK,4BAA4B,EAEjC,KAAK,cAAc,EACpB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,KAAK,iCAAiC,EACtC,KAAK,mBAAmB,EAEzB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,KAAK,kCAAkC,EACvC,KAAK,oBAAoB,EAE1B,MAAM,4BAA4B,CAAC;AAEpC,cAAc,YAAY,CAAC;AAE3B,wGAAwG;AACxG,MAAM,WAAW,oBAAoB;IACnC,yDAAyD;IACzD,SAAS,EAAE,SAAS,CAAC;IACrB,qDAAqD;IACrD,KAAK,EAAE,YAAY,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC,8GAA8G;IAC9G,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAAC,MAAM,GAAG,UAAU,CAAC,EAAE,CAAC;IAC5D,kGAAkG;IAClG,uBAAuB,CAAC,EAAE,SAAS,oBAAoB,EAAE,CAAC;CAC3D;AAED;;;;GAIG;AACH,MAAM,M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