@moq/net 0.4.8 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (662) hide show
  1. package/README.md +7 -1
  2. package/announce.d.ts +1 -1
  3. package/announce.d.ts.map +1 -1
  4. package/announce.js +1 -1
  5. package/announce.js.map +1 -1
  6. package/announced.d.ts +51 -47
  7. package/announced.d.ts.map +1 -1
  8. package/announced.js +183 -31
  9. package/announced.js.map +1 -1
  10. package/announced.test-util.d.ts +2 -0
  11. package/announced.test-util.d.ts.map +1 -0
  12. package/announced.test-util.js +27 -0
  13. package/announced.test-util.js.map +1 -0
  14. package/auth.d.ts +64 -0
  15. package/auth.d.ts.map +1 -0
  16. package/auth.js +20 -0
  17. package/auth.js.map +1 -0
  18. package/auth_gate.d.ts +2 -0
  19. package/auth_gate.d.ts.map +1 -0
  20. package/auth_gate.js +24 -0
  21. package/auth_gate.js.map +1 -0
  22. package/auth_session.d.ts +49 -0
  23. package/auth_session.d.ts.map +1 -0
  24. package/auth_session.js +1097 -0
  25. package/auth_session.js.map +1 -0
  26. package/bandwidth.d.ts +129 -25
  27. package/bandwidth.d.ts.map +1 -1
  28. package/bandwidth.js +410 -138
  29. package/bandwidth.js.map +1 -1
  30. package/bandwidth_api.d.ts +1 -1
  31. package/bandwidth_api.d.ts.map +1 -1
  32. package/bandwidth_api.js +1 -1
  33. package/bandwidth_api.js.map +1 -1
  34. package/broadcast.d.ts +62 -36
  35. package/broadcast.d.ts.map +1 -1
  36. package/broadcast.js +556 -172
  37. package/broadcast.js.map +1 -1
  38. package/cache.d.ts +103 -0
  39. package/cache.d.ts.map +1 -0
  40. package/cache.js +254 -0
  41. package/cache.js.map +1 -0
  42. package/claim.test-util.d.ts +5 -0
  43. package/claim.test-util.d.ts.map +1 -0
  44. package/claim.test-util.js +13 -0
  45. package/claim.test-util.js.map +1 -0
  46. package/connection/accept.d.ts +20 -12
  47. package/connection/accept.d.ts.map +1 -1
  48. package/connection/accept.js +97 -129
  49. package/connection/accept.js.map +1 -1
  50. package/connection/browser.d.ts.map +1 -1
  51. package/connection/browser.js +28 -19
  52. package/connection/browser.js.map +1 -1
  53. package/connection/connect.d.ts +119 -23
  54. package/connection/connect.d.ts.map +1 -1
  55. package/connection/connect.js +535 -216
  56. package/connection/connect.js.map +1 -1
  57. package/connection/dials.d.ts +45 -0
  58. package/connection/dials.d.ts.map +1 -0
  59. package/connection/dials.js +109 -0
  60. package/connection/dials.js.map +1 -0
  61. package/connection/endpoint.d.ts +60 -0
  62. package/connection/endpoint.d.ts.map +1 -0
  63. package/connection/endpoint.js +114 -0
  64. package/connection/endpoint.js.map +1 -0
  65. package/connection/forward.d.ts +7 -1
  66. package/connection/forward.d.ts.map +1 -1
  67. package/connection/forward.js +265 -80
  68. package/connection/forward.js.map +1 -1
  69. package/connection/goaway.d.ts +16 -9
  70. package/connection/goaway.d.ts.map +1 -1
  71. package/connection/goaway.js +22 -108
  72. package/connection/goaway.js.map +1 -1
  73. package/connection/handshake.d.ts +32 -6
  74. package/connection/handshake.d.ts.map +1 -1
  75. package/connection/handshake.js +97 -20
  76. package/connection/handshake.js.map +1 -1
  77. package/connection/index.d.ts +3 -6
  78. package/connection/index.d.ts.map +1 -1
  79. package/connection/index.js +1 -2
  80. package/connection/index.js.map +1 -1
  81. package/connection/liveness.d.ts +14 -0
  82. package/connection/liveness.d.ts.map +1 -0
  83. package/connection/liveness.js +30 -0
  84. package/connection/liveness.js.map +1 -0
  85. package/connection/pool.d.ts +137 -58
  86. package/connection/pool.d.ts.map +1 -1
  87. package/connection/pool.js +281 -117
  88. package/connection/pool.js.map +1 -1
  89. package/connection/reload.d.ts +60 -12
  90. package/connection/reload.d.ts.map +1 -1
  91. package/connection/reload.js +859 -233
  92. package/connection/reload.js.map +1 -1
  93. package/connection/session.d.ts +91 -0
  94. package/connection/session.d.ts.map +1 -0
  95. package/connection/session.js +2 -0
  96. package/connection/session.js.map +1 -0
  97. package/connection/stats.d.ts +20 -11
  98. package/connection/stats.d.ts.map +1 -1
  99. package/connection/stats.js +64 -8
  100. package/connection/stats.js.map +1 -1
  101. package/connection/ticker.d.ts +4 -0
  102. package/connection/ticker.d.ts.map +1 -0
  103. package/connection/ticker.js +66 -0
  104. package/connection/ticker.js.map +1 -0
  105. package/connection/tokens.d.ts +2 -0
  106. package/connection/tokens.d.ts.map +1 -0
  107. package/connection/tokens.js +404 -0
  108. package/connection/tokens.js.map +1 -0
  109. package/connection/transport.d.ts.map +1 -1
  110. package/connection/transport.js +4 -4
  111. package/connection/transport.js.map +1 -1
  112. package/connection/withdrawal.d.ts +2 -0
  113. package/connection/withdrawal.d.ts.map +1 -0
  114. package/connection/withdrawal.js +23 -0
  115. package/connection/withdrawal.js.map +1 -0
  116. package/consume.js +213 -46
  117. package/consume.js.map +1 -1
  118. package/cover.d.ts +59 -0
  119. package/cover.d.ts.map +1 -0
  120. package/cover.js +375 -0
  121. package/cover.js.map +1 -0
  122. package/datagram.d.ts +70 -11
  123. package/datagram.d.ts.map +1 -1
  124. package/datagram.js +317 -1
  125. package/datagram.js.map +1 -1
  126. package/datagram_stream.d.ts +18 -0
  127. package/datagram_stream.d.ts.map +1 -0
  128. package/datagram_stream.js +67 -0
  129. package/datagram_stream.js.map +1 -0
  130. package/defined.test-util.d.ts +2 -0
  131. package/defined.test-util.d.ts.map +1 -0
  132. package/defined.test-util.js +7 -0
  133. package/defined.test-util.js.map +1 -0
  134. package/delivered.d.ts +75 -0
  135. package/delivered.d.ts.map +1 -0
  136. package/delivered.js +228 -0
  137. package/delivered.js.map +1 -0
  138. package/demand.d.ts +173 -0
  139. package/demand.d.ts.map +1 -0
  140. package/demand.js +582 -0
  141. package/demand.js.map +1 -0
  142. package/drops.d.ts +93 -0
  143. package/drops.d.ts.map +1 -0
  144. package/drops.js +283 -0
  145. package/drops.js.map +1 -0
  146. package/error.d.ts +120 -21
  147. package/error.d.ts.map +1 -1
  148. package/error.js +230 -37
  149. package/error.js.map +1 -1
  150. package/errors.d.ts +2 -2
  151. package/errors.d.ts.map +1 -1
  152. package/errors.js +2 -2
  153. package/errors.js.map +1 -1
  154. package/fill.d.ts +174 -0
  155. package/fill.d.ts.map +1 -0
  156. package/fill.js +760 -0
  157. package/fill.js.map +1 -0
  158. package/flate/decoder.d.ts +2 -0
  159. package/flate/decoder.d.ts.map +1 -0
  160. package/flate/decoder.js +63 -0
  161. package/flate/decoder.js.map +1 -0
  162. package/flate/encoder.d.ts +2 -0
  163. package/flate/encoder.d.ts.map +1 -0
  164. package/flate/encoder.js +64 -0
  165. package/flate/encoder.js.map +1 -0
  166. package/flate/group.d.ts +34 -0
  167. package/flate/group.d.ts.map +1 -0
  168. package/flate/group.js +140 -0
  169. package/flate/group.js.map +1 -0
  170. package/frame.d.ts +80 -0
  171. package/frame.d.ts.map +1 -0
  172. package/frame.js +296 -0
  173. package/frame.js.map +1 -0
  174. package/frame.test-util.d.ts +5 -0
  175. package/frame.test-util.d.ts.map +1 -0
  176. package/frame.test-util.js +9 -0
  177. package/frame.test-util.js.map +1 -0
  178. package/group.d.ts +108 -45
  179. package/group.d.ts.map +1 -1
  180. package/group.js +1616 -307
  181. package/group.js.map +1 -1
  182. package/group_api.d.ts +7 -0
  183. package/group_api.d.ts.map +1 -0
  184. package/group_api.js +7 -0
  185. package/group_api.js.map +1 -0
  186. package/hop.d.ts +62 -76
  187. package/hop.d.ts.map +1 -1
  188. package/hop.js +92 -64
  189. package/hop.js.map +1 -1
  190. package/identity.d.ts +39 -0
  191. package/identity.d.ts.map +1 -0
  192. package/identity.js +47 -0
  193. package/identity.js.map +1 -0
  194. package/ietf/active_count.d.ts +2 -0
  195. package/ietf/active_count.d.ts.map +1 -0
  196. package/ietf/active_count.js +30 -0
  197. package/ietf/active_count.js.map +1 -0
  198. package/ietf/aliases.js +69 -35
  199. package/ietf/aliases.js.map +1 -1
  200. package/ietf/cluster.d.ts +3 -3
  201. package/ietf/cluster.js +14 -62
  202. package/ietf/cluster.js.map +1 -1
  203. package/ietf/connection.d.ts +1 -31
  204. package/ietf/connection.d.ts.map +1 -1
  205. package/ietf/connection.js +305 -143
  206. package/ietf/connection.js.map +1 -1
  207. package/ietf/datagram.d.ts +60 -0
  208. package/ietf/datagram.d.ts.map +1 -0
  209. package/ietf/datagram.js +152 -0
  210. package/ietf/datagram.js.map +1 -0
  211. package/ietf/error.d.ts +3 -3
  212. package/ietf/error.js +82 -100
  213. package/ietf/error.js.map +1 -1
  214. package/ietf/fetch.d.ts +91 -35
  215. package/ietf/fetch.d.ts.map +1 -1
  216. package/ietf/fetch.js +172 -93
  217. package/ietf/fetch.js.map +1 -1
  218. package/ietf/filter.d.ts +20 -30
  219. package/ietf/filter.d.ts.map +1 -1
  220. package/ietf/filter.js +140 -171
  221. package/ietf/filter.js.map +1 -1
  222. package/ietf/goaway.d.ts +11 -9
  223. package/ietf/goaway.d.ts.map +1 -1
  224. package/ietf/goaway.js +22 -35
  225. package/ietf/goaway.js.map +1 -1
  226. package/ietf/hidden.js +1 -1
  227. package/ietf/hidden.js.map +1 -1
  228. package/ietf/index.d.ts +2 -14
  229. package/ietf/index.d.ts.map +1 -1
  230. package/ietf/index.js +4 -14
  231. package/ietf/index.js.map +1 -1
  232. package/ietf/join.d.ts +7 -0
  233. package/ietf/join.d.ts.map +1 -0
  234. package/ietf/join.js +234 -0
  235. package/ietf/join.js.map +1 -0
  236. package/ietf/message.d.ts +18 -2
  237. package/ietf/message.d.ts.map +1 -1
  238. package/ietf/message.js +60 -40
  239. package/ietf/message.js.map +1 -1
  240. package/ietf/message.test-util.d.ts +6 -0
  241. package/ietf/message.test-util.d.ts.map +1 -0
  242. package/ietf/message.test-util.js +13 -0
  243. package/ietf/message.test-util.js.map +1 -0
  244. package/ietf/namespace.d.ts +17 -3
  245. package/ietf/namespace.d.ts.map +1 -1
  246. package/ietf/namespace.js +78 -26
  247. package/ietf/namespace.js.map +1 -1
  248. package/ietf/object.d.ts +194 -32
  249. package/ietf/object.d.ts.map +1 -1
  250. package/ietf/object.js +405 -178
  251. package/ietf/object.js.map +1 -1
  252. package/ietf/parameters.d.ts +22 -42
  253. package/ietf/parameters.d.ts.map +1 -1
  254. package/ietf/parameters.js +260 -472
  255. package/ietf/parameters.js.map +1 -1
  256. package/ietf/priority.d.ts +17 -2
  257. package/ietf/priority.d.ts.map +1 -1
  258. package/ietf/priority.js +19 -4
  259. package/ietf/priority.js.map +1 -1
  260. package/ietf/properties.d.ts +5 -4
  261. package/ietf/properties.d.ts.map +1 -1
  262. package/ietf/properties.js +78 -48
  263. package/ietf/properties.js.map +1 -1
  264. package/ietf/publish.d.ts +22 -30
  265. package/ietf/publish.d.ts.map +1 -1
  266. package/ietf/publish.js +45 -139
  267. package/ietf/publish.js.map +1 -1
  268. package/ietf/publish_namespace.d.ts +46 -69
  269. package/ietf/publish_namespace.d.ts.map +1 -1
  270. package/ietf/publish_namespace.js +66 -184
  271. package/ietf/publish_namespace.js.map +1 -1
  272. package/ietf/publisher.js +1800 -600
  273. package/ietf/publisher.js.map +1 -1
  274. package/ietf/range.d.ts +2 -0
  275. package/ietf/range.d.ts.map +1 -0
  276. package/ietf/range.js +222 -0
  277. package/ietf/range.js.map +1 -0
  278. package/ietf/request.d.ts +3 -27
  279. package/ietf/request.d.ts.map +1 -1
  280. package/ietf/request.js +41 -78
  281. package/ietf/request.js.map +1 -1
  282. package/ietf/request_stream.d.ts +1 -6
  283. package/ietf/request_stream.d.ts.map +1 -1
  284. package/ietf/request_stream.js +306 -16
  285. package/ietf/request_stream.js.map +1 -1
  286. package/ietf/session.d.ts +41 -0
  287. package/ietf/session.d.ts.map +1 -0
  288. package/ietf/session.js +65 -0
  289. package/ietf/session.js.map +1 -0
  290. package/ietf/setup.d.ts +4 -29
  291. package/ietf/setup.d.ts.map +1 -1
  292. package/ietf/setup.js +6 -117
  293. package/ietf/setup.js.map +1 -1
  294. package/ietf/setup_options.d.ts +31 -0
  295. package/ietf/setup_options.d.ts.map +1 -0
  296. package/ietf/setup_options.js +138 -0
  297. package/ietf/setup_options.js.map +1 -0
  298. package/ietf/solicit.js +4 -14
  299. package/ietf/solicit.js.map +1 -1
  300. package/ietf/subscribe.d.ts +48 -41
  301. package/ietf/subscribe.d.ts.map +1 -1
  302. package/ietf/subscribe.js +118 -239
  303. package/ietf/subscribe.js.map +1 -1
  304. package/ietf/subscribe_namespace.d.ts +13 -76
  305. package/ietf/subscribe_namespace.d.ts.map +1 -1
  306. package/ietf/subscribe_namespace.js +30 -201
  307. package/ietf/subscribe_namespace.js.map +1 -1
  308. package/ietf/subscriber.js +1839 -536
  309. package/ietf/subscriber.js.map +1 -1
  310. package/ietf/token.js +105 -27
  311. package/ietf/token.js.map +1 -1
  312. package/ietf/track.d.ts +25 -22
  313. package/ietf/track.d.ts.map +1 -1
  314. package/ietf/track.js +48 -61
  315. package/ietf/track.js.map +1 -1
  316. package/ietf/version.d.ts +13 -42
  317. package/ietf/version.d.ts.map +1 -1
  318. package/ietf/version.js +22 -48
  319. package/ietf/version.js.map +1 -1
  320. package/implementation.d.ts +2 -0
  321. package/implementation.d.ts.map +1 -0
  322. package/implementation.js +5 -0
  323. package/implementation.js.map +1 -0
  324. package/index.d.ts +25 -10
  325. package/index.d.ts.map +1 -1
  326. package/index.js +19 -6
  327. package/index.js.map +1 -1
  328. package/internal.d.ts +1512 -64
  329. package/internal.d.ts.map +1 -1
  330. package/internal.js +557 -79
  331. package/internal.js.map +1 -1
  332. package/ledger.d.ts +23 -0
  333. package/ledger.d.ts.map +1 -0
  334. package/ledger.js +247 -0
  335. package/ledger.js.map +1 -0
  336. package/listing.d.ts +132 -0
  337. package/listing.d.ts.map +1 -0
  338. package/listing.js +486 -0
  339. package/listing.js.map +1 -0
  340. package/lite/announce.d.ts +124 -63
  341. package/lite/announce.d.ts.map +1 -1
  342. package/lite/announce.js +384 -158
  343. package/lite/announce.js.map +1 -1
  344. package/lite/announce_stream.d.ts +2 -0
  345. package/lite/announce_stream.d.ts.map +1 -0
  346. package/lite/announce_stream.js +92 -0
  347. package/lite/announce_stream.js.map +1 -0
  348. package/lite/answer.d.ts +22 -0
  349. package/lite/answer.d.ts.map +1 -0
  350. package/lite/answer.js +301 -0
  351. package/lite/answer.js.map +1 -0
  352. package/lite/auth.d.ts +45 -0
  353. package/lite/auth.d.ts.map +1 -0
  354. package/lite/auth.js +204 -0
  355. package/lite/auth.js.map +1 -0
  356. package/lite/compat.d.ts +31 -0
  357. package/lite/compat.d.ts.map +1 -0
  358. package/lite/compat.js +86 -0
  359. package/lite/compat.js.map +1 -0
  360. package/lite/connection.d.ts +1 -45
  361. package/lite/connection.d.ts.map +1 -1
  362. package/lite/connection.js +360 -77
  363. package/lite/connection.js.map +1 -1
  364. package/lite/datagram-end.d.ts +26 -0
  365. package/lite/datagram-end.d.ts.map +1 -0
  366. package/lite/datagram-end.js +30 -0
  367. package/lite/datagram-end.js.map +1 -0
  368. package/lite/datagram.d.ts +73 -8
  369. package/lite/datagram.d.ts.map +1 -1
  370. package/lite/datagram.js +144 -9
  371. package/lite/datagram.js.map +1 -1
  372. package/lite/duration.d.ts +6 -0
  373. package/lite/duration.d.ts.map +1 -0
  374. package/lite/duration.js +14 -0
  375. package/lite/duration.js.map +1 -0
  376. package/lite/error.d.ts +9 -0
  377. package/lite/error.d.ts.map +1 -0
  378. package/lite/error.js +24 -0
  379. package/lite/error.js.map +1 -0
  380. package/lite/fills.d.ts +162 -0
  381. package/lite/fills.d.ts.map +1 -0
  382. package/lite/fills.js +486 -0
  383. package/lite/fills.js.map +1 -0
  384. package/lite/fit.d.ts +49 -0
  385. package/lite/fit.d.ts.map +1 -0
  386. package/lite/fit.js +305 -0
  387. package/lite/fit.js.map +1 -0
  388. package/lite/goaway.d.ts +1 -1
  389. package/lite/goaway.d.ts.map +1 -1
  390. package/lite/goaway.js +5 -13
  391. package/lite/goaway.js.map +1 -1
  392. package/lite/group.d.ts +64 -13
  393. package/lite/group.d.ts.map +1 -1
  394. package/lite/group.js +108 -30
  395. package/lite/group.js.map +1 -1
  396. package/lite/identity.d.ts +25 -0
  397. package/lite/identity.d.ts.map +1 -0
  398. package/lite/identity.js +79 -0
  399. package/lite/identity.js.map +1 -0
  400. package/lite/index.d.ts +2 -1
  401. package/lite/index.d.ts.map +1 -1
  402. package/lite/index.js +2 -1
  403. package/lite/index.js.map +1 -1
  404. package/lite/message.d.ts +9 -1
  405. package/lite/message.d.ts.map +1 -1
  406. package/lite/message.js +28 -44
  407. package/lite/message.js.map +1 -1
  408. package/lite/priority.d.ts +9 -2
  409. package/lite/priority.d.ts.map +1 -1
  410. package/lite/priority.js +11 -105
  411. package/lite/priority.js.map +1 -1
  412. package/lite/probe.d.ts.map +1 -1
  413. package/lite/probe.js +9 -0
  414. package/lite/probe.js.map +1 -1
  415. package/lite/publisher.d.ts +10 -1
  416. package/lite/publisher.d.ts.map +1 -1
  417. package/lite/publisher.js +2548 -437
  418. package/lite/publisher.js.map +1 -1
  419. package/lite/routed.d.ts +59 -0
  420. package/lite/routed.d.ts.map +1 -0
  421. package/lite/routed.js +564 -0
  422. package/lite/routed.js.map +1 -0
  423. package/lite/session.d.ts +1 -27
  424. package/lite/session.d.ts.map +1 -1
  425. package/lite/session.js +0 -99
  426. package/lite/session.js.map +1 -1
  427. package/lite/setup.d.ts +29 -2
  428. package/lite/setup.d.ts.map +1 -1
  429. package/lite/setup.js +95 -21
  430. package/lite/setup.js.map +1 -1
  431. package/lite/stream.d.ts +5 -11
  432. package/lite/stream.d.ts.map +1 -1
  433. package/lite/stream.js +5 -1
  434. package/lite/stream.js.map +1 -1
  435. package/lite/subscribe.d.ts +475 -75
  436. package/lite/subscribe.d.ts.map +1 -1
  437. package/lite/subscribe.js +1373 -142
  438. package/lite/subscribe.js.map +1 -1
  439. package/lite/subscriber.d.ts +7 -44
  440. package/lite/subscriber.d.ts.map +1 -1
  441. package/lite/subscriber.js +3290 -515
  442. package/lite/subscriber.js.map +1 -1
  443. package/lite/track.d.ts +24 -9
  444. package/lite/track.d.ts.map +1 -1
  445. package/lite/track.js +116 -37
  446. package/lite/track.js.map +1 -1
  447. package/lite/tracks.d.ts +55 -0
  448. package/lite/tracks.d.ts.map +1 -0
  449. package/lite/tracks.js +114 -0
  450. package/lite/tracks.js.map +1 -0
  451. package/lite/version.d.ts +132 -24
  452. package/lite/version.d.ts.map +1 -1
  453. package/lite/version.js +378 -33
  454. package/lite/version.js.map +1 -1
  455. package/meta.d.ts +77 -0
  456. package/meta.d.ts.map +1 -0
  457. package/meta.js +130 -0
  458. package/meta.js.map +1 -0
  459. package/meta_api.d.ts +7 -0
  460. package/meta_api.d.ts.map +1 -0
  461. package/meta_api.js +7 -0
  462. package/meta_api.js.map +1 -0
  463. package/mint.d.ts +28 -0
  464. package/mint.d.ts.map +1 -0
  465. package/mint.js +66 -0
  466. package/mint.js.map +1 -0
  467. package/open.d.ts +63 -0
  468. package/open.d.ts.map +1 -0
  469. package/open.js +250 -0
  470. package/open.js.map +1 -0
  471. package/origin.d.ts +392 -140
  472. package/origin.d.ts.map +1 -1
  473. package/origin.js +2533 -686
  474. package/origin.js.map +1 -1
  475. package/package.json +8 -13
  476. package/path.d.ts +51 -51
  477. package/path.d.ts.map +1 -1
  478. package/path.js +210 -57
  479. package/path.js.map +1 -1
  480. package/payload.d.ts +33 -0
  481. package/payload.d.ts.map +1 -0
  482. package/payload.js +65 -0
  483. package/payload.js.map +1 -0
  484. package/prefix.d.ts +152 -0
  485. package/prefix.d.ts.map +1 -0
  486. package/prefix.js +349 -0
  487. package/prefix.js.map +1 -0
  488. package/priority.d.ts +8 -0
  489. package/priority.d.ts.map +1 -0
  490. package/priority.js +185 -0
  491. package/priority.js.map +1 -0
  492. package/queue.d.ts +2 -0
  493. package/queue.d.ts.map +1 -0
  494. package/queue.js +46 -0
  495. package/queue.js.map +1 -0
  496. package/ranges.d.ts +36 -0
  497. package/ranges.d.ts.map +1 -0
  498. package/ranges.js +312 -0
  499. package/ranges.js.map +1 -0
  500. package/rescale.d.ts +24 -0
  501. package/rescale.d.ts.map +1 -0
  502. package/rescale.js +39 -0
  503. package/rescale.js.map +1 -0
  504. package/resume.d.ts +55 -0
  505. package/resume.d.ts.map +1 -0
  506. package/resume.js +4173 -0
  507. package/resume.js.map +1 -0
  508. package/resume.test-util.d.ts +2 -0
  509. package/resume.test-util.d.ts.map +1 -0
  510. package/resume.test-util.js +25 -0
  511. package/resume.test-util.js.map +1 -0
  512. package/skips.d.ts +48 -0
  513. package/skips.d.ts.map +1 -0
  514. package/skips.js +182 -0
  515. package/skips.js.map +1 -0
  516. package/stream.d.ts +75 -37
  517. package/stream.d.ts.map +1 -1
  518. package/stream.js +236 -92
  519. package/stream.js.map +1 -1
  520. package/subscription.test-util.d.ts +2 -0
  521. package/subscription.test-util.d.ts.map +1 -0
  522. package/subscription.test-util.js +49 -0
  523. package/subscription.test-util.js.map +1 -0
  524. package/tail.d.ts +23 -14
  525. package/tail.d.ts.map +1 -1
  526. package/tail.js +224 -97
  527. package/tail.js.map +1 -1
  528. package/time.d.ts +41 -102
  529. package/time.d.ts.map +1 -1
  530. package/time.js +129 -91
  531. package/time.js.map +1 -1
  532. package/timed.d.ts +21 -0
  533. package/timed.d.ts.map +1 -0
  534. package/timed.js +2 -0
  535. package/timed.js.map +1 -0
  536. package/track.d.ts +577 -245
  537. package/track.d.ts.map +1 -1
  538. package/track.js +7306 -771
  539. package/track.js.map +1 -1
  540. package/track_api.d.ts +7 -0
  541. package/track_api.d.ts.map +1 -0
  542. package/track_api.js +7 -0
  543. package/track_api.js.map +1 -0
  544. package/util/abort.d.ts +1 -0
  545. package/util/abort.d.ts.map +1 -1
  546. package/util/abort.js +23 -1
  547. package/util/abort.js.map +1 -1
  548. package/util/announce.d.ts +4 -0
  549. package/util/announce.d.ts.map +1 -0
  550. package/util/announce.js +11 -0
  551. package/util/announce.js.map +1 -0
  552. package/util/bigint.d.ts +3 -0
  553. package/util/bigint.d.ts.map +1 -0
  554. package/util/bigint.js +8 -0
  555. package/util/bigint.js.map +1 -0
  556. package/util/change.d.ts +12 -0
  557. package/util/change.d.ts.map +1 -0
  558. package/util/change.js +18 -0
  559. package/util/change.js.map +1 -0
  560. package/util/deadline.d.ts +2 -0
  561. package/util/deadline.d.ts.map +1 -0
  562. package/util/deadline.js +108 -0
  563. package/util/deadline.js.map +1 -0
  564. package/util/decode_error.d.ts +3 -0
  565. package/util/decode_error.d.ts.map +1 -0
  566. package/util/decode_error.js +11 -0
  567. package/util/decode_error.js.map +1 -0
  568. package/util/demand.d.ts +8 -0
  569. package/util/demand.d.ts.map +1 -0
  570. package/util/demand.js +24 -0
  571. package/util/demand.js.map +1 -0
  572. package/util/given_up.d.ts +2 -0
  573. package/util/given_up.d.ts.map +1 -0
  574. package/util/given_up.js +101 -0
  575. package/util/given_up.js.map +1 -0
  576. package/util/hex.d.ts +6 -0
  577. package/util/hex.d.ts.map +1 -1
  578. package/util/hex.js +19 -4
  579. package/util/hex.js.map +1 -1
  580. package/util/key.d.ts +15 -0
  581. package/util/key.d.ts.map +1 -0
  582. package/util/key.js +38 -0
  583. package/util/key.js.map +1 -0
  584. package/util/limits.d.ts +2 -0
  585. package/util/limits.d.ts.map +1 -0
  586. package/util/limits.js +76 -0
  587. package/util/limits.js.map +1 -0
  588. package/util/observed.d.ts +23 -0
  589. package/util/observed.d.ts.map +1 -0
  590. package/util/observed.js +122 -0
  591. package/util/observed.js.map +1 -0
  592. package/util/sorted.d.ts +2 -0
  593. package/util/sorted.d.ts.map +1 -0
  594. package/util/sorted.js +149 -0
  595. package/util/sorted.js.map +1 -0
  596. package/util/timeout.d.ts +2 -1
  597. package/util/timeout.d.ts.map +1 -1
  598. package/util/timeout.js +13 -9
  599. package/util/timeout.js.map +1 -1
  600. package/util/timer.d.ts +2 -0
  601. package/util/timer.d.ts.map +1 -0
  602. package/util/timer.js +19 -0
  603. package/util/timer.js.map +1 -0
  604. package/util/utf8.d.ts +2 -0
  605. package/util/utf8.d.ts.map +1 -1
  606. package/util/utf8.js +7 -1
  607. package/util/utf8.js.map +1 -1
  608. package/util/varint.d.ts.map +1 -1
  609. package/util/varint.js +8 -1
  610. package/util/varint.js.map +1 -1
  611. package/util/watch.d.ts +8 -0
  612. package/util/watch.d.ts.map +1 -0
  613. package/util/watch.js +38 -0
  614. package/util/watch.js.map +1 -0
  615. package/util/yield.d.ts +2 -0
  616. package/util/yield.d.ts.map +1 -0
  617. package/util/yield.js +31 -0
  618. package/util/yield.js.map +1 -0
  619. package/varint.d.ts +0 -29
  620. package/varint.d.ts.map +1 -1
  621. package/varint.js +7 -28
  622. package/varint.js.map +1 -1
  623. package/varint_api.d.ts +7 -0
  624. package/varint_api.d.ts.map +1 -0
  625. package/varint_api.js +7 -0
  626. package/varint_api.js.map +1 -0
  627. package/version.d.ts +2 -0
  628. package/version.d.ts.map +1 -0
  629. package/version.js +4 -0
  630. package/version.js.map +1 -0
  631. package/window.d.ts +570 -0
  632. package/window.d.ts.map +1 -0
  633. package/window.js +1677 -0
  634. package/window.js.map +1 -0
  635. package/wire.d.ts +145 -30
  636. package/wire.d.ts.map +1 -1
  637. package/wire.js +27 -0
  638. package/wire.js.map +1 -1
  639. package/connection/established.d.ts +0 -53
  640. package/connection/established.d.ts.map +0 -1
  641. package/connection/established.js +0 -2
  642. package/connection/established.js.map +0 -1
  643. package/ietf/adapter.d.ts +0 -75
  644. package/ietf/adapter.d.ts.map +0 -1
  645. package/ietf/adapter.js +0 -699
  646. package/ietf/adapter.js.map +0 -1
  647. package/lite/datagram_stream.d.ts +0 -8
  648. package/lite/datagram_stream.d.ts.map +0 -1
  649. package/lite/datagram_stream.js +0 -43
  650. package/lite/datagram_stream.js.map +0 -1
  651. package/lite/fetch.d.ts +0 -28
  652. package/lite/fetch.d.ts.map +0 -1
  653. package/lite/fetch.js +0 -78
  654. package/lite/fetch.js.map +0 -1
  655. package/util/log.d.ts +0 -5
  656. package/util/log.d.ts.map +0 -1
  657. package/util/log.js +0 -16
  658. package/util/log.js.map +0 -1
  659. package/zod.d.ts +0 -13
  660. package/zod.d.ts.map +0 -1
  661. package/zod.js +0 -18
  662. package/zod.js.map +0 -1
package/window.js ADDED
@@ -0,0 +1,1677 @@
1
+ /**
2
+ * A subscriber's own ranges, resolved to the group starts it reads, and the helpers that
3
+ * translate ranges between the model and the wire. Mirrors the Rust `subscription` module.
4
+ *
5
+ * The aggregate a publisher sees is the union of every subscriber's ranges, so it says what
6
+ * arrives, not what one subscriber asked for. A {@link Window} is the per-subscriber side:
7
+ * resolved at the hop serving the subscriber, it filters that subscriber's reads.
8
+ *
9
+ * @module
10
+ */
11
+ import { Duration } from "@moq/clock";
12
+ import { Counts, Cover } from "./cover.js";
13
+ import { NO_MAX_DELAY, runsFrom, wireMillis } from "./internal.js";
14
+ import { max, min, UNBOUNDED } from "./util/key.js";
15
+ import { Sorted } from "./util/sorted.js";
16
+ /** Check one end's shape, naming `field` in the error. */
17
+ function timeOf(field, value) {
18
+ if (value === undefined)
19
+ return undefined;
20
+ if (typeof value === "bigint" && value >= 0n)
21
+ return value;
22
+ throw new TypeError(`${field} must be a time, a non-negative bigint`);
23
+ }
24
+ /**
25
+ * Check one start's shape: a time, or a live join, its lag and max each rounded up to a whole
26
+ * millisecond or null for no limit, an omitted max as zero.
27
+ */
28
+ function checkStart(value) {
29
+ if (typeof value === "object" && value !== null && "live" in value) {
30
+ const live = value.live;
31
+ if (typeof live !== "object" || live === null)
32
+ throw new TypeError("a live range start must be { min, max }");
33
+ const { min, max } = live;
34
+ if (min !== null && !Duration.is(min))
35
+ throw new TypeError("a live range start's min must be a Duration or null");
36
+ if (max !== undefined && max !== null && !Duration.is(max))
37
+ throw new TypeError("a live range start's max must be a Duration or null");
38
+ return {
39
+ live: {
40
+ min: min === null ? null : wireMillis("live range start min", min),
41
+ max: max === null ? null : wireMillis("live range start max", max ?? Duration.ZERO),
42
+ },
43
+ };
44
+ }
45
+ return timeOf("range start", value);
46
+ }
47
+ /**
48
+ * Check and copy a caller's ranges, refusing a malformed one rather than serving it widened. A
49
+ * default priority or order is left out, so equal ranges compare equal however they were written.
50
+ */
51
+ export function rangesOf(ranges) {
52
+ if (!Array.isArray(ranges))
53
+ throw new TypeError("ranges must be an array");
54
+ return ranges.map((range) => {
55
+ if (typeof range !== "object" || range === null)
56
+ throw new TypeError("a range must be an object");
57
+ const out = {};
58
+ const start = checkStart(range.start);
59
+ const end = timeOf("range end", range.end);
60
+ if (start !== undefined)
61
+ out.start = start;
62
+ if (end !== undefined)
63
+ out.end = end;
64
+ return withPolicy(out, { priority: priorityOf(range.priority), order: orderOf(range.order) });
65
+ });
66
+ }
67
+ // A plain JS caller can pass anything, and silently serving a typo at the default would hide it.
68
+ function priorityOf(value) {
69
+ if (value === undefined)
70
+ return 0;
71
+ if (!Number.isInteger(value) || value < -128 || value > 127)
72
+ throw new RangeError(`priority must be an integer in -128..=127: ${value}`);
73
+ return value;
74
+ }
75
+ function orderOf(order) {
76
+ if (order === undefined)
77
+ return "descending";
78
+ if (order !== "ascending" && order !== "descending")
79
+ throw new TypeError(`unknown order: ${String(order)}`);
80
+ return order;
81
+ }
82
+ /**
83
+ * The default subscription, its one range at `priority`: how a datagram track is subscribed, whose
84
+ * only knob is a priority.
85
+ */
86
+ export function latestAt(priority) {
87
+ return {
88
+ ranges: [
89
+ withPolicy({ start: { live: { min: Duration.ZERO, max: Duration.ZERO } } }, { priority, order: "descending" }),
90
+ ],
91
+ };
92
+ }
93
+ /** The policy of a range that names none. */
94
+ export const DEFAULT_POLICY = Object.freeze({ priority: 0, order: "descending" });
95
+ /** The policy `range` asks under. */
96
+ export function policyOf(range) {
97
+ return { priority: range.priority ?? 0, order: range.order ?? "descending" };
98
+ }
99
+ /** `range` under `policy`, which it leaves out where it is the default. */
100
+ export function withPolicy(range, policy) {
101
+ const { priority: _, order: __, ...out } = range;
102
+ const ranged = out;
103
+ if (policy.priority !== 0)
104
+ ranged.priority = policy.priority;
105
+ if (policy.order === "ascending")
106
+ ranged.order = "ascending";
107
+ return ranged;
108
+ }
109
+ /**
110
+ * A number ranking how urgently a policy sends: the priority, then newest first, which wins a tie.
111
+ * Ranges at one rank cover a range at an equal or lower one, so it adds nothing beside them.
112
+ * Mirrors the Rust `Policy::rank`, reversed.
113
+ */
114
+ export function policyRank(policy) {
115
+ return policy.priority * 2 + (policy.order === "descending" ? 1 : 0);
116
+ }
117
+ /** The policy {@link policyRank} ranks `rank`. */
118
+ export function rankPolicy(rank) {
119
+ const descending = ((rank % 2) + 2) % 2 === 1;
120
+ return { priority: (rank - (descending ? 1 : 0)) / 2, order: descending ? "descending" : "ascending" };
121
+ }
122
+ /**
123
+ * What `ranges` ask as a whole, as a wire without a policy per range carries it: the highest
124
+ * priority, newest first if any range at that priority is. The default without any. Mirrors the
125
+ * Rust `Policy::of`.
126
+ */
127
+ export function rangesPolicy(ranges) {
128
+ let top;
129
+ for (const range of ranges) {
130
+ const rank = policyRank(policyOf(range));
131
+ if (top === undefined || rank > top)
132
+ top = rank;
133
+ }
134
+ return top === undefined ? { ...DEFAULT_POLICY } : rankPolicy(top);
135
+ }
136
+ /** The time `range` starts from, undefined when it doesn't name one. Mirrors the Rust `Range::time`. */
137
+ export function rangeTime(range) {
138
+ return typeof range.start === "bigint" ? range.start : undefined;
139
+ }
140
+ /**
141
+ * The lag of the live join `range` starts with, in whole milliseconds rounded up as the wire
142
+ * carries it, null for no lag limit, and undefined for a range that doesn't join live. Mirrors
143
+ * the Rust `live_millis`.
144
+ */
145
+ export function liveMillis(range) {
146
+ if (typeof range.start !== "object")
147
+ return undefined;
148
+ const { min } = range.start.live;
149
+ return min && Math.ceil(min.asMillis());
150
+ }
151
+ /**
152
+ * A live join's max as a budget in milliseconds, rounded up: {@link NO_MAX_DELAY} for none, and
153
+ * zero when omitted. Mirrors the Rust `budget`.
154
+ */
155
+ function budgetOf(max) {
156
+ if (max === null)
157
+ return NO_MAX_DELAY;
158
+ return max === undefined ? 0 : Math.min(Math.ceil(max.asMillis()), NO_MAX_DELAY);
159
+ }
160
+ /** A budget in milliseconds as a live join's max: {@link NO_MAX_DELAY} is no limit. Mirrors the Rust `max_of`. */
161
+ export function maxOf(budget) {
162
+ return budget >= NO_MAX_DELAY ? null : Duration.millis(budget);
163
+ }
164
+ /**
165
+ * The staleness budget in milliseconds of the groups `range` covers: its max for a live join, and
166
+ * none ({@link NO_MAX_DELAY}) for history. Mirrors the Rust `Range::budget`.
167
+ */
168
+ export function rangeBudget(range) {
169
+ return typeof range.start === "object" ? budgetOf(range.start.live.max) : NO_MAX_DELAY;
170
+ }
171
+ /**
172
+ * The widest staleness budget in milliseconds any of `ranges` gives a group: none while a history
173
+ * range is asked for, and otherwise the largest live join's max, zero with no range. Mirrors the
174
+ * Rust `Subscription::widest`.
175
+ */
176
+ export function widest(ranges) {
177
+ let widest = 0;
178
+ for (const range of ranges)
179
+ if (!isEmpty(range))
180
+ widest = Math.max(widest, rangeBudget(range));
181
+ return widest;
182
+ }
183
+ /**
184
+ * The live join `range` asks, as its lag in milliseconds and its end, as one key: the key a join
185
+ * is placed and answered under, which a change of max alone keeps. Undefined for history. Mirrors
186
+ * the Rust `Range::join`.
187
+ */
188
+ export function joinOf(range) {
189
+ const lag = liveMillis(range);
190
+ return lag === undefined ? undefined : `${lag}:${range.end ?? ""}`;
191
+ }
192
+ /**
193
+ * The range from group `start` up to the exclusive group `end`, or onward for undefined: what a
194
+ * wire's group bounds become. Undefined when it holds nothing, which an exclusive end of 0 says
195
+ * whatever the start. Mirrors the Rust `Range::span`.
196
+ */
197
+ export function span(start, end) {
198
+ if (end === 0n)
199
+ return undefined;
200
+ const range = {};
201
+ if (start !== undefined)
202
+ range.start = start;
203
+ if (end !== undefined)
204
+ range.end = end - 1n;
205
+ return isEmpty(range) ? undefined : range;
206
+ }
207
+ /** Whether `range` certainly delivers nothing: it ends before a time start. */
208
+ export function isEmpty(range) {
209
+ const time = rangeTime(range);
210
+ return time !== undefined && range.end !== undefined && range.end < time;
211
+ }
212
+ /**
213
+ * A canonical order, as the Rust `Range` sorts: from the oldest group first, then by time, then
214
+ * live joins by lag, then by end, open first.
215
+ */
216
+ export function compare(a, b) {
217
+ const rank = (range) => {
218
+ const live = liveMillis(range);
219
+ // A lag or max of none sorts first, as Rust's `Option` does.
220
+ if (live !== undefined)
221
+ return [2, BigInt(live ?? -1), BigInt(liveMax(range))];
222
+ const time = rangeTime(range);
223
+ return time === undefined ? [0, 0n, 0n] : [1, time, 0n];
224
+ };
225
+ const [ra, va, ma] = rank(a);
226
+ const [rb, vb, mb] = rank(b);
227
+ return (ra - rb ||
228
+ order(va, vb) ||
229
+ order(ma, mb) ||
230
+ order(a.end ?? -1n, b.end ?? -1n) ||
231
+ policyRank(policyOf(b)) - policyRank(policyOf(a)));
232
+ }
233
+ // A live join's max as a sort key: -1 for none, so it sorts first, else its milliseconds.
234
+ function liveMax(range) {
235
+ const budget = rangeBudget(range);
236
+ return budget >= NO_MAX_DELAY ? -1 : budget;
237
+ }
238
+ function order(a, b) {
239
+ return a < b ? -1 : a > b ? 1 : 0;
240
+ }
241
+ /** Whether two lists hold the same ranges in the same order. */
242
+ export function sameRanges(a, b) {
243
+ return a.length === b.length && a.every((range, index) => rangeKey(range) === rangeKey(b[index]));
244
+ }
245
+ /**
246
+ * Whether two lists ask for the same groups range by range, whatever their policies: a change of
247
+ * policy alone resolves nothing again.
248
+ */
249
+ export function sameSpans(a, b) {
250
+ return a.length === b.length && a.every((range, index) => spanKey(range) === spanKey(b[index]));
251
+ }
252
+ /** A range as a string, equal exactly when the ranges are; JSON can't hold a bigint. */
253
+ export function rangeKey(range) {
254
+ return `${spanKey(range)}@${range.priority ?? 0}${range.order === "ascending" ? "a" : "d"}`;
255
+ }
256
+ /** {@link rangeKey} without the range's policy: equal when two ranges ask for the same groups. */
257
+ function spanKey(range) {
258
+ const live = liveMillis(range);
259
+ const start = live === undefined ? (rangeTime(range) ?? "") : `L${live}/${liveMax(range)}`;
260
+ return `${start}..${range.end ?? ""}`;
261
+ }
262
+ /**
263
+ * Whether `next` keeps where `old` resolved: the same groups under another policy, or the same
264
+ * live join under another max. Mirrors the Rust `Range::retunes`.
265
+ */
266
+ export function retunes(old, next) {
267
+ const join = joinOf(next);
268
+ return spanKey(old) === spanKey(next) || (join !== undefined && joinOf(old) === join);
269
+ }
270
+ /**
271
+ * The union of `ranges` in a canonical order, without empty or redundant ranges.
272
+ *
273
+ * Time spans merge where they overlap or touch, as the group starts they cover. The rest can't be
274
+ * placed without the live edge, so only an open range drops what it certainly covers: the open
275
+ * time span drops ranges starting inside it, and a range from the oldest group following the live
276
+ * edge covers everything. A live join reaches no further back than its lag, so it covers nothing
277
+ * else, and live joins of the same lag and end are one range under the largest max among them:
278
+ * where a join lands doesn't depend on it.
279
+ *
280
+ * Ranges fold so only within one policy. Across policies a range drops only once ranges at
281
+ * policies that {@link covers | cover} its own already ask for every group it does, since folding
282
+ * it into a range at another policy would lose its priority or order. Mirrors the Rust `union`.
283
+ */
284
+ export function union(ranges) {
285
+ const classes = new Map();
286
+ for (const range of ranges) {
287
+ if (isEmpty(range))
288
+ continue;
289
+ const rank = policyRank(policyOf(range));
290
+ const held = classes.get(rank);
291
+ const bare = withPolicy(range, DEFAULT_POLICY);
292
+ if (held)
293
+ held.push(bare);
294
+ else
295
+ classes.set(rank, [bare]);
296
+ }
297
+ const out = [];
298
+ // The ranges at every policy covering the next, each policy covering all that follow it.
299
+ const above = [];
300
+ for (const rank of [...classes.keys()].sort((a, b) => b - a)) {
301
+ const policy = rankPolicy(rank);
302
+ const spans = fold(classes.get(rank) ?? []);
303
+ const covered = fold(above);
304
+ for (const range of spans) {
305
+ if (!sameRanges(fold([...above, range]), covered))
306
+ out.push(withPolicy(range, policy));
307
+ }
308
+ above.push(...spans);
309
+ }
310
+ out.sort(compare);
311
+ return out;
312
+ }
313
+ // The union of `ranges`, all at one policy, as {@link union} folds them.
314
+ function fold(ranges) {
315
+ const kept = ranges.filter((range) => !isEmpty(range));
316
+ if (kept.some((range) => range.start === undefined && range.end === undefined))
317
+ return [{}];
318
+ const spans = [];
319
+ for (const range of kept) {
320
+ const time = rangeTime(range);
321
+ if (time === undefined)
322
+ continue;
323
+ spans.push({ start: time, end: range.end === undefined ? UNBOUNDED : range.end + 1n });
324
+ }
325
+ spans.sort((a, b) => order(a.start, b.start) || order(a.end, b.end));
326
+ const runs = [];
327
+ for (const span of spans) {
328
+ const last = runs.at(-1);
329
+ if (last && span.start <= last.end)
330
+ last.end = max(last.end, span.end);
331
+ else
332
+ runs.push({ ...span });
333
+ }
334
+ const out = runs.map((run) => run.end === UNBOUNDED ? { start: run.start } : { start: run.start, end: run.end - 1n });
335
+ // A range with no time start can't be placed against the time spans. One from the oldest group
336
+ // takes in every one from there ending before it.
337
+ const joins = new Map();
338
+ let oldest;
339
+ for (const range of kept) {
340
+ const join = joinOf(range);
341
+ if (join === undefined) {
342
+ if (rangeTime(range) === undefined && (oldest === undefined || (range.end ?? 0n) > (oldest.end ?? 0n)))
343
+ oldest = range;
344
+ continue;
345
+ }
346
+ const held = joins.get(join);
347
+ if (!held || rangeBudget(range) > rangeBudget(held))
348
+ joins.set(join, range);
349
+ }
350
+ if (oldest !== undefined)
351
+ out.push(oldest);
352
+ out.push(...joins.values());
353
+ out.sort(compare);
354
+ return out.filter((range, index) => index === 0 || rangeKey(range) !== rangeKey(out[index - 1]));
355
+ }
356
+ /**
357
+ * The lowest time any range starts from, or undefined when one names none: the floor a wire
358
+ * without ranges carries. Mirrors the Rust `Subscription::start`.
359
+ */
360
+ export function rangesStart(ranges) {
361
+ let floor;
362
+ for (const range of ranges) {
363
+ if (isEmpty(range))
364
+ continue;
365
+ const time = rangeTime(range);
366
+ if (time === undefined)
367
+ return undefined;
368
+ floor = min(floor ?? time, time);
369
+ }
370
+ return floor;
371
+ }
372
+ /**
373
+ * The exclusive end past every range, or undefined when one is open: the cap a wire without
374
+ * ranges carries. No range at all ends at group 0. Mirrors the Rust `Subscription::end`.
375
+ */
376
+ export function rangesEnd(ranges) {
377
+ let end = 0n;
378
+ for (const range of ranges) {
379
+ if (isEmpty(range))
380
+ continue;
381
+ if (range.end === undefined)
382
+ return undefined;
383
+ end = max(end, range.end + 1n);
384
+ }
385
+ return end;
386
+ }
387
+ /** The ranges a SUBSCRIBE carries for `ranges`: all but the empty ones. Mirrors the Rust `wire_ranges`. */
388
+ export function wireRanges(ranges) {
389
+ return ranges.filter((range) => !isEmpty(range));
390
+ }
391
+ /**
392
+ * {@link wireRanges} of `subscription`, and the group each live join among them was
393
+ * {@link Subscription.placed | placed} at, by its index there.
394
+ */
395
+ export function wireHeld(subscription) {
396
+ const ranges = [];
397
+ const placed = new Map();
398
+ for (const [index, range] of (subscription.ranges ?? [{}]).entries()) {
399
+ if (isEmpty(range))
400
+ continue;
401
+ const group = subscription.placed?.get(index);
402
+ if (group !== undefined)
403
+ placed.set(ranges.length, group);
404
+ ranges.push(range);
405
+ }
406
+ return { ranges, placed };
407
+ }
408
+ /**
409
+ * `ranges` with each live join `placed` at a group, by index, as a time start there: how a wire
410
+ * before lite-07 narrows it, whose one max age still takes the join's max (see the original
411
+ * ranges). Mirrors the Rust `Subscription::times`.
412
+ */
413
+ export function placedAsTimes(ranges, placed) {
414
+ if (placed.size === 0)
415
+ return [...ranges];
416
+ return ranges.map((range, index) => {
417
+ const group = placed.get(index);
418
+ if (group === undefined)
419
+ return range;
420
+ const out = { start: group };
421
+ if (range.end !== undefined)
422
+ out.end = range.end;
423
+ return withPolicy(out, policyOf(range));
424
+ });
425
+ }
426
+ /** Whether every range has an end, so a lite-07 publisher ends the subscription once it covers them. */
427
+ export function bounded(ranges) {
428
+ return ranges.every((range) => range.end !== undefined);
429
+ }
430
+ /**
431
+ * Whether a subscriber joins at the live edge: an open range with no time start, a live join or one
432
+ * from the oldest group, following it. A time start resumes a known group instead, such as a route
433
+ * switch picking up where the reader stopped. Mirrors the Rust `Prefs::joins_live`.
434
+ */
435
+ export function joinsLive(ranges) {
436
+ return ranges.some((range) => rangeTime(range) === undefined && range.end === undefined);
437
+ }
438
+ /**
439
+ * The one span a wire without ranges carries for `ranges`, which the subscribers' own reads
440
+ * narrow again: from the lowest time start up to {@link rangesEnd}.
441
+ *
442
+ * A subscription whose ranges all lack a time start asks with no start, which a pre-06 publisher
443
+ * reads as the latest group and a lite-06 one as no floor: a wire without ranges carries a live
444
+ * join so. One mixing the two asks from group 0, since no single start covers both: the latest
445
+ * group may sit below the time start. Mirrors the Rust `wire_span`.
446
+ */
447
+ export function wireSpan(ranges) {
448
+ let start = rangesStart(ranges);
449
+ if (start === undefined && ranges.some((range) => !isEmpty(range) && rangeTime(range) !== undefined))
450
+ start = 0n;
451
+ return { start, end: rangesEnd(ranges) };
452
+ }
453
+ /** A range holding nothing: nothing sorts below group 0. Mirrors the Rust `Range::EMPTY`. */
454
+ export const EMPTY = Object.freeze({ start: 1n, end: 0n });
455
+ /**
456
+ * `ranges` with every range raised to start no earlier than `floor` and stop before the exclusive
457
+ * `end`, without ever widening one. Mirrors the Rust `Subscription::clamped`.
458
+ *
459
+ * `resolved` names where each range resolved, by index, so its start becomes its own group,
460
+ * raised to `floor` or capped at `end`. A start not resolved keeps its time, since the floor may
461
+ * lie before it, so the caller drops what it resolves to below `floor`. An end not resolved
462
+ * becomes the cap.
463
+ */
464
+ export function clamped(ranges, floor, end, resolved) {
465
+ if (floor === undefined && end === undefined)
466
+ return [...ranges];
467
+ return ranges.map((range, index) => {
468
+ const at = resolved[index];
469
+ let start = at ? at.start : range.start;
470
+ if (floor !== undefined && typeof start === "bigint")
471
+ start = max(start, floor);
472
+ let requested = range.end;
473
+ if (requested !== undefined && at?.end !== undefined) {
474
+ if (at.end === 0n)
475
+ return EMPTY;
476
+ requested = at.end - 1n;
477
+ }
478
+ if (end !== undefined) {
479
+ if (end === 0n)
480
+ return EMPTY;
481
+ requested = requested === undefined ? end - 1n : min(requested, end - 1n);
482
+ }
483
+ const out = {};
484
+ if (start !== undefined)
485
+ out.start = start;
486
+ if (requested !== undefined)
487
+ out.end = requested;
488
+ return withPolicy(out, policyOf(range));
489
+ });
490
+ }
491
+ /** A live join placed from any answer. Mirrors the Rust `Joining::default`. */
492
+ export const SINCE_ZERO = { since: 0 };
493
+ // The group `joining` places a join at, if it was placed already.
494
+ function placedAt(joining) {
495
+ return joining?.at;
496
+ }
497
+ /**
498
+ * `ranges` as a copy asked from `floor` through the exclusive `end` asks for them, and the group each
499
+ * live join among them is {@link Subscription.placed | placed} at, by index: as {@link clamped} places
500
+ * them, except a live join, which stays itself, placed at the group {@link clamped} starts it at. As
501
+ * a time start it would read as history, whose groups never go stale. A join `placed` already starts
502
+ * no earlier than where it was placed, and stays placed whatever the bounds. Mirrors the Rust
503
+ * `Range::asked` and the resume `asked`.
504
+ */
505
+ export function asked(ranges, floor, end, resolved, placed = new Map()) {
506
+ const out = [];
507
+ const at = new Map();
508
+ for (const [index, range] of ranges.entries()) {
509
+ const group = placed.get(index);
510
+ const clamp = group === undefined
511
+ ? clamped([range], floor, end, [resolved[index]])[0]
512
+ : clamped([range], floor === undefined ? group : max(floor, group), end, [
513
+ resolved[index] ?? { start: group, floored: true },
514
+ ])[0];
515
+ const start = range.start;
516
+ if (typeof start !== "object" || typeof clamp.start !== "bigint" || isEmpty(clamp)) {
517
+ out.push(clamp);
518
+ continue;
519
+ }
520
+ const join = { start };
521
+ if (clamp.end !== undefined)
522
+ join.end = clamp.end;
523
+ at.set(index, clamp.start);
524
+ out.push(withPolicy(join, policyOf(range)));
525
+ }
526
+ return { ranges: out, placed: at };
527
+ }
528
+ /** A hop that holds no groups of its own, such as a reader following route copies that resolve its ranges for it. */
529
+ export const UNHELD = Object.freeze({
530
+ resolves: () => false,
531
+ defers: () => true,
532
+ containing: () => "unknown",
533
+ nextStart: () => undefined,
534
+ complete: () => true,
535
+ });
536
+ /**
537
+ * A hop that holds no groups of its own but knows every time it is asked starts a group, such as
538
+ * a publisher's reported start: a time start names the group starting there.
539
+ */
540
+ export const AT_KEYS = Object.freeze({
541
+ ...UNHELD,
542
+ defers: () => false,
543
+ containing: (time) => ({ at: time }),
544
+ });
545
+ /**
546
+ * A subscriber's ranges resolved to the group starts it reads, each by the key its caller gave
547
+ * it, so an edit resolves only the ranges it adds and leaves every other where it resolved.
548
+ * Mirrors the Rust `Window`.
549
+ *
550
+ * A start resolves when its range is placed, and again only when it is replaced. A live join
551
+ * waits, reading everything, until {@link settle} names a first group. A time start the groups
552
+ * held can't place yet waits too, reading only the groups from its time meanwhile, since whichever
553
+ * group contains it starts at or before it.
554
+ */
555
+ export class Window {
556
+ // What these were resolved from, by key, in key order.
557
+ #ranges = new Map();
558
+ // Each non-empty range placed, by key.
559
+ #spans = new Map();
560
+ // The groups the spans cover, counted so an edit moves only its own runs.
561
+ #groups = new Cover();
562
+ // The part of {@link #groups} a miss may ask for: see {@link missable}.
563
+ #missable = new Cover();
564
+ // The ranges whose live join waits on a first group, by key: everything is in range meanwhile.
565
+ #pending = new Set();
566
+ /**
567
+ * The spans, by key, whose time start waits on a group arriving, by that time: a change to the
568
+ * groups held tries only those its {@link Holdings.unplaced} leaves out.
569
+ */
570
+ #waiting = new ByTime();
571
+ /** The {@link Unplaced.mark} every waiting span was last tried under. */
572
+ #waited;
573
+ /**
574
+ * The spans, by key, still looking up their time start, which read nothing meanwhile, by that
575
+ * time: kept apart so an answer finds them without visiting every span.
576
+ */
577
+ #looking = new ByTime();
578
+ /**
579
+ * The ranges, by key, of a live join that can't tell where it starts while groups are held, such
580
+ * as a relay copy waiting on upstream to answer for it, so they read nothing yet: a group held is
581
+ * no reason to start at it.
582
+ */
583
+ #reaching = new Set();
584
+ /** How each live join's range is placed, by key. */
585
+ #joining = new Map();
586
+ // How many spans name each start, and how many name none: see {@link start}.
587
+ #named = new Counts();
588
+ #unnamed = 0;
589
+ // The budgets of the live joins among the ranges, counted, for the widest asked.
590
+ #budgets = new Counts();
591
+ // The non-empty history ranges among the ranges, placed or not.
592
+ #histories = 0;
593
+ // The groups the spans read, by the budget of their ranges.
594
+ #layers = new Map();
595
+ // {@link budget} as last read, until the budgets, the layers, or the ranges not yet placed move.
596
+ #budget;
597
+ /**
598
+ * Resolve `ranges`, keyed by position, time starts from `holdings`, and a live join back by its
599
+ * lag from the live edge, `latest` being the latest group.
600
+ */
601
+ constructor(ranges, latest, holdings = UNHELD) {
602
+ for (const [index, range] of ranges.entries()) {
603
+ this.#ranges.set(BigInt(index), range);
604
+ this.#countBudget(range, 1);
605
+ }
606
+ this.#place(latest, holdings);
607
+ }
608
+ /**
609
+ * {@link Window} for ranges the caller keys itself, in key order, each live join's range placed as
610
+ * `joining` of its key says (from join tick 0 by default). Mirrors the Rust `Window::keyed_since`.
611
+ */
612
+ static keyed(ranges, latest, holdings = UNHELD, joining = () => SINCE_ZERO) {
613
+ const window = new Window([], undefined, holdings);
614
+ for (const [key, range] of ranges) {
615
+ window.#ranges.set(key, range);
616
+ window.#countBudget(range, 1);
617
+ const join = joining(key);
618
+ if (liveMillis(range) !== undefined || join.at !== undefined)
619
+ window.#joining.set(key, join);
620
+ }
621
+ window.#place(latest, holdings);
622
+ return window;
623
+ }
624
+ /**
625
+ * How long each group stays worth delivering once superseded, as these ranges set it: the same
626
+ * value until the ranges or where they are placed move. Mirrors the Rust `Window::budget`.
627
+ */
628
+ budget() {
629
+ if (this.#budget)
630
+ return this.#budget;
631
+ this.#budget = new Budget({
632
+ layers: [...this.#layers].map(([budget, cover]) => [budget, cover.runs()]),
633
+ unplaced: this.#unplacedBudgets((key) => this.#ranges.get(key)),
634
+ widest: this.#histories > 0 ? NO_MAX_DELAY : (this.#budgets.last() ?? 0),
635
+ });
636
+ return this.#budget;
637
+ }
638
+ /**
639
+ * Each range not yet placed, as `rangeOf` its key says, as the exclusive end of the groups it may
640
+ * cover and its budget: a pending one may cover any up to its end, since every group is in range
641
+ * meanwhile, and a live join still reaching may start at any. Mirrors the Rust
642
+ * `Window::unplaced_budgets`.
643
+ */
644
+ #unplacedBudgets(rangeOf) {
645
+ const unplaced = [];
646
+ for (const key of [...this.#pending, ...this.#reaching]) {
647
+ const range = rangeOf(key);
648
+ if (range === undefined || isEmpty(range))
649
+ continue;
650
+ unplaced.push([range.end === undefined ? UNBOUNDED : range.end + 1n, rangeBudget(range)]);
651
+ }
652
+ return unplaced;
653
+ }
654
+ /**
655
+ * The ranges covering group `key`, by key, each with the budget in milliseconds `rangeOf` its key
656
+ * gives, these ranges as given by default: a range not yet placed may cover any group up to its
657
+ * end, as in {@link budget}. Mirrors the Rust `Window::covering`.
658
+ */
659
+ covering(key, rangeOf = (id) => this.#ranges.get(id)) {
660
+ const ids = [];
661
+ for (const [id, span] of this.#spans)
662
+ if (span.reads && span.reads.start <= key && key < span.reads.end)
663
+ ids.push(id);
664
+ for (const id of [...this.#pending, ...this.#reaching]) {
665
+ const range = rangeOf(id);
666
+ if (range !== undefined && !isEmpty(range) && (range.end === undefined || range.end >= key))
667
+ ids.push(id);
668
+ }
669
+ const covering = [];
670
+ for (const id of ids) {
671
+ const range = rangeOf(id);
672
+ if (range !== undefined)
673
+ covering.push([id, rangeBudget(range)]);
674
+ }
675
+ return covering;
676
+ }
677
+ /**
678
+ * {@link budget} with each range's staleness taken from `ranges`, by key, rather than from the
679
+ * range it was placed as: a front's reader places a live join where a copy resolved it, as a time
680
+ * start, and the join's groups still go stale past its max. Mirrors the Rust `Window::budget_of`.
681
+ */
682
+ budgetOf(ranges) {
683
+ const layers = new Map();
684
+ for (const [key, span] of this.#spans) {
685
+ const range = ranges.get(key);
686
+ if (!span.reads || range === undefined)
687
+ continue;
688
+ const budget = rangeBudget(range);
689
+ const layer = layers.get(budget) ?? new Cover();
690
+ layers.set(budget, layer);
691
+ layer.add(span.reads.start, span.reads.end, 1);
692
+ }
693
+ return new Budget({
694
+ layers: [...layers].map(([budget, cover]) => [budget, cover.runs()]),
695
+ unplaced: this.#unplacedBudgets((key) => ranges.get(key)),
696
+ widest: widest(ranges.values()),
697
+ });
698
+ }
699
+ // Count a live join's budget among {@link #budgets}, or a history range among {@link #histories},
700
+ // once more, or once less for a `delta` of -1.
701
+ #countBudget(range, delta) {
702
+ if (liveMillis(range) === undefined) {
703
+ if (isEmpty(range))
704
+ return;
705
+ this.#histories += delta;
706
+ }
707
+ else if (delta > 0)
708
+ this.#budgets.add(rangeBudget(range));
709
+ else
710
+ this.#budgets.remove(rangeBudget(range));
711
+ this.#budget = undefined;
712
+ }
713
+ /** What these were resolved from, in key order. Costs every range. */
714
+ get ranges() {
715
+ return [...this.#ranges.values()];
716
+ }
717
+ /**
718
+ * Retire the ranges keyed `retire`, then resolve `add`, leaving every other range where it
719
+ * resolved. A key added again replaces its range in place, but a live join added again with only
720
+ * its max or policy changed, and placed as it was, keeps where it started, as does any other range
721
+ * added again with only its policy changed; any other key added is past every key held. Each live
722
+ * join's range is placed as `joining` of its key says (from join tick 0 by default). Costs the
723
+ * ranges edited, never the rest. Returns whether any group it reads, or how long one stays
724
+ * fresh, may have moved: a change of policy alone moves none. Mirrors the Rust
725
+ * `Window::edit_since`.
726
+ */
727
+ edit(retire, add, latest, holdings = UNHELD, joining = () => SINCE_ZERO) {
728
+ let moved = false;
729
+ const retuned = new Set();
730
+ for (const [key, range] of add) {
731
+ const old = this.#ranges.get(key);
732
+ // A live join keeps where it started whatever its max, and any range whose policy alone
733
+ // moved keeps where it resolved; one added again unchanged resolves afresh.
734
+ const kept = old !== undefined &&
735
+ (joinOf(range) !== undefined
736
+ ? joinOf(old) === joinOf(range)
737
+ : rangeKey(old) !== rangeKey(range) && retunes(old, range));
738
+ if (kept && placedAt(this.#joining.get(key)) === placedAt(joining(key)))
739
+ retuned.add(key);
740
+ }
741
+ const readded = new Set(add.map(([key]) => key));
742
+ for (const key of retire) {
743
+ if (retuned.has(key))
744
+ continue;
745
+ moved = true;
746
+ this.#retire(key);
747
+ // One added again is replaced in place below, keeping its order.
748
+ if (!readded.has(key)) {
749
+ const range = this.#ranges.get(key);
750
+ if (range)
751
+ this.#countBudget(range, -1);
752
+ this.#ranges.delete(key);
753
+ this.#joining.delete(key);
754
+ }
755
+ }
756
+ for (const [key, range] of add) {
757
+ const old = this.#ranges.get(key);
758
+ if (retuned.has(key) &&
759
+ old !== undefined &&
760
+ retunes(old, range) &&
761
+ rangeBudget(old) === rangeBudget(range)) {
762
+ // Its policy alone moved, which nothing here reads.
763
+ this.#ranges.set(key, range);
764
+ continue;
765
+ }
766
+ moved = true;
767
+ if (old !== undefined)
768
+ this.#countBudget(old, -1);
769
+ this.#countBudget(range, 1);
770
+ if (retuned.has(key)) {
771
+ this.#ranges.set(key, range);
772
+ // Its groups take the new budget where it stands.
773
+ const span = this.#spans.get(key);
774
+ if (span) {
775
+ this.#uncount(key, span);
776
+ this.#count(key, { ...span, budget: rangeBudget(range) });
777
+ }
778
+ continue;
779
+ }
780
+ this.#retire(key);
781
+ this.#ranges.set(key, range);
782
+ const join = joining(key);
783
+ if (liveMillis(range) !== undefined || join.at !== undefined)
784
+ this.#joining.set(key, join);
785
+ else
786
+ this.#joining.delete(key);
787
+ if (!isEmpty(range))
788
+ this.#placeRange(key, latest, holdings);
789
+ }
790
+ return moved;
791
+ }
792
+ /**
793
+ * Note each live join range keyed in `since` as added at its tick instead (see
794
+ * `Prefs.stamp`), placing again any still waiting on an answer, which an answer recorded since
795
+ * that tick may now place. One already placed stays where it started, and one placed at a group
796
+ * takes no tick. Returns whether the groups in range moved.
797
+ */
798
+ stamp(since, latest, holdings = UNHELD) {
799
+ const placed = this.#spans.size;
800
+ for (const [key, tick] of since) {
801
+ if (!(this.#joining.get(key)?.since !== undefined))
802
+ continue;
803
+ this.#joining.set(key, { since: tick });
804
+ if (!this.#reaching.delete(key))
805
+ continue;
806
+ this.#budget = undefined;
807
+ this.#placeRange(key, latest, holdings);
808
+ }
809
+ return this.#spans.size > placed;
810
+ }
811
+ // Take out what the range keyed `key` placed, leaving the range itself.
812
+ #retire(key) {
813
+ const pending = this.#pending.delete(key);
814
+ if (this.#reaching.delete(key) || pending)
815
+ this.#budget = undefined;
816
+ const span = this.#spans.get(key);
817
+ if (span)
818
+ this.#uncount(key, span);
819
+ }
820
+ /** Whether a live join still waits on a first group. */
821
+ get pending() {
822
+ return this.#pending.size > 0;
823
+ }
824
+ /**
825
+ * Whether a live join's reach back waits on its upstream's answer or a group held starting. Mirrors
826
+ * the Rust `Window::is_reaching`.
827
+ */
828
+ get reaching() {
829
+ return this.#reaching.size > 0;
830
+ }
831
+ /**
832
+ * The keys of the ranges still waiting on a live join or a time start. Mirrors the Rust
833
+ * `Window::unsettled`.
834
+ */
835
+ unsettled() {
836
+ const keys = new Set([...this.#pending, ...this.#reaching.keys()]);
837
+ for (const entry of this.#waiting.all())
838
+ for (const key of entry.keys)
839
+ keys.add(key);
840
+ for (const entry of this.#looking.all())
841
+ for (const key of entry.keys)
842
+ keys.add(key);
843
+ return keys;
844
+ }
845
+ /**
846
+ * Whether a range not yet placed may still come to hold group `key`: placed, it starts at or below
847
+ * the group its time or live edge falls in, which may be `key` or one below. A time at or past
848
+ * `end`, where group `key` is known to end, falls in a later group, however far the range got
849
+ * placing it. Mirrors the Rust `Window::may_hold`.
850
+ */
851
+ mayHold(key, end) {
852
+ const reaches = (unsettled) => {
853
+ const range = this.#ranges.get(unsettled);
854
+ if (range === undefined || (range.end !== undefined && range.end < key))
855
+ return false;
856
+ const time = rangeTime(range);
857
+ return time === undefined || end === undefined || time < end;
858
+ };
859
+ for (const unsettled of this.#pending)
860
+ if (reaches(unsettled))
861
+ return true;
862
+ for (const unsettled of this.#reaching)
863
+ if (reaches(unsettled))
864
+ return true;
865
+ for (const timed of [this.#waiting, this.#looking]) {
866
+ const entries = end === undefined ? timed.all() : timed.in(0n, end);
867
+ for (const entry of entries)
868
+ for (const unsettled of entry.keys)
869
+ if (reaches(unsettled))
870
+ return true;
871
+ }
872
+ return false;
873
+ }
874
+ /** Whether nothing is left to resolve as groups arrive. */
875
+ get settled() {
876
+ return this.#pending.size === 0 && !this.#unplaced() && this.#looking.size === 0;
877
+ }
878
+ /**
879
+ * Resolve what the groups now held settle: live joins once `latest` exists or the holdings can
880
+ * tell where they start, and time starts once a group arrives. Returns whether the groups in
881
+ * range moved.
882
+ */
883
+ settle(latest, holdings = UNHELD) {
884
+ if (this.pending) {
885
+ if (latest === undefined)
886
+ return false;
887
+ // Every range places again, as when first resolved: the first group arrives once per track.
888
+ this.#place(latest, holdings);
889
+ return !this.pending;
890
+ }
891
+ let moved = false;
892
+ if (this.#reaching.size > 0) {
893
+ const placed = this.#spans.size;
894
+ const reaching = [...this.#reaching];
895
+ this.#reaching.clear();
896
+ this.#budget = undefined;
897
+ for (const key of reaching)
898
+ this.#placeRange(key, latest, holdings);
899
+ moved = this.#spans.size > placed;
900
+ }
901
+ if (this.#waiting.size > 0 && this.#startWaiting(holdings))
902
+ moved = true;
903
+ return moved;
904
+ }
905
+ // Start the spans whose time start waits on a group, once one arrived, or once nothing can
906
+ // arrive any more. Only the times the holdings may now place are tried, so a change placing one
907
+ // of them costs that one, not every span still waiting. Returns whether any started.
908
+ #startWaiting(holdings) {
909
+ const complete = holdings.complete();
910
+ const unplaced = complete ? undefined : holdings.unplaced?.();
911
+ let tried;
912
+ if (unplaced !== undefined && this.#waited === unplaced.mark) {
913
+ tried = [];
914
+ for (const gap of outside(unplaced.runs)) {
915
+ for (const entry of this.#waiting.takeIn(gap.start, gap.end))
916
+ tried.push(entry);
917
+ }
918
+ }
919
+ else {
920
+ tried = this.#waiting.takeAll();
921
+ this.#waited = unplaced?.mark;
922
+ }
923
+ let started = false;
924
+ for (const entry of tried) {
925
+ // Every key waiting on one time waits under the widest budget among their ranges.
926
+ const containing = holdings.containing(entry.time, this.#widestOf(entry.keys));
927
+ let start;
928
+ let floored;
929
+ if (typeof containing === "object")
930
+ [start, floored] = [containing.at, true];
931
+ // The group holding it is older than any held, or nothing can arrive.
932
+ else if (containing === "before" || complete)
933
+ [start, floored] = [0n, false];
934
+ else {
935
+ this.#waiting.put(entry);
936
+ continue;
937
+ }
938
+ started = true;
939
+ for (const key of entry.keys)
940
+ this.#startSpan(key, entry.time, start, floored, holdings);
941
+ }
942
+ return started;
943
+ }
944
+ // Start the span keyed `key`, whose time start `at` waited, at `start`: looked up where the
945
+ // groups held can't place it, as when first resolved, and otherwise reading its groups.
946
+ #startSpan(key, at, start, floored, holdings) {
947
+ const span = this.#spans.get(key);
948
+ if (!span)
949
+ return;
950
+ this.#uncount(key, span);
951
+ span.start = start;
952
+ span.floored = floored;
953
+ span.waiting = false;
954
+ const floor = unresolved(at, start, floored, holdings);
955
+ if (floor !== undefined)
956
+ span.looking = { time: at, floor };
957
+ this.#count(key, span);
958
+ }
959
+ /**
960
+ * The time starts still waiting on the groups held, lowest first: what a received copy asks its
961
+ * upstream to place. Mirrors the Rust `Window::asks`.
962
+ */
963
+ asks() {
964
+ const times = [];
965
+ for (const entry of this.#waiting.all())
966
+ times.push(entry.time);
967
+ return times;
968
+ }
969
+ /**
970
+ * The live joins still waiting on an answer, as `[lag, end, budget, since]`: the lag in
971
+ * milliseconds (null for no lag limit), the range's end, its budget in milliseconds, and the join tick its range was added
972
+ * at. What a received copy asks its upstream to place. Mirrors the Rust `Window::joins`.
973
+ */
974
+ joins() {
975
+ const joins = [];
976
+ for (const key of this.#reaching) {
977
+ const range = this.#ranges.get(key);
978
+ const lag = range && liveMillis(range);
979
+ if (range && lag !== undefined)
980
+ joins.push([lag, range.end, rangeBudget(range), this.#joining.get(key)?.since ?? 0]);
981
+ }
982
+ return joins;
983
+ }
984
+ /**
985
+ * The times in `[start, end)` of the time starts not yet resolved: waiting on the groups held, or
986
+ * on a lookup of the track's history. The group containing each starts at or before its time, so
987
+ * one at or past a read's floor and below a group may still land between them. Mirrors the Rust
988
+ * `Window::unresolved_in`.
989
+ */
990
+ unresolvedIn(start, end) {
991
+ const times = [];
992
+ for (const entry of this.#waiting.in(start, end))
993
+ times.push(entry.time);
994
+ for (const entry of this.#looking.in(start, end))
995
+ times.push(entry.time);
996
+ return times;
997
+ }
998
+ /** Whether group `key` is in range. Everything is while a live join waits on a first group. */
999
+ contains(key) {
1000
+ return this.pending || this.#groups.contains(key);
1001
+ }
1002
+ /**
1003
+ * Whether a range placed so far holds group `key`: unlike {@link contains}, not every group while a
1004
+ * live join waits on a first group. Mirrors the Rust `Window::holds`.
1005
+ */
1006
+ holds(key) {
1007
+ return this.#groups.contains(key);
1008
+ }
1009
+ /** The lowest group in range at or above `from`, undefined when none is. `from` itself while a live join waits on a first group. */
1010
+ next(from) {
1011
+ return this.pending ? from : this.#groups.next(from);
1012
+ }
1013
+ /** The highest group in range below `before`, undefined when none is. The one just below while a live join waits on a first group. */
1014
+ prev(before) {
1015
+ if (this.pending)
1016
+ return before > 0n ? before - 1n : undefined;
1017
+ return this.#groups.prev(before);
1018
+ }
1019
+ /**
1020
+ * The lowest group in range, undefined while a start waits, a time start's lookup included: it may
1021
+ * resolve to a group below every one in range. Nothing in range floors at group 0.
1022
+ */
1023
+ floor() {
1024
+ if (this.pending || this.#unplaced() || this.#looking.size > 0)
1025
+ return undefined;
1026
+ return this.#groups.lowest() ?? 0n;
1027
+ }
1028
+ /**
1029
+ * The lowest group the ranges name, undefined while one reaches back to the oldest group or a
1030
+ * start waits.
1031
+ */
1032
+ start() {
1033
+ if (this.pending || this.#reaching.size > 0 || this.#unnamed > 0)
1034
+ return undefined;
1035
+ return this.#named.first();
1036
+ }
1037
+ /** Where the run in range reaching past every group starts, undefined when none does. A live join that waits runs from 0. */
1038
+ openFrom() {
1039
+ return this.pending ? 0n : this.#groups.openFrom();
1040
+ }
1041
+ /** The exclusive end past every group in range, undefined when one stays open or a start waits. Nothing in range ends at 0. */
1042
+ end() {
1043
+ if (!this.settled)
1044
+ return undefined;
1045
+ const end = this.#groups.end();
1046
+ return end === UNBOUNDED ? undefined : end;
1047
+ }
1048
+ /** The runs in range from `from` up to the exclusive `end`, lowest first. */
1049
+ runs(from = 0n, end = UNBOUNDED) {
1050
+ if (from >= end)
1051
+ return [];
1052
+ if (this.pending)
1053
+ return [{ start: from, end }];
1054
+ return this.#groups.runs(from, end);
1055
+ }
1056
+ /**
1057
+ * The runs the placed ranges name: unlike {@link runs}, a live join still waiting on a first
1058
+ * group names none of them. Mirrors the Rust `Window::placed`.
1059
+ */
1060
+ placed(from = 0n, end = UNBOUNDED) {
1061
+ return from >= end ? [] : this.#groups.runs(from, end);
1062
+ }
1063
+ /**
1064
+ * The part of {@link runs} a miss may ask a fill for: all of it but what starts before a time
1065
+ * start that fell before every group held. Mirrors the Rust `Window::missable`.
1066
+ */
1067
+ missable(from = 0n, end = UNBOUNDED) {
1068
+ if (from >= end)
1069
+ return [];
1070
+ if (this.pending)
1071
+ return [{ start: from, end }];
1072
+ return this.#missable.runs(from, end);
1073
+ }
1074
+ /**
1075
+ * The range keyed `key` resolved: undefined for one whose start still waits, or is still looked
1076
+ * up, or that isn't held. One empty as given ends at its start. Mirrors the Rust
1077
+ * `Window::resolved_key`.
1078
+ */
1079
+ resolvedKey(key) {
1080
+ const range = this.#ranges.get(key);
1081
+ if (range === undefined)
1082
+ return undefined;
1083
+ const time = rangeTime(range);
1084
+ if (time !== undefined && isEmpty(range))
1085
+ return { start: time, end: time, floored: true };
1086
+ const span = this.#spans.get(key);
1087
+ if (!span || span.looking || span.waiting)
1088
+ return undefined;
1089
+ return { start: span.start, end: span.end === UNBOUNDED ? undefined : span.end, floored: span.floored };
1090
+ }
1091
+ /** Each range resolved, in key order, as {@link resolvedKey} reads it. Mirrors the Rust `Window::resolved`. */
1092
+ resolved() {
1093
+ return [...this.#ranges.keys()].map((key) => this.resolvedKey(key));
1094
+ }
1095
+ /**
1096
+ * Where each range starts once delivery starts at `from`, in key order: undefined for one with
1097
+ * no group there, or whose start still waits. Mirrors the Rust `Window::starts`.
1098
+ */
1099
+ starts(from) {
1100
+ return this.resolved().map((range) => {
1101
+ if (!range)
1102
+ return undefined;
1103
+ const start = max(range.start, from);
1104
+ return range.end === undefined || range.end > start ? start : undefined;
1105
+ });
1106
+ }
1107
+ /** The time starts still looked up, by time. Mirrors the Rust `Window::lookups`. */
1108
+ lookups() {
1109
+ const lookups = [];
1110
+ for (const { keys } of this.#looking.all()) {
1111
+ for (const key of keys) {
1112
+ const looking = this.#spans.get(key)?.looking;
1113
+ if (looking)
1114
+ lookups.push(looking);
1115
+ }
1116
+ }
1117
+ return lookups;
1118
+ }
1119
+ /**
1120
+ * The lookup of time start `at` answered: its spans start where the answer says, never where the
1121
+ * groups held happen to. Left unanswered because no handler is left, they start where the groups
1122
+ * held say, as with no handler at all, unless the cache since evicted the group their floor was
1123
+ * past and a handler is back: then they look it up again from lower down. Returns whether the
1124
+ * groups in range moved. Mirrors the Rust `Window::found`.
1125
+ */
1126
+ found(at, found, holdings = UNHELD) {
1127
+ const keys = this.#looking.take(at);
1128
+ if (keys === undefined)
1129
+ return false;
1130
+ for (const key of keys) {
1131
+ const span = this.#spans.get(key);
1132
+ if (!span)
1133
+ continue;
1134
+ this.#uncount(key, span);
1135
+ const asked = span.looking?.floor;
1136
+ span.looking = undefined;
1137
+ if (found.kind === "group") {
1138
+ span.start = found.group;
1139
+ span.floored = true;
1140
+ }
1141
+ else if (found.kind === "dropped") {
1142
+ // A floor is one past the group held below the time, which then holds it; from 0, no
1143
+ // group starts at or before it, so the span reads from the time on.
1144
+ span.start = found.floor > 0n ? found.floor - 1n : at;
1145
+ span.floored = true;
1146
+ }
1147
+ else {
1148
+ const containing = holdings.containing(at, this.#budgetOf(key));
1149
+ span.start = typeof containing === "object" ? containing.at : 0n;
1150
+ span.floored = typeof containing === "object";
1151
+ if (asked !== undefined) {
1152
+ // Nothing held at or before the time any more, maybe nothing at all: the group containing
1153
+ // it may start anywhere below the floor asked.
1154
+ const floor = span.floored
1155
+ ? unresolved(at, span.start, true, holdings)
1156
+ : holdings.looksUp?.()
1157
+ ? 0n
1158
+ : undefined;
1159
+ if (floor !== undefined && floor < asked)
1160
+ span.looking = { time: at, floor };
1161
+ }
1162
+ }
1163
+ this.#count(key, span);
1164
+ }
1165
+ return true;
1166
+ }
1167
+ /**
1168
+ * What rebuilding from the spans would read, may ask a miss for, and name as its start, for a test
1169
+ * to check the counted runs against.
1170
+ */
1171
+ fresh() {
1172
+ const groups = [];
1173
+ const missable = [];
1174
+ let named;
1175
+ for (const span of this.#spans.values()) {
1176
+ const [reads, asks] = spanReads(span);
1177
+ if (reads)
1178
+ groups.push(reads);
1179
+ if (asks)
1180
+ missable.push(asks);
1181
+ if (span.floored)
1182
+ named = min(named ?? span.start, span.start);
1183
+ }
1184
+ return { runs: merge(groups), missable: merge(missable), named };
1185
+ }
1186
+ /** The lowest start the spans name, counted. */
1187
+ named() {
1188
+ return this.#named.first();
1189
+ }
1190
+ // Whether a time start or a live join waits on the groups held to place it.
1191
+ #unplaced() {
1192
+ return this.#waiting.size > 0 || this.#reaching.size > 0;
1193
+ }
1194
+ // The budget of the range keyed `key`, none for one not held.
1195
+ #budgetOf(key) {
1196
+ const range = this.#ranges.get(key);
1197
+ return range === undefined ? NO_MAX_DELAY : rangeBudget(range);
1198
+ }
1199
+ // The widest budget among the ranges keyed `keys`.
1200
+ #widestOf(keys) {
1201
+ let widest = 0;
1202
+ for (const key of keys)
1203
+ widest = Math.max(widest, this.#budgetOf(key));
1204
+ return widest;
1205
+ }
1206
+ // Resolve every range again from scratch.
1207
+ #place(latest, holdings) {
1208
+ this.#reaching.clear();
1209
+ this.#spans.clear();
1210
+ this.#pending.clear();
1211
+ this.#waiting = new ByTime();
1212
+ this.#looking = new ByTime();
1213
+ this.#groups = new Cover();
1214
+ this.#missable = new Cover();
1215
+ this.#layers.clear();
1216
+ this.#budget = undefined;
1217
+ this.#named = new Counts();
1218
+ this.#unnamed = 0;
1219
+ for (const [key, range] of this.#ranges) {
1220
+ if (!isEmpty(range))
1221
+ this.#placeRange(key, latest, holdings);
1222
+ }
1223
+ // Each span waiting was just tried against these holdings.
1224
+ this.#waited = holdings.unplaced?.()?.mark;
1225
+ }
1226
+ // Resolve the non-empty range keyed `key` into a span, or note what it waits on.
1227
+ #placeRange(key, latest, holdings) {
1228
+ const range = this.#ranges.get(key);
1229
+ if (range === undefined)
1230
+ return;
1231
+ const end = range.end === undefined ? UNBOUNDED : range.end + 1n;
1232
+ let at = rangeTime(range);
1233
+ const live = typeof range.start === "object" ? range.start.live.min : undefined;
1234
+ const budget = rangeBudget(range);
1235
+ // Placed already (live join or exact-group Position start): start at that exact group without
1236
+ // time-containment lookup.
1237
+ const placed = this.#joining.get(key)?.at;
1238
+ if (placed !== undefined) {
1239
+ this.#count(key, { at: undefined, start: placed, end, floored: true, waiting: false, budget });
1240
+ return;
1241
+ }
1242
+ // A hop whose copies place the join waits on them, as one with no group yet does.
1243
+ if (live !== undefined && holdings.defers?.()) {
1244
+ this.#pending.add(key);
1245
+ this.#budget = undefined;
1246
+ return;
1247
+ }
1248
+ // Placed already, where a copy resolved it: one left with no start reached back.
1249
+ if (live !== undefined && holdings.resolves()) {
1250
+ let join;
1251
+ if (holdings.answersJoins?.()) {
1252
+ // Only its upstream's answer places it, whatever this hop holds: read none of the groups
1253
+ // held until it does.
1254
+ join = holdings.join?.(live, range.end, budget, this.#joining.get(key)?.since ?? 0);
1255
+ // Nothing to deliver: a start past its end holds nothing.
1256
+ if (join === "empty" && range.end !== undefined)
1257
+ join = { group: range.end + 1n };
1258
+ if (join === undefined || join === "empty") {
1259
+ this.#reaching.add(key);
1260
+ this.#budget = undefined;
1261
+ return;
1262
+ }
1263
+ }
1264
+ else {
1265
+ const since = this.#joining.get(key)?.since ?? 0;
1266
+ // Its upstream's answer places it, whatever the groups held measure: nothing to deliver
1267
+ // starts it past its end, while an unbounded one measures.
1268
+ const answer = holdings.answeredJoin?.(live, range.end, budget, since);
1269
+ const answered = answer?.start ?? (answer && range.end !== undefined ? range.end + 1n : undefined);
1270
+ if (answered !== undefined) {
1271
+ join = { group: answered };
1272
+ }
1273
+ else if (latest === undefined) {
1274
+ // Nothing to measure the live edge by yet: wait for the first group, reading everything
1275
+ // meanwhile.
1276
+ this.#pending.add(key);
1277
+ this.#budget = undefined;
1278
+ return;
1279
+ }
1280
+ else if (live?.asNanos() === 0) {
1281
+ // The latest group holds the live edge. One made before any group then starts at the
1282
+ // first, which was the latest when it was placed.
1283
+ join = { group: latest };
1284
+ }
1285
+ else {
1286
+ join = holdings.join?.(live, range.end, budget, since);
1287
+ // Nothing to measure the live edge by yet: read none of the groups held until it resolves.
1288
+ if (join === undefined || join === "empty") {
1289
+ this.#reaching.add(key);
1290
+ this.#budget = undefined;
1291
+ return;
1292
+ }
1293
+ }
1294
+ }
1295
+ // A group a join measured starts there; a time is placed among the groups held; one past
1296
+ // every group starts as a range from the oldest group does.
1297
+ if (join === "oldest")
1298
+ at = undefined;
1299
+ else if (join.group !== undefined) {
1300
+ this.#count(key, { at: undefined, start: join.group, end, floored: true, waiting: false, budget });
1301
+ return;
1302
+ }
1303
+ else
1304
+ at = join.time;
1305
+ }
1306
+ const start = startOf(at, latest, budget, holdings);
1307
+ if (start === "pending") {
1308
+ this.#pending.add(key);
1309
+ this.#budget = undefined;
1310
+ return;
1311
+ }
1312
+ if (start === "waiting") {
1313
+ // Reads only the groups from its time until the groups held place it; see #startWaiting.
1314
+ this.#count(key, { at, start: 0n, end, floored: false, waiting: true, budget });
1315
+ return;
1316
+ }
1317
+ // A time start the groups held can't place reads nothing until its lookup answers.
1318
+ const floor = at === undefined ? undefined : unresolved(at, start.group, start.floored, holdings);
1319
+ const looking = at !== undefined && floor !== undefined ? { time: at, floor } : undefined;
1320
+ this.#count(key, { at, start: start.group, end, floored: start.floored, looking, waiting: false, budget });
1321
+ }
1322
+ // Hold `span` under `key`, counting the groups it reads and where it starts.
1323
+ #count(key, span) {
1324
+ [span.reads, span.asks] = spanReads(span);
1325
+ if (span.reads) {
1326
+ this.#groups.add(span.reads.start, span.reads.end, 1);
1327
+ const layer = this.#layers.get(span.budget) ?? new Cover();
1328
+ this.#layers.set(span.budget, layer);
1329
+ layer.add(span.reads.start, span.reads.end, 1);
1330
+ this.#budget = undefined;
1331
+ }
1332
+ if (span.asks)
1333
+ this.#missable.add(span.asks.start, span.asks.end, 1);
1334
+ if (span.floored)
1335
+ this.#named.add(span.start);
1336
+ else
1337
+ this.#unnamed++;
1338
+ if (span.looking)
1339
+ this.#looking.add(span.looking.time, key);
1340
+ if (span.waiting)
1341
+ this.#waiting.add(span.at ?? 0n, key);
1342
+ this.#spans.set(key, span);
1343
+ }
1344
+ // Take back what {@link #count} counted for `span`, and take it out of the spans.
1345
+ #uncount(key, span) {
1346
+ if (span.reads) {
1347
+ this.#groups.add(span.reads.start, span.reads.end, -1);
1348
+ const layer = this.#layers.get(span.budget);
1349
+ layer?.add(span.reads.start, span.reads.end, -1);
1350
+ if (layer?.lowest() === undefined)
1351
+ this.#layers.delete(span.budget);
1352
+ this.#budget = undefined;
1353
+ }
1354
+ if (span.asks)
1355
+ this.#missable.add(span.asks.start, span.asks.end, -1);
1356
+ if (span.floored)
1357
+ this.#named.remove(span.start);
1358
+ else
1359
+ this.#unnamed--;
1360
+ if (span.looking)
1361
+ this.#looking.remove(span.looking.time, key);
1362
+ if (span.waiting)
1363
+ this.#waiting.remove(span.at ?? 0n, key);
1364
+ this.#spans.delete(key);
1365
+ }
1366
+ }
1367
+ /**
1368
+ * How long each group stays worth delivering once superseded, in milliseconds, as a reader's ranges
1369
+ * set it: the widest budget among the ranges covering it, a live join's being its max, and a history
1370
+ * range's as long as the publisher keeps it ({@link NO_MAX_DELAY}). A range not yet placed may cover
1371
+ * any group up to its end. A group none covers, such as one handed out before the ranges moved, gets
1372
+ * the widest budget any range asks: it was reached through one of them, so a history range keeps it
1373
+ * from going stale. Immutable: a window hands out a new one when its ranges move. Mirrors the Rust
1374
+ * `Budget`.
1375
+ */
1376
+ export class Budget {
1377
+ /** The groups the placed ranges cover, by the budget they give, widest first, each sorted and disjoint. */
1378
+ layers;
1379
+ /**
1380
+ * The ranges not yet placed, each as the exclusive end of the groups it may cover and its budget:
1381
+ * by end ascending, budget descending, leaving out any another covers at least as widely.
1382
+ */
1383
+ unplaced;
1384
+ // The widest budget any range asks, placed or not, which a group no range covers gets.
1385
+ #widest;
1386
+ constructor(parts) {
1387
+ this.layers = parts.layers.filter(([, runs]) => runs.length > 0).sort(([a], [b]) => b - a);
1388
+ // A range covering through a later group at least as widely hides one through an earlier.
1389
+ const unplaced = [...parts.unplaced].sort(([a, x], [b, y]) => (a < b ? -1 : a > b ? 1 : x - y));
1390
+ const kept = [];
1391
+ for (let i = unplaced.length - 1; i >= 0; i--) {
1392
+ const wider = kept.at(-1);
1393
+ if (wider === undefined || unplaced[i][1] > wider[1])
1394
+ kept.push(unplaced[i]);
1395
+ }
1396
+ this.unplaced = kept.reverse();
1397
+ this.#widest = parts.widest;
1398
+ }
1399
+ /** The same budget for every group. */
1400
+ static uniform(budget) {
1401
+ return new Budget({ layers: [], unplaced: [], widest: budget });
1402
+ }
1403
+ /** This budget, each group's no wider than `bound`, undefined for none. */
1404
+ clamped(bound) {
1405
+ if (bound === undefined || this.#widest <= bound)
1406
+ return this;
1407
+ return new Budget({
1408
+ layers: this.layers.map(([budget, runs]) => [Math.min(budget, bound), runs]),
1409
+ unplaced: this.unplaced.map(([end, budget]) => [end, Math.min(budget, bound)]),
1410
+ widest: bound,
1411
+ });
1412
+ }
1413
+ /** How long group `key` stays worth delivering once superseded. */
1414
+ at(key) {
1415
+ let at;
1416
+ for (const [budget, runs] of this.layers) {
1417
+ const run = runs[runsFrom(runs, key)];
1418
+ if (run !== undefined && run.start <= key) {
1419
+ at = budget;
1420
+ break;
1421
+ }
1422
+ }
1423
+ const unplaced = this.unplaced[this.#unplacedFrom(key)];
1424
+ if (unplaced !== undefined)
1425
+ at = Math.max(at ?? 0, unplaced[1]);
1426
+ return at ?? this.#widest;
1427
+ }
1428
+ /** The widest budget any group gets, counting a range not yet placed. */
1429
+ widest() {
1430
+ return this.#widest;
1431
+ }
1432
+ /** Whether every group gets the same budget. */
1433
+ isUniform() {
1434
+ return (this.layers.every(([budget]) => budget === this.#widest) &&
1435
+ this.unplaced.every(([, budget]) => budget === this.#widest));
1436
+ }
1437
+ /** Whether `other` gives every group the same budget, as this one's copy. */
1438
+ equals(other) {
1439
+ if (other === this)
1440
+ return true;
1441
+ return (other !== undefined &&
1442
+ this.#widest === other.#widest &&
1443
+ this.layers.length === other.layers.length &&
1444
+ this.layers.every(([budget, runs], i) => budget === other.layers[i][0] && runs === other.layers[i][1]) &&
1445
+ this.unplaced.length === other.unplaced.length &&
1446
+ this.unplaced.every(([end, budget], i) => end === other.unplaced[i][0] && budget === other.unplaced[i][1]));
1447
+ }
1448
+ /** `[start, end)` cut where the budget changes, each stretch with its budget, lowest first. */
1449
+ runs(start, end) {
1450
+ const runs = [];
1451
+ for (let at = start; at < end;) {
1452
+ const [budget, next] = this.stretch(at);
1453
+ const to = next === undefined ? end : min(next, end);
1454
+ runs.push([{ start: at, end: to }, budget]);
1455
+ at = to;
1456
+ }
1457
+ return runs;
1458
+ }
1459
+ /** The budget of group `key`, and the next key whose budget differs, undefined for none. */
1460
+ stretch(key) {
1461
+ const budget = this.at(key);
1462
+ if (this.isUniform())
1463
+ return [budget, undefined];
1464
+ for (let cut = this.#cutAfter(key); cut !== undefined; cut = this.#cutAfter(cut)) {
1465
+ if (this.at(cut) !== budget)
1466
+ return [budget, cut];
1467
+ }
1468
+ return [budget, undefined];
1469
+ }
1470
+ // The next key past `key` where a layer's run or an unplaced range starts or ends, so the budget
1471
+ // may change.
1472
+ #cutAfter(key) {
1473
+ let cut;
1474
+ const take = (at) => {
1475
+ if (at < UNBOUNDED)
1476
+ cut = cut === undefined ? at : min(cut, at);
1477
+ };
1478
+ for (const [, runs] of this.layers) {
1479
+ const run = runs[runsFrom(runs, key)];
1480
+ if (run !== undefined)
1481
+ take(run.start <= key ? run.end : run.start);
1482
+ }
1483
+ const unplaced = this.unplaced[this.#unplacedFrom(key)];
1484
+ if (unplaced !== undefined)
1485
+ take(unplaced[0]);
1486
+ return cut;
1487
+ }
1488
+ // The index of the first unplaced range that may cover `key`.
1489
+ #unplacedFrom(key) {
1490
+ let lo = 0;
1491
+ let hi = this.unplaced.length;
1492
+ while (lo < hi) {
1493
+ const mid = (lo + hi) >>> 1;
1494
+ if (this.unplaced[mid][0] <= key)
1495
+ lo = mid + 1;
1496
+ else
1497
+ hi = mid;
1498
+ }
1499
+ return lo;
1500
+ }
1501
+ }
1502
+ const compareTimes = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
1503
+ /**
1504
+ * {@link Timed} entries ordered by time, so the times in a run are found without a walk, and a
1505
+ * span joins or leaves its time without shifting every other.
1506
+ */
1507
+ class ByTime {
1508
+ #entries = new Sorted((entry) => entry.time, compareTimes);
1509
+ get size() {
1510
+ return this.#entries.size;
1511
+ }
1512
+ /** Every entry, lowest first. */
1513
+ *all() {
1514
+ const first = this.#entries.first();
1515
+ if (first)
1516
+ yield* this.#entries.from(first.time);
1517
+ }
1518
+ /** Every entry whose time is in `[start, end)`, lowest first. */
1519
+ *in(start, end) {
1520
+ for (const entry of this.#entries.from(start)) {
1521
+ if (entry.time >= end)
1522
+ return;
1523
+ yield entry;
1524
+ }
1525
+ }
1526
+ /** Add the span keyed `key` under `time`. */
1527
+ add(time, key) {
1528
+ const entry = this.#entries.ceil(time);
1529
+ if (entry?.time === time)
1530
+ entry.keys.add(key);
1531
+ else
1532
+ this.#entries.insert({ time, keys: new Set([key]) });
1533
+ }
1534
+ /** Take the span keyed `key` out from under `time`, if it is there. */
1535
+ remove(time, key) {
1536
+ const entry = this.#entries.ceil(time);
1537
+ if (entry?.time !== time || !entry.keys.delete(key))
1538
+ return;
1539
+ if (entry.keys.size === 0)
1540
+ this.#entries.delete(time);
1541
+ }
1542
+ /** Put back an entry taken out, whose time no entry holds. */
1543
+ put(entry) {
1544
+ this.#entries.insert(entry);
1545
+ }
1546
+ /** Take out the spans under `time`, if any. */
1547
+ take(time) {
1548
+ const entry = this.#entries.ceil(time);
1549
+ if (entry?.time !== time)
1550
+ return undefined;
1551
+ this.#entries.delete(time);
1552
+ return entry.keys;
1553
+ }
1554
+ /** Take out every entry whose time is in `[start, end)`, lowest first. */
1555
+ takeIn(start, end) {
1556
+ const taken = [];
1557
+ for (const entry of this.#entries.from(start)) {
1558
+ if (end < UNBOUNDED && entry.time >= end)
1559
+ break;
1560
+ taken.push(entry);
1561
+ }
1562
+ for (const entry of taken)
1563
+ this.#entries.delete(entry.time);
1564
+ return taken;
1565
+ }
1566
+ /** Take out every entry, lowest first. */
1567
+ takeAll() {
1568
+ const taken = [...this.all()];
1569
+ this.#entries = new Sorted((entry) => entry.time, compareTimes);
1570
+ return taken;
1571
+ }
1572
+ }
1573
+ // What `span` reads, and the part of it a miss may ask for, each undefined for none.
1574
+ function spanReads(span) {
1575
+ // A span still looking up its start reads nothing until it knows which group that is.
1576
+ if (span.looking)
1577
+ return [undefined, undefined];
1578
+ // A span waiting on its time start reads from that time meanwhile, and may ask a miss there, as
1579
+ // one reaching back past every group held does: whichever group contains it starts at or before
1580
+ // it, so every group from there on is in range.
1581
+ if (span.waiting) {
1582
+ const at = span.at ?? 0n;
1583
+ if (at >= span.end)
1584
+ return [undefined, undefined];
1585
+ return [
1586
+ { start: at, end: span.end },
1587
+ { start: at, end: span.end },
1588
+ ];
1589
+ }
1590
+ if (span.start >= span.end)
1591
+ return [undefined, undefined];
1592
+ // A time start before every group held reaches back to the group containing it, which is unknown
1593
+ // here, so it asks no miss below its time: every key from there on is in range, the key at the
1594
+ // time itself being the containing group if it exists.
1595
+ const reach = !span.floored && span.at !== undefined ? span.at : span.start;
1596
+ return [{ start: span.start, end: span.end }, reach < span.end ? { start: reach, end: span.end } : undefined];
1597
+ }
1598
+ // The times outside sorted, disjoint `runs`, as runs lowest first.
1599
+ function outside(runs) {
1600
+ const gaps = [];
1601
+ let from = 0n;
1602
+ for (const run of runs) {
1603
+ if (run.end <= run.start)
1604
+ continue;
1605
+ if (run.start > from)
1606
+ gaps.push({ start: from, end: run.start });
1607
+ from = max(from, run.end);
1608
+ }
1609
+ if (from < UNBOUNDED)
1610
+ gaps.push({ start: from, end: UNBOUNDED });
1611
+ return gaps;
1612
+ }
1613
+ // `runs` merged into sorted, disjoint runs, the empty ones dropped.
1614
+ function merge(runs) {
1615
+ const merged = [];
1616
+ for (const run of runs.filter((run) => run.end > run.start).sort((a, b) => order(a.start, b.start))) {
1617
+ const last = merged.at(-1);
1618
+ if (last && run.start <= last.end)
1619
+ last.end = max(last.end, run.end);
1620
+ else
1621
+ merged.push({ ...run });
1622
+ }
1623
+ return merged;
1624
+ }
1625
+ /**
1626
+ * The first group a range starting from time `at` starts at (undefined for the oldest group), and
1627
+ * whether it names that group rather than reaching back to the oldest one. `"pending"` while a hop
1628
+ * that holds no groups waits on a latest group, reading everything meanwhile, and `"waiting"` for
1629
+ * a time start the groups held can't place yet. Mirrors the Rust `start`.
1630
+ */
1631
+ function startOf(at, latest, budget, holdings) {
1632
+ // Nothing sits below group 0, so the oldest start needs no latest group.
1633
+ if (at === undefined)
1634
+ return { group: 0n, floored: false };
1635
+ // No group starts before 0, so time 0 names group 0 whatever is held.
1636
+ if (at === 0n)
1637
+ return { group: 0n, floored: true };
1638
+ const containing = holdings.containing(at, budget);
1639
+ if (typeof containing === "object")
1640
+ return { group: containing.at, floored: true };
1641
+ // The group holding it is older than any held, so reach back to the oldest.
1642
+ if (containing === "before")
1643
+ return { group: 0n, floored: false };
1644
+ // Before any group, which a hop that holds none (such as a reader following route copies)
1645
+ // leaves to the copies to resolve.
1646
+ if (latest === undefined && !holdings.resolves())
1647
+ return "pending";
1648
+ // Nothing can arrive any more, so nothing held reaches past it.
1649
+ if (holdings.complete())
1650
+ return { group: 0n, floored: false };
1651
+ // The groups held can't place it, so wait for one that can rather than guess from the latest:
1652
+ // a copy can see the live edge before the groups its publisher resolved the time to.
1653
+ return "waiting";
1654
+ }
1655
+ /**
1656
+ * The oldest key a fill of the group containing time start `at`, resolved to `start`, asks from,
1657
+ * when the groups held can't tell which group contains it and a handler may: it is before every
1658
+ * group held, or a group the cache evicted may start after `start`, the newest held at or before
1659
+ * it, and still at or before `at`. Starts the producer skipped hold none. Mirrors the Rust
1660
+ * `unresolved`.
1661
+ */
1662
+ function unresolved(at, start, floored, holdings) {
1663
+ if (!holdings.looksUp?.())
1664
+ return undefined;
1665
+ if (!floored)
1666
+ return holdings.nextStart(0n) === undefined ? undefined : 0n;
1667
+ // Past a group held at the start, or one starting at the time, which contains it whatever its End.
1668
+ // A start below the time no group holds is a whole group's End, where a group the cache let go may
1669
+ // start and end before the time, so the search takes it in. Mirrors the Rust `unresolved`.
1670
+ const next = holdings.nextStart(start) === start || start >= at ? start + 1n : start;
1671
+ const after = holdings.nextStart(next);
1672
+ if (after === undefined)
1673
+ return undefined;
1674
+ const gap = min(after, at + 1n);
1675
+ return holdings.neverProduced?.(next, gap) || next >= gap ? undefined : next;
1676
+ }
1677
+ //# sourceMappingURL=window.js.map