@ghostlytools/cli 1.11.2 → 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 (97) hide show
  1. package/README.md +34 -28
  2. package/SKILL.md +4 -2
  3. package/dist/assets/{api-CtXgscmK.js → api-BwyFq4lA.js} +2 -2
  4. package/dist/assets/{api-BM3PsXbB.js → api-MYc6QNRs.js} +306 -114
  5. package/dist/assets/api-MYc6QNRs.js.map +1 -0
  6. package/dist/assets/{apiKit-lCTQ0EBG.js → apiKit-CfsNx9ZR.js} +62 -16
  7. package/dist/assets/apiKit-CfsNx9ZR.js.map +1 -0
  8. package/dist/assets/{appFetch-BKHy5H8J.js → appFetch-DVYZOZFw.js} +3 -3
  9. package/dist/assets/{appFetch-BKHy5H8J.js.map → appFetch-DVYZOZFw.js.map} +1 -1
  10. package/dist/assets/{apps-DO_gAZgm.js → apps-Dbw2Hcko.js} +1 -1
  11. package/dist/assets/{apps-Do26w3c7.js → apps-h12II1us.js} +150 -57
  12. package/dist/assets/apps-h12II1us.js.map +1 -0
  13. package/dist/assets/{arkade-CZrLbz-M.js → arkade-Bw_4PZRG.js} +2 -2
  14. package/dist/assets/{arkade-CZrLbz-M.js.map → arkade-Bw_4PZRG.js.map} +1 -1
  15. package/dist/assets/{backup-D21x1f5P.js → backup-CdEzTq5C.js} +1 -1
  16. package/dist/assets/{backup-j47AzDvG.js → backup-Chvy79SN.js} +60 -55
  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-CiGlBVBT.js → buttons-BgoExb4E.js} +37 -7
  21. package/dist/assets/buttons-BgoExb4E.js.map +1 -0
  22. package/dist/assets/buttons-DK2aAGat.js +2 -0
  23. package/dist/assets/{client-raIncVtF.js → client-DY8tZZWi.js} +28 -8
  24. package/dist/assets/client-DY8tZZWi.js.map +1 -0
  25. package/dist/assets/{codec-DcG2AeyE.js → codec-BnMg-Grj.js} +2 -2
  26. package/dist/assets/{codec-DcG2AeyE.js.map → codec-BnMg-Grj.js.map} +1 -1
  27. package/dist/assets/{envelope-Dud6lntA.js → envelope-CdV7vHVZ.js} +2 -2
  28. package/dist/assets/{envelope-Dud6lntA.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-DRKRSw14.js → fedimintSdk-CPFfVK-G.js} +2 -2
  32. package/dist/assets/{fedimintSdk-DRKRSw14.js.map → fedimintSdk-CPFfVK-G.js.map} +1 -1
  33. package/dist/assets/{fixedHosts-CNX7QiUD.js → fixedHosts-CbgqrIKs.js} +3 -3
  34. package/dist/assets/{fixedHosts-CNX7QiUD.js.map → fixedHosts-CbgqrIKs.js.map} +1 -1
  35. package/dist/assets/{host-8RskzQgB.js → host-C0NMNQLl.js} +702 -143
  36. package/dist/assets/host-C0NMNQLl.js.map +1 -0
  37. package/dist/assets/{hyperdhtRelay-uAThbFub.js → hyperdhtRelay-Dz7pvcBA.js} +2 -2
  38. package/dist/assets/{hyperdhtRelay-uAThbFub.js.map → hyperdhtRelay-Dz7pvcBA.js.map} +1 -1
  39. package/dist/assets/{irohWeb-DGelfgDJ.js → irohWeb-caAj4Dms.js} +19 -12
  40. package/dist/assets/irohWeb-caAj4Dms.js.map +1 -0
  41. package/dist/assets/{main-BC33ooYg.js → main-C6t5zrD5.js} +383 -110
  42. package/dist/assets/main-C6t5zrD5.js.map +1 -0
  43. package/dist/assets/{mainline-DYrjsS1t.js → mainline-DCuGUSZF.js} +35 -18
  44. package/dist/assets/mainline-DCuGUSZF.js.map +1 -0
  45. package/dist/assets/{networkReason-n2oBs4by.js → networkReason-BbQW1Xpc.js} +3 -3
  46. package/dist/assets/{networkReason-n2oBs4by.js.map → networkReason-BbQW1Xpc.js.map} +1 -1
  47. package/dist/assets/{nwc-Do3VSlIS.js → nwc-CSDRMZ0e.js} +3 -3
  48. package/dist/assets/{nwc-Do3VSlIS.js.map → nwc-CSDRMZ0e.js.map} +1 -1
  49. package/dist/assets/{nwcProvider-Du5aWNIn.js → nwcProvider-PE0wli8n.js} +2 -2
  50. package/dist/assets/{nwcProvider-Du5aWNIn.js.map → nwcProvider-PE0wli8n.js.map} +1 -1
  51. package/dist/assets/{ownDid-CwwYu8s2.js → ownDid-C1R5Kd8q.js} +2 -2
  52. package/dist/assets/{ownDid-CwwYu8s2.js.map → ownDid-C1R5Kd8q.js.map} +1 -1
  53. package/dist/assets/{ownStatus-D7dB4zJB.js → ownStatus-BoTf5WqQ.js} +27 -9
  54. package/dist/assets/ownStatus-BoTf5WqQ.js.map +1 -0
  55. package/dist/assets/{profiles-NyF5FnYF.js → profiles-DszHjEJi.js} +25 -10
  56. package/dist/assets/profiles-DszHjEJi.js.map +1 -0
  57. package/dist/assets/{registry-DKDInr0A.js → registry-Da-r6eC3.js} +30 -12
  58. package/dist/assets/registry-Da-r6eC3.js.map +1 -0
  59. package/dist/assets/{registry-C734VrO9.js → registry-zUZBSHYv.js} +1 -1
  60. package/dist/assets/{resolve-3Tqh9AWQ.js → resolve-tUzh4lHh.js} +2 -2
  61. package/dist/assets/{resolve-3Tqh9AWQ.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-B6PQ6iae.js → server-C1jZZ61O.js} +896 -277
  66. package/dist/assets/server-C1jZZ61O.js.map +1 -0
  67. package/dist/assets/{src-ChJijTlM.js → src-DJe-OYxd.js} +1010 -236
  68. package/dist/assets/src-DJe-OYxd.js.map +1 -0
  69. package/dist/assets/{sshForges-Brf0fslM.js → sshForges-C9zTO72R.js} +4 -3
  70. package/dist/assets/sshForges-C9zTO72R.js.map +1 -0
  71. package/dist/assets/{translate-xLme9Rdh.js → translate-CUuC5szR.js} +36 -15
  72. package/dist/assets/translate-CUuC5szR.js.map +1 -0
  73. package/dist/assets/{usdt-CSU5KzTV.js → usdt-DI51znt-.js} +3 -3
  74. package/dist/assets/{usdt-CSU5KzTV.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-BM3PsXbB.js.map +0 -1
  79. package/dist/assets/apiKit-lCTQ0EBG.js.map +0 -1
  80. package/dist/assets/apps-Do26w3c7.js.map +0 -1
  81. package/dist/assets/backup-j47AzDvG.js.map +0 -1
  82. package/dist/assets/buttons-CiGlBVBT.js.map +0 -1
  83. package/dist/assets/buttons-D2l2bVCG.js +0 -2
  84. package/dist/assets/client-raIncVtF.js.map +0 -1
  85. package/dist/assets/errors-Dt-ZnyOX.js.map +0 -1
  86. package/dist/assets/host-8RskzQgB.js.map +0 -1
  87. package/dist/assets/irohWeb-DGelfgDJ.js.map +0 -1
  88. package/dist/assets/main-BC33ooYg.js.map +0 -1
  89. package/dist/assets/mainline-DYrjsS1t.js.map +0 -1
  90. package/dist/assets/ownStatus-D7dB4zJB.js.map +0 -1
  91. package/dist/assets/profiles-NyF5FnYF.js.map +0 -1
  92. package/dist/assets/registry-DKDInr0A.js.map +0 -1
  93. package/dist/assets/secretGuard-DUr6bgKc.js +0 -2
  94. package/dist/assets/server-B6PQ6iae.js.map +0 -1
  95. package/dist/assets/src-ChJijTlM.js.map +0 -1
  96. package/dist/assets/sshForges-Brf0fslM.js.map +0 -1
  97. package/dist/assets/translate-xLme9Rdh.js.map +0 -1
@@ -1,12 +1,13 @@
1
- import { As as callSignalHeardAt, Ds as answersOffer, Es as RTC_CONFIG, Ma as ACTIVE_DHT_POLL_MS, Ms as extractParamsFromSdp, Ns as parseCallSignal, Os as buildSdpFromSignal, Ps as signalHasVideo, Qr as STATUS_IDLE_AFTER_MS, _d as fromBase64Url, ks as callRtcConfig, ls as DAEMON_WATCH_PEER_IDLE_MS, qu as setLinkTraceSink, wa as isPublicAddress, wi as checkPushEndpoint, ws as CALL_SIGNAL_MAX_AGE_MS } from "./src-ChJijTlM.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-DGelfgDJ.js";
4
- import { n as fedimintSdkOn } from "./fedimintSdk-DRKRSw14.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-lCTQ0EBG.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-BM3PsXbB.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-NyF5FnYF.js";
9
- 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-raIncVtF.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";
10
11
  import { createRequire } from "node:module";
11
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";
12
13
  import { open, readFile } from "node:fs/promises";
@@ -21,8 +22,8 @@ import { Worker } from "node:worker_threads";
21
22
  import { request } from "node:https";
22
23
  import { lookup } from "node:dns";
23
24
  import { WebSocket } from "ws";
24
- import { deserialize, serialize } from "node:v8";
25
- 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";
26
27
  //#region src/announce.ts
27
28
  /**
28
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
@@ -33,7 +34,7 @@ function announceJoins(hub, node) {
33
34
  const say = (chat) => {
34
35
  if (hub.said(chat, "join")) return;
35
36
  hub.markSaid(chat, "join");
36
- const nick = node.getState().settings.nick?.trim();
37
+ const nick = node.getSettings().nick?.trim();
37
38
  node.sendMessage({
38
39
  linkId: chat,
39
40
  text: nick ? `👋 ${nick} joined` : "👋 joined"
@@ -155,7 +156,10 @@ var AudioSocket = class AudioSocket {
155
156
  this.unproven.delete(socket);
156
157
  const before = this.client;
157
158
  this.client = socket;
158
- before?.end();
159
+ if (before) {
160
+ before.end();
161
+ setTimeout(() => before.destroy(), 2e3).unref();
162
+ }
159
163
  handlers.onClient?.(true);
160
164
  };
161
165
  let timer = null;
@@ -595,6 +599,57 @@ function envSwitch(name, fallback, env = process.env) {
595
599
  throw new CliError("usage", `${name} takes 0 or 1 (or false/true, off/on, no/yes), not ${JSON.stringify(value)}`);
596
600
  }
597
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
598
653
  //#region src/calls/media.ts
599
654
  var loading = null;
600
655
  var OPUS_APPLICATION_VOIP = 2048;
@@ -738,6 +793,7 @@ async function openConnection(stack, options, offer, held = false) {
738
793
  iceServers,
739
794
  ...bind ? { bindAddress: bind } : {}
740
795
  });
796
+ const released = rtcThreads.opened();
741
797
  const born = Date.now();
742
798
  const sink = options.log ?? (() => {});
743
799
  const log = (line) => sink(`+${Date.now() - born}ms ${line}`);
@@ -813,7 +869,8 @@ async function openConnection(stack, options, offer, held = false) {
813
869
  },
814
870
  closed: false,
815
871
  failed: false,
816
- onState: null
872
+ onState: null,
873
+ released
817
874
  };
818
875
  log(`candidates here: ${(local.c ?? []).join(" | ") || "none"}`);
819
876
  try {
@@ -821,7 +878,11 @@ async function openConnection(stack, options, offer, held = false) {
821
878
  } catch {}
822
879
  return conn;
823
880
  } catch (error) {
824
- pc.close();
881
+ try {
882
+ pc.close();
883
+ } finally {
884
+ released();
885
+ }
825
886
  throw error;
826
887
  }
827
888
  }
@@ -836,6 +897,7 @@ function closeConnection(conn) {
836
897
  try {
837
898
  conn.pc.close();
838
899
  } catch {}
900
+ conn.released();
839
901
  }
840
902
  /**
841
903
  * The answer's connection, made while the call rings: its description and candidates are ready when the call is
@@ -911,9 +973,7 @@ var CallMedia = class CallMedia {
911
973
  payloadType: conn.payloadType,
912
974
  ssrc: conn.ssrc,
913
975
  track: {
914
- send: (packet) => {
915
- if (track.isOpen()) track.sendMessageBinary(packet);
916
- },
976
+ send: trackSend(track),
917
977
  onPacket: (listener) => listeners.push(listener)
918
978
  },
919
979
  encoder: stack.opus(options.rate),
@@ -972,6 +1032,19 @@ function mdnsNote(signal, log) {
972
1032
  log(`${host}: an mDNS name, not resolved here: the call connects only if the contact's checks reach this side`);
973
1033
  }
974
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
+ }
975
1048
  /** Whether a call's ICE servers (`callIceServers`) have a TURN server: its relay candidate is then the one waited for. */
976
1049
  function callUsesTurn(servers) {
977
1050
  return servers.some((server) => typeof server !== "string");
@@ -1329,6 +1402,7 @@ var CallManager = class {
1329
1402
  return this.view(call);
1330
1403
  }
1331
1404
  async answer(ref, options) {
1405
+ if (this.host.answers === false) throw new CliError("unavailable", CALLS_NEED_DAEMON);
1332
1406
  const call = this.find(ref, (c) => c.direction === "in" && c.state === "ringing");
1333
1407
  if (call.direction !== "in" || call.state !== "ringing") throw new CliError("refused", "That call is not ringing here");
1334
1408
  this.callable(call.chat);
@@ -1351,7 +1425,7 @@ var CallManager = class {
1351
1425
  v: 0
1352
1426
  });
1353
1427
  } catch (error) {
1354
- await this.end(call, "failed", true, false);
1428
+ await this.end(call, "failed", "unreachable");
1355
1429
  throw error;
1356
1430
  } finally {
1357
1431
  early?.close();
@@ -1378,6 +1452,7 @@ var CallManager = class {
1378
1452
  }
1379
1453
  /** Drops the audio the program queued and the call has not sent yet (barge-in). */
1380
1454
  flush(ref) {
1455
+ if (this.host.answers === false) throw new CliError("unavailable", CALLS_NEED_DAEMON);
1381
1456
  const call = this.find(ref);
1382
1457
  const flushedMs = call.queue?.flush() ?? 0;
1383
1458
  return {
@@ -1515,7 +1590,7 @@ var CallManager = class {
1515
1590
  this.refused(call, "the connection failed as the answer went in");
1516
1591
  return;
1517
1592
  }
1518
- 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");
1519
1594
  }
1520
1595
  });
1521
1596
  if (call.ended || attempt !== call.attempt) {
@@ -1592,6 +1667,7 @@ var CallManager = class {
1592
1667
  }, every);
1593
1668
  call.watcher.unref?.();
1594
1669
  }
1670
+ /** `tell`: the contact is sent a hang-up; `"unreachable"`, one that says this side could not connect (`r: "u"`, WISP 601). */
1595
1671
  async end(call, reason, tell, report = true) {
1596
1672
  if (call.ended) return;
1597
1673
  call.ended = true;
@@ -1615,10 +1691,15 @@ var CallManager = class {
1615
1691
  call.early = null;
1616
1692
  const closing = call.socket?.close();
1617
1693
  if (tell) {
1618
- 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
+ } : {
1619
1699
  t: "h",
1620
1700
  ts: this.stamp(call.chat)
1621
- }).catch(() => {});
1701
+ };
1702
+ await this.send(call.chat, hangUp).catch(() => {});
1622
1703
  if (!this.stopping) {
1623
1704
  const timer = setTimeout(() => {
1624
1705
  this.clearing.delete(call.chat);
@@ -1751,6 +1832,10 @@ var CallManager = class {
1751
1832
  const link = this.link(this.chatId(ref));
1752
1833
  if (this.host.answers === false) throw new CliError("unavailable", CALLS_NEED_DAEMON);
1753
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
+ }
1754
1839
  if (link.callsUnavailable) throw new CliError("unavailable", link.callsUnavailable);
1755
1840
  if (this.host.engine.getState().settings?.relayOnly === true) throw new CliError("unavailable", CALLS_OFF_RELAY_ONLY);
1756
1841
  return link;
@@ -1760,6 +1845,8 @@ var CallManager = class {
1760
1845
  }
1761
1846
  };
1762
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";
1763
1850
  /**
1764
1851
  * Hide my IP from contacts (`relayOnly`, WISP 601 Relay only): libdatachannel's relay policy only keeps the other
1765
1852
  * candidates out of the signal; its ICE still checks from this machine's own sockets, and a call connected on a direct
@@ -1823,6 +1910,13 @@ var DaemonPresence = class {
1823
1910
  this.timer = setInterval(() => this.node.setPresence({ presence: "active" }), PRESENCE_EVERY_MS);
1824
1911
  this.timer.unref?.();
1825
1912
  }
1913
+ /**
1914
+ * Whether the engine's online notices go (WISP 401 § Online notices, `NodeOptions.onlineNotices`): only while presence
1915
+ * is said. Off, a contact's closed app was pushed "<bot> is online" 30 s after each start, while the bot showed no dot.
1916
+ */
1917
+ saysOnline() {
1918
+ return this.timer !== void 0;
1919
+ }
1826
1920
  stop() {
1827
1921
  clearInterval(this.timer);
1828
1922
  this.timer = void 0;
@@ -1909,7 +2003,19 @@ var EventHub = class {
1909
2003
  /** `file.*` events whose message was not seen yet: they wait for it a moment (the engine stores it right after). */
1910
2004
  pendingFiles = /* @__PURE__ */ new Map();
1911
2005
  baselined = false;
1912
- /** Chats and groups whose history the baseline did not read whole, and how to read it: until a file event needs it. */
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();
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. */
1913
2019
  unread = /* @__PURE__ */ new Map();
1914
2020
  /**
1915
2021
  * Whether the seen set notes mentions (`+m`): false on a set written before it did, whose mentions were reported with
@@ -1927,8 +2033,11 @@ var EventHub = class {
1927
2033
  this.now = now;
1928
2034
  this.seenDb = seenDb;
1929
2035
  }
1930
- /** Loads the journal's last seq and the seen set; call before `sink` is attached. */
1931
- 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 } = {}) {
1932
2041
  const reserved = readDropped(this.journalPath + ".reserved") ?? 0;
1933
2042
  try {
1934
2043
  const dropped = readDropped(this.journalPath + ".dropped");
@@ -1971,7 +2080,27 @@ var EventHub = class {
1971
2080
  this.seq = reserved;
1972
2081
  this.markDropped(Math.max(this.dropped, reserved));
1973
2082
  }
1974
- 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) => {
1975
2104
  const request = indexedDB.open(this.seenDb, 1);
1976
2105
  request.onupgradeneeded = () => {
1977
2106
  request.result.createObjectStore(SEEN);
@@ -1980,12 +2109,15 @@ var EventHub = class {
1980
2109
  request.onsuccess = () => resolve(request.result);
1981
2110
  request.onerror = () => reject(request.error);
1982
2111
  });
2112
+ }
2113
+ /** The notes in `range` (all with none) into memory. */
2114
+ async readNotes(range) {
1983
2115
  const store = this.db.transaction(SEEN).objectStore(SEEN);
1984
2116
  const all = (request) => new Promise((resolve, reject) => {
1985
2117
  request.onsuccess = () => resolve(request.result);
1986
2118
  request.onerror = () => reject(request.error);
1987
2119
  });
1988
- 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))]);
1989
2121
  for (let i = 0; i < keys.length; i++) {
1990
2122
  const [chat, id] = keys[i];
1991
2123
  this.seenOf(chat).set(id, values[i]);
@@ -2008,10 +2140,11 @@ var EventHub = class {
2008
2140
  */
2009
2141
  baseline(state, histories, rest) {
2010
2142
  this.stateChanged(state);
2143
+ this.stateKnown = true;
2011
2144
  for (const [chat, messages] of histories) this.messages(chat, messages);
2012
2145
  for (const [chat, messages] of rest?.newest ?? []) {
2013
2146
  this.messages(chat, messages, []);
2014
- this.unread.set(chat, rest.whole);
2147
+ this.unread.set(chat, rest.files);
2015
2148
  }
2016
2149
  this.baselined = true;
2017
2150
  this.firstRun = false;
@@ -2027,13 +2160,73 @@ var EventHub = class {
2027
2160
  }
2028
2161
  }
2029
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
+ /**
2030
2223
  * Drops the notes of settled messages, and of reactions, whose last activity is older than `SEEN_KEEP_MS`: from memory
2031
2224
  * and from the store. A message of mine still on its way (or failed, which a retry sends again) keeps its note. The
2032
2225
  * 30 days count back from the newest activity the set knows when the clock reads later than it: a clock run ahead (a
2033
2226
  * wrong RTC, a VM restored with a skewed clock) would otherwise drop the notes of the recent history, which the next
2034
2227
  * start with the clock right reports again.
2035
2228
  */
2036
- sweep() {
2229
+ sweep(only) {
2037
2230
  let newest = 0;
2038
2231
  for (const [chat, known] of this.seen) {
2039
2232
  if (chat === FORMAT || chat.startsWith("said:")) continue;
@@ -2041,8 +2234,9 @@ var EventHub = class {
2041
2234
  }
2042
2235
  const cutoff = Math.min(this.now(), newest) - SEEN_KEEP_MS;
2043
2236
  let dropped = 0;
2044
- for (const [chat, known] of this.seen) {
2045
- 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;
2046
2240
  const reactions = chat.startsWith("reactions:");
2047
2241
  const gone = [];
2048
2242
  for (const [id, note] of known) {
@@ -2117,7 +2311,7 @@ var EventHub = class {
2117
2311
  to: Math.min(this.dropped, next - 1)
2118
2312
  });
2119
2313
  };
2120
- if (existsSync(this.journalPath)) for (const line of journalTail(this.journalPath).lines) {
2314
+ if (existsSync(this.journalPath)) for (const line of journalAfter(this.journalPath, since)) {
2121
2315
  let event;
2122
2316
  try {
2123
2317
  event = JSON.parse(line);
@@ -2371,6 +2565,7 @@ var EventHub = class {
2371
2565
  return from === Infinity ? void 0 : from;
2372
2566
  }
2373
2567
  handle(message) {
2568
+ if (this.waits(message)) return;
2374
2569
  switch (message.kind) {
2375
2570
  case "state":
2376
2571
  this.stateChanged(message.state);
@@ -2396,14 +2591,14 @@ var EventHub = class {
2396
2591
  }
2397
2592
  }
2398
2593
  stateChanged(state) {
2399
- const quiet = !this.baselined;
2594
+ const quiet = !this.stateKnown;
2400
2595
  const links = new Map(state.links.map((link) => [link.id, link]));
2401
2596
  for (const [id, link] of links) {
2402
2597
  const shape = chatShape(link), before = this.chats.get(id);
2403
2598
  this.chats.set(id, shape);
2404
2599
  if (quiet) continue;
2405
2600
  if (!before) {
2406
- this.emit("chat.created", `chat.created:${id}`, {
2601
+ this.emit("chat.created", `chat.created:${id}:${this.now()}`, {
2407
2602
  chat: id,
2408
2603
  summary: {
2409
2604
  ...chatJson(link),
@@ -2447,8 +2642,12 @@ var EventHub = class {
2447
2642
  }
2448
2643
  const removedChats = [...this.chats.keys()].filter((id) => !links.has(id));
2449
2644
  for (const id of removedChats) {
2645
+ const peer = this.chats.get(id)?.peer;
2450
2646
  this.chats.delete(id);
2451
- if (!quiet) this.emit("chat.removed", `chat.removed:${id}`, { chat: id });
2647
+ if (!quiet) this.emit("chat.removed", `chat.removed:${id}:${this.now()}`, {
2648
+ chat: id,
2649
+ ...peer ? { peer } : {}
2650
+ });
2452
2651
  }
2453
2652
  const chatsGone = new Set(removedChats);
2454
2653
  this.forget(removedChats, (event) => chatsGone.has(event.chat) && event.type !== "chat.removed");
@@ -2458,7 +2657,7 @@ var EventHub = class {
2458
2657
  this.groups.set(id, shape);
2459
2658
  if (quiet) continue;
2460
2659
  if (!before) {
2461
- this.emit("group.created", `group.created:${id}`, {
2660
+ this.emit("group.created", `group.created:${id}:${this.now()}`, {
2462
2661
  group: id,
2463
2662
  summary: groupJson(group)
2464
2663
  });
@@ -2468,8 +2667,7 @@ var EventHub = class {
2468
2667
  group: id,
2469
2668
  status: shape.status
2470
2669
  });
2471
- const joined = shape.members.filter((key) => !before.members.includes(key));
2472
- const left = before.members.filter((key) => !shape.members.includes(key));
2670
+ const { joined, left } = membersDiff(before.members, shape.members);
2473
2671
  if (joined.length || left.length) this.emit("group.members", `group.members:${id}:${this.now()}`, {
2474
2672
  group: id,
2475
2673
  joined,
@@ -2493,15 +2691,21 @@ var EventHub = class {
2493
2691
  const removedGroups = [...this.groups.keys()].filter((id) => !groups.has(id));
2494
2692
  for (const id of removedGroups) {
2495
2693
  this.groups.delete(id);
2496
- if (!quiet) this.emit("group.removed", `group.removed:${id}`, { group: id });
2694
+ if (!quiet) this.emit("group.removed", `group.removed:${id}:${this.now()}`, { group: id });
2497
2695
  }
2498
2696
  const groupsGone = new Set(removedGroups);
2499
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;
2500
2700
  for (const [id, payment] of Object.entries(state.payments ?? {})) {
2501
2701
  const shape = `${payment.state}|${payment.error ?? ""}|${payment.closed ? 1 : 0}`, before = this.payments.get(id);
2502
2702
  this.payments.set(id, shape);
2503
2703
  if (quiet || before === shape) continue;
2504
- const json = paymentJson(payment);
2704
+ const group = payment.group ?? groupOf(payment.linkId);
2705
+ const json = {
2706
+ ...paymentJson(payment),
2707
+ ...group && { group }
2708
+ };
2505
2709
  if (before === void 0) this.emit("payment.created", `payment.created:${id}`, {
2506
2710
  chat: payment.linkId,
2507
2711
  payment: json
@@ -2752,7 +2956,14 @@ var EventHub = class {
2752
2956
  }
2753
2957
  continue;
2754
2958
  }
2755
- const noted = noteOf(state, at);
2959
+ const [delivery] = splitState(state), [was] = before === void 0 ? [void 0] : splitState(before);
2960
+ const passed = passesIn(note, was), moved = !group && message.sender === "me" && was !== void 0 && delivery !== was;
2961
+ const again = moved && (passed.been & deliveryBit(delivery)) !== 0;
2962
+ const passes = moved ? {
2963
+ count: passed.count + 1,
2964
+ been: passed.been | deliveryBit(delivery)
2965
+ } : passed;
2966
+ const noted = noteOf(state, at, passes);
2756
2967
  known.set(message.id, noted);
2757
2968
  changes.push([message.id, noted]);
2758
2969
  if (quiet) continue;
@@ -2791,14 +3002,14 @@ var EventHub = class {
2791
3002
  message: json()
2792
3003
  });
2793
3004
  } else {
2794
- const [delivery, edits] = splitState(state), [was, edited] = splitState(before);
3005
+ const [, edits] = splitState(state), [, edited] = splitState(before);
2795
3006
  if (edits > edited && !(message.sender === "me" && message.edit?.restore)) this.emit("message.edited", `message.edited:${chat}:${message.id}:${edits}`, {
2796
3007
  chat,
2797
3008
  messageId: message.id,
2798
3009
  edits,
2799
3010
  message: json()
2800
3011
  });
2801
- if (message.sender === "me" && delivery !== was) this.emit("message.delivery", `message.delivery:${chat}:${message.id}:${delivery}`, {
3012
+ if (moved) this.emit("message.delivery", `message.delivery:${chat}:${message.id}:${delivery}${again ? `:${passes.count}` : ""}`, {
2802
3013
  chat,
2803
3014
  messageId: message.id,
2804
3015
  delivery,
@@ -2935,9 +3146,35 @@ var AGES_NOTED = "ages";
2935
3146
  var UNDERWAY = "files:";
2936
3147
  /**
2937
3148
  * A note: what the seen set keeps of a message (or a reaction's number), then `@` and the time of its last activity in
2938
- * base 36 (`received+p#e2@mg3k1x2a`), by which `sweep` drops it. A note from before has no time.
3149
+ * base 36 (`received+p#e2@mg3k1x2a`), by which `sweep` drops it. A note from before has no time. A message of mine whose
3150
+ * delivery changed ends with its passes (`sent@mg3k1x2a!4.b`): `!`, how many changes, `.` and the deliveries it was in.
2939
3151
  */
2940
- var noteOf = (state, at) => `${state}@${Math.max(0, Math.floor(at)).toString(36)}`;
3152
+ var noteOf = (state, at, passes) => `${state}@${Math.max(0, Math.floor(at)).toString(36)}${passes?.count ? `!${passes.count}.${passes.been.toString(36)}` : ""}`;
3153
+ var DELIVERIES = [
3154
+ "sending",
3155
+ "sent",
3156
+ "queued",
3157
+ "waiting",
3158
+ "held",
3159
+ "delivered",
3160
+ "failed"
3161
+ ];
3162
+ var deliveryBit = (delivery) => {
3163
+ const at = DELIVERIES.indexOf(delivery);
3164
+ return 1 << (at === -1 ? DELIVERIES.length : at);
3165
+ };
3166
+ function passesIn(note, delivery) {
3167
+ const mark = note?.lastIndexOf("!") ?? -1;
3168
+ if (mark === -1) return {
3169
+ count: 0,
3170
+ been: delivery === void 0 ? 0 : deliveryBit(delivery)
3171
+ };
3172
+ const [count, been] = note.slice(mark + 1).split(".");
3173
+ return {
3174
+ count: Number(count) || 0,
3175
+ been: parseInt(been ?? "", 36) || 0
3176
+ };
3177
+ }
2941
3178
  function stateIn(note) {
2942
3179
  const at = note.lastIndexOf("@");
2943
3180
  return at === -1 ? note : note.slice(0, at);
@@ -2999,7 +3236,8 @@ function chatShape(link) {
2999
3236
  typing: !!link.peerTyping,
3000
3237
  typingKind: link.peerTyping ? link.peerTypingKind ?? "typing" : "typing",
3001
3238
  typingStatus: link.peerTyping ? link.peerTypingStatus ?? null : null,
3002
- pin: link.pin ?? null
3239
+ pin: link.pin ?? null,
3240
+ peer: view.peer || null
3003
3241
  };
3004
3242
  }
3005
3243
  /** A `typing.started`'s word: the kind, and the contact's status line when it gave one (already cleaned). */
@@ -3021,6 +3259,25 @@ function groupShape(group) {
3021
3259
  };
3022
3260
  }
3023
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
+ /**
3024
3281
  * The journal's whole lines within its last `JOURNAL_MAX_BYTES`, and its size: never the whole file as one string, so
3025
3282
  * a journal grown past what a string can hold (or a trim's bound) is still read, and in bounded time. Lines `forget`
3026
3283
  * blanked are left out.
@@ -3032,6 +3289,63 @@ function journalTail(path) {
3032
3289
  size
3033
3290
  };
3034
3291
  }
3292
+ /** What `journalAfter` reads first from the journal's end; each read further back is twice the last. */
3293
+ var REPLAY_CHUNK = 65536;
3294
+ /** The bytes of a line's start that hold its seq (`seqAt`), and how they begin. */
3295
+ var SEQ_PREFIX = 32;
3296
+ var SEQ_START = Buffer.from("{\"seq\":");
3297
+ /**
3298
+ * The journal's lines after `since`, within its last `JOURNAL_MAX_BYTES`, as `journalTail` gives them: its end is read
3299
+ * back in growing chunks, each line's seq taken from its `{"seq":N` start (as each event is written), until a line at or
3300
+ * before `since`; only what follows it is made into text. A line with no seq there (blanked by `forget`, torn) is passed
3301
+ * over and given, as `journalTail` gives it.
3302
+ */
3303
+ function journalAfter(path, since) {
3304
+ const fd = openSync(path, "r");
3305
+ const read = (from, to) => {
3306
+ const bytes = Buffer.alloc(to - from);
3307
+ let done = 0;
3308
+ while (done < bytes.length) {
3309
+ const n = readSync(fd, bytes, done, bytes.length - done, from + done);
3310
+ if (!n) break;
3311
+ done += n;
3312
+ }
3313
+ return bytes.subarray(0, done);
3314
+ };
3315
+ try {
3316
+ const size = fstatSync(fd).size;
3317
+ const cut = Math.max(0, size - JOURNAL_MAX_BYTES - 1);
3318
+ if (cut === 0 && (seqAt(read(0, Math.min(size, SEQ_PREFIX)), 0) ?? -1) > since) return journalTail(path).lines;
3319
+ let after = size, found = false, from = size, chunk = REPLAY_CHUNK;
3320
+ const chunks = [];
3321
+ while (from > cut && !found) {
3322
+ const next = Math.max(cut, from - chunk), bytes = read(next, Math.min(size, from + SEQ_PREFIX));
3323
+ chunks.unshift(bytes.subarray(0, Math.min(bytes.length, from - next)));
3324
+ for (let newline = bytes.lastIndexOf(10, from - next - 1); newline !== -1; newline = newline > 0 ? bytes.lastIndexOf(10, newline - 1) : -1) {
3325
+ const seq = seqAt(bytes, newline + 1);
3326
+ if (seq !== void 0 && seq <= since) {
3327
+ found = true;
3328
+ break;
3329
+ }
3330
+ after = next + newline + 1;
3331
+ }
3332
+ if (!found && next === 0 && (seqAt(bytes, 0) ?? Infinity) > since) after = 0;
3333
+ from = next;
3334
+ chunk *= 2;
3335
+ }
3336
+ return (chunks.length === 1 ? chunks[0] : Buffer.concat(chunks)).toString("utf8", after - from).split("\n").filter((line) => line.trimStart());
3337
+ } finally {
3338
+ closeSync(fd);
3339
+ }
3340
+ }
3341
+ /** The seq a journal line begins with (`{"seq":N,` or `{"seq":N}`), read from its first bytes, never made into text. */
3342
+ function seqAt(bytes, at) {
3343
+ for (let i = 0; i < SEQ_START.length; i++) if (bytes[at + i] !== SEQ_START[i]) return void 0;
3344
+ let seq = 0, i = at + SEQ_START.length;
3345
+ const end = Math.min(bytes.length, at + SEQ_PREFIX);
3346
+ for (; i < end && bytes[i] >= 48 && bytes[i] <= 57; i++) seq = seq * 10 + bytes[i] - 48;
3347
+ return i > at + SEQ_START.length && (bytes[i] === 44 || bytes[i] === 125) ? seq : void 0;
3348
+ }
3035
3349
  /**
3036
3350
  * What opening needs of the journal: its last seq, its number of lines (as `journalTail` counts them) and its size. The
3037
3351
  * lines are counted by their bytes and only the last whole one is made into text.
@@ -3065,11 +3379,10 @@ function journalEnd(path) {
3065
3379
  let tail;
3066
3380
  if (bytes.length > start && bytes[bytes.length - 1] !== 10) {
3067
3381
  const from = Math.max(start, bytes.lastIndexOf(10) + 1);
3068
- const named = /^\{"seq":(\d+)[,}]/.exec(bytes.toString("latin1", from, Math.min(bytes.length, from + 32)))?.[1];
3069
3382
  tail = {
3070
3383
  at: size - bytes.length + from,
3071
3384
  whole: read === from,
3072
- seq: named === void 0 ? void 0 : Number(named)
3385
+ seq: seqAt(bytes, from)
3073
3386
  };
3074
3387
  }
3075
3388
  return {
@@ -3383,7 +3696,8 @@ var SAID = {
3383
3696
  "full.sent": "the group is full: told the joiner on the entry session",
3384
3697
  "full.received": "the group is full: knocking on until there is room",
3385
3698
  "welcome.sent": "welcome sent",
3386
- "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)"
3387
3701
  };
3388
3702
  /** The log line for one join-trace line, or null when it is not said. */
3389
3703
  function joinLogLine(traced) {
@@ -4359,10 +4673,17 @@ var FOLLOWS = "__ghostly_journal__";
4359
4673
  var SEQ = "__ghostly_entry__";
4360
4674
  /** How long `close` waits for the transactions still running: one left open for good does not keep the process. */
4361
4675
  var CLOSE_WAIT_MS = 5e3;
4362
- /** 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. */
4363
4677
  var EXPORT_PAGE = 256;
4364
4678
  /** How long a relaxed transaction's entry may wait for a sync when no other transaction brings one. */
4365
4679
  var RELAXED_SYNC_MS = 1e3;
4680
+ /**
4681
+ * How long the trees' layers wait for the ones after them before they go to SQLite, all in one SQLite transaction. One
4682
+ * per journal entry (a message's send, each of its delivery steps, its read) rewrote the pages each touched: ~80 KB of
4683
+ * rows.db's WAL per message, and a checkpoint on the daemon's thread (30-150 ms) every ~50 messages. The journal holds
4684
+ * them meanwhile, and reads find them in their layers.
4685
+ */
4686
+ var APPLY_MS = 250;
4366
4687
  var BLOB = "__ghostlyBlob";
4367
4688
  var RANGE = "__ghostlyRange";
4368
4689
  var PROBE = "__ghostly_storage_probe__";
@@ -4404,6 +4725,9 @@ async function openPersistentIndexedDb(dir, options = {}) {
4404
4725
  let unsynced = [];
4405
4726
  /** The syncs running off the thread, one after another, until no `complete` waits for one. */
4406
4727
  let syncing = null;
4728
+ /** The last journal entry whose layers wait for `applying` to take them to SQLite. */
4729
+ let applyUpTo = 0;
4730
+ let applying = null;
4407
4731
  /** The journal holds a chat's ratchet state, and with the next one it holds a key already used. */
4408
4732
  let holdsRatchet = false;
4409
4733
  /** The journal holds a delete of a chat or a message (or a settings write), and what came before it with it. */
@@ -4639,13 +4963,23 @@ async function openPersistentIndexedDb(dir, options = {}) {
4639
4963
  return transaction.dispatchEvent(abort);
4640
4964
  }
4641
4965
  /**
4642
- * The journal has entry `entry`: the trees' layers up to it go to SQLite. One that fails stays in memory, where reads
4643
- * find it, and goes with the next entry, or before the next snapshot (which fails without it).
4966
+ * The journal has entry `entry`: the trees' layers up to it go to SQLite within `APPLY_MS`, with the entries written
4967
+ * meanwhile. One that fails stays in memory, where reads find it, and goes with the next entry, or before the next
4968
+ * snapshot (which fails without it). A process that dies first replays them from the journal.
4644
4969
  */
4645
4970
  function written(entry) {
4971
+ if (!trees || !writing) return;
4972
+ applyUpTo = Math.max(applyUpTo, entry);
4973
+ applying ??= setTimeout(applyNow, APPLY_MS).unref();
4974
+ }
4975
+ function applyNow() {
4976
+ if (applying) {
4977
+ clearTimeout(applying);
4978
+ applying = null;
4979
+ }
4646
4980
  if (!trees || !writing) return;
4647
4981
  try {
4648
- trees.apply(entry);
4982
+ trees.apply(applyUpTo);
4649
4983
  } catch (error) {
4650
4984
  failed(error);
4651
4985
  }
@@ -4662,7 +4996,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4662
4996
  tree.clear();
4663
4997
  return deleted;
4664
4998
  };
4665
- 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));
4666
5000
  }
4667
5001
  /** A store whose records and index entries are in rows.db already (a snapshot of format 3): its trees attached. */
4668
5002
  function attachTrees(store) {
@@ -4802,18 +5136,69 @@ async function openPersistentIndexedDb(dir, options = {}) {
4802
5136
  throw error;
4803
5137
  }
4804
5138
  }
4805
- /**
4806
- * What is held, as a snapshot. `inline`: every value in it (format 1, `exportSnapshot`); otherwise the stores kept on
4807
- * disk say only that they are (format 3).
4808
- */
4809
- 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) {
4810
5157
  const snapshot = {
4811
5158
  format: 1,
4812
- ...journalName !== void 0 && { journal: journalName },
5159
+ journal: journalName,
4813
5160
  databases: []
4814
5161
  };
4815
5162
  const held = [];
4816
- 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 = [];
4817
5202
  let reader = null;
4818
5203
  try {
4819
5204
  for (const db of raw._databases.values()) {
@@ -4821,59 +5206,73 @@ async function openPersistentIndexedDb(dir, options = {}) {
4821
5206
  const stores = [];
4822
5207
  for (const store of db.rawObjectStores.values()) {
4823
5208
  if (store.deleted) continue;
4824
- const live = [...store.rawIndexes.values()].filter((index) => !index.deleted);
4825
- const meta = {
4826
- name: store.name,
4827
- keyPath: store.keyPath,
4828
- autoIncrement: store.autoIncrement,
4829
- keyGenerator: store.keyGenerator?.num ?? null,
4830
- indexes: live.map(({ name, keyPath, multiEntry, unique }) => ({
4831
- name,
4832
- keyPath,
4833
- multiEntry,
4834
- unique
4835
- }))
4836
- };
4837
- if (!inline && disk(store)) {
4838
- if (!onTrees(store)) toDisk(store);
5209
+ if (!onTrees(store)) {
4839
5210
  stores.push({
4840
- ...meta,
4841
- records: [],
4842
- onDisk: true
5211
+ meta: storeMeta(store),
5212
+ records: Array.from(store.records.values(), ({ key, value }) => ({
5213
+ key,
5214
+ value
5215
+ }))
4843
5216
  });
4844
- snapshot.format = 3;
4845
5217
  continue;
4846
5218
  }
4847
- const saved = {
4848
- ...meta,
4849
- records: []
4850
- };
4851
- stores.push(saved);
4852
- if (onTrees(store)) {
4853
- reader ??= openTreeReader(sqlite, rowsPath, classes.constraintError);
4854
- later.push([saved, store.records.records.at(reader)]);
4855
- } else {
4856
- for (const record of store.records.values()) saved.records.push([record.key, record.value]);
4857
- held.push(saved);
4858
- }
5219
+ reader ??= openTreeReader(sqlite, rowsPath, classes.constraintError);
5220
+ stores.push({
5221
+ meta: storeMeta(store),
5222
+ records: store.records.records.at(reader)
5223
+ });
4859
5224
  }
4860
- snapshot.databases.push({
5225
+ databases.push({
4861
5226
  name: db.name,
4862
5227
  version: db.version,
4863
5228
  stores
4864
5229
  });
4865
5230
  }
4866
- for (const store of held) for (const record of store.records) record[1] = await blobsToDisk(record[1]);
4867
- 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();
4868
5268
  } finally {
4869
5269
  reader?.db.close();
4870
5270
  }
4871
- return snapshot;
4872
5271
  }
4873
5272
  async function snapshotToDisk() {
4874
5273
  await synced();
4875
5274
  const name = randomBytes(8).toString("hex");
4876
- const snapshot = await capture(name, false);
5275
+ const snapshot = await capture(name);
4877
5276
  if (trees) {
4878
5277
  trees.apply(seq);
4879
5278
  trees.sync();
@@ -4941,7 +5340,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4941
5340
  }
4942
5341
  const installed = globalThis;
4943
5342
  installed.indexedDB = factory;
4944
- installed.IDBKeyRange = IDBKeyRange;
5343
+ installed.IDBKeyRange = IDBKeyRange$1;
4945
5344
  return {
4946
5345
  factory,
4947
5346
  recovered: !replayed.closed,
@@ -4959,6 +5358,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4959
5358
  if (!writing) return;
4960
5359
  await settled();
4961
5360
  await enqueue(leave);
5361
+ applyNow();
4962
5362
  writing = false;
4963
5363
  if (journalFd !== null) {
4964
5364
  closeSync(journalFd);
@@ -4969,18 +5369,9 @@ async function openPersistentIndexedDb(dir, options = {}) {
4969
5369
  journalBytes,
4970
5370
  async exportSnapshot(path) {
4971
5371
  await settled();
4972
- const snapshot = await capture(void 0, true);
4973
- for (const db of snapshot.databases) for (const store of db.stores) {
4974
- if (store.name !== "links") continue;
4975
- store.records = store.records.map(([key, value]) => {
4976
- if (!value || typeof value !== "object" || !("ratchetState" in value)) return [key, value];
4977
- const { ratchetState: _ratchet, ...link } = value;
4978
- return [key, link];
4979
- });
4980
- }
4981
5372
  const file = await open(path, "w", 384);
4982
5373
  try {
4983
- await file.writeFile(serialize(snapshot));
5374
+ await exportTo(file);
4984
5375
  await file.sync();
4985
5376
  } finally {
4986
5377
  await file.close();
@@ -4992,6 +5383,109 @@ async function openPersistentIndexedDb(dir, options = {}) {
4992
5383
  function writeAll(fd, bytes) {
4993
5384
  for (let at = 0; at < bytes.length;) at += writeSync(fd, bytes, at);
4994
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
+ }
4995
5489
  /** A journal entry as it is on disk: its length, then its bytes. */
4996
5490
  function framed(entry) {
4997
5491
  const header = Buffer.alloc(4);
@@ -5560,7 +6054,7 @@ function watch(store) {
5560
6054
  return change;
5561
6055
  }
5562
6056
  function keyToDisk(key) {
5563
- if (key instanceof IDBKeyRange) return { [RANGE]: {
6057
+ if (key instanceof IDBKeyRange$1) return { [RANGE]: {
5564
6058
  lower: key.lower,
5565
6059
  upper: key.upper,
5566
6060
  lowerOpen: key.lowerOpen,
@@ -5571,9 +6065,9 @@ function keyToDisk(key) {
5571
6065
  function keyFromDisk(key) {
5572
6066
  const range = key?.[RANGE];
5573
6067
  if (!range) return key;
5574
- if (range.lower === void 0) return IDBKeyRange.upperBound(range.upper, range.upperOpen);
5575
- if (range.upper === void 0) return IDBKeyRange.lowerBound(range.lower, range.lowerOpen);
5576
- 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);
5577
6071
  }
5578
6072
  function hasBlob(value, depth = 0) {
5579
6073
  if (value instanceof Blob) return true;
@@ -5691,6 +6185,7 @@ function safe(read) {
5691
6185
  }
5692
6186
  //#endregion
5693
6187
  //#region src/runtime/engine.ts
6188
+ setPublicKeyFastPath(nativePublicKey);
5694
6189
  /**
5695
6190
  * The HyperDHT network: `GHOSTLY_HYPERDHT_BOOTSTRAP` ("host:port,…") replaces the public bootstrap nodes, as it does
5696
6191
  * for the Desktop's sidecar; a network all on loopback (the tests') is announced on loopback too.
@@ -5800,6 +6295,7 @@ async function installWebRtc() {
5800
6295
  "RTCPeerConnectionIceEvent",
5801
6296
  "RTCCertificate"
5802
6297
  ]) scope[name] ??= polyfill[name];
6298
+ countConnections(scope);
5803
6299
  refuseDataAnswers(scope);
5804
6300
  if (!publicStun()) leaveOutPublicStun(scope);
5805
6301
  return true;
@@ -5808,6 +6304,27 @@ async function installWebRtc() {
5808
6304
  return false;
5809
6305
  }
5810
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
+ }
5811
6328
  /**
5812
6329
  * For tests: `GHOSTLY_TEST_REFUSE_DATA_ANSWERS=<n>` refuses one in every n answers this process applies to its offers
5813
6330
  * (the first, the n+1st, and so on), as libdatachannel 0.24.5 refuses one in its race (`ANSWER_REFUSED_REDIALS` in
@@ -5895,6 +6412,7 @@ async function startRuntime(paths, options = {}) {
5895
6412
  const said = options.logJoins ? joinLogLine(line) : null;
5896
6413
  if (said) process.stderr.write(said + "\n");
5897
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 });
5898
6416
  const store = await openPersistentIndexedDb(paths.db);
5899
6417
  const restored = join(paths.dir, RESTORED_MARK);
5900
6418
  if (existsSync(restored)) {
@@ -5922,8 +6440,8 @@ async function startRuntime(paths, options = {}) {
5922
6440
  const [{ registerFileBytes }, { NodeFileBytes }] = await Promise.all([import("./fileBytes-BtIyoDF2.js"), import("./fileBytes-B0z9A6Le.js")]);
5923
6441
  registerFileBytes("native", async () => new NodeFileBytes(paths.files), true);
5924
6442
  if (await (await import("./blobFiles-BmsrvZc3.js")).moveBlobFiles()) await store.compact();
5925
- const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-B6PQ6iae.js"), import("./endpoint-DarqblYE.js")]);
5926
- const mainline = dht.off ? null : new (await (import("./mainline-DYrjsS1t.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({
5927
6445
  bootstrap: dht.bootstrap,
5928
6446
  host: dht.host
5929
6447
  });
@@ -5931,7 +6449,7 @@ async function startRuntime(paths, options = {}) {
5931
6449
  ...pinned ? { relays: pinned } : {},
5932
6450
  ...relayBudgetForTests()
5933
6451
  };
5934
- const transport = mainline || pinned ? new (await (import("./mainline-DYrjsS1t.js"))).RelaysAndDht(mainline, relayOptions, !!pinned) : null;
6452
+ const transport = mainline || pinned ? new (await (import("./mainline-DCuGUSZF.js"))).RelaysAndDht(mainline, relayOptions, !!pinned) : null;
5935
6453
  const apps = servedApps(paths.dir).length > 0;
5936
6454
  const server = new EngineServer({
5937
6455
  ...transport ? { transport } : {},
@@ -5954,6 +6472,7 @@ async function startRuntime(paths, options = {}) {
5954
6472
  callsSupport: callsUnavailable === null,
5955
6473
  ...callsUnavailable ? { callsUnavailable } : {},
5956
6474
  ...options.autoJoins ? { autoJoins: options.autoJoins } : {},
6475
+ onlineNotices: options.onlineNotices ?? (() => false),
5957
6476
  ...options.deferGroups ? { deferGroups: true } : {},
5958
6477
  ...options.deferChats ? { deferChats: true } : {},
5959
6478
  ...options.offline ? { offline: true } : {},
@@ -6017,11 +6536,14 @@ async function openHost(paths, mode, version, options = {}) {
6017
6536
  let runtime;
6018
6537
  let groupAuto;
6019
6538
  const autoJoins = (chat) => !!groupAuto?.autoJoins(chat);
6539
+ let presence;
6540
+ const onlineNotices = () => !!presence?.saysOnline();
6020
6541
  try {
6021
6542
  runtime = await startRuntime(paths, {
6022
6543
  ...options,
6023
6544
  ...mode === "daemon" ? { logJoins: true } : {},
6024
6545
  autoJoins,
6546
+ onlineNotices,
6025
6547
  keepNewest: 100
6026
6548
  });
6027
6549
  } catch (error) {
@@ -6030,31 +6552,59 @@ async function openHost(paths, mode, version, options = {}) {
6030
6552
  }
6031
6553
  const hub = new EventHub(paths.events);
6032
6554
  let calls;
6033
- let presence;
6034
6555
  try {
6035
- await hub.open();
6036
- const node = runtime.server.node;
6037
- const now = node.getState();
6038
- const mark = runtime.server.historyMark;
6039
- 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();
6040
6563
  const whole = (id) => id.startsWith("group:") ? node.groupMessages({ groupId: id.slice(6) }) : node.getMessages(id);
6041
- const closed = !runtime.store.recovered;
6042
- const kept = runtime.server.takeStartNewest();
6043
- for (const id of [...now.links.map((link) => link.id), ...now.groups.map((group) => `group:${group.id}`)]) {
6044
- const page = closed && !runtime.server.historyChanged(id) ? kept.get(id) ?? await node.messagePage({
6045
- linkId: id,
6046
- limit: 100
6047
- }) : null;
6048
- if (page && !page.more) histories.set(id, page.messages);
6049
- else if (page && hub.inStep(id, page.messages)) newest.set(id, page.messages);
6050
- else histories.set(id, await whole(id));
6051
- }
6052
- hub.baseline(now, histories, {
6053
- newest,
6054
- whole
6055
- });
6056
- await hub.ended((id) => fileStore.get(id));
6057
- 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();
6058
6608
  calls = new CallManager({
6059
6609
  engine: node,
6060
6610
  emit: (type, id, fields) => hub.emit(type, id, fields),
@@ -6071,10 +6621,7 @@ async function openHost(paths, mode, version, options = {}) {
6071
6621
  });
6072
6622
  groupAuto = joiner;
6073
6623
  hub.onState((s) => joiner.check(s));
6074
- runtime.server.attach(hub.sink, {
6075
- mark,
6076
- ids: [...histories.keys(), ...newest.keys()]
6077
- });
6624
+ server.attach(hub.sink, read);
6078
6625
  joiner.check(node.getState());
6079
6626
  if (mode === "daemon") {
6080
6627
  const said = new DaemonPresence(node);
@@ -6102,8 +6649,13 @@ async function openHost(paths, mode, version, options = {}) {
6102
6649
  ctx,
6103
6650
  close: () => closing ??= (async () => {
6104
6651
  await Promise.all([calls.stopAll(), flushCardUpdates(ctx)]).catch(() => {});
6652
+ hub.sink.post({
6653
+ kind: "state",
6654
+ state: runtime.server.node.getState()
6655
+ });
6105
6656
  presence?.stop();
6106
6657
  runtime.server.detach(hub.sink);
6658
+ await hub.caughtUp();
6107
6659
  hub.release();
6108
6660
  await runtime.close().catch(() => {});
6109
6661
  release();
@@ -6146,8 +6698,8 @@ async function serve(host) {
6146
6698
  return {
6147
6699
  server,
6148
6700
  close: () => closing ??= new Promise((resolve) => {
6149
- removeDaemonSecret(ctx.runtime.paths.dir);
6150
6701
  server.close(() => resolve());
6702
+ removeDaemonSecret(ctx.runtime.paths.dir);
6151
6703
  stopping.abort();
6152
6704
  within(Promise.all(answering), STOP_ANSWERS_MS).then(() => {
6153
6705
  for (const socket of sockets) socket.end();
@@ -6267,6 +6819,13 @@ function connection(ctx, socket, secret, answering) {
6267
6819
  });
6268
6820
  return;
6269
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
+ }
6270
6829
  const id = typeof request.id === "number" || typeof request.id === "string" ? request.id : null;
6271
6830
  if (typeof request.method !== "string") {
6272
6831
  write({
@@ -6347,4 +6906,4 @@ function connection(ctx, socket, secret, answering) {
6347
6906
  //#endregion
6348
6907
  export { BASELINE_NEWEST, MAX_UNREAD, STOP_ANSWERS_MS, keepServing, openHost, serve };
6349
6908
 
6350
- //# sourceMappingURL=host-8RskzQgB.js.map
6909
+ //# sourceMappingURL=host-C0NMNQLl.js.map