@ghostlytools/cli 1.10.2 → 1.10.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 (84) hide show
  1. package/README.md +4 -2
  2. package/dist/assets/{api-DcE4KylM.js → api-BdaxsDvA.js} +2 -2
  3. package/dist/assets/{api-EUGHGnRz.js → api-C4MLg5Rs.js} +42 -17
  4. package/dist/assets/api-C4MLg5Rs.js.map +1 -0
  5. package/dist/assets/{apiKit-CJVp_crp.js → apiKit-BbNaxZGZ.js} +27 -11
  6. package/dist/assets/apiKit-BbNaxZGZ.js.map +1 -0
  7. package/dist/assets/{apps-C54T_MOT.js → apps-BluQgEhv.js} +3 -3
  8. package/dist/assets/{apps-C54T_MOT.js.map → apps-BluQgEhv.js.map} +1 -1
  9. package/dist/assets/{apps-C-KbyLCa.js → apps-BtGFth7o.js} +1 -1
  10. package/dist/assets/{arkade-BmxJTa65.js → arkade-DdZx4b_I.js} +2 -2
  11. package/dist/assets/{arkade-BmxJTa65.js.map → arkade-DdZx4b_I.js.map} +1 -1
  12. package/dist/assets/{backup-BxSH5BZG.js → backup-DyDsW7sC.js} +6 -6
  13. package/dist/assets/{backup-BxSH5BZG.js.map → backup-DyDsW7sC.js.map} +1 -1
  14. package/dist/assets/{backup-C0Plp-DF.js → backup-I1n7ccz7.js} +1 -1
  15. package/dist/assets/{buttons-B-ZJYdo0.js → buttons-Do7dCku6.js} +4 -4
  16. package/dist/assets/{buttons-B-ZJYdo0.js.map → buttons-Do7dCku6.js.map} +1 -1
  17. package/dist/assets/buttons-DpjwXePb.js +2 -0
  18. package/dist/assets/{client-CDa87uYD.js → client-CWsjRiH8.js} +2 -2
  19. package/dist/assets/{client-CDa87uYD.js.map → client-CWsjRiH8.js.map} +1 -1
  20. package/dist/assets/{codec-D45W1L8n.js → codec-DPaXhuYc.js} +2 -2
  21. package/dist/assets/{codec-D45W1L8n.js.map → codec-DPaXhuYc.js.map} +1 -1
  22. package/dist/assets/{envelope-C3yFCQa-.js → envelope-5Gu9yGk_.js} +2 -2
  23. package/dist/assets/{envelope-C3yFCQa-.js.map → envelope-5Gu9yGk_.js.map} +1 -1
  24. package/dist/assets/{fixedHosts-C6nCHjOa.js → fixedHosts-PR1BcfKX.js} +7 -8
  25. package/dist/assets/fixedHosts-PR1BcfKX.js.map +1 -0
  26. package/dist/assets/{groupFiles-DYopNE57.js → groupFiles-BuToTMUk.js} +213 -90
  27. package/dist/assets/groupFiles-BuToTMUk.js.map +1 -0
  28. package/dist/assets/{host-G5seUwaI.js → host-fkQYBvKH.js} +226 -29
  29. package/dist/assets/host-fkQYBvKH.js.map +1 -0
  30. package/dist/assets/{hyperdhtRelay-B_uHjg_r.js → hyperdhtRelay-DDCak5PY.js} +2 -2
  31. package/dist/assets/{hyperdhtRelay-B_uHjg_r.js.map → hyperdhtRelay-DDCak5PY.js.map} +1 -1
  32. package/dist/assets/{irohWeb-C-2oslSU.js → irohWeb-B4_swKx0.js} +2 -2
  33. package/dist/assets/{irohWeb-C-2oslSU.js.map → irohWeb-B4_swKx0.js.map} +1 -1
  34. package/dist/assets/{main-Dk--nD7X.js → main-ClYsttvY.js} +49 -30
  35. package/dist/assets/main-ClYsttvY.js.map +1 -0
  36. package/dist/assets/{mainline-HPlVrl8R.js → mainline-Hsk2SZ1B.js} +63 -3
  37. package/dist/assets/mainline-Hsk2SZ1B.js.map +1 -0
  38. package/dist/assets/{networkReason-CfpO9ayz.js → networkReason-C9Hr-hO7.js} +4 -4
  39. package/dist/assets/{networkReason-CfpO9ayz.js.map → networkReason-C9Hr-hO7.js.map} +1 -1
  40. package/dist/assets/{nwc-BY7BPttn.js → nwc-BbK6qNxS.js} +3 -3
  41. package/dist/assets/{nwc-BY7BPttn.js.map → nwc-BbK6qNxS.js.map} +1 -1
  42. package/dist/assets/{nwcProvider-CfpbNUf7.js → nwcProvider-D_tt2X3Z.js} +2 -2
  43. package/dist/assets/{nwcProvider-CfpbNUf7.js.map → nwcProvider-D_tt2X3Z.js.map} +1 -1
  44. package/dist/assets/{ownDid-DJFZM2cV.js → ownDid-DkK_jem4.js} +2 -2
  45. package/dist/assets/{ownDid-DJFZM2cV.js.map → ownDid-DkK_jem4.js.map} +1 -1
  46. package/dist/assets/{profiles-DKmZGXp6.js → profiles-aV9vaK0B.js} +7 -5
  47. package/dist/assets/profiles-aV9vaK0B.js.map +1 -0
  48. package/dist/assets/{registry-CAzrtV0_.js → registry-C9T6tYCO.js} +1 -1
  49. package/dist/assets/{registry-BUm4cAzV.js → registry-CEjnubep.js} +8 -7
  50. package/dist/assets/registry-CEjnubep.js.map +1 -0
  51. package/dist/assets/{resolve-CDD4X68j.js → resolve-BBOrN13E.js} +24 -5
  52. package/dist/assets/resolve-BBOrN13E.js.map +1 -0
  53. package/dist/assets/{server-DcOsgYDU.js → server-BJ3T-kx3.js} +247 -107
  54. package/dist/assets/server-BJ3T-kx3.js.map +1 -0
  55. package/dist/assets/{src-D31D1_oY.js → src-BIdfHB7t.js} +254 -71
  56. package/dist/assets/src-BIdfHB7t.js.map +1 -0
  57. package/dist/assets/{sshForges-DerRwMf3.js → sshForges-CRg-NGld.js} +2 -2
  58. package/dist/assets/{sshForges-DerRwMf3.js.map → sshForges-CRg-NGld.js.map} +1 -1
  59. package/dist/assets/{translate-C-sj0VJe.js → translate-b0X61WMI.js} +2 -2
  60. package/dist/assets/{translate-C-sj0VJe.js.map → translate-b0X61WMI.js.map} +1 -1
  61. package/dist/assets/{usdt-DwHqp625.js → usdt-6CQdfye1.js} +7 -4
  62. package/dist/assets/usdt-6CQdfye1.js.map +1 -0
  63. package/dist/assets/{waitNotice-D52RWbV4.js → waitNotice-Ce3YCinu.js} +27 -5
  64. package/dist/assets/waitNotice-Ce3YCinu.js.map +1 -0
  65. package/dist/assets/{words-Db9WgatT.js → words-Dk6o6wz4.js} +3 -3
  66. package/dist/assets/words-Dk6o6wz4.js.map +1 -0
  67. package/dist/ghostly.mjs +1 -1
  68. package/package.json +1 -1
  69. package/dist/assets/api-EUGHGnRz.js.map +0 -1
  70. package/dist/assets/apiKit-CJVp_crp.js.map +0 -1
  71. package/dist/assets/buttons-DA3wZfG0.js +0 -2
  72. package/dist/assets/fixedHosts-C6nCHjOa.js.map +0 -1
  73. package/dist/assets/groupFiles-DYopNE57.js.map +0 -1
  74. package/dist/assets/host-G5seUwaI.js.map +0 -1
  75. package/dist/assets/main-Dk--nD7X.js.map +0 -1
  76. package/dist/assets/mainline-HPlVrl8R.js.map +0 -1
  77. package/dist/assets/profiles-DKmZGXp6.js.map +0 -1
  78. package/dist/assets/registry-BUm4cAzV.js.map +0 -1
  79. package/dist/assets/resolve-CDD4X68j.js.map +0 -1
  80. package/dist/assets/server-DcOsgYDU.js.map +0 -1
  81. package/dist/assets/src-D31D1_oY.js.map +0 -1
  82. package/dist/assets/usdt-DwHqp625.js.map +0 -1
  83. package/dist/assets/waitNotice-D52RWbV4.js.map +0 -1
  84. package/dist/assets/words-Db9WgatT.js.map +0 -1
@@ -1,10 +1,10 @@
1
- import { Au as setLinkTraceSink, Qu as fromBase64Url, _s as parseCallSignal, cs as CALL_SIGNAL_MAX_AGE_MS, ds as answersOffer, fi as checkPushEndpoint, fs as buildSdpFromSignal, gs as extractParamsFromSdp, ms as callSignalHeardAt, ps as callRtcConfig, qo as DAEMON_WATCH_PEER_IDLE_MS, ua as isPublicAddress, us as RTC_CONFIG, vs as signalHasVideo } from "./src-D31D1_oY.js";
2
- import { o as fedimintSdkOn, r as irohRelayProblem } from "./irohWeb-C-2oslSU.js";
1
+ import { Nu as setLinkTraceSink, Xo as DAEMON_WATCH_PEER_IDLE_MS, _s as callSignalHeardAt, ba as ACTIVE_DHT_POLL_MS, bs as parseCallSignal, ds as CALL_SIGNAL_MAX_AGE_MS, fa as isPublicAddress, fi as checkPushEndpoint, gs as callRtcConfig, hs as buildSdpFromSignal, ms as answersOffer, nd as fromBase64Url, ps as RTC_CONFIG, xs as signalHasVideo, ys as extractParamsFromSdp } from "./src-BIdfHB7t.js";
2
+ import { o as fedimintSdkOn, r as irohRelayProblem } from "./irohWeb-B4_swKx0.js";
3
3
  import { r as asCliError, t as CliError } from "./errors-Dt-ZnyOX.js";
4
- import { s as findChat, u as findOwnNamedChat } from "./apiKit-CJVp_crp.js";
5
- import { _ as flushCardUpdates, c as paymentJson, d as nodeLocalFetch, f as chatJson, g as resumeHolds, h as groupMessageJson, l as reportAppFrames, m as groupJson, p as chatMessageJson, r as callApi, u as servedApps } from "./api-EUGHGnRz.js";
6
- import { S as runtimePath, g as startingPath, n as RESTORED_MARK, r as acquireLock, v as ownFolder, x as privateFolder, y as ownProfile } from "./profiles-DKmZGXp6.js";
7
- import { c as proof, d as DEFAULT_RATE, f as PlaybackQueue, g as isCallRate, h as frameSamples, l as removeDaemonSecret, m as frameBytes, o as greeting, p as bytesToMs, s as newDaemonSecret } from "./client-CDa87uYD.js";
4
+ import { s as findChat, u as findOwnNamedChat } from "./apiKit-BbNaxZGZ.js";
5
+ import { _ as flushCardUpdates, c as paymentJson, d as nodeLocalFetch, f as chatJson, g as resumeHolds, h as groupMessageJson, l as reportAppFrames, m as groupJson, p as chatMessageJson, r as callApi, u as servedApps } from "./api-C4MLg5Rs.js";
6
+ import { C as runtimePath, S as privateFolder, _ as startingPath, b as ownProfile, n as RESTORED_MARK, r as acquireLock, y as ownFolder } from "./profiles-aV9vaK0B.js";
7
+ import { c as proof, d as DEFAULT_RATE, f as PlaybackQueue, g as isCallRate, h as frameSamples, l as removeDaemonSecret, m as frameBytes, o as greeting, p as bytesToMs, s as newDaemonSecret } from "./client-CWsjRiH8.js";
8
8
  import { createRequire } from "node:module";
9
9
  import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, fsyncSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, statSync, truncateSync, writeFileSync, writeSync } from "node:fs";
10
10
  import { readFile } from "node:fs/promises";
@@ -984,6 +984,11 @@ var CallManager = class {
984
984
  missedAt = /* @__PURE__ */ new Map();
985
985
  /** Per chat: the contact's last call held back (`QUICK_REDIAL_MS`) until its hang-up, and when it came. */
986
986
  held = /* @__PURE__ */ new Map();
987
+ /**
988
+ * The chats whose contact's calls are being held back (`QUICK_REDIAL_MS`) since the last that rang: the log says so for
989
+ * the first only. A detached daemon's log only grows, and a contact's loop would set its pace.
990
+ */
991
+ bursts = /* @__PURE__ */ new Set();
987
992
  /** The early answers being made, those of ended calls too (MAX_EARLY_ANSWERS). */
988
993
  makings = /* @__PURE__ */ new Set();
989
994
  auto;
@@ -1019,7 +1024,8 @@ var CallManager = class {
1019
1024
  if (signal.t === "o" && !call) {
1020
1025
  this.lastSignal.set(chat, signal.ts);
1021
1026
  if (this.quickRedial(chat)) {
1022
- process.stderr.write(`ghostly: chat ${chat}: a call came right after a missed one that barely rang: kept as missed\n`);
1027
+ if (!this.bursts.has(chat)) process.stderr.write(`ghostly: chat ${chat}: a call came right after a missed one that barely rang: kept as missed, and the next ones that come as fast\n`);
1028
+ this.bursts.add(chat);
1023
1029
  this.missedAt.set(chat, this.now);
1024
1030
  this.held.set(chat, {
1025
1031
  offer: signal,
@@ -1036,7 +1042,10 @@ var CallManager = class {
1036
1042
  } else if (signal.t === "o" && call?.direction === "in" && call.state === "connecting" && call.media && signal.ts > (call.offer?.ts ?? 0)) {
1037
1043
  this.lastSignal.set(chat, signal.ts);
1038
1044
  if (call.reanswers < this.maxRedials) this.reanswer(call, signal);
1039
- else process.stderr.write(`ghostly: call ${call.id}: the contact offered again past the last start-over: ignored\n`);
1045
+ else if (!call.ignoring) {
1046
+ call.ignoring = true;
1047
+ process.stderr.write(`ghostly: call ${call.id}: the contact offered again past the last start-over: ignored, and its next offers on this call\n`);
1048
+ }
1040
1049
  } else if (signal.t === "v") this.lastSignal.set(chat, signal.ts);
1041
1050
  else if (signal.t === "h") {
1042
1051
  this.lastSignal.set(chat, signal.ts);
@@ -1060,6 +1069,7 @@ var CallManager = class {
1060
1069
  }
1061
1070
  incoming(chat, offer) {
1062
1071
  this.held.delete(chat);
1072
+ this.bursts.delete(chat);
1063
1073
  const link = this.link(chat);
1064
1074
  const call = this.add(chat, "in", this.auto.rate);
1065
1075
  call.offer = offer;
@@ -1387,6 +1397,7 @@ var CallManager = class {
1387
1397
  offering: false,
1388
1398
  redials: 0,
1389
1399
  reanswers: 0,
1400
+ ignoring: false,
1390
1401
  attempt: 0,
1391
1402
  answered: 0,
1392
1403
  answeredAt: 0,
@@ -1712,8 +1723,9 @@ var FORGET_SEARCHED_IDS = 16;
1712
1723
  var SEEN_DB = "ghostly-cli";
1713
1724
  var SEEN = "seen";
1714
1725
  /**
1715
- * How long the seen set keeps the note of a settled message after its last activity (its arrival, its edit, a reaction):
1716
- * older notes are dropped, and a message that came here longer ago with no note is no news. A daemon kept a note per
1726
+ * How long the seen set keeps the note of a settled message after its last activity (its arrival, its edit, a reaction),
1727
+ * each as this device's clock has it: older notes are dropped, and a message that came here longer ago with no note is
1728
+ * no news. A daemon kept a note per
1717
1729
  * message of its whole history in memory, for as long as it ran.
1718
1730
  */
1719
1731
  var SEEN_KEEP_MS = 2592e6;
@@ -1745,10 +1757,27 @@ var EventHub = class {
1745
1757
  marked = false;
1746
1758
  /** Whether `dropped` is ahead of `<journal>.dropped` (a full disk refused it): the next event's write tries it again. */
1747
1759
  markOwed = false;
1760
+ /**
1761
+ * The highest seq `<journal>.reserved` holds: no event is told a seq past it before the mark says so, and a start
1762
+ * numbers on from it. A full disk can take an event and its `.dropped` mark both, after it was told; the next start
1763
+ * then never gives its seq again, and a replay across it says `events.gap`. A stop gives back what it did not use.
1764
+ */
1765
+ reserved = 0;
1748
1766
  /** Typing changes told this run: a change's id carries it, as it takes no seq of its own (`emitLive`). */
1749
1767
  typingChanges = 0;
1750
1768
  journalLines = 0;
1751
1769
  journalBytes = 0;
1770
+ /**
1771
+ * Where the journal first names each chat, group and message (`namedIn`), by the hash of the id as written
1772
+ * (`hashOfId`; two ids that share one only read more), so `forget` reads it from there on: each deletion read the
1773
+ * whole journal (up to 32 MiB) from disk and searched it, a contact's delete for everyone included. Built from the
1774
+ * journal at the first `forget`, kept by `emit`; null again once lines move (a trim, a rewrite).
1775
+ */
1776
+ firstNamed = null;
1777
+ /** The journal's size `firstNamed` covers: another size (a write it did not see) builds it again. */
1778
+ namedSize = 0;
1779
+ /** Where the first line that cannot be read is, which `forget` reads from too: it may name what is removed. */
1780
+ unreadableAt = Infinity;
1752
1781
  listeners = /* @__PURE__ */ new Set();
1753
1782
  stateListeners = /* @__PURE__ */ new Set();
1754
1783
  callListeners = /* @__PURE__ */ new Set();
@@ -1785,6 +1814,7 @@ var EventHub = class {
1785
1814
  }
1786
1815
  /** Loads the journal's last seq and the seen set; call before `sink` is attached. */
1787
1816
  async open() {
1817
+ const reserved = readDropped(this.journalPath + ".reserved") ?? 0;
1788
1818
  try {
1789
1819
  const dropped = readDropped(this.journalPath + ".dropped");
1790
1820
  if (existsSync(this.journalPath)) {
@@ -1821,6 +1851,11 @@ var EventHub = class {
1821
1851
  this.journalBytes = 0;
1822
1852
  this.markDropped(this.seq);
1823
1853
  }
1854
+ this.reserved = reserved;
1855
+ if (reserved > this.seq) {
1856
+ this.seq = reserved;
1857
+ this.markDropped(Math.max(this.dropped, reserved));
1858
+ }
1824
1859
  this.db = await new Promise((resolve, reject) => {
1825
1860
  const request = indexedDB.open(this.seenDb, 1);
1826
1861
  request.onupgradeneeded = () => {
@@ -1878,10 +1913,18 @@ var EventHub = class {
1878
1913
  }
1879
1914
  /**
1880
1915
  * Drops the notes of settled messages, and of reactions, whose last activity is older than `SEEN_KEEP_MS`: from memory
1881
- * and from the store. A message of mine still on its way (or failed, which a retry sends again) keeps its note.
1916
+ * and from the store. A message of mine still on its way (or failed, which a retry sends again) keeps its note. The
1917
+ * 30 days count back from the newest activity the set knows when the clock reads later than it: a clock run ahead (a
1918
+ * wrong RTC, a VM restored with a skewed clock) would otherwise drop the notes of the recent history, which the next
1919
+ * start with the clock right reports again.
1882
1920
  */
1883
1921
  sweep() {
1884
- const cutoff = this.now() - SEEN_KEEP_MS;
1922
+ let newest = 0;
1923
+ for (const [chat, known] of this.seen) {
1924
+ if (chat === FORMAT || chat.startsWith("said:")) continue;
1925
+ for (const note of known.values()) newest = Math.max(newest, timeIn(note) ?? 0);
1926
+ }
1927
+ const cutoff = Math.min(this.now(), newest) - SEEN_KEEP_MS;
1885
1928
  let dropped = 0;
1886
1929
  for (const [chat, known] of this.seen) {
1887
1930
  if (chat === FORMAT || chat.startsWith("said:")) continue;
@@ -1912,7 +1955,7 @@ var EventHub = class {
1912
1955
  const cutoff = this.now() - SEEN_KEEP_MS;
1913
1956
  return messages.every((message) => {
1914
1957
  const note = known?.get(message.id), before = note === void 0 ? void 0 : stateIn(note);
1915
- if (note === void 0 && message.timestamp < cutoff && activityOf(message) < cutoff) return true;
1958
+ if (note === void 0 && cameAt(message) < cutoff && activityOf(message) < cutoff) return true;
1916
1959
  let state = stateOf(message);
1917
1960
  if (before !== void 0 && mentionedIn(before) && !mentionedIn(state)) state = withFlag(state, MENTIONED);
1918
1961
  if (before !== state) return false;
@@ -1986,7 +2029,40 @@ var EventHub = class {
1986
2029
  this.marked = true;
1987
2030
  this.markOwed = false;
1988
2031
  } catch (error) {
1989
- this.markOwed = true;
2032
+ try {
2033
+ writeMark(this.journalPath + ".dropped", seq, false);
2034
+ this.marked = true;
2035
+ this.markOwed = false;
2036
+ } catch {
2037
+ this.markOwed = true;
2038
+ process.stderr.write(`ghostly: event journal error: ${error instanceof Error ? error.message : String(error)}\n`);
2039
+ }
2040
+ }
2041
+ }
2042
+ /**
2043
+ * Reserves seqs ahead of the one about to be told, in `<journal>.reserved`, rewritten where it is (no new block, so a
2044
+ * full disk takes it): written before the event goes anywhere, as `markDropped` is. Refused, the event still goes
2045
+ * (the reservation made before covers the next ones), and the next event tries again.
2046
+ */
2047
+ reserve() {
2048
+ if (this.reserved - this.seq >= 128) return;
2049
+ try {
2050
+ writeMark(this.journalPath + ".reserved", this.seq + 256, true);
2051
+ this.reserved = this.seq + 256;
2052
+ } catch (error) {
2053
+ process.stderr.write(`ghostly: event journal error: ${error instanceof Error ? error.message : String(error)}\n`);
2054
+ }
2055
+ }
2056
+ /**
2057
+ * At a stop: the seqs reserved past the last one told are given back, so the next start numbers on from it, with no
2058
+ * jump. Not written (a disk that takes no rewrite either), the next start numbers past the reservation instead.
2059
+ */
2060
+ release() {
2061
+ if (this.reserved <= this.seq) return;
2062
+ try {
2063
+ writeMark(this.journalPath + ".reserved", this.seq, true);
2064
+ this.reserved = this.seq;
2065
+ } catch (error) {
1990
2066
  process.stderr.write(`ghostly: event journal error: ${error instanceof Error ? error.message : String(error)}\n`);
1991
2067
  }
1992
2068
  }
@@ -2002,12 +2078,17 @@ var EventHub = class {
2002
2078
  at: this.now(),
2003
2079
  ...fields
2004
2080
  };
2081
+ this.reserve();
2005
2082
  let written = false;
2006
2083
  try {
2007
2084
  if (this.markOwed) this.markDropped(this.dropped);
2008
2085
  const line = Buffer.from(JSON.stringify(event) + "\n");
2009
- appendWhole(this.journalPath, line);
2086
+ const at = appendWhole(this.journalPath, line);
2010
2087
  written = true;
2088
+ if (this.firstNamed && at === this.namedSize) {
2089
+ for (const id of namedIn(event)) if (!this.firstNamed.has(hashOfId(id))) this.firstNamed.set(hashOfId(id), at);
2090
+ this.namedSize = at + line.length;
2091
+ } else this.firstNamed = null;
2011
2092
  this.journalBytes += line.length;
2012
2093
  if (++this.journalLines > 2e4 || this.journalBytes > 33554432) this.trimJournal();
2013
2094
  } catch (error) {
@@ -2047,6 +2128,7 @@ var EventHub = class {
2047
2128
  const kept = lines.slice(start);
2048
2129
  const first = firstSeq(kept);
2049
2130
  this.markDropped(Math.max(this.dropped, first === void 0 ? this.seq : first - 1));
2131
+ this.firstNamed = null;
2050
2132
  writeFileSync(this.journalPath + ".tmp", kept.length ? kept.join("\n") + "\n" : "", { mode: 384 });
2051
2133
  renameSync(this.journalPath + ".tmp", this.journalPath);
2052
2134
  this.journalLines = kept.length;
@@ -2064,7 +2146,9 @@ var EventHub = class {
2064
2146
  try {
2065
2147
  rmSync(this.journalPath + ".unreadable", { force: true });
2066
2148
  if (!existsSync(this.journalPath)) return;
2067
- const { bytes, start, at } = journalWindow(this.journalPath);
2149
+ const named = this.namedFrom(ids);
2150
+ if (named === void 0) return;
2151
+ let { bytes, start, at } = journalWindow(this.journalPath, named);
2068
2152
  const needles = ids.map((id) => JSON.stringify(id)), few = ids.length <= FORGET_SEARCHED_IDS;
2069
2153
  const names = (line) => needles.some((needle) => line.includes(needle));
2070
2154
  const edits = [];
@@ -2111,6 +2195,15 @@ var EventHub = class {
2111
2195
  }
2112
2196
  return;
2113
2197
  }
2198
+ this.firstNamed = null;
2199
+ if (start === 0 && at > 0) {
2200
+ const window = journalWindow(this.journalPath);
2201
+ for (const edit of edits) {
2202
+ edit.from += at - window.at;
2203
+ edit.to += at - window.at;
2204
+ }
2205
+ ({bytes, start, at} = window);
2206
+ }
2114
2207
  const parts = [];
2115
2208
  let from = start, removed = 0;
2116
2209
  for (const edit of edits) {
@@ -2126,9 +2219,42 @@ var EventHub = class {
2126
2219
  this.journalLines -= removed;
2127
2220
  this.journalBytes = written.length;
2128
2221
  } catch (error) {
2222
+ this.firstNamed = null;
2129
2223
  process.stderr.write(`ghostly: event journal error: ${error instanceof Error ? error.message : String(error)}\n`);
2130
2224
  }
2131
2225
  }
2226
+ /**
2227
+ * The first place in the journal a line may name one of these ids (`namedIn`), or undefined when none does. The
2228
+ * first call reads the journal for it once.
2229
+ */
2230
+ namedFrom(ids) {
2231
+ if (!this.firstNamed || statSync(this.journalPath).size !== this.namedSize) {
2232
+ const { bytes, start, at, size } = journalWindow(this.journalPath);
2233
+ const firstNamed = /* @__PURE__ */ new Map();
2234
+ for (const key of NAMING_KEYS) for (let found = bytes.indexOf(key, start); found !== -1; found = bytes.indexOf(key, found + key.length)) {
2235
+ const open = found + key.length;
2236
+ if (bytes[open] !== 34) continue;
2237
+ let close = open + 1;
2238
+ while (close < bytes.length && bytes[close] !== 34 && bytes[close] !== 10) close += bytes[close] === 92 ? 2 : 1;
2239
+ if (bytes[close] !== 34) continue;
2240
+ const hash = hashOf(bytes, open, close + 1), line = at + Math.max(start, bytes.lastIndexOf(10, found) + 1);
2241
+ if ((firstNamed.get(hash) ?? Infinity) > line) firstNamed.set(hash, line);
2242
+ }
2243
+ this.unreadableAt = Infinity;
2244
+ for (const [from, to] of linesOf(bytes, start)) {
2245
+ if (bytes[from] === 123 && bytes[to - 1] === 125) continue;
2246
+ if (bytes.subarray(from, to).some((byte) => byte !== 32)) {
2247
+ this.unreadableAt = at + from;
2248
+ break;
2249
+ }
2250
+ }
2251
+ this.firstNamed = firstNamed;
2252
+ this.namedSize = size;
2253
+ }
2254
+ let from = this.unreadableAt;
2255
+ for (const id of ids) from = Math.min(from, this.firstNamed.get(hashOfId(id)) ?? Infinity);
2256
+ return from === Infinity ? void 0 : from;
2257
+ }
2132
2258
  handle(message) {
2133
2259
  switch (message.kind) {
2134
2260
  case "state":
@@ -2429,13 +2555,13 @@ var EventHub = class {
2429
2555
  const note = known.get(message.id), before = note === void 0 ? void 0 : stateIn(note);
2430
2556
  const at = activityOf(message);
2431
2557
  const json = () => group ? groupMessageJson(message, this.state?.groups.find((g) => g.id === group)) : chatMessageJson(message, this.state?.links.find((l) => l.id === chat));
2432
- if (before === void 0 && message.timestamp < cutoff) {
2558
+ if (before === void 0 && cameAt(message) < cutoff) {
2433
2559
  if (at < cutoff) continue;
2434
2560
  const noted = noteOf(state, at);
2435
2561
  known.set(message.id, noted);
2436
2562
  changes.push([message.id, noted]);
2437
2563
  const edit = message.edit;
2438
- if (quiet || !edit || edit.at < cutoff || message.sender === "me" && edit.restore) continue;
2564
+ if (quiet || !edit || editedAt(edit) < cutoff || message.sender === "me" && edit.restore) continue;
2439
2565
  if (group) this.emit("group.message.edited", `group.message.edited:${group}:${message.id}:${edit.seq}`, {
2440
2566
  group,
2441
2567
  messageId: message.id,
@@ -2649,9 +2775,20 @@ function timeIn(note) {
2649
2775
  const at = note.lastIndexOf("@");
2650
2776
  return at === -1 ? void 0 : parseInt(note.slice(at + 1), 36);
2651
2777
  }
2778
+ /**
2779
+ * When a message came here: its place (a received row's is when it was stored here), or for a group's line, whose place
2780
+ * is its commit's moment, when this device stored it (`heardAt`; absent on lines stored before it).
2781
+ */
2782
+ function cameAt(message) {
2783
+ return Math.max(message.timestamp, message.heardAt ?? 0);
2784
+ }
2785
+ /** When an edit came here: a contact's or member's is dated by their clock (`at`), and taken here at `heardAt`. */
2786
+ function editedAt(edit) {
2787
+ return Math.max(edit.at, edit.heardAt ?? 0);
2788
+ }
2652
2789
  /** The last time a message changed here: it came, it was edited, someone reacted. */
2653
2790
  function activityOf(message) {
2654
- let at = Math.max(message.timestamp, message.edit?.at ?? 0);
2791
+ let at = Math.max(cameAt(message), message.edit ? editedAt(message.edit) : 0);
2655
2792
  for (const r of Object.values(message.reactions ?? {})) at = Math.max(at, r.at ?? 0);
2656
2793
  return at;
2657
2794
  }
@@ -2779,7 +2916,10 @@ function firstSeq(lines) {
2779
2916
  if (typeof seq === "number") return seq;
2780
2917
  } catch {}
2781
2918
  }
2782
- /** The journal's dropped mark (`EventHub.dropped`), or undefined when there is none (a journal from before it). */
2919
+ /**
2920
+ * The journal's dropped mark (`EventHub.dropped`) or its reserved one (`EventHub.reserved`), or undefined when there is
2921
+ * none (a journal from before it).
2922
+ */
2783
2923
  function readDropped(path) {
2784
2924
  try {
2785
2925
  const seq = Number(readFileSync(path, "utf8").trim());
@@ -2789,8 +2929,30 @@ function readDropped(path) {
2789
2929
  }
2790
2930
  }
2791
2931
  /**
2932
+ * Writes a mark over the one there, as a line of fixed width (`000…0042`, which `readDropped` reads, v1.10's too): a
2933
+ * rewrite never grows the file past its first block, so a full disk takes it, and one cut short leaves the new mark's
2934
+ * first digits before the old one's last, never a number below the old one when the mark grows. `create`: a file that
2935
+ * is not there is made (a first start).
2936
+ */
2937
+ function writeMark(path, seq, create) {
2938
+ const line = Buffer.from(String(seq).padStart(19, "0") + "\n");
2939
+ let fd;
2940
+ try {
2941
+ fd = openSync(path, "r+");
2942
+ } catch (error) {
2943
+ if (!create || error.code !== "ENOENT") throw error;
2944
+ fd = openSync(path, "wx", 384);
2945
+ }
2946
+ try {
2947
+ for (let at = 0; at < line.length;) at += writeSync(fd, line, at, line.length - at, at);
2948
+ } finally {
2949
+ closeSync(fd);
2950
+ }
2951
+ }
2952
+ /**
2792
2953
  * Appends a line whole or not at all: what a full disk took of it is cut back (as the store's journal does), so the
2793
- * next line, once there is room, starts a line of its own instead of making one no reader can parse with it.
2954
+ * next line, once there is room, starts a line of its own instead of making one no reader can parse with it. Returns
2955
+ * where the line begins in the file.
2794
2956
  */
2795
2957
  function appendWhole(path, line) {
2796
2958
  const fd = openSync(path, "a", 384);
@@ -2806,7 +2968,39 @@ function appendWhole(path, line) {
2806
2968
  } finally {
2807
2969
  closeSync(fd);
2808
2970
  }
2971
+ return whole;
2972
+ }
2973
+ /**
2974
+ * The ids an event is of, as `forget`'s callers tell what is removed: its chat, its group, its message (`messageId`,
2975
+ * `message.id`), the reply that pressed it (`replyId`) and the message it quotes (`message.replyTo.id`).
2976
+ */
2977
+ function namedIn(event) {
2978
+ const message = event.message;
2979
+ return [
2980
+ event.chat,
2981
+ event.group,
2982
+ event.messageId,
2983
+ event.replyId,
2984
+ message?.id,
2985
+ message?.replyTo?.id
2986
+ ].filter((id) => typeof id === "string");
2987
+ }
2988
+ /** The keys whose values `namedIn` reads, as a journal line has them: `id` stands for `message.id` and its quote's. */
2989
+ var NAMING_KEYS = [
2990
+ "\"chat\":",
2991
+ "\"group\":",
2992
+ "\"messageId\":",
2993
+ "\"replyId\":",
2994
+ "\"id\":"
2995
+ ].map((key) => Buffer.from(key));
2996
+ /** FNV-1a of these bytes. */
2997
+ function hashOf(bytes, from = 0, to = bytes.length) {
2998
+ let hash = 2166136261;
2999
+ for (let i = from; i < to; i++) hash = Math.imul(hash ^ bytes[i], 16777619);
3000
+ return hash >>> 0;
2809
3001
  }
3002
+ /** The hash of an id as a journal line writes it (quoted, escaped). */
3003
+ var hashOfId = (id) => hashOf(Buffer.from(JSON.stringify(id)));
2810
3004
  /** The bounds in `bytes` of each line from `start` on, its newline left out. */
2811
3005
  function linesOf(bytes, start) {
2812
3006
  const lines = [];
@@ -2829,13 +3023,13 @@ function linesNaming(bytes, start, needles) {
2829
3023
  }
2830
3024
  /**
2831
3025
  * The bytes of the journal's last `JOURNAL_MAX_BYTES`, `start` where its first whole line begins in them, `at` where
2832
- * they begin in the file, and its size.
3026
+ * they begin in the file, and its size. `lineAt`: where a line begins, read from there on when it is within them.
2833
3027
  */
2834
- function journalWindow(path) {
3028
+ function journalWindow(path, lineAt = 0) {
2835
3029
  const fd = openSync(path, "r");
2836
3030
  try {
2837
3031
  const size = fstatSync(fd).size;
2838
- const from = Math.max(0, size - JOURNAL_MAX_BYTES - 1);
3032
+ const cut = Math.max(0, size - JOURNAL_MAX_BYTES - 1), from = Math.max(cut, lineAt);
2839
3033
  const buffer = Buffer.alloc(size - from);
2840
3034
  let read = 0;
2841
3035
  while (read < buffer.length) {
@@ -2846,7 +3040,7 @@ function journalWindow(path) {
2846
3040
  const bytes = buffer.subarray(0, read);
2847
3041
  return {
2848
3042
  bytes,
2849
- start: from > 0 ? bytes.indexOf(10) + 1 || bytes.length : 0,
3043
+ start: from > cut || from === 0 ? 0 : bytes.indexOf(10) + 1 || bytes.length,
2850
3044
  at: from,
2851
3045
  size
2852
3046
  };
@@ -5169,12 +5363,12 @@ async function startRuntime(paths, options = {}) {
5169
5363
  const [{ registerFileBytes }, { NodeFileBytes }] = await Promise.all([import("./fileBytes-DMzrIdCu.js"), import("./fileBytes-Douk1yOp.js")]);
5170
5364
  registerFileBytes("native", async () => new NodeFileBytes(paths.files), true);
5171
5365
  if (await (await import("./blobFiles-BnjN_Vrz.js")).moveBlobFiles()) await store.compact();
5172
- const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-DcOsgYDU.js"), import("./endpoint-DWHkXWVM.js")]);
5173
- const mainline = dht.off ? null : new (await (import("./mainline-HPlVrl8R.js"))).Mainline({
5366
+ const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-BJ3T-kx3.js"), import("./endpoint-DWHkXWVM.js")]);
5367
+ const mainline = dht.off ? null : new (await (import("./mainline-Hsk2SZ1B.js"))).Mainline({
5174
5368
  bootstrap: dht.bootstrap,
5175
5369
  host: dht.host
5176
5370
  });
5177
- const transport = mainline || pinned ? new (await (import("./mainline-HPlVrl8R.js"))).RelaysAndDht(mainline, pinned ? { relays: pinned } : {}, !!pinned) : null;
5371
+ const transport = mainline || pinned ? new (await (import("./mainline-Hsk2SZ1B.js"))).RelaysAndDht(mainline, pinned ? { relays: pinned } : {}, !!pinned) : null;
5178
5372
  const apps = servedApps(paths.dir).length > 0;
5179
5373
  const server = new EngineServer({
5180
5374
  ...transport ? { transport } : {},
@@ -5188,6 +5382,7 @@ async function startRuntime(paths, options = {}) {
5188
5382
  apps: apps ? "wire" : false,
5189
5383
  staysOnline: envSwitch("GHOSTLY_HUB", true),
5190
5384
  watchPeerIdleMs: DAEMON_WATCH_PEER_IDLE_MS,
5385
+ peerDhtPollMs: ACTIVE_DHT_POLL_MS,
5191
5386
  localFetch: nodeLocalFetch,
5192
5387
  pushSend: (request) => nodePushSend(request),
5193
5388
  holdFetch: nodeHoldFetch(),
@@ -5296,6 +5491,7 @@ async function openHost(paths, mode, version, options = {}) {
5296
5491
  ids: [...histories.keys(), ...newest.keys()]
5297
5492
  });
5298
5493
  } catch (error) {
5494
+ hub.release();
5299
5495
  await runtime.close().catch(() => {});
5300
5496
  release();
5301
5497
  throw error;
@@ -5314,6 +5510,7 @@ async function openHost(paths, mode, version, options = {}) {
5314
5510
  close: () => closing ??= (async () => {
5315
5511
  await Promise.all([calls.stopAll(), flushCardUpdates(ctx)]).catch(() => {});
5316
5512
  runtime.server.detach(hub.sink);
5513
+ hub.release();
5317
5514
  await runtime.close().catch(() => {});
5318
5515
  release();
5319
5516
  })()
@@ -5534,4 +5731,4 @@ function connection(ctx, socket, secret) {
5534
5731
  //#endregion
5535
5732
  export { BASELINE_NEWEST, MAX_UNREAD, keepServing, openHost, serve };
5536
5733
 
5537
- //# sourceMappingURL=host-G5seUwaI.js.map
5734
+ //# sourceMappingURL=host-fkQYBvKH.js.map