@moq/net 0.1.9 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/announced.d.ts +40 -24
  2. package/announced.d.ts.map +1 -1
  3. package/announced.js +74 -40
  4. package/announced.js.map +1 -1
  5. package/announced.test.d.ts +2 -0
  6. package/announced.test.d.ts.map +1 -0
  7. package/announced.test.js +37 -0
  8. package/announced.test.js.map +1 -0
  9. package/broadcast.d.ts +80 -25
  10. package/broadcast.d.ts.map +1 -1
  11. package/broadcast.js +266 -49
  12. package/broadcast.js.map +1 -1
  13. package/broadcast.test.d.ts +2 -0
  14. package/broadcast.test.d.ts.map +1 -0
  15. package/broadcast.test.js +219 -0
  16. package/broadcast.test.js.map +1 -0
  17. package/connection/accept.d.ts +6 -0
  18. package/connection/accept.d.ts.map +1 -1
  19. package/connection/accept.js +30 -10
  20. package/connection/accept.js.map +1 -1
  21. package/connection/browser.d.ts +5 -0
  22. package/connection/browser.d.ts.map +1 -0
  23. package/connection/browser.js +25 -0
  24. package/connection/browser.js.map +1 -0
  25. package/connection/browser.test.d.ts +2 -0
  26. package/connection/browser.test.d.ts.map +1 -0
  27. package/connection/browser.test.js +29 -0
  28. package/connection/browser.test.js.map +1 -0
  29. package/connection/connect.d.ts +43 -1
  30. package/connection/connect.d.ts.map +1 -1
  31. package/connection/connect.js +96 -107
  32. package/connection/connect.js.map +1 -1
  33. package/connection/connect.test.d.ts +2 -0
  34. package/connection/connect.test.d.ts.map +1 -0
  35. package/connection/connect.test.js +118 -0
  36. package/connection/connect.test.js.map +1 -0
  37. package/connection/established.d.ts +29 -15
  38. package/connection/established.d.ts.map +1 -1
  39. package/connection/established.js.map +1 -1
  40. package/connection/index.d.ts +8 -0
  41. package/connection/index.d.ts.map +1 -1
  42. package/connection/index.js +6 -0
  43. package/connection/index.js.map +1 -1
  44. package/connection/reload.d.ts +43 -7
  45. package/connection/reload.d.ts.map +1 -1
  46. package/connection/reload.js +159 -62
  47. package/connection/reload.js.map +1 -1
  48. package/connection/reload.test.js +65 -0
  49. package/connection/reload.test.js.map +1 -1
  50. package/connection/stats.d.ts +92 -0
  51. package/connection/stats.d.ts.map +1 -0
  52. package/connection/stats.js +35 -0
  53. package/connection/stats.js.map +1 -0
  54. package/connection/stats.test.d.ts +2 -0
  55. package/connection/stats.test.d.ts.map +1 -0
  56. package/connection/stats.test.js +120 -0
  57. package/connection/stats.test.js.map +1 -0
  58. package/connection/transport.d.ts +10 -0
  59. package/connection/transport.d.ts.map +1 -0
  60. package/connection/transport.js +12 -0
  61. package/connection/transport.js.map +1 -0
  62. package/consume.d.ts +26 -0
  63. package/consume.d.ts.map +1 -0
  64. package/consume.js +40 -0
  65. package/consume.js.map +1 -0
  66. package/datagram.d.ts +23 -0
  67. package/datagram.d.ts.map +1 -0
  68. package/datagram.js +3 -0
  69. package/datagram.js.map +1 -0
  70. package/error.d.ts +62 -0
  71. package/error.d.ts.map +1 -0
  72. package/error.js +92 -0
  73. package/error.js.map +1 -0
  74. package/error.test.d.ts +2 -0
  75. package/error.test.d.ts.map +1 -0
  76. package/error.test.js +89 -0
  77. package/error.test.js.map +1 -0
  78. package/group.d.ts +125 -36
  79. package/group.d.ts.map +1 -1
  80. package/group.js +244 -67
  81. package/group.js.map +1 -1
  82. package/group.test.js +121 -35
  83. package/group.test.js.map +1 -1
  84. package/ietf/adapter.d.ts +2 -2
  85. package/ietf/adapter.d.ts.map +1 -1
  86. package/ietf/adapter.js +15 -5
  87. package/ietf/adapter.js.map +1 -1
  88. package/ietf/connection.d.ts +19 -6
  89. package/ietf/connection.d.ts.map +1 -1
  90. package/ietf/connection.js +23 -8
  91. package/ietf/connection.js.map +1 -1
  92. package/ietf/ietf.test.js +58 -1
  93. package/ietf/ietf.test.js.map +1 -1
  94. package/ietf/object.d.ts +12 -4
  95. package/ietf/object.d.ts.map +1 -1
  96. package/ietf/object.js +92 -9
  97. package/ietf/object.js.map +1 -1
  98. package/ietf/publisher.d.ts +2 -2
  99. package/ietf/publisher.d.ts.map +1 -1
  100. package/ietf/publisher.js +12 -12
  101. package/ietf/publisher.js.map +1 -1
  102. package/ietf/subscribe_namespace.d.ts +0 -4
  103. package/ietf/subscribe_namespace.d.ts.map +1 -1
  104. package/ietf/subscribe_namespace.js +0 -5
  105. package/ietf/subscribe_namespace.js.map +1 -1
  106. package/ietf/subscriber.d.ts +8 -4
  107. package/ietf/subscriber.d.ts.map +1 -1
  108. package/ietf/subscriber.js +171 -93
  109. package/ietf/subscriber.js.map +1 -1
  110. package/index.d.ts +10 -5
  111. package/index.d.ts.map +1 -1
  112. package/index.js +10 -5
  113. package/index.js.map +1 -1
  114. package/integration.test.js +634 -12
  115. package/integration.test.js.map +1 -1
  116. package/internal.d.ts +14 -0
  117. package/internal.d.ts.map +1 -0
  118. package/internal.js +8 -0
  119. package/internal.js.map +1 -0
  120. package/lite/announce.d.ts +55 -13
  121. package/lite/announce.d.ts.map +1 -1
  122. package/lite/announce.js +221 -68
  123. package/lite/announce.js.map +1 -1
  124. package/lite/announce.test.d.ts +2 -0
  125. package/lite/announce.test.d.ts.map +1 -0
  126. package/lite/announce.test.js +71 -0
  127. package/lite/announce.test.js.map +1 -0
  128. package/lite/connection.d.ts +45 -19
  129. package/lite/connection.d.ts.map +1 -1
  130. package/lite/connection.js +85 -80
  131. package/lite/connection.js.map +1 -1
  132. package/lite/datagram.d.ts +23 -0
  133. package/lite/datagram.d.ts.map +1 -0
  134. package/lite/datagram.js +57 -0
  135. package/lite/datagram.js.map +1 -0
  136. package/lite/datagram.test.d.ts +2 -0
  137. package/lite/datagram.test.d.ts.map +1 -0
  138. package/lite/datagram.test.js +26 -0
  139. package/lite/datagram.test.js.map +1 -0
  140. package/lite/datagram_stream.d.ts +8 -0
  141. package/lite/datagram_stream.d.ts.map +1 -0
  142. package/lite/datagram_stream.js +44 -0
  143. package/lite/datagram_stream.js.map +1 -0
  144. package/lite/datagram_stream.test.d.ts +2 -0
  145. package/lite/datagram_stream.test.d.ts.map +1 -0
  146. package/lite/datagram_stream.test.js +15 -0
  147. package/lite/datagram_stream.test.js.map +1 -0
  148. package/lite/fetch.d.ts.map +1 -1
  149. package/lite/fetch.js +2 -2
  150. package/lite/fetch.js.map +1 -1
  151. package/lite/fetch.test.d.ts +2 -0
  152. package/lite/fetch.test.d.ts.map +1 -0
  153. package/lite/fetch.test.js +41 -0
  154. package/lite/fetch.test.js.map +1 -0
  155. package/lite/index.d.ts +3 -0
  156. package/lite/index.d.ts.map +1 -1
  157. package/lite/index.js +3 -0
  158. package/lite/index.js.map +1 -1
  159. package/lite/publisher.d.ts +13 -6
  160. package/lite/publisher.d.ts.map +1 -1
  161. package/lite/publisher.js +274 -64
  162. package/lite/publisher.js.map +1 -1
  163. package/lite/publisher.test.d.ts +2 -0
  164. package/lite/publisher.test.d.ts.map +1 -0
  165. package/lite/publisher.test.js +65 -0
  166. package/lite/publisher.test.js.map +1 -0
  167. package/lite/setup.d.ts +72 -6
  168. package/lite/setup.d.ts.map +1 -1
  169. package/lite/setup.js +184 -31
  170. package/lite/setup.js.map +1 -1
  171. package/lite/setup.test.d.ts +2 -0
  172. package/lite/setup.test.d.ts.map +1 -0
  173. package/lite/setup.test.js +122 -0
  174. package/lite/setup.test.js.map +1 -0
  175. package/lite/stream.d.ts +12 -3
  176. package/lite/stream.d.ts.map +1 -1
  177. package/lite/stream.js +7 -4
  178. package/lite/stream.js.map +1 -1
  179. package/lite/subscribe.d.ts +37 -23
  180. package/lite/subscribe.d.ts.map +1 -1
  181. package/lite/subscribe.js +70 -55
  182. package/lite/subscribe.js.map +1 -1
  183. package/lite/subscribe.test.d.ts +2 -0
  184. package/lite/subscribe.test.d.ts.map +1 -0
  185. package/lite/subscribe.test.js +68 -0
  186. package/lite/subscribe.test.js.map +1 -0
  187. package/lite/subscriber.d.ts +24 -17
  188. package/lite/subscriber.d.ts.map +1 -1
  189. package/lite/subscriber.js +515 -90
  190. package/lite/subscriber.js.map +1 -1
  191. package/lite/subscriber.test.js +2 -2
  192. package/lite/subscriber.test.js.map +1 -1
  193. package/lite/track.d.ts +25 -13
  194. package/lite/track.d.ts.map +1 -1
  195. package/lite/track.js +60 -35
  196. package/lite/track.js.map +1 -1
  197. package/lite/track.test.js +12 -10
  198. package/lite/track.test.js.map +1 -1
  199. package/lite/version.d.ts +17 -4
  200. package/lite/version.d.ts.map +1 -1
  201. package/lite/version.js +88 -17
  202. package/lite/version.js.map +1 -1
  203. package/mock.d.ts +42 -3
  204. package/mock.d.ts.map +1 -1
  205. package/mock.js +88 -17
  206. package/mock.js.map +1 -1
  207. package/package.json +3 -3
  208. package/path.d.ts +30 -0
  209. package/path.d.ts.map +1 -1
  210. package/path.js +49 -0
  211. package/path.js.map +1 -1
  212. package/path.test.js +41 -0
  213. package/path.test.js.map +1 -1
  214. package/stream.d.ts.map +1 -1
  215. package/stream.js +25 -4
  216. package/stream.js.map +1 -1
  217. package/stream.test.js +42 -0
  218. package/stream.test.js.map +1 -1
  219. package/time.d.ts +109 -86
  220. package/time.d.ts.map +1 -1
  221. package/time.js +80 -30
  222. package/time.js.map +1 -1
  223. package/time.test.d.ts +2 -0
  224. package/time.test.d.ts.map +1 -0
  225. package/time.test.js +12 -0
  226. package/time.test.js.map +1 -0
  227. package/track.d.ts +225 -45
  228. package/track.d.ts.map +1 -1
  229. package/track.js +505 -93
  230. package/track.js.map +1 -1
  231. package/track.test.js +138 -29
  232. package/track.test.js.map +1 -1
  233. package/util/timeout.d.ts +5 -0
  234. package/util/timeout.d.ts.map +1 -0
  235. package/util/timeout.js +18 -0
  236. package/util/timeout.js.map +1 -0
  237. package/util/timeout.test.d.ts +2 -0
  238. package/util/timeout.test.d.ts.map +1 -0
  239. package/util/timeout.test.js +18 -0
  240. package/util/timeout.test.js.map +1 -0
  241. package/varint.d.ts +6 -0
  242. package/varint.d.ts.map +1 -1
  243. package/varint.js +6 -2
  244. package/varint.js.map +1 -1
  245. package/zod.d.ts +4 -4
  246. package/zod.d.ts.map +1 -1
  247. package/zod.js.map +1 -1
  248. package/bandwidth.d.ts +0 -11
  249. package/bandwidth.d.ts.map +0 -1
  250. package/bandwidth.js +0 -7
  251. package/bandwidth.js.map +0 -1
  252. package/util/error.d.ts +0 -3
  253. package/util/error.d.ts.map +0 -1
  254. package/util/error.js +0 -9
  255. package/util/error.js.map +0 -1
  256. package/util/index.d.ts +0 -2
  257. package/util/index.d.ts.map +0 -1
  258. package/util/index.js +0 -3
  259. package/util/index.js.map +0 -1
package/announced.d.ts CHANGED
@@ -1,45 +1,61 @@
1
- import { Signal } from "@moq/signals";
1
+ /**
2
+ * Broadcast announcement streams: which broadcast paths are available under a prefix.
3
+ *
4
+ * @module
5
+ */
6
+ import { type GetPromise } from "@moq/signals";
2
7
  import * as Path from "./path.js";
3
8
  /**
4
9
  * The availability of a broadcast.
5
10
  *
6
11
  * @public
7
12
  */
8
- export interface AnnouncedEntry {
13
+ export interface Event {
14
+ /** Broadcast path relative to the prefix passed to `announced()`. */
9
15
  path: Path.Valid;
16
+ /** True when the broadcast is available, false when it was removed. */
10
17
  active: boolean;
11
18
  }
12
- /** Reactive backing state for an {@link Announced}: the pending queue plus a closed flag. */
13
- export declare class AnnouncedState {
14
- queue: Signal<AnnouncedEntry[]>;
15
- closed: Signal<boolean | Error>;
16
- }
17
19
  /**
18
- * Handles writing announcements to the announcement queue.
20
+ * The write side of an announcement stream.
19
21
  *
20
22
  * @public
21
23
  */
22
- export declare class Announced {
23
- /** Reactive backing state. */
24
- state: AnnouncedState;
24
+ export declare class Producer {
25
+ #private;
25
26
  /** Path prefix this stream is scoped to. */
26
27
  prefix: Path.Valid;
27
- /** Resolves with the abort error (or undefined) once closed. */
28
- readonly closed: Promise<Error | undefined>;
29
28
  constructor(prefix?: Path.Valid);
30
29
  /**
31
- * Writes an announcement to the queue.
32
- * @param announcement - The announcement to write
33
- */
34
- append(announcement: AnnouncedEntry): void;
35
- /**
36
- * Closes the writer.
37
- * @param abort - If provided, throw this exception instead of returning undefined.
30
+ * Settles once the stream closes: `null` on a clean close, or the abort {@link Error}.
31
+ * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
38
32
  */
33
+ get closed(): GetPromise<Error | null>;
34
+ /** A read handle for this announcement stream. */
35
+ consume(): Consumer;
36
+ /** Writes an announcement to the queue. */
37
+ append(event: Event): void;
38
+ /** Closes the writer. Idempotent. */
39
+ close(abort?: Error): void;
40
+ }
41
+ /**
42
+ * The read side of an announcement stream.
43
+ *
44
+ * Created internally: obtain one from {@link Producer.consume} or the connection's
45
+ * `announced(prefix)`.
46
+ *
47
+ * @public
48
+ */
49
+ export declare class Consumer {
50
+ #private;
51
+ /** Path prefix this stream is scoped to. */
52
+ prefix: Path.Valid;
53
+ private constructor();
54
+ /** Settles once the stream closes; see {@link Producer.closed}. */
55
+ get closed(): GetPromise<Error | null>;
56
+ /** Returns the next announcement. */
57
+ next(): Promise<Event | undefined>;
58
+ /** Closes the reader. Idempotent. */
39
59
  close(abort?: Error): void;
40
- /**
41
- * Returns the next announcement.
42
- */
43
- next(): Promise<AnnouncedEntry | undefined>;
44
60
  }
45
61
  //# sourceMappingURL=announced.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"announced.d.ts","sourceRoot":"","sources":["../src/announced.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;CAChB;AAED,6FAA6F;AAC7F,qBAAa,cAAc;IAC1B,KAAK,2BAAoC;IACzC,MAAM,0BAAsC;CAC5C;AAED;;;;GAIG;AACH,qBAAa,SAAS;IACrB,8BAA8B;IAC9B,KAAK,iBAAwB;IAE7B,4CAA4C;IAC5C,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC;IAEnB,gEAAgE;IA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CAAC,KAAK,GAAG,SAAS,CAAC,CAWvC;IAED,qCAAqC;IACrC,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,QAElB;CACD"}
package/announced.js CHANGED
@@ -1,70 +1,104 @@
1
1
  /* @ts-self-types="./announced.d.ts" */
2
- import { Signal } from "@moq/signals";
2
+ /**
3
+ * Broadcast announcement streams: which broadcast paths are available under a prefix.
4
+ *
5
+ * @module
6
+ */
7
+ import { Once, Signal } from "@moq/signals";
3
8
  import * as Path from "./path.js";
4
- /** Reactive backing state for an {@link Announced}: the pending queue plus a closed flag. */
5
- export class AnnouncedState {
9
+ /** Reactive backing state shared by announcement producers and consumers. */
10
+ class AnnounceState {
6
11
  queue = new Signal([]);
7
- closed = new Signal(false);
12
+ closed = new Once();
13
+ }
14
+ // Once.set throws on a second settle, and both ends of a stream can close independently.
15
+ function closeState(state, abort) {
16
+ if (state.closed.peek() !== undefined)
17
+ return;
18
+ state.closed.set(abort ?? null);
19
+ state.queue.mutate((queue) => {
20
+ queue.length = 0;
21
+ });
8
22
  }
9
23
  /**
10
- * Handles writing announcements to the announcement queue.
24
+ * The write side of an announcement stream.
11
25
  *
12
26
  * @public
13
27
  */
14
- export class Announced {
15
- /** Reactive backing state. */
16
- state = new AnnouncedState();
28
+ export class Producer {
17
29
  /** Path prefix this stream is scoped to. */
18
30
  prefix;
19
- /** Resolves with the abort error (or undefined) once closed. */
20
- closed;
31
+ #state = new AnnounceState();
21
32
  constructor(prefix = Path.empty()) {
22
33
  this.prefix = prefix;
23
- this.closed = new Promise((resolve) => {
24
- const dispose = this.state.closed.subscribe((closed) => {
25
- if (!closed)
26
- return;
27
- resolve(closed instanceof Error ? closed : undefined);
28
- dispose();
29
- });
30
- });
31
34
  }
32
35
  /**
33
- * Writes an announcement to the queue.
34
- * @param announcement - The announcement to write
36
+ * Settles once the stream closes: `null` on a clean close, or the abort {@link Error}.
37
+ * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
35
38
  */
36
- append(announcement) {
37
- if (this.state.closed.peek())
38
- throw new Error("announced is closed");
39
- this.state.queue.mutate((queue) => {
40
- queue.push(announcement);
39
+ get closed() {
40
+ return this.#state.closed;
41
+ }
42
+ /** A read handle for this announcement stream. */
43
+ consume() {
44
+ return makeConsumer(this.prefix, this.#state);
45
+ }
46
+ /** Writes an announcement to the queue. */
47
+ append(event) {
48
+ if (this.#state.closed.peek() !== undefined)
49
+ throw new Error("announcements are closed");
50
+ this.#state.queue.mutate((queue) => {
51
+ queue.push(event);
41
52
  });
42
53
  }
43
- /**
44
- * Closes the writer.
45
- * @param abort - If provided, throw this exception instead of returning undefined.
46
- */
54
+ /** Closes the writer. Idempotent. */
47
55
  close(abort) {
48
- this.state.closed.set(abort ?? true);
49
- this.state.queue.mutate((queue) => {
50
- queue.length = 0;
51
- });
56
+ closeState(this.#state, abort);
52
57
  }
53
- /**
54
- * Returns the next announcement.
55
- */
58
+ }
59
+ // Constructs a Consumer from within this module without exposing a public constructor
60
+ // that would leak the unexported AnnounceState. Assigned in the class's static block.
61
+ let makeConsumer;
62
+ /**
63
+ * The read side of an announcement stream.
64
+ *
65
+ * Created internally: obtain one from {@link Producer.consume} or the connection's
66
+ * `announced(prefix)`.
67
+ *
68
+ * @public
69
+ */
70
+ export class Consumer {
71
+ /** Path prefix this stream is scoped to. */
72
+ prefix;
73
+ #state;
74
+ constructor(prefix, state) {
75
+ this.prefix = prefix;
76
+ this.#state = state;
77
+ }
78
+ /** Settles once the stream closes; see {@link Producer.closed}. */
79
+ get closed() {
80
+ return this.#state.closed;
81
+ }
82
+ static {
83
+ makeConsumer = (prefix, state) => new Consumer(prefix, state);
84
+ }
85
+ /** Returns the next announcement. */
56
86
  async next() {
57
87
  for (;;) {
58
- const announce = this.state.queue.peek().shift();
88
+ const announce = this.#state.queue.peek().shift();
59
89
  if (announce)
60
90
  return announce;
61
- const closed = this.state.closed.peek();
91
+ const closed = this.#state.closed.peek();
62
92
  if (closed instanceof Error)
63
93
  throw closed;
64
- if (closed)
94
+ if (closed !== undefined)
65
95
  return undefined;
66
- await Signal.race(this.state.queue, this.state.closed);
96
+ await Signal.race(this.#state.queue, this.#state.closed);
67
97
  }
68
98
  }
99
+ /** Closes the reader. Idempotent. */
100
+ close(abort) {
101
+ closeState(this.#state, abort);
102
+ }
69
103
  }
70
104
  //# sourceMappingURL=announced.js.map
package/announced.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"announced.js","sourceRoot":"","sources":["../src/announced.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAYlC,6FAA6F;AAC7F,MAAM,OAAO,cAAc;IAC1B,KAAK,GAAG,IAAI,MAAM,CAAmB,EAAE,CAAC,CAAC;IACzC,MAAM,GAAG,IAAI,MAAM,CAAkB,KAAK,CAAC,CAAC;CAC5C;AAED;;;;GAIG;AACH,MAAM,OAAO,SAAS;IACrB,8BAA8B;IAC9B,KAAK,GAAG,IAAI,cAAc,EAAE,CAAC;IAE7B,4CAA4C;IAC5C,MAAM,CAAa;IAEnB,gEAAgE;IACvD,MAAM,CAA6B;IAE5C,YAAY,MAAM,GAAG,IAAI,CAAC,KAAK,EAAE;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YACrC,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,MAAM,EAAE,EAAE;gBACtD,IAAI,CAAC,MAAM;oBAAE,OAAO;gBACpB,OAAO,CAAC,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;gBACtD,OAAO,EAAE,CAAC;YACX,CAAC,CAAC,CAAC;QACJ,CAAC,CAAC,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,YAA4B;QAClC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE;YAAE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACrE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACjC,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,KAAa;QAClB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACjC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI;QACT,SAAS,CAAC;YACT,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC;YACjD,IAAI,QAAQ;gBAAE,OAAO,QAAQ,CAAC;YAE9B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACxC,IAAI,MAAM,YAAY,KAAK;gBAAE,MAAM,MAAM,CAAC;YAC1C,IAAI,MAAM;gBAAE,OAAO,SAAS,CAAC;YAE7B,MAAM,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACxD,CAAC;IACF,CAAC;CACD","sourcesContent":["import { Signal } from \"@moq/signals\";\nimport * as Path from \"./path.js\";\n\n/**\n * The availability of a broadcast.\n *\n * @public\n */\nexport interface AnnouncedEntry {\n\tpath: Path.Valid;\n\tactive: boolean;\n}\n\n/** Reactive backing state for an {@link Announced}: the pending queue plus a closed flag. */\nexport class AnnouncedState {\n\tqueue = new Signal<AnnouncedEntry[]>([]);\n\tclosed = new Signal<boolean | Error>(false);\n}\n\n/**\n * Handles writing announcements to the announcement queue.\n *\n * @public\n */\nexport class Announced {\n\t/** Reactive backing state. */\n\tstate = new AnnouncedState();\n\n\t/** Path prefix this stream is scoped to. */\n\tprefix: Path.Valid;\n\n\t/** Resolves with the abort error (or undefined) once closed. */\n\treadonly closed: Promise<Error | undefined>;\n\n\tconstructor(prefix = Path.empty()) {\n\t\tthis.prefix = prefix;\n\t\tthis.closed = new Promise((resolve) => {\n\t\t\tconst dispose = this.state.closed.subscribe((closed) => {\n\t\t\t\tif (!closed) return;\n\t\t\t\tresolve(closed instanceof Error ? closed : undefined);\n\t\t\t\tdispose();\n\t\t\t});\n\t\t});\n\t}\n\n\t/**\n\t * Writes an announcement to the queue.\n\t * @param announcement - The announcement to write\n\t */\n\tappend(announcement: AnnouncedEntry) {\n\t\tif (this.state.closed.peek()) throw new Error(\"announced is closed\");\n\t\tthis.state.queue.mutate((queue) => {\n\t\t\tqueue.push(announcement);\n\t\t});\n\t}\n\n\t/**\n\t * Closes the writer.\n\t * @param abort - If provided, throw this exception instead of returning undefined.\n\t */\n\tclose(abort?: Error) {\n\t\tthis.state.closed.set(abort ?? true);\n\t\tthis.state.queue.mutate((queue) => {\n\t\t\tqueue.length = 0;\n\t\t});\n\t}\n\n\t/**\n\t * Returns the next announcement.\n\t */\n\tasync next(): Promise<AnnouncedEntry | undefined> {\n\t\tfor (;;) {\n\t\t\tconst announce = this.state.queue.peek().shift();\n\t\t\tif (announce) return announce;\n\n\t\t\tconst closed = this.state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed) return undefined;\n\n\t\t\tawait Signal.race(this.state.queue, this.state.closed);\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"announced.js","sourceRoot":"","sources":["../src/announced.ts"],"names":[],"mappings":"AAAA;;;;G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1D,CAAC;IACF,CAAC;IAED,qCAAqC;IACrC,KAAK,CAAC,KAAa;QAClB,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAChC,CAAC;CACD","sourcesContent":["/**\n * Broadcast announcement streams: which broadcast paths are available under a prefix.\n *\n * @module\n */\nimport { type GetPromise, Once, Signal } from \"@moq/signals\";\nimport * as Path from \"./path.js\";\n\n/**\n * The availability of a broadcast.\n *\n * @public\n */\nexport interface Event {\n\t/** Broadcast path relative to the prefix passed to `announced()`. */\n\tpath: Path.Valid;\n\t/** True when the broadcast is available, false when it was removed. */\n\tactive: boolean;\n}\n\n/** Reactive backing state shared by announcement producers and consumers. */\nclass AnnounceState {\n\tqueue = new Signal<Event[]>([]);\n\tclosed = new Once<Error | null>();\n}\n\n// Once.set throws on a second settle, and both ends of a stream can close independently.\nfunction closeState(state: AnnounceState, abort?: Error) {\n\tif (state.closed.peek() !== undefined) return;\n\tstate.closed.set(abort ?? null);\n\tstate.queue.mutate((queue) => {\n\t\tqueue.length = 0;\n\t});\n}\n\n/**\n * The write side of an announcement stream.\n *\n * @public\n */\nexport class Producer {\n\t/** Path prefix this stream is scoped to. */\n\tprefix: Path.Valid;\n\n\t#state = new AnnounceState();\n\n\tconstructor(prefix = Path.empty()) {\n\t\tthis.prefix = prefix;\n\t}\n\n\t/**\n\t * Settles once the stream closes: `null` on a clean close, or the abort {@link Error}.\n\t * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.\n\t */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\t/** A read handle for this announcement stream. */\n\tconsume(): Consumer {\n\t\treturn makeConsumer(this.prefix, this.#state);\n\t}\n\n\t/** Writes an announcement to the queue. */\n\tappend(event: Event) {\n\t\tif (this.#state.closed.peek() !== undefined) throw new Error(\"announcements are closed\");\n\t\tthis.#state.queue.mutate((queue) => {\n\t\t\tqueue.push(event);\n\t\t});\n\t}\n\n\t/** Closes the writer. Idempotent. */\n\tclose(abort?: Error) {\n\t\tcloseState(this.#state, abort);\n\t}\n}\n\n// Constructs a Consumer from within this module without exposing a public constructor\n// that would leak the unexported AnnounceState. Assigned in the class's static block.\nlet makeConsumer: (prefix: Path.Valid, state: AnnounceState) => Consumer;\n\n/**\n * The read side of an announcement stream.\n *\n * Created internally: obtain one from {@link Producer.consume} or the connection's\n * `announced(prefix)`.\n *\n * @public\n */\nexport class Consumer {\n\t/** Path prefix this stream is scoped to. */\n\tprefix: Path.Valid;\n\n\t#state: AnnounceState;\n\n\tprivate constructor(prefix: Path.Valid, state: AnnounceState) {\n\t\tthis.prefix = prefix;\n\t\tthis.#state = state;\n\t}\n\n\t/** Settles once the stream closes; see {@link Producer.closed}. */\n\tget closed(): GetPromise<Error | null> {\n\t\treturn this.#state.closed;\n\t}\n\n\tstatic {\n\t\tmakeConsumer = (prefix, state) => new Consumer(prefix, state);\n\t}\n\n\t/** Returns the next announcement. */\n\tasync next(): Promise<Event | undefined> {\n\t\tfor (;;) {\n\t\t\tconst announce = this.#state.queue.peek().shift();\n\t\t\tif (announce) return announce;\n\n\t\t\tconst closed = this.#state.closed.peek();\n\t\t\tif (closed instanceof Error) throw closed;\n\t\t\tif (closed !== undefined) return undefined;\n\n\t\t\tawait Signal.race(this.#state.queue, this.#state.closed);\n\t\t}\n\t}\n\n\t/** Closes the reader. Idempotent. */\n\tclose(abort?: Error) {\n\t\tcloseState(this.#state, abort);\n\t}\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=announced.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"announced.test.d.ts","sourceRoot":"","sources":["../src/announced.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,37 @@
1
+ /* @ts-self-types="./announced.test.d.ts" */
2
+ import { expect, test } from "bun:test";
3
+ import * as Announce from "./announced.js";
4
+ import * as Path from "./path.js";
5
+ const p = (s) => Path.from(s);
6
+ test("next streams every appended event in order", async () => {
7
+ const producer = new Announce.Producer();
8
+ const consumer = producer.consume();
9
+ producer.append({ path: p("a"), active: true });
10
+ producer.append({ path: p("a"), active: false });
11
+ expect(await consumer.next()).toEqual({ path: p("a"), active: true });
12
+ expect(await consumer.next()).toEqual({ path: p("a"), active: false });
13
+ });
14
+ test("a same-name re-announce is a distinct update", async () => {
15
+ const producer = new Announce.Producer();
16
+ const consumer = producer.consume();
17
+ // A republish (a lite-06 RESTART, or an unannounce+announce that coalesces) arrives as a
18
+ // redundant active:true. The stream carries it as its own update, which is what lets a watcher
19
+ // notice the new instance even though membership never observably flipped.
20
+ producer.append({ path: p("a"), active: true });
21
+ producer.append({ path: p("a"), active: true });
22
+ expect(await consumer.next()).toEqual({ path: p("a"), active: true });
23
+ expect(await consumer.next()).toEqual({ path: p("a"), active: true });
24
+ });
25
+ test("closing resolves next with undefined", async () => {
26
+ const producer = new Announce.Producer();
27
+ const consumer = producer.consume();
28
+ producer.close();
29
+ expect(await consumer.next()).toBeUndefined();
30
+ });
31
+ test("aborting rejects next", async () => {
32
+ const producer = new Announce.Producer();
33
+ const consumer = producer.consume();
34
+ producer.close(new Error("boom"));
35
+ await expect(consumer.next()).rejects.toThrow("boom");
36
+ });
37
+ //# sourceMappingURL=announced.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"announced.test.js","sourceRoot":"","sources":["../src/announced.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,MAAM,CAAC,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAEtC,IAAI,CAAC,4CAA4C,EAAE,KAAK,IAAI,EAAE;IAC7D,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;IACzC,MAAM,Q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AAM,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;AACvE,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,sCAAsC,EAAE,KAAK,IAAI,EAAE;IACvD,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;IACzC,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;IAEpC,QAAQ,CAAC,KAAK,EAAE,CAAC;IACjB,MAAM,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,aAAa,EAAE,CAAC;AAC/C,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,uBAAuB,EAAE,KAAK,IAAI,EAAE;IACxC,MAAM,QAAQ,GAAG,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;IACzC,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;IAEpC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IAClC,MAAM,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;AACvD,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport * as Announce from \"./announced.ts\";\nimport * as Path from \"./path.ts\";\n\nconst p = (s: string) => Path.from(s);\n\ntest(\"next streams every appended event in order\", async () => {\n\tconst producer = new Announce.Producer();\n\tconst consumer = producer.consume();\n\n\tproducer.append({ path: p(\"a\"), active: true });\n\tproducer.append({ path: p(\"a\"), active: false });\n\n\texpect(await consumer.next()).toEqual({ path: p(\"a\"), active: true });\n\texpect(await consumer.next()).toEqual({ path: p(\"a\"), active: false });\n});\n\ntest(\"a same-name re-announce is a distinct update\", async () => {\n\tconst producer = new Announce.Producer();\n\tconst consumer = producer.consume();\n\n\t// A republish (a lite-06 RESTART, or an unannounce+announce that coalesces) arrives as a\n\t// redundant active:true. The stream carries it as its own update, which is what lets a watcher\n\t// notice the new instance even though membership never observably flipped.\n\tproducer.append({ path: p(\"a\"), active: true });\n\tproducer.append({ path: p(\"a\"), active: true });\n\n\texpect(await consumer.next()).toEqual({ path: p(\"a\"), active: true });\n\texpect(await consumer.next()).toEqual({ path: p(\"a\"), active: true });\n});\n\ntest(\"closing resolves next with undefined\", async () => {\n\tconst producer = new Announce.Producer();\n\tconst consumer = producer.consume();\n\n\tproducer.close();\n\texpect(await consumer.next()).toBeUndefined();\n});\n\ntest(\"aborting rejects next\", async () => {\n\tconst producer = new Announce.Producer();\n\tconst consumer = producer.consume();\n\n\tproducer.close(new Error(\"boom\"));\n\tawait expect(consumer.next()).rejects.toThrow(\"boom\");\n});\n"]}
package/broadcast.d.ts CHANGED
@@ -1,38 +1,93 @@
1
- import { Signal } from "@moq/signals";
2
- import { Track } from "./track";
3
- /** A track a subscriber asked for, along with its requested delivery priority. */
4
- export interface TrackRequest {
5
- track: Track;
6
- priority: number;
7
- }
8
- /** Reactive backing state for a {@link Broadcast}: requested tracks plus a closed flag. */
9
- export declare class BroadcastState {
10
- requested: Signal<TrackRequest[]>;
11
- closed: Signal<boolean | Error>;
12
- }
13
1
  /**
14
- * Handles writing and managing tracks in a broadcast.
2
+ * Broadcast role handles: a named collection of tracks produced by a publisher.
3
+ *
4
+ * @module
5
+ */
6
+ import { type GetPromise } from "@moq/signals";
7
+ import type { Consumer as GroupConsumer } from "./group";
8
+ import * as track from "./track";
9
+ /**
10
+ * The write side of a broadcast.
15
11
  *
16
12
  * @public
17
13
  */
18
- export declare class Broadcast {
19
- /** Reactive backing state. */
20
- state: BroadcastState;
21
- /** Resolves with the abort error (or undefined) once closed. */
22
- readonly closed: Promise<Error | undefined>;
23
- constructor();
14
+ export declare class Producer implements track.Broadcast {
15
+ #private;
24
16
  /**
25
- * A track requested over the network.
17
+ * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.
18
+ * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
26
19
  */
27
- requested(): Promise<TrackRequest | undefined>;
20
+ get closed(): GetPromise<Error | null>;
21
+ /** A read handle for this broadcast. */
22
+ consume(): Consumer;
23
+ /** Return the next track requested by a peer. */
24
+ requested(): Promise<track.Request | undefined>;
25
+ /** Insert a track that is served directly, without an on-demand request round-trip. */
26
+ insertTrack(track: track.Producer): void;
27
+ /** Create a track, insert it into the broadcast, and return its producer. */
28
+ createTrack(name: string, info?: Partial<track.Info>): track.Producer;
29
+ /** Remove a statically inserted track by name. */
30
+ removeTrack(name: string): void;
31
+ /** Open a live subscription to a track. Used by the publishing wire layer. */
32
+ subscribe(name: string, options?: track.Subscription): track.Subscriber;
33
+ /** Resolve a track's immutable info. Used by the publishing wire layer. */
34
+ resolveTrackInfo(name: string): Promise<track.Info>;
35
+ /** Fetch a single group from the local retained window. Used by track handles. */
36
+ fetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer>;
37
+ /** A lazy read handle for a track on this broadcast. */
38
+ track(name: string): track.Consumer;
39
+ /** Close the broadcast, optionally with an error to abort waiters. Idempotent. */
40
+ close(abort?: Error): void;
41
+ }
42
+ /**
43
+ * The read side of a broadcast.
44
+ *
45
+ * Created internally: obtain one from {@link Producer.consume} or the connection's
46
+ * `consume(path)`. The wire layers subclass it to resolve tracks over the network.
47
+ *
48
+ * @public
49
+ */
50
+ export declare class Consumer implements track.Broadcast {
51
+ #private;
52
+ protected constructor(state?: never);
28
53
  /**
29
- * Populates the provided track over the network.
54
+ * Settles once the broadcast closes: `null` on a clean close, or the abort {@link Error}.
55
+ * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it.
56
+ *
57
+ * Shared by every {@link clone}: it settles once the last handle closes. The subscribing
58
+ * wire layer peeks it to evict a closed entry from its per-path consume cache.
30
59
  */
31
- subscribe(name: string, priority: number): Track;
60
+ get closed(): GetPromise<Error | null>;
32
61
  /**
33
- * Closes the writer and all associated tracks.
62
+ * Return another handle to the same broadcast, reference-counted with this one.
34
63
  *
35
- * @param abort - If provided, throw this exception instead of returning undefined.
64
+ * Both handles read the same tracks and share one {@link closed} state; the broadcast
65
+ * closes only once *every* handle has {@link close}d. Used by the connection's per-path
66
+ * consume cache to share one subscription across callers. Subclasses that resolve info over
67
+ * the wire override this to preserve their type (see the wire layer's consumed broadcast).
68
+ */
69
+ clone(): Consumer;
70
+ protected shareState(): never;
71
+ /** Get a lazy handle for a track on this broadcast. Repeat subscriptions dedupe onto one upstream subscription. */
72
+ track(name: string): track.Consumer;
73
+ /** Open a live subscription to a track. Used by the subscribing wire layer. Repeat subscriptions to the same track share one upstream subscription. */
74
+ subscribe(name: string, options?: track.Subscription): track.Subscriber;
75
+ /** Return the next track requested by the local consumer. Used by the subscribing wire layer. */
76
+ requested(): Promise<track.Request | undefined>;
77
+ /**
78
+ * Resolve a track's immutable info. Used by track handles. This base resolves it from
79
+ * the local producers; the consuming wire layer overrides it to fetch over the wire.
80
+ */
81
+ resolveTrackInfo(name: string): Promise<track.Info>;
82
+ /**
83
+ * Fetch a single group by sequence. Used by track handles. This base serves from the
84
+ * local retained window; the consuming wire layer overrides it to fetch over the wire
85
+ * (or to reject when the transport has no FETCH).
86
+ */
87
+ fetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise<GroupConsumer>;
88
+ /**
89
+ * Release this handle. The broadcast is closed (optionally with an error to abort waiters)
90
+ * once this was the last live handle; while other {@link clone}s remain open it stays live.
36
91
  */
37
92
  close(abort?: Error): void;
38
93
  }
@@ -1 +1 @@
1
- {"version":3,"file":"broadcast.d.ts","sourceRoot":"","sources":["../src/broadcast.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,kFAAkF;AAClF,MAAM,WAAW,YA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