@moq/net 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 (254) hide show
  1. package/README.md +70 -0
  2. package/announced.d.ts +41 -0
  3. package/announced.d.ts.map +1 -0
  4. package/announced.js +65 -0
  5. package/announced.js.map +1 -0
  6. package/bandwidth.d.ts +11 -0
  7. package/bandwidth.d.ts.map +1 -0
  8. package/bandwidth.js +6 -0
  9. package/bandwidth.js.map +1 -0
  10. package/broadcast.d.ts +35 -0
  11. package/broadcast.d.ts.map +1 -0
  12. package/broadcast.js +72 -0
  13. package/broadcast.js.map +1 -0
  14. package/connection/accept.d.ts +14 -0
  15. package/connection/accept.d.ts.map +1 -0
  16. package/connection/accept.js +133 -0
  17. package/connection/accept.js.map +1 -0
  18. package/connection/connect.d.ts +19 -0
  19. package/connection/connect.d.ts.map +1 -0
  20. package/connection/connect.js +284 -0
  21. package/connection/connect.js.map +1 -0
  22. package/connection/established.d.ts +22 -0
  23. package/connection/established.d.ts.map +1 -0
  24. package/connection/established.js +2 -0
  25. package/connection/established.js.map +1 -0
  26. package/connection/handshake.d.ts +10 -0
  27. package/connection/handshake.d.ts.map +1 -0
  28. package/connection/handshake.js +47 -0
  29. package/connection/handshake.js.map +1 -0
  30. package/connection/index.d.ts +5 -0
  31. package/connection/index.d.ts.map +1 -0
  32. package/connection/index.js +5 -0
  33. package/connection/index.js.map +1 -0
  34. package/connection/reload.d.ts +32 -0
  35. package/connection/reload.d.ts.map +1 -0
  36. package/connection/reload.js +127 -0
  37. package/connection/reload.js.map +1 -0
  38. package/group.d.ts +34 -0
  39. package/group.d.ts.map +1 -0
  40. package/group.js +92 -0
  41. package/group.js.map +1 -0
  42. package/ietf/adapter.d.ts +65 -0
  43. package/ietf/adapter.d.ts.map +1 -0
  44. package/ietf/adapter.js +625 -0
  45. package/ietf/adapter.js.map +1 -0
  46. package/ietf/connection.d.ts +65 -0
  47. package/ietf/connection.d.ts.map +1 -0
  48. package/ietf/connection.js +239 -0
  49. package/ietf/connection.js.map +1 -0
  50. package/ietf/control.d.ts +135 -0
  51. package/ietf/control.d.ts.map +1 -0
  52. package/ietf/control.js +237 -0
  53. package/ietf/control.js.map +1 -0
  54. package/ietf/fetch.d.ts +64 -0
  55. package/ietf/fetch.d.ts.map +1 -0
  56. package/ietf/fetch.js +99 -0
  57. package/ietf/fetch.js.map +1 -0
  58. package/ietf/goaway.d.ts +15 -0
  59. package/ietf/goaway.d.ts.map +1 -0
  60. package/ietf/goaway.js +33 -0
  61. package/ietf/goaway.js.map +1 -0
  62. package/ietf/ietf.test.d.ts +2 -0
  63. package/ietf/ietf.test.d.ts.map +1 -0
  64. package/ietf/ietf.test.js +800 -0
  65. package/ietf/ietf.test.js.map +1 -0
  66. package/ietf/index.d.ts +18 -0
  67. package/ietf/index.d.ts.map +1 -0
  68. package/ietf/index.js +18 -0
  69. package/ietf/index.js.map +1 -0
  70. package/ietf/message.d.ts +4 -0
  71. package/ietf/message.d.ts.map +1 -0
  72. package/ietf/message.js +53 -0
  73. package/ietf/message.js.map +1 -0
  74. package/ietf/namespace.d.ts +5 -0
  75. package/ietf/namespace.d.ts.map +1 -0
  76. package/ietf/namespace.js +21 -0
  77. package/ietf/namespace.js.map +1 -0
  78. package/ietf/object.d.ts +37 -0
  79. package/ietf/object.d.ts.map +1 -0
  80. package/ietf/object.js +129 -0
  81. package/ietf/object.js.map +1 -0
  82. package/ietf/parameters.d.ts +56 -0
  83. package/ietf/parameters.d.ts.map +1 -0
  84. package/ietf/parameters.js +336 -0
  85. package/ietf/parameters.js.map +1 -0
  86. package/ietf/properties.d.ts +4 -0
  87. package/ietf/properties.d.ts.map +1 -0
  88. package/ietf/properties.js +32 -0
  89. package/ietf/properties.js.map +1 -0
  90. package/ietf/publish.d.ts +68 -0
  91. package/ietf/publish.d.ts.map +1 -0
  92. package/ietf/publish.js +191 -0
  93. package/ietf/publish.js.map +1 -0
  94. package/ietf/publish_namespace.d.ts +68 -0
  95. package/ietf/publish_namespace.d.ts.map +1 -0
  96. package/ietf/publish_namespace.js +175 -0
  97. package/ietf/publish_namespace.js.map +1 -0
  98. package/ietf/publisher.d.ts +50 -0
  99. package/ietf/publisher.d.ts.map +1 -0
  100. package/ietf/publisher.js +329 -0
  101. package/ietf/publisher.js.map +1 -0
  102. package/ietf/request.d.ts +52 -0
  103. package/ietf/request.d.ts.map +1 -0
  104. package/ietf/request.js +118 -0
  105. package/ietf/request.js.map +1 -0
  106. package/ietf/setup.d.ts +38 -0
  107. package/ietf/setup.d.ts.map +1 -0
  108. package/ietf/setup.js +128 -0
  109. package/ietf/setup.js.map +1 -0
  110. package/ietf/subscribe.d.ts +66 -0
  111. package/ietf/subscribe.d.ts.map +1 -0
  112. package/ietf/subscribe.js +275 -0
  113. package/ietf/subscribe.js.map +1 -0
  114. package/ietf/subscribe_namespace.d.ts +89 -0
  115. package/ietf/subscribe_namespace.d.ts.map +1 -0
  116. package/ietf/subscribe_namespace.js +195 -0
  117. package/ietf/subscribe_namespace.js.map +1 -0
  118. package/ietf/subscriber.d.ts +53 -0
  119. package/ietf/subscriber.d.ts.map +1 -0
  120. package/ietf/subscriber.js +395 -0
  121. package/ietf/subscriber.js.map +1 -0
  122. package/ietf/track.d.ts +40 -0
  123. package/ietf/track.d.ts.map +1 -0
  124. package/ietf/track.js +107 -0
  125. package/ietf/track.js.map +1 -0
  126. package/ietf/version.d.ts +50 -0
  127. package/ietf/version.d.ts.map +1 -0
  128. package/ietf/version.js +55 -0
  129. package/ietf/version.js.map +1 -0
  130. package/index.d.ts +11 -0
  131. package/index.d.ts.map +1 -0
  132. package/index.js +16 -0
  133. package/index.js.map +1 -0
  134. package/integration.test.d.ts +2 -0
  135. package/integration.test.d.ts.map +1 -0
  136. package/integration.test.js +83 -0
  137. package/integration.test.js.map +1 -0
  138. package/lite/announce.d.ts +35 -0
  139. package/lite/announce.d.ts.map +1 -0
  140. package/lite/announce.js +165 -0
  141. package/lite/announce.js.map +1 -0
  142. package/lite/connection.d.ts +47 -0
  143. package/lite/connection.d.ts.map +1 -0
  144. package/lite/connection.js +218 -0
  145. package/lite/connection.js.map +1 -0
  146. package/lite/fetch.d.ts +14 -0
  147. package/lite/fetch.d.ts.map +1 -0
  148. package/lite/fetch.js +46 -0
  149. package/lite/fetch.js.map +1 -0
  150. package/lite/goaway.d.ts +10 -0
  151. package/lite/goaway.d.ts.map +1 -0
  152. package/lite/goaway.js +36 -0
  153. package/lite/goaway.js.map +1 -0
  154. package/lite/group.d.ts +18 -0
  155. package/lite/group.d.ts.map +1 -0
  156. package/lite/group.js +45 -0
  157. package/lite/group.js.map +1 -0
  158. package/lite/index.d.ts +11 -0
  159. package/lite/index.d.ts.map +1 -0
  160. package/lite/index.js +11 -0
  161. package/lite/index.js.map +1 -0
  162. package/lite/message.d.ts +5 -0
  163. package/lite/message.d.ts.map +1 -0
  164. package/lite/message.js +50 -0
  165. package/lite/message.js.map +1 -0
  166. package/lite/origin.d.ts +19 -0
  167. package/lite/origin.d.ts.map +1 -0
  168. package/lite/origin.js +28 -0
  169. package/lite/origin.js.map +1 -0
  170. package/lite/probe.d.ts +12 -0
  171. package/lite/probe.d.ts.map +1 -0
  172. package/lite/probe.js +59 -0
  173. package/lite/probe.js.map +1 -0
  174. package/lite/publisher.d.ts +56 -0
  175. package/lite/publisher.d.ts.map +1 -0
  176. package/lite/publisher.js +305 -0
  177. package/lite/publisher.js.map +1 -0
  178. package/lite/session.d.ts +36 -0
  179. package/lite/session.d.ts.map +1 -0
  180. package/lite/session.js +129 -0
  181. package/lite/session.js.map +1 -0
  182. package/lite/stream.d.ts +18 -0
  183. package/lite/stream.d.ts.map +1 -0
  184. package/lite/stream.js +11 -0
  185. package/lite/stream.js.map +1 -0
  186. package/lite/subscribe.d.ts +91 -0
  187. package/lite/subscribe.d.ts.map +1 -0
  188. package/lite/subscribe.js +267 -0
  189. package/lite/subscribe.js.map +1 -0
  190. package/lite/subscriber.d.ts +80 -0
  191. package/lite/subscriber.d.ts.map +1 -0
  192. package/lite/subscriber.js +245 -0
  193. package/lite/subscriber.js.map +1 -0
  194. package/lite/version.d.ts +12 -0
  195. package/lite/version.d.ts.map +1 -0
  196. package/lite/version.js +23 -0
  197. package/lite/version.js.map +1 -0
  198. package/mock.d.ts +16 -0
  199. package/mock.d.ts.map +1 -0
  200. package/mock.js +153 -0
  201. package/mock.js.map +1 -0
  202. package/package.json +27 -0
  203. package/path.d.ts +87 -0
  204. package/path.d.ts.map +1 -0
  205. package/path.js +98 -0
  206. package/path.js.map +1 -0
  207. package/path.test.d.ts +2 -0
  208. package/path.test.d.ts.map +1 -0
  209. package/path.test.js +153 -0
  210. package/path.test.js.map +1 -0
  211. package/stream.d.ts +57 -0
  212. package/stream.d.ts.map +1 -0
  213. package/stream.js +325 -0
  214. package/stream.js.map +1 -0
  215. package/stream.test.d.ts +2 -0
  216. package/stream.test.d.ts.map +1 -0
  217. package/stream.test.js +209 -0
  218. package/stream.test.js.map +1 -0
  219. package/time.d.ts +77 -0
  220. package/time.d.ts.map +1 -0
  221. package/time.js +65 -0
  222. package/time.js.map +1 -0
  223. package/track.d.ts +66 -0
  224. package/track.d.ts.map +1 -0
  225. package/track.js +203 -0
  226. package/track.js.map +1 -0
  227. package/track.test.d.ts +2 -0
  228. package/track.test.d.ts.map +1 -0
  229. package/track.test.js +39 -0
  230. package/track.test.js.map +1 -0
  231. package/util/error.d.ts +3 -0
  232. package/util/error.d.ts.map +1 -0
  233. package/util/error.js +8 -0
  234. package/util/error.js.map +1 -0
  235. package/util/hex.d.ts +2 -0
  236. package/util/hex.d.ts.map +1 -0
  237. package/util/hex.js +12 -0
  238. package/util/hex.js.map +1 -0
  239. package/util/index.d.ts +2 -0
  240. package/util/index.d.ts.map +1 -0
  241. package/util/index.js +2 -0
  242. package/util/index.js.map +1 -0
  243. package/varint.d.ts +28 -0
  244. package/varint.d.ts.map +1 -0
  245. package/varint.js +279 -0
  246. package/varint.js.map +1 -0
  247. package/varint.test.d.ts +2 -0
  248. package/varint.test.d.ts.map +1 -0
  249. package/varint.test.js +105 -0
  250. package/varint.test.js.map +1 -0
  251. package/zod.d.ts +6 -0
  252. package/zod.d.ts.map +1 -0
  253. package/zod.js +12 -0
  254. package/zod.js.map +1 -0
package/track.js ADDED
@@ -0,0 +1,203 @@
1
+ import { Signal } from "@moq/signals";
2
+ import { Group } from "./group.js";
3
+ export class TrackState {
4
+ groups = new Signal([]);
5
+ closed = new Signal(false);
6
+ }
7
+ export class Track {
8
+ name;
9
+ state = new TrackState();
10
+ #next;
11
+ #nextSequence = 0;
12
+ closed;
13
+ constructor(name) {
14
+ this.name = name;
15
+ this.closed = new Promise((resolve) => {
16
+ const dispose = this.state.closed.subscribe((closed) => {
17
+ if (!closed)
18
+ return;
19
+ resolve(closed instanceof Error ? closed : undefined);
20
+ dispose();
21
+ });
22
+ });
23
+ }
24
+ /**
25
+ * Appends a new group to the track.
26
+ * @returns A GroupProducer for the new group
27
+ */
28
+ appendGroup() {
29
+ if (this.state.closed.peek())
30
+ throw new Error("track is closed");
31
+ const group = new Group(this.#next ?? 0);
32
+ this.#next = group.sequence + 1;
33
+ this.state.groups.mutate((groups) => {
34
+ groups.push(group);
35
+ groups.sort((a, b) => a.sequence - b.sequence);
36
+ });
37
+ return group;
38
+ }
39
+ /**
40
+ * Inserts an existing group into the track.
41
+ * @param group - The group to insert
42
+ */
43
+ writeGroup(group) {
44
+ if (this.state.closed.peek())
45
+ throw new Error("track is closed");
46
+ // Only advance #next upward (for appendGroup auto-increment).
47
+ if (group.sequence >= (this.#next ?? 0)) {
48
+ this.#next = group.sequence + 1;
49
+ }
50
+ this.state.groups.mutate((groups) => {
51
+ groups.push(group);
52
+ groups.sort((a, b) => a.sequence - b.sequence);
53
+ });
54
+ }
55
+ /**
56
+ * Appends a frame to the track in its own group.
57
+ *
58
+ * @param frame - The frame to append
59
+ */
60
+ writeFrame(frame) {
61
+ const group = this.appendGroup();
62
+ group.writeFrame(frame);
63
+ group.close();
64
+ }
65
+ writeString(str) {
66
+ const group = this.appendGroup();
67
+ group.writeString(str);
68
+ group.close();
69
+ }
70
+ writeJson(json) {
71
+ const group = this.appendGroup();
72
+ group.writeJson(json);
73
+ group.close();
74
+ }
75
+ writeBool(bool) {
76
+ const group = this.appendGroup();
77
+ group.writeBool(bool);
78
+ group.close();
79
+ }
80
+ /**
81
+ * Receive the next group available on this track, in arrival order.
82
+ *
83
+ * Groups may arrive out of order or with gaps due to network conditions.
84
+ * Use {@link nextGroupOrdered} if you need groups in sequence order,
85
+ * skipping those that arrive too late.
86
+ */
87
+ async recvGroup() {
88
+ for (;;) {
89
+ const groups = this.state.groups.peek();
90
+ if (groups.length > 0) {
91
+ return groups.shift();
92
+ }
93
+ const closed = this.state.closed.peek();
94
+ if (closed instanceof Error)
95
+ throw closed;
96
+ if (closed)
97
+ return undefined;
98
+ await Signal.race(this.state.groups, this.state.closed);
99
+ }
100
+ }
101
+ /**
102
+ * @deprecated Use {@link recvGroup} for arrival order, or {@link nextGroupOrdered} for sequence order.
103
+ */
104
+ async nextGroup() {
105
+ return this.recvGroup();
106
+ }
107
+ /**
108
+ * Return the next group with a strictly-greater sequence number than the last returned.
109
+ *
110
+ * Late arrivals (with a sequence number at or below the last one returned) are silently skipped.
111
+ *
112
+ * NOTE: This will be renamed to `nextGroup` in the next major version.
113
+ */
114
+ async nextGroupOrdered() {
115
+ for (;;) {
116
+ const group = await this.recvGroup();
117
+ if (!group)
118
+ return undefined;
119
+ if (group.sequence < this.#nextSequence) {
120
+ group.close();
121
+ continue;
122
+ }
123
+ this.#nextSequence = group.sequence + 1;
124
+ return group;
125
+ }
126
+ }
127
+ async readFrame() {
128
+ return (await this.readFrameSequence())?.data;
129
+ }
130
+ // Returns the sequence number of the group and frame, not just the data.
131
+ async readFrameSequence() {
132
+ for (;;) {
133
+ const groups = this.state.groups.peek();
134
+ // Discard old groups.
135
+ while (groups.length > 1) {
136
+ const frames = groups[0].state.frames.peek();
137
+ const next = frames.shift();
138
+ if (next) {
139
+ const frame = groups[0].state.total.peek() - frames.length - 1;
140
+ return { group: groups[0].sequence, frame, data: next };
141
+ }
142
+ // Skip this old group
143
+ groups.shift()?.close();
144
+ }
145
+ // If there's no groups, wait for a new one.
146
+ if (groups.length === 0) {
147
+ const closed = this.state.closed.peek();
148
+ if (closed instanceof Error)
149
+ throw closed;
150
+ if (closed)
151
+ return undefined;
152
+ await Signal.race(this.state.groups, this.state.closed);
153
+ continue;
154
+ }
155
+ // If there's a group, wait for a frame.
156
+ const group = groups[0];
157
+ const frames = group.state.frames.peek();
158
+ const next = frames.shift();
159
+ if (next) {
160
+ const frame = group.state.total.peek() - frames.length - 1;
161
+ return { group: group.sequence, frame, data: next };
162
+ }
163
+ // If the track is closed, return undefined.
164
+ const closed = this.state.closed.peek();
165
+ if (closed instanceof Error)
166
+ throw closed;
167
+ if (closed)
168
+ return undefined;
169
+ // NOTE: We don't care if the latest group was closed or not.
170
+ await Signal.race(this.state.groups, this.state.closed, group.state.frames);
171
+ }
172
+ }
173
+ async readString() {
174
+ const next = await this.readFrame();
175
+ if (!next)
176
+ return undefined;
177
+ return new TextDecoder().decode(next);
178
+ }
179
+ async readJson() {
180
+ const next = await this.readString();
181
+ if (!next)
182
+ return undefined;
183
+ return JSON.parse(next);
184
+ }
185
+ async readBool() {
186
+ const next = await this.readFrame();
187
+ if (!next)
188
+ return undefined;
189
+ if (next.byteLength !== 1 || !(next[0] === 0 || next[0] === 1))
190
+ throw new Error("invalid bool frame");
191
+ return next[0] === 1;
192
+ }
193
+ /**
194
+ * Closes the publisher and all associated groups.
195
+ */
196
+ close(abort) {
197
+ this.state.closed.set(abort ?? true);
198
+ for (const group of this.state.groups.peek()) {
199
+ group.close(abort);
200
+ }
201
+ }
202
+ }
203
+ //# sourceMappingURL=track.js.map
package/track.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"track.js","sourceRoot":"","sources":["../src/track.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAA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QACrC,IAAI,CAAC,IAAI;YAAE,OAAO,SAAS,CAAC;QAC5B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,QAAQ;QACb,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,SAAS,EAAE,CAAC;QACpC,IAAI,CAAC,IAAI;YAAE,OAAO,SAAS,CAAC;QAC5B,IAAI,IAAI,CAAC,UAAU,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACtG,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAa;QAClB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;QAErC,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;YAC9C,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;IACF,CAAC;CACD","sourcesContent":["import { Signal } from \"@moq/signals\";\nimport { Group } from \"./group.ts\";\n\nexport class TrackState {\n\tgroups = new Signal<Group[]>([]);\n\tclosed = new Signal<boolean | Error>(false);\n}\n\nexport class Track {\n\treadonly name: string;\n\n\tstate = new TrackState();\n\t#next?: number;\n\t#nextSequence = 0;\n\n\treadonly closed: Promise<Error | undefined>;\n\n\tconstructor(name: string) {\n\t\tthis.name = name;\n\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 * Appends a new group to the track.\n\t * @returns A GroupProducer for the new group\n\t */\n\tappendGroup(): Group {\n\t\tif (this.state.closed.peek()) throw new Error(\"track is closed\");\n\n\t\tconst group = new Group(this.#next ?? 0);\n\n\t\tthis.#next = group.sequence + 1;\n\t\tthis.state.groups.mutate((groups) => {\n\t\t\tgroups.push(group);\n\t\t\tgroups.sort((a, b) => a.sequence - b.sequence);\n\t\t});\n\n\t\treturn group;\n\t}\n\n\t/**\n\t * Inserts an existing group into the track.\n\t * @param group - The group to insert\n\t */\n\twriteGroup(group: Group) {\n\t\tif (this.state.closed.peek()) throw new Error(\"track is closed\");\n\n\t\t// Only advance #next upward (for appendGroup auto-increment).\n\t\tif (group.sequence >= (this.#next ?? 0)) {\n\t\t\tthis.#next = group.sequence + 1;\n\t\t}\n\n\t\tthis.state.groups.mutate((groups) => {\n\t\t\tgroups.push(group);\n\t\t\tgroups.sort((a, b) => a.sequence - b.sequence);\n\t\t});\n\t}\n\n\t/**\n\t * Appends a frame to the track in its own group.\n\t *\n\t * @param frame - The frame to append\n\t */\n\twriteFrame(frame: Uint8Array) {\n\t\tconst group = this.appendGroup();\n\t\tgroup.writeFrame(frame);\n\t\tgroup.close();\n\t}\n\n\twriteString(str: string) {\n\t\tconst group = this.appendGroup();\n\t\tgroup.writeString(str);\n\t\tgroup.close();\n\t}\n\n\twriteJson(json: unknown) {\n\t\tconst group = this.appendGroup();\n\t\tgroup.writeJson(json);\n\t\tgroup.close();\n\t}\n\n\twriteBool(bool: boolean) {\n\t\tconst group = this.appendGroup();\n\t\tgroup.writeBool(bool);\n\t\tgroup.close();\n\t}\n\n\t/**\n\t * Receive the next group available on this track, in arrival order.\n\t *\n\t * Groups may arrive out of order or with gaps due to network conditions.\n\t * Use {@link nextGroupOrdered} if you need groups in sequence order,\n\t * skipping those that arrive too late.\n\t */\n\tasync recvGroup(): Promise<Group | undefined> {\n\t\tfor (;;) {\n\t\t\tconst groups = this.state.groups.peek();\n\t\t\tif (groups.length > 0) {\n\t\t\t\treturn groups.shift();\n\t\t\t}\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.groups, this.state.closed);\n\t\t}\n\t}\n\n\t/**\n\t * @deprecated Use {@link recvGroup} for arrival order, or {@link nextGroupOrdered} for sequence order.\n\t */\n\tasync nextGroup(): Promise<Group | undefined> {\n\t\treturn this.recvGroup();\n\t}\n\n\t/**\n\t * Return the next group with a strictly-greater sequence number than the last returned.\n\t *\n\t * Late arrivals (with a sequence number at or below the last one returned) are silently skipped.\n\t *\n\t * NOTE: This will be renamed to `nextGroup` in the next major version.\n\t */\n\tasync nextGroupOrdered(): Promise<Group | undefined> {\n\t\tfor (;;) {\n\t\t\tconst group = await this.recvGroup();\n\t\t\tif (!group) return undefined;\n\t\t\tif (group.sequence < this.#nextSequence) {\n\t\t\t\tgroup.close();\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.#nextSequence = group.sequence + 1;\n\t\t\treturn group;\n\t\t}\n\t}\n\n\tasync readFrame(): Promise<Uint8Array | undefined> {\n\t\treturn (await this.readFrameSequence())?.data;\n\t}\n\n\t// Returns the sequence number of the group and frame, not just the data.\n\tasync readFrameSequence(): Promise<{ group: number; frame: number; data: Uint8Array } | undefined> {\n\t\tfor (;;) {\n\t\t\tconst groups = this.state.groups.peek();\n\n\t\t\t// Discard old groups.\n\t\t\twhile (groups.length > 1) {\n\t\t\t\tconst frames = groups[0].state.frames.peek();\n\t\t\t\tconst next = frames.shift();\n\t\t\t\tif (next) {\n\t\t\t\t\tconst frame = groups[0].state.total.peek() - frames.length - 1;\n\t\t\t\t\treturn { group: groups[0].sequence, frame, data: next };\n\t\t\t\t}\n\n\t\t\t\t// Skip this old group\n\t\t\t\tgroups.shift()?.close();\n\t\t\t}\n\n\t\t\t// If there's no groups, wait for a new one.\n\t\t\tif (groups.length === 0) {\n\t\t\t\tconst closed = this.state.closed.peek();\n\t\t\t\tif (closed instanceof Error) throw closed;\n\t\t\t\tif (closed) return undefined;\n\n\t\t\t\tawait Signal.race(this.state.groups, this.state.closed);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// If there's a group, wait for a frame.\n\t\t\tconst group = groups[0];\n\t\t\tconst frames = group.state.frames.peek();\n\t\t\tconst next = frames.shift();\n\t\t\tif (next) {\n\t\t\t\tconst frame = group.state.total.peek() - frames.length - 1;\n\t\t\t\treturn { group: group.sequence, frame, data: next };\n\t\t\t}\n\n\t\t\t// If the track is closed, return undefined.\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\t// NOTE: We don't care if the latest group was closed or not.\n\t\t\tawait Signal.race(this.state.groups, this.state.closed, group.state.frames);\n\t\t}\n\t}\n\n\tasync readString(): Promise<string | undefined> {\n\t\tconst next = await this.readFrame();\n\t\tif (!next) return undefined;\n\t\treturn new TextDecoder().decode(next);\n\t}\n\n\tasync readJson(): Promise<unknown | undefined> {\n\t\tconst next = await this.readString();\n\t\tif (!next) return undefined;\n\t\treturn JSON.parse(next);\n\t}\n\n\tasync readBool(): Promise<boolean | undefined> {\n\t\tconst next = await this.readFrame();\n\t\tif (!next) return undefined;\n\t\tif (next.byteLength !== 1 || !(next[0] === 0 || next[0] === 1)) throw new Error(\"invalid bool frame\");\n\t\treturn next[0] === 1;\n\t}\n\n\t/**\n\t * Closes the publisher and all associated groups.\n\t */\n\tclose(abort?: Error) {\n\t\tthis.state.closed.set(abort ?? true);\n\n\t\tfor (const group of this.state.groups.peek()) {\n\t\t\tgroup.close(abort);\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=track.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"track.test.d.ts","sourceRoot":"","sources":["../src/track.test.ts"],"names":[],"mappings":""}
package/track.test.js ADDED
@@ -0,0 +1,39 @@
1
+ import { expect, test } from "bun:test";
2
+ import { Group } from "./group.js";
3
+ import { Track } from "./track.js";
4
+ test("nextGroupOrdered skips late arrivals", async () => {
5
+ const track = new Track("test");
6
+ track.writeGroup(new Group(5));
7
+ const first = await track.nextGroupOrdered();
8
+ expect(first?.sequence).toBe(5);
9
+ // Late arrivals with sequence <= last returned are skipped.
10
+ track.writeGroup(new Group(3));
11
+ track.writeGroup(new Group(4));
12
+ track.writeGroup(new Group(7));
13
+ const next = await track.nextGroupOrdered();
14
+ expect(next?.sequence).toBe(7);
15
+ });
16
+ test("nextGroupOrdered returns buffered groups in sequence", async () => {
17
+ const track = new Track("test");
18
+ track.writeGroup(new Group(3));
19
+ track.writeGroup(new Group(5));
20
+ expect((await track.nextGroupOrdered())?.sequence).toBe(3);
21
+ expect((await track.nextGroupOrdered())?.sequence).toBe(5);
22
+ });
23
+ test("recvGroup after nextGroupOrdered still returns late arrivals", async () => {
24
+ const track = new Track("test");
25
+ track.writeGroup(new Group(5));
26
+ // Ordered returns seq 5, advancing its cursor.
27
+ const ordered = await track.nextGroupOrdered();
28
+ expect(ordered?.sequence).toBe(5);
29
+ // recvGroup is independent of the ordered cursor: a late seq 3 still surfaces.
30
+ track.writeGroup(new Group(3));
31
+ const recv = await track.recvGroup();
32
+ expect(recv?.sequence).toBe(3);
33
+ });
34
+ test("nextGroupOrdered returns undefined when track closes", async () => {
35
+ const track = new Track("test");
36
+ track.close();
37
+ expect(await track.nextGroupOrdered()).toBeUndefined();
38
+ });
39
+ //# sourceMappingURL=track.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"track.test.js","sourceRoot":"","sources":["../src/track.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,IAAI,CAAC,sCAAsC,EAAE,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,CAAC,8DAA8D,EAAE,KAAK,IAAI,EAAE;IAC/E,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC;IAEhC,KAAK,CAAC,UAAU,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAE/B,+CAA+C;IAC/C,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,gBAAgB,EAAE,CAAC;IAC/C,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAElC,+EAA+E;IAC/E,KAAK,CAAC,UAAU,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/B,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,SAAS,EAAE,CAAC;IACrC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAChC,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,sDAAsD,EAAE,KAAK,IAAI,EAAE;IACvE,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC;IAChC,KAAK,CAAC,KAAK,EAAE,CAAC;IACd,MAAM,CAAC,MAAM,KAAK,CAAC,gBAAgB,EAAE,CAAC,CAAC,aAAa,EAAE,CAAC;AACxD,CAAC,CAAC,CAAC","sourcesContent":["import { expect, test } from \"bun:test\";\nimport { Group } from \"./group.ts\";\nimport { Track } from \"./track.ts\";\n\ntest(\"nextGroupOrdered skips late arrivals\", async () => {\n\tconst track = new Track(\"test\");\n\n\ttrack.writeGroup(new Group(5));\n\n\tconst first = await track.nextGroupOrdered();\n\texpect(first?.sequence).toBe(5);\n\n\t// Late arrivals with sequence <= last returned are skipped.\n\ttrack.writeGroup(new Group(3));\n\ttrack.writeGroup(new Group(4));\n\ttrack.writeGroup(new Group(7));\n\n\tconst next = await track.nextGroupOrdered();\n\texpect(next?.sequence).toBe(7);\n});\n\ntest(\"nextGroupOrdered returns buffered groups in sequence\", async () => {\n\tconst track = new Track(\"test\");\n\n\ttrack.writeGroup(new Group(3));\n\ttrack.writeGroup(new Group(5));\n\n\texpect((await track.nextGroupOrdered())?.sequence).toBe(3);\n\texpect((await track.nextGroupOrdered())?.sequence).toBe(5);\n});\n\ntest(\"recvGroup after nextGroupOrdered still returns late arrivals\", async () => {\n\tconst track = new Track(\"test\");\n\n\ttrack.writeGroup(new Group(5));\n\n\t// Ordered returns seq 5, advancing its cursor.\n\tconst ordered = await track.nextGroupOrdered();\n\texpect(ordered?.sequence).toBe(5);\n\n\t// recvGroup is independent of the ordered cursor: a late seq 3 still surfaces.\n\ttrack.writeGroup(new Group(3));\n\tconst recv = await track.recvGroup();\n\texpect(recv?.sequence).toBe(3);\n});\n\ntest(\"nextGroupOrdered returns undefined when track closes\", async () => {\n\tconst track = new Track(\"test\");\n\ttrack.close();\n\texpect(await track.nextGroupOrdered()).toBeUndefined();\n});\n"]}
@@ -0,0 +1,3 @@
1
+ export declare function error(err: unknown): Error;
2
+ export declare function unreachable(value: never): never;
3
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/util/error.ts"],"names":[],"mappings":"AACA,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,KAAK,CAEzC;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAE/C"}
package/util/error.js ADDED
@@ -0,0 +1,8 @@
1
+ // I hate javascript.
2
+ export function error(err) {
3
+ return err instanceof Error ? err : new Error(String(err));
4
+ }
5
+ export function unreachable(value) {
6
+ throw new Error(`unreachable: ${value}`);
7
+ }
8
+ //# sourceMappingURL=error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/util/error.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,MAAM,UAAU,KAAK,CAAC,GAAY;IACjC,OAAO,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAY;IACvC,MAAM,IAAI,KAAK,CAAC,gBAAgB,KAAK,EAAE,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["// I hate javascript.\nexport function error(err: unknown): Error {\n\treturn err instanceof Error ? err : new Error(String(err));\n}\n\nexport function unreachable(value: never): never {\n\tthrow new Error(`unreachable: ${value}`);\n}\n"]}
package/util/hex.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function toBytes(hex: string): Uint8Array<ArrayBuffer>;
2
+ //# sourceMappingURL=hex.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/util/hex.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,2BAYlC"}
package/util/hex.js ADDED
@@ -0,0 +1,12 @@
1
+ export function toBytes(hex) {
2
+ hex = hex.startsWith("0x") ? hex.slice(2) : hex;
3
+ if (hex.length % 2) {
4
+ throw new Error("invalid hex string length");
5
+ }
6
+ const matches = hex.match(/.{2}/g);
7
+ if (!matches) {
8
+ throw new Error("invalid hex string format");
9
+ }
10
+ return new Uint8Array(matches.map((byte) => parseInt(byte, 16)));
11
+ }
12
+ //# sourceMappingURL=hex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hex.js","sourceRoot":"","sources":["../../src/util/hex.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,OAAO,CAAC,GAAW;IAClC,GAAG,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;IAChD,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC9C,CAAC;IAED,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACnC,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC9C,CAAC;IAED,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC","sourcesContent":["export function toBytes(hex: string) {\n\thex = hex.startsWith(\"0x\") ? hex.slice(2) : hex;\n\tif (hex.length % 2) {\n\t\tthrow new Error(\"invalid hex string length\");\n\t}\n\n\tconst matches = hex.match(/.{2}/g);\n\tif (!matches) {\n\t\tthrow new Error(\"invalid hex string format\");\n\t}\n\n\treturn new Uint8Array(matches.map((byte) => parseInt(byte, 16)));\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export * from "./error.ts";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
package/util/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./error.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC","sourcesContent":["export * from \"./error.ts\";\n"]}
package/varint.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ export declare const MAX_U6: number;
2
+ export declare const MAX_U14: number;
3
+ export declare const MAX_U30: number;
4
+ export declare const MAX_U53: number;
5
+ export declare function sizeLeadingOnes(v: number | bigint): number;
6
+ export declare function encodeLeadingOnesTo(dst: ArrayBuffer, v: number | bigint): Uint8Array;
7
+ export declare function encodeLeadingOnes(v: number | bigint): Uint8Array;
8
+ export declare function decodeLeadingOnes(buf: Uint8Array): [bigint, Uint8Array];
9
+ /**
10
+ * Returns the number of bytes needed to encode a value as a varint.
11
+ */
12
+ export declare function size(v: number): number;
13
+ /**
14
+ * Encodes a number or bigint into a scratch buffer.
15
+ * Used by stream.ts to avoid allocations.
16
+ */
17
+ export declare function encodeTo(dst: ArrayBuffer, v: number | bigint): Uint8Array;
18
+ /**
19
+ * Encodes a number as a QUIC variable-length integer.
20
+ * Returns a new Uint8Array containing the encoded bytes.
21
+ */
22
+ export declare function encode(v: number): Uint8Array;
23
+ /**
24
+ * Decodes a QUIC variable-length integer from a buffer.
25
+ * Returns a tuple of [value, remaining buffer].
26
+ */
27
+ export declare function decode(buf: Uint8Array): [number, Uint8Array];
28
+ //# sourceMappingURL=varint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"varint.d.ts","sourceRoot":"","sources":["../src/varint.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,MAAM,QAAa,CAAC;AACjC,eAAO,MAAM,OAAO,QAAc,CAAC;AACnC,eAAO,MAAM,OAAO,QAAc,CAAC;AACnC,eAAO,MAAM,OAAO,QAA0B,CAAC;AAgB/C,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAY1D;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,CAkDpF;AAED,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,CAEhE;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,UAAU,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAmFvE;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAMtC;AA6BD;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,CAmBzE;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,UAAU,CAE5C;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,UAAU,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CA8B5D"}
package/varint.js ADDED
@@ -0,0 +1,279 @@
1
+ // QUIC variable-length integer encoding/decoding
2
+ // https://www.rfc-editor.org/rfc/rfc9000#section-16
3
+ export const MAX_U6 = 2 ** 6 - 1;
4
+ export const MAX_U14 = 2 ** 14 - 1;
5
+ export const MAX_U30 = 2 ** 30 - 1;
6
+ export const MAX_U53 = Number.MAX_SAFE_INTEGER;
7
+ // Leading-ones varint encoding/decoding (draft-17 Section 1.4.1)
8
+ // Encoding scheme:
9
+ // 0xxxxxxx → 1 byte (7 bits)
10
+ // 10xxxxxx + 1B → 2 bytes (14 bits)
11
+ // 110xxxxx + 2B → 3 bytes (21 bits)
12
+ // 1110xxxx + 3B → 4 bytes (28 bits)
13
+ // 11110xxx + 4B → 5 bytes (35 bits)
14
+ // 111110xx + 5B → 6 bytes (42 bits)
15
+ // 1111110x + 6B → 7 bytes (49 bits), draft-18+ only (invalid in draft-17 per #1595)
16
+ // 11111110 + 7B → 8 bytes (56 bits)
17
+ // 11111111 + 8B → 9 bytes (64 bits)
18
+ const MAX_U64 = (1n << 64n) - 1n;
19
+ export function sizeLeadingOnes(v) {
20
+ const b = BigInt(v);
21
+ if (b < 0n)
22
+ throw new RangeError(`value is negative: ${v}`);
23
+ if (b > MAX_U64)
24
+ throw new RangeError(`value exceeds 64 bits: ${v}`);
25
+ if (b < 1n << 7n)
26
+ return 1;
27
+ if (b < 1n << 14n)
28
+ return 2;
29
+ if (b < 1n << 21n)
30
+ return 3;
31
+ if (b < 1n << 28n)
32
+ return 4;
33
+ if (b < 1n << 35n)
34
+ return 5;
35
+ if (b < 1n << 42n)
36
+ return 6;
37
+ if (b < 1n << 56n)
38
+ return 8;
39
+ return 9;
40
+ }
41
+ export function encodeLeadingOnesTo(dst, v) {
42
+ const x = BigInt(v);
43
+ if (x < 0n)
44
+ throw new RangeError(`underflow, value is negative: ${v}`);
45
+ if (x > MAX_U64)
46
+ throw new RangeError(`value exceeds 64 bits: ${v}`);
47
+ const view = new DataView(dst);
48
+ if (x < 1n << 7n) {
49
+ view.setUint8(0, Number(x));
50
+ return new Uint8Array(dst, 0, 1);
51
+ }
52
+ if (x < 1n << 14n) {
53
+ view.setUint8(0, 0x80 | Number(x >> 8n));
54
+ view.setUint8(1, Number(x & 0xffn));
55
+ return new Uint8Array(dst, 0, 2);
56
+ }
57
+ if (x < 1n << 21n) {
58
+ view.setUint8(0, 0xc0 | Number(x >> 16n));
59
+ view.setUint16(1, Number(x & 0xffffn));
60
+ return new Uint8Array(dst, 0, 3);
61
+ }
62
+ if (x < 1n << 28n) {
63
+ view.setUint8(0, 0xe0 | Number(x >> 24n));
64
+ view.setUint8(1, Number((x >> 16n) & 0xffn));
65
+ view.setUint16(2, Number(x & 0xffffn));
66
+ return new Uint8Array(dst, 0, 4);
67
+ }
68
+ if (x < 1n << 35n) {
69
+ view.setUint8(0, 0xf0 | Number(x >> 32n));
70
+ view.setUint32(1, Number(x & 0xffffffffn));
71
+ return new Uint8Array(dst, 0, 5);
72
+ }
73
+ if (x < 1n << 42n) {
74
+ view.setUint8(0, 0xf8 | Number(x >> 40n));
75
+ view.setUint8(1, Number((x >> 32n) & 0xffn));
76
+ view.setUint32(2, Number(x & 0xffffffffn));
77
+ return new Uint8Array(dst, 0, 6);
78
+ }
79
+ if (x < 1n << 56n) {
80
+ // 11111110 + 7 bytes
81
+ view.setUint8(0, 0xfe);
82
+ view.setUint8(1, Number((x >> 48n) & 0xffn));
83
+ view.setUint16(2, Number((x >> 32n) & 0xffffn));
84
+ view.setUint32(4, Number(x & 0xffffffffn));
85
+ return new Uint8Array(dst, 0, 8);
86
+ }
87
+ // 11111111 + 8 bytes
88
+ view.setUint8(0, 0xff);
89
+ view.setBigUint64(1, x);
90
+ return new Uint8Array(dst, 0, 9);
91
+ }
92
+ export function encodeLeadingOnes(v) {
93
+ return encodeLeadingOnesTo(new ArrayBuffer(9), v);
94
+ }
95
+ export function decodeLeadingOnes(buf) {
96
+ if (buf.length === 0)
97
+ throw new Error("buffer is empty");
98
+ const b = buf[0];
99
+ // Count leading 1-bits
100
+ let ones = 0;
101
+ for (let bit = 7; bit >= 0; bit--) {
102
+ if (b & (1 << bit))
103
+ ones++;
104
+ else
105
+ break;
106
+ }
107
+ // 1111110x is a 7-byte form: invalid on draft-17, allowed on draft-18+ per #1595.
108
+ // This standalone decoder is permissive (Postel-style) since we lack version context here.
109
+ let totalSize;
110
+ if (ones <= 5)
111
+ totalSize = ones + 1;
112
+ else if (ones === 6)
113
+ totalSize = 7;
114
+ else if (ones === 7)
115
+ totalSize = 8;
116
+ else
117
+ totalSize = 9; // ones === 8
118
+ if (buf.length < totalSize) {
119
+ throw new Error(`buffer too short: need ${totalSize} bytes, have ${buf.length}`);
120
+ }
121
+ const view = new DataView(buf.buffer, buf.byteOffset, totalSize);
122
+ const remain = buf.subarray(totalSize);
123
+ let value;
124
+ switch (ones) {
125
+ case 0:
126
+ value = BigInt(b);
127
+ break;
128
+ case 1:
129
+ value = (BigInt(b & 0x3f) << 8n) | BigInt(buf[1]);
130
+ break;
131
+ case 2:
132
+ value = (BigInt(b & 0x1f) << 16n) | BigInt(view.getUint16(1));
133
+ break;
134
+ case 3:
135
+ value = (BigInt(b & 0x0f) << 24n) | (BigInt(buf[1]) << 16n) | (BigInt(buf[2]) << 8n) | BigInt(buf[3]);
136
+ break;
137
+ case 4:
138
+ value = (BigInt(b & 0x07) << 32n) | BigInt(view.getUint32(1));
139
+ break;
140
+ case 5:
141
+ value =
142
+ (BigInt(b & 0x03) << 40n) |
143
+ (BigInt(buf[1]) << 32n) |
144
+ (BigInt(buf[2]) << 24n) |
145
+ (BigInt(buf[3]) << 16n) |
146
+ (BigInt(buf[4]) << 8n) |
147
+ BigInt(buf[5]);
148
+ break;
149
+ case 6: {
150
+ // 1111110x + 6 bytes = 49 bits (draft-18+)
151
+ value =
152
+ (BigInt(b & 0x01) << 48n) |
153
+ (BigInt(buf[1]) << 40n) |
154
+ (BigInt(buf[2]) << 32n) |
155
+ (BigInt(buf[3]) << 24n) |
156
+ (BigInt(buf[4]) << 16n) |
157
+ (BigInt(buf[5]) << 8n) |
158
+ BigInt(buf[6]);
159
+ break;
160
+ }
161
+ case 7: {
162
+ // 11111110 + 7 bytes = 56 usable bits
163
+ const hi = new Uint8Array(8);
164
+ hi[0] = 0;
165
+ hi.set(buf.subarray(1, 8), 1);
166
+ value = new DataView(hi.buffer).getBigUint64(0);
167
+ break;
168
+ }
169
+ case 8: {
170
+ // 11111111 + 8 bytes = 64 bits
171
+ value = new DataView(buf.buffer, buf.byteOffset + 1, 8).getBigUint64(0);
172
+ break;
173
+ }
174
+ default:
175
+ throw new Error("impossible");
176
+ }
177
+ return [value, remain];
178
+ }
179
+ /**
180
+ * Returns the number of bytes needed to encode a value as a varint.
181
+ */
182
+ export function size(v) {
183
+ if (v <= MAX_U6)
184
+ return 1;
185
+ if (v <= MAX_U14)
186
+ return 2;
187
+ if (v <= MAX_U30)
188
+ return 4;
189
+ if (v <= MAX_U53)
190
+ return 8;
191
+ throw new Error(`overflow, value larger than 53-bits: ${v}`);
192
+ }
193
+ // Helper functions for writing to an ArrayBuffer
194
+ function setUint8(dst, v) {
195
+ const buffer = new Uint8Array(dst, 0, 1);
196
+ buffer[0] = v;
197
+ return buffer;
198
+ }
199
+ function setUint16(dst, v) {
200
+ const view = new DataView(dst, 0, 2);
201
+ view.setUint16(0, v);
202
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
203
+ }
204
+ function setUint32(dst, v) {
205
+ const view = new DataView(dst, 0, 4);
206
+ view.setUint32(0, v);
207
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
208
+ }
209
+ function setUint64(dst, v) {
210
+ const view = new DataView(dst, 0, 8);
211
+ view.setBigUint64(0, v);
212
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
213
+ }
214
+ const MAX_U62 = 2n ** 62n - 1n;
215
+ /**
216
+ * Encodes a number or bigint into a scratch buffer.
217
+ * Used by stream.ts to avoid allocations.
218
+ */
219
+ export function encodeTo(dst, v) {
220
+ const b = BigInt(v);
221
+ if (b < 0n) {
222
+ throw new Error(`underflow, value is negative: ${v}`);
223
+ }
224
+ if (b > MAX_U62) {
225
+ throw new Error(`overflow, value larger than 62-bits: ${v}`);
226
+ }
227
+ const n = Number(b);
228
+ if (n <= MAX_U6) {
229
+ return setUint8(dst, n);
230
+ }
231
+ if (n <= MAX_U14) {
232
+ return setUint16(dst, n | 0x4000);
233
+ }
234
+ if (n <= MAX_U30) {
235
+ return setUint32(dst, n | 0x80000000);
236
+ }
237
+ return setUint64(dst, b | 0xc000000000000000n);
238
+ }
239
+ /**
240
+ * Encodes a number as a QUIC variable-length integer.
241
+ * Returns a new Uint8Array containing the encoded bytes.
242
+ */
243
+ export function encode(v) {
244
+ return encodeTo(new ArrayBuffer(8), v);
245
+ }
246
+ /**
247
+ * Decodes a QUIC variable-length integer from a buffer.
248
+ * Returns a tuple of [value, remaining buffer].
249
+ */
250
+ export function decode(buf) {
251
+ if (buf.length === 0) {
252
+ throw new Error("buffer is empty");
253
+ }
254
+ const size = 1 << ((buf[0] & 0xc0) >> 6);
255
+ if (buf.length < size) {
256
+ throw new Error(`buffer too short: need ${size} bytes, have ${buf.length}`);
257
+ }
258
+ const view = new DataView(buf.buffer, buf.byteOffset, size);
259
+ const remain = buf.subarray(size);
260
+ let v;
261
+ if (size === 1) {
262
+ v = buf[0] & 0x3f;
263
+ }
264
+ else if (size === 2) {
265
+ v = view.getUint16(0) & 0x3fff;
266
+ }
267
+ else if (size === 4) {
268
+ v = view.getUint32(0) & 0x3fffffff;
269
+ }
270
+ else if (size === 8) {
271
+ // NOTE: Precision loss above 2^53, but we're using number type
272
+ v = Number(view.getBigUint64(0) & 0x3fffffffffffffffn);
273
+ }
274
+ else {
275
+ throw new Error("impossible");
276
+ }
277
+ return [v, remain];
278
+ }
279
+ //# sourceMappingURL=varint.js.map