@ghostlytools/cli 1.13.2 → 1.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +9 -8
  2. package/dist/assets/{api-DV52o7A8.js → api-B5qxNMgg.js} +2 -2
  3. package/dist/assets/{api-O57SNJcb.js → api-iiij_pP5.js} +213 -59
  4. package/dist/assets/api-iiij_pP5.js.map +1 -0
  5. package/dist/assets/{apiKit-CPTFJHA7.js → apiKit-BPuHOoXF.js} +17 -3
  6. package/dist/assets/apiKit-BPuHOoXF.js.map +1 -0
  7. package/dist/assets/{appFetch-Ca9DNQQ2.js → appFetch-D89FnCxr.js} +2 -2
  8. package/dist/assets/{appFetch-Ca9DNQQ2.js.map → appFetch-D89FnCxr.js.map} +1 -1
  9. package/dist/assets/{apps-uZTu5FlR.js → apps-BpZcd86Y.js} +1 -1
  10. package/dist/assets/{apps-gXpGFPiv.js → apps-D2YGSPJe.js} +45 -19
  11. package/dist/assets/apps-D2YGSPJe.js.map +1 -0
  12. package/dist/assets/{backup-CxH3auHB.js → backup-BrMe9VPY.js} +11 -6
  13. package/dist/assets/backup-BrMe9VPY.js.map +1 -0
  14. package/dist/assets/{backup-Di2U6QVH.js → backup-COkkM8Yy.js} +1 -1
  15. package/dist/assets/bark_ffi_wasm_bg-CjjCN3FD.js +5 -0
  16. package/dist/assets/{bitcoind-BpZn3bRN.js → bitcoind-CK1QEUHn.js} +2 -2
  17. package/dist/assets/{bitcoind-BpZn3bRN.js.map → bitcoind-CK1QEUHn.js.map} +1 -1
  18. package/dist/assets/bitcoindevkit_bg-D6IFIaJi.js +5 -0
  19. package/dist/assets/{blobFiles-DDs34n0r.js → blobFiles-DA3UEn37.js} +3 -3
  20. package/dist/assets/{blobFiles-DDs34n0r.js.map → blobFiles-DA3UEn37.js.map} +1 -1
  21. package/dist/assets/buttons-BWEdoxgK.js +1 -0
  22. package/dist/assets/{buttons-D4udKEvT.js → buttons-UsDPQJsQ.js} +4 -4
  23. package/dist/assets/{buttons-D4udKEvT.js.map → buttons-UsDPQJsQ.js.map} +1 -1
  24. package/dist/assets/{client-DGx7E8b3.js → client-Cj-VLeTs.js} +2 -2
  25. package/dist/assets/{client-DGx7E8b3.js.map → client-Cj-VLeTs.js.map} +1 -1
  26. package/dist/assets/{fedimintSdk-D-6H9Pe7.js → fedimintSdk-eLXBrj9i.js} +2 -2
  27. package/dist/assets/{fedimintSdk-D-6H9Pe7.js.map → fedimintSdk-eLXBrj9i.js.map} +1 -1
  28. package/dist/assets/fedimint_client_wasm_bg-CRQlnJdu.js +5 -0
  29. package/dist/assets/{fileBytes-CfvUXdSy.js → fileBytes-B2jXQI8e.js} +3 -3
  30. package/dist/assets/{fileBytes-CfvUXdSy.js.map → fileBytes-B2jXQI8e.js.map} +1 -1
  31. package/dist/assets/{fileBytes-tj6-VZcB.js → fileBytes-BQrW3m-d.js} +2 -2
  32. package/dist/assets/{fileBytes-tj6-VZcB.js.map → fileBytes-BQrW3m-d.js.map} +1 -1
  33. package/dist/assets/{fileBytes-CCKbwqG_.js → fileBytes-CtjxJ6AL.js} +1 -1
  34. package/dist/assets/{fileBytesIdb-BJirUAjI.js → fileBytesIdb-B7Wjw0S8.js} +3 -3
  35. package/dist/assets/{fileBytesIdb-BJirUAjI.js.map → fileBytesIdb-B7Wjw0S8.js.map} +1 -1
  36. package/dist/assets/fileBytesIdb-Fcj2mwj6.js +1 -0
  37. package/dist/assets/{fileBytesOpfs-DRUYLER4.js → fileBytesOpfs-BW0u-_f_.js} +3 -3
  38. package/dist/assets/{fileBytesOpfs-DRUYLER4.js.map → fileBytesOpfs-BW0u-_f_.js.map} +1 -1
  39. package/dist/assets/{fixedHosts-CODN2pTL.js → fixedHosts-CrO01M_y.js} +3 -3
  40. package/dist/assets/{fixedHosts-CODN2pTL.js.map → fixedHosts-CrO01M_y.js.map} +1 -1
  41. package/dist/assets/{groupMeta-CeZpqVVC.js → groupMeta-DHJxOu0K.js} +99 -10
  42. package/dist/assets/groupMeta-DHJxOu0K.js.map +1 -0
  43. package/dist/assets/{host-CRA7h5A2.js → host-C4J4y-Zq.js} +187 -29
  44. package/dist/assets/host-C4J4y-Zq.js.map +1 -0
  45. package/dist/assets/{idb-ewvbqyDI.js → idb-CmIR_bxg.js} +2 -1
  46. package/dist/assets/idb-CmIR_bxg.js.map +1 -0
  47. package/dist/assets/{identityProofs-B8zCp5_0.js → identityProofs-C0rIGT6y.js} +20 -3
  48. package/dist/assets/identityProofs-C0rIGT6y.js.map +1 -0
  49. package/dist/assets/{irohWeb-mnOi2Jp7.js → irohWeb-BXgunUhu.js} +113 -33
  50. package/dist/assets/irohWeb-BXgunUhu.js.map +1 -0
  51. package/dist/assets/{main-Chc871HV.js → main-BLs-zUTt.js} +68 -28
  52. package/dist/assets/main-BLs-zUTt.js.map +1 -0
  53. package/dist/assets/{mainline-CDUXtV-I.js → mainline-CYek1H10.js} +2 -2
  54. package/dist/assets/{mainline-CDUXtV-I.js.map → mainline-CYek1H10.js.map} +1 -1
  55. package/dist/assets/{networkReason-DYlPZS5M.js → networkReason-C6LYbjF2.js} +2 -2
  56. package/dist/assets/{networkReason-DYlPZS5M.js.map → networkReason-C6LYbjF2.js.map} +1 -1
  57. package/dist/assets/{ownDid-BOuSlDv-.js → ownDid-Dg-8e_hs.js} +6 -5
  58. package/dist/assets/ownDid-Dg-8e_hs.js.map +1 -0
  59. package/dist/assets/{ownStatus-CLA1VPkL.js → ownStatus-BOx4vLi6.js} +34 -11
  60. package/dist/assets/ownStatus-BOx4vLi6.js.map +1 -0
  61. package/dist/assets/{profiles-BH1bH9aE.js → profiles-BA0ePikY.js} +33 -8
  62. package/dist/assets/profiles-BA0ePikY.js.map +1 -0
  63. package/dist/assets/{registry-DAq16T-V.js → registry-DuSwXntJ.js} +1 -1
  64. package/dist/assets/{registry-DP_-RA-v.js → registry-OoIgxDrO.js} +25 -13
  65. package/dist/assets/registry-OoIgxDrO.js.map +1 -0
  66. package/dist/assets/{relay-gJTOYQJZ.js → relay-NT5bf1WA.js} +9 -6
  67. package/dist/assets/relay-NT5bf1WA.js.map +1 -0
  68. package/dist/assets/{resolve-B1eQ4dVQ.js → resolve-DUmE70Eo.js} +3 -3
  69. package/dist/assets/{resolve-B1eQ4dVQ.js.map → resolve-DUmE70Eo.js.map} +1 -1
  70. package/dist/assets/{restoredRows-D_WSHE7H.js → restoredRows-Cgaxx1xW.js} +2 -2
  71. package/dist/assets/{restoredRows-D_WSHE7H.js.map → restoredRows-Cgaxx1xW.js.map} +1 -1
  72. package/dist/assets/{server-Bd2ofxc1.js → server-WWlXICVB.js} +555 -110
  73. package/dist/assets/server-WWlXICVB.js.map +1 -0
  74. package/dist/assets/{sshForges-As7dbGXM.js → sshForges-Cfu-dSwc.js} +2 -2
  75. package/dist/assets/{sshForges-As7dbGXM.js.map → sshForges-Cfu-dSwc.js.map} +1 -1
  76. package/dist/assets/translate-DnuQMfj0.js.map +1 -1
  77. package/dist/assets/{usdt-DLmtf8F9.js → usdt-BkO4SY07.js} +2 -2
  78. package/dist/assets/{usdt-DLmtf8F9.js.map → usdt-BkO4SY07.js.map} +1 -1
  79. package/dist/assets/{waitNotice-BHuMofaH.js → waitNotice-DSECf6II.js} +2 -2
  80. package/dist/assets/{waitNotice-BHuMofaH.js.map → waitNotice-DSECf6II.js.map} +1 -1
  81. package/dist/ghostly.mjs +1 -1
  82. package/npm-shrinkwrap.json +2 -2
  83. package/package.json +1 -1
  84. package/dist/assets/api-O57SNJcb.js.map +0 -1
  85. package/dist/assets/apiKit-CPTFJHA7.js.map +0 -1
  86. package/dist/assets/apps-gXpGFPiv.js.map +0 -1
  87. package/dist/assets/backup-CxH3auHB.js.map +0 -1
  88. package/dist/assets/bark_ffi_wasm_bg-CcafsJla.js +0 -5
  89. package/dist/assets/bark_ffi_wasm_bg-CcafsJla.js.map +0 -1
  90. package/dist/assets/bark_ffi_wasm_bg-VNmaJsc9.wasm +0 -0
  91. package/dist/assets/bitcoindevkit_bg-BWQHx7vd.js +0 -5
  92. package/dist/assets/bitcoindevkit_bg-BWQHx7vd.js.map +0 -1
  93. package/dist/assets/bitcoindevkit_bg-DXil67yS.wasm +0 -0
  94. package/dist/assets/buttons-YTYEqvGC.js +0 -1
  95. package/dist/assets/fedimint_client_wasm_bg-Cr5xGD-w.js +0 -5
  96. package/dist/assets/fedimint_client_wasm_bg-Cr5xGD-w.js.map +0 -1
  97. package/dist/assets/fedimint_client_wasm_bg-DeFtkIv6.wasm +0 -0
  98. package/dist/assets/fileBytesIdb-B0zzJ6p7.js +0 -1
  99. package/dist/assets/groupMeta-CeZpqVVC.js.map +0 -1
  100. package/dist/assets/host-CRA7h5A2.js.map +0 -1
  101. package/dist/assets/idb-ewvbqyDI.js.map +0 -1
  102. package/dist/assets/identityProofs-B8zCp5_0.js.map +0 -1
  103. package/dist/assets/irohWeb-mnOi2Jp7.js.map +0 -1
  104. package/dist/assets/main-Chc871HV.js.map +0 -1
  105. package/dist/assets/ownDid-BOuSlDv-.js.map +0 -1
  106. package/dist/assets/ownStatus-CLA1VPkL.js.map +0 -1
  107. package/dist/assets/profiles-BH1bH9aE.js.map +0 -1
  108. package/dist/assets/registry-DP_-RA-v.js.map +0 -1
  109. package/dist/assets/relay-gJTOYQJZ.js.map +0 -1
  110. package/dist/assets/server-Bd2ofxc1.js.map +0 -1
@@ -1,18 +1,19 @@
1
- import { p as fileStore } from "./idb-ewvbqyDI.js";
2
- import { _ as groupJson, b as resumeHolds, d as reportAppFrames, f as servedApps, g as chatMessageJson, h as chatJson, p as nodeLocalFetch, r as callApi, u as paymentJson, v as groupMessageJson, x as flushCardUpdates, y as groupPayTwins } from "./api-O57SNJcb.js";
3
- import { I as sinceSeq, s as findChat, u as findOwnNamedChat } from "./apiKit-CPTFJHA7.js";
1
+ import { p as fileStore } from "./idb-CmIR_bxg.js";
2
+ import { _ as groupJson, b as resumeHolds, d as reportAppFrames, f as servedApps, g as chatMessageJson, h as chatJson, p as nodeLocalFetch, r as callApi, u as paymentJson, v as groupMessageJson, x as flushCardUpdates, y as groupPayTwins } from "./api-iiij_pP5.js";
3
+ import { L as sinceSeq, o as dhtOnly, s as findChat, u as findOwnNamedChat } from "./apiKit-BPuHOoXF.js";
4
4
  import { o as shownValue, r as asCliError, t as CliError } from "./errors-JVJsO8Iu.js";
5
- import { C as ownFolder, D as runtimePath, E as privateFolder, n as RESTORED_MARK, r as acquireLock, w as ownProfile, x as startingPath } from "./profiles-BH1bH9aE.js";
6
- import { _ as frameBytes, b as monotonic, d as proof, f as removeDaemonSecret, g as bytesToMs, h as PlaybackQueue, l as greeting, m as DEFAULT_RATE, u as newDaemonSecret, v as frameSamples, y as isCallRate } from "./client-DGx7E8b3.js";
7
- import { Ci as DAEMON_WATCH_PEER_IDLE_MS, Gi as callSignalHeardAt, Hi as answersOffer, Ji as parseCallSignal, Qt as checkPushEndpoint, St as STATUS_IDLE_AFTER_MS, Ui as buildSdpFromSignal, Vi as RTC_CONFIG, Wi as callRtcConfig, Yi as signalHasVideo, _n as ACTIVE_DHT_POLL_MS, qi as extractParamsFromSdp, zi as CALL_SIGNAL_MAX_AGE_MS } from "./groupMeta-CeZpqVVC.js";
8
- import { g as parsePayLink, r as irohRelayProblem } from "./irohWeb-mnOi2Jp7.js";
5
+ import { C as dropRuntimeFolder, D as privateFolder, O as runtimePath, T as ownProfile, n as RESTORED_MARK, r as acquireLock, w as ownFolder, x as startingPath } from "./profiles-BA0ePikY.js";
6
+ import { _ as frameBytes, b as monotonic, d as proof, f as removeDaemonSecret, g as bytesToMs, h as PlaybackQueue, l as greeting, m as DEFAULT_RATE, u as newDaemonSecret, v as frameSamples, y as isCallRate } from "./client-Cj-VLeTs.js";
7
+ import { Ci as DAEMON_WATCH_PEER_IDLE_MS, Gi as callSignalHeardAt, Hi as answersOffer, Ji as parseCallSignal, Qt as checkPushEndpoint, St as STATUS_IDLE_AFTER_MS, Ui as buildSdpFromSignal, Vi as RTC_CONFIG, Wi as callRtcConfig, Yi as signalHasVideo, _n as ACTIVE_DHT_POLL_MS, qi as extractParamsFromSdp, zi as CALL_SIGNAL_MAX_AGE_MS } from "./groupMeta-DHJxOu0K.js";
8
+ import { g as parsePayLink, r as irohRelayProblem } from "./irohWeb-BXgunUhu.js";
9
9
  import { i as fromBase64Url } from "./bytes-C1DNGY18.js";
10
- import { u as setLinkTraceSink } from "./relay-gJTOYQJZ.js";
10
+ import { u as setLinkTraceSink } from "./relay-NT5bf1WA.js";
11
11
  import { b as setPublicKeyFastPath } from "./pkarr-BQo4PcCg.js";
12
- import { p as nativePublicKey } from "./apps-gXpGFPiv.js";
13
- import { n as fedimintSdkOn } from "./fedimintSdk-D-6H9Pe7.js";
12
+ import { p as nativePublicKey } from "./apps-D2YGSPJe.js";
13
+ import { n as fedimintSdkOn } from "./fedimintSdk-eLXBrj9i.js";
14
14
  import { s as isPublicAddress } from "./domainProofs-b3Y1Gj9g.js";
15
15
  import { createRequire } from "node:module";
16
+ import { fileURLToPath, pathToFileURL } from "node:url";
16
17
  import { appendFileSync, chmodSync, closeSync, existsSync, fstatSync, fsync, fsyncSync, ftruncateSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, statSync, truncateSync, writeFileSync, writeSync } from "node:fs";
17
18
  import { open, readFile } from "node:fs/promises";
18
19
  import { dirname, isAbsolute, join, resolve } from "node:path";
@@ -22,7 +23,6 @@ import { Readable } from "node:stream";
22
23
  import { connect, createServer, isIP } from "node:net";
23
24
  import { createInterface } from "node:readline";
24
25
  import { createSocket } from "node:dgram";
25
- import { fileURLToPath, pathToFileURL } from "node:url";
26
26
  import { Worker } from "node:worker_threads";
27
27
  import { request } from "node:https";
28
28
  import { lookup } from "node:dns";
@@ -153,6 +153,72 @@ function feedQueue(queue) {
153
153
  };
154
154
  }
155
155
  /**
156
+ * The most pieces a frame is written in (`kernelShare`): 1.25 ms of audio each. Linux counts its socket buffer per
157
+ * write (~1 KiB each however small, ~200 KiB in all), so it holds 3.6 s of 20 ms frames at 8 or 16 kHz and 1 s at
158
+ * 48 kHz for a program that stopped reading, and ~0.4 s in 16 pieces. macOS counts bytes (8 KiB): one write a frame.
159
+ */
160
+ var MAX_PIECES = 16;
161
+ var kernelShares = /* @__PURE__ */ new Map();
162
+ /**
163
+ * How many pieces each frame of `rate` is written in, and how much of it the kernel then holds for a program that
164
+ * reads none. Each count, 1, 2, 4, ... `MAX_PIECES`, is measured on the call's own socket before anyone else may open
165
+ * it (`AudioSocket.open`); the first that leaves the kernel half of `MAX_UNREAD_MS` or less is taken, else the one
166
+ * that held least (a count is taken only when it holds 5% less than fewer pieces did: macOS stays at one). Once per
167
+ * rate, since it depends on the system, not on the call. Windows, or no measure (an error): one piece, nothing held,
168
+ * the bound what waits in the daemon, as before; the next call measures again.
169
+ */
170
+ async function kernelShare(path, rate, onConnection) {
171
+ const known = kernelShares.get(rate);
172
+ if (known) return known;
173
+ const none = {
174
+ pieces: 1,
175
+ heldMs: 0
176
+ };
177
+ if (process.platform === "win32") return none;
178
+ let best = null;
179
+ for (let pieces = 1; pieces <= MAX_PIECES && !(best && best.heldMs <= 500); pieces *= 2) {
180
+ const held = await kernelHolds(path, frameBytes(rate) / pieces, rate * 2 * 3e4 / 1e3, onConnection);
181
+ if (held === null) return none;
182
+ const heldMs = bytesToMs(rate, held);
183
+ if (!best || heldMs < best.heldMs * .95) best = {
184
+ pieces,
185
+ heldMs
186
+ };
187
+ }
188
+ kernelShares.set(rate, best);
189
+ return best;
190
+ }
191
+ /**
192
+ * Bytes the kernel takes in writes of `piece` bytes, one by one as `AudioSocket` writes them, until it holds one back
193
+ * (or `most`): all in one turn of the event loop, so the reader, this process's own connection, has read none. Null
194
+ * when no connection came.
195
+ */
196
+ function kernelHolds(path, piece, most, onConnection) {
197
+ return new Promise((resolve) => {
198
+ const reader = connect(path);
199
+ reader.on("error", () => {});
200
+ const done = (held) => {
201
+ clearTimeout(timer);
202
+ onConnection(null);
203
+ reader.destroy();
204
+ resolve(held);
205
+ };
206
+ const timer = setTimeout(() => done(null), 2e3);
207
+ onConnection((writer) => {
208
+ writer.on("error", () => {});
209
+ const bytes = Buffer.alloc(piece);
210
+ let held = 0;
211
+ while (held < most) {
212
+ writer.write(bytes);
213
+ if (writer.writableLength > 0) break;
214
+ held += piece;
215
+ }
216
+ writer.destroy();
217
+ done(held);
218
+ });
219
+ });
220
+ }
221
+ /**
156
222
  * Serves one call's audio to one program at a time: what the program writes is handed to `onAudio` as it comes (raw
157
223
  * PCM, any amount); `write` gives it the caller's audio. A second connection replaces the first. `close` ends the
158
224
  * program's connection (it reads EOF) and removes the socket.
@@ -170,19 +236,28 @@ var AudioSocket = class AudioSocket {
170
236
  rate;
171
237
  requireToken;
172
238
  proveMs;
239
+ kernel;
173
240
  client = null;
174
241
  /** Connections that have not sent the token yet. */
175
242
  unproven = /* @__PURE__ */ new Set();
176
243
  closed = false;
244
+ /**
245
+ * Audio for the program that waits in the daemon while the kernel holds a write back (it is full: the program reads
246
+ * slower than the call speaks), in the pieces `kernelShare` measured. Handed to the kernel one write at a time:
247
+ * Node's own buffer would join them into one larger write, which the kernel takes more of.
248
+ */
249
+ backlog = [];
250
+ backlogBytes = 0;
177
251
  /** Frames the program did not read in time, since the start. */
178
252
  dropped = 0;
179
253
  token = randomBytes(32).toString("hex");
180
- constructor(path, server, rate, requireToken, proveMs) {
254
+ constructor(path, server, rate, requireToken, proveMs, kernel) {
181
255
  this.path = path;
182
256
  this.server = server;
183
257
  this.rate = rate;
184
258
  this.requireToken = requireToken;
185
259
  this.proveMs = proveMs;
260
+ this.kernel = kernel;
186
261
  }
187
262
  static async open(path, rate, handlers, options = {}) {
188
263
  const shared = !!options.shared && process.platform !== "win32";
@@ -192,7 +267,8 @@ var AudioSocket = class AudioSocket {
192
267
  if (existsSync(path)) rmSync(path, { force: true });
193
268
  }
194
269
  let self = null;
195
- const server = createServer((socket) => self ? self.accept(socket, handlers) : socket.destroy());
270
+ let probe = null;
271
+ const server = createServer((socket) => self ? self.accept(socket, handlers) : probe ? probe(socket) : socket.destroy());
196
272
  await new Promise((resolve, reject) => {
197
273
  server.once("error", reject);
198
274
  server.listen(path, () => {
@@ -200,8 +276,11 @@ var AudioSocket = class AudioSocket {
200
276
  resolve();
201
277
  });
202
278
  });
279
+ const kernel = await kernelShare(path, rate, (take) => {
280
+ probe = take;
281
+ });
203
282
  if (process.platform !== "win32") chmodSync(path, shared ? 432 : 384);
204
- self = new AudioSocket(path, server, rate, options.requireToken ?? (shared || process.platform === "win32"), options.proveMs ?? 5e3);
283
+ self = new AudioSocket(path, server, rate, options.requireToken ?? (shared || process.platform === "win32"), options.proveMs ?? 5e3, kernel);
205
284
  return self;
206
285
  }
207
286
  get connected() {
@@ -220,6 +299,8 @@ var AudioSocket = class AudioSocket {
220
299
  this.unproven.delete(socket);
221
300
  const before = this.client;
222
301
  this.client = socket;
302
+ this.backlog = [];
303
+ this.backlogBytes = 0;
223
304
  if (before) {
224
305
  before.end();
225
306
  setTimeout(() => before.destroy(), 2e3).unref();
@@ -264,25 +345,47 @@ var AudioSocket = class AudioSocket {
264
345
  this.unproven.delete(socket);
265
346
  if (this.client !== socket) return;
266
347
  this.client = null;
348
+ this.backlog = [];
349
+ this.backlogBytes = 0;
267
350
  handlers.onClient?.(false);
268
351
  });
269
352
  }
270
- /** One frame of the caller's audio for the program; dropped when nobody listens or the program stopped reading. */
353
+ /**
354
+ * One frame of the caller's audio for the program; dropped when nobody listens or the program stopped reading. A
355
+ * program that keeps up gets it at once: the kernel takes it, nothing waits. Once the kernel holds one back, it is
356
+ * full (`kernel.heldMs` the program has not read), and what waits in the daemon may make up the rest of
357
+ * `MAX_UNREAD_MS`.
358
+ */
271
359
  write(frame) {
272
360
  const client = this.client;
273
361
  if (!client || client.destroyed) return;
274
- if (bytesToMs(this.rate, client.writableLength) > 1e3) {
362
+ if ((client.writableLength > 0 || this.backlog.length) && this.kernel.heldMs + bytesToMs(this.rate, client.writableLength + this.backlogBytes + frame.length) > 1e3) {
275
363
  this.dropped++;
276
364
  return;
277
365
  }
278
- client.write(frame);
366
+ const piece = Math.ceil(frame.length / this.kernel.pieces / 2) * 2;
367
+ for (let at = 0; at < frame.length; at += piece) this.backlog.push(frame.subarray(at, at + piece));
368
+ this.backlogBytes += frame.length;
369
+ this.pump();
279
370
  }
371
+ /** Hands the kernel what waits, one write a piece, for as long as it takes each whole (`backlog`). */
372
+ pump = () => {
373
+ const client = this.client;
374
+ while (client && !client.destroyed && client.writableLength === 0 && this.backlog.length) {
375
+ const piece = this.backlog.shift();
376
+ this.backlogBytes -= piece.length;
377
+ client.write(piece, this.pump);
378
+ }
379
+ };
280
380
  /** Ends the program's connection (EOF once what was written is read) and removes the socket. */
281
381
  close() {
282
382
  if (this.closed) return Promise.resolve();
283
383
  this.closed = true;
284
384
  const client = this.client;
285
385
  this.client = null;
386
+ if (client && !client.destroyed) for (const piece of this.backlog) client.write(piece);
387
+ this.backlog = [];
388
+ this.backlogBytes = 0;
286
389
  client?.end();
287
390
  if (client) setTimeout(() => client.destroy(), 2e3).unref();
288
391
  for (const socket of this.unproven) socket.destroy();
@@ -290,6 +393,7 @@ var AudioSocket = class AudioSocket {
290
393
  return new Promise((resolve) => {
291
394
  this.server.close(() => resolve());
292
395
  if (process.platform !== "win32") rmSync(this.path, { force: true });
396
+ dropRuntimeFolder(this.path);
293
397
  });
294
398
  }
295
399
  };
@@ -1944,6 +2048,15 @@ var CallManager = class {
1944
2048
  if (this.host.answers === false) throw new CliError("unavailable", CALLS_NEED_DAEMON);
1945
2049
  if (!link.profile) throw new CliError("unavailable", "Calls are in chats made since 0.4 (the chat session): this is a compatibility chat");
1946
2050
  if (link.callsUnavailable === CALLS_NEED_LIVE) {
2051
+ try {
2052
+ dhtOnly(link);
2053
+ } catch (error) {
2054
+ if (!(error instanceof CliError)) throw error;
2055
+ throw new CliError("unavailable", `${CALLS_NEED_LIVE}. ${error.message}`, {
2056
+ chat: link.id,
2057
+ ...error.details
2058
+ });
2059
+ }
1947
2060
  const shown = /^[\w.:-]+$/.test(link.label ?? "") ? link.label : link.id;
1948
2061
  throw new CliError("unavailable", `${CALLS_NEED_LIVE}, and your contact's app is not connected now: ghostly chat wait ${shown} --until live waits for it, then call again`, { chat: link.id });
1949
2062
  }
@@ -2106,6 +2219,8 @@ var EventHub = class {
2106
2219
  seen = /* @__PURE__ */ new Map();
2107
2220
  firstRun = false;
2108
2221
  chats = /* @__PURE__ */ new Map();
2222
+ /** The chats a live session carried at some state of this run, gone down since or not (`wasLive`). */
2223
+ liveThisRun = /* @__PURE__ */ new Set();
2109
2224
  groups = /* @__PURE__ */ new Map();
2110
2225
  payments = /* @__PURE__ */ new Map();
2111
2226
  transfers = /* @__PURE__ */ new Map();
@@ -2284,6 +2399,10 @@ var EventHub = class {
2284
2399
  this.baselined = true;
2285
2400
  for (const id of [...state.links.map((link) => link.id), ...state.groups.map((group) => `group:${group.id}`)]) this.unread.set(id, reads.files);
2286
2401
  }
2402
+ /** Whether a live session carried the chat at some state of this run: its daemon's start is behind it (`chat connect`). */
2403
+ wasLive(chat) {
2404
+ return this.liveThisRun.has(chat);
2405
+ }
2287
2406
  /** The catch-ups still running (`perChat`): their events are told once they end. */
2288
2407
  async caughtUp() {
2289
2408
  while (this.catchingUp.size) await Promise.all(this.catchingUp);
@@ -2710,6 +2829,7 @@ var EventHub = class {
2710
2829
  if (last.get(id) === link && this.chats.has(id)) continue;
2711
2830
  const shape = chatShape(link), before = this.chats.get(id);
2712
2831
  this.chats.set(id, shape);
2832
+ if (shape.live) this.liveThisRun.add(id);
2713
2833
  if (quiet) continue;
2714
2834
  if (!before) {
2715
2835
  this.emit("chat.created", `chat.created:${id}:${this.now()}`, {
@@ -2758,6 +2878,7 @@ var EventHub = class {
2758
2878
  for (const id of removedChats) {
2759
2879
  const peer = this.chats.get(id)?.peer;
2760
2880
  this.chats.delete(id);
2881
+ this.liveThisRun.delete(id);
2761
2882
  if (!quiet) this.emit("chat.removed", `chat.removed:${id}:${this.now()}`, {
2762
2883
  chat: id,
2763
2884
  ...peer ? { peer } : {}
@@ -5165,7 +5286,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5165
5286
  * A committed transaction's entry, numbered `entry` (its first op says so, where there are trees), from the
5166
5287
  * transaction whose rollback log is `owner`. Written at once, it may fail (a full disk): the reason is given back,
5167
5288
  * and the transaction is to be refused, not completed. "unsynced": written, and its `complete` waits for a sync.
5168
- * "queued": written behind what the queue holds (a fold, a Blob's entry), and `complete` called once it is: what
5289
+ * "queued": written behind what the queue holds (a fold, a Blob's entry), and `complete` called once a sync covers it: what
5169
5290
  * completed before was on no disk while the fold waited for the sync in flight, and a process killed then lost a
5170
5291
  * text the engine had told the contact it held.
5171
5292
  */
@@ -5192,14 +5313,27 @@ async function openPersistentIndexedDb(dir, options = {}) {
5192
5313
  }
5193
5314
  trees?.complete(owner, entry);
5194
5315
  held++;
5316
+ const done = () => {
5317
+ held--;
5318
+ complete();
5319
+ };
5320
+ let sharing = false;
5195
5321
  enqueue(async () => {
5196
5322
  try {
5197
5323
  if (entry <= foldedUpTo) return;
5198
5324
  if (writeEntry(serialize(await Promise.all(ops.map(async (op) => op.kind === "put" ? {
5199
5325
  ...op,
5200
5326
  value: await blobsToDisk(op.value)
5201
- } : op))), lazy ? "lazy" : "now")) {
5327
+ } : op))), lazy ? "lazy" : "shared")) {
5202
5328
  holdsDelete ||= deleting;
5329
+ if (!lazy) {
5330
+ sharing = true;
5331
+ shareSync(() => {
5332
+ written(entry);
5333
+ done();
5334
+ });
5335
+ return;
5336
+ }
5203
5337
  written(entry);
5204
5338
  }
5205
5339
  } catch (error) {
@@ -5207,8 +5341,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5207
5341
  await writeSnapshot().catch(failed);
5208
5342
  }
5209
5343
  }).then(() => {
5210
- held--;
5211
- complete();
5344
+ if (!sharing) done();
5212
5345
  });
5213
5346
  return "queued";
5214
5347
  }
@@ -5422,6 +5555,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5422
5555
  * holds, the transactions whose layers go to the trees with it.
5423
5556
  */
5424
5557
  async function capture(journalName) {
5558
+ await unwritten();
5425
5559
  const snapshot = {
5426
5560
  format: 1,
5427
5561
  journal: journalName,
@@ -5471,6 +5605,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5471
5605
  async function exportTo(file) {
5472
5606
  const databases = [];
5473
5607
  let reader = null;
5608
+ await unwritten();
5474
5609
  try {
5475
5610
  for (const db of raw._databases.values()) {
5476
5611
  if (db.name === PROBE) continue;
@@ -5612,6 +5747,26 @@ async function openPersistentIndexedDb(dir, options = {}) {
5612
5747
  quiet = busy() ? 0 : quiet + 1;
5613
5748
  }
5614
5749
  }
5750
+ /**
5751
+ * Resolves in a turn where no running transaction has written to a store held in memory (or after `CLOSE_WAIT_MS`):
5752
+ * fake-indexeddb puts its writes in the records at once and takes them back if it aborts. A fold read them meanwhile,
5753
+ * and its snapshot kept what the transaction never committed (a refused write, after a restart), or a part of what it
5754
+ * did (its other part in the journal entry a process killed then never wrote). One that commits meanwhile waits behind
5755
+ * the fold in the queue (`append`): the snapshot holds it, whole.
5756
+ */
5757
+ async function unwritten() {
5758
+ const until = Date.now() + CLOSE_WAIT_MS;
5759
+ while (Date.now() < until && writesHeld()) await new Promise((resolve) => setImmediate(resolve));
5760
+ }
5761
+ function writesHeld() {
5762
+ return [...raw._databases.values()].some((db) => db.transactions.some((transaction) => {
5763
+ const { _state, _rollbackLog } = transaction;
5764
+ return _state !== "finished" && !!pending.get(_rollbackLog)?.some((op) => {
5765
+ const store = db.rawObjectStores.get(op.store);
5766
+ return !store || !disk(store);
5767
+ });
5768
+ }));
5769
+ }
5615
5770
  /** Whether a transaction is running or waiting to, or for the sync or the queued write that tells the engine it completed. */
5616
5771
  function busy() {
5617
5772
  return syncing !== null || held > 0 || [...raw._databases.values()].some((db) => db.transactions.some((transaction) => transaction._state !== "finished"));
@@ -6766,7 +6921,7 @@ async function startRuntime(paths, options = {}) {
6766
6921
  const restored = join(paths.dir, RESTORED_MARK);
6767
6922
  if (existsSync(restored)) {
6768
6923
  try {
6769
- const rows = await import("./restoredRows-D_WSHE7H.js");
6924
+ const rows = await import("./restoredRows-Cgaxx1xW.js");
6770
6925
  await rows.markRestoredWallet();
6771
6926
  await rows.markLeftOutFiles(leftOutIn(restored));
6772
6927
  await rows.forgetRatchets();
@@ -6786,11 +6941,11 @@ async function startRuntime(paths, options = {}) {
6786
6941
  await installEventSource();
6787
6942
  await preloadWalletModules();
6788
6943
  (await import("./ghostly_iroh_web-9VOwZcJQ.js")).initSync({ module: irohWasmBytes() });
6789
- const [{ registerFileBytes }, { NodeFileBytes }] = await Promise.all([import("./fileBytes-CCKbwqG_.js"), import("./fileBytes-tj6-VZcB.js")]);
6944
+ const [{ registerFileBytes }, { NodeFileBytes }] = await Promise.all([import("./fileBytes-CtjxJ6AL.js"), import("./fileBytes-BQrW3m-d.js")]);
6790
6945
  registerFileBytes("native", async () => new NodeFileBytes(paths.files), true);
6791
- if (await (await import("./blobFiles-DDs34n0r.js")).moveBlobFiles()) await store.compact();
6792
- const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-Bd2ofxc1.js"), import("./endpoint-Cvu-taMs.js")]);
6793
- const mainline = dht.off ? null : new (await (import("./mainline-CDUXtV-I.js"))).Mainline({
6946
+ if (await (await import("./blobFiles-DA3UEn37.js")).moveBlobFiles()) await store.compact();
6947
+ const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-WWlXICVB.js"), import("./endpoint-Cvu-taMs.js")]);
6948
+ const mainline = dht.off ? null : new (await (import("./mainline-CYek1H10.js"))).Mainline({
6794
6949
  bootstrap: dht.bootstrap,
6795
6950
  host: dht.host
6796
6951
  });
@@ -6798,7 +6953,7 @@ async function startRuntime(paths, options = {}) {
6798
6953
  ...pinned ? { relays: pinned } : {},
6799
6954
  ...relayBudgetForTests()
6800
6955
  };
6801
- const transport = mainline || pinned ? new (await (import("./mainline-CDUXtV-I.js"))).RelaysAndDht(mainline, relayOptions, !!pinned) : null;
6956
+ const transport = mainline || pinned ? new (await (import("./mainline-CYek1H10.js"))).RelaysAndDht(mainline, relayOptions, !!pinned) : null;
6802
6957
  const apps = servedApps(paths.dir).length > 0;
6803
6958
  const server = new EngineServer({
6804
6959
  ...transport ? { transport } : {},
@@ -7051,7 +7206,10 @@ async function serve(host) {
7051
7206
  return {
7052
7207
  server,
7053
7208
  close: () => closing ??= new Promise((resolve) => {
7054
- server.close(() => resolve());
7209
+ server.close(() => {
7210
+ dropRuntimeFolder(path);
7211
+ resolve();
7212
+ });
7055
7213
  removeDaemonSecret(ctx.runtime.paths.dir);
7056
7214
  stopping.abort();
7057
7215
  within(Promise.all(answering), STOP_ANSWERS_MS).then(() => {
@@ -7260,4 +7418,4 @@ function connection(ctx, socket, secret, answering) {
7260
7418
  }
7261
7419
  export { BASELINE_NEWEST, MAX_UNREAD, STOP_ANSWERS_MS, keepServing, openHost, serve };
7262
7420
 
7263
- //# sourceMappingURL=host-CRA7h5A2.js.map
7421
+ //# sourceMappingURL=host-C4J4y-Zq.js.map