@moq/net 0.4.7 → 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 (664) 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 -2
  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 -32
  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 -139
  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 -2
  33. package/bandwidth_api.js.map +1 -1
  34. package/broadcast.d.ts +64 -38
  35. package/broadcast.d.ts.map +1 -1
  36. package/broadcast.js +556 -173
  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 +21 -13
  47. package/connection/accept.d.ts.map +1 -1
  48. package/connection/accept.js +97 -130
  49. package/connection/accept.js.map +1 -1
  50. package/connection/browser.d.ts.map +1 -1
  51. package/connection/browser.js +28 -20
  52. package/connection/browser.js.map +1 -1
  53. package/connection/connect.d.ts +120 -24
  54. package/connection/connect.d.ts.map +1 -1
  55. package/connection/connect.js +535 -217
  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 -81
  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 -95
  72. package/connection/goaway.js.map +1 -1
  73. package/connection/handshake.d.ts +34 -8
  74. package/connection/handshake.d.ts.map +1 -1
  75. package/connection/handshake.js +97 -21
  76. package/connection/handshake.js.map +1 -1
  77. package/connection/index.d.ts +4 -7
  78. package/connection/index.d.ts.map +1 -1
  79. package/connection/index.js +1 -3
  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 +142 -63
  86. package/connection/pool.d.ts.map +1 -1
  87. package/connection/pool.js +281 -118
  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 -234
  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 -9
  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 -5
  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 -47
  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 -2
  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 -38
  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 -3
  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 -308
  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 -65
  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 -36
  199. package/ietf/aliases.js.map +1 -1
  200. package/ietf/cluster.d.ts +3 -3
  201. package/ietf/cluster.js +14 -55
  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 -144
  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 -101
  213. package/ietf/error.js.map +1 -1
  214. package/ietf/fetch.d.ts +93 -37
  215. package/ietf/fetch.d.ts.map +1 -1
  216. package/ietf/fetch.js +172 -94
  217. package/ietf/fetch.js.map +1 -1
  218. package/ietf/filter.d.ts +21 -31
  219. package/ietf/filter.d.ts.map +1 -1
  220. package/ietf/filter.js +140 -172
  221. package/ietf/filter.js.map +1 -1
  222. package/ietf/goaway.d.ts +12 -10
  223. package/ietf/goaway.d.ts.map +1 -1
  224. package/ietf/goaway.js +22 -36
  225. package/ietf/goaway.js.map +1 -1
  226. package/ietf/hidden.js +1 -2
  227. package/ietf/hidden.js.map +1 -1
  228. package/ietf/index.d.ts +8 -20
  229. package/ietf/index.d.ts.map +1 -1
  230. package/ietf/index.js +4 -15
  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 -41
  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 +19 -5
  245. package/ietf/namespace.d.ts.map +1 -1
  246. package/ietf/namespace.js +78 -27
  247. package/ietf/namespace.js.map +1 -1
  248. package/ietf/object.d.ts +195 -33
  249. package/ietf/object.d.ts.map +1 -1
  250. package/ietf/object.js +405 -179
  251. package/ietf/object.js.map +1 -1
  252. package/ietf/parameters.d.ts +25 -45
  253. package/ietf/parameters.d.ts.map +1 -1
  254. package/ietf/parameters.js +260 -473
  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 -5
  259. package/ietf/priority.js.map +1 -1
  260. package/ietf/properties.d.ts +6 -5
  261. package/ietf/properties.d.ts.map +1 -1
  262. package/ietf/properties.js +78 -49
  263. package/ietf/properties.js.map +1 -1
  264. package/ietf/publish.d.ts +25 -33
  265. package/ietf/publish.d.ts.map +1 -1
  266. package/ietf/publish.js +45 -140
  267. package/ietf/publish.js.map +1 -1
  268. package/ietf/publish_namespace.d.ts +49 -72
  269. package/ietf/publish_namespace.d.ts.map +1 -1
  270. package/ietf/publish_namespace.js +66 -185
  271. package/ietf/publish_namespace.js.map +1 -1
  272. package/ietf/publisher.js +1800 -601
  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 +6 -30
  279. package/ietf/request.d.ts.map +1 -1
  280. package/ietf/request.js +41 -79
  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 -17
  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 +5 -30
  291. package/ietf/setup.d.ts.map +1 -1
  292. package/ietf/setup.js +6 -118
  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 -15
  299. package/ietf/solicit.js.map +1 -1
  300. package/ietf/subscribe.d.ts +52 -45
  301. package/ietf/subscribe.d.ts.map +1 -1
  302. package/ietf/subscribe.js +118 -240
  303. package/ietf/subscribe.js.map +1 -1
  304. package/ietf/subscribe_namespace.d.ts +16 -79
  305. package/ietf/subscribe_namespace.d.ts.map +1 -1
  306. package/ietf/subscribe_namespace.js +30 -202
  307. package/ietf/subscribe_namespace.js.map +1 -1
  308. package/ietf/subscriber.js +1839 -537
  309. package/ietf/subscriber.js.map +1 -1
  310. package/ietf/token.js +105 -28
  311. package/ietf/token.js.map +1 -1
  312. package/ietf/track.d.ts +27 -24
  313. package/ietf/track.d.ts.map +1 -1
  314. package/ietf/track.js +48 -62
  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 -49
  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 +34 -19
  325. package/index.d.ts.map +1 -1
  326. package/index.js +19 -7
  327. package/index.js.map +1 -1
  328. package/internal.d.ts +1515 -67
  329. package/internal.d.ts.map +1 -1
  330. package/internal.js +557 -80
  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 +127 -66
  341. package/lite/announce.d.ts.map +1 -1
  342. package/lite/announce.js +384 -159
  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 -78
  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 -10
  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 +2 -2
  389. package/lite/goaway.d.ts.map +1 -1
  390. package/lite/goaway.js +5 -14
  391. package/lite/goaway.js.map +1 -1
  392. package/lite/group.d.ts +67 -16
  393. package/lite/group.d.ts.map +1 -1
  394. package/lite/group.js +108 -31
  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 +14 -13
  401. package/lite/index.d.ts.map +1 -1
  402. package/lite/index.js +2 -2
  403. package/lite/index.js.map +1 -1
  404. package/lite/message.d.ts +10 -2
  405. package/lite/message.d.ts.map +1 -1
  406. package/lite/message.js +28 -45
  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 -106
  411. package/lite/priority.js.map +1 -1
  412. package/lite/probe.d.ts +2 -2
  413. package/lite/probe.d.ts.map +1 -1
  414. package/lite/probe.js +9 -1
  415. package/lite/probe.js.map +1 -1
  416. package/lite/publisher.d.ts +10 -1
  417. package/lite/publisher.d.ts.map +1 -1
  418. package/lite/publisher.js +2548 -438
  419. package/lite/publisher.js.map +1 -1
  420. package/lite/routed.d.ts +59 -0
  421. package/lite/routed.d.ts.map +1 -0
  422. package/lite/routed.js +564 -0
  423. package/lite/routed.js.map +1 -0
  424. package/lite/session.d.ts +2 -28
  425. package/lite/session.d.ts.map +1 -1
  426. package/lite/session.js +0 -100
  427. package/lite/session.js.map +1 -1
  428. package/lite/setup.d.ts +32 -5
  429. package/lite/setup.d.ts.map +1 -1
  430. package/lite/setup.js +95 -22
  431. package/lite/setup.js.map +1 -1
  432. package/lite/stream.d.ts +5 -11
  433. package/lite/stream.d.ts.map +1 -1
  434. package/lite/stream.js +5 -2
  435. package/lite/stream.js.map +1 -1
  436. package/lite/subscribe.d.ts +478 -78
  437. package/lite/subscribe.d.ts.map +1 -1
  438. package/lite/subscribe.js +1373 -143
  439. package/lite/subscribe.js.map +1 -1
  440. package/lite/subscriber.d.ts +7 -44
  441. package/lite/subscriber.d.ts.map +1 -1
  442. package/lite/subscriber.js +3290 -516
  443. package/lite/subscriber.js.map +1 -1
  444. package/lite/track.d.ts +27 -12
  445. package/lite/track.d.ts.map +1 -1
  446. package/lite/track.js +116 -38
  447. package/lite/track.js.map +1 -1
  448. package/lite/tracks.d.ts +55 -0
  449. package/lite/tracks.d.ts.map +1 -0
  450. package/lite/tracks.js +114 -0
  451. package/lite/tracks.js.map +1 -0
  452. package/lite/version.d.ts +132 -24
  453. package/lite/version.d.ts.map +1 -1
  454. package/lite/version.js +378 -34
  455. package/lite/version.js.map +1 -1
  456. package/meta.d.ts +77 -0
  457. package/meta.d.ts.map +1 -0
  458. package/meta.js +130 -0
  459. package/meta.js.map +1 -0
  460. package/meta_api.d.ts +7 -0
  461. package/meta_api.d.ts.map +1 -0
  462. package/meta_api.js +7 -0
  463. package/meta_api.js.map +1 -0
  464. package/mint.d.ts +28 -0
  465. package/mint.d.ts.map +1 -0
  466. package/mint.js +66 -0
  467. package/mint.js.map +1 -0
  468. package/open.d.ts +63 -0
  469. package/open.d.ts.map +1 -0
  470. package/open.js +250 -0
  471. package/open.js.map +1 -0
  472. package/origin.d.ts +394 -142
  473. package/origin.d.ts.map +1 -1
  474. package/origin.js +2533 -687
  475. package/origin.js.map +1 -1
  476. package/package.json +8 -13
  477. package/path.d.ts +51 -51
  478. package/path.d.ts.map +1 -1
  479. package/path.js +210 -58
  480. package/path.js.map +1 -1
  481. package/payload.d.ts +33 -0
  482. package/payload.d.ts.map +1 -0
  483. package/payload.js +65 -0
  484. package/payload.js.map +1 -0
  485. package/prefix.d.ts +152 -0
  486. package/prefix.d.ts.map +1 -0
  487. package/prefix.js +349 -0
  488. package/prefix.js.map +1 -0
  489. package/priority.d.ts +8 -0
  490. package/priority.d.ts.map +1 -0
  491. package/priority.js +185 -0
  492. package/priority.js.map +1 -0
  493. package/queue.d.ts +2 -0
  494. package/queue.d.ts.map +1 -0
  495. package/queue.js +46 -0
  496. package/queue.js.map +1 -0
  497. package/ranges.d.ts +36 -0
  498. package/ranges.d.ts.map +1 -0
  499. package/ranges.js +312 -0
  500. package/ranges.js.map +1 -0
  501. package/rescale.d.ts +24 -0
  502. package/rescale.d.ts.map +1 -0
  503. package/rescale.js +39 -0
  504. package/rescale.js.map +1 -0
  505. package/resume.d.ts +55 -0
  506. package/resume.d.ts.map +1 -0
  507. package/resume.js +4173 -0
  508. package/resume.js.map +1 -0
  509. package/resume.test-util.d.ts +2 -0
  510. package/resume.test-util.d.ts.map +1 -0
  511. package/resume.test-util.js +25 -0
  512. package/resume.test-util.js.map +1 -0
  513. package/skips.d.ts +48 -0
  514. package/skips.d.ts.map +1 -0
  515. package/skips.js +182 -0
  516. package/skips.js.map +1 -0
  517. package/stream.d.ts +78 -40
  518. package/stream.d.ts.map +1 -1
  519. package/stream.js +236 -93
  520. package/stream.js.map +1 -1
  521. package/subscription.test-util.d.ts +2 -0
  522. package/subscription.test-util.d.ts.map +1 -0
  523. package/subscription.test-util.js +49 -0
  524. package/subscription.test-util.js.map +1 -0
  525. package/tail.d.ts +23 -14
  526. package/tail.d.ts.map +1 -1
  527. package/tail.js +224 -98
  528. package/tail.js.map +1 -1
  529. package/time.d.ts +41 -102
  530. package/time.d.ts.map +1 -1
  531. package/time.js +129 -92
  532. package/time.js.map +1 -1
  533. package/timed.d.ts +21 -0
  534. package/timed.d.ts.map +1 -0
  535. package/timed.js +2 -0
  536. package/timed.js.map +1 -0
  537. package/track.d.ts +578 -246
  538. package/track.d.ts.map +1 -1
  539. package/track.js +7306 -772
  540. package/track.js.map +1 -1
  541. package/track_api.d.ts +7 -0
  542. package/track_api.d.ts.map +1 -0
  543. package/track_api.js +7 -0
  544. package/track_api.js.map +1 -0
  545. package/util/abort.d.ts +1 -0
  546. package/util/abort.d.ts.map +1 -1
  547. package/util/abort.js +23 -2
  548. package/util/abort.js.map +1 -1
  549. package/util/announce.d.ts +4 -0
  550. package/util/announce.d.ts.map +1 -0
  551. package/util/announce.js +11 -0
  552. package/util/announce.js.map +1 -0
  553. package/util/bigint.d.ts +3 -0
  554. package/util/bigint.d.ts.map +1 -0
  555. package/util/bigint.js +8 -0
  556. package/util/bigint.js.map +1 -0
  557. package/util/change.d.ts +12 -0
  558. package/util/change.d.ts.map +1 -0
  559. package/util/change.js +18 -0
  560. package/util/change.js.map +1 -0
  561. package/util/deadline.d.ts +2 -0
  562. package/util/deadline.d.ts.map +1 -0
  563. package/util/deadline.js +108 -0
  564. package/util/deadline.js.map +1 -0
  565. package/util/decode_error.d.ts +3 -0
  566. package/util/decode_error.d.ts.map +1 -0
  567. package/util/decode_error.js +11 -0
  568. package/util/decode_error.js.map +1 -0
  569. package/util/demand.d.ts +8 -0
  570. package/util/demand.d.ts.map +1 -0
  571. package/util/demand.js +24 -0
  572. package/util/demand.js.map +1 -0
  573. package/util/given_up.d.ts +2 -0
  574. package/util/given_up.d.ts.map +1 -0
  575. package/util/given_up.js +101 -0
  576. package/util/given_up.js.map +1 -0
  577. package/util/hex.d.ts +6 -0
  578. package/util/hex.d.ts.map +1 -1
  579. package/util/hex.js +19 -5
  580. package/util/hex.js.map +1 -1
  581. package/util/key.d.ts +15 -0
  582. package/util/key.d.ts.map +1 -0
  583. package/util/key.js +38 -0
  584. package/util/key.js.map +1 -0
  585. package/util/limits.d.ts +2 -0
  586. package/util/limits.d.ts.map +1 -0
  587. package/util/limits.js +76 -0
  588. package/util/limits.js.map +1 -0
  589. package/util/observed.d.ts +23 -0
  590. package/util/observed.d.ts.map +1 -0
  591. package/util/observed.js +122 -0
  592. package/util/observed.js.map +1 -0
  593. package/util/sorted.d.ts +2 -0
  594. package/util/sorted.d.ts.map +1 -0
  595. package/util/sorted.js +149 -0
  596. package/util/sorted.js.map +1 -0
  597. package/util/timeout.d.ts +2 -1
  598. package/util/timeout.d.ts.map +1 -1
  599. package/util/timeout.js +13 -10
  600. package/util/timeout.js.map +1 -1
  601. package/util/timer.d.ts +2 -0
  602. package/util/timer.d.ts.map +1 -0
  603. package/util/timer.js +19 -0
  604. package/util/timer.js.map +1 -0
  605. package/util/u64.js +0 -1
  606. package/util/utf8.d.ts +2 -0
  607. package/util/utf8.d.ts.map +1 -1
  608. package/util/utf8.js +7 -2
  609. package/util/utf8.js.map +1 -1
  610. package/util/varint.d.ts.map +1 -1
  611. package/util/varint.js +8 -2
  612. package/util/varint.js.map +1 -1
  613. package/util/watch.d.ts +8 -0
  614. package/util/watch.d.ts.map +1 -0
  615. package/util/watch.js +38 -0
  616. package/util/watch.js.map +1 -0
  617. package/util/yield.d.ts +2 -0
  618. package/util/yield.d.ts.map +1 -0
  619. package/util/yield.js +31 -0
  620. package/util/yield.js.map +1 -0
  621. package/varint.d.ts +0 -29
  622. package/varint.d.ts.map +1 -1
  623. package/varint.js +7 -29
  624. package/varint.js.map +1 -1
  625. package/varint_api.d.ts +7 -0
  626. package/varint_api.d.ts.map +1 -0
  627. package/varint_api.js +7 -0
  628. package/varint_api.js.map +1 -0
  629. package/version.d.ts +2 -0
  630. package/version.d.ts.map +1 -0
  631. package/version.js +4 -0
  632. package/version.js.map +1 -0
  633. package/window.d.ts +570 -0
  634. package/window.d.ts.map +1 -0
  635. package/window.js +1677 -0
  636. package/window.js.map +1 -0
  637. package/wire.d.ts +150 -35
  638. package/wire.d.ts.map +1 -1
  639. package/wire.js +27 -1
  640. package/wire.js.map +1 -1
  641. package/connection/established.d.ts +0 -53
  642. package/connection/established.d.ts.map +0 -1
  643. package/connection/established.js +0 -3
  644. package/connection/established.js.map +0 -1
  645. package/ietf/adapter.d.ts +0 -75
  646. package/ietf/adapter.d.ts.map +0 -1
  647. package/ietf/adapter.js +0 -700
  648. package/ietf/adapter.js.map +0 -1
  649. package/lite/datagram_stream.d.ts +0 -8
  650. package/lite/datagram_stream.d.ts.map +0 -1
  651. package/lite/datagram_stream.js +0 -44
  652. package/lite/datagram_stream.js.map +0 -1
  653. package/lite/fetch.d.ts +0 -28
  654. package/lite/fetch.d.ts.map +0 -1
  655. package/lite/fetch.js +0 -79
  656. package/lite/fetch.js.map +0 -1
  657. package/util/log.d.ts +0 -5
  658. package/util/log.d.ts.map +0 -1
  659. package/util/log.js +0 -17
  660. package/util/log.js.map +0 -1
  661. package/zod.d.ts +0 -13
  662. package/zod.d.ts.map +0 -1
  663. package/zod.js +0 -19
  664. package/zod.js.map +0 -1
package/lite/subscribe.js CHANGED
@@ -1,20 +1,28 @@
1
- /* @ts-self-types="./subscribe.d.ts" */
1
+ import { Duration as ClockDuration } from "@moq/clock";
2
+ import { ProtocolViolation } from "../error.js";
3
+ import { NO_MAX_DELAY } from "../internal.js";
2
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";
3
10
  import * as Message from "./message.js";
4
- 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";
5
13
  /**
6
14
  * Encode the `Group Start` field shared by SUBSCRIBE and SUBSCRIBE_UPDATE.
7
15
  *
8
16
  * Lite-06 writes the raw floor (`undefined` and 0 are both group 0). A pre-06 wire
9
- * 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
10
18
  * publisher starts) and an explicit 0 is 1, replay from the beginning.
11
19
  */
12
20
  async function encodeStartGroup(w, version, startGroup) {
13
21
  if (resolvesStart(version)) {
14
- await w.u53(startGroup ?? 0);
22
+ await w.u62(startGroup ?? 0n);
15
23
  return;
16
24
  }
17
- await w.u53(startGroup === undefined ? 0 : startGroup + 1);
25
+ await w.u62(startGroup === undefined ? 0n : startGroup + 1n);
18
26
  }
19
27
  /**
20
28
  * Decode the `Group Start` field shared by SUBSCRIBE and SUBSCRIBE_UPDATE.
@@ -23,10 +31,10 @@ async function encodeStartGroup(w, version, startGroup) {
23
31
  * {@link canonicalStartGroup} once the frame bounds are known.
24
32
  */
25
33
  async function decodeStartGroup(r, version) {
26
- const value = await r.u53();
34
+ const value = await r.u62();
27
35
  if (resolvesStart(version))
28
36
  return value;
29
- return value > 0 ? value - 1 : undefined;
37
+ return value > 0n ? value - 1n : undefined;
30
38
  }
31
39
  /**
32
40
  * Canonicalize a decoded floor: a lite-06 `Group Start` of 0 with no frame offset is the
@@ -35,7 +43,7 @@ async function decodeStartGroup(r, version) {
35
43
  * partway through group 0: a catalog never leaves it).
36
44
  */
37
45
  function canonicalStartGroup(version, startGroup, startFrame) {
38
- if (resolvesStart(version) && startGroup === 0 && startFrame === 0)
46
+ if (resolvesStart(version) && startGroup === 0n && startFrame === 0)
39
47
  return undefined;
40
48
  return startGroup;
41
49
  }
@@ -74,6 +82,876 @@ async function decodeFrameBounds(r, version, startGroup, endGroup) {
74
82
  }
75
83
  return { startFrame, endFrame: endFrame > 0 ? endFrame - 1 : undefined };
76
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
+ }
77
955
  /** Step over the retired `Ordered` byte on a version whose layout still has it. */
78
956
  async function skipGroupOrder(r, version) {
79
957
  if (hasGroupOrder(version))
@@ -85,71 +963,179 @@ async function padGroupOrder(w, version) {
85
963
  await w.bool(false);
86
964
  }
87
965
  /**
88
- * Exclusive model/local cap from a decoded inclusive last group.
89
- *
90
- * The wire's `Group End` is inclusive once decoded; the track model and `setGroups` are
91
- * 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.
92
998
  */
93
- export function exclusiveGroupEnd(inclusive) {
94
- 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
+ }
95
1020
  }
96
- /** 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. */
97
1022
  export const EMPTY_RANGE = "empty subscription range cannot be encoded";
98
1023
  /**
99
1024
  * Inclusive last group a Subscribe message carries, from an exclusive model end.
100
1025
  *
101
1026
  * Empty (`0`) cannot be encoded: the wire's 0 means unbounded. Callers refuse an empty
102
- * requested range with {@link emptyRange} before reaching here.
1027
+ * requested range before reaching here.
103
1028
  */
104
1029
  export function inclusiveGroupEnd(exclusive) {
105
1030
  if (exclusive === undefined)
106
1031
  return undefined;
107
- if (exclusive === 0)
1032
+ if (exclusive === 0n)
108
1033
  throw new Error(EMPTY_RANGE);
109
- return exclusive - 1;
1034
+ return exclusive - 1n;
110
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;
111
1038
  /**
112
- * Whether a requested range asks for nothing.
113
- *
114
- * The wire has no encoding for one: its bounds are inclusive, so flooring the end would
115
- * either hand back the group the caller excluded (0 means unbounded) or invert the
116
- * range once the two bounds meet. An absent start is the live edge, so it only empties
117
- * 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.
118
1041
  */
119
- export function emptyRange({ startGroup, endGroup }) {
120
- 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;
121
1054
  }
122
1055
  export class SubscribeUpdate {
1056
+ /** A refresh request for this group or later, judged against the local track's refresh. */
1057
+ wantedGroup;
123
1058
  priority;
124
- /** Subscriber max latency in milliseconds; zero skips once a newer group is available. */
125
- maxAge;
1059
+ /** See {@link Subscribe.order}. */
1060
+ order;
1061
+ /** See {@link Subscribe.maxDelay}. */
1062
+ maxDelay;
126
1063
  startGroup;
127
1064
  endGroup;
128
1065
  /** See {@link Subscribe.startFrame}. */
129
1066
  startFrame;
130
1067
  /** See {@link Subscribe.endFrame}. */
131
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;
132
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;
133
1085
  this.priority = props.priority;
134
- this.maxAge = props.maxAge ?? 0;
1086
+ this.order = props.order ?? "descending";
1087
+ this.maxDelay = props.maxDelay === undefined ? 0 : props.maxDelay;
135
1088
  this.startGroup = props.startGroup;
136
1089
  this.endGroup = props.endGroup;
137
1090
  this.startFrame = props.startFrame ?? 0;
138
1091
  this.endFrame = props.endFrame;
1092
+ this.ranges = props.ranges;
1093
+ this.remove = props.remove;
1094
+ this.retune = props.retune;
139
1095
  }
140
1096
  async #encode(w, version) {
141
1097
  switch (version) {
142
1098
  case Version.DRAFT_01:
143
1099
  case Version.DRAFT_02:
144
- await w.u8(this.priority);
1100
+ await w.u8(toWire(this.priority));
145
1101
  break;
146
1102
  default:
147
- await w.u8(this.priority);
148
- await padGroupOrder(w, version);
149
- await w.u53(this.maxAge);
150
- await encodeStartGroup(w, version, this.startGroup);
151
- await w.u53(this.endGroup !== undefined ? this.endGroup + 1 : 0);
152
- 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);
153
1139
  break;
154
1140
  }
155
1141
  }
@@ -157,26 +1143,45 @@ export class SubscribeUpdate {
157
1143
  switch (version) {
158
1144
  case Version.DRAFT_01:
159
1145
  case Version.DRAFT_02:
160
- return new SubscribeUpdate({ priority: await r.u8() });
1146
+ return new SubscribeUpdate({ priority: fromWire(await r.u8()), maxDelay: null });
161
1147
  default: {
162
- const priority = await r.u8();
163
- await skipGroupOrder(r, version);
164
- const maxAge = await r.u53();
165
- const startGroup = await decodeStartGroup(r, version);
166
- const endGroup = (await r.u53()) || undefined;
167
- const end = endGroup !== undefined ? endGroup - 1 : undefined;
168
- 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;
169
1172
  return new SubscribeUpdate({
1173
+ wantedGroup: wanted === 0n ? undefined : wanted - 1n,
170
1174
  priority,
171
- maxAge,
172
- startGroup: canonicalStartGroup(version, startGroup, frames.startFrame),
173
- endGroup: end,
174
- ...frames,
1175
+ order,
1176
+ maxDelay,
1177
+ ...span,
175
1178
  });
176
1179
  }
177
1180
  }
178
1181
  }
179
1182
  async encode(w, version) {
1183
+ if (this.wantedGroup !== undefined && this.wantedGroup < 0n)
1184
+ throw new RangeError("wanted group must be non-negative");
180
1185
  return Message.encode(w, (w) => this.#encode(w, version));
181
1186
  }
182
1187
  static async decode(r, version) {
@@ -189,10 +1194,22 @@ export class SubscribeUpdate {
189
1194
  export class Subscribe {
190
1195
  id;
191
1196
  broadcast;
1197
+ /** The publisher identity the subscriber expects. Lite-07+. */
1198
+ identity;
192
1199
  track;
193
1200
  priority;
194
- /** Subscriber max latency in milliseconds; zero skips once a newer group is available. */
195
- 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;
196
1213
  startGroup;
197
1214
  endGroup;
198
1215
  /**
@@ -206,60 +1223,88 @@ export class Subscribe {
206
1223
  * whole group. Lite-06+, and meaningless without an explicit `endGroup`.
207
1224
  */
208
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;
209
1237
  constructor(props) {
210
1238
  this.id = props.id;
211
1239
  this.broadcast = props.broadcast;
1240
+ this.identity = props.identity;
212
1241
  this.track = props.track;
213
1242
  this.priority = props.priority;
214
- this.maxAge = props.maxAge ?? 0;
1243
+ this.order = props.order ?? "descending";
1244
+ this.maxDelay = props.maxDelay === undefined ? 0 : props.maxDelay;
215
1245
  this.startGroup = props.startGroup;
216
1246
  this.endGroup = props.endGroup;
217
1247
  this.startFrame = props.startFrame ?? 0;
218
1248
  this.endFrame = props.endFrame;
1249
+ this.ranges = props.ranges;
1250
+ this.meta = props.meta ?? false;
219
1251
  }
220
1252
  async #encode(w, version) {
221
1253
  await w.u62(this.id);
222
1254
  await w.string(Path.encode(this.broadcast));
1255
+ await encodePin(w, version, this.identity);
1256
+ await encodeNode(w, version);
223
1257
  await w.string(this.track);
224
- await w.u8(this.priority);
225
1258
  switch (version) {
226
1259
  case Version.DRAFT_01:
227
1260
  case Version.DRAFT_02:
1261
+ await w.u8(toWire(this.priority));
228
1262
  break;
229
1263
  default:
230
- await padGroupOrder(w, version);
231
- await w.u53(this.maxAge);
232
- await encodeStartGroup(w, version, this.startGroup);
233
- await w.u53(this.endGroup !== undefined ? this.endGroup + 1 : 0);
234
- 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);
235
1271
  break;
236
1272
  }
1273
+ await encodeMeta(w, version, this.meta);
237
1274
  }
238
1275
  static async #decode(r, version) {
239
1276
  const id = await r.u62();
240
1277
  const broadcast = Path.decode(await r.string());
1278
+ const identity = await decodePin(r, version);
1279
+ await decodeNode(r, version);
241
1280
  const track = await r.string();
242
- const priority = await r.u8();
243
1281
  switch (version) {
244
1282
  case Version.DRAFT_01:
245
1283
  case Version.DRAFT_02:
246
- return new Subscribe({ id, broadcast, track, priority });
1284
+ return new Subscribe({ id, broadcast, track, priority: fromWire(await r.u8()), maxDelay: null });
247
1285
  default: {
248
- await skipGroupOrder(r, version);
249
- const maxAge = await r.u53();
250
- const startGroup = await decodeStartGroup(r, version);
251
- const endGroup = (await r.u53()) || undefined;
252
- const end = endGroup !== undefined ? endGroup - 1 : undefined;
253
- 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);
254
1298
  return new Subscribe({
255
1299
  id,
256
1300
  broadcast,
1301
+ identity,
257
1302
  track,
258
1303
  priority,
259
- maxAge,
260
- startGroup: canonicalStartGroup(version, startGroup, frames.startFrame),
261
- endGroup: end,
262
- ...frames,
1304
+ order,
1305
+ maxDelay,
1306
+ ...span,
1307
+ meta,
263
1308
  });
264
1309
  }
265
1310
  }
@@ -279,13 +1324,13 @@ export class Subscribe {
279
1324
  */
280
1325
  export class SubscribeOk {
281
1326
  priority;
282
- /** Accepted subscriber max latency in milliseconds. */
283
- maxAge;
1327
+ /** Accepted subscriber max delay in milliseconds. */
1328
+ maxDelay;
284
1329
  startGroup;
285
1330
  endGroup;
286
- constructor({ priority = 0, maxAge = 0, startGroup = undefined, endGroup = undefined, }) {
1331
+ constructor({ priority = 0, maxDelay = 0, startGroup = undefined, endGroup = undefined, }) {
287
1332
  this.priority = priority;
288
- this.maxAge = maxAge;
1333
+ this.maxDelay = maxDelay;
289
1334
  this.startGroup = startGroup;
290
1335
  this.endGroup = endGroup;
291
1336
  }
@@ -295,22 +1340,22 @@ export class SubscribeOk {
295
1340
  // noop
296
1341
  break;
297
1342
  case Version.DRAFT_01:
298
- await w.u8(this.priority ?? 0);
1343
+ await w.u8(toWire(this.priority));
299
1344
  break;
300
1345
  // Draft-05+ never sends SUBSCRIBE_OK, but keep the field layout matching
301
1346
  // Draft-03/04 so a stray future use stays well-formed.
302
1347
  default:
303
- await w.u8(this.priority);
1348
+ await w.u8(toWire(this.priority));
304
1349
  await padGroupOrder(w, version);
305
- await w.u53(this.maxAge);
306
- await w.u53(this.startGroup !== undefined ? this.startGroup + 1 : 0);
307
- 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);
308
1353
  break;
309
1354
  }
310
1355
  }
311
1356
  static async #decode(version, r) {
312
1357
  let priority;
313
- let maxAge;
1358
+ let maxDelay;
314
1359
  let startGroup;
315
1360
  let endGroup;
316
1361
  switch (version) {
@@ -318,21 +1363,21 @@ export class SubscribeOk {
318
1363
  // noop
319
1364
  break;
320
1365
  case Version.DRAFT_01:
321
- priority = await r.u8();
1366
+ priority = fromWire(await r.u8());
322
1367
  break;
323
1368
  default:
324
- priority = await r.u8();
1369
+ priority = fromWire(await r.u8());
325
1370
  await skipGroupOrder(r, version);
326
- maxAge = await r.u53();
327
- startGroup = await r.u53();
328
- endGroup = await r.u53();
1371
+ maxDelay = await r.u53();
1372
+ startGroup = await r.u62();
1373
+ endGroup = await r.u62();
329
1374
  break;
330
1375
  }
331
1376
  return new SubscribeOk({
332
1377
  priority,
333
- maxAge,
334
- startGroup: startGroup !== undefined && startGroup > 0 ? startGroup - 1 : undefined,
335
- 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,
336
1381
  });
337
1382
  }
338
1383
  async encode(w, version) {
@@ -343,81 +1388,231 @@ export class SubscribeOk {
343
1388
  }
344
1389
  }
345
1390
  /**
346
- * Resolves the absolute start group of a Draft-05+ subscription. The first message
347
- * the publisher sends, once the start group is known. A value greater than the
348
- * 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.
349
1395
  *
350
- * There is no start frame: a partial group is only served to a subscriber that asked
351
- * for one, so delivery begins either at the requested `startFrame` (when this is the
352
- * 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.
353
1399
  */
354
1400
  export class SubscribeStart {
355
- group;
356
- constructor(group) {
357
- 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;
358
1411
  }
359
- async encode(w) {
1412
+ async encode(w, version) {
360
1413
  return Message.encode(w, async (w) => {
361
- 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);
362
1430
  });
363
1431
  }
364
- static async decode(r) {
365
- 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
+ });
366
1456
  }
367
1457
  }
368
1458
  /**
369
- * Signals that no group at or after `group` (exclusive upper bound) will be produced
370
- * 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.
371
1461
  */
372
1462
  export class SubscribeEnd {
373
- /** 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. */
374
1464
  group;
375
- /**
376
- * The number of group streams the publisher opened for this subscription.
377
- * Draft-07+ only; not on the wire before, where it decodes as 0.
378
- */
379
- streams;
380
- constructor(group, streams = 0) {
1465
+ constructor(group) {
381
1466
  this.group = group;
382
- 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;
383
1514
  }
384
1515
  async encode(w, version) {
385
1516
  return Message.encode(w, async (w) => {
386
- await w.u53(this.group);
387
- if (hasStreamCount(version))
388
- await w.u53(this.streams);
1517
+ await encodeGroups(w, this.start, this.end, version);
1518
+ if (!hasSkip(version))
1519
+ await w.u53(0);
389
1520
  });
390
1521
  }
391
1522
  static async decode(r, version) {
392
- 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
+ });
393
1529
  }
394
1530
  }
395
- /// Indicates that one or more groups have been dropped.
396
- ///
397
- /// Draft-03 to Draft-06 only: Draft-07 counts group streams in SUBSCRIBE_END instead.
398
- 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. */
399
1537
  start;
1538
+ /** One past the last skipped group key. */
400
1539
  end;
401
- error;
402
1540
  constructor(props) {
403
1541
  this.start = props.start;
404
1542
  this.end = props.end;
405
- this.error = props.error;
406
1543
  }
407
- async #encode(w) {
408
- await w.u53(this.start);
409
- await w.u53(this.end);
410
- 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;
411
1568
  }
412
- static async #decode(r) {
413
- 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
+ });
414
1580
  }
415
- async encode(w) {
416
- 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
+ });
417
1589
  }
418
- static async decode(r) {
419
- 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;
420
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 }) };
421
1616
  }
422
1617
  export async function encodeSubscribeResponse(w, resp, version) {
423
1618
  switch (version) {
@@ -438,25 +1633,33 @@ export async function encodeSubscribeResponse(w, resp, version) {
438
1633
  }
439
1634
  else if ("drop" in resp) {
440
1635
  await w.u53(0x1);
441
- await resp.drop.encode(w);
1636
+ await resp.drop.encode(w, version);
442
1637
  }
443
1638
  else {
444
- throw new Error("SUBSCRIBE_START/END not supported for this version");
1639
+ throw new Error("SUBSCRIBE_START/FINISH not supported for this version");
445
1640
  }
446
1641
  break;
447
1642
  default:
448
1643
  // Draft-05+: SUBSCRIBE_OK is gone; START/END/DROP carry the resolved range.
449
1644
  if ("start" in resp) {
450
- await w.u53(0x0);
451
- await resp.start.encode(w);
1645
+ await w.u53(hasRanges(version) ? 0x4 : 0x0);
1646
+ await resp.start.encode(w, version);
452
1647
  }
453
1648
  else if ("end" in resp) {
454
1649
  await w.u53(0x1);
455
1650
  await resp.end.encode(w, version);
456
1651
  }
457
- else if ("drop" in resp && !hasStreamCount(version)) {
1652
+ else if ("drop" in resp) {
458
1653
  await w.u53(0x2);
459
- 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);
460
1663
  }
461
1664
  else {
462
1665
  throw new Error("subscribe response not supported for this version");
@@ -476,28 +1679,55 @@ export async function decodeSubscribeResponse(r, version) {
476
1679
  case 0x0:
477
1680
  return { ok: await SubscribeOk.decode(r, version) };
478
1681
  case 0x1:
479
- return { drop: await SubscribeDrop.decode(r) };
1682
+ return { drop: await SubscribeDrop.decode(r, version) };
480
1683
  default:
481
- throw new Error(`unknown subscribe response type: ${typ}`);
1684
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
482
1685
  }
483
1686
  }
484
1687
  default: {
485
1688
  const typ = await r.u53();
486
1689
  switch (typ) {
487
1690
  case 0x0:
488
- 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) };
489
1694
  case 0x1:
490
1695
  return { end: await SubscribeEnd.decode(r, version) };
491
1696
  case 0x2:
492
- if (hasStreamCount(version))
493
- throw new Error(`unknown subscribe response type: ${typ}`);
494
- 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) };
495
1710
  default:
496
- throw new Error(`unknown subscribe response type: ${typ}`);
1711
+ throw new ProtocolViolation(`unknown subscribe response type: ${typ}`);
497
1712
  }
498
1713
  }
499
1714
  }
500
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
+ }
501
1731
  /** Like {@link decodeSubscribeResponse} but resolves `undefined` on a clean FIN. */
502
1732
  export async function decodeSubscribeResponseMaybe(r, version) {
503
1733
  if (await r.done())