@ghostlytools/cli 1.13.2 → 1.13.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 (83) hide show
  1. package/README.md +9 -8
  2. package/dist/assets/{api-DV52o7A8.js → api-DO0YMLiB.js} +2 -2
  3. package/dist/assets/{api-O57SNJcb.js → api-Dz2cw7WD.js} +155 -44
  4. package/dist/assets/api-Dz2cw7WD.js.map +1 -0
  5. package/dist/assets/{apiKit-CPTFJHA7.js → apiKit-DAFsWtyk.js} +17 -3
  6. package/dist/assets/apiKit-DAFsWtyk.js.map +1 -0
  7. package/dist/assets/{appFetch-Ca9DNQQ2.js → appFetch-BQO5Pk7W.js} +2 -2
  8. package/dist/assets/{appFetch-Ca9DNQQ2.js.map → appFetch-BQO5Pk7W.js.map} +1 -1
  9. package/dist/assets/{apps-gXpGFPiv.js → apps-B88rYWRm.js} +45 -19
  10. package/dist/assets/apps-B88rYWRm.js.map +1 -0
  11. package/dist/assets/{apps-uZTu5FlR.js → apps-D4KRpZB4.js} +1 -1
  12. package/dist/assets/{backup-Di2U6QVH.js → backup-BeXBtbVL.js} +1 -1
  13. package/dist/assets/{backup-CxH3auHB.js → backup-CNLY905d.js} +10 -5
  14. package/dist/assets/backup-CNLY905d.js.map +1 -0
  15. package/dist/assets/bark_ffi_wasm_bg-CjjCN3FD.js +5 -0
  16. package/dist/assets/{bitcoind-BpZn3bRN.js → bitcoind-BXZdQ1wZ.js} +2 -2
  17. package/dist/assets/{bitcoind-BpZn3bRN.js.map → bitcoind-BXZdQ1wZ.js.map} +1 -1
  18. package/dist/assets/bitcoindevkit_bg-D6IFIaJi.js +5 -0
  19. package/dist/assets/{buttons-D4udKEvT.js → buttons-BKojbx0Q.js} +4 -4
  20. package/dist/assets/{buttons-D4udKEvT.js.map → buttons-BKojbx0Q.js.map} +1 -1
  21. package/dist/assets/buttons-_5Fw8uHn.js +1 -0
  22. package/dist/assets/{client-DGx7E8b3.js → client-BtPhfWjK.js} +2 -2
  23. package/dist/assets/{client-DGx7E8b3.js.map → client-BtPhfWjK.js.map} +1 -1
  24. package/dist/assets/{fedimintSdk-D-6H9Pe7.js → fedimintSdk-eLXBrj9i.js} +2 -2
  25. package/dist/assets/{fedimintSdk-D-6H9Pe7.js.map → fedimintSdk-eLXBrj9i.js.map} +1 -1
  26. package/dist/assets/fedimint_client_wasm_bg-CRQlnJdu.js +5 -0
  27. package/dist/assets/{fixedHosts-CODN2pTL.js → fixedHosts-BJFL8Ell.js} +3 -3
  28. package/dist/assets/{fixedHosts-CODN2pTL.js.map → fixedHosts-BJFL8Ell.js.map} +1 -1
  29. package/dist/assets/{groupMeta-CeZpqVVC.js → groupMeta-Cz95F6sX.js} +98 -9
  30. package/dist/assets/groupMeta-Cz95F6sX.js.map +1 -0
  31. package/dist/assets/{host-CRA7h5A2.js → host-BsBWxTtW.js} +172 -22
  32. package/dist/assets/host-BsBWxTtW.js.map +1 -0
  33. package/dist/assets/{identityProofs-B8zCp5_0.js → identityProofs-zI_Iy9uI.js} +15 -1
  34. package/dist/assets/identityProofs-zI_Iy9uI.js.map +1 -0
  35. package/dist/assets/{irohWeb-mnOi2Jp7.js → irohWeb-BLDcHwYz.js} +50 -13
  36. package/dist/assets/irohWeb-BLDcHwYz.js.map +1 -0
  37. package/dist/assets/{main-Chc871HV.js → main-D48Ben7C.js} +68 -28
  38. package/dist/assets/main-D48Ben7C.js.map +1 -0
  39. package/dist/assets/{ownDid-BOuSlDv-.js → ownDid-CwxFBzOH.js} +5 -4
  40. package/dist/assets/ownDid-CwxFBzOH.js.map +1 -0
  41. package/dist/assets/{ownStatus-CLA1VPkL.js → ownStatus-DjD0sQKF.js} +34 -11
  42. package/dist/assets/ownStatus-DjD0sQKF.js.map +1 -0
  43. package/dist/assets/{profiles-BH1bH9aE.js → profiles-BAJf0c9E.js} +33 -8
  44. package/dist/assets/profiles-BAJf0c9E.js.map +1 -0
  45. package/dist/assets/{registry-DAq16T-V.js → registry-CHwHTWbK.js} +1 -1
  46. package/dist/assets/{registry-DP_-RA-v.js → registry-CPuexTle.js} +25 -13
  47. package/dist/assets/registry-CPuexTle.js.map +1 -0
  48. package/dist/assets/{resolve-B1eQ4dVQ.js → resolve-BqmGXKn4.js} +2 -2
  49. package/dist/assets/{resolve-B1eQ4dVQ.js.map → resolve-BqmGXKn4.js.map} +1 -1
  50. package/dist/assets/{server-Bd2ofxc1.js → server-e4oKeNez.js} +399 -88
  51. package/dist/assets/server-e4oKeNez.js.map +1 -0
  52. package/dist/assets/{sshForges-As7dbGXM.js → sshForges-Cqt3qfWe.js} +2 -2
  53. package/dist/assets/{sshForges-As7dbGXM.js.map → sshForges-Cqt3qfWe.js.map} +1 -1
  54. package/dist/assets/translate-DnuQMfj0.js.map +1 -1
  55. package/dist/assets/{waitNotice-BHuMofaH.js → waitNotice-mB0Ym145.js} +2 -2
  56. package/dist/assets/{waitNotice-BHuMofaH.js.map → waitNotice-mB0Ym145.js.map} +1 -1
  57. package/dist/ghostly.mjs +1 -1
  58. package/npm-shrinkwrap.json +2 -2
  59. package/package.json +1 -1
  60. package/dist/assets/api-O57SNJcb.js.map +0 -1
  61. package/dist/assets/apiKit-CPTFJHA7.js.map +0 -1
  62. package/dist/assets/apps-gXpGFPiv.js.map +0 -1
  63. package/dist/assets/backup-CxH3auHB.js.map +0 -1
  64. package/dist/assets/bark_ffi_wasm_bg-CcafsJla.js +0 -5
  65. package/dist/assets/bark_ffi_wasm_bg-CcafsJla.js.map +0 -1
  66. package/dist/assets/bark_ffi_wasm_bg-VNmaJsc9.wasm +0 -0
  67. package/dist/assets/bitcoindevkit_bg-BWQHx7vd.js +0 -5
  68. package/dist/assets/bitcoindevkit_bg-BWQHx7vd.js.map +0 -1
  69. package/dist/assets/bitcoindevkit_bg-DXil67yS.wasm +0 -0
  70. package/dist/assets/buttons-YTYEqvGC.js +0 -1
  71. package/dist/assets/fedimint_client_wasm_bg-Cr5xGD-w.js +0 -5
  72. package/dist/assets/fedimint_client_wasm_bg-Cr5xGD-w.js.map +0 -1
  73. package/dist/assets/fedimint_client_wasm_bg-DeFtkIv6.wasm +0 -0
  74. package/dist/assets/groupMeta-CeZpqVVC.js.map +0 -1
  75. package/dist/assets/host-CRA7h5A2.js.map +0 -1
  76. package/dist/assets/identityProofs-B8zCp5_0.js.map +0 -1
  77. package/dist/assets/irohWeb-mnOi2Jp7.js.map +0 -1
  78. package/dist/assets/main-Chc871HV.js.map +0 -1
  79. package/dist/assets/ownDid-BOuSlDv-.js.map +0 -1
  80. package/dist/assets/ownStatus-CLA1VPkL.js.map +0 -1
  81. package/dist/assets/profiles-BH1bH9aE.js.map +0 -1
  82. package/dist/assets/registry-DP_-RA-v.js.map +0 -1
  83. package/dist/assets/server-Bd2ofxc1.js.map +0 -1
@@ -1,18 +1,19 @@
1
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";
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-Dz2cw7WD.js";
3
+ import { L as sinceSeq, o as dhtOnly, s as findChat, u as findOwnNamedChat } from "./apiKit-DAFsWtyk.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-BAJf0c9E.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-BtPhfWjK.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-Cz95F6sX.js";
8
+ import { g as parsePayLink, r as irohRelayProblem } from "./irohWeb-BLDcHwYz.js";
9
9
  import { i as fromBase64Url } from "./bytes-C1DNGY18.js";
10
10
  import { u as setLinkTraceSink } from "./relay-gJTOYQJZ.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-B88rYWRm.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
  }
@@ -5165,7 +5278,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5165
5278
  * A committed transaction's entry, numbered `entry` (its first op says so, where there are trees), from the
5166
5279
  * transaction whose rollback log is `owner`. Written at once, it may fail (a full disk): the reason is given back,
5167
5280
  * 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
5281
+ * "queued": written behind what the queue holds (a fold, a Blob's entry), and `complete` called once a sync covers it: what
5169
5282
  * completed before was on no disk while the fold waited for the sync in flight, and a process killed then lost a
5170
5283
  * text the engine had told the contact it held.
5171
5284
  */
@@ -5192,14 +5305,27 @@ async function openPersistentIndexedDb(dir, options = {}) {
5192
5305
  }
5193
5306
  trees?.complete(owner, entry);
5194
5307
  held++;
5308
+ const done = () => {
5309
+ held--;
5310
+ complete();
5311
+ };
5312
+ let sharing = false;
5195
5313
  enqueue(async () => {
5196
5314
  try {
5197
5315
  if (entry <= foldedUpTo) return;
5198
5316
  if (writeEntry(serialize(await Promise.all(ops.map(async (op) => op.kind === "put" ? {
5199
5317
  ...op,
5200
5318
  value: await blobsToDisk(op.value)
5201
- } : op))), lazy ? "lazy" : "now")) {
5319
+ } : op))), lazy ? "lazy" : "shared")) {
5202
5320
  holdsDelete ||= deleting;
5321
+ if (!lazy) {
5322
+ sharing = true;
5323
+ shareSync(() => {
5324
+ written(entry);
5325
+ done();
5326
+ });
5327
+ return;
5328
+ }
5203
5329
  written(entry);
5204
5330
  }
5205
5331
  } catch (error) {
@@ -5207,8 +5333,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5207
5333
  await writeSnapshot().catch(failed);
5208
5334
  }
5209
5335
  }).then(() => {
5210
- held--;
5211
- complete();
5336
+ if (!sharing) done();
5212
5337
  });
5213
5338
  return "queued";
5214
5339
  }
@@ -5422,6 +5547,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5422
5547
  * holds, the transactions whose layers go to the trees with it.
5423
5548
  */
5424
5549
  async function capture(journalName) {
5550
+ await unwritten();
5425
5551
  const snapshot = {
5426
5552
  format: 1,
5427
5553
  journal: journalName,
@@ -5471,6 +5597,7 @@ async function openPersistentIndexedDb(dir, options = {}) {
5471
5597
  async function exportTo(file) {
5472
5598
  const databases = [];
5473
5599
  let reader = null;
5600
+ await unwritten();
5474
5601
  try {
5475
5602
  for (const db of raw._databases.values()) {
5476
5603
  if (db.name === PROBE) continue;
@@ -5612,6 +5739,26 @@ async function openPersistentIndexedDb(dir, options = {}) {
5612
5739
  quiet = busy() ? 0 : quiet + 1;
5613
5740
  }
5614
5741
  }
5742
+ /**
5743
+ * Resolves in a turn where no running transaction has written to a store held in memory (or after `CLOSE_WAIT_MS`):
5744
+ * fake-indexeddb puts its writes in the records at once and takes them back if it aborts. A fold read them meanwhile,
5745
+ * and its snapshot kept what the transaction never committed (a refused write, after a restart), or a part of what it
5746
+ * did (its other part in the journal entry a process killed then never wrote). One that commits meanwhile waits behind
5747
+ * the fold in the queue (`append`): the snapshot holds it, whole.
5748
+ */
5749
+ async function unwritten() {
5750
+ const until = Date.now() + CLOSE_WAIT_MS;
5751
+ while (Date.now() < until && writesHeld()) await new Promise((resolve) => setImmediate(resolve));
5752
+ }
5753
+ function writesHeld() {
5754
+ return [...raw._databases.values()].some((db) => db.transactions.some((transaction) => {
5755
+ const { _state, _rollbackLog } = transaction;
5756
+ return _state !== "finished" && !!pending.get(_rollbackLog)?.some((op) => {
5757
+ const store = db.rawObjectStores.get(op.store);
5758
+ return !store || !disk(store);
5759
+ });
5760
+ }));
5761
+ }
5615
5762
  /** Whether a transaction is running or waiting to, or for the sync or the queued write that tells the engine it completed. */
5616
5763
  function busy() {
5617
5764
  return syncing !== null || held > 0 || [...raw._databases.values()].some((db) => db.transactions.some((transaction) => transaction._state !== "finished"));
@@ -6789,7 +6936,7 @@ async function startRuntime(paths, options = {}) {
6789
6936
  const [{ registerFileBytes }, { NodeFileBytes }] = await Promise.all([import("./fileBytes-CCKbwqG_.js"), import("./fileBytes-tj6-VZcB.js")]);
6790
6937
  registerFileBytes("native", async () => new NodeFileBytes(paths.files), true);
6791
6938
  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")]);
6939
+ const [{ EngineServer }, { createHyperEndpoint }] = await Promise.all([import("./server-e4oKeNez.js"), import("./endpoint-Cvu-taMs.js")]);
6793
6940
  const mainline = dht.off ? null : new (await (import("./mainline-CDUXtV-I.js"))).Mainline({
6794
6941
  bootstrap: dht.bootstrap,
6795
6942
  host: dht.host
@@ -7051,7 +7198,10 @@ async function serve(host) {
7051
7198
  return {
7052
7199
  server,
7053
7200
  close: () => closing ??= new Promise((resolve) => {
7054
- server.close(() => resolve());
7201
+ server.close(() => {
7202
+ dropRuntimeFolder(path);
7203
+ resolve();
7204
+ });
7055
7205
  removeDaemonSecret(ctx.runtime.paths.dir);
7056
7206
  stopping.abort();
7057
7207
  within(Promise.all(answering), STOP_ANSWERS_MS).then(() => {
@@ -7260,4 +7410,4 @@ function connection(ctx, socket, secret, answering) {
7260
7410
  }
7261
7411
  export { BASELINE_NEWEST, MAX_UNREAD, STOP_ANSWERS_MS, keepServing, openHost, serve };
7262
7412
 
7263
- //# sourceMappingURL=host-CRA7h5A2.js.map
7413
+ //# sourceMappingURL=host-BsBWxTtW.js.map