@ghostlytools/cli 1.11.4 → 1.12.1
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 +31 -14
- package/dist/assets/{api-MYc6QNRs.js → api-BwEgbvvK.js} +395 -131
- package/dist/assets/api-BwEgbvvK.js.map +1 -0
- package/dist/assets/{api-BwyFq4lA.js → api-jRUXnBdX.js} +2 -2
- package/dist/assets/{apiKit-CfsNx9ZR.js → apiKit-BlsNk-Cx.js} +48 -11
- package/dist/assets/apiKit-BlsNk-Cx.js.map +1 -0
- package/dist/assets/appFetch-CW9fJPHh.js +602 -0
- package/dist/assets/appFetch-CW9fJPHh.js.map +1 -0
- package/dist/assets/apps-BcG4u6SM.js +1 -0
- package/dist/assets/{apps-h12II1us.js → apps-CL8TXHhu.js} +20 -14
- package/dist/assets/apps-CL8TXHhu.js.map +1 -0
- package/dist/assets/{argon2id-088uQfnH.js → argon2id-B9wFdSj6.js} +2 -3
- package/dist/assets/{argon2id-088uQfnH.js.map → argon2id-B9wFdSj6.js.map} +1 -1
- package/dist/assets/{arkade-Bw_4PZRG.js → arkade-DryM-q5n.js} +6 -7
- package/dist/assets/arkade-DryM-q5n.js.map +1 -0
- package/dist/assets/{backup-Chvy79SN.js → backup-BA41EDvh.js} +110 -26
- package/dist/assets/backup-BA41EDvh.js.map +1 -0
- package/dist/assets/backup-xZfV-ytw.js +1 -0
- package/dist/assets/bark_ffi_wasm_bg-CcafsJla.js +5 -0
- package/dist/assets/bark_ffi_wasm_bg-CcafsJla.js.map +1 -0
- package/dist/assets/{bark_ffi_wasm_bg-Bxru_BVW.wasm → bark_ffi_wasm_bg-VNmaJsc9.wasm} +0 -0
- package/dist/assets/{bitcoind-CdSmvy2y.js → bitcoind-Dh7n9amb.js} +4 -5
- package/dist/assets/{bitcoind-CdSmvy2y.js.map → bitcoind-Dh7n9amb.js.map} +1 -1
- package/dist/assets/bitcoindevkit_bg-BWQHx7vd.js +5 -0
- package/dist/assets/bitcoindevkit_bg-BWQHx7vd.js.map +1 -0
- package/dist/assets/bitcoindevkit_bg-DXil67yS.wasm +0 -0
- package/dist/assets/{blobFiles-BmsrvZc3.js → blobFiles-DDs34n0r.js} +4 -5
- package/dist/assets/{blobFiles-BmsrvZc3.js.map → blobFiles-DDs34n0r.js.map} +1 -1
- package/dist/assets/breez_sdk_spark_wasm_bg.wasm +0 -0
- package/dist/assets/buttons-CsGdQNnR.js +1 -0
- package/dist/assets/{buttons-BgoExb4E.js → buttons-SH1tNRh7.js} +45 -23
- package/dist/assets/buttons-SH1tNRh7.js.map +1 -0
- package/dist/assets/bytes-C1DNGY18.js +132 -0
- package/dist/assets/bytes-C1DNGY18.js.map +1 -0
- package/dist/assets/{childEnv-K_IzUqQx.js → childEnv-C-99hdbL.js} +2 -3
- package/dist/assets/{childEnv-K_IzUqQx.js.map → childEnv-C-99hdbL.js.map} +1 -1
- package/dist/assets/{client-DY8tZZWi.js → client-DGS-atRu.js} +5 -8
- package/dist/assets/{client-DY8tZZWi.js.map → client-DGS-atRu.js.map} +1 -1
- package/dist/assets/{codec-BnMg-Grj.js → codec-B2rH1mfx.js} +2 -4
- package/dist/assets/{codec-BnMg-Grj.js.map → codec-B2rH1mfx.js.map} +1 -1
- package/dist/assets/domainProofs-BDNP1dIy.js +280 -0
- package/dist/assets/domainProofs-BDNP1dIy.js.map +1 -0
- package/dist/assets/{endpoint-DarqblYE.js → endpoint-Cvu-taMs.js} +462 -424
- package/dist/assets/endpoint-Cvu-taMs.js.map +1 -0
- package/dist/assets/engineErrors--9hfgKlK.js +150 -0
- package/dist/assets/engineErrors--9hfgKlK.js.map +1 -0
- package/dist/assets/{envelope-CdV7vHVZ.js → envelope-Bi2kMqbp.js} +3 -4
- package/dist/assets/{envelope-CdV7vHVZ.js.map → envelope-Bi2kMqbp.js.map} +1 -1
- package/dist/assets/{errors-CAh6GLJZ.js → errors-DDLFO4Gf.js} +5 -4
- package/dist/assets/errors-DDLFO4Gf.js.map +1 -0
- package/dist/assets/{fedimintSdk-CPFfVK-G.js → fedimintSdk-D-6H9Pe7.js} +4 -5
- package/dist/assets/{fedimintSdk-CPFfVK-G.js.map → fedimintSdk-D-6H9Pe7.js.map} +1 -1
- package/dist/assets/fedimint_client_wasm_bg-Cr5xGD-w.js +5 -0
- package/dist/assets/{fedimint_client_wasm_bg-BtHHlFpp.js.map → fedimint_client_wasm_bg-Cr5xGD-w.js.map} +1 -1
- package/dist/assets/fileBytes-CCKbwqG_.js +1 -0
- package/dist/assets/{fileBytes-BLPZq9YU.js → fileBytes-CfvUXdSy.js} +5 -7
- package/dist/assets/fileBytes-CfvUXdSy.js.map +1 -0
- package/dist/assets/{fileBytes-B0z9A6Le.js → fileBytes-tj6-VZcB.js} +3 -4
- package/dist/assets/{fileBytes-B0z9A6Le.js.map → fileBytes-tj6-VZcB.js.map} +1 -1
- package/dist/assets/fileBytesIdb-B0zzJ6p7.js +1 -0
- package/dist/assets/{fileBytesIdb-Bmlrl9EO.js → fileBytesIdb-BJirUAjI.js} +4 -5
- package/dist/assets/{fileBytesIdb-Bmlrl9EO.js.map → fileBytesIdb-BJirUAjI.js.map} +1 -1
- package/dist/assets/{fileBytesOpfs-DBS0kbPT.js → fileBytesOpfs-DRUYLER4.js} +4 -5
- package/dist/assets/{fileBytesOpfs-DBS0kbPT.js.map → fileBytesOpfs-DRUYLER4.js.map} +1 -1
- package/dist/assets/{fixedHosts-CbgqrIKs.js → fixedHosts-CPSsGOI2.js} +4 -5
- package/dist/assets/{fixedHosts-CbgqrIKs.js.map → fixedHosts-CPSsGOI2.js.map} +1 -1
- package/dist/assets/{ghostly_iroh_web-BzAXJUYH.js → ghostly_iroh_web-DIj5xGmR.js} +2 -3
- package/dist/assets/{ghostly_iroh_web-BzAXJUYH.js.map → ghostly_iroh_web-DIj5xGmR.js.map} +1 -1
- package/dist/assets/{src-DJe-OYxd.js → groupMeta-DxuJzko_.js} +6009 -25536
- package/dist/assets/groupMeta-DxuJzko_.js.map +1 -0
- package/dist/assets/{host-C0NMNQLl.js → host-BxM-HMr0.js} +262 -134
- package/dist/assets/host-BxM-HMr0.js.map +1 -0
- package/dist/assets/{hyperdhtRelay-Dz7pvcBA.js → hyperdhtRelay-DLUiwfnt.js} +36 -9
- package/dist/assets/hyperdhtRelay-DLUiwfnt.js.map +1 -0
- package/dist/assets/hyperdhtRelay-bjNGacf5.js +32 -0
- package/dist/assets/hyperdhtRelay-bjNGacf5.js.map +1 -0
- package/dist/assets/{idb-CEuoNsTv.js → idb-ewvbqyDI.js} +2 -3
- package/dist/assets/{idb-CEuoNsTv.js.map → idb-ewvbqyDI.js.map} +1 -1
- package/dist/assets/identityProofs-Cksc_K7_.js +940 -0
- package/dist/assets/identityProofs-Cksc_K7_.js.map +1 -0
- package/dist/assets/{irohWeb-caAj4Dms.js → irohWeb-CbnGtUo6.js} +3231 -87
- package/dist/assets/irohWeb-CbnGtUo6.js.map +1 -0
- package/dist/assets/joinTrace-CBr1jbch.js +1 -0
- package/dist/assets/{joinTrace-CjRDFy85.js → joinTrace-xKBe73j9.js} +2 -3
- package/dist/assets/{joinTrace-CjRDFy85.js.map → joinTrace-xKBe73j9.js.map} +1 -1
- package/dist/assets/{legacy_ciphers-JJOu0JXL.js → legacy_ciphers-BnYATjxo.js} +2 -3
- package/dist/assets/{legacy_ciphers-JJOu0JXL.js.map → legacy_ciphers-BnYATjxo.js.map} +1 -1
- package/dist/assets/linkPreview-DdMf-YAc.js +252 -0
- package/dist/assets/linkPreview-DdMf-YAc.js.map +1 -0
- package/dist/assets/{main-C6t5zrD5.js → main-CwTaepxR.js} +296 -122
- package/dist/assets/main-CwTaepxR.js.map +1 -0
- package/dist/assets/{mainline-DCuGUSZF.js → mainline-CVrd0C1n.js} +72 -18
- package/dist/assets/mainline-CVrd0C1n.js.map +1 -0
- package/dist/assets/mints-BcBODVVW.js +1 -0
- package/dist/assets/{mints-j5XAOk4f.js → mints-CMDLbMeH.js} +8 -6
- package/dist/assets/mints-CMDLbMeH.js.map +1 -0
- package/dist/assets/{nacl-fast-CDhNCYHs.js → nacl-fast-B1ThWvuM.js} +2 -3
- package/dist/assets/{nacl-fast-CDhNCYHs.js.map → nacl-fast-B1ThWvuM.js.map} +1 -1
- package/dist/assets/{networkReason-BbQW1Xpc.js → networkReason-Be-hMghr.js} +10 -14
- package/dist/assets/{networkReason-BbQW1Xpc.js.map → networkReason-Be-hMghr.js.map} +1 -1
- package/dist/assets/{noble_curves-DDWn5AH0.js → noble_curves-DqpNlhAU.js} +3 -4
- package/dist/assets/{noble_curves-DDWn5AH0.js.map → noble_curves-DqpNlhAU.js.map} +1 -1
- package/dist/assets/{noble_hashes-B7AZyFyS.js → noble_hashes-LFe1N2xt.js} +3 -4
- package/dist/assets/{noble_hashes-B7AZyFyS.js.map → noble_hashes-LFe1N2xt.js.map} +1 -1
- package/dist/assets/{nwc-CSDRMZ0e.js → nwc-DKE2FXr6.js} +5 -6
- package/dist/assets/{nwc-CSDRMZ0e.js.map → nwc-DKE2FXr6.js.map} +1 -1
- package/dist/assets/nwcProvider-pL2XqSbW.js +208 -0
- package/dist/assets/nwcProvider-pL2XqSbW.js.map +1 -0
- package/dist/assets/{openpgp-BBFr-zHQ.js → openpgp-LkR_Zof0.js} +14 -15
- package/dist/assets/{openpgp-BBFr-zHQ.js.map → openpgp-LkR_Zof0.js.map} +1 -1
- package/dist/assets/ownDid-DY3rYqDI.js +862 -0
- package/dist/assets/ownDid-DY3rYqDI.js.map +1 -0
- package/dist/assets/{ownStatus-BoTf5WqQ.js → ownStatus-DtzIllg2.js} +70 -20
- package/dist/assets/ownStatus-DtzIllg2.js.map +1 -0
- package/dist/assets/pairedTransports-BKmvlKz1.js +581 -0
- package/dist/assets/pairedTransports-BKmvlKz1.js.map +1 -0
- package/dist/assets/paymentIntent-DySU3Ajm.js +349 -0
- package/dist/assets/paymentIntent-DySU3Ajm.js.map +1 -0
- package/dist/assets/pkarr-BQo4PcCg.js +367 -0
- package/dist/assets/pkarr-BQo4PcCg.js.map +1 -0
- package/dist/assets/{profiles-DszHjEJi.js → profiles-BOYGlmu2.js} +9 -10
- package/dist/assets/profiles-BOYGlmu2.js.map +1 -0
- package/dist/assets/registry-4W1LGlCF.js +1 -0
- package/dist/assets/{registry-Da-r6eC3.js → registry-Bep4umOM.js} +1296 -80
- package/dist/assets/registry-Bep4umOM.js.map +1 -0
- package/dist/assets/relay-Cjfmdec0.js +2219 -0
- package/dist/assets/relay-Cjfmdec0.js.map +1 -0
- package/dist/assets/resolve-CIc3KZMV.js +970 -0
- package/dist/assets/resolve-CIc3KZMV.js.map +1 -0
- package/dist/assets/{restoredRows-C3LcxmOw.js → restoredRows-D_WSHE7H.js} +3 -4
- package/dist/assets/{restoredRows-C3LcxmOw.js.map → restoredRows-D_WSHE7H.js.map} +1 -1
- package/dist/assets/secretGuard-Bm6nci0I.js +1 -0
- package/dist/assets/{secretGuard-CsVHDdYk.js → secretGuard-BrATm_gl.js} +43 -22
- package/dist/assets/{secretGuard-CsVHDdYk.js.map → secretGuard-BrATm_gl.js.map} +1 -1
- package/dist/assets/{server-C1jZZ61O.js → server-D4gf-LnD.js} +9797 -479
- package/dist/assets/server-D4gf-LnD.js.map +1 -0
- package/dist/assets/{sha512-BYId-G-T.js → sha512-DST9fbWH.js} +2 -3
- package/dist/assets/{sha512-BYId-G-T.js.map → sha512-DST9fbWH.js.map} +1 -1
- package/dist/assets/sshForges-BnvwmNj-.js +539 -0
- package/dist/assets/sshForges-BnvwmNj-.js.map +1 -0
- package/dist/assets/{storage-Bya5b2Xt.js → storage-BpjdxvO2.js} +243 -34
- package/dist/assets/storage-BpjdxvO2.js.map +1 -0
- package/dist/assets/text-li76pThX.js +106 -0
- package/dist/assets/text-li76pThX.js.map +1 -0
- package/dist/assets/{translate-CUuC5szR.js → translate-BcbSWkuq.js} +21 -26
- package/dist/assets/{translate-CUuC5szR.js.map → translate-BcbSWkuq.js.map} +1 -1
- package/dist/assets/{tree-store-N362nuzZ.js → tree-store-CAchkO_l.js} +22 -6
- package/dist/assets/tree-store-CAchkO_l.js.map +1 -0
- package/dist/assets/{types-Clyb52K2.js → types-CdWwgvyt.js} +2 -3
- package/dist/assets/{types-Clyb52K2.js.map → types-CdWwgvyt.js.map} +1 -1
- package/dist/assets/{unbzip2-stream-B1C-JX9L.js → unbzip2-stream-BtkYGpC5.js} +2 -3
- package/dist/assets/{unbzip2-stream-B1C-JX9L.js.map → unbzip2-stream-BtkYGpC5.js.map} +1 -1
- package/dist/assets/{usdt-DI51znt-.js → usdt-CBJ4Q5Ji.js} +7 -7
- package/dist/assets/usdt-CBJ4Q5Ji.js.map +1 -0
- package/dist/assets/{waitNotice-Cn0WJ-He.js → waitNotice-DQvZgz2T.js} +27 -4
- package/dist/assets/waitNotice-DQvZgz2T.js.map +1 -0
- package/dist/assets/{web-DwgdhfcY.js → web-68CYFCl9.js} +314 -91
- package/dist/assets/web-68CYFCl9.js.map +1 -0
- package/dist/ghostly.mjs +3 -5
- package/dist/ghostly.mjs.map +1 -1
- package/npm-shrinkwrap.json +225 -200
- package/package.json +16 -16
- package/dist/assets/api-MYc6QNRs.js.map +0 -1
- package/dist/assets/apiKit-CfsNx9ZR.js.map +0 -1
- package/dist/assets/appFetch-DVYZOZFw.js +0 -194
- package/dist/assets/appFetch-DVYZOZFw.js.map +0 -1
- package/dist/assets/apps-Dbw2Hcko.js +0 -2
- package/dist/assets/apps-h12II1us.js.map +0 -1
- package/dist/assets/arkade-Bw_4PZRG.js.map +0 -1
- package/dist/assets/backup-CdEzTq5C.js +0 -2
- package/dist/assets/backup-Chvy79SN.js.map +0 -1
- package/dist/assets/bark_ffi_wasm_bg-EY2uc0Bx.js +0 -6
- package/dist/assets/bark_ffi_wasm_bg-EY2uc0Bx.js.map +0 -1
- package/dist/assets/bitcoindProvider-BvoRh0D5.js +0 -97
- package/dist/assets/bitcoindProvider-BvoRh0D5.js.map +0 -1
- package/dist/assets/bitcoindevkit_bg-B-iLWrmX.js +0 -6
- package/dist/assets/bitcoindevkit_bg-B-iLWrmX.js.map +0 -1
- package/dist/assets/bitcoindevkit_bg-CRkZTSbO.wasm +0 -0
- package/dist/assets/buttons-BgoExb4E.js.map +0 -1
- package/dist/assets/buttons-DK2aAGat.js +0 -2
- package/dist/assets/endpoint-DarqblYE.js.map +0 -1
- package/dist/assets/errors-CAh6GLJZ.js.map +0 -1
- package/dist/assets/fedimint_client_wasm_bg-BtHHlFpp.js +0 -6
- package/dist/assets/fileBytes-BLPZq9YU.js.map +0 -1
- package/dist/assets/fileBytes-BtIyoDF2.js +0 -2
- package/dist/assets/fileBytesIdb-D8mAK4pF.js +0 -2
- package/dist/assets/host-C0NMNQLl.js.map +0 -1
- package/dist/assets/hyperdhtRelay-1wzya_0L.js +0 -25
- package/dist/assets/hyperdhtRelay-1wzya_0L.js.map +0 -1
- package/dist/assets/hyperdhtRelay-Dz7pvcBA.js.map +0 -1
- package/dist/assets/irohWeb-caAj4Dms.js.map +0 -1
- package/dist/assets/joinTrace-D-fnt2p3.js +0 -2
- package/dist/assets/jsonOut-POtMBS_V.js +0 -16
- package/dist/assets/jsonOut-POtMBS_V.js.map +0 -1
- package/dist/assets/main-C6t5zrD5.js.map +0 -1
- package/dist/assets/mainline-DCuGUSZF.js.map +0 -1
- package/dist/assets/mints-BaOmx517.js +0 -2
- package/dist/assets/mints-j5XAOk4f.js.map +0 -1
- package/dist/assets/nwcProvider-PE0wli8n.js +0 -88
- package/dist/assets/nwcProvider-PE0wli8n.js.map +0 -1
- package/dist/assets/ownDid-C1R5Kd8q.js +0 -282
- package/dist/assets/ownDid-C1R5Kd8q.js.map +0 -1
- package/dist/assets/ownStatus-BoTf5WqQ.js.map +0 -1
- package/dist/assets/profiles-DszHjEJi.js.map +0 -1
- package/dist/assets/registry-Da-r6eC3.js.map +0 -1
- package/dist/assets/registry-zUZBSHYv.js +0 -2
- package/dist/assets/resolve-tUzh4lHh.js +0 -400
- package/dist/assets/resolve-tUzh4lHh.js.map +0 -1
- package/dist/assets/secretGuard-DCb1I0_B.js +0 -2
- package/dist/assets/server-C1jZZ61O.js.map +0 -1
- package/dist/assets/src-DJe-OYxd.js.map +0 -1
- package/dist/assets/sshForges-C9zTO72R.js +0 -261
- package/dist/assets/sshForges-C9zTO72R.js.map +0 -1
- package/dist/assets/storage-Bya5b2Xt.js.map +0 -1
- package/dist/assets/tree-store-N362nuzZ.js.map +0 -1
- package/dist/assets/usdt-DI51znt-.js.map +0 -1
- package/dist/assets/waitNotice-Cn0WJ-He.js.map +0 -1
- package/dist/assets/web-DwgdhfcY.js.map +0 -1
|
@@ -0,0 +1,2219 @@
|
|
|
1
|
+
import { a as createRelayPayload, c as openRelayPayload, i as RELAY_PAYLOAD_MAX_BYTES, l as parseRelayPayload, n as PKARR_FUTURE_SKEW_MS, s as newerPacket, u as readOwnRelayPayload } from "./pkarr-BQo4PcCg.js";
|
|
2
|
+
// ../core/src/transport.ts
|
|
3
|
+
/**
|
|
4
|
+
* `transport` with `extra` added to every request's options: a group's edges say `group` so. The optional methods
|
|
5
|
+
* stay optional (a caller reads `configure` as "the DHT is reached directly").
|
|
6
|
+
*/
|
|
7
|
+
function withRequestOptions(transport, extra) {
|
|
8
|
+
const wrapped = {
|
|
9
|
+
publish: (identity, records, options) => transport.publish(identity, records, {
|
|
10
|
+
...options,
|
|
11
|
+
...extra
|
|
12
|
+
}),
|
|
13
|
+
resolve: (pubKeyZ32, options) => transport.resolve(pubKeyZ32, {
|
|
14
|
+
...options,
|
|
15
|
+
...extra
|
|
16
|
+
}),
|
|
17
|
+
describe: () => transport.describe()
|
|
18
|
+
};
|
|
19
|
+
if (transport.publishPayload) wrapped.publishPayload = (pubKeyZ32, payload, options) => transport.publishPayload(pubKeyZ32, payload, {
|
|
20
|
+
...options,
|
|
21
|
+
...extra
|
|
22
|
+
});
|
|
23
|
+
if (transport.discovery) wrapped.discovery = () => transport.discovery();
|
|
24
|
+
if (transport.subscribe) wrapped.subscribe = (listener) => transport.subscribe(listener);
|
|
25
|
+
if (transport.networkChanged) wrapped.networkChanged = () => transport.networkChanged();
|
|
26
|
+
if (transport.readAnsweredAt) wrapped.readAnsweredAt = (pubKeyZ32) => transport.readAnsweredAt(pubKeyZ32);
|
|
27
|
+
if (transport.readHeldBackFor) wrapped.readHeldBackFor = (pubKeyZ32) => transport.readHeldBackFor(pubKeyZ32);
|
|
28
|
+
if (transport.publishHeld) wrapped.publishHeld = (pubKeyZ32, options) => transport.publishHeld(pubKeyZ32, {
|
|
29
|
+
...options,
|
|
30
|
+
...extra
|
|
31
|
+
});
|
|
32
|
+
if (transport.onServerTime) wrapped.onServerTime = (listener) => transport.onServerTime(listener);
|
|
33
|
+
if (transport.configure) wrapped.configure = (options) => transport.configure(options);
|
|
34
|
+
if (transport.turnRead) wrapped.turnRead = (pubKeyZ32, options) => transport.turnRead(pubKeyZ32, options);
|
|
35
|
+
if (transport.turnPut) wrapped.turnPut = (pubKeyZ32, payload, conditions) => transport.turnPut(pubKeyZ32, payload, conditions);
|
|
36
|
+
if (transport.turnWarm) wrapped.turnWarm = () => transport.turnWarm();
|
|
37
|
+
if (transport.forget) wrapped.forget = (keys) => transport.forget(keys);
|
|
38
|
+
return wrapped;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A request the transport held back to stay within a request budget, its own or a relay's rate limit: nothing went
|
|
42
|
+
* out, and the same request goes through once the budget frees one, in about `retryInMs`. A wait, never a failure:
|
|
43
|
+
* whoever publishes tries again then, and says nothing is wrong meanwhile.
|
|
44
|
+
*/
|
|
45
|
+
var DiscoveryBudgetError = class extends Error {
|
|
46
|
+
constructor(retryInMs, message = "Discovery request budget reached; retry shortly") {
|
|
47
|
+
const errors = Error;
|
|
48
|
+
const limit = errors.stackTraceLimit;
|
|
49
|
+
if (typeof limit === "number") errors.stackTraceLimit = 0;
|
|
50
|
+
try {
|
|
51
|
+
super(message);
|
|
52
|
+
} finally {
|
|
53
|
+
if (typeof limit === "number") errors.stackTraceLimit = limit;
|
|
54
|
+
}
|
|
55
|
+
this.code = "discovery-budget";
|
|
56
|
+
this.retryInMs = retryInMs;
|
|
57
|
+
this.name = "DiscoveryBudgetError";
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/** Whether `error` is a request the budget held back (`DiscoveryBudgetError`), also from another copy of this module. */
|
|
61
|
+
function isDiscoveryBudgetError(error) {
|
|
62
|
+
return error instanceof DiscoveryBudgetError || typeof error === "object" && error !== null && error.code === "discovery-budget" && typeof error.retryInMs === "number";
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A publish no relay took (`RelayTransport`), saying whether one of them may hold the packet all the same. Not
|
|
66
|
+
* `mayBeStored` only when every relay was not asked now (its budget, its rate limit, its breaker, its network
|
|
67
|
+
* cooldown), answered that it did not take it (a 4xx), or could not be reached before the request went (a refused
|
|
68
|
+
* connection or a name that does not resolve, where the runtime says so: Node does, a browser does not), or when there
|
|
69
|
+
* is no relay at all. A relay that did not answer in time, dropped the connection, or answered with a server error
|
|
70
|
+
* may have stored it. The message is the publish's usual one.
|
|
71
|
+
*/
|
|
72
|
+
var PublishFailedError = class extends Error {
|
|
73
|
+
mayBeStored;
|
|
74
|
+
code = "publish-failed";
|
|
75
|
+
constructor(message, mayBeStored) {
|
|
76
|
+
super(message);
|
|
77
|
+
this.mayBeStored = mayBeStored;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Whether `error` is a publish that went nowhere (`PublishFailedError` with `mayBeStored` false), also from another
|
|
82
|
+
* copy of this module. Anything else (a timeout, the DHT's own failure beside the relays', a Desktop's error in
|
|
83
|
+
* words) may have left this device.
|
|
84
|
+
*/
|
|
85
|
+
function publishedNowhere(error) {
|
|
86
|
+
return typeof error === "object" && error !== null && error.code === "publish-failed" && error.mayBeStored === false;
|
|
87
|
+
}
|
|
88
|
+
/** When to try again after the budget held a request back: when it frees one, but never sooner than `min` nor later than `max`. */
|
|
89
|
+
function budgetRetryMs(error, min, max) {
|
|
90
|
+
return Math.min(max, Math.max(min, Number.isFinite(error.retryInMs) ? error.retryInMs : max));
|
|
91
|
+
}
|
|
92
|
+
var RelayBreaker = class {
|
|
93
|
+
options;
|
|
94
|
+
circuits = /* @__PURE__ */ new Map();
|
|
95
|
+
threshold;
|
|
96
|
+
baseMs;
|
|
97
|
+
maxMs;
|
|
98
|
+
allDownProbeMs;
|
|
99
|
+
now;
|
|
100
|
+
/** When a relay was last asked because every relay was left alone. */
|
|
101
|
+
lastAllDownProbe = -Infinity;
|
|
102
|
+
constructor(options = {}) {
|
|
103
|
+
this.options = options;
|
|
104
|
+
this.threshold = options.threshold ?? 3;
|
|
105
|
+
this.baseMs = options.baseMs ?? 6e4;
|
|
106
|
+
this.maxMs = options.maxMs ?? 3e5;
|
|
107
|
+
this.allDownProbeMs = options.allDownProbeMs ?? 15e3;
|
|
108
|
+
this.now = options.now ?? Date.now;
|
|
109
|
+
}
|
|
110
|
+
circuit(relay) {
|
|
111
|
+
let circuit = this.circuits.get(relay);
|
|
112
|
+
if (!circuit) this.circuits.set(relay, circuit = {
|
|
113
|
+
failures: 0,
|
|
114
|
+
trips: 0,
|
|
115
|
+
openUntil: 0,
|
|
116
|
+
probing: false
|
|
117
|
+
});
|
|
118
|
+
return circuit;
|
|
119
|
+
}
|
|
120
|
+
/** How long the relay is still left alone, in ms; 0 when a request may go (closed, or its probe is due). */
|
|
121
|
+
blockedFor(relay) {
|
|
122
|
+
const circuit = this.circuits.get(relay);
|
|
123
|
+
if (!circuit || circuit.openUntil === 0) return 0;
|
|
124
|
+
const left = circuit.openUntil - this.now();
|
|
125
|
+
if (left > 0) return left;
|
|
126
|
+
return circuit.probing ? this.baseMs : 0;
|
|
127
|
+
}
|
|
128
|
+
/** Why the relay is left alone, while it is: its rate limit or failures. */
|
|
129
|
+
blockedKind(relay) {
|
|
130
|
+
return this.blockedFor(relay) > 0 ? this.circuits.get(relay)?.kind : void 0;
|
|
131
|
+
}
|
|
132
|
+
/** A request is going to the relay now: its probe, when the wait is over. Call only when `blockedFor` is 0. */
|
|
133
|
+
begin(relay) {
|
|
134
|
+
const circuit = this.circuits.get(relay);
|
|
135
|
+
if (circuit && circuit.openUntil !== 0 && circuit.openUntil <= this.now()) circuit.probing = true;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* When every relay of `relays` is left alone for failing (none for its rate limit, none with its probe due or out),
|
|
139
|
+
* the one whose wait ends first may be asked now, once every `BREAKER_ALL_DOWN_PROBE_MS`. Returns it, or null. The
|
|
140
|
+
* caller marks the request with `beginAllDown` when it really goes (a request its budget holds back is no probe).
|
|
141
|
+
*/
|
|
142
|
+
allDownProbe(relays) {
|
|
143
|
+
const now = this.now();
|
|
144
|
+
if (!relays.length || now - this.lastAllDownProbe < this.allDownProbeMs) return null;
|
|
145
|
+
let pick = null, soonest = Infinity;
|
|
146
|
+
for (const relay of relays) {
|
|
147
|
+
const circuit = this.circuits.get(relay);
|
|
148
|
+
if (!circuit || circuit.openUntil <= now || circuit.probing || circuit.kind !== "error") return null;
|
|
149
|
+
if (circuit.openUntil < soonest) {
|
|
150
|
+
soonest = circuit.openUntil;
|
|
151
|
+
pick = relay;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return pick;
|
|
155
|
+
}
|
|
156
|
+
/** The probe `allDownProbe` named is going now: its answer closes the breaker, a failure trips it again. */
|
|
157
|
+
beginAllDown(relay) {
|
|
158
|
+
const circuit = this.circuits.get(relay);
|
|
159
|
+
if (circuit) circuit.probing = true;
|
|
160
|
+
this.lastAllDownProbe = this.now();
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* A request that went to the relay was dropped before it answered, by this client (another relay answered first), or
|
|
164
|
+
* its answer says nothing either way: a probe among them is no longer out, and the next request may probe again.
|
|
165
|
+
*/
|
|
166
|
+
cancel(relay) {
|
|
167
|
+
const circuit = this.circuits.get(relay);
|
|
168
|
+
if (circuit) circuit.probing = false;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The relay answered: whatever was wrong is over. When it was left alone and so was every other relay of `relays`
|
|
172
|
+
* (by default those that failed), for failing, the outage is over for them too: they are asked again now, not at the
|
|
173
|
+
* end of a wait of up to `BREAKER_MAX_MS` (two clients whose probes another relay answered each would share none
|
|
174
|
+
* for minutes). Their trips stay: one really down trips again at once, for longer.
|
|
175
|
+
*/
|
|
176
|
+
success(relay, relays = [...this.circuits.keys()]) {
|
|
177
|
+
const circuit = this.circuits.get(relay);
|
|
178
|
+
if (!circuit) return;
|
|
179
|
+
const recovered = circuit.openUntil !== 0;
|
|
180
|
+
this.circuits.delete(relay);
|
|
181
|
+
if (!recovered) return;
|
|
182
|
+
const others = relays.filter((r) => r !== relay).map((r) => this.circuits.get(r));
|
|
183
|
+
if (others.length && others.every((other) => !!other && other.openUntil !== 0 && other.kind === "error")) {
|
|
184
|
+
const now = this.now();
|
|
185
|
+
for (const other of others) other.openUntil = Math.min(other.openUntil, now);
|
|
186
|
+
}
|
|
187
|
+
this.options.onRecover?.(relay);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The relay failed a request; a probe that fails trips it again at once. While it is left alone and no probe is out,
|
|
191
|
+
* a failure is a request sent before it tripped (a start burst): already counted in, it trips nothing again.
|
|
192
|
+
*/
|
|
193
|
+
failure(relay, kind, reason) {
|
|
194
|
+
const circuit = this.circuit(relay);
|
|
195
|
+
if (!circuit.probing && circuit.openUntil > this.now()) return;
|
|
196
|
+
circuit.failures++;
|
|
197
|
+
circuit.kind = kind;
|
|
198
|
+
circuit.reason = reason;
|
|
199
|
+
if (circuit.probing || circuit.failures >= this.threshold) this.trip(relay, circuit);
|
|
200
|
+
}
|
|
201
|
+
trip(relay, circuit) {
|
|
202
|
+
this.lastAllDownProbe = Math.max(this.lastAllDownProbe, this.now());
|
|
203
|
+
const forMs = Math.min(this.baseMs * 2 ** circuit.trips, this.maxMs);
|
|
204
|
+
circuit.openUntil = this.now() + forMs;
|
|
205
|
+
circuit.trips++;
|
|
206
|
+
circuit.failures = 0;
|
|
207
|
+
circuit.probing = false;
|
|
208
|
+
this.options.onTrip?.(relay, circuit.reason ?? "", forMs);
|
|
209
|
+
}
|
|
210
|
+
/** How long until a relay left alone is asked again: its wait, or the next probe while every relay of `relays` is left alone. */
|
|
211
|
+
askedAgainIn(relay, relays) {
|
|
212
|
+
const wait = this.blockedFor(relay);
|
|
213
|
+
if (!relays.every((r) => this.blockedFor(r) > 0)) return wait;
|
|
214
|
+
return Math.min(wait, Math.max(0, this.lastAllDownProbe + this.allDownProbeMs - this.now()));
|
|
215
|
+
}
|
|
216
|
+
/** A new network: every relay starts fresh. */
|
|
217
|
+
reset() {
|
|
218
|
+
this.circuits.clear();
|
|
219
|
+
this.lastAllDownProbe = -Infinity;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Each relay's health, in the order given. While every one is left alone for failing, `until` is the next probe
|
|
223
|
+
* (`allDownProbe`, every 15 s), whose answer ends the wait of them all: said as the end of each one's wait, a daemon's
|
|
224
|
+
* `status` and the connection panel named a time up to 5 minutes after the relays were seen answering again.
|
|
225
|
+
*/
|
|
226
|
+
health(relays) {
|
|
227
|
+
const allDown = relays.length > 0 && relays.every((relay) => this.blockedFor(relay) > 0 && this.circuits.get(relay)?.kind === "error");
|
|
228
|
+
return relays.map((relay) => {
|
|
229
|
+
const circuit = this.circuits.get(relay);
|
|
230
|
+
if (!circuit || circuit.openUntil === 0 || this.blockedFor(relay) === 0) return {
|
|
231
|
+
relay,
|
|
232
|
+
state: "ok"
|
|
233
|
+
};
|
|
234
|
+
const until = allDown ? this.now() + this.askedAgainIn(relay, relays) : Math.max(circuit.openUntil, this.now());
|
|
235
|
+
return {
|
|
236
|
+
relay,
|
|
237
|
+
state: circuit.kind === "throttled" ? "throttled" : "failing",
|
|
238
|
+
until,
|
|
239
|
+
...circuit.reason && { reason: circuit.reason }
|
|
240
|
+
};
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
// ../core/src/linkTrace.ts
|
|
245
|
+
var sink = null;
|
|
246
|
+
/** Where the lines go besides the console; `null` removes it. */
|
|
247
|
+
function setLinkTraceSink(next) {
|
|
248
|
+
sink = next;
|
|
249
|
+
}
|
|
250
|
+
function traceLink(me, step, detail) {
|
|
251
|
+
const console_ = globalThis.__ghostlyLinkTrace;
|
|
252
|
+
if (!console_ && !sink) return;
|
|
253
|
+
const line = JSON.stringify({
|
|
254
|
+
t: Date.now(),
|
|
255
|
+
me: me.slice(0, 6),
|
|
256
|
+
step,
|
|
257
|
+
...detail
|
|
258
|
+
});
|
|
259
|
+
if (console_) console.debug(`[ghostly:link] ${line}`);
|
|
260
|
+
try {
|
|
261
|
+
sink?.(line);
|
|
262
|
+
} catch {}
|
|
263
|
+
}
|
|
264
|
+
// ../core/src/relay.ts
|
|
265
|
+
var asker = (options, write) => ({
|
|
266
|
+
background: !!options.background,
|
|
267
|
+
group: !!options.group,
|
|
268
|
+
urgent: !!options.urgent,
|
|
269
|
+
door: !!options.door,
|
|
270
|
+
write,
|
|
271
|
+
signal: !!options.signal,
|
|
272
|
+
watch: !!options.watch && !write,
|
|
273
|
+
watchWrite: !!options.watch && write,
|
|
274
|
+
departed: !!options.departed && !write,
|
|
275
|
+
awaited: !!options.awaited && !write,
|
|
276
|
+
resume: !!options.resume && !write,
|
|
277
|
+
back: !!options.back
|
|
278
|
+
});
|
|
279
|
+
/** A 1:1 chat's request: neither a group's nor a background one. */
|
|
280
|
+
var isChat = (who) => !who.background && !who.group;
|
|
281
|
+
/** A 1:1 chat's offer or answer, or its read for the answer to its offer: it may use the allowance (`SIGNALING_ALLOWANCE_SHARE`). */
|
|
282
|
+
var isChatSignal = (who) => who.signal && isChat(who);
|
|
283
|
+
/** A 1:1 chat's read of a contact that just went away: a few may use the chat's allowance (`DEPARTED_READS`). */
|
|
284
|
+
var isDepartedRead = (who) => who.departed && isChat(who);
|
|
285
|
+
/** A 1:1 chat's read of an invite this side made, its joiner due: a few may use the chat's allowance (`AWAITED_READS`). */
|
|
286
|
+
var isAwaitedRead = (who) => who.awaited && isChat(who);
|
|
287
|
+
/** A group's edge looking fast for a signal (`GROUP_BURST_MS`). */
|
|
288
|
+
var isGroupUrgentRead = (who) => who.group && who.urgent && !who.write && !who.background;
|
|
289
|
+
/**
|
|
290
|
+
* A group edge's read for a member that went away from a live session and has not shown itself back (`WATCH_SHARE`).
|
|
291
|
+
* A 1:1 chat's is not one: chats have their own reserve (`CHAT_RESERVE`), and a chat watching its contact must not wait
|
|
292
|
+
* behind the edges that watch the same contact.
|
|
293
|
+
*/
|
|
294
|
+
var isWatch = (who) => who.watch && who.group && !who.write;
|
|
295
|
+
/** A 1:1 chat's read of a contact that went away from a live session and has not shown itself back (`CHAT_WATCH_SHARE`). */
|
|
296
|
+
var isChatWatch = (who) => who.watch && isChat(who);
|
|
297
|
+
/**
|
|
298
|
+
* A group edge's request for a member that may be back: it may use the groups' allowance (`GROUP_SIGNALING_ALLOWANCE_SHARE`).
|
|
299
|
+
* So may its reads of a member that was live when this app last ran, until one is answered (`resume`): its offer waits on them.
|
|
300
|
+
*/
|
|
301
|
+
var isGroupSignal = (who) => who.group && !who.background && (who.signal || isWatch(who) || who.watchWrite || who.resume);
|
|
302
|
+
/** A group edge's read for the signal it waits for, the answer to its offer (`signal`): first on the floor (`ANSWER_READ_FIRST_MS`). */
|
|
303
|
+
var isGroupAnswerRead = (who) => isGroupSignal(who) && who.signal && !who.write;
|
|
304
|
+
/** Such a read, as the budget weighs it (`ANSWER_READ_FIRST_MS`). */
|
|
305
|
+
var ANSWER_READER = {
|
|
306
|
+
background: false,
|
|
307
|
+
group: true,
|
|
308
|
+
urgent: true,
|
|
309
|
+
door: false,
|
|
310
|
+
write: false,
|
|
311
|
+
signal: true,
|
|
312
|
+
watch: false,
|
|
313
|
+
watchWrite: false,
|
|
314
|
+
departed: false,
|
|
315
|
+
awaited: false,
|
|
316
|
+
resume: false,
|
|
317
|
+
back: false
|
|
318
|
+
};
|
|
319
|
+
/** A community member's way back to its group while it has no edge up: it may use its own allowance (`WAY_BACK_SHARE`). */
|
|
320
|
+
var isWayBack = (who) => who.back && who.group && !who.background;
|
|
321
|
+
/** Watch reads that the same other limits hold take turns together: urgent or not, background or not; the chats' on their own. */
|
|
322
|
+
var watchLane = (who) => isChat(who) ? "chat" : `${+who.urgent}${+who.background}`;
|
|
323
|
+
/** A group's read that is not a background one: an edge's, a private group's knock poll (`GROUP_RATION_WINDOW_MS`). */
|
|
324
|
+
var isGroupRead = (who) => who.group && !who.write && !who.background;
|
|
325
|
+
/**
|
|
326
|
+
* A read that takes its turn per key with the others (`READ_TURN_MS`): a background one, and a 1:1 chat's at any pace.
|
|
327
|
+
* Not a community door's bell.
|
|
328
|
+
*/
|
|
329
|
+
var isTurnRead = (who) => !who.write && !who.door && (who.background || !who.group);
|
|
330
|
+
/** A write that goes first once the budget frees a request (see `WRITE_FIRST_MS`): a chat's or a group's, never a background one. */
|
|
331
|
+
var firstWriter = (who) => !who.write || who.background ? null : who.group ? "group" : "chat";
|
|
332
|
+
/** A key's turn neither asked nor read in the last minute: it is dropped, and asking again starts a new one (`TURNS_PRUNE_MS`). */
|
|
333
|
+
var staleTurn = (turn, now) => now - Math.max(turn.askedAt, turn.servedAt) >= 6e4;
|
|
334
|
+
/**
|
|
335
|
+
* The times of `list` (oldest first) within the last `ms`. One dated after now, the device clock gone back since (an NTP
|
|
336
|
+
* step, a clock that ran ahead fixed by hand), counts as of now: as just spent, not for as long as the clock went back.
|
|
337
|
+
* Counted as they were, an hour's step back held every background read for 59 minutes, and a daemon with 50 contacts
|
|
338
|
+
* away read 4 of them in the next 15 (bug hunt, 2026-11-07).
|
|
339
|
+
* `list` itself when every time is already so (the usual case): `heldFor` reads eight lists at each request the budget
|
|
340
|
+
* holds, and copying each twice was a third of the CPU of a daemon whose 200 contacts were away.
|
|
341
|
+
*/
|
|
342
|
+
var within = (list, now, ms) => {
|
|
343
|
+
let kept = true;
|
|
344
|
+
for (const at of list) if (at > now || now - at >= ms) {
|
|
345
|
+
kept = false;
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
return kept ? list : list.map((at) => Math.min(at, now)).filter((at) => now - at < ms);
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
* Keys and when each was last set, those of the last minute: how many and the latest, without walking them all (the
|
|
352
|
+
* chats that need a relay, `chatNeeds`). Each is set at the time it is set, so the sets come oldest first; a clock
|
|
353
|
+
* gone back makes the later ones as of now.
|
|
354
|
+
*/
|
|
355
|
+
var RecentKeys = class {
|
|
356
|
+
at = /* @__PURE__ */ new Map();
|
|
357
|
+
/** Every set, oldest first, from `head`; one a later set or a delete replaced is skipped. */
|
|
358
|
+
order = [];
|
|
359
|
+
head = 0;
|
|
360
|
+
set(key, now) {
|
|
361
|
+
this.prune(now);
|
|
362
|
+
this.at.set(key, now);
|
|
363
|
+
this.order.push([key, now]);
|
|
364
|
+
}
|
|
365
|
+
delete(key) {
|
|
366
|
+
this.at.delete(key);
|
|
367
|
+
}
|
|
368
|
+
size(now) {
|
|
369
|
+
this.prune(now);
|
|
370
|
+
return this.at.size;
|
|
371
|
+
}
|
|
372
|
+
/** When the key set last was, -Infinity with none in the last minute. */
|
|
373
|
+
latest(now) {
|
|
374
|
+
this.prune(now);
|
|
375
|
+
while (this.order.length > this.head) {
|
|
376
|
+
const [key, at] = this.order[this.order.length - 1];
|
|
377
|
+
if (this.at.get(key) === at) return at;
|
|
378
|
+
this.order.pop();
|
|
379
|
+
}
|
|
380
|
+
return -Infinity;
|
|
381
|
+
}
|
|
382
|
+
/** The keys set in the last minute, oldest first. */
|
|
383
|
+
*entries(now) {
|
|
384
|
+
this.prune(now);
|
|
385
|
+
for (let i = this.head; i < this.order.length; i++) if (this.at.get(this.order[i][0]) === this.order[i][1]) yield this.order[i];
|
|
386
|
+
}
|
|
387
|
+
/** Drops the keys set a minute ago or more. */
|
|
388
|
+
prune(now) {
|
|
389
|
+
if (this.order.length > this.head && now < this.order[this.order.length - 1][1]) {
|
|
390
|
+
for (const [key, at] of this.at) this.at.set(key, Math.min(at, now));
|
|
391
|
+
this.order = [...this.at].sort((a, b) => a[1] - b[1]);
|
|
392
|
+
this.head = 0;
|
|
393
|
+
}
|
|
394
|
+
while (this.head < this.order.length) {
|
|
395
|
+
const [key, at] = this.order[this.head];
|
|
396
|
+
if (this.at.get(key) === at) {
|
|
397
|
+
if (now - at < 6e4) break;
|
|
398
|
+
this.at.delete(key);
|
|
399
|
+
}
|
|
400
|
+
this.head++;
|
|
401
|
+
}
|
|
402
|
+
if (this.head > 64 && this.head * 2 > this.order.length) {
|
|
403
|
+
this.order = this.order.slice(this.head);
|
|
404
|
+
this.head = 0;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
/**
|
|
409
|
+
* Public Pkarr relays. They are generic Pkarr infrastructure (an HTTP bridge to
|
|
410
|
+
* the Mainline DHT), not a Ghostly backend: they only ever see signed,
|
|
411
|
+
* encrypted packets. Every client uses this list: the browser clients read and
|
|
412
|
+
* write through it, Ghostly Desktop writes to it (so browser contacts see its
|
|
413
|
+
* packets) and reads from it only when "Also use Pkarr relays" is on. Adding a
|
|
414
|
+
* relay is one line here; see docs/RELAYS.md for how one is chosen.
|
|
415
|
+
*/
|
|
416
|
+
var DEFAULT_RELAYS = [
|
|
417
|
+
"https://pkarr.pubky.org",
|
|
418
|
+
"https://pkarr.pubky.app",
|
|
419
|
+
"https://pkarr.miguelmedeiros.dev"
|
|
420
|
+
];
|
|
421
|
+
/**
|
|
422
|
+
* Relay lists that were the defaults once: a profile that still has one gets today's defaults. When a relay
|
|
423
|
+
* joins `DEFAULT_RELAYS`, the list as it was goes here, so profiles on the old defaults move with it.
|
|
424
|
+
*/
|
|
425
|
+
var PREVIOUS_DEFAULT_RELAYS = [["https://pkarr.pubky.org", "https://pkarr.pubky.app"]];
|
|
426
|
+
/** The defaults for `relays` when it is one of the old default lists, `relays` itself otherwise. */
|
|
427
|
+
function currentRelays(relays) {
|
|
428
|
+
const same = (a, b) => a.length === b.length && a.every((relay, i) => relay === b[i]);
|
|
429
|
+
return PREVIOUS_DEFAULT_RELAYS.some((old) => same(old, relays)) ? [...DEFAULT_RELAYS] : relays;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Of the background share, what background writes may spend in a minute on a relay (10 of the 20): the rest is kept
|
|
433
|
+
* for background reads. Those writes are mostly the upkeep of chats whose contacts are away (the link's heartbeat, the
|
|
434
|
+
* DHT floor's keep-alive, the capability record), about one a minute for every two chats. A profile with 200 such chats
|
|
435
|
+
* spent the share on them, and on one relay 147 of its 200 contacts were not read once in 25 minutes: a contact back
|
|
436
|
+
* then went unnoticed (bug hunt, 2026-11-01). Upkeep held back goes when a request of this share frees.
|
|
437
|
+
*/
|
|
438
|
+
var BACKGROUND_WRITES_SHARE = 1 / 2;
|
|
439
|
+
/**
|
|
440
|
+
* A link's write the budget refused (its presence, its offer, its answer) goes first when the minute
|
|
441
|
+
* frees a request: reads on that relay wait this long after the refusal. A link that polls fast for a
|
|
442
|
+
* peer while its offer waits would otherwise take every request the minute frees, and the offer (the
|
|
443
|
+
* only thing that peer is waiting for) would go out only once the polling slowed down, half a minute later.
|
|
444
|
+
*/
|
|
445
|
+
var WRITE_FIRST_MS = 5e3;
|
|
446
|
+
/** The most of a relay's minute the chats' reserve takes, however many chats need it: the rest stays for everything else. */
|
|
447
|
+
var CHAT_RESERVE_MOST_SHARE = 5 / 6;
|
|
448
|
+
/**
|
|
449
|
+
* A 1:1 chat's signaling (a new offer or answer, and its reads for the answer to its offer, `signal`) may go over a
|
|
450
|
+
* relay's minute by this fraction of it: 6 on a relay of 30, 12 on one of 60, 1 on a relay of 5 (relay.pkarr.org and
|
|
451
|
+
* pkarr.miguelmedeiros.dev allow 10), and never more than that in any minute; its reads leave the last of those to a write. Everything else still stops at
|
|
452
|
+
* the limit. The minute before a chat leaves DHT only can be spent by then (the pairing, the texts over the DHT), and
|
|
453
|
+
* its offer or answer, the one thing the contact waits for, then waited 40 s for requests to age out while the
|
|
454
|
+
* contact's attempt gave up: the matrix's extension chat stayed "On DHT · retrying live" 46 times in 120 (2026-09-30).
|
|
455
|
+
*/
|
|
456
|
+
var SIGNALING_ALLOWANCE_SHARE = 1 / 5;
|
|
457
|
+
/**
|
|
458
|
+
* A group edge's requests for a member that may be back may go over a relay's minute by this fraction of it (4 on a
|
|
459
|
+
* relay of 30, 8 on one of 60, none on a relay of 5): its reads of a member that went away and its packet
|
|
460
|
+
* written meanwhile, which that member reads first when it is back (`watch`), its offer or answer, and its reads for
|
|
461
|
+
* the answer to its offer (`signal`). Its reads leave the last of those to a write and go
|
|
462
|
+
* one at a time (`GROUP_OVER_PACE_MS`), so the allowance lasts until the member is back, and the read that finds its
|
|
463
|
+
* offer still has the answer behind it. Less than a chat's (`SIGNALING_ALLOWANCE_SHARE`), which keeps room past the
|
|
464
|
+
* groups'. A private group's admin that had just let members in and seen them restart had spent its minute in 30 s;
|
|
465
|
+
* a member back then, with no other member online to carry its offer, had it read only when the minute freed: 18 to
|
|
466
|
+
* 42 s later, one round in four of the member-back runs (2026-10-03).
|
|
467
|
+
*
|
|
468
|
+
* It is also theirs whatever the chats do: while the edges' signaling has had fewer than that many requests on a relay
|
|
469
|
+
* in the last minute, neither the limit, nor the chats' own allowance past it, nor their reserve (`CHAT_RESERVE`) holds
|
|
470
|
+
* it back. Its reads leave the last of it to a write there too. A daemon with 30 chats and 5 groups, back after a
|
|
471
|
+
* restart, had its chats signal for 332 s: they kept their reserve, their allowance took what the limit left, and the
|
|
472
|
+
* edges got nothing. By then their attempts had failed and backed off, and the group members were reachable again 13
|
|
473
|
+
* to 25 min after the restart (CLI bug hunt, 2026-10-08).
|
|
474
|
+
*
|
|
475
|
+
* An edge back after a restart reads its member before it offers (`resume`), and those reads are on the floor too: as
|
|
476
|
+
* plain reads, they waited behind the chats as before, and the edges never got to signal. The same daemon had its first
|
|
477
|
+
* member back 71 to 367 s after the restart, and group texts sent in its first minutes were read 2 to 7 min late (CLI
|
|
478
|
+
* bug hunt, 2026-10-14).
|
|
479
|
+
*/
|
|
480
|
+
var GROUP_SIGNALING_ALLOWANCE_SHARE = 2 / 15;
|
|
481
|
+
/**
|
|
482
|
+
* A community member's way back to its group while it has no edge up (its readings of the beacon, its request in a hub's
|
|
483
|
+
* lobby: `back`) may go past everything that holds a group's request back by this fraction of a relay's minute (6 on a
|
|
484
|
+
* relay of 30, 12 on one of 60, 1 on a relay of 5), its own: not the edges' (`GROUP_SIGNALING_ALLOWANCE_SHARE`),
|
|
485
|
+
* and what it takes counts there alone, so it shortens no one else's minute or allowance. Its reads leave the last of it
|
|
486
|
+
* to a write. A web app opened with 30 chats whose contacts were online and 15 groups whose edges all offered at once:
|
|
487
|
+
* the edges' offers and reads for their members spent the groups' allowance in its first second, the chats' reserve
|
|
488
|
+
* and their offers still waiting held the rest, and its 5 communities read their beacon (who the hubs are) a minute
|
|
489
|
+
* later, or not in 3 minutes; 2 of them were not back in 6 (bug hunt, 2026-10-14). Sized for 5 communities' way back
|
|
490
|
+
* in the first minute on the default relays: a reading of each beacon, and a read and a write in a hub's lobby (a write
|
|
491
|
+
* goes to every relay). A member asks it only while it has no edge up in that community.
|
|
492
|
+
*
|
|
493
|
+
* At worst a relay then sees, in one minute, its limit, the chats' allowance past it, the edges', this one and the edges'
|
|
494
|
+
* answer reads' (`ANSWER_READ_ALLOWANCE`): 49 on a relay of 30 (pkarr.pubky.org allows 50 an address,
|
|
495
|
+
* `x-ratelimit-limit`), 95 on pkarr.pubky.app's 60 (it allows 1000) and 7 on a relay of 5 (relay.pkarr.org and
|
|
496
|
+
* pkarr.miguelmedeiros.dev allow 10). A share raised past those limits gets 429s (docs/TRANSPORTS.md, "The default list").
|
|
497
|
+
*/
|
|
498
|
+
var WAY_BACK_SHARE = 1 / 5;
|
|
499
|
+
/** Past the limit, a group's reads within its allowance go no more often than this on a relay. */
|
|
500
|
+
var GROUP_OVER_PACE_MS = 5e3;
|
|
501
|
+
/**
|
|
502
|
+
* Groups' urgent reads (their edges looking fast for a signal) take at most a quarter of a relay's minute in any this
|
|
503
|
+
* long, so that they never spend it in a burst. An app back after a restart has every edge offering and looking fast at
|
|
504
|
+
* once; the members at the other end answer only once they notice its old sessions went (about 20 s with
|
|
505
|
+
* node-datachannel). On the default relays the edges spent the groups' share of the minute in those seconds, and read
|
|
506
|
+
* nothing for 40 s more: a private group's edges were live again 75 to 110 s after a restart (bug hunt r5a, 2026-09-29).
|
|
507
|
+
*/
|
|
508
|
+
var GROUP_BURST_MS = 15e3;
|
|
509
|
+
/**
|
|
510
|
+
* A group's edges' reads of members that went away from a live session and have not shown themselves back (`watch`: a
|
|
511
|
+
* link looks for them closely for two minutes, `WATCH_PEER_MS`) take at most this share of a relay's requests, whatever
|
|
512
|
+
* their number, spread over the minute (`GROUP_RATION_WINDOW_MS`): 2 in any 10 s on a relay of 30. Under it, the key
|
|
513
|
+
* read longest ago goes first. A member alone in a private group whose three other members left at once watched each of
|
|
514
|
+
* them every 2 s: those reads and the admin's knock poll spent both relays' minute, the member back first had its offer
|
|
515
|
+
* read only when the minute freed, 25 to 44 s on, and its edge came up then (bug hunt r12, measured again 2026-10-03).
|
|
516
|
+
*/
|
|
517
|
+
var WATCH_SHARE = 1 / 3;
|
|
518
|
+
/**
|
|
519
|
+
* 1:1 chats' reads of contacts that went away from a live session and have not shown themselves back (`watch`) take at
|
|
520
|
+
* most this share of a relay's requests the same way, on their own (a chat never waits behind the edges that watch the
|
|
521
|
+
* same contact): 3 in any 10 s on a relay of 30, more than one contact's fast poll asks for. The key read longest ago
|
|
522
|
+
* goes first. Not the few reads of a contact that just went away past the limit (`DEPARTED_READS`). Ten contacts that
|
|
523
|
+
* left a CLI daemon at once were read every 2 s each: those reads spent both relays' minute in the 10 s after their
|
|
524
|
+
* goodbyes, when none could be back yet, then the daemon read nobody, nor answered anyone, for 45 s, and the chats with
|
|
525
|
+
* no HyperDHT listener at the daemon were live again 42 to 51 s after their contacts were back (CLI bug hunt, 2026-10-14).
|
|
526
|
+
*/
|
|
527
|
+
var CHAT_WATCH_SHARE = 1 / 2;
|
|
528
|
+
/**
|
|
529
|
+
* A key a group reads that asked within this long and was not read since keeps its turn: while one read longer ago
|
|
530
|
+
* waits, the others wait too (`WATCH_SHARE`, `GROUP_RATION_WINDOW_MS`). One that stops asking loses it.
|
|
531
|
+
*/
|
|
532
|
+
var WATCH_TURN_MS = 5e3;
|
|
533
|
+
/**
|
|
534
|
+
* Background reads, and a 1:1 chat's at any pace, take turns per key the same way: while a key read longer ago asked
|
|
535
|
+
* within this long and was not read since, a read of another key waits (a chat's, at a pace that is not the background
|
|
536
|
+
* one, only for another chat's key). A saved chat that is not live reads its contact every 30 s in the
|
|
537
|
+
* background, and one the budget refused read again only at its next poll. A request frees 60 s after the one that took
|
|
538
|
+
* it, which is the poll after next of the chat that took it: the same chats took every request that freed. A daemon with
|
|
539
|
+
* 30 chats and 5 groups, back after a restart while its groups signaled, read some contacts once in 10 min, and those
|
|
540
|
+
* chats stayed down although both sides were online and offering (CLI bug hunt, 2026-10-12). A link's background read
|
|
541
|
+
* the budget refused reads again when it frees a request (`LinkSession`), so the key whose turn it is is there to take it.
|
|
542
|
+
* A chat not live after a restart looks fast while its offer is out, and those reads took no turn: the budget answered
|
|
543
|
+
* them from the packet read before, every 2 to 8 s for 10 minutes, while the chats that were live already, the groups'
|
|
544
|
+
* edges and the writes took every request that freed. A quarter of a daemon's 30 chats read their contact once in 10
|
|
545
|
+
* minutes, their contacts online and offering, and showed them offline at 6 minutes (CLI bug hunt, 2026-10-13).
|
|
546
|
+
*/
|
|
547
|
+
var READ_TURN_MS = 5e3;
|
|
548
|
+
/**
|
|
549
|
+
* The keys' turns asked and read over a minute ago are dropped at most this often: walking every key on each ask cost in
|
|
550
|
+
* proportion to the chats, 12-14% of an idle web app's script with 200 chats.
|
|
551
|
+
*/
|
|
552
|
+
var TURNS_PRUNE_MS = 1e3;
|
|
553
|
+
/**
|
|
554
|
+
* A 1:1 chat's read the budget held back this long without a break is starved: it goes before the writes that wait
|
|
555
|
+
* (`WRITE_FIRST_MS`) and past the background share, within the minute, the key held back longest first. A daemon back
|
|
556
|
+
* after a restart spends its whole minute on writes (each chat's and edge's offer, its packets after the attempt), which
|
|
557
|
+
* go first, and requests free in bursts as the minute's first requests age out: every burst went to the writes that
|
|
558
|
+
* waited, and the reads of the chats that were not in an attempt never had their turn (CLI restart lab, 2026-10-13).
|
|
559
|
+
*/
|
|
560
|
+
var READ_STARVED_MS = 6e4;
|
|
561
|
+
/** How long a read that let an older one go first waits before it asks again. */
|
|
562
|
+
var TURN_WAIT_MS = 1e3;
|
|
563
|
+
/**
|
|
564
|
+
* Once a group's read found a relay's minute full, the groups' reads there are spread over the minute for a minute
|
|
565
|
+
* (`GROUP_RATION_MS`): at most a sixth of it in any 10 s (5 on a relay of 30), the key read longest ago first. A sliding
|
|
566
|
+
* minute that demand keeps full hands out its requests in bursts: the minute is spent in 25 to 35 s as requests age
|
|
567
|
+
* out, then nothing frees for the rest of it. A private group's admin (its knock poll every 2 s while its link is new)
|
|
568
|
+
* whose members left and came back read nothing for 18 to 42 s at a time, and the member back first waited that long
|
|
569
|
+
* for its edge (bug hunt r12, measured again on 2026-10-03). Writes are not counted: they go first anyway.
|
|
570
|
+
*/
|
|
571
|
+
var GROUP_RATION_WINDOW_MS = 1e4;
|
|
572
|
+
var GROUP_RATION_MS = 6e4;
|
|
573
|
+
/**
|
|
574
|
+
* While a link polls fast (an `urgent` read: its peer, or the peer's offer or answer, is due any moment) within this
|
|
575
|
+
* long, background requests (a community's periodic looks) are held to `BACKGROUND_WHILE_SIGNALING` a minute on each
|
|
576
|
+
* relay rather than their usual share. A fast poll reads every 2 s, one relay after the other.
|
|
577
|
+
*/
|
|
578
|
+
var SIGNALING_WINDOW_MS = 1e4;
|
|
579
|
+
/** A relay that missed a key's newest packet gets it (`catchUpLater`) no sooner than this, and tries again this often. */
|
|
580
|
+
var CATCH_UP_MIN_MS = 1e3;
|
|
581
|
+
var CATCH_UP_RETRY_MS = 5e3;
|
|
582
|
+
/** A catch-up put waits behind every link's request: a background write. A group edge's offer or answer goes as its own (`putEverywhere`). */
|
|
583
|
+
var CATCH_UP_ASKER = {
|
|
584
|
+
background: true,
|
|
585
|
+
group: false,
|
|
586
|
+
urgent: false,
|
|
587
|
+
door: false,
|
|
588
|
+
write: true,
|
|
589
|
+
signal: false,
|
|
590
|
+
watch: false,
|
|
591
|
+
watchWrite: false,
|
|
592
|
+
departed: false,
|
|
593
|
+
awaited: false,
|
|
594
|
+
resume: false,
|
|
595
|
+
back: false
|
|
596
|
+
};
|
|
597
|
+
/** Keys whose last relay is remembered for their next read (`resolve`): a profile's links and lookups, with room. */
|
|
598
|
+
var READ_TURNS_KEPT = 512;
|
|
599
|
+
/** A relay that fails at the network level is left alone for this long. */
|
|
600
|
+
var NETWORK_ERROR_COOLDOWN_MS = 2e4;
|
|
601
|
+
/**
|
|
602
|
+
* Relays whose limit per address is far from the others': this client's share of theirs, a minute, in place of
|
|
603
|
+
* `REQUESTS_PER_MINUTE`. relay.pkarr.org (not a default, see docs/RELAYS.md) and pkarr.miguelmedeiros.dev (a default
|
|
604
|
+
* since 2026-10-17) allow 10 (`x-ratelimit-limit`), which a browser cannot read without CORS exposing it: half of it
|
|
605
|
+
* leaves room for a second app behind the same address. pkarr.pubky.app allows 1000 (pkarr.pubky.org, on the same address,
|
|
606
|
+
* 50): 60 of ours leaves room for many peers behind one address, and a page that pairs three contacts in a minute,
|
|
607
|
+
* about ten requests a relay each, still has requests to spare there (2026-09-28). Writes go to every relay and
|
|
608
|
+
* count once one took them; reads go to a relay with requests left.
|
|
609
|
+
*/
|
|
610
|
+
var RELAY_REQUESTS_PER_MINUTE = {
|
|
611
|
+
"https://relay.pkarr.org": 5,
|
|
612
|
+
"https://pkarr.pubky.app": 60,
|
|
613
|
+
"https://pkarr.miguelmedeiros.dev": 5
|
|
614
|
+
};
|
|
615
|
+
/**
|
|
616
|
+
* A read asks the next relay too when the one it asked has not answered within this long, and takes the first good
|
|
617
|
+
* answer. Measured on 2026-10-03 from a home connection: a relay hands over a packet it holds in 0.25 s (0.7 s on a new
|
|
618
|
+
* connection), and one that has to fetch the packet from the DHT first takes 2.7 s. Reads used to wait for a relay's
|
|
619
|
+
* answer before asking the next one, up to the 10 s timeout: a relay that answered slowly or not at all (pkarr.pubky.org
|
|
620
|
+
* on 2026-10-03) made every read of a pairing that slow, and the pairing never finished.
|
|
621
|
+
*/
|
|
622
|
+
var HEDGE_MS = 1500;
|
|
623
|
+
/**
|
|
624
|
+
* The hedge delay for a key whose last read found nothing (404): a relay takes 3 to 5 s to say so (it asks the DHT
|
|
625
|
+
* every time), and a hedge at `HEDGE_MS` would double every poll of a key nobody has published yet.
|
|
626
|
+
*/
|
|
627
|
+
var HEDGE_MISSING_MS = 6e3;
|
|
628
|
+
/**
|
|
629
|
+
* A packet that took longer than this to arrive, or a hedge lost to a relay that answered with the packet, is slow: it
|
|
630
|
+
* counts against the relay's breaker like a failure (three in a row trip it, logged as "slow"), while another relay is
|
|
631
|
+
* there to ask. A 404 says nothing either way: every relay is slow to give one.
|
|
632
|
+
*/
|
|
633
|
+
var SLOW_MS = 3e3;
|
|
634
|
+
/** A relay found slow goes after the others in every read for this long, unless it answers fast meanwhile. */
|
|
635
|
+
var SLOW_DEMOTE_MS = 6e4;
|
|
636
|
+
/** How long each source has to answer a read or a put of the turn record (WISP 06 § Publishing and reading). */
|
|
637
|
+
var TURN_SOURCE_TIMEOUT_MS = 8e3;
|
|
638
|
+
/** A relay's refusals of a turn put: someone else wrote (409 an older or equal packet, 412 the condition, 428 a put in flight). */
|
|
639
|
+
var TURN_REFUSALS = [
|
|
640
|
+
409,
|
|
641
|
+
412,
|
|
642
|
+
428
|
|
643
|
+
];
|
|
644
|
+
/** How many times a payload signed here goes again past a relay's later copy (`putRedated`): three puts at most. */
|
|
645
|
+
var PAYLOAD_REDATES = 2;
|
|
646
|
+
/** On a relay's 404: the sequence of an item the DHT holds under the key that is no signed packet. */
|
|
647
|
+
var INVALID_PACKET_SEQ_HEADER = "pkarr-invalid-signed-packet-seq";
|
|
648
|
+
/** What `answer` says, or false when it has not said within `ms`. */
|
|
649
|
+
function answeredWithin(answer, ms) {
|
|
650
|
+
let timer;
|
|
651
|
+
const late = new Promise((resolve) => {
|
|
652
|
+
timer = setTimeout(() => resolve(false), ms);
|
|
653
|
+
});
|
|
654
|
+
return Promise.race([answer, late]).finally(() => clearTimeout(timer));
|
|
655
|
+
}
|
|
656
|
+
function normalizeRelayUrl(input) {
|
|
657
|
+
try {
|
|
658
|
+
const url = new URL(input.trim());
|
|
659
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
|
|
660
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
|
|
661
|
+
} catch {
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Why a line of a Pkarr relay list is not a relay `normalizeRelayUrl` keeps, with the line as typed; null when it is
|
|
667
|
+
* one. A host alone or another scheme (`relay.example.org`, `wss://…`) was meant as an https:// relay.
|
|
668
|
+
*/
|
|
669
|
+
function relayUrlProblem(input) {
|
|
670
|
+
const line = input.trim();
|
|
671
|
+
if (normalizeRelayUrl(line)) return null;
|
|
672
|
+
return looksLikeHost(line) || looksLikeHost(line.replace(/^[a-z][a-z\d+.-]*:\/\//i, "")) ? `Use an https:// relay address: ${line}` : `Not a relay address: ${line}`;
|
|
673
|
+
}
|
|
674
|
+
/** No space, and a dot after the first character, before the last one and before any `/` or `:`; linear, for a long pasted line. */
|
|
675
|
+
function looksLikeHost(text) {
|
|
676
|
+
if (/\s/.test(text)) return false;
|
|
677
|
+
const slash = text.search(/[/:]/);
|
|
678
|
+
const dot = text.indexOf(".", 1);
|
|
679
|
+
return dot > 0 && dot < text.length - 1 && (slash < 0 || dot < slash);
|
|
680
|
+
}
|
|
681
|
+
/** A relay the breaker leaves alone for failing (`RelayBreaker`): nothing was asked of it now. */
|
|
682
|
+
var RelayLeftAloneError = class extends Error {};
|
|
683
|
+
/**
|
|
684
|
+
* A relay a put did not get an answer from. `sent` false: the request never left (its network cooling down after a
|
|
685
|
+
* failure, or a connection that could not be made, `connectFailed`); true: it may have reached the relay.
|
|
686
|
+
*/
|
|
687
|
+
var RelayNoAnswerError = class extends Error {
|
|
688
|
+
sent;
|
|
689
|
+
constructor(message, sent) {
|
|
690
|
+
super(message);
|
|
691
|
+
this.sent = sent;
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
/** What Node says of a connection it could not make: the request never left. A browser's TypeError says nothing either way. */
|
|
695
|
+
var CONNECT_FAILURES = /* @__PURE__ */ new Set([
|
|
696
|
+
"ECONNREFUSED",
|
|
697
|
+
"ENOTFOUND",
|
|
698
|
+
"EAI_AGAIN",
|
|
699
|
+
"EHOSTUNREACH",
|
|
700
|
+
"ENETUNREACH",
|
|
701
|
+
"ENETDOWN",
|
|
702
|
+
"EHOSTDOWN",
|
|
703
|
+
"EADDRNOTAVAIL",
|
|
704
|
+
"UND_ERR_CONNECT_TIMEOUT"
|
|
705
|
+
]);
|
|
706
|
+
/** A fetch that failed before its request left this device: Node's `cause.code` (each of an AggregateError's, for several addresses). */
|
|
707
|
+
function connectFailed(error) {
|
|
708
|
+
const cause = error?.cause;
|
|
709
|
+
if (!cause || typeof cause !== "object") return false;
|
|
710
|
+
if (Array.isArray(cause.errors) && cause.errors.length) return cause.errors.every((e) => CONNECT_FAILURES.has(e?.code));
|
|
711
|
+
return CONNECT_FAILURES.has(cause.code);
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* The error a publish ends with after a second round dated past a relay's later copy failed too: one that may have
|
|
715
|
+
* stored the first round's packet still may, whatever the second round's relays said.
|
|
716
|
+
*/
|
|
717
|
+
function laterRound(first, later) {
|
|
718
|
+
return publishedNowhere(later) && !publishedNowhere(first) ? new PublishFailedError(later.message, true) : later;
|
|
719
|
+
}
|
|
720
|
+
/** A packet date no further ahead of this clock than readers take (`newerPacket`): one this client may date past. */
|
|
721
|
+
function withinSkew(timestamp) {
|
|
722
|
+
return timestamp <= BigInt(Date.now() + PKARR_FUTURE_SKEW_MS) * 1000n;
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* A read the relays failed (or left alone for failing before), when its caller has another path to ask
|
|
726
|
+
* (`RelayTransport.resolve` with `keptOnFailure` false): `kept` is the newest packet the relays gave for the key before.
|
|
727
|
+
*/
|
|
728
|
+
var RelaysFailedError = class extends Error {
|
|
729
|
+
kept;
|
|
730
|
+
constructor(kept) {
|
|
731
|
+
super("No Pkarr relay reachable");
|
|
732
|
+
this.kept = kept;
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
var RelayTransport = class {
|
|
736
|
+
relays;
|
|
737
|
+
timeoutMs;
|
|
738
|
+
fetchFn;
|
|
739
|
+
timeListeners = /* @__PURE__ */ new Set();
|
|
740
|
+
lastTimestamp = /* @__PURE__ */ new Map();
|
|
741
|
+
newest = /* @__PURE__ */ new Map();
|
|
742
|
+
/**
|
|
743
|
+
* The latest time of any packet read under a key, whoever dated it: the next packet this client puts there is dated
|
|
744
|
+
* past it (`nextTimestamp`). A key is not always one writer's with one clock: a group's lobby, beacon and knock
|
|
745
|
+
* records are written by every member, an inviter warms its contact's key with an empty packet, and this device's
|
|
746
|
+
* own clock may have been set back since it last wrote. A relay and the DHT keep only the packet dated latest, so
|
|
747
|
+
* one dated by this clock alone, when it is behind, was refused (409) or silently never read, until this clock
|
|
748
|
+
* caught up with the other one: minutes, or an hour. Only up to `PKARR_FUTURE_SKEW_MS` ahead of this clock
|
|
749
|
+
* (`withinSkew`): a packet one holder of a shared key dated further ahead must not date every later one of this
|
|
750
|
+
* device past it (past 64 bits, unsignable; else ahead of every reader's clock, never taken over one dated now).
|
|
751
|
+
*/
|
|
752
|
+
seenTimestamp = /* @__PURE__ */ new Map();
|
|
753
|
+
/**
|
|
754
|
+
* The last packet this client put under a key that a relay took. A read the budget holds back answers the newer of
|
|
755
|
+
* it and the last one read: a hub that wrote its own beacon entry since it last read the beacon must not see that
|
|
756
|
+
* entry as old as the one it replaced (it would stop counting itself at its community's door until its next write).
|
|
757
|
+
*/
|
|
758
|
+
written = /* @__PURE__ */ new Map();
|
|
759
|
+
coolingDown = /* @__PURE__ */ new Map();
|
|
760
|
+
networkCooldown = /* @__PURE__ */ new Map();
|
|
761
|
+
cursor = 0;
|
|
762
|
+
/** Per key, the relay that answered its last read: the next read of the key starts at the one after it (`resolve`). */
|
|
763
|
+
lastReadFrom = /* @__PURE__ */ new Map();
|
|
764
|
+
spent = /* @__PURE__ */ new Map();
|
|
765
|
+
spentBackground = /* @__PURE__ */ new Map();
|
|
766
|
+
/** Background writes on each relay in the last minute (`BACKGROUND_WRITES_SHARE`). */
|
|
767
|
+
spentBackgroundWrites = /* @__PURE__ */ new Map();
|
|
768
|
+
/** Groups' urgent reads on each relay in the last `GROUP_BURST_MS`: edges' reads for an answer apart (`ANSWER_READ_FIRST_MS`). */
|
|
769
|
+
spentGroupUrgent = /* @__PURE__ */ new Map();
|
|
770
|
+
spentAnswerUrgent = /* @__PURE__ */ new Map();
|
|
771
|
+
/** Watch reads on each relay in the last `GROUP_RATION_WINDOW_MS` (`WATCH_SHARE`). */
|
|
772
|
+
spentWatch = /* @__PURE__ */ new Map();
|
|
773
|
+
/** 1:1 chats' watch reads on each relay in the last `GROUP_RATION_WINDOW_MS` (`CHAT_WATCH_SHARE`). */
|
|
774
|
+
spentChatWatch = /* @__PURE__ */ new Map();
|
|
775
|
+
/** Groups' reads past the limit on each relay in the last `GROUP_OVER_PACE_MS` (`GROUP_SIGNALING_ALLOWANCE_SHARE`). */
|
|
776
|
+
spentGroupOver = /* @__PURE__ */ new Map();
|
|
777
|
+
/** Group edges' signaling requests on each relay in the last minute: their floor (`GROUP_SIGNALING_ALLOWANCE_SHARE`). */
|
|
778
|
+
spentGroupSignal = /* @__PURE__ */ new Map();
|
|
779
|
+
/** Community members' ways back on each relay in the last minute: their allowance (`WAY_BACK_SHARE`). */
|
|
780
|
+
spentWayBack = /* @__PURE__ */ new Map();
|
|
781
|
+
/** Edges' reads for an answer that only their own allowance let through, on each relay in the last minute (`ANSWER_READ_ALLOWANCE`). */
|
|
782
|
+
spentAnswerAllowance = /* @__PURE__ */ new Map();
|
|
783
|
+
/** Per contact, its departed reads past the limit in the last minute, every relay together (`DEPARTED_READS`). */
|
|
784
|
+
departedOver = /* @__PURE__ */ new Map();
|
|
785
|
+
/** The awaited reads past the limit in the last minute, every invite and every relay together (`AWAITED_READS`). */
|
|
786
|
+
awaitedOver = [];
|
|
787
|
+
/** When an awaited invite's read last asked: the departed reads leave it room for a minute (`AWAITED_READS`). */
|
|
788
|
+
awaitedAt = -Infinity;
|
|
789
|
+
/**
|
|
790
|
+
* Per key a group reads (not in the background), when a read of it last went out and when it last asked: whose turn
|
|
791
|
+
* it is under the watch share (`WATCH_SHARE`) and while the groups' reads are rationed (`GROUP_RATION_WINDOW_MS`). And
|
|
792
|
+
* per key a chat watches, whose turn it is under theirs (`CHAT_WATCH_SHARE`).
|
|
793
|
+
*/
|
|
794
|
+
readTurns = /* @__PURE__ */ new Map();
|
|
795
|
+
/** Per key read in turn (`isTurnRead`), when a read of it last went out and when it last asked (`READ_TURN_MS`). */
|
|
796
|
+
keyTurns = /* @__PURE__ */ new Map();
|
|
797
|
+
/** When `readTurns` and `keyTurns` last dropped their stale turns (`TURNS_PRUNE_MS`). */
|
|
798
|
+
turnsPrunedAt = /* @__PURE__ */ new Map();
|
|
799
|
+
/** Groups' reads (not background ones) on each relay in the last `GROUP_RATION_WINDOW_MS`. */
|
|
800
|
+
spentGroupReads = /* @__PURE__ */ new Map();
|
|
801
|
+
/** When a group's read last found each relay's minute full: its reads there are rationed for `GROUP_RATION_MS`. */
|
|
802
|
+
groupsFullAt = /* @__PURE__ */ new Map();
|
|
803
|
+
/** When a link's write was last refused on each relay (`chat <relay>`, `group <relay>`), while it waits for the budget. */
|
|
804
|
+
writeWaiting = /* @__PURE__ */ new Map();
|
|
805
|
+
/** Per relay, the keys whose edges' reads for an answer it last refused, and when, until one is read (`ANSWER_READ_FIRST_MS`). */
|
|
806
|
+
answerReadWaiting = /* @__PURE__ */ new Map();
|
|
807
|
+
/**
|
|
808
|
+
* Per key a 1:1 chat asks for: when it last read urgently (for the next minute, groups leave it `CHAT_RESERVE` on
|
|
809
|
+
* every relay) and when the budget last refused it on each relay (the same, on that relay). A read of the key at a
|
|
810
|
+
* slow pace (the chat is live, or back in the background) drops it: `CHAT_RESERVE`. Per relay, the later of the two
|
|
811
|
+
* for each key (`chatNeed`); `chatUrgentAt` keeps the urgent ones for a relay not asked yet.
|
|
812
|
+
*/
|
|
813
|
+
chatNeeds = /* @__PURE__ */ new Map();
|
|
814
|
+
chatUrgentAt = new RecentKeys();
|
|
815
|
+
/** When a link (a chat's or a group's) last asked urgently: background requests yield for `SIGNALING_WINDOW_MS`. */
|
|
816
|
+
urgentAt = -Infinity;
|
|
817
|
+
/** Timestamp of the last packet sent to each relay, per key: the compare-and-swap value for the next one. */
|
|
818
|
+
lastPut = /* @__PURE__ */ new Map();
|
|
819
|
+
/** Per relay and key, the newest packet that relay held back for its budget while another relay took it (`catchUpLater`). */
|
|
820
|
+
catchUp = /* @__PURE__ */ new Map();
|
|
821
|
+
catchUpTimer = null;
|
|
822
|
+
/**
|
|
823
|
+
* Per key whose reads the budget held back since its last read went out: when a request frees for it
|
|
824
|
+
* (`readHeldBackFor`), how many times in a row, and since when (`READ_STARVED_MS`).
|
|
825
|
+
*/
|
|
826
|
+
heldBack = /* @__PURE__ */ new Map();
|
|
827
|
+
/** When the relays last answered a read of each key, for `FRESH_READ_MS`. */
|
|
828
|
+
readAt = /* @__PURE__ */ new Map();
|
|
829
|
+
freshReadMs;
|
|
830
|
+
perMinute;
|
|
831
|
+
/** `requestsPerMinute` was given: it bounds every relay, one with its own share (`RELAY_REQUESTS_PER_MINUTE`) too. */
|
|
832
|
+
perMinuteGiven;
|
|
833
|
+
backgroundPerMinute;
|
|
834
|
+
backgroundWhileSignaling;
|
|
835
|
+
breaker;
|
|
836
|
+
/** The relay the last read was answered by. */
|
|
837
|
+
lastRelay = null;
|
|
838
|
+
hedgeMs;
|
|
839
|
+
/** Until when each relay found slow (`SLOW_MS`) goes after the others in a read (`readOrder`). */
|
|
840
|
+
slowUntil = /* @__PURE__ */ new Map();
|
|
841
|
+
/** Per relay, when each of this client's requests still waiting for its answer went out (`unanswering`). */
|
|
842
|
+
waiting = /* @__PURE__ */ new Map();
|
|
843
|
+
/** When each relay last answered a request of this client, whatever it said (`unanswering`). */
|
|
844
|
+
answeredAt = /* @__PURE__ */ new Map();
|
|
845
|
+
/** Keys whose last read was answered 404: their next read hedges later (`HEDGE_MISSING_MS`). */
|
|
846
|
+
missing = /* @__PURE__ */ new Set();
|
|
847
|
+
listeners = /* @__PURE__ */ new Set();
|
|
848
|
+
constructor(options = {}) {
|
|
849
|
+
this.relays = [];
|
|
850
|
+
this.setRelays(options.relays ?? DEFAULT_RELAYS);
|
|
851
|
+
this.timeoutMs = options.timeoutMs ?? 1e4;
|
|
852
|
+
this.fetchFn = options.fetch ?? ((...args) => fetch(...args));
|
|
853
|
+
this.perMinute = options.requestsPerMinute ?? 30;
|
|
854
|
+
this.freshReadMs = options.freshReadMs ?? 500;
|
|
855
|
+
this.hedgeMs = options.hedgeMs ?? 1500;
|
|
856
|
+
this.perMinuteGiven = options.requestsPerMinute !== void 0;
|
|
857
|
+
this.backgroundPerMinute = this.perMinute === 30 ? 20 : Math.ceil(this.perMinute * 2 / 3);
|
|
858
|
+
this.backgroundWhileSignaling = this.perMinute === 30 ? 5 : Math.ceil(this.perMinute / 6);
|
|
859
|
+
const log = options.log ?? ((line) => console.info(`ghostly: ${line}`));
|
|
860
|
+
this.breaker = new RelayBreaker({
|
|
861
|
+
...options.breaker,
|
|
862
|
+
onTrip: (relay, reason, forMs) => {
|
|
863
|
+
log(`relay ${new URL(relay).host} ${reason === "no answer" ? "did not answer" : reason === "slow" ? "answered too slowly" : `failed (${reason})`}; asked again in ${Math.round(forMs / 1e3)} s`);
|
|
864
|
+
options.breaker?.onTrip?.(relay, reason, forMs);
|
|
865
|
+
this.changed("tripped");
|
|
866
|
+
},
|
|
867
|
+
onRecover: (relay) => {
|
|
868
|
+
log(`relay ${new URL(relay).host} answers again`);
|
|
869
|
+
options.breaker?.onRecover?.(relay);
|
|
870
|
+
this.changed("recovered");
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
/** How reads go and how each relay is doing, for the connection panel. */
|
|
875
|
+
discovery() {
|
|
876
|
+
const relays = this.breaker.health(this.relays).map((health) => {
|
|
877
|
+
const limited = this.rateLimitedFor(health.relay);
|
|
878
|
+
return health.state === "ok" && limited > 0 ? {
|
|
879
|
+
...health,
|
|
880
|
+
state: "throttled",
|
|
881
|
+
until: Date.now() + limited,
|
|
882
|
+
reason: "rate limited (429)"
|
|
883
|
+
} : health;
|
|
884
|
+
});
|
|
885
|
+
return {
|
|
886
|
+
path: this.lastRelay ? {
|
|
887
|
+
via: "relay",
|
|
888
|
+
relay: this.lastRelay
|
|
889
|
+
} : null,
|
|
890
|
+
relays
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
/** Back online on some network: relays that failed (or rate limited this address) on the old one are asked again. */
|
|
894
|
+
networkChanged() {
|
|
895
|
+
this.breaker.reset();
|
|
896
|
+
this.networkCooldown.clear();
|
|
897
|
+
this.coolingDown.clear();
|
|
898
|
+
this.slowUntil.clear();
|
|
899
|
+
this.changed("recovered");
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* `PkarrTransport.forget`: the packets and dates this client kept under these keys go, on every relay (`written`,
|
|
903
|
+
* `newest`, `lastTimestamp`, `seenTimestamp`, `lastPut`). A spare invite replaced unused left them here for its two keys
|
|
904
|
+
* for good: an idle CLI daemon gained 8 keys an hour (343 in `written` and 690 `lastPut` entries after a day). The maps
|
|
905
|
+
* that bound themselves (`readAt`, `lastReadFrom`, `heldBack`, `missing`, the turns) and the catch-ups, which go once
|
|
906
|
+
* put, are left as they are. A later put under one of these keys goes as a key's first.
|
|
907
|
+
*/
|
|
908
|
+
forget(keys) {
|
|
909
|
+
const gone = new Set(keys);
|
|
910
|
+
if (gone.size === 0) return;
|
|
911
|
+
for (const key of gone) for (const perKey of [
|
|
912
|
+
this.lastTimestamp,
|
|
913
|
+
this.seenTimestamp,
|
|
914
|
+
this.newest,
|
|
915
|
+
this.written
|
|
916
|
+
]) perKey.delete(key);
|
|
917
|
+
for (const slot of this.lastPut.keys()) if (gone.has(slot.slice(slot.lastIndexOf(" ") + 1))) this.lastPut.delete(slot);
|
|
918
|
+
}
|
|
919
|
+
/** Called when a relay trips or recovers. */
|
|
920
|
+
subscribe(listener) {
|
|
921
|
+
this.listeners.add(listener);
|
|
922
|
+
return () => this.listeners.delete(listener);
|
|
923
|
+
}
|
|
924
|
+
changed(change) {
|
|
925
|
+
for (const listener of this.listeners) try {
|
|
926
|
+
listener(change);
|
|
927
|
+
} catch {}
|
|
928
|
+
}
|
|
929
|
+
/** What the relay did, for its breaker. */
|
|
930
|
+
answered(relay, failure, method) {
|
|
931
|
+
if (failure) {
|
|
932
|
+
this.breaker.failure(relay, failure.kind, failure.reason);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
if (method) this.networkCooldown.delete(`${method} ${relay}`);
|
|
936
|
+
this.breaker.success(relay, this.relays);
|
|
937
|
+
}
|
|
938
|
+
setRelays(relays) {
|
|
939
|
+
const normalized = relays.map(normalizeRelayUrl).filter((r) => r !== null);
|
|
940
|
+
this.relays = [...new Set(normalized)];
|
|
941
|
+
}
|
|
942
|
+
describe() {
|
|
943
|
+
return {
|
|
944
|
+
protocol: "Pkarr relays (HTTP) → Mainline DHT (BEP44)",
|
|
945
|
+
relays: [...this.relays]
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Publishes to every relay. Peers may be configured with different relay
|
|
950
|
+
* sets; writing everywhere keeps the overlap warm, the DHT covers the rest.
|
|
951
|
+
* Returns once one relay took the packet: the others finish in the background.
|
|
952
|
+
*/
|
|
953
|
+
async publish(identity, records, options = {}) {
|
|
954
|
+
if (this.relays.length === 0) throw new PublishFailedError("No Pkarr relays configured", false);
|
|
955
|
+
await this.publishDated(identity, records, (key, payload, timestamp, conflict) => this.putEverywhere(key, payload, timestamp, options, conflict), options);
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* The time the next packet under a key is dated: this clock's, and past every packet this client put or read there.
|
|
959
|
+
* BEP44 sequence numbers must strictly increase, and the clock that dated the last one may not be this one's.
|
|
960
|
+
*/
|
|
961
|
+
nextTimestamp(pubKeyZ32) {
|
|
962
|
+
const now = BigInt(Date.now()) * 1000n;
|
|
963
|
+
const last = this.lastTimestamp.get(pubKeyZ32) ?? 0n, seen = this.seenTimestamp.get(pubKeyZ32) ?? 0n;
|
|
964
|
+
const floor = last > seen ? last : seen;
|
|
965
|
+
const timestamp = now > floor ? now : floor + 1n;
|
|
966
|
+
this.lastTimestamp.set(pubKeyZ32, timestamp);
|
|
967
|
+
return timestamp;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Signs the records, dated by `nextTimestamp`, and hands the payload to `put` (the relays; the CLI adds the DHT).
|
|
971
|
+
* When every relay's budget (or rate limit) holds this asker back, it throws that wait before signing anything
|
|
972
|
+
* (`publishWait`): a daemon at its budget signed about 500 packets a minute that never went out. `elsewhere`: `put`
|
|
973
|
+
* also takes the packet where no budget holds it back (the CLI's DHT), so it is signed whatever the relays say.
|
|
974
|
+
* `put` calls `conflict` for a relay that answered 409: it holds a packet under this key dated at or after this one,
|
|
975
|
+
* which this client has not read (an inviter's warm packet under the key it gave its contact, from a clock ahead of
|
|
976
|
+
* this one). That packet is read from that relay, and the records go again dated just past it, once. Without this
|
|
977
|
+
* the packet was refused until this device's clock passed the other one's, however long that took, and on a relay
|
|
978
|
+
* that did take it, readers still preferred the later-dated empty packet of another relay. `read`: how to learn the
|
|
979
|
+
* held packet's date where it is not a relay to ask (the CLI's DHT, whose nodes answer 302 for a later-dated packet).
|
|
980
|
+
*/
|
|
981
|
+
async publishDated(identity, records, put, options = {}, again = false, elsewhere = false) {
|
|
982
|
+
const key = identity.pubKeyZ32;
|
|
983
|
+
if (!again && !elsewhere) {
|
|
984
|
+
const held = this.publishWait(key, asker(options, true));
|
|
985
|
+
if (held) throw new DiscoveryBudgetError(held.wait, `Publish held back on every relay: ${held.reasons.join("; ")}`);
|
|
986
|
+
const refused = this.refusedEverywhere(key, asker(options, true));
|
|
987
|
+
if (refused) throw refused;
|
|
988
|
+
}
|
|
989
|
+
const timestamp = this.nextTimestamp(key);
|
|
990
|
+
let redated;
|
|
991
|
+
const conflict = (relay, read) => {
|
|
992
|
+
if (again || redated) return;
|
|
993
|
+
redated = (async () => {
|
|
994
|
+
const held = read ? await read().catch(() => null) : await this.heldAt(relay, key, asker(options, true));
|
|
995
|
+
if (held === null || held < timestamp || !withinSkew(held)) return false;
|
|
996
|
+
traceLink(key, "publish-redated", {
|
|
997
|
+
aheadMs: Number((held - timestamp) / 1000n),
|
|
998
|
+
relay
|
|
999
|
+
});
|
|
1000
|
+
if (this.lastTimestamp.get(key) !== timestamp) return true;
|
|
1001
|
+
await this.publishDated(identity, records, put, options, true, elsewhere);
|
|
1002
|
+
return true;
|
|
1003
|
+
})();
|
|
1004
|
+
redated.catch(() => {});
|
|
1005
|
+
};
|
|
1006
|
+
try {
|
|
1007
|
+
await put(key, createRelayPayload(identity, records, timestamp), timestamp, conflict);
|
|
1008
|
+
} catch (error) {
|
|
1009
|
+
const pending = redated;
|
|
1010
|
+
if (!pending || !await pending.catch((later) => {
|
|
1011
|
+
throw laterRound(error, later);
|
|
1012
|
+
})) throw error;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
/** `PkarrTransport.publishHeld`: `publishDated`'s check before signing, for a payload signed elsewhere in this process. */
|
|
1016
|
+
publishHeld(pubKeyZ32, options = {}) {
|
|
1017
|
+
if (this.relays.length === 0) return null;
|
|
1018
|
+
const held = this.publishWait(pubKeyZ32, asker(options, true));
|
|
1019
|
+
return held ? new DiscoveryBudgetError(held.wait, `Publish held back on every relay: ${held.reasons.join("; ")}`) : null;
|
|
1020
|
+
}
|
|
1021
|
+
/**
|
|
1022
|
+
* How long until some relay takes a publish of this asker, and why each holds it back; null when one would now. Walks the relays as `put` does,
|
|
1023
|
+
* without asking any: a relay the budget holds back gets its refused `take` (the asker's write then waits first in
|
|
1024
|
+
* line, as when `put` is refused), one rate limiting or throttling this address is a wait, one left alone for failing
|
|
1025
|
+
* is none. Null too when a relay fails another way (its network cooling down): the publish then goes, and fails, as
|
|
1026
|
+
* before, and when no relay is held back at all.
|
|
1027
|
+
*/
|
|
1028
|
+
publishWait(pubKeyZ32, who) {
|
|
1029
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1030
|
+
let wait = Infinity;
|
|
1031
|
+
const held = [], reasons = [];
|
|
1032
|
+
for (const relay of this.relays) {
|
|
1033
|
+
if (relay !== probe && this.networkCoolingDown(relay, "PUT")) return null;
|
|
1034
|
+
const limited = this.rateLimitedFor(relay);
|
|
1035
|
+
if (limited > 0) {
|
|
1036
|
+
wait = Math.min(wait, limited);
|
|
1037
|
+
reasons.push(`${relay}: Discovery relay is cooling down after a 429; retry shortly`);
|
|
1038
|
+
continue;
|
|
1039
|
+
}
|
|
1040
|
+
const blocked = relay === probe ? 0 : this.breaker.blockedFor(relay);
|
|
1041
|
+
if (blocked > 0) {
|
|
1042
|
+
if (this.breaker.blockedKind(relay) === "throttled") {
|
|
1043
|
+
wait = Math.min(wait, blocked);
|
|
1044
|
+
reasons.push(`${relay}: Discovery relay is throttling this address; retry shortly`);
|
|
1045
|
+
} else reasons.push(`${relay} is left alone after failing`);
|
|
1046
|
+
continue;
|
|
1047
|
+
}
|
|
1048
|
+
if (this.heldFor(relay, who, Date.now(), pubKeyZ32) <= 0) return null;
|
|
1049
|
+
held.push(relay);
|
|
1050
|
+
const left = this.heldFor(relay, who);
|
|
1051
|
+
wait = Math.min(wait, left);
|
|
1052
|
+
reasons.push(`${relay}: discovery budget reached; retry in ${Math.ceil(left / 1e3)} s`);
|
|
1053
|
+
}
|
|
1054
|
+
if (wait === Infinity) return null;
|
|
1055
|
+
for (const relay of held) this.take(relay, who, pubKeyZ32);
|
|
1056
|
+
return {
|
|
1057
|
+
wait,
|
|
1058
|
+
reasons
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* A publish that every relay would refuse now without being asked (its network cooling down, left alone after failing,
|
|
1063
|
+
* its rate limit or this asker's budget), and not as a wait alone (`publishWait` says those): the failure `putEverywhere`
|
|
1064
|
+
* would end in, in the same words, said before the packet is signed and dated. Null when some relay would be asked. With
|
|
1065
|
+
* every relay out of reach on a page that is online, each chat signed a packet every few seconds that went nowhere: ~58
|
|
1066
|
+
* a second at 300 chats (bug hunt, 2026-11-12).
|
|
1067
|
+
*/
|
|
1068
|
+
refusedEverywhere(pubKeyZ32, who) {
|
|
1069
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1070
|
+
const now = Date.now();
|
|
1071
|
+
const reasons = [], held = [];
|
|
1072
|
+
for (const relay of this.relays) {
|
|
1073
|
+
const refused = this.refusal(relay, relay === probe);
|
|
1074
|
+
if (refused) {
|
|
1075
|
+
reasons.push(String(refused));
|
|
1076
|
+
continue;
|
|
1077
|
+
}
|
|
1078
|
+
if (this.heldFor(relay, who, now, pubKeyZ32) <= 0) return null;
|
|
1079
|
+
held.push(relay);
|
|
1080
|
+
reasons.push(String(new DiscoveryBudgetError(this.heldFor(relay, who))));
|
|
1081
|
+
}
|
|
1082
|
+
for (const relay of held) this.take(relay, who, pubKeyZ32);
|
|
1083
|
+
return new PublishFailedError(`Publish failed on every relay: ${reasons.join("; ")}`, false);
|
|
1084
|
+
}
|
|
1085
|
+
/** What `put` refuses a request to this relay with before its budget is asked; null when it gets that far. */
|
|
1086
|
+
refusal(relay, probe) {
|
|
1087
|
+
if (!probe && this.networkCoolingDown(relay, "PUT")) return new RelayNoAnswerError(`${relay} did not answer a moment ago; retry shortly`, false);
|
|
1088
|
+
const limited = this.rateLimitedFor(relay);
|
|
1089
|
+
if (limited > 0) return new DiscoveryBudgetError(limited, "Discovery relay is cooling down after a 429; retry shortly");
|
|
1090
|
+
const blocked = probe ? 0 : this.breaker.blockedFor(relay);
|
|
1091
|
+
if (blocked > 0) {
|
|
1092
|
+
if (this.breaker.blockedKind(relay) === "throttled") return new DiscoveryBudgetError(blocked, "Discovery relay is throttling this address; retry shortly");
|
|
1093
|
+
return new RelayLeftAloneError(`${relay} is left alone after failing; asked again in ${Math.max(1, Math.ceil(this.breaker.askedAgainIn(relay, this.relays) / 1e3))} s`);
|
|
1094
|
+
}
|
|
1095
|
+
return null;
|
|
1096
|
+
}
|
|
1097
|
+
/** One relay's packet for a key, read now: its time, kept for `nextTimestamp`. Null when it has none or does not answer. */
|
|
1098
|
+
async heldAt(relay, pubKeyZ32, who) {
|
|
1099
|
+
if (!this.take(relay, who, pubKeyZ32)) return null;
|
|
1100
|
+
try {
|
|
1101
|
+
let payload;
|
|
1102
|
+
if (!(await this.request(`${relay}/${pubKeyZ32}`, { method: "GET" }, async (r, signal) => {
|
|
1103
|
+
if (r.ok && r.status !== 404) payload = await readRelayBody(r, signal);
|
|
1104
|
+
})).ok || !payload) return null;
|
|
1105
|
+
const packet = parseRelayPayload(pubKeyZ32, payload);
|
|
1106
|
+
this.sawTimestamp(pubKeyZ32, packet.timestampMicros);
|
|
1107
|
+
return packet.timestampMicros;
|
|
1108
|
+
} catch {
|
|
1109
|
+
return null;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
/** A packet under this key was read (on a relay, or by the CLI on the DHT): its time, for `nextTimestamp`. */
|
|
1113
|
+
sawTimestamp(pubKeyZ32, timestamp) {
|
|
1114
|
+
if (withinSkew(timestamp) && timestamp > (this.seenTimestamp.get(pubKeyZ32) ?? 0n)) this.seenTimestamp.set(pubKeyZ32, timestamp);
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Puts a payload `publishDated` just signed, dated `timestamp`, on every relay. Its signature is not checked: it was
|
|
1118
|
+
* made here (`publishPayload` checks one signed elsewhere, to read its sequence number). `conflict`: as `publishDated`
|
|
1119
|
+
* gives it.
|
|
1120
|
+
*/
|
|
1121
|
+
async putSigned(pubKeyZ32, payload, timestamp, options = {}, conflict) {
|
|
1122
|
+
if (this.relays.length === 0) throw new PublishFailedError("No Pkarr relays configured", false);
|
|
1123
|
+
await this.putEverywhere(pubKeyZ32, payload, timestamp, options, conflict);
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Puts a payload signed elsewhere (a did:dht document), byte for byte, on every relay; one signed here says its date
|
|
1127
|
+
* (`signedAt`) and is not checked again. `conflict`: as `publishDated` gives it.
|
|
1128
|
+
*/
|
|
1129
|
+
async publishPayload(pubKeyZ32, payload, options = {}, conflict) {
|
|
1130
|
+
if (this.relays.length === 0) throw new PublishFailedError("No Pkarr relays configured", false);
|
|
1131
|
+
const seq = options.signedAt ?? openRelayPayload(pubKeyZ32, payload).seq;
|
|
1132
|
+
if (options.signedAt === void 0 || !options.redate) return this.putEverywhere(pubKeyZ32, payload, seq, options, conflict);
|
|
1133
|
+
await this.putRedated(pubKeyZ32, payload, seq, options, PAYLOAD_REDATES);
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* A payload every relay's budget held back while another path took it (the CLI's DHT): the publish is out, and each
|
|
1137
|
+
* relay gets it once its budget frees a request for the same asker (`catchUpLater`), unless a newer packet of the key
|
|
1138
|
+
* goes there first. Retried by the link instead, each try was signed and put on the DHT again (1-1.6 puts a minute per
|
|
1139
|
+
* chat at budget, not one per 4 min). `timestamp`: its date when signed here; read from the payload otherwise.
|
|
1140
|
+
*/
|
|
1141
|
+
putLater(pubKeyZ32, payload, timestamp, options = {}) {
|
|
1142
|
+
const at = timestamp ?? options.signedAt ?? openRelayPayload(pubKeyZ32, payload).seq;
|
|
1143
|
+
try {
|
|
1144
|
+
this.written.set(pubKeyZ32, readOwnRelayPayload(pubKeyZ32, payload));
|
|
1145
|
+
} catch {}
|
|
1146
|
+
for (const relay of this.relays) this.catchUpLater(relay, pubKeyZ32, payload, at, asker(options, true));
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* A payload signed in this process (a group edge's offer or answer, `CarriedTransport`) put on every relay, past what
|
|
1150
|
+
* a relay that answers 409 holds: that one is read, and the records go again at once, signed by `options.redate` dated
|
|
1151
|
+
* just past it, `left` times at most. A packet put here is the key's latest too: a publish dated here next
|
|
1152
|
+
* (`nextTimestamp`, the goodbye after the edge stops) goes past it. Before, the 409 failed the publish (or, when another
|
|
1153
|
+
* relay took it, left the relay's copy in place), the link tried again 4 s on with its clock's date, refused as long as
|
|
1154
|
+
* the relay's copy was later, and an edge started again in the same millisecond lost its new offer to its last one.
|
|
1155
|
+
*/
|
|
1156
|
+
async putRedated(key, payload, timestamp, options, left) {
|
|
1157
|
+
if (timestamp > (this.lastTimestamp.get(key) ?? 0n)) this.lastTimestamp.set(key, timestamp);
|
|
1158
|
+
let redated;
|
|
1159
|
+
const conflict = (relay) => {
|
|
1160
|
+
if (left <= 0 || redated) return;
|
|
1161
|
+
redated = (async () => {
|
|
1162
|
+
const held = await this.heldAt(relay, key, asker(options, true));
|
|
1163
|
+
if (held === null || held < timestamp || !withinSkew(held)) return false;
|
|
1164
|
+
if (this.lastTimestamp.get(key) !== timestamp) return true;
|
|
1165
|
+
const again = options.redate(held);
|
|
1166
|
+
if (!again) return true;
|
|
1167
|
+
traceLink(key, "publish-redated", {
|
|
1168
|
+
aheadMs: Number((held - timestamp) / 1000n),
|
|
1169
|
+
relay
|
|
1170
|
+
});
|
|
1171
|
+
await this.putRedated(key, again.payload, again.signedAt, {
|
|
1172
|
+
...options,
|
|
1173
|
+
signedAt: again.signedAt
|
|
1174
|
+
}, left - 1);
|
|
1175
|
+
return true;
|
|
1176
|
+
})();
|
|
1177
|
+
redated.catch(() => {});
|
|
1178
|
+
};
|
|
1179
|
+
try {
|
|
1180
|
+
await this.putEverywhere(key, payload, timestamp, options, conflict);
|
|
1181
|
+
} catch (error) {
|
|
1182
|
+
const pending = redated;
|
|
1183
|
+
if (!pending || !await pending.catch((later) => {
|
|
1184
|
+
throw laterRound(error, later);
|
|
1185
|
+
})) throw error;
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
/**
|
|
1189
|
+
* Puts the packet on every relay and settles as soon as one of them took it. A relay that is slow to
|
|
1190
|
+
* answer (one that stores the packet and answers the PUT only at the timeout) must not hold a link's
|
|
1191
|
+
* presence, offer or answer back: the rest finish in the background, their outcomes still counted.
|
|
1192
|
+
* When no relay takes it, this waits for all of them to say why.
|
|
1193
|
+
*/
|
|
1194
|
+
putEverywhere(pubKeyZ32, payload, timestamp, options, conflict) {
|
|
1195
|
+
this.readAt.delete(pubKeyZ32);
|
|
1196
|
+
this.missing.delete(pubKeyZ32);
|
|
1197
|
+
for (const relay of this.relays) this.catchUp.delete(`${relay} ${pubKeyZ32}`);
|
|
1198
|
+
const waitingBefore = new Map(this.writeWaiting);
|
|
1199
|
+
const writer = firstWriter(asker(options, true));
|
|
1200
|
+
const noWriteWaits = () => {
|
|
1201
|
+
if (!writer) return;
|
|
1202
|
+
for (const relay of this.relays) {
|
|
1203
|
+
const key = `${writer} ${relay}`;
|
|
1204
|
+
if (this.writeWaiting.get(key) !== waitingBefore.get(key)) this.writeWaiting.delete(key);
|
|
1205
|
+
}
|
|
1206
|
+
};
|
|
1207
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1208
|
+
let first;
|
|
1209
|
+
const edgeSignal = isGroupSignal(asker(options, true));
|
|
1210
|
+
const failed = /* @__PURE__ */ new Set();
|
|
1211
|
+
const nowhere = this.relays.map(() => false);
|
|
1212
|
+
const puts = this.relays.map(async (relay, i) => {
|
|
1213
|
+
const slot = `${relay} ${pubKeyZ32}`;
|
|
1214
|
+
const previous = this.lastPut.get(slot);
|
|
1215
|
+
this.lastPut.set(slot, timestamp);
|
|
1216
|
+
if (first && isChat(asker(options, true)) && this.spareHeldFor(relay, Date.now()) > 0 && await answeredWithin(first, 3e3)) {
|
|
1217
|
+
nowhere[i] = true;
|
|
1218
|
+
throw new DiscoveryBudgetError(Math.max(CATCH_UP_MIN_MS, this.spareHeldFor(relay, Date.now())));
|
|
1219
|
+
}
|
|
1220
|
+
let response;
|
|
1221
|
+
try {
|
|
1222
|
+
response = await this.put(relay, pubKeyZ32, payload, previous, asker(options, true), relay === probe, (ok) => {
|
|
1223
|
+
first ??= ok;
|
|
1224
|
+
});
|
|
1225
|
+
if (response.status === 412) response = await this.put(relay, pubKeyZ32, payload, void 0, asker(options, true));
|
|
1226
|
+
} catch (error) {
|
|
1227
|
+
if (!isDiscoveryBudgetError(error) && !(error instanceof RelayLeftAloneError)) failed.add(relay);
|
|
1228
|
+
nowhere[i] = isDiscoveryBudgetError(error) || error instanceof RelayLeftAloneError || error instanceof RelayNoAnswerError && !error.sent;
|
|
1229
|
+
throw error;
|
|
1230
|
+
}
|
|
1231
|
+
nowhere[i] = response.status >= 400 && response.status < 500;
|
|
1232
|
+
if (response.status >= 500 || response.status === 428) failed.add(relay);
|
|
1233
|
+
if (response.status === 409) conflict?.(relay);
|
|
1234
|
+
if (response.status === 429) throw new DiscoveryBudgetError(this.coolDown(relay, response), `${relay} responded 429; retry shortly`);
|
|
1235
|
+
if (!response.ok) throw new Error(`${relay} responded ${response.status}`);
|
|
1236
|
+
});
|
|
1237
|
+
const settled = Promise.allSettled(puts);
|
|
1238
|
+
return new Promise((resolve, reject) => {
|
|
1239
|
+
let accepted = false;
|
|
1240
|
+
for (const put of puts) put.then(() => {
|
|
1241
|
+
if (accepted) return;
|
|
1242
|
+
accepted = true;
|
|
1243
|
+
noWriteWaits();
|
|
1244
|
+
try {
|
|
1245
|
+
this.written.set(pubKeyZ32, readOwnRelayPayload(pubKeyZ32, payload));
|
|
1246
|
+
} catch {}
|
|
1247
|
+
resolve();
|
|
1248
|
+
}, () => {});
|
|
1249
|
+
settled.then((results) => {
|
|
1250
|
+
if (accepted) {
|
|
1251
|
+
noWriteWaits();
|
|
1252
|
+
results.forEach((r, i) => {
|
|
1253
|
+
if (r.status !== "rejected") return;
|
|
1254
|
+
if (isDiscoveryBudgetError(r.reason) || edgeSignal && failed.has(this.relays[i])) this.catchUpLater(this.relays[i], pubKeyZ32, payload, timestamp, edgeSignal ? asker(options, true) : CATCH_UP_ASKER);
|
|
1255
|
+
});
|
|
1256
|
+
return;
|
|
1257
|
+
}
|
|
1258
|
+
const held = results.map((r) => r.status === "rejected" && isDiscoveryBudgetError(r.reason) ? r.reason : null);
|
|
1259
|
+
const leftAlone = (r) => r.status === "rejected" && r.reason instanceof RelayLeftAloneError;
|
|
1260
|
+
const waits = held.filter((e) => e !== null);
|
|
1261
|
+
const reasons = results.map((r) => r.status === "rejected" ? String(r.reason) : "").join("; ");
|
|
1262
|
+
if (waits.length === results.length) reject(new DiscoveryBudgetError(Math.min(...waits.map((e) => e.retryInMs)), `Publish held back on every relay: ${reasons}`));
|
|
1263
|
+
else if (waits.length && results.every((r, i) => held[i] !== null || leftAlone(r))) reject(new DiscoveryBudgetError(Math.min(...waits.map((e) => e.retryInMs)), `Publish held back on every relay not left alone: ${reasons}`));
|
|
1264
|
+
else reject(new PublishFailedError(`Publish failed on every relay: ${reasons}`, !nowhere.every(Boolean)));
|
|
1265
|
+
});
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Public relays rate limit by IP (120 requests a minute at the time of
|
|
1270
|
+
* writing) and several peers may share one address, so a poll costs one
|
|
1271
|
+
* request: one relay is asked, the next one when this one fails, or when it
|
|
1272
|
+
* has not answered within `HEDGE_MS` (a hedge: the first good answer wins and
|
|
1273
|
+
* the other request is dropped). The newest validly signed packet seen so far
|
|
1274
|
+
* wins, which also covers a relay that is still serving an older cached copy.
|
|
1275
|
+
*
|
|
1276
|
+
* In turn per key: a key's next read starts at the relay after the one that answered its last read. A relay that
|
|
1277
|
+
* does not have a packet yet (a publish that reached only the other relay, one rate limited there) answers a key's
|
|
1278
|
+
* reads at most once in a row. With one turn for all keys, a link that read its peer between two other reads landed
|
|
1279
|
+
* on the same relay every time: a restarted member's offer, only on `pkarr.pubky.app`, went unread for 12 to 60 s
|
|
1280
|
+
* while the member at the other end read `pkarr.pubky.org` (bug hunt r5a, 2026-09-29). A relay found slow lately
|
|
1281
|
+
* (`SLOW_DEMOTE_MS`) goes after the others, whatever the turn.
|
|
1282
|
+
*
|
|
1283
|
+
* A hedge spends a second request, so it goes only where the budget has requests to spare (`hedgeRoom`), never for a
|
|
1284
|
+
* background read (nobody waits on it), and later for a key whose last read found nothing (`HEDGE_MISSING_MS`).
|
|
1285
|
+
*
|
|
1286
|
+
* `keptOnFailure` false: a read the relays failed throws `RelaysFailedError` with the packet they gave last, in place
|
|
1287
|
+
* of handing that back as their answer (a CLI asks the DHT then, `RelaysAndDht`).
|
|
1288
|
+
*/
|
|
1289
|
+
async resolve(pubKeyZ32, options = {}, keptOnFailure = true) {
|
|
1290
|
+
if (this.relays.length === 0) throw new Error("No Pkarr relays configured");
|
|
1291
|
+
if (options.background && !options.urgent && !options.group) this.chatNeedLapsed(pubKeyZ32);
|
|
1292
|
+
const readAt = this.readAt.get(pubKeyZ32), age = readAt === void 0 ? Infinity : Date.now() - readAt;
|
|
1293
|
+
if (age >= 0 && age < this.freshReadMs) return this.newest.get(pubKeyZ32) ?? null;
|
|
1294
|
+
const order = this.readOrder(pubKeyZ32);
|
|
1295
|
+
const probe = this.breaker.allDownProbe(this.relays);
|
|
1296
|
+
let budgetWait = Infinity, down = false;
|
|
1297
|
+
const who = asker(options, false);
|
|
1298
|
+
const tried = /* @__PURE__ */ new Set();
|
|
1299
|
+
let hedgeAfter = options.background ? Infinity : this.missing.has(pubKeyZ32) ? Math.max(this.hedgeMs, HEDGE_MISSING_MS) : this.hedgeMs;
|
|
1300
|
+
/**
|
|
1301
|
+
* The next relay to ask, its request taken from its budget, or null. A hedge (`hedge`) goes only to a relay with
|
|
1302
|
+
* requests to spare; one passed over for that is still there if the relay asked fails.
|
|
1303
|
+
*/
|
|
1304
|
+
const next = (hedge) => {
|
|
1305
|
+
for (const relay of order) {
|
|
1306
|
+
if (tried.has(relay)) continue;
|
|
1307
|
+
if (relay !== probe && this.networkCoolingDown(relay, "GET")) {
|
|
1308
|
+
tried.add(relay);
|
|
1309
|
+
down = true;
|
|
1310
|
+
continue;
|
|
1311
|
+
}
|
|
1312
|
+
const limited = this.rateLimitedFor(relay);
|
|
1313
|
+
if (limited > 0) {
|
|
1314
|
+
tried.add(relay);
|
|
1315
|
+
budgetWait = Math.min(budgetWait, limited);
|
|
1316
|
+
continue;
|
|
1317
|
+
}
|
|
1318
|
+
const blocked = relay === probe ? 0 : this.breaker.blockedFor(relay);
|
|
1319
|
+
if (blocked > 0) {
|
|
1320
|
+
tried.add(relay);
|
|
1321
|
+
if (this.breaker.blockedKind(relay) === "throttled") budgetWait = Math.min(budgetWait, blocked);
|
|
1322
|
+
else down = true;
|
|
1323
|
+
continue;
|
|
1324
|
+
}
|
|
1325
|
+
if (hedge && (this.heldFor(relay, who, Date.now(), pubKeyZ32) > 0 || !this.hedgeRoom(relay))) continue;
|
|
1326
|
+
tried.add(relay);
|
|
1327
|
+
if (!this.take(relay, who, pubKeyZ32)) {
|
|
1328
|
+
budgetWait = Math.min(budgetWait, this.heldFor(relay, who, Date.now(), pubKeyZ32));
|
|
1329
|
+
continue;
|
|
1330
|
+
}
|
|
1331
|
+
if (relay === probe) this.breaker.beginAllDown(relay);
|
|
1332
|
+
else this.breaker.begin(relay);
|
|
1333
|
+
return relay;
|
|
1334
|
+
}
|
|
1335
|
+
return null;
|
|
1336
|
+
};
|
|
1337
|
+
const asked = /* @__PURE__ */ new Map();
|
|
1338
|
+
let lastAsked = 0;
|
|
1339
|
+
const ask = (hedge) => {
|
|
1340
|
+
const relay = next(hedge);
|
|
1341
|
+
if (!relay) return false;
|
|
1342
|
+
asked.set(relay, this.readFromRelay(relay, pubKeyZ32));
|
|
1343
|
+
lastAsked = Date.now();
|
|
1344
|
+
return true;
|
|
1345
|
+
};
|
|
1346
|
+
ask(false);
|
|
1347
|
+
let winner = null;
|
|
1348
|
+
while (asked.size > 0 && !winner) {
|
|
1349
|
+
const hedgeIn = hedgeAfter - (Date.now() - lastAsked);
|
|
1350
|
+
if (hedgeIn <= 0) {
|
|
1351
|
+
if (!ask(true)) hedgeAfter = Infinity;
|
|
1352
|
+
continue;
|
|
1353
|
+
}
|
|
1354
|
+
let timer;
|
|
1355
|
+
const hedgeDue = hedgeIn === Infinity ? null : new Promise((resolve) => {
|
|
1356
|
+
timer = setTimeout(() => resolve(null), hedgeIn);
|
|
1357
|
+
});
|
|
1358
|
+
const settled = await Promise.race([...[...asked.values()].map((attempt) => attempt.done.then((outcome) => ({
|
|
1359
|
+
attempt,
|
|
1360
|
+
outcome
|
|
1361
|
+
}))), ...hedgeDue ? [hedgeDue] : []]);
|
|
1362
|
+
clearTimeout(timer);
|
|
1363
|
+
if (!settled) continue;
|
|
1364
|
+
const { attempt, outcome } = settled;
|
|
1365
|
+
asked.delete(attempt.relay);
|
|
1366
|
+
const relay = attempt.relay;
|
|
1367
|
+
if (outcome.kind === "packet" || outcome.kind === "missing") {
|
|
1368
|
+
winner = {
|
|
1369
|
+
attempt,
|
|
1370
|
+
outcome
|
|
1371
|
+
};
|
|
1372
|
+
break;
|
|
1373
|
+
}
|
|
1374
|
+
if (outcome.kind === "throttled") {
|
|
1375
|
+
budgetWait = Math.min(budgetWait, this.coolDown(relay, outcome.response));
|
|
1376
|
+
this.answered(relay, {
|
|
1377
|
+
kind: "throttled",
|
|
1378
|
+
reason: "rate limited (429)"
|
|
1379
|
+
});
|
|
1380
|
+
} else if (outcome.kind === "failed") {
|
|
1381
|
+
this.networkCooldown.set(`GET ${relay}`, Date.now() + NETWORK_ERROR_COOLDOWN_MS);
|
|
1382
|
+
this.answered(relay, {
|
|
1383
|
+
kind: "error",
|
|
1384
|
+
reason: outcome.reason
|
|
1385
|
+
});
|
|
1386
|
+
down = true;
|
|
1387
|
+
}
|
|
1388
|
+
ask(false);
|
|
1389
|
+
}
|
|
1390
|
+
if (winner) {
|
|
1391
|
+
const { attempt, outcome } = winner, relay = attempt.relay;
|
|
1392
|
+
const took = Date.now() - attempt.started;
|
|
1393
|
+
if (outcome.kind === "packet") {
|
|
1394
|
+
this.sawTimestamp(pubKeyZ32, outcome.packet.timestampMicros);
|
|
1395
|
+
this.newest.set(pubKeyZ32, newerPacket(this.newest.get(pubKeyZ32), outcome.packet));
|
|
1396
|
+
this.missing.delete(pubKeyZ32);
|
|
1397
|
+
} else {
|
|
1398
|
+
this.missing.delete(pubKeyZ32);
|
|
1399
|
+
this.missing.add(pubKeyZ32);
|
|
1400
|
+
if (this.missing.size > READ_TURNS_KEPT) this.missing.delete(this.missing.values().next().value);
|
|
1401
|
+
}
|
|
1402
|
+
if (took <= 3e3) {
|
|
1403
|
+
this.answered(relay, void 0, "GET");
|
|
1404
|
+
this.slowUntil.delete(relay);
|
|
1405
|
+
} else {
|
|
1406
|
+
this.networkCooldown.delete(`GET ${relay}`);
|
|
1407
|
+
if (outcome.kind === "packet") this.slow(relay);
|
|
1408
|
+
else this.breaker.cancel(relay);
|
|
1409
|
+
}
|
|
1410
|
+
for (const loser of asked.values()) {
|
|
1411
|
+
loser.controller.abort();
|
|
1412
|
+
if (loser.started <= attempt.started && (outcome.kind === "packet" || Date.now() - loser.started >= 6e3)) this.slow(loser.relay);
|
|
1413
|
+
else this.breaker.cancel(loser.relay);
|
|
1414
|
+
}
|
|
1415
|
+
this.lastRelay = relay;
|
|
1416
|
+
this.answeredRead(pubKeyZ32);
|
|
1417
|
+
this.readFrom(pubKeyZ32, relay);
|
|
1418
|
+
return this.newest.get(pubKeyZ32) ?? null;
|
|
1419
|
+
}
|
|
1420
|
+
const resting = this.relays.every((r) => this.isCoolingDown(r, "GET") || this.breaker.blockedFor(r) > 0 || this.heldFor(r, who, Date.now(), pubKeyZ32) > 0);
|
|
1421
|
+
const known = down ? this.newest.get(pubKeyZ32) : newerPacket(this.newest.get(pubKeyZ32), this.written.get(pubKeyZ32));
|
|
1422
|
+
if (resting && known) {
|
|
1423
|
+
if (down && !keptOnFailure) throw new RelaysFailedError(known);
|
|
1424
|
+
if (!down && budgetWait < Infinity) this.heldBackUntil(pubKeyZ32, Date.now() + budgetWait);
|
|
1425
|
+
return known;
|
|
1426
|
+
}
|
|
1427
|
+
if (!down && budgetWait < Infinity) {
|
|
1428
|
+
this.heldBackUntil(pubKeyZ32, Date.now() + budgetWait);
|
|
1429
|
+
throw new DiscoveryBudgetError(budgetWait);
|
|
1430
|
+
}
|
|
1431
|
+
throw new Error("No Pkarr relay reachable");
|
|
1432
|
+
}
|
|
1433
|
+
/**
|
|
1434
|
+
* The order a key's read asks the relays in: in turn per key (see `resolve`), relays found slow lately or leaving a
|
|
1435
|
+
* request unanswered (`unanswering`) last. A relay
|
|
1436
|
+
* with a small share of its own (`RELAY_REQUESTS_PER_MINUTE` under `REQUESTS_PER_MINUTE`: pkarr.miguelmedeiros.dev's 5)
|
|
1437
|
+
* takes no turns: it comes after the others, for when they refuse, fail or are slow, unless every relay is one. In the
|
|
1438
|
+
* turn, it was spent in seconds and the relay after it took its turns too: pkarr.pubky.org got two reads in three and
|
|
1439
|
+
* pkarr.pubky.app, with twice its share, one. Its few requests are then left for writes, which go to every relay.
|
|
1440
|
+
*/
|
|
1441
|
+
readOrder(pubKeyZ32) {
|
|
1442
|
+
const full = this.relays.filter((relay) => !((RELAY_REQUESTS_PER_MINUTE[relay] ?? Infinity) < 30));
|
|
1443
|
+
const ring = full.length ? full : this.relays;
|
|
1444
|
+
const last = this.lastReadFrom.get(pubKeyZ32), lastAt = last === void 0 ? -1 : this.relays.indexOf(last);
|
|
1445
|
+
const after = lastAt < 0 ? -1 : ring.findIndex((relay) => this.relays.indexOf(relay) > lastAt);
|
|
1446
|
+
const start = lastAt < 0 ? this.cursor++ : after < 0 ? 0 : after;
|
|
1447
|
+
const turn = [...ring.map((_, i) => ring[(start + i) % ring.length]), ...this.relays.filter((relay) => !ring.includes(relay))];
|
|
1448
|
+
const now = Date.now(), behind = (relay) => (this.slowUntil.get(relay) ?? 0) > now || this.unanswering(relay, now);
|
|
1449
|
+
return [...turn.filter((relay) => !behind(relay)), ...turn.filter(behind)];
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* A request of this client has waited on the relay for `HEDGE_MS` and it has answered none since: it goes after the
|
|
1453
|
+
* others in a read (`readOrder`), at no cost of a request. A relay that took connections and never answered held a
|
|
1454
|
+
* fresh device's first pairing at its turn: the key's last read had found nothing (`HEDGE_MISSING_MS`), so the read
|
|
1455
|
+
* waited 6 s on it before asking the relay that had held the packet all along (8 s join to live, not 2.5 s).
|
|
1456
|
+
*/
|
|
1457
|
+
unanswering(relay, now) {
|
|
1458
|
+
const since = this.answeredAt.get(relay) ?? -Infinity;
|
|
1459
|
+
for (const request of this.waiting.get(relay) ?? []) if (request.at > since && now - request.at >= this.hedgeMs) return true;
|
|
1460
|
+
return false;
|
|
1461
|
+
}
|
|
1462
|
+
/** One GET of a key at one relay, which `controller` drops. Never rejects: what happened is the outcome. */
|
|
1463
|
+
readFromRelay(relay, pubKeyZ32) {
|
|
1464
|
+
const controller = new AbortController();
|
|
1465
|
+
return {
|
|
1466
|
+
relay,
|
|
1467
|
+
controller,
|
|
1468
|
+
started: Date.now(),
|
|
1469
|
+
done: (async () => {
|
|
1470
|
+
let status = 0;
|
|
1471
|
+
try {
|
|
1472
|
+
let payload;
|
|
1473
|
+
const response = await this.request(`${relay}/${pubKeyZ32}`, { method: "GET" }, async (r, signal) => {
|
|
1474
|
+
status = r.status;
|
|
1475
|
+
if (r.ok && r.status !== 404) payload = await readRelayBody(r, signal);
|
|
1476
|
+
}, controller.signal);
|
|
1477
|
+
status = response.status;
|
|
1478
|
+
if (response.status === 429) return {
|
|
1479
|
+
kind: "throttled",
|
|
1480
|
+
response
|
|
1481
|
+
};
|
|
1482
|
+
if (response.status === 404) return { kind: "missing" };
|
|
1483
|
+
if (!response.ok) throw new Error(`${relay} responded ${response.status}`);
|
|
1484
|
+
return {
|
|
1485
|
+
kind: "packet",
|
|
1486
|
+
packet: parseRelayPayload(pubKeyZ32, payload)
|
|
1487
|
+
};
|
|
1488
|
+
} catch {
|
|
1489
|
+
if (controller.signal.aborted) return { kind: "cancelled" };
|
|
1490
|
+
return {
|
|
1491
|
+
kind: "failed",
|
|
1492
|
+
reason: status >= 200 && status < 300 ? "invalid packet" : status ? `HTTP ${status}` : "no answer"
|
|
1493
|
+
};
|
|
1494
|
+
}
|
|
1495
|
+
})()
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* The relay was slow (`SLOW_MS`): it goes after the others for a while, and counts it against its breaker while
|
|
1500
|
+
* another relay is there to ask. With none, a slow answer beats no answer: it counts as one.
|
|
1501
|
+
*/
|
|
1502
|
+
slow(relay) {
|
|
1503
|
+
this.slowUntil.set(relay, Date.now() + SLOW_DEMOTE_MS);
|
|
1504
|
+
if (this.relays.some((r) => r !== relay && this.breaker.blockedFor(r) === 0 && !this.isCoolingDown(r, "GET"))) this.breaker.failure(relay, "error", "slow");
|
|
1505
|
+
else this.breaker.success(relay, this.relays);
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* A hedge may go to this relay: its minute has more requests left than what groups leave a chat (`reserveOf`), so
|
|
1509
|
+
* a second request for a read that already went out never takes one a pairing needs.
|
|
1510
|
+
*/
|
|
1511
|
+
hedgeRoom(relay) {
|
|
1512
|
+
const now = Date.now();
|
|
1513
|
+
return within(this.spent.get(relay) ?? [], now, 6e4).length < this.limitOf(relay) - this.reserveOf(relay);
|
|
1514
|
+
}
|
|
1515
|
+
readAnsweredAt(pubKeyZ32) {
|
|
1516
|
+
return this.readAt.get(pubKeyZ32);
|
|
1517
|
+
}
|
|
1518
|
+
/** The last read of this key was answered 404 (`HEDGE_MISSING_MS`): a relay takes seconds to say so. */
|
|
1519
|
+
lastReadMissing(pubKeyZ32) {
|
|
1520
|
+
return this.missing.has(pubKeyZ32);
|
|
1521
|
+
}
|
|
1522
|
+
readHeldBackFor(pubKeyZ32) {
|
|
1523
|
+
const held = this.heldBack.get(pubKeyZ32);
|
|
1524
|
+
const left = held === void 0 ? 0 : held.until - Date.now();
|
|
1525
|
+
return left > 0 ? left : void 0;
|
|
1526
|
+
}
|
|
1527
|
+
heldBackUntil(pubKeyZ32, until) {
|
|
1528
|
+
const held = this.heldBack.get(pubKeyZ32), times = (held?.times ?? 0) + 1;
|
|
1529
|
+
this.heldBack.delete(pubKeyZ32);
|
|
1530
|
+
this.heldBack.set(pubKeyZ32, {
|
|
1531
|
+
until,
|
|
1532
|
+
times,
|
|
1533
|
+
since: held?.since ?? Date.now()
|
|
1534
|
+
});
|
|
1535
|
+
if (this.heldBack.size > READ_TURNS_KEPT) this.heldBack.delete(this.heldBack.keys().next().value);
|
|
1536
|
+
}
|
|
1537
|
+
/** The relays answered a read of this key now; answers older than `FRESH_READ_MS` are dropped as the list grows. */
|
|
1538
|
+
answeredRead(pubKeyZ32) {
|
|
1539
|
+
const now = Date.now();
|
|
1540
|
+
this.heldBack.delete(pubKeyZ32);
|
|
1541
|
+
this.readAt.delete(pubKeyZ32);
|
|
1542
|
+
this.readAt.set(pubKeyZ32, now);
|
|
1543
|
+
if (this.readAt.size > 64) for (const [key, at] of this.readAt) {
|
|
1544
|
+
if (now - at < this.freshReadMs) break;
|
|
1545
|
+
this.readAt.delete(key);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
/** The relay that answered a key's read, for its next one; the keys read longest ago go past `READ_TURNS_KEPT`. */
|
|
1549
|
+
readFrom(pubKeyZ32, relay) {
|
|
1550
|
+
this.lastReadFrom.delete(pubKeyZ32);
|
|
1551
|
+
this.lastReadFrom.set(pubKeyZ32, relay);
|
|
1552
|
+
if (this.lastReadFrom.size > READ_TURNS_KEPT) this.lastReadFrom.delete(this.lastReadFrom.keys().next().value);
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* A relay whose budget (this client's, or its own rate limit) held a packet back that another relay took: it gets
|
|
1556
|
+
* that packet when it frees a request, as a background write (`who`: a group edge's offer or answer as its own, also
|
|
1557
|
+
* after that relay failed it). Only the newest per key and relay; a newer packet put everywhere drops it. Without this,
|
|
1558
|
+
* that relay kept the key's older packet for good, and an app that restarted and read it first found a contact's offer
|
|
1559
|
+
* long answered, answered it again, and waited on it (bug hunt r7a).
|
|
1560
|
+
*/
|
|
1561
|
+
catchUpLater(relay, key, payload, timestamp, who = CATCH_UP_ASKER) {
|
|
1562
|
+
const slot = `${relay} ${key}`;
|
|
1563
|
+
if ((this.lastPut.get(slot) ?? 0n) > timestamp) return;
|
|
1564
|
+
const pending = this.catchUp.get(slot);
|
|
1565
|
+
if (pending && pending.timestamp >= timestamp) return;
|
|
1566
|
+
this.catchUp.set(slot, {
|
|
1567
|
+
relay,
|
|
1568
|
+
key,
|
|
1569
|
+
payload,
|
|
1570
|
+
timestamp,
|
|
1571
|
+
who
|
|
1572
|
+
});
|
|
1573
|
+
this.scheduleCatchUp(CATCH_UP_MIN_MS);
|
|
1574
|
+
}
|
|
1575
|
+
scheduleCatchUp(inMs) {
|
|
1576
|
+
if (this.catchUpTimer || this.catchUp.size === 0) return;
|
|
1577
|
+
this.catchUpTimer = setTimeout(() => {
|
|
1578
|
+
this.catchUpTimer = null;
|
|
1579
|
+
this.runCatchUp();
|
|
1580
|
+
}, inMs);
|
|
1581
|
+
}
|
|
1582
|
+
async runCatchUp() {
|
|
1583
|
+
let next = Infinity;
|
|
1584
|
+
for (const [slot, item] of [...this.catchUp]) {
|
|
1585
|
+
if (this.catchUp.get(slot) !== item) continue;
|
|
1586
|
+
const coolingDown = (this.networkCooldown.get(`PUT ${item.relay}`) ?? 0) - Date.now();
|
|
1587
|
+
const wait = Math.max(this.heldFor(item.relay, item.who), this.rateLimitedFor(item.relay), this.breaker.blockedFor(item.relay), coolingDown);
|
|
1588
|
+
if (wait > 0) {
|
|
1589
|
+
next = Math.min(next, wait);
|
|
1590
|
+
continue;
|
|
1591
|
+
}
|
|
1592
|
+
this.catchUp.delete(slot);
|
|
1593
|
+
try {
|
|
1594
|
+
const response = await this.put(item.relay, item.key, item.payload, void 0, item.who);
|
|
1595
|
+
if ((response.status === 428 || response.status === 429) && !this.catchUp.has(slot)) {
|
|
1596
|
+
this.catchUp.set(slot, item);
|
|
1597
|
+
next = Math.min(next, CATCH_UP_RETRY_MS);
|
|
1598
|
+
}
|
|
1599
|
+
} catch (error) {
|
|
1600
|
+
if (isDiscoveryBudgetError(error) && !this.catchUp.has(slot)) {
|
|
1601
|
+
this.catchUp.set(slot, item);
|
|
1602
|
+
next = Math.min(next, error.retryInMs);
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
if (this.catchUp.size) this.scheduleCatchUp(Math.max(CATCH_UP_MIN_MS, next === Infinity ? CATCH_UP_RETRY_MS : next));
|
|
1607
|
+
}
|
|
1608
|
+
/** `sent`: told, once the budget took the request, whether the relay takes the packet (a 2xx). */
|
|
1609
|
+
async put(relay, pubKeyZ32, payload, replaces, who, probe = false, sent) {
|
|
1610
|
+
const refused = this.refusal(relay, probe);
|
|
1611
|
+
if (refused) throw refused;
|
|
1612
|
+
if (!this.take(relay, who, pubKeyZ32)) throw new DiscoveryBudgetError(this.heldFor(relay, who));
|
|
1613
|
+
if (probe) this.breaker.beginAllDown(relay);
|
|
1614
|
+
else this.breaker.begin(relay);
|
|
1615
|
+
const request = this.request(`${relay}/${pubKeyZ32}`, {
|
|
1616
|
+
method: "PUT",
|
|
1617
|
+
body: payload,
|
|
1618
|
+
headers: replaces === void 0 ? void 0 : { "If-Match": replaces.toString() }
|
|
1619
|
+
});
|
|
1620
|
+
sent?.(request.then((r) => r.ok, () => false));
|
|
1621
|
+
let response;
|
|
1622
|
+
try {
|
|
1623
|
+
response = await request;
|
|
1624
|
+
} catch (error) {
|
|
1625
|
+
this.networkCooldown.set(`PUT ${relay}`, Date.now() + NETWORK_ERROR_COOLDOWN_MS);
|
|
1626
|
+
this.answered(relay, {
|
|
1627
|
+
kind: "error",
|
|
1628
|
+
reason: "no answer"
|
|
1629
|
+
});
|
|
1630
|
+
throw new RelayNoAnswerError(`${relay} did not answer`, !connectFailed(error));
|
|
1631
|
+
}
|
|
1632
|
+
if (response.status === 429) this.answered(relay, {
|
|
1633
|
+
kind: "throttled",
|
|
1634
|
+
reason: "rate limited (429)"
|
|
1635
|
+
});
|
|
1636
|
+
else if (response.status >= 500) this.answered(relay, {
|
|
1637
|
+
kind: "error",
|
|
1638
|
+
reason: `HTTP ${response.status}`
|
|
1639
|
+
});
|
|
1640
|
+
else this.answered(relay, void 0, "PUT");
|
|
1641
|
+
return response;
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* Discovery reads and writes share a bounded per-relay request budget; background requests only part of it, and
|
|
1645
|
+
* groups leave a chat the budget refused its reserve. A link's write the budget refused goes before any read
|
|
1646
|
+
* once a request is free again.
|
|
1647
|
+
*/
|
|
1648
|
+
take(relay, who, key) {
|
|
1649
|
+
const now = Date.now();
|
|
1650
|
+
const writer = firstWriter(who);
|
|
1651
|
+
if (who.urgent && !who.background) {
|
|
1652
|
+
this.urgentAt = now;
|
|
1653
|
+
if (isChat(who)) this.chatNeed(key, now);
|
|
1654
|
+
}
|
|
1655
|
+
if (isGroupRead(who) || isChatWatch(who)) this.readAsked(key, who, now);
|
|
1656
|
+
if (isTurnRead(who)) this.turnAsked(key, who, now);
|
|
1657
|
+
if (isAwaitedRead(who)) this.awaitedAt = now;
|
|
1658
|
+
if (this.heldFor(relay, who, now, key) > 0) {
|
|
1659
|
+
if (isGroupRead(who) && (this.spent.get(relay)?.length ?? 0) >= this.limitOf(relay)) this.groupsFullAt.set(relay, now);
|
|
1660
|
+
if (isChat(who)) this.chatNeed(key, now, relay);
|
|
1661
|
+
if (writer) this.writeWaiting.set(`${writer} ${relay}`, now);
|
|
1662
|
+
if (isGroupAnswerRead(who)) {
|
|
1663
|
+
const waiting = this.answerReadWaiting.get(relay) ?? /* @__PURE__ */ new Map();
|
|
1664
|
+
this.answerReadWaiting.set(relay, waiting.set(key, now));
|
|
1665
|
+
}
|
|
1666
|
+
return false;
|
|
1667
|
+
}
|
|
1668
|
+
if (writer) this.writeWaiting.delete(`${writer} ${relay}`);
|
|
1669
|
+
if (isGroupAnswerRead(who)) this.answerReadWaiting.get(relay)?.delete(key);
|
|
1670
|
+
if (isWayBack(who) && this.heldFor(relay, {
|
|
1671
|
+
...who,
|
|
1672
|
+
back: false
|
|
1673
|
+
}, now, key, false) > 0) {
|
|
1674
|
+
this.spentWayBack.set(relay, [...this.spentWayBack.get(relay) ?? [], now]);
|
|
1675
|
+
if (isGroupRead(who)) this.readTurns.set(key, {
|
|
1676
|
+
servedAt: now,
|
|
1677
|
+
askedAt: now,
|
|
1678
|
+
waiting: false,
|
|
1679
|
+
watch: false,
|
|
1680
|
+
lane: watchLane(who)
|
|
1681
|
+
});
|
|
1682
|
+
return true;
|
|
1683
|
+
}
|
|
1684
|
+
if (isGroupAnswerRead(who) && this.heldFor(relay, who, now, key, false, false) > 0) {
|
|
1685
|
+
this.spentAnswerAllowance.set(relay, [...this.spentAnswerAllowance.get(relay) ?? [], now]);
|
|
1686
|
+
this.readTurns.set(key, {
|
|
1687
|
+
servedAt: now,
|
|
1688
|
+
askedAt: now,
|
|
1689
|
+
waiting: false,
|
|
1690
|
+
watch: false,
|
|
1691
|
+
lane: watchLane(who),
|
|
1692
|
+
answer: true
|
|
1693
|
+
});
|
|
1694
|
+
return true;
|
|
1695
|
+
}
|
|
1696
|
+
if (isGroupSignal(who) && !who.write && this.spent.get(relay).length >= this.limitOf(relay)) this.spentGroupOver.get(relay).push(now);
|
|
1697
|
+
if (isGroupSignal(who)) this.spentGroupSignal.get(relay).push(now);
|
|
1698
|
+
const departedOver = isDepartedRead(who) && this.spent.get(relay).length >= this.limitOf(relay);
|
|
1699
|
+
if (departedOver) this.overAdd(this.departedOver, key, now);
|
|
1700
|
+
if (isAwaitedRead(who) && this.spent.get(relay).length >= this.limitOf(relay)) this.awaitedOver = [...within(this.awaitedOver, now, 6e4), now];
|
|
1701
|
+
this.spent.get(relay).push(now);
|
|
1702
|
+
if (who.background) this.spentBackground.get(relay).push(now);
|
|
1703
|
+
if (who.background && who.write) this.spentBackgroundWrites.get(relay).push(now);
|
|
1704
|
+
if (isGroupUrgentRead(who) && isGroupAnswerRead(who)) this.spentAnswerUrgent.set(relay, [...this.spentAnswerUrgent.get(relay) ?? [], now]);
|
|
1705
|
+
else if (isGroupUrgentRead(who)) this.spentGroupUrgent.get(relay).push(now);
|
|
1706
|
+
if (isWatch(who)) this.spentWatch.get(relay).push(now);
|
|
1707
|
+
if (isChatWatch(who) && !departedOver) this.spentChatWatch.get(relay).push(now);
|
|
1708
|
+
if (isGroupRead(who)) this.spentGroupReads.get(relay).push(now);
|
|
1709
|
+
if (isGroupRead(who) || isChatWatch(who)) this.readTurns.set(key, {
|
|
1710
|
+
servedAt: now,
|
|
1711
|
+
askedAt: now,
|
|
1712
|
+
waiting: false,
|
|
1713
|
+
watch: isWatch(who) || isChatWatch(who),
|
|
1714
|
+
lane: watchLane(who)
|
|
1715
|
+
});
|
|
1716
|
+
if (isTurnRead(who)) this.keyTurns.set(key, {
|
|
1717
|
+
servedAt: now,
|
|
1718
|
+
askedAt: now,
|
|
1719
|
+
waiting: false,
|
|
1720
|
+
who
|
|
1721
|
+
});
|
|
1722
|
+
return true;
|
|
1723
|
+
}
|
|
1724
|
+
/**
|
|
1725
|
+
* How long until this relay's budget takes this request, 0 when it would now: the oldest request of the minute over
|
|
1726
|
+
* a limit ages out, a chat's reserve lapses, or a waiting write has had its turn. Keeps the lists to the minute, oldest first.
|
|
1727
|
+
*
|
|
1728
|
+
* Who goes first: a chat's refused write holds back everything but chat writes; a group's refused write holds back
|
|
1729
|
+
* group reads and background requests, never a chat's. Background requests yield to a link that signals. A chat's
|
|
1730
|
+
* signaling goes over the limit by its allowance (`SIGNALING_ALLOWANCE_SHARE`), and so do a few of its reads of a
|
|
1731
|
+
* contact that just went away (`DEPARTED_READS`) or of an invite awaited (`AWAITED_READS`). `ownAllowance` false: what holds an edge's read for its answer
|
|
1732
|
+
* without its own allowance (`ANSWER_READ_ALLOWANCE`).
|
|
1733
|
+
*/
|
|
1734
|
+
heldFor(relay, who, now = Date.now(), key, turns = true, ownAllowance = true) {
|
|
1735
|
+
const recent = within(this.spent.get(relay) ?? [], now, 6e4);
|
|
1736
|
+
const recentBackground = within(this.spentBackground.get(relay) ?? [], now, 6e4);
|
|
1737
|
+
this.spent.set(relay, recent);
|
|
1738
|
+
this.spentBackground.set(relay, recentBackground);
|
|
1739
|
+
const recentBackgroundWrites = within(this.spentBackgroundWrites.get(relay) ?? [], now, 6e4);
|
|
1740
|
+
this.spentBackgroundWrites.set(relay, recentBackgroundWrites);
|
|
1741
|
+
const over = (list, limit) => list.length >= limit ? list[list.length - limit] + 6e4 - now : 0;
|
|
1742
|
+
const limit = this.limitOf(relay);
|
|
1743
|
+
const departed = isDepartedRead(who) && key !== void 0 && recent.length >= limit && this.overReads(this.departedOver, key, now) < 6;
|
|
1744
|
+
if (isAwaitedRead(who)) this.awaitedOver = within(this.awaitedOver, now, 6e4);
|
|
1745
|
+
const awaited = isAwaitedRead(who) && key !== void 0 && recent.length >= limit && this.awaitedOver.length < 4;
|
|
1746
|
+
const share = isChatSignal(who) || departed || awaited ? SIGNALING_ALLOWANCE_SHARE : isGroupSignal(who) ? GROUP_SIGNALING_ALLOWANCE_SHARE : 0;
|
|
1747
|
+
let allowance = share ? Math.max(0, Math.floor(limit * share) - (who.write ? 0 : 1)) : 0;
|
|
1748
|
+
if (departed && now - this.awaitedAt < 6e4) allowance = Math.max(0, allowance - 2);
|
|
1749
|
+
const groupSignaled = within(this.spentGroupSignal.get(relay) ?? [], now, 6e4);
|
|
1750
|
+
this.spentGroupSignal.set(relay, groupSignaled);
|
|
1751
|
+
const groupFloor = isGroupSignal(who) && groupSignaled.length < allowance;
|
|
1752
|
+
let wait = groupFloor ? 0 : over(recent, limit + allowance);
|
|
1753
|
+
const starved = key !== void 0 && this.starved(key, who, now);
|
|
1754
|
+
const groupOver = within(this.spentGroupOver.get(relay) ?? [], now, GROUP_OVER_PACE_MS);
|
|
1755
|
+
this.spentGroupOver.set(relay, groupOver);
|
|
1756
|
+
if (isGroupSignal(who) && !who.write && recent.length >= limit && groupOver.length > 0) wait = Math.max(wait, groupOver[groupOver.length - 1] + GROUP_OVER_PACE_MS - now);
|
|
1757
|
+
if (who.background && !starved) {
|
|
1758
|
+
const backgroundShare = Math.min(this.backgroundPerMinute, Math.ceil(limit * 2 / 3));
|
|
1759
|
+
wait = Math.max(wait, over(recentBackground, backgroundShare));
|
|
1760
|
+
if (who.write) wait = Math.max(wait, over(recentBackgroundWrites, Math.max(1, Math.floor(backgroundShare * BACKGROUND_WRITES_SHARE))));
|
|
1761
|
+
const signaling = this.urgentAt + SIGNALING_WINDOW_MS - now;
|
|
1762
|
+
if (signaling > 0 && !who.door) wait = Math.max(wait, Math.min(signaling, over(recentBackground, Math.min(this.backgroundWhileSignaling, Math.ceil(limit / 6)))));
|
|
1763
|
+
}
|
|
1764
|
+
const burst = within(this.spentGroupUrgent.get(relay) ?? [], now, GROUP_BURST_MS);
|
|
1765
|
+
this.spentGroupUrgent.set(relay, burst);
|
|
1766
|
+
const answerBurst = within(this.spentAnswerUrgent.get(relay) ?? [], now, GROUP_BURST_MS);
|
|
1767
|
+
this.spentAnswerUrgent.set(relay, answerBurst);
|
|
1768
|
+
if (isGroupUrgentRead(who)) {
|
|
1769
|
+
const most = Math.max(8, Math.floor(limit * GROUP_BURST_MS / 6e4));
|
|
1770
|
+
const spent = isGroupAnswerRead(who) ? answerBurst : burst;
|
|
1771
|
+
if (spent.length >= most) wait = Math.max(wait, spent[spent.length - most] + GROUP_BURST_MS - now);
|
|
1772
|
+
}
|
|
1773
|
+
const watched = within(this.spentWatch.get(relay) ?? [], now, GROUP_RATION_WINDOW_MS);
|
|
1774
|
+
this.spentWatch.set(relay, watched);
|
|
1775
|
+
let turn = 0;
|
|
1776
|
+
if (isWatch(who)) {
|
|
1777
|
+
const most = Math.max(1, Math.ceil(limit * WATCH_SHARE * GROUP_RATION_WINDOW_MS / 6e4));
|
|
1778
|
+
if (watched.length >= most) wait = Math.max(wait, watched[watched.length - most] + GROUP_RATION_WINDOW_MS - now);
|
|
1779
|
+
if (this.readsAhead(key, now, watchLane(who)) > 0) turn = TURN_WAIT_MS;
|
|
1780
|
+
}
|
|
1781
|
+
const chatWatched = within(this.spentChatWatch.get(relay) ?? [], now, GROUP_RATION_WINDOW_MS);
|
|
1782
|
+
this.spentChatWatch.set(relay, chatWatched);
|
|
1783
|
+
if (isChatWatch(who) && !departed) {
|
|
1784
|
+
const most = Math.max(1, Math.ceil(limit * CHAT_WATCH_SHARE * GROUP_RATION_WINDOW_MS / 6e4));
|
|
1785
|
+
if (chatWatched.length >= most) wait = Math.max(wait, chatWatched[chatWatched.length - most] + GROUP_RATION_WINDOW_MS - now);
|
|
1786
|
+
if (this.readsAhead(key, now, watchLane(who)) > 0) turn = TURN_WAIT_MS;
|
|
1787
|
+
}
|
|
1788
|
+
const rationed = within(this.spentGroupReads.get(relay) ?? [], now, GROUP_RATION_WINDOW_MS);
|
|
1789
|
+
this.spentGroupReads.set(relay, rationed);
|
|
1790
|
+
const pastLimit = isGroupSignal(who) && recent.length >= limit;
|
|
1791
|
+
if (isGroupRead(who) && !pastLimit && now - (this.groupsFullAt.get(relay) ?? -Infinity) < GROUP_RATION_MS) {
|
|
1792
|
+
const most = Math.max(1, Math.ceil(limit * GROUP_RATION_WINDOW_MS / 6e4));
|
|
1793
|
+
if (rationed.length >= most) wait = Math.max(wait, rationed[rationed.length - most] + GROUP_RATION_WINDOW_MS - now);
|
|
1794
|
+
if (this.readsAhead(key, now, void 0, isGroupAnswerRead(who)) > 0) turn = TURN_WAIT_MS;
|
|
1795
|
+
}
|
|
1796
|
+
if (who.group && !groupFloor || who.background) {
|
|
1797
|
+
const reserved = this.chatReservedFor(relay, now);
|
|
1798
|
+
if (reserved > 0) wait = Math.max(wait, Math.min(reserved, over(recent, limit - this.chatReserveOf(relay, now))));
|
|
1799
|
+
}
|
|
1800
|
+
const writer = firstWriter(who);
|
|
1801
|
+
const waiting = (lane) => {
|
|
1802
|
+
const at = this.writeWaiting.get(`${lane} ${relay}`);
|
|
1803
|
+
return at !== void 0 && now - at < 5e3 ? at + WRITE_FIRST_MS - now : 0;
|
|
1804
|
+
};
|
|
1805
|
+
const signalOver = isChatSignal(who) && !who.write && recent.length >= limit;
|
|
1806
|
+
if (writer !== "chat" && !departed && !awaited && !signalOver && !starved) wait = Math.max(wait, waiting("chat"));
|
|
1807
|
+
if ((writer === null && who.group && !pastLimit && !isGroupAnswerRead(who) || who.background) && !starved) wait = Math.max(wait, waiting("group"));
|
|
1808
|
+
if (isGroupSignal(who) && !isGroupAnswerRead(who)) {
|
|
1809
|
+
const ahead = this.answerReadAhead(relay, now);
|
|
1810
|
+
if (ahead && this.heldFor(relay, ANSWER_READER, now, ahead.key, false, false) <= 0) wait = Math.max(wait, ahead.ms);
|
|
1811
|
+
}
|
|
1812
|
+
if (turns && wait <= 0 && isTurnRead(who) && this.turnAhead(relay, key, who, now)) turn = TURN_WAIT_MS;
|
|
1813
|
+
if (wait > 0 && isWayBack(who) && this.wayBackLeft(relay, who, now) > 0) return 0;
|
|
1814
|
+
if (wait > 0 && ownAllowance && isGroupAnswerRead(who) && this.answerAllowanceFree(relay, now)) return 0;
|
|
1815
|
+
return Math.max(wait, turn, 0);
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* The edges' reads for their answers may take their own allowance on this relay now (`ANSWER_READ_ALLOWANCE`): fewer
|
|
1819
|
+
* than it in the last minute, and none within `ANSWER_READ_SPACING_MS`. None where the groups have no allowance.
|
|
1820
|
+
*/
|
|
1821
|
+
answerAllowanceFree(relay, now) {
|
|
1822
|
+
const spent = within(this.spentAnswerAllowance.get(relay) ?? [], now, 6e4);
|
|
1823
|
+
this.spentAnswerAllowance.set(relay, spent);
|
|
1824
|
+
const most = Math.min(3, Math.floor(this.limitOf(relay) * GROUP_SIGNALING_ALLOWANCE_SHARE));
|
|
1825
|
+
return spent.length < most && (spent.length === 0 || now - spent[spent.length - 1] >= 2e4);
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* The edges' reads for their answers the budget refused on this relay within `ANSWER_READ_FIRST_MS`, still not read:
|
|
1829
|
+
* the key refused last, and how long the groups' other signaling waits for them there. Undefined when there is none.
|
|
1830
|
+
*/
|
|
1831
|
+
answerReadAhead(relay, now) {
|
|
1832
|
+
const waiting = this.answerReadWaiting.get(relay);
|
|
1833
|
+
if (!waiting) return void 0;
|
|
1834
|
+
let last;
|
|
1835
|
+
for (const [key, at] of waiting) if (now - at >= 5e3) waiting.delete(key);
|
|
1836
|
+
else if (!last || at > last.at) last = {
|
|
1837
|
+
key,
|
|
1838
|
+
at
|
|
1839
|
+
};
|
|
1840
|
+
return last && {
|
|
1841
|
+
key: last.key,
|
|
1842
|
+
ms: last.at + 5e3 - now
|
|
1843
|
+
};
|
|
1844
|
+
}
|
|
1845
|
+
/** What is left of the community members' way back allowance on this relay (`WAY_BACK_SHARE`); its reads leave a write the last of it. */
|
|
1846
|
+
wayBackLeft(relay, who, now) {
|
|
1847
|
+
const spent = within(this.spentWayBack.get(relay) ?? [], now, 6e4);
|
|
1848
|
+
this.spentWayBack.set(relay, spent);
|
|
1849
|
+
return Math.floor(this.limitOf(relay) * WAY_BACK_SHARE) - (who.write ? 0 : 1) - spent.length;
|
|
1850
|
+
}
|
|
1851
|
+
/** A group's read of `key`, or a chat's watch read, asks: it waits for its turn from now on, unless it is taken (`readsAhead`). */
|
|
1852
|
+
readAsked(key, who, now) {
|
|
1853
|
+
this.pruneTurns(this.readTurns, now);
|
|
1854
|
+
const turn = this.readTurns.get(key);
|
|
1855
|
+
this.readTurns.set(key, {
|
|
1856
|
+
servedAt: turn && !staleTurn(turn, now) ? turn.servedAt : -Infinity,
|
|
1857
|
+
askedAt: now,
|
|
1858
|
+
waiting: true,
|
|
1859
|
+
watch: isWatch(who) || isChatWatch(who),
|
|
1860
|
+
lane: watchLane(who),
|
|
1861
|
+
answer: isGroupAnswerRead(who)
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
/**
|
|
1865
|
+
* Keys a group reads, still asking (within `WATCH_TURN_MS`), that were read longer ago than `key`: a share keeps a
|
|
1866
|
+
* request for each. With `watchLane`, only watched keys the same other limits hold: one the groups' burst holds back
|
|
1867
|
+
* would keep a turn it cannot take. The chats' watched keys only in their own lane (`CHAT_WATCH_SHARE`). `answer`: an
|
|
1868
|
+
* edge's read for its answer, which waits only for the others' (`ANSWER_READ_FIRST_MS`).
|
|
1869
|
+
*/
|
|
1870
|
+
readsAhead(key, now, watchLane, answer = false) {
|
|
1871
|
+
const mine = key === void 0 ? -Infinity : this.readTurns.get(key)?.servedAt ?? -Infinity;
|
|
1872
|
+
let ahead = 0;
|
|
1873
|
+
for (const [other, turn] of this.readTurns) {
|
|
1874
|
+
if (other === key || !turn.waiting || now - turn.askedAt >= WATCH_TURN_MS) continue;
|
|
1875
|
+
if (watchLane !== void 0 ? !turn.watch || turn.lane !== watchLane : turn.lane === "chat") continue;
|
|
1876
|
+
if (answer && !turn.answer) continue;
|
|
1877
|
+
if (turn.servedAt < mine) ahead++;
|
|
1878
|
+
}
|
|
1879
|
+
return ahead;
|
|
1880
|
+
}
|
|
1881
|
+
/** A read of `key` that takes turns asks: it waits for its turn from now on, unless it is taken (`turnAhead`). */
|
|
1882
|
+
turnAsked(key, who, now) {
|
|
1883
|
+
this.pruneTurns(this.keyTurns, now);
|
|
1884
|
+
const turn = this.keyTurns.get(key);
|
|
1885
|
+
this.keyTurns.set(key, {
|
|
1886
|
+
servedAt: turn && !staleTurn(turn, now) ? turn.servedAt : -Infinity,
|
|
1887
|
+
askedAt: now,
|
|
1888
|
+
waiting: true,
|
|
1889
|
+
who
|
|
1890
|
+
});
|
|
1891
|
+
}
|
|
1892
|
+
/** Drops the turns asked and read over a minute ago, at most once in `TURNS_PRUNE_MS` (or when the clock went back). */
|
|
1893
|
+
pruneTurns(turns, now) {
|
|
1894
|
+
const prunedAt = this.turnsPrunedAt.get(turns) ?? -Infinity;
|
|
1895
|
+
if (Math.abs(now - prunedAt) < TURNS_PRUNE_MS) return;
|
|
1896
|
+
this.turnsPrunedAt.set(turns, now);
|
|
1897
|
+
const back = now < prunedAt;
|
|
1898
|
+
for (const [key, turn] of turns) {
|
|
1899
|
+
if (back) {
|
|
1900
|
+
turn.servedAt = Math.min(turn.servedAt, now);
|
|
1901
|
+
turn.askedAt = Math.min(turn.askedAt, now);
|
|
1902
|
+
}
|
|
1903
|
+
if (staleTurn(turn, now)) turns.delete(key);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
/**
|
|
1907
|
+
* Whether a key read in turn longer ago than `key`, still asking, would take this relay's next request: one read in the
|
|
1908
|
+
* background for a background read, and a chat's that is starved (`READ_STARVED_MS`) for any. One asks again after
|
|
1909
|
+
* the budget held it back (a link's poll does, a one-off look does not), and keeps asking within `READ_TURN_MS` of its
|
|
1910
|
+
* last ask, or of when it was told the budget frees a request for it (`readHeldBackFor`).
|
|
1911
|
+
* One the budget holds back on its own account (a group's read, for the chats' reserve) keeps no turn: a group key
|
|
1912
|
+
* waiting for the chats held every chat's read back (CLI lab, 2026-10-13).
|
|
1913
|
+
*/
|
|
1914
|
+
turnAhead(relay, key, who, now) {
|
|
1915
|
+
const mine = key === void 0 ? -Infinity : this.keyTurns.get(key)?.servedAt ?? -Infinity;
|
|
1916
|
+
for (const [other, turn] of this.keyTurns) {
|
|
1917
|
+
const held = this.heldBack.get(other);
|
|
1918
|
+
if (other === key || !turn.waiting || turn.servedAt >= mine || !held || held.times < 2) continue;
|
|
1919
|
+
if (!(who.background && turn.who.background) && !this.starved(other, turn.who, now)) continue;
|
|
1920
|
+
if (now - Math.max(turn.askedAt, held.until) < READ_TURN_MS && this.heldFor(relay, turn.who, now, other, false) === 0) return true;
|
|
1921
|
+
}
|
|
1922
|
+
return false;
|
|
1923
|
+
}
|
|
1924
|
+
/** A 1:1 chat's read of `key` the budget has held back for `READ_STARVED_MS` without a break. */
|
|
1925
|
+
starved(key, who, now) {
|
|
1926
|
+
const since = this.heldBack.get(key)?.since;
|
|
1927
|
+
return !who.group && !who.write && !who.door && since !== void 0 && now - since >= READ_STARVED_MS;
|
|
1928
|
+
}
|
|
1929
|
+
/** This key's departed reads past the limit in the last minute (`DEPARTED_READS`). */
|
|
1930
|
+
overReads(over, key, now) {
|
|
1931
|
+
const reads = within(over.get(key) ?? [], now, 6e4);
|
|
1932
|
+
if (reads.length) over.set(key, reads);
|
|
1933
|
+
else over.delete(key);
|
|
1934
|
+
return reads.length;
|
|
1935
|
+
}
|
|
1936
|
+
/** One more, the keys with none in the last minute dropped. */
|
|
1937
|
+
overAdd(over, key, now) {
|
|
1938
|
+
for (const [other, reads] of over) if (now - reads[reads.length - 1] >= 6e4) over.delete(other);
|
|
1939
|
+
over.set(key, [...over.get(key) ?? [], now]);
|
|
1940
|
+
}
|
|
1941
|
+
/** A chat's request of `key` asked urgently (it needs every relay), or the budget refused it on `relay` (it needs that one). */
|
|
1942
|
+
chatNeed(key, now, relay) {
|
|
1943
|
+
if (relay !== void 0) {
|
|
1944
|
+
this.chatNeedsOn(relay, now).set(key, now);
|
|
1945
|
+
return;
|
|
1946
|
+
}
|
|
1947
|
+
this.chatUrgentAt.set(key, now);
|
|
1948
|
+
for (const needs of this.chatNeeds.values()) needs.set(key, now);
|
|
1949
|
+
}
|
|
1950
|
+
/** The chats that need this relay: those a request of was refused there, and the urgent ones. */
|
|
1951
|
+
chatNeedsOn(relay, now) {
|
|
1952
|
+
let needs = this.chatNeeds.get(relay);
|
|
1953
|
+
if (!needs) {
|
|
1954
|
+
this.chatNeeds.set(relay, needs = new RecentKeys());
|
|
1955
|
+
for (const [key, at] of this.chatUrgentAt.entries(now)) needs.set(key, at);
|
|
1956
|
+
}
|
|
1957
|
+
return needs;
|
|
1958
|
+
}
|
|
1959
|
+
/** The chat reading `key` has nothing due: it needs no relay any more. */
|
|
1960
|
+
chatNeedLapsed(key) {
|
|
1961
|
+
this.chatUrgentAt.delete(key);
|
|
1962
|
+
for (const needs of this.chatNeeds.values()) needs.delete(key);
|
|
1963
|
+
}
|
|
1964
|
+
/**
|
|
1965
|
+
* How long groups still leave a chat its reserve on this relay (`CHAT_RESERVE`); a chat's need a minute old goes.
|
|
1966
|
+
* Kept up to date as needs come and go (`RecentKeys`): walking them all for each request cost a daemon with 500
|
|
1967
|
+
* contacts away 2.7 s of CPU per 10 idle minutes (bug hunt, 2026-11-01).
|
|
1968
|
+
*/
|
|
1969
|
+
chatReservedFor(relay, now) {
|
|
1970
|
+
return this.chatNeedsOn(relay, now).latest(now) + 6e4 - now;
|
|
1971
|
+
}
|
|
1972
|
+
/**
|
|
1973
|
+
* How long a chat's packet another relay already took waits for this one: while chats need the relays, a copy takes
|
|
1974
|
+
* none of the requests their reserve keeps (`chatReserveOf`), as a group's request does not. 0 when it goes now.
|
|
1975
|
+
*/
|
|
1976
|
+
spareHeldFor(relay, now) {
|
|
1977
|
+
const reserved = this.chatReservedFor(relay, now);
|
|
1978
|
+
if (reserved <= 0) return 0;
|
|
1979
|
+
const recent = within(this.spent.get(relay) ?? [], now, 6e4);
|
|
1980
|
+
const keep = this.limitOf(relay) - this.chatReserveOf(relay, now);
|
|
1981
|
+
return recent.length >= keep ? Math.min(reserved, recent[recent.length - keep] + 6e4 - now) : 0;
|
|
1982
|
+
}
|
|
1983
|
+
/** Requests allowed to this relay a minute: this client's budget, or the relay's own share (`RELAY_REQUESTS_PER_MINUTE`). */
|
|
1984
|
+
limitOf(relay) {
|
|
1985
|
+
const share = RELAY_REQUESTS_PER_MINUTE[relay];
|
|
1986
|
+
if (share === void 0) return this.perMinute;
|
|
1987
|
+
return this.perMinuteGiven ? Math.min(this.perMinute, share) : share;
|
|
1988
|
+
}
|
|
1989
|
+
/** What the chats that need the relays keep on this relay (`CHAT_RESERVE_PER_CHAT`), never less than `reserveOf`. */
|
|
1990
|
+
chatReserveOf(relay, now) {
|
|
1991
|
+
const needing = this.chatNeedsOn(relay, now).size(now);
|
|
1992
|
+
return Math.max(this.reserveOf(relay), Math.min(needing * 3, Math.floor(this.limitOf(relay) * CHAT_RESERVE_MOST_SHARE)));
|
|
1993
|
+
}
|
|
1994
|
+
/** What groups leave a chat on this relay: `CHAT_RESERVE`, or a third of a smaller budget. */
|
|
1995
|
+
reserveOf(relay) {
|
|
1996
|
+
return Math.min(10, Math.floor(this.limitOf(relay) / 3));
|
|
1997
|
+
}
|
|
1998
|
+
isCoolingDown(relay, method) {
|
|
1999
|
+
return this.rateLimitedFor(relay) > 0 || this.networkCoolingDown(relay, method);
|
|
2000
|
+
}
|
|
2001
|
+
/** Left alone after failing at the network level. */
|
|
2002
|
+
networkCoolingDown(relay, method) {
|
|
2003
|
+
return (this.networkCooldown.get(`${method} ${relay}`) ?? 0) > Date.now();
|
|
2004
|
+
}
|
|
2005
|
+
/** How much longer the relay asked (429) to be left alone; 0 when it did not. */
|
|
2006
|
+
rateLimitedFor(relay) {
|
|
2007
|
+
return Math.max(0, (this.coolingDown.get(relay) ?? 0) - Date.now());
|
|
2008
|
+
}
|
|
2009
|
+
/** Leaves the relay alone for as long as its 429 asks; returns that, in ms. */
|
|
2010
|
+
coolDown(relay, response) {
|
|
2011
|
+
const retryAfter = Number.parseInt(response.headers.get("retry-after") ?? "", 10);
|
|
2012
|
+
const seconds = Number.isFinite(retryAfter) ? Math.min(Math.max(retryAfter, 1), 120) : 15;
|
|
2013
|
+
this.coolingDown.set(relay, Date.now() + seconds * 1e3);
|
|
2014
|
+
return seconds * 1e3;
|
|
2015
|
+
}
|
|
2016
|
+
/**
|
|
2017
|
+
* One request under one time limit. `read` runs before the timer is cleared, so a body read there is bounded in
|
|
2018
|
+
* time as well: a relay that sends its headers and then trickles, or never ends, is given up on like one that
|
|
2019
|
+
* never answered.
|
|
2020
|
+
*/
|
|
2021
|
+
/**
|
|
2022
|
+
* The relay's own time, when its answer says it and this runtime may read it: Node and the Desktop can, a browser
|
|
2023
|
+
* only when the relay exposes the header to other origins. Held against this device's clock (`ClockWatch`).
|
|
2024
|
+
*/
|
|
2025
|
+
serverTime(url, response, sent) {
|
|
2026
|
+
if (!this.timeListeners.size) return;
|
|
2027
|
+
let date = NaN;
|
|
2028
|
+
try {
|
|
2029
|
+
if (Number(response.headers.get("age") ?? 0) > 0) return;
|
|
2030
|
+
date = Date.parse(response.headers.get("date") ?? "");
|
|
2031
|
+
} catch {}
|
|
2032
|
+
if (!Number.isFinite(date)) return;
|
|
2033
|
+
const time = {
|
|
2034
|
+
source: new URL(url).origin,
|
|
2035
|
+
date,
|
|
2036
|
+
sent,
|
|
2037
|
+
received: Date.now()
|
|
2038
|
+
};
|
|
2039
|
+
for (const listener of this.timeListeners) try {
|
|
2040
|
+
listener(time);
|
|
2041
|
+
} catch {}
|
|
2042
|
+
}
|
|
2043
|
+
onServerTime(listener) {
|
|
2044
|
+
this.timeListeners.add(listener);
|
|
2045
|
+
return () => this.timeListeners.delete(listener);
|
|
2046
|
+
}
|
|
2047
|
+
/**
|
|
2048
|
+
* The turn record's read (WISP 06 § Publishing and reading): every relay, in parallel, `TURN_SOURCE_TIMEOUT_MS`
|
|
2049
|
+
* each. Unlike `resolve`, it does not stop at the first relay that answers, keeps nothing, and answers nothing from
|
|
2050
|
+
* memory. Each request counts in the relay's minute, so chats see it, but is never held back by it: a device may
|
|
2051
|
+
* not act before it has read its turn.
|
|
2052
|
+
*/
|
|
2053
|
+
turnRead(pubKeyZ32, options = {}) {
|
|
2054
|
+
const timeoutMs = Math.min(options.timeoutMs ?? 8e3, TURN_SOURCE_TIMEOUT_MS);
|
|
2055
|
+
return Promise.all(this.relays.map(async (relay) => {
|
|
2056
|
+
try {
|
|
2057
|
+
let payload;
|
|
2058
|
+
const read = async (r, signal) => {
|
|
2059
|
+
if (r.ok) payload = await readRelayBody(r, signal);
|
|
2060
|
+
};
|
|
2061
|
+
let response = await this.turnRequest(relay, `${pubKeyZ32}?policy=NetworkOnly`, { method: "GET" }, read, timeoutMs);
|
|
2062
|
+
const stale = response.status === 400;
|
|
2063
|
+
if (stale) response = await this.turnRequest(relay, pubKeyZ32, { method: "GET" }, read, timeoutMs);
|
|
2064
|
+
const age = stale ? { stale: true } : {};
|
|
2065
|
+
if (response.status === 404) {
|
|
2066
|
+
const invalid = response.headers.get(INVALID_PACKET_SEQ_HEADER);
|
|
2067
|
+
return {
|
|
2068
|
+
source: relay,
|
|
2069
|
+
answered: true,
|
|
2070
|
+
payloads: [],
|
|
2071
|
+
...age,
|
|
2072
|
+
...invalid && /^\d{1,19}$/.test(invalid) ? { sequences: [invalid] } : {}
|
|
2073
|
+
};
|
|
2074
|
+
}
|
|
2075
|
+
if (!response.ok || !payload) return {
|
|
2076
|
+
source: relay,
|
|
2077
|
+
answered: false,
|
|
2078
|
+
payloads: [],
|
|
2079
|
+
detail: `HTTP ${response.status}`
|
|
2080
|
+
};
|
|
2081
|
+
return {
|
|
2082
|
+
source: relay,
|
|
2083
|
+
answered: true,
|
|
2084
|
+
payloads: [payload],
|
|
2085
|
+
...age
|
|
2086
|
+
};
|
|
2087
|
+
} catch (error) {
|
|
2088
|
+
return {
|
|
2089
|
+
source: relay,
|
|
2090
|
+
answered: false,
|
|
2091
|
+
payloads: [],
|
|
2092
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
2093
|
+
};
|
|
2094
|
+
}
|
|
2095
|
+
}));
|
|
2096
|
+
}
|
|
2097
|
+
/**
|
|
2098
|
+
* The turn record's put: the bytes as given, to each relay `conditions` names, with `If-Match` on the sequence that
|
|
2099
|
+
* relay is known to hold (none where it held no record). Unlike `putEverywhere`, a relay that refuses (409, 412,
|
|
2100
|
+
* 428) is not asked again without the condition: its refusal is the answer, and every relay's answer is reported.
|
|
2101
|
+
*/
|
|
2102
|
+
turnPut(pubKeyZ32, payload, conditions) {
|
|
2103
|
+
const relays = this.relays.filter((relay) => conditions[relay] !== void 0);
|
|
2104
|
+
return Promise.all(relays.map(async (relay) => {
|
|
2105
|
+
const replaces = conditions[relay];
|
|
2106
|
+
try {
|
|
2107
|
+
const response = await this.turnRequest(relay, pubKeyZ32, {
|
|
2108
|
+
method: "PUT",
|
|
2109
|
+
body: payload,
|
|
2110
|
+
headers: replaces === null ? void 0 : { "If-Match": replaces }
|
|
2111
|
+
});
|
|
2112
|
+
if (response.ok) return {
|
|
2113
|
+
source: relay,
|
|
2114
|
+
outcome: "stored",
|
|
2115
|
+
detail: `HTTP ${response.status}`
|
|
2116
|
+
};
|
|
2117
|
+
return {
|
|
2118
|
+
source: relay,
|
|
2119
|
+
outcome: TURN_REFUSALS.includes(response.status) ? "refused" : "failed",
|
|
2120
|
+
detail: `HTTP ${response.status}`
|
|
2121
|
+
};
|
|
2122
|
+
} catch (error) {
|
|
2123
|
+
return {
|
|
2124
|
+
source: relay,
|
|
2125
|
+
outcome: "failed",
|
|
2126
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
2127
|
+
};
|
|
2128
|
+
}
|
|
2129
|
+
}));
|
|
2130
|
+
}
|
|
2131
|
+
async turnRequest(relay, path, init, read, timeoutMs = TURN_SOURCE_TIMEOUT_MS) {
|
|
2132
|
+
this.spent.get(relay)?.push(Date.now());
|
|
2133
|
+
const controller = new AbortController();
|
|
2134
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
2135
|
+
try {
|
|
2136
|
+
const response = await this.fetchFn(`${relay}/${path}`, {
|
|
2137
|
+
...init,
|
|
2138
|
+
cache: "no-store",
|
|
2139
|
+
signal: controller.signal
|
|
2140
|
+
});
|
|
2141
|
+
await read?.(response, controller.signal);
|
|
2142
|
+
return response;
|
|
2143
|
+
} finally {
|
|
2144
|
+
clearTimeout(timer);
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
/** `cancel`: this client drops the request (a hedged read another relay answered first). */
|
|
2148
|
+
async request(url, init, read, cancel) {
|
|
2149
|
+
const controller = new AbortController();
|
|
2150
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
2151
|
+
const drop = () => controller.abort();
|
|
2152
|
+
cancel?.addEventListener("abort", drop, { once: true });
|
|
2153
|
+
const relay = url.slice(0, url.lastIndexOf("/")), request = { at: Date.now() };
|
|
2154
|
+
const waiting = this.waiting.get(relay) ?? /* @__PURE__ */ new Set();
|
|
2155
|
+
this.waiting.set(relay, waiting.add(request));
|
|
2156
|
+
try {
|
|
2157
|
+
const sent = Date.now();
|
|
2158
|
+
const response = await this.fetchFn(url, {
|
|
2159
|
+
...init,
|
|
2160
|
+
cache: "no-store",
|
|
2161
|
+
signal: controller.signal
|
|
2162
|
+
});
|
|
2163
|
+
this.answeredAt.set(relay, Date.now());
|
|
2164
|
+
this.serverTime(url, response, sent);
|
|
2165
|
+
await read?.(response, controller.signal);
|
|
2166
|
+
return response;
|
|
2167
|
+
} finally {
|
|
2168
|
+
waiting.delete(request);
|
|
2169
|
+
clearTimeout(timer);
|
|
2170
|
+
cancel?.removeEventListener("abort", drop);
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
};
|
|
2174
|
+
/**
|
|
2175
|
+
* A relay's answer body, at most RELAY_PAYLOAD_MAX_BYTES: refused on a larger `content-length` before anything is
|
|
2176
|
+
* read, and otherwise streamed with a running count, so a relay (a default one or one the person added, working or
|
|
2177
|
+
* compromised) cannot fill memory with a huge or endless body. Stops when `signal` aborts.
|
|
2178
|
+
*/
|
|
2179
|
+
async function readRelayBody(response, signal, max = RELAY_PAYLOAD_MAX_BYTES) {
|
|
2180
|
+
const tooLarge = () => /* @__PURE__ */ new Error(`Relay answer is larger than ${max} bytes`);
|
|
2181
|
+
if (Number(response.headers.get("content-length")) > max) {
|
|
2182
|
+
response.body?.cancel().catch(() => {});
|
|
2183
|
+
throw tooLarge();
|
|
2184
|
+
}
|
|
2185
|
+
if (!response.body) return /* @__PURE__ */ new Uint8Array(0);
|
|
2186
|
+
const reader = response.body.getReader();
|
|
2187
|
+
const stop = () => {
|
|
2188
|
+
reader.cancel().catch(() => {});
|
|
2189
|
+
};
|
|
2190
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
2191
|
+
try {
|
|
2192
|
+
const chunks = [];
|
|
2193
|
+
let length = 0;
|
|
2194
|
+
for (;;) {
|
|
2195
|
+
if (signal.aborted) throw new Error("Relay answer timed out");
|
|
2196
|
+
const { done, value } = await reader.read();
|
|
2197
|
+
if (signal.aborted) throw new Error("Relay answer timed out");
|
|
2198
|
+
if (done) break;
|
|
2199
|
+
length += value.byteLength;
|
|
2200
|
+
if (length > max) {
|
|
2201
|
+
stop();
|
|
2202
|
+
throw tooLarge();
|
|
2203
|
+
}
|
|
2204
|
+
chunks.push(value);
|
|
2205
|
+
}
|
|
2206
|
+
const body = new Uint8Array(length);
|
|
2207
|
+
let at = 0;
|
|
2208
|
+
for (const chunk of chunks) {
|
|
2209
|
+
body.set(chunk, at);
|
|
2210
|
+
at += chunk.byteLength;
|
|
2211
|
+
}
|
|
2212
|
+
return body;
|
|
2213
|
+
} finally {
|
|
2214
|
+
signal.removeEventListener("abort", stop);
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
export { RelaysFailedError as a, normalizeRelayUrl as c, traceLink as d, DiscoveryBudgetError as f, withRequestOptions as g, publishedNowhere as h, RelayTransport as i, relayUrlProblem as l, isDiscoveryBudgetError as m, HEDGE_MISSING_MS as n, SLOW_MS as o, budgetRetryMs as p, HEDGE_MS as r, currentRelays as s, DEFAULT_RELAYS as t, setLinkTraceSink as u };
|
|
2218
|
+
|
|
2219
|
+
//# sourceMappingURL=relay-Cjfmdec0.js.map
|