@ghostlytools/cli 1.11.3 → 1.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/README.md +27 -22
  2. package/SKILL.md +4 -2
  3. package/dist/assets/{api-DgU4MZPD.js → api-BwyFq4lA.js} +2 -2
  4. package/dist/assets/{api-DBlaoz8i.js → api-MYc6QNRs.js} +187 -87
  5. package/dist/assets/api-MYc6QNRs.js.map +1 -0
  6. package/dist/assets/{apiKit-DesObIDf.js → apiKit-CfsNx9ZR.js} +60 -14
  7. package/dist/assets/apiKit-CfsNx9ZR.js.map +1 -0
  8. package/dist/assets/{appFetch-C8yZIq1e.js → appFetch-DVYZOZFw.js} +2 -2
  9. package/dist/assets/{appFetch-C8yZIq1e.js.map → appFetch-DVYZOZFw.js.map} +1 -1
  10. package/dist/assets/{apps-rzxsjfBV.js → apps-Dbw2Hcko.js} +1 -1
  11. package/dist/assets/{apps-DkMjDHML.js → apps-h12II1us.js} +52 -30
  12. package/dist/assets/apps-h12II1us.js.map +1 -0
  13. package/dist/assets/{arkade-CarKONmH.js → arkade-Bw_4PZRG.js} +2 -2
  14. package/dist/assets/{arkade-CarKONmH.js.map → arkade-Bw_4PZRG.js.map} +1 -1
  15. package/dist/assets/{backup-CgWU7x0T.js → backup-CdEzTq5C.js} +1 -1
  16. package/dist/assets/{backup-C0OJCJlM.js → backup-Chvy79SN.js} +49 -36
  17. package/dist/assets/backup-Chvy79SN.js.map +1 -0
  18. package/dist/assets/bark_ffi_wasm_bg-EY2uc0Bx.js.map +1 -1
  19. package/dist/assets/bitcoindevkit_bg-B-iLWrmX.js.map +1 -1
  20. package/dist/assets/{buttons-fZTEa_2i.js → buttons-BgoExb4E.js} +6 -5
  21. package/dist/assets/{buttons-fZTEa_2i.js.map → buttons-BgoExb4E.js.map} +1 -1
  22. package/dist/assets/buttons-DK2aAGat.js +2 -0
  23. package/dist/assets/{client-Ibk3-e60.js → client-DY8tZZWi.js} +28 -8
  24. package/dist/assets/client-DY8tZZWi.js.map +1 -0
  25. package/dist/assets/{codec-DMpQqVSt.js → codec-BnMg-Grj.js} +2 -2
  26. package/dist/assets/{codec-DMpQqVSt.js.map → codec-BnMg-Grj.js.map} +1 -1
  27. package/dist/assets/{envelope-CvQVCGdf.js → envelope-CdV7vHVZ.js} +2 -2
  28. package/dist/assets/{envelope-CvQVCGdf.js.map → envelope-CdV7vHVZ.js.map} +1 -1
  29. package/dist/assets/{errors-Dt-ZnyOX.js → errors-CAh6GLJZ.js} +27 -2
  30. package/dist/assets/errors-CAh6GLJZ.js.map +1 -0
  31. package/dist/assets/{fedimintSdk-zV_5Qdqz.js → fedimintSdk-CPFfVK-G.js} +2 -2
  32. package/dist/assets/{fedimintSdk-zV_5Qdqz.js.map → fedimintSdk-CPFfVK-G.js.map} +1 -1
  33. package/dist/assets/{fixedHosts-6gKn8dp2.js → fixedHosts-CbgqrIKs.js} +3 -3
  34. package/dist/assets/{fixedHosts-6gKn8dp2.js.map → fixedHosts-CbgqrIKs.js.map} +1 -1
  35. package/dist/assets/{host-CYlIiY3r.js → host-C0NMNQLl.js} +556 -126
  36. package/dist/assets/host-C0NMNQLl.js.map +1 -0
  37. package/dist/assets/{hyperdhtRelay-CdsLcAhY.js → hyperdhtRelay-Dz7pvcBA.js} +2 -2
  38. package/dist/assets/{hyperdhtRelay-CdsLcAhY.js.map → hyperdhtRelay-Dz7pvcBA.js.map} +1 -1
  39. package/dist/assets/{irohWeb-D2IuloLL.js → irohWeb-caAj4Dms.js} +7 -7
  40. package/dist/assets/{irohWeb-D2IuloLL.js.map → irohWeb-caAj4Dms.js.map} +1 -1
  41. package/dist/assets/{main-C0PfjKTe.js → main-C6t5zrD5.js} +298 -92
  42. package/dist/assets/main-C6t5zrD5.js.map +1 -0
  43. package/dist/assets/{mainline-Cu61qcPS.js → mainline-DCuGUSZF.js} +2 -2
  44. package/dist/assets/{mainline-Cu61qcPS.js.map → mainline-DCuGUSZF.js.map} +1 -1
  45. package/dist/assets/{networkReason-Dt7Yx_n4.js → networkReason-BbQW1Xpc.js} +3 -3
  46. package/dist/assets/{networkReason-Dt7Yx_n4.js.map → networkReason-BbQW1Xpc.js.map} +1 -1
  47. package/dist/assets/{nwc-CtZypDcv.js → nwc-CSDRMZ0e.js} +3 -3
  48. package/dist/assets/{nwc-CtZypDcv.js.map → nwc-CSDRMZ0e.js.map} +1 -1
  49. package/dist/assets/{nwcProvider-DYHkd0iu.js → nwcProvider-PE0wli8n.js} +2 -2
  50. package/dist/assets/{nwcProvider-DYHkd0iu.js.map → nwcProvider-PE0wli8n.js.map} +1 -1
  51. package/dist/assets/{ownDid-CJ0ta8CL.js → ownDid-C1R5Kd8q.js} +2 -2
  52. package/dist/assets/{ownDid-CJ0ta8CL.js.map → ownDid-C1R5Kd8q.js.map} +1 -1
  53. package/dist/assets/{ownStatus-D4HC6tNR.js → ownStatus-BoTf5WqQ.js} +19 -6
  54. package/dist/assets/ownStatus-BoTf5WqQ.js.map +1 -0
  55. package/dist/assets/{profiles-Ccdq6Xf6.js → profiles-DszHjEJi.js} +25 -10
  56. package/dist/assets/profiles-DszHjEJi.js.map +1 -0
  57. package/dist/assets/{registry-LvxsLJ5S.js → registry-Da-r6eC3.js} +17 -10
  58. package/dist/assets/registry-Da-r6eC3.js.map +1 -0
  59. package/dist/assets/{registry-CE0PpQAE.js → registry-zUZBSHYv.js} +1 -1
  60. package/dist/assets/{resolve-6kVu2DE5.js → resolve-tUzh4lHh.js} +2 -2
  61. package/dist/assets/{resolve-6kVu2DE5.js.map → resolve-tUzh4lHh.js.map} +1 -1
  62. package/dist/assets/{secretGuard-B7Hj0Kvz.js → secretGuard-CsVHDdYk.js} +68 -6
  63. package/dist/assets/{secretGuard-B7Hj0Kvz.js.map → secretGuard-CsVHDdYk.js.map} +1 -1
  64. package/dist/assets/secretGuard-DCb1I0_B.js +2 -0
  65. package/dist/assets/{server-Q3VcNqGt.js → server-C1jZZ61O.js} +667 -196
  66. package/dist/assets/server-C1jZZ61O.js.map +1 -0
  67. package/dist/assets/{src-DjVj2ynY.js → src-DJe-OYxd.js} +826 -198
  68. package/dist/assets/src-DJe-OYxd.js.map +1 -0
  69. package/dist/assets/{sshForges-DeZ9cN9Q.js → sshForges-C9zTO72R.js} +2 -2
  70. package/dist/assets/{sshForges-DeZ9cN9Q.js.map → sshForges-C9zTO72R.js.map} +1 -1
  71. package/dist/assets/{translate-iQZq4TnY.js → translate-CUuC5szR.js} +3 -3
  72. package/dist/assets/{translate-iQZq4TnY.js.map → translate-CUuC5szR.js.map} +1 -1
  73. package/dist/assets/{usdt-DnyTGdTX.js → usdt-DI51znt-.js} +3 -3
  74. package/dist/assets/{usdt-DnyTGdTX.js.map → usdt-DI51znt-.js.map} +1 -1
  75. package/dist/ghostly.mjs +1 -1
  76. package/npm-shrinkwrap.json +2 -2
  77. package/package.json +1 -1
  78. package/dist/assets/api-DBlaoz8i.js.map +0 -1
  79. package/dist/assets/apiKit-DesObIDf.js.map +0 -1
  80. package/dist/assets/apps-DkMjDHML.js.map +0 -1
  81. package/dist/assets/backup-C0OJCJlM.js.map +0 -1
  82. package/dist/assets/buttons-Cco0Agfa.js +0 -2
  83. package/dist/assets/client-Ibk3-e60.js.map +0 -1
  84. package/dist/assets/errors-Dt-ZnyOX.js.map +0 -1
  85. package/dist/assets/host-CYlIiY3r.js.map +0 -1
  86. package/dist/assets/main-C0PfjKTe.js.map +0 -1
  87. package/dist/assets/ownStatus-D4HC6tNR.js.map +0 -1
  88. package/dist/assets/profiles-Ccdq6Xf6.js.map +0 -1
  89. package/dist/assets/registry-LvxsLJ5S.js.map +0 -1
  90. package/dist/assets/secretGuard-DUr6bgKc.js +0 -2
  91. package/dist/assets/server-Q3VcNqGt.js.map +0 -1
  92. package/dist/assets/src-DjVj2ynY.js.map +0 -1
@@ -1,13 +1,13 @@
1
- import { $r as STATUS_IDLE_AFTER_MS, As as buildSdpFromSignal, Es as CALL_SIGNAL_MAX_AGE_MS, Fs as parseCallSignal, Is as signalHasVideo, Ms as callSignalHeardAt, Na as ACTIVE_DHT_POLL_MS, Os as RTC_CONFIG, Ps as extractParamsFromSdp, Ta as isPublicAddress, Ti as checkPushEndpoint, Yu as setLinkTraceSink, bd as fromBase64Url, dd as setPublicKeyFastPath, ds as DAEMON_WATCH_PEER_IDLE_MS, js as callRtcConfig, ks as answersOffer } from "./src-DjVj2ynY.js";
1
+ import { As as RTC_CONFIG, Da as isPublicAddress, Di as checkPushEndpoint, Fa as ACTIVE_DHT_POLL_MS, Is as extractParamsFromSdp, Ls as parseCallSignal, Ms as buildSdpFromSignal, Ns as callRtcConfig, Os as CALL_SIGNAL_MAX_AGE_MS, Ps as callSignalHeardAt, Rs as signalHasVideo, Xu as setLinkTraceSink, fd as setPublicKeyFastPath, js as answersOffer, ps as DAEMON_WATCH_PEER_IDLE_MS, ti as STATUS_IDLE_AFTER_MS, xd as fromBase64Url } from "./src-DJe-OYxd.js";
2
2
  import { p as fileStore } from "./idb-CEuoNsTv.js";
3
- import { r as irohRelayProblem } from "./irohWeb-D2IuloLL.js";
4
- import { n as fedimintSdkOn } from "./fedimintSdk-zV_5Qdqz.js";
5
- import { r as asCliError, t as CliError } from "./errors-Dt-ZnyOX.js";
6
- import { A as sinceSeq, s as findChat, u as findOwnNamedChat } from "./apiKit-DesObIDf.js";
7
- import { _ as groupPayTwins, d as servedApps, f as nodeLocalFetch, g as groupMessageJson, h as groupJson, l as paymentJson, m as chatMessageJson, p as chatJson, r as callApi, u as reportAppFrames, v as resumeHolds, y as flushCardUpdates } from "./api-DBlaoz8i.js";
8
- import { C as ownProfile, E as runtimePath, S as ownFolder, T as privateFolder, b as startingPath, n as RESTORED_MARK, r as acquireLock } from "./profiles-Ccdq6Xf6.js";
9
- import { p as nativePublicKey } from "./apps-DkMjDHML.js";
10
- import { _ as frameSamples, c as greeting, d as removeDaemonSecret, g as frameBytes, h as bytesToMs, l as newDaemonSecret, m as PlaybackQueue, p as DEFAULT_RATE, u as proof, v as isCallRate, y as monotonic } from "./client-Ibk3-e60.js";
3
+ import { d as parsePayLink, r as irohRelayProblem } from "./irohWeb-caAj4Dms.js";
4
+ import { n as fedimintSdkOn } from "./fedimintSdk-CPFfVK-G.js";
5
+ import { r as asCliError, t as CliError } from "./errors-CAh6GLJZ.js";
6
+ import { M as sinceSeq, s as findChat, u as findOwnNamedChat } from "./apiKit-CfsNx9ZR.js";
7
+ import { _ as groupPayTwins, d as servedApps, f as nodeLocalFetch, g as groupMessageJson, h as groupJson, l as paymentJson, m as chatMessageJson, p as chatJson, r as callApi, u as reportAppFrames, v as resumeHolds, y as flushCardUpdates } from "./api-MYc6QNRs.js";
8
+ import { C as ownFolder, D as runtimePath, E as privateFolder, n as RESTORED_MARK, r as acquireLock, w as ownProfile, x as startingPath } from "./profiles-DszHjEJi.js";
9
+ import { p as nativePublicKey } from "./apps-h12II1us.js";
10
+ import { _ as frameBytes, b as monotonic, d as proof, f as removeDaemonSecret, g as bytesToMs, h as PlaybackQueue, l as greeting, m as DEFAULT_RATE, u as newDaemonSecret, v as frameSamples, y as isCallRate } from "./client-DY8tZZWi.js";
11
11
  import { createRequire } from "node:module";
12
12
  import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, fsync, fsyncSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, statSync, truncateSync, writeFileSync, writeSync } from "node:fs";
13
13
  import { open, readFile } from "node:fs/promises";
@@ -22,8 +22,8 @@ import { Worker } from "node:worker_threads";
22
22
  import { request } from "node:https";
23
23
  import { lookup } from "node:dns";
24
24
  import { WebSocket } from "ws";
25
- import { deserialize, serialize } from "node:v8";
26
- import { IDBFactory, IDBKeyRange, IDBTransaction } from "fake-indexeddb";
25
+ import { DefaultSerializer, deserialize, serialize } from "node:v8";
26
+ import { IDBFactory, IDBKeyRange as IDBKeyRange$1, IDBTransaction } from "fake-indexeddb";
27
27
  //#region src/announce.ts
28
28
  /**
29
29
  * What the app's chat screen does by itself, done here for a profile no screen shows (apps/ui/src/hooks/useChat.ts): the side
@@ -34,7 +34,7 @@ function announceJoins(hub, node) {
34
34
  const say = (chat) => {
35
35
  if (hub.said(chat, "join")) return;
36
36
  hub.markSaid(chat, "join");
37
- const nick = node.getState().settings.nick?.trim();
37
+ const nick = node.getSettings().nick?.trim();
38
38
  node.sendMessage({
39
39
  linkId: chat,
40
40
  text: nick ? `👋 ${nick} joined` : "👋 joined"
@@ -599,6 +599,57 @@ function envSwitch(name, fallback, env = process.env) {
599
599
  throw new CliError("usage", `${name} takes 0 or 1 (or false/true, off/on, no/yes), not ${JSON.stringify(value)}`);
600
600
  }
601
601
  //#endregion
602
+ //#region src/runtime/rtcThreads.ts
603
+ /**
604
+ * How long no WebRTC connection is open before libdatachannel's threads are let go. For tests,
605
+ * `GHOSTLY_TEST_RTC_IDLE_MS=<ms>` sets it.
606
+ */
607
+ var RTC_IDLE_MS = Number(process.env.GHOSTLY_TEST_RTC_IDLE_MS) || 6e4;
608
+ /**
609
+ * libdatachannel's threads (usrsctp's timer, a tick every 10 ms, and a pool of a thread per core) start with the
610
+ * process's first WebRTC connection and stay for its life, every connection closed or not: a daemon whose contacts all
611
+ * went away woke about 100 times a second for good. Every connection made here (a chat's data link, a call's) is
612
+ * counted from its making to its close; once none has been open for `idleMs`, node-datachannel's `cleanup()` lets them
613
+ * go, and the next connection starts them again. `cleanup()` closes every connection there is: it runs only with none
614
+ * open, checked again once it has been loaded.
615
+ */
616
+ var RtcThreads = class {
617
+ cleanup;
618
+ idleMs;
619
+ open = 0;
620
+ timer;
621
+ constructor(cleanup, idleMs = RTC_IDLE_MS) {
622
+ this.cleanup = cleanup;
623
+ this.idleMs = idleMs;
624
+ }
625
+ /** No connection is open. */
626
+ get idle() {
627
+ return this.open === 0;
628
+ }
629
+ /** A connection made: what to call once it is closed (once; more calls do nothing). */
630
+ opened() {
631
+ this.open++;
632
+ clearTimeout(this.timer);
633
+ this.timer = void 0;
634
+ let closed = false;
635
+ return () => {
636
+ if (closed) return;
637
+ closed = true;
638
+ if (--this.open > 0) return;
639
+ this.timer = setTimeout(() => {
640
+ this.timer = void 0;
641
+ if (this.open === 0) Promise.resolve(this.cleanup()).catch(() => {});
642
+ }, this.idleMs);
643
+ this.timer.unref?.();
644
+ };
645
+ }
646
+ };
647
+ /** The process's one count: the chats' connections and the calls' share libdatachannel's threads. */
648
+ var rtcThreads = new RtcThreads(async () => {
649
+ const ndc = await import("node-datachannel");
650
+ if (rtcThreads.idle) (ndc.cleanup ?? ndc.default.cleanup)();
651
+ });
652
+ //#endregion
602
653
  //#region src/calls/media.ts
603
654
  var loading = null;
604
655
  var OPUS_APPLICATION_VOIP = 2048;
@@ -742,6 +793,7 @@ async function openConnection(stack, options, offer, held = false) {
742
793
  iceServers,
743
794
  ...bind ? { bindAddress: bind } : {}
744
795
  });
796
+ const released = rtcThreads.opened();
745
797
  const born = Date.now();
746
798
  const sink = options.log ?? (() => {});
747
799
  const log = (line) => sink(`+${Date.now() - born}ms ${line}`);
@@ -817,7 +869,8 @@ async function openConnection(stack, options, offer, held = false) {
817
869
  },
818
870
  closed: false,
819
871
  failed: false,
820
- onState: null
872
+ onState: null,
873
+ released
821
874
  };
822
875
  log(`candidates here: ${(local.c ?? []).join(" | ") || "none"}`);
823
876
  try {
@@ -825,7 +878,11 @@ async function openConnection(stack, options, offer, held = false) {
825
878
  } catch {}
826
879
  return conn;
827
880
  } catch (error) {
828
- pc.close();
881
+ try {
882
+ pc.close();
883
+ } finally {
884
+ released();
885
+ }
829
886
  throw error;
830
887
  }
831
888
  }
@@ -840,6 +897,7 @@ function closeConnection(conn) {
840
897
  try {
841
898
  conn.pc.close();
842
899
  } catch {}
900
+ conn.released();
843
901
  }
844
902
  /**
845
903
  * The answer's connection, made while the call rings: its description and candidates are ready when the call is
@@ -915,9 +973,7 @@ var CallMedia = class CallMedia {
915
973
  payloadType: conn.payloadType,
916
974
  ssrc: conn.ssrc,
917
975
  track: {
918
- send: (packet) => {
919
- if (track.isOpen()) track.sendMessageBinary(packet);
920
- },
976
+ send: trackSend(track),
921
977
  onPacket: (listener) => listeners.push(listener)
922
978
  },
923
979
  encoder: stack.opus(options.rate),
@@ -976,6 +1032,19 @@ function mdnsNote(signal, log) {
976
1032
  log(`${host}: an mDNS name, not resolved here: the call connects only if the contact's checks reach this side`);
977
1033
  }
978
1034
  }
1035
+ /**
1036
+ * Sends a call's packets on its track, while it is open. A track can close between `isOpen` and the send (the
1037
+ * connection failing), and libdatachannel then throws "Track is not open" from the audio's timer, where nothing catches
1038
+ * it: that frame is dropped, as one sent while the track is closed is.
1039
+ */
1040
+ function trackSend(track) {
1041
+ return (packet) => {
1042
+ if (!track.isOpen()) return;
1043
+ try {
1044
+ track.sendMessageBinary(packet);
1045
+ } catch {}
1046
+ };
1047
+ }
979
1048
  /** Whether a call's ICE servers (`callIceServers`) have a TURN server: its relay candidate is then the one waited for. */
980
1049
  function callUsesTurn(servers) {
981
1050
  return servers.some((server) => typeof server !== "string");
@@ -1333,6 +1402,7 @@ var CallManager = class {
1333
1402
  return this.view(call);
1334
1403
  }
1335
1404
  async answer(ref, options) {
1405
+ if (this.host.answers === false) throw new CliError("unavailable", CALLS_NEED_DAEMON);
1336
1406
  const call = this.find(ref, (c) => c.direction === "in" && c.state === "ringing");
1337
1407
  if (call.direction !== "in" || call.state !== "ringing") throw new CliError("refused", "That call is not ringing here");
1338
1408
  this.callable(call.chat);
@@ -1355,7 +1425,7 @@ var CallManager = class {
1355
1425
  v: 0
1356
1426
  });
1357
1427
  } catch (error) {
1358
- await this.end(call, "failed", true);
1428
+ await this.end(call, "failed", "unreachable");
1359
1429
  throw error;
1360
1430
  } finally {
1361
1431
  early?.close();
@@ -1382,6 +1452,7 @@ var CallManager = class {
1382
1452
  }
1383
1453
  /** Drops the audio the program queued and the call has not sent yet (barge-in). */
1384
1454
  flush(ref) {
1455
+ if (this.host.answers === false) throw new CliError("unavailable", CALLS_NEED_DAEMON);
1385
1456
  const call = this.find(ref);
1386
1457
  const flushedMs = call.queue?.flush() ?? 0;
1387
1458
  return {
@@ -1519,7 +1590,7 @@ var CallManager = class {
1519
1590
  this.refused(call, "the connection failed as the answer went in");
1520
1591
  return;
1521
1592
  }
1522
- this.arm(call, MEDIA_GRACE_MS, state === "closed" ? "remote-hangup" : "failed", false);
1593
+ this.arm(call, MEDIA_GRACE_MS, state === "closed" ? "remote-hangup" : "failed", state !== "closed");
1523
1594
  }
1524
1595
  });
1525
1596
  if (call.ended || attempt !== call.attempt) {
@@ -1596,6 +1667,7 @@ var CallManager = class {
1596
1667
  }, every);
1597
1668
  call.watcher.unref?.();
1598
1669
  }
1670
+ /** `tell`: the contact is sent a hang-up; `"unreachable"`, one that says this side could not connect (`r: "u"`, WISP 601). */
1599
1671
  async end(call, reason, tell, report = true) {
1600
1672
  if (call.ended) return;
1601
1673
  call.ended = true;
@@ -1619,10 +1691,15 @@ var CallManager = class {
1619
1691
  call.early = null;
1620
1692
  const closing = call.socket?.close();
1621
1693
  if (tell) {
1622
- await this.send(call.chat, {
1694
+ const hangUp = tell === "unreachable" || reason === "failed" && call.connectedAt === null ? {
1695
+ t: "h",
1696
+ ts: this.stamp(call.chat),
1697
+ r: "u"
1698
+ } : {
1623
1699
  t: "h",
1624
1700
  ts: this.stamp(call.chat)
1625
- }).catch(() => {});
1701
+ };
1702
+ await this.send(call.chat, hangUp).catch(() => {});
1626
1703
  if (!this.stopping) {
1627
1704
  const timer = setTimeout(() => {
1628
1705
  this.clearing.delete(call.chat);
@@ -1755,6 +1832,10 @@ var CallManager = class {
1755
1832
  const link = this.link(this.chatId(ref));
1756
1833
  if (this.host.answers === false) throw new CliError("unavailable", CALLS_NEED_DAEMON);
1757
1834
  if (!link.profile) throw new CliError("unavailable", "Calls are in chats made since 0.4 (the chat session): this is a compatibility chat");
1835
+ if (link.callsUnavailable === CALLS_NEED_LIVE) {
1836
+ const shown = /^[\w.:-]+$/.test(link.label ?? "") ? link.label : link.id;
1837
+ throw new CliError("unavailable", `${CALLS_NEED_LIVE}, and your contact's app is not connected now: ghostly chat wait ${shown} --until live waits for it, then call again`, { chat: link.id });
1838
+ }
1758
1839
  if (link.callsUnavailable) throw new CliError("unavailable", link.callsUnavailable);
1759
1840
  if (this.host.engine.getState().settings?.relayOnly === true) throw new CliError("unavailable", CALLS_OFF_RELAY_ONLY);
1760
1841
  return link;
@@ -1764,6 +1845,8 @@ var CallManager = class {
1764
1845
  }
1765
1846
  };
1766
1847
  var CALLS_NEED_DAEMON = "Calls need the daemon (a call outlives this command): start it with `ghostly daemon --detach`";
1848
+ /** The engine's `callsUnavailable` for a chat with no live session (ghostlink's, or node.ts's with no link running). */
1849
+ var CALLS_NEED_LIVE = "Calls need a live connection";
1767
1850
  /**
1768
1851
  * Hide my IP from contacts (`relayOnly`, WISP 601 Relay only): libdatachannel's relay policy only keeps the other
1769
1852
  * candidates out of the signal; its ICE still checks from this machine's own sockets, and a call connected on a direct
@@ -1920,6 +2003,18 @@ var EventHub = class {
1920
2003
  /** `file.*` events whose message was not seen yet: they wait for it a moment (the engine stores it right after). */
1921
2004
  pendingFiles = /* @__PURE__ */ new Map();
1922
2005
  baselined = false;
2006
+ /** Whether the chats and groups are known (`perChat`, `baseline`): a change to them is an event from then on. */
2007
+ stateKnown = false;
2008
+ /**
2009
+ * How a chat or group is caught up when its history's first event comes (`perChat`): until then its notes are not
2010
+ * read. Null when the baseline took them all.
2011
+ */
2012
+ chatReads = null;
2013
+ /** Chats and groups caught up (`perChat`): their events are handled as they come. */
2014
+ caught = /* @__PURE__ */ new Set();
2015
+ /** Chats and groups being caught up, and the events of their histories that wait for it, in order. */
2016
+ behind = /* @__PURE__ */ new Map();
2017
+ catchingUp = /* @__PURE__ */ new Set();
1923
2018
  /** Chats and groups whose history the baseline did not read whole, and how to read their file rows: until a file event needs them. */
1924
2019
  unread = /* @__PURE__ */ new Map();
1925
2020
  /**
@@ -1938,8 +2033,11 @@ var EventHub = class {
1938
2033
  this.now = now;
1939
2034
  this.seenDb = seenDb;
1940
2035
  }
1941
- /** Loads the journal's last seq and the seen set; call before `sink` is attached. */
1942
- async open() {
2036
+ /**
2037
+ * Loads the journal's last seq and the seen set; call before `sink` is attached. `seen: false`: the seen set is read
2038
+ * later, whole (`loadSeen`) or as each chat needs it (`loadOwnNotes`, `perChat`).
2039
+ */
2040
+ async open({ seen = true } = {}) {
1943
2041
  const reserved = readDropped(this.journalPath + ".reserved") ?? 0;
1944
2042
  try {
1945
2043
  const dropped = readDropped(this.journalPath + ".dropped");
@@ -1982,7 +2080,27 @@ var EventHub = class {
1982
2080
  this.seq = reserved;
1983
2081
  this.markDropped(Math.max(this.dropped, reserved));
1984
2082
  }
1985
- this.db = await new Promise((resolve, reject) => {
2083
+ if (seen) await this.loadSeen();
2084
+ }
2085
+ /** Reads the whole seen set (`open` with `seen: false`); call before `baseline`. */
2086
+ async loadSeen() {
2087
+ await this.openSeen();
2088
+ await this.readNotes();
2089
+ }
2090
+ /**
2091
+ * Reads only the seen set's own notes (its marks, the transfers under way, what this side said once), for `perChat`:
2092
+ * true when that can catch the chats up one by one. Not on a first run, nor on a set without its marks, which the
2093
+ * baseline reads whole once (`loadSeen`).
2094
+ */
2095
+ async loadOwnNotes() {
2096
+ await this.openSeen();
2097
+ await this.readNotes(IDBKeyRange.bound([FORMAT], [FORMAT, []]));
2098
+ await this.readNotes(IDBKeyRange.bound([UNDERWAY], [UNDERWAY, []]));
2099
+ await this.readNotes(IDBKeyRange.bound(["said:"], ["said;"], false, true));
2100
+ return !this.firstRun && this.mentionsNoted && this.agesNoted;
2101
+ }
2102
+ async openSeen() {
2103
+ this.db ??= await new Promise((resolve, reject) => {
1986
2104
  const request = indexedDB.open(this.seenDb, 1);
1987
2105
  request.onupgradeneeded = () => {
1988
2106
  request.result.createObjectStore(SEEN);
@@ -1991,12 +2109,15 @@ var EventHub = class {
1991
2109
  request.onsuccess = () => resolve(request.result);
1992
2110
  request.onerror = () => reject(request.error);
1993
2111
  });
2112
+ }
2113
+ /** The notes in `range` (all with none) into memory. */
2114
+ async readNotes(range) {
1994
2115
  const store = this.db.transaction(SEEN).objectStore(SEEN);
1995
2116
  const all = (request) => new Promise((resolve, reject) => {
1996
2117
  request.onsuccess = () => resolve(request.result);
1997
2118
  request.onerror = () => reject(request.error);
1998
2119
  });
1999
- const [keys, values] = await Promise.all([all(store.getAllKeys()), all(store.getAll())]);
2120
+ const [keys, values] = await Promise.all([all(store.getAllKeys(range)), all(store.getAll(range))]);
2000
2121
  for (let i = 0; i < keys.length; i++) {
2001
2122
  const [chat, id] = keys[i];
2002
2123
  this.seenOf(chat).set(id, values[i]);
@@ -2019,6 +2140,7 @@ var EventHub = class {
2019
2140
  */
2020
2141
  baseline(state, histories, rest) {
2021
2142
  this.stateChanged(state);
2143
+ this.stateKnown = true;
2022
2144
  for (const [chat, messages] of histories) this.messages(chat, messages);
2023
2145
  for (const [chat, messages] of rest?.newest ?? []) {
2024
2146
  this.messages(chat, messages, []);
@@ -2038,13 +2160,73 @@ var EventHub = class {
2038
2160
  }
2039
2161
  }
2040
2162
  /**
2163
+ * The baseline one chat or group at a time, after `loadOwnNotes` said it can: the chats and groups are known now, and
2164
+ * each one's notes are read (by key range) and its history caught up as the baseline would (`reads`) when the first
2165
+ * event of its history comes; its events wait for that. A one-shot hears a few chats at most: the whole set, a note
2166
+ * per message active in the last 30 days, took a `message send` 0.07 s and 56 MB at 50,000 notes.
2167
+ */
2168
+ perChat(state, reads) {
2169
+ this.chatReads = reads;
2170
+ this.stateChanged(state);
2171
+ this.stateKnown = true;
2172
+ this.baselined = true;
2173
+ for (const id of [...state.links.map((link) => link.id), ...state.groups.map((group) => `group:${group.id}`)]) this.unread.set(id, reads.files);
2174
+ }
2175
+ /** The catch-ups still running (`perChat`): their events are told once they end. */
2176
+ async caughtUp() {
2177
+ while (this.catchingUp.size) await Promise.all(this.catchingUp);
2178
+ }
2179
+ /** An event of a history not caught up yet (`perChat`): it waits, and the first one starts the catch-up. */
2180
+ waits(message) {
2181
+ if (!this.chatReads || message.kind !== "messages" && message.kind !== "message-changes") return false;
2182
+ const chat = message.linkId;
2183
+ if (this.caught.has(chat)) return false;
2184
+ const waiting = this.behind.get(chat);
2185
+ if (waiting) {
2186
+ waiting.push(message);
2187
+ return true;
2188
+ }
2189
+ this.behind.set(chat, [message]);
2190
+ const running = this.catchUp(chat, message.kind === "messages" && !message.more).catch((error) => {
2191
+ process.stderr.write(`ghostly: event error: ${error instanceof Error ? error.stack : String(error)}\n`);
2192
+ }).then(() => {
2193
+ this.caught.add(chat);
2194
+ const events = this.behind.get(chat) ?? [];
2195
+ this.behind.delete(chat);
2196
+ if (chat.startsWith("group:") ? this.groups.has(chat.slice(6)) : this.chats.has(chat)) for (const event of events) this.sink.post(event);
2197
+ this.catchingUp.delete(running);
2198
+ });
2199
+ this.catchingUp.add(running);
2200
+ return true;
2201
+ }
2202
+ /**
2203
+ * One chat's or group's notes, and its history as the baseline takes it (`baseline`): its newest page when the history
2204
+ * is in step with them (`inStep`), else whole. `whole`: the event that waits is its whole history, which says it all.
2205
+ */
2206
+ async catchUp(chat, whole) {
2207
+ await this.readNotes(IDBKeyRange.bound([chat], [chat, []]));
2208
+ await this.readNotes(IDBKeyRange.bound([`reactions:${chat}`], [`reactions:${chat}`, []]));
2209
+ const reads = this.chatReads;
2210
+ if (whole) this.unread.delete(chat);
2211
+ else {
2212
+ const page = await reads.newest(chat);
2213
+ if (page && !page.more) this.messages(chat, page.messages);
2214
+ else if (page && this.inStep(chat, page.messages)) this.messages(chat, page.messages, []);
2215
+ else {
2216
+ this.messages(chat, await reads.whole(chat));
2217
+ this.unread.delete(chat);
2218
+ }
2219
+ }
2220
+ this.sweep([chat, `reactions:${chat}`]);
2221
+ }
2222
+ /**
2041
2223
  * Drops the notes of settled messages, and of reactions, whose last activity is older than `SEEN_KEEP_MS`: from memory
2042
2224
  * and from the store. A message of mine still on its way (or failed, which a retry sends again) keeps its note. The
2043
2225
  * 30 days count back from the newest activity the set knows when the clock reads later than it: a clock run ahead (a
2044
2226
  * wrong RTC, a VM restored with a skewed clock) would otherwise drop the notes of the recent history, which the next
2045
2227
  * start with the clock right reports again.
2046
2228
  */
2047
- sweep() {
2229
+ sweep(only) {
2048
2230
  let newest = 0;
2049
2231
  for (const [chat, known] of this.seen) {
2050
2232
  if (chat === FORMAT || chat.startsWith("said:")) continue;
@@ -2052,8 +2234,9 @@ var EventHub = class {
2052
2234
  }
2053
2235
  const cutoff = Math.min(this.now(), newest) - SEEN_KEEP_MS;
2054
2236
  let dropped = 0;
2055
- for (const [chat, known] of this.seen) {
2056
- if (chat === FORMAT || chat === UNDERWAY || chat.startsWith("said:")) continue;
2237
+ for (const chat of only ?? [...this.seen.keys()]) {
2238
+ const known = this.seen.get(chat);
2239
+ if (!known || chat === FORMAT || chat === UNDERWAY || chat.startsWith("said:")) continue;
2057
2240
  const reactions = chat.startsWith("reactions:");
2058
2241
  const gone = [];
2059
2242
  for (const [id, note] of known) {
@@ -2382,6 +2565,7 @@ var EventHub = class {
2382
2565
  return from === Infinity ? void 0 : from;
2383
2566
  }
2384
2567
  handle(message) {
2568
+ if (this.waits(message)) return;
2385
2569
  switch (message.kind) {
2386
2570
  case "state":
2387
2571
  this.stateChanged(message.state);
@@ -2407,7 +2591,7 @@ var EventHub = class {
2407
2591
  }
2408
2592
  }
2409
2593
  stateChanged(state) {
2410
- const quiet = !this.baselined;
2594
+ const quiet = !this.stateKnown;
2411
2595
  const links = new Map(state.links.map((link) => [link.id, link]));
2412
2596
  for (const [id, link] of links) {
2413
2597
  const shape = chatShape(link), before = this.chats.get(id);
@@ -2458,8 +2642,12 @@ var EventHub = class {
2458
2642
  }
2459
2643
  const removedChats = [...this.chats.keys()].filter((id) => !links.has(id));
2460
2644
  for (const id of removedChats) {
2645
+ const peer = this.chats.get(id)?.peer;
2461
2646
  this.chats.delete(id);
2462
- if (!quiet) this.emit("chat.removed", `chat.removed:${id}:${this.now()}`, { chat: id });
2647
+ if (!quiet) this.emit("chat.removed", `chat.removed:${id}:${this.now()}`, {
2648
+ chat: id,
2649
+ ...peer ? { peer } : {}
2650
+ });
2463
2651
  }
2464
2652
  const chatsGone = new Set(removedChats);
2465
2653
  this.forget(removedChats, (event) => chatsGone.has(event.chat) && event.type !== "chat.removed");
@@ -2479,8 +2667,7 @@ var EventHub = class {
2479
2667
  group: id,
2480
2668
  status: shape.status
2481
2669
  });
2482
- const joined = shape.members.filter((key) => !before.members.includes(key));
2483
- const left = before.members.filter((key) => !shape.members.includes(key));
2670
+ const { joined, left } = membersDiff(before.members, shape.members);
2484
2671
  if (joined.length || left.length) this.emit("group.members", `group.members:${id}:${this.now()}`, {
2485
2672
  group: id,
2486
2673
  joined,
@@ -2508,11 +2695,17 @@ var EventHub = class {
2508
2695
  }
2509
2696
  const groupsGone = new Set(removedGroups);
2510
2697
  this.forget(removedGroups, (event) => groupsGone.has(event.group) && event.type !== "group.removed");
2698
+ let edgeGroups;
2699
+ const groupOf = (linkId) => (edgeGroups ??= new Map(state.groups.flatMap((g) => g.members.flatMap((m) => m.edge ? [[m.edge.linkId, g.id]] : [])))).get(linkId) ?? parsePayLink(linkId)?.groupId;
2511
2700
  for (const [id, payment] of Object.entries(state.payments ?? {})) {
2512
2701
  const shape = `${payment.state}|${payment.error ?? ""}|${payment.closed ? 1 : 0}`, before = this.payments.get(id);
2513
2702
  this.payments.set(id, shape);
2514
2703
  if (quiet || before === shape) continue;
2515
- const json = paymentJson(payment);
2704
+ const group = payment.group ?? groupOf(payment.linkId);
2705
+ const json = {
2706
+ ...paymentJson(payment),
2707
+ ...group && { group }
2708
+ };
2516
2709
  if (before === void 0) this.emit("payment.created", `payment.created:${id}`, {
2517
2710
  chat: payment.linkId,
2518
2711
  payment: json
@@ -3043,7 +3236,8 @@ function chatShape(link) {
3043
3236
  typing: !!link.peerTyping,
3044
3237
  typingKind: link.peerTyping ? link.peerTypingKind ?? "typing" : "typing",
3045
3238
  typingStatus: link.peerTyping ? link.peerTypingStatus ?? null : null,
3046
- pin: link.pin ?? null
3239
+ pin: link.pin ?? null,
3240
+ peer: view.peer || null
3047
3241
  };
3048
3242
  }
3049
3243
  /** A `typing.started`'s word: the kind, and the contact's status line when it gave one (already cleaned). */
@@ -3065,6 +3259,25 @@ function groupShape(group) {
3065
3259
  };
3066
3260
  }
3067
3261
  /**
3262
+ * Who joined and who left between two of `groupShape`'s sorted member lists, in one walk of both: a state comes up to
3263
+ * every 50 ms, and a full community has 256 members, so a lookup of each key in the other list would cost each state
3264
+ * O(members²) per group though nothing changed.
3265
+ */
3266
+ function membersDiff(before, after) {
3267
+ const joined = [], left = [];
3268
+ let i = 0, j = 0;
3269
+ while (i < before.length || j < after.length) if (j >= after.length || i < before.length && before[i] < after[j]) left.push(before[i++]);
3270
+ else if (i >= before.length || after[j] < before[i]) joined.push(after[j++]);
3271
+ else {
3272
+ i++;
3273
+ j++;
3274
+ }
3275
+ return {
3276
+ joined,
3277
+ left
3278
+ };
3279
+ }
3280
+ /**
3068
3281
  * The journal's whole lines within its last `JOURNAL_MAX_BYTES`, and its size: never the whole file as one string, so
3069
3282
  * a journal grown past what a string can hold (or a trim's bound) is still read, and in bounded time. Lines `forget`
3070
3283
  * blanked are left out.
@@ -3483,7 +3696,8 @@ var SAID = {
3483
3696
  "full.sent": "the group is full: told the joiner on the entry session",
3484
3697
  "full.received": "the group is full: knocking on until there is room",
3485
3698
  "welcome.sent": "welcome sent",
3486
- "welcome.received": "welcome received: in the group"
3699
+ "welcome.received": "welcome received: in the group",
3700
+ "join.unanswered": "nobody answered for an hour: knocks stopped (ghostly group accept knocks again)"
3487
3701
  };
3488
3702
  /** The log line for one join-trace line, or null when it is not said. */
3489
3703
  function joinLogLine(traced) {
@@ -4459,7 +4673,7 @@ var FOLLOWS = "__ghostly_journal__";
4459
4673
  var SEQ = "__ghostly_entry__";
4460
4674
  /** How long `close` waits for the transactions still running: one left open for good does not keep the process. */
4461
4675
  var CLOSE_WAIT_MS = 5e3;
4462
- /** Records of rows.db an export reads in one turn of the event loop: a few milliseconds' worth. */
4676
+ /** Records an export reads and serializes in one turn of the event loop, then writes: a few milliseconds' worth. */
4463
4677
  var EXPORT_PAGE = 256;
4464
4678
  /** How long a relaxed transaction's entry may wait for a sync when no other transaction brings one. */
4465
4679
  var RELAXED_SYNC_MS = 1e3;
@@ -4782,7 +4996,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4782
4996
  tree.clear();
4783
4997
  return deleted;
4784
4998
  };
4785
- if (index) records.deleteByValue = (key) => tree.deleteByValue(key instanceof IDBKeyRange ? key : IDBKeyRange.only(key));
4999
+ if (index) records.deleteByValue = (key) => tree.deleteByValue(key instanceof IDBKeyRange$1 ? key : IDBKeyRange$1.only(key));
4786
5000
  }
4787
5001
  /** A store whose records and index entries are in rows.db already (a snapshot of format 3): its trees attached. */
4788
5002
  function attachTrees(store) {
@@ -4922,18 +5136,69 @@ async function openPersistentIndexedDb(dir, options = {}) {
4922
5136
  throw error;
4923
5137
  }
4924
5138
  }
4925
- /**
4926
- * What is held, as a snapshot. `inline`: every value in it (format 1, `exportSnapshot`); otherwise the stores kept on
4927
- * disk say only that they are (format 3).
4928
- */
4929
- async function capture(journalName, inline) {
5139
+ /** A store's part of a snapshot, all but its records. */
5140
+ function storeMeta(store) {
5141
+ const live = [...store.rawIndexes.values()].filter((index) => !index.deleted);
5142
+ return {
5143
+ name: store.name,
5144
+ keyPath: store.keyPath,
5145
+ autoIncrement: store.autoIncrement,
5146
+ keyGenerator: store.keyGenerator?.num ?? null,
5147
+ indexes: live.map(({ name, keyPath, multiEntry, unique }) => ({
5148
+ name,
5149
+ keyPath,
5150
+ multiEntry,
5151
+ unique
5152
+ }))
5153
+ };
5154
+ }
5155
+ /** What is held, as a snapshot of format 3: the stores kept on disk say only that they are. */
5156
+ async function capture(journalName) {
4930
5157
  const snapshot = {
4931
5158
  format: 1,
4932
- ...journalName !== void 0 && { journal: journalName },
5159
+ journal: journalName,
4933
5160
  databases: []
4934
5161
  };
4935
5162
  const held = [];
4936
- const later = [];
5163
+ for (const db of raw._databases.values()) {
5164
+ if (db.name === PROBE) continue;
5165
+ const stores = [];
5166
+ for (const store of db.rawObjectStores.values()) {
5167
+ if (store.deleted) continue;
5168
+ if (disk(store)) {
5169
+ if (!onTrees(store)) toDisk(store);
5170
+ stores.push({
5171
+ ...storeMeta(store),
5172
+ records: [],
5173
+ onDisk: true
5174
+ });
5175
+ snapshot.format = 3;
5176
+ continue;
5177
+ }
5178
+ const saved = {
5179
+ ...storeMeta(store),
5180
+ records: []
5181
+ };
5182
+ for (const record of store.records.values()) saved.records.push([record.key, record.value]);
5183
+ stores.push(saved);
5184
+ held.push(saved);
5185
+ }
5186
+ snapshot.databases.push({
5187
+ name: db.name,
5188
+ version: db.version,
5189
+ stores
5190
+ });
5191
+ }
5192
+ for (const store of held) for (const record of store.records) record[1] = await blobsToDisk(record[1]);
5193
+ return snapshot;
5194
+ }
5195
+ /**
5196
+ * The whole store as a snapshot of format 1 (every value inline, `rows.db` read), written to `file` as it is read: a
5197
+ * page of records serialized and written at a time. Built whole and serialized in one turn, an export of 400,000
5198
+ * messages held the store as objects and its serialized copy at once (+340 MB) and the daemon for 390 ms.
5199
+ */
5200
+ async function exportTo(file) {
5201
+ const databases = [];
4937
5202
  let reader = null;
4938
5203
  try {
4939
5204
  for (const db of raw._databases.values()) {
@@ -4941,59 +5206,73 @@ async function openPersistentIndexedDb(dir, options = {}) {
4941
5206
  const stores = [];
4942
5207
  for (const store of db.rawObjectStores.values()) {
4943
5208
  if (store.deleted) continue;
4944
- const live = [...store.rawIndexes.values()].filter((index) => !index.deleted);
4945
- const meta = {
4946
- name: store.name,
4947
- keyPath: store.keyPath,
4948
- autoIncrement: store.autoIncrement,
4949
- keyGenerator: store.keyGenerator?.num ?? null,
4950
- indexes: live.map(({ name, keyPath, multiEntry, unique }) => ({
4951
- name,
4952
- keyPath,
4953
- multiEntry,
4954
- unique
4955
- }))
4956
- };
4957
- if (!inline && disk(store)) {
4958
- if (!onTrees(store)) toDisk(store);
5209
+ if (!onTrees(store)) {
4959
5210
  stores.push({
4960
- ...meta,
4961
- records: [],
4962
- onDisk: true
5211
+ meta: storeMeta(store),
5212
+ records: Array.from(store.records.values(), ({ key, value }) => ({
5213
+ key,
5214
+ value
5215
+ }))
4963
5216
  });
4964
- snapshot.format = 3;
4965
5217
  continue;
4966
5218
  }
4967
- const saved = {
4968
- ...meta,
4969
- records: []
4970
- };
4971
- stores.push(saved);
4972
- if (onTrees(store)) {
4973
- reader ??= openTreeReader(sqlite, rowsPath, classes.constraintError);
4974
- later.push([saved, store.records.records.at(reader)]);
4975
- } else {
4976
- for (const record of store.records.values()) saved.records.push([record.key, record.value]);
4977
- held.push(saved);
4978
- }
5219
+ reader ??= openTreeReader(sqlite, rowsPath, classes.constraintError);
5220
+ stores.push({
5221
+ meta: storeMeta(store),
5222
+ records: store.records.records.at(reader)
5223
+ });
4979
5224
  }
4980
- snapshot.databases.push({
5225
+ databases.push({
4981
5226
  name: db.name,
4982
5227
  version: db.version,
4983
5228
  stores
4984
5229
  });
4985
5230
  }
4986
- for (const store of held) for (const record of store.records) record[1] = await blobsToDisk(record[1]);
4987
- for (const [saved, tree] of later) for (const record of tree.getAllRecords()) if (saved.records.push([record.key, record.value]) % EXPORT_PAGE === 0) await new Promise((resolve) => setImmediate(resolve));
5231
+ const out = new SnapshotWriter(file);
5232
+ out.begin("o");
5233
+ out.value("format", 1);
5234
+ out.key("databases");
5235
+ out.begin("A", databases.length);
5236
+ for (const db of databases) {
5237
+ out.begin("o");
5238
+ out.value("name", db.name);
5239
+ out.value("version", db.version);
5240
+ out.key("stores");
5241
+ out.begin("A", db.stores.length);
5242
+ for (const { meta, records } of db.stores) {
5243
+ out.begin("o");
5244
+ for (const [key, value] of Object.entries(meta)) out.value(key, value);
5245
+ out.key("records");
5246
+ const count = Array.isArray(records) ? records.length : records.size();
5247
+ out.begin("A", count);
5248
+ let written = 0;
5249
+ for (const record of Array.isArray(records) ? records : records.getAllRecords()) {
5250
+ let value = Array.isArray(records) ? await blobsToDisk(record.value) : record.value;
5251
+ if (meta.name === "links" && value && typeof value === "object" && "ratchetState" in value) {
5252
+ const { ratchetState: _ratchet, ...link } = value;
5253
+ value = link;
5254
+ }
5255
+ out.item([record.key, value]);
5256
+ if (++written % EXPORT_PAGE === 0) await out.flush();
5257
+ }
5258
+ if (written !== count) throw new Error("The store changed while it was exported: try again");
5259
+ out.end("A", count);
5260
+ out.end("o", Object.keys(meta).length + 1);
5261
+ }
5262
+ out.end("A", db.stores.length);
5263
+ out.end("o", 3);
5264
+ }
5265
+ out.end("A", databases.length);
5266
+ out.end("o", 2);
5267
+ await out.flush();
4988
5268
  } finally {
4989
5269
  reader?.db.close();
4990
5270
  }
4991
- return snapshot;
4992
5271
  }
4993
5272
  async function snapshotToDisk() {
4994
5273
  await synced();
4995
5274
  const name = randomBytes(8).toString("hex");
4996
- const snapshot = await capture(name, false);
5275
+ const snapshot = await capture(name);
4997
5276
  if (trees) {
4998
5277
  trees.apply(seq);
4999
5278
  trees.sync();
@@ -5061,7 +5340,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5061
5340
  }
5062
5341
  const installed = globalThis;
5063
5342
  installed.indexedDB = factory;
5064
- installed.IDBKeyRange = IDBKeyRange;
5343
+ installed.IDBKeyRange = IDBKeyRange$1;
5065
5344
  return {
5066
5345
  factory,
5067
5346
  recovered: !replayed.closed,
@@ -5090,18 +5369,9 @@ async function openPersistentIndexedDb(dir, options = {}) {
5090
5369
  journalBytes,
5091
5370
  async exportSnapshot(path) {
5092
5371
  await settled();
5093
- const snapshot = await capture(void 0, true);
5094
- for (const db of snapshot.databases) for (const store of db.stores) {
5095
- if (store.name !== "links") continue;
5096
- store.records = store.records.map(([key, value]) => {
5097
- if (!value || typeof value !== "object" || !("ratchetState" in value)) return [key, value];
5098
- const { ratchetState: _ratchet, ...link } = value;
5099
- return [key, link];
5100
- });
5101
- }
5102
5372
  const file = await open(path, "w", 384);
5103
5373
  try {
5104
- await file.writeFile(serialize(snapshot));
5374
+ await exportTo(file);
5105
5375
  await file.sync();
5106
5376
  } finally {
5107
5377
  await file.close();
@@ -5113,6 +5383,109 @@ async function openPersistentIndexedDb(dir, options = {}) {
5113
5383
  function writeAll(fd, bytes) {
5114
5384
  for (let at = 0; at < bytes.length;) at += writeSync(fd, bytes, at);
5115
5385
  }
5386
+ /**
5387
+ * A value v8 would serialize whole, written in pieces: the objects and arrays around the records begun and ended here
5388
+ * by hand, as v8 itself writes them, and the records serialized a page at a time, each page's bytes to `file`. A reader
5389
+ * gets what `deserialize` of `serialize(value)` gives (the bytes differ at most by v8's alignment padding before a
5390
+ * two-byte string, which it skips): any ghostly reads it.
5391
+ *
5392
+ * A serializer keeps every object it wrote (an object met again is written as the number it had): one for the whole
5393
+ * export held every record of it, as the snapshot built whole did. Each page has its own, so a reader's numbers are not
5394
+ * its numbers, and no value it writes may meet an object twice: one that would (`meetsTwice`, rare in a store's records)
5395
+ * is written as a copy in which each meeting is an object of its own (`unshared`).
5396
+ */
5397
+ var SnapshotWriter = class {
5398
+ file;
5399
+ serializer = new DefaultSerializer();
5400
+ seen = /* @__PURE__ */ new Set();
5401
+ constructor(file) {
5402
+ this.file = file;
5403
+ this.serializer.writeHeader();
5404
+ }
5405
+ /** An object ("o") or an array ("A", of `length` items). */
5406
+ begin(tag, length) {
5407
+ this.tag(tag);
5408
+ if (length !== void 0) this.serializer.writeUint32(length);
5409
+ }
5410
+ /** An object's end, with how many properties it has; an array's, with no other property and its length. */
5411
+ end(tag, count) {
5412
+ this.tag(tag === "o" ? "{" : "$");
5413
+ if (tag === "A") this.serializer.writeUint32(0);
5414
+ this.serializer.writeUint32(count);
5415
+ }
5416
+ key(name) {
5417
+ this.serializer.writeValue(name);
5418
+ }
5419
+ value(name, value) {
5420
+ this.key(name);
5421
+ this.item(value);
5422
+ }
5423
+ /** An array's item (a record), or an object's value. */
5424
+ item(value) {
5425
+ if (meetsTwice(value, this.seen)) {
5426
+ value = unshared(value);
5427
+ if (meetsTwice(value, this.seen)) throw new Error("A record of the store could not be written to a backup");
5428
+ }
5429
+ this.serializer.writeValue(value);
5430
+ }
5431
+ /** What is serialized so far, to the file; the next page has a serializer of its own. */
5432
+ async flush() {
5433
+ const bytes = this.serializer.releaseBuffer();
5434
+ this.serializer = new DefaultSerializer();
5435
+ this.seen = /* @__PURE__ */ new Set();
5436
+ for (let at = 0; at < bytes.length;) at += (await this.file.write(bytes, at)).bytesWritten;
5437
+ }
5438
+ tag(tag) {
5439
+ this.serializer.writeRawBytes(Buffer.from(tag, "latin1"));
5440
+ }
5441
+ };
5442
+ /**
5443
+ * Whether serializing `value` meets an object twice, or one in `seen` (met before by the same serializer): within a
5444
+ * value, an object held in two places, or in itself. Every object v8 walks into is walked here (an object's or an
5445
+ * array's own enumerable properties, a Map's and a Set's entries, an error's cause); the others hold none.
5446
+ */
5447
+ function meetsTwice(value, seen) {
5448
+ if (value === null || typeof value !== "object") return false;
5449
+ if (seen.has(value)) return true;
5450
+ seen.add(value);
5451
+ if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer || value instanceof Date || value instanceof RegExp) return false;
5452
+ if (value instanceof Map) {
5453
+ for (const [k, v] of value) if (meetsTwice(k, seen) || meetsTwice(v, seen)) return true;
5454
+ return false;
5455
+ }
5456
+ if (value instanceof Set) {
5457
+ for (const item of value) if (meetsTwice(item, seen)) return true;
5458
+ return false;
5459
+ }
5460
+ if (value instanceof Error && meetsTwice(value.cause, seen)) return true;
5461
+ for (const key of Object.keys(value)) if (meetsTwice(value[key], seen)) return true;
5462
+ return false;
5463
+ }
5464
+ /**
5465
+ * `value` with each place that holds an object holding a copy of its own: what a reader of it gets is the same, but for
5466
+ * which of its parts are one object. One that holds itself has no such copy, and is refused.
5467
+ */
5468
+ function unshared(value, path = /* @__PURE__ */ new Set()) {
5469
+ if (value === null || typeof value !== "object") return value;
5470
+ if (path.has(value)) throw new Error("A record of the store holds itself: it cannot be written to a backup");
5471
+ if (Buffer.isBuffer(value)) return Buffer.from(value);
5472
+ if (value instanceof DataView) return new DataView(value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength));
5473
+ if (ArrayBuffer.isView(value)) return value.slice();
5474
+ if (value instanceof ArrayBuffer) return value.slice(0);
5475
+ if (value instanceof Date) return new Date(value);
5476
+ if (value instanceof RegExp) return new RegExp(value);
5477
+ if (value instanceof Error) return structuredClone(value);
5478
+ path.add(value);
5479
+ try {
5480
+ if (value instanceof Map) return new Map([...value].map(([k, v]) => [unshared(k, path), unshared(v, path)]));
5481
+ if (value instanceof Set) return new Set([...value].map((item) => unshared(item, path)));
5482
+ const out = Array.isArray(value) ? new Array(value.length) : {};
5483
+ for (const key of Object.keys(value)) out[key] = unshared(value[key], path);
5484
+ return out;
5485
+ } finally {
5486
+ path.delete(value);
5487
+ }
5488
+ }
5116
5489
  /** A journal entry as it is on disk: its length, then its bytes. */
5117
5490
  function framed(entry) {
5118
5491
  const header = Buffer.alloc(4);
@@ -5681,7 +6054,7 @@ function watch(store) {
5681
6054
  return change;
5682
6055
  }
5683
6056
  function keyToDisk(key) {
5684
- if (key instanceof IDBKeyRange) return { [RANGE]: {
6057
+ if (key instanceof IDBKeyRange$1) return { [RANGE]: {
5685
6058
  lower: key.lower,
5686
6059
  upper: key.upper,
5687
6060
  lowerOpen: key.lowerOpen,
@@ -5692,9 +6065,9 @@ function keyToDisk(key) {
5692
6065
  function keyFromDisk(key) {
5693
6066
  const range = key?.[RANGE];
5694
6067
  if (!range) return key;
5695
- if (range.lower === void 0) return IDBKeyRange.upperBound(range.upper, range.upperOpen);
5696
- if (range.upper === void 0) return IDBKeyRange.lowerBound(range.lower, range.lowerOpen);
5697
- return IDBKeyRange.bound(range.lower, range.upper, range.lowerOpen, range.upperOpen);
6068
+ if (range.lower === void 0) return IDBKeyRange$1.upperBound(range.upper, range.upperOpen);
6069
+ if (range.upper === void 0) return IDBKeyRange$1.lowerBound(range.lower, range.lowerOpen);
6070
+ return IDBKeyRange$1.bound(range.lower, range.upper, range.lowerOpen, range.upperOpen);
5698
6071
  }
5699
6072
  function hasBlob(value, depth = 0) {
5700
6073
  if (value instanceof Blob) return true;
@@ -5922,6 +6295,7 @@ async function installWebRtc() {
5922
6295
  "RTCPeerConnectionIceEvent",
5923
6296
  "RTCCertificate"
5924
6297
  ]) scope[name] ??= polyfill[name];
6298
+ countConnections(scope);
5925
6299
  refuseDataAnswers(scope);
5926
6300
  if (!publicStun()) leaveOutPublicStun(scope);
5927
6301
  return true;
@@ -5930,6 +6304,27 @@ async function installWebRtc() {
5930
6304
  return false;
5931
6305
  }
5932
6306
  }
6307
+ /** Each connection the engine makes is counted until it is closed, so libdatachannel's threads go with the last (`rtcThreads`). */
6308
+ function countConnections(scope) {
6309
+ const Base = scope.RTCPeerConnection;
6310
+ scope.RTCPeerConnection = class extends Base {
6311
+ #closed;
6312
+ constructor(config) {
6313
+ super(config);
6314
+ this.#closed = rtcThreads.opened();
6315
+ this.addEventListener("connectionstatechange", () => {
6316
+ if (this.connectionState === "closed") this.#closed();
6317
+ });
6318
+ }
6319
+ close() {
6320
+ try {
6321
+ super.close();
6322
+ } finally {
6323
+ this.#closed();
6324
+ }
6325
+ }
6326
+ };
6327
+ }
5933
6328
  /**
5934
6329
  * For tests: `GHOSTLY_TEST_REFUSE_DATA_ANSWERS=<n>` refuses one in every n answers this process applies to its offers
5935
6330
  * (the first, the n+1st, and so on), as libdatachannel 0.24.5 refuses one in its race (`ANSWER_REFUSED_REDIALS` in
@@ -6017,6 +6412,7 @@ async function startRuntime(paths, options = {}) {
6017
6412
  const said = options.logJoins ? joinLogLine(line) : null;
6018
6413
  if (said) process.stderr.write(said + "\n");
6019
6414
  });
6415
+ for (const name of existsSync(paths.db) ? readdirSync(paths.db) : []) if (/^backup-store-.*\.bin$/.test(name)) rmSync(join(paths.db, name), { force: true });
6020
6416
  const store = await openPersistentIndexedDb(paths.db);
6021
6417
  const restored = join(paths.dir, RESTORED_MARK);
6022
6418
  if (existsSync(restored)) {
@@ -6044,8 +6440,8 @@ async function startRuntime(paths, options = {}) {
6044
6440
  const [{ registerFileBytes }, { NodeFileBytes }] = await Promise.all([import("./fileBytes-BtIyoDF2.js"), import("./fileBytes-B0z9A6Le.js")]);
6045
6441
  registerFileBytes("native", async () => new NodeFileBytes(paths.files), true);
6046
6442
  if (await (await import("./blobFiles-BmsrvZc3.js")).moveBlobFiles()) await store.compact();
6047
- const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-Q3VcNqGt.js"), import("./endpoint-DarqblYE.js")]);
6048
- const mainline = dht.off ? null : new (await (import("./mainline-Cu61qcPS.js"))).Mainline({
6443
+ const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-C1jZZ61O.js"), import("./endpoint-DarqblYE.js")]);
6444
+ const mainline = dht.off ? null : new (await (import("./mainline-DCuGUSZF.js"))).Mainline({
6049
6445
  bootstrap: dht.bootstrap,
6050
6446
  host: dht.host
6051
6447
  });
@@ -6053,7 +6449,7 @@ async function startRuntime(paths, options = {}) {
6053
6449
  ...pinned ? { relays: pinned } : {},
6054
6450
  ...relayBudgetForTests()
6055
6451
  };
6056
- const transport = mainline || pinned ? new (await (import("./mainline-Cu61qcPS.js"))).RelaysAndDht(mainline, relayOptions, !!pinned) : null;
6452
+ const transport = mainline || pinned ? new (await (import("./mainline-DCuGUSZF.js"))).RelaysAndDht(mainline, relayOptions, !!pinned) : null;
6057
6453
  const apps = servedApps(paths.dir).length > 0;
6058
6454
  const server = new EngineServer({
6059
6455
  ...transport ? { transport } : {},
@@ -6157,29 +6553,58 @@ async function openHost(paths, mode, version, options = {}) {
6157
6553
  const hub = new EventHub(paths.events);
6158
6554
  let calls;
6159
6555
  try {
6160
- await hub.open();
6161
- const node = runtime.server.node;
6162
- const now = node.getState();
6163
- const mark = runtime.server.historyMark;
6164
- const histories = /* @__PURE__ */ new Map(), newest = /* @__PURE__ */ new Map();
6556
+ const { server, store } = runtime, node = server.node;
6557
+ const closed = !store.recovered;
6558
+ const oneByOne = mode === "one-shot" && closed;
6559
+ await hub.open({ seen: !oneByOne });
6560
+ const perChat = oneByOne && await hub.loadOwnNotes();
6561
+ if (oneByOne && !perChat) await hub.loadSeen();
6562
+ const kept = server.takeStartNewest();
6165
6563
  const whole = (id) => id.startsWith("group:") ? node.groupMessages({ groupId: id.slice(6) }) : node.getMessages(id);
6166
- const closed = !runtime.store.recovered;
6167
- const kept = runtime.server.takeStartNewest();
6168
- for (const id of [...now.links.map((link) => link.id), ...now.groups.map((group) => `group:${group.id}`)]) {
6169
- const page = closed && !runtime.server.historyChanged(id) ? kept.get(id) ?? await node.messagePage({
6170
- linkId: id,
6171
- limit: 100
6172
- }) : null;
6173
- if (page && !page.more) histories.set(id, page.messages);
6174
- else if (page && hub.inStep(id, page.messages)) newest.set(id, page.messages);
6175
- else histories.set(id, await whole(id));
6176
- }
6177
- hub.baseline(now, histories, {
6178
- newest,
6179
- files: (id) => node.fileMessages(id)
6180
- });
6181
- await hub.ended((id) => fileStore.get(id));
6182
- announceJoins(hub, node);
6564
+ const baseline = async () => {
6565
+ const now = node.getState();
6566
+ const mark = server.historyMark;
6567
+ const histories = /* @__PURE__ */ new Map(), newest = /* @__PURE__ */ new Map();
6568
+ for (const id of [...now.links.map((link) => link.id), ...now.groups.map((group) => `group:${group.id}`)]) {
6569
+ const page = closed && !server.historyChanged(id) ? kept.get(id) ?? await node.messagePage({
6570
+ linkId: id,
6571
+ limit: 100
6572
+ }) : null;
6573
+ if (page && !page.more) histories.set(id, page.messages);
6574
+ else if (page && hub.inStep(id, page.messages)) newest.set(id, page.messages);
6575
+ else histories.set(id, await whole(id));
6576
+ }
6577
+ hub.baseline(now, histories, {
6578
+ newest,
6579
+ files: (id) => node.fileMessages(id)
6580
+ });
6581
+ await hub.ended((id) => fileStore.get(id));
6582
+ announceJoins(hub, node);
6583
+ return {
6584
+ mark,
6585
+ ids: [...histories.keys(), ...newest.keys()]
6586
+ };
6587
+ };
6588
+ let read;
6589
+ if (perChat) {
6590
+ const now = node.getState();
6591
+ const ids = [...now.links.map((link) => link.id), ...now.groups.map((group) => `group:${group.id}`)];
6592
+ read = {
6593
+ mark: server.historyMark,
6594
+ ids
6595
+ };
6596
+ const untouched = new Set(ids.filter((id) => !server.historyChanged(id)));
6597
+ hub.perChat(now, {
6598
+ newest: async (id) => untouched.has(id) ? kept.get(id) ?? await node.messagePage({
6599
+ linkId: id,
6600
+ limit: 100
6601
+ }) : null,
6602
+ whole,
6603
+ files: (id) => node.fileMessages(id)
6604
+ });
6605
+ await hub.ended((id) => fileStore.get(id));
6606
+ announceJoins(hub, node);
6607
+ } else read = await baseline();
6183
6608
  calls = new CallManager({
6184
6609
  engine: node,
6185
6610
  emit: (type, id, fields) => hub.emit(type, id, fields),
@@ -6196,10 +6621,7 @@ async function openHost(paths, mode, version, options = {}) {
6196
6621
  });
6197
6622
  groupAuto = joiner;
6198
6623
  hub.onState((s) => joiner.check(s));
6199
- runtime.server.attach(hub.sink, {
6200
- mark,
6201
- ids: [...histories.keys(), ...newest.keys()]
6202
- });
6624
+ server.attach(hub.sink, read);
6203
6625
  joiner.check(node.getState());
6204
6626
  if (mode === "daemon") {
6205
6627
  const said = new DaemonPresence(node);
@@ -6233,6 +6655,7 @@ async function openHost(paths, mode, version, options = {}) {
6233
6655
  });
6234
6656
  presence?.stop();
6235
6657
  runtime.server.detach(hub.sink);
6658
+ await hub.caughtUp();
6236
6659
  hub.release();
6237
6660
  await runtime.close().catch(() => {});
6238
6661
  release();
@@ -6275,8 +6698,8 @@ async function serve(host) {
6275
6698
  return {
6276
6699
  server,
6277
6700
  close: () => closing ??= new Promise((resolve) => {
6278
- removeDaemonSecret(ctx.runtime.paths.dir);
6279
6701
  server.close(() => resolve());
6702
+ removeDaemonSecret(ctx.runtime.paths.dir);
6280
6703
  stopping.abort();
6281
6704
  within(Promise.all(answering), STOP_ANSWERS_MS).then(() => {
6282
6705
  for (const socket of sockets) socket.end();
@@ -6396,6 +6819,13 @@ function connection(ctx, socket, secret, answering) {
6396
6819
  });
6397
6820
  return;
6398
6821
  }
6822
+ if (typeof request !== "object" || request === null || Array.isArray(request)) {
6823
+ write({
6824
+ id: null,
6825
+ error: new CliError("bad_request", "A request is a JSON object: {\"id\", \"method\", \"params\"}").toJSON()
6826
+ });
6827
+ return;
6828
+ }
6399
6829
  const id = typeof request.id === "number" || typeof request.id === "string" ? request.id : null;
6400
6830
  if (typeof request.method !== "string") {
6401
6831
  write({
@@ -6476,4 +6906,4 @@ function connection(ctx, socket, secret, answering) {
6476
6906
  //#endregion
6477
6907
  export { BASELINE_NEWEST, MAX_UNREAD, STOP_ANSWERS_MS, keepServing, openHost, serve };
6478
6908
 
6479
- //# sourceMappingURL=host-CYlIiY3r.js.map
6909
+ //# sourceMappingURL=host-C0NMNQLl.js.map