@moq/net 0.4.1 → 0.4.3

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 (184) hide show
  1. package/broadcast.d.ts +21 -7
  2. package/broadcast.d.ts.map +1 -1
  3. package/broadcast.js +38 -16
  4. package/broadcast.js.map +1 -1
  5. package/connection/accept.js +6 -4
  6. package/connection/accept.js.map +1 -1
  7. package/connection/connect.d.ts.map +1 -1
  8. package/connection/connect.js +5 -4
  9. package/connection/connect.js.map +1 -1
  10. package/connection/forward.js +54 -47
  11. package/connection/forward.js.map +1 -1
  12. package/connection/goaway.d.ts +34 -0
  13. package/connection/goaway.d.ts.map +1 -0
  14. package/connection/goaway.js +176 -0
  15. package/connection/goaway.js.map +1 -0
  16. package/connection/handshake.d.ts +4 -2
  17. package/connection/handshake.d.ts.map +1 -1
  18. package/connection/handshake.js +42 -10
  19. package/connection/handshake.js.map +1 -1
  20. package/connection/pool.d.ts +16 -1
  21. package/connection/pool.d.ts.map +1 -1
  22. package/connection/pool.js +32 -10
  23. package/connection/pool.js.map +1 -1
  24. package/connection/reload.d.ts.map +1 -1
  25. package/connection/reload.js +184 -30
  26. package/connection/reload.js.map +1 -1
  27. package/error.d.ts +11 -0
  28. package/error.d.ts.map +1 -1
  29. package/error.js +14 -0
  30. package/error.js.map +1 -1
  31. package/errors.d.ts +1 -1
  32. package/errors.d.ts.map +1 -1
  33. package/errors.js +1 -1
  34. package/errors.js.map +1 -1
  35. package/group.d.ts.map +1 -1
  36. package/group.js +5 -4
  37. package/group.js.map +1 -1
  38. package/hop.d.ts +8 -0
  39. package/hop.d.ts.map +1 -1
  40. package/hop.js +16 -0
  41. package/hop.js.map +1 -1
  42. package/ietf/adapter.d.ts +4 -0
  43. package/ietf/adapter.d.ts.map +1 -1
  44. package/ietf/adapter.js +19 -8
  45. package/ietf/adapter.js.map +1 -1
  46. package/ietf/cluster.d.ts +3 -3
  47. package/ietf/cluster.js +3 -3
  48. package/ietf/cluster.js.map +1 -1
  49. package/ietf/connection.d.ts.map +1 -1
  50. package/ietf/connection.js +110 -37
  51. package/ietf/connection.js.map +1 -1
  52. package/ietf/fetch.d.ts +7 -20
  53. package/ietf/fetch.d.ts.map +1 -1
  54. package/ietf/fetch.js +52 -22
  55. package/ietf/fetch.js.map +1 -1
  56. package/ietf/filter.d.ts +20 -4
  57. package/ietf/filter.d.ts.map +1 -1
  58. package/ietf/filter.js +165 -17
  59. package/ietf/filter.js.map +1 -1
  60. package/ietf/goaway.d.ts +9 -1
  61. package/ietf/goaway.d.ts.map +1 -1
  62. package/ietf/goaway.js +20 -0
  63. package/ietf/goaway.js.map +1 -1
  64. package/ietf/hidden.js +1 -1
  65. package/ietf/hidden.js.map +1 -1
  66. package/ietf/index.d.ts +1 -1
  67. package/ietf/index.d.ts.map +1 -1
  68. package/ietf/index.js +1 -1
  69. package/ietf/index.js.map +1 -1
  70. package/ietf/object.d.ts +9 -6
  71. package/ietf/object.d.ts.map +1 -1
  72. package/ietf/object.js +47 -38
  73. package/ietf/object.js.map +1 -1
  74. package/ietf/parameters.d.ts +13 -8
  75. package/ietf/parameters.d.ts.map +1 -1
  76. package/ietf/parameters.js +177 -30
  77. package/ietf/parameters.js.map +1 -1
  78. package/ietf/publish.d.ts +1 -0
  79. package/ietf/publish.d.ts.map +1 -1
  80. package/ietf/publish.js +1 -0
  81. package/ietf/publish.js.map +1 -1
  82. package/ietf/publish_namespace.d.ts +2 -2
  83. package/ietf/publish_namespace.js +2 -2
  84. package/ietf/publish_namespace.js.map +1 -1
  85. package/ietf/publisher.js +404 -202
  86. package/ietf/publisher.js.map +1 -1
  87. package/ietf/request_stream.d.ts +7 -0
  88. package/ietf/request_stream.d.ts.map +1 -0
  89. package/ietf/request_stream.js +20 -0
  90. package/ietf/request_stream.js.map +1 -0
  91. package/ietf/subscribe.d.ts +7 -1
  92. package/ietf/subscribe.d.ts.map +1 -1
  93. package/ietf/subscribe.js +24 -17
  94. package/ietf/subscribe.js.map +1 -1
  95. package/ietf/subscribe_namespace.d.ts.map +1 -1
  96. package/ietf/subscribe_namespace.js +7 -3
  97. package/ietf/subscribe_namespace.js.map +1 -1
  98. package/ietf/subscriber.js +83 -56
  99. package/ietf/subscriber.js.map +1 -1
  100. package/ietf/token.d.ts +2 -0
  101. package/ietf/token.d.ts.map +1 -0
  102. package/ietf/token.js +99 -0
  103. package/ietf/token.js.map +1 -0
  104. package/ietf/track.d.ts +4 -0
  105. package/ietf/track.d.ts.map +1 -1
  106. package/ietf/track.js +6 -20
  107. package/ietf/track.js.map +1 -1
  108. package/index.d.ts +1 -1
  109. package/index.d.ts.map +1 -1
  110. package/index.js.map +1 -1
  111. package/internal.d.ts +12 -3
  112. package/internal.d.ts.map +1 -1
  113. package/internal.js +32 -0
  114. package/internal.js.map +1 -1
  115. package/lite/announce.d.ts.map +1 -1
  116. package/lite/announce.js +7 -4
  117. package/lite/announce.js.map +1 -1
  118. package/lite/connection.d.ts.map +1 -1
  119. package/lite/connection.js +22 -7
  120. package/lite/connection.js.map +1 -1
  121. package/lite/datagram.d.ts +3 -2
  122. package/lite/datagram.d.ts.map +1 -1
  123. package/lite/datagram.js +7 -8
  124. package/lite/datagram.js.map +1 -1
  125. package/lite/group.d.ts +12 -8
  126. package/lite/group.d.ts.map +1 -1
  127. package/lite/group.js +37 -16
  128. package/lite/group.js.map +1 -1
  129. package/lite/message.d.ts +2 -2
  130. package/lite/message.d.ts.map +1 -1
  131. package/lite/message.js +14 -5
  132. package/lite/message.js.map +1 -1
  133. package/lite/publisher.js +39 -45
  134. package/lite/publisher.js.map +1 -1
  135. package/lite/setup.d.ts +1 -1
  136. package/lite/setup.d.ts.map +1 -1
  137. package/lite/setup.js +22 -19
  138. package/lite/setup.js.map +1 -1
  139. package/lite/subscribe.d.ts.map +1 -1
  140. package/lite/subscribe.js +4 -5
  141. package/lite/subscribe.js.map +1 -1
  142. package/lite/subscriber.d.ts.map +1 -1
  143. package/lite/subscriber.js +210 -127
  144. package/lite/subscriber.js.map +1 -1
  145. package/origin.d.ts +9 -1
  146. package/origin.d.ts.map +1 -1
  147. package/origin.js +341 -146
  148. package/origin.js.map +1 -1
  149. package/package.json +2 -2
  150. package/stream.d.ts +77 -12
  151. package/stream.d.ts.map +1 -1
  152. package/stream.js +359 -122
  153. package/stream.js.map +1 -1
  154. package/tail.d.ts +7 -0
  155. package/tail.d.ts.map +1 -1
  156. package/tail.js +68 -22
  157. package/tail.js.map +1 -1
  158. package/track.d.ts +14 -3
  159. package/track.d.ts.map +1 -1
  160. package/track.js +170 -76
  161. package/track.js.map +1 -1
  162. package/util/abort.d.ts +2 -0
  163. package/util/abort.d.ts.map +1 -0
  164. package/util/abort.js +20 -0
  165. package/util/abort.js.map +1 -0
  166. package/util/u64.d.ts +39 -0
  167. package/util/u64.d.ts.map +1 -0
  168. package/util/u64.js +83 -0
  169. package/util/u64.js.map +1 -0
  170. package/util/varint.d.ts +29 -0
  171. package/util/varint.d.ts.map +1 -0
  172. package/util/varint.js +198 -0
  173. package/util/varint.js.map +1 -0
  174. package/varint.d.ts +10 -6
  175. package/varint.d.ts.map +1 -1
  176. package/varint.js +40 -237
  177. package/varint.js.map +1 -1
  178. package/wire.d.ts +17 -3
  179. package/wire.d.ts.map +1 -1
  180. package/wire.js.map +1 -1
  181. package/ietf/control.d.ts +0 -135
  182. package/ietf/control.d.ts.map +0 -1
  183. package/ietf/control.js +0 -242
  184. package/ietf/control.js.map +0 -1
@@ -5,17 +5,18 @@ import * as broadcast from "../broadcast.js";
5
5
  import { BroadcastCache } from "../consume.js";
6
6
  import { closeError, controlTimeout, error, ProtocolViolation, reason, sessionCause } from "../error.js";
7
7
  import * as netGroup from "../group.js";
8
- import { Cost, routesEqual, UNKNOWN_HOP } from "../hop.js";
8
+ import { Cost, randomHop, routesEqual, stampHops, UNKNOWN_HOP } from "../hop.js";
9
9
  import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.js";
10
10
  import * as Path from "../path.js";
11
- import { TAIL_GRACE_MS, Tail } from "../tail.js";
11
+ import { UnexpectedEnd } from "../stream.js";
12
+ import { Tail } from "../tail.js";
12
13
  import { Timestamp } from "../time.js";
13
14
  import { TimeoutError, withTimeout } from "../util/timeout.js";
14
15
  import { overrideBroadcastWire, wireOf } from "../wire.js";
15
16
  import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.js";
16
17
  import * as Cluster from "./cluster.js";
17
18
  import { requestReason, toRequestCode } from "./error.js";
18
- import { Frame } from "./object.js";
19
+ import { Frame, hasFirstObjectBit } from "./object.js";
19
20
  import { fromWire, toWire } from "./priority.js";
20
21
  import { PublishDone, PublishError, publishDoneClean } from "./publish.js";
21
22
  import { PublishNamespaceDone, PublishNamespaceError, PublishNamespaceOk, PublishNamespaceUpdate, } from "./publish_namespace.js";
@@ -39,6 +40,7 @@ function sees(filter, path) {
39
40
  */
40
41
  export class Subscriber {
41
42
  #session;
43
+ #localClose = false;
42
44
  // The transport, so a request cut off by the session's close ends with the session's
43
45
  // error. Optional for tests that drive a bare session.
44
46
  #quic;
@@ -54,6 +56,9 @@ export class Subscriber {
54
56
  #timescales = new Map();
55
57
  // Dedup consumed broadcasts per path: repeat consume() calls share one subscription.
56
58
  #consumes = new BroadcastCache();
59
+ // A random Hop ID of this connection's own, written as the first hop of any path that
60
+ // names no publisher, so a publisher that reconnects reads as a new one.
61
+ #stamp = randomHop();
57
62
  // Paths with a legacy PUBLISH_NAMESPACE request in flight, reserved synchronously.
58
63
  // The count below is only taken once the OK is written, and two requests that both
59
64
  // got past the duplicate check before either attached would both take one.
@@ -70,6 +75,10 @@ export class Subscriber {
70
75
  #announcedConsumers = new Map();
71
76
  // Whether the peer understands the HIDDEN parameter (MoQ Hidden).
72
77
  #hidden;
78
+ /** Marks this subscriber's deliberate local session close. @internal */
79
+ close() {
80
+ this.#localClose = true;
81
+ }
73
82
  /**
74
83
  * Creates a new Subscriber instance.
75
84
  *
@@ -92,11 +101,17 @@ export class Subscriber {
92
101
  #reflected(advert) {
93
102
  return advert !== undefined && this.#cluster !== undefined && Cluster.loops(advert, this.#cluster.self);
94
103
  }
95
- /** The route an advertisement carries; one without a path is anonymous and free. */
104
+ /**
105
+ * The route an advertisement carries; one without a path is free. A path that names no
106
+ * publisher, or none at all, gets this connection's stamp in front of a 0.
107
+ */
96
108
  #route(advert) {
97
109
  if (advert === undefined)
98
- return { hops: [UNKNOWN_HOP], cost: Cost.zero };
99
- return { hops: advert.hops, cost: { warm: advert.cost, cold: advert.cost } };
110
+ return { hops: [this.#stamp, UNKNOWN_HOP], cost: Cost.zero };
111
+ // A full chain, or a stamp colliding with an entry (a 1-in-2^53 draw), keeps the
112
+ // path as sent.
113
+ const hops = stampHops(advert.hops, this.#stamp) ?? [...advert.hops];
114
+ return { hops, cost: { warm: advert.cost, cold: advert.cost } };
100
115
  }
101
116
  /**
102
117
  * Gets an announced reader matching `scope`. Paths are relative to the session,
@@ -156,11 +171,16 @@ export class Subscriber {
156
171
  * Replace the stored route for a path that is already announced. A no-op when the
157
172
  * hops and cost did not change; otherwise consumers hear `updated` so a forwarder
158
173
  * can reprice without retracting.
174
+ *
175
+ * A new first hop is a new publisher: holders keep their broadcast to drain, but the
176
+ * next consume starts fresh rather than reusing the old publisher's cached track info.
159
177
  */
160
178
  #updateAnnounce(path, route) {
161
179
  const existing = this.#announced.get(path);
162
180
  if (existing === undefined || routesEqual(existing.route, route))
163
181
  return;
182
+ if (existing.route.hops[0] !== route.hops[0])
183
+ this.#consumes.evict(path);
164
184
  existing.route = route;
165
185
  console.debug(`announced: broadcast=${path} rerouted`);
166
186
  for (const [consumer, filter] of this.#announcedConsumers) {
@@ -515,7 +535,7 @@ export class Subscriber {
515
535
  }
516
536
  catch (err) {
517
537
  const e = await sessionCause(this.#quic, err);
518
- producer.close(e);
538
+ producer.close(this.#localClose ? undefined : e);
519
539
  stream.abort(e);
520
540
  console.warn(`subscribe error: id=${requestId} broadcast=${broadcast} track=${request.name} error=${reason(e)}`);
521
541
  }
@@ -532,26 +552,25 @@ export class Subscriber {
532
552
  * An error status aborts the track with it. A clean one leaves streams in flight, since
533
553
  * QUIC does not order them, so wait until the Stream Count many have been read, or a
534
554
  * bounded grace for the ones that never arrive (the draft says to use a timeout). The count
535
- * is only a hint: a peer may send 0 regardless, so 0 waits out the grace. A request stream
536
- * that ends without one ends the track the same way.
555
+ * is only a hint: a peer may send 0 regardless, so 0 waits out the grace. A request
556
+ * stream that FINs without PUBLISH_DONE is a protocol violation.
537
557
  */
538
558
  async #runPublishDone(stream, subscription) {
539
559
  const version = this.#session.version;
540
- let count;
541
- if (!(await stream.reader.done())) {
542
- const typeId = await stream.reader.u53();
543
- if (typeId !== PublishDone.id) {
544
- throw new ProtocolViolation(`unexpected message on a subscription: 0x${typeId.toString(16)}`);
545
- }
546
- const done = await PublishDone.decode(stream.reader, version);
547
- if (!publishDoneClean(done.statusCode, version)) {
548
- throw new Error(`publish done: status=0x${done.statusCode.toString(16)} reason=${done.reasonPhrase}`);
549
- }
550
- count = done.streamCount;
560
+ if (await stream.reader.done())
561
+ throw new ProtocolViolation("subscribe stream ended without PUBLISH_DONE");
562
+ const typeId = await stream.reader.u53();
563
+ if (typeId !== PublishDone.id) {
564
+ throw new ProtocolViolation(`unexpected message on a subscription: 0x${typeId.toString(16)}`);
565
+ }
566
+ const done = await PublishDone.decode(stream.reader, version);
567
+ if (!publishDoneClean(done.statusCode, version)) {
568
+ throw new Error(`publish done: status=0x${done.statusCode.toString(16)} reason=${done.reasonPhrase}`);
551
569
  }
570
+ const count = done.streamCount;
552
571
  const { tail, track } = subscription;
553
- const complete = () => count !== undefined && count > 0n && BigInt(tail.streams) >= count;
554
- await tail.settle(complete, TAIL_GRACE_MS, track.closed);
572
+ const complete = () => count > 0n && BigInt(tail.streams) >= count;
573
+ await tail.settle(complete, track.closed);
555
574
  }
556
575
  /**
557
576
  * Tell the publisher to stop serving a subscription we are walking away from.
@@ -756,20 +775,7 @@ export class Subscriber {
756
775
  }
757
776
  }
758
777
  else {
759
- // A different original publisher is a different advertisement, which the
760
- // draft has withdrawn and made again: its content is not continuous with
761
- // what is held. Refusing the update closes the stream, which is that
762
- // withdrawal.
763
- if (update.update.hops !== undefined && update.update.hops[0] !== held.hops[0]) {
764
- console.warn(`publish_namespace update changes the publisher: broadcast=${path}`);
765
- await stream.writer.u53(RequestError.id);
766
- await new RequestError({
767
- errorCode: toRequestCode("not_supported", "publish_namespace", version),
768
- reasonPhrase: "a new publisher is a new advertisement",
769
- }).encode(stream.writer, version);
770
- stream.close();
771
- return;
772
- }
778
+ // A different original publisher applies in place too, as it does inline.
773
779
  held = Cluster.apply(held, update.update);
774
780
  }
775
781
  // A path that now runs through us is unusable, so give it back. The update
@@ -857,7 +863,7 @@ export class Subscriber {
857
863
  subscription = await this.#aliases.get(group.trackAlias);
858
864
  }
859
865
  catch (err) {
860
- const e = error(err);
866
+ const e = await sessionCause(this.#quic, err);
861
867
  // Ours: we cancelled the subscription and the publisher has not stopped yet.
862
868
  // Anything else on this alias is the publisher sending data for a track it never
863
869
  // acknowledged, which is worth seeing.
@@ -875,38 +881,58 @@ export class Subscriber {
875
881
  let producer;
876
882
  const open = () => {
877
883
  if (!producer) {
884
+ // The publisher contradicted its own end, which no later group can repair.
885
+ if (subscription.end !== undefined && group.groupId >= subscription.end) {
886
+ throw new ProtocolViolation(`group ${group.groupId} is at or past the declared end ${subscription.end}`);
887
+ }
878
888
  producer = new netGroup.Producer(group.groupId);
879
889
  track.writeGroup(producer);
880
890
  }
881
891
  return producer;
882
892
  };
883
893
  try {
884
- // FIRST_OBJECT clear says this stream starts partway through the group, which the
885
- // draft lets a publisher do to answer a filter. Nothing above here can use it: the
886
- // objects that would arrive are not decodable without the missing head, and a group
887
- // is the unit an application resyncs on. Drop it and pick up at the next group, the
888
- // same degradation as a publisher that no longer holds the head.
894
+ // FIRST_OBJECT clear is the publisher's claim that the stream starts partway
895
+ // through the group. The first Object ID is absolute either way, and IDs start
896
+ // at 0, so a clear bit on object 0 is still the whole group. Any other first ID,
897
+ // or a stream with no object, has a hole at the front: drop it and pick up at
898
+ // the next group.
889
899
  //
890
- // This only saves reading a stream we would throw away. The bit is the publisher's
891
- // claim, so what is enforced is the object ids themselves: `Frame.decode` holds every
892
- // object to starting at 0 and incrementing by 1, whatever the header said and on the
893
- // drafts that have no such bit to read.
894
- if (!group.flags.firstObject) {
895
- console.debug(`dropping a group with no head: alias=${group.trackAlias} group=${group.groupId}`);
896
- stream.stop(new Error("a group must start at object 0"));
897
- return;
900
+ // Drafts before the bit cannot say this in the header, so the first ID is the only
901
+ // signal: a non-zero one is the same hole and is dropped the same way, but an empty
902
+ // stream there is still a group. A later gap, or a header that claimed the group
903
+ // starts at object 0, still fails it: `Frame.decode` refuses every non-zero delta.
904
+ const legacy = !hasFirstObjectBit(this.#session.version);
905
+ if (!group.flags.firstObject || legacy) {
906
+ let id;
907
+ try {
908
+ id = await stream.peekU62();
909
+ }
910
+ catch (err) {
911
+ if (!(err instanceof UnexpectedEnd))
912
+ throw err;
913
+ }
914
+ if (id !== 0n && !(legacy && id === undefined)) {
915
+ console.debug(`dropping a group with no head: alias=${group.trackAlias} group=${group.groupId}`);
916
+ stream.stop(new Error("a group must start at object 0"));
917
+ return;
918
+ }
898
919
  }
899
920
  // The alias binds after SUBSCRIBE_OK commits the track property; an omitted
900
921
  // header priority inherits it (draft-21 section 10.4).
901
922
  if (!group.flags.hasPriority)
902
923
  group.publisherPriority = toWire((await track.info()).priority);
924
+ const decode = (c) => Frame.decode(c, group.flags, this.#timescales.get(group.trackAlias));
903
925
  for (;;) {
904
- // Only the group's own stream ends it: a track that closes first has already
905
- // closed (or aborted) this group through its cache.
906
- const done = await (producer ? race([stream.done(), producer.closed]) : stream.done());
907
- if (done !== false)
926
+ // Every object already buffered is written without an await, so the reader wakes
927
+ // once per batch rather than once per object. Only the group's own stream ends it:
928
+ // a track that closes first has already closed (or aborted) this group through its
929
+ // cache.
930
+ const frame = stream.tryDecode(decode) ??
931
+ (await (producer
932
+ ? race([stream.decodeMaybe(decode), producer.closed])
933
+ : stream.decodeMaybe(decode)));
934
+ if (!frame || frame instanceof Error)
908
935
  break;
909
- const frame = await Frame.decode(stream, group.flags, this.#timescales.get(group.trackAlias), this.#session.version);
910
936
  if (frame.endOfTrack) {
911
937
  // No object at or past this location exists: after the group's last object
912
938
  // the track ends with it, and at object 0 it ends before it.
@@ -918,6 +944,7 @@ export class Subscriber {
918
944
  catch (err) {
919
945
  throw new ProtocolViolation(`invalid END_OF_TRACK: ${reason(error(err))}`);
920
946
  }
947
+ subscription.end ??= end;
921
948
  return;
922
949
  }
923
950
  if (frame.payload === undefined)
@@ -928,7 +955,7 @@ export class Subscriber {
928
955
  open().close();
929
956
  }
930
957
  catch (err) {
931
- const e = error(err);
958
+ const e = await sessionCause(this.#quic, err);
932
959
  if (e instanceof ProtocolViolation) {
933
960
  // The publisher broke the track's end, which no later group can repair.
934
961
  producer?.close(e);