@ghostlytools/cli 1.11.1 → 1.11.3

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 (107) hide show
  1. package/README.md +32 -23
  2. package/dist/assets/{api-DL0Uazf6.js → api-DBlaoz8i.js} +478 -141
  3. package/dist/assets/api-DBlaoz8i.js.map +1 -0
  4. package/dist/assets/{api-D89ubgas.js → api-DgU4MZPD.js} +2 -2
  5. package/dist/assets/{apiKit-D78MXTrk.js → apiKit-DesObIDf.js} +19 -9
  6. package/dist/assets/apiKit-DesObIDf.js.map +1 -0
  7. package/dist/assets/appFetch-C8yZIq1e.js +194 -0
  8. package/dist/assets/appFetch-C8yZIq1e.js.map +1 -0
  9. package/dist/assets/{apps--pAqmfgQ.js → apps-DkMjDHML.js} +142 -67
  10. package/dist/assets/apps-DkMjDHML.js.map +1 -0
  11. package/dist/assets/{apps-BlSl1nGn.js → apps-rzxsjfBV.js} +1 -1
  12. package/dist/assets/{arkade-DxB-MR43.js → arkade-CarKONmH.js} +2 -2
  13. package/dist/assets/{arkade-DxB-MR43.js.map → arkade-CarKONmH.js.map} +1 -1
  14. package/dist/assets/{backup-B3JJd75o.js → backup-C0OJCJlM.js} +90 -47
  15. package/dist/assets/backup-C0OJCJlM.js.map +1 -0
  16. package/dist/assets/{backup-T7gFpU80.js → backup-CgWU7x0T.js} +1 -1
  17. package/dist/assets/bark_ffi_wasm_bg-EY2uc0Bx.js.map +1 -1
  18. package/dist/assets/{bitcoind-CZchoh43.js → bitcoind-CdSmvy2y.js} +3 -3
  19. package/dist/assets/{bitcoind-CZchoh43.js.map → bitcoind-CdSmvy2y.js.map} +1 -1
  20. package/dist/assets/{bitcoindProvider-DGw3xBSa.js → bitcoindProvider-BvoRh0D5.js} +2 -2
  21. package/dist/assets/{bitcoindProvider-DGw3xBSa.js.map → bitcoindProvider-BvoRh0D5.js.map} +1 -1
  22. package/dist/assets/bitcoindevkit_bg-B-iLWrmX.js.map +1 -1
  23. package/dist/assets/buttons-Cco0Agfa.js +2 -0
  24. package/dist/assets/{buttons-C1VbDihe.js → buttons-fZTEa_2i.js} +47 -7
  25. package/dist/assets/buttons-fZTEa_2i.js.map +1 -0
  26. package/dist/assets/{childEnv-BVOQK9EA.js → childEnv-K_IzUqQx.js} +4 -3
  27. package/dist/assets/childEnv-K_IzUqQx.js.map +1 -0
  28. package/dist/assets/{client-CGLuWFRV.js → client-Ibk3-e60.js} +31 -3
  29. package/dist/assets/client-Ibk3-e60.js.map +1 -0
  30. package/dist/assets/{codec-YGJDr7uH.js → codec-DMpQqVSt.js} +2 -2
  31. package/dist/assets/{codec-YGJDr7uH.js.map → codec-DMpQqVSt.js.map} +1 -1
  32. package/dist/assets/endpoint-DarqblYE.js +5052 -0
  33. package/dist/assets/endpoint-DarqblYE.js.map +1 -0
  34. package/dist/assets/{envelope-Dwq8EN8x.js → envelope-CvQVCGdf.js} +2 -2
  35. package/dist/assets/{envelope-Dwq8EN8x.js.map → envelope-CvQVCGdf.js.map} +1 -1
  36. package/dist/assets/{fedimintSdk-CqTeMdF0.js → fedimintSdk-zV_5Qdqz.js} +6 -3
  37. package/dist/assets/fedimintSdk-zV_5Qdqz.js.map +1 -0
  38. package/dist/assets/{fixedHosts-CINXidpd.js → fixedHosts-6gKn8dp2.js} +3 -3
  39. package/dist/assets/{fixedHosts-CINXidpd.js.map → fixedHosts-6gKn8dp2.js.map} +1 -1
  40. package/dist/assets/{host-CGlHkiQv.js → host-CYlIiY3r.js} +455 -104
  41. package/dist/assets/host-CYlIiY3r.js.map +1 -0
  42. package/dist/assets/{hyperdhtRelay-Bj5jJT5w.js → hyperdhtRelay-CdsLcAhY.js} +2 -2
  43. package/dist/assets/{hyperdhtRelay-Bj5jJT5w.js.map → hyperdhtRelay-CdsLcAhY.js.map} +1 -1
  44. package/dist/assets/{irohWeb-CYn0uB_A.js → irohWeb-D2IuloLL.js} +705 -43
  45. package/dist/assets/irohWeb-D2IuloLL.js.map +1 -0
  46. package/dist/assets/jsonOut-POtMBS_V.js +16 -0
  47. package/dist/assets/jsonOut-POtMBS_V.js.map +1 -0
  48. package/dist/assets/{main-CgLW_Gyx.js → main-C0PfjKTe.js} +263 -128
  49. package/dist/assets/main-C0PfjKTe.js.map +1 -0
  50. package/dist/assets/{mainline-B0h8RchD.js → mainline-Cu61qcPS.js} +60 -24
  51. package/dist/assets/mainline-Cu61qcPS.js.map +1 -0
  52. package/dist/assets/{networkReason-BBosp2KG.js → networkReason-Dt7Yx_n4.js} +3 -3
  53. package/dist/assets/{networkReason-BBosp2KG.js.map → networkReason-Dt7Yx_n4.js.map} +1 -1
  54. package/dist/assets/{nwc-SSNS1aJ-.js → nwc-CtZypDcv.js} +6 -5
  55. package/dist/assets/nwc-CtZypDcv.js.map +1 -0
  56. package/dist/assets/{nwcProvider-BXqJmM98.js → nwcProvider-DYHkd0iu.js} +6 -3
  57. package/dist/assets/{nwcProvider-BXqJmM98.js.map → nwcProvider-DYHkd0iu.js.map} +1 -1
  58. package/dist/assets/{ownDid--DtH_Jdi.js → ownDid-CJ0ta8CL.js} +3 -3
  59. package/dist/assets/{ownDid--DtH_Jdi.js.map → ownDid-CJ0ta8CL.js.map} +1 -1
  60. package/dist/assets/{ownStatus-CQPGjCUR.js → ownStatus-D4HC6tNR.js} +12 -6
  61. package/dist/assets/{ownStatus-CQPGjCUR.js.map → ownStatus-D4HC6tNR.js.map} +1 -1
  62. package/dist/assets/{profiles-4UQ1Hsds.js → profiles-Ccdq6Xf6.js} +82 -6
  63. package/dist/assets/profiles-Ccdq6Xf6.js.map +1 -0
  64. package/dist/assets/{registry-B6BXKv6Q.js → registry-CE0PpQAE.js} +1 -1
  65. package/dist/assets/{registry-BKCFTEYp.js → registry-LvxsLJ5S.js} +88 -25
  66. package/dist/assets/registry-LvxsLJ5S.js.map +1 -0
  67. package/dist/assets/{resolve-MFeFljsN.js → resolve-6kVu2DE5.js} +4 -3
  68. package/dist/assets/resolve-6kVu2DE5.js.map +1 -0
  69. package/dist/assets/{server-rHKhnog1.js → server-Q3VcNqGt.js} +630 -933
  70. package/dist/assets/server-Q3VcNqGt.js.map +1 -0
  71. package/dist/assets/{src-BzsyAvPf.js → src-DjVj2ynY.js} +580 -168
  72. package/dist/assets/src-DjVj2ynY.js.map +1 -0
  73. package/dist/assets/{sshForges-qITn-RoV.js → sshForges-DeZ9cN9Q.js} +4 -3
  74. package/dist/assets/sshForges-DeZ9cN9Q.js.map +1 -0
  75. package/dist/assets/{translate-Bkr_iDgp.js → translate-iQZq4TnY.js} +36 -15
  76. package/dist/assets/translate-iQZq4TnY.js.map +1 -0
  77. package/dist/assets/{types-C4_KafL_.js → types-Clyb52K2.js} +2 -1
  78. package/dist/assets/types-Clyb52K2.js.map +1 -0
  79. package/dist/assets/{usdt-Bp7T2iqt.js → usdt-DnyTGdTX.js} +3 -3
  80. package/dist/assets/{usdt-Bp7T2iqt.js.map → usdt-DnyTGdTX.js.map} +1 -1
  81. package/dist/ghostly.mjs +1 -1
  82. package/npm-shrinkwrap.json +3592 -0
  83. package/package.json +5 -3
  84. package/dist/assets/api-DL0Uazf6.js.map +0 -1
  85. package/dist/assets/apiKit-D78MXTrk.js.map +0 -1
  86. package/dist/assets/apps--pAqmfgQ.js.map +0 -1
  87. package/dist/assets/backup-B3JJd75o.js.map +0 -1
  88. package/dist/assets/buttons-C1VbDihe.js.map +0 -1
  89. package/dist/assets/buttons-DwWuK_uN.js +0 -2
  90. package/dist/assets/childEnv-BVOQK9EA.js.map +0 -1
  91. package/dist/assets/client-CGLuWFRV.js.map +0 -1
  92. package/dist/assets/endpoint-DWHkXWVM.js +0 -419
  93. package/dist/assets/endpoint-DWHkXWVM.js.map +0 -1
  94. package/dist/assets/fedimintSdk-CqTeMdF0.js.map +0 -1
  95. package/dist/assets/host-CGlHkiQv.js.map +0 -1
  96. package/dist/assets/irohWeb-CYn0uB_A.js.map +0 -1
  97. package/dist/assets/main-CgLW_Gyx.js.map +0 -1
  98. package/dist/assets/mainline-B0h8RchD.js.map +0 -1
  99. package/dist/assets/nwc-SSNS1aJ-.js.map +0 -1
  100. package/dist/assets/profiles-4UQ1Hsds.js.map +0 -1
  101. package/dist/assets/registry-BKCFTEYp.js.map +0 -1
  102. package/dist/assets/resolve-MFeFljsN.js.map +0 -1
  103. package/dist/assets/server-rHKhnog1.js.map +0 -1
  104. package/dist/assets/src-BzsyAvPf.js.map +0 -1
  105. package/dist/assets/sshForges-qITn-RoV.js.map +0 -1
  106. package/dist/assets/translate-Bkr_iDgp.js.map +0 -1
  107. package/dist/assets/types-C4_KafL_.js.map +0 -1
@@ -1,15 +1,16 @@
1
- import { Ca as isPublicAddress, Ci as checkPushEndpoint, Cs as CALL_SIGNAL_MAX_AGE_MS, Ds as buildSdpFromSignal, Es as answersOffer, Ku as setLinkTraceSink, Ms as parseCallSignal, Ns as signalHasVideo, Os as callRtcConfig, Ts as RTC_CONFIG, cs as DAEMON_WATCH_PEER_IDLE_MS, hd as fromBase64Url, ja as ACTIVE_DHT_POLL_MS, js as extractParamsFromSdp, ks as callSignalHeardAt } from "./src-BzsyAvPf.js";
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";
2
2
  import { p as fileStore } from "./idb-CEuoNsTv.js";
3
- import { r as irohRelayProblem } from "./irohWeb-CYn0uB_A.js";
4
- import { n as fedimintSdkOn } from "./fedimintSdk-CqTeMdF0.js";
3
+ import { r as irohRelayProblem } from "./irohWeb-D2IuloLL.js";
4
+ import { n as fedimintSdkOn } from "./fedimintSdk-zV_5Qdqz.js";
5
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-D78MXTrk.js";
7
- import { _ as resumeHolds, 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 flushCardUpdates } from "./api-DL0Uazf6.js";
8
- import { C as privateFolder, b as ownFolder, n as RESTORED_MARK, r as acquireLock, v as startingPath, w as runtimePath, x as ownProfile } from "./profiles-4UQ1Hsds.js";
9
- import { _ as isCallRate, c as newDaemonSecret, f as DEFAULT_RATE, g as frameSamples, h as frameBytes, l as proof, m as bytesToMs, p as PlaybackQueue, s as greeting, u as removeDaemonSecret, v as monotonic } from "./client-CGLuWFRV.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";
10
11
  import { createRequire } from "node:module";
11
- import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, fsyncSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, statSync, truncateSync, writeFileSync, writeSync } from "node:fs";
12
- import { readFile } from "node:fs/promises";
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
+ import { open, readFile } from "node:fs/promises";
13
14
  import { dirname, join } from "node:path";
14
15
  import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
15
16
  import { networkInterfaces } from "node:os";
@@ -20,6 +21,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
20
21
  import { Worker } from "node:worker_threads";
21
22
  import { request } from "node:https";
22
23
  import { lookup } from "node:dns";
24
+ import { WebSocket } from "ws";
23
25
  import { deserialize, serialize } from "node:v8";
24
26
  import { IDBFactory, IDBKeyRange, IDBTransaction } from "fake-indexeddb";
25
27
  //#region src/announce.ts
@@ -75,6 +77,21 @@ function clearAudioSockets(profileDir) {
75
77
  } catch {}
76
78
  }
77
79
  /**
80
+ * The socket's handlers for a call's queue: what the program writes is queued, and a program that takes the socket
81
+ * (the first, or one that replaces the last) starts on a queue of its own (`PlaybackQueue.newWriter`): the last
82
+ * one's leftover, half a sample included, would otherwise shift all of its audio by a byte.
83
+ */
84
+ function feedQueue(queue) {
85
+ return {
86
+ onAudio: (chunk) => {
87
+ queue.push(chunk);
88
+ },
89
+ onClient: (connected) => {
90
+ if (connected) queue.newWriter();
91
+ }
92
+ };
93
+ }
94
+ /**
78
95
  * Serves one call's audio to one program at a time: what the program writes is handed to `onAudio` as it comes (raw
79
96
  * PCM, any amount); `write` gives it the caller's audio. A second connection replaces the first. `close` ends the
80
97
  * program's connection (it reads EOF) and removes the socket.
@@ -139,7 +156,10 @@ var AudioSocket = class AudioSocket {
139
156
  this.unproven.delete(socket);
140
157
  const before = this.client;
141
158
  this.client = socket;
142
- before?.end();
159
+ if (before) {
160
+ before.end();
161
+ setTimeout(() => before.destroy(), 2e3).unref();
162
+ }
143
163
  handlers.onClient?.(true);
144
164
  };
145
165
  let timer = null;
@@ -259,6 +279,10 @@ var RtpWriter = class {
259
279
  this.timestamp = this.timestamp + 960 >>> 0;
260
280
  return Buffer.concat([header, payload]);
261
281
  }
282
+ /** Moves the timestamp on by frames that were never sent (their time went by), the sequence as it is. */
283
+ skip(frames) {
284
+ this.timestamp = this.timestamp + frames * 960 >>> 0;
285
+ }
262
286
  };
263
287
  /** The most frames a gap in the packets becomes (concealed by the decoder, see CallAudio). */
264
288
  var MAX_LOST_FRAMES = 3;
@@ -521,11 +545,13 @@ var CallAudio = class {
521
545
  * slower than a frame for a while (every 40 ms under a large file) keeps up, and a program writing at real time
522
546
  * stays there. The contact's jitter buffer takes the short burst. A stall past MAX_CATCH_UP_MS is not replayed: the
523
547
  * clock starts again and what the program wrote during it is dropped, or it would be heard that much late for the
524
- * rest of the call.
548
+ * rest of the call. The frames it skipped still move the RTP timestamp on (the sampling time, RFC 3550), so the
549
+ * contact's jitter buffer does not take the gap for seconds of jitter.
525
550
  */
526
551
  catchUp(late) {
527
552
  const now = this.options.now ?? monotonic;
528
553
  if (late > 1e3) {
554
+ this.writer.skip(Math.round(late / 20));
529
555
  this.owed = late;
530
556
  this.kept = this.ahead;
531
557
  this.owedUntil = now() + MAX_CATCH_UP_MS;
@@ -1329,7 +1355,7 @@ var CallManager = class {
1329
1355
  v: 0
1330
1356
  });
1331
1357
  } catch (error) {
1332
- await this.end(call, "failed", true, false);
1358
+ await this.end(call, "failed", true);
1333
1359
  throw error;
1334
1360
  } finally {
1335
1361
  early?.close();
@@ -1457,7 +1483,7 @@ var CallManager = class {
1457
1483
  */
1458
1484
  async attach(call, create) {
1459
1485
  const queue = call.queue = new PlaybackQueue(call.rate);
1460
- const socket = await AudioSocket.open(audioSocketPath(this.host.profileDir, call.id), call.rate, { onAudio: (chunk) => queue.push(chunk) });
1486
+ const socket = await AudioSocket.open(audioSocketPath(this.host.profileDir, call.id), call.rate, feedQueue(queue));
1461
1487
  if (call.ended) {
1462
1488
  await socket.close();
1463
1489
  return;
@@ -1772,6 +1798,47 @@ function readAuto(path) {
1772
1798
  return off;
1773
1799
  }
1774
1800
  }
1801
+ //#endregion
1802
+ //#region src/daemonPresence.ts
1803
+ /** How often a daemon that shares its presence says `active` again: well within the engine's 10 minutes to `idle`. */
1804
+ var PRESENCE_EVERY_MS = STATUS_IDLE_AFTER_MS / 2;
1805
+ /**
1806
+ * A headless profile's presence (WISP 401 § Presence and status): off unless `settings set daemonPresence true`. On,
1807
+ * a running daemon says `active` to every paired contact whose app offers `status/1`, as a page does on input, and keeps
1808
+ * saying it while it runs; contacts see its dot. Turned off, it is taken back at once. When the daemon stops, its
1809
+ * sessions end and presence with them. A one-shot command never says it: it is gone a moment later.
1810
+ */
1811
+ var DaemonPresence = class {
1812
+ node;
1813
+ timer;
1814
+ constructor(node) {
1815
+ this.node = node;
1816
+ }
1817
+ /** Follows the setting in each state the engine draws. */
1818
+ check(state) {
1819
+ const on = state.settings.daemonPresence === true;
1820
+ if (on === (this.timer !== void 0)) return;
1821
+ if (!on) {
1822
+ this.stop();
1823
+ this.node.setPresence({ presence: null });
1824
+ return;
1825
+ }
1826
+ this.node.setPresence({ presence: "active" });
1827
+ this.timer = setInterval(() => this.node.setPresence({ presence: "active" }), PRESENCE_EVERY_MS);
1828
+ this.timer.unref?.();
1829
+ }
1830
+ /**
1831
+ * Whether the engine's online notices go (WISP 401 § Online notices, `NodeOptions.onlineNotices`): only while presence
1832
+ * is said. Off, a contact's closed app was pushed "<bot> is online" 30 s after each start, while the bot showed no dot.
1833
+ */
1834
+ saysOnline() {
1835
+ return this.timer !== void 0;
1836
+ }
1837
+ stop() {
1838
+ clearInterval(this.timer);
1839
+ this.timer = void 0;
1840
+ }
1841
+ };
1775
1842
  /** A journal past this is trimmed, and only this much of its end is ever read. */
1776
1843
  var JOURNAL_MAX_BYTES = 33554432;
1777
1844
  /**
@@ -1853,7 +1920,7 @@ var EventHub = class {
1853
1920
  /** `file.*` events whose message was not seen yet: they wait for it a moment (the engine stores it right after). */
1854
1921
  pendingFiles = /* @__PURE__ */ new Map();
1855
1922
  baselined = false;
1856
- /** Chats and groups whose history the baseline did not read whole, and how to read it: until a file event needs it. */
1923
+ /** Chats and groups whose history the baseline did not read whole, and how to read their file rows: until a file event needs them. */
1857
1924
  unread = /* @__PURE__ */ new Map();
1858
1925
  /**
1859
1926
  * Whether the seen set notes mentions (`+m`): false on a set written before it did, whose mentions were reported with
@@ -1955,7 +2022,7 @@ var EventHub = class {
1955
2022
  for (const [chat, messages] of histories) this.messages(chat, messages);
1956
2023
  for (const [chat, messages] of rest?.newest ?? []) {
1957
2024
  this.messages(chat, messages, []);
1958
- this.unread.set(chat, rest.whole);
2025
+ this.unread.set(chat, rest.files);
1959
2026
  }
1960
2027
  this.baselined = true;
1961
2028
  this.firstRun = false;
@@ -2061,7 +2128,7 @@ var EventHub = class {
2061
2128
  to: Math.min(this.dropped, next - 1)
2062
2129
  });
2063
2130
  };
2064
- if (existsSync(this.journalPath)) for (const line of journalTail(this.journalPath).lines) {
2131
+ if (existsSync(this.journalPath)) for (const line of journalAfter(this.journalPath, since)) {
2065
2132
  let event;
2066
2133
  try {
2067
2134
  event = JSON.parse(line);
@@ -2347,7 +2414,7 @@ var EventHub = class {
2347
2414
  this.chats.set(id, shape);
2348
2415
  if (quiet) continue;
2349
2416
  if (!before) {
2350
- this.emit("chat.created", `chat.created:${id}`, {
2417
+ this.emit("chat.created", `chat.created:${id}:${this.now()}`, {
2351
2418
  chat: id,
2352
2419
  summary: {
2353
2420
  ...chatJson(link),
@@ -2392,7 +2459,7 @@ var EventHub = class {
2392
2459
  const removedChats = [...this.chats.keys()].filter((id) => !links.has(id));
2393
2460
  for (const id of removedChats) {
2394
2461
  this.chats.delete(id);
2395
- if (!quiet) this.emit("chat.removed", `chat.removed:${id}`, { chat: id });
2462
+ if (!quiet) this.emit("chat.removed", `chat.removed:${id}:${this.now()}`, { chat: id });
2396
2463
  }
2397
2464
  const chatsGone = new Set(removedChats);
2398
2465
  this.forget(removedChats, (event) => chatsGone.has(event.chat) && event.type !== "chat.removed");
@@ -2402,7 +2469,7 @@ var EventHub = class {
2402
2469
  this.groups.set(id, shape);
2403
2470
  if (quiet) continue;
2404
2471
  if (!before) {
2405
- this.emit("group.created", `group.created:${id}`, {
2472
+ this.emit("group.created", `group.created:${id}:${this.now()}`, {
2406
2473
  group: id,
2407
2474
  summary: groupJson(group)
2408
2475
  });
@@ -2437,7 +2504,7 @@ var EventHub = class {
2437
2504
  const removedGroups = [...this.groups.keys()].filter((id) => !groups.has(id));
2438
2505
  for (const id of removedGroups) {
2439
2506
  this.groups.delete(id);
2440
- if (!quiet) this.emit("group.removed", `group.removed:${id}`, { group: id });
2507
+ if (!quiet) this.emit("group.removed", `group.removed:${id}:${this.now()}`, { group: id });
2441
2508
  }
2442
2509
  const groupsGone = new Set(removedGroups);
2443
2510
  this.forget(removedGroups, (event) => groupsGone.has(event.group) && event.type !== "group.removed");
@@ -2654,6 +2721,8 @@ var EventHub = class {
2654
2721
  this.reactions(chat, group, messages, quiet);
2655
2722
  const files = [];
2656
2723
  const cutoff = this.now() - SEEN_KEEP_MS;
2724
+ let twinLines;
2725
+ const twins = () => twinLines ??= group ? groupPayTwins(messages, new Map(messages.flatMap((m) => m.groupPay ? [[m.groupPay.id, m.groupPay]] : [])), this.state?.payments ?? {}, this.state?.groups.find((g) => g.id === group)) : /* @__PURE__ */ new Map();
2657
2726
  for (const message of messages) {
2658
2727
  present.add(message.id);
2659
2728
  if (message.file && this.fileMessages.get(message.file.id) !== message.id) {
@@ -2663,7 +2732,7 @@ var EventHub = class {
2663
2732
  let state = stateOf(message);
2664
2733
  const note = known.get(message.id), before = note === void 0 ? void 0 : stateIn(note);
2665
2734
  const at = activityOf(message);
2666
- const json = () => group ? groupMessageJson(message, this.state?.groups.find((g) => g.id === group)) : chatMessageJson(message, this.state?.links.find((l) => l.id === chat));
2735
+ const json = () => group ? groupMessageJson(message, this.state?.groups.find((g) => g.id === group), twins().get(message.id)) : chatMessageJson(message, this.state?.links.find((l) => l.id === chat));
2667
2736
  if (before === void 0 && cameAt(message) < cutoff) {
2668
2737
  if (at < cutoff) continue;
2669
2738
  const noted = noteOf(state, at);
@@ -2694,7 +2763,14 @@ var EventHub = class {
2694
2763
  }
2695
2764
  continue;
2696
2765
  }
2697
- const noted = noteOf(state, at);
2766
+ const [delivery] = splitState(state), [was] = before === void 0 ? [void 0] : splitState(before);
2767
+ const passed = passesIn(note, was), moved = !group && message.sender === "me" && was !== void 0 && delivery !== was;
2768
+ const again = moved && (passed.been & deliveryBit(delivery)) !== 0;
2769
+ const passes = moved ? {
2770
+ count: passed.count + 1,
2771
+ been: passed.been | deliveryBit(delivery)
2772
+ } : passed;
2773
+ const noted = noteOf(state, at, passes);
2698
2774
  known.set(message.id, noted);
2699
2775
  changes.push([message.id, noted]);
2700
2776
  if (quiet) continue;
@@ -2717,8 +2793,9 @@ var EventHub = class {
2717
2793
  if (message.sender === "peer" && message.press) this.pressed(chat, group, message, json());
2718
2794
  continue;
2719
2795
  }
2720
- if (message.sender === "peer" && message.press && !pressedIn(before)) this.pressed(chat, group, message, json());
2721
- if (group && mentionedIn(state) && !mentionedIn(before) && this.mentionsNoted) this.emit("group.mentioned", `group.mentioned:${group}:${message.id}`, {
2796
+ const mentioned = !!group && mentionedIn(state) && !mentionedIn(before) && this.mentionsNoted;
2797
+ if (message.sender === "peer" && message.press && !pressedIn(before)) this.pressed(chat, group, message, json(), !!group && !mentioned);
2798
+ if (mentioned) this.emit("group.mentioned", `group.mentioned:${group}:${message.id}`, {
2722
2799
  group,
2723
2800
  messageId: message.id,
2724
2801
  message: json()
@@ -2732,14 +2809,14 @@ var EventHub = class {
2732
2809
  message: json()
2733
2810
  });
2734
2811
  } else {
2735
- const [delivery, edits] = splitState(state), [was, edited] = splitState(before);
2812
+ const [, edits] = splitState(state), [, edited] = splitState(before);
2736
2813
  if (edits > edited && !(message.sender === "me" && message.edit?.restore)) this.emit("message.edited", `message.edited:${chat}:${message.id}:${edits}`, {
2737
2814
  chat,
2738
2815
  messageId: message.id,
2739
2816
  edits,
2740
2817
  message: json()
2741
2818
  });
2742
- if (message.sender === "me" && delivery !== was) this.emit("message.delivery", `message.delivery:${chat}:${message.id}:${delivery}`, {
2819
+ if (moved) this.emit("message.delivery", `message.delivery:${chat}:${message.id}:${delivery}${again ? `:${passes.count}` : ""}`, {
2743
2820
  chat,
2744
2821
  messageId: message.id,
2745
2822
  delivery,
@@ -2788,9 +2865,9 @@ var EventHub = class {
2788
2865
  * profile gave the chat, as `chat list` gives it; null without one, and in a group) and the reply (`replyId`).
2789
2866
  * `untrusted.name` is the name the person gave themselves, as an agent turn's: theirs to choose, so data, never
2790
2867
  * instructions, and under `untrusted` only. The engine marks a reply a press only while the buttons were open for
2791
- * that person.
2868
+ * that person. `message` (the reply, as `group.message` gives it) only on a group press learned after its reply's event.
2792
2869
  */
2793
- pressed(chat, group, message, json) {
2870
+ pressed(chat, group, message, json, withReply = false) {
2794
2871
  const press = message.press;
2795
2872
  const link = group ? void 0 : this.state?.links.find((l) => l.id === chat);
2796
2873
  const name = link ? chatJson(link).label : null;
@@ -2803,7 +2880,8 @@ var EventHub = class {
2803
2880
  name,
2804
2881
  replyId: message.id,
2805
2882
  ...press.inferred ? { inferred: true } : {},
2806
- untrusted: { name: json.nick ?? null }
2883
+ untrusted: { name: json.nick ?? null },
2884
+ ...withReply ? { message: json } : {}
2807
2885
  });
2808
2886
  }
2809
2887
  /**
@@ -2875,9 +2953,35 @@ var AGES_NOTED = "ages";
2875
2953
  var UNDERWAY = "files:";
2876
2954
  /**
2877
2955
  * A note: what the seen set keeps of a message (or a reaction's number), then `@` and the time of its last activity in
2878
- * base 36 (`received+p#e2@mg3k1x2a`), by which `sweep` drops it. A note from before has no time.
2956
+ * base 36 (`received+p#e2@mg3k1x2a`), by which `sweep` drops it. A note from before has no time. A message of mine whose
2957
+ * delivery changed ends with its passes (`sent@mg3k1x2a!4.b`): `!`, how many changes, `.` and the deliveries it was in.
2879
2958
  */
2880
- var noteOf = (state, at) => `${state}@${Math.max(0, Math.floor(at)).toString(36)}`;
2959
+ var noteOf = (state, at, passes) => `${state}@${Math.max(0, Math.floor(at)).toString(36)}${passes?.count ? `!${passes.count}.${passes.been.toString(36)}` : ""}`;
2960
+ var DELIVERIES = [
2961
+ "sending",
2962
+ "sent",
2963
+ "queued",
2964
+ "waiting",
2965
+ "held",
2966
+ "delivered",
2967
+ "failed"
2968
+ ];
2969
+ var deliveryBit = (delivery) => {
2970
+ const at = DELIVERIES.indexOf(delivery);
2971
+ return 1 << (at === -1 ? DELIVERIES.length : at);
2972
+ };
2973
+ function passesIn(note, delivery) {
2974
+ const mark = note?.lastIndexOf("!") ?? -1;
2975
+ if (mark === -1) return {
2976
+ count: 0,
2977
+ been: delivery === void 0 ? 0 : deliveryBit(delivery)
2978
+ };
2979
+ const [count, been] = note.slice(mark + 1).split(".");
2980
+ return {
2981
+ count: Number(count) || 0,
2982
+ been: parseInt(been ?? "", 36) || 0
2983
+ };
2984
+ }
2881
2985
  function stateIn(note) {
2882
2986
  const at = note.lastIndexOf("@");
2883
2987
  return at === -1 ? note : note.slice(0, at);
@@ -2972,6 +3076,63 @@ function journalTail(path) {
2972
3076
  size
2973
3077
  };
2974
3078
  }
3079
+ /** What `journalAfter` reads first from the journal's end; each read further back is twice the last. */
3080
+ var REPLAY_CHUNK = 65536;
3081
+ /** The bytes of a line's start that hold its seq (`seqAt`), and how they begin. */
3082
+ var SEQ_PREFIX = 32;
3083
+ var SEQ_START = Buffer.from("{\"seq\":");
3084
+ /**
3085
+ * The journal's lines after `since`, within its last `JOURNAL_MAX_BYTES`, as `journalTail` gives them: its end is read
3086
+ * back in growing chunks, each line's seq taken from its `{"seq":N` start (as each event is written), until a line at or
3087
+ * before `since`; only what follows it is made into text. A line with no seq there (blanked by `forget`, torn) is passed
3088
+ * over and given, as `journalTail` gives it.
3089
+ */
3090
+ function journalAfter(path, since) {
3091
+ const fd = openSync(path, "r");
3092
+ const read = (from, to) => {
3093
+ const bytes = Buffer.alloc(to - from);
3094
+ let done = 0;
3095
+ while (done < bytes.length) {
3096
+ const n = readSync(fd, bytes, done, bytes.length - done, from + done);
3097
+ if (!n) break;
3098
+ done += n;
3099
+ }
3100
+ return bytes.subarray(0, done);
3101
+ };
3102
+ try {
3103
+ const size = fstatSync(fd).size;
3104
+ const cut = Math.max(0, size - JOURNAL_MAX_BYTES - 1);
3105
+ if (cut === 0 && (seqAt(read(0, Math.min(size, SEQ_PREFIX)), 0) ?? -1) > since) return journalTail(path).lines;
3106
+ let after = size, found = false, from = size, chunk = REPLAY_CHUNK;
3107
+ const chunks = [];
3108
+ while (from > cut && !found) {
3109
+ const next = Math.max(cut, from - chunk), bytes = read(next, Math.min(size, from + SEQ_PREFIX));
3110
+ chunks.unshift(bytes.subarray(0, Math.min(bytes.length, from - next)));
3111
+ for (let newline = bytes.lastIndexOf(10, from - next - 1); newline !== -1; newline = newline > 0 ? bytes.lastIndexOf(10, newline - 1) : -1) {
3112
+ const seq = seqAt(bytes, newline + 1);
3113
+ if (seq !== void 0 && seq <= since) {
3114
+ found = true;
3115
+ break;
3116
+ }
3117
+ after = next + newline + 1;
3118
+ }
3119
+ if (!found && next === 0 && (seqAt(bytes, 0) ?? Infinity) > since) after = 0;
3120
+ from = next;
3121
+ chunk *= 2;
3122
+ }
3123
+ return (chunks.length === 1 ? chunks[0] : Buffer.concat(chunks)).toString("utf8", after - from).split("\n").filter((line) => line.trimStart());
3124
+ } finally {
3125
+ closeSync(fd);
3126
+ }
3127
+ }
3128
+ /** The seq a journal line begins with (`{"seq":N,` or `{"seq":N}`), read from its first bytes, never made into text. */
3129
+ function seqAt(bytes, at) {
3130
+ for (let i = 0; i < SEQ_START.length; i++) if (bytes[at + i] !== SEQ_START[i]) return void 0;
3131
+ let seq = 0, i = at + SEQ_START.length;
3132
+ const end = Math.min(bytes.length, at + SEQ_PREFIX);
3133
+ for (; i < end && bytes[i] >= 48 && bytes[i] <= 57; i++) seq = seq * 10 + bytes[i] - 48;
3134
+ return i > at + SEQ_START.length && (bytes[i] === 44 || bytes[i] === 125) ? seq : void 0;
3135
+ }
2975
3136
  /**
2976
3137
  * What opening needs of the journal: its last seq, its number of lines (as `journalTail` counts them) and its size. The
2977
3138
  * lines are counted by their bytes and only the last whole one is made into text.
@@ -3005,11 +3166,10 @@ function journalEnd(path) {
3005
3166
  let tail;
3006
3167
  if (bytes.length > start && bytes[bytes.length - 1] !== 10) {
3007
3168
  const from = Math.max(start, bytes.lastIndexOf(10) + 1);
3008
- const named = /^\{"seq":(\d+)[,}]/.exec(bytes.toString("latin1", from, Math.min(bytes.length, from + 32)))?.[1];
3009
3169
  tail = {
3010
3170
  at: size - bytes.length + from,
3011
3171
  whole: read === from,
3012
- seq: named === void 0 ? void 0 : Number(named)
3172
+ seq: seqAt(bytes, from)
3013
3173
  };
3014
3174
  }
3015
3175
  return {
@@ -3577,21 +3737,56 @@ function nodeHoldFetch(lookup = publicOnlyLookup) {
3577
3737
  /**
3578
3738
  * Whether the CLI's Iroh may dial a contact through the relay its descriptor names (irohWeb.ts `publicRelay`): at a
3579
3739
  * public address, or at a name whose every address is public now, as a held item's is (`publicOnlyLookup`). Asked again
3580
- * before each dial. The wasm resolves the name once more when it connects, so a name that changes its answer in
3581
- * between is not caught here.
3740
+ * before each dial. A name it takes is then opened only through that same lookup (`relaySockets`), each time the wasm
3741
+ * connects to it, so a name that answers this machine or its network after the check is not reached either.
3582
3742
  */
3583
3743
  function nodePublicRelay(lookup = publicOnlyLookup) {
3584
3744
  return (relay) => new Promise((resolve) => {
3585
- let host;
3745
+ let url;
3586
3746
  try {
3587
- host = new URL(relay).hostname.replace(/^\[(.*)\]$/, "$1");
3747
+ url = new URL(relay);
3588
3748
  } catch {
3589
3749
  return resolve(false);
3590
3750
  }
3751
+ const host = url.hostname.replace(/^\[(.*)\]$/, "$1");
3591
3752
  if (isIP(host)) return resolve(isPublicIp(host));
3592
- lookup(host, { all: true }, (error) => resolve(!error));
3753
+ lookup(host, { all: true }, (error) => {
3754
+ if (!error) relaySockets(globalThis).set(socketKey(url), lookup);
3755
+ resolve(!error);
3756
+ });
3593
3757
  });
3594
3758
  }
3759
+ /** A WebSocket URL's host and port, the same for a relay's https:// URL and the wss:// one the wasm opens. */
3760
+ function socketKey(url) {
3761
+ const port = url.port || (url.protocol === "https:" || url.protocol === "wss:" ? "443" : "80");
3762
+ return `${url.hostname.replace(/\.$/, "")}:${port}`;
3763
+ }
3764
+ var installed = /* @__PURE__ */ new WeakMap();
3765
+ /**
3766
+ * The wasm Iroh opens its relays with the global `WebSocket`, which resolves the name again by itself. From the first
3767
+ * contact's relay `nodePublicRelay` takes, the global one opens a socket to that relay's host and port with `ws` and
3768
+ * that check's lookup instead, so the address it dials is one the lookup took then; every other socket is the global
3769
+ * one as before. Answers the host and port → lookup table.
3770
+ */
3771
+ function relaySockets(scope) {
3772
+ let names = installed.get(scope);
3773
+ if (names) return names;
3774
+ const checked = names = /* @__PURE__ */ new Map();
3775
+ installed.set(scope, names);
3776
+ scope.WebSocket = new Proxy(scope.WebSocket, { construct(target, args, newTarget) {
3777
+ let lookup;
3778
+ try {
3779
+ lookup = checked.get(socketKey(new URL(args[0])));
3780
+ } catch {}
3781
+ if (!lookup) return Reflect.construct(target, args, newTarget);
3782
+ return new WebSocket(args[0], args[1], {
3783
+ lookup,
3784
+ followRedirects: false,
3785
+ perMessageDeflate: false
3786
+ });
3787
+ } });
3788
+ return names;
3789
+ }
3595
3790
  //#endregion
3596
3791
  //#region src/runtime/rows.ts
3597
3792
  /** The warning `node:sqlite` gives as it loads (experimental on Node 22 and 23): said once for every command, it is noise. */
@@ -3828,7 +4023,15 @@ var TreeFile = class {
3828
4023
  }
3829
4024
  /** Everything SQLite holds is on disk: before a snapshot takes the place of the journal that had it. */
3830
4025
  sync() {
3831
- this.db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
4026
+ const { busy } = this.db.prepare("PRAGMA wal_checkpoint(TRUNCATE)").get();
4027
+ if (!busy) return;
4028
+ const { file } = this.db.prepare("PRAGMA database_list").get();
4029
+ const fd = openSync(`${file}-wal`, "r");
4030
+ try {
4031
+ fsyncSync(fd);
4032
+ } finally {
4033
+ closeSync(fd);
4034
+ }
3832
4035
  }
3833
4036
  /** Trees whose store or index is gone from the schema (deleted, a database removed) go, with their entries. */
3834
4037
  sweep(keep) {
@@ -3931,7 +4134,7 @@ var TreeFile = class {
3931
4134
  }
3932
4135
  }
3933
4136
  };
3934
- var SqlTree = class {
4137
+ var SqlTree = class SqlTree {
3935
4138
  file;
3936
4139
  id;
3937
4140
  _keysAreUnique;
@@ -4113,6 +4316,15 @@ var SqlTree = class {
4113
4316
  }
4114
4317
  return out;
4115
4318
  }
4319
+ /**
4320
+ * This tree as it is now, its rows read from `file` (rows.db as it was at this turn, `openTreeReader`) under the
4321
+ * layers of the transactions finished so far: not the ones after, nor one still running.
4322
+ */
4323
+ at(file) {
4324
+ const tree = new SqlTree(file, this.id, this._keysAreUnique, this.index);
4325
+ tree.pending = [...this.pending];
4326
+ return tree;
4327
+ }
4116
4328
  /** @internal */
4117
4329
  complete(owner, seq) {
4118
4330
  if (!this.active || this.active.owner !== owner && this.active.owner !== null) return;
@@ -4160,6 +4372,22 @@ function openTreeFile(sqlite, path, constraintError) {
4160
4372
  db.exec("PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA secure_delete = ON");
4161
4373
  return new TreeFile(db, constraintError);
4162
4374
  }
4375
+ /**
4376
+ * rows.db as it is at this call, for a long read that leaves the event loop turns between its pages (a backup's
4377
+ * export): a second connection, read-only, in one read transaction, which the writes after it do not change (WAL).
4378
+ * Closing its `db` ends it.
4379
+ */
4380
+ function openTreeReader(sqlite, path, constraintError) {
4381
+ const db = new sqlite.DatabaseSync(path, { readOnly: true });
4382
+ try {
4383
+ db.exec("BEGIN");
4384
+ db.prepare("SELECT 1 FROM entries LIMIT 1").get();
4385
+ return new TreeFile(db, constraintError);
4386
+ } catch (error) {
4387
+ db.close();
4388
+ throw error;
4389
+ }
4390
+ }
4163
4391
  /** A range's keys as bytes: from the first (in) up to the second (out), undefined where it is open. */
4164
4392
  function bounds(range) {
4165
4393
  return [range?.lower === void 0 ? void 0 : keyBytes(range.lower) + (range.lowerOpen ? "ÿ" : ""), range?.upper === void 0 ? void 0 : keyBytes(range.upper) + (range.upperOpen ? "" : "ÿ")];
@@ -4231,8 +4459,17 @@ var FOLLOWS = "__ghostly_journal__";
4231
4459
  var SEQ = "__ghostly_entry__";
4232
4460
  /** How long `close` waits for the transactions still running: one left open for good does not keep the process. */
4233
4461
  var CLOSE_WAIT_MS = 5e3;
4462
+ /** Records of rows.db an export reads in one turn of the event loop: a few milliseconds' worth. */
4463
+ var EXPORT_PAGE = 256;
4234
4464
  /** How long a relaxed transaction's entry may wait for a sync when no other transaction brings one. */
4235
4465
  var RELAXED_SYNC_MS = 1e3;
4466
+ /**
4467
+ * How long the trees' layers wait for the ones after them before they go to SQLite, all in one SQLite transaction. One
4468
+ * per journal entry (a message's send, each of its delivery steps, its read) rewrote the pages each touched: ~80 KB of
4469
+ * rows.db's WAL per message, and a checkpoint on the daemon's thread (30-150 ms) every ~50 messages. The journal holds
4470
+ * them meanwhile, and reads find them in their layers.
4471
+ */
4472
+ var APPLY_MS = 250;
4236
4473
  var BLOB = "__ghostlyBlob";
4237
4474
  var RANGE = "__ghostlyRange";
4238
4475
  var PROBE = "__ghostly_storage_probe__";
@@ -4270,6 +4507,13 @@ async function openPersistentIndexedDb(dir, options = {}) {
4270
4507
  let unsaved = false;
4271
4508
  /** Set while the journal holds relaxed entries no sync has covered yet. */
4272
4509
  let relaxed = null;
4510
+ /** The `complete`s of transactions whose entries the running sync does not cover: the next one does. */
4511
+ let unsynced = [];
4512
+ /** The syncs running off the thread, one after another, until no `complete` waits for one. */
4513
+ let syncing = null;
4514
+ /** The last journal entry whose layers wait for `applying` to take them to SQLite. */
4515
+ let applyUpTo = 0;
4516
+ let applying = null;
4273
4517
  /** The journal holds a chat's ratchet state, and with the next one it holds a key already used. */
4274
4518
  let holdsRatchet = false;
4275
4519
  /** The journal holds a delete of a chat or a message (or a settings write), and what came before it with it. */
@@ -4410,6 +4654,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4410
4654
  const dispatch = txProto.dispatchEvent;
4411
4655
  txProto.dispatchEvent = function(event) {
4412
4656
  if (event.type === "complete" && writing) {
4657
+ let entry = 0, waits = false;
4413
4658
  if (this.mode === "versionchange") {
4414
4659
  schemaDirty = true;
4415
4660
  if (trees) {
@@ -4418,13 +4663,22 @@ async function openPersistentIndexedDb(dir, options = {}) {
4418
4663
  }
4419
4664
  } else {
4420
4665
  const ops = pending.get(this._rollbackLog);
4421
- if (ops?.length) {
4422
- const refused = append(ops, this.durability === "relaxed", ++seq, this._rollbackLog);
4423
- if (refused) return refuse(this, refused.error, event);
4424
- }
4666
+ if (!ops?.length) trees?.abort(this._rollbackLog);
4667
+ if (ops?.length) entry = ++seq;
4668
+ const appended = ops?.length ? append(ops, this.durability === "relaxed", entry, this._rollbackLog) : null;
4669
+ if (appended && appended !== "unsynced") return refuse(this, appended.error, event);
4670
+ waits = appended === "unsynced";
4425
4671
  }
4426
4672
  pending.delete(this._rollbackLog);
4427
4673
  if (schemaDirty) enqueue(writeSnapshot);
4674
+ if (waits) {
4675
+ shareSync(() => {
4676
+ written(entry);
4677
+ dispatch.call(this, event);
4678
+ });
4679
+ this.db._rawDatabase.processTransactions(event);
4680
+ return true;
4681
+ }
4428
4682
  } else if (event.type === "abort") {
4429
4683
  pending.delete(this._rollbackLog);
4430
4684
  trees?.abort(this._rollbackLog);
@@ -4445,7 +4699,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4445
4699
  /**
4446
4700
  * A committed transaction's entry, numbered `entry` (its first op says so, where there are trees), from the
4447
4701
  * transaction whose rollback log is `owner`. Written at once, it may fail (a full disk): the reason is given back,
4448
- * and the transaction is to be refused, not completed.
4702
+ * and the transaction is to be refused, not completed. "unsynced": written, and its `complete` waits for a sync.
4449
4703
  */
4450
4704
  function append(ops, lazy, entry, owner) {
4451
4705
  holdsDelete ||= ops.some(deletes);
@@ -4458,11 +4712,12 @@ async function openPersistentIndexedDb(dir, options = {}) {
4458
4712
  if (queued === 0 && !ops.some((op) => op.kind === "put" && hasBlob(op.value))) {
4459
4713
  let done;
4460
4714
  try {
4461
- done = writeEntry(serialize(ops), lazy);
4715
+ done = writeEntry(serialize(ops), lazy ? "lazy" : "shared");
4462
4716
  } catch (error) {
4463
4717
  return { error };
4464
4718
  }
4465
4719
  trees?.complete(owner, entry);
4720
+ if (done && !lazy) return "unsynced";
4466
4721
  if (done) written(entry);
4467
4722
  return null;
4468
4723
  }
@@ -4471,7 +4726,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4471
4726
  if (writeEntry(serialize(await Promise.all(ops.map(async (op) => op.kind === "put" ? {
4472
4727
  ...op,
4473
4728
  value: await blobsToDisk(op.value)
4474
- } : op))), lazy)) written(entry);
4729
+ } : op))), lazy ? "lazy" : "now")) written(entry);
4475
4730
  });
4476
4731
  return null;
4477
4732
  }
@@ -4494,13 +4749,23 @@ async function openPersistentIndexedDb(dir, options = {}) {
4494
4749
  return transaction.dispatchEvent(abort);
4495
4750
  }
4496
4751
  /**
4497
- * The journal has entry `entry`: the trees' layers up to it go to SQLite. One that fails stays in memory, where reads
4498
- * find it, and goes with the next entry, or before the next snapshot (which fails without it).
4752
+ * The journal has entry `entry`: the trees' layers up to it go to SQLite within `APPLY_MS`, with the entries written
4753
+ * meanwhile. One that fails stays in memory, where reads find it, and goes with the next entry, or before the next
4754
+ * snapshot (which fails without it). A process that dies first replays them from the journal.
4499
4755
  */
4500
4756
  function written(entry) {
4757
+ if (!trees || !writing) return;
4758
+ applyUpTo = Math.max(applyUpTo, entry);
4759
+ applying ??= setTimeout(applyNow, APPLY_MS).unref();
4760
+ }
4761
+ function applyNow() {
4762
+ if (applying) {
4763
+ clearTimeout(applying);
4764
+ applying = null;
4765
+ }
4501
4766
  if (!trees || !writing) return;
4502
4767
  try {
4503
- trees.apply(entry);
4768
+ trees.apply(applyUpTo);
4504
4769
  } catch (error) {
4505
4770
  failed(error);
4506
4771
  }
@@ -4564,11 +4829,13 @@ async function openPersistentIndexedDb(dir, options = {}) {
4564
4829
  process.stderr.write(`ghostly: storage write failed: ${error instanceof Error ? error.message : String(error)}\n`);
4565
4830
  }
4566
4831
  /**
4567
- * `lazy`: a relaxed transaction's entry. A sync blocks the whole process for milliseconds, and what such a
4568
- * transaction wrote can be made again (the event set's note of a message it already reported): written, it outlives
4569
- * the process; a power cut may take its last second.
4832
+ * `sync`: "now", synced before this returns; "shared", synced by the caller through `shareSync`; "lazy", a relaxed
4833
+ * transaction's entry. A sync blocks the whole process for milliseconds, and what such a transaction wrote can be
4834
+ * made again (the event set's note of a message it already reported): written, it outlives the process; a power cut
4835
+ * may take its last second.
4570
4836
  */
4571
- function writeEntry(entry, lazy = false) {
4837
+ function writeEntry(entry, sync = "now") {
4838
+ const lazy = sync === "lazy";
4572
4839
  if (!writing) return false;
4573
4840
  let whole = -1;
4574
4841
  try {
@@ -4582,7 +4849,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4582
4849
  }])));
4583
4850
  marked = false;
4584
4851
  writeAll(journalFd, framed(entry));
4585
- if (!lazy) sync();
4852
+ if (sync === "now") syncNow();
4586
4853
  } catch (error) {
4587
4854
  unsaved = true;
4588
4855
  if (journalFd !== null && whole >= 0) try {
@@ -4592,7 +4859,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4592
4859
  }
4593
4860
  if (lazy) relaxed ??= setTimeout(() => {
4594
4861
  try {
4595
- sync();
4862
+ syncNow();
4596
4863
  } catch (error) {
4597
4864
  failed(error);
4598
4865
  }
@@ -4601,13 +4868,50 @@ async function openPersistentIndexedDb(dir, options = {}) {
4601
4868
  if (statSync(journalPath).size > COMPACT_BYTES || ratchetDue() || deleteDue()) enqueue(writeSnapshot);
4602
4869
  return true;
4603
4870
  }
4604
- function sync() {
4871
+ function syncNow() {
4605
4872
  if (relaxed) {
4606
4873
  clearTimeout(relaxed);
4607
4874
  relaxed = null;
4608
4875
  }
4609
4876
  if (journalFd !== null) fsyncSync(journalFd);
4610
4877
  }
4878
+ /**
4879
+ * `done` (a transaction's `complete`) once a sync covers what the journal has now. A sync blocked the whole process
4880
+ * (5 ms on a Mac's SSD, where it flushes the drive's cache), one per transaction: 8 sends at once went no faster than
4881
+ * 1, and every other call waited behind them. It runs off the thread now, and every entry written while it runs
4882
+ * goes with the next one. One that fails leaves what the journal has unsure: the store is folded into a whole
4883
+ * snapshot before the `complete`s go (they go even if that fails, as a Blob's entry written late does).
4884
+ */
4885
+ function shareSync(done) {
4886
+ unsynced.push(done);
4887
+ syncing ??= (async () => {
4888
+ while (unsynced.length) {
4889
+ const batch = unsynced;
4890
+ unsynced = [];
4891
+ if (relaxed) {
4892
+ clearTimeout(relaxed);
4893
+ relaxed = null;
4894
+ }
4895
+ const fd = journalFd;
4896
+ try {
4897
+ if (fd !== null) await new Promise((resolve, reject) => fsync(fd, (error) => error ? reject(error) : resolve()));
4898
+ } catch (error) {
4899
+ failed(error);
4900
+ const folded = [...batch, ...unsynced.splice(0)];
4901
+ enqueue(writeSnapshot).then(() => {
4902
+ for (const complete of folded) complete();
4903
+ });
4904
+ continue;
4905
+ }
4906
+ for (const complete of batch) complete();
4907
+ }
4908
+ syncing = null;
4909
+ })();
4910
+ }
4911
+ /** Resolves once no sync runs: a snapshot or a close may take the journal's place. */
4912
+ async function synced() {
4913
+ while (syncing) await syncing;
4914
+ }
4611
4915
  async function writeSnapshot() {
4612
4916
  const dirty = schemaDirty;
4613
4917
  schemaDirty = false;
@@ -4628,50 +4932,66 @@ async function openPersistentIndexedDb(dir, options = {}) {
4628
4932
  ...journalName !== void 0 && { journal: journalName },
4629
4933
  databases: []
4630
4934
  };
4631
- for (const db of raw._databases.values()) {
4632
- if (db.name === PROBE) continue;
4633
- const stores = [];
4634
- for (const store of db.rawObjectStores.values()) {
4635
- if (store.deleted) continue;
4636
- const live = [...store.rawIndexes.values()].filter((index) => !index.deleted);
4637
- const meta = {
4638
- name: store.name,
4639
- keyPath: store.keyPath,
4640
- autoIncrement: store.autoIncrement,
4641
- keyGenerator: store.keyGenerator?.num ?? null,
4642
- indexes: live.map(({ name, keyPath, multiEntry, unique }) => ({
4643
- name,
4644
- keyPath,
4645
- multiEntry,
4646
- unique
4647
- }))
4648
- };
4649
- if (!inline && disk(store)) {
4650
- if (!onTrees(store)) toDisk(store);
4651
- stores.push({
4935
+ const held = [];
4936
+ const later = [];
4937
+ let reader = null;
4938
+ try {
4939
+ for (const db of raw._databases.values()) {
4940
+ if (db.name === PROBE) continue;
4941
+ const stores = [];
4942
+ for (const store of db.rawObjectStores.values()) {
4943
+ 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);
4959
+ stores.push({
4960
+ ...meta,
4961
+ records: [],
4962
+ onDisk: true
4963
+ });
4964
+ snapshot.format = 3;
4965
+ continue;
4966
+ }
4967
+ const saved = {
4652
4968
  ...meta,
4653
- records: [],
4654
- onDisk: true
4655
- });
4656
- snapshot.format = 3;
4657
- continue;
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
+ }
4658
4979
  }
4659
- const records = [];
4660
- for (const record of store.records.values()) records.push([record.key, await blobsToDisk(record.value)]);
4661
- stores.push({
4662
- ...meta,
4663
- records
4980
+ snapshot.databases.push({
4981
+ name: db.name,
4982
+ version: db.version,
4983
+ stores
4664
4984
  });
4665
4985
  }
4666
- snapshot.databases.push({
4667
- name: db.name,
4668
- version: db.version,
4669
- stores
4670
- });
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));
4988
+ } finally {
4989
+ reader?.db.close();
4671
4990
  }
4672
4991
  return snapshot;
4673
4992
  }
4674
4993
  async function snapshotToDisk() {
4994
+ await synced();
4675
4995
  const name = randomBytes(8).toString("hex");
4676
4996
  const snapshot = await capture(name, false);
4677
4997
  if (trees) {
@@ -4709,6 +5029,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4709
5029
  }
4710
5030
  /** What `close` leaves: a short journal with the mark at its end, synced (its relaxed entries too), or a fresh snapshot. */
4711
5031
  async function leave() {
5032
+ await synced();
4712
5033
  if (schemaDirty || unsaved || journalBytes() > KEEP_BYTES || ratchetDue() || deleteDue()) return writeSnapshot();
4713
5034
  if (marked) return;
4714
5035
  writeEntry(serialize([{
@@ -4734,9 +5055,9 @@ async function openPersistentIndexedDb(dir, options = {}) {
4734
5055
  quiet = busy() ? 0 : quiet + 1;
4735
5056
  }
4736
5057
  }
4737
- /** Whether a transaction is running or waiting to. */
5058
+ /** Whether a transaction is running or waiting to, or for the sync that tells the engine it completed. */
4738
5059
  function busy() {
4739
- return [...raw._databases.values()].some((db) => db.transactions.some((transaction) => transaction._state !== "finished"));
5060
+ return syncing !== null || [...raw._databases.values()].some((db) => db.transactions.some((transaction) => transaction._state !== "finished"));
4740
5061
  }
4741
5062
  const installed = globalThis;
4742
5063
  installed.indexedDB = factory;
@@ -4758,6 +5079,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
4758
5079
  if (!writing) return;
4759
5080
  await settled();
4760
5081
  await enqueue(leave);
5082
+ applyNow();
4761
5083
  writing = false;
4762
5084
  if (journalFd !== null) {
4763
5085
  closeSync(journalFd);
@@ -4777,12 +5099,12 @@ async function openPersistentIndexedDb(dir, options = {}) {
4777
5099
  return [key, link];
4778
5100
  });
4779
5101
  }
4780
- const fd = openSync(path, "w", 384);
5102
+ const file = await open(path, "w", 384);
4781
5103
  try {
4782
- writeAll(fd, serialize(snapshot));
4783
- fsyncSync(fd);
5104
+ await file.writeFile(serialize(snapshot));
5105
+ await file.sync();
4784
5106
  } finally {
4785
- closeSync(fd);
5107
+ await file.close();
4786
5108
  }
4787
5109
  }
4788
5110
  };
@@ -5490,6 +5812,7 @@ function safe(read) {
5490
5812
  }
5491
5813
  //#endregion
5492
5814
  //#region src/runtime/engine.ts
5815
+ setPublicKeyFastPath(nativePublicKey);
5493
5816
  /**
5494
5817
  * The HyperDHT network: `GHOSTLY_HYPERDHT_BOOTSTRAP` ("host:port,…") replaces the public bootstrap nodes, as it does
5495
5818
  * for the Desktop's sidecar; a network all on loopback (the tests') is announced on loopback too.
@@ -5627,6 +5950,14 @@ function refuseDataAnswers(scope) {
5627
5950
  }
5628
5951
  };
5629
5952
  }
5953
+ /**
5954
+ * For tests: `GHOSTLY_TEST_RELAY_REQUESTS_PER_MINUTE=<n>` gives the relays' budget n requests a minute (`Infinity`: no
5955
+ * bound), so a test on loopback relays need not wait out the minute its pairing spent (`REQUESTS_PER_MINUTE`).
5956
+ */
5957
+ function relayBudgetForTests() {
5958
+ const perMinute = Number(process.env.GHOSTLY_TEST_RELAY_REQUESTS_PER_MINUTE);
5959
+ return perMinute > 0 ? { requestsPerMinute: perMinute } : {};
5960
+ }
5630
5961
  /** `GHOSTLY_STUN=0`: every connection the engine makes leaves the apps' public STUN servers out of its configuration. */
5631
5962
  function leaveOutPublicStun(scope) {
5632
5963
  const Base = scope.RTCPeerConnection;
@@ -5713,12 +6044,16 @@ async function startRuntime(paths, options = {}) {
5713
6044
  const [{ registerFileBytes }, { NodeFileBytes }] = await Promise.all([import("./fileBytes-BtIyoDF2.js"), import("./fileBytes-B0z9A6Le.js")]);
5714
6045
  registerFileBytes("native", async () => new NodeFileBytes(paths.files), true);
5715
6046
  if (await (await import("./blobFiles-BmsrvZc3.js")).moveBlobFiles()) await store.compact();
5716
- const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-rHKhnog1.js"), import("./endpoint-DWHkXWVM.js")]);
5717
- const mainline = dht.off ? null : new (await (import("./mainline-B0h8RchD.js"))).Mainline({
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({
5718
6049
  bootstrap: dht.bootstrap,
5719
6050
  host: dht.host
5720
6051
  });
5721
- const transport = mainline || pinned ? new (await (import("./mainline-B0h8RchD.js"))).RelaysAndDht(mainline, pinned ? { relays: pinned } : {}, !!pinned) : null;
6052
+ const relayOptions = {
6053
+ ...pinned ? { relays: pinned } : {},
6054
+ ...relayBudgetForTests()
6055
+ };
6056
+ const transport = mainline || pinned ? new (await (import("./mainline-Cu61qcPS.js"))).RelaysAndDht(mainline, relayOptions, !!pinned) : null;
5722
6057
  const apps = servedApps(paths.dir).length > 0;
5723
6058
  const server = new EngineServer({
5724
6059
  ...transport ? { transport } : {},
@@ -5737,9 +6072,11 @@ async function startRuntime(paths, options = {}) {
5737
6072
  localFetch: nodeLocalFetch,
5738
6073
  pushSend: (request) => nodePushSend(request),
5739
6074
  holdFetch: nodeHoldFetch(),
6075
+ webrtcIgnoresRelayOnly: true,
5740
6076
  callsSupport: callsUnavailable === null,
5741
6077
  ...callsUnavailable ? { callsUnavailable } : {},
5742
6078
  ...options.autoJoins ? { autoJoins: options.autoJoins } : {},
6079
+ onlineNotices: options.onlineNotices ?? (() => false),
5743
6080
  ...options.deferGroups ? { deferGroups: true } : {},
5744
6081
  ...options.deferChats ? { deferChats: true } : {},
5745
6082
  ...options.offline ? { offline: true } : {},
@@ -5803,11 +6140,14 @@ async function openHost(paths, mode, version, options = {}) {
5803
6140
  let runtime;
5804
6141
  let groupAuto;
5805
6142
  const autoJoins = (chat) => !!groupAuto?.autoJoins(chat);
6143
+ let presence;
6144
+ const onlineNotices = () => !!presence?.saysOnline();
5806
6145
  try {
5807
6146
  runtime = await startRuntime(paths, {
5808
6147
  ...options,
5809
6148
  ...mode === "daemon" ? { logJoins: true } : {},
5810
6149
  autoJoins,
6150
+ onlineNotices,
5811
6151
  keepNewest: 100
5812
6152
  });
5813
6153
  } catch (error) {
@@ -5836,7 +6176,7 @@ async function openHost(paths, mode, version, options = {}) {
5836
6176
  }
5837
6177
  hub.baseline(now, histories, {
5838
6178
  newest,
5839
- whole
6179
+ files: (id) => node.fileMessages(id)
5840
6180
  });
5841
6181
  await hub.ended((id) => fileStore.get(id));
5842
6182
  announceJoins(hub, node);
@@ -5861,6 +6201,12 @@ async function openHost(paths, mode, version, options = {}) {
5861
6201
  ids: [...histories.keys(), ...newest.keys()]
5862
6202
  });
5863
6203
  joiner.check(node.getState());
6204
+ if (mode === "daemon") {
6205
+ const said = new DaemonPresence(node);
6206
+ presence = said;
6207
+ hub.onState((s) => said.check(s));
6208
+ said.check(node.getState());
6209
+ }
5864
6210
  } catch (error) {
5865
6211
  hub.release();
5866
6212
  await runtime.close().catch(() => {});
@@ -5881,6 +6227,11 @@ async function openHost(paths, mode, version, options = {}) {
5881
6227
  ctx,
5882
6228
  close: () => closing ??= (async () => {
5883
6229
  await Promise.all([calls.stopAll(), flushCardUpdates(ctx)]).catch(() => {});
6230
+ hub.sink.post({
6231
+ kind: "state",
6232
+ state: runtime.server.node.getState()
6233
+ });
6234
+ presence?.stop();
5884
6235
  runtime.server.detach(hub.sink);
5885
6236
  hub.release();
5886
6237
  await runtime.close().catch(() => {});
@@ -6125,4 +6476,4 @@ function connection(ctx, socket, secret, answering) {
6125
6476
  //#endregion
6126
6477
  export { BASELINE_NEWEST, MAX_UNREAD, STOP_ANSWERS_MS, keepServing, openHost, serve };
6127
6478
 
6128
- //# sourceMappingURL=host-CGlHkiQv.js.map
6479
+ //# sourceMappingURL=host-CYlIiY3r.js.map