@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
@@ -1,33 +1,97 @@
1
- import { Effect, Once, Signal } from "@moq/signals";
2
- import * as Announce from "../announced.js";
3
- import { Allocator } from "../bandwidth.js";
4
- import { error, SessionCode, SessionError } from "../error.js";
1
+ import { Duration, now, sleep } from "@moq/clock";
2
+ import { Derived, Effect, Once, Signal } from "@moq/signals";
3
+ import { Handle as AuthHandle } from "../auth.js";
4
+ import { ended } from "../auth_session.js";
5
+ import { Allocator, reserved, Viewers } from "../bandwidth.js";
6
+ import { ConnectTimeout, error, reason } from "../error.js";
5
7
  import * as Path from "../path.js";
6
- import * as Time from "../time.js";
8
+ import { Follow, Observed } from "../util/observed.js";
9
+ import { after } from "../util/timer.js";
7
10
  import { wireOf } from "../wire.js";
8
- import { connect } from "./connect.js";
11
+ import { webTransportDialed } from "./browser.js";
12
+ import { establish, normalizeVersions, Reached, reachable, } from "./connect.js";
13
+ import { Dials } from "./dials.js";
14
+ import { parse } from "./endpoint.js";
15
+ import { DISCOVERY_REMOVED } from "./forward.js";
9
16
  import { DEFAULT_HANDOVER, dialed, handover, pinnedTransport, target } from "./goaway.js";
17
+ import { checkCost, connectTimeout } from "./handshake.js";
18
+ import { estimates } from "./stats.js";
19
+ import { tick } from "./ticker.js";
20
+ import { heldOf, Verdict } from "./tokens.js";
10
21
  /**
11
- * The backoff applied to whichever {@link ReloadDelay} fields a caller leaves out.
22
+ * The wait before a session on the WebSocket fallback first probes WebTransport, before jitter.
23
+ * Each failed probe doubles it, up to {@link PROBE_MAX}, matching moq-tokio.
24
+ */
25
+ const PROBE_INITIAL = Duration.secs(10);
26
+ /** The longest wait between WebTransport probes, before jitter. */
27
+ const PROBE_MAX = Duration.secs(300);
28
+ /**
29
+ * The backoff applied to whichever {@link Backoff} fields a caller leaves out.
12
30
  *
13
- * The timeout is short on purpose: a failure that clears within it was transient, and one that
14
- * doesn't should surface on {@link Reload.error} rather than leave the page silently
15
- * reconnecting for minutes. A loop nobody watches wants `timeout: 0` instead, since there is
16
- * no one to react. Giving up does not dispose the loop: a new URL or a disable/re-enable
17
- * starts another sequence.
31
+ * Retries are unlimited by default: a loop that gives up stays offline until something
32
+ * redials it, and a page usually has nothing that will, so an outage of any length has to
33
+ * recover on its own. {@link Reload.status} already shows the outage. A caller that can react
34
+ * sets a timeout to surface a persistent failure on {@link Reload.error}; giving up does not
35
+ * dispose the loop, and a new URL or a disable/re-enable starts another sequence.
18
36
  */
19
- const DEFAULT_DELAY = {
20
- initial: Time.Milli(1000),
37
+ const DEFAULT_BACKOFF = {
38
+ initial: Duration.millis(1000),
21
39
  multiplier: 2,
22
- max: Time.Milli(5000),
23
- timeout: Time.Milli(10000),
40
+ max: Duration.millis(5000),
41
+ healthy: Duration.millis(1000),
42
+ timeout: null,
24
43
  };
25
- /** How often the send-rate estimate is sampled from the live transport. */
26
- const BANDWIDTH_POLL = 100;
44
+ /**
45
+ * {@link Backoff} resolved against its defaults, with the same meaning as moq-tokio's
46
+ * backoff. Throws a RangeError for a value that has none.
47
+ *
48
+ * Field by field rather than by spread: a caller building `{ initial: maybeInitial }` from an
49
+ * optional value passes an explicit undefined, which a spread would take as the answer.
50
+ */
51
+ function pacing(backoff) {
52
+ const initial = backoff.initial ?? DEFAULT_BACKOFF.initial;
53
+ // `null` means no bound, so only `undefined` takes the default.
54
+ const cap = backoff.max === undefined ? DEFAULT_BACKOFF.max : backoff.max;
55
+ const multiplier = backoff.multiplier ?? DEFAULT_BACKOFF.multiplier;
56
+ if (!Number.isFinite(multiplier))
57
+ throw new RangeError(`invalid backoff.multiplier: ${multiplier}`);
58
+ // Zero would count every session as healthy, resetting the retry window forever under a
59
+ // peer that drops each one on sight.
60
+ const healthy = backoff.healthy ?? DEFAULT_BACKOFF.healthy;
61
+ if (healthy.isZero())
62
+ throw new RangeError("backoff.healthy must be positive");
63
+ const timeout = backoff.timeout === undefined ? DEFAULT_BACKOFF.timeout : backoff.timeout;
64
+ // A multiplier below 1 means no growth, not a delay that shrinks to nothing.
65
+ const growth = Math.max(multiplier, 1);
66
+ return {
67
+ initial: cap === null ? initial : initial.min(cap),
68
+ healthy,
69
+ timeout,
70
+ // How long a session must last to start a new run of redirects. One that outlived the
71
+ // capped delay already redials no faster than the backoff would allow. Uncapped, the
72
+ // healthy bar is the only rate the config names.
73
+ settled: cap ?? healthy,
74
+ /** The delay after one that waited `delay`. */
75
+ next(delay) {
76
+ const next = Math.min(delay.asNanos() * growth, cap?.asNanos() ?? Number.POSITIVE_INFINITY);
77
+ // Saturates, as Rust's Duration does, rather than overflowing the wait.
78
+ return Number.isFinite(next) ? Duration.nanos(next) : delay;
79
+ },
80
+ };
81
+ }
82
+ /** How often the send-rate estimate is sampled from the live transport while a reservation reads it. */
83
+ const BANDWIDTH_POLL = Duration.millis(100);
27
84
  /**
28
85
  * The reconnect loop behind a Connection handle: connects, waits for the session to end, then
29
86
  * redials with exponential backoff.
30
87
  *
88
+ * The first attempt for a URL fails fast instead: until a session to it was established (see
89
+ * {@link Backoff}), a failed attempt or a closed session sets {@link Reload.error}
90
+ * rather than retrying, so a setup mistake such as an
91
+ * untrusted certificate or no version in common surfaces at once. A page opened before its
92
+ * relay is reachable therefore stays disconnected until a new URL or a disable/re-enable,
93
+ * {@link Dials} included.
94
+ *
31
95
  * @internal
32
96
  */
33
97
  export class Reload {
@@ -35,12 +99,32 @@ export class Reload {
35
99
  url;
36
100
  /** Whether reconnecting is active. */
37
101
  enabled;
38
- /** Current connection status. */
102
+ /**
103
+ * Credentials every dial presents; see {@link ConnectProps.tokens}. Replacing them reaches
104
+ * the next dial and, on a live session with AUTH, presents the new tokens at once,
105
+ * withdrawing the replaced ones once every new one has its answer. With {@link Dials}, the set
106
+ * the latest dial signed, which only that dial presents.
107
+ */
108
+ tokens;
109
+ // Where each dial asks for its tokens, instead of reading `tokens`.
110
+ #dials;
111
+ /**
112
+ * Why the loop is retrying: how the last attempt or session ended since a session last served,
113
+ * or undefined. The next {@link Dials} dial reads it. Mirrors Rust's `Monitor::failure()`.
114
+ */
115
+ failure;
116
+ #failure = new Signal(undefined);
117
+ /**
118
+ * Current connection status. A session reads as connected only once its peer accepts the
119
+ * credential (on moq-lite-03/04, which never answer it, once it stays up past the healthy
120
+ * bar), so a refused one never does.
121
+ */
39
122
  status = new Signal("disconnected");
40
123
  /**
41
124
  * The failure that stopped retrying the current URL, or undefined while the loop is live.
42
125
  *
43
- * Set on an auth rejection or when the retry window expires. Cleared when a new URL or a
126
+ * Set when the first attempt for a URL fails, or when the retry window
127
+ * expires. Cleared when a new URL or a
44
128
  * disable/re-enable starts another sequence, not on a page hide/show. Transient drops
45
129
  * that are still being retried leave this empty.
46
130
  */
@@ -51,26 +135,55 @@ export class Reload {
51
135
  * The current connection's PROBE estimates, spanning reconnects.
52
136
  *
53
137
  * Undefined while disconnected: the estimates belong to a single connection.
54
- * See {@link Established.probe}.
138
+ * Observation requests updates; `peek()` alone is passive.
139
+ * See {@link Session.probe}.
55
140
  */
56
141
  probe;
142
+ #probeSource = new Signal(undefined);
57
143
  /**
58
144
  * Divides this connection's send-rate estimate among the tracks sharing it.
59
145
  *
60
- * The same instance across reconnects, so reservations survive a drop. The
146
+ * The same identity across reconnects, so reservations survive a drop. The
61
147
  * estimate is `undefined` while disconnected, which tells a sender to hold
62
- * its rate rather than encode at zero.
148
+ * its rate rather than encode at zero. It is sampled from the transport only
149
+ * while a reservation reads it, so the first reservation after none waits one
150
+ * snapshot for its slice. It stays `undefined` on a transport that cannot
151
+ * report one: the WebSocket fallback, WebTransport without
152
+ * `getStats()`, or Safari's stub, which reads as none.
153
+ */
154
+ allocator;
155
+ /**
156
+ * Divides this connection's receive estimate among the broadcasts watched over it.
157
+ *
158
+ * The same identity across reconnects, so viewers survive a drop, and every session
159
+ * the loop dials sends their ceilings summed as its PROBE target. A live viewer
160
+ * requests PROBE; viewers report `undefined` while disconnected.
161
+ */
162
+ viewers;
163
+ /**
164
+ * The auth handle every session of the loop is built on, spanning reconnects.
165
+ *
166
+ * Each session presents every token it holds, so a route naming one keeps it across a
167
+ * reconnect, and its grant follows the live session. It spans URL changes too, so its
168
+ * tokens go to whatever URL the loop dials next. It ends once the loop closes and its
169
+ * sessions finish draining, and aborting it stops the loop at its next reconnect, with the
170
+ * abort's error.
63
171
  */
64
- bandwidth;
172
+ auth = new AuthHandle();
173
+ // The peer's verdict on the latest tokens, across reconnects.
174
+ #verdict;
175
+ /** Protocol versions to offer, most preferred first, kept across retries and redirects. */
176
+ versions;
177
+ /** Whether the URL carries the connection credential as `auth` (not reactive); see {@link ConnectProps.urlAuth}. */
178
+ urlAuth;
65
179
  /** WebTransport options applied to each connection attempt (not reactive). */
66
180
  webtransport;
67
181
  /** WebSocket fallback options applied to each connection attempt (not reactive). */
68
182
  websocket;
69
- /**
70
- * Whether the relay supports broadcast discovery, applied to each connection attempt (not
71
- * reactive). Undefined defers to the default for the URL. See {@link Established.discovery}.
72
- */
73
- discovery;
183
+ /** The bound on each connection attempt, or `null` for none. See {@link ConnectProps.timeout}. */
184
+ timeout;
185
+ /** The price of each session's link. See {@link ConnectProps.cost}. */
186
+ cost;
74
187
  /**
75
188
  * The origin whose broadcasts are served, spanning reconnects (not reactive).
76
189
  *
@@ -83,24 +196,28 @@ export class Reload {
83
196
  * The origin fed with the peer's announced broadcasts, spanning reconnects (not
84
197
  * reactive).
85
198
  *
86
- * The entries a session fed retract when it dies, and the next session re-populates the
87
- * table, so a consumer watching the origin sees offline/online transitions across a
88
- * reconnect. See the `consume` connect option.
199
+ * The entries a session fed retract when it dies, and the next session asks again for
200
+ * whatever is still watched, so a consumer watching the origin sees offline/online
201
+ * transitions across a reconnect. See the `consume` connect option.
89
202
  */
90
203
  consume;
91
204
  /** Backoff settings for the reconnect loop; an unset field uses its default. */
92
- delay;
205
+ backoff;
93
206
  /** How to react to the peer's GOAWAY (not reactive). */
94
207
  goaway;
95
208
  /**
96
209
  * The URL an accepted GOAWAY redirect assigned, or undefined while the loop dials
97
210
  * {@link Reload.url}. Sticky across reconnects: a redirect is an assignment, not a
98
- * detour. Cleared when a new URL or a disable/re-enable starts another sequence.
211
+ * detour. Cleared when a dial to it fails, so the redial returns to {@link Reload.url},
212
+ * or when a new URL or a disable/re-enable starts another sequence.
99
213
  */
100
214
  redirect;
101
215
  /** The reactive effect scope driving the connect loop; closed by {@link Reload.close}. */
102
216
  #signals = new Effect();
103
217
  // Sampled from the live session; undefined while disconnected or the transport has none.
218
+ #sample = new Observed(undefined);
219
+ /** The latest transport sample while a reporter or bandwidth reservation needs it. */
220
+ sample = this.#sample;
104
221
  #estimate = new Signal(undefined);
105
222
  /**
106
223
  * Settles once this loop is disposed via {@link Reload.close}: `null` on a clean close,
@@ -118,69 +235,124 @@ export class Reload {
118
235
  // A session the peer sent GOAWAY on, serving its groups in flight while the replacement
119
236
  // dials. Retired when it closes, at its handover cap, or when the sequence ends.
120
237
  #draining;
121
- // Whether a connect attempt is in flight, so a retiring predecessor reports the right status.
238
+ // A redirect from the draining fallback's peer, held while a replacement serves.
239
+ #pending;
240
+ // Sessions this loop ended that are still draining, each until its close settles.
241
+ #ending = new Map();
242
+ // The current run of migrations. A redirect back to an endpoint that already sent us away is
243
+ // a loop, however healthy each session looked. Reset when a session ends without a GOAWAY,
244
+ // outlives the capped delay, or a new sequence starts.
245
+ #redirects = new Redirects();
246
+ // Whether a run is dialing: from its start until each connect attempt settles.
122
247
  #dialing = false;
123
248
  // The current wait between attempts, doubling per failure, and when the retry window expires.
124
- // Both are undefined between sequences, so a later edit to `delay` applies to the next one.
249
+ // Both are undefined between sequences, so a later edit to `backoff` applies to the next one.
125
250
  #delay;
251
+ // When the retry window expires, or null when it never does.
126
252
  #deadline;
127
253
  // The URL the current retry sequence is for. Cleared when disabled, URL-less, or given
128
254
  // up, so the next attempt starts a fresh backoff window.
129
255
  #sequenceHref;
130
256
  // The href that exhausted its retry sequence. Survives a page hide/show so resume does
131
- // not redial credentials the peer already refused. Cleared when the URL changes or the
257
+ // not restart a sequence that already gave up. Cleared when the URL changes or the
132
258
  // loop is disabled.
133
259
  #givenUpHref;
134
- // Increased by 1 each time to trigger a reload.
260
+ // The configured href a session's peer accepted the credential of or drained with a GOAWAY,
261
+ // or a moq-lite-03/04 session stayed up past the healthy bar for. Until it matches, a failed
262
+ // attempt or a closed session gives up rather than retrying. Kept across a page hide/show and a disable/re-enable, which
263
+ // resume a URL that worked.
264
+ #establishedHref;
135
265
  #tick = new Signal(0);
136
266
  // True after the browser freezes or hides the page until it visibly resumes.
137
267
  #suspended = new Signal(false);
268
+ // Watches the page for #suspended, outliving the loop until the sessions it ended settle.
269
+ #suspension = new Effect();
138
270
  // Use the serialized URL as the reactive connection key. URL objects use identity
139
271
  // equality, but replacing one with an equivalent instance should not reconnect.
140
272
  #url;
141
273
  constructor(props) {
274
+ if (props && "discovery" in props)
275
+ throw new TypeError(DISCOVERY_REMOVED);
142
276
  this.url = Signal.from(props?.url);
143
277
  this.enabled = Signal.from(props?.enabled ?? true);
144
- this.delay = props?.delay ?? {};
278
+ const tokens = props?.tokens ?? [];
279
+ this.#dials = tokens instanceof Dials ? tokens : undefined;
280
+ this.tokens = Signal.from(tokens instanceof Dials ? [] : tokens);
281
+ this.#verdict = new Verdict(this.tokens, this.auth);
282
+ this.backoff = props?.backoff ?? {};
283
+ // Validated here, like the timeout, rather than by every retry.
284
+ pacing(this.backoff);
145
285
  this.goaway = props?.goaway ?? {};
146
286
  this.redirect = this.#redirect;
287
+ this.versions = normalizeVersions(props);
288
+ this.urlAuth = props?.urlAuth;
147
289
  this.webtransport = props?.webtransport;
148
290
  this.websocket = props?.websocket;
149
- this.discovery = props?.discovery;
291
+ // Validated here, like the versions, rather than by every attempt.
292
+ this.timeout = connectTimeout(props?.timeout);
293
+ checkCost(props?.cost);
294
+ this.cost = props?.cost;
150
295
  this.publish = props?.publish;
151
296
  this.consume = props?.consume;
152
- // Requests on the consume origin stay pending across a reconnect, and before the
153
- // first session establishes, rather than reading as unroutable the moment no session
154
- // is attached. Released only when this loop is disposed: giving up the current URL
155
- // is recoverable (a new URL or a disable/re-enable starts another sequence), so a
156
- // request must keep waiting rather than go unroutable in the gap.
297
+ // The consume origin's announcement streams are not synced while no session is there to
298
+ // deliver an initial set, so an empty table before the first connect, or during a
299
+ // reconnect, never reads as nothing being announced. The next session takes its own
300
+ // hold before this one is released. Taken now rather than in the effect, which runs
301
+ // a microtask later, after a watcher created alongside would already have synced.
157
302
  if (this.consume) {
158
- this.#signals.cleanup(wireOf(this.consume).expect());
303
+ const consume = wireOf(this.consume);
304
+ let hold = this.enabled.peek() ? consume.snapshot(Path.empty()) : undefined;
305
+ this.#signals.run((effect) => {
306
+ if (effect.get(this.enabled) && !effect.get(this.established)) {
307
+ hold ??= consume.snapshot(Path.empty());
308
+ }
309
+ else {
310
+ hold?.();
311
+ hold = undefined;
312
+ }
313
+ });
314
+ this.#signals.cleanup(() => hold?.());
159
315
  }
160
316
  this.error = this.#error;
317
+ this.failure = this.#failure;
161
318
  const win = globalThis.window;
162
319
  const doc = globalThis.document;
163
320
  if (typeof win !== "undefined" && typeof doc !== "undefined") {
164
- this.#signals.event(win, "pagehide", () => this.#suspended.set(true));
165
- this.#signals.event(win, "pageshow", () => this.#suspended.set(false));
166
- this.#signals.event(win, "unload", () => this.#suspended.set(true));
167
- this.#signals.event(doc, "visibilitychange", () => {
321
+ this.#suspension.event(win, "pagehide", () => this.#suspended.set(true));
322
+ this.#suspension.event(win, "pageshow", () => this.#suspended.set(false));
323
+ this.#suspension.event(win, "unload", () => this.#suspended.set(true));
324
+ this.#suspension.event(doc, "visibilitychange", () => {
168
325
  if (!doc.hidden)
169
326
  this.#suspended.set(false);
170
327
  });
328
+ // Nothing would run to finish a drain begun before the page went away either.
329
+ this.#suspension.run((effect) => {
330
+ if (!effect.get(this.#suspended))
331
+ return;
332
+ for (const connection of this.#ending.keys())
333
+ connection.abort();
334
+ this.#ending.clear();
335
+ });
171
336
  }
172
- this.probe = this.#signals.computed((effect) => {
173
- const connection = effect.get(this.established);
174
- return connection && effect.get(connection.probe);
337
+ this.probe = new Follow(this.#probeSource);
338
+ this.#signals.run((effect) => {
339
+ effect.set(this.#probeSource, effect.get(this.established)?.probe, undefined);
175
340
  });
176
- this.bandwidth = new Allocator(this.#estimate);
177
- this.#signals.cleanup(() => this.bandwidth.close());
178
- // The transport has no event for the send-rate estimate, so sample on our
179
- // own schedule and skip a tick while the previous snapshot is outstanding.
341
+ this.allocator = new Allocator(this.#estimate);
342
+ this.#signals.cleanup(() => this.allocator.close());
343
+ // Lazy: the registry observes the estimate only while a viewer is live, and only
344
+ // that observation requests PROBE from the current session.
345
+ this.viewers = new Viewers(new Derived([this.probe], (probe) => probe?.estimatedRecvRate));
346
+ this.#signals.cleanup(() => this.viewers.close());
347
+ // Share the allocator's poll with report readers. Skip an overlapping request, and
348
+ // clear the sample when the session changes or the last consumer leaves.
180
349
  this.#signals.run((effect) => {
181
350
  effect.set(this.#estimate, undefined);
351
+ effect.set(this.#sample, undefined);
352
+ if (!effect.get(reserved(this.allocator)) && !effect.get(this.#sample.wanted))
353
+ return;
182
354
  const connection = effect.get(this.established);
183
- if (!connection)
355
+ if (!connection || !estimates(connection))
184
356
  return;
185
357
  let pending = false;
186
358
  const sample = async () => {
@@ -188,19 +360,34 @@ export class Reload {
188
360
  return;
189
361
  pending = true;
190
362
  try {
191
- const stats = await effect.race(connection.stats());
192
- if (stats)
363
+ const at = now();
364
+ const stats = await effect.race([connection.stats()]);
365
+ if (stats) {
366
+ this.#sample.set({ ...stats, at });
193
367
  this.#estimate.set(stats.estimatedSendRate);
368
+ }
194
369
  }
195
370
  finally {
196
371
  pending = false;
197
372
  }
198
373
  };
199
374
  void sample();
200
- effect.interval(() => void sample(), BANDWIDTH_POLL);
375
+ // One page-wide ticker samples every connection in the same wakeup.
376
+ effect.cleanup(tick(BANDWIDTH_POLL, () => void sample()));
377
+ });
378
+ // The live session holds the current tokens on AUTH; a dial picks them up itself.
379
+ this.#signals.run((effect) => {
380
+ const tokens = effect.get(this.tokens);
381
+ // Before the held tokens change, so a token the set kept is judged by what it was.
382
+ this.#verdict.refresh();
383
+ // A signed set belongs to its own dial: another session presenting it would spend it.
384
+ if (this.#dials)
385
+ return;
386
+ const connection = effect.get(this.established);
387
+ if (connection)
388
+ heldOf(connection)?.update(tokens);
201
389
  });
202
390
  this.#url = this.#signals.computed((effect) => effect.get(this.url)?.href);
203
- // Create a reactive root so cleanup is easier.
204
391
  this.#signals.run(this.#connect.bind(this));
205
392
  }
206
393
  #connect(effect) {
@@ -240,84 +427,241 @@ export class Reload {
240
427
  this.#error.set(undefined);
241
428
  this.#givenUpHref = undefined;
242
429
  }
243
- // A drained predecessor still serves while its replacement dials.
244
- if (!this.#draining)
245
- this.status.set("connecting");
430
+ this.#setDialing(true);
246
431
  // This run's teardown, handed to connect() so a rerun cancels the attempt in flight.
247
432
  const signal = effect.abort;
248
- // The session this run serves, closed with the run. A drained one leaves this slot
433
+ // The session this run serves, ended with the run. A drained one leaves this slot
249
434
  // for #draining, which outlives the run.
250
435
  let current;
436
+ // Set once the session is live, so #retry can tell a healthy session that
437
+ // later dropped from a connect failure or a peer that flaps immediately.
438
+ let connected;
251
439
  effect.cleanup(() => {
252
440
  if (current) {
253
- current.close();
441
+ // A session torn down while live, as on a page hide, may already have earned it.
442
+ if (connected !== undefined)
443
+ this.#settle(href, current, connected);
444
+ this.#end(current);
254
445
  if (this.established.peek() === current)
255
- this.established.set(undefined);
446
+ this.#serve(undefined);
256
447
  current = undefined;
257
448
  }
258
- if (this.established.peek() === undefined)
259
- this.status.set("disconnected");
449
+ this.#setDialing(false);
260
450
  });
261
451
  effect.spawn(async () => {
262
- // Set once the session is live, so #retry can tell a healthy session that
263
- // later dropped from a connect failure or a peer that flaps immediately.
264
- let connected;
452
+ // Whether the last failure was a dial to a GOAWAY redirect's target.
453
+ let redirectFailed = false;
265
454
  try {
455
+ // The WebTransport session a probe opened, which the next dial finishes its MoQ
456
+ // handshake over instead of racing.
457
+ let reached;
266
458
  // Loops only to migrate: a GOAWAY off a healthy session dials its replacement
267
459
  // straight away, with no backoff.
268
460
  for (;;) {
269
461
  const dialing = this.#redirect.peek() ?? url;
270
- this.#dialing = true;
462
+ // The retry window counts from the first dial of a run, so a dial that hangs
463
+ // spends it too.
464
+ this.#deadline ??= this.#window(now());
465
+ this.#setDialing(true);
466
+ this.#verdict.dialing();
467
+ let tokens = reached?.tokens;
468
+ // Signing spends the attempt's own timeout, so a stuck signer fails like a slow dial.
469
+ let timeout = this.timeout;
470
+ if (!tokens && this.#dials) {
471
+ let signed;
472
+ try {
473
+ signed = await this.#sign(this.#dials, dialing, signal, this.#failure.peek());
474
+ }
475
+ catch (err) {
476
+ this.#setDialing(false);
477
+ // A redirect's target left unanswered falls back to the URL, as one that failed does.
478
+ redirectFailed = this.#redirect.peek() !== undefined;
479
+ throw err;
480
+ }
481
+ // A teardown that closed the dials is no refusal.
482
+ if (signal.aborted)
483
+ return;
484
+ if ("refused" in signed) {
485
+ this.#setDialing(false);
486
+ console.warn("dial refused by its signer, not retrying:", signed.refused);
487
+ this.#giveUp(signed.refused);
488
+ return;
489
+ }
490
+ tokens = signed.tokens;
491
+ timeout = signed.left;
492
+ }
493
+ tokens ??= this.tokens.peek();
494
+ // A probe's dial signed its own set too, which `answered()` judges from here.
495
+ if (this.#dials)
496
+ this.tokens.set(tokens);
271
497
  let connection;
498
+ const reusing = reached;
499
+ reached = undefined;
272
500
  try {
273
- connection = await connect({
501
+ connection = await establish({
274
502
  url: dialing,
503
+ tokens,
504
+ urlAuth: this.urlAuth,
275
505
  // A redirect names the relay; a fallback URL pinned for the old one does not follow.
276
- websocket: this.#redirect.peek() ? { ...this.websocket, url: undefined } : this.websocket,
506
+ websocket: this.#redirect.peek()
507
+ ? { ...this.websocket, url: undefined }
508
+ : this.websocket,
509
+ versions: this.versions,
277
510
  webtransport: this.webtransport,
278
- discovery: this.discovery,
511
+ timeout,
279
512
  publish: this.publish,
280
513
  consume: this.consume,
514
+ cost: this.cost,
515
+ auth: this.auth,
516
+ viewers: this.viewers,
281
517
  signal,
282
- });
518
+ }, reusing);
283
519
  }
284
- finally {
285
- this.#dialing = false;
520
+ catch (err) {
521
+ this.#setDialing(false);
522
+ redirectFailed = this.#redirect.peek() !== undefined;
523
+ throw err;
286
524
  }
287
- // Hand the connection to the effect, which closes it now if this run is already over.
525
+ // Hand the connection to the effect. One that lands after this run is over never
526
+ // served anything, so there is nothing to drain.
288
527
  if (signal.aborted) {
289
- connection.close();
528
+ connection.abort();
290
529
  return;
291
530
  }
292
531
  current = connection;
293
- // The replacement serves now; a predecessor keeps draining its groups in flight.
294
- this.established.set(connection);
295
- this.status.set("connected");
296
- connected = performance.now();
532
+ this.#verdict.connected(connection);
533
+ connected = now();
534
+ // Serve only once the session is established: the peer accepted its credential or
535
+ // drained it with a GOAWAY. On a version that never answers (moq-lite-03/04),
536
+ // staying up past the healthy bar is the only sign, so it counts there alone.
537
+ // Until then it may still be a refusal, which never reads as connected. A close
538
+ // first is handled below.
539
+ const auth = wireOf(connection).auth;
540
+ const bar = this.#pacing().healthy;
541
+ const acceptance = auth.accepted;
542
+ if (!acceptance.peek()) {
543
+ const waiting = new AbortController();
544
+ const waits = [Signal.race(acceptance), connection.closed, connection.draining];
545
+ if (auth.silent)
546
+ waits.push(sleep(bar, { signal: waiting.signal }).catch(() => { }));
547
+ await effect.race(waits).finally(() => waiting.abort());
548
+ if (signal.aborted)
549
+ return;
550
+ }
551
+ if (acceptance.peek() ||
552
+ connection.draining.peek() !== undefined ||
553
+ (auth.silent && now().since(connected).ge(bar))) {
554
+ // The replacement serves now; a predecessor keeps draining its groups in flight.
555
+ this.#serve(connection);
556
+ this.#failure.set(undefined);
557
+ }
558
+ this.#setDialing(false);
297
559
  // A cancelled effect resolves undefined, so the sentinel tells the session
298
560
  // closing (null for clean, an Error otherwise) apart from this run being
299
- // torn down. Anything else is the peer's GOAWAY.
300
- const ended = await effect.race(connection.closed, wireOf(connection).goaway);
301
- if (ended === undefined)
561
+ // torn down. Anything else is the peer's GOAWAY, or WebTransport reachable
562
+ // again under a session on the WebSocket fallback.
563
+ const probing = new AbortController();
564
+ const probe = this.#probe(connection, dialing, probing.signal);
565
+ const first = await effect
566
+ .race([connection.closed, connection.draining, probe])
567
+ .finally(() => probing.abort());
568
+ // The peer's GOAWAY wins over a probe that got through in the same turn, so its
569
+ // redirect is never lost.
570
+ const drain = connection.draining.peek();
571
+ const raced = first instanceof Reached && drain !== undefined ? drain : first;
572
+ // A probe the race did not take still holds an open WebTransport session.
573
+ void probe.then((left) => {
574
+ if (left !== raced)
575
+ left.transport.close();
576
+ });
577
+ if (raced === undefined)
302
578
  return;
579
+ // Leaving the fallback is a GOAWAY of our own: the session drains as the
580
+ // predecessor while the replacement finishes its handshake over the probe.
581
+ if (raced instanceof Reached) {
582
+ console.info("WebTransport reachable again; leaving the WebSocket fallback");
583
+ wireOf(connection).leave();
584
+ reached = raced;
585
+ }
586
+ const ended = raced instanceof Reached ? { uri: "" } : raced;
587
+ this.#settle(href, connection, connected);
303
588
  if (ended === null || ended instanceof Error) {
304
- console.warn("connection closed, reconnecting");
589
+ // Until a session was established, a close stops the URL as a failed attempt
590
+ // does: it is how a relay refuses a live session before accepting its credential.
591
+ // No error kind is read.
592
+ if (this.#establishedHref !== href) {
593
+ console.warn(`connection closed, not retrying: ${ended ? reason(ended) : "no error"}`);
594
+ current = undefined;
595
+ this.#serve(undefined);
596
+ this.#giveUp(ended ?? new Error("connection closed"));
597
+ return;
598
+ }
599
+ console.warn(`connection closed, reconnecting: ${ended ? reason(ended) : "no error"}`);
600
+ this.#redirects.reset();
601
+ current = undefined;
602
+ // A predecessor still draining serves again, so this is no outage.
305
603
  if (this.established.peek() === connection)
306
- this.established.set(undefined);
604
+ this.#serve(this.#draining?.serving());
605
+ this.#applyPending();
307
606
  this.#retry(effect, connected, ended ?? undefined);
308
607
  return;
309
608
  }
310
609
  current = undefined;
610
+ // A peer's GOAWAY establishes the URL however soon it came: the peer accepted the
611
+ // session and asked us to move, which a refusal never does, so a page opened while
612
+ // its relay drains rides out the restart. Leaving the fallback is our own.
613
+ if (!reached)
614
+ this.#establishedHref = href;
311
615
  // A pinned WebSocket URL can win the race against the primary. Judge the
312
616
  // redirect against that endpoint, not the primary we never reached.
313
617
  const socket = this.#redirect.peek() ? undefined : this.websocket?.url;
314
- if (!this.#migrate(connection, dialed(dialing, connection.transport, socket), ended))
618
+ const at = dialed(dialing, connection.transport, socket);
619
+ const before = this.#redirect.peek();
620
+ if (!this.#migrate(connection, at, ended)) {
621
+ reached?.transport.close();
622
+ return;
623
+ }
624
+ // Moving off the fallback is no peer's redirect or restart, and the probe
625
+ // backoff already paces it, so its replacement dials at once. A fallback that
626
+ // served past the healthy bar still earns the healthy reset.
627
+ if (reached) {
628
+ this.#follow(connection, at, pinnedTransport(connection.transport, this.#pinned()), href);
629
+ if (now().since(connected).ge(this.#pacing().healthy)) {
630
+ this.#delay = undefined;
631
+ this.#deadline = undefined;
632
+ }
633
+ continue;
634
+ }
635
+ const after = this.#redirect.peek();
636
+ // Two peers that each hold a session past the healthy bar can still bounce us between
637
+ // them forever. Returning to an endpoint that already sent us elsewhere in this
638
+ // run of migrations escalates the backoff, which never resets mid-loop, so the
639
+ // retry window still applies. A one-way rolling restart never revisits one, and
640
+ // a GOAWAY back to the same place is a restart, not a move, so it never counts.
641
+ // A session that outlived the capped delay already redials no faster than the
642
+ // backoff would allow, so it starts a new run: rolling restarts days apart
643
+ // never add up to a loop.
644
+ const { settled, healthy } = this.#pacing();
645
+ const lived = now().since(connected);
646
+ if (lived.ge(settled))
647
+ this.#redirects.reset();
648
+ // Only a redirect this GOAWAY named is a move. An empty or ignored one reconnects
649
+ // in place, even when that reruns the race a WebSocket fallback won.
650
+ const from = destination(at);
651
+ const to = after && after !== before ? destination(after) : from;
652
+ if (to !== from && this.#redirects.move(from, to)) {
653
+ this.#retry(effect, undefined, new Error("redirect loop"));
315
654
  return;
316
- // A session that outlived the initial delay was healthy, so its handover is not a
317
- // failure. One redirected almost at once still migrates, but through the backoff,
318
- // so two peers bouncing us between them escalate and eventually give up.
319
- if (performance.now() - connected < this.#initial()) {
320
- this.#retry(effect, connected, new Error("peer redirected immediately"));
655
+ }
656
+ // A healthy session's handover is not a failure. One redirected sooner still
657
+ // migrates, but through the backoff, so two peers bouncing us between them
658
+ // escalate to the capped delay. An empty URI is a peer restarting, which drains
659
+ // each new session until it goes; every one serves, so that is no failure either.
660
+ if (lived.lt(healthy)) {
661
+ if (ended.uri === "")
662
+ this.#restart(effect);
663
+ else
664
+ this.#retry(effect, connected, new Error("peer redirected immediately"));
321
665
  return;
322
666
  }
323
667
  this.#delay = undefined;
@@ -325,24 +669,122 @@ export class Reload {
325
669
  }
326
670
  }
327
671
  catch (err) {
328
- // Treat teardown as cancellation, not a connection failure.
329
672
  if (signal.aborted)
330
673
  return;
674
+ // An ended auth handle refuses every later session, so no retry can recover.
675
+ const closed = ended(this.auth);
676
+ if (closed) {
677
+ this.#giveUp(closed);
678
+ return;
679
+ }
680
+ // Attempts fail fast until a session was established: a URL whose peer never accepted
681
+ // one is more likely misconfigured than restarting, and only the app can fix that.
682
+ if (this.#establishedHref !== href) {
683
+ console.warn("connection failed, not retrying:", err);
684
+ this.#giveUp(error(err));
685
+ return;
686
+ }
331
687
  console.warn("connection error:", err);
688
+ // A redirect target that no longer answers is dropped: the redial returns to the
689
+ // configured URL, which may still answer. Rust's Reconnect follows the same rule.
690
+ if (redirectFailed)
691
+ this.#redirect.set(undefined);
332
692
  this.#retry(effect, connected, err);
333
693
  }
334
694
  });
335
695
  }
696
+ /**
697
+ * Ask `dials` for the tokens of a dial to `dialing`, within the connect timeout, which the
698
+ * signing spends: the tokens and the timeout left for the dial, or the app's refusal. Rejects
699
+ * with `ConnectTimeout` once the timeout passes unanswered, or with the teardown once `signal` aborts.
700
+ */
701
+ async #sign(dials, dialing, signal, failure) {
702
+ const limit = this.timeout;
703
+ const started = now();
704
+ const bound = new AbortController();
705
+ const abort = () => bound.abort(signal.reason);
706
+ if (signal.aborted)
707
+ abort();
708
+ else
709
+ signal.addEventListener("abort", abort, { once: true });
710
+ const disarm = limit === null ? () => { } : after(limit, () => bound.abort(new ConnectTimeout(limit)));
711
+ try {
712
+ const answer = await dials.request(dialing, bound.signal, failure);
713
+ if ("refused" in answer)
714
+ return answer;
715
+ if (limit === null)
716
+ return { tokens: answer.tokens, left: null };
717
+ // Zero is refused as a timeout, so a dial with no time left times out here instead.
718
+ const left = limit.saturatingSub(now().since(started));
719
+ if (left.isZero())
720
+ throw new ConnectTimeout(limit);
721
+ return { tokens: answer.tokens, left };
722
+ }
723
+ finally {
724
+ signal.removeEventListener("abort", abort);
725
+ disarm();
726
+ }
727
+ }
728
+ /**
729
+ * Resolve with the WebTransport session that reaches `dialing` while `connection` runs on the
730
+ * WebSocket fallback, probing on a jittered backoff from {@link PROBE_INITIAL} up to {@link PROBE_MAX}.
731
+ * Never resolves for a session on another transport, where WebTransport is unsupported, or
732
+ * once `signal` aborts.
733
+ */
734
+ async #probe(connection, dialing, signal) {
735
+ const never = new Promise(() => { });
736
+ if (connection.transport !== "websocket" || !webTransportDialed(this.webtransport))
737
+ return never;
738
+ try {
739
+ for (let delay = PROBE_INITIAL;; delay = delay.mul(2).min(PROBE_MAX)) {
740
+ await sleep(delay.mul(0.5 + Math.random() / 2), { signal });
741
+ let tokens = this.tokens.peek();
742
+ let timeout = this.timeout;
743
+ if (this.#dials) {
744
+ let signed;
745
+ try {
746
+ // The serving session is fine, so the probe reports no failure.
747
+ signed = await this.#sign(this.#dials, dialing, signal, undefined);
748
+ }
749
+ catch (err) {
750
+ if (signal.aborted)
751
+ throw err;
752
+ console.warn("a probe's tokens timed out:", err);
753
+ continue;
754
+ }
755
+ if ("refused" in signed) {
756
+ console.warn("a probe's dial was refused by its signer:", signed.refused);
757
+ continue;
758
+ }
759
+ tokens = signed.tokens;
760
+ timeout = signed.left;
761
+ }
762
+ const props = {
763
+ tokens,
764
+ urlAuth: this.urlAuth,
765
+ versions: this.versions,
766
+ webtransport: this.webtransport,
767
+ websocket: this.#redirect.peek() ? undefined : this.websocket,
768
+ timeout,
769
+ };
770
+ const reached = await reachable(dialing, props, signal);
771
+ if (reached)
772
+ return reached;
773
+ }
774
+ }
775
+ catch {
776
+ // The sleep rejects once the session ends.
777
+ return never;
778
+ }
779
+ }
336
780
  /**
337
781
  * Act on the peer's GOAWAY for `connection`, which was dialed at `dialing`: resolve where
338
782
  * to go next and leave the old session serving until it drains. Returns false when the
339
783
  * redirect is refused, which ends the sequence rather than redialing.
340
784
  */
341
785
  #migrate(connection, dialing, drain) {
342
- const hashes = this.webtransport?.serverCertificateHashes?.length ?? 0;
343
- const configured = hashes > 0 || this.webtransport?.serverCertificate !== undefined;
344
786
  // The pin is a WebTransport option. A WebSocket that won the race never used it.
345
- const pinned = pinnedTransport(connection.transport, configured);
787
+ const pinned = pinnedTransport(connection.transport, this.#pinned());
346
788
  let next;
347
789
  try {
348
790
  next = target(this.goaway.redirect ?? "same-host", drain.uri, dialing, pinned);
@@ -351,9 +793,8 @@ export class Reload {
351
793
  // The peer is leaving and named somewhere we won't go: redialing the old address
352
794
  // would ignore it, so stop here.
353
795
  console.warn("GOAWAY redirect refused:", err);
354
- connection.close();
355
- this.established.set(undefined);
356
- this.status.set("disconnected");
796
+ this.#end(connection);
797
+ this.#serve(undefined);
357
798
  this.#giveUp(error(err));
358
799
  return false;
359
800
  }
@@ -363,23 +804,147 @@ export class Reload {
363
804
  this.#redirectHref = this.#sequenceHref;
364
805
  }
365
806
  console.info("GOAWAY received; migrating");
366
- // A newer GOAWAY retires an older predecessor rather than holding two open.
367
- this.#draining?.retire();
807
+ // A newer GOAWAY retires an older predecessor rather than serving from two.
808
+ this.#retire();
368
809
  const cap = handover(this.goaway.handover ?? DEFAULT_HANDOVER, drain.timeout);
369
810
  const draining = new Draining(connection, cap, () => {
370
811
  if (this.#draining === draining)
371
812
  this.#draining = undefined;
372
813
  // If nothing replaced it yet, nothing is serving.
373
- if (this.established.peek() !== connection)
374
- return;
375
- this.established.set(undefined);
376
- this.status.set(this.#dialing ? "connecting" : "disconnected");
814
+ if (this.established.peek() === connection)
815
+ this.#serve(undefined);
377
816
  });
378
817
  this.#draining = draining;
379
818
  return true;
380
819
  }
381
- #initial() {
382
- return this.delay?.initial ?? DEFAULT_DELAY.initial;
820
+ /** Whether a certificate pin is configured for WebTransport. */
821
+ #pinned() {
822
+ const hashes = this.webtransport?.serverCertificateHashes?.length ?? 0;
823
+ return hashes > 0 || this.webtransport?.serverCertificate !== undefined;
824
+ }
825
+ /**
826
+ * Follow a redirect the peer of `connection`, a fallback session we left for a probe that
827
+ * got through, sends while it drains: whenever nothing else serves, the next dial goes where
828
+ * the peer named, and a dial still headed for the leaving peer starts over. A restart in
829
+ * place or a refused redirect is ignored, since we are already leaving. No loop scoring:
830
+ * only a probe that got through leads here, and the probe's backoff paces those.
831
+ */
832
+ #follow(connection, dialing, pinned, href) {
833
+ void connection.draining.then((drain) => {
834
+ // The peer accepted the fallback session and asked us to move, so its GOAWAY establishes
835
+ // the URL as one off the live session does, while the sequence still targets it.
836
+ if (this.#sequenceHref === href)
837
+ this.#establishedHref = href;
838
+ let next;
839
+ try {
840
+ next = target(this.goaway.redirect ?? "same-host", drain.uri, dialing, pinned);
841
+ }
842
+ catch {
843
+ return;
844
+ }
845
+ if (!next)
846
+ return;
847
+ this.#pending = { connection, next };
848
+ this.#applyPending();
849
+ });
850
+ }
851
+ /**
852
+ * Apply the pending redirect once its session is the one serving, which a replacement that
853
+ * closes hands back to; drop it once that session retires.
854
+ */
855
+ #applyPending() {
856
+ const pending = this.#pending;
857
+ if (!pending)
858
+ return;
859
+ if (this.#draining?.serving() !== pending.connection) {
860
+ this.#pending = undefined;
861
+ return;
862
+ }
863
+ if (this.established.peek() !== pending.connection)
864
+ return;
865
+ this.#pending = undefined;
866
+ console.info("draining fallback peer redirected; following");
867
+ this.#redirect.set(pending.next);
868
+ this.#redirectHref = this.#sequenceHref;
869
+ // A backoff still paces the next dial, which now goes to the new target.
870
+ if (this.#dialing)
871
+ this.#tick.update((prev) => prev + 1);
872
+ }
873
+ /**
874
+ * End a session this loop is done with. Its groups in flight finish within the close
875
+ * deadline, unless the page is going away: nothing would run to finish them, and an abort
876
+ * closes the transport now rather than leaving the browser to sever it.
877
+ */
878
+ #end(connection) {
879
+ if (this.#suspended.peek()) {
880
+ connection.abort();
881
+ return;
882
+ }
883
+ // A served live track never finishes, so the deadline aborting the rest is the usual end.
884
+ const ending = connection.close().catch(() => { });
885
+ this.#ending.set(connection, ending);
886
+ void ending.then(() => this.#ending.delete(connection));
887
+ }
888
+ /** Serve `connection`, or nothing. */
889
+ #serve(connection) {
890
+ this.established.set(connection);
891
+ this.#report();
892
+ }
893
+ #setDialing(dialing) {
894
+ this.#dialing = dialing;
895
+ this.#report();
896
+ }
897
+ // The one derivation of status: serving, else dialing, else neither. A drained predecessor
898
+ // serves until it retires, so its replacement's dial reads as connected.
899
+ #report() {
900
+ this.status.set(this.established.peek() ? "connected" : this.#dialing ? "connecting" : "disconnected");
901
+ }
902
+ /** Stop serving from a drained predecessor, ending it if still open. */
903
+ #retire() {
904
+ const draining = this.#draining?.retire();
905
+ if (draining)
906
+ this.#end(draining);
907
+ }
908
+ // A session establishes its URL once the peer accepted its credential, which a closed
909
+ // session still reports, or, on a version that never answers the credential
910
+ // (moq-lite-03/04), once it stayed up past the healthy bar.
911
+ #settle(href, session, connected) {
912
+ const auth = wireOf(session).auth;
913
+ if (auth.accepted.peek() || (auth.silent && now().since(connected).ge(this.#pacing().healthy))) {
914
+ this.#establishedHref = href;
915
+ }
916
+ }
917
+ // Resolved per use rather than at construction, so an edit to `backoff` (including one that
918
+ // drops a field back to its default) applies to the next retry.
919
+ #pacing() {
920
+ return pacing(this.backoff ?? {});
921
+ }
922
+ // When a retry window starting `at` expires.
923
+ #window(at) {
924
+ const { timeout } = this.#pacing();
925
+ return timeout === null ? null : at.saturatingAdd(timeout);
926
+ }
927
+ /**
928
+ * Wait out the backoff after a restarting peer drained a session short of the healthy bar.
929
+ * The delay escalates, so the redials cannot hammer it, but the wait never spends the retry
930
+ * window: the window restarts once the wait is over.
931
+ */
932
+ #restart(effect) {
933
+ const wait = this.#backoff();
934
+ const tick = this.#tick.peek() + 1;
935
+ effect.timer(() => {
936
+ this.#deadline = undefined;
937
+ this.#tick.update((prev) => Math.max(prev, tick));
938
+ }, wait);
939
+ }
940
+ // The jittered wait before the next attempt, escalating the delay for the one after.
941
+ // Equal jitter, so a fleet of tabs knocked offline together doesn't reconnect on the same tick.
942
+ #backoff() {
943
+ const pacing = this.#pacing();
944
+ this.#delay ??= pacing.initial;
945
+ const wait = this.#delay.mul(0.5 + Math.random() / 2);
946
+ this.#delay = pacing.next(this.#delay);
947
+ return wait;
383
948
  }
384
949
  /**
385
950
  * Schedule the next connect attempt after the current backoff, or stop once the retry window
@@ -387,171 +952,232 @@ export class Reload {
387
952
  * `cause` the error that killed it, if it died with one.
388
953
  */
389
954
  #retry(effect, connected, cause) {
390
- // Resolved per sequence rather than at construction, so an edit to `delay` (including
391
- // one that drops a field back to its default) applies to the next retry. Field by
392
- // field rather than by spread: a caller building `{ initial: maybeInitial }` from an
393
- // optional value passes an explicit undefined, which a spread would take as the
394
- // answer, turning the backoff into NaN or the window into forever.
395
- const delay = this.delay ?? {};
396
- const initial = this.#initial();
397
- const multiplier = delay.multiplier ?? DEFAULT_DELAY.multiplier;
398
- const max = delay.max ?? DEFAULT_DELAY.max;
399
- const timeout = delay.timeout ?? DEFAULT_DELAY.timeout;
400
- // Report disconnected during the backoff rather than when the retry reruns the
401
- // effect, unless a drained predecessor still serves until it retires.
402
- if (this.established.peek() === undefined)
403
- this.status.set("disconnected");
404
- // A session that outlived the initial delay was healthy, so clear the backoff and
405
- // start a fresh retry window: a one-off drop should reconnect promptly. Anything
406
- // shorter is a peer that accepts and immediately severs, which has to keep
407
- // escalating or we hammer it forever at the initial delay.
408
- if (connected !== undefined && performance.now() - connected >= initial) {
955
+ this.#failure.set(cause === undefined ? new Error("connection closed") : error(cause));
956
+ // A healthy session's end clears the backoff and starts a fresh retry window: a one-off
957
+ // drop should reconnect promptly. Anything shorter is a peer that accepts and soon
958
+ // severs, which has to keep escalating or we hammer it forever at the initial delay.
959
+ if (connected !== undefined && now().since(connected).ge(this.#pacing().healthy)) {
409
960
  this.#delay = undefined;
410
961
  this.#deadline = undefined;
411
962
  }
412
- // An auth rejection stops this URL however long the session lived. UNAUTHORIZED is a
413
- // specified code rather than one we guessed at, so this is the peer saying these
414
- // credentials will never work; retrying them just burns the window. Matches
415
- // moq-tokio's reconnect loop, which stops on the same close. A new URL or a
416
- // disable/re-enable starts another sequence; the handle itself is not disposed.
417
- //
418
- // Only a session close says that. The stream registry gives 2 to DELIVERY_TIMEOUT,
419
- // so a stream reset during the SETUP exchange would otherwise suppress reconnect
420
- // for good.
421
- if (cause instanceof SessionError && cause.code === SessionCode.Unauthorized) {
422
- console.warn("session rejected as unauthorized, not retrying");
423
- this.#giveUp(cause);
424
- return;
425
- }
426
- const now = performance.now();
427
- this.#delay ??= initial;
428
- this.#deadline ??= timeout > 0 ? now + timeout : Number.POSITIVE_INFINITY;
429
- if (now >= this.#deadline) {
963
+ const at = now();
964
+ // A healthy session's end starts the window here.
965
+ this.#deadline ??= this.#window(at);
966
+ const wait = this.#backoff();
967
+ // No attempt starts once the window has passed: a wait that reaches the deadline gives up
968
+ // there instead of dialing.
969
+ const deadline = this.#deadline;
970
+ // A graceful close has no error, so report the timeout itself.
971
+ const failure = cause === undefined ? new Error("reconnect timed out") : error(cause);
972
+ const giveUp = () => {
430
973
  console.warn("reconnect timed out");
431
- // A graceful close has no error, so report the timeout itself.
432
- this.#giveUp(cause === undefined ? new Error("reconnect timed out") : error(cause));
974
+ this.#giveUp(failure);
975
+ };
976
+ const left = deadline === null ? undefined : deadline.since(at);
977
+ if (left !== undefined && wait.ge(left)) {
978
+ if (!left.isZero())
979
+ effect.timer(giveUp, left);
980
+ else
981
+ giveUp();
433
982
  return;
434
983
  }
435
- // Equal jitter, so a fleet of tabs knocked offline together doesn't reconnect on the same
436
- // tick, and never past the deadline the retry window promised.
437
- const wait = Math.min(this.#delay * (0.5 + Math.random() / 2), this.#deadline - now);
438
- this.#delay = Math.min(this.#delay * multiplier, max);
439
984
  const tick = this.#tick.peek() + 1;
440
- effect.timer(() => this.#tick.update((prev) => Math.max(prev, tick)), wait);
985
+ effect.timer(() => {
986
+ // Checked again when the wait ends: a timer that fires late, as after the device
987
+ // slept, must not dial past the window.
988
+ if (deadline !== null && now().ge(deadline))
989
+ return giveUp();
990
+ this.#tick.update((prev) => Math.max(prev, tick));
991
+ }, wait);
441
992
  }
442
993
  #giveUp(cause) {
994
+ this.#verdict.gaveUp(cause);
443
995
  this.#error.set(cause);
444
996
  this.#givenUpHref = this.#sequenceHref;
445
997
  this.#resetSequence();
446
998
  }
447
999
  #resetSequence() {
1000
+ this.#failure.set(undefined);
448
1001
  this.#delay = undefined;
449
1002
  this.#deadline = undefined;
450
1003
  this.#sequenceHref = undefined;
451
- this.#draining?.retire();
1004
+ this.#redirects.reset();
1005
+ this.#retire();
452
1006
  }
453
1007
  /**
454
- * Subscribe to broadcast announcements matching `scope`, spanning reconnects.
1008
+ * Resolve once the peer first answers the latest {@link tokens}, like Rust's `Tokens::poll_answered`.
1009
+ * With {@link Dials}, that is the set the latest dial was answered with, none before the first:
1010
+ * read a refusal from the next dial's `failure` instead.
455
1011
  *
456
- * The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active
457
- * is retracted (a `retracted` update) whenever the connection drops and re-announced on
458
- * reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.
459
- *
460
- * Stays empty while the relay lacks {@link Established.discovery}.
1012
+ * It covers the tokens the latest set newly presented, or the initial ones before any set:
1013
+ * each is granted in band, or is the connection credential a dial carried and the peer
1014
+ * accepted. A token the set kept, the connection credential included, counts only once the
1015
+ * peer refuses it, and nothing is re-checked after the answer. Rejects with the peer's refusal,
1016
+ * `Unsupported` when a session cannot carry a token in band, the loop's end, or the failure that
1017
+ * stopped it retrying its URL while no session answered, such as a refused first dial. A set replaced
1018
+ * before its answer moves the wait to the new one.
461
1019
  */
462
- announced(scope = Path.Pattern.all(), options) {
463
- // With a consume origin the table already spans reconnects (the forwarder retracts
464
- // a dead session's entries), so its stream is the same thing with less machinery.
465
- if (this.consume)
466
- return this.consume.announced(scope, options);
467
- const producer = new Announce.Producer();
468
- const consumer = producer.consume();
469
- const pump = new Effect();
470
- pump.run((effect) => {
471
- const conn = effect.get(this.established);
472
- if (!conn)
473
- return;
474
- // Without discovery the upstream announce stream never yields, so leave the
475
- // consumer empty rather than opening a subscription that can't be answered.
476
- if (!conn.discovery)
477
- return;
478
- const upstream = conn.announced(scope, options);
479
- effect.cleanup(() => upstream.close());
480
- // Track what this connection announced so we can retract it if the connection
481
- // drops; the last event rides along for the retraction.
482
- const active = new Map();
483
- effect.spawn(async () => {
484
- try {
485
- for (;;) {
486
- const entry = await effect.race(upstream.next());
487
- if (!entry)
488
- break;
489
- if (Announce.isActive(entry.kind))
490
- active.set(entry.prefix, entry);
491
- else
492
- active.delete(entry.prefix);
493
- producer.append(entry);
494
- }
495
- }
496
- catch {
497
- // A dropped connection resets the announce stream; the retractions below cover it.
498
- }
499
- finally {
500
- // Retract everything from the connection that just went away, so a per-broadcast
501
- // watcher tears down instead of clinging to the dead route.
502
- if (consumer.closed.peek() === undefined) {
503
- for (const entry of active.values()) {
504
- producer.append({ ...entry, kind: "retracted" });
505
- }
506
- }
507
- }
508
- });
509
- });
510
- this.#signals.cleanup(() => pump.close());
511
- void consumer.closed.then(() => pump.close());
512
- return consumer;
1020
+ answered() {
1021
+ return this.#verdict.answered();
513
1022
  }
514
- /**
515
- * Snapshot the live connection's transport counters, or undefined while disconnected.
516
- * See {@link Established.stats}.
517
- */
518
- async stats() {
519
- return this.established.peek()?.stats();
520
- }
521
- /** Stop reconnecting, close the current connection, and settle {@link Reload.closed}. Idempotent. */
1023
+ /** Stop reconnecting, drain and close the current connection, and settle {@link Reload.closed}. Idempotent. */
522
1024
  close(abort) {
523
1025
  this.#signals.close();
524
- this.#draining?.retire();
1026
+ this.#retire();
1027
+ // No session will present the tokens again, so they end with the loop, once the sessions
1028
+ // still draining no longer need their grants or a page hide to cut them short.
1029
+ const err = abort ?? new Error("connection closed");
1030
+ const done = () => {
1031
+ this.auth.abort(err);
1032
+ this.#suspension.close();
1033
+ };
1034
+ if (this.#ending.size === 0)
1035
+ done();
1036
+ else
1037
+ void Promise.all(this.#ending.values()).then(done);
525
1038
  if (this.#closed.peek() === undefined)
526
1039
  this.#closed.set(abort ?? null);
527
1040
  }
528
1041
  }
1042
+ /**
1043
+ * Pump `upstream` into `producer` for the rest of `effect`'s run: its events, then an `offline`
1044
+ * for each route still active once it closes or the run ends, so nothing clings to a source
1045
+ * that went away. Synced follows the upstream's, and owes those retractions too.
1046
+ *
1047
+ * @internal
1048
+ */
1049
+ export function pumpAnnounced(effect, upstream, producer) {
1050
+ // The last event per active route rides along for its retraction.
1051
+ const active = new Map();
1052
+ let live = true;
1053
+ const retract = () => {
1054
+ live = false;
1055
+ if (producer.closed.peek() !== undefined || active.size === 0)
1056
+ return;
1057
+ // Behind first, so a reader parked on the stream takes these as owed, not as news.
1058
+ producer.behind();
1059
+ // The source went away, which says nothing of whether the origin ended them.
1060
+ for (const entry of active.values())
1061
+ producer.append({ ...entry, kind: "offline" });
1062
+ active.clear();
1063
+ };
1064
+ // Torn down in reverse: the mirror stops, the retractions land, then the upstream closes,
1065
+ // so its closing never reads as synced.
1066
+ effect.cleanup(() => upstream.close());
1067
+ effect.cleanup(retract);
1068
+ // A new upstream owes its own initial set, and the retractions of the one before.
1069
+ producer.behind();
1070
+ effect.watch(upstream.synced, (synced) => {
1071
+ if (!synced)
1072
+ producer.behind();
1073
+ else if (upstream.closed.peek() === undefined)
1074
+ producer.synced();
1075
+ });
1076
+ effect.spawn(async () => {
1077
+ try {
1078
+ for (;;) {
1079
+ const entry = await effect.race([upstream.next()]);
1080
+ if (!entry || !live || producer.closed.peek() !== undefined)
1081
+ break;
1082
+ if (entry.kind === "end" || entry.kind === "offline")
1083
+ active.delete(entry.prefix);
1084
+ else
1085
+ active.set(entry.prefix, entry);
1086
+ producer.append(entry);
1087
+ }
1088
+ }
1089
+ catch {
1090
+ // A dropped connection resets the announce stream; the retractions below cover it.
1091
+ }
1092
+ finally {
1093
+ if (live) {
1094
+ // The upstream ended on its own: nothing more comes from it, so once its
1095
+ // retractions are read there is nothing left to wait for.
1096
+ retract();
1097
+ producer.behind();
1098
+ producer.synced();
1099
+ }
1100
+ }
1101
+ });
1102
+ }
529
1103
  /**
530
1104
  * A session the peer sent GOAWAY on, left serving so its groups in flight finish. It retires
531
- * when it closes on its own or overstays its handover window, and `onRetire` runs once either way.
1105
+ * when it closes on its own, overstays its handover window, or is retired by the loop, and
1106
+ * `onRetire` runs once either way. The window still caps a retired session's close: it is
1107
+ * aborted at the window's end if still open.
532
1108
  */
533
1109
  class Draining {
534
1110
  #connection;
535
- #timer;
1111
+ #timer = new AbortController();
536
1112
  #onRetire;
537
1113
  #retired = false;
538
1114
  constructor(connection, handover, onRetire) {
539
1115
  this.#connection = connection;
540
1116
  this.#onRetire = onRetire;
541
- this.#timer = setTimeout(() => {
542
- console.warn("old session did not drain in time; closing");
1117
+ void sleep(handover, { signal: this.#timer.signal }).then(() => {
1118
+ if (this.retire())
1119
+ console.warn("old session did not drain in time; closing");
1120
+ connection.abort();
1121
+ }, () => { });
1122
+ void connection.closed.then(() => {
1123
+ this.#timer.abort();
543
1124
  this.retire();
544
- }, handover);
545
- void connection.closed.then(() => this.retire());
1125
+ });
546
1126
  }
547
- /** Close the old session now, whatever remains of its window. Idempotent. */
1127
+ /** The old session while it still serves, before it retires. */
1128
+ serving() {
1129
+ return this.#retired ? undefined : this.#connection;
1130
+ }
1131
+ /** Stop serving from the old session, returning it the first time for the caller to end. */
548
1132
  retire() {
549
1133
  if (this.#retired)
550
- return;
1134
+ return undefined;
551
1135
  this.#retired = true;
552
- clearTimeout(this.#timer);
553
- this.#connection.close();
554
1136
  this.#onRetire();
1137
+ return this.#connection;
1138
+ }
1139
+ }
1140
+ /**
1141
+ * Where `url` dials, without its userinfo, query, or fragment, where a peer may mint fresh
1142
+ * credentials on every redirect, and with its host as the dial reads it. Two URLs that differ
1143
+ * only there reach the same place.
1144
+ */
1145
+ function destination(url) {
1146
+ const bare = new URL(parse(url).url);
1147
+ bare.username = "";
1148
+ bare.password = "";
1149
+ bare.search = "";
1150
+ bare.hash = "";
1151
+ return bare.href;
1152
+ }
1153
+ /**
1154
+ * A run of GOAWAY redirects, and whether it has revisited an endpoint. The peer picks every
1155
+ * hop, so remembering them all would grow without bound. Brent's cycle detection instead keeps
1156
+ * one departed endpoint, replaced at doubling intervals, and catches a cycle of any length
1157
+ * within a few laps of it. Once caught, the run stays a loop until reset.
1158
+ */
1159
+ export class Redirects {
1160
+ #mark;
1161
+ #span = 1;
1162
+ #hops = 0;
1163
+ #loop = false;
1164
+ /** Record leaving `from` for a different endpoint `to`, and return whether this run loops. */
1165
+ move(from, to) {
1166
+ if (to === this.#mark)
1167
+ this.#loop = true;
1168
+ if (++this.#hops === this.#span) {
1169
+ this.#mark = from;
1170
+ this.#span *= 2;
1171
+ this.#hops = 0;
1172
+ }
1173
+ return this.#loop;
1174
+ }
1175
+ /** Start a new run. */
1176
+ reset() {
1177
+ this.#mark = undefined;
1178
+ this.#span = 1;
1179
+ this.#hops = 0;
1180
+ this.#loop = false;
555
1181
  }
556
1182
  }
557
1183
  //# sourceMappingURL=reload.js.map