engine7 7.1.137 → 7.1.140

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.
@@ -0,0 +1,131 @@
1
+ const require=(await import('node:module')).createRequire(import.meta.url)
2
+
3
+ // src/continuity/resource-main.ts
4
+ import { readFileSync as readFileSync3 } from "node:fs";
5
+ import { resolve as resolve2 } from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+
8
+ // src/continuity/key-service-store.ts
9
+ import { DatabaseSync } from "node:sqlite";
10
+ import { constants, lstatSync, mkdirSync, openSync, closeSync, readFileSync, writeSync, fstatSync, fsyncSync, unlinkSync } from "node:fs";
11
+ function assertPrivateStat(stat, directory) {
12
+ if ((directory ? !stat.isDirectory() : !stat.isFile()) || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.()))
13
+ throw new Error("KEY_SERVICE_PRIVATE_PATH_REQUIRED");
14
+ }
15
+ function readPrivateFile(path, maximumBytes = 65536) {
16
+ assertPrivateStat(lstatSync(path), false);
17
+ const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
18
+ try {
19
+ const stat = fstatSync(fd);
20
+ assertPrivateStat(stat, false);
21
+ if (stat.size > maximumBytes) throw new Error("KEY_SERVICE_INVALID_FILE_SIZE");
22
+ return readFileSync(fd);
23
+ } finally {
24
+ closeSync(fd);
25
+ }
26
+ }
27
+
28
+ // src/continuity/retirement.ts
29
+ import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
30
+ import { createHash } from "node:crypto";
31
+ import { existsSync } from "node:fs";
32
+ import { accessSync, constants as constants3, lstatSync as lstatSync3, readdirSync, readFileSync as readFileSync2, readlinkSync, realpathSync as realpathSync2, rmSync } from "node:fs";
33
+ import { dirname, isAbsolute, join, resolve, relative } from "node:path";
34
+
35
+ // src/continuity/publication-objects.ts
36
+ import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
37
+ import {
38
+ constants as constants2,
39
+ openSync as openSync2,
40
+ closeSync as closeSync2,
41
+ fstatSync as fstatSync2,
42
+ fsyncSync as fsyncSync2,
43
+ readSync,
44
+ lstatSync as lstatSync2,
45
+ realpathSync,
46
+ mkdirSync as mkdirSync2
47
+ } from "node:fs";
48
+ var PUBLICATION_CHUNK_BYTES = 1024 * 1024;
49
+ var MAGIC = Buffer.from("E7P1");
50
+ var INLINE_BYTES = 64 * 1024;
51
+ var noFollow = constants2.O_NOFOLLOW ?? 0;
52
+
53
+ // src/continuity/retirement.ts
54
+ var identity = (value) => JSON.stringify([value.tenantId, value.companionId, value.seatId]);
55
+ var sha = (value) => createHash("sha256").update(value).digest("hex");
56
+ var LocalSeatResource = class {
57
+ constructor(path, identity2) {
58
+ this.path = path;
59
+ this.identity = identity2;
60
+ if (!isAbsolute(path) || resolve(path) !== path || path.split("/").filter(Boolean).length < 3) throw new Error("TRAVEL_RETIREMENT_PATH");
61
+ this.id = "directory-" + sha(path);
62
+ }
63
+ id;
64
+ check() {
65
+ if (!existsSync(this.path)) return false;
66
+ const stat = lstatSync3(this.path);
67
+ if (!stat.isDirectory() || stat.isSymbolicLink() || realpathSync2(this.path) !== this.path || process.platform !== "win32" && stat.uid !== process.getuid()) throw new Error("TRAVEL_RETIREMENT_PATH");
68
+ if (identity(JSON.parse(readPrivateFile(join(this.path, ".travel-seat-resource.json"), 65536).toString())) !== identity(this.identity)) throw new Error("TRAVEL_RETIREMENT_IDENTITY");
69
+ return true;
70
+ }
71
+ async inventory() {
72
+ return this.inventoryNow();
73
+ }
74
+ inventoryNow() {
75
+ if (!this.check()) return [];
76
+ const result = [];
77
+ const visit = (directory) => {
78
+ for (const item of readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
79
+ const path = join(directory, item.name), version = relative(this.path, path);
80
+ if (item.isSymbolicLink()) result.push({ version, digest: sha(readlinkSync(path)) });
81
+ else if (item.isDirectory()) visit(path);
82
+ else if (item.isFile()) {
83
+ if (item.name === ".travel-seat-resource.json" && identity(JSON.parse(readPrivateFile(path, 65536).toString())) !== identity(this.identity)) throw new Error("TRAVEL_RETIREMENT_IDENTITY");
84
+ result.push({ version, digest: sha(readFileSync2(path)) });
85
+ } else throw new Error("TRAVEL_RETIREMENT_PATH");
86
+ if (result.length > 1e6) throw new Error("TRAVEL_RETIREMENT_BUDGET");
87
+ }
88
+ };
89
+ visit(this.path);
90
+ return result.length === 1 && result[0].version === ".travel-seat-resource.json" ? [] : result;
91
+ }
92
+ async purge(expectedInventory) {
93
+ const allowed = new Map(expectedInventory.map((item) => [item.version, item.digest]));
94
+ if (this.inventoryNow().some((item) => allowed.get(item.version) !== item.digest)) throw new Error("TRAVEL_RETIREMENT_RESOURCE_CHANGED");
95
+ if (this.check()) {
96
+ let removeRoot = true;
97
+ try {
98
+ accessSync(dirname(this.path), constants3.W_OK);
99
+ } catch (error) {
100
+ if (error.code !== "EACCES") throw error;
101
+ removeRoot = false;
102
+ }
103
+ if (removeRoot) rmSync(this.path, { recursive: true });
104
+ else for (const name of readdirSync(this.path)) if (name !== ".travel-seat-resource.json") rmSync(join(this.path, name), { recursive: true });
105
+ }
106
+ if (this.inventoryNow().length) throw new Error("TRAVEL_RETIREMENT_PENDING");
107
+ }
108
+ };
109
+
110
+ // src/continuity/resource-main.ts
111
+ async function runSeatResourceCli(args) {
112
+ if (args.length === 1 && args[0] === "--help") return { usage: "travel-resource inventory|purge --config /absolute/private-config.json" };
113
+ if (args.length !== 3 || !["inventory", "purge"].includes(args[0]) || args[1] !== "--config") throw new Error("TRAVEL_RETIREMENT_COMMAND");
114
+ const config = JSON.parse(readPrivateFile(resolve2(args[2]), 65536).toString());
115
+ const resource = new LocalSeatResource(config.directory, config.identity);
116
+ if (args[0] === "inventory") return resource.inventory();
117
+ const input = readFileSync3(0);
118
+ if (input.length > 64 * 1024 * 1024) throw new Error("TRAVEL_RETIREMENT_BUDGET");
119
+ const inventory = JSON.parse(input.toString());
120
+ if (!Array.isArray(inventory) || inventory.length > 1e6 || inventory.some((row) => !row || typeof row.version !== "string" || typeof row.digest !== "string" || !/^[a-f0-9]{64}$/.test(row.digest))) throw new Error("TRAVEL_RETIREMENT_REQUEST");
121
+ await resource.purge(inventory);
122
+ return { purged: true };
123
+ }
124
+ if (process.argv[1] && /[\/](resource-main\.ts|travel-resource\.mjs)$/.test(process.argv[1]) && resolve2(process.argv[1]) === fileURLToPath(import.meta.url)) runSeatResourceCli(process.argv.slice(2)).then((value) => process.stdout.write(JSON.stringify(value) + "\n")).catch((error) => {
125
+ const reason = error instanceof Error && /^TRAVEL_[A-Z_]+$/.test(error.message) ? error.message : ["EACCES", "EPERM", "ENOENT", "ENOSPC", "EIO"].includes(error?.code) ? error.code : "REFUSED";
126
+ process.stderr.write("TRAVEL_RETIREMENT_RESOURCE_REFUSED " + reason + "\n");
127
+ process.exitCode = 1;
128
+ });
129
+ export {
130
+ runSeatResourceCli
131
+ };
@@ -1,19 +1,78 @@
1
1
  const require=(await import('node:module')).createRequire(import.meta.url)
2
2
 
3
3
  // src/continuity/retirement-main.ts
4
- import { readFileSync as readFileSync6, existsSync as existsSync3 } from "node:fs";
5
- import { isAbsolute as isAbsolute3, resolve as resolve4 } from "node:path";
4
+ import { readFileSync as readFileSync5, existsSync as existsSync3 } from "node:fs";
5
+ import { isAbsolute as isAbsolute4, resolve as resolve5 } from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
7
  import { execFile } from "node:child_process";
8
8
  import { promisify } from "node:util";
9
- import { createHash as createHash5 } from "node:crypto";
9
+ import { createHash as createHash6 } from "node:crypto";
10
+
11
+ // src/continuity/user-seat-resource.ts
12
+ import { spawn } from "node:child_process";
13
+ import { createHash } from "node:crypto";
14
+ import { isAbsolute, resolve } from "node:path";
15
+ var UserSeatResource = class {
16
+ constructor(config) {
17
+ this.config = config;
18
+ if (!/^[a-z_][a-z0-9_-]{0,31}$/.test(config.runAs.user) || [config.directory, config.runAs.node, config.runAs.entry, config.runAs.configFile].some((path2) => !isAbsolute(path2) || resolve(path2) !== path2 || /[\r\n\0]/.test(path2))) throw new Error("TRAVEL_RETIREMENT_CONFIG");
19
+ this.identity = config.identity;
20
+ this.path = config.directory;
21
+ this.id = "directory-" + createHash("sha256").update(this.path).digest("hex");
22
+ }
23
+ id;
24
+ path;
25
+ identity;
26
+ call(action, input) {
27
+ return new Promise((done, reject) => {
28
+ const child = spawn("/usr/bin/sudo", ["-n", "/usr/sbin/runuser", "-u", this.config.runAs.user, "--", this.config.runAs.node, this.config.runAs.entry, action, "--config", this.config.runAs.configFile], { stdio: ["pipe", "pipe", "pipe"] });
29
+ const chunks = [];
30
+ let bytes = 0, stderr = "";
31
+ const timer = setTimeout(() => child.kill("SIGKILL"), 12e4);
32
+ child.stdin.on("error", () => {
33
+ });
34
+ child.stderr.on("data", (chunk) => {
35
+ stderr = (stderr + chunk.toString()).slice(-4096);
36
+ });
37
+ child.stdout.on("data", (chunk) => {
38
+ bytes += chunk.length;
39
+ if (bytes > 64 * 1024 * 1024) child.kill("SIGKILL");
40
+ else chunks.push(chunk);
41
+ });
42
+ child.once("error", reject);
43
+ child.once("close", (code) => {
44
+ clearTimeout(timer);
45
+ if (code !== 0) {
46
+ const reason = stderr.match(/TRAVEL_RETIREMENT_RESOURCE_REFUSED ([A-Z][A-Z0-9_]{0,95})(?:\s|$)/)?.[1];
47
+ if (reason) process.stderr.write("[travel-retirement-resource] " + JSON.stringify({ action, reason }) + "\n");
48
+ reject(new Error(reason?.startsWith("TRAVEL_") ? reason : "TRAVEL_RETIREMENT_PROVIDER_PENDING"));
49
+ return;
50
+ }
51
+ try {
52
+ done(JSON.parse(Buffer.concat(chunks).toString()));
53
+ } catch {
54
+ reject(new Error("TRAVEL_RETIREMENT_PROVIDER_PENDING"));
55
+ }
56
+ });
57
+ child.stdin.end(input === void 0 ? "" : JSON.stringify(input));
58
+ });
59
+ }
60
+ async inventory() {
61
+ const rows = await this.call("inventory");
62
+ if (!Array.isArray(rows) || rows.length > 1e6) throw new Error("TRAVEL_RETIREMENT_BUDGET");
63
+ return rows;
64
+ }
65
+ async purge(inventory) {
66
+ if ((await this.call("purge", inventory)).purged !== true) throw new Error("TRAVEL_RETIREMENT_PROVIDER_PENDING");
67
+ }
68
+ };
10
69
 
11
70
  // src/continuity/retirement.ts
12
71
  import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
13
- import { createHash } from "node:crypto";
72
+ import { createHash as createHash2 } from "node:crypto";
14
73
  import { existsSync } from "node:fs";
15
- import { lstatSync as lstatSync3, readdirSync, readFileSync as readFileSync3, readlinkSync, realpathSync as realpathSync2, rmSync } from "node:fs";
16
- import { isAbsolute, join as join3, resolve as resolve2, relative } from "node:path";
74
+ import { accessSync, constants as constants3, lstatSync as lstatSync3, readdirSync, readFileSync as readFileSync2, readlinkSync, realpathSync as realpathSync2, rmSync } from "node:fs";
75
+ import { dirname as dirname2, isAbsolute as isAbsolute2, join as join3, resolve as resolve3, relative } from "node:path";
17
76
 
18
77
  // src/continuity/publication-objects.ts
19
78
  import { DatabaseSync } from "node:sqlite";
@@ -24,22 +83,18 @@ import {
24
83
  fstatSync,
25
84
  fsyncSync,
26
85
  readSync,
27
- writeSync,
28
86
  lstatSync,
29
87
  realpathSync,
30
- mkdirSync,
31
- readFileSync,
32
- writeFileSync,
33
- linkSync,
34
- unlinkSync
88
+ mkdirSync
35
89
  } from "node:fs";
36
- import { join, dirname, resolve } from "node:path";
90
+ import { join, dirname, resolve as resolve2 } from "node:path";
37
91
  var PUBLICATION_CHUNK_BYTES = 1024 * 1024;
38
92
  var MAGIC = Buffer.from("E7P1");
93
+ var INLINE_BYTES = 64 * 1024;
39
94
  var noFollow = constants.O_NOFOLLOW ?? 0;
40
95
  function assertPrivateDirectory(path2) {
41
96
  const info = lstatSync(path2);
42
- if (!info.isDirectory() || info.isSymbolicLink() || realpathSync(path2) !== resolve(path2) || process.platform !== "win32" && (info.mode & 63 || info.uid !== process.getuid()))
97
+ if (!info.isDirectory() || info.isSymbolicLink() || realpathSync(path2) !== resolve2(path2) || process.platform !== "win32" && (info.mode & 63 || info.uid !== process.getuid()))
43
98
  throw new Error("PUBLICATION_PRIVATE_DIRECTORY_REQUIRED");
44
99
  }
45
100
  function syncDir(path2) {
@@ -53,7 +108,7 @@ function syncDir(path2) {
53
108
  }
54
109
  function preparePrivateDirectory(path2) {
55
110
  const missing = [];
56
- let parent = resolve(path2);
111
+ let parent = resolve2(path2);
57
112
  for (; ; ) {
58
113
  try {
59
114
  lstatSync(parent);
@@ -100,7 +155,7 @@ function preparePrivateDatabase(path2) {
100
155
 
101
156
  // src/continuity/key-service-store.ts
102
157
  import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
103
- import { constants as constants2, lstatSync as lstatSync2, mkdirSync as mkdirSync2, openSync as openSync2, closeSync as closeSync2, readFileSync as readFileSync2, writeSync as writeSync2, fstatSync as fstatSync2, fsyncSync as fsyncSync2, unlinkSync as unlinkSync2 } from "node:fs";
158
+ import { constants as constants2, lstatSync as lstatSync2, mkdirSync as mkdirSync2, openSync as openSync2, closeSync as closeSync2, readFileSync, writeSync, fstatSync as fstatSync2, fsyncSync as fsyncSync2, unlinkSync } from "node:fs";
104
159
  import { join as join2 } from "node:path";
105
160
  import { randomUUID } from "node:crypto";
106
161
  function validateKeyIdentity(value) {
@@ -135,7 +190,7 @@ function readPrivateFile(path2, maximumBytes = 65536) {
135
190
  const stat = fstatSync2(fd);
136
191
  assertPrivateStat(stat, false);
137
192
  if (stat.size > maximumBytes) throw new Error("KEY_SERVICE_INVALID_FILE_SIZE");
138
- return readFileSync2(fd);
193
+ return readFileSync(fd);
139
194
  } finally {
140
195
  closeSync2(fd);
141
196
  }
@@ -199,11 +254,11 @@ var KeyServiceStore = class {
199
254
  const fd = openSync2(path2, constants2.O_WRONLY | constants2.O_CREAT | constants2.O_EXCL, 384);
200
255
  try {
201
256
  let written = 0;
202
- while (written < key.byteLength) written += writeSync2(fd, key, written, key.byteLength - written);
257
+ while (written < key.byteLength) written += writeSync(fd, key, written, key.byteLength - written);
203
258
  fsyncSync2(fd);
204
259
  } catch (error) {
205
260
  closeSync2(fd);
206
- unlinkSync2(path2);
261
+ unlinkSync(path2);
207
262
  throw error;
208
263
  }
209
264
  closeSync2(fd);
@@ -218,7 +273,7 @@ var KeyServiceStore = class {
218
273
  try {
219
274
  this.db.prepare("INSERT INTO seat_keys(key_id,identity,secret_file,state) VALUES(?,?,?,'active')").run(identity3.keyId, text, filename);
220
275
  } catch (error) {
221
- unlinkSync2(path2);
276
+ unlinkSync(path2);
222
277
  throw error;
223
278
  }
224
279
  });
@@ -295,7 +350,7 @@ var KeyServiceStore = class {
295
350
  const file = join2(this.secrets, filename);
296
351
  try {
297
352
  assertPrivateStat(lstatSync2(file), false);
298
- unlinkSync2(file);
353
+ unlinkSync(file);
299
354
  } catch (error) {
300
355
  if (error.code !== "ENOENT") throw error;
301
356
  }
@@ -327,12 +382,12 @@ var KeyServiceStore = class {
327
382
 
328
383
  // src/continuity/retirement.ts
329
384
  var identity = (value) => JSON.stringify([value.tenantId, value.companionId, value.seatId]);
330
- var sha = (value) => createHash("sha256").update(value).digest("hex");
385
+ var sha = (value) => createHash2("sha256").update(value).digest("hex");
331
386
  var LocalSeatResource = class {
332
387
  constructor(path2, identity3) {
333
388
  this.path = path2;
334
389
  this.identity = identity3;
335
- if (!isAbsolute(path2) || resolve2(path2) !== path2 || path2.split("/").filter(Boolean).length < 3) throw new Error("TRAVEL_RETIREMENT_PATH");
390
+ if (!isAbsolute2(path2) || resolve3(path2) !== path2 || path2.split("/").filter(Boolean).length < 3) throw new Error("TRAVEL_RETIREMENT_PATH");
336
391
  this.id = "directory-" + sha(path2);
337
392
  }
338
393
  id;
@@ -356,19 +411,29 @@ var LocalSeatResource = class {
356
411
  else if (item.isDirectory()) visit(path2);
357
412
  else if (item.isFile()) {
358
413
  if (item.name === ".travel-seat-resource.json" && identity(JSON.parse(readPrivateFile(path2, 65536).toString())) !== identity(this.identity)) throw new Error("TRAVEL_RETIREMENT_IDENTITY");
359
- result.push({ version, digest: sha(readFileSync3(path2)) });
414
+ result.push({ version, digest: sha(readFileSync2(path2)) });
360
415
  } else throw new Error("TRAVEL_RETIREMENT_PATH");
361
416
  if (result.length > 1e6) throw new Error("TRAVEL_RETIREMENT_BUDGET");
362
417
  }
363
418
  };
364
419
  visit(this.path);
365
- return result;
420
+ return result.length === 1 && result[0].version === ".travel-seat-resource.json" ? [] : result;
366
421
  }
367
422
  async purge(expectedInventory) {
368
423
  const allowed = new Map(expectedInventory.map((item) => [item.version, item.digest]));
369
424
  if (this.inventoryNow().some((item) => allowed.get(item.version) !== item.digest)) throw new Error("TRAVEL_RETIREMENT_RESOURCE_CHANGED");
370
- if (this.check()) rmSync(this.path, { recursive: true });
371
- if (existsSync(this.path)) throw new Error("TRAVEL_RETIREMENT_PENDING");
425
+ if (this.check()) {
426
+ let removeRoot = true;
427
+ try {
428
+ accessSync(dirname2(this.path), constants3.W_OK);
429
+ } catch (error) {
430
+ if (error.code !== "EACCES") throw error;
431
+ removeRoot = false;
432
+ }
433
+ if (removeRoot) rmSync(this.path, { recursive: true });
434
+ else for (const name of readdirSync(this.path)) if (name !== ".travel-seat-resource.json") rmSync(join3(this.path, name), { recursive: true });
435
+ }
436
+ if (this.inventoryNow().length) throw new Error("TRAVEL_RETIREMENT_PENDING");
372
437
  }
373
438
  };
374
439
  var TravelRetirement = class {
@@ -430,10 +495,10 @@ var TravelRetirement = class {
430
495
  if (required) throw new Error("TRAVEL_RETIREMENT_RECOVERY_REQUIRED");
431
496
  return null;
432
497
  }
433
- if (recovered.loaded !== true || identity(recovered.identity) !== identity(this.options.identity) || !isAbsolute(recovered.ordinaryStateDir) || ![recovered.checkpointDigest, recovered.channelReceiptsDigest].every((value) => /^[a-f0-9]{64}$/.test(value))) throw new Error("TRAVEL_RETIREMENT_RECOVERY_REQUIRED");
498
+ if (recovered.loaded !== true || identity(recovered.identity) !== identity(this.options.identity) || !isAbsolute2(recovered.ordinaryStateDir) || ![recovered.checkpointDigest, recovered.channelReceiptsDigest].every((value) => /^[a-f0-9]{64}$/.test(value))) throw new Error("TRAVEL_RETIREMENT_RECOVERY_REQUIRED");
434
499
  for (const resource of this.options.resources) {
435
500
  if (resource.path) {
436
- const local = resolve2(recovered.ordinaryStateDir), path2 = resolve2(resource.path), journal = resolve2(this.options.directory);
501
+ const local = resolve3(recovered.ordinaryStateDir), path2 = resolve3(resource.path), journal = resolve3(this.options.directory);
437
502
  if ([local, journal].some((p) => p === path2 || p.startsWith(path2 + "/") || path2.startsWith(p + "/"))) throw new Error("TRAVEL_RETIREMENT_LOCAL_OVERLAP");
438
503
  }
439
504
  }
@@ -454,7 +519,8 @@ var TravelRetirement = class {
454
519
  row = this.db.prepare("SELECT * FROM resources WHERE id=?").get(resource.id);
455
520
  }
456
521
  const original = JSON.parse(String(row.inventory));
457
- if (current.some((item) => !original.some((known) => item.version === known.version && item.digest === known.digest))) throw new Error("TRAVEL_RETIREMENT_RESOURCE_CHANGED");
522
+ const allowed = new Map(original.map((item) => [item.version, item.digest]));
523
+ if (current.some((item) => allowed.get(item.version) !== item.digest)) throw new Error("TRAVEL_RETIREMENT_RESOURCE_CHANGED");
458
524
  await verifyRecovery(true);
459
525
  await resource.purge(original);
460
526
  if ((await resource.inventory()).length) throw new Error("TRAVEL_RETIREMENT_PROVIDER_PENDING");
@@ -480,15 +546,15 @@ var TravelRetirement = class {
480
546
  };
481
547
 
482
548
  // src/continuity/local-exit.ts
483
- import { cpSync, existsSync as existsSync2, mkdirSync as mkdirSync5, readFileSync as readFileSync5, readdirSync as readdirSync3, realpathSync as realpathSync4, rmSync as rmSync2, writeFileSync as writeFileSync3 } from "node:fs";
549
+ import { cpSync, existsSync as existsSync2, mkdirSync as mkdirSync5, readFileSync as readFileSync4, readdirSync as readdirSync3, realpathSync as realpathSync4, rmSync as rmSync2, writeFileSync as writeFileSync2 } from "node:fs";
484
550
  import { DatabaseSync as DatabaseSync5 } from "node:sqlite";
485
- import { isAbsolute as isAbsolute2, join as join6, dirname as dirname3, resolve as resolve3 } from "node:path";
486
- import { createHash as createHash4, verify as verify2 } from "node:crypto";
551
+ import { isAbsolute as isAbsolute3, join as join6, dirname as dirname4, resolve as resolve4 } from "node:path";
552
+ import { createHash as createHash5, verify as verify2 } from "node:crypto";
487
553
 
488
554
  // src/continuity/runtime-lock.ts
489
555
  import { randomUUID as randomUUID2 } from "node:crypto";
490
556
  import { AsyncLocalStorage } from "node:async_hooks";
491
- import { closeSync as closeSync3, lstatSync as lstatSync4, mkdirSync as mkdirSync3, openSync as openSync3, readFileSync as readFileSync4, realpathSync as realpathSync3 } from "node:fs";
557
+ import { closeSync as closeSync3, lstatSync as lstatSync4, mkdirSync as mkdirSync3, openSync as openSync3, readFileSync as readFileSync3, realpathSync as realpathSync3 } from "node:fs";
492
558
  import { join as join4 } from "node:path";
493
559
  import { createRequire } from "node:module";
494
560
  var sqlite;
@@ -563,7 +629,7 @@ async function withCheckpointLock(stateDir, operation) {
563
629
  for (const name of [".engine.pid", ".bg-worker.pid"]) {
564
630
  let value;
565
631
  try {
566
- value = readFileSync4(join4(stateDir, name), "utf8").trim();
632
+ value = readFileSync3(join4(stateDir, name), "utf8").trim();
567
633
  } catch (error) {
568
634
  if (error.code === "ENOENT") continue;
569
635
  throw new Error("WRITER_STATE_UNKNOWN", { cause: error });
@@ -585,15 +651,15 @@ async function withCheckpointLock(stateDir, operation) {
585
651
 
586
652
  // src/continuity/local-dedupe.ts
587
653
  import { DatabaseSync as DatabaseSync4 } from "node:sqlite";
588
- import { createHash as createHash2 } from "node:crypto";
589
- import { dirname as dirname2 } from "node:path";
590
- var sha2 = (value) => createHash2("sha256").update(value).digest("hex");
654
+ import { createHash as createHash3 } from "node:crypto";
655
+ import { dirname as dirname3 } from "node:path";
656
+ var sha2 = (value) => createHash3("sha256").update(value).digest("hex");
591
657
  var hash = (value) => typeof value === "string" && /^[a-f0-9]{64}$/.test(value);
592
658
  var PlatformReceiptStore = class {
593
659
  db;
594
660
  constructor(file, namespace) {
595
661
  if (!namespace || namespace.length > 1024) throw new Error("PLATFORM_RECEIPT_IDENTITY");
596
- preparePrivateDirectory(dirname2(file));
662
+ preparePrivateDirectory(dirname3(file));
597
663
  preparePrivateDatabase(file);
598
664
  this.db = new DatabaseSync4(file);
599
665
  try {
@@ -653,10 +719,10 @@ var PlatformReceiptStore = class {
653
719
  };
654
720
 
655
721
  // src/continuity/local-exit-proof.ts
656
- import { createHash as createHash3, verify } from "node:crypto";
657
- import { createReadStream, mkdirSync as mkdirSync4, readdirSync as readdirSync2, writeFileSync as writeFileSync2 } from "node:fs";
722
+ import { createHash as createHash4, verify } from "node:crypto";
723
+ import { createReadStream, mkdirSync as mkdirSync4, readdirSync as readdirSync2, writeFileSync } from "node:fs";
658
724
  import { join as join5, relative as relative2 } from "node:path";
659
- var hash2 = (value) => createHash3("sha256").update(value).digest("hex");
725
+ var hash2 = (value) => createHash4("sha256").update(value).digest("hex");
660
726
  var equal = (a, b) => JSON.stringify(a) === JSON.stringify(b);
661
727
  function copyLocalExitPath(source, path2) {
662
728
  const name = relative2(source, path2).replaceAll("\\", "/");
@@ -674,7 +740,7 @@ async function localExitFilesDigest(root, ordinary = false) {
674
740
  continue;
675
741
  }
676
742
  if (!entry.isFile() || files.length >= 1e6) throw new Error("TRAVEL_EXIT_RECOVERY_REQUIRED");
677
- const digest = createHash3("sha256");
743
+ const digest = createHash4("sha256");
678
744
  for await (const chunk of createReadStream(file)) digest.update(chunk);
679
745
  files.push({ path: relative2(root, file).replaceAll("\\", "/"), sha256: digest.digest("hex") });
680
746
  }
@@ -714,7 +780,7 @@ async function verifyStoppedLocalExitProof(options) {
714
780
 
715
781
  // src/continuity/local-exit.ts
716
782
  var identity2 = (value) => JSON.stringify([value.tenantId, value.companionId, value.seatId]);
717
- var sha3 = (value) => createHash4("sha256").update(value).digest("hex");
783
+ var sha3 = (value) => createHash5("sha256").update(value).digest("hex");
718
784
  function detachReplication(directory) {
719
785
  for (const entry of readdirSync3(directory, { withFileTypes: true })) {
720
786
  const file = join6(directory, entry.name);
@@ -742,9 +808,9 @@ function detachReplication(directory) {
742
808
  }
743
809
  }
744
810
  async function createOrdinaryLocalRecovery(options) {
745
- const source = resolve3(options.stateDir), destination = resolve3(options.destination);
746
- if (!isAbsolute2(options.destination) || source !== options.stateDir || destination !== options.destination || realpathSync4(source) !== source || destination === source || destination.startsWith(source + "/") || source.startsWith(destination + "/") || realpathSync4(dirname3(destination)) !== dirname3(destination)) throw new Error("TRAVEL_EXIT_PATH");
747
- const activation = JSON.parse(readFileSync5(join6(source, ".travel-active.json"), "utf8")), permit = activation.permit, { signature, ...signed } = permit;
811
+ const source = resolve4(options.stateDir), destination = resolve4(options.destination);
812
+ if (!isAbsolute3(options.destination) || source !== options.stateDir || destination !== options.destination || realpathSync4(source) !== source || destination === source || destination.startsWith(source + "/") || source.startsWith(destination + "/") || realpathSync4(dirname4(destination)) !== dirname4(destination)) throw new Error("TRAVEL_EXIT_PATH");
813
+ const activation = JSON.parse(readFileSync4(join6(source, ".travel-active.json"), "utf8")), permit = activation.permit, { signature, ...signed } = permit;
748
814
  if (activation.format !== "engine7-runtime-activation-v1" || identity2(activation.identity) !== identity2(options.identity) || identity2(permit) !== identity2(options.identity) || permit.nodeId !== options.localNodeId || activation.nodeId !== options.localNodeId || !verify2(null, Buffer.from(JSON.stringify(signed)), options.publicKey, Buffer.from(signature, "base64")) || identity2(options.dedupeReceipt.identity) !== identity2(options.identity) || options.dedupeReceipt.checkpointDigest !== options.checkpointDigest || options.dedupeReceipt.deletionWatermark !== permit.deletionWatermark) throw new Error("TRAVEL_EXIT_RECOVERY_REQUIRED");
749
815
  return withCheckpointLock(source, async () => {
750
816
  if (!options.authority || !options.principal || options.authority.publicKey !== options.publicKey) throw new Error("TRAVEL_EXIT_RECOVERY_REQUIRED");
@@ -786,7 +852,7 @@ async function createOrdinaryLocalRecovery(options) {
786
852
  await proof.check();
787
853
  const body = { ...receipt, ordinaryFilesDigest: await localExitFilesDigest(destination, true) };
788
854
  const result = { ...body, digest: sha3(JSON.stringify(body)) };
789
- writeFileSync3(join6(destination, ".continuity/travel-exit-receipt.json"), JSON.stringify(result), { mode: 384, flag: "wx", flush: true });
855
+ writeFileSync2(join6(destination, ".continuity/travel-exit-receipt.json"), JSON.stringify(result), { mode: 384, flag: "wx", flush: true });
790
856
  return result;
791
857
  } catch (error) {
792
858
  if (existsSync2(destination)) rmSync2(destination, { recursive: true });
@@ -832,9 +898,12 @@ var TravelReportClient = class {
832
898
  this.timeout = options.timeoutMs ?? 8e3;
833
899
  if (!Number.isSafeInteger(this.timeout) || this.timeout < 1 || this.timeout > 6e4) throw new Error("TRAVEL_CONTROL_INVALID_CONFIG");
834
900
  this.key = options.key ? Buffer.from(options.key) : void 0;
835
- this.agent = new Agent({ ca: options.ca, cert: options.cert, key: this.key, minVersion: "TLSv1.3", rejectUnauthorized: true, keepAlive: true, maxSockets: 1 });
901
+ const tls = { ca: options.ca, cert: options.cert, key: this.key, minVersion: "TLSv1.3", rejectUnauthorized: true, maxSockets: 1 };
902
+ this.agent = new Agent({ ...tls, keepAlive: true });
903
+ this.mutationAgent = new Agent({ ...tls, keepAlive: false });
836
904
  }
837
905
  agent;
906
+ mutationAgent;
838
907
  key;
839
908
  url;
840
909
  timeout;
@@ -851,7 +920,7 @@ var TravelReportClient = class {
851
920
  const unavailable = () => new Error(this.closed ? "TRAVEL_CONTROL_CLOSED" : "TRAVEL_CONTROL_UNAVAILABLE");
852
921
  const remaining = deadline - performance.now();
853
922
  if (remaining <= 0) return Promise.reject(unavailable());
854
- return new Promise((resolve5, reject) => {
923
+ return new Promise((resolve6, reject) => {
855
924
  let complete = false, responseStarted = false;
856
925
  const finish = (error, value) => {
857
926
  if (complete) return;
@@ -859,9 +928,9 @@ var TravelReportClient = class {
859
928
  clearTimeout(timer);
860
929
  this.pending.delete(req);
861
930
  if (error) reject(error);
862
- else resolve5(value);
931
+ else resolve6(value);
863
932
  };
864
- const req = request(this.url, { method: "POST", agent: this.agent, headers: {
933
+ const req = request(this.url, { method: "POST", agent: readActions.has(input?.action) ? this.agent : this.mutationAgent, headers: {
865
934
  Authorization: "Bearer " + this.options.credential,
866
935
  "X-Engine7-Travel-Node": this.options.nodeId,
867
936
  "Content-Type": "application/json",
@@ -912,7 +981,7 @@ var TravelReportClient = class {
912
981
  complete = true;
913
982
  clearTimeout(timer);
914
983
  this.pending.delete(req);
915
- resolve5(this.attempt(input, false, deadline, body));
984
+ resolve6(this.attempt(input, false, deadline, body));
916
985
  } else finish(unavailable());
917
986
  });
918
987
  req.once("close", () => {
@@ -936,6 +1005,7 @@ var TravelReportClient = class {
936
1005
  this.closed = true;
937
1006
  for (const req of this.pending) req.destroy();
938
1007
  this.agent.destroy();
1008
+ this.mutationAgent.destroy();
939
1009
  this.key?.fill(0);
940
1010
  }
941
1011
  };
@@ -962,6 +1032,9 @@ var TravelManagementClient = class extends TravelReportClient {
962
1032
  if (url.pathname !== "/travel/manage/v1") throw new Error("TRAVEL_MANAGEMENT_ENDPOINT_REQUIRED");
963
1033
  super(options);
964
1034
  }
1035
+ async bindInitialCheckpoint(digest, buildManifestId) {
1036
+ await this.call({ action: "bindInitialCheckpoint", digest, buildManifestId });
1037
+ }
965
1038
  async verifyAncestor(ancestor, descendant) {
966
1039
  await this.call({ action: "verifyAncestor", ancestor, descendant });
967
1040
  }
@@ -983,6 +1056,9 @@ var TravelManagementClient = class extends TravelReportClient {
983
1056
  async assertPermit(permit, operation) {
984
1057
  await this.call({ action: "assertPermit", permit, operation });
985
1058
  }
1059
+ async assertPermitWithStatus(permit, operation) {
1060
+ return (await this.call({ action: "assertPermit", permit, operation })).status;
1061
+ }
986
1062
  async acquire(ready) {
987
1063
  return (await this.call({ action: "acquire", ready })).permit;
988
1064
  }
@@ -1014,9 +1090,9 @@ var TravelManagementClient = class extends TravelReportClient {
1014
1090
 
1015
1091
  // src/continuity/retirement-main.ts
1016
1092
  var command = promisify(execFile);
1017
- var sha4 = (value) => createHash5("sha256").update(value).digest("hex");
1093
+ var sha4 = (value) => createHash6("sha256").update(value).digest("hex");
1018
1094
  var path = (value) => {
1019
- if (typeof value !== "string" || !isAbsolute3(value) || resolve4(value) !== value) throw new Error("TRAVEL_RETIREMENT_PATH");
1095
+ if (typeof value !== "string" || !isAbsolute4(value) || resolve5(value) !== value) throw new Error("TRAVEL_RETIREMENT_PATH");
1020
1096
  return value;
1021
1097
  };
1022
1098
  async function runTravelRetirementCli(args) {
@@ -1024,7 +1100,7 @@ async function runTravelRetirementCli(args) {
1024
1100
  if (args.length !== 3 || !["run", "recover-local"].includes(args[0]) || args[1] !== "--config") throw new Error("TRAVEL_RETIREMENT_INVALID_COMMAND");
1025
1101
  const config = JSON.parse(readPrivateFile(path(args[2]), 4 * 1024 * 1024).toString());
1026
1102
  if (args[0] === "recover-local") {
1027
- const client2 = new TravelManagementClient({ ...config.control, ca: readFileSync6(path(config.tls.caFile)), cert: readFileSync6(path(config.tls.certFile)), key: readPrivateFile(path(config.tls.keyFile), 65536) });
1103
+ const client2 = new TravelManagementClient({ ...config.control, ca: readFileSync5(path(config.tls.caFile)), cert: readFileSync5(path(config.tls.certFile)), key: readPrivateFile(path(config.tls.keyFile), 65536) });
1028
1104
  try {
1029
1105
  const { principal, publicKey } = await client2.retirementStatus();
1030
1106
  if (publicKey !== config.publicKey) throw new Error("TRAVEL_EXIT_RECOVERY_REQUIRED");
@@ -1039,7 +1115,7 @@ async function runTravelRetirementCli(args) {
1039
1115
  }
1040
1116
  if (!Array.isArray(config.resources) || !Array.isArray(config.stopServices) || config.stopServices.some((service) => typeof service !== "string" || !/^engine7-travel[-a-zA-Z0-9_.@]+\.service$/.test(service))) throw new Error("TRAVEL_RETIREMENT_CONFIG");
1041
1117
  if (!config.stopServices.length && config.nodesAlreadyStopped !== true) throw new Error("TRAVEL_RETIREMENT_STOP_REQUIRED");
1042
- const client = new TravelManagementClient({ ...config.control, ca: readFileSync6(path(config.tls.caFile)), cert: readFileSync6(path(config.tls.certFile)), key: readPrivateFile(path(config.tls.keyFile), 65536) });
1118
+ const client = new TravelManagementClient({ ...config.control, ca: readFileSync5(path(config.tls.caFile)), cert: readFileSync5(path(config.tls.certFile)), key: readPrivateFile(path(config.tls.keyFile), 65536) });
1043
1119
  let job, keys;
1044
1120
  try {
1045
1121
  const control = await client.retirementStatus();
@@ -1062,10 +1138,10 @@ async function runTravelRetirementCli(args) {
1062
1138
  jobId: config.jobId,
1063
1139
  retire: () => client.retire(config.jobId),
1064
1140
  stopNodes: async () => {
1065
- if (config.stopServices.length) await command("systemctl", ["stop", ...config.stopServices]);
1141
+ if (config.stopServices.length) await command(config.stopViaSudo === true ? "/usr/bin/sudo" : "systemctl", config.stopViaSudo === true ? ["-n", "/usr/bin/systemctl", "stop", ...config.stopServices] : ["stop", ...config.stopServices]);
1066
1142
  },
1067
1143
  verifyLocalRecovery,
1068
- resources: config.resources.map((resource) => new LocalSeatResource(path(resource.directory), config.identity)),
1144
+ resources: config.resources.map((resource) => resource.runAs ? new UserSeatResource({ ...resource, identity: config.identity }) : new LocalSeatResource(path(resource.directory), config.identity)),
1069
1145
  revokeKey: async () => {
1070
1146
  keys.revoke(config.keyIdentity);
1071
1147
  return { revoked: true };
@@ -1080,7 +1156,7 @@ async function runTravelRetirementCli(args) {
1080
1156
  client.close();
1081
1157
  }
1082
1158
  }
1083
- if (process.argv[1] && /[\\/](retirement-main\.ts|travel-retire\.mjs)$/.test(fileURLToPath(import.meta.url)) && resolve4(process.argv[1]) === fileURLToPath(import.meta.url)) {
1159
+ if (process.argv[1] && /[\\/](retirement-main\.ts|travel-retire\.mjs)$/.test(fileURLToPath(import.meta.url)) && resolve5(process.argv[1]) === fileURLToPath(import.meta.url)) {
1084
1160
  runTravelRetirementCli(process.argv.slice(2)).then((result) => process.stdout.write(JSON.stringify(result) + "\n")).catch(() => {
1085
1161
  process.stderr.write("TRAVEL_RETIREMENT_REFUSED\n");
1086
1162
  process.exitCode = 1;