@moq/net 0.4.7 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (664) hide show
  1. package/README.md +7 -1
  2. package/announce.d.ts +1 -1
  3. package/announce.d.ts.map +1 -1
  4. package/announce.js +1 -2
  5. package/announce.js.map +1 -1
  6. package/announced.d.ts +51 -47
  7. package/announced.d.ts.map +1 -1
  8. package/announced.js +183 -32
  9. package/announced.js.map +1 -1
  10. package/announced.test-util.d.ts +2 -0
  11. package/announced.test-util.d.ts.map +1 -0
  12. package/announced.test-util.js +27 -0
  13. package/announced.test-util.js.map +1 -0
  14. package/auth.d.ts +64 -0
  15. package/auth.d.ts.map +1 -0
  16. package/auth.js +20 -0
  17. package/auth.js.map +1 -0
  18. package/auth_gate.d.ts +2 -0
  19. package/auth_gate.d.ts.map +1 -0
  20. package/auth_gate.js +24 -0
  21. package/auth_gate.js.map +1 -0
  22. package/auth_session.d.ts +49 -0
  23. package/auth_session.d.ts.map +1 -0
  24. package/auth_session.js +1097 -0
  25. package/auth_session.js.map +1 -0
  26. package/bandwidth.d.ts +129 -25
  27. package/bandwidth.d.ts.map +1 -1
  28. package/bandwidth.js +410 -139
  29. package/bandwidth.js.map +1 -1
  30. package/bandwidth_api.d.ts +1 -1
  31. package/bandwidth_api.d.ts.map +1 -1
  32. package/bandwidth_api.js +1 -2
  33. package/bandwidth_api.js.map +1 -1
  34. package/broadcast.d.ts +64 -38
  35. package/broadcast.d.ts.map +1 -1
  36. package/broadcast.js +556 -173
  37. package/broadcast.js.map +1 -1
  38. package/cache.d.ts +103 -0
  39. package/cache.d.ts.map +1 -0
  40. package/cache.js +254 -0
  41. package/cache.js.map +1 -0
  42. package/claim.test-util.d.ts +5 -0
  43. package/claim.test-util.d.ts.map +1 -0
  44. package/claim.test-util.js +13 -0
  45. package/claim.test-util.js.map +1 -0
  46. package/connection/accept.d.ts +21 -13
  47. package/connection/accept.d.ts.map +1 -1
  48. package/connection/accept.js +97 -130
  49. package/connection/accept.js.map +1 -1
  50. package/connection/browser.d.ts.map +1 -1
  51. package/connection/browser.js +28 -20
  52. package/connection/browser.js.map +1 -1
  53. package/connection/connect.d.ts +120 -24
  54. package/connection/connect.d.ts.map +1 -1
  55. package/connection/connect.js +535 -217
  56. package/connection/connect.js.map +1 -1
  57. package/connection/dials.d.ts +45 -0
  58. package/connection/dials.d.ts.map +1 -0
  59. package/connection/dials.js +109 -0
  60. package/connection/dials.js.map +1 -0
  61. package/connection/endpoint.d.ts +60 -0
  62. package/connection/endpoint.d.ts.map +1 -0
  63. package/connection/endpoint.js +114 -0
  64. package/connection/endpoint.js.map +1 -0
  65. package/connection/forward.d.ts +7 -1
  66. package/connection/forward.d.ts.map +1 -1
  67. package/connection/forward.js +265 -81
  68. package/connection/forward.js.map +1 -1
  69. package/connection/goaway.d.ts +16 -9
  70. package/connection/goaway.d.ts.map +1 -1
  71. package/connection/goaway.js +22 -95
  72. package/connection/goaway.js.map +1 -1
  73. package/connection/handshake.d.ts +34 -8
  74. package/connection/handshake.d.ts.map +1 -1
  75. package/connection/handshake.js +97 -21
  76. package/connection/handshake.js.map +1 -1
  77. package/connection/index.d.ts +4 -7
  78. package/connection/index.d.ts.map +1 -1
  79. package/connection/index.js +1 -3
  80. package/connection/index.js.map +1 -1
  81. package/connection/liveness.d.ts +14 -0
  82. package/connection/liveness.d.ts.map +1 -0
  83. package/connection/liveness.js +30 -0
  84. package/connection/liveness.js.map +1 -0
  85. package/connection/pool.d.ts +142 -63
  86. package/connection/pool.d.ts.map +1 -1
  87. package/connection/pool.js +281 -118
  88. package/connection/pool.js.map +1 -1
  89. package/connection/reload.d.ts +60 -12
  90. package/connection/reload.d.ts.map +1 -1
  91. package/connection/reload.js +859 -234
  92. package/connection/reload.js.map +1 -1
  93. package/connection/session.d.ts +91 -0
  94. package/connection/session.d.ts.map +1 -0
  95. package/connection/session.js +2 -0
  96. package/connection/session.js.map +1 -0
  97. package/connection/stats.d.ts +20 -11
  98. package/connection/stats.d.ts.map +1 -1
  99. package/connection/stats.js +64 -9
  100. package/connection/stats.js.map +1 -1
  101. package/connection/ticker.d.ts +4 -0
  102. package/connection/ticker.d.ts.map +1 -0
  103. package/connection/ticker.js +66 -0
  104. package/connection/ticker.js.map +1 -0
  105. package/connection/tokens.d.ts +2 -0
  106. package/connection/tokens.d.ts.map +1 -0
  107. package/connection/tokens.js +404 -0
  108. package/connection/tokens.js.map +1 -0
  109. package/connection/transport.d.ts.map +1 -1
  110. package/connection/transport.js +4 -5
  111. package/connection/transport.js.map +1 -1
  112. package/connection/withdrawal.d.ts +2 -0
  113. package/connection/withdrawal.d.ts.map +1 -0
  114. package/connection/withdrawal.js +23 -0
  115. package/connection/withdrawal.js.map +1 -0
  116. package/consume.js +213 -47
  117. package/consume.js.map +1 -1
  118. package/cover.d.ts +59 -0
  119. package/cover.d.ts.map +1 -0
  120. package/cover.js +375 -0
  121. package/cover.js.map +1 -0
  122. package/datagram.d.ts +70 -11
  123. package/datagram.d.ts.map +1 -1
  124. package/datagram.js +317 -2
  125. package/datagram.js.map +1 -1
  126. package/datagram_stream.d.ts +18 -0
  127. package/datagram_stream.d.ts.map +1 -0
  128. package/datagram_stream.js +67 -0
  129. package/datagram_stream.js.map +1 -0
  130. package/defined.test-util.d.ts +2 -0
  131. package/defined.test-util.d.ts.map +1 -0
  132. package/defined.test-util.js +7 -0
  133. package/defined.test-util.js.map +1 -0
  134. package/delivered.d.ts +75 -0
  135. package/delivered.d.ts.map +1 -0
  136. package/delivered.js +228 -0
  137. package/delivered.js.map +1 -0
  138. package/demand.d.ts +173 -0
  139. package/demand.d.ts.map +1 -0
  140. package/demand.js +582 -0
  141. package/demand.js.map +1 -0
  142. package/drops.d.ts +93 -0
  143. package/drops.d.ts.map +1 -0
  144. package/drops.js +283 -0
  145. package/drops.js.map +1 -0
  146. package/error.d.ts +120 -21
  147. package/error.d.ts.map +1 -1
  148. package/error.js +230 -38
  149. package/error.js.map +1 -1
  150. package/errors.d.ts +2 -2
  151. package/errors.d.ts.map +1 -1
  152. package/errors.js +2 -3
  153. package/errors.js.map +1 -1
  154. package/fill.d.ts +174 -0
  155. package/fill.d.ts.map +1 -0
  156. package/fill.js +760 -0
  157. package/fill.js.map +1 -0
  158. package/flate/decoder.d.ts +2 -0
  159. package/flate/decoder.d.ts.map +1 -0
  160. package/flate/decoder.js +63 -0
  161. package/flate/decoder.js.map +1 -0
  162. package/flate/encoder.d.ts +2 -0
  163. package/flate/encoder.d.ts.map +1 -0
  164. package/flate/encoder.js +64 -0
  165. package/flate/encoder.js.map +1 -0
  166. package/flate/group.d.ts +34 -0
  167. package/flate/group.d.ts.map +1 -0
  168. package/flate/group.js +140 -0
  169. package/flate/group.js.map +1 -0
  170. package/frame.d.ts +80 -0
  171. package/frame.d.ts.map +1 -0
  172. package/frame.js +296 -0
  173. package/frame.js.map +1 -0
  174. package/frame.test-util.d.ts +5 -0
  175. package/frame.test-util.d.ts.map +1 -0
  176. package/frame.test-util.js +9 -0
  177. package/frame.test-util.js.map +1 -0
  178. package/group.d.ts +108 -45
  179. package/group.d.ts.map +1 -1
  180. package/group.js +1616 -308
  181. package/group.js.map +1 -1
  182. package/group_api.d.ts +7 -0
  183. package/group_api.d.ts.map +1 -0
  184. package/group_api.js +7 -0
  185. package/group_api.js.map +1 -0
  186. package/hop.d.ts +62 -76
  187. package/hop.d.ts.map +1 -1
  188. package/hop.js +92 -65
  189. package/hop.js.map +1 -1
  190. package/identity.d.ts +39 -0
  191. package/identity.d.ts.map +1 -0
  192. package/identity.js +47 -0
  193. package/identity.js.map +1 -0
  194. package/ietf/active_count.d.ts +2 -0
  195. package/ietf/active_count.d.ts.map +1 -0
  196. package/ietf/active_count.js +30 -0
  197. package/ietf/active_count.js.map +1 -0
  198. package/ietf/aliases.js +69 -36
  199. package/ietf/aliases.js.map +1 -1
  200. package/ietf/cluster.d.ts +3 -3
  201. package/ietf/cluster.js +14 -55
  202. package/ietf/cluster.js.map +1 -1
  203. package/ietf/connection.d.ts +1 -31
  204. package/ietf/connection.d.ts.map +1 -1
  205. package/ietf/connection.js +305 -144
  206. package/ietf/connection.js.map +1 -1
  207. package/ietf/datagram.d.ts +60 -0
  208. package/ietf/datagram.d.ts.map +1 -0
  209. package/ietf/datagram.js +152 -0
  210. package/ietf/datagram.js.map +1 -0
  211. package/ietf/error.d.ts +3 -3
  212. package/ietf/error.js +82 -101
  213. package/ietf/error.js.map +1 -1
  214. package/ietf/fetch.d.ts +93 -37
  215. package/ietf/fetch.d.ts.map +1 -1
  216. package/ietf/fetch.js +172 -94
  217. package/ietf/fetch.js.map +1 -1
  218. package/ietf/filter.d.ts +21 -31
  219. package/ietf/filter.d.ts.map +1 -1
  220. package/ietf/filter.js +140 -172
  221. package/ietf/filter.js.map +1 -1
  222. package/ietf/goaway.d.ts +12 -10
  223. package/ietf/goaway.d.ts.map +1 -1
  224. package/ietf/goaway.js +22 -36
  225. package/ietf/goaway.js.map +1 -1
  226. package/ietf/hidden.js +1 -2
  227. package/ietf/hidden.js.map +1 -1
  228. package/ietf/index.d.ts +8 -20
  229. package/ietf/index.d.ts.map +1 -1
  230. package/ietf/index.js +4 -15
  231. package/ietf/index.js.map +1 -1
  232. package/ietf/join.d.ts +7 -0
  233. package/ietf/join.d.ts.map +1 -0
  234. package/ietf/join.js +234 -0
  235. package/ietf/join.js.map +1 -0
  236. package/ietf/message.d.ts +18 -2
  237. package/ietf/message.d.ts.map +1 -1
  238. package/ietf/message.js +60 -41
  239. package/ietf/message.js.map +1 -1
  240. package/ietf/message.test-util.d.ts +6 -0
  241. package/ietf/message.test-util.d.ts.map +1 -0
  242. package/ietf/message.test-util.js +13 -0
  243. package/ietf/message.test-util.js.map +1 -0
  244. package/ietf/namespace.d.ts +19 -5
  245. package/ietf/namespace.d.ts.map +1 -1
  246. package/ietf/namespace.js +78 -27
  247. package/ietf/namespace.js.map +1 -1
  248. package/ietf/object.d.ts +195 -33
  249. package/ietf/object.d.ts.map +1 -1
  250. package/ietf/object.js +405 -179
  251. package/ietf/object.js.map +1 -1
  252. package/ietf/parameters.d.ts +25 -45
  253. package/ietf/parameters.d.ts.map +1 -1
  254. package/ietf/parameters.js +260 -473
  255. package/ietf/parameters.js.map +1 -1
  256. package/ietf/priority.d.ts +17 -2
  257. package/ietf/priority.d.ts.map +1 -1
  258. package/ietf/priority.js +19 -5
  259. package/ietf/priority.js.map +1 -1
  260. package/ietf/properties.d.ts +6 -5
  261. package/ietf/properties.d.ts.map +1 -1
  262. package/ietf/properties.js +78 -49
  263. package/ietf/properties.js.map +1 -1
  264. package/ietf/publish.d.ts +25 -33
  265. package/ietf/publish.d.ts.map +1 -1
  266. package/ietf/publish.js +45 -140
  267. package/ietf/publish.js.map +1 -1
  268. package/ietf/publish_namespace.d.ts +49 -72
  269. package/ietf/publish_namespace.d.ts.map +1 -1
  270. package/ietf/publish_namespace.js +66 -185
  271. package/ietf/publish_namespace.js.map +1 -1
  272. package/ietf/publisher.js +1800 -601
  273. package/ietf/publisher.js.map +1 -1
  274. package/ietf/range.d.ts +2 -0
  275. package/ietf/range.d.ts.map +1 -0
  276. package/ietf/range.js +222 -0
  277. package/ietf/range.js.map +1 -0
  278. package/ietf/request.d.ts +6 -30
  279. package/ietf/request.d.ts.map +1 -1
  280. package/ietf/request.js +41 -79
  281. package/ietf/request.js.map +1 -1
  282. package/ietf/request_stream.d.ts +1 -6
  283. package/ietf/request_stream.d.ts.map +1 -1
  284. package/ietf/request_stream.js +306 -17
  285. package/ietf/request_stream.js.map +1 -1
  286. package/ietf/session.d.ts +41 -0
  287. package/ietf/session.d.ts.map +1 -0
  288. package/ietf/session.js +65 -0
  289. package/ietf/session.js.map +1 -0
  290. package/ietf/setup.d.ts +5 -30
  291. package/ietf/setup.d.ts.map +1 -1
  292. package/ietf/setup.js +6 -118
  293. package/ietf/setup.js.map +1 -1
  294. package/ietf/setup_options.d.ts +31 -0
  295. package/ietf/setup_options.d.ts.map +1 -0
  296. package/ietf/setup_options.js +138 -0
  297. package/ietf/setup_options.js.map +1 -0
  298. package/ietf/solicit.js +4 -15
  299. package/ietf/solicit.js.map +1 -1
  300. package/ietf/subscribe.d.ts +52 -45
  301. package/ietf/subscribe.d.ts.map +1 -1
  302. package/ietf/subscribe.js +118 -240
  303. package/ietf/subscribe.js.map +1 -1
  304. package/ietf/subscribe_namespace.d.ts +16 -79
  305. package/ietf/subscribe_namespace.d.ts.map +1 -1
  306. package/ietf/subscribe_namespace.js +30 -202
  307. package/ietf/subscribe_namespace.js.map +1 -1
  308. package/ietf/subscriber.js +1839 -537
  309. package/ietf/subscriber.js.map +1 -1
  310. package/ietf/token.js +105 -28
  311. package/ietf/token.js.map +1 -1
  312. package/ietf/track.d.ts +27 -24
  313. package/ietf/track.d.ts.map +1 -1
  314. package/ietf/track.js +48 -62
  315. package/ietf/track.js.map +1 -1
  316. package/ietf/version.d.ts +13 -42
  317. package/ietf/version.d.ts.map +1 -1
  318. package/ietf/version.js +22 -49
  319. package/ietf/version.js.map +1 -1
  320. package/implementation.d.ts +2 -0
  321. package/implementation.d.ts.map +1 -0
  322. package/implementation.js +5 -0
  323. package/implementation.js.map +1 -0
  324. package/index.d.ts +34 -19
  325. package/index.d.ts.map +1 -1
  326. package/index.js +19 -7
  327. package/index.js.map +1 -1
  328. package/internal.d.ts +1515 -67
  329. package/internal.d.ts.map +1 -1
  330. package/internal.js +557 -80
  331. package/internal.js.map +1 -1
  332. package/ledger.d.ts +23 -0
  333. package/ledger.d.ts.map +1 -0
  334. package/ledger.js +247 -0
  335. package/ledger.js.map +1 -0
  336. package/listing.d.ts +132 -0
  337. package/listing.d.ts.map +1 -0
  338. package/listing.js +486 -0
  339. package/listing.js.map +1 -0
  340. package/lite/announce.d.ts +127 -66
  341. package/lite/announce.d.ts.map +1 -1
  342. package/lite/announce.js +384 -159
  343. package/lite/announce.js.map +1 -1
  344. package/lite/announce_stream.d.ts +2 -0
  345. package/lite/announce_stream.d.ts.map +1 -0
  346. package/lite/announce_stream.js +92 -0
  347. package/lite/announce_stream.js.map +1 -0
  348. package/lite/answer.d.ts +22 -0
  349. package/lite/answer.d.ts.map +1 -0
  350. package/lite/answer.js +301 -0
  351. package/lite/answer.js.map +1 -0
  352. package/lite/auth.d.ts +45 -0
  353. package/lite/auth.d.ts.map +1 -0
  354. package/lite/auth.js +204 -0
  355. package/lite/auth.js.map +1 -0
  356. package/lite/compat.d.ts +31 -0
  357. package/lite/compat.d.ts.map +1 -0
  358. package/lite/compat.js +86 -0
  359. package/lite/compat.js.map +1 -0
  360. package/lite/connection.d.ts +1 -45
  361. package/lite/connection.d.ts.map +1 -1
  362. package/lite/connection.js +360 -78
  363. package/lite/connection.js.map +1 -1
  364. package/lite/datagram-end.d.ts +26 -0
  365. package/lite/datagram-end.d.ts.map +1 -0
  366. package/lite/datagram-end.js +30 -0
  367. package/lite/datagram-end.js.map +1 -0
  368. package/lite/datagram.d.ts +73 -8
  369. package/lite/datagram.d.ts.map +1 -1
  370. package/lite/datagram.js +144 -10
  371. package/lite/datagram.js.map +1 -1
  372. package/lite/duration.d.ts +6 -0
  373. package/lite/duration.d.ts.map +1 -0
  374. package/lite/duration.js +14 -0
  375. package/lite/duration.js.map +1 -0
  376. package/lite/error.d.ts +9 -0
  377. package/lite/error.d.ts.map +1 -0
  378. package/lite/error.js +24 -0
  379. package/lite/error.js.map +1 -0
  380. package/lite/fills.d.ts +162 -0
  381. package/lite/fills.d.ts.map +1 -0
  382. package/lite/fills.js +486 -0
  383. package/lite/fills.js.map +1 -0
  384. package/lite/fit.d.ts +49 -0
  385. package/lite/fit.d.ts.map +1 -0
  386. package/lite/fit.js +305 -0
  387. package/lite/fit.js.map +1 -0
  388. package/lite/goaway.d.ts +2 -2
  389. package/lite/goaway.d.ts.map +1 -1
  390. package/lite/goaway.js +5 -14
  391. package/lite/goaway.js.map +1 -1
  392. package/lite/group.d.ts +67 -16
  393. package/lite/group.d.ts.map +1 -1
  394. package/lite/group.js +108 -31
  395. package/lite/group.js.map +1 -1
  396. package/lite/identity.d.ts +25 -0
  397. package/lite/identity.d.ts.map +1 -0
  398. package/lite/identity.js +79 -0
  399. package/lite/identity.js.map +1 -0
  400. package/lite/index.d.ts +14 -13
  401. package/lite/index.d.ts.map +1 -1
  402. package/lite/index.js +2 -2
  403. package/lite/index.js.map +1 -1
  404. package/lite/message.d.ts +10 -2
  405. package/lite/message.d.ts.map +1 -1
  406. package/lite/message.js +28 -45
  407. package/lite/message.js.map +1 -1
  408. package/lite/priority.d.ts +9 -2
  409. package/lite/priority.d.ts.map +1 -1
  410. package/lite/priority.js +11 -106
  411. package/lite/priority.js.map +1 -1
  412. package/lite/probe.d.ts +2 -2
  413. package/lite/probe.d.ts.map +1 -1
  414. package/lite/probe.js +9 -1
  415. package/lite/probe.js.map +1 -1
  416. package/lite/publisher.d.ts +10 -1
  417. package/lite/publisher.d.ts.map +1 -1
  418. package/lite/publisher.js +2548 -438
  419. package/lite/publisher.js.map +1 -1
  420. package/lite/routed.d.ts +59 -0
  421. package/lite/routed.d.ts.map +1 -0
  422. package/lite/routed.js +564 -0
  423. package/lite/routed.js.map +1 -0
  424. package/lite/session.d.ts +2 -28
  425. package/lite/session.d.ts.map +1 -1
  426. package/lite/session.js +0 -100
  427. package/lite/session.js.map +1 -1
  428. package/lite/setup.d.ts +32 -5
  429. package/lite/setup.d.ts.map +1 -1
  430. package/lite/setup.js +95 -22
  431. package/lite/setup.js.map +1 -1
  432. package/lite/stream.d.ts +5 -11
  433. package/lite/stream.d.ts.map +1 -1
  434. package/lite/stream.js +5 -2
  435. package/lite/stream.js.map +1 -1
  436. package/lite/subscribe.d.ts +478 -78
  437. package/lite/subscribe.d.ts.map +1 -1
  438. package/lite/subscribe.js +1373 -143
  439. package/lite/subscribe.js.map +1 -1
  440. package/lite/subscriber.d.ts +7 -44
  441. package/lite/subscriber.d.ts.map +1 -1
  442. package/lite/subscriber.js +3290 -516
  443. package/lite/subscriber.js.map +1 -1
  444. package/lite/track.d.ts +27 -12
  445. package/lite/track.d.ts.map +1 -1
  446. package/lite/track.js +116 -38
  447. package/lite/track.js.map +1 -1
  448. package/lite/tracks.d.ts +55 -0
  449. package/lite/tracks.d.ts.map +1 -0
  450. package/lite/tracks.js +114 -0
  451. package/lite/tracks.js.map +1 -0
  452. package/lite/version.d.ts +132 -24
  453. package/lite/version.d.ts.map +1 -1
  454. package/lite/version.js +378 -34
  455. package/lite/version.js.map +1 -1
  456. package/meta.d.ts +77 -0
  457. package/meta.d.ts.map +1 -0
  458. package/meta.js +130 -0
  459. package/meta.js.map +1 -0
  460. package/meta_api.d.ts +7 -0
  461. package/meta_api.d.ts.map +1 -0
  462. package/meta_api.js +7 -0
  463. package/meta_api.js.map +1 -0
  464. package/mint.d.ts +28 -0
  465. package/mint.d.ts.map +1 -0
  466. package/mint.js +66 -0
  467. package/mint.js.map +1 -0
  468. package/open.d.ts +63 -0
  469. package/open.d.ts.map +1 -0
  470. package/open.js +250 -0
  471. package/open.js.map +1 -0
  472. package/origin.d.ts +394 -142
  473. package/origin.d.ts.map +1 -1
  474. package/origin.js +2533 -687
  475. package/origin.js.map +1 -1
  476. package/package.json +8 -13
  477. package/path.d.ts +51 -51
  478. package/path.d.ts.map +1 -1
  479. package/path.js +210 -58
  480. package/path.js.map +1 -1
  481. package/payload.d.ts +33 -0
  482. package/payload.d.ts.map +1 -0
  483. package/payload.js +65 -0
  484. package/payload.js.map +1 -0
  485. package/prefix.d.ts +152 -0
  486. package/prefix.d.ts.map +1 -0
  487. package/prefix.js +349 -0
  488. package/prefix.js.map +1 -0
  489. package/priority.d.ts +8 -0
  490. package/priority.d.ts.map +1 -0
  491. package/priority.js +185 -0
  492. package/priority.js.map +1 -0
  493. package/queue.d.ts +2 -0
  494. package/queue.d.ts.map +1 -0
  495. package/queue.js +46 -0
  496. package/queue.js.map +1 -0
  497. package/ranges.d.ts +36 -0
  498. package/ranges.d.ts.map +1 -0
  499. package/ranges.js +312 -0
  500. package/ranges.js.map +1 -0
  501. package/rescale.d.ts +24 -0
  502. package/rescale.d.ts.map +1 -0
  503. package/rescale.js +39 -0
  504. package/rescale.js.map +1 -0
  505. package/resume.d.ts +55 -0
  506. package/resume.d.ts.map +1 -0
  507. package/resume.js +4173 -0
  508. package/resume.js.map +1 -0
  509. package/resume.test-util.d.ts +2 -0
  510. package/resume.test-util.d.ts.map +1 -0
  511. package/resume.test-util.js +25 -0
  512. package/resume.test-util.js.map +1 -0
  513. package/skips.d.ts +48 -0
  514. package/skips.d.ts.map +1 -0
  515. package/skips.js +182 -0
  516. package/skips.js.map +1 -0
  517. package/stream.d.ts +78 -40
  518. package/stream.d.ts.map +1 -1
  519. package/stream.js +236 -93
  520. package/stream.js.map +1 -1
  521. package/subscription.test-util.d.ts +2 -0
  522. package/subscription.test-util.d.ts.map +1 -0
  523. package/subscription.test-util.js +49 -0
  524. package/subscription.test-util.js.map +1 -0
  525. package/tail.d.ts +23 -14
  526. package/tail.d.ts.map +1 -1
  527. package/tail.js +224 -98
  528. package/tail.js.map +1 -1
  529. package/time.d.ts +41 -102
  530. package/time.d.ts.map +1 -1
  531. package/time.js +129 -92
  532. package/time.js.map +1 -1
  533. package/timed.d.ts +21 -0
  534. package/timed.d.ts.map +1 -0
  535. package/timed.js +2 -0
  536. package/timed.js.map +1 -0
  537. package/track.d.ts +578 -246
  538. package/track.d.ts.map +1 -1
  539. package/track.js +7306 -772
  540. package/track.js.map +1 -1
  541. package/track_api.d.ts +7 -0
  542. package/track_api.d.ts.map +1 -0
  543. package/track_api.js +7 -0
  544. package/track_api.js.map +1 -0
  545. package/util/abort.d.ts +1 -0
  546. package/util/abort.d.ts.map +1 -1
  547. package/util/abort.js +23 -2
  548. package/util/abort.js.map +1 -1
  549. package/util/announce.d.ts +4 -0
  550. package/util/announce.d.ts.map +1 -0
  551. package/util/announce.js +11 -0
  552. package/util/announce.js.map +1 -0
  553. package/util/bigint.d.ts +3 -0
  554. package/util/bigint.d.ts.map +1 -0
  555. package/util/bigint.js +8 -0
  556. package/util/bigint.js.map +1 -0
  557. package/util/change.d.ts +12 -0
  558. package/util/change.d.ts.map +1 -0
  559. package/util/change.js +18 -0
  560. package/util/change.js.map +1 -0
  561. package/util/deadline.d.ts +2 -0
  562. package/util/deadline.d.ts.map +1 -0
  563. package/util/deadline.js +108 -0
  564. package/util/deadline.js.map +1 -0
  565. package/util/decode_error.d.ts +3 -0
  566. package/util/decode_error.d.ts.map +1 -0
  567. package/util/decode_error.js +11 -0
  568. package/util/decode_error.js.map +1 -0
  569. package/util/demand.d.ts +8 -0
  570. package/util/demand.d.ts.map +1 -0
  571. package/util/demand.js +24 -0
  572. package/util/demand.js.map +1 -0
  573. package/util/given_up.d.ts +2 -0
  574. package/util/given_up.d.ts.map +1 -0
  575. package/util/given_up.js +101 -0
  576. package/util/given_up.js.map +1 -0
  577. package/util/hex.d.ts +6 -0
  578. package/util/hex.d.ts.map +1 -1
  579. package/util/hex.js +19 -5
  580. package/util/hex.js.map +1 -1
  581. package/util/key.d.ts +15 -0
  582. package/util/key.d.ts.map +1 -0
  583. package/util/key.js +38 -0
  584. package/util/key.js.map +1 -0
  585. package/util/limits.d.ts +2 -0
  586. package/util/limits.d.ts.map +1 -0
  587. package/util/limits.js +76 -0
  588. package/util/limits.js.map +1 -0
  589. package/util/observed.d.ts +23 -0
  590. package/util/observed.d.ts.map +1 -0
  591. package/util/observed.js +122 -0
  592. package/util/observed.js.map +1 -0
  593. package/util/sorted.d.ts +2 -0
  594. package/util/sorted.d.ts.map +1 -0
  595. package/util/sorted.js +149 -0
  596. package/util/sorted.js.map +1 -0
  597. package/util/timeout.d.ts +2 -1
  598. package/util/timeout.d.ts.map +1 -1
  599. package/util/timeout.js +13 -10
  600. package/util/timeout.js.map +1 -1
  601. package/util/timer.d.ts +2 -0
  602. package/util/timer.d.ts.map +1 -0
  603. package/util/timer.js +19 -0
  604. package/util/timer.js.map +1 -0
  605. package/util/u64.js +0 -1
  606. package/util/utf8.d.ts +2 -0
  607. package/util/utf8.d.ts.map +1 -1
  608. package/util/utf8.js +7 -2
  609. package/util/utf8.js.map +1 -1
  610. package/util/varint.d.ts.map +1 -1
  611. package/util/varint.js +8 -2
  612. package/util/varint.js.map +1 -1
  613. package/util/watch.d.ts +8 -0
  614. package/util/watch.d.ts.map +1 -0
  615. package/util/watch.js +38 -0
  616. package/util/watch.js.map +1 -0
  617. package/util/yield.d.ts +2 -0
  618. package/util/yield.d.ts.map +1 -0
  619. package/util/yield.js +31 -0
  620. package/util/yield.js.map +1 -0
  621. package/varint.d.ts +0 -29
  622. package/varint.d.ts.map +1 -1
  623. package/varint.js +7 -29
  624. package/varint.js.map +1 -1
  625. package/varint_api.d.ts +7 -0
  626. package/varint_api.d.ts.map +1 -0
  627. package/varint_api.js +7 -0
  628. package/varint_api.js.map +1 -0
  629. package/version.d.ts +2 -0
  630. package/version.d.ts.map +1 -0
  631. package/version.js +4 -0
  632. package/version.js.map +1 -0
  633. package/window.d.ts +570 -0
  634. package/window.d.ts.map +1 -0
  635. package/window.js +1677 -0
  636. package/window.js.map +1 -0
  637. package/wire.d.ts +150 -35
  638. package/wire.d.ts.map +1 -1
  639. package/wire.js +27 -1
  640. package/wire.js.map +1 -1
  641. package/connection/established.d.ts +0 -53
  642. package/connection/established.d.ts.map +0 -1
  643. package/connection/established.js +0 -3
  644. package/connection/established.js.map +0 -1
  645. package/ietf/adapter.d.ts +0 -75
  646. package/ietf/adapter.d.ts.map +0 -1
  647. package/ietf/adapter.js +0 -700
  648. package/ietf/adapter.js.map +0 -1
  649. package/lite/datagram_stream.d.ts +0 -8
  650. package/lite/datagram_stream.d.ts.map +0 -1
  651. package/lite/datagram_stream.js +0 -44
  652. package/lite/datagram_stream.js.map +0 -1
  653. package/lite/fetch.d.ts +0 -28
  654. package/lite/fetch.d.ts.map +0 -1
  655. package/lite/fetch.js +0 -79
  656. package/lite/fetch.js.map +0 -1
  657. package/util/log.d.ts +0 -5
  658. package/util/log.d.ts.map +0 -1
  659. package/util/log.js +0 -17
  660. package/util/log.js.map +0 -1
  661. package/zod.d.ts +0 -13
  662. package/zod.d.ts.map +0 -1
  663. package/zod.js +0 -19
  664. package/zod.js.map +0 -1
@@ -1,34 +1,97 @@
1
- /* @ts-self-types="./reload.d.ts" */
2
- import { Effect, Once, Signal } from "@moq/signals";
3
- import * as Announce from "../announced.js";
4
- import { Allocator } from "../bandwidth.js";
5
- 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";
6
7
  import * as Path from "../path.js";
7
- import * as Time from "../time.js";
8
+ import { Follow, Observed } from "../util/observed.js";
9
+ import { after } from "../util/timer.js";
8
10
  import { wireOf } from "../wire.js";
9
- 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";
10
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";
11
21
  /**
12
- * 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.
13
30
  *
14
- * The timeout is short on purpose: a failure that clears within it was transient, and one that
15
- * doesn't should surface on {@link Reload.error} rather than leave the page silently
16
- * reconnecting for minutes. A loop nobody watches wants `timeout: 0` instead, since there is
17
- * no one to react. Giving up does not dispose the loop: a new URL or a disable/re-enable
18
- * 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.
19
36
  */
20
- const DEFAULT_DELAY = {
21
- initial: Time.Milli(1000),
37
+ const DEFAULT_BACKOFF = {
38
+ initial: Duration.millis(1000),
22
39
  multiplier: 2,
23
- max: Time.Milli(5000),
24
- timeout: Time.Milli(10000),
40
+ max: Duration.millis(5000),
41
+ healthy: Duration.millis(1000),
42
+ timeout: null,
25
43
  };
26
- /** How often the send-rate estimate is sampled from the live transport. */
27
- 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);
28
84
  /**
29
85
  * The reconnect loop behind a Connection handle: connects, waits for the session to end, then
30
86
  * redials with exponential backoff.
31
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
+ *
32
95
  * @internal
33
96
  */
34
97
  export class Reload {
@@ -36,12 +99,32 @@ export class Reload {
36
99
  url;
37
100
  /** Whether reconnecting is active. */
38
101
  enabled;
39
- /** 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
+ */
40
122
  status = new Signal("disconnected");
41
123
  /**
42
124
  * The failure that stopped retrying the current URL, or undefined while the loop is live.
43
125
  *
44
- * 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
45
128
  * disable/re-enable starts another sequence, not on a page hide/show. Transient drops
46
129
  * that are still being retried leave this empty.
47
130
  */
@@ -52,26 +135,55 @@ export class Reload {
52
135
  * The current connection's PROBE estimates, spanning reconnects.
53
136
  *
54
137
  * Undefined while disconnected: the estimates belong to a single connection.
55
- * See {@link Established.probe}.
138
+ * Observation requests updates; `peek()` alone is passive.
139
+ * See {@link Session.probe}.
56
140
  */
57
141
  probe;
142
+ #probeSource = new Signal(undefined);
58
143
  /**
59
144
  * Divides this connection's send-rate estimate among the tracks sharing it.
60
145
  *
61
- * The same instance across reconnects, so reservations survive a drop. The
146
+ * The same identity across reconnects, so reservations survive a drop. The
62
147
  * estimate is `undefined` while disconnected, which tells a sender to hold
63
- * 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.
64
171
  */
65
- 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;
66
179
  /** WebTransport options applied to each connection attempt (not reactive). */
67
180
  webtransport;
68
181
  /** WebSocket fallback options applied to each connection attempt (not reactive). */
69
182
  websocket;
70
- /**
71
- * Whether the relay supports broadcast discovery, applied to each connection attempt (not
72
- * reactive). Undefined defers to the default for the URL. See {@link Established.discovery}.
73
- */
74
- 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;
75
187
  /**
76
188
  * The origin whose broadcasts are served, spanning reconnects (not reactive).
77
189
  *
@@ -84,24 +196,28 @@ export class Reload {
84
196
  * The origin fed with the peer's announced broadcasts, spanning reconnects (not
85
197
  * reactive).
86
198
  *
87
- * The entries a session fed retract when it dies, and the next session re-populates the
88
- * table, so a consumer watching the origin sees offline/online transitions across a
89
- * 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.
90
202
  */
91
203
  consume;
92
204
  /** Backoff settings for the reconnect loop; an unset field uses its default. */
93
- delay;
205
+ backoff;
94
206
  /** How to react to the peer's GOAWAY (not reactive). */
95
207
  goaway;
96
208
  /**
97
209
  * The URL an accepted GOAWAY redirect assigned, or undefined while the loop dials
98
210
  * {@link Reload.url}. Sticky across reconnects: a redirect is an assignment, not a
99
- * 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.
100
213
  */
101
214
  redirect;
102
215
  /** The reactive effect scope driving the connect loop; closed by {@link Reload.close}. */
103
216
  #signals = new Effect();
104
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;
105
221
  #estimate = new Signal(undefined);
106
222
  /**
107
223
  * Settles once this loop is disposed via {@link Reload.close}: `null` on a clean close,
@@ -119,69 +235,124 @@ export class Reload {
119
235
  // A session the peer sent GOAWAY on, serving its groups in flight while the replacement
120
236
  // dials. Retired when it closes, at its handover cap, or when the sequence ends.
121
237
  #draining;
122
- // 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.
123
247
  #dialing = false;
124
248
  // The current wait between attempts, doubling per failure, and when the retry window expires.
125
- // 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.
126
250
  #delay;
251
+ // When the retry window expires, or null when it never does.
127
252
  #deadline;
128
253
  // The URL the current retry sequence is for. Cleared when disabled, URL-less, or given
129
254
  // up, so the next attempt starts a fresh backoff window.
130
255
  #sequenceHref;
131
256
  // The href that exhausted its retry sequence. Survives a page hide/show so resume does
132
- // 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
133
258
  // loop is disabled.
134
259
  #givenUpHref;
135
- // 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;
136
265
  #tick = new Signal(0);
137
266
  // True after the browser freezes or hides the page until it visibly resumes.
138
267
  #suspended = new Signal(false);
268
+ // Watches the page for #suspended, outliving the loop until the sessions it ended settle.
269
+ #suspension = new Effect();
139
270
  // Use the serialized URL as the reactive connection key. URL objects use identity
140
271
  // equality, but replacing one with an equivalent instance should not reconnect.
141
272
  #url;
142
273
  constructor(props) {
274
+ if (props && "discovery" in props)
275
+ throw new TypeError(DISCOVERY_REMOVED);
143
276
  this.url = Signal.from(props?.url);
144
277
  this.enabled = Signal.from(props?.enabled ?? true);
145
- 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);
146
285
  this.goaway = props?.goaway ?? {};
147
286
  this.redirect = this.#redirect;
287
+ this.versions = normalizeVersions(props);
288
+ this.urlAuth = props?.urlAuth;
148
289
  this.webtransport = props?.webtransport;
149
290
  this.websocket = props?.websocket;
150
- 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;
151
295
  this.publish = props?.publish;
152
296
  this.consume = props?.consume;
153
- // Requests on the consume origin stay pending across a reconnect, and before the
154
- // first session establishes, rather than reading as unroutable the moment no session
155
- // is attached. Released only when this loop is disposed: giving up the current URL
156
- // is recoverable (a new URL or a disable/re-enable starts another sequence), so a
157
- // 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.
158
302
  if (this.consume) {
159
- 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?.());
160
315
  }
161
316
  this.error = this.#error;
317
+ this.failure = this.#failure;
162
318
  const win = globalThis.window;
163
319
  const doc = globalThis.document;
164
320
  if (typeof win !== "undefined" && typeof doc !== "undefined") {
165
- this.#signals.event(win, "pagehide", () => this.#suspended.set(true));
166
- this.#signals.event(win, "pageshow", () => this.#suspended.set(false));
167
- this.#signals.event(win, "unload", () => this.#suspended.set(true));
168
- 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", () => {
169
325
  if (!doc.hidden)
170
326
  this.#suspended.set(false);
171
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
+ });
172
336
  }
173
- this.probe = this.#signals.computed((effect) => {
174
- const connection = effect.get(this.established);
175
- 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);
176
340
  });
177
- this.bandwidth = new Allocator(this.#estimate);
178
- this.#signals.cleanup(() => this.bandwidth.close());
179
- // The transport has no event for the send-rate estimate, so sample on our
180
- // 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.
181
349
  this.#signals.run((effect) => {
182
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;
183
354
  const connection = effect.get(this.established);
184
- if (!connection)
355
+ if (!connection || !estimates(connection))
185
356
  return;
186
357
  let pending = false;
187
358
  const sample = async () => {
@@ -189,19 +360,34 @@ export class Reload {
189
360
  return;
190
361
  pending = true;
191
362
  try {
192
- const stats = await effect.race(connection.stats());
193
- if (stats)
363
+ const at = now();
364
+ const stats = await effect.race([connection.stats()]);
365
+ if (stats) {
366
+ this.#sample.set({ ...stats, at });
194
367
  this.#estimate.set(stats.estimatedSendRate);
368
+ }
195
369
  }
196
370
  finally {
197
371
  pending = false;
198
372
  }
199
373
  };
200
374
  void sample();
201
- 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);
202
389
  });
203
390
  this.#url = this.#signals.computed((effect) => effect.get(this.url)?.href);
204
- // Create a reactive root so cleanup is easier.
205
391
  this.#signals.run(this.#connect.bind(this));
206
392
  }
207
393
  #connect(effect) {
@@ -241,84 +427,241 @@ export class Reload {
241
427
  this.#error.set(undefined);
242
428
  this.#givenUpHref = undefined;
243
429
  }
244
- // A drained predecessor still serves while its replacement dials.
245
- if (!this.#draining)
246
- this.status.set("connecting");
430
+ this.#setDialing(true);
247
431
  // This run's teardown, handed to connect() so a rerun cancels the attempt in flight.
248
432
  const signal = effect.abort;
249
- // 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
250
434
  // for #draining, which outlives the run.
251
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;
252
439
  effect.cleanup(() => {
253
440
  if (current) {
254
- 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);
255
445
  if (this.established.peek() === current)
256
- this.established.set(undefined);
446
+ this.#serve(undefined);
257
447
  current = undefined;
258
448
  }
259
- if (this.established.peek() === undefined)
260
- this.status.set("disconnected");
449
+ this.#setDialing(false);
261
450
  });
262
451
  effect.spawn(async () => {
263
- // Set once the session is live, so #retry can tell a healthy session that
264
- // later dropped from a connect failure or a peer that flaps immediately.
265
- let connected;
452
+ // Whether the last failure was a dial to a GOAWAY redirect's target.
453
+ let redirectFailed = false;
266
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;
267
458
  // Loops only to migrate: a GOAWAY off a healthy session dials its replacement
268
459
  // straight away, with no backoff.
269
460
  for (;;) {
270
461
  const dialing = this.#redirect.peek() ?? url;
271
- 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);
272
497
  let connection;
498
+ const reusing = reached;
499
+ reached = undefined;
273
500
  try {
274
- connection = await connect({
501
+ connection = await establish({
275
502
  url: dialing,
503
+ tokens,
504
+ urlAuth: this.urlAuth,
276
505
  // A redirect names the relay; a fallback URL pinned for the old one does not follow.
277
- 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,
278
510
  webtransport: this.webtransport,
279
- discovery: this.discovery,
511
+ timeout,
280
512
  publish: this.publish,
281
513
  consume: this.consume,
514
+ cost: this.cost,
515
+ auth: this.auth,
516
+ viewers: this.viewers,
282
517
  signal,
283
- });
518
+ }, reusing);
284
519
  }
285
- finally {
286
- this.#dialing = false;
520
+ catch (err) {
521
+ this.#setDialing(false);
522
+ redirectFailed = this.#redirect.peek() !== undefined;
523
+ throw err;
287
524
  }
288
- // 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.
289
527
  if (signal.aborted) {
290
- connection.close();
528
+ connection.abort();
291
529
  return;
292
530
  }
293
531
  current = connection;
294
- // The replacement serves now; a predecessor keeps draining its groups in flight.
295
- this.established.set(connection);
296
- this.status.set("connected");
297
- 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);
298
559
  // A cancelled effect resolves undefined, so the sentinel tells the session
299
560
  // closing (null for clean, an Error otherwise) apart from this run being
300
- // torn down. Anything else is the peer's GOAWAY.
301
- const ended = await effect.race(connection.closed, wireOf(connection).goaway);
302
- 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)
303
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);
304
588
  if (ended === null || ended instanceof Error) {
305
- 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.
306
603
  if (this.established.peek() === connection)
307
- this.established.set(undefined);
604
+ this.#serve(this.#draining?.serving());
605
+ this.#applyPending();
308
606
  this.#retry(effect, connected, ended ?? undefined);
309
607
  return;
310
608
  }
311
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;
312
615
  // A pinned WebSocket URL can win the race against the primary. Judge the
313
616
  // redirect against that endpoint, not the primary we never reached.
314
617
  const socket = this.#redirect.peek() ? undefined : this.websocket?.url;
315
- 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"));
316
654
  return;
317
- // A session that outlived the initial delay was healthy, so its handover is not a
318
- // failure. One redirected almost at once still migrates, but through the backoff,
319
- // so two peers bouncing us between them escalate and eventually give up.
320
- if (performance.now() - connected < this.#initial()) {
321
- 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"));
322
665
  return;
323
666
  }
324
667
  this.#delay = undefined;
@@ -326,24 +669,122 @@ export class Reload {
326
669
  }
327
670
  }
328
671
  catch (err) {
329
- // Treat teardown as cancellation, not a connection failure.
330
672
  if (signal.aborted)
331
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
+ }
332
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);
333
692
  this.#retry(effect, connected, err);
334
693
  }
335
694
  });
336
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
+ }
337
780
  /**
338
781
  * Act on the peer's GOAWAY for `connection`, which was dialed at `dialing`: resolve where
339
782
  * to go next and leave the old session serving until it drains. Returns false when the
340
783
  * redirect is refused, which ends the sequence rather than redialing.
341
784
  */
342
785
  #migrate(connection, dialing, drain) {
343
- const hashes = this.webtransport?.serverCertificateHashes?.length ?? 0;
344
- const configured = hashes > 0 || this.webtransport?.serverCertificate !== undefined;
345
786
  // The pin is a WebTransport option. A WebSocket that won the race never used it.
346
- const pinned = pinnedTransport(connection.transport, configured);
787
+ const pinned = pinnedTransport(connection.transport, this.#pinned());
347
788
  let next;
348
789
  try {
349
790
  next = target(this.goaway.redirect ?? "same-host", drain.uri, dialing, pinned);
@@ -352,9 +793,8 @@ export class Reload {
352
793
  // The peer is leaving and named somewhere we won't go: redialing the old address
353
794
  // would ignore it, so stop here.
354
795
  console.warn("GOAWAY redirect refused:", err);
355
- connection.close();
356
- this.established.set(undefined);
357
- this.status.set("disconnected");
796
+ this.#end(connection);
797
+ this.#serve(undefined);
358
798
  this.#giveUp(error(err));
359
799
  return false;
360
800
  }
@@ -364,23 +804,147 @@ export class Reload {
364
804
  this.#redirectHref = this.#sequenceHref;
365
805
  }
366
806
  console.info("GOAWAY received; migrating");
367
- // A newer GOAWAY retires an older predecessor rather than holding two open.
368
- this.#draining?.retire();
807
+ // A newer GOAWAY retires an older predecessor rather than serving from two.
808
+ this.#retire();
369
809
  const cap = handover(this.goaway.handover ?? DEFAULT_HANDOVER, drain.timeout);
370
810
  const draining = new Draining(connection, cap, () => {
371
811
  if (this.#draining === draining)
372
812
  this.#draining = undefined;
373
813
  // If nothing replaced it yet, nothing is serving.
374
- if (this.established.peek() !== connection)
375
- return;
376
- this.established.set(undefined);
377
- this.status.set(this.#dialing ? "connecting" : "disconnected");
814
+ if (this.established.peek() === connection)
815
+ this.#serve(undefined);
378
816
  });
379
817
  this.#draining = draining;
380
818
  return true;
381
819
  }
382
- #initial() {
383
- 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;
384
948
  }
385
949
  /**
386
950
  * Schedule the next connect attempt after the current backoff, or stop once the retry window
@@ -388,171 +952,232 @@ export class Reload {
388
952
  * `cause` the error that killed it, if it died with one.
389
953
  */
390
954
  #retry(effect, connected, cause) {
391
- // Resolved per sequence rather than at construction, so an edit to `delay` (including
392
- // one that drops a field back to its default) applies to the next retry. Field by
393
- // field rather than by spread: a caller building `{ initial: maybeInitial }` from an
394
- // optional value passes an explicit undefined, which a spread would take as the
395
- // answer, turning the backoff into NaN or the window into forever.
396
- const delay = this.delay ?? {};
397
- const initial = this.#initial();
398
- const multiplier = delay.multiplier ?? DEFAULT_DELAY.multiplier;
399
- const max = delay.max ?? DEFAULT_DELAY.max;
400
- const timeout = delay.timeout ?? DEFAULT_DELAY.timeout;
401
- // Report disconnected during the backoff rather than when the retry reruns the
402
- // effect, unless a drained predecessor still serves until it retires.
403
- if (this.established.peek() === undefined)
404
- this.status.set("disconnected");
405
- // A session that outlived the initial delay was healthy, so clear the backoff and
406
- // start a fresh retry window: a one-off drop should reconnect promptly. Anything
407
- // shorter is a peer that accepts and immediately severs, which has to keep
408
- // escalating or we hammer it forever at the initial delay.
409
- 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)) {
410
960
  this.#delay = undefined;
411
961
  this.#deadline = undefined;
412
962
  }
413
- // An auth rejection stops this URL however long the session lived. UNAUTHORIZED is a
414
- // specified code rather than one we guessed at, so this is the peer saying these
415
- // credentials will never work; retrying them just burns the window. Matches
416
- // moq-tokio's reconnect loop, which stops on the same close. A new URL or a
417
- // disable/re-enable starts another sequence; the handle itself is not disposed.
418
- //
419
- // Only a session close says that. The stream registry gives 2 to DELIVERY_TIMEOUT,
420
- // so a stream reset during the SETUP exchange would otherwise suppress reconnect
421
- // for good.
422
- if (cause instanceof SessionError && cause.code === SessionCode.Unauthorized) {
423
- console.warn("session rejected as unauthorized, not retrying");
424
- this.#giveUp(cause);
425
- return;
426
- }
427
- const now = performance.now();
428
- this.#delay ??= initial;
429
- this.#deadline ??= timeout > 0 ? now + timeout : Number.POSITIVE_INFINITY;
430
- 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 = () => {
431
973
  console.warn("reconnect timed out");
432
- // A graceful close has no error, so report the timeout itself.
433
- 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();
434
982
  return;
435
983
  }
436
- // Equal jitter, so a fleet of tabs knocked offline together doesn't reconnect on the same
437
- // tick, and never past the deadline the retry window promised.
438
- const wait = Math.min(this.#delay * (0.5 + Math.random() / 2), this.#deadline - now);
439
- this.#delay = Math.min(this.#delay * multiplier, max);
440
984
  const tick = this.#tick.peek() + 1;
441
- 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);
442
992
  }
443
993
  #giveUp(cause) {
994
+ this.#verdict.gaveUp(cause);
444
995
  this.#error.set(cause);
445
996
  this.#givenUpHref = this.#sequenceHref;
446
997
  this.#resetSequence();
447
998
  }
448
999
  #resetSequence() {
1000
+ this.#failure.set(undefined);
449
1001
  this.#delay = undefined;
450
1002
  this.#deadline = undefined;
451
1003
  this.#sequenceHref = undefined;
452
- this.#draining?.retire();
1004
+ this.#redirects.reset();
1005
+ this.#retire();
453
1006
  }
454
1007
  /**
455
- * 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.
456
1011
  *
457
- * The same {@link Announce.Consumer} stream as {@link Established.announced}, but everything active
458
- * is retracted (a `retracted` update) whenever the connection drops and re-announced on
459
- * reconnect, so a consumer draining `next()` never clings to a dead route across a reconnect.
460
- *
461
- * 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.
462
1019
  */
463
- announced(scope = Path.Pattern.all(), options) {
464
- // With a consume origin the table already spans reconnects (the forwarder retracts
465
- // a dead session's entries), so its stream is the same thing with less machinery.
466
- if (this.consume)
467
- return this.consume.announced(scope, options);
468
- const producer = new Announce.Producer();
469
- const consumer = producer.consume();
470
- const pump = new Effect();
471
- pump.run((effect) => {
472
- const conn = effect.get(this.established);
473
- if (!conn)
474
- return;
475
- // Without discovery the upstream announce stream never yields, so leave the
476
- // consumer empty rather than opening a subscription that can't be answered.
477
- if (!conn.discovery)
478
- return;
479
- const upstream = conn.announced(scope, options);
480
- effect.cleanup(() => upstream.close());
481
- // Track what this connection announced so we can retract it if the connection
482
- // drops; the last event rides along for the retraction.
483
- const active = new Map();
484
- effect.spawn(async () => {
485
- try {
486
- for (;;) {
487
- const entry = await effect.race(upstream.next());
488
- if (!entry)
489
- break;
490
- if (Announce.isActive(entry.kind))
491
- active.set(entry.prefix, entry);
492
- else
493
- active.delete(entry.prefix);
494
- producer.append(entry);
495
- }
496
- }
497
- catch {
498
- // A dropped connection resets the announce stream; the retractions below cover it.
499
- }
500
- finally {
501
- // Retract everything from the connection that just went away, so a per-broadcast
502
- // watcher tears down instead of clinging to the dead route.
503
- if (consumer.closed.peek() === undefined) {
504
- for (const entry of active.values()) {
505
- producer.append({ ...entry, kind: "retracted" });
506
- }
507
- }
508
- }
509
- });
510
- });
511
- this.#signals.cleanup(() => pump.close());
512
- void consumer.closed.then(() => pump.close());
513
- return consumer;
1020
+ answered() {
1021
+ return this.#verdict.answered();
514
1022
  }
515
- /**
516
- * Snapshot the live connection's transport counters, or undefined while disconnected.
517
- * See {@link Established.stats}.
518
- */
519
- async stats() {
520
- return this.established.peek()?.stats();
521
- }
522
- /** 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. */
523
1024
  close(abort) {
524
1025
  this.#signals.close();
525
- 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);
526
1038
  if (this.#closed.peek() === undefined)
527
1039
  this.#closed.set(abort ?? null);
528
1040
  }
529
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
+ }
530
1103
  /**
531
1104
  * A session the peer sent GOAWAY on, left serving so its groups in flight finish. It retires
532
- * 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.
533
1108
  */
534
1109
  class Draining {
535
1110
  #connection;
536
- #timer;
1111
+ #timer = new AbortController();
537
1112
  #onRetire;
538
1113
  #retired = false;
539
1114
  constructor(connection, handover, onRetire) {
540
1115
  this.#connection = connection;
541
1116
  this.#onRetire = onRetire;
542
- this.#timer = setTimeout(() => {
543
- 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();
544
1124
  this.retire();
545
- }, handover);
546
- void connection.closed.then(() => this.retire());
1125
+ });
547
1126
  }
548
- /** 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. */
549
1132
  retire() {
550
1133
  if (this.#retired)
551
- return;
1134
+ return undefined;
552
1135
  this.#retired = true;
553
- clearTimeout(this.#timer);
554
- this.#connection.close();
555
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;
556
1181
  }
557
1182
  }
558
1183
  //# sourceMappingURL=reload.js.map