@moq/net 0.4.8 → 0.22.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 (662) hide show
  1. package/README.md +7 -1
  2. package/announce.d.ts +1 -1
  3. package/announce.d.ts.map +1 -1
  4. package/announce.js +1 -1
  5. package/announce.js.map +1 -1
  6. package/announced.d.ts +51 -47
  7. package/announced.d.ts.map +1 -1
  8. package/announced.js +183 -31
  9. package/announced.js.map +1 -1
  10. package/announced.test-util.d.ts +2 -0
  11. package/announced.test-util.d.ts.map +1 -0
  12. package/announced.test-util.js +27 -0
  13. package/announced.test-util.js.map +1 -0
  14. package/auth.d.ts +64 -0
  15. package/auth.d.ts.map +1 -0
  16. package/auth.js +20 -0
  17. package/auth.js.map +1 -0
  18. package/auth_gate.d.ts +2 -0
  19. package/auth_gate.d.ts.map +1 -0
  20. package/auth_gate.js +24 -0
  21. package/auth_gate.js.map +1 -0
  22. package/auth_session.d.ts +49 -0
  23. package/auth_session.d.ts.map +1 -0
  24. package/auth_session.js +1097 -0
  25. package/auth_session.js.map +1 -0
  26. package/bandwidth.d.ts +129 -25
  27. package/bandwidth.d.ts.map +1 -1
  28. package/bandwidth.js +410 -138
  29. package/bandwidth.js.map +1 -1
  30. package/bandwidth_api.d.ts +1 -1
  31. package/bandwidth_api.d.ts.map +1 -1
  32. package/bandwidth_api.js +1 -1
  33. package/bandwidth_api.js.map +1 -1
  34. package/broadcast.d.ts +62 -36
  35. package/broadcast.d.ts.map +1 -1
  36. package/broadcast.js +556 -172
  37. package/broadcast.js.map +1 -1
  38. package/cache.d.ts +103 -0
  39. package/cache.d.ts.map +1 -0
  40. package/cache.js +254 -0
  41. package/cache.js.map +1 -0
  42. package/claim.test-util.d.ts +5 -0
  43. package/claim.test-util.d.ts.map +1 -0
  44. package/claim.test-util.js +13 -0
  45. package/claim.test-util.js.map +1 -0
  46. package/connection/accept.d.ts +20 -12
  47. package/connection/accept.d.ts.map +1 -1
  48. package/connection/accept.js +97 -129
  49. package/connection/accept.js.map +1 -1
  50. package/connection/browser.d.ts.map +1 -1
  51. package/connection/browser.js +28 -19
  52. package/connection/browser.js.map +1 -1
  53. package/connection/connect.d.ts +119 -23
  54. package/connection/connect.d.ts.map +1 -1
  55. package/connection/connect.js +535 -216
  56. package/connection/connect.js.map +1 -1
  57. package/connection/dials.d.ts +45 -0
  58. package/connection/dials.d.ts.map +1 -0
  59. package/connection/dials.js +109 -0
  60. package/connection/dials.js.map +1 -0
  61. package/connection/endpoint.d.ts +60 -0
  62. package/connection/endpoint.d.ts.map +1 -0
  63. package/connection/endpoint.js +114 -0
  64. package/connection/endpoint.js.map +1 -0
  65. package/connection/forward.d.ts +7 -1
  66. package/connection/forward.d.ts.map +1 -1
  67. package/connection/forward.js +265 -80
  68. package/connection/forward.js.map +1 -1
  69. package/connection/goaway.d.ts +16 -9
  70. package/connection/goaway.d.ts.map +1 -1
  71. package/connection/goaway.js +22 -108
  72. package/connection/goaway.js.map +1 -1
  73. package/connection/handshake.d.ts +32 -6
  74. package/connection/handshake.d.ts.map +1 -1
  75. package/connection/handshake.js +97 -20
  76. package/connection/handshake.js.map +1 -1
  77. package/connection/index.d.ts +3 -6
  78. package/connection/index.d.ts.map +1 -1
  79. package/connection/index.js +1 -2
  80. package/connection/index.js.map +1 -1
  81. package/connection/liveness.d.ts +14 -0
  82. package/connection/liveness.d.ts.map +1 -0
  83. package/connection/liveness.js +30 -0
  84. package/connection/liveness.js.map +1 -0
  85. package/connection/pool.d.ts +137 -58
  86. package/connection/pool.d.ts.map +1 -1
  87. package/connection/pool.js +281 -117
  88. package/connection/pool.js.map +1 -1
  89. package/connection/reload.d.ts +60 -12
  90. package/connection/reload.d.ts.map +1 -1
  91. package/connection/reload.js +859 -233
  92. package/connection/reload.js.map +1 -1
  93. package/connection/session.d.ts +91 -0
  94. package/connection/session.d.ts.map +1 -0
  95. package/connection/session.js +2 -0
  96. package/connection/session.js.map +1 -0
  97. package/connection/stats.d.ts +20 -11
  98. package/connection/stats.d.ts.map +1 -1
  99. package/connection/stats.js +64 -8
  100. package/connection/stats.js.map +1 -1
  101. package/connection/ticker.d.ts +4 -0
  102. package/connection/ticker.d.ts.map +1 -0
  103. package/connection/ticker.js +66 -0
  104. package/connection/ticker.js.map +1 -0
  105. package/connection/tokens.d.ts +2 -0
  106. package/connection/tokens.d.ts.map +1 -0
  107. package/connection/tokens.js +404 -0
  108. package/connection/tokens.js.map +1 -0
  109. package/connection/transport.d.ts.map +1 -1
  110. package/connection/transport.js +4 -4
  111. package/connection/transport.js.map +1 -1
  112. package/connection/withdrawal.d.ts +2 -0
  113. package/connection/withdrawal.d.ts.map +1 -0
  114. package/connection/withdrawal.js +23 -0
  115. package/connection/withdrawal.js.map +1 -0
  116. package/consume.js +213 -46
  117. package/consume.js.map +1 -1
  118. package/cover.d.ts +59 -0
  119. package/cover.d.ts.map +1 -0
  120. package/cover.js +375 -0
  121. package/cover.js.map +1 -0
  122. package/datagram.d.ts +70 -11
  123. package/datagram.d.ts.map +1 -1
  124. package/datagram.js +317 -1
  125. package/datagram.js.map +1 -1
  126. package/datagram_stream.d.ts +18 -0
  127. package/datagram_stream.d.ts.map +1 -0
  128. package/datagram_stream.js +67 -0
  129. package/datagram_stream.js.map +1 -0
  130. package/defined.test-util.d.ts +2 -0
  131. package/defined.test-util.d.ts.map +1 -0
  132. package/defined.test-util.js +7 -0
  133. package/defined.test-util.js.map +1 -0
  134. package/delivered.d.ts +75 -0
  135. package/delivered.d.ts.map +1 -0
  136. package/delivered.js +228 -0
  137. package/delivered.js.map +1 -0
  138. package/demand.d.ts +173 -0
  139. package/demand.d.ts.map +1 -0
  140. package/demand.js +582 -0
  141. package/demand.js.map +1 -0
  142. package/drops.d.ts +93 -0
  143. package/drops.d.ts.map +1 -0
  144. package/drops.js +283 -0
  145. package/drops.js.map +1 -0
  146. package/error.d.ts +120 -21
  147. package/error.d.ts.map +1 -1
  148. package/error.js +230 -37
  149. package/error.js.map +1 -1
  150. package/errors.d.ts +2 -2
  151. package/errors.d.ts.map +1 -1
  152. package/errors.js +2 -2
  153. package/errors.js.map +1 -1
  154. package/fill.d.ts +174 -0
  155. package/fill.d.ts.map +1 -0
  156. package/fill.js +760 -0
  157. package/fill.js.map +1 -0
  158. package/flate/decoder.d.ts +2 -0
  159. package/flate/decoder.d.ts.map +1 -0
  160. package/flate/decoder.js +63 -0
  161. package/flate/decoder.js.map +1 -0
  162. package/flate/encoder.d.ts +2 -0
  163. package/flate/encoder.d.ts.map +1 -0
  164. package/flate/encoder.js +64 -0
  165. package/flate/encoder.js.map +1 -0
  166. package/flate/group.d.ts +34 -0
  167. package/flate/group.d.ts.map +1 -0
  168. package/flate/group.js +140 -0
  169. package/flate/group.js.map +1 -0
  170. package/frame.d.ts +80 -0
  171. package/frame.d.ts.map +1 -0
  172. package/frame.js +296 -0
  173. package/frame.js.map +1 -0
  174. package/frame.test-util.d.ts +5 -0
  175. package/frame.test-util.d.ts.map +1 -0
  176. package/frame.test-util.js +9 -0
  177. package/frame.test-util.js.map +1 -0
  178. package/group.d.ts +108 -45
  179. package/group.d.ts.map +1 -1
  180. package/group.js +1616 -307
  181. package/group.js.map +1 -1
  182. package/group_api.d.ts +7 -0
  183. package/group_api.d.ts.map +1 -0
  184. package/group_api.js +7 -0
  185. package/group_api.js.map +1 -0
  186. package/hop.d.ts +62 -76
  187. package/hop.d.ts.map +1 -1
  188. package/hop.js +92 -64
  189. package/hop.js.map +1 -1
  190. package/identity.d.ts +39 -0
  191. package/identity.d.ts.map +1 -0
  192. package/identity.js +47 -0
  193. package/identity.js.map +1 -0
  194. package/ietf/active_count.d.ts +2 -0
  195. package/ietf/active_count.d.ts.map +1 -0
  196. package/ietf/active_count.js +30 -0
  197. package/ietf/active_count.js.map +1 -0
  198. package/ietf/aliases.js +69 -35
  199. package/ietf/aliases.js.map +1 -1
  200. package/ietf/cluster.d.ts +3 -3
  201. package/ietf/cluster.js +14 -62
  202. package/ietf/cluster.js.map +1 -1
  203. package/ietf/connection.d.ts +1 -31
  204. package/ietf/connection.d.ts.map +1 -1
  205. package/ietf/connection.js +305 -143
  206. package/ietf/connection.js.map +1 -1
  207. package/ietf/datagram.d.ts +60 -0
  208. package/ietf/datagram.d.ts.map +1 -0
  209. package/ietf/datagram.js +152 -0
  210. package/ietf/datagram.js.map +1 -0
  211. package/ietf/error.d.ts +3 -3
  212. package/ietf/error.js +82 -100
  213. package/ietf/error.js.map +1 -1
  214. package/ietf/fetch.d.ts +91 -35
  215. package/ietf/fetch.d.ts.map +1 -1
  216. package/ietf/fetch.js +172 -93
  217. package/ietf/fetch.js.map +1 -1
  218. package/ietf/filter.d.ts +20 -30
  219. package/ietf/filter.d.ts.map +1 -1
  220. package/ietf/filter.js +140 -171
  221. package/ietf/filter.js.map +1 -1
  222. package/ietf/goaway.d.ts +11 -9
  223. package/ietf/goaway.d.ts.map +1 -1
  224. package/ietf/goaway.js +22 -35
  225. package/ietf/goaway.js.map +1 -1
  226. package/ietf/hidden.js +1 -1
  227. package/ietf/hidden.js.map +1 -1
  228. package/ietf/index.d.ts +2 -14
  229. package/ietf/index.d.ts.map +1 -1
  230. package/ietf/index.js +4 -14
  231. package/ietf/index.js.map +1 -1
  232. package/ietf/join.d.ts +7 -0
  233. package/ietf/join.d.ts.map +1 -0
  234. package/ietf/join.js +234 -0
  235. package/ietf/join.js.map +1 -0
  236. package/ietf/message.d.ts +18 -2
  237. package/ietf/message.d.ts.map +1 -1
  238. package/ietf/message.js +60 -40
  239. package/ietf/message.js.map +1 -1
  240. package/ietf/message.test-util.d.ts +6 -0
  241. package/ietf/message.test-util.d.ts.map +1 -0
  242. package/ietf/message.test-util.js +13 -0
  243. package/ietf/message.test-util.js.map +1 -0
  244. package/ietf/namespace.d.ts +17 -3
  245. package/ietf/namespace.d.ts.map +1 -1
  246. package/ietf/namespace.js +78 -26
  247. package/ietf/namespace.js.map +1 -1
  248. package/ietf/object.d.ts +194 -32
  249. package/ietf/object.d.ts.map +1 -1
  250. package/ietf/object.js +405 -178
  251. package/ietf/object.js.map +1 -1
  252. package/ietf/parameters.d.ts +22 -42
  253. package/ietf/parameters.d.ts.map +1 -1
  254. package/ietf/parameters.js +260 -472
  255. package/ietf/parameters.js.map +1 -1
  256. package/ietf/priority.d.ts +17 -2
  257. package/ietf/priority.d.ts.map +1 -1
  258. package/ietf/priority.js +19 -4
  259. package/ietf/priority.js.map +1 -1
  260. package/ietf/properties.d.ts +5 -4
  261. package/ietf/properties.d.ts.map +1 -1
  262. package/ietf/properties.js +78 -48
  263. package/ietf/properties.js.map +1 -1
  264. package/ietf/publish.d.ts +22 -30
  265. package/ietf/publish.d.ts.map +1 -1
  266. package/ietf/publish.js +45 -139
  267. package/ietf/publish.js.map +1 -1
  268. package/ietf/publish_namespace.d.ts +46 -69
  269. package/ietf/publish_namespace.d.ts.map +1 -1
  270. package/ietf/publish_namespace.js +66 -184
  271. package/ietf/publish_namespace.js.map +1 -1
  272. package/ietf/publisher.js +1800 -600
  273. package/ietf/publisher.js.map +1 -1
  274. package/ietf/range.d.ts +2 -0
  275. package/ietf/range.d.ts.map +1 -0
  276. package/ietf/range.js +222 -0
  277. package/ietf/range.js.map +1 -0
  278. package/ietf/request.d.ts +3 -27
  279. package/ietf/request.d.ts.map +1 -1
  280. package/ietf/request.js +41 -78
  281. package/ietf/request.js.map +1 -1
  282. package/ietf/request_stream.d.ts +1 -6
  283. package/ietf/request_stream.d.ts.map +1 -1
  284. package/ietf/request_stream.js +306 -16
  285. package/ietf/request_stream.js.map +1 -1
  286. package/ietf/session.d.ts +41 -0
  287. package/ietf/session.d.ts.map +1 -0
  288. package/ietf/session.js +65 -0
  289. package/ietf/session.js.map +1 -0
  290. package/ietf/setup.d.ts +4 -29
  291. package/ietf/setup.d.ts.map +1 -1
  292. package/ietf/setup.js +6 -117
  293. package/ietf/setup.js.map +1 -1
  294. package/ietf/setup_options.d.ts +31 -0
  295. package/ietf/setup_options.d.ts.map +1 -0
  296. package/ietf/setup_options.js +138 -0
  297. package/ietf/setup_options.js.map +1 -0
  298. package/ietf/solicit.js +4 -14
  299. package/ietf/solicit.js.map +1 -1
  300. package/ietf/subscribe.d.ts +48 -41
  301. package/ietf/subscribe.d.ts.map +1 -1
  302. package/ietf/subscribe.js +118 -239
  303. package/ietf/subscribe.js.map +1 -1
  304. package/ietf/subscribe_namespace.d.ts +13 -76
  305. package/ietf/subscribe_namespace.d.ts.map +1 -1
  306. package/ietf/subscribe_namespace.js +30 -201
  307. package/ietf/subscribe_namespace.js.map +1 -1
  308. package/ietf/subscriber.js +1839 -536
  309. package/ietf/subscriber.js.map +1 -1
  310. package/ietf/token.js +105 -27
  311. package/ietf/token.js.map +1 -1
  312. package/ietf/track.d.ts +25 -22
  313. package/ietf/track.d.ts.map +1 -1
  314. package/ietf/track.js +48 -61
  315. package/ietf/track.js.map +1 -1
  316. package/ietf/version.d.ts +13 -42
  317. package/ietf/version.d.ts.map +1 -1
  318. package/ietf/version.js +22 -48
  319. package/ietf/version.js.map +1 -1
  320. package/implementation.d.ts +2 -0
  321. package/implementation.d.ts.map +1 -0
  322. package/implementation.js +5 -0
  323. package/implementation.js.map +1 -0
  324. package/index.d.ts +25 -10
  325. package/index.d.ts.map +1 -1
  326. package/index.js +19 -6
  327. package/index.js.map +1 -1
  328. package/internal.d.ts +1512 -64
  329. package/internal.d.ts.map +1 -1
  330. package/internal.js +557 -79
  331. package/internal.js.map +1 -1
  332. package/ledger.d.ts +23 -0
  333. package/ledger.d.ts.map +1 -0
  334. package/ledger.js +247 -0
  335. package/ledger.js.map +1 -0
  336. package/listing.d.ts +132 -0
  337. package/listing.d.ts.map +1 -0
  338. package/listing.js +486 -0
  339. package/listing.js.map +1 -0
  340. package/lite/announce.d.ts +124 -63
  341. package/lite/announce.d.ts.map +1 -1
  342. package/lite/announce.js +384 -158
  343. package/lite/announce.js.map +1 -1
  344. package/lite/announce_stream.d.ts +2 -0
  345. package/lite/announce_stream.d.ts.map +1 -0
  346. package/lite/announce_stream.js +92 -0
  347. package/lite/announce_stream.js.map +1 -0
  348. package/lite/answer.d.ts +22 -0
  349. package/lite/answer.d.ts.map +1 -0
  350. package/lite/answer.js +301 -0
  351. package/lite/answer.js.map +1 -0
  352. package/lite/auth.d.ts +45 -0
  353. package/lite/auth.d.ts.map +1 -0
  354. package/lite/auth.js +204 -0
  355. package/lite/auth.js.map +1 -0
  356. package/lite/compat.d.ts +31 -0
  357. package/lite/compat.d.ts.map +1 -0
  358. package/lite/compat.js +86 -0
  359. package/lite/compat.js.map +1 -0
  360. package/lite/connection.d.ts +1 -45
  361. package/lite/connection.d.ts.map +1 -1
  362. package/lite/connection.js +360 -77
  363. package/lite/connection.js.map +1 -1
  364. package/lite/datagram-end.d.ts +26 -0
  365. package/lite/datagram-end.d.ts.map +1 -0
  366. package/lite/datagram-end.js +30 -0
  367. package/lite/datagram-end.js.map +1 -0
  368. package/lite/datagram.d.ts +73 -8
  369. package/lite/datagram.d.ts.map +1 -1
  370. package/lite/datagram.js +144 -9
  371. package/lite/datagram.js.map +1 -1
  372. package/lite/duration.d.ts +6 -0
  373. package/lite/duration.d.ts.map +1 -0
  374. package/lite/duration.js +14 -0
  375. package/lite/duration.js.map +1 -0
  376. package/lite/error.d.ts +9 -0
  377. package/lite/error.d.ts.map +1 -0
  378. package/lite/error.js +24 -0
  379. package/lite/error.js.map +1 -0
  380. package/lite/fills.d.ts +162 -0
  381. package/lite/fills.d.ts.map +1 -0
  382. package/lite/fills.js +486 -0
  383. package/lite/fills.js.map +1 -0
  384. package/lite/fit.d.ts +49 -0
  385. package/lite/fit.d.ts.map +1 -0
  386. package/lite/fit.js +305 -0
  387. package/lite/fit.js.map +1 -0
  388. package/lite/goaway.d.ts +1 -1
  389. package/lite/goaway.d.ts.map +1 -1
  390. package/lite/goaway.js +5 -13
  391. package/lite/goaway.js.map +1 -1
  392. package/lite/group.d.ts +64 -13
  393. package/lite/group.d.ts.map +1 -1
  394. package/lite/group.js +108 -30
  395. package/lite/group.js.map +1 -1
  396. package/lite/identity.d.ts +25 -0
  397. package/lite/identity.d.ts.map +1 -0
  398. package/lite/identity.js +79 -0
  399. package/lite/identity.js.map +1 -0
  400. package/lite/index.d.ts +2 -1
  401. package/lite/index.d.ts.map +1 -1
  402. package/lite/index.js +2 -1
  403. package/lite/index.js.map +1 -1
  404. package/lite/message.d.ts +9 -1
  405. package/lite/message.d.ts.map +1 -1
  406. package/lite/message.js +28 -44
  407. package/lite/message.js.map +1 -1
  408. package/lite/priority.d.ts +9 -2
  409. package/lite/priority.d.ts.map +1 -1
  410. package/lite/priority.js +11 -105
  411. package/lite/priority.js.map +1 -1
  412. package/lite/probe.d.ts.map +1 -1
  413. package/lite/probe.js +9 -0
  414. package/lite/probe.js.map +1 -1
  415. package/lite/publisher.d.ts +10 -1
  416. package/lite/publisher.d.ts.map +1 -1
  417. package/lite/publisher.js +2548 -437
  418. package/lite/publisher.js.map +1 -1
  419. package/lite/routed.d.ts +59 -0
  420. package/lite/routed.d.ts.map +1 -0
  421. package/lite/routed.js +564 -0
  422. package/lite/routed.js.map +1 -0
  423. package/lite/session.d.ts +1 -27
  424. package/lite/session.d.ts.map +1 -1
  425. package/lite/session.js +0 -99
  426. package/lite/session.js.map +1 -1
  427. package/lite/setup.d.ts +29 -2
  428. package/lite/setup.d.ts.map +1 -1
  429. package/lite/setup.js +95 -21
  430. package/lite/setup.js.map +1 -1
  431. package/lite/stream.d.ts +5 -11
  432. package/lite/stream.d.ts.map +1 -1
  433. package/lite/stream.js +5 -1
  434. package/lite/stream.js.map +1 -1
  435. package/lite/subscribe.d.ts +475 -75
  436. package/lite/subscribe.d.ts.map +1 -1
  437. package/lite/subscribe.js +1373 -142
  438. package/lite/subscribe.js.map +1 -1
  439. package/lite/subscriber.d.ts +7 -44
  440. package/lite/subscriber.d.ts.map +1 -1
  441. package/lite/subscriber.js +3290 -515
  442. package/lite/subscriber.js.map +1 -1
  443. package/lite/track.d.ts +24 -9
  444. package/lite/track.d.ts.map +1 -1
  445. package/lite/track.js +116 -37
  446. package/lite/track.js.map +1 -1
  447. package/lite/tracks.d.ts +55 -0
  448. package/lite/tracks.d.ts.map +1 -0
  449. package/lite/tracks.js +114 -0
  450. package/lite/tracks.js.map +1 -0
  451. package/lite/version.d.ts +132 -24
  452. package/lite/version.d.ts.map +1 -1
  453. package/lite/version.js +378 -33
  454. package/lite/version.js.map +1 -1
  455. package/meta.d.ts +77 -0
  456. package/meta.d.ts.map +1 -0
  457. package/meta.js +130 -0
  458. package/meta.js.map +1 -0
  459. package/meta_api.d.ts +7 -0
  460. package/meta_api.d.ts.map +1 -0
  461. package/meta_api.js +7 -0
  462. package/meta_api.js.map +1 -0
  463. package/mint.d.ts +28 -0
  464. package/mint.d.ts.map +1 -0
  465. package/mint.js +66 -0
  466. package/mint.js.map +1 -0
  467. package/open.d.ts +63 -0
  468. package/open.d.ts.map +1 -0
  469. package/open.js +250 -0
  470. package/open.js.map +1 -0
  471. package/origin.d.ts +392 -140
  472. package/origin.d.ts.map +1 -1
  473. package/origin.js +2533 -686
  474. package/origin.js.map +1 -1
  475. package/package.json +8 -13
  476. package/path.d.ts +51 -51
  477. package/path.d.ts.map +1 -1
  478. package/path.js +210 -57
  479. package/path.js.map +1 -1
  480. package/payload.d.ts +33 -0
  481. package/payload.d.ts.map +1 -0
  482. package/payload.js +65 -0
  483. package/payload.js.map +1 -0
  484. package/prefix.d.ts +152 -0
  485. package/prefix.d.ts.map +1 -0
  486. package/prefix.js +349 -0
  487. package/prefix.js.map +1 -0
  488. package/priority.d.ts +8 -0
  489. package/priority.d.ts.map +1 -0
  490. package/priority.js +185 -0
  491. package/priority.js.map +1 -0
  492. package/queue.d.ts +2 -0
  493. package/queue.d.ts.map +1 -0
  494. package/queue.js +46 -0
  495. package/queue.js.map +1 -0
  496. package/ranges.d.ts +36 -0
  497. package/ranges.d.ts.map +1 -0
  498. package/ranges.js +312 -0
  499. package/ranges.js.map +1 -0
  500. package/rescale.d.ts +24 -0
  501. package/rescale.d.ts.map +1 -0
  502. package/rescale.js +39 -0
  503. package/rescale.js.map +1 -0
  504. package/resume.d.ts +55 -0
  505. package/resume.d.ts.map +1 -0
  506. package/resume.js +4173 -0
  507. package/resume.js.map +1 -0
  508. package/resume.test-util.d.ts +2 -0
  509. package/resume.test-util.d.ts.map +1 -0
  510. package/resume.test-util.js +25 -0
  511. package/resume.test-util.js.map +1 -0
  512. package/skips.d.ts +48 -0
  513. package/skips.d.ts.map +1 -0
  514. package/skips.js +182 -0
  515. package/skips.js.map +1 -0
  516. package/stream.d.ts +75 -37
  517. package/stream.d.ts.map +1 -1
  518. package/stream.js +236 -92
  519. package/stream.js.map +1 -1
  520. package/subscription.test-util.d.ts +2 -0
  521. package/subscription.test-util.d.ts.map +1 -0
  522. package/subscription.test-util.js +49 -0
  523. package/subscription.test-util.js.map +1 -0
  524. package/tail.d.ts +23 -14
  525. package/tail.d.ts.map +1 -1
  526. package/tail.js +224 -97
  527. package/tail.js.map +1 -1
  528. package/time.d.ts +41 -102
  529. package/time.d.ts.map +1 -1
  530. package/time.js +129 -91
  531. package/time.js.map +1 -1
  532. package/timed.d.ts +21 -0
  533. package/timed.d.ts.map +1 -0
  534. package/timed.js +2 -0
  535. package/timed.js.map +1 -0
  536. package/track.d.ts +577 -245
  537. package/track.d.ts.map +1 -1
  538. package/track.js +7306 -771
  539. package/track.js.map +1 -1
  540. package/track_api.d.ts +7 -0
  541. package/track_api.d.ts.map +1 -0
  542. package/track_api.js +7 -0
  543. package/track_api.js.map +1 -0
  544. package/util/abort.d.ts +1 -0
  545. package/util/abort.d.ts.map +1 -1
  546. package/util/abort.js +23 -1
  547. package/util/abort.js.map +1 -1
  548. package/util/announce.d.ts +4 -0
  549. package/util/announce.d.ts.map +1 -0
  550. package/util/announce.js +11 -0
  551. package/util/announce.js.map +1 -0
  552. package/util/bigint.d.ts +3 -0
  553. package/util/bigint.d.ts.map +1 -0
  554. package/util/bigint.js +8 -0
  555. package/util/bigint.js.map +1 -0
  556. package/util/change.d.ts +12 -0
  557. package/util/change.d.ts.map +1 -0
  558. package/util/change.js +18 -0
  559. package/util/change.js.map +1 -0
  560. package/util/deadline.d.ts +2 -0
  561. package/util/deadline.d.ts.map +1 -0
  562. package/util/deadline.js +108 -0
  563. package/util/deadline.js.map +1 -0
  564. package/util/decode_error.d.ts +3 -0
  565. package/util/decode_error.d.ts.map +1 -0
  566. package/util/decode_error.js +11 -0
  567. package/util/decode_error.js.map +1 -0
  568. package/util/demand.d.ts +8 -0
  569. package/util/demand.d.ts.map +1 -0
  570. package/util/demand.js +24 -0
  571. package/util/demand.js.map +1 -0
  572. package/util/given_up.d.ts +2 -0
  573. package/util/given_up.d.ts.map +1 -0
  574. package/util/given_up.js +101 -0
  575. package/util/given_up.js.map +1 -0
  576. package/util/hex.d.ts +6 -0
  577. package/util/hex.d.ts.map +1 -1
  578. package/util/hex.js +19 -4
  579. package/util/hex.js.map +1 -1
  580. package/util/key.d.ts +15 -0
  581. package/util/key.d.ts.map +1 -0
  582. package/util/key.js +38 -0
  583. package/util/key.js.map +1 -0
  584. package/util/limits.d.ts +2 -0
  585. package/util/limits.d.ts.map +1 -0
  586. package/util/limits.js +76 -0
  587. package/util/limits.js.map +1 -0
  588. package/util/observed.d.ts +23 -0
  589. package/util/observed.d.ts.map +1 -0
  590. package/util/observed.js +122 -0
  591. package/util/observed.js.map +1 -0
  592. package/util/sorted.d.ts +2 -0
  593. package/util/sorted.d.ts.map +1 -0
  594. package/util/sorted.js +149 -0
  595. package/util/sorted.js.map +1 -0
  596. package/util/timeout.d.ts +2 -1
  597. package/util/timeout.d.ts.map +1 -1
  598. package/util/timeout.js +13 -9
  599. package/util/timeout.js.map +1 -1
  600. package/util/timer.d.ts +2 -0
  601. package/util/timer.d.ts.map +1 -0
  602. package/util/timer.js +19 -0
  603. package/util/timer.js.map +1 -0
  604. package/util/utf8.d.ts +2 -0
  605. package/util/utf8.d.ts.map +1 -1
  606. package/util/utf8.js +7 -1
  607. package/util/utf8.js.map +1 -1
  608. package/util/varint.d.ts.map +1 -1
  609. package/util/varint.js +8 -1
  610. package/util/varint.js.map +1 -1
  611. package/util/watch.d.ts +8 -0
  612. package/util/watch.d.ts.map +1 -0
  613. package/util/watch.js +38 -0
  614. package/util/watch.js.map +1 -0
  615. package/util/yield.d.ts +2 -0
  616. package/util/yield.d.ts.map +1 -0
  617. package/util/yield.js +31 -0
  618. package/util/yield.js.map +1 -0
  619. package/varint.d.ts +0 -29
  620. package/varint.d.ts.map +1 -1
  621. package/varint.js +7 -28
  622. package/varint.js.map +1 -1
  623. package/varint_api.d.ts +7 -0
  624. package/varint_api.d.ts.map +1 -0
  625. package/varint_api.js +7 -0
  626. package/varint_api.js.map +1 -0
  627. package/version.d.ts +2 -0
  628. package/version.d.ts.map +1 -0
  629. package/version.js +4 -0
  630. package/version.js.map +1 -0
  631. package/window.d.ts +570 -0
  632. package/window.d.ts.map +1 -0
  633. package/window.js +1677 -0
  634. package/window.js.map +1 -0
  635. package/wire.d.ts +145 -30
  636. package/wire.d.ts.map +1 -1
  637. package/wire.js +27 -0
  638. package/wire.js.map +1 -1
  639. package/connection/established.d.ts +0 -53
  640. package/connection/established.d.ts.map +0 -1
  641. package/connection/established.js +0 -2
  642. package/connection/established.js.map +0 -1
  643. package/ietf/adapter.d.ts +0 -75
  644. package/ietf/adapter.d.ts.map +0 -1
  645. package/ietf/adapter.js +0 -699
  646. package/ietf/adapter.js.map +0 -1
  647. package/lite/datagram_stream.d.ts +0 -8
  648. package/lite/datagram_stream.d.ts.map +0 -1
  649. package/lite/datagram_stream.js +0 -43
  650. package/lite/datagram_stream.js.map +0 -1
  651. package/lite/fetch.d.ts +0 -28
  652. package/lite/fetch.d.ts.map +0 -1
  653. package/lite/fetch.js +0 -78
  654. package/lite/fetch.js.map +0 -1
  655. package/util/log.d.ts +0 -5
  656. package/util/log.d.ts.map +0 -1
  657. package/util/log.js +0 -16
  658. package/util/log.js.map +0 -1
  659. package/zod.d.ts +0 -13
  660. package/zod.d.ts.map +0 -1
  661. package/zod.js +0 -18
  662. package/zod.js.map +0 -1
package/lite/subscribe.js CHANGED
@@ -1,19 +1,28 @@
1
+ import { Duration as ClockDuration } from "@moq/clock";
2
+ import { ProtocolViolation } from "../error.js";
3
+ import { NO_MAX_DELAY } from "../internal.js";
1
4
  import * as Path from "../path.js";
5
+ import { UNBOUNDED } from "../util/key.js";
6
+ import { lengthLeadingOnes, parts, split } from "../util/varint.js";
7
+ import { withPolicy as atPolicy, EMPTY, isEmpty, liveMillis, rangeBudget, policyOf as rangePolicy, rangeTime, } from "../window.js";
8
+ import * as Duration from "./duration.js";
9
+ import { decodeNode, decodePin, encodeNode, encodePin } from "./identity.js";
2
10
  import * as Message from "./message.js";
3
- import { hasFrameBounds, hasGroupOrder, hasStreamCount, resolvesStart, Version } from "./version.js";
11
+ import { fromWire, toWire } from "./priority.js";
12
+ import { hasFrameBounds, hasGroupOrder, hasGroupSpan, hasMeta, hasOrder, hasRanges, hasRefresh, hasSkip, resolvesStart, Version, } from "./version.js";
4
13
  /**
5
14
  * Encode the `Group Start` field shared by SUBSCRIBE and SUBSCRIBE_UPDATE.
6
15
  *
7
16
  * Lite-06 writes the raw floor (`undefined` and 0 are both group 0). A pre-06 wire
8
- * encodes the sequence + 1: omitting the floor is 0 (the latest group, where the
17
+ * encodes the key + 1: omitting the floor is 0 (the latest group, where the
9
18
  * publisher starts) and an explicit 0 is 1, replay from the beginning.
10
19
  */
11
20
  async function encodeStartGroup(w, version, startGroup) {
12
21
  if (resolvesStart(version)) {
13
- await w.u53(startGroup ?? 0);
22
+ await w.u62(startGroup ?? 0n);
14
23
  return;
15
24
  }
16
- await w.u53(startGroup === undefined ? 0 : startGroup + 1);
25
+ await w.u62(startGroup === undefined ? 0n : startGroup + 1n);
17
26
  }
18
27
  /**
19
28
  * Decode the `Group Start` field shared by SUBSCRIBE and SUBSCRIBE_UPDATE.
@@ -22,10 +31,10 @@ async function encodeStartGroup(w, version, startGroup) {
22
31
  * {@link canonicalStartGroup} once the frame bounds are known.
23
32
  */
24
33
  async function decodeStartGroup(r, version) {
25
- const value = await r.u53();
34
+ const value = await r.u62();
26
35
  if (resolvesStart(version))
27
36
  return value;
28
- return value > 0 ? value - 1 : undefined;
37
+ return value > 0n ? value - 1n : undefined;
29
38
  }
30
39
  /**
31
40
  * Canonicalize a decoded floor: a lite-06 `Group Start` of 0 with no frame offset is the
@@ -34,7 +43,7 @@ async function decodeStartGroup(r, version) {
34
43
  * partway through group 0: a catalog never leaves it).
35
44
  */
36
45
  function canonicalStartGroup(version, startGroup, startFrame) {
37
- if (resolvesStart(version) && startGroup === 0 && startFrame === 0)
46
+ if (resolvesStart(version) && startGroup === 0n && startFrame === 0)
38
47
  return undefined;
39
48
  return startGroup;
40
49
  }
@@ -73,6 +82,876 @@ async function decodeFrameBounds(r, version, startGroup, endGroup) {
73
82
  }
74
83
  return { startFrame, endFrame: endFrame > 0 ? endFrame - 1 : undefined };
75
84
  }
85
+ /** The policy `range` carries. */
86
+ export function policyOf(range) {
87
+ return { priority: range.priority ?? 0, order: range.order ?? "descending" };
88
+ }
89
+ /** `range` under `policy`, which it omits where it is the default. */
90
+ export function withPolicy(range, policy) {
91
+ const { priority: _, order: __, ...rest } = range;
92
+ const out = rest;
93
+ if (policy.priority)
94
+ out.priority = policy.priority;
95
+ if (policy.order === "ascending")
96
+ out.order = "ascending";
97
+ return out;
98
+ }
99
+ /**
100
+ * What a subscription of `ranges` asks as a whole, for a wire or a sender without a policy per
101
+ * range: the highest priority, newest first if any range at that priority is. Mirrors the Rust
102
+ * `Policy::of`.
103
+ */
104
+ export function subscriptionPolicy(ranges) {
105
+ let top;
106
+ for (const range of ranges) {
107
+ const policy = policyOf(range);
108
+ if (top === undefined ||
109
+ policy.priority > top.priority ||
110
+ (policy.priority === top.priority && policy.order === "descending"))
111
+ top = policy;
112
+ }
113
+ return top ?? { priority: 0, order: "descending" };
114
+ }
115
+ /**
116
+ * The policy a group goes out at, `covers` being the Range IDs covering it, each at its current
117
+ * budget, and `aged` the widest budget it is known to have aged past: the highest priority
118
+ * among the ranges still wanting it, at that priority's order, descending breaking a tie. The
119
+ * subscription's whole when no range with a policy wants it. `lasts` is the budget whose aging may
120
+ * lower that policy, undefined once none can: a range ranked below it wanting the group longer.
121
+ * Mirrors the Rust `Steer::serving`.
122
+ */
123
+ export function groupPolicy(steer, covers, aged) {
124
+ const wanting = [];
125
+ for (const id of covers) {
126
+ const steering = steer.steering(id);
127
+ if (steering && (aged === undefined || steering.budget > aged))
128
+ wanting.push([steering.policy, steering.budget]);
129
+ }
130
+ let top;
131
+ for (const [policy] of wanting) {
132
+ if (top === undefined ||
133
+ policy.priority > top.priority ||
134
+ (policy.priority === top.priority && policy.order === "descending"))
135
+ top = policy;
136
+ }
137
+ if (!top)
138
+ return { policy: steer.whole };
139
+ const best = top;
140
+ const same = (policy) => policy.priority === best.priority && policy.order === best.order;
141
+ let lasts = 0;
142
+ for (const [policy, budget] of wanting)
143
+ if (same(policy))
144
+ lasts = Math.max(lasts, budget);
145
+ const outlasted = wanting.some(([policy, budget]) => !same(policy) && budget > lasts);
146
+ return outlasted ? { policy: best, lasts } : { policy: best };
147
+ }
148
+ /**
149
+ * The wire form of model `ranges`: the same times, a time being a group start, and live joins by lag
150
+ * and max, or, `placed` already at a group by index, as `joined` there (see {@link WireJoined}).
151
+ */
152
+ export function toWireRanges(ranges, placed) {
153
+ return ranges.map((range, index) => {
154
+ const out = withPolicy({}, rangePolicy(range));
155
+ const time = rangeTime(range);
156
+ const min = liveMillis(range);
157
+ const group = placed?.get(index);
158
+ if (time !== undefined)
159
+ out.start = group === undefined ? { time } : { absolute: group, frame: 0 };
160
+ else if (min !== undefined) {
161
+ const budget = rangeBudget(range);
162
+ const max = budget < NO_MAX_DELAY ? budget : null;
163
+ out.start = group === undefined ? { live: { min, max } } : { joined: { absolute: group, frame: 0, max } };
164
+ }
165
+ if (range.end !== undefined)
166
+ out.end = { time: range.end };
167
+ return out;
168
+ });
169
+ }
170
+ /**
171
+ * The lite-07 Anchor Type and values of a range's start or `end`: None (0), Time (1), Position (2),
172
+ * Live (3), or Joined (4). Both time ends are inclusive, so an absolute end, exclusive, goes out as
173
+ * the time just before it; one at group 0 holds nothing a time can say. An end or start partway
174
+ * through a group is a Position: its start, then the frame. A live join goes out as its lag then
175
+ * its max in milliseconds plus one, 0 for no limit, and only as a start; one already placed as its
176
+ * group's start, the frame, which may be 0, then its max as a live join carries it.
177
+ */
178
+ function wireAnchor(anchor, end) {
179
+ if (anchor?.joined !== undefined) {
180
+ if (end)
181
+ throw new Error("a range ends only at a group");
182
+ const { absolute, frame, max } = anchor.joined;
183
+ if (max !== null && (!Number.isSafeInteger(max) || max < 0))
184
+ throw new RangeError("duration exceeds safe milliseconds");
185
+ return [4, absolute, BigInt(frame), max === null ? 0n : BigInt(max) + 1n];
186
+ }
187
+ if (anchor?.live !== undefined) {
188
+ if (end)
189
+ throw new Error("a range ends only at a group");
190
+ const { min, max } = anchor.live;
191
+ for (const limit of [min, max]) {
192
+ if (limit !== null && (!Number.isSafeInteger(limit) || limit < 0))
193
+ throw new RangeError("duration exceeds safe milliseconds");
194
+ }
195
+ // Each plus one, so zero says no limit.
196
+ return [3, min === null ? 0n : BigInt(min) + 1n, max === null ? 0n : BigInt(max) + 1n];
197
+ }
198
+ if (anchor?.absolute !== undefined) {
199
+ return [2, anchor.absolute, BigInt(anchor.frame ?? 0)];
200
+ }
201
+ if (anchor?.time !== undefined)
202
+ return [1, anchor.time];
203
+ return [0];
204
+ }
205
+ /** Encode one lite-07 range: its policy, then its start, then its end; see {@link wireAnchor}. */
206
+ async function encodeRange(w, range) {
207
+ await encodePolicy(w, policyOf(range));
208
+ for (const [anchor, end] of [
209
+ [range.start, false],
210
+ [range.end, true],
211
+ ]) {
212
+ const [kind, ...values] = wireAnchor(anchor, end);
213
+ await w.u53(kind);
214
+ for (const value of values)
215
+ await w.u62(value);
216
+ }
217
+ }
218
+ /**
219
+ * Encode the span after the max delay. Before lite-07 that is `Group Start`, `Group End`,
220
+ * and the frame bounds; lite-07 carries a list of ranges, which is `ranges` when given and
221
+ * otherwise this span as its only one.
222
+ */
223
+ async function encodeSpan(w, version, span, ranges, policy) {
224
+ if (ranges !== undefined && hasRanges(version)) {
225
+ if (ranges.length === 0)
226
+ throw new Error(EMPTY_RANGE);
227
+ await w.u53(ranges.length);
228
+ for (const range of ranges)
229
+ await encodeRange(w, range);
230
+ return;
231
+ }
232
+ if (!hasRanges(version)) {
233
+ await encodeStartGroup(w, version, span.startGroup);
234
+ await w.u62(span.endGroup !== undefined ? span.endGroup + 1n : 0n);
235
+ await encodeFrameBounds(w, version, span);
236
+ return;
237
+ }
238
+ // A lite-07 range starts partway through a group as a Position, but ends only at a group's
239
+ // start, so it asks no slice of a group.
240
+ if (span.endFrame !== undefined || (span.startFrame !== 0 && span.startGroup === undefined))
241
+ throw new Error("frame bounds not supported for this version");
242
+ await w.u53(1);
243
+ const range = {};
244
+ if (span.startGroup !== undefined)
245
+ range.start = span.startFrame
246
+ ? { absolute: span.startGroup, frame: span.startFrame }
247
+ : { time: span.startGroup };
248
+ if (span.endGroup !== undefined)
249
+ range.end = { time: span.endGroup };
250
+ await encodeRange(w, withPolicy(range, policy ?? {}));
251
+ }
252
+ /**
253
+ * The model ranges a decoded SUBSCRIBE or SUBSCRIBE_UPDATE asks for: its lite-07 list, or the one
254
+ * span an older wire carries, whole groups either way. Mirrors the Rust `Bounds::new`.
255
+ *
256
+ * A group ID is a group's start, so an absolute anchor and a time anchor both name one: a time
257
+ * start resolves to the group containing it. An older wire means its start as the first group
258
+ * starting at or after it, which the publisher's floor keeps (see `cursorFloor`), and its span with
259
+ * no start takes `legacy` (see {@link legacyStart}).
260
+ */
261
+ export function requestedRanges(msg, legacy) {
262
+ if (msg.ranges)
263
+ return msg.ranges.map(requestedRange);
264
+ // A wire before lite-07 carries one priority and order, which every range takes.
265
+ const policy = { priority: msg.priority ?? 0, order: msg.order ?? "descending" };
266
+ const range = {};
267
+ const max = legacyMax(legacy);
268
+ // One max age bounds a named start too: a live join placed there (see {@link requestedPlaced}).
269
+ if (msg.startGroup !== undefined && max !== undefined)
270
+ range.start = { live: { min: ClockDuration.ZERO, max } };
271
+ else if (msg.startGroup !== undefined)
272
+ range.start = msg.startGroup;
273
+ else if (legacy !== undefined)
274
+ range.start = legacy;
275
+ if (msg.endGroup !== undefined)
276
+ range.end = msg.endGroup;
277
+ return [atPolicy(range, policy)];
278
+ }
279
+ /**
280
+ * The start a span with none takes on a `version` before lite-07, where one `Subscriber Max Age`
281
+ * (`maxDelay` milliseconds, `null` for none) bounds the whole subscription; undefined on lite-07,
282
+ * which carries one on each live start instead, and for the oldest group.
283
+ *
284
+ * An absent start takes every group still within that max age, or every group with none, as
285
+ * lite-01/02 never carry one. Lite-06 resolves it there. Before lite-06 an absent start is the
286
+ * latest group, which the publisher places as the floor once the source's feed says where it
287
+ * starts (see `cursorFloor`). A named start with a max age is a live join placed there; without
288
+ * one, a history floor bounded by the publisher's max age. Mirrors the Rust `Bounds::legacy`.
289
+ */
290
+ export function legacyStart(version, maxDelay) {
291
+ if (hasRanges(version) || maxDelay === null)
292
+ return undefined;
293
+ // A join with no lag limit takes every group still within its max. Mirrors the Rust `Start::within`.
294
+ return { live: { min: null, max: ClockDuration.millis(maxDelay) } };
295
+ }
296
+ /** The max age a pre-07 `legacy` start carries, undefined when it carries none. */
297
+ function legacyMax(legacy) {
298
+ if (legacy === undefined || typeof legacy === "bigint" || legacy.live.max === null)
299
+ return undefined;
300
+ return legacy.live.max ?? ClockDuration.ZERO;
301
+ }
302
+ /**
303
+ * The group each live join already placed among the ranges `msg` asks for starts at, by its index:
304
+ * a lite-07 {@link WireJoined}, or an older wire's named start under a max age (see
305
+ * {@link requestedRanges}). See `Subscription.placed`.
306
+ */
307
+ export function requestedPlaced(msg, legacy) {
308
+ const placed = new Map();
309
+ if (!msg.ranges) {
310
+ if (msg.startGroup !== undefined && legacyMax(legacy) !== undefined)
311
+ placed.set(0, msg.startGroup);
312
+ return placed;
313
+ }
314
+ for (const [index, range] of msg.ranges.entries()) {
315
+ const at = range.start?.joined?.absolute ?? range.start?.absolute;
316
+ if (at !== undefined)
317
+ placed.set(index, at);
318
+ }
319
+ return placed;
320
+ }
321
+ /** The model range one lite-07 range asks for; see {@link requestedRanges}. */
322
+ export function requestedRange(range) {
323
+ const policy = policyOf(range);
324
+ // Empty, as group bounds say it: keep its place, covering no group.
325
+ const start = range.start?.absolute;
326
+ const end = range.end && endGroup(range.end);
327
+ if (frameEmpty(range))
328
+ return atPolicy({ ...EMPTY }, policy);
329
+ if (end === 0n)
330
+ return atPolicy({ ...EMPTY }, policy);
331
+ const out = {};
332
+ const live = range.start?.live;
333
+ const joined = range.start?.joined;
334
+ const maxOf = (max) => (max === null || max >= NO_MAX_DELAY ? null : ClockDuration.millis(max));
335
+ if (live !== undefined)
336
+ out.start = { live: { min: live.min === null ? null : ClockDuration.millis(live.min), max: maxOf(live.max) } };
337
+ // Placed already, so its lag places nothing: it starts where it was placed (see
338
+ // {@link requestedPlaced}) and expires as a live join under its max.
339
+ else if (joined !== undefined)
340
+ out.start = { live: { min: ClockDuration.ZERO, max: maxOf(joined.max) } };
341
+ else if (range.start)
342
+ out.start = start ?? range.start.time;
343
+ // An absolute end is exclusive; every model end includes its group.
344
+ if (range.end)
345
+ out.end = end === undefined ? range.end.time : end - 1n;
346
+ return atPolicy(out, policy);
347
+ }
348
+ /**
349
+ * Decode one lite-07 range end, `undefined` for none: a time, inclusive as an end, a position
350
+ * partway through a group, never at frame 0, which a time says, a live join's lag then max in
351
+ * milliseconds, a start only, or one already placed, its group's start, a frame that may be 0, then
352
+ * its max, a start only too. Any other Anchor Type is a protocol violation.
353
+ */
354
+ async function decodeAnchor(r, end) {
355
+ const kind = await r.u53();
356
+ switch (kind) {
357
+ case 0:
358
+ return undefined;
359
+ case 1:
360
+ return { time: await r.u62() };
361
+ case 2: {
362
+ const absolute = await r.u62();
363
+ const frame = await r.u53();
364
+ return { absolute, frame };
365
+ }
366
+ case 3:
367
+ if (end)
368
+ throw new ProtocolViolation("a range ends at a live join");
369
+ return {
370
+ live: {
371
+ min: (await Duration.decodeOptional(r)) ?? null,
372
+ max: (await Duration.decodeOptional(r)) ?? null,
373
+ },
374
+ };
375
+ case 4: {
376
+ if (end)
377
+ throw new ProtocolViolation("a range ends at a live join");
378
+ const absolute = await r.u62();
379
+ const frame = await r.u53();
380
+ return { joined: { absolute, frame, max: (await Duration.decodeOptional(r)) ?? null } };
381
+ }
382
+ default:
383
+ throw new ProtocolViolation(`unknown range anchor: ${kind}`);
384
+ }
385
+ }
386
+ /** Decode one lite-07 range: its policy, then its start, then its end. */
387
+ async function decodeRange(r) {
388
+ const policy = await decodePolicy(r);
389
+ const start = await decodeAnchor(r, false);
390
+ const end = await decodeAnchor(r, true);
391
+ const range = {};
392
+ if (start)
393
+ range.start = start;
394
+ if (end)
395
+ range.end = end;
396
+ return withPolicy(range, policy);
397
+ }
398
+ /** Encode a lite-07 range's or retune's `Priority` then `Group Order`. */
399
+ async function encodePolicy(w, policy) {
400
+ await w.u8(toWire(policy.priority));
401
+ await encodeOrder(w, Version.DRAFT_07, policy.order);
402
+ }
403
+ /** Decode the inverse of {@link encodePolicy}. */
404
+ async function decodePolicy(r) {
405
+ const priority = fromWire(await r.u8());
406
+ const order = await decodeOrder(r, Version.DRAFT_07);
407
+ return { priority, order };
408
+ }
409
+ /** Decode a lite-07 `Range Count` and its ranges, which may be none. */
410
+ async function decodeRanges(r) {
411
+ // Not preallocated: each range costs at least two bytes of the bounded message.
412
+ const ranges = [];
413
+ for (let count = await r.u53(); count > 0; count--)
414
+ ranges.push(await decodeRange(r));
415
+ return ranges;
416
+ }
417
+ /**
418
+ * Lite-07 `ranges` and the one span covering every one of them, which the subscriber narrows
419
+ * again. A range covers whole groups but for a Position start (see {@link positionFrames}),
420
+ * whose frame the span keeps when the span starts there. The publisher reads every Position
421
+ * from the ranges themselves.
422
+ */
423
+ export function spanOf(version, ranges) {
424
+ let start;
425
+ let end;
426
+ let floor = true;
427
+ let live = false;
428
+ for (const range of ranges) {
429
+ const [from, to] = rangeBounds(range);
430
+ if (from === undefined)
431
+ floor = false;
432
+ else if (start === undefined || from < start)
433
+ start = from;
434
+ if (to === undefined)
435
+ live = true;
436
+ else if (end === undefined || end < to)
437
+ end = to;
438
+ }
439
+ start = floor ? start : undefined;
440
+ const startFrame = start === undefined ? 0 : (positionFrames(ranges).get(start) ?? 0);
441
+ return { ranges: [...ranges], ...spanBetween(version, start, live ? undefined : end, startFrame) };
442
+ }
443
+ /**
444
+ * The group a lite-07 range starts at and the exclusive group it ends before, either undefined
445
+ * when it has none: a time is the start of the group it names, inclusive as an end.
446
+ */
447
+ export function rangeBounds(range) {
448
+ const from = range.start?.absolute ?? range.start?.time ?? range.start?.joined?.absolute;
449
+ const end = range.end;
450
+ const to = end && (endGroup(end) ?? (end.time === undefined ? undefined : end.time + 1n));
451
+ return [from, to];
452
+ }
453
+ /**
454
+ * The exclusive group an absolute end stops before, `undefined` for a time: past its group when
455
+ * it stops partway through it.
456
+ */
457
+ function endGroup(end) {
458
+ if (end.absolute === undefined)
459
+ return undefined;
460
+ return end.frame ? end.absolute + 1n : end.absolute;
461
+ }
462
+ /**
463
+ * How one lite-07 range, as its subscriber asked it, counts toward a fetch: `undefined` when it
464
+ * covers no group, else whether it names its groups, from a Position up to an end. See
465
+ * {@link ExactRanges}.
466
+ */
467
+ function exactRange(range) {
468
+ if (isEmpty(requestedRange(range)))
469
+ return undefined;
470
+ return range.start?.absolute !== undefined && range.end !== undefined;
471
+ }
472
+ /**
473
+ * How a lite-07 subscription's live ranges, as its subscriber asked them, count toward a fetch,
474
+ * by Range ID, tallied as they come and go so an edit costs only the ranges it names. Mirrors
475
+ * the Rust `Prefs::fetches`.
476
+ */
477
+ export class ExactRanges {
478
+ #ranges = new Map();
479
+ // How many ranges read a group they don't name, and how many name a group to read.
480
+ #inexact = 0;
481
+ #named = 0;
482
+ /** The ranges a SUBSCRIBE asked, keyed by their positions, which are their Range IDs. */
483
+ constructor(ranges) {
484
+ for (const [id, range] of ranges.entries())
485
+ this.set(BigInt(id), range);
486
+ }
487
+ /** Hold `range` under `id`, replacing any range held there. */
488
+ set(id, range) {
489
+ this.delete(id);
490
+ const exact = exactRange(range);
491
+ this.#ranges.set(id, exact);
492
+ this.#count(exact, 1);
493
+ }
494
+ /** Drop the range held under `id`, if any. */
495
+ delete(id) {
496
+ if (!this.#ranges.has(id))
497
+ return;
498
+ this.#count(this.#ranges.get(id), -1);
499
+ this.#ranges.delete(id);
500
+ }
501
+ /**
502
+ * Whether the ranges read only named groups, a fetch: none of them other ranges, and at least
503
+ * one not empty. Mirrors the Rust `Subscription::exact_bounded`.
504
+ */
505
+ fetches() {
506
+ return this.#inexact === 0 && this.#named > 0;
507
+ }
508
+ #count(exact, by) {
509
+ if (exact === false)
510
+ this.#inexact += by;
511
+ if (exact === true)
512
+ this.#named += by;
513
+ }
514
+ }
515
+ /** Whether a range ends at or before its start, frames included: empty, covering no group. */
516
+ function frameEmpty(range) {
517
+ return range.end !== undefined && endsBefore(range.end, range.start);
518
+ }
519
+ /** Whether absolute `end`, exclusive, stops at or before `start`, frames included. */
520
+ function endsBefore(end, start) {
521
+ if (end.absolute === undefined)
522
+ return false;
523
+ const from = start?.absolute !== undefined ? start : start?.joined;
524
+ if (from === undefined)
525
+ return false;
526
+ if (end.absolute !== from.absolute)
527
+ return end.absolute < from.absolute;
528
+ return (end.frame ?? 0) <= (from.frame ?? 0);
529
+ }
530
+ /**
531
+ * The span from group `start`, at frame `startFrame` of it, up to the exclusive group `end`, either
532
+ * group undefined for none.
533
+ */
534
+ export function spanBetween(version, start, end, startFrame = 0) {
535
+ // An exclusive end at the very first group names nothing.
536
+ if (end === 0n)
537
+ throw new ProtocolViolation(EMPTY_RANGE);
538
+ return {
539
+ startGroup: canonicalStartGroup(version, start, startFrame),
540
+ startFrame,
541
+ endGroup: end === undefined ? undefined : end - 1n,
542
+ };
543
+ }
544
+ /**
545
+ * The first frame each lite-07 Position start asks its group from, by group, for each group no
546
+ * other range may cover: a group is delivered once, so one another range covers goes whole.
547
+ * A peer may send thousands of ranges, so this sorts their bounds once rather than scanning
548
+ * them per Position.
549
+ */
550
+ export function positionFrames(ranges) {
551
+ const positions = [];
552
+ for (const range of ranges) {
553
+ if (frameEmpty(range))
554
+ continue;
555
+ // A Position start, or a placed join's, partway through its group.
556
+ const start = range.start?.joined ?? range.start;
557
+ // One ending at or before the group it starts partway through covers nothing.
558
+ const [, to] = rangeBounds(range);
559
+ if (start?.absolute !== undefined && start.frame && (to === undefined || to > start.absolute))
560
+ positions.push([start.absolute, start.frame]);
561
+ }
562
+ return onlyCovered(positions, ranges);
563
+ }
564
+ /**
565
+ * The frame each lite-07 Position end stops its group before, by group, for each group no other
566
+ * range may cover, as {@link positionFrames} keeps starts. Mirrors the Rust `stops`.
567
+ */
568
+ export function positionStops(ranges) {
569
+ const positions = [];
570
+ for (const range of ranges) {
571
+ if (frameEmpty(range))
572
+ continue;
573
+ const end = range.end;
574
+ // One starting past the group it stops partway through covers nothing.
575
+ const [from, to] = rangeBounds(range);
576
+ if (end?.absolute !== undefined && end.frame && (from === undefined || to === undefined || from < to))
577
+ positions.push([end.absolute, end.frame]);
578
+ }
579
+ return onlyCovered(positions, ranges);
580
+ }
581
+ /** The `positions` whose group no range in `ranges` but their own may cover, by group. */
582
+ function onlyCovered(positions, ranges) {
583
+ const frames = new Map();
584
+ if (positions.length === 0)
585
+ return frames;
586
+ // A range may cover a group unless it starts past it or ends at or before it. One that
587
+ // starts at or past its end covers none, so with it left out, no range does both and the
588
+ // ranges covering a group are the rest less those two counts.
589
+ const starts = [];
590
+ const ends = [];
591
+ let count = 0;
592
+ for (const range of ranges) {
593
+ if (frameEmpty(range))
594
+ continue;
595
+ // No start, or no end, may cover it.
596
+ const [from, to] = rangeBounds(range);
597
+ if (from !== undefined && to !== undefined && from >= to)
598
+ continue;
599
+ count++;
600
+ if (from !== undefined)
601
+ starts.push(from);
602
+ if (to !== undefined)
603
+ ends.push(to);
604
+ }
605
+ const order = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
606
+ starts.sort(order);
607
+ ends.sort(order);
608
+ for (const [group, frame] of positions) {
609
+ const past = starts.length - countAtOrBelow(starts, group);
610
+ const before = countAtOrBelow(ends, group);
611
+ if (count - past - before === 1)
612
+ frames.set(group, frame);
613
+ }
614
+ return frames;
615
+ }
616
+ /** How many of the `sorted` values are at or below `value`. */
617
+ function countAtOrBelow(sorted, value) {
618
+ let low = 0;
619
+ let high = sorted.length;
620
+ while (low < high) {
621
+ const mid = (low + high) >> 1;
622
+ if (sorted[mid] <= value)
623
+ low = mid + 1;
624
+ else
625
+ high = mid;
626
+ }
627
+ return low;
628
+ }
629
+ /**
630
+ * The inverse of {@link encodeSpan}. A lite-07 list decodes as its ranges and as the one span
631
+ * covering every range in it, which the subscriber narrows again.
632
+ */
633
+ async function decodeSpan(r, version) {
634
+ if (!hasRanges(version)) {
635
+ const startGroup = await decodeStartGroup(r, version);
636
+ const endGroup = (await r.u62()) || undefined;
637
+ const end = endGroup !== undefined ? endGroup - 1n : undefined;
638
+ const frames = await decodeFrameBounds(r, version, startGroup, end);
639
+ return { startGroup: canonicalStartGroup(version, startGroup, frames.startFrame), endGroup: end, ...frames };
640
+ }
641
+ const ranges = await decodeRanges(r);
642
+ if (ranges.length === 0)
643
+ throw new ProtocolViolation("a subscription asks for no range");
644
+ return spanOf(version, ranges);
645
+ }
646
+ /**
647
+ * The ranges `msg` carries on lite-07: its list, or else the one span it encodes as its only
648
+ * range, in whole groups.
649
+ */
650
+ export function rangesOf(msg) {
651
+ if (msg.ranges)
652
+ return msg.ranges;
653
+ const range = {};
654
+ if (msg.startGroup !== undefined)
655
+ range.start = { absolute: msg.startGroup };
656
+ if (msg.endGroup !== undefined)
657
+ range.end = { absolute: msg.endGroup + 1n };
658
+ return [range];
659
+ }
660
+ /** The bytes `value` takes as a lite-07 varint, which counts leading ones. */
661
+ export function varintSize(value) {
662
+ const lo = split(value);
663
+ return lengthLeadingOnes(parts.hi, lo);
664
+ }
665
+ /** The bytes a lite-07 range takes on the wire, whose varints count leading ones. */
666
+ export function rangeSize(range) {
667
+ const anchor = (anchor, end) => {
668
+ let total = 0;
669
+ for (const value of wireAnchor(anchor, end))
670
+ total += varintSize(value);
671
+ return total;
672
+ };
673
+ // Its priority and order, a byte each.
674
+ return 2 + anchor(range.start, false) + anchor(range.end, true);
675
+ }
676
+ /** A range's anchors and policy as a key, so equal ranges compare equal. */
677
+ function rangeKey(range) {
678
+ const { priority, order } = policyOf(range);
679
+ return `${anchorsKey(range)}@${priority}${order[0]}`;
680
+ }
681
+ /** A range's anchors alone as a key. */
682
+ function anchorsKey(range) {
683
+ const anchor = (anchor) => {
684
+ if (anchor?.absolute !== undefined)
685
+ return `a${anchor.absolute}.${anchor.frame ?? 0}`;
686
+ if (anchor?.time !== undefined)
687
+ return `t${anchor.time}`;
688
+ if (anchor?.live !== undefined)
689
+ return `l${anchor.live.min}/${anchor.live.max ?? "-"}`;
690
+ if (anchor?.joined !== undefined)
691
+ return `j${anchor.joined.absolute}.${anchor.joined.frame}/${anchor.joined.max ?? "-"}`;
692
+ return "-";
693
+ };
694
+ return `${anchor(range.start)}:${anchor(range.end)}`;
695
+ }
696
+ /**
697
+ * A subscription's live ranges by Range ID, which both ends of a Subscribe Stream keep: the
698
+ * subscriber to write each update as an edit, the publisher to resolve one. Mirrors the Rust
699
+ * `LiveRanges`.
700
+ *
701
+ * The SUBSCRIBE's ranges take Range IDs from 0 in order, and each range an update adds takes the
702
+ * next. Before lite-07 an update replaces the list, numbered afresh.
703
+ */
704
+ export class LiveRanges {
705
+ #version;
706
+ #next = 0n;
707
+ // In Range ID order, since each id is the next.
708
+ #live = new Map();
709
+ // The bytes the live ranges take on the wire, held to what one SUBSCRIBE carries.
710
+ #size = 0;
711
+ /** The ranges a SUBSCRIBE carried. */
712
+ constructor(version, ranges) {
713
+ this.#version = version;
714
+ this.#add(ranges);
715
+ }
716
+ #add(ranges) {
717
+ for (const range of ranges) {
718
+ // Only lite-07 numbers ranges on the wire, and only it is held to one SUBSCRIBE.
719
+ if (hasRanges(this.#version))
720
+ this.#size += rangeSize(range);
721
+ this.#live.set(this.#next++, range);
722
+ }
723
+ }
724
+ #remove(id) {
725
+ const range = this.#live.get(id);
726
+ if (!range)
727
+ return undefined;
728
+ if (hasRanges(this.#version))
729
+ this.#size -= rangeSize(range);
730
+ this.#live.delete(id);
731
+ return range;
732
+ }
733
+ // Replace the live range with Range ID `id` by `range` in place, keeping Range ID order.
734
+ #replace(id, range) {
735
+ const old = this.#live.get(id);
736
+ if (old && hasRanges(this.#version))
737
+ this.#size += rangeSize(range) - rangeSize(old);
738
+ this.#live.set(id, range);
739
+ }
740
+ /** The Range ID the next range added takes. */
741
+ next() {
742
+ return this.#next;
743
+ }
744
+ /** The live range with Range ID `id`. */
745
+ get(id) {
746
+ return this.#live.get(id);
747
+ }
748
+ /** Whether `id` was ever assigned. */
749
+ assigned(id) {
750
+ return id < this.#next;
751
+ }
752
+ /** Every live Range ID, in order. */
753
+ ids() {
754
+ return [...this.#live.keys()];
755
+ }
756
+ /** Every live range, in Range ID order. */
757
+ ranges() {
758
+ return [...this.#live.values()];
759
+ }
760
+ /** Every live range with its Range ID, in order. */
761
+ entries() {
762
+ return [...this.#live];
763
+ }
764
+ /**
765
+ * Apply a received lite-07 `update`'s edit, returning it by Range ID: the ranges retired, then
766
+ * each added with the Range ID it took, then each live join retuned to its new max in place,
767
+ * costing the ranges it names. Throws {@link ProtocolViolation} for an edit it cannot apply.
768
+ * Before lite-07 an update restates its ranges, so nothing is numbered and the edit is empty.
769
+ * Mirrors the Rust `LiveRanges::apply`.
770
+ */
771
+ apply(update) {
772
+ if (!hasRanges(this.#version))
773
+ return { retired: [], added: [], retuned: [] };
774
+ const retired = update.remove ?? [];
775
+ for (const id of retired) {
776
+ // An id never assigned, or already retired (including earlier in this list).
777
+ if (!this.#remove(id))
778
+ throw new ProtocolViolation(`unknown range id: ${id}`);
779
+ }
780
+ // Retuned before the adds, so a retune names a range live before this edit.
781
+ const retuned = [];
782
+ for (const [id, retune] of update.retune ?? []) {
783
+ // Each range once: one retuned twice in an edit says nothing a single one doesn't.
784
+ if (retuned.some(([tuned]) => tuned === id))
785
+ throw new ProtocolViolation(`range id retuned twice: ${id}`);
786
+ const range = this.#live.get(id);
787
+ if (range === undefined)
788
+ throw new ProtocolViolation(`no live range to retune: ${id}`);
789
+ const live = range.start?.live;
790
+ const joined = range.start?.joined;
791
+ // Only a live join has a max age to change.
792
+ if (live === undefined && joined === undefined && retune.max !== null)
793
+ throw new ProtocolViolation(`no live join to retune: ${id}`);
794
+ // One already placed stays where it was placed.
795
+ let tuned = range;
796
+ if (joined)
797
+ tuned = { ...range, start: { joined: { ...joined, max: retune.max } } };
798
+ else if (live)
799
+ tuned = { ...range, start: { live: { min: live.min, max: retune.max } } };
800
+ tuned = withPolicy(tuned, retune);
801
+ this.#replace(id, tuned);
802
+ retuned.push([id, tuned]);
803
+ }
804
+ const first = this.#next;
805
+ const add = update.ranges ?? [];
806
+ this.#add(add);
807
+ // Asking for nothing is refused like a SUBSCRIBE that does, and a set no SUBSCRIBE
808
+ // could carry like one too large to send.
809
+ if (this.#live.size === 0)
810
+ throw new ProtocolViolation("a subscription asks for no range");
811
+ if (this.#size > Message.MAX_MESSAGE_SIZE)
812
+ throw new ProtocolViolation("too many ranges");
813
+ return {
814
+ retired: [...retired],
815
+ added: add.map((range, index) => [first + BigInt(index), range]),
816
+ retuned,
817
+ };
818
+ }
819
+ /**
820
+ * The edit that turns the live ranges into `ranges`, which become the live ones: the Range IDs
821
+ * to retire and the ranges to add. A range already live keeps its Range ID, so an update that
822
+ * changes one range sends only that one, and the publisher keeps where the rest resolved.
823
+ * Before lite-07 replaced ranges take fresh Range IDs.
824
+ *
825
+ * `ranges` are the demand's, then `kinds.probes` probes, then `kinds.fills` asking for fills,
826
+ * and `kinds.kindOf` names what each live one asks for: a probe or a fill asking for what a
827
+ * demand range does still takes a range of its own, so each Range ID stays one kind's.
828
+ * `fillIds` and `probeIds` are the Range IDs the added ones of each took. The live ranges in
829
+ * `renew` are retired and added again even when wanted, so the publisher resolves them afresh.
830
+ * A range wanted again under another policy, or a live join under another max, as the same
831
+ * kind, is retuned in place, keeping its Range ID and so where it was placed. Mirrors the Rust
832
+ * `SubStream::edit`.
833
+ */
834
+ edit(ranges, kinds = {}, renew = new Set()) {
835
+ if (ranges.length === 0)
836
+ throw new Error(EMPTY_RANGE);
837
+ if (!hasRanges(this.#version)) {
838
+ const same = ranges.length === this.#live.size &&
839
+ this.ranges().every((range, index) => rangeKey(range) === rangeKey(ranges[index]));
840
+ if (!same) {
841
+ this.#live.clear();
842
+ this.#add(ranges);
843
+ }
844
+ return { remove: [], add: [...ranges], retune: [], fillIds: [], probeIds: [] };
845
+ }
846
+ const probes = kinds.probes ?? 0;
847
+ const demand = ranges.length - (kinds.fills ?? 0) - probes;
848
+ const kindOf = kinds.kindOf ?? (() => "demand");
849
+ const kindAt = (index) => index < demand ? "demand" : index < demand + probes ? "probe" : "fill";
850
+ const key = (range, kind) => `${kind[0]}${rangeKey(range)}`;
851
+ // Each live range keeps at most one wanted copy, so duplicates edit like any other.
852
+ const wanted = new Map();
853
+ ranges.forEach((range, index) => {
854
+ const k = key(range, kindAt(index));
855
+ wanted.set(k, (wanted.get(k) ?? 0) + 1);
856
+ });
857
+ const remove = [];
858
+ // The bytes the live ranges take once the edit applies.
859
+ let size = this.#size;
860
+ for (const [id, range] of this.#live) {
861
+ const k = key(range, kindOf(id));
862
+ const count = wanted.get(k) ?? 0;
863
+ if (count > 0 && !renew.has(id)) {
864
+ wanted.set(k, count - 1);
865
+ }
866
+ else {
867
+ remove.push(id);
868
+ size -= rangeSize(range);
869
+ }
870
+ }
871
+ // In the order asked for, each with its kind, so the publisher numbers them the way this
872
+ // side does.
873
+ const adding = [];
874
+ ranges.forEach((range, index) => {
875
+ const kind = kindAt(index);
876
+ const k = key(range, kind);
877
+ const count = wanted.get(k) ?? 0;
878
+ if (count === 0)
879
+ return;
880
+ wanted.set(k, count - 1);
881
+ adding.push([range, kind]);
882
+ });
883
+ // A range wanted again under another policy, or a live join under another max, its placement
884
+ // kept: one being renewed is placed afresh. Added ranges are indexed by kind, start, and end,
885
+ // where a live join's start is its lag, one placed its placement, and any other as is, so
886
+ // each retire finds its match without scanning them all.
887
+ const startKey = (anchor) => anchor?.live !== undefined
888
+ ? `l${anchor.live.min}`
889
+ : anchor?.joined !== undefined
890
+ ? `j${anchor.joined.absolute}.${anchor.joined.frame}`
891
+ : `o${anchorsKey({ start: anchor })}`;
892
+ const retuneKey = (range, kind) => `${kind[0]}${startKey(range.start)}${anchorsKey({ end: range.end })}`;
893
+ const byKey = new Map();
894
+ adding.forEach(([range, kind], index) => {
895
+ const k = retuneKey(range, kind);
896
+ const queue = byKey.get(k);
897
+ if (queue)
898
+ queue.push(index);
899
+ else
900
+ byKey.set(k, [index]);
901
+ });
902
+ const taken = new Set();
903
+ const retuned = [];
904
+ const kept = remove.filter((id) => {
905
+ if (renew.has(id))
906
+ return true;
907
+ const range = this.#live.get(id);
908
+ if (!range)
909
+ return true;
910
+ const queue = byKey.get(retuneKey(range, kindOf(id)));
911
+ const index = queue?.shift();
912
+ if (index === undefined)
913
+ return true;
914
+ retuned.push([id, adding[index][0]]);
915
+ taken.add(index);
916
+ return false;
917
+ });
918
+ if (taken.size > 0) {
919
+ const rest = adding.filter((_, index) => !taken.has(index));
920
+ adding.length = 0;
921
+ adding.push(...rest);
922
+ }
923
+ const add = [];
924
+ const fillIds = [];
925
+ const probeIds = [];
926
+ const first = this.#next;
927
+ for (const [range, kind] of adding) {
928
+ add.push(range);
929
+ const id = first + BigInt(add.length - 1);
930
+ if (kind === "fill")
931
+ fillIds.push(id);
932
+ else if (kind === "probe")
933
+ probeIds.push(id);
934
+ }
935
+ // Checked before anything changes, so a refused edit leaves the ranges as last sent.
936
+ for (const range of add)
937
+ size += rangeSize(range);
938
+ // A retuned join was counted out among the removed, so it counts back in as it now reads.
939
+ for (const [, range] of retuned)
940
+ size += rangeSize(range);
941
+ if (size > Message.MAX_MESSAGE_SIZE)
942
+ throw new Error("too many ranges for one SUBSCRIBE");
943
+ for (const id of kept)
944
+ this.#remove(id);
945
+ for (const [id, range] of retuned)
946
+ this.#replace(id, range);
947
+ this.#add(add);
948
+ const retune = retuned.map(([id, range]) => [
949
+ id,
950
+ { ...policyOf(range), max: range.start?.live?.max ?? range.start?.joined?.max ?? null },
951
+ ]);
952
+ return { remove: kept, add, retune, fillIds, probeIds };
953
+ }
954
+ }
76
955
  /** Step over the retired `Ordered` byte on a version whose layout still has it. */
77
956
  async function skipGroupOrder(r, version) {
78
957
  if (hasGroupOrder(version))
@@ -84,71 +963,179 @@ async function padGroupOrder(w, version) {
84
963
  await w.bool(false);
85
964
  }
86
965
  /**
87
- * Exclusive model/local cap from a decoded inclusive last group.
88
- *
89
- * The wire's `Group End` is inclusive once decoded; the track model and `setGroups` are
90
- * exclusive. `undefined` stays unbounded.
966
+ * Decode the byte after the subscriber priority shared by SUBSCRIBE and SUBSCRIBE_UPDATE:
967
+ * lite-07's `Group Order`, or the retired `Ordered` byte an older layout still carries.
968
+ */
969
+ async function decodeOrder(r, version) {
970
+ if (!hasOrder(version)) {
971
+ await skipGroupOrder(r, version);
972
+ return "descending";
973
+ }
974
+ const order = await r.u8();
975
+ switch (order) {
976
+ case 0:
977
+ return "descending";
978
+ case 1:
979
+ return "ascending";
980
+ default:
981
+ throw new ProtocolViolation(`unknown group order: ${order}`);
982
+ }
983
+ }
984
+ /**
985
+ * The inverse of {@link decodeOrder}. A wire without `Group Order` serves newest first, so an
986
+ * ascending subscription over it falls back to descending.
987
+ */
988
+ async function encodeOrder(w, version, order) {
989
+ if (!hasOrder(version)) {
990
+ await padGroupOrder(w, version);
991
+ return;
992
+ }
993
+ await w.u8(order === "ascending" ? 1 : 0);
994
+ }
995
+ /**
996
+ * Decode SUBSCRIBE's trailing `Meta` byte, lite-07's only: 1 for headers alone, 0 for the payload,
997
+ * anything else malformed. An earlier wire always asks for the payload.
91
998
  */
92
- export function exclusiveGroupEnd(inclusive) {
93
- return inclusive === undefined ? undefined : inclusive + 1;
999
+ async function decodeMeta(r, version) {
1000
+ if (!hasMeta(version))
1001
+ return false;
1002
+ const meta = await r.u8();
1003
+ switch (meta) {
1004
+ case 0:
1005
+ return false;
1006
+ case 1:
1007
+ return true;
1008
+ default:
1009
+ throw new ProtocolViolation(`unknown meta: ${meta}`);
1010
+ }
1011
+ }
1012
+ /** The inverse of {@link decodeMeta}, throwing for a metadata subscription on a wire without one. */
1013
+ async function encodeMeta(w, version, meta) {
1014
+ if (hasMeta(version)) {
1015
+ await w.u8(meta ? 1 : 0);
1016
+ }
1017
+ else if (meta) {
1018
+ throw new Error("a metadata subscription needs moq-lite-07");
1019
+ }
94
1020
  }
95
- /** The error for a requested range the wire cannot carry; see {@link emptyRange}. */
1021
+ /** The error for a requested range the wire cannot carry: one asking for nothing. */
96
1022
  export const EMPTY_RANGE = "empty subscription range cannot be encoded";
97
1023
  /**
98
1024
  * Inclusive last group a Subscribe message carries, from an exclusive model end.
99
1025
  *
100
1026
  * Empty (`0`) cannot be encoded: the wire's 0 means unbounded. Callers refuse an empty
101
- * requested range with {@link emptyRange} before reaching here.
1027
+ * requested range before reaching here.
102
1028
  */
103
1029
  export function inclusiveGroupEnd(exclusive) {
104
1030
  if (exclusive === undefined)
105
1031
  return undefined;
106
- if (exclusive === 0)
1032
+ if (exclusive === 0n)
107
1033
  throw new Error(EMPTY_RANGE);
108
- return exclusive - 1;
1034
+ return exclusive - 1n;
109
1035
  }
1036
+ /** What lite-03 to lite-06 send for no `Subscriber Max Age` limit, which their bare milliseconds can't say. */
1037
+ const MAX_SAFE_DELAY_MS = Number.MAX_SAFE_INTEGER;
110
1038
  /**
111
- * Whether a requested range asks for nothing.
112
- *
113
- * The wire has no encoding for one: its bounds are inclusive, so flooring the end would
114
- * either hand back the group the caller excluded (0 means unbounded) or invert the
115
- * range once the two bounds meet. An absent start is the live edge, so it only empties
116
- * the range when the end is 0.
1039
+ * Encode the `Subscriber Max Age` before lite-07, bare milliseconds with `null` (no limit) as
1040
+ * {@link MAX_SAFE_DELAY_MS}. Lite-07 carries none: each live start has its own.
117
1041
  */
118
- export function emptyRange({ startGroup, endGroup }) {
119
- return endGroup !== undefined && (startGroup ?? 0) >= endGroup;
1042
+ async function encodeMaxDelay(w, version, maxDelay) {
1043
+ if (hasRanges(version))
1044
+ return;
1045
+ await w.u53(maxDelay ?? MAX_SAFE_DELAY_MS);
1046
+ }
1047
+ /** Decode the inverse of {@link encodeMaxDelay}: {@link MAX_SAFE_DELAY_MS} or more says no limit. */
1048
+ async function decodeMaxDelay(r, version) {
1049
+ if (hasRanges(version))
1050
+ return null;
1051
+ // Read wide: a peer may send any varint at or past the sentinel, which still means no limit.
1052
+ const millis = await r.u62();
1053
+ return millis < BigInt(MAX_SAFE_DELAY_MS) ? Number(millis) : null;
120
1054
  }
121
1055
  export class SubscribeUpdate {
1056
+ /** A refresh request for this group or later, judged against the local track's refresh. */
1057
+ wantedGroup;
122
1058
  priority;
123
- /** Subscriber max latency in milliseconds; zero skips once a newer group is available. */
124
- maxAge;
1059
+ /** See {@link Subscribe.order}. */
1060
+ order;
1061
+ /** See {@link Subscribe.maxDelay}. */
1062
+ maxDelay;
125
1063
  startGroup;
126
1064
  endGroup;
127
1065
  /** See {@link Subscribe.startFrame}. */
128
1066
  startFrame;
129
1067
  /** See {@link Subscribe.endFrame}. */
130
1068
  endFrame;
1069
+ /**
1070
+ * See {@link Subscribe.ranges}. On lite-07 the wire carries the ranges to add, after retiring
1071
+ * {@link remove}; {@link LiveRanges.apply} resolves a received update to every live range.
1072
+ */
1073
+ ranges;
1074
+ /** Lite-07: the Range IDs of the live ranges to retire before adding {@link ranges}. */
1075
+ remove;
1076
+ /**
1077
+ * Lite-07: each live range to give a new policy, and a live join a new max, in place, keeping
1078
+ * where it was placed, by Range ID.
1079
+ */
1080
+ retune;
131
1081
  constructor(props) {
1082
+ if (props.wantedGroup !== undefined && props.wantedGroup < 0n)
1083
+ throw new RangeError("wanted group must be non-negative");
1084
+ this.wantedGroup = props.wantedGroup;
132
1085
  this.priority = props.priority;
133
- this.maxAge = props.maxAge ?? 0;
1086
+ this.order = props.order ?? "descending";
1087
+ this.maxDelay = props.maxDelay === undefined ? 0 : props.maxDelay;
134
1088
  this.startGroup = props.startGroup;
135
1089
  this.endGroup = props.endGroup;
136
1090
  this.startFrame = props.startFrame ?? 0;
137
1091
  this.endFrame = props.endFrame;
1092
+ this.ranges = props.ranges;
1093
+ this.remove = props.remove;
1094
+ this.retune = props.retune;
138
1095
  }
139
1096
  async #encode(w, version) {
140
1097
  switch (version) {
141
1098
  case Version.DRAFT_01:
142
1099
  case Version.DRAFT_02:
143
- await w.u8(this.priority);
1100
+ await w.u8(toWire(this.priority));
144
1101
  break;
145
1102
  default:
146
- await w.u8(this.priority);
147
- await padGroupOrder(w, version);
148
- await w.u53(this.maxAge);
149
- await encodeStartGroup(w, version, this.startGroup);
150
- await w.u53(this.endGroup !== undefined ? this.endGroup + 1 : 0);
151
- await encodeFrameBounds(w, version, this);
1103
+ // Lite-07 carries a priority and order per range instead.
1104
+ if (!hasRanges(version)) {
1105
+ await w.u8(toWire(this.priority));
1106
+ await encodeOrder(w, version, this.order);
1107
+ }
1108
+ await encodeMaxDelay(w, version, this.maxDelay);
1109
+ if (hasRanges(version)) {
1110
+ // An edit of the live ranges, which may add none: `ranges`, or else the span as
1111
+ // the one range added once `remove` retires the rest.
1112
+ if (this.ranges === undefined && this.remove === undefined)
1113
+ throw new Error("a lite-07 update edits its ranges");
1114
+ const remove = this.remove ?? [];
1115
+ await w.u53(remove.length);
1116
+ for (const id of remove)
1117
+ await w.u62(id);
1118
+ if (this.ranges === undefined) {
1119
+ await encodeSpan(w, version, this, undefined, this);
1120
+ }
1121
+ else {
1122
+ await w.u53(this.ranges.length);
1123
+ for (const range of this.ranges)
1124
+ await encodeRange(w, range);
1125
+ }
1126
+ const retune = this.retune ?? [];
1127
+ await w.u53(retune.length);
1128
+ for (const [id, tuned] of retune) {
1129
+ await w.u62(id);
1130
+ await encodePolicy(w, tuned);
1131
+ await Duration.encodeOptional(w, tuned.max ?? undefined);
1132
+ }
1133
+ }
1134
+ else {
1135
+ await encodeSpan(w, version, this, this.ranges);
1136
+ }
1137
+ if (hasRefresh(version))
1138
+ await w.u62(this.wantedGroup === undefined ? 0n : this.wantedGroup + 1n);
152
1139
  break;
153
1140
  }
154
1141
  }
@@ -156,26 +1143,45 @@ export class SubscribeUpdate {
156
1143
  switch (version) {
157
1144
  case Version.DRAFT_01:
158
1145
  case Version.DRAFT_02:
159
- return new SubscribeUpdate({ priority: await r.u8() });
1146
+ return new SubscribeUpdate({ priority: fromWire(await r.u8()), maxDelay: null });
160
1147
  default: {
161
- const priority = await r.u8();
162
- await skipGroupOrder(r, version);
163
- const maxAge = await r.u53();
164
- const startGroup = await decodeStartGroup(r, version);
165
- const endGroup = (await r.u53()) || undefined;
166
- const end = endGroup !== undefined ? endGroup - 1 : undefined;
167
- const frames = await decodeFrameBounds(r, version, startGroup, end);
1148
+ // Lite-07 carries a priority and order per range, which the publisher reads off the live
1149
+ // ranges once it applies the edit.
1150
+ const priority = hasRanges(version) ? 0 : fromWire(await r.u8());
1151
+ const order = hasRanges(version) ? "descending" : await decodeOrder(r, version);
1152
+ const maxDelay = await decodeMaxDelay(r, version);
1153
+ let span;
1154
+ if (hasRanges(version)) {
1155
+ // Not preallocated, like the ranges: each id costs a byte of the bounded message.
1156
+ const remove = [];
1157
+ for (let count = await r.u53(); count > 0; count--)
1158
+ remove.push(await r.u62());
1159
+ const ranges = await decodeRanges(r);
1160
+ const retune = [];
1161
+ for (let count = await r.u53(); count > 0; count--) {
1162
+ const id = await r.u62();
1163
+ const policy = await decodePolicy(r);
1164
+ retune.push([id, { ...policy, max: (await Duration.decodeOptional(r)) ?? null }]);
1165
+ }
1166
+ span = { startFrame: 0, remove, ranges, retune };
1167
+ }
1168
+ else {
1169
+ span = await decodeSpan(r, version);
1170
+ }
1171
+ const wanted = hasRefresh(version) ? await r.u62() : 0n;
168
1172
  return new SubscribeUpdate({
1173
+ wantedGroup: wanted === 0n ? undefined : wanted - 1n,
169
1174
  priority,
170
- maxAge,
171
- startGroup: canonicalStartGroup(version, startGroup, frames.startFrame),
172
- endGroup: end,
173
- ...frames,
1175
+ order,
1176
+ maxDelay,
1177
+ ...span,
174
1178
  });
175
1179
  }
176
1180
  }
177
1181
  }
178
1182
  async encode(w, version) {
1183
+ if (this.wantedGroup !== undefined && this.wantedGroup < 0n)
1184
+ throw new RangeError("wanted group must be non-negative");
179
1185
  return Message.encode(w, (w) => this.#encode(w, version));
180
1186
  }
181
1187
  static async decode(r, version) {
@@ -188,10 +1194,22 @@ export class SubscribeUpdate {
188
1194
  export class Subscribe {
189
1195
  id;
190
1196
  broadcast;
1197
+ /** The publisher identity the subscriber expects. Lite-07+. */
1198
+ identity;
191
1199
  track;
192
1200
  priority;
193
- /** Subscriber max latency in milliseconds; zero skips once a newer group is available. */
194
- maxAge;
1201
+ /**
1202
+ * Which pending group to send first. Lite-07+; earlier wires decode as descending and
1203
+ * cannot ask for anything else.
1204
+ */
1205
+ order;
1206
+ /**
1207
+ * The `Subscriber Max Age` before lite-07, in milliseconds, `null` for no limit beyond the
1208
+ * publisher's; zero skips once a newer group is available. Lite-01/02 carry none and decode as
1209
+ * `null`. Lite-07 carries a max on each live start instead, so this is ignored there and decodes
1210
+ * as `null`.
1211
+ */
1212
+ maxDelay;
195
1213
  startGroup;
196
1214
  endGroup;
197
1215
  /**
@@ -205,60 +1223,88 @@ export class Subscribe {
205
1223
  * whole group. Lite-06+, and meaningless without an explicit `endGroup`.
206
1224
  */
207
1225
  endFrame;
1226
+ /**
1227
+ * Every range. Lite-07 encodes these in place of the span when given, and older versions
1228
+ * the span alone; decoding a lite-07 list fills them alongside the span covering them all.
1229
+ */
1230
+ ranges;
1231
+ /**
1232
+ * Whether each frame is delivered as its header alone, without its payload, for the
1233
+ * subscription's whole life. Lite-07+: an earlier wire cannot carry it, so encoding one there
1234
+ * throws rather than asking for the payload.
1235
+ */
1236
+ meta;
208
1237
  constructor(props) {
209
1238
  this.id = props.id;
210
1239
  this.broadcast = props.broadcast;
1240
+ this.identity = props.identity;
211
1241
  this.track = props.track;
212
1242
  this.priority = props.priority;
213
- this.maxAge = props.maxAge ?? 0;
1243
+ this.order = props.order ?? "descending";
1244
+ this.maxDelay = props.maxDelay === undefined ? 0 : props.maxDelay;
214
1245
  this.startGroup = props.startGroup;
215
1246
  this.endGroup = props.endGroup;
216
1247
  this.startFrame = props.startFrame ?? 0;
217
1248
  this.endFrame = props.endFrame;
1249
+ this.ranges = props.ranges;
1250
+ this.meta = props.meta ?? false;
218
1251
  }
219
1252
  async #encode(w, version) {
220
1253
  await w.u62(this.id);
221
1254
  await w.string(Path.encode(this.broadcast));
1255
+ await encodePin(w, version, this.identity);
1256
+ await encodeNode(w, version);
222
1257
  await w.string(this.track);
223
- await w.u8(this.priority);
224
1258
  switch (version) {
225
1259
  case Version.DRAFT_01:
226
1260
  case Version.DRAFT_02:
1261
+ await w.u8(toWire(this.priority));
227
1262
  break;
228
1263
  default:
229
- await padGroupOrder(w, version);
230
- await w.u53(this.maxAge);
231
- await encodeStartGroup(w, version, this.startGroup);
232
- await w.u53(this.endGroup !== undefined ? this.endGroup + 1 : 0);
233
- await encodeFrameBounds(w, version, this);
1264
+ // Lite-07 carries a priority and order per range instead.
1265
+ if (!hasRanges(version)) {
1266
+ await w.u8(toWire(this.priority));
1267
+ await encodeOrder(w, version, this.order);
1268
+ }
1269
+ await encodeMaxDelay(w, version, this.maxDelay);
1270
+ await encodeSpan(w, version, this, this.ranges, this);
234
1271
  break;
235
1272
  }
1273
+ await encodeMeta(w, version, this.meta);
236
1274
  }
237
1275
  static async #decode(r, version) {
238
1276
  const id = await r.u62();
239
1277
  const broadcast = Path.decode(await r.string());
1278
+ const identity = await decodePin(r, version);
1279
+ await decodeNode(r, version);
240
1280
  const track = await r.string();
241
- const priority = await r.u8();
242
1281
  switch (version) {
243
1282
  case Version.DRAFT_01:
244
1283
  case Version.DRAFT_02:
245
- return new Subscribe({ id, broadcast, track, priority });
1284
+ return new Subscribe({ id, broadcast, track, priority: fromWire(await r.u8()), maxDelay: null });
246
1285
  default: {
247
- await skipGroupOrder(r, version);
248
- const maxAge = await r.u53();
249
- const startGroup = await decodeStartGroup(r, version);
250
- const endGroup = (await r.u53()) || undefined;
251
- const end = endGroup !== undefined ? endGroup - 1 : undefined;
252
- const frames = await decodeFrameBounds(r, version, startGroup, end);
1286
+ let priority = 0;
1287
+ let order = "descending";
1288
+ if (!hasRanges(version)) {
1289
+ priority = fromWire(await r.u8());
1290
+ order = await decodeOrder(r, version);
1291
+ }
1292
+ const maxDelay = await decodeMaxDelay(r, version);
1293
+ const span = await decodeSpan(r, version);
1294
+ // Each range carries its own priority and order, which the subscription runs at as a whole.
1295
+ if (span.ranges)
1296
+ ({ priority, order } = subscriptionPolicy(span.ranges));
1297
+ const meta = await decodeMeta(r, version);
253
1298
  return new Subscribe({
254
1299
  id,
255
1300
  broadcast,
1301
+ identity,
256
1302
  track,
257
1303
  priority,
258
- maxAge,
259
- startGroup: canonicalStartGroup(version, startGroup, frames.startFrame),
260
- endGroup: end,
261
- ...frames,
1304
+ order,
1305
+ maxDelay,
1306
+ ...span,
1307
+ meta,
262
1308
  });
263
1309
  }
264
1310
  }
@@ -278,13 +1324,13 @@ export class Subscribe {
278
1324
  */
279
1325
  export class SubscribeOk {
280
1326
  priority;
281
- /** Accepted subscriber max latency in milliseconds. */
282
- maxAge;
1327
+ /** Accepted subscriber max delay in milliseconds. */
1328
+ maxDelay;
283
1329
  startGroup;
284
1330
  endGroup;
285
- constructor({ priority = 0, maxAge = 0, startGroup = undefined, endGroup = undefined, }) {
1331
+ constructor({ priority = 0, maxDelay = 0, startGroup = undefined, endGroup = undefined, }) {
286
1332
  this.priority = priority;
287
- this.maxAge = maxAge;
1333
+ this.maxDelay = maxDelay;
288
1334
  this.startGroup = startGroup;
289
1335
  this.endGroup = endGroup;
290
1336
  }
@@ -294,22 +1340,22 @@ export class SubscribeOk {
294
1340
  // noop
295
1341
  break;
296
1342
  case Version.DRAFT_01:
297
- await w.u8(this.priority ?? 0);
1343
+ await w.u8(toWire(this.priority));
298
1344
  break;
299
1345
  // Draft-05+ never sends SUBSCRIBE_OK, but keep the field layout matching
300
1346
  // Draft-03/04 so a stray future use stays well-formed.
301
1347
  default:
302
- await w.u8(this.priority);
1348
+ await w.u8(toWire(this.priority));
303
1349
  await padGroupOrder(w, version);
304
- await w.u53(this.maxAge);
305
- await w.u53(this.startGroup !== undefined ? this.startGroup + 1 : 0);
306
- await w.u53(this.endGroup !== undefined ? this.endGroup + 1 : 0);
1350
+ await w.u53(this.maxDelay);
1351
+ await w.u62(this.startGroup !== undefined ? this.startGroup + 1n : 0n);
1352
+ await w.u62(this.endGroup !== undefined ? this.endGroup + 1n : 0n);
307
1353
  break;
308
1354
  }
309
1355
  }
310
1356
  static async #decode(version, r) {
311
1357
  let priority;
312
- let maxAge;
1358
+ let maxDelay;
313
1359
  let startGroup;
314
1360
  let endGroup;
315
1361
  switch (version) {
@@ -317,21 +1363,21 @@ export class SubscribeOk {
317
1363
  // noop
318
1364
  break;
319
1365
  case Version.DRAFT_01:
320
- priority = await r.u8();
1366
+ priority = fromWire(await r.u8());
321
1367
  break;
322
1368
  default:
323
- priority = await r.u8();
1369
+ priority = fromWire(await r.u8());
324
1370
  await skipGroupOrder(r, version);
325
- maxAge = await r.u53();
326
- startGroup = await r.u53();
327
- endGroup = await r.u53();
1371
+ maxDelay = await r.u53();
1372
+ startGroup = await r.u62();
1373
+ endGroup = await r.u62();
328
1374
  break;
329
1375
  }
330
1376
  return new SubscribeOk({
331
1377
  priority,
332
- maxAge,
333
- startGroup: startGroup !== undefined && startGroup > 0 ? startGroup - 1 : undefined,
334
- endGroup: endGroup !== undefined && endGroup > 0 ? endGroup - 1 : undefined,
1378
+ maxDelay,
1379
+ startGroup: startGroup !== undefined && startGroup > 0n ? startGroup - 1n : undefined,
1380
+ endGroup: endGroup !== undefined && endGroup > 0n ? endGroup - 1n : undefined,
335
1381
  });
336
1382
  }
337
1383
  async encode(w, version) {
@@ -342,81 +1388,231 @@ export class SubscribeOk {
342
1388
  }
343
1389
  }
344
1390
  /**
345
- * Resolves the absolute start group of a Draft-05+ subscription. The first message
346
- * the publisher sends, once the start group is known. A value greater than the
347
- * requested start implicitly drops the leading range.
1391
+ * Says where one range of a Draft-05+ subscription starts, once the publisher resolves it. Before
1392
+ * lite-07 one answers the subscription's one range; lite-07 answers each range by Range ID, from
1393
+ * the SUBSCRIBE or an update, along with what the publisher knows of the largest position. A start
1394
+ * above the requested one implicitly drops the groups below it.
348
1395
  *
349
- * There is no start frame: a partial group is only served to a subscriber that asked
350
- * for one, so delivery begins either at the requested `startFrame` (when this is the
351
- * requested group) or at frame 0 (when the publisher resolved to a later one).
1396
+ * There is no start frame: a partial group is only served to a subscriber that asked for
1397
+ * one, so a range begins at its requested frame when it starts at its requested group,
1398
+ * and at frame 0 of a later one otherwise.
352
1399
  */
353
1400
  export class SubscribeStart {
354
- group;
355
- constructor(group) {
356
- this.group = group;
1401
+ /** The range's Range ID; 0 before lite-07, whose one range has none on the wire. */
1402
+ id;
1403
+ /** The group its delivery starts at, `undefined` for a range with no group there. */
1404
+ start;
1405
+ /** What the publisher knows of the largest position; always `"unknown"` before lite-07. */
1406
+ largest;
1407
+ constructor(props) {
1408
+ this.id = props.id;
1409
+ this.start = props.start;
1410
+ this.largest = props.largest;
357
1411
  }
358
- async encode(w) {
1412
+ async encode(w, version) {
359
1413
  return Message.encode(w, async (w) => {
360
- await w.u53(this.group);
1414
+ if (!hasRanges(version)) {
1415
+ if (this.id !== 0n || this.start === undefined || this.largest !== "unknown")
1416
+ throw new Error("one start group before draft-07");
1417
+ return w.u62(this.start);
1418
+ }
1419
+ await w.u62(this.id);
1420
+ // Group + 1, so 0 is a range with no group there.
1421
+ await w.u62(this.start === undefined ? 0n : this.start + 1n);
1422
+ if (this.largest === "unknown")
1423
+ return w.u8(0);
1424
+ if (this.largest === "empty")
1425
+ return w.u8(1);
1426
+ await w.u8(2);
1427
+ await w.u62(this.largest.position.group);
1428
+ await w.u53(this.largest.position.frame);
1429
+ await w.u8(this.largest.ended ? 1 : 0);
361
1430
  });
362
1431
  }
363
- static async decode(r) {
364
- return Message.decode(r, async (r) => new SubscribeStart(await r.u53()));
1432
+ static async decode(r, version) {
1433
+ return Message.decode(r, async (r) => {
1434
+ if (!hasRanges(version))
1435
+ return new SubscribeStart({ id: 0n, start: await r.u62(), largest: "unknown" });
1436
+ const id = await r.u62();
1437
+ const wire = await r.u62();
1438
+ const start = wire === 0n ? undefined : wire - 1n;
1439
+ const type = await r.u8();
1440
+ switch (type) {
1441
+ case 0:
1442
+ return new SubscribeStart({ id, start, largest: "unknown" });
1443
+ case 1:
1444
+ return new SubscribeStart({ id, start, largest: "empty" });
1445
+ case 2: {
1446
+ const position = { group: await r.u62(), frame: await r.u53() };
1447
+ const ended = await r.u8();
1448
+ if (ended > 1)
1449
+ throw new ProtocolViolation(`invalid Largest Ended: ${ended}`);
1450
+ return new SubscribeStart({ id, start, largest: { position, ended: ended === 1 } });
1451
+ }
1452
+ default:
1453
+ throw new ProtocolViolation(`invalid Largest Type: ${type}`);
1454
+ }
1455
+ });
365
1456
  }
366
1457
  }
367
1458
  /**
368
- * Signals that no group at or after `group` (exclusive upper bound) will be produced
369
- * on a Draft-05+ subscription. `0` means the track ended before producing any groups.
1459
+ * SUBSCRIBE_END: no group at or after `group` (exclusive upper bound) will be produced on a
1460
+ * Draft-05+ subscription. `0` means the track ended before producing any groups.
370
1461
  */
371
1462
  export class SubscribeEnd {
372
- /** The exclusive final group sequence: the first sequence that will never be produced. */
1463
+ /** The exclusive final group key: the first key that will never be produced. */
373
1464
  group;
374
- /**
375
- * The number of group streams the publisher opened for this subscription.
376
- * Draft-07+ only; not on the wire before, where it decodes as 0.
377
- */
378
- streams;
379
- constructor(group, streams = 0) {
1465
+ constructor(group) {
380
1466
  this.group = group;
381
- this.streams = streams;
1467
+ }
1468
+ async encode(w, _version) {
1469
+ return Message.encode(w, async (w) => {
1470
+ await w.u62(this.group);
1471
+ });
1472
+ }
1473
+ static async decode(r, _version) {
1474
+ return Message.decode(r, async (r) => new SubscribeEnd(await r.u62()));
1475
+ }
1476
+ }
1477
+ /**
1478
+ * A SUBSCRIBE_DROP range: a non-empty `[Group Start, Group End)` on lite-07, an
1479
+ * inclusive `Group Start` and `Group End` before. Either way it ends by the reserved key
1480
+ * 2^64-1, so it never names that group.
1481
+ */
1482
+ async function decodeGroups(r, version) {
1483
+ const start = await r.u62();
1484
+ const span = hasGroupSpan(version);
1485
+ const end = span ? await r.u62() : (await r.u62()) + 1n;
1486
+ if (end < start || (span && end === start) || end >= UNBOUNDED)
1487
+ throw new ProtocolViolation(`invalid group range: [${start}, ${end})`);
1488
+ return { start, end };
1489
+ }
1490
+ async function encodeGroups(w, start, end, version) {
1491
+ const span = hasGroupSpan(version);
1492
+ // An empty range at 0 has no inclusive end to spell before lite-07, and lite-07 has no empty one.
1493
+ if (end < start || end >= UNBOUNDED || (span ? end === start : end === 0n))
1494
+ throw new RangeError(`invalid group range: [${start}, ${end})`);
1495
+ await w.u62(start);
1496
+ await w.u62(span ? end : end - 1n);
1497
+ }
1498
+ /**
1499
+ * Names groups this subscription will not deliver, and nothing more: not found here, expired
1500
+ * under the publisher's own max age, or reset where the GROUP header may be lost. On lite-07 it
1501
+ * speaks for this subscription alone, so a reader still owed a group asks for it afresh; before
1502
+ * lite-07 it carries an application code (read and dropped) and reads as stale.
1503
+ *
1504
+ * Draft-03+; publishers send it on Draft-05 and later.
1505
+ */
1506
+ export class SubscribeDrop {
1507
+ /** The first dropped group key. */
1508
+ start;
1509
+ /** One past the last dropped group key. */
1510
+ end;
1511
+ constructor(props) {
1512
+ this.start = props.start;
1513
+ this.end = props.end;
382
1514
  }
383
1515
  async encode(w, version) {
384
1516
  return Message.encode(w, async (w) => {
385
- await w.u53(this.group);
386
- if (hasStreamCount(version))
387
- await w.u53(this.streams);
1517
+ await encodeGroups(w, this.start, this.end, version);
1518
+ if (!hasSkip(version))
1519
+ await w.u53(0);
388
1520
  });
389
1521
  }
390
1522
  static async decode(r, version) {
391
- return Message.decode(r, async (r) => new SubscribeEnd(await r.u53(), hasStreamCount(version) ? await r.u53() : 0));
1523
+ return Message.decode(r, async (r) => {
1524
+ const { start, end } = await decodeGroups(r, version);
1525
+ if (!hasSkip(version))
1526
+ await r.u62();
1527
+ return new SubscribeDrop({ start, end });
1528
+ });
392
1529
  }
393
1530
  }
394
- /// Indicates that one or more groups have been dropped.
395
- ///
396
- /// Draft-03 to Draft-06 only: Draft-07 counts group streams in SUBSCRIBE_END instead.
397
- export class SubscribeDrop {
1531
+ /**
1532
+ * Names group starts the publisher declared never produced: permanent, and it supersedes a copy
1533
+ * that did not finish the group. Lite-07+.
1534
+ */
1535
+ export class SubscribeSkip {
1536
+ /** The first skipped group key. */
398
1537
  start;
1538
+ /** One past the last skipped group key. */
399
1539
  end;
400
- error;
401
1540
  constructor(props) {
402
1541
  this.start = props.start;
403
1542
  this.end = props.end;
404
- this.error = props.error;
405
1543
  }
406
- async #encode(w) {
407
- await w.u53(this.start);
408
- await w.u53(this.end);
409
- await w.u53(this.error);
1544
+ async encode(w, version) {
1545
+ if (!hasSkip(version))
1546
+ throw new Error("SUBSCRIBE_SKIP needs lite-07");
1547
+ return Message.encode(w, (w) => encodeGroups(w, this.start, this.end, version));
1548
+ }
1549
+ static async decode(r, version) {
1550
+ return Message.decode(r, async (r) => new SubscribeSkip(await decodeGroups(r, version)));
1551
+ }
1552
+ }
1553
+ /**
1554
+ * Names groups judged stale under a finite `Max Age`, so only a reader no wider settles on them;
1555
+ * a wider one asks for them on a request of its own. Lite-07+.
1556
+ */
1557
+ export class SubscribeOld {
1558
+ /** The first stale group key. */
1559
+ start;
1560
+ /** One past the last stale group key. */
1561
+ end;
1562
+ /** The `Max Age` in milliseconds the groups were judged stale under. */
1563
+ maxAge;
1564
+ constructor(props) {
1565
+ this.start = props.start;
1566
+ this.end = props.end;
1567
+ this.maxAge = props.maxAge;
410
1568
  }
411
- static async #decode(r) {
412
- return new SubscribeDrop({ start: await r.u53(), end: await r.u53(), error: await r.u53() });
1569
+ async encode(w, version) {
1570
+ if (!hasSkip(version))
1571
+ throw new Error("SUBSCRIBE_OLD needs lite-07");
1572
+ // Rounded down, so a reader settles on it only when no wider than the verdict's budget.
1573
+ const maxAge = Math.floor(this.maxAge);
1574
+ if (!Number.isSafeInteger(maxAge) || maxAge < 0)
1575
+ throw new RangeError("max age exceeds safe milliseconds");
1576
+ return Message.encode(w, async (w) => {
1577
+ await encodeGroups(w, this.start, this.end, version);
1578
+ await w.u62(BigInt(maxAge));
1579
+ });
413
1580
  }
414
- async encode(w) {
415
- return Message.encode(w, this.#encode.bind(this));
1581
+ static async decode(r, version) {
1582
+ return Message.decode(r, async (r) => {
1583
+ const { start, end } = await decodeGroups(r, version);
1584
+ const maxAge = await r.u62();
1585
+ if (maxAge > BigInt(Number.MAX_SAFE_INTEGER))
1586
+ throw new RangeError("max age exceeds safe milliseconds");
1587
+ return new SubscribeOld({ start, end, maxAge: Number(maxAge) });
1588
+ });
416
1589
  }
417
- static async decode(r) {
418
- return Message.decode(r, SubscribeDrop.#decode);
1590
+ }
1591
+ /**
1592
+ * The response giving up `[start, end)` as `hole`, as `version` carries it. Before lite-07 every
1593
+ * one is a drop. A skip is a skip; a stale verdict names the finite budget it was judged under,
1594
+ * and an `old` with no verdict of its own names `unjudged`, the narrowest finite live-join max
1595
+ * age; with neither, or for anything else, it is a drop. Mirrors the Rust
1596
+ * `SubscribeResponse::given_up`.
1597
+ */
1598
+ export function givenUp(start, end, hole, unjudged, version) {
1599
+ if (!hasSkip(version))
1600
+ return { drop: new SubscribeDrop({ start, end }) };
1601
+ let maxAge;
1602
+ switch (hole.reason) {
1603
+ case "skip":
1604
+ return { skip: new SubscribeSkip({ start, end }) };
1605
+ case "drop":
1606
+ maxAge = undefined;
1607
+ break;
1608
+ case "old":
1609
+ maxAge = hole.maxAge ?? unjudged;
1610
+ break;
419
1611
  }
1612
+ // No finite budget, including one at the no-limit bound, names no Max Age: a drop.
1613
+ if (maxAge === undefined || maxAge >= NO_MAX_DELAY)
1614
+ return { drop: new SubscribeDrop({ start, end }) };
1615
+ return { old: new SubscribeOld({ start, end, maxAge }) };
420
1616
  }
421
1617
  export async function encodeSubscribeResponse(w, resp, version) {
422
1618
  switch (version) {
@@ -437,25 +1633,33 @@ export async function encodeSubscribeResponse(w, resp, version) {
437
1633
  }
438
1634
  else if ("drop" in resp) {
439
1635
  await w.u53(0x1);
440
- await resp.drop.encode(w);
1636
+ await resp.drop.encode(w, version);
441
1637
  }
442
1638
  else {
443
- throw new Error("SUBSCRIBE_START/END not supported for this version");
1639
+ throw new Error("SUBSCRIBE_START/FINISH not supported for this version");
444
1640
  }
445
1641
  break;
446
1642
  default:
447
1643
  // Draft-05+: SUBSCRIBE_OK is gone; START/END/DROP carry the resolved range.
448
1644
  if ("start" in resp) {
449
- await w.u53(0x0);
450
- await resp.start.encode(w);
1645
+ await w.u53(hasRanges(version) ? 0x4 : 0x0);
1646
+ await resp.start.encode(w, version);
451
1647
  }
452
1648
  else if ("end" in resp) {
453
1649
  await w.u53(0x1);
454
1650
  await resp.end.encode(w, version);
455
1651
  }
456
- else if ("drop" in resp && !hasStreamCount(version)) {
1652
+ else if ("drop" in resp) {
457
1653
  await w.u53(0x2);
458
- await resp.drop.encode(w);
1654
+ await resp.drop.encode(w, version);
1655
+ }
1656
+ else if ("skip" in resp && hasSkip(version)) {
1657
+ await w.u53(0x3);
1658
+ await resp.skip.encode(w, version);
1659
+ }
1660
+ else if ("old" in resp && hasSkip(version)) {
1661
+ await w.u53(0x5);
1662
+ await resp.old.encode(w, version);
459
1663
  }
460
1664
  else {
461
1665
  throw new Error("subscribe response not supported for this version");
@@ -475,28 +1679,55 @@ export async function decodeSubscribeResponse(r, version) {
475
1679
  case 0x0:
476
1680
  return { ok: await SubscribeOk.decode(r, version) };
477
1681
  case 0x1:
478
- return { drop: await SubscribeDrop.decode(r) };
1682
+ return { drop: await SubscribeDrop.decode(r, version) };
479
1683
  default:
480
- throw new Error(`unknown subscribe response type: ${typ}`);
1684
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
481
1685
  }
482
1686
  }
483
1687
  default: {
484
1688
  const typ = await r.u53();
485
1689
  switch (typ) {
486
1690
  case 0x0:
487
- return { start: await SubscribeStart.decode(r) };
1691
+ if (hasRanges(version))
1692
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
1693
+ return { start: await SubscribeStart.decode(r, version) };
488
1694
  case 0x1:
489
1695
  return { end: await SubscribeEnd.decode(r, version) };
490
1696
  case 0x2:
491
- if (hasStreamCount(version))
492
- throw new Error(`unknown subscribe response type: ${typ}`);
493
- return { drop: await SubscribeDrop.decode(r) };
1697
+ return { drop: await SubscribeDrop.decode(r, version) };
1698
+ case 0x3:
1699
+ if (!hasSkip(version))
1700
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
1701
+ return { skip: await SubscribeSkip.decode(r, version) };
1702
+ case 0x4:
1703
+ if (!hasRanges(version))
1704
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
1705
+ return { start: await SubscribeStart.decode(r, version) };
1706
+ case 0x5:
1707
+ if (!hasSkip(version))
1708
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
1709
+ return { old: await SubscribeOld.decode(r, version) };
494
1710
  default:
495
- throw new Error(`unknown subscribe response type: ${typ}`);
1711
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
496
1712
  }
497
1713
  }
498
1714
  }
499
1715
  }
1716
+ /**
1717
+ * Whether the next response `r` holds whole, without reading the stream, is a lite-07
1718
+ * SUBSCRIBE_START, so a subscriber applying a burst of them can take the next before applying.
1719
+ */
1720
+ export function startBuffered(r, version) {
1721
+ if (!hasRanges(version))
1722
+ return false;
1723
+ const start = r.tryPeek((c) => {
1724
+ if (c.u53() !== 0x4)
1725
+ return false;
1726
+ c.read(c.u53());
1727
+ return true;
1728
+ });
1729
+ return start === true;
1730
+ }
500
1731
  /** Like {@link decodeSubscribeResponse} but resolves `undefined` on a clean FIN. */
501
1732
  export async function decodeSubscribeResponseMaybe(r, version) {
502
1733
  if (await r.done())