@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.
- package/README.md +7 -1
- package/announce.d.ts +1 -1
- package/announce.d.ts.map +1 -1
- package/announce.js +1 -1
- package/announce.js.map +1 -1
- package/announced.d.ts +51 -47
- package/announced.d.ts.map +1 -1
- package/announced.js +183 -31
- package/announced.js.map +1 -1
- package/announced.test-util.d.ts +2 -0
- package/announced.test-util.d.ts.map +1 -0
- package/announced.test-util.js +27 -0
- package/announced.test-util.js.map +1 -0
- package/auth.d.ts +64 -0
- package/auth.d.ts.map +1 -0
- package/auth.js +20 -0
- package/auth.js.map +1 -0
- package/auth_gate.d.ts +2 -0
- package/auth_gate.d.ts.map +1 -0
- package/auth_gate.js +24 -0
- package/auth_gate.js.map +1 -0
- package/auth_session.d.ts +49 -0
- package/auth_session.d.ts.map +1 -0
- package/auth_session.js +1097 -0
- package/auth_session.js.map +1 -0
- package/bandwidth.d.ts +129 -25
- package/bandwidth.d.ts.map +1 -1
- package/bandwidth.js +410 -138
- package/bandwidth.js.map +1 -1
- package/bandwidth_api.d.ts +1 -1
- package/bandwidth_api.d.ts.map +1 -1
- package/bandwidth_api.js +1 -1
- package/bandwidth_api.js.map +1 -1
- package/broadcast.d.ts +62 -36
- package/broadcast.d.ts.map +1 -1
- package/broadcast.js +556 -172
- package/broadcast.js.map +1 -1
- package/cache.d.ts +103 -0
- package/cache.d.ts.map +1 -0
- package/cache.js +254 -0
- package/cache.js.map +1 -0
- package/claim.test-util.d.ts +5 -0
- package/claim.test-util.d.ts.map +1 -0
- package/claim.test-util.js +13 -0
- package/claim.test-util.js.map +1 -0
- package/connection/accept.d.ts +20 -12
- package/connection/accept.d.ts.map +1 -1
- package/connection/accept.js +97 -129
- package/connection/accept.js.map +1 -1
- package/connection/browser.d.ts.map +1 -1
- package/connection/browser.js +28 -19
- package/connection/browser.js.map +1 -1
- package/connection/connect.d.ts +119 -23
- package/connection/connect.d.ts.map +1 -1
- package/connection/connect.js +535 -216
- package/connection/connect.js.map +1 -1
- package/connection/dials.d.ts +45 -0
- package/connection/dials.d.ts.map +1 -0
- package/connection/dials.js +109 -0
- package/connection/dials.js.map +1 -0
- package/connection/endpoint.d.ts +60 -0
- package/connection/endpoint.d.ts.map +1 -0
- package/connection/endpoint.js +114 -0
- package/connection/endpoint.js.map +1 -0
- package/connection/forward.d.ts +7 -1
- package/connection/forward.d.ts.map +1 -1
- package/connection/forward.js +265 -80
- package/connection/forward.js.map +1 -1
- package/connection/goaway.d.ts +16 -9
- package/connection/goaway.d.ts.map +1 -1
- package/connection/goaway.js +22 -108
- package/connection/goaway.js.map +1 -1
- package/connection/handshake.d.ts +32 -6
- package/connection/handshake.d.ts.map +1 -1
- package/connection/handshake.js +97 -20
- package/connection/handshake.js.map +1 -1
- package/connection/index.d.ts +3 -6
- package/connection/index.d.ts.map +1 -1
- package/connection/index.js +1 -2
- package/connection/index.js.map +1 -1
- package/connection/liveness.d.ts +14 -0
- package/connection/liveness.d.ts.map +1 -0
- package/connection/liveness.js +30 -0
- package/connection/liveness.js.map +1 -0
- package/connection/pool.d.ts +137 -58
- package/connection/pool.d.ts.map +1 -1
- package/connection/pool.js +281 -117
- package/connection/pool.js.map +1 -1
- package/connection/reload.d.ts +60 -12
- package/connection/reload.d.ts.map +1 -1
- package/connection/reload.js +859 -233
- package/connection/reload.js.map +1 -1
- package/connection/session.d.ts +91 -0
- package/connection/session.d.ts.map +1 -0
- package/connection/session.js +2 -0
- package/connection/session.js.map +1 -0
- package/connection/stats.d.ts +20 -11
- package/connection/stats.d.ts.map +1 -1
- package/connection/stats.js +64 -8
- package/connection/stats.js.map +1 -1
- package/connection/ticker.d.ts +4 -0
- package/connection/ticker.d.ts.map +1 -0
- package/connection/ticker.js +66 -0
- package/connection/ticker.js.map +1 -0
- package/connection/tokens.d.ts +2 -0
- package/connection/tokens.d.ts.map +1 -0
- package/connection/tokens.js +404 -0
- package/connection/tokens.js.map +1 -0
- package/connection/transport.d.ts.map +1 -1
- package/connection/transport.js +4 -4
- package/connection/transport.js.map +1 -1
- package/connection/withdrawal.d.ts +2 -0
- package/connection/withdrawal.d.ts.map +1 -0
- package/connection/withdrawal.js +23 -0
- package/connection/withdrawal.js.map +1 -0
- package/consume.js +213 -46
- package/consume.js.map +1 -1
- package/cover.d.ts +59 -0
- package/cover.d.ts.map +1 -0
- package/cover.js +375 -0
- package/cover.js.map +1 -0
- package/datagram.d.ts +70 -11
- package/datagram.d.ts.map +1 -1
- package/datagram.js +317 -1
- package/datagram.js.map +1 -1
- package/datagram_stream.d.ts +18 -0
- package/datagram_stream.d.ts.map +1 -0
- package/datagram_stream.js +67 -0
- package/datagram_stream.js.map +1 -0
- package/defined.test-util.d.ts +2 -0
- package/defined.test-util.d.ts.map +1 -0
- package/defined.test-util.js +7 -0
- package/defined.test-util.js.map +1 -0
- package/delivered.d.ts +75 -0
- package/delivered.d.ts.map +1 -0
- package/delivered.js +228 -0
- package/delivered.js.map +1 -0
- package/demand.d.ts +173 -0
- package/demand.d.ts.map +1 -0
- package/demand.js +582 -0
- package/demand.js.map +1 -0
- package/drops.d.ts +93 -0
- package/drops.d.ts.map +1 -0
- package/drops.js +283 -0
- package/drops.js.map +1 -0
- package/error.d.ts +120 -21
- package/error.d.ts.map +1 -1
- package/error.js +230 -37
- package/error.js.map +1 -1
- package/errors.d.ts +2 -2
- package/errors.d.ts.map +1 -1
- package/errors.js +2 -2
- package/errors.js.map +1 -1
- package/fill.d.ts +174 -0
- package/fill.d.ts.map +1 -0
- package/fill.js +760 -0
- package/fill.js.map +1 -0
- package/flate/decoder.d.ts +2 -0
- package/flate/decoder.d.ts.map +1 -0
- package/flate/decoder.js +63 -0
- package/flate/decoder.js.map +1 -0
- package/flate/encoder.d.ts +2 -0
- package/flate/encoder.d.ts.map +1 -0
- package/flate/encoder.js +64 -0
- package/flate/encoder.js.map +1 -0
- package/flate/group.d.ts +34 -0
- package/flate/group.d.ts.map +1 -0
- package/flate/group.js +140 -0
- package/flate/group.js.map +1 -0
- package/frame.d.ts +80 -0
- package/frame.d.ts.map +1 -0
- package/frame.js +296 -0
- package/frame.js.map +1 -0
- package/frame.test-util.d.ts +5 -0
- package/frame.test-util.d.ts.map +1 -0
- package/frame.test-util.js +9 -0
- package/frame.test-util.js.map +1 -0
- package/group.d.ts +108 -45
- package/group.d.ts.map +1 -1
- package/group.js +1616 -307
- package/group.js.map +1 -1
- package/group_api.d.ts +7 -0
- package/group_api.d.ts.map +1 -0
- package/group_api.js +7 -0
- package/group_api.js.map +1 -0
- package/hop.d.ts +62 -76
- package/hop.d.ts.map +1 -1
- package/hop.js +92 -64
- package/hop.js.map +1 -1
- package/identity.d.ts +39 -0
- package/identity.d.ts.map +1 -0
- package/identity.js +47 -0
- package/identity.js.map +1 -0
- package/ietf/active_count.d.ts +2 -0
- package/ietf/active_count.d.ts.map +1 -0
- package/ietf/active_count.js +30 -0
- package/ietf/active_count.js.map +1 -0
- package/ietf/aliases.js +69 -35
- package/ietf/aliases.js.map +1 -1
- package/ietf/cluster.d.ts +3 -3
- package/ietf/cluster.js +14 -62
- package/ietf/cluster.js.map +1 -1
- package/ietf/connection.d.ts +1 -31
- package/ietf/connection.d.ts.map +1 -1
- package/ietf/connection.js +305 -143
- package/ietf/connection.js.map +1 -1
- package/ietf/datagram.d.ts +60 -0
- package/ietf/datagram.d.ts.map +1 -0
- package/ietf/datagram.js +152 -0
- package/ietf/datagram.js.map +1 -0
- package/ietf/error.d.ts +3 -3
- package/ietf/error.js +82 -100
- package/ietf/error.js.map +1 -1
- package/ietf/fetch.d.ts +91 -35
- package/ietf/fetch.d.ts.map +1 -1
- package/ietf/fetch.js +172 -93
- package/ietf/fetch.js.map +1 -1
- package/ietf/filter.d.ts +20 -30
- package/ietf/filter.d.ts.map +1 -1
- package/ietf/filter.js +140 -171
- package/ietf/filter.js.map +1 -1
- package/ietf/goaway.d.ts +11 -9
- package/ietf/goaway.d.ts.map +1 -1
- package/ietf/goaway.js +22 -35
- package/ietf/goaway.js.map +1 -1
- package/ietf/hidden.js +1 -1
- package/ietf/hidden.js.map +1 -1
- package/ietf/index.d.ts +2 -14
- package/ietf/index.d.ts.map +1 -1
- package/ietf/index.js +4 -14
- package/ietf/index.js.map +1 -1
- package/ietf/join.d.ts +7 -0
- package/ietf/join.d.ts.map +1 -0
- package/ietf/join.js +234 -0
- package/ietf/join.js.map +1 -0
- package/ietf/message.d.ts +18 -2
- package/ietf/message.d.ts.map +1 -1
- package/ietf/message.js +60 -40
- package/ietf/message.js.map +1 -1
- package/ietf/message.test-util.d.ts +6 -0
- package/ietf/message.test-util.d.ts.map +1 -0
- package/ietf/message.test-util.js +13 -0
- package/ietf/message.test-util.js.map +1 -0
- package/ietf/namespace.d.ts +17 -3
- package/ietf/namespace.d.ts.map +1 -1
- package/ietf/namespace.js +78 -26
- package/ietf/namespace.js.map +1 -1
- package/ietf/object.d.ts +194 -32
- package/ietf/object.d.ts.map +1 -1
- package/ietf/object.js +405 -178
- package/ietf/object.js.map +1 -1
- package/ietf/parameters.d.ts +22 -42
- package/ietf/parameters.d.ts.map +1 -1
- package/ietf/parameters.js +260 -472
- package/ietf/parameters.js.map +1 -1
- package/ietf/priority.d.ts +17 -2
- package/ietf/priority.d.ts.map +1 -1
- package/ietf/priority.js +19 -4
- package/ietf/priority.js.map +1 -1
- package/ietf/properties.d.ts +5 -4
- package/ietf/properties.d.ts.map +1 -1
- package/ietf/properties.js +78 -48
- package/ietf/properties.js.map +1 -1
- package/ietf/publish.d.ts +22 -30
- package/ietf/publish.d.ts.map +1 -1
- package/ietf/publish.js +45 -139
- package/ietf/publish.js.map +1 -1
- package/ietf/publish_namespace.d.ts +46 -69
- package/ietf/publish_namespace.d.ts.map +1 -1
- package/ietf/publish_namespace.js +66 -184
- package/ietf/publish_namespace.js.map +1 -1
- package/ietf/publisher.js +1800 -600
- package/ietf/publisher.js.map +1 -1
- package/ietf/range.d.ts +2 -0
- package/ietf/range.d.ts.map +1 -0
- package/ietf/range.js +222 -0
- package/ietf/range.js.map +1 -0
- package/ietf/request.d.ts +3 -27
- package/ietf/request.d.ts.map +1 -1
- package/ietf/request.js +41 -78
- package/ietf/request.js.map +1 -1
- package/ietf/request_stream.d.ts +1 -6
- package/ietf/request_stream.d.ts.map +1 -1
- package/ietf/request_stream.js +306 -16
- package/ietf/request_stream.js.map +1 -1
- package/ietf/session.d.ts +41 -0
- package/ietf/session.d.ts.map +1 -0
- package/ietf/session.js +65 -0
- package/ietf/session.js.map +1 -0
- package/ietf/setup.d.ts +4 -29
- package/ietf/setup.d.ts.map +1 -1
- package/ietf/setup.js +6 -117
- package/ietf/setup.js.map +1 -1
- package/ietf/setup_options.d.ts +31 -0
- package/ietf/setup_options.d.ts.map +1 -0
- package/ietf/setup_options.js +138 -0
- package/ietf/setup_options.js.map +1 -0
- package/ietf/solicit.js +4 -14
- package/ietf/solicit.js.map +1 -1
- package/ietf/subscribe.d.ts +48 -41
- package/ietf/subscribe.d.ts.map +1 -1
- package/ietf/subscribe.js +118 -239
- package/ietf/subscribe.js.map +1 -1
- package/ietf/subscribe_namespace.d.ts +13 -76
- package/ietf/subscribe_namespace.d.ts.map +1 -1
- package/ietf/subscribe_namespace.js +30 -201
- package/ietf/subscribe_namespace.js.map +1 -1
- package/ietf/subscriber.js +1839 -536
- package/ietf/subscriber.js.map +1 -1
- package/ietf/token.js +105 -27
- package/ietf/token.js.map +1 -1
- package/ietf/track.d.ts +25 -22
- package/ietf/track.d.ts.map +1 -1
- package/ietf/track.js +48 -61
- package/ietf/track.js.map +1 -1
- package/ietf/version.d.ts +13 -42
- package/ietf/version.d.ts.map +1 -1
- package/ietf/version.js +22 -48
- package/ietf/version.js.map +1 -1
- package/implementation.d.ts +2 -0
- package/implementation.d.ts.map +1 -0
- package/implementation.js +5 -0
- package/implementation.js.map +1 -0
- package/index.d.ts +25 -10
- package/index.d.ts.map +1 -1
- package/index.js +19 -6
- package/index.js.map +1 -1
- package/internal.d.ts +1512 -64
- package/internal.d.ts.map +1 -1
- package/internal.js +557 -79
- package/internal.js.map +1 -1
- package/ledger.d.ts +23 -0
- package/ledger.d.ts.map +1 -0
- package/ledger.js +247 -0
- package/ledger.js.map +1 -0
- package/listing.d.ts +132 -0
- package/listing.d.ts.map +1 -0
- package/listing.js +486 -0
- package/listing.js.map +1 -0
- package/lite/announce.d.ts +124 -63
- package/lite/announce.d.ts.map +1 -1
- package/lite/announce.js +384 -158
- package/lite/announce.js.map +1 -1
- package/lite/announce_stream.d.ts +2 -0
- package/lite/announce_stream.d.ts.map +1 -0
- package/lite/announce_stream.js +92 -0
- package/lite/announce_stream.js.map +1 -0
- package/lite/answer.d.ts +22 -0
- package/lite/answer.d.ts.map +1 -0
- package/lite/answer.js +301 -0
- package/lite/answer.js.map +1 -0
- package/lite/auth.d.ts +45 -0
- package/lite/auth.d.ts.map +1 -0
- package/lite/auth.js +204 -0
- package/lite/auth.js.map +1 -0
- package/lite/compat.d.ts +31 -0
- package/lite/compat.d.ts.map +1 -0
- package/lite/compat.js +86 -0
- package/lite/compat.js.map +1 -0
- package/lite/connection.d.ts +1 -45
- package/lite/connection.d.ts.map +1 -1
- package/lite/connection.js +360 -77
- package/lite/connection.js.map +1 -1
- package/lite/datagram-end.d.ts +26 -0
- package/lite/datagram-end.d.ts.map +1 -0
- package/lite/datagram-end.js +30 -0
- package/lite/datagram-end.js.map +1 -0
- package/lite/datagram.d.ts +73 -8
- package/lite/datagram.d.ts.map +1 -1
- package/lite/datagram.js +144 -9
- package/lite/datagram.js.map +1 -1
- package/lite/duration.d.ts +6 -0
- package/lite/duration.d.ts.map +1 -0
- package/lite/duration.js +14 -0
- package/lite/duration.js.map +1 -0
- package/lite/error.d.ts +9 -0
- package/lite/error.d.ts.map +1 -0
- package/lite/error.js +24 -0
- package/lite/error.js.map +1 -0
- package/lite/fills.d.ts +162 -0
- package/lite/fills.d.ts.map +1 -0
- package/lite/fills.js +486 -0
- package/lite/fills.js.map +1 -0
- package/lite/fit.d.ts +49 -0
- package/lite/fit.d.ts.map +1 -0
- package/lite/fit.js +305 -0
- package/lite/fit.js.map +1 -0
- package/lite/goaway.d.ts +1 -1
- package/lite/goaway.d.ts.map +1 -1
- package/lite/goaway.js +5 -13
- package/lite/goaway.js.map +1 -1
- package/lite/group.d.ts +64 -13
- package/lite/group.d.ts.map +1 -1
- package/lite/group.js +108 -30
- package/lite/group.js.map +1 -1
- package/lite/identity.d.ts +25 -0
- package/lite/identity.d.ts.map +1 -0
- package/lite/identity.js +79 -0
- package/lite/identity.js.map +1 -0
- package/lite/index.d.ts +2 -1
- package/lite/index.d.ts.map +1 -1
- package/lite/index.js +2 -1
- package/lite/index.js.map +1 -1
- package/lite/message.d.ts +9 -1
- package/lite/message.d.ts.map +1 -1
- package/lite/message.js +28 -44
- package/lite/message.js.map +1 -1
- package/lite/priority.d.ts +9 -2
- package/lite/priority.d.ts.map +1 -1
- package/lite/priority.js +11 -105
- package/lite/priority.js.map +1 -1
- package/lite/probe.d.ts.map +1 -1
- package/lite/probe.js +9 -0
- package/lite/probe.js.map +1 -1
- package/lite/publisher.d.ts +10 -1
- package/lite/publisher.d.ts.map +1 -1
- package/lite/publisher.js +2548 -437
- package/lite/publisher.js.map +1 -1
- package/lite/routed.d.ts +59 -0
- package/lite/routed.d.ts.map +1 -0
- package/lite/routed.js +564 -0
- package/lite/routed.js.map +1 -0
- package/lite/session.d.ts +1 -27
- package/lite/session.d.ts.map +1 -1
- package/lite/session.js +0 -99
- package/lite/session.js.map +1 -1
- package/lite/setup.d.ts +29 -2
- package/lite/setup.d.ts.map +1 -1
- package/lite/setup.js +95 -21
- package/lite/setup.js.map +1 -1
- package/lite/stream.d.ts +5 -11
- package/lite/stream.d.ts.map +1 -1
- package/lite/stream.js +5 -1
- package/lite/stream.js.map +1 -1
- package/lite/subscribe.d.ts +475 -75
- package/lite/subscribe.d.ts.map +1 -1
- package/lite/subscribe.js +1373 -142
- package/lite/subscribe.js.map +1 -1
- package/lite/subscriber.d.ts +7 -44
- package/lite/subscriber.d.ts.map +1 -1
- package/lite/subscriber.js +3290 -515
- package/lite/subscriber.js.map +1 -1
- package/lite/track.d.ts +24 -9
- package/lite/track.d.ts.map +1 -1
- package/lite/track.js +116 -37
- package/lite/track.js.map +1 -1
- package/lite/tracks.d.ts +55 -0
- package/lite/tracks.d.ts.map +1 -0
- package/lite/tracks.js +114 -0
- package/lite/tracks.js.map +1 -0
- package/lite/version.d.ts +132 -24
- package/lite/version.d.ts.map +1 -1
- package/lite/version.js +378 -33
- package/lite/version.js.map +1 -1
- package/meta.d.ts +77 -0
- package/meta.d.ts.map +1 -0
- package/meta.js +130 -0
- package/meta.js.map +1 -0
- package/meta_api.d.ts +7 -0
- package/meta_api.d.ts.map +1 -0
- package/meta_api.js +7 -0
- package/meta_api.js.map +1 -0
- package/mint.d.ts +28 -0
- package/mint.d.ts.map +1 -0
- package/mint.js +66 -0
- package/mint.js.map +1 -0
- package/open.d.ts +63 -0
- package/open.d.ts.map +1 -0
- package/open.js +250 -0
- package/open.js.map +1 -0
- package/origin.d.ts +392 -140
- package/origin.d.ts.map +1 -1
- package/origin.js +2533 -686
- package/origin.js.map +1 -1
- package/package.json +8 -13
- package/path.d.ts +51 -51
- package/path.d.ts.map +1 -1
- package/path.js +210 -57
- package/path.js.map +1 -1
- package/payload.d.ts +33 -0
- package/payload.d.ts.map +1 -0
- package/payload.js +65 -0
- package/payload.js.map +1 -0
- package/prefix.d.ts +152 -0
- package/prefix.d.ts.map +1 -0
- package/prefix.js +349 -0
- package/prefix.js.map +1 -0
- package/priority.d.ts +8 -0
- package/priority.d.ts.map +1 -0
- package/priority.js +185 -0
- package/priority.js.map +1 -0
- package/queue.d.ts +2 -0
- package/queue.d.ts.map +1 -0
- package/queue.js +46 -0
- package/queue.js.map +1 -0
- package/ranges.d.ts +36 -0
- package/ranges.d.ts.map +1 -0
- package/ranges.js +312 -0
- package/ranges.js.map +1 -0
- package/rescale.d.ts +24 -0
- package/rescale.d.ts.map +1 -0
- package/rescale.js +39 -0
- package/rescale.js.map +1 -0
- package/resume.d.ts +55 -0
- package/resume.d.ts.map +1 -0
- package/resume.js +4173 -0
- package/resume.js.map +1 -0
- package/resume.test-util.d.ts +2 -0
- package/resume.test-util.d.ts.map +1 -0
- package/resume.test-util.js +25 -0
- package/resume.test-util.js.map +1 -0
- package/skips.d.ts +48 -0
- package/skips.d.ts.map +1 -0
- package/skips.js +182 -0
- package/skips.js.map +1 -0
- package/stream.d.ts +75 -37
- package/stream.d.ts.map +1 -1
- package/stream.js +236 -92
- package/stream.js.map +1 -1
- package/subscription.test-util.d.ts +2 -0
- package/subscription.test-util.d.ts.map +1 -0
- package/subscription.test-util.js +49 -0
- package/subscription.test-util.js.map +1 -0
- package/tail.d.ts +23 -14
- package/tail.d.ts.map +1 -1
- package/tail.js +224 -97
- package/tail.js.map +1 -1
- package/time.d.ts +41 -102
- package/time.d.ts.map +1 -1
- package/time.js +129 -91
- package/time.js.map +1 -1
- package/timed.d.ts +21 -0
- package/timed.d.ts.map +1 -0
- package/timed.js +2 -0
- package/timed.js.map +1 -0
- package/track.d.ts +577 -245
- package/track.d.ts.map +1 -1
- package/track.js +7306 -771
- package/track.js.map +1 -1
- package/track_api.d.ts +7 -0
- package/track_api.d.ts.map +1 -0
- package/track_api.js +7 -0
- package/track_api.js.map +1 -0
- package/util/abort.d.ts +1 -0
- package/util/abort.d.ts.map +1 -1
- package/util/abort.js +23 -1
- package/util/abort.js.map +1 -1
- package/util/announce.d.ts +4 -0
- package/util/announce.d.ts.map +1 -0
- package/util/announce.js +11 -0
- package/util/announce.js.map +1 -0
- package/util/bigint.d.ts +3 -0
- package/util/bigint.d.ts.map +1 -0
- package/util/bigint.js +8 -0
- package/util/bigint.js.map +1 -0
- package/util/change.d.ts +12 -0
- package/util/change.d.ts.map +1 -0
- package/util/change.js +18 -0
- package/util/change.js.map +1 -0
- package/util/deadline.d.ts +2 -0
- package/util/deadline.d.ts.map +1 -0
- package/util/deadline.js +108 -0
- package/util/deadline.js.map +1 -0
- package/util/decode_error.d.ts +3 -0
- package/util/decode_error.d.ts.map +1 -0
- package/util/decode_error.js +11 -0
- package/util/decode_error.js.map +1 -0
- package/util/demand.d.ts +8 -0
- package/util/demand.d.ts.map +1 -0
- package/util/demand.js +24 -0
- package/util/demand.js.map +1 -0
- package/util/given_up.d.ts +2 -0
- package/util/given_up.d.ts.map +1 -0
- package/util/given_up.js +101 -0
- package/util/given_up.js.map +1 -0
- package/util/hex.d.ts +6 -0
- package/util/hex.d.ts.map +1 -1
- package/util/hex.js +19 -4
- package/util/hex.js.map +1 -1
- package/util/key.d.ts +15 -0
- package/util/key.d.ts.map +1 -0
- package/util/key.js +38 -0
- package/util/key.js.map +1 -0
- package/util/limits.d.ts +2 -0
- package/util/limits.d.ts.map +1 -0
- package/util/limits.js +76 -0
- package/util/limits.js.map +1 -0
- package/util/observed.d.ts +23 -0
- package/util/observed.d.ts.map +1 -0
- package/util/observed.js +122 -0
- package/util/observed.js.map +1 -0
- package/util/sorted.d.ts +2 -0
- package/util/sorted.d.ts.map +1 -0
- package/util/sorted.js +149 -0
- package/util/sorted.js.map +1 -0
- package/util/timeout.d.ts +2 -1
- package/util/timeout.d.ts.map +1 -1
- package/util/timeout.js +13 -9
- package/util/timeout.js.map +1 -1
- package/util/timer.d.ts +2 -0
- package/util/timer.d.ts.map +1 -0
- package/util/timer.js +19 -0
- package/util/timer.js.map +1 -0
- package/util/utf8.d.ts +2 -0
- package/util/utf8.d.ts.map +1 -1
- package/util/utf8.js +7 -1
- package/util/utf8.js.map +1 -1
- package/util/varint.d.ts.map +1 -1
- package/util/varint.js +8 -1
- package/util/varint.js.map +1 -1
- package/util/watch.d.ts +8 -0
- package/util/watch.d.ts.map +1 -0
- package/util/watch.js +38 -0
- package/util/watch.js.map +1 -0
- package/util/yield.d.ts +2 -0
- package/util/yield.d.ts.map +1 -0
- package/util/yield.js +31 -0
- package/util/yield.js.map +1 -0
- package/varint.d.ts +0 -29
- package/varint.d.ts.map +1 -1
- package/varint.js +7 -28
- package/varint.js.map +1 -1
- package/varint_api.d.ts +7 -0
- package/varint_api.d.ts.map +1 -0
- package/varint_api.js +7 -0
- package/varint_api.js.map +1 -0
- package/version.d.ts +2 -0
- package/version.d.ts.map +1 -0
- package/version.js +4 -0
- package/version.js.map +1 -0
- package/window.d.ts +570 -0
- package/window.d.ts.map +1 -0
- package/window.js +1677 -0
- package/window.js.map +1 -0
- package/wire.d.ts +145 -30
- package/wire.d.ts.map +1 -1
- package/wire.js +27 -0
- package/wire.js.map +1 -1
- package/connection/established.d.ts +0 -53
- package/connection/established.d.ts.map +0 -1
- package/connection/established.js +0 -2
- package/connection/established.js.map +0 -1
- package/ietf/adapter.d.ts +0 -75
- package/ietf/adapter.d.ts.map +0 -1
- package/ietf/adapter.js +0 -699
- package/ietf/adapter.js.map +0 -1
- package/lite/datagram_stream.d.ts +0 -8
- package/lite/datagram_stream.d.ts.map +0 -1
- package/lite/datagram_stream.js +0 -43
- package/lite/datagram_stream.js.map +0 -1
- package/lite/fetch.d.ts +0 -28
- package/lite/fetch.d.ts.map +0 -1
- package/lite/fetch.js +0 -78
- package/lite/fetch.js.map +0 -1
- package/util/log.d.ts +0 -5
- package/util/log.d.ts.map +0 -1
- package/util/log.js +0 -16
- package/util/log.js.map +0 -1
- package/zod.d.ts +0 -13
- package/zod.d.ts.map +0 -1
- package/zod.js +0 -18
- package/zod.js.map +0 -1
package/origin.js
CHANGED
|
@@ -4,33 +4,36 @@
|
|
|
4
4
|
* Publish broadcasts into an origin and hand the origin to one or more connections to
|
|
5
5
|
* serve them; the broadcasts outlive any single session. Hand the same (or another)
|
|
6
6
|
* origin to a connection's `consume` option and the peer's announced routes appear
|
|
7
|
-
* in the table too: each route covers a path prefix,
|
|
8
|
-
* it resolves through the session that announced it.
|
|
9
|
-
* `rs/moq-net`.
|
|
7
|
+
* in the table too, under whatever its readers watch: each route covers a path prefix,
|
|
8
|
+
* and a request for a path under it resolves through the session that announced it.
|
|
9
|
+
* Mirrors the `origin` module in `rs/moq-net`.
|
|
10
10
|
*
|
|
11
11
|
* @module
|
|
12
12
|
*/
|
|
13
|
-
import { Derived,
|
|
13
|
+
import { Derived, Once, race, Signal } from "@moq/signals";
|
|
14
14
|
import * as announce from "./announced.js";
|
|
15
15
|
import * as broadcast from "./broadcast.js";
|
|
16
16
|
import { StreamCode, StreamError } from "./error.js";
|
|
17
|
-
import {
|
|
18
|
-
import
|
|
17
|
+
import { routesEqual, WireRoute } from "./hop.js";
|
|
18
|
+
import * as Identity from "./identity.js";
|
|
19
|
+
import { arrival, bestOfTier, compareRoutes, compareTie, compareTier, coveringPrefixes, firstEligible, hiddenBelow, hooks, receive, routeHops, superseded, } from "./internal.js";
|
|
19
20
|
import * as Path from "./path.js";
|
|
20
|
-
import {
|
|
21
|
-
|
|
21
|
+
import { Front } from "./resume.js";
|
|
22
|
+
import { closeUnwanted } from "./util/announce.js";
|
|
23
|
+
import { registerWire, sameIdentity, wireOf, } from "./wire.js";
|
|
22
24
|
/** The rooted permissions shared by a handle and every route it inserts. */
|
|
23
25
|
class Scope {
|
|
24
26
|
static all = new Scope(Path.empty());
|
|
25
27
|
root;
|
|
28
|
+
/** The absolute prefixes this handle reaches; unset for every path. */
|
|
26
29
|
allowed;
|
|
27
30
|
constructor(root, allowed) {
|
|
28
31
|
this.root = root;
|
|
29
32
|
this.allowed = allowed;
|
|
30
33
|
}
|
|
31
|
-
narrow(root,
|
|
34
|
+
narrow(root, prefixes) {
|
|
32
35
|
const joined = Path.encode(Path.join(this.root, root));
|
|
33
|
-
const rooted =
|
|
36
|
+
const rooted = prefixes.rooted(joined);
|
|
34
37
|
const allowed = this.allowed?.intersect(rooted) ?? rooted;
|
|
35
38
|
if (allowed.size === 0)
|
|
36
39
|
throw new Error("origin scopes do not overlap");
|
|
@@ -39,6 +42,21 @@ class Scope {
|
|
|
39
42
|
matches(path) {
|
|
40
43
|
return this.allowed?.matches(path) ?? true;
|
|
41
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Where a claim at the absolute `prefix` is filed, so each place serves its whole subtree:
|
|
47
|
+
* the prefix itself when this scope covers it, and otherwise each of its prefixes beneath
|
|
48
|
+
* it, with how many segments beneath the claim's prefix it lies. Mirrors rs/moq-net's `filed`.
|
|
49
|
+
*/
|
|
50
|
+
filed(prefix) {
|
|
51
|
+
if (!this.allowed || this.allowed.matches(prefix))
|
|
52
|
+
return [[prefix, 0n]];
|
|
53
|
+
const depth = Path.parts(prefix).length;
|
|
54
|
+
return this.allowed
|
|
55
|
+
.toArray()
|
|
56
|
+
.map((member) => member.text)
|
|
57
|
+
.filter((at) => Path.check(at) === undefined && Path.hasPrefix(prefix, at))
|
|
58
|
+
.map((at) => [at, BigInt(Path.parts(at).length - depth)]);
|
|
59
|
+
}
|
|
42
60
|
path(path) {
|
|
43
61
|
const joined = Path.encode(Path.join(this.root, path));
|
|
44
62
|
if (!this.matches(joined))
|
|
@@ -47,25 +65,69 @@ class Scope {
|
|
|
47
65
|
}
|
|
48
66
|
prefix(prefix) {
|
|
49
67
|
const joined = Path.encode(Path.join(this.root, prefix));
|
|
50
|
-
if (
|
|
68
|
+
if (!this.overlaps(joined))
|
|
51
69
|
throw new Error("prefix is outside the origin scope");
|
|
52
|
-
}
|
|
53
70
|
return joined;
|
|
54
71
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
72
|
+
/**
|
|
73
|
+
* The absolute subtrees a reader of `prefix`, relative to the root, lists within this scope.
|
|
74
|
+
* Throws {@link Path.InvalidPrefix} for a prefix holding a `*`, which no grant can name, and
|
|
75
|
+
* an error for one past the wire limits, which no session could ask its peer about.
|
|
76
|
+
*/
|
|
77
|
+
listed(prefix) {
|
|
78
|
+
const rooted = new Path.Prefixes([Path.Prefix.parse(Path.encode(Path.join(this.root, prefix)))]);
|
|
79
|
+
if (!this.allowed)
|
|
80
|
+
return rooted;
|
|
81
|
+
// A grant can still name a path the wire refuses, past its limits or ending in an escaped
|
|
82
|
+
// `/`; no session could ask about it.
|
|
83
|
+
const listed = this.allowed.intersect(rooted).toArray();
|
|
84
|
+
return new Path.Prefixes(listed.filter((member) => Path.tryDecode(member.text) !== undefined));
|
|
85
|
+
}
|
|
86
|
+
/** The root-relative prefixes this scope asks its peer about: its own, already disjoint. */
|
|
87
|
+
#heads() {
|
|
61
88
|
if (!this.allowed)
|
|
62
89
|
return [Path.empty()];
|
|
63
|
-
|
|
64
|
-
|
|
90
|
+
return [...this.allowed.rebase(this.root)]
|
|
91
|
+
.map((prefix) => prefix.text)
|
|
92
|
+
.filter((head) => Path.tryDecode(head) !== undefined);
|
|
93
|
+
}
|
|
94
|
+
#headsCache;
|
|
95
|
+
/**
|
|
96
|
+
* The root-relative prefixes a session asks its peer about for one watched absolute prefix:
|
|
97
|
+
* a watch above a head requests the head, since this scope may not ask for more, and a
|
|
98
|
+
* watch inside one requests itself.
|
|
99
|
+
*/
|
|
100
|
+
interests(absolute) {
|
|
101
|
+
if (this === Scope.all)
|
|
102
|
+
return [absolute];
|
|
103
|
+
this.#headsCache ??= this.#heads();
|
|
104
|
+
const relative = Path.stripPrefix(this.root, absolute);
|
|
105
|
+
if (relative === null)
|
|
106
|
+
return Path.hasPrefix(absolute, this.root) ? [...this.#headsCache] : [];
|
|
107
|
+
const out = [];
|
|
108
|
+
for (const head of this.#headsCache) {
|
|
109
|
+
if (Path.hasPrefix(relative, head))
|
|
110
|
+
out.push(head);
|
|
111
|
+
else if (Path.hasPrefix(head, relative) && this.overlaps(absolute))
|
|
112
|
+
return [relative];
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
/** Whether this scope allows everything beneath the root-relative `prefix`. */
|
|
117
|
+
covers(prefix) {
|
|
118
|
+
if (!this.allowed)
|
|
119
|
+
return true;
|
|
120
|
+
// A peer's prefix may fit the wire alone and overflow once joined: nothing allowed lies there.
|
|
121
|
+
const path = Path.tryJoin(this.root, prefix);
|
|
122
|
+
return path !== undefined && Path.check(path) === undefined && this.allowed.matches(path);
|
|
123
|
+
}
|
|
124
|
+
/** Whether this scope allows anything beneath the absolute `prefix`. */
|
|
125
|
+
overlaps(prefix) {
|
|
126
|
+
return this.allowed?.overlaps(prefix) ?? true;
|
|
65
127
|
}
|
|
66
128
|
/** The exact paths within this scope, relative to its root. */
|
|
67
129
|
projectPaths(values) {
|
|
68
|
-
if (
|
|
130
|
+
if (this === Scope.all)
|
|
69
131
|
return values;
|
|
70
132
|
const out = new Map();
|
|
71
133
|
for (const [path, value] of values) {
|
|
@@ -79,24 +141,25 @@ class Scope {
|
|
|
79
141
|
}
|
|
80
142
|
/**
|
|
81
143
|
* The advertisements that may serve this scope, relative to its root. Every prefix at or
|
|
82
|
-
* above the root presents as the empty path,
|
|
83
|
-
* a request beneath the root resolves in.
|
|
144
|
+
* above the root presents as the empty path, as far above it as its claim is above the root,
|
|
145
|
+
* most specific first, since that is the order a request beneath the root resolves in.
|
|
84
146
|
*/
|
|
85
147
|
projectRoutes(values) {
|
|
86
148
|
if (!values || this === Scope.all)
|
|
87
149
|
return values;
|
|
88
150
|
const out = new Map();
|
|
89
151
|
const covering = [];
|
|
90
|
-
const allowed = this.allowed
|
|
152
|
+
const allowed = this.allowed;
|
|
153
|
+
const rootDepth = Path.parts(this.root).length;
|
|
91
154
|
for (const [path, candidates] of values) {
|
|
92
155
|
const relative = Path.stripPrefix(this.root, path);
|
|
93
156
|
const above = relative === null || relative === Path.empty();
|
|
94
157
|
if (above && !Path.hasPrefix(path, this.root))
|
|
95
158
|
continue;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
159
|
+
// A route serves the whole subtree it is filed at, so it may serve this scope when the two overlap.
|
|
160
|
+
if (allowed && !allowed.overlaps(path))
|
|
161
|
+
continue;
|
|
162
|
+
const visible = candidates;
|
|
100
163
|
if (visible.length === 0)
|
|
101
164
|
continue;
|
|
102
165
|
if (!above) {
|
|
@@ -106,92 +169,111 @@ class Scope {
|
|
|
106
169
|
// Hold the empty path's place in the order until every covering prefix is known.
|
|
107
170
|
if (covering.length === 0)
|
|
108
171
|
out.set(Path.empty(), []);
|
|
109
|
-
|
|
172
|
+
// Raised to the root, a claim is that much further above it, so its Above names its own
|
|
173
|
+
// prefix: claims of one prefix supersede each other wherever each is filed. It keeps
|
|
174
|
+
// where it is filed, so a read beneath the root ranks it as a request there resolves, and
|
|
175
|
+
// its place among its filing's claims, which the merged row below no longer shows.
|
|
176
|
+
const raised = BigInt(rootDepth - Path.parts(path).length);
|
|
177
|
+
covering.push([
|
|
178
|
+
path,
|
|
179
|
+
visible.map((value, order) => raised === 0n
|
|
180
|
+
? { ...value, order }
|
|
181
|
+
: {
|
|
182
|
+
...value,
|
|
183
|
+
filed: path,
|
|
184
|
+
order,
|
|
185
|
+
route: { ...value.route, above: (value.route.above ?? 0n) + raised },
|
|
186
|
+
}),
|
|
187
|
+
]);
|
|
110
188
|
}
|
|
111
189
|
if (covering.length > 0) {
|
|
112
|
-
|
|
113
|
-
|
|
190
|
+
// The most specific claim first, as a request beneath the root resolves; claims of one
|
|
191
|
+
// prefix rank against each other wherever each is filed, hashed on the root, as a
|
|
192
|
+
// request for it resolves.
|
|
193
|
+
const ranked = covering.flatMap(([prefix, visible]) => visible.map((value) => ({
|
|
194
|
+
value,
|
|
195
|
+
prefix,
|
|
196
|
+
depth: Path.parts(prefix).length,
|
|
197
|
+
order: value.order ?? 0,
|
|
198
|
+
arrival: arrival(value.ref),
|
|
199
|
+
route: value.route,
|
|
200
|
+
})));
|
|
201
|
+
// A broadcast published above the root serves only its own path, so a tier-mate that
|
|
202
|
+
// serves the root ranks ahead of it, as `presented` ranks them.
|
|
203
|
+
const serves = (entry) => !entry.value.exact || entry.prefix === this.root;
|
|
204
|
+
ranked.sort((a, b) => Number((a.route.above ?? 0n) - (b.route.above ?? 0n)) ||
|
|
205
|
+
Number(serves(b)) - Number(serves(a)) ||
|
|
206
|
+
compareTier(this.root, a, b) ||
|
|
207
|
+
compareTie(this.root, a, b));
|
|
208
|
+
out.set(Path.empty(), ranked.map(({ value }) => value));
|
|
114
209
|
}
|
|
115
210
|
return out;
|
|
116
211
|
}
|
|
117
212
|
}
|
|
118
|
-
/** Whether
|
|
119
|
-
function
|
|
120
|
-
|
|
213
|
+
/** Whether something strictly above `node` is watched, so `node` rides its request. */
|
|
214
|
+
function coveredAbove(node) {
|
|
215
|
+
for (let above = node.parent; above; above = above.parent)
|
|
216
|
+
if (above.count > 0)
|
|
217
|
+
return true;
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
/** The broadest watched prefixes strictly beneath `node`: the ones it covers. */
|
|
221
|
+
function frontier(node, out = []) {
|
|
222
|
+
for (const child of node.children.values()) {
|
|
223
|
+
if (child.count > 0)
|
|
224
|
+
out.push(child.path);
|
|
225
|
+
else
|
|
226
|
+
frontier(child, out);
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
/** Whether `entry` may serve a request for `path`. */
|
|
231
|
+
function admits(entry, path) {
|
|
232
|
+
return entry.scope.matches(path);
|
|
121
233
|
}
|
|
122
234
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
235
|
+
* Orders advertisements at a prefix: the most specific claim, then the better route, then a
|
|
236
|
+
* local broadcast on a tie, then fewer hops. A remaining tie is left to the table's newest-first
|
|
237
|
+
* order; see {@link compareTier}.
|
|
126
238
|
*/
|
|
127
|
-
class CoveringRoot {
|
|
128
|
-
#root;
|
|
129
|
-
#covering;
|
|
130
|
-
constructor(root) {
|
|
131
|
-
this.#root = root;
|
|
132
|
-
}
|
|
133
|
-
/** The presented path for `path`, or undefined when it is outside the root or a broader cover. */
|
|
134
|
-
relative(path) {
|
|
135
|
-
const relative = Path.stripPrefix(this.#root, path);
|
|
136
|
-
if (relative !== null && relative !== Path.empty())
|
|
137
|
-
return relative;
|
|
138
|
-
if (relative === null && !Path.hasPrefix(path, this.#root))
|
|
139
|
-
return undefined;
|
|
140
|
-
if (this.#covering !== undefined && !Path.hasPrefix(this.#covering, path))
|
|
141
|
-
return undefined;
|
|
142
|
-
this.#covering = path;
|
|
143
|
-
return Path.empty();
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
/** Orders advertisements at one prefix: the better route, then a local broadcast on a tie, then fewer hops. */
|
|
147
239
|
function compareCandidates(a, b) {
|
|
148
|
-
|
|
240
|
+
const above = (a.route.above ?? 0n) - (b.route.above ?? 0n);
|
|
241
|
+
return ((above < 0n ? -1 : above > 0n ? 1 : 0) ||
|
|
242
|
+
compareRoutes(a.route, b.route) ||
|
|
149
243
|
Number(b.exact) - Number(a.exact) ||
|
|
150
|
-
a.route
|
|
151
|
-
}
|
|
152
|
-
/** Orders two routes by preference: identified before anonymous, then lower warm cost, then lower cold cost. */
|
|
153
|
-
function compareRoutes(a, b) {
|
|
154
|
-
const anonymous = Number(isAnonymous(a)) - Number(isAnonymous(b));
|
|
155
|
-
if (anonymous !== 0)
|
|
156
|
-
return anonymous;
|
|
157
|
-
if (a.cost.warm !== b.cost.warm)
|
|
158
|
-
return a.cost.warm < b.cost.warm ? -1 : 1;
|
|
159
|
-
if (a.cost.cold !== b.cost.cold)
|
|
160
|
-
return a.cost.cold < b.cost.cold ? -1 : 1;
|
|
161
|
-
return 0;
|
|
162
|
-
}
|
|
163
|
-
/** The preferred of `entries` (newest first) not skipped: the best route, then fewest hops, then newest. */
|
|
164
|
-
function preferredEntry(entries, skip) {
|
|
165
|
-
let best;
|
|
166
|
-
for (const entry of entries) {
|
|
167
|
-
if (skip?.(entry))
|
|
168
|
-
continue;
|
|
169
|
-
if (!best) {
|
|
170
|
-
best = entry;
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
const a = entry.route.peek();
|
|
174
|
-
const b = best.route.peek();
|
|
175
|
-
const order = compareRoutes(a, b) || a.hops.length - b.hops.length;
|
|
176
|
-
if (order < 0)
|
|
177
|
-
best = entry;
|
|
178
|
-
}
|
|
179
|
-
return best;
|
|
244
|
+
routeHops(a.route) - routeHops(b.route));
|
|
180
245
|
}
|
|
181
246
|
/** Whether a session received `entry`, so it is never forwarded to a peer. */
|
|
182
247
|
function received(entry) {
|
|
183
248
|
return !entry.originated;
|
|
184
249
|
}
|
|
185
|
-
|
|
186
|
-
|
|
250
|
+
/** What a peer cannot be served through: a received route, or one of another identity than it pinned. */
|
|
251
|
+
function unservable(identity) {
|
|
252
|
+
return (claim, route) => (!(claim instanceof broadcast.Consumer) && received(claim)) ||
|
|
253
|
+
(identity !== undefined && route.identity !== identity);
|
|
187
254
|
}
|
|
188
|
-
/**
|
|
255
|
+
/** Whether `slot` still serves an older epoch while it waits on its identity's newer one: nobody joins it meanwhile. */
|
|
256
|
+
function outdated(slot) {
|
|
257
|
+
const current = slot.route.peek();
|
|
258
|
+
if (current?.front === undefined || slot.failover === undefined)
|
|
259
|
+
return false;
|
|
260
|
+
return (slot.failover.route.peek().epoch ?? 0n) !== (current.epoch ?? 0n);
|
|
261
|
+
}
|
|
262
|
+
function unroutable() {
|
|
263
|
+
return new StreamError(StreamCode.Unroutable, { message: "unroutable" });
|
|
264
|
+
}
|
|
265
|
+
// A selected handler refused these requests; a coalesced route gap can have the same error.
|
|
266
|
+
const refusedRequests = new WeakSet();
|
|
267
|
+
/** A claim's queue of requests, from {@link Producer.claim}: what a handler drains. */
|
|
189
268
|
class ServeState {
|
|
190
269
|
root;
|
|
191
|
-
|
|
270
|
+
/** The prefix the route claims; every served path is at or beneath it. */
|
|
271
|
+
prefix;
|
|
272
|
+
constructor(root, prefix) {
|
|
192
273
|
this.root = root;
|
|
274
|
+
this.prefix = prefix;
|
|
193
275
|
}
|
|
194
|
-
queue = new Signal(
|
|
276
|
+
queue = new Signal(new Set());
|
|
195
277
|
pending = new Map();
|
|
196
278
|
served = new Map();
|
|
197
279
|
rejected = new Map();
|
|
@@ -201,6 +283,37 @@ class ServeState {
|
|
|
201
283
|
demanding = new Map();
|
|
202
284
|
closed = new Once();
|
|
203
285
|
settled = new Signal(0);
|
|
286
|
+
/** Counts {@link reset}s, so a waiter can tell its request was released rather than answered. */
|
|
287
|
+
resets = 0;
|
|
288
|
+
/**
|
|
289
|
+
* Whether the origin publishes a broadcast of another identity that wins the absolute `path`.
|
|
290
|
+
* Set on an application's claim, whose handler may answer with such a broadcast under that
|
|
291
|
+
* broadcast's own identity: a request still reaching the claim past it can only be pinned to
|
|
292
|
+
* the claim's, and is refused rather than start a second job. Mirrors the Rust
|
|
293
|
+
* `ServeState::publishing`.
|
|
294
|
+
*/
|
|
295
|
+
publishes = () => false;
|
|
296
|
+
/**
|
|
297
|
+
* The claim's cost while it is announced at its own {@link prefix}, where a job answering it
|
|
298
|
+
* ranks in the claim's tier, cost first. See {@link Request.route}.
|
|
299
|
+
*/
|
|
300
|
+
cost = () => undefined;
|
|
301
|
+
/** The announced cost of each job's route at the claim's own {@link prefix}, undefined while hidden. */
|
|
302
|
+
jobs = new Set();
|
|
303
|
+
/** Whether `cost` prices a job at the claim's own prefix above the announced claim. */
|
|
304
|
+
exceeds(cost) {
|
|
305
|
+
const claim = this.cost();
|
|
306
|
+
return claim !== undefined && cost > claim;
|
|
307
|
+
}
|
|
308
|
+
/** Whether `cost` prices the claim below one of its announced jobs at its own prefix. */
|
|
309
|
+
undercuts(cost) {
|
|
310
|
+
for (const job of this.jobs) {
|
|
311
|
+
const priced = job();
|
|
312
|
+
if (priced !== undefined && priced > cost)
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
204
317
|
onChange = () => { };
|
|
205
318
|
onReject = () => { };
|
|
206
319
|
enqueue(path) {
|
|
@@ -215,8 +328,19 @@ class ServeState {
|
|
|
215
328
|
const request = makeRequest(Path.stripPrefix(this.root, path) ?? Path.empty(), this);
|
|
216
329
|
this.pending.set(path, request);
|
|
217
330
|
this.queue.mutate((queue) => {
|
|
218
|
-
queue.
|
|
331
|
+
queue.add(request);
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
/** Withdraw only this pending request; a later answer cannot replace a newer one. */
|
|
335
|
+
forget(path, request) {
|
|
336
|
+
if (this.pending.get(path) !== request)
|
|
337
|
+
return;
|
|
338
|
+
this.pending.delete(path);
|
|
339
|
+
this.queue.mutate((queue) => {
|
|
340
|
+
queue.delete(request);
|
|
219
341
|
});
|
|
342
|
+
this.rejected.delete(path);
|
|
343
|
+
finishRequest(request, unroutable());
|
|
220
344
|
}
|
|
221
345
|
accept(request, front) {
|
|
222
346
|
const path = Path.join(this.root, request.path);
|
|
@@ -232,6 +356,10 @@ class ServeState {
|
|
|
232
356
|
this.settled.update((n) => n + 1);
|
|
233
357
|
return;
|
|
234
358
|
}
|
|
359
|
+
// A route claiming a prefix names no broadcast, so a path it serves on request has no
|
|
360
|
+
// inventory to list.
|
|
361
|
+
if (path !== this.prefix)
|
|
362
|
+
hooks.stampUnlisted(front);
|
|
235
363
|
this.served.set(path, front);
|
|
236
364
|
void front.closed.then(() => {
|
|
237
365
|
if (this.served.get(path) !== front)
|
|
@@ -242,6 +370,9 @@ class ServeState {
|
|
|
242
370
|
this.onChange(path);
|
|
243
371
|
this.settled.update((n) => n + 1);
|
|
244
372
|
}
|
|
373
|
+
has(request) {
|
|
374
|
+
return this.closed.peek() === undefined && this.pending.get(Path.join(this.root, request.path)) === request;
|
|
375
|
+
}
|
|
245
376
|
reject(request, err) {
|
|
246
377
|
const path = Path.join(this.root, request.path);
|
|
247
378
|
if (this.pending.get(path) !== request)
|
|
@@ -252,42 +383,77 @@ class ServeState {
|
|
|
252
383
|
this.onReject(path, err);
|
|
253
384
|
this.settled.update((n) => n + 1);
|
|
254
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* A job's exact route just took `request`'s path under an identity of its own, so the claim
|
|
388
|
+
* never answers with it: that would hand requesters resolved under the claim's identity another
|
|
389
|
+
* stream. The request is refused instead, and its requesters resolve the path afresh: an
|
|
390
|
+
* unpinned one onto the job, a peer naming no identity once the job's announcement reaches
|
|
391
|
+
* it, and one pinned to the claim's identity is refused.
|
|
392
|
+
*/
|
|
393
|
+
release(request) {
|
|
394
|
+
const path = Path.join(this.root, request.path);
|
|
395
|
+
// Already forgotten when nothing else wanted it once the route landed.
|
|
396
|
+
this.forget(path, request);
|
|
397
|
+
this.onReject(path, unroutable());
|
|
398
|
+
this.settled.update((n) => n + 1);
|
|
399
|
+
}
|
|
255
400
|
close(abort) {
|
|
256
401
|
if (this.closed.peek() !== undefined)
|
|
257
402
|
return;
|
|
258
|
-
const err = abort ??
|
|
403
|
+
const err = abort ?? unroutable();
|
|
259
404
|
this.closed.set(err);
|
|
405
|
+
this.reset(err);
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Release answers and the requests handlers hold when the publisher identity changes: a
|
|
409
|
+
* handler's late answer is for the old one, and the requesters ask the new one again.
|
|
410
|
+
*/
|
|
411
|
+
reset(err = unroutable()) {
|
|
412
|
+
this.resets++;
|
|
260
413
|
const queued = [...this.pending.values()];
|
|
261
414
|
this.pending.clear();
|
|
262
415
|
this.queue.mutate((queue) => {
|
|
263
|
-
queue.
|
|
416
|
+
queue.clear();
|
|
264
417
|
});
|
|
265
418
|
for (const request of queued) {
|
|
266
419
|
finishRequest(request, err);
|
|
267
420
|
}
|
|
268
|
-
|
|
421
|
+
const served = [...this.served];
|
|
422
|
+
this.served.clear();
|
|
423
|
+
for (const [path, front] of served) {
|
|
269
424
|
front.close();
|
|
270
425
|
this.onChange(path);
|
|
271
426
|
}
|
|
272
|
-
this.served.clear();
|
|
273
427
|
this.rejected.clear();
|
|
274
428
|
this.demanding.clear();
|
|
275
429
|
this.settled.update((n) => n + 1);
|
|
276
430
|
}
|
|
277
431
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
super.set(value, notify);
|
|
432
|
+
// Navigation belongs to relative references, resolved before publication.
|
|
433
|
+
function publishPath(path) {
|
|
434
|
+
nameable(path);
|
|
435
|
+
if (Path.parts(path).some((segment) => segment === "." || segment === "..")) {
|
|
436
|
+
throw new Error("broadcast path contains a navigation segment");
|
|
284
437
|
}
|
|
285
438
|
}
|
|
439
|
+
// A `*` stays out of every path while a prefix refuses one (so a pattern written where a
|
|
440
|
+
// prefix belongs fails loudly): a broadcast no listing can root at, and that pre-0.22
|
|
441
|
+
// subscribers read as a wildcard, is refused before it is published or watched.
|
|
442
|
+
function nameable(path) {
|
|
443
|
+
if (path.includes("*"))
|
|
444
|
+
throw new Error("path contains a `*`");
|
|
445
|
+
}
|
|
446
|
+
// End a created broadcast the origin no longer routes. Consumer handles never end a produced
|
|
447
|
+
// broadcast, so the origin closes its producer; tracks already subscribed keep serving.
|
|
448
|
+
function retire({ producer, front }) {
|
|
449
|
+
front.close();
|
|
450
|
+
producer.close();
|
|
451
|
+
}
|
|
286
452
|
/** Reactive backing state shared by origin producers and consumers. */
|
|
287
453
|
class OriginState {
|
|
288
454
|
// Both tables decouple the application producing into the origin from the
|
|
289
|
-
// connections serving or feeding it.
|
|
290
|
-
//
|
|
455
|
+
// connections serving or feeding it. Emptied once the origin closes, after which
|
|
456
|
+
// `closed` refuses late writes.
|
|
291
457
|
//
|
|
292
458
|
// Created is what this endpoint publishes, keyed by exact path, announced or not.
|
|
293
459
|
// Local is the announced subset, with its route in advertisedLocal: a broadcast
|
|
@@ -297,68 +463,105 @@ class OriginState {
|
|
|
297
463
|
// received entry back to a peer, which is what makes an origin shared by both
|
|
298
464
|
// directions echo-free.
|
|
299
465
|
created = new Map();
|
|
300
|
-
local = new
|
|
301
|
-
advertisedLocal = new
|
|
302
|
-
routes = new
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
466
|
+
local = new Signal(new Map());
|
|
467
|
+
advertisedLocal = new Signal(new Map());
|
|
468
|
+
routes = new Signal(new Map());
|
|
469
|
+
// The candidate table announcement listings share, patched at each path a write {@link changed}
|
|
470
|
+
// while a listing is open, so an announce costs its own path however many routes the origin
|
|
471
|
+
// holds. The first write with no listing open drops it, so does the origin closing, and the
|
|
472
|
+
// next listing rebuilds it; until then a new listing reuses it as it stands, still current.
|
|
473
|
+
#snapshot;
|
|
474
|
+
// Each open listing's changed paths it has yet to present.
|
|
475
|
+
#listings = new Set();
|
|
476
|
+
/**
|
|
477
|
+
* Record that the candidates at `paths` changed. Every write to `local`, `advertisedLocal`,
|
|
478
|
+
* `routes`, or a filed entry's route calls this after the write, before its notification lands.
|
|
479
|
+
*/
|
|
480
|
+
changed(paths) {
|
|
481
|
+
if (this.#listings.size === 0) {
|
|
482
|
+
this.#snapshot = undefined;
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
for (const path of paths) {
|
|
486
|
+
if (this.#snapshot) {
|
|
487
|
+
const list = this.#candidatesAt(path);
|
|
488
|
+
if (list.length > 0)
|
|
489
|
+
this.#snapshot.set(path, list);
|
|
490
|
+
else
|
|
491
|
+
this.#snapshot.delete(path);
|
|
492
|
+
}
|
|
493
|
+
for (const pending of this.#listings)
|
|
494
|
+
pending.add(path);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/** Collect the paths {@link changed} from now on, for one listing, until the returned dispose. */
|
|
498
|
+
listen() {
|
|
499
|
+
const pending = new Set();
|
|
500
|
+
this.#listings.add(pending);
|
|
501
|
+
return [pending, () => this.#listings.delete(pending)];
|
|
502
|
+
}
|
|
503
|
+
/** Drop the candidate table along with the tables it reads, once the origin closes. */
|
|
504
|
+
clearSnapshot() {
|
|
505
|
+
this.#snapshot = undefined;
|
|
506
|
+
}
|
|
507
|
+
/** The candidate table, shared by announcement listings, current as of every {@link changed} write. */
|
|
309
508
|
snapshot() {
|
|
310
|
-
|
|
311
|
-
if (version === this.#snapshotVersion)
|
|
312
|
-
return this.#snapshot;
|
|
313
|
-
const candidates = this.candidates();
|
|
314
|
-
const available = new Map();
|
|
315
|
-
const visible = new Map();
|
|
316
|
-
for (const [path, [best]] of candidates) {
|
|
317
|
-
available.set(path, best.route);
|
|
318
|
-
if (!hiddenBelow(Path.empty(), path))
|
|
319
|
-
visible.set(path, best.route);
|
|
320
|
-
}
|
|
321
|
-
this.#snapshot = { candidates, routes: available, visible };
|
|
322
|
-
this.#snapshotVersion = version;
|
|
509
|
+
this.#snapshot ??= this.candidates();
|
|
323
510
|
return this.#snapshot;
|
|
324
511
|
}
|
|
325
512
|
/**
|
|
326
513
|
* Every advertisement per prefix, most preferred first, without the `skip`ped entries.
|
|
327
|
-
* Readers select after filtering by their scope,
|
|
328
|
-
*
|
|
514
|
+
* Readers select after filtering by their scope, then drop what a higher epoch of the
|
|
515
|
+
* same identity supersedes (see {@link firstEligible}), so a route they cannot see never
|
|
516
|
+
* hides one they can.
|
|
329
517
|
*/
|
|
330
518
|
candidates(skip) {
|
|
331
519
|
const out = new Map();
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
if (skip?.(entry))
|
|
336
|
-
continue;
|
|
337
|
-
list.push({ identity: entry.identity, route: entry.route.peek(), claim: entry.claim, exact: false });
|
|
338
|
-
}
|
|
520
|
+
const routes = this.routes.peek();
|
|
521
|
+
for (const path of routes.keys()) {
|
|
522
|
+
const list = this.#candidatesAt(path, skip);
|
|
339
523
|
if (list.length > 0)
|
|
340
524
|
out.set(path, list);
|
|
341
525
|
}
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
const list = out.get(path);
|
|
346
|
-
if (list)
|
|
347
|
-
list.push(local);
|
|
348
|
-
else
|
|
349
|
-
out.set(path, [local]);
|
|
350
|
-
}
|
|
351
|
-
for (const list of out.values()) {
|
|
352
|
-
// Stable, so equal routes keep the table's newest-first order.
|
|
353
|
-
if (list.length > 1)
|
|
354
|
-
list.sort(compareCandidates);
|
|
526
|
+
for (const path of this.local.peek().keys()) {
|
|
527
|
+
if (!routes.has(path))
|
|
528
|
+
out.set(path, this.#candidatesAt(path));
|
|
355
529
|
}
|
|
356
530
|
return out;
|
|
357
531
|
}
|
|
532
|
+
#candidatesAt(path, skip) {
|
|
533
|
+
const list = [];
|
|
534
|
+
for (const entry of this.routes.peek().get(path) ?? []) {
|
|
535
|
+
if (skip?.(entry))
|
|
536
|
+
continue;
|
|
537
|
+
list.push({
|
|
538
|
+
ref: entry.ref,
|
|
539
|
+
route: entry.route.peek(),
|
|
540
|
+
exact: false,
|
|
541
|
+
lost: () => entry.lost,
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
const front = this.local.peek().get(path);
|
|
545
|
+
if (front) {
|
|
546
|
+
list.push({
|
|
547
|
+
ref: front,
|
|
548
|
+
route: this.advertisedLocal.peek().get(path) ?? WireRoute.default,
|
|
549
|
+
exact: true,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
// Stable, so equal routes keep the table's newest-first order.
|
|
553
|
+
if (list.length > 1)
|
|
554
|
+
list.sort(compareCandidates);
|
|
555
|
+
return list;
|
|
556
|
+
}
|
|
358
557
|
// Originated advertisements sessions should forward: exact-path announces plus
|
|
359
|
-
// originated dynamics.
|
|
558
|
+
// originated dynamics. Ref is the local front or the route entry, so a
|
|
360
559
|
// republish diffs as retract-then-announce and a re-price as another active.
|
|
560
|
+
// Undefined once the origin closes, which ends the sessions publishing it.
|
|
361
561
|
originated = new Signal(new Map());
|
|
562
|
+
// Every open prefix route's server, advertised or not, so the origin's close ends each
|
|
563
|
+
// handler loop even for a route that is hidden or withdrawn.
|
|
564
|
+
servers = new Set();
|
|
362
565
|
// Broadcasts materialized from a served route, keyed by exact path. Shared by every
|
|
363
566
|
// request for the path so repeats reuse one accept; dropped (and closed) when the
|
|
364
567
|
// providing route goes away or the last request releases it.
|
|
@@ -367,14 +570,165 @@ class OriginState {
|
|
|
367
570
|
// answer them with blind subscriptions. Never announced: an answered request is assumed
|
|
368
571
|
// present, not known live, so it must not read as an availability claim.
|
|
369
572
|
requests = new Signal(new Map());
|
|
573
|
+
// Path-local wakeups for followers, which survive the end of a resolved identity.
|
|
574
|
+
// A counter rather than the identity: the identity is a plain object, so two routes with
|
|
575
|
+
// identical metadata compare equal, and a coalesced write could hide a takeover
|
|
576
|
+
// or the origin closing. Watchers read the table and `closed` themselves when woken.
|
|
577
|
+
watchers = new Map();
|
|
578
|
+
// Slots taken out of `requests` because another identity won their path, still open for
|
|
579
|
+
// their handles. Nothing joins them, but they are refreshed with the table, so one ends
|
|
580
|
+
// once its own identity stops serving, except retained answers from an in-place Restart.
|
|
581
|
+
detached = new Map();
|
|
582
|
+
// An in-place Restart forgets served answers for future requests, but resolved handles
|
|
583
|
+
// retain their old broadcast without participating in routing or same-identity failover.
|
|
584
|
+
retained = new WeakSet();
|
|
370
585
|
// How many sessions are attached, and how many of those support broadcast discovery.
|
|
371
586
|
// What backs the public `discovery` getter.
|
|
372
587
|
sessions = new Signal({ total: 0, discovery: 0 });
|
|
373
|
-
//
|
|
374
|
-
//
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
588
|
+
// The prefixes whose initial announcements a session is still delivering, counted. An
|
|
589
|
+
// announcement stream overlapping one has not synced yet.
|
|
590
|
+
snapshots = new Signal(new Map());
|
|
591
|
+
/** Hold an initial set in flight under the absolute `prefix` until the returned dispose. */
|
|
592
|
+
addSnapshot(prefix) {
|
|
593
|
+
this.snapshots.mutate((counts) => {
|
|
594
|
+
counts.set(prefix, (counts.get(prefix) ?? 0) + 1);
|
|
595
|
+
});
|
|
596
|
+
let landed = false;
|
|
597
|
+
return () => {
|
|
598
|
+
if (landed)
|
|
599
|
+
return;
|
|
600
|
+
landed = true;
|
|
601
|
+
this.snapshots.mutate((counts) => {
|
|
602
|
+
const count = (counts.get(prefix) ?? 0) - 1;
|
|
603
|
+
if (count > 0)
|
|
604
|
+
counts.set(prefix, count);
|
|
605
|
+
else
|
|
606
|
+
counts.delete(prefix);
|
|
607
|
+
});
|
|
608
|
+
this.settle(prefix);
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
// The prefixes whose pending plain requests may have lost their last reason to wait.
|
|
612
|
+
#unsettled = new Set();
|
|
613
|
+
/**
|
|
614
|
+
* Recheck the pending plain requests under the absolute `prefix` once the current task
|
|
615
|
+
* settles, so a session releasing its initial set and downgrading reads as one step.
|
|
616
|
+
*/
|
|
617
|
+
settle(prefix) {
|
|
618
|
+
if (this.#unsettled.size === 0)
|
|
619
|
+
queueMicrotask(() => this.#settled());
|
|
620
|
+
this.#unsettled.add(prefix);
|
|
621
|
+
}
|
|
622
|
+
#settled() {
|
|
623
|
+
const requests = this.requests.peek();
|
|
624
|
+
const visited = new Set();
|
|
625
|
+
for (const prefix of this.#unsettled) {
|
|
626
|
+
// Every request watches its own path, so the watches beneath the prefix name exactly
|
|
627
|
+
// the requests it can settle, without scanning the rest of the table.
|
|
628
|
+
let node = this.#watches;
|
|
629
|
+
for (const part of Path.parts(prefix))
|
|
630
|
+
node = node?.children.get(part);
|
|
631
|
+
const stack = node ? [node] : [];
|
|
632
|
+
for (let next = stack.pop(); next; next = stack.pop()) {
|
|
633
|
+
if (visited.has(next))
|
|
634
|
+
continue;
|
|
635
|
+
visited.add(next);
|
|
636
|
+
// Children first: settling a request may prune its node.
|
|
637
|
+
stack.push(...next.children.values());
|
|
638
|
+
const slot = requests.get(next.path);
|
|
639
|
+
if (slot)
|
|
640
|
+
for (const absent of [...slot.absent])
|
|
641
|
+
absent();
|
|
642
|
+
// A pinned slot waits detached.
|
|
643
|
+
for (const other of [...(this.detached.get(next.path) ?? [])]) {
|
|
644
|
+
for (const absent of [...other.absent])
|
|
645
|
+
absent();
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
this.#unsettled.clear();
|
|
650
|
+
}
|
|
651
|
+
/** Whether every attached session discovers and none owes an initial set covering `path`. */
|
|
652
|
+
synced(path) {
|
|
653
|
+
const { total, discovery } = this.sessions.peek();
|
|
654
|
+
if (total === 0 || discovery < total)
|
|
655
|
+
return false;
|
|
656
|
+
const snapshots = this.snapshots.peek();
|
|
657
|
+
for (const prefix of coveringPrefixes(path))
|
|
658
|
+
if (snapshots.has(prefix))
|
|
659
|
+
return false;
|
|
660
|
+
return true;
|
|
661
|
+
}
|
|
662
|
+
// The literal heads readers are watching, counted, as a trie: announcement streams,
|
|
663
|
+
// requests, and followers. Sessions ask their peer for announcements under the broadest of
|
|
664
|
+
// these and nothing else, and hear each change to that covering set as it happens, so a
|
|
665
|
+
// watch coming or going touches only its prefix and the watches directly above or below.
|
|
666
|
+
#watches = { path: Path.empty(), children: new Map(), count: 0 };
|
|
667
|
+
#interests = new Set();
|
|
668
|
+
/** Count `heads` as watched until the returned dispose runs. */
|
|
669
|
+
watch(heads) {
|
|
670
|
+
for (const head of heads)
|
|
671
|
+
this.#watchOne(head);
|
|
672
|
+
let released = false;
|
|
673
|
+
return () => {
|
|
674
|
+
if (released)
|
|
675
|
+
return;
|
|
676
|
+
released = true;
|
|
677
|
+
for (const head of heads)
|
|
678
|
+
this.#unwatchOne(head);
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
#watchOne(head) {
|
|
682
|
+
let node = this.#watches;
|
|
683
|
+
for (const part of Path.parts(head)) {
|
|
684
|
+
let child = node.children.get(part);
|
|
685
|
+
if (!child) {
|
|
686
|
+
child = { path: Path.join(node.path, part), parent: node, children: new Map(), count: 0 };
|
|
687
|
+
node.children.set(part, child);
|
|
688
|
+
}
|
|
689
|
+
node = child;
|
|
690
|
+
}
|
|
691
|
+
if (node.count++ > 0 || coveredAbove(node))
|
|
692
|
+
return;
|
|
693
|
+
// Opened before the narrower ones it covers are released, so nothing retracts.
|
|
694
|
+
this.#emit(node.path, true);
|
|
695
|
+
for (const narrower of frontier(node))
|
|
696
|
+
this.#emit(narrower, false);
|
|
697
|
+
}
|
|
698
|
+
#unwatchOne(head) {
|
|
699
|
+
let node = this.#watches;
|
|
700
|
+
for (const part of Path.parts(head))
|
|
701
|
+
node = node?.children.get(part);
|
|
702
|
+
if (!node || node.count === 0)
|
|
703
|
+
throw new Error("unwatched prefix released");
|
|
704
|
+
if (--node.count === 0 && !coveredAbove(node)) {
|
|
705
|
+
for (const narrower of frontier(node))
|
|
706
|
+
this.#emit(narrower, true);
|
|
707
|
+
this.#emit(node.path, false);
|
|
708
|
+
}
|
|
709
|
+
// Prune the segments nothing watches any more.
|
|
710
|
+
while (node.parent && node.count === 0 && node.children.size === 0) {
|
|
711
|
+
node.parent.children.delete(Path.parts(node.path).at(-1) ?? "");
|
|
712
|
+
node = node.parent;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
#emit(prefix, wanted) {
|
|
716
|
+
for (const listener of this.#interests)
|
|
717
|
+
listener(prefix, wanted);
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Hear every change to the covering set of watches, starting with each prefix in it now.
|
|
721
|
+
* Returns the unsubscribe.
|
|
722
|
+
*/
|
|
723
|
+
interests(listener) {
|
|
724
|
+
this.#interests.add(listener);
|
|
725
|
+
if (this.#watches.count > 0)
|
|
726
|
+
listener(this.#watches.path, true);
|
|
727
|
+
else
|
|
728
|
+
for (const prefix of frontier(this.#watches))
|
|
729
|
+
listener(prefix, true);
|
|
730
|
+
return () => this.#interests.delete(listener);
|
|
731
|
+
}
|
|
378
732
|
closed = new Once();
|
|
379
733
|
/**
|
|
380
734
|
* Recompute what `path` resolves to, waking only the requests watching that path.
|
|
@@ -384,40 +738,343 @@ class OriginState {
|
|
|
384
738
|
* the answer for a single path.
|
|
385
739
|
*/
|
|
386
740
|
refresh(path) {
|
|
387
|
-
|
|
388
|
-
|
|
741
|
+
this.watchers.get(path)?.version.update((n) => n + 1);
|
|
742
|
+
const slot = this.requests.peek().get(path);
|
|
743
|
+
const detached = [...(this.detached.get(path) ?? [])];
|
|
744
|
+
if (slot)
|
|
745
|
+
this.reroute(path, slot);
|
|
746
|
+
for (const other of detached)
|
|
747
|
+
this.reroute(path, other);
|
|
748
|
+
this.forget(path);
|
|
749
|
+
}
|
|
750
|
+
/** Release unanswered requests no wire waiter or local route still needs. */
|
|
751
|
+
forget(path) {
|
|
752
|
+
const slot = this.requests.peek().get(path);
|
|
753
|
+
// An empty slot remains joinable until its deferred teardown removes it.
|
|
754
|
+
const best = slot ? this.bestEntry(path) : undefined;
|
|
755
|
+
const wanted = best && !this.localWins(path, best) ? best.server : undefined;
|
|
756
|
+
const failovers = this.detached.get(path);
|
|
757
|
+
let held;
|
|
758
|
+
for (const prefix of coveringPrefixes(path)) {
|
|
759
|
+
for (const entry of this.routes.peek().get(prefix) ?? []) {
|
|
760
|
+
const server = entry.server;
|
|
761
|
+
const pending = server?.pending.get(path);
|
|
762
|
+
if (!server || !pending || server.demanding.has(path) || server === wanted)
|
|
763
|
+
continue;
|
|
764
|
+
if (failovers) {
|
|
765
|
+
// Build once, only when pending work needs a failover ownership check.
|
|
766
|
+
if (!held) {
|
|
767
|
+
held = new Set();
|
|
768
|
+
for (const slot of failovers) {
|
|
769
|
+
if (slot.handles.size > 0 && slot.failover)
|
|
770
|
+
held.add(slot.failover);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
if (held.has(entry))
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
server.forget(path, pending);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Recompute what `slot` resolves to. A slot never moves to another publisher identity: while
|
|
782
|
+
* its own still serves and another wins `path`, it keeps what it resolved for the handles
|
|
783
|
+
* already on it and the next request resolves the winner on a slot of its own; once its own
|
|
784
|
+
* stops serving, it ends, except for an answer retained across an in-place Restart.
|
|
785
|
+
*/
|
|
786
|
+
reroute(path, slot) {
|
|
787
|
+
const before = slot.route.peek()?.source;
|
|
788
|
+
this.#reroute(path, slot);
|
|
789
|
+
// Only the same route still winning, with no takeover pending, is news for a read stranded
|
|
790
|
+
// on it, as the Rust front unstrands only when the selected route is the serving one.
|
|
791
|
+
const after = slot.route.peek();
|
|
792
|
+
if (before === undefined || after?.source !== before || after.pending || slot.failover)
|
|
793
|
+
return;
|
|
794
|
+
for (const moved of [...slot.moved])
|
|
795
|
+
moved();
|
|
796
|
+
}
|
|
797
|
+
#reroute(path, slot) {
|
|
798
|
+
if (this.retained.has(slot))
|
|
389
799
|
return;
|
|
800
|
+
const current = slot.route.peek();
|
|
801
|
+
// A pinned slot follows its identity before it resolves; once resolved, the detached branch
|
|
802
|
+
// passes its source along, so losing its identity's last route ends it even while parked.
|
|
803
|
+
if (slot.pin !== undefined && !current?.source) {
|
|
804
|
+
this.follow(path, slot, slot.pin, undefined);
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
// A detached slot follows its identity once resolved, parked or not.
|
|
808
|
+
if (current?.source && this.detached.get(path)?.has(slot)) {
|
|
809
|
+
this.follow(path, slot, current.identity, current.source);
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
if (current?.front && current.source) {
|
|
813
|
+
if (!this.serves(path, current.source, current.identity, false)) {
|
|
814
|
+
this.drop(path, slot, unroutable());
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
const next = this.identity(path);
|
|
818
|
+
const held = { ref: current.source, route: { identity: current.identity } };
|
|
819
|
+
if (next && !sameIdentity(held, next)) {
|
|
820
|
+
this.retire(path, slot);
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
// Another epoch of the identity wins: the slot's handles move onto it once it answers, but
|
|
824
|
+
// nobody joins them meanwhile, since an older epoch never stands in for a newer one.
|
|
825
|
+
if (next && (next.route.epoch ?? 0n) !== (current.epoch ?? 0n)) {
|
|
826
|
+
this.retire(path, slot);
|
|
827
|
+
this.follow(path, slot, current.identity, current.source);
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
390
831
|
slot.route.set(this.route(path, slot));
|
|
391
832
|
}
|
|
392
833
|
/**
|
|
393
|
-
* `
|
|
394
|
-
*
|
|
834
|
+
* Keep `slot` on `identity` through its cheapest route, as Rust's lookup does whichever
|
|
835
|
+
* identity wins `path`: a local broadcast announced under it, else its best route, each serving
|
|
836
|
+
* the same bytes. A move to a route that has not answered yet keeps the front `source` gave
|
|
837
|
+
* until it does. Without a route a resolved slot ends, and an unresolved one waits, its handles
|
|
838
|
+
* ending once discovery syncs. Without an identity, nothing says another source serves the same
|
|
839
|
+
* bytes, so the slot stays on `source` until it stops serving.
|
|
395
840
|
*/
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
841
|
+
follow(path, slot, identity, source) {
|
|
842
|
+
// A slot it follows records a pending takeover in `failover`, so one the slot was taken
|
|
843
|
+
// out of the table with is stale.
|
|
844
|
+
const current = slot.route.peek();
|
|
845
|
+
if (current?.pending)
|
|
846
|
+
slot.route.set({ ...current, pending: false });
|
|
847
|
+
if (identity === undefined) {
|
|
848
|
+
if (source && this.serves(path, source, identity, true))
|
|
849
|
+
return;
|
|
850
|
+
this.drop(path, slot, unroutable());
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
// A broadcast of the identity above `path` shadows its broader routes, as in Rust's lookup.
|
|
854
|
+
const skip = (claim, route) => route.identity !== identity || (!(claim instanceof broadcast.Consumer) && !claim.server);
|
|
855
|
+
const entry = this.bestEntry(path, skip);
|
|
856
|
+
const local = this.local.peek().get(path);
|
|
857
|
+
if (local &&
|
|
858
|
+
this.advertisedLocal.peek().get(path)?.identity === identity &&
|
|
859
|
+
this.localWins(path, entry, skip)) {
|
|
860
|
+
slot.failover = undefined;
|
|
861
|
+
if (source === local)
|
|
862
|
+
return;
|
|
863
|
+
this.retain(path, slot, undefined);
|
|
864
|
+
const epoch = this.advertisedLocal.peek().get(path)?.epoch;
|
|
865
|
+
slot.route.set({ front: local, identity, epoch, source: local, local: true });
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
if (!entry?.server) {
|
|
869
|
+
slot.failover = undefined;
|
|
870
|
+
if (source)
|
|
871
|
+
this.drop(path, slot, unroutable());
|
|
872
|
+
else
|
|
873
|
+
slot.route.set(undefined);
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
if (entry.ref === source && current?.front !== undefined && current.front.closed.peek() === undefined) {
|
|
877
|
+
// Already on the cheapest, still answering: a takeover still pending speaks for nobody.
|
|
878
|
+
slot.failover = undefined;
|
|
399
879
|
return;
|
|
400
|
-
|
|
401
|
-
|
|
880
|
+
}
|
|
881
|
+
// An application's claim refuses a path this origin publishes, rather than ask its
|
|
882
|
+
// handler for another job or hand out an answer it held from before: see
|
|
883
|
+
// {@link ServeState.publishes}.
|
|
884
|
+
if (entry.server.publishes(path)) {
|
|
885
|
+
slot.failover = undefined;
|
|
886
|
+
for (const closed of slot.handles)
|
|
887
|
+
refusedRequests.add(closed);
|
|
888
|
+
this.drop(path, slot, unroutable());
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
const served = entry.server.served.get(path);
|
|
892
|
+
if (!served || served.closed.peek() !== undefined) {
|
|
893
|
+
// Held by the slot, so the pending request outlives a forget.
|
|
894
|
+
slot.failover = entry;
|
|
895
|
+
entry.server.enqueue(path);
|
|
896
|
+
if (!source)
|
|
897
|
+
slot.route.set({ identity });
|
|
898
|
+
// Its answer closed: readers park on the identity until the route answers again.
|
|
899
|
+
else if (current?.front?.closed.peek() !== undefined) {
|
|
900
|
+
slot.route.set({ identity, epoch: current?.epoch, source });
|
|
901
|
+
}
|
|
402
902
|
return;
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
903
|
+
}
|
|
904
|
+
slot.failover = undefined;
|
|
905
|
+
this.retain(path, slot, served);
|
|
906
|
+
const { epoch, claimed } = entry.route.peek();
|
|
907
|
+
slot.route.set({ front: served, identity, epoch, source: entry.ref, claim: claimed });
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Make `front` the session answer `slot` holds, closing the one it held unless the cache or
|
|
911
|
+
* another detached slot shares it: a session serves one answer per path, so a pinned slot and
|
|
912
|
+
* the path's own requests can take the same one.
|
|
913
|
+
*/
|
|
914
|
+
retain(path, slot, front) {
|
|
915
|
+
const previous = slot.retired;
|
|
916
|
+
slot.retired = front;
|
|
917
|
+
if (previous && previous !== front)
|
|
918
|
+
this.closeUnheld(path, previous);
|
|
919
|
+
}
|
|
920
|
+
/** Close a session answer nothing at `path` holds any more: neither the cache nor a detached slot. */
|
|
921
|
+
closeUnheld(path, front) {
|
|
922
|
+
if (this.materialized.get(path)?.front === front)
|
|
407
923
|
return;
|
|
924
|
+
for (const slot of this.detached.get(path) ?? [])
|
|
925
|
+
if (slot.retired === front)
|
|
926
|
+
return;
|
|
927
|
+
front.close();
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Whether the identity `source` served under `identity` still serves `path`. Under an
|
|
931
|
+
* identity any route of it does, unless `exact` asks for `source` itself; without one only
|
|
932
|
+
* `source` does, since nothing says another serves the same bytes.
|
|
933
|
+
*/
|
|
934
|
+
serves(path, source, identity, exact) {
|
|
935
|
+
const matches = (candidate) => candidate === source || (!exact && identity !== undefined);
|
|
936
|
+
const local = this.local.peek().get(path);
|
|
937
|
+
const advertised = this.advertisedLocal.peek().get(path);
|
|
938
|
+
if (local && advertised && advertised.identity === identity && matches(local))
|
|
939
|
+
return true;
|
|
940
|
+
for (const [prefix, entries] of this.routes.peek()) {
|
|
941
|
+
if (!Path.hasPrefix(prefix, path))
|
|
942
|
+
continue;
|
|
943
|
+
for (const entry of entries) {
|
|
944
|
+
if (!entry.server || !admits(entry, path) || entry.route.peek().identity !== identity)
|
|
945
|
+
continue;
|
|
946
|
+
if (matches(entry.ref))
|
|
947
|
+
return true;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return false;
|
|
951
|
+
}
|
|
952
|
+
/** The publisher identity a request for `path` resolves through, when one serves it. */
|
|
953
|
+
identity(path) {
|
|
954
|
+
const entry = this.bestEntry(path);
|
|
955
|
+
const local = this.local.peek().get(path);
|
|
956
|
+
if (local && this.localWins(path, entry)) {
|
|
957
|
+
const advertised = this.advertisedLocal.peek().get(path);
|
|
958
|
+
return { ref: local, route: { identity: advertised?.identity, epoch: advertised?.epoch } };
|
|
408
959
|
}
|
|
960
|
+
if (!entry?.server)
|
|
961
|
+
return undefined;
|
|
962
|
+
return { ref: entry.ref, route: entry.route.peek() };
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Take `slot` out of the table with what it resolved: its handles stay on that identity
|
|
966
|
+
* until they close, and nothing joins it again.
|
|
967
|
+
*/
|
|
968
|
+
retire(path, slot) {
|
|
409
969
|
this.requests.mutate((map) => {
|
|
410
|
-
if (map
|
|
970
|
+
if (map.get(path) === slot)
|
|
411
971
|
map.delete(path);
|
|
412
972
|
});
|
|
973
|
+
let detached = this.detached.get(path);
|
|
974
|
+
if (!detached) {
|
|
975
|
+
detached = new Set();
|
|
976
|
+
this.detached.set(path, detached);
|
|
977
|
+
}
|
|
978
|
+
detached.add(slot);
|
|
979
|
+
const cached = this.materialized.get(path);
|
|
980
|
+
if (cached && cached.front === slot.route.peek()?.front) {
|
|
981
|
+
this.materialized.delete(path);
|
|
982
|
+
slot.retired = cached.front;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
/** Preserve this route's resolved answers before its server resets for another identity. */
|
|
986
|
+
restart(entry) {
|
|
987
|
+
for (const path of entry.server?.served.keys() ?? []) {
|
|
988
|
+
const joined = this.requests.peek().get(path);
|
|
989
|
+
const slots = [...(this.detached.get(path) ?? [])];
|
|
990
|
+
if (joined)
|
|
991
|
+
slots.push(joined);
|
|
992
|
+
for (const slot of slots) {
|
|
993
|
+
const current = slot.route.peek();
|
|
994
|
+
if (!current?.front ||
|
|
995
|
+
current.front.closed.peek() !== undefined ||
|
|
996
|
+
current.source !== entry.ref ||
|
|
997
|
+
this.retained.has(slot))
|
|
998
|
+
continue;
|
|
999
|
+
this.retire(path, slot);
|
|
1000
|
+
// reset() releases the server's front; retain a separate owner even when no
|
|
1001
|
+
// request has read active yet and therefore no per-request clone exists.
|
|
1002
|
+
slot.retired = current.front.clone();
|
|
1003
|
+
slot.failover = undefined;
|
|
1004
|
+
this.retained.add(slot);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
/** Whether a plain request needs the non-discovery session's blind answer. */
|
|
1009
|
+
blind(slot) {
|
|
1010
|
+
if (slot.blind === 0)
|
|
1011
|
+
return false;
|
|
1012
|
+
const { total, discovery } = this.sessions.peek();
|
|
1013
|
+
return discovery < total;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* `entry` refused `path` with `err`: the request ends with `err`, even while another
|
|
1017
|
+
* source serves, since asking the next candidate would turn one refusal into a request
|
|
1018
|
+
* per candidate.
|
|
1019
|
+
*/
|
|
1020
|
+
refuse(path, entry, err) {
|
|
1021
|
+
// A detached slot failing over to this route ends with its answer.
|
|
1022
|
+
for (const other of [...(this.detached.get(path) ?? [])]) {
|
|
1023
|
+
if (other.failover === entry) {
|
|
1024
|
+
for (const closed of other.handles)
|
|
1025
|
+
refusedRequests.add(closed);
|
|
1026
|
+
this.drop(path, other, err);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
const slot = this.requests.peek().get(path);
|
|
1030
|
+
if (!slot)
|
|
1031
|
+
return;
|
|
1032
|
+
// Only the route the request is waiting on speaks for it; one superseded by another
|
|
1033
|
+
// route or a local broadcast has a moot answer. Not for a handle pinned to the refusing
|
|
1034
|
+
// claim's identity while this origin publishes the path, though: that claim refuses every
|
|
1035
|
+
// request for the path (see ServeState.publishes), so it would refuse the handle's own.
|
|
1036
|
+
if (this.bestEntry(path) !== entry || this.localWins(path, entry)) {
|
|
1037
|
+
if (!entry.server?.publishes(path))
|
|
1038
|
+
return;
|
|
1039
|
+
const refused = entry.route.peek().identity;
|
|
1040
|
+
for (const pinned of [...slot.pinned])
|
|
1041
|
+
if (pinned.identity === refused)
|
|
1042
|
+
pinned.refuse(err);
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1045
|
+
for (const closed of slot.handles)
|
|
1046
|
+
refusedRequests.add(closed);
|
|
1047
|
+
this.drop(path, slot, err);
|
|
1048
|
+
}
|
|
1049
|
+
/** End `slot` with `err`, wherever it is held: it leaves the table and every handle closes. */
|
|
1050
|
+
drop(path, slot, err) {
|
|
1051
|
+
const joined = this.requests.peek().get(path) === slot;
|
|
1052
|
+
if (joined) {
|
|
1053
|
+
this.requests.mutate((map) => {
|
|
1054
|
+
map.delete(path);
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
this.detach(path, slot);
|
|
1058
|
+
slot.failover = undefined;
|
|
413
1059
|
slot.answer?.close();
|
|
414
1060
|
slot.answer = undefined;
|
|
415
1061
|
slot.route.set(undefined);
|
|
416
|
-
|
|
1062
|
+
if (joined)
|
|
1063
|
+
this.releaseMaterialized(path);
|
|
1064
|
+
this.forget(path);
|
|
417
1065
|
for (const closed of slot.handles)
|
|
418
1066
|
closed.set(err);
|
|
419
1067
|
slot.handles.clear();
|
|
420
1068
|
}
|
|
1069
|
+
/** Forget a detached slot, releasing the front it took. */
|
|
1070
|
+
detach(path, slot) {
|
|
1071
|
+
const detached = this.detached.get(path);
|
|
1072
|
+
if (!detached?.delete(slot))
|
|
1073
|
+
return;
|
|
1074
|
+
if (detached.size === 0)
|
|
1075
|
+
this.detached.delete(path);
|
|
1076
|
+
this.retain(path, slot, undefined);
|
|
1077
|
+
}
|
|
421
1078
|
/**
|
|
422
1079
|
* Recompute every open request covered by `prefix`, after a route was inserted or
|
|
423
1080
|
* removed there: a route covers many paths, so a single-path refresh is not enough.
|
|
@@ -425,14 +1082,55 @@ class OriginState {
|
|
|
425
1082
|
* releases a retracted route's session subscription even when nothing reads it again.
|
|
426
1083
|
*/
|
|
427
1084
|
refreshPrefix(prefix) {
|
|
428
|
-
for (const [path,
|
|
1085
|
+
for (const [path, watch] of this.watchers)
|
|
1086
|
+
if (Path.hasPrefix(prefix, path))
|
|
1087
|
+
watch.version.update((n) => n + 1);
|
|
1088
|
+
const detached = [...this.detached].map(([path, slots]) => [path, [...slots]]);
|
|
1089
|
+
for (const [path, slot] of [...this.requests.peek()]) {
|
|
429
1090
|
if (Path.hasPrefix(prefix, path))
|
|
430
|
-
|
|
1091
|
+
this.reroute(path, slot);
|
|
1092
|
+
}
|
|
1093
|
+
for (const [path, slots] of detached) {
|
|
1094
|
+
if (!Path.hasPrefix(prefix, path))
|
|
1095
|
+
continue;
|
|
1096
|
+
for (const slot of slots)
|
|
1097
|
+
this.reroute(path, slot);
|
|
1098
|
+
}
|
|
1099
|
+
for (const path of this.requests.peek().keys())
|
|
1100
|
+
if (Path.hasPrefix(prefix, path))
|
|
1101
|
+
this.forget(path);
|
|
1102
|
+
// Read again: a slot rerouting may have retired into it, releasing what it waited on.
|
|
1103
|
+
for (const path of this.detached.keys())
|
|
1104
|
+
if (Path.hasPrefix(prefix, path))
|
|
1105
|
+
this.forget(path);
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* Recompute `path` and every request beneath it, after a broadcast there changed: as a claim
|
|
1109
|
+
* on its subtree it can shadow or supersede what those resolve to. Found through the watch
|
|
1110
|
+
* trie, which holds every request's path, so a publish costs its own subtree rather than a
|
|
1111
|
+
* scan of every open request.
|
|
1112
|
+
*/
|
|
1113
|
+
refreshBeneath(path) {
|
|
1114
|
+
this.refresh(path);
|
|
1115
|
+
let node = this.#watches;
|
|
1116
|
+
for (const part of Path.parts(path))
|
|
1117
|
+
node = node?.children.get(part);
|
|
1118
|
+
if (!node)
|
|
1119
|
+
return;
|
|
1120
|
+
// Collected first: a reroute can end a request, which prunes the trie.
|
|
1121
|
+
const beneath = [];
|
|
1122
|
+
const stack = [...node.children.values()];
|
|
1123
|
+
for (let next = stack.pop(); next; next = stack.pop()) {
|
|
1124
|
+
if (next.count > 0)
|
|
1125
|
+
beneath.push(next.path);
|
|
1126
|
+
stack.push(...next.children.values());
|
|
431
1127
|
}
|
|
1128
|
+
for (const covered of beneath)
|
|
1129
|
+
this.refresh(covered);
|
|
432
1130
|
}
|
|
433
1131
|
/** Rebuild the publisher-facing originated table after an advertisement write. */
|
|
434
1132
|
rebuildOriginated() {
|
|
435
|
-
if (
|
|
1133
|
+
if (this.closed.peek() !== undefined) {
|
|
436
1134
|
this.originated.set(undefined);
|
|
437
1135
|
return;
|
|
438
1136
|
}
|
|
@@ -449,39 +1147,105 @@ class OriginState {
|
|
|
449
1147
|
if (!cached)
|
|
450
1148
|
return;
|
|
451
1149
|
this.materialized.delete(path);
|
|
452
|
-
cached.front
|
|
1150
|
+
this.closeUnheld(path, cached.front);
|
|
453
1151
|
}
|
|
454
|
-
/**
|
|
1152
|
+
/**
|
|
1153
|
+
* The best route entry among the most specific claims covering `path`, ignoring skipped ones.
|
|
1154
|
+
* An announced local broadcast published above `path` is one of those claims: it serves only
|
|
1155
|
+
* its own path, so a tier-mate that serves `path` outranks it, but it still supersedes an older
|
|
1156
|
+
* epoch of its identity and shadows broader claims, and undefined means it won. The local
|
|
1157
|
+
* broadcast at `path` itself competes through {@link localWins}. Mirrors
|
|
1158
|
+
* `RouteTable::best_route` in rs/moq-net.
|
|
1159
|
+
*/
|
|
455
1160
|
bestEntry(path, skip) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
1161
|
+
const routes = this.routes.peek();
|
|
1162
|
+
const local = this.local.peek();
|
|
1163
|
+
const advertised = this.advertisedLocal.peek();
|
|
1164
|
+
const filed = [];
|
|
1165
|
+
const exact = Path.parts(path).length;
|
|
1166
|
+
let depth = exact;
|
|
1167
|
+
for (const prefix of coveringPrefixes(path)) {
|
|
1168
|
+
let order = 0;
|
|
1169
|
+
for (const entry of routes.get(prefix) ?? []) {
|
|
1170
|
+
const route = entry.route.peek();
|
|
1171
|
+
if (!admits(entry, path) || skip?.(entry, route))
|
|
1172
|
+
continue;
|
|
1173
|
+
filed.push({
|
|
1174
|
+
prefix,
|
|
1175
|
+
depth,
|
|
1176
|
+
order: order++,
|
|
1177
|
+
arrival: arrival(entry.ref),
|
|
1178
|
+
candidate: entry,
|
|
1179
|
+
route,
|
|
1180
|
+
});
|
|
467
1181
|
}
|
|
1182
|
+
const front = depth < exact ? local.get(prefix) : undefined;
|
|
1183
|
+
const route = front && advertised.get(prefix);
|
|
1184
|
+
if (front && route && !skip?.(front, route))
|
|
1185
|
+
filed.push({ prefix, depth, order, arrival: arrival(front), candidate: front, route });
|
|
1186
|
+
depth--;
|
|
468
1187
|
}
|
|
469
|
-
|
|
1188
|
+
const own = local.has(path) ? advertised.get(path) : undefined;
|
|
1189
|
+
const best = bestOfTier(path, filed, {
|
|
1190
|
+
others: (specificity) => (own && specificity === exact ? [own] : []),
|
|
1191
|
+
serves: (entry) => !(entry.candidate instanceof broadcast.Consumer),
|
|
1192
|
+
})?.candidate;
|
|
1193
|
+
return best instanceof broadcast.Consumer ? undefined : best;
|
|
470
1194
|
}
|
|
471
1195
|
/**
|
|
472
1196
|
* Whether the announced local broadcast at `path` wins over `entry`, the best route a
|
|
473
|
-
* session or dynamic handle announced there.
|
|
474
|
-
* identified route strictly cheaper than the local one wins, and the local broadcast
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
1197
|
+
* session or dynamic handle announced there. They compare as any two routes do: an
|
|
1198
|
+
* identified route strictly cheaper than the local one wins, and the local broadcast wins a
|
|
1199
|
+
* tie. A route at a shorter prefix never competes, since the most specific prefix wins
|
|
1200
|
+
* outright. A higher epoch of the local broadcast's identity at the path supersedes it, among
|
|
1201
|
+
* the routes `skip` leaves, which must be what `entry` was chosen from. False when nothing is
|
|
1202
|
+
* announced locally at `path`.
|
|
1203
|
+
*/
|
|
1204
|
+
localWins(path, entry, skip) {
|
|
1205
|
+
const local = this.advertisedLocal.peek().get(path);
|
|
1206
|
+
if (!local || !this.local.peek().has(path))
|
|
1207
|
+
return false;
|
|
1208
|
+
if (!entry || !this.routes.peek().get(path)?.includes(entry))
|
|
1209
|
+
return true;
|
|
1210
|
+
const remote = entry.route.peek();
|
|
1211
|
+
// A broadcast exactly here shadows every broader claim shown here, whatever its epoch or
|
|
1212
|
+
// cost: the best claim is that broad only when each one is.
|
|
1213
|
+
if ((remote.above ?? 0n) > 0n)
|
|
1214
|
+
return true;
|
|
1215
|
+
// Within one identity only the higher epoch is eligible, however cheap the other is, so
|
|
1216
|
+
// any route at the path the request could take supersedes the local broadcast, not only
|
|
1217
|
+
// the best one.
|
|
1218
|
+
const rivals = (this.routes.peek().get(path) ?? [])
|
|
1219
|
+
.filter((candidate) => candidate.scope.matches(path))
|
|
1220
|
+
.map((candidate) => [candidate, candidate.route.peek()])
|
|
1221
|
+
.filter(([candidate, rival]) => !(skip?.(candidate, rival) ?? false))
|
|
1222
|
+
.map(([, rival]) => rival)
|
|
1223
|
+
// A broader claim shown here competes with nothing the local broadcast competes with.
|
|
1224
|
+
.filter((rival) => (rival.above ?? 0n) === 0n);
|
|
1225
|
+
if (superseded(local, rivals))
|
|
481
1226
|
return false;
|
|
482
|
-
if (
|
|
1227
|
+
if (superseded(remote, [local]))
|
|
483
1228
|
return true;
|
|
484
|
-
return compareRoutes(local,
|
|
1229
|
+
return compareRoutes(local, remote) <= 0;
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Whether a claim of `identity` serving `path` refuses a new request pinned to it, because
|
|
1233
|
+
* this origin publishes the path under another identity (see {@link publishes}).
|
|
1234
|
+
*/
|
|
1235
|
+
refuses(path, identity) {
|
|
1236
|
+
const skip = (claim, route) => route.identity !== identity || (!(claim instanceof broadcast.Consumer) && !claim.server);
|
|
1237
|
+
return this.bestEntry(path, skip)?.server?.publishes(path) ?? false;
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Whether an announced local broadcast of another identity than `identity` wins `path`, so a
|
|
1241
|
+
* request reaching a claim of `identity` for it can only be pinned to that identity. Another
|
|
1242
|
+
* epoch of `identity` continues its stream, so it does not count. Mirrors the Rust `publishes`.
|
|
1243
|
+
*/
|
|
1244
|
+
publishes(path, identity) {
|
|
1245
|
+
const local = this.advertisedLocal.peek().get(path);
|
|
1246
|
+
if (!local || local.identity === identity)
|
|
1247
|
+
return false;
|
|
1248
|
+
return this.localWins(path, this.bestEntry(path));
|
|
485
1249
|
}
|
|
486
1250
|
/**
|
|
487
1251
|
* What `path` resolves to: an announced local publish, a broadcast materialized from
|
|
@@ -490,39 +1254,65 @@ class OriginState {
|
|
|
490
1254
|
* Materialization is lazy and cached per path: the first request under a route opens
|
|
491
1255
|
* the providing session's subscription and repeats share it. A better route is made
|
|
492
1256
|
* before the old one breaks: the current front keeps serving until the new route
|
|
493
|
-
* answers (then swaps) or refuses (then
|
|
1257
|
+
* answers (then swaps) or refuses (then the request ends). A retracted route swaps at once.
|
|
494
1258
|
*/
|
|
495
1259
|
route(path, slot) {
|
|
496
|
-
const entry = this.bestEntry(path
|
|
497
|
-
const local = this.local.peek()
|
|
1260
|
+
const entry = this.bestEntry(path);
|
|
1261
|
+
const local = this.local.peek().get(path);
|
|
498
1262
|
if (local && this.localWins(path, entry)) {
|
|
499
1263
|
// Nothing reads a remote front the local broadcast replaced, so close its session subscription.
|
|
500
1264
|
this.releaseMaterialized(path);
|
|
501
|
-
|
|
1265
|
+
const advertised = this.advertisedLocal.peek().get(path);
|
|
1266
|
+
return {
|
|
1267
|
+
front: local,
|
|
1268
|
+
identity: advertised?.identity,
|
|
1269
|
+
epoch: advertised?.epoch,
|
|
1270
|
+
source: local,
|
|
1271
|
+
local: true,
|
|
1272
|
+
};
|
|
502
1273
|
}
|
|
503
1274
|
let cached = this.materialized.get(path);
|
|
504
1275
|
if (cached && cached.front.closed.peek() !== undefined) {
|
|
505
1276
|
this.materialized.delete(path);
|
|
506
1277
|
cached = undefined;
|
|
507
1278
|
}
|
|
508
|
-
|
|
509
|
-
|
|
1279
|
+
const identity = entry?.route.peek().identity;
|
|
1280
|
+
const epoch = entry?.route.peek().epoch;
|
|
1281
|
+
if (cached && cached.entry === entry && cached.identity === identity && cached.epoch === epoch)
|
|
1282
|
+
return cached;
|
|
510
1283
|
if (!entry?.server) {
|
|
511
1284
|
this.releaseMaterialized(path);
|
|
512
|
-
return slot.answer;
|
|
1285
|
+
return slot.answer ? { front: slot.answer } : undefined;
|
|
513
1286
|
}
|
|
514
1287
|
const served = entry.server.served.get(path);
|
|
515
1288
|
if (served && served.closed.peek() === undefined) {
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
1289
|
+
const claim = entry.route.peek().claimed;
|
|
1290
|
+
const resolution = { entry, front: served, identity, epoch, source: entry.ref, claim };
|
|
1291
|
+
this.materialized.set(path, resolution);
|
|
1292
|
+
if (cached && cached.front !== served)
|
|
1293
|
+
this.closeUnheld(path, cached.front);
|
|
1294
|
+
return resolution;
|
|
519
1295
|
}
|
|
520
1296
|
entry.server.enqueue(path);
|
|
521
|
-
|
|
1297
|
+
// The same identity and epoch over another route: the old front serves until the new one
|
|
1298
|
+
// answers. An older epoch never stands in for a newer one.
|
|
1299
|
+
const kept = cached?.identity === identity && (cached?.epoch ?? 0n) === (epoch ?? 0n) ? cached : undefined;
|
|
1300
|
+
return {
|
|
1301
|
+
front: kept?.front,
|
|
1302
|
+
identity,
|
|
1303
|
+
epoch: kept?.epoch,
|
|
1304
|
+
source: kept?.source,
|
|
1305
|
+
claim: kept?.claim,
|
|
1306
|
+
pending: true,
|
|
1307
|
+
};
|
|
522
1308
|
}
|
|
523
1309
|
}
|
|
1310
|
+
// Route constructors, assigned in the classes' static blocks so no class exposes one.
|
|
1311
|
+
let makeRoute;
|
|
1312
|
+
let makeClaim;
|
|
1313
|
+
let exactRoute;
|
|
524
1314
|
/**
|
|
525
|
-
* The write side of an origin:
|
|
1315
|
+
* The write side of an origin: route broadcasts into it by path and advertise them.
|
|
526
1316
|
*
|
|
527
1317
|
* Independent of any connection. A connection given this origin (via its `publish` option)
|
|
528
1318
|
* announces and serves the table's originated advertisements for as long as the session
|
|
@@ -530,29 +1320,67 @@ class OriginState {
|
|
|
530
1320
|
* the origin down. A reconnecting session re-announces the table on each attach, so
|
|
531
1321
|
* advertisements made while offline surface on the next connection.
|
|
532
1322
|
*
|
|
533
|
-
* Create
|
|
534
|
-
* {@link
|
|
535
|
-
*
|
|
1323
|
+
* Create a {@link route} at a path and fill its broadcast; create it hidden and
|
|
1324
|
+
* {@link Route.announce} it once its first tracks exist, since nobody can see or reach a hidden
|
|
1325
|
+
* route. To publish broadcasts on demand instead, {@link claim} a prefix and answer the
|
|
1326
|
+
* requests beneath it.
|
|
536
1327
|
*
|
|
537
1328
|
* @public
|
|
538
1329
|
*/
|
|
539
1330
|
export class Producer {
|
|
540
1331
|
#state = new OriginState();
|
|
541
1332
|
#scope = Scope.all;
|
|
1333
|
+
#priority = 0;
|
|
542
1334
|
#requests;
|
|
1335
|
+
// Set on a derived handle, which borrows the origin: what it registered, so its close
|
|
1336
|
+
// releases only that. The owner has none, since its close ends the whole origin.
|
|
1337
|
+
#borrowed;
|
|
543
1338
|
// The reader backing the passthroughs, so holding a Producer never requires the
|
|
544
|
-
// consume().x() stutter for everyday reads. One
|
|
1339
|
+
// consume().x() stutter for everyday reads. One object, so `discovery` keeps its
|
|
545
1340
|
// identity across reads.
|
|
546
1341
|
#reader = makeConsumer(this.#state, this.#scope);
|
|
1342
|
+
static {
|
|
1343
|
+
exactRoute = (producer, path, options, request) => producer.#exact(path, options, request);
|
|
1344
|
+
}
|
|
547
1345
|
constructor() {
|
|
548
1346
|
const thisProducer = this;
|
|
549
1347
|
registerWire(this, {
|
|
550
1348
|
receive: (prefix, route) => this.#receive(prefix, route),
|
|
551
|
-
interests: () =>
|
|
552
|
-
|
|
553
|
-
|
|
1349
|
+
interests: (listener) => {
|
|
1350
|
+
if (this.#borrowed?.closed)
|
|
1351
|
+
return () => { };
|
|
1352
|
+
// What the listener currently wants, so a derived handle's close can retract it
|
|
1353
|
+
// and its session stops asking the peer for announcements nothing will land.
|
|
1354
|
+
const wants = new Map();
|
|
1355
|
+
const unlisten = this.#state.interests((absolute, wanted) => {
|
|
1356
|
+
for (const prefix of this.#scope.interests(absolute)) {
|
|
1357
|
+
const count = (wants.get(prefix) ?? 0) + (wanted ? 1 : -1);
|
|
1358
|
+
if (count > 0)
|
|
1359
|
+
wants.set(prefix, count);
|
|
1360
|
+
else
|
|
1361
|
+
wants.delete(prefix);
|
|
1362
|
+
listener(prefix, wanted);
|
|
1363
|
+
}
|
|
1364
|
+
});
|
|
1365
|
+
return this.#hold(() => {
|
|
1366
|
+
unlisten();
|
|
1367
|
+
for (const [prefix, count] of wants)
|
|
1368
|
+
for (let i = 0; i < count; i++)
|
|
1369
|
+
listener(prefix, false);
|
|
1370
|
+
});
|
|
1371
|
+
},
|
|
1372
|
+
// A peer's route holding a `*`, or one that overflows the path bounds once rooted, is
|
|
1373
|
+
// skipped: `receive` would refuse it (see `nameable`), and that throw would end
|
|
1374
|
+
// discovery for the whole session.
|
|
1375
|
+
accepts: (prefix) => {
|
|
1376
|
+
if (prefix.includes("*"))
|
|
1377
|
+
return false;
|
|
1378
|
+
const path = Path.tryJoin(this.#scope.root, prefix);
|
|
1379
|
+
// The wire still counts escaped `/`s, which `receive` checks when it encodes the path.
|
|
1380
|
+
return path !== undefined && Path.check(path) === undefined && this.#scope.overlaps(path);
|
|
1381
|
+
},
|
|
554
1382
|
attach: (discovery) => this.#attach(discovery),
|
|
555
|
-
|
|
1383
|
+
snapshot: (prefix) => this.#borrowed?.closed ? () => { } : this.#hold(this.#state.addSnapshot(this.#scope.prefix(prefix))),
|
|
556
1384
|
get requests() {
|
|
557
1385
|
if (thisProducer.#scope === Scope.all)
|
|
558
1386
|
return thisProducer.#state.requests;
|
|
@@ -560,17 +1388,48 @@ export class Producer {
|
|
|
560
1388
|
return thisProducer.#requests;
|
|
561
1389
|
},
|
|
562
1390
|
changed: () => this.#changed(),
|
|
1391
|
+
blind: (slot) => this.#state.blind(slot),
|
|
563
1392
|
answer: (path, front) => this.#answer(this.#scope.path(path), front),
|
|
564
1393
|
routes: (path) => wireOf(this.#reader).routes(path),
|
|
1394
|
+
get priority() {
|
|
1395
|
+
return thisProducer.#priority;
|
|
1396
|
+
},
|
|
565
1397
|
});
|
|
566
1398
|
}
|
|
567
|
-
/**
|
|
568
|
-
|
|
569
|
-
|
|
1399
|
+
/**
|
|
1400
|
+
* Narrow this handle to `prefixes` beneath `root`, presenting paths relative to that root.
|
|
1401
|
+
* Each prefix covers its whole subtree. The result borrows the origin; see {@link close}.
|
|
1402
|
+
*/
|
|
1403
|
+
scope(root, prefixes) {
|
|
1404
|
+
if (this.#borrowed?.closed)
|
|
1405
|
+
throw new Error("origin handle is closed");
|
|
1406
|
+
return this.#derive(this.#scope.narrow(root, prefixes), this.#priority);
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Rank the link a session given this handle as its `consume` origin lands its routes
|
|
1410
|
+
* through: higher wins, and every handle starts at the neutral 0. An integer from -128 to 127.
|
|
1411
|
+
*
|
|
1412
|
+
* Only an identity's newest epoch is eligible, but among eligible routes priority decides
|
|
1413
|
+
* before cost, so links to different operators rank by this instead of comparing their
|
|
1414
|
+
* prices. On moq-lite-07 a route ranks by the lowest priority of any link on its path. The
|
|
1415
|
+
* result borrows the origin, as {@link scope}'s does, and its own derived handles keep its
|
|
1416
|
+
* priority.
|
|
1417
|
+
*/
|
|
1418
|
+
withPriority(priority) {
|
|
1419
|
+
if (this.#borrowed?.closed)
|
|
1420
|
+
throw new Error("origin handle is closed");
|
|
1421
|
+
if (!Number.isInteger(priority) || priority < -128 || priority > 127) {
|
|
1422
|
+
throw new RangeError("origin priority must be an integer from -128 to 127");
|
|
1423
|
+
}
|
|
1424
|
+
return this.#derive(this.#scope, priority);
|
|
1425
|
+
}
|
|
1426
|
+
#derive(scope, priority) {
|
|
570
1427
|
const producer = new Producer();
|
|
571
1428
|
producer.#state = this.#state;
|
|
572
1429
|
producer.#scope = scope;
|
|
1430
|
+
producer.#priority = priority;
|
|
573
1431
|
producer.#reader = makeConsumer(this.#state, scope);
|
|
1432
|
+
producer.#borrowed = { releases: new Set(), closed: false };
|
|
574
1433
|
return producer;
|
|
575
1434
|
}
|
|
576
1435
|
/**
|
|
@@ -581,203 +1440,430 @@ export class Producer {
|
|
|
581
1440
|
return this.#state.closed;
|
|
582
1441
|
}
|
|
583
1442
|
/**
|
|
584
|
-
* Create a
|
|
1443
|
+
* Create a route at exactly `path`, owning a new broadcast to fill.
|
|
585
1444
|
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
1445
|
+
* This is how local content enters an origin. Fill the broadcast through
|
|
1446
|
+
* {@link Route.broadcast}. The route serves a fresh random identity, minted here and kept
|
|
1447
|
+
* for its lifetime, across {@link Route.withdraw} and {@link Route.announce}, since its
|
|
1448
|
+
* keys continue: a new route is a new stream that never splices into an old one. A
|
|
1449
|
+
* publisher that continues one key space across restarts may name a stable identity
|
|
1450
|
+
* instead ({@link RouteOptions.identity}).
|
|
590
1451
|
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
* a
|
|
1452
|
+
* Announced at once unless created {@link RouteOptions.hidden | hidden}. A hidden route
|
|
1453
|
+
* exists for nobody until {@link Route.announce}: announce streams skip it and a request
|
|
1454
|
+
* for its path finds nothing, on this origin exactly as at a peer. Create it hidden when
|
|
1455
|
+
* the tracks a subscriber needs first, such as a catalog, are not there yet. To serve paths
|
|
1456
|
+
* on demand without publishing each one, use {@link claim}.
|
|
1457
|
+
*
|
|
1458
|
+
* Closing the route retracts it and closes its broadcast, and so does closing the
|
|
1459
|
+
* broadcast. Creating a path again supersedes the previous route: the origin retracts it
|
|
1460
|
+
* and closes its broadcast, and tracks already subscribed keep serving until they end. At
|
|
1461
|
+
* the same identity, a local broadcast competes on cost and wins ties.
|
|
595
1462
|
*/
|
|
596
|
-
|
|
1463
|
+
route(path, options = {}) {
|
|
1464
|
+
return this.#exact(path, options);
|
|
1465
|
+
}
|
|
1466
|
+
/** An exact route at `path`, answering `request` on its first announce when it is a job's. */
|
|
1467
|
+
#exact(path, options, request) {
|
|
597
1468
|
path = this.#scope.path(path);
|
|
598
|
-
|
|
599
|
-
|
|
1469
|
+
if (this.#borrowed?.closed)
|
|
1470
|
+
throw new Error("origin handle is closed");
|
|
1471
|
+
publishPath(path);
|
|
1472
|
+
if (this.#state.closed.peek() !== undefined)
|
|
600
1473
|
throw new Error("origin is closed");
|
|
1474
|
+
const created = this.#state.created;
|
|
1475
|
+
// Normalized first, so a bad cost throws before anything is created.
|
|
1476
|
+
let info = WireRoute.normalize({
|
|
1477
|
+
identity: options.identity?.id ?? Identity.random(),
|
|
1478
|
+
epoch: options.identity?.epoch ?? 0n,
|
|
1479
|
+
cost: options.cost,
|
|
1480
|
+
auth: options.auth,
|
|
1481
|
+
});
|
|
1482
|
+
// A job at its claim's own prefix ranks in the claim's tier, cost first: priced above the
|
|
1483
|
+
// claim, it would lose its path to the claim, which would ask for another job.
|
|
1484
|
+
const claim = request && jobServer(request).prefix === path ? jobServer(request) : undefined;
|
|
1485
|
+
if (claim?.exceeds(info.cost)) {
|
|
1486
|
+
rejectJob(request, unroutable());
|
|
1487
|
+
throw unroutable();
|
|
1488
|
+
}
|
|
601
1489
|
const producer = new broadcast.Producer();
|
|
602
1490
|
hooks.stampPath(producer, path);
|
|
603
1491
|
const front = producer.consume();
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
});
|
|
1492
|
+
const announced = () => this.#state.local.peek().get(path) === front;
|
|
1493
|
+
const retract = () => this.#retractExact(path, front);
|
|
1494
|
+
const entry = { producer, front };
|
|
608
1495
|
const previous = created.get(path);
|
|
609
|
-
created.set(path,
|
|
1496
|
+
created.set(path, entry);
|
|
610
1497
|
if (previous) {
|
|
611
|
-
this.#retractExact(path, previous);
|
|
612
|
-
previous
|
|
1498
|
+
this.#retractExact(path, previous.front);
|
|
1499
|
+
retire(previous);
|
|
613
1500
|
}
|
|
614
|
-
//
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
1501
|
+
// The request a job's route releases when it first announces.
|
|
1502
|
+
let pending = request;
|
|
1503
|
+
const priced = () => (announced() ? info.cost : undefined);
|
|
1504
|
+
claim?.jobs.add(priced);
|
|
1505
|
+
// Drop the route when its broadcast closes, unless a recreate already replaced it: a
|
|
1506
|
+
// stale broadcast closing must not unpublish the live one. Clearing `created` also stops
|
|
1507
|
+
// a later announce. A job's unanswered request can never be answered now.
|
|
1508
|
+
const release = this.#hold(() => {
|
|
1509
|
+
stop();
|
|
1510
|
+
retract();
|
|
1511
|
+
claim?.jobs.delete(priced);
|
|
1512
|
+
if (created.get(path) === entry)
|
|
1513
|
+
created.delete(path);
|
|
1514
|
+
retire(entry);
|
|
1515
|
+
const job = pending;
|
|
1516
|
+
pending = undefined;
|
|
1517
|
+
if (job)
|
|
1518
|
+
rejectJob(job, unroutable());
|
|
620
1519
|
});
|
|
621
|
-
|
|
1520
|
+
// Synchronous, so a request made right after the broadcast closes finds it gone.
|
|
1521
|
+
const stop = hooks.onClosed(producer, release);
|
|
1522
|
+
const announce = () => {
|
|
1523
|
+
if (producer.closed.peek() !== undefined)
|
|
1524
|
+
throw new Error("broadcast is closed");
|
|
1525
|
+
const job = pending;
|
|
1526
|
+
if (!job) {
|
|
1527
|
+
if (claim?.exceeds(info.cost))
|
|
1528
|
+
throw unroutable();
|
|
1529
|
+
this.#advertiseExact(path, front, info);
|
|
1530
|
+
return;
|
|
1531
|
+
}
|
|
1532
|
+
pending = undefined;
|
|
1533
|
+
try {
|
|
1534
|
+
// A request released meanwhile can never be answered, so this route never announces.
|
|
1535
|
+
if (!jobPending(job) || claim?.exceeds(info.cost))
|
|
1536
|
+
throw unroutable();
|
|
1537
|
+
this.#advertiseExact(path, front, info);
|
|
1538
|
+
}
|
|
1539
|
+
catch (err) {
|
|
1540
|
+
release();
|
|
1541
|
+
rejectJob(job, err instanceof Error ? err : new Error(String(err)));
|
|
1542
|
+
throw err;
|
|
1543
|
+
}
|
|
1544
|
+
// After the route lands, so a requester the release sends back resolves the path onto it.
|
|
1545
|
+
releaseJob(job);
|
|
1546
|
+
};
|
|
1547
|
+
const change = (next) => {
|
|
1548
|
+
if (producer.closed.peek() !== undefined)
|
|
1549
|
+
throw new Error("broadcast is closed");
|
|
1550
|
+
if (this.#state.closed.peek() !== undefined)
|
|
1551
|
+
throw new Error("origin is closed");
|
|
1552
|
+
if (next.cost !== info.cost && claim?.exceeds(next.cost))
|
|
1553
|
+
throw unroutable();
|
|
1554
|
+
info = next;
|
|
1555
|
+
if (announced())
|
|
1556
|
+
this.#advertiseExact(path, front, info);
|
|
1557
|
+
};
|
|
1558
|
+
const route = makeRoute(producer, {
|
|
1559
|
+
identity: () => info.identity,
|
|
1560
|
+
announce,
|
|
1561
|
+
withdraw: retract,
|
|
1562
|
+
announced,
|
|
1563
|
+
change: (update) => change(WireRoute.normalize({ ...info, ...update })),
|
|
1564
|
+
close: release,
|
|
1565
|
+
end: release,
|
|
1566
|
+
info: () => (announced() ? this.#state.advertisedLocal.peek().get(path) : undefined),
|
|
1567
|
+
update: (next) => {
|
|
1568
|
+
const named = WireRoute.normalize(next);
|
|
1569
|
+
// Refused before it is kept, so a refused price never lingers for a later change.
|
|
1570
|
+
if (claim?.exceeds(named.cost))
|
|
1571
|
+
throw unroutable();
|
|
1572
|
+
info = named.identity === undefined ? { ...named, identity: info.identity, epoch: info.epoch } : named;
|
|
1573
|
+
announce();
|
|
1574
|
+
},
|
|
1575
|
+
});
|
|
1576
|
+
if (!options.hidden)
|
|
1577
|
+
announce();
|
|
1578
|
+
return route;
|
|
622
1579
|
}
|
|
623
1580
|
#advertiseExact(path, front, route) {
|
|
624
|
-
if (
|
|
1581
|
+
if (this.#state.closed.peek() !== undefined)
|
|
625
1582
|
throw new Error("origin is closed");
|
|
626
|
-
if (this.#state.created
|
|
1583
|
+
if (this.#state.created.get(path)?.front !== front)
|
|
627
1584
|
throw new Error("broadcast is closed");
|
|
628
1585
|
// Both maps move together, so every reader sees the broadcast and its route at once.
|
|
629
1586
|
this.#state.local.mutate((broadcasts) => {
|
|
630
|
-
broadcasts
|
|
1587
|
+
broadcasts.set(path, front);
|
|
631
1588
|
});
|
|
632
1589
|
this.#state.advertisedLocal.mutate((advertised) => {
|
|
633
|
-
advertised
|
|
1590
|
+
advertised.set(path, route);
|
|
634
1591
|
});
|
|
1592
|
+
this.#state.changed([path]);
|
|
635
1593
|
this.#state.rebuildOriginated();
|
|
636
|
-
this.#state.
|
|
1594
|
+
this.#state.refreshBeneath(path);
|
|
637
1595
|
}
|
|
638
1596
|
#retractExact(path, front) {
|
|
639
|
-
if (this.#state.local.peek()
|
|
1597
|
+
if (this.#state.local.peek().get(path) !== front)
|
|
640
1598
|
return;
|
|
641
1599
|
this.#state.local.mutate((broadcasts) => {
|
|
642
|
-
broadcasts
|
|
1600
|
+
broadcasts.delete(path);
|
|
643
1601
|
});
|
|
644
1602
|
this.#state.advertisedLocal.mutate((advertised) => {
|
|
645
|
-
advertised
|
|
1603
|
+
advertised.delete(path);
|
|
646
1604
|
});
|
|
1605
|
+
this.#state.changed([path]);
|
|
647
1606
|
this.#state.rebuildOriginated();
|
|
648
|
-
this.#state.
|
|
1607
|
+
this.#state.refreshBeneath(path);
|
|
649
1608
|
}
|
|
650
1609
|
/**
|
|
651
|
-
*
|
|
1610
|
+
* Claim `prefix`, yielding the requests beneath it.
|
|
1611
|
+
*
|
|
1612
|
+
* A route is always a prefix on the wire: a claim covers `prefix` and every path beneath it
|
|
1613
|
+
* (the empty prefix claims every path). A service that only serves some of them advertises
|
|
1614
|
+
* the covering prefix and rejects the rest as they are requested; consumers filter
|
|
1615
|
+
* announced paths locally. The claim is priced, credentialed and shown like an exact
|
|
1616
|
+
* route, and forwarded by sessions while it is visible.
|
|
652
1617
|
*
|
|
653
|
-
* A
|
|
654
|
-
*
|
|
655
|
-
*
|
|
656
|
-
*
|
|
657
|
-
*
|
|
658
|
-
*
|
|
659
|
-
*
|
|
1618
|
+
* A consumer resolving a path under it that no announced local broadcast wins is handed to
|
|
1619
|
+
* {@link Claim.requested} as a {@link Request}: answer it by serving a broadcast already held
|
|
1620
|
+
* ({@link Request.serve}), under this claim's identity, or by creating the path's own exact
|
|
1621
|
+
* route ({@link Request.route}), a job. The claim keeps its identity, minted unless
|
|
1622
|
+
* {@link RouteOptions.identity} names one, for as long as it lives: each job's exact route
|
|
1623
|
+
* carries its own, so a job that ends and restarts is a new stream at its path without
|
|
1624
|
+
* touching the claim or its other paths. A publisher that knows its broadcasts creates each
|
|
1625
|
+
* one's exact {@link route} instead, so subscribers can enumerate them.
|
|
660
1626
|
*/
|
|
661
|
-
|
|
662
|
-
|
|
1627
|
+
claim(prefix, options = {}) {
|
|
1628
|
+
const info = WireRoute.normalize({
|
|
1629
|
+
identity: options.identity?.id,
|
|
1630
|
+
epoch: options.identity?.epoch,
|
|
1631
|
+
cost: options.cost,
|
|
1632
|
+
auth: options.auth,
|
|
1633
|
+
});
|
|
1634
|
+
return this.#serving(prefix, info, true, true, !options.hidden);
|
|
663
1635
|
}
|
|
664
1636
|
/**
|
|
665
|
-
* Land a route a peer announced,
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
* @internal
|
|
1637
|
+
* Land a route a peer announced, taken as given. Not originated, so a session never announces it
|
|
1638
|
+
* back.
|
|
669
1639
|
*/
|
|
670
|
-
#receive(prefix, route
|
|
671
|
-
return this.#
|
|
1640
|
+
#receive(prefix, route) {
|
|
1641
|
+
return this.#serving(prefix, WireRoute.normalize(route), false, false, true);
|
|
672
1642
|
}
|
|
673
|
-
#
|
|
1643
|
+
#serving(prefix, route, originated, publishes, visible) {
|
|
674
1644
|
prefix = this.#scope.prefix(prefix);
|
|
675
|
-
|
|
1645
|
+
// A route that names no identity serves one of its own.
|
|
1646
|
+
if (route.identity === undefined)
|
|
1647
|
+
route = { ...route, identity: Identity.random(), epoch: 0n };
|
|
1648
|
+
publishPath(prefix);
|
|
1649
|
+
const server = new ServeState(this.#scope.root, prefix);
|
|
676
1650
|
server.onChange = (path) => this.#state.refresh(path);
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
1651
|
+
if (publishes)
|
|
1652
|
+
server.publishes = (path) => this.#state.publishes(path, current.identity);
|
|
1653
|
+
// Filed where the scope lets it serve whole subtrees, each place as many segments above
|
|
1654
|
+
// its path as the claim is broader: one claim, with one ref and one server.
|
|
1655
|
+
const ref = {};
|
|
1656
|
+
let current = route;
|
|
1657
|
+
const lowered = (route, below) => below === 0n ? route : { ...route, above: (route.above ?? 0n) + below };
|
|
1658
|
+
const filed = this.#scope.filed(prefix).map(([at, below]) => {
|
|
1659
|
+
const entry = {
|
|
1660
|
+
ref,
|
|
1661
|
+
scope: this.#scope,
|
|
1662
|
+
route: new Signal(lowered(route, below)),
|
|
1663
|
+
originated,
|
|
1664
|
+
server,
|
|
1665
|
+
lost: false,
|
|
1666
|
+
};
|
|
1667
|
+
return { at, below, entry };
|
|
1668
|
+
});
|
|
1669
|
+
if (filed.length === 0)
|
|
1670
|
+
throw new Error("prefix is outside the origin scope");
|
|
1671
|
+
const filings = filed.map(({ at }) => at);
|
|
1672
|
+
server.onReject = (path, err) => {
|
|
1673
|
+
const place = filed.find(({ at }) => Path.hasPrefix(at, path));
|
|
1674
|
+
if (place)
|
|
1675
|
+
this.#state.refuse(path, place.entry, err);
|
|
684
1676
|
};
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
1677
|
+
const refresh = () => {
|
|
1678
|
+
this.#state.rebuildOriginated();
|
|
1679
|
+
for (const { at } of filed)
|
|
1680
|
+
this.#state.refreshPrefix(at);
|
|
1681
|
+
};
|
|
1682
|
+
let inserted = false;
|
|
1683
|
+
let ended = false;
|
|
1684
|
+
// Closing the origin drops the whole table without visiting each route, so a route
|
|
1685
|
+
// still marked inserted learns of it here, matching an exact route's "origin is closed".
|
|
1686
|
+
const open = () => {
|
|
1687
|
+
if (ended)
|
|
1688
|
+
throw new Error("claim is closed");
|
|
1689
|
+
if (this.#state.closed.peek() === undefined)
|
|
690
1690
|
return;
|
|
1691
|
+
inserted = false;
|
|
1692
|
+
throw new Error("origin is closed");
|
|
1693
|
+
};
|
|
1694
|
+
// Only a claim filed at its own prefix shares a tier with a job there.
|
|
1695
|
+
const own = filed.some(({ below }) => below === 0n);
|
|
1696
|
+
server.cost = () => (own && inserted && this.#state.closed.peek() === undefined ? current.cost : undefined);
|
|
1697
|
+
const insert = () => {
|
|
1698
|
+
open();
|
|
1699
|
+
if (inserted)
|
|
1700
|
+
return;
|
|
1701
|
+
if (own && server.undercuts(current.cost))
|
|
1702
|
+
throw unroutable();
|
|
1703
|
+
this.#state.routes.mutate((routes) => {
|
|
1704
|
+
for (const { at, entry } of filed) {
|
|
1705
|
+
const entries = routes.get(at);
|
|
1706
|
+
if (entries)
|
|
1707
|
+
entries.unshift(entry);
|
|
1708
|
+
else
|
|
1709
|
+
routes.set(at, [entry]);
|
|
1710
|
+
}
|
|
1711
|
+
});
|
|
1712
|
+
this.#state.changed(filings);
|
|
1713
|
+
// Newest at each filing, so newest across them too, as Rust files a new entry each time.
|
|
1714
|
+
receive(ref);
|
|
1715
|
+
inserted = true;
|
|
1716
|
+
refresh();
|
|
1717
|
+
};
|
|
1718
|
+
// Take the entries out of the table, then end (or renew) the server before the requests
|
|
1719
|
+
// beneath them reroute, so none of them resolves through an answer it already gave.
|
|
1720
|
+
const remove = (server) => {
|
|
1721
|
+
if (inserted) {
|
|
1722
|
+
inserted = false;
|
|
1723
|
+
this.#state.routes.mutate((routes) => {
|
|
1724
|
+
for (const { at, entry } of filed) {
|
|
1725
|
+
const entries = routes.get(at);
|
|
1726
|
+
if (!entries)
|
|
1727
|
+
continue;
|
|
1728
|
+
const index = entries.indexOf(entry);
|
|
1729
|
+
if (index < 0)
|
|
1730
|
+
continue;
|
|
1731
|
+
entries.splice(index, 1);
|
|
1732
|
+
if (entries.length === 0)
|
|
1733
|
+
routes.delete(at);
|
|
1734
|
+
}
|
|
1735
|
+
});
|
|
1736
|
+
this.#state.changed(filings);
|
|
691
1737
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
1738
|
+
server();
|
|
1739
|
+
refresh();
|
|
1740
|
+
};
|
|
1741
|
+
// A received route torn down by any path (its handle, or the scoped handle it came through)
|
|
1742
|
+
// was lost with its session; only its announcer ending it on the wire withdraws it.
|
|
1743
|
+
let lost = !originated;
|
|
1744
|
+
const release = this.#hold(() => {
|
|
1745
|
+
if (!ended)
|
|
1746
|
+
for (const { entry } of filed)
|
|
1747
|
+
entry.lost = lost;
|
|
1748
|
+
ended = true;
|
|
1749
|
+
this.#state.servers.delete(server);
|
|
1750
|
+
remove(() => server.close());
|
|
697
1751
|
});
|
|
698
|
-
|
|
1752
|
+
// A route made after its origin (or this handle) closed is born retracted.
|
|
1753
|
+
if (this.#borrowed?.closed || this.#state.closed.peek() !== undefined) {
|
|
1754
|
+
ended = true;
|
|
699
1755
|
server.close();
|
|
700
|
-
return makeDynamic(prefix, entry, this.#state, () => { });
|
|
701
1756
|
}
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
1757
|
+
else {
|
|
1758
|
+
this.#state.servers.add(server);
|
|
1759
|
+
if (visible)
|
|
1760
|
+
insert();
|
|
1761
|
+
}
|
|
1762
|
+
const update = (next) => {
|
|
1763
|
+
open();
|
|
1764
|
+
const previous = current.identity;
|
|
1765
|
+
const restart = inserted && previous !== next.identity;
|
|
1766
|
+
const renewed = previous !== next.identity || (current.epoch ?? 0n) !== (next.epoch ?? 0n);
|
|
1767
|
+
current = next;
|
|
1768
|
+
for (const { below, entry } of filed) {
|
|
1769
|
+
if (restart)
|
|
1770
|
+
this.#state.restart(entry);
|
|
1771
|
+
entry.route.set(lowered(next, below));
|
|
1772
|
+
}
|
|
1773
|
+
if (previous !== next.identity)
|
|
1774
|
+
server.reset();
|
|
1775
|
+
if (!inserted)
|
|
1776
|
+
return;
|
|
1777
|
+
// A claim is new when it restarts, so it moves to the newest place and yields a tie
|
|
1778
|
+
// it held to one that kept its identity (see compareTie), at each filing and across them.
|
|
1779
|
+
if (renewed)
|
|
1780
|
+
receive(ref);
|
|
705
1781
|
this.#state.routes.mutate((routes) => {
|
|
706
|
-
|
|
707
|
-
if (!entries)
|
|
1782
|
+
if (!renewed)
|
|
708
1783
|
return;
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
1784
|
+
for (const { at, entry } of filed) {
|
|
1785
|
+
const entries = routes.get(at);
|
|
1786
|
+
const index = entries?.indexOf(entry) ?? -1;
|
|
1787
|
+
if (!entries || index <= 0)
|
|
1788
|
+
continue;
|
|
1789
|
+
entries.splice(index, 1);
|
|
1790
|
+
entries.unshift(entry);
|
|
1791
|
+
}
|
|
715
1792
|
});
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
slot.refused.delete(entry);
|
|
719
|
-
server.close();
|
|
720
|
-
this.#state.rebuildOriginated();
|
|
721
|
-
this.#state.refreshPrefix(prefix);
|
|
1793
|
+
this.#state.changed(filings);
|
|
1794
|
+
refresh();
|
|
722
1795
|
};
|
|
723
|
-
|
|
1796
|
+
const publisher = publishes ? this : undefined;
|
|
1797
|
+
return makeClaim({
|
|
1798
|
+
identity: () => current.identity,
|
|
1799
|
+
announce: insert,
|
|
1800
|
+
withdraw: () => {
|
|
1801
|
+
if (ended || !inserted)
|
|
1802
|
+
return;
|
|
1803
|
+
remove(() => server.reset());
|
|
1804
|
+
},
|
|
1805
|
+
announced: () => inserted && this.#state.closed.peek() === undefined,
|
|
1806
|
+
change: (changed) => {
|
|
1807
|
+
const next = WireRoute.normalize({ ...current, ...changed });
|
|
1808
|
+
if (own && next.cost !== current.cost && server.undercuts(next.cost))
|
|
1809
|
+
throw unroutable();
|
|
1810
|
+
update(next);
|
|
1811
|
+
},
|
|
1812
|
+
close: release,
|
|
1813
|
+
end: () => {
|
|
1814
|
+
lost = false;
|
|
1815
|
+
release();
|
|
1816
|
+
},
|
|
1817
|
+
info: () => (inserted && this.#state.closed.peek() === undefined ? current : undefined),
|
|
1818
|
+
update: (next) => {
|
|
1819
|
+
// A route naming no identity keeps the one it serves.
|
|
1820
|
+
const named = WireRoute.normalize(next);
|
|
1821
|
+
update(named.identity === undefined
|
|
1822
|
+
? { ...named, identity: current.identity, epoch: current.epoch }
|
|
1823
|
+
: named);
|
|
1824
|
+
insert();
|
|
1825
|
+
},
|
|
1826
|
+
}, () => serveRequests(server, publisher));
|
|
724
1827
|
}
|
|
725
1828
|
/**
|
|
726
1829
|
* Register an attached session, counting it toward the `discovery` state. Returns the
|
|
727
1830
|
* detach; call it exactly once when the session dies.
|
|
728
|
-
*
|
|
729
|
-
* @internal
|
|
730
1831
|
*/
|
|
731
1832
|
#attach(discovery) {
|
|
1833
|
+
if (this.#borrowed?.closed)
|
|
1834
|
+
return () => { };
|
|
732
1835
|
this.#sessions(1, discovery);
|
|
733
|
-
|
|
734
|
-
let detached = false;
|
|
735
|
-
return () => {
|
|
736
|
-
if (detached)
|
|
737
|
-
return;
|
|
738
|
-
detached = true;
|
|
739
|
-
this.#sessions(-1, discovery);
|
|
740
|
-
release();
|
|
741
|
-
};
|
|
1836
|
+
return this.#hold(() => this.#sessions(-1, discovery));
|
|
742
1837
|
}
|
|
743
1838
|
#sessions(delta, discovery) {
|
|
744
1839
|
this.#state.sessions.update(({ total, discovery: d }) => ({
|
|
745
1840
|
total: total + delta,
|
|
746
1841
|
discovery: d + (discovery ? delta : 0),
|
|
747
1842
|
}));
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
*
|
|
759
|
-
* @internal
|
|
760
|
-
*/
|
|
761
|
-
#expect() {
|
|
762
|
-
this.#state.answerers.update((count) => count + 1);
|
|
763
|
-
let released = false;
|
|
764
|
-
return () => {
|
|
765
|
-
if (released)
|
|
1843
|
+
// Losing a session without discovery can leave every pending request synced.
|
|
1844
|
+
this.#state.settle(Path.empty());
|
|
1845
|
+
}
|
|
1846
|
+
// Run `release` once, at the first of its own call or this derived handle's close, so a
|
|
1847
|
+
// borrower's close withdraws what it registered. The owner's close tears down everything.
|
|
1848
|
+
#hold(release) {
|
|
1849
|
+
const borrowed = this.#borrowed;
|
|
1850
|
+
let done = false;
|
|
1851
|
+
const held = () => {
|
|
1852
|
+
if (done)
|
|
766
1853
|
return;
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
this.#state.answerers.update((count) => Math.max(0, count - 1));
|
|
1854
|
+
done = true;
|
|
1855
|
+
borrowed?.releases.delete(held);
|
|
1856
|
+
release();
|
|
771
1857
|
};
|
|
1858
|
+
borrowed?.releases.add(held);
|
|
1859
|
+
return held;
|
|
772
1860
|
}
|
|
773
1861
|
/**
|
|
774
|
-
* Resolves once anything a serving session scans changes: the open requests,
|
|
775
|
-
* side of the routing table.
|
|
776
|
-
*
|
|
777
|
-
* @internal
|
|
1862
|
+
* Resolves once anything a serving session scans changes: the open requests, either
|
|
1863
|
+
* side of the routing table, or the attached sessions that decide which requests are blind.
|
|
778
1864
|
*/
|
|
779
1865
|
#changed() {
|
|
780
|
-
return Signal.race(this.#state.requests, this.#state.local, this.#state.routes, this.#state.advertisedLocal);
|
|
1866
|
+
return Signal.race(this.#state.requests, this.#state.local, this.#state.routes, this.#state.advertisedLocal, this.#state.sessions);
|
|
781
1867
|
}
|
|
782
1868
|
/**
|
|
783
1869
|
* Provide `front` as the answer for the open request on `path`, taking ownership of it.
|
|
@@ -787,27 +1873,32 @@ export class Producer {
|
|
|
787
1873
|
* withdraw releases the front and, if it was the standing answer, vacates the slot and
|
|
788
1874
|
* wakes the other serving loops so a standby session answers immediately; call it when
|
|
789
1875
|
* the session dies.
|
|
790
|
-
*
|
|
791
|
-
* @internal
|
|
792
1876
|
*/
|
|
793
1877
|
#answer(path, front) {
|
|
794
|
-
const slot = this.#state.requests.peek()
|
|
795
|
-
if (!slot || slot.answer !== undefined) {
|
|
1878
|
+
const slot = this.#state.requests.peek().get(path);
|
|
1879
|
+
if (!slot || slot.answer !== undefined || this.#borrowed?.closed) {
|
|
796
1880
|
front.close();
|
|
797
1881
|
return undefined;
|
|
798
1882
|
}
|
|
799
1883
|
slot.answer = front;
|
|
800
1884
|
this.#state.refresh(path);
|
|
801
|
-
return () => {
|
|
1885
|
+
return this.#hold(() => {
|
|
802
1886
|
if (slot.answer === front) {
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
1887
|
+
// A request the answer resolved ends with its session: the next session is another
|
|
1888
|
+
// publisher identity, since nothing (no identity) says it serves the same bytes.
|
|
1889
|
+
if (slot.route.peek()?.front === front) {
|
|
1890
|
+
this.#state.drop(path, slot, unroutable());
|
|
1891
|
+
}
|
|
1892
|
+
else {
|
|
1893
|
+
slot.answer = undefined;
|
|
1894
|
+
this.#state.refresh(path);
|
|
1895
|
+
// The route signal only reaches this path's requesters; poke the map so every
|
|
1896
|
+
// serving loop re-scans and one of them re-answers.
|
|
1897
|
+
this.#state.requests.mutate(() => { });
|
|
1898
|
+
}
|
|
808
1899
|
}
|
|
809
1900
|
front.close();
|
|
810
|
-
};
|
|
1901
|
+
});
|
|
811
1902
|
}
|
|
812
1903
|
/** A read handle for this origin, the side a connection's `publish` option borrows. */
|
|
813
1904
|
consume() {
|
|
@@ -817,67 +1908,122 @@ export class Producer {
|
|
|
817
1908
|
get discovery() {
|
|
818
1909
|
return this.#reader.discovery;
|
|
819
1910
|
}
|
|
820
|
-
/** Resolve
|
|
1911
|
+
/** Resolve one publisher identity at `path`; see {@link Consumer.request}. */
|
|
821
1912
|
request(path, options) {
|
|
822
1913
|
return this.#reader.request(path, options);
|
|
823
1914
|
}
|
|
824
|
-
/**
|
|
825
|
-
|
|
826
|
-
return this.#reader.
|
|
1915
|
+
/** Follow `path` across publisher identities; see {@link Consumer.follow}. */
|
|
1916
|
+
follow(path, options) {
|
|
1917
|
+
return this.#reader.follow(path, options);
|
|
827
1918
|
}
|
|
828
|
-
/** The available broadcasts
|
|
829
|
-
announced(
|
|
830
|
-
return this.#reader.announced(
|
|
1919
|
+
/** The available broadcasts at or beneath `prefix`, as a live stream; see {@link Consumer.announced}. */
|
|
1920
|
+
announced(prefix, options) {
|
|
1921
|
+
return this.#reader.announced(prefix, options);
|
|
831
1922
|
}
|
|
832
|
-
/**
|
|
1923
|
+
/** Every route change at or beneath `prefix`; see {@link Consumer.routes}. @internal */
|
|
1924
|
+
routes(prefix, options) {
|
|
1925
|
+
return this.#reader.routes(prefix, options);
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Close the origin, every broadcast it still routes, and its announcement streams. Idempotent.
|
|
1929
|
+
*
|
|
1930
|
+
* A handle from {@link scope} borrows the origin: closing it
|
|
1931
|
+
* withdraws only the broadcasts, routes, and sessions (with their answers) registered
|
|
1932
|
+
* through it and refuses new ones, ignoring `abort`. Only the handle made with `new Producer()` closes the origin.
|
|
1933
|
+
*/
|
|
833
1934
|
close(abort) {
|
|
1935
|
+
const borrowed = this.#borrowed;
|
|
1936
|
+
if (borrowed) {
|
|
1937
|
+
if (borrowed.closed)
|
|
1938
|
+
return;
|
|
1939
|
+
borrowed.closed = true;
|
|
1940
|
+
for (const release of [...borrowed.releases])
|
|
1941
|
+
release();
|
|
1942
|
+
borrowed.releases.clear();
|
|
1943
|
+
return;
|
|
1944
|
+
}
|
|
834
1945
|
if (this.#state.closed.peek() !== undefined)
|
|
835
1946
|
return;
|
|
836
1947
|
this.#state.closed.set(abort ?? null);
|
|
837
|
-
for (const
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
this.#state.
|
|
842
|
-
this.#state.
|
|
1948
|
+
for (const watch of this.#state.watchers.values())
|
|
1949
|
+
watch.version.update((n) => n + 1);
|
|
1950
|
+
// Cleared before the broadcasts close, so each route's synchronous retraction finds
|
|
1951
|
+
// nothing left to rebuild rather than rebuilding the table once per route.
|
|
1952
|
+
const created = this.#state.created;
|
|
1953
|
+
this.#state.created = new Map();
|
|
1954
|
+
this.#state.local.update(() => new Map());
|
|
1955
|
+
this.#state.advertisedLocal.update(() => new Map());
|
|
1956
|
+
for (const entry of created.values())
|
|
1957
|
+
retire(entry);
|
|
843
1958
|
this.#state.routes.update((routes) => {
|
|
844
|
-
for (const entries of routes
|
|
1959
|
+
for (const entries of routes.values()) {
|
|
845
1960
|
for (const entry of entries)
|
|
846
1961
|
entry.server?.close(abort);
|
|
847
1962
|
}
|
|
848
|
-
return
|
|
1963
|
+
return new Map();
|
|
849
1964
|
});
|
|
1965
|
+
this.#state.clearSnapshot();
|
|
1966
|
+
// A hidden or withdrawn prefix route is out of the table but its handler still waits.
|
|
1967
|
+
for (const server of this.#state.servers)
|
|
1968
|
+
server.close(abort);
|
|
1969
|
+
this.#state.servers.clear();
|
|
850
1970
|
this.#state.originated.update(() => undefined);
|
|
851
1971
|
// Materialized broadcasts are handles we opened; release them.
|
|
852
1972
|
for (const cached of this.#state.materialized.values()) {
|
|
853
1973
|
cached.front.close();
|
|
854
1974
|
}
|
|
855
1975
|
this.#state.materialized.clear();
|
|
856
|
-
//
|
|
857
|
-
|
|
858
|
-
this.#state.answerers.set(0);
|
|
1976
|
+
// Every open request ends with the origin, pending or resolved, so an await never hangs.
|
|
1977
|
+
const ended = [];
|
|
859
1978
|
this.#state.requests.update((requests) => {
|
|
860
|
-
for (const slot of requests
|
|
1979
|
+
for (const slot of requests.values()) {
|
|
861
1980
|
slot.answer?.close();
|
|
862
1981
|
slot.answer = undefined;
|
|
863
1982
|
slot.route.set(undefined);
|
|
1983
|
+
ended.push(slot);
|
|
864
1984
|
}
|
|
865
|
-
return
|
|
1985
|
+
return new Map();
|
|
866
1986
|
});
|
|
1987
|
+
for (const [path, slots] of [...this.#state.detached]) {
|
|
1988
|
+
for (const slot of slots) {
|
|
1989
|
+
this.#state.detach(path, slot);
|
|
1990
|
+
slot.route.set(undefined);
|
|
1991
|
+
ended.push(slot);
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
for (const slot of ended) {
|
|
1995
|
+
for (const closed of slot.handles)
|
|
1996
|
+
if (closed.peek() === undefined)
|
|
1997
|
+
closed.set(abort ?? null);
|
|
1998
|
+
}
|
|
867
1999
|
}
|
|
868
2000
|
}
|
|
869
2001
|
// Constructs a Consumer from within this module without exposing a public constructor
|
|
870
2002
|
// that would leak the unexported OriginState. Assigned in the class's static block.
|
|
871
2003
|
let makeConsumer;
|
|
872
|
-
// Same for Requesting: a public constructor would let a caller forge a handle that no origin
|
|
2004
|
+
// Same for Requesting and Following: a public constructor would let a caller forge a handle that no origin
|
|
873
2005
|
// ever registered, whose lifecycle guarantees are then false. `@internal` alone would not
|
|
874
2006
|
// stop it, since the declaration emit keeps the constructor.
|
|
875
2007
|
let makeRequesting;
|
|
876
|
-
let
|
|
2008
|
+
let makeFollowing;
|
|
877
2009
|
let makeRequest;
|
|
878
2010
|
let finishRequest;
|
|
2011
|
+
let setRequestPublisher;
|
|
2012
|
+
let jobPending;
|
|
2013
|
+
let releaseJob;
|
|
2014
|
+
let rejectJob;
|
|
2015
|
+
let jobServer;
|
|
879
2016
|
/**
|
|
880
|
-
* An
|
|
2017
|
+
* An owned request for one publisher identity at a broadcast path; see {@link Consumer.request}.
|
|
2018
|
+
*
|
|
2019
|
+
* Await {@link broadcast} for the broadcast, as Rust awaits `request(path)`; read {@link active} and
|
|
2020
|
+
* {@link closed} to follow it reactively instead. The handle is not itself awaitable, so an
|
|
2021
|
+
* `await` or an async return never takes the broadcast out from under the request that owns it.
|
|
2022
|
+
*
|
|
2023
|
+
* ```ts
|
|
2024
|
+
* const request = origin.request(Path.from("room"));
|
|
2025
|
+
* const broadcast = await request.broadcast();
|
|
2026
|
+
* ```
|
|
881
2027
|
*
|
|
882
2028
|
* @public
|
|
883
2029
|
*/
|
|
@@ -885,49 +2031,105 @@ export class Requesting {
|
|
|
885
2031
|
/** The requested path. */
|
|
886
2032
|
path;
|
|
887
2033
|
/**
|
|
888
|
-
* The
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
*
|
|
892
|
-
* answer, which is assumed present rather than known live: a missing broadcast
|
|
893
|
-
* surfaces as a reset on the first track subscription, not here. Drops back to
|
|
894
|
-
* undefined when the providing route dies and resolves again when another appears.
|
|
895
|
-
*
|
|
896
|
-
* Yours for as long as the request is open: it is a handle of this request's own, so
|
|
897
|
-
* closing it ends your view of the path rather than the route everyone else reads.
|
|
898
|
-
* {@link close} releases whatever is current.
|
|
2034
|
+
* The accepted broadcast, or undefined while the request is pending.
|
|
2035
|
+
* Another route of the same identity taking over keeps
|
|
2036
|
+
* this handle: its tracks resume on that route from the first group each reader lacks.
|
|
2037
|
+
* Closing this handle affects only this request.
|
|
899
2038
|
*/
|
|
900
2039
|
active;
|
|
901
2040
|
/**
|
|
902
|
-
*
|
|
903
|
-
*
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
2041
|
+
* Settles on refusal, origin shutdown, explicit close, or the accepted identity ending.
|
|
2042
|
+
* Fails `Unroutable` while pending once discovery syncs without a route for it.
|
|
2043
|
+
*/
|
|
2044
|
+
closed;
|
|
2045
|
+
#dispose;
|
|
2046
|
+
#disposed = false;
|
|
2047
|
+
#answer;
|
|
2048
|
+
constructor(path, active, closed, dispose) {
|
|
2049
|
+
this.path = path;
|
|
2050
|
+
this.active = active;
|
|
2051
|
+
this.closed = closed;
|
|
2052
|
+
this.#dispose = dispose;
|
|
2053
|
+
}
|
|
2054
|
+
static {
|
|
2055
|
+
makeRequesting = (path, active, closed, dispose) => new Requesting(path, active, closed, dispose);
|
|
2056
|
+
}
|
|
2057
|
+
/**
|
|
2058
|
+
* Resolves with the broadcast once {@link active} holds one. Rejects with the error
|
|
2059
|
+
* {@link closed} carries if the request ends first, such as `Unroutable`, or with a
|
|
2060
|
+
* `StreamCode.Cancel` error if it closed cleanly: this handle or its origin was closed. The
|
|
2061
|
+
* answer is fixed by the first call, and every later call gets the same one. The request
|
|
2062
|
+
* still owns the broadcast, so close the request, not only the broadcast, when done.
|
|
2063
|
+
*/
|
|
2064
|
+
broadcast() {
|
|
2065
|
+
this.#answer ??= answer(this.active, this.closed);
|
|
2066
|
+
return this.#answer;
|
|
2067
|
+
}
|
|
2068
|
+
/** Withdraw the request. The path stays routed for any other open request. Idempotent. */
|
|
2069
|
+
close() {
|
|
2070
|
+
if (this.#disposed)
|
|
2071
|
+
return;
|
|
2072
|
+
this.#disposed = true;
|
|
2073
|
+
this.#dispose();
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
// The first of `active` holding a broadcast or `closed` settling, as a promise.
|
|
2077
|
+
function answer(active, closed) {
|
|
2078
|
+
return new Promise((resolve, reject) => {
|
|
2079
|
+
let done = false;
|
|
2080
|
+
const stops = [];
|
|
2081
|
+
const check = () => {
|
|
2082
|
+
if (done)
|
|
2083
|
+
return;
|
|
2084
|
+
const ended = closed.peek();
|
|
2085
|
+
const value = ended === undefined ? active.peek() : undefined;
|
|
2086
|
+
if (ended === undefined && !value)
|
|
2087
|
+
return;
|
|
2088
|
+
done = true;
|
|
2089
|
+
for (const stop of stops)
|
|
2090
|
+
stop();
|
|
2091
|
+
if (value)
|
|
2092
|
+
resolve(value);
|
|
2093
|
+
else
|
|
2094
|
+
reject(ended ?? new StreamError(StreamCode.Cancel, { message: "request closed" }));
|
|
2095
|
+
};
|
|
2096
|
+
check();
|
|
2097
|
+
if (!done)
|
|
2098
|
+
stops.push(active.subscribe(check), closed.subscribe(check));
|
|
2099
|
+
});
|
|
2100
|
+
}
|
|
2101
|
+
/**
|
|
2102
|
+
* An owned follower of the publisher identities announced at a broadcast path; see {@link Consumer.follow}.
|
|
2103
|
+
*
|
|
2104
|
+
* @public
|
|
2105
|
+
*/
|
|
2106
|
+
export class Following {
|
|
2107
|
+
/** The followed path. */
|
|
2108
|
+
path;
|
|
2109
|
+
/**
|
|
2110
|
+
* The current announced broadcast, or undefined between publisher identities.
|
|
2111
|
+
* Moves to each new identity, or waits for the pinned identity when one was requested.
|
|
2112
|
+
* Another route of the same identity keeps this handle and resumes its tracks.
|
|
2113
|
+
* Closing this handle affects only this follower.
|
|
910
2114
|
*/
|
|
911
|
-
|
|
2115
|
+
active;
|
|
912
2116
|
/**
|
|
913
|
-
* Settles
|
|
914
|
-
*
|
|
915
|
-
* request is needed to try again.
|
|
2117
|
+
* Settles on refusal, origin shutdown, or explicit close.
|
|
2118
|
+
* Waits through gaps in announcements for a later start, even when pinned to an identity.
|
|
916
2119
|
*/
|
|
917
2120
|
closed;
|
|
918
2121
|
#dispose;
|
|
919
2122
|
#disposed = false;
|
|
920
|
-
constructor(path, active,
|
|
2123
|
+
constructor(path, active, closed, dispose) {
|
|
921
2124
|
this.path = path;
|
|
922
2125
|
this.active = active;
|
|
923
|
-
this.unroutable = unroutable;
|
|
924
2126
|
this.closed = closed;
|
|
925
2127
|
this.#dispose = dispose;
|
|
926
2128
|
}
|
|
927
2129
|
static {
|
|
928
|
-
|
|
2130
|
+
makeFollowing = (path, active, closed, dispose) => new Following(path, active, closed, dispose);
|
|
929
2131
|
}
|
|
930
|
-
/** Withdraw the
|
|
2132
|
+
/** Withdraw the follower. The path stays routed for any other open request. Idempotent. */
|
|
931
2133
|
close() {
|
|
932
2134
|
if (this.#disposed)
|
|
933
2135
|
return;
|
|
@@ -956,12 +2158,14 @@ export class Consumer {
|
|
|
956
2158
|
this.#discovery = new Derived([state.sessions], ({ total, discovery }) => total === 0 ? undefined : discovery === total);
|
|
957
2159
|
registerWire(this, {
|
|
958
2160
|
routes: (path) => this.#routes(scope.path(path)),
|
|
959
|
-
broadcasts: scope === Scope.all ? state.local : new Derived([state.local], (local) => scope.projectPaths(local)),
|
|
960
2161
|
advertised: scope === Scope.all
|
|
961
2162
|
? state.originated
|
|
962
2163
|
: new Derived([state.originated], (routes) => scope.projectRoutes(routes)),
|
|
963
|
-
local: (path) => this.#local(scope.path(path)),
|
|
964
|
-
demand: (path) => this.#demand(scope.path(path)),
|
|
2164
|
+
local: (path, identity) => this.#local(scope.path(path), identity),
|
|
2165
|
+
demand: (path, identity, signal) => this.#demand(scope.path(path), identity, signal),
|
|
2166
|
+
covers: (prefix) => scope.covers(prefix),
|
|
2167
|
+
allowed: scope.allowed?.rebase(scope.root),
|
|
2168
|
+
root: scope.root,
|
|
965
2169
|
});
|
|
966
2170
|
}
|
|
967
2171
|
static {
|
|
@@ -993,102 +2197,267 @@ export class Consumer {
|
|
|
993
2197
|
* Availability, not a handle: {@link request} is the only way to consume by path. A
|
|
994
2198
|
* request on a routed path resolves to that route and never to a blind answer, which is
|
|
995
2199
|
* why a serving session leaves it alone.
|
|
996
|
-
*
|
|
997
|
-
* @internal
|
|
998
2200
|
*/
|
|
999
2201
|
#routes(path) {
|
|
1000
|
-
if (this.#state.local.peek()
|
|
2202
|
+
if (this.#state.local.peek().has(path))
|
|
1001
2203
|
return true;
|
|
1002
2204
|
return this.#state.bestEntry(path) !== undefined;
|
|
1003
2205
|
}
|
|
1004
2206
|
/**
|
|
1005
|
-
* Resolve `path`,
|
|
2207
|
+
* Resolve one publisher identity serving `path`, pinned to `identity` when given.
|
|
1006
2208
|
*
|
|
1007
|
-
* The
|
|
1008
|
-
*
|
|
1009
|
-
*
|
|
1010
|
-
*
|
|
1011
|
-
*
|
|
1012
|
-
*
|
|
1013
|
-
* falls back to that blind behavior only when discovery is unavailable. Close the request
|
|
1014
|
-
* when done. On a closed origin it never resolves.
|
|
2209
|
+
* The request owns discovery of its path until closed and retries route churn while
|
|
2210
|
+
* pending. It fails `Unroutable` once at least one session is attached and every
|
|
2211
|
+
* session's discovery has synced without a route for it, and can use a session without
|
|
2212
|
+
* discovery instead. With no session attached, such as between reconnects, it waits. Once resolved it keeps
|
|
2213
|
+
* that identity, ending when the identity stops serving. Use {@link follow} to move on
|
|
2214
|
+
* to the next one. Await the handle for the broadcast, or the error ending it first.
|
|
1015
2215
|
*
|
|
1016
|
-
*
|
|
1017
|
-
*
|
|
1018
|
-
*
|
|
1019
|
-
*
|
|
1020
|
-
* arrives. It matters only on an origin mixing sessions that announce with sessions that
|
|
1021
|
-
* cannot, where a path only the silent session carries may sit behind another session's
|
|
1022
|
-
* answer. Prefer {@link unroutable} and announcements over blind requests when the origin
|
|
1023
|
-
* feeds from more than one connection.
|
|
2216
|
+
* A job's exact route ({@link Request.route}) carries its own identity. On this origin, a
|
|
2217
|
+
* request waiting on the job's claim resolves onto the job once its route announces. Through
|
|
2218
|
+
* a peer's claim, the request resolves the claim's broadcast instead, which is reset once the
|
|
2219
|
+
* job's route announces: read an on-demand job across a session with {@link follow}.
|
|
1024
2220
|
*/
|
|
1025
2221
|
request(path, options = {}) {
|
|
2222
|
+
return this.#resolve(path, options.identity, false);
|
|
2223
|
+
}
|
|
2224
|
+
/**
|
|
2225
|
+
* Follow `path` across publisher identities, pinned to `identity` when given.
|
|
2226
|
+
*
|
|
2227
|
+
* `active` moves to each identity announced at the path, clears between them, and never
|
|
2228
|
+
* closes on a gap: a pinned follower waits for its identity to return. Owns discovery of
|
|
2229
|
+
* its path until closed. Requires discovery: a session without it never serves a follower.
|
|
2230
|
+
* A selected handler's refusal or the origin closing ends it.
|
|
2231
|
+
*/
|
|
2232
|
+
follow(path, options = {}) {
|
|
2233
|
+
const identity = options.identity;
|
|
2234
|
+
const absolute = this.#scope.path(path);
|
|
2235
|
+
nameable(absolute);
|
|
2236
|
+
let watch = this.#state.watchers.get(absolute);
|
|
2237
|
+
if (!watch) {
|
|
2238
|
+
watch = { users: 0, version: new Signal(0) };
|
|
2239
|
+
this.#state.watchers.set(absolute, watch);
|
|
2240
|
+
}
|
|
2241
|
+
watch.users++;
|
|
2242
|
+
const watched = watch;
|
|
2243
|
+
let unwatch = () => { };
|
|
2244
|
+
let previous = this.#state.identity(absolute);
|
|
2245
|
+
const active = new Signal(undefined);
|
|
2246
|
+
const closed = new Once();
|
|
2247
|
+
let current;
|
|
2248
|
+
let subscriptions = [];
|
|
2249
|
+
let disposed = false;
|
|
2250
|
+
const release = () => {
|
|
2251
|
+
for (const stop of subscriptions)
|
|
2252
|
+
stop();
|
|
2253
|
+
subscriptions = [];
|
|
2254
|
+
current?.close();
|
|
2255
|
+
current = undefined;
|
|
2256
|
+
};
|
|
2257
|
+
const finish = (error) => {
|
|
2258
|
+
if (disposed)
|
|
2259
|
+
return;
|
|
2260
|
+
disposed = true;
|
|
2261
|
+
unwatch();
|
|
2262
|
+
if (--watched.users === 0)
|
|
2263
|
+
this.#state.watchers.delete(absolute);
|
|
2264
|
+
release();
|
|
2265
|
+
active.set(undefined);
|
|
2266
|
+
closed.set(error);
|
|
2267
|
+
};
|
|
2268
|
+
const ended = (error, request = current) => {
|
|
2269
|
+
if (!error)
|
|
2270
|
+
return;
|
|
2271
|
+
// Provenance survives a coalesced end/start, even when both routes serve one identity.
|
|
2272
|
+
if (error instanceof StreamError &&
|
|
2273
|
+
error.code === StreamCode.Unroutable &&
|
|
2274
|
+
request &&
|
|
2275
|
+
!refusedRequests.has(request.closed)) {
|
|
2276
|
+
active.set(undefined);
|
|
2277
|
+
return;
|
|
2278
|
+
}
|
|
2279
|
+
finish(error);
|
|
2280
|
+
};
|
|
2281
|
+
const replace = (next) => {
|
|
2282
|
+
if (disposed) {
|
|
2283
|
+
next?.close();
|
|
2284
|
+
return;
|
|
2285
|
+
}
|
|
2286
|
+
const value = next?.active.peek();
|
|
2287
|
+
if (next &&
|
|
2288
|
+
current?.closed.peek() === undefined &&
|
|
2289
|
+
value &&
|
|
2290
|
+
value.closed === current?.active.peek()?.closed &&
|
|
2291
|
+
value.identity === current.active.peek()?.identity) {
|
|
2292
|
+
next.close();
|
|
2293
|
+
return;
|
|
2294
|
+
}
|
|
2295
|
+
release();
|
|
2296
|
+
current = next;
|
|
2297
|
+
active.set(value);
|
|
2298
|
+
if (!next)
|
|
2299
|
+
return;
|
|
2300
|
+
subscriptions.push(next.active.subscribe((value) => {
|
|
2301
|
+
active.set(value);
|
|
2302
|
+
}));
|
|
2303
|
+
subscriptions.push(next.closed.subscribe((error) => ended(error, next)));
|
|
2304
|
+
ended(next.closed.peek(), next);
|
|
2305
|
+
};
|
|
2306
|
+
try {
|
|
2307
|
+
replace(this.#resolve(path, identity, true));
|
|
2308
|
+
}
|
|
2309
|
+
catch (error) {
|
|
2310
|
+
finish(error instanceof Error ? error : new Error(String(error)));
|
|
2311
|
+
throw error;
|
|
2312
|
+
}
|
|
2313
|
+
const changed = () => {
|
|
2314
|
+
const shutdown = this.#state.closed.peek();
|
|
2315
|
+
if (shutdown !== undefined) {
|
|
2316
|
+
finish(shutdown);
|
|
2317
|
+
return;
|
|
2318
|
+
}
|
|
2319
|
+
// A refusal landing in the same turn as this wakeup is final; never retry past it.
|
|
2320
|
+
ended(current?.closed.peek());
|
|
2321
|
+
if (disposed)
|
|
2322
|
+
return;
|
|
2323
|
+
const next = this.#state.identity(absolute);
|
|
2324
|
+
const had = previous !== undefined;
|
|
2325
|
+
const same = previous && next && sameIdentity(previous, next);
|
|
2326
|
+
previous = next;
|
|
2327
|
+
if (!next) {
|
|
2328
|
+
if (had)
|
|
2329
|
+
replace(this.#resolve(path, identity, true));
|
|
2330
|
+
}
|
|
2331
|
+
else if (!same || !current || current.closed.peek() !== undefined)
|
|
2332
|
+
replace(this.#resolve(path, identity, true));
|
|
2333
|
+
};
|
|
2334
|
+
if (!disposed) {
|
|
2335
|
+
unwatch = watched.version.subscribe(changed);
|
|
2336
|
+
changed();
|
|
2337
|
+
}
|
|
2338
|
+
return makeFollowing(path, active, closed, () => finish(null));
|
|
2339
|
+
}
|
|
2340
|
+
// One resolution of `path`. A follower's (`announced`) takes only an announced front and
|
|
2341
|
+
// never reports absence: {@link follow} retries it on the next identity instead.
|
|
2342
|
+
#resolve(path, identity, announced) {
|
|
1026
2343
|
const relative = path;
|
|
1027
2344
|
path = this.#scope.path(path);
|
|
2345
|
+
nameable(path);
|
|
1028
2346
|
const requests = this.#state.requests.peek();
|
|
1029
|
-
if (
|
|
1030
|
-
// Closed origin: a request that can never resolve
|
|
2347
|
+
if (this.#state.closed.peek() !== undefined) {
|
|
2348
|
+
// Closed origin: a request that can never resolve ends at once.
|
|
1031
2349
|
const closed = new Once();
|
|
1032
|
-
|
|
2350
|
+
closed.set(this.#state.closed.peek() ?? null);
|
|
2351
|
+
return makeRequesting(relative, new Signal(undefined), closed, () => { });
|
|
2352
|
+
}
|
|
2353
|
+
// A pin naming another identity than the winner follows that identity's own routes, on a
|
|
2354
|
+
// slot shared only by requests pinned to it, rather than filter the winner afterward.
|
|
2355
|
+
const pin = identity !== undefined && this.#state.identity(path)?.route.identity !== identity ? identity : undefined;
|
|
2356
|
+
let fresh;
|
|
2357
|
+
// A claim refusing the pin hands a new handle no answer it held from before.
|
|
2358
|
+
const refused = pin !== undefined && this.#state.refuses(path, pin);
|
|
2359
|
+
let slot = pin === undefined
|
|
2360
|
+
? requests.get(path)
|
|
2361
|
+
: [...(this.#state.detached.get(path) ?? [])].find((other) => other.pin === pin && !refused && !this.#state.retained.has(other) && !outdated(other));
|
|
2362
|
+
if (!slot && pin !== undefined) {
|
|
2363
|
+
const created = {
|
|
2364
|
+
blind: 0,
|
|
2365
|
+
handles: new Set(),
|
|
2366
|
+
absent: new Set(),
|
|
2367
|
+
moved: new Set(),
|
|
2368
|
+
pinned: new Set(),
|
|
2369
|
+
route: new Signal(undefined),
|
|
2370
|
+
pin,
|
|
2371
|
+
};
|
|
2372
|
+
slot = created;
|
|
2373
|
+
let detached = this.#state.detached.get(path);
|
|
2374
|
+
if (!detached) {
|
|
2375
|
+
detached = new Set();
|
|
2376
|
+
this.#state.detached.set(path, detached);
|
|
2377
|
+
}
|
|
2378
|
+
detached.add(created);
|
|
2379
|
+
// Followed once this handle is on it, below, since following can refuse it at once.
|
|
2380
|
+
fresh = created;
|
|
1033
2381
|
}
|
|
1034
|
-
|
|
1035
|
-
if (!slot) {
|
|
2382
|
+
else if (!slot) {
|
|
1036
2383
|
// Seeded through the constructor, so a path the table already routes resolves on the
|
|
1037
2384
|
// first read. It must not go through a silent set: that still captures the pre-seed
|
|
1038
2385
|
// value as the baseline the next change is compared against, and never flushes to
|
|
1039
2386
|
// clear it, so a seeded route retracting to undefined would look like no change and
|
|
1040
2387
|
// notify nobody.
|
|
1041
|
-
const refused = new Set();
|
|
1042
2388
|
const created = {
|
|
1043
2389
|
blind: 0,
|
|
1044
2390
|
handles: new Set(),
|
|
1045
|
-
|
|
1046
|
-
|
|
2391
|
+
absent: new Set(),
|
|
2392
|
+
moved: new Set(),
|
|
2393
|
+
pinned: new Set(),
|
|
2394
|
+
route: new Signal(this.#state.route(path, {})),
|
|
1047
2395
|
};
|
|
1048
2396
|
slot = created;
|
|
1049
2397
|
this.#state.requests.mutate((map) => {
|
|
1050
|
-
map
|
|
2398
|
+
map.set(path, created);
|
|
1051
2399
|
});
|
|
1052
2400
|
}
|
|
1053
2401
|
const closed = new Once();
|
|
1054
2402
|
slot.handles.add(closed);
|
|
1055
|
-
|
|
1056
|
-
if (
|
|
2403
|
+
// Counted, not subscribed: serving sessions decide blindness from the live discovery state.
|
|
2404
|
+
if (!announced)
|
|
1057
2405
|
slot.blind += 1;
|
|
2406
|
+
// Resolution owns discovery from the first ask, not just after a route arrives.
|
|
2407
|
+
const unwatch = this.#state.watch([path]);
|
|
1058
2408
|
this.#state.requests.mutate(() => { });
|
|
1059
|
-
// An announcement-gated request falls back to a blind subscription only while at
|
|
1060
|
-
// least one attached session cannot announce. It returns to the gate if discovery
|
|
1061
|
-
// becomes complete again, and remains gated with no session attached.
|
|
1062
|
-
const unsubscribeDiscovery = options.announced
|
|
1063
|
-
? this.#discovery.subscribe((discovery) => {
|
|
1064
|
-
const next = discovery === false;
|
|
1065
|
-
if (next === blind)
|
|
1066
|
-
return;
|
|
1067
|
-
blind = next;
|
|
1068
|
-
slot.blind += next ? 1 : -1;
|
|
1069
|
-
this.#state.requests.mutate(() => { });
|
|
1070
|
-
})
|
|
1071
|
-
: () => { };
|
|
1072
2409
|
// Hand out a handle of the request's own rather than the table's. Closing a consumer
|
|
1073
2410
|
// closes the broadcast once it was the last one, and the table often holds the only
|
|
1074
2411
|
// other handle, so lending its front out means an ordinary close() by one requester
|
|
1075
2412
|
// can unpublish the path for everybody else.
|
|
1076
2413
|
const taken = slot;
|
|
1077
|
-
// Memoized on the route's
|
|
2414
|
+
// Memoized on the route's ref: the same front resolving again returns the handle
|
|
1078
2415
|
// we already made, and only a real swap clones a new one (cloning before closing the
|
|
1079
2416
|
// old, so a broadcast that both routes share never briefly loses its last handle).
|
|
2417
|
+
// Under a carried identity the handle is a logical front instead: another route of it
|
|
2418
|
+
// serves the same stream, so the front re-sources its tracks and the handle stays.
|
|
1080
2419
|
let released = false;
|
|
1081
2420
|
let source;
|
|
1082
2421
|
let handle;
|
|
1083
|
-
|
|
2422
|
+
let logical;
|
|
2423
|
+
// Whether another identity than the one the handle reads wins the path, judged over every
|
|
2424
|
+
// route the origin holds: a claim's refusal is then final. Mirrors the Rust `superseded`.
|
|
2425
|
+
const superseded = () => {
|
|
2426
|
+
const winner = this.#state.identity(path)?.route.identity;
|
|
2427
|
+
return winner !== undefined && winner !== source?.identity;
|
|
2428
|
+
};
|
|
2429
|
+
const own = (resolution) => {
|
|
1084
2430
|
if (released)
|
|
1085
2431
|
return undefined;
|
|
1086
|
-
|
|
2432
|
+
// A follower takes only a front something announced; a blind answer is for plain handles.
|
|
2433
|
+
const offered = announced && !resolution?.source ? undefined : resolution?.front;
|
|
2434
|
+
const front = identity === undefined || resolution?.identity === identity ? offered : undefined;
|
|
2435
|
+
if (logical && source?.identity !== undefined && resolution?.identity === source.identity) {
|
|
2436
|
+
// Between routes of the identity, readers wait for the next one to answer.
|
|
2437
|
+
if (front)
|
|
2438
|
+
logical.serve(front, resolution?.local, resolution?.claim);
|
|
2439
|
+
else
|
|
2440
|
+
logical.park();
|
|
2441
|
+
return handle;
|
|
2442
|
+
}
|
|
2443
|
+
if (front !== source?.front || resolution?.identity !== source?.identity) {
|
|
1087
2444
|
const previous = handle;
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
2445
|
+
const concluded = logical;
|
|
2446
|
+
source = front ? { front, identity: resolution?.identity } : undefined;
|
|
2447
|
+
// A minted identity names an announcement over a wire that carries none, whose
|
|
2448
|
+
// route may answer a re-request with another stream, so nothing re-sources it.
|
|
2449
|
+
const carried = resolution?.identity !== undefined && !Identity.isMinted(resolution.identity);
|
|
2450
|
+
logical = front && carried ? new Front(superseded) : undefined;
|
|
2451
|
+
logical?.serve(front, resolution?.local, resolution?.claim);
|
|
2452
|
+
handle = logical ? logical.consumer : front?.clone();
|
|
2453
|
+
if (handle) {
|
|
1091
2454
|
hooks.stampPath(handle, relative);
|
|
2455
|
+
hooks.stampIdentity(handle, resolution?.identity);
|
|
2456
|
+
// A logical front lists what the route it was made through lists.
|
|
2457
|
+
if (front && hooks.unlisted(front))
|
|
2458
|
+
hooks.stampUnlisted(handle);
|
|
2459
|
+
}
|
|
2460
|
+
concluded?.conclude();
|
|
1092
2461
|
previous?.close();
|
|
1093
2462
|
}
|
|
1094
2463
|
return handle;
|
|
@@ -1099,23 +2468,79 @@ export class Consumer {
|
|
|
1099
2468
|
// holder that only ever peeked would then pin a route that has already been retracted
|
|
1100
2469
|
// until it happened to read again. Following the route as well retires it promptly,
|
|
1101
2470
|
// and the memo makes the two paths agree: whichever runs first does the swap.
|
|
1102
|
-
|
|
1103
|
-
//
|
|
1104
|
-
// the
|
|
1105
|
-
//
|
|
1106
|
-
const
|
|
1107
|
-
|
|
2471
|
+
// A pending plain request reports absence, as Rust's request does, once discovery of
|
|
2472
|
+
// its path has synced without a route for its identity. Sessions hold that sync from
|
|
2473
|
+
// when the watch opens, so a fresh request waits for its peer's answer. A follower
|
|
2474
|
+
// keeps waiting for a start, and a resolved request ends only with its identity.
|
|
2475
|
+
const absent = () => {
|
|
2476
|
+
if (released || closed.peek() !== undefined)
|
|
2477
|
+
return;
|
|
2478
|
+
if (source) {
|
|
2479
|
+
taken.absent.delete(absent);
|
|
2480
|
+
return;
|
|
2481
|
+
}
|
|
2482
|
+
const resolution = route.peek();
|
|
2483
|
+
if (resolution && (identity === undefined || resolution.identity === identity))
|
|
2484
|
+
return;
|
|
2485
|
+
if (!this.#state.synced(path))
|
|
2486
|
+
return;
|
|
2487
|
+
closed.set(unroutable());
|
|
2488
|
+
release();
|
|
2489
|
+
};
|
|
2490
|
+
if (!announced)
|
|
2491
|
+
taken.absent.add(absent);
|
|
2492
|
+
// The table moved while the same route may still win: a read stranded on it asks again.
|
|
2493
|
+
// Queued behind the route's own flush, so a route that changed is served first, and
|
|
2494
|
+
// checked again then: a takeover gone pending meanwhile is no news.
|
|
2495
|
+
const moved = () => {
|
|
2496
|
+
const at = route.peek()?.source;
|
|
2497
|
+
queueMicrotask(() => {
|
|
2498
|
+
const now = route.peek();
|
|
2499
|
+
if (released || now?.source !== at || now?.pending || taken.failover)
|
|
2500
|
+
return;
|
|
2501
|
+
logical?.moved();
|
|
2502
|
+
});
|
|
2503
|
+
};
|
|
2504
|
+
taken.moved.add(moved);
|
|
2505
|
+
const pinned = identity !== undefined && taken.pin === undefined
|
|
2506
|
+
? {
|
|
2507
|
+
identity,
|
|
2508
|
+
refuse: (err) => {
|
|
2509
|
+
if (released || closed.peek() !== undefined)
|
|
2510
|
+
return;
|
|
2511
|
+
refusedRequests.add(closed);
|
|
2512
|
+
closed.set(err);
|
|
2513
|
+
release();
|
|
2514
|
+
},
|
|
2515
|
+
}
|
|
2516
|
+
: undefined;
|
|
2517
|
+
if (pinned)
|
|
2518
|
+
taken.pinned.add(pinned);
|
|
2519
|
+
const unsubscribe = route.subscribe((resolution) => {
|
|
2520
|
+
own(resolution);
|
|
2521
|
+
if (!announced)
|
|
2522
|
+
absent();
|
|
2523
|
+
});
|
|
2524
|
+
const release = () => {
|
|
1108
2525
|
// Releases this request's handle; the route itself belongs to the table.
|
|
2526
|
+
if (released)
|
|
2527
|
+
return;
|
|
1109
2528
|
released = true;
|
|
1110
|
-
unsubscribeDiscovery();
|
|
1111
2529
|
unsubscribe();
|
|
2530
|
+
taken.absent.delete(absent);
|
|
2531
|
+
taken.moved.delete(moved);
|
|
2532
|
+
if (pinned)
|
|
2533
|
+
taken.pinned.delete(pinned);
|
|
2534
|
+
logical?.conclude();
|
|
2535
|
+
logical = undefined;
|
|
1112
2536
|
handle?.close();
|
|
1113
2537
|
handle = undefined;
|
|
1114
2538
|
source = undefined;
|
|
2539
|
+
unwatch();
|
|
1115
2540
|
taken.handles.delete(closed);
|
|
1116
2541
|
if (closed.peek() === undefined)
|
|
1117
2542
|
closed.set(null);
|
|
1118
|
-
if (
|
|
2543
|
+
if (!announced)
|
|
1119
2544
|
taken.blind -= 1;
|
|
1120
2545
|
this.#state.requests.mutate(() => { });
|
|
1121
2546
|
if (taken.handles.size > 0)
|
|
@@ -1126,123 +2551,259 @@ export class Consumer {
|
|
|
1126
2551
|
queueMicrotask(() => {
|
|
1127
2552
|
if (taken.handles.size > 0)
|
|
1128
2553
|
return;
|
|
1129
|
-
// A refused slot already tore itself down, and the path may hold a newer one.
|
|
1130
|
-
|
|
2554
|
+
// A refused slot already tore itself down, and the path may hold a newer one. A
|
|
2555
|
+
// detached slot owns the front it took.
|
|
2556
|
+
if (this.#state.requests.peek().get(path) !== taken) {
|
|
2557
|
+
this.#state.detach(path, taken);
|
|
2558
|
+
this.#state.forget(path);
|
|
1131
2559
|
return;
|
|
2560
|
+
}
|
|
1132
2561
|
this.#state.requests.mutate((map) => {
|
|
1133
|
-
map
|
|
2562
|
+
map.delete(path);
|
|
1134
2563
|
});
|
|
1135
2564
|
taken.answer?.close();
|
|
1136
2565
|
taken.answer = undefined;
|
|
1137
2566
|
taken.route.set(undefined);
|
|
1138
2567
|
this.#state.releaseMaterialized(path);
|
|
2568
|
+
this.#state.forget(path);
|
|
1139
2569
|
});
|
|
1140
|
-
}
|
|
2570
|
+
};
|
|
2571
|
+
if (fresh)
|
|
2572
|
+
this.#state.follow(path, fresh, identity, undefined);
|
|
2573
|
+
if (!announced)
|
|
2574
|
+
absent();
|
|
2575
|
+
return makeRequesting(relative, active, closed, release);
|
|
1141
2576
|
}
|
|
1142
2577
|
/**
|
|
1143
|
-
* The announced
|
|
1144
|
-
*
|
|
1145
|
-
*
|
|
1146
|
-
*
|
|
1147
|
-
*
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
for (const [path, entry] of this.#listed(patterns, hidden))
|
|
1157
|
-
routes.set(path, entry.route);
|
|
1158
|
-
return routes;
|
|
1159
|
-
});
|
|
2578
|
+
* The paths announced at or beneath `prefix`, relative to the root, within this handle's
|
|
2579
|
+
* scope, each reported as it becomes present or absent: every currently advertised one
|
|
2580
|
+
* arrives first as `present`. Another identity taking a path over keeps it present;
|
|
2581
|
+
* {@link follow} it to see the broadcast change. A local broadcast appears once it announces,
|
|
2582
|
+
* exactly as a peer sees it. Only routes at or beneath the prefix are listed, unless
|
|
2583
|
+
* `options.covering` also asks for every route whose subtree overlaps it, a broadcast
|
|
2584
|
+
* included. The stream ends when the origin closes or the consumer is closed. Hidden routes
|
|
2585
|
+
* are left out unless `options.hidden` opts in (see {@link Announce.Options}). While open, it
|
|
2586
|
+
* watches the prefix: a `consume` session asks its peer for announcements there. Throws
|
|
2587
|
+
* {@link Path.InvalidPrefix} for a prefix holding a `*`.
|
|
2588
|
+
*/
|
|
2589
|
+
announced(prefix = Path.empty(), options) {
|
|
2590
|
+
return announce.list(this.routes(prefix, options));
|
|
1160
2591
|
}
|
|
1161
2592
|
/**
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1164
|
-
*
|
|
1165
|
-
*
|
|
1166
|
-
*
|
|
1167
|
-
* closed. Hidden routes are left out unless `options.hidden` opts in (see {@link announce.Options}).
|
|
2593
|
+
* Every change to the routes at or beneath `prefix`, for code that forwards routes: a
|
|
2594
|
+
* re-price is an `update`, and another identity serving a path a `restart`. The prefix,
|
|
2595
|
+
* options and the watch are as for {@link announced}.
|
|
2596
|
+
*
|
|
2597
|
+
* @internal
|
|
1168
2598
|
*/
|
|
1169
|
-
|
|
2599
|
+
routes(prefix = Path.empty(), options) {
|
|
1170
2600
|
const producer = new announce.Producer();
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
2601
|
+
const reach = options?.exact ? "above" : options?.covering ? "both" : "beneath";
|
|
2602
|
+
let listed = this.#scope.listed(prefix);
|
|
2603
|
+
// An exact listing asks what could serve the path itself, which a scope reaching only
|
|
2604
|
+
// beneath it never lets this handle request: nothing to list or wait on.
|
|
2605
|
+
if (reach === "above" && !this.#scope.covers(prefix))
|
|
2606
|
+
listed = new Path.Prefixes();
|
|
2607
|
+
void producer.closed.then(this.#state.watch(listed.toArray().map((member) => member.text)));
|
|
2608
|
+
void this.#runAnnounced(producer, listed, options?.hidden ?? false, reach);
|
|
2609
|
+
const consumer = producer.consume();
|
|
2610
|
+
// The last reader leaving ends the stream and its watch.
|
|
2611
|
+
closeUnwanted(producer);
|
|
2612
|
+
return consumer;
|
|
2613
|
+
}
|
|
2614
|
+
/** What one announcement listing presents of the table, by path or in full. */
|
|
2615
|
+
#view(listed, hidden, reach) {
|
|
2616
|
+
const root = this.#scope.root;
|
|
2617
|
+
// The members are disjoint, so at most one lies at or above a path: found among the path's
|
|
2618
|
+
// own prefixes. Those beneath a path are a contiguous run of the sorted texts.
|
|
2619
|
+
const heads = listed.toArray().map((member) => member.text);
|
|
2620
|
+
const named = new Set(heads);
|
|
2621
|
+
const sorted = [...heads].sort();
|
|
2622
|
+
const covering = (path) => {
|
|
2623
|
+
for (const prefix of coveringPrefixes(path))
|
|
2624
|
+
if (named.has(prefix))
|
|
2625
|
+
return prefix;
|
|
2626
|
+
return undefined;
|
|
2627
|
+
};
|
|
2628
|
+
const beneath = (path) => {
|
|
2629
|
+
if (path === Path.empty() || named.has(path))
|
|
2630
|
+
return sorted.length > 0;
|
|
2631
|
+
const below = `${path}/`;
|
|
2632
|
+
let lo = 0;
|
|
2633
|
+
let hi = sorted.length;
|
|
2634
|
+
while (lo < hi) {
|
|
2635
|
+
const mid = (lo + hi) >>> 1;
|
|
2636
|
+
if (sorted[mid] < below)
|
|
2637
|
+
lo = mid + 1;
|
|
2638
|
+
else
|
|
2639
|
+
hi = mid;
|
|
1192
2640
|
}
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
2641
|
+
return sorted[lo]?.startsWith(below) ?? false;
|
|
2642
|
+
};
|
|
2643
|
+
// Every candidate is a claim on the subtree it is filed at, a broadcast included, so a
|
|
2644
|
+
// covering reader sees each one whose subtree overlaps its scope, as Rust's cursor does. A
|
|
2645
|
+
// segment hidden beneath the member the path falls under hides it; a member beneath the path
|
|
2646
|
+
// sees it whole.
|
|
2647
|
+
const seen = (path) => {
|
|
2648
|
+
if (reach !== "beneath" && beneath(path))
|
|
2649
|
+
return true;
|
|
2650
|
+
if (reach === "above")
|
|
2651
|
+
return false;
|
|
2652
|
+
const head = covering(path);
|
|
2653
|
+
return head !== undefined && (hidden || !hiddenBelow(head, path));
|
|
2654
|
+
};
|
|
2655
|
+
const present = (entry) => ({
|
|
2656
|
+
ref: entry.ref,
|
|
2657
|
+
route: entry.route,
|
|
2658
|
+
lost: entry.lost,
|
|
2659
|
+
});
|
|
2660
|
+
// Every prefix at or above the root presents as the empty path, where the claims covering the
|
|
2661
|
+
// root compete as one tier, as a request beneath the root resolves.
|
|
2662
|
+
const file = (atRoot, path, candidates) => {
|
|
2663
|
+
const depth = Path.parts(path).length;
|
|
2664
|
+
candidates.forEach((candidate, order) => {
|
|
2665
|
+
atRoot.push({
|
|
2666
|
+
prefix: path,
|
|
2667
|
+
depth,
|
|
2668
|
+
order,
|
|
2669
|
+
arrival: arrival(candidate.ref),
|
|
2670
|
+
candidate,
|
|
2671
|
+
route: candidate.route,
|
|
2672
|
+
});
|
|
1202
2673
|
});
|
|
1203
|
-
}
|
|
1204
|
-
|
|
2674
|
+
};
|
|
2675
|
+
// Hashed on the root, as a request for it resolves. A broadcast above the root serves only
|
|
2676
|
+
// its own path, so a tier-mate that serves the root ranks ahead of it, as `presented` ranks them.
|
|
2677
|
+
const best = (atRoot) => {
|
|
2678
|
+
const best = bestOfTier(root, atRoot, {
|
|
2679
|
+
serves: (entry) => !entry.candidate.exact || entry.prefix === root,
|
|
2680
|
+
});
|
|
2681
|
+
return best && present(best.candidate);
|
|
2682
|
+
};
|
|
2683
|
+
// Where an absolute path presents: beneath the root by its relative path, at or above it as
|
|
2684
|
+
// the empty path, and anywhere else not at all.
|
|
2685
|
+
const key = (path) => {
|
|
2686
|
+
const relative = Path.stripPrefix(root, path);
|
|
2687
|
+
if (relative !== null)
|
|
2688
|
+
return relative;
|
|
2689
|
+
return Path.hasPrefix(path, root) ? Path.empty() : undefined;
|
|
2690
|
+
};
|
|
2691
|
+
return {
|
|
2692
|
+
key,
|
|
2693
|
+
all: () => {
|
|
2694
|
+
const next = new Map();
|
|
2695
|
+
const atRoot = [];
|
|
2696
|
+
for (const [path, candidates] of this.#state.snapshot()) {
|
|
2697
|
+
if (!seen(path))
|
|
2698
|
+
continue;
|
|
2699
|
+
const relative = key(path);
|
|
2700
|
+
if (relative === undefined)
|
|
2701
|
+
continue;
|
|
2702
|
+
if (relative === Path.empty()) {
|
|
2703
|
+
// Hold the empty path's place in the order the snapshot lists its first claim.
|
|
2704
|
+
if (atRoot.length === 0)
|
|
2705
|
+
next.set(Path.empty(), present(candidates[0]));
|
|
2706
|
+
file(atRoot, path, candidates);
|
|
2707
|
+
continue;
|
|
2708
|
+
}
|
|
2709
|
+
// The preferred candidate wins, unless a higher epoch of its identity supersedes it.
|
|
2710
|
+
const entry = firstEligible(candidates, () => true);
|
|
2711
|
+
if (entry)
|
|
2712
|
+
next.set(relative, present(entry));
|
|
2713
|
+
}
|
|
2714
|
+
const root = best(atRoot);
|
|
2715
|
+
if (root)
|
|
2716
|
+
next.set(Path.empty(), root);
|
|
2717
|
+
return next;
|
|
2718
|
+
},
|
|
2719
|
+
at: (path) => {
|
|
2720
|
+
const snapshot = this.#state.snapshot();
|
|
2721
|
+
if (key(path) !== Path.empty()) {
|
|
2722
|
+
const candidates = snapshot.get(path);
|
|
2723
|
+
const entry = candidates && seen(path) ? firstEligible(candidates, () => true) : undefined;
|
|
2724
|
+
return entry && present(entry);
|
|
2725
|
+
}
|
|
2726
|
+
const atRoot = [];
|
|
2727
|
+
for (const prefix of coveringPrefixes(root)) {
|
|
2728
|
+
const candidates = snapshot.get(prefix);
|
|
2729
|
+
if (candidates && seen(prefix))
|
|
2730
|
+
file(atRoot, prefix, candidates);
|
|
2731
|
+
}
|
|
2732
|
+
return best(atRoot);
|
|
2733
|
+
},
|
|
2734
|
+
};
|
|
1205
2735
|
}
|
|
1206
|
-
async #runAnnounced(producer,
|
|
1207
|
-
|
|
1208
|
-
//
|
|
1209
|
-
|
|
1210
|
-
//
|
|
1211
|
-
|
|
2736
|
+
async #runAnnounced(producer, listed, hidden, reach) {
|
|
2737
|
+
const view = this.#view(listed, hidden, reach);
|
|
2738
|
+
// The paths written since the last wake: only those can change what this listing presents.
|
|
2739
|
+
const [pending, unlisten] = this.#state.listen();
|
|
2740
|
+
// Keyed by the presented path (from the origin, not the scope), valued by ref
|
|
2741
|
+
// plus route. Diffing the identity rather than mere presence means a republish emits a
|
|
2742
|
+
// restart; a re-price of the same identity emits an update.
|
|
2743
|
+
let active;
|
|
1212
2744
|
try {
|
|
1213
2745
|
for (;;) {
|
|
1214
|
-
|
|
1215
|
-
const advertisedLocal = this.#state.advertisedLocal.peek();
|
|
1216
|
-
const routes = this.#state.routes.peek();
|
|
1217
|
-
if (local === undefined && advertisedLocal === undefined && routes === undefined)
|
|
2746
|
+
if (this.#state.closed.peek() !== undefined)
|
|
1218
2747
|
break;
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
2748
|
+
// The whole table once, then each changed path as presented now, or undefined once gone.
|
|
2749
|
+
let next;
|
|
2750
|
+
if (active === undefined) {
|
|
2751
|
+
active = new Map();
|
|
2752
|
+
next = view.all();
|
|
2753
|
+
}
|
|
2754
|
+
else {
|
|
2755
|
+
next = new Map();
|
|
2756
|
+
for (const path of pending) {
|
|
2757
|
+
const relative = view.key(path);
|
|
2758
|
+
if (relative !== undefined && !next.has(relative))
|
|
2759
|
+
next.set(relative, view.at(path));
|
|
2760
|
+
}
|
|
1229
2761
|
}
|
|
2762
|
+
pending.clear();
|
|
1230
2763
|
for (const [path, snap] of next) {
|
|
1231
2764
|
const prev = active.get(path);
|
|
1232
|
-
if (
|
|
2765
|
+
if (prev && !snap)
|
|
1233
2766
|
producer.append({
|
|
1234
2767
|
prefix: path,
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
route: snap.route,
|
|
2768
|
+
kind: prev.lost?.() ? "offline" : "end",
|
|
2769
|
+
route: prev.route,
|
|
1238
2770
|
});
|
|
2771
|
+
}
|
|
2772
|
+
for (const [path, snap] of next) {
|
|
2773
|
+
const prev = active.get(path);
|
|
2774
|
+
if (!snap) {
|
|
2775
|
+
active.delete(path);
|
|
2776
|
+
continue;
|
|
2777
|
+
}
|
|
2778
|
+
if (!prev) {
|
|
2779
|
+
producer.append({ prefix: path, kind: "start", route: snap.route });
|
|
2780
|
+
}
|
|
2781
|
+
else if (!sameIdentity(prev, snap)) {
|
|
2782
|
+
producer.append({ prefix: path, kind: "restart", route: snap.route });
|
|
1239
2783
|
}
|
|
1240
2784
|
else if (!routesEqual(prev.route, snap.route)) {
|
|
1241
|
-
producer.append({ prefix: path,
|
|
2785
|
+
producer.append({ prefix: path, kind: "update", route: snap.route });
|
|
1242
2786
|
}
|
|
2787
|
+
active.set(path, snap);
|
|
1243
2788
|
}
|
|
1244
|
-
|
|
1245
|
-
|
|
2789
|
+
// Synced once no session is still delivering an initial set this stream could
|
|
2790
|
+
// list: one under a hidden segment holds only a reader that opts in.
|
|
2791
|
+
let behind = false;
|
|
2792
|
+
for (const prefix of this.#state.snapshots.peek().keys()) {
|
|
2793
|
+
for (const member of listed) {
|
|
2794
|
+
if (reach === "above"
|
|
2795
|
+
? !Path.hasPrefix(prefix, member.text)
|
|
2796
|
+
: !member.overlaps(prefix))
|
|
2797
|
+
continue;
|
|
2798
|
+
if (hidden || !hiddenBelow(member.text, prefix))
|
|
2799
|
+
behind = true;
|
|
2800
|
+
}
|
|
2801
|
+
}
|
|
2802
|
+
if (behind)
|
|
2803
|
+
producer.behind();
|
|
2804
|
+
else
|
|
2805
|
+
producer.synced();
|
|
2806
|
+
await Signal.race(this.#state.local, this.#state.advertisedLocal, this.#state.routes, this.#state.snapshots, producer.closed);
|
|
1246
2807
|
if (producer.closed.peek() !== undefined)
|
|
1247
2808
|
return;
|
|
1248
2809
|
}
|
|
@@ -1250,161 +2811,377 @@ export class Consumer {
|
|
|
1250
2811
|
catch {
|
|
1251
2812
|
// The reader closed between the check and an append; nothing left to do.
|
|
1252
2813
|
}
|
|
2814
|
+
finally {
|
|
2815
|
+
unlisten();
|
|
2816
|
+
}
|
|
1253
2817
|
producer.close();
|
|
1254
2818
|
}
|
|
1255
|
-
/**
|
|
1256
|
-
* The local table, borrowed by the wire publishers to answer subscribes.
|
|
1257
|
-
*
|
|
1258
|
-
* Deliberately excludes received routes: a session never re-announces what a peer
|
|
1259
|
-
* told it, so an origin wired to both directions of a connection cannot echo.
|
|
1260
|
-
* Borrowed, not owned: do not close the fronts. Undefined once the origin closes.
|
|
1261
|
-
*
|
|
1262
|
-
* @internal
|
|
1263
|
-
*/
|
|
1264
|
-
/**
|
|
1265
|
-
* Originated advertisements a session should forward: exact-path announces plus
|
|
1266
|
-
* originated dynamics. Undefined once the origin closes.
|
|
1267
|
-
*
|
|
1268
|
-
* @internal
|
|
1269
|
-
*/
|
|
1270
2819
|
/**
|
|
1271
2820
|
* The announced local broadcast at `path`, when it beats the originated routes there.
|
|
1272
2821
|
* Resolves through what rebuildOriginated advertised: a peer never sees received routes.
|
|
1273
2822
|
*/
|
|
1274
|
-
#local(path) {
|
|
1275
|
-
const local = this.#state.local.peek()
|
|
1276
|
-
if (local
|
|
1277
|
-
return
|
|
1278
|
-
|
|
2823
|
+
#local(path, identity) {
|
|
2824
|
+
const local = this.#state.local.peek().get(path);
|
|
2825
|
+
if (!local)
|
|
2826
|
+
return undefined;
|
|
2827
|
+
if (identity !== undefined && this.#state.advertisedLocal.peek().get(path)?.identity !== identity)
|
|
2828
|
+
return undefined;
|
|
2829
|
+
// Pinned, it competes only with its own identity's routes, whichever identity wins.
|
|
2830
|
+
const skip = unservable(identity);
|
|
2831
|
+
if (!this.#state.localWins(path, this.#state.bestEntry(path, skip), skip))
|
|
2832
|
+
return undefined;
|
|
2833
|
+
return local;
|
|
1279
2834
|
}
|
|
1280
2835
|
/**
|
|
1281
2836
|
* Resolve `path` for serving: an announced local broadcast, or wait for an originated
|
|
1282
2837
|
* dynamic to accept it. Undefined when nothing here can serve the path.
|
|
1283
|
-
*
|
|
1284
|
-
* @internal
|
|
1285
2838
|
*/
|
|
1286
|
-
async #demand(path) {
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
if (!entry?.server)
|
|
2839
|
+
async #demand(path, identity, signal) {
|
|
2840
|
+
signal?.throwIfAborted();
|
|
2841
|
+
// Nothing can serve a path holding `*` (see `nameable`), so a peer's request for one is
|
|
2842
|
+
// refused here rather than handed to a claim whose answer would throw.
|
|
2843
|
+
if (path.includes("*"))
|
|
1292
2844
|
return undefined;
|
|
1293
|
-
const
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
2845
|
+
const cancelled = new Once();
|
|
2846
|
+
let release;
|
|
2847
|
+
const abort = () => {
|
|
2848
|
+
// Withdraw before a handler can answer in the same turn as cancellation.
|
|
2849
|
+
release?.();
|
|
2850
|
+
cancelled.set(true);
|
|
2851
|
+
};
|
|
2852
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
1299
2853
|
try {
|
|
2854
|
+
// Each pass asks the identity that wins now; an identity change releasing the request starts another.
|
|
1300
2855
|
for (;;) {
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
const
|
|
1305
|
-
if (
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
throw closed;
|
|
2856
|
+
signal?.throwIfAborted();
|
|
2857
|
+
// A peer naming one publisher identity takes that identity's own routes, whichever
|
|
2858
|
+
// wins, and is never handed another.
|
|
2859
|
+
const local = this.#local(path, identity);
|
|
2860
|
+
if (local)
|
|
2861
|
+
return local;
|
|
2862
|
+
const entry = this.#state.bestEntry(path, unservable(identity));
|
|
2863
|
+
if (!entry?.server) {
|
|
2864
|
+
if (identity !== undefined &&
|
|
2865
|
+
(this.#local(path) || this.#state.bestEntry(path, unservable(undefined))?.server))
|
|
2866
|
+
throw unroutable();
|
|
1313
2867
|
return undefined;
|
|
1314
2868
|
}
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
2869
|
+
const server = entry.server;
|
|
2870
|
+
// Past a broadcast this origin publishes at the path, such as the job answering it,
|
|
2871
|
+
// an application's claim refuses rather than ask its handler for another or hand
|
|
2872
|
+
// out an answer it held from before.
|
|
2873
|
+
if (server.publishes(path))
|
|
2874
|
+
throw unroutable();
|
|
2875
|
+
const live = server.served.get(path);
|
|
2876
|
+
if (live && live.closed.peek() === undefined)
|
|
2877
|
+
return live;
|
|
2878
|
+
const resets = server.resets;
|
|
2879
|
+
server.enqueue(path);
|
|
2880
|
+
server.demanding.set(path, (server.demanding.get(path) ?? 0) + 1);
|
|
2881
|
+
release = () => {
|
|
2882
|
+
// Abort and finally share this registration; release it only once.
|
|
2883
|
+
release = undefined;
|
|
2884
|
+
// A reset already cleared the count, and a later pass may have registered anew.
|
|
2885
|
+
if (server.resets === resets) {
|
|
2886
|
+
const n = (server.demanding.get(path) ?? 1) - 1;
|
|
2887
|
+
if (n <= 0) {
|
|
2888
|
+
server.demanding.delete(path);
|
|
2889
|
+
server.rejected.delete(path);
|
|
2890
|
+
// Only the final waiter changes which pending routes are still needed.
|
|
2891
|
+
this.#state.forget(path);
|
|
2892
|
+
}
|
|
2893
|
+
else
|
|
2894
|
+
server.demanding.set(path, n);
|
|
2895
|
+
}
|
|
2896
|
+
};
|
|
2897
|
+
try {
|
|
2898
|
+
for (;;) {
|
|
2899
|
+
signal?.throwIfAborted();
|
|
2900
|
+
// The route may name another identity by the time its handler answers.
|
|
2901
|
+
if (identity !== undefined && entry.route.peek().identity !== identity)
|
|
2902
|
+
throw unroutable();
|
|
2903
|
+
const served = server.served.get(path);
|
|
2904
|
+
if (served && served.closed.peek() === undefined)
|
|
2905
|
+
return served;
|
|
2906
|
+
const rejected = server.rejected.get(path);
|
|
2907
|
+
if (rejected) {
|
|
2908
|
+
throw rejected;
|
|
2909
|
+
}
|
|
2910
|
+
const closed = server.closed.peek();
|
|
2911
|
+
if (closed !== undefined) {
|
|
2912
|
+
if (closed)
|
|
2913
|
+
throw closed;
|
|
2914
|
+
return undefined;
|
|
2915
|
+
}
|
|
2916
|
+
// Released by an identity change, which also dropped this pass's count: ask again.
|
|
2917
|
+
if (server.resets !== resets)
|
|
2918
|
+
break;
|
|
2919
|
+
if (!server.pending.has(path)) {
|
|
2920
|
+
// Released by a job's exact route, a stream of its own: a peer naming no
|
|
2921
|
+
// identity subscribes again once that route's announcement reaches it, and
|
|
2922
|
+
// one pinned to the claim's identity is refused, unless the job carries it.
|
|
2923
|
+
if (identity === undefined)
|
|
2924
|
+
return undefined;
|
|
2925
|
+
const job = this.#local(path, identity);
|
|
2926
|
+
if (job)
|
|
2927
|
+
return job;
|
|
2928
|
+
throw unroutable();
|
|
2929
|
+
}
|
|
2930
|
+
await race([Signal.race(server.settled, server.closed), cancelled]);
|
|
2931
|
+
}
|
|
2932
|
+
}
|
|
2933
|
+
finally {
|
|
2934
|
+
release?.();
|
|
2935
|
+
}
|
|
1318
2936
|
}
|
|
1319
2937
|
}
|
|
1320
2938
|
finally {
|
|
1321
|
-
|
|
1322
|
-
if (n <= 0)
|
|
1323
|
-
server.demanding.delete(path);
|
|
1324
|
-
else
|
|
1325
|
-
server.demanding.set(path, n);
|
|
2939
|
+
signal?.removeEventListener("abort", abort);
|
|
1326
2940
|
}
|
|
1327
2941
|
}
|
|
1328
2942
|
}
|
|
2943
|
+
// Expose `control` to the protocol layer, which advertises a route as a session's update does.
|
|
2944
|
+
function registerControl(handle, control) {
|
|
2945
|
+
registerWire(handle, {
|
|
2946
|
+
get info() {
|
|
2947
|
+
return control.info();
|
|
2948
|
+
},
|
|
2949
|
+
update: (info) => control.update(info),
|
|
2950
|
+
end: () => control.end(),
|
|
2951
|
+
});
|
|
2952
|
+
}
|
|
2953
|
+
/** The requests arriving on a claim's `server`, each answerable with a route of `publisher`'s when it has one. */
|
|
2954
|
+
async function* serveRequests(server, publisher) {
|
|
2955
|
+
let current;
|
|
2956
|
+
const drop = () => {
|
|
2957
|
+
current?.reject(unroutable());
|
|
2958
|
+
current = undefined;
|
|
2959
|
+
};
|
|
2960
|
+
// Keep the cursor while draining: restarting it would rescan deleted entries.
|
|
2961
|
+
let queued = server.queue.peek().values();
|
|
2962
|
+
try {
|
|
2963
|
+
for (;;) {
|
|
2964
|
+
const next = queued.next();
|
|
2965
|
+
if (!next.done) {
|
|
2966
|
+
drop();
|
|
2967
|
+
server.queue.mutate((queue) => {
|
|
2968
|
+
queue.delete(next.value);
|
|
2969
|
+
});
|
|
2970
|
+
// Only a request pinned to the claim's identity reaches it past a broadcast this
|
|
2971
|
+
// origin publishes at the path, such as the job answering it: that stream is not
|
|
2972
|
+
// the claim's, so the claim refuses rather than start another. Checked here too,
|
|
2973
|
+
// since the broadcast may have landed while the request was queued.
|
|
2974
|
+
if (publisher && server.publishes(Path.join(server.root, next.value.path))) {
|
|
2975
|
+
next.value.reject(unroutable());
|
|
2976
|
+
continue;
|
|
2977
|
+
}
|
|
2978
|
+
current = next.value;
|
|
2979
|
+
setRequestPublisher(current, publisher);
|
|
2980
|
+
yield next.value;
|
|
2981
|
+
continue;
|
|
2982
|
+
}
|
|
2983
|
+
if (server.closed.peek() !== undefined)
|
|
2984
|
+
return;
|
|
2985
|
+
await Signal.race(server.queue, server.closed);
|
|
2986
|
+
queued = server.queue.peek().values();
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
finally {
|
|
2990
|
+
drop();
|
|
2991
|
+
}
|
|
2992
|
+
}
|
|
1329
2993
|
/**
|
|
1330
|
-
*
|
|
1331
|
-
*
|
|
2994
|
+
* An exact route in an origin, owning its broadcast.
|
|
2995
|
+
*
|
|
2996
|
+
* Create one with {@link Producer.route}, or a job's with {@link Request.route}.
|
|
2997
|
+
*
|
|
2998
|
+
* ```ts
|
|
2999
|
+
* // Fill the catalog before readers can find it.
|
|
3000
|
+
* const route = origin.route(Path.from("live/alice"), { hidden: true });
|
|
3001
|
+
* route.broadcast.createTrack("catalog.json");
|
|
3002
|
+
* route.announce(); // visible now, same identity
|
|
3003
|
+
* route.setCost(5n); // re-prices in place
|
|
3004
|
+
* route.withdraw(); // hidden; announce() again continues the same stream
|
|
3005
|
+
* ```
|
|
1332
3006
|
*
|
|
1333
|
-
*
|
|
1334
|
-
* with {@link StreamCode.NoCapacity}. {@link update} re-prices it in place.
|
|
3007
|
+
* {@link close} retracts it and closes its broadcast, and so does closing the broadcast.
|
|
1335
3008
|
*
|
|
1336
3009
|
* @public
|
|
1337
3010
|
*/
|
|
1338
|
-
export class
|
|
1339
|
-
/** The
|
|
1340
|
-
|
|
1341
|
-
#
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
this.prefix = Path.stripPrefix(entry.scope.root, prefix) ?? Path.empty();
|
|
1347
|
-
this.#entry = entry;
|
|
1348
|
-
this.#state = state;
|
|
1349
|
-
this.#retract = retract;
|
|
3011
|
+
export class Route {
|
|
3012
|
+
/** The broadcast this route owns, to fill with tracks. */
|
|
3013
|
+
broadcast;
|
|
3014
|
+
#control;
|
|
3015
|
+
constructor(producer, control) {
|
|
3016
|
+
this.broadcast = producer;
|
|
3017
|
+
this.#control = control;
|
|
3018
|
+
registerControl(this, control);
|
|
1350
3019
|
}
|
|
1351
3020
|
static {
|
|
1352
|
-
|
|
3021
|
+
makeRoute = (producer, control) => new Route(producer, control);
|
|
1353
3022
|
}
|
|
1354
|
-
/**
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
this.#
|
|
1360
|
-
|
|
1361
|
-
|
|
3023
|
+
/**
|
|
3024
|
+
* The stream this route serves, minted when it was created unless {@link RouteOptions.identity}
|
|
3025
|
+
* named one, and fixed for the route's life.
|
|
3026
|
+
*/
|
|
3027
|
+
get identity() {
|
|
3028
|
+
return this.#control.identity();
|
|
3029
|
+
}
|
|
3030
|
+
/**
|
|
3031
|
+
* Make the route visible, or keep it so. Idempotent.
|
|
3032
|
+
*
|
|
3033
|
+
* Visible to local consumers before it returns. A job's route ({@link Request.route}) also
|
|
3034
|
+
* releases the requesters waiting on its path through the claim, which resolve it afresh: onto
|
|
3035
|
+
* this route, under its own identity, unless pinned to the claim's. Throws once the route or its
|
|
3036
|
+
* broadcast closed, and, announcing nothing, with {@link StreamCode | StreamCode.Unroutable} when
|
|
3037
|
+
* a job's request was released or its claim retracted meanwhile: that route can never announce.
|
|
3038
|
+
* A job's route priced above its visible claim at the claim's own prefix (see
|
|
3039
|
+
* {@link Request.route}) throws the same way, and its first announce can never be retried.
|
|
3040
|
+
*/
|
|
3041
|
+
announce() {
|
|
3042
|
+
this.#control.announce();
|
|
3043
|
+
}
|
|
3044
|
+
/**
|
|
3045
|
+
* Hide the route from everyone, local and remote alike, keeping its broadcast and identity:
|
|
3046
|
+
* {@link announce} brings the same stream back. Requests for the path fail with
|
|
3047
|
+
* {@link StreamCode | StreamCode.Unroutable} meanwhile, while tracks already in flight carry
|
|
3048
|
+
* on to their own end. Idempotent.
|
|
3049
|
+
*/
|
|
3050
|
+
withdraw() {
|
|
3051
|
+
this.#control.withdraw();
|
|
3052
|
+
}
|
|
3053
|
+
/** Whether the route is visible. */
|
|
3054
|
+
get announced() {
|
|
3055
|
+
return this.#control.announced();
|
|
3056
|
+
}
|
|
3057
|
+
/**
|
|
3058
|
+
* Re-price the route: lower wins among routes covering the same prefix and anonymity. A
|
|
3059
|
+
* visible route changes in place, as an update to its readers; a hidden one announces at
|
|
3060
|
+
* this price. Throws once the route or its broadcast closed, and with
|
|
3061
|
+
* {@link StreamCode | StreamCode.Unroutable}, keeping the old price, when a job's route would
|
|
3062
|
+
* cost more than its visible claim at the claim's own prefix (see {@link Request.route}).
|
|
3063
|
+
*/
|
|
3064
|
+
setCost(cost) {
|
|
3065
|
+
this.#control.change({ cost });
|
|
3066
|
+
}
|
|
3067
|
+
/**
|
|
3068
|
+
* Swap the credential the advertising request carries; see {@link RouteOptions.auth}.
|
|
3069
|
+
* Undefined advertises under the session's scope. Throws once the route or its broadcast closed.
|
|
3070
|
+
*/
|
|
3071
|
+
// fallow-ignore-next-line unused-class-member
|
|
3072
|
+
setAuth(auth) {
|
|
3073
|
+
this.#control.change({ auth });
|
|
1362
3074
|
}
|
|
1363
|
-
/** Retract the route and
|
|
3075
|
+
/** Retract the route for good, closing its broadcast and refusing a job's waiting requesters. Idempotent. */
|
|
1364
3076
|
close() {
|
|
1365
|
-
|
|
1366
|
-
return;
|
|
1367
|
-
this.#closed = true;
|
|
1368
|
-
this.#retract();
|
|
3077
|
+
this.#control.close();
|
|
1369
3078
|
}
|
|
1370
|
-
/**
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
3079
|
+
/** Calls {@link close}, so `using` retracts the route. */
|
|
3080
|
+
[Symbol.dispose]() {
|
|
3081
|
+
this.close();
|
|
3082
|
+
}
|
|
3083
|
+
}
|
|
3084
|
+
/**
|
|
3085
|
+
* A prefix route in an origin: a claim that every path beneath a prefix can be served,
|
|
3086
|
+
* answering each request as it arrives.
|
|
3087
|
+
*
|
|
3088
|
+
* Create one with {@link Producer.claim}. Requests arrive on {@link requested}; answer each by
|
|
3089
|
+
* serving a broadcast already held or by creating the path's own {@link Route}.
|
|
3090
|
+
*
|
|
3091
|
+
* ```ts
|
|
3092
|
+
* const claim = origin.claim(Path.from("room"));
|
|
3093
|
+
* for await (const request of claim.requested()) {
|
|
3094
|
+
* const route = request.route({ hidden: true });
|
|
3095
|
+
* route.broadcast.createTrack("catalog.json");
|
|
3096
|
+
* route.announce(); // the requesters read it
|
|
3097
|
+
* }
|
|
3098
|
+
* ```
|
|
3099
|
+
*
|
|
3100
|
+
* {@link close} retracts it and rejects the requests still waiting.
|
|
3101
|
+
*
|
|
3102
|
+
* @public
|
|
3103
|
+
*/
|
|
3104
|
+
export class Claim {
|
|
3105
|
+
#control;
|
|
3106
|
+
#requested;
|
|
3107
|
+
constructor(control, requested) {
|
|
3108
|
+
this.#control = control;
|
|
3109
|
+
this.#requested = requested;
|
|
3110
|
+
registerControl(this, control);
|
|
3111
|
+
}
|
|
3112
|
+
static {
|
|
3113
|
+
makeClaim = (control, requested) => new Claim(control, requested);
|
|
3114
|
+
}
|
|
3115
|
+
/**
|
|
3116
|
+
* The stream this claim serves, minted when it was created unless {@link RouteOptions.identity}
|
|
3117
|
+
* named one. A session's claim follows its peer's.
|
|
3118
|
+
*/
|
|
3119
|
+
get identity() {
|
|
3120
|
+
return this.#control.identity();
|
|
3121
|
+
}
|
|
3122
|
+
/**
|
|
3123
|
+
* Make the claim visible, or keep it so. Idempotent. Visible to local consumers before it
|
|
3124
|
+
* returns. Throws once the claim or the origin closed, and with
|
|
3125
|
+
* {@link StreamCode | StreamCode.Unroutable}, staying hidden, when it would cost less than a
|
|
3126
|
+
* visible job's route at its own prefix (see {@link Request.route}).
|
|
3127
|
+
*/
|
|
3128
|
+
announce() {
|
|
3129
|
+
this.#control.announce();
|
|
3130
|
+
}
|
|
3131
|
+
/**
|
|
3132
|
+
* Hide the claim from everyone, local and remote alike, keeping its identity: {@link announce}
|
|
3133
|
+
* brings it back. Requests beneath it fail with {@link StreamCode | StreamCode.Unroutable}
|
|
3134
|
+
* meanwhile. Idempotent.
|
|
3135
|
+
*/
|
|
3136
|
+
withdraw() {
|
|
3137
|
+
this.#control.withdraw();
|
|
3138
|
+
}
|
|
3139
|
+
/** Whether the claim is visible. */
|
|
3140
|
+
get announced() {
|
|
3141
|
+
return this.#control.announced();
|
|
3142
|
+
}
|
|
3143
|
+
/**
|
|
3144
|
+
* Re-price the claim: lower wins among routes covering the same prefix and anonymity. A
|
|
3145
|
+
* visible claim changes in place, as an update to its readers; a hidden one announces at
|
|
3146
|
+
* this price. Throws once the claim or the origin closed, and with
|
|
3147
|
+
* {@link StreamCode | StreamCode.Unroutable}, keeping the old price, when the claim would cost
|
|
3148
|
+
* less than a visible job's route at its own prefix (see {@link Request.route}).
|
|
3149
|
+
*/
|
|
3150
|
+
setCost(cost) {
|
|
3151
|
+
this.#control.change({ cost });
|
|
3152
|
+
}
|
|
3153
|
+
/**
|
|
3154
|
+
* Swap the credential the advertising request carries; see {@link RouteOptions.auth}.
|
|
3155
|
+
* Undefined advertises under the session's scope. Throws once the claim or the origin closed.
|
|
3156
|
+
*/
|
|
3157
|
+
// fallow-ignore-next-line unused-class-member
|
|
3158
|
+
setAuth(auth) {
|
|
3159
|
+
this.#control.change({ auth });
|
|
3160
|
+
}
|
|
3161
|
+
/**
|
|
3162
|
+
* The requests beneath this claim, as they arrive, each to answer with {@link Request.serve},
|
|
3163
|
+
* {@link Request.route} or {@link Request.reject}. Advancing the iterator or closing it without
|
|
3164
|
+
* answering rejects the request. Ends once the claim is closed or the origin closes.
|
|
3165
|
+
*/
|
|
3166
|
+
requested() {
|
|
3167
|
+
return this.#requested();
|
|
3168
|
+
}
|
|
3169
|
+
/** Retract the claim for good, rejecting the requests still waiting. Idempotent. */
|
|
3170
|
+
close() {
|
|
3171
|
+
this.#control.close();
|
|
3172
|
+
}
|
|
3173
|
+
/** Calls {@link close}, so `using` retracts the claim. */
|
|
3174
|
+
[Symbol.dispose]() {
|
|
3175
|
+
this.close();
|
|
1400
3176
|
}
|
|
1401
3177
|
}
|
|
1402
3178
|
/**
|
|
1403
3179
|
* A pending request for a broadcast to be served on demand.
|
|
1404
3180
|
*
|
|
1405
|
-
* Yielded by {@link
|
|
1406
|
-
*
|
|
1407
|
-
*
|
|
3181
|
+
* Yielded by {@link Claim.requested}. Answer it one of three ways: {@link serve} a broadcast
|
|
3182
|
+
* already held, under the claim's identity; create the path's own exact route with
|
|
3183
|
+
* {@link route}, a job of its own identity; or {@link reject} it with an error. Advancing the iterator or closing it
|
|
3184
|
+
* without answering rejects the request.
|
|
1408
3185
|
*
|
|
1409
3186
|
* @public
|
|
1410
3187
|
*/
|
|
@@ -1412,36 +3189,106 @@ export class Request {
|
|
|
1412
3189
|
/** The path that was requested. */
|
|
1413
3190
|
path;
|
|
1414
3191
|
#server;
|
|
1415
|
-
|
|
3192
|
+
// Answered by the application, or withdrawn by the origin before it was.
|
|
3193
|
+
#state = "pending";
|
|
3194
|
+
#publisher;
|
|
3195
|
+
// Settled once the origin withdraws it: nobody waits on its answer any more.
|
|
3196
|
+
#unwanted = new Once();
|
|
1416
3197
|
constructor(path, server) {
|
|
1417
3198
|
this.path = path;
|
|
1418
3199
|
this.#server = server;
|
|
1419
3200
|
}
|
|
1420
3201
|
static {
|
|
1421
3202
|
makeRequest = (path, server) => new Request(path, server);
|
|
3203
|
+
setRequestPublisher = (request, publisher) => {
|
|
3204
|
+
request.#publisher = publisher;
|
|
3205
|
+
};
|
|
3206
|
+
jobPending = (request) => request.#server.has(request);
|
|
3207
|
+
// Its exact route owns availability: never cache a published job under the prefix,
|
|
3208
|
+
// which would keep serving it after that exact route is withdrawn.
|
|
3209
|
+
releaseJob = (request) => request.#server.release(request);
|
|
3210
|
+
rejectJob = (request, err) => request.#server.reject(request, err);
|
|
3211
|
+
jobServer = (request) => request.#server;
|
|
1422
3212
|
finishRequest = (request, err) => {
|
|
1423
|
-
request.#
|
|
3213
|
+
request.#state = "withdrawn";
|
|
3214
|
+
if (request.#unwanted.peek() === undefined)
|
|
3215
|
+
request.#unwanted.set(true);
|
|
1424
3216
|
void err;
|
|
1425
3217
|
};
|
|
1426
3218
|
}
|
|
1427
3219
|
/**
|
|
1428
|
-
*
|
|
3220
|
+
* Resolves once no requester waits on the answer any more: each left, or the origin moved
|
|
3221
|
+
* them to another route. Final: a later requester gets a new request.
|
|
3222
|
+
*/
|
|
3223
|
+
// Public API for a claimant's owner, such as an orchestrator deciding whether to launch work.
|
|
3224
|
+
// fallow-ignore-next-line unused-class-member
|
|
3225
|
+
async unwanted() {
|
|
3226
|
+
await this.#unwanted;
|
|
3227
|
+
}
|
|
3228
|
+
/**
|
|
3229
|
+
* Answer with the path's own exact route, owning a new broadcast.
|
|
1429
3230
|
*
|
|
1430
|
-
* The
|
|
1431
|
-
*
|
|
3231
|
+
* The route mints its own identity unless `options` names one, as {@link Producer.route}
|
|
3232
|
+
* does: the job is its own stream, so a job that ends and restarts beneath the same claim is a
|
|
3233
|
+
* new stream at its path, and the claim keeps its identity and keeps answering later
|
|
3234
|
+
* requests. A reader that resolved the path through the claim moves to the job's identity as a
|
|
3235
|
+
* new stream: a follower is handed the job's broadcast, and a request pinned to the claim's
|
|
3236
|
+
* identity is refused. Created visible, the route announces now and releases the requesters
|
|
3237
|
+
* waiting on its path, which resolve it afresh. Created {@link RouteOptions.hidden | hidden},
|
|
3238
|
+
* they wait until {@link Route.announce}, so the job can fill its catalog first; closing the
|
|
3239
|
+
* route or its broadcast before then refuses them.
|
|
3240
|
+
*
|
|
3241
|
+
* At the claim's own prefix, the job may cost no more than the claim: both rank in one tier,
|
|
3242
|
+
* where cost comes first, so a pricier job would lose its path to the claim, which would ask for
|
|
3243
|
+
* another job. Neither may be re-priced past the other while both are visible.
|
|
3244
|
+
*
|
|
3245
|
+
* Throws {@link StreamCode | StreamCode.Unroutable} on a request a session forwarded, which can
|
|
3246
|
+
* only {@link serve} its peer's broadcast, when `options` price the job above its claim at the
|
|
3247
|
+
* claim's own prefix, refusing the requesters, and, announcing nothing, once the request was
|
|
3248
|
+
* released or its claim retracted. Otherwise it throws as {@link Producer.route} does.
|
|
1432
3249
|
*/
|
|
1433
|
-
|
|
1434
|
-
if (this.#
|
|
3250
|
+
route(options = {}) {
|
|
3251
|
+
if (this.#state !== "pending")
|
|
3252
|
+
throw new Error("request is closed");
|
|
3253
|
+
if (!this.#publisher)
|
|
3254
|
+
throw unroutable();
|
|
3255
|
+
let route;
|
|
3256
|
+
try {
|
|
3257
|
+
route = exactRoute(this.#publisher, this.path, options, this);
|
|
3258
|
+
}
|
|
3259
|
+
finally {
|
|
3260
|
+
// Refused for its price, the request is answered: its requesters were refused.
|
|
3261
|
+
if (!jobPending(this))
|
|
3262
|
+
this.#state = "answered";
|
|
3263
|
+
}
|
|
3264
|
+
this.#state = "answered";
|
|
3265
|
+
return route;
|
|
3266
|
+
}
|
|
3267
|
+
/**
|
|
3268
|
+
* Serve a held broadcast under this claim without announcing an exact route.
|
|
3269
|
+
*
|
|
3270
|
+
* Takes ownership of a consumer; a producer stays the caller's. Every answer at this
|
|
3271
|
+
* path belongs to the claim's identity and must continue the same keys and
|
|
3272
|
+
* bytes. Start a new claim for different content, or use {@link route} for a job
|
|
3273
|
+
* that can restart independently, whose route carries its own identity.
|
|
3274
|
+
*/
|
|
3275
|
+
serve(source) {
|
|
3276
|
+
if (this.#state !== "pending") {
|
|
3277
|
+
// A withdrawn request takes no answer. Only the handed-off consumer is ours;
|
|
3278
|
+
// the application still owns its producer and any other consumers.
|
|
3279
|
+
if (this.#state === "withdrawn" && source instanceof broadcast.Consumer)
|
|
3280
|
+
source.close();
|
|
1435
3281
|
return;
|
|
1436
|
-
|
|
3282
|
+
}
|
|
3283
|
+
this.#state = "answered";
|
|
1437
3284
|
const front = source instanceof broadcast.Producer ? source.consume() : source;
|
|
1438
3285
|
this.#server.accept(this, front);
|
|
1439
3286
|
}
|
|
1440
3287
|
/** Reject the request, resolving every awaiting requester with `err`. */
|
|
1441
3288
|
reject(err) {
|
|
1442
|
-
if (this.#
|
|
3289
|
+
if (this.#state !== "pending")
|
|
1443
3290
|
return;
|
|
1444
|
-
this.#
|
|
3291
|
+
this.#state = "answered";
|
|
1445
3292
|
this.#server.reject(this, err);
|
|
1446
3293
|
}
|
|
1447
3294
|
}
|