@hediet/linkrpc-cli 0.0.3-next.20261102.2 → 0.0.3-next.20261104.1

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.
package/dist/daemon.js CHANGED
@@ -463,6 +463,48 @@ async function createAutostartManager(options) {
463
463
  return new XdgAutostart(path$3.posix.join(configHome, "autostart", `${name}.desktop`), "LinkRPC Hub");
464
464
  }
465
465
  //#endregion
466
+ //#region ../linkrpc-hub-daemon/src/durableWrite.ts
467
+ /**
468
+ * Atomically replace `file` with `data`, durably: after a crash or power loss,
469
+ * the file holds either the old or the new content.
470
+ *
471
+ * A rename alone is not enough: file systems journal the rename (metadata)
472
+ * but may not have written the data yet, which leaves a file of the new size
473
+ * filled with zeros. The data is therefore flushed before the rename, and the
474
+ * directory entry after it (POSIX; Windows cannot open directories as files).
475
+ */
476
+ function writeFileDurably(file, data, options = {}) {
477
+ const tmp = `${file}.${process.pid}.tmp`;
478
+ try {
479
+ const fd = fs$3.openSync(tmp, "w", options.mode);
480
+ try {
481
+ fs$3.writeFileSync(fd, data);
482
+ fs$3.fsyncSync(fd);
483
+ } finally {
484
+ fs$3.closeSync(fd);
485
+ }
486
+ fs$3.renameSync(tmp, file);
487
+ } catch (error) {
488
+ fs$3.rmSync(tmp, { force: true });
489
+ throw error;
490
+ }
491
+ if (process.platform !== "win32") _fsyncDirectory(path$3.dirname(file));
492
+ }
493
+ function _fsyncDirectory(dir) {
494
+ const fd = fs$3.openSync(dir, "r");
495
+ try {
496
+ fs$3.fsyncSync(fd);
497
+ } catch (error) {
498
+ if (![
499
+ "EINVAL",
500
+ "ENOTSUP",
501
+ "EISDIR"
502
+ ].includes(error.code ?? "")) throw error;
503
+ } finally {
504
+ fs$3.closeSync(fd);
505
+ }
506
+ }
507
+ //#endregion
466
508
  //#region ../linkrpc-hub-daemon/src/discovery.ts
467
509
  /**
468
510
  * How to reach one daemon endpoint. The daemon writes one file for
@@ -492,15 +534,13 @@ function readEndpointDiscovery(file) {
492
534
  return;
493
535
  }
494
536
  }
495
- /** Atomically write a JSON file readable only by the current user. */
537
+ /** Atomically and durably write a JSON file readable only by the current user. */
496
538
  function writePrivateJson(file, value) {
497
539
  fs$3.mkdirSync(path$3.dirname(file), {
498
540
  recursive: true,
499
541
  mode: 448
500
542
  });
501
- const tmp = `${file}.${process.pid}.tmp`;
502
- fs$3.writeFileSync(tmp, JSON.stringify(value, null, 2) + "\n", { mode: 384 });
503
- fs$3.renameSync(tmp, file);
543
+ writeFileDurably(file, JSON.stringify(value, null, 2) + "\n", { mode: 384 });
504
544
  }
505
545
  //#endregion
506
546
  //#region ../linkrpc-hub-daemon/src/callLog/callQuery.ts
@@ -3331,7 +3371,7 @@ var ConfigConflictError = class extends Error {
3331
3371
  super(`${file} changed since it was read; reload and try again`);
3332
3372
  }
3333
3373
  };
3334
- /** The daemon config file: revisioned reads, validated atomic writes, and change watching. */
3374
+ /** The daemon config file: revisioned reads, validated atomic and durable writes, and change watching. */
3335
3375
  var ConfigFile = class {
3336
3376
  path;
3337
3377
  _schemaPath;
@@ -3370,9 +3410,7 @@ var ConfigFile = class {
3370
3410
  write(text, expectedRevision) {
3371
3411
  parseDaemonConfigText(text);
3372
3412
  if (this.read().revision !== expectedRevision) throw new ConfigConflictError(this.path);
3373
- const tmp = `${this.path}.${process.pid}.tmp`;
3374
- fs$3.writeFileSync(tmp, text);
3375
- fs$3.renameSync(tmp, this.path);
3413
+ writeFileDurably(this.path, text);
3376
3414
  return configRevision(text);
3377
3415
  }
3378
3416
  /** Invoke `onChange` (debounced) whenever the file may have changed. */
@@ -5145,6 +5183,8 @@ var HubDaemon = class HubDaemon {
5145
5183
  _watcher;
5146
5184
  _statusChangeScheduled = false;
5147
5185
  _stopping = false;
5186
+ /** While the hub restarts, its services are stopped and not yet re-added. */
5187
+ _restartingHub = false;
5148
5188
  _shutdown;
5149
5189
  _resolveDone;
5150
5190
  constructor(home, _lock, _log) {
@@ -5276,6 +5316,7 @@ var HubDaemon = class HubDaemon {
5276
5316
  });
5277
5317
  if (hubKey !== this._hubKey || !this._hub) {
5278
5318
  if (this._hub) this._log.line("hub settings changed; restarting the hub and all services");
5319
+ this._restartingHub = true;
5279
5320
  await this._disposeAllServices();
5280
5321
  const endpointSnapshot = this._endpoints?.snapshot();
5281
5322
  await this._endpoints?.dispose();
@@ -5325,6 +5366,7 @@ var HubDaemon = class HubDaemon {
5325
5366
  }
5326
5367
  const removed = [...this._services.keys()].filter((name) => !Object.hasOwn(config.services, name));
5327
5368
  await Promise.all(removed.map((name) => this._removeService(name)));
5369
+ this._restartingHub = false;
5328
5370
  await Promise.all(Object.entries(config.services).map(([name, service]) => this._applyService(name, service)));
5329
5371
  }
5330
5372
  async _applyService(name, service) {
@@ -5471,8 +5513,10 @@ var HubDaemon = class HubDaemon {
5471
5513
  }
5472
5514
  _service(name) {
5473
5515
  const service = this._services.get(name);
5474
- if (!service) throw new Error(`unknown service '${name}'`);
5475
- return service;
5516
+ if (service) return service;
5517
+ if (this._stopping) throw new Error("the hub daemon is shutting down");
5518
+ if (this._restartingHub) throw new Error("the hub is restarting; try again shortly");
5519
+ throw new Error(`unknown service '${name}'`);
5476
5520
  }
5477
5521
  _discovery(endpoint) {
5478
5522
  return {