@adea-ai/app-framework 0.31.0 → 0.32.0

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 (107) hide show
  1. package/README.md +26 -11
  2. package/dist/app/create-application.js +4 -3
  3. package/dist/app/serial-queue.d.ts +5 -0
  4. package/dist/app/serial-queue.js +14 -0
  5. package/dist/app/value-guards.d.ts +15 -0
  6. package/dist/app/value-guards.js +29 -0
  7. package/dist/ci/toolchain-identity.js +1 -1
  8. package/dist/desktop/channel-proof.js +2 -1
  9. package/dist/desktop/client-asset-stream.js +6 -12
  10. package/dist/desktop/desktop-host.d.ts +25 -5
  11. package/dist/desktop/desktop-host.js +75 -20
  12. package/dist/framework-source.json +1 -1
  13. package/dist/installer/command-artifact.js +4 -3
  14. package/dist/installer/command-qualification-command.js +6 -5
  15. package/dist/installer/hosted-runner.d.ts +9 -2
  16. package/dist/installer/hosted-runner.js +18 -5
  17. package/dist/installer/installed-payload.js +20 -46
  18. package/dist/installer/installer-archive.d.ts +33 -0
  19. package/dist/installer/installer-archive.js +116 -0
  20. package/dist/installer/installer-ci-bootstrap.js +10 -6
  21. package/dist/installer/installer-extraction.js +54 -120
  22. package/dist/installer/installer-identity.js +33 -111
  23. package/dist/installer/installer-observation.js +3 -2
  24. package/dist/installer/installer-process-cleanup.js +7 -46
  25. package/dist/installer/installer-qualification-command.js +6 -5
  26. package/dist/installer/installer-upgrade-command.js +5 -5
  27. package/dist/installer/installer-worker-guard.js +2 -1
  28. package/dist/native/canonical-path.d.ts +5 -0
  29. package/dist/native/canonical-path.js +10 -0
  30. package/dist/native/cef-helper.js +5 -4
  31. package/dist/native/electrobun-source-patch.js +3 -2
  32. package/dist/native/{directory-exchange.d.ts → native-rename.d.ts} +2 -0
  33. package/dist/native/native-rename.js +126 -0
  34. package/dist/native/native-secret-store.js +2 -6
  35. package/dist/native/native-utilities.js +4 -4
  36. package/dist/native/posix-launcher-build.js +1 -1
  37. package/dist/native/posix-tree-removal.js +2 -2
  38. package/dist/native/windows-process.d.ts +9 -0
  39. package/dist/native/windows-process.js +60 -0
  40. package/dist/release/feed-release-cut.js +2 -1
  41. package/dist/release/framework-package-publish.js +1 -1
  42. package/dist/release/framework-update-lock.js +2 -1
  43. package/dist/release/github-cli.d.ts +18 -0
  44. package/dist/release/github-cli.js +13 -1
  45. package/dist/release/github-release-cut.js +3 -2
  46. package/dist/release/github-release-provenance.js +2 -1
  47. package/dist/release/github-release-publisher.js +2 -1
  48. package/dist/release/local-provenance.js +3 -2
  49. package/dist/release/local-release.js +6 -6
  50. package/dist/release/qualification-run.d.ts +25 -0
  51. package/dist/release/qualification-run.js +43 -0
  52. package/dist/release/release-artifact.js +3 -11
  53. package/dist/release/release-assets-plan.js +16 -6
  54. package/dist/release/release-config.js +14 -14
  55. package/dist/release/release-cut-command.js +14 -4
  56. package/dist/release/release-cut.js +3 -2
  57. package/dist/release/release-descriptor-staging.js +13 -27
  58. package/dist/release/release-finalize.js +2 -1
  59. package/dist/release/release-publication.js +3 -11
  60. package/dist/release/release-publish-command.js +15 -6
  61. package/dist/release/release-qualification.js +3 -2
  62. package/dist/release/retained-release-build.js +12 -12
  63. package/dist/update-helper.bundle.js +250 -190
  64. package/dist/updates/desktop-updater.js +53 -58
  65. package/dist/updates/packaged-launch-admission.js +3 -2
  66. package/dist/updates/packaged-release-config.js +6 -5
  67. package/dist/updates/release-feed.js +9 -9
  68. package/dist/updates/signed-release.js +5 -5
  69. package/dist/updates/stable-file.d.ts +18 -0
  70. package/dist/updates/stable-file.js +54 -1
  71. package/dist/updates/update-adapter.d.ts +2 -13
  72. package/dist/updates/update-arm.js +7 -5
  73. package/dist/updates/update-channel-store.js +7 -9
  74. package/dist/updates/update-discovery.js +30 -34
  75. package/dist/updates/update-download-cleanup.js +2 -1
  76. package/dist/updates/update-download.js +2 -4
  77. package/dist/updates/update-extraction.js +1 -1
  78. package/dist/updates/update-generation-lease.js +4 -3
  79. package/dist/updates/update-handoff-plan.js +3 -2
  80. package/dist/updates/update-helper.js +8 -6
  81. package/dist/updates/update-instance-lease.js +3 -2
  82. package/dist/updates/update-launch-gate.js +12 -25
  83. package/dist/updates/update-launch.js +3 -2
  84. package/dist/updates/update-lock.js +3 -2
  85. package/dist/updates/update-marker.js +3 -2
  86. package/dist/updates/update-process-fence.js +10 -39
  87. package/dist/updates/update-process-liveness.js +2 -1
  88. package/dist/updates/update-resume.js +26 -22
  89. package/dist/updates/update-retention-marker.js +7 -5
  90. package/dist/updates/update-retention-plan.js +2 -2
  91. package/dist/updates/update-retirement.js +40 -39
  92. package/dist/updates/update-state-store.js +29 -37
  93. package/dist/updates/update-state.d.ts +8 -3
  94. package/dist/updates/update-swap-stage.js +6 -5
  95. package/dist/updates/update-swap.d.ts +1 -1
  96. package/dist/updates/update-swap.js +8 -6
  97. package/package.json +4 -12
  98. package/src/desktop/client-asset-stream.ts +6 -12
  99. package/src/updates/update-adapter.ts +2 -14
  100. package/src/updates/update-state.ts +11 -2
  101. package/dist/native/directory-exchange.js +0 -90
  102. package/dist/native/exclusive-rename.d.ts +0 -2
  103. package/dist/native/exclusive-rename.js +0 -52
  104. package/dist/release/retention-dry-run.d.ts +0 -46
  105. package/dist/release/retention-dry-run.js +0 -94
  106. package/dist/updates/update-exclusive-rename.d.ts +0 -3
  107. package/dist/updates/update-exclusive-rename.js +0 -5
@@ -1,8 +1,25 @@
1
1
  // @bun
2
+ // src/app/value-guards.ts
3
+ function isRecord(value) {
4
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5
+ }
6
+ var UUID_SOURCE = /[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/.source;
7
+ var UUID = new RegExp(`^${UUID_SOURCE}$`, "i");
8
+ function isUuid(value) {
9
+ return typeof value === "string" && UUID.test(value);
10
+ }
11
+ function isSha256Hex(value) {
12
+ return typeof value === "string" && /^[a-f0-9]{64}$/.test(value);
13
+ }
14
+ function errorCode(error) {
15
+ const code = isRecord(error) || error instanceof Error ? error.code : undefined;
16
+ return typeof code === "string" ? code : undefined;
17
+ }
18
+
2
19
  // src/updates/update-helper.ts
3
- import { constants as constants6 } from "fs";
4
- import { lstat as lstat4, open as open6, realpath as realpath2 } from "fs/promises";
5
- import { isAbsolute as isAbsolute8, join as join9, resolve as resolve7 } from "path";
20
+ import { constants as constants7 } from "fs";
21
+ import { lstat as lstat2, open as open7, realpath as realpath2 } from "fs/promises";
22
+ import { join as join9 } from "path";
6
23
 
7
24
  // src/updates/update-marker.ts
8
25
  import { randomUUID } from "crypto";
@@ -48,7 +65,7 @@ async function readUpdateMarker(staging, name) {
48
65
  try {
49
66
  file = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
50
67
  } catch (error) {
51
- if (error.code === "ENOENT")
68
+ if (errorCode(error) === "ENOENT")
52
69
  return;
53
70
  throw error;
54
71
  }
@@ -67,14 +84,20 @@ async function readUpdateMarker(staging, name) {
67
84
 
68
85
  // src/updates/update-launch.ts
69
86
  import { spawn } from "child_process";
70
- import { dirname, isAbsolute, join as join2, resolve } from "path";
87
+ import { dirname, join as join2 } from "path";
71
88
 
72
89
  // src/updates/launch-environment.ts
73
90
  var INHERITED_LAUNCH_LEASE_ENV = "ADEA_INHERITED_LAUNCH_LEASE_FD";
74
91
 
92
+ // src/native/canonical-path.ts
93
+ import { isAbsolute, resolve } from "path";
94
+ function isCanonicalPath(value) {
95
+ return typeof value === "string" && isAbsolute(value) && resolve(value) === value && !value.includes("\x00");
96
+ }
97
+
75
98
  // src/updates/update-launch.ts
76
99
  function updateLauncherPath(platform, target) {
77
- if (typeof target !== "string" || !isAbsolute(target) || resolve(target) !== target)
100
+ if (!isCanonicalPath(target))
78
101
  throw new Error("Invalid update target");
79
102
  if (platform === "macos-arm64")
80
103
  return join2(target, "Contents", "MacOS", "launcher");
@@ -101,10 +124,51 @@ async function launchInstalledApp(platform, target, env = process.env) {
101
124
  }
102
125
 
103
126
  // src/updates/update-process-fence.ts
104
- import { dlopen, FFIType, ptr } from "bun:ffi";
127
+ import { dlopen as dlopen2, FFIType as FFIType2, ptr as ptr2 } from "bun:ffi";
105
128
  import { readFile, realpath } from "fs/promises";
106
129
  import { isAbsolute as isAbsolute2 } from "path";
107
130
 
131
+ // src/native/windows-process.ts
132
+ import { dlopen, FFIType, ptr } from "bun:ffi";
133
+ var PROCESS_QUERY_LIMITED_INFORMATION = 4096;
134
+ var STILL_ACTIVE = 259;
135
+ var MAX_PATH_CHARS = 32768;
136
+ function kernel32() {
137
+ return dlopen("kernel32.dll", {
138
+ OpenProcess: { args: [FFIType.u32, FFIType.i32, FFIType.u32], returns: FFIType.ptr },
139
+ GetExitCodeProcess: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
140
+ QueryFullProcessImageNameW: {
141
+ args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr],
142
+ returns: FFIType.i32
143
+ },
144
+ CloseHandle: { args: [FFIType.ptr], returns: FFIType.i32 }
145
+ }).symbols;
146
+ }
147
+ var symbols;
148
+ function windowsProcessImage(pid, options) {
149
+ symbols ??= kernel32();
150
+ const handle = symbols.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
151
+ if (!handle)
152
+ return "missing";
153
+ try {
154
+ if (options.requireRunning) {
155
+ const code = new Uint32Array(1);
156
+ if (!symbols.GetExitCodeProcess(handle, ptr(code)))
157
+ throw new Error("Unable to query process state");
158
+ if (code[0] !== STILL_ACTIVE)
159
+ return "missing";
160
+ }
161
+ const name = new Uint16Array(MAX_PATH_CHARS), length = new Uint32Array([MAX_PATH_CHARS]);
162
+ if (!symbols.QueryFullProcessImageNameW(handle, 0, ptr(name), ptr(length)))
163
+ throw new Error("Unable to query process executable");
164
+ if (!length[0] || length[0] >= MAX_PATH_CHARS)
165
+ throw new Error("Invalid process executable path");
166
+ return Buffer.from(name.buffer, 0, length[0] * 2).toString("utf16le");
167
+ } finally {
168
+ symbols.CloseHandle(handle);
169
+ }
170
+ }
171
+
108
172
  // src/updates/linux-process-state.ts
109
173
  function linuxProcessIsExited(stat, pid) {
110
174
  const prefix = `${pid} (`;
@@ -132,7 +196,7 @@ function isProcessRunning(pid) {
132
196
  process.kill(pid, 0);
133
197
  return true;
134
198
  } catch (error) {
135
- if (error.code === "ESRCH")
199
+ if (errorCode(error) === "ESRCH")
136
200
  return false;
137
201
  throw new Error("Cannot inspect update process", { cause: error });
138
202
  }
@@ -146,7 +210,7 @@ async function hasVerifiedProcessExited(pid) {
146
210
  try {
147
211
  stat = await readFile(`/proc/${pid}/stat`, "utf8");
148
212
  } catch (error) {
149
- const code = error.code;
213
+ const code = errorCode(error);
150
214
  if ((code === "ENOENT" || code === "ESRCH") && !isProcessRunning(pid))
151
215
  return true;
152
216
  throw new Error("Cannot inspect update process", { cause: error });
@@ -157,12 +221,12 @@ async function executable(pid) {
157
221
  if (process.platform === "linux")
158
222
  return realpath(`/proc/${pid}/exe`);
159
223
  if (process.platform === "darwin") {
160
- const library = dlopen("/usr/lib/libproc.dylib", {
161
- proc_pidpath: { args: [FFIType.i32, FFIType.ptr, FFIType.u32], returns: FFIType.i32 }
224
+ const library = dlopen2("/usr/lib/libproc.dylib", {
225
+ proc_pidpath: { args: [FFIType2.i32, FFIType2.ptr, FFIType2.u32], returns: FFIType2.i32 }
162
226
  });
163
227
  try {
164
228
  const buffer = Buffer.alloc(4096);
165
- const count = library.symbols.proc_pidpath(pid, ptr(buffer), buffer.length);
229
+ const count = library.symbols.proc_pidpath(pid, ptr2(buffer), buffer.length);
166
230
  if (count <= 0 || count >= buffer.length)
167
231
  throw new Error("Cannot inspect update process");
168
232
  const end = buffer.indexOf(0);
@@ -177,33 +241,15 @@ async function executable(pid) {
177
241
  }
178
242
  }
179
243
  if (process.platform === "win32") {
180
- const library = dlopen("kernel32.dll", {
181
- OpenProcess: { args: [FFIType.u32, FFIType.i32, FFIType.u32], returns: FFIType.ptr },
182
- QueryFullProcessImageNameW: {
183
- args: [FFIType.ptr, FFIType.u32, FFIType.ptr, FFIType.ptr],
184
- returns: FFIType.i32
185
- },
186
- CloseHandle: { args: [FFIType.ptr], returns: FFIType.i32 }
187
- });
244
+ let image;
188
245
  try {
189
- const handle = library.symbols.OpenProcess(4096, 0, pid);
190
- if (!handle)
191
- throw new Error("Cannot open update process");
192
- try {
193
- const buffer = Buffer.alloc(32768 * 2), length = Buffer.alloc(4);
194
- length.writeUInt32LE(32768);
195
- if (!library.symbols.QueryFullProcessImageNameW(handle, 0, ptr(buffer), ptr(length)))
196
- throw new Error("Cannot inspect update process");
197
- const count = length.readUInt32LE();
198
- if (!count || count >= 32768)
199
- throw new Error("Invalid update process path");
200
- return await realpath(buffer.subarray(0, count * 2).toString("utf16le"));
201
- } finally {
202
- library.symbols.CloseHandle(handle);
203
- }
204
- } finally {
205
- library.close();
246
+ image = windowsProcessImage(pid, { requireRunning: false });
247
+ } catch (error) {
248
+ throw new Error("Cannot inspect update process", { cause: error });
206
249
  }
250
+ if (image === "missing")
251
+ throw new Error("Cannot open update process");
252
+ return realpath(image);
207
253
  }
208
254
  throw new Error("Unsupported update process platform");
209
255
  }
@@ -262,50 +308,68 @@ async function waitForUpdateProcesses(options) {
262
308
 
263
309
  // src/updates/update-swap.ts
264
310
  import { createHash } from "crypto";
265
- import { constants as constants4 } from "fs";
266
- import { lstat as lstat2, open as open4, readdir, readFile as readFile4, readlink } from "fs/promises";
267
- import { basename as basename2, dirname as dirname3, isAbsolute as isAbsolute5, join as join6, resolve as resolve4 } from "path";
311
+ import { constants as constants5 } from "fs";
312
+ import { lstat, open as open5, readdir, readFile as readFile3, readlink } from "fs/promises";
313
+ import { basename as basename2, dirname as dirname3, join as join6 } from "path";
268
314
 
269
- // src/native/directory-exchange.ts
315
+ // src/native/native-rename.ts
270
316
  import { randomUUID as randomUUID2 } from "crypto";
271
317
  import { mkdir, readFile as readFile2, rm, writeFile as writeFile2 } from "fs/promises";
272
- import { dlopen as dlopen2, FFIType as FFIType2, ptr as ptr2 } from "bun:ffi";
273
- import { isAbsolute as isAbsolute3, join as join3, resolve as resolve2 } from "path";
274
- var RENAME_EXCHANGE = 2;
275
- function exchangeDirectories(source, destination) {
276
- if (typeof source !== "string" || typeof destination !== "string" || !isAbsolute3(source) || !isAbsolute3(destination) || resolve2(source) !== source || resolve2(destination) !== destination || source === destination || source.includes("\x00") || destination.includes("\x00"))
277
- throw new Error("Invalid atomic directory exchange paths");
318
+ import { dlopen as dlopen3, FFIType as FFIType3, ptr as ptr3 } from "bun:ffi";
319
+ import { join as join3 } from "path";
320
+ var DARWIN_RENAME_SWAP = 2;
321
+ var LINUX_RENAME_EXCHANGE = 2;
322
+ var AT_FDCWD = -100;
323
+ var library;
324
+ function renameLibrary() {
325
+ if (library)
326
+ return library;
278
327
  if (process.platform === "darwin") {
279
- const library = dlopen2("libSystem.B.dylib", {
280
- renamex_np: { args: [FFIType2.ptr, FFIType2.ptr, FFIType2.u32], returns: FFIType2.i32 }
328
+ const { symbols } = dlopen3("libSystem.B.dylib", {
329
+ renamex_np: { args: [FFIType3.ptr, FFIType3.ptr, FFIType3.u32], returns: FFIType3.i32 }
281
330
  });
282
- try {
283
- const from = Buffer.from(source + "\x00"), to = Buffer.from(destination + "\x00");
284
- if (library.symbols.renamex_np(ptr2(from), ptr2(to), RENAME_EXCHANGE) !== 0)
285
- throw new Error("Atomic directory exchange is unsupported or failed");
286
- } finally {
287
- library.close();
288
- }
331
+ library = {
332
+ platform: "darwin",
333
+ rename: (from, to, flags) => symbols.renamex_np(ptr3(from), ptr3(to), flags) === 0
334
+ };
289
335
  } else if (process.platform === "linux") {
290
- const library = dlopen2("libc.so.6", {
336
+ const { symbols } = dlopen3("libc.so.6", {
291
337
  renameat2: {
292
- args: [FFIType2.i32, FFIType2.ptr, FFIType2.i32, FFIType2.ptr, FFIType2.u32],
293
- returns: FFIType2.i32
338
+ args: [FFIType3.i32, FFIType3.ptr, FFIType3.i32, FFIType3.ptr, FFIType3.u32],
339
+ returns: FFIType3.i32
294
340
  }
295
341
  });
296
- try {
297
- const from = Buffer.from(source + "\x00"), to = Buffer.from(destination + "\x00");
298
- if (library.symbols.renameat2(-100, ptr2(from), -100, ptr2(to), RENAME_EXCHANGE) !== 0)
299
- throw new Error("Atomic directory exchange is unsupported or failed");
300
- } finally {
301
- library.close();
302
- }
303
- } else {
342
+ library = {
343
+ platform: "linux",
344
+ rename: (from, to, flags) => symbols.renameat2(AT_FDCWD, ptr3(from), AT_FDCWD, ptr3(to), flags) === 0
345
+ };
346
+ } else if (process.platform === "win32") {
347
+ const { symbols } = dlopen3("kernel32.dll", {
348
+ MoveFileW: { args: [FFIType3.ptr, FFIType3.ptr], returns: FFIType3.i32 }
349
+ });
350
+ library = { platform: "win32", move: (from, to) => symbols.MoveFileW(ptr3(from), ptr3(to)) !== 0 };
351
+ } else
352
+ throw new Error("Unsupported native rename platform");
353
+ return library;
354
+ }
355
+ function renamePaths(source, destination) {
356
+ return isCanonicalPath(source) && isCanonicalPath(destination) && source !== destination;
357
+ }
358
+ var cString = (path) => Buffer.from(path + "\x00");
359
+ function exchangeDirectories(source, destination) {
360
+ if (!renamePaths(source, destination))
361
+ throw new Error("Invalid atomic directory exchange paths");
362
+ if (process.platform !== "darwin" && process.platform !== "linux")
304
363
  throw new Error("Atomic directory exchange is unsupported on this platform");
305
- }
364
+ const native = renameLibrary();
365
+ if (native.platform === "win32")
366
+ throw new Error("Atomic directory exchange is unsupported on this platform");
367
+ const flags = native.platform === "darwin" ? DARWIN_RENAME_SWAP : LINUX_RENAME_EXCHANGE;
368
+ if (!native.rename(cString(source), cString(destination), flags))
369
+ throw new Error("Atomic directory exchange is unsupported or failed");
306
370
  }
307
371
  async function probeDirectoryExchange(parent, exchange = exchangeDirectories) {
308
- if (typeof parent !== "string" || !isAbsolute3(parent) || resolve2(parent) !== parent || parent.includes("\x00"))
372
+ if (!isCanonicalPath(parent))
309
373
  throw new Error("Invalid directory exchange probe parent");
310
374
  const id = randomUUID2();
311
375
  const first = join3(parent, `.framework-exchange-probe-${id}-a`);
@@ -341,17 +405,17 @@ async function probeDirectoryExchange(parent, exchange = exchangeDirectories) {
341
405
 
342
406
  // src/updates/update-state-store.ts
343
407
  import { randomUUID as randomUUID3 } from "crypto";
344
- import { constants as constants3 } from "fs";
345
- import { lstat, open as open3, readFile as readFile3, rename, unlink as unlink2, writeFile as writeFile3 } from "fs/promises";
408
+ import { constants as constants4 } from "fs";
409
+ import { open as open4, rename, unlink as unlink2, writeFile as writeFile3 } from "fs/promises";
346
410
  import { join as join5 } from "path";
347
411
 
348
412
  // src/updates/update-lock.ts
349
- import { basename, dirname as dirname2, isAbsolute as isAbsolute4, resolve as resolve3, join as join4 } from "path";
413
+ import { basename, dirname as dirname2, join as join4 } from "path";
350
414
 
351
415
  // src/updates/native-file-lock.ts
352
416
  import { constants as constants2 } from "fs";
353
417
  import { open as open2 } from "fs/promises";
354
- import { dlopen as dlopen3, FFIType as FFIType3, ptr as ptr3, read } from "bun:ffi";
418
+ import { dlopen as dlopen4, FFIType as FFIType4, ptr as ptr4, read } from "bun:ffi";
355
419
  var LOCK_SH = 1;
356
420
  var LOCK_EX = 2;
357
421
  var LOCK_NB = 4;
@@ -431,9 +495,9 @@ function loadPosixLibrary(darwin) {
431
495
  let failure;
432
496
  for (const name of candidates) {
433
497
  try {
434
- return dlopen3(name, {
435
- flock: { args: [FFIType3.i32, FFIType3.i32], returns: FFIType3.i32 },
436
- [errnoSymbol]: { args: [], returns: FFIType3.ptr }
498
+ return dlopen4(name, {
499
+ flock: { args: [FFIType4.i32, FFIType4.i32], returns: FFIType4.i32 },
500
+ [errnoSymbol]: { args: [], returns: FFIType4.ptr }
437
501
  });
438
502
  } catch (error) {
439
503
  failure = error;
@@ -444,32 +508,32 @@ function loadPosixLibrary(darwin) {
444
508
  async function tryAcquireWindows(path, mode) {
445
509
  if (process.arch !== "x64" && process.arch !== "arm64")
446
510
  throw new Error("Unsupported Windows native file lock architecture");
447
- const library = dlopen3("kernel32.dll", {
511
+ const library = dlopen4("kernel32.dll", {
448
512
  CreateFileW: {
449
513
  args: [
450
- FFIType3.ptr,
451
- FFIType3.u32,
452
- FFIType3.u32,
453
- FFIType3.ptr,
454
- FFIType3.u32,
455
- FFIType3.u32,
456
- FFIType3.u64
514
+ FFIType4.ptr,
515
+ FFIType4.u32,
516
+ FFIType4.u32,
517
+ FFIType4.ptr,
518
+ FFIType4.u32,
519
+ FFIType4.u32,
520
+ FFIType4.u64
457
521
  ],
458
- returns: FFIType3.u64
522
+ returns: FFIType4.u64
459
523
  },
460
524
  LockFileEx: {
461
- args: [FFIType3.u64, FFIType3.u32, FFIType3.u32, FFIType3.u32, FFIType3.u32, FFIType3.ptr],
462
- returns: FFIType3.i32
525
+ args: [FFIType4.u64, FFIType4.u32, FFIType4.u32, FFIType4.u32, FFIType4.u32, FFIType4.ptr],
526
+ returns: FFIType4.i32
463
527
  },
464
528
  UnlockFileEx: {
465
- args: [FFIType3.u64, FFIType3.u32, FFIType3.u32, FFIType3.u32, FFIType3.ptr],
466
- returns: FFIType3.i32
529
+ args: [FFIType4.u64, FFIType4.u32, FFIType4.u32, FFIType4.u32, FFIType4.ptr],
530
+ returns: FFIType4.i32
467
531
  },
468
- CloseHandle: { args: [FFIType3.u64], returns: FFIType3.i32 },
469
- GetLastError: { args: [], returns: FFIType3.u32 }
532
+ CloseHandle: { args: [FFIType4.u64], returns: FFIType4.i32 },
533
+ GetLastError: { args: [], returns: FFIType4.u32 }
470
534
  });
471
535
  const name = Buffer.from(path + "\x00", "utf16le");
472
- const handle = library.symbols.CreateFileW(ptr3(name), 3221225472, 3, 0, 4, 128, 0);
536
+ const handle = library.symbols.CreateFileW(ptr4(name), 3221225472, 3, 0, 4, 128, 0);
473
537
  if (handle === 0n || handle === INVALID_HANDLE_VALUE) {
474
538
  const error = library.symbols.GetLastError();
475
539
  library.close();
@@ -477,7 +541,7 @@ async function tryAcquireWindows(path, mode) {
477
541
  }
478
542
  const overlapped = Buffer.alloc(32);
479
543
  const flags = LOCKFILE_FAIL_IMMEDIATELY | (mode === "exclusive" ? LOCKFILE_EXCLUSIVE_LOCK : 0);
480
- if (!library.symbols.LockFileEx(handle, flags, 0, 1, 0, ptr3(overlapped))) {
544
+ if (!library.symbols.LockFileEx(handle, flags, 0, 1, 0, ptr4(overlapped))) {
481
545
  const error = library.symbols.GetLastError();
482
546
  library.symbols.CloseHandle(handle);
483
547
  library.close();
@@ -493,7 +557,7 @@ async function tryAcquireWindows(path, mode) {
493
557
  acquired = false;
494
558
  let failure;
495
559
  try {
496
- if (!library.symbols.UnlockFileEx(handle, 0, 1, 0, ptr3(overlapped))) {
560
+ if (!library.symbols.UnlockFileEx(handle, 0, 1, 0, ptr4(overlapped))) {
497
561
  const error = library.symbols.GetLastError();
498
562
  failure = new Error("Could not release native file lock (Windows error " + error + ")");
499
563
  }
@@ -512,7 +576,7 @@ async function tryAcquireWindows(path, mode) {
512
576
 
513
577
  // src/updates/update-lock.ts
514
578
  function updateLockPath(target) {
515
- if (typeof target !== "string" || !isAbsolute4(target) || resolve3(target) !== target || target.includes("\x00"))
579
+ if (!isCanonicalPath(target))
516
580
  throw new Error("Invalid update lock target");
517
581
  return join4(dirname2(target), `.${basename(target)}.framework-update.lock`);
518
582
  }
@@ -520,6 +584,34 @@ async function tryAcquireUpdateLock(target) {
520
584
  return tryAcquireNativeFileLock(updateLockPath(target), "exclusive");
521
585
  }
522
586
 
587
+ // src/updates/stable-file.ts
588
+ import { constants as constants3 } from "fs";
589
+ import { open as open3 } from "fs/promises";
590
+ async function readSmallStateFile(path, maximum, message) {
591
+ let file;
592
+ try {
593
+ file = await open3(path, constants3.O_RDONLY | (constants3.O_NOFOLLOW ?? 0) | (constants3.O_NONBLOCK ?? 0));
594
+ } catch (error) {
595
+ const code = errorCode(error);
596
+ if (code === "ENOENT")
597
+ return;
598
+ if (code === "ELOOP" || code === "EMLINK")
599
+ throw new Error(message, { cause: error });
600
+ throw error;
601
+ }
602
+ try {
603
+ const info = await file.stat();
604
+ if (!info.isFile() || info.size < 1 || info.size > maximum)
605
+ throw new Error(message);
606
+ const text = await file.readFile("utf8");
607
+ if (Buffer.byteLength(text) !== info.size)
608
+ throw new Error(message);
609
+ return text;
610
+ } finally {
611
+ await file.close();
612
+ }
613
+ }
614
+
523
615
  // src/updates/update-state-store.ts
524
616
  class UpdateLockBusyError extends Error {
525
617
  constructor() {
@@ -545,7 +637,7 @@ function parseState(value) {
545
637
  const candidateTreeIdentity = record.candidateTreeIdentity;
546
638
  const candidateTreeDigest = record.candidateTreeDigest;
547
639
  const previousTreeIdentity = record.previousTreeIdentity;
548
- if (record.schema !== 1 || typeof record.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(record.generation) || !["staged", "armed", "swapping", "completed", "retiring", "retired", "failed"].includes(String(record.phase)) || typeof record.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.version) || typeof record.previousVersion !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.previousVersion) || candidateTreeDigest !== undefined && (typeof candidateTreeDigest !== "string" || !/^[0-9a-f]{64}$/.test(candidateTreeDigest)) || candidateTreeIdentity !== undefined && (typeof candidateTreeIdentity !== "object" || candidateTreeIdentity === null || Array.isArray(candidateTreeIdentity) || Object.keys(candidateTreeIdentity).length !== 2 || typeof candidateTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.device) || typeof candidateTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.inode)) || previousTreeIdentity !== undefined && (typeof previousTreeIdentity !== "object" || previousTreeIdentity === null || Array.isArray(previousTreeIdentity) || Object.keys(previousTreeIdentity).length !== 2 || typeof previousTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.device) || typeof previousTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.inode)) || record.signedRelease !== undefined && (typeof record.signedRelease !== "object" || record.signedRelease === null || Array.isArray(record.signedRelease) || Object.keys(record.signedRelease).length !== 2 || !Object.hasOwn(record.signedRelease, "descriptor") || typeof record.signedRelease.signature !== "string") || record.helperPid !== undefined && (!Number.isSafeInteger(record.helperPid) || record.helperPid < 2) || record.helperOwner !== undefined && (typeof record.helperOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(record.helperOwner)) || record.launchGateOwner !== undefined && (typeof record.launchGateOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(record.launchGateOwner)) || Object.keys(record).some((key) => ![
640
+ if (record.schema !== 1 || typeof record.generation !== "string" || !isUuid(record.generation) || !["staged", "armed", "swapping", "completed", "retiring", "retired", "failed"].includes(String(record.phase)) || typeof record.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.version) || typeof record.previousVersion !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(record.previousVersion) || candidateTreeDigest !== undefined && (typeof candidateTreeDigest !== "string" || !isSha256Hex(candidateTreeDigest)) || candidateTreeIdentity !== undefined && (typeof candidateTreeIdentity !== "object" || candidateTreeIdentity === null || Array.isArray(candidateTreeIdentity) || Object.keys(candidateTreeIdentity).length !== 2 || typeof candidateTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.device) || typeof candidateTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(candidateTreeIdentity.inode)) || previousTreeIdentity !== undefined && (typeof previousTreeIdentity !== "object" || previousTreeIdentity === null || Array.isArray(previousTreeIdentity) || Object.keys(previousTreeIdentity).length !== 2 || typeof previousTreeIdentity.device !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.device) || typeof previousTreeIdentity.inode !== "string" || !/^[1-9]\d{0,19}$/.test(previousTreeIdentity.inode)) || record.signedRelease !== undefined && (typeof record.signedRelease !== "object" || record.signedRelease === null || Array.isArray(record.signedRelease) || Object.keys(record.signedRelease).length !== 2 || !Object.hasOwn(record.signedRelease, "descriptor") || typeof record.signedRelease.signature !== "string") || record.helperPid !== undefined && (!Number.isSafeInteger(record.helperPid) || record.helperPid < 2) || record.helperOwner !== undefined && (typeof record.helperOwner !== "string" || !isUuid(record.helperOwner)) || record.launchGateOwner !== undefined && (typeof record.launchGateOwner !== "string" || !isUuid(record.launchGateOwner)) || Object.keys(record).some((key) => ![
549
641
  "schema",
550
642
  "generation",
551
643
  "phase",
@@ -567,21 +659,11 @@ function parseState(value) {
567
659
  return Object.freeze(record);
568
660
  }
569
661
  async function readUpdateStageState(staging) {
570
- const path = join5(staging, "state.json");
571
- let info;
572
- try {
573
- info = await lstat(path);
574
- } catch (error) {
575
- if (error.code === "ENOENT")
576
- return;
577
- throw error;
578
- }
579
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > 4096)
580
- throw new Error("Invalid update staging state");
581
- return parseState(JSON.parse(await readFile3(path, "utf8")));
662
+ const text = await readSmallStateFile(join5(staging, "state.json"), 4096, "Invalid update staging state");
663
+ return text === undefined ? undefined : parseState(JSON.parse(text));
582
664
  }
583
665
  async function syncUpdateStageDirectory(staging) {
584
- const directory = await open3(staging, constants3.O_RDONLY);
666
+ const directory = await open4(staging, constants4.O_RDONLY);
585
667
  try {
586
668
  await directory.sync();
587
669
  } finally {
@@ -589,39 +671,36 @@ async function syncUpdateStageDirectory(staging) {
589
671
  }
590
672
  }
591
673
  async function writeUpdateStageState(staging, state) {
592
- if (typeof state.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(state.generation))
593
- throw new Error("Invalid update staging state");
594
- const path = join5(staging, "state.json");
595
- const temporary = join5(staging, `.state-${state.generation}-${randomUUID3()}.tmp`);
596
- await writeFile3(temporary, JSON.stringify(state) + `
597
- `, { flag: "wx", mode: 384 });
598
- try {
599
- await rename(temporary, path);
600
- } catch (error) {
601
- await unlink2(temporary).catch(() => {});
602
- throw error;
603
- }
674
+ await replaceUpdateStageState(staging, state, false);
604
675
  }
605
676
  async function writeUpdateStageStateDurably(staging, state) {
606
- if (typeof state.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(state.generation))
677
+ await replaceUpdateStageState(staging, state, true);
678
+ }
679
+ async function replaceUpdateStageState(staging, state, durable) {
680
+ if (typeof state.generation !== "string" || !isUuid(state.generation))
607
681
  throw new Error("Invalid update staging state");
608
682
  const path = join5(staging, "state.json");
609
683
  const temporary = join5(staging, `.state-${state.generation}-${randomUUID3()}.tmp`);
684
+ const contents = JSON.stringify(state) + `
685
+ `;
610
686
  let renamed = false;
611
687
  try {
612
- const file = await open3(temporary, constants3.O_WRONLY | constants3.O_CREAT | constants3.O_EXCL, 384);
613
- try {
614
- await file.writeFile(JSON.stringify(state) + `
615
- `);
616
- await file.sync();
617
- } finally {
618
- await file.close();
619
- }
688
+ if (durable) {
689
+ const file = await open4(temporary, constants4.O_WRONLY | constants4.O_CREAT | constants4.O_EXCL, 384);
690
+ try {
691
+ await file.writeFile(contents);
692
+ await file.sync();
693
+ } finally {
694
+ await file.close();
695
+ }
696
+ } else
697
+ await writeFile3(temporary, contents, { flag: "wx", mode: 384 });
620
698
  await rename(temporary, path);
621
699
  renamed = true;
622
- await syncUpdateStageDirectory(staging);
700
+ if (durable)
701
+ await syncUpdateStageDirectory(staging);
623
702
  } catch (error) {
624
- if (!renamed)
703
+ if (!renamed && errorCode(error) !== "EEXIST")
625
704
  await unlink2(temporary).catch(() => {});
626
705
  throw error;
627
706
  }
@@ -629,14 +708,14 @@ async function writeUpdateStageStateDurably(staging, state) {
629
708
 
630
709
  // src/updates/update-swap.ts
631
710
  function updateStagingFor(target) {
632
- if (typeof target !== "string" || !isAbsolute5(target) || resolve4(target) !== target)
711
+ if (!isCanonicalPath(target))
633
712
  throw new Error("Invalid update target");
634
713
  return join6(dirname3(target), `.${basename2(target)}.framework-update`);
635
714
  }
636
715
  async function readUpdateTreeVersion(tree) {
637
716
  const versionPath = join6(tree, ...process.platform === "darwin" ? ["Contents", "Resources"] : ["Resources"], "version.json");
638
717
  try {
639
- const value = JSON.parse(await readFile4(versionPath, "utf8"));
718
+ const value = JSON.parse(await readFile3(versionPath, "utf8"));
640
719
  if (typeof value !== "object" || value === null || typeof value.version !== "string")
641
720
  return;
642
721
  return value.version;
@@ -650,7 +729,7 @@ function updateTreeIdentityFromStat(info) {
650
729
  return { device: info.dev.toString(10), inode: info.ino.toString(10) };
651
730
  }
652
731
  async function readUpdateTreeIdentity(tree) {
653
- const info = await lstat2(tree, { bigint: true });
732
+ const info = await lstat(tree, { bigint: true });
654
733
  return updateTreeIdentityFromStat(info);
655
734
  }
656
735
  function sameUpdateTreeIdentity(left, right) {
@@ -662,7 +741,7 @@ function sameTreeEntrySnapshot(left, right) {
662
741
  async function readUpdateTreeDigest(tree) {
663
742
  const hash = createHash("sha256");
664
743
  hash.update("adea-framework-update-tree\x00v1\x00", "utf8");
665
- const root = await lstat2(tree, { bigint: true });
744
+ const root = await lstat(tree, { bigint: true });
666
745
  if (!root.isDirectory() || root.isSymbolicLink())
667
746
  throw new Error("Update tree is not a real directory");
668
747
  const rootDevice = root.dev;
@@ -686,14 +765,14 @@ async function readUpdateTreeDigest(tree) {
686
765
  entryCount++;
687
766
  };
688
767
  const visit = async (path, relative) => {
689
- const before = await lstat2(path, { bigint: true });
768
+ const before = await lstat(path, { bigint: true });
690
769
  if (relative === "" && !sameTreeEntrySnapshot(root, before))
691
770
  throw new Error("Update tree root changed before its digest was computed");
692
771
  if (before.dev !== rootDevice)
693
772
  throw new Error("Update tree crosses a filesystem device");
694
773
  if (before.isSymbolicLink()) {
695
774
  const target = Buffer.from(await readlink(path), "utf8");
696
- const after = await lstat2(path, { bigint: true });
775
+ const after = await lstat(path, { bigint: true });
697
776
  if (!sameTreeEntrySnapshot(before, after))
698
777
  throw new Error("Update tree changed while reading a symbolic link");
699
778
  writeRecord("symlink", relative, before.mode, BigInt(target.length), target);
@@ -706,7 +785,7 @@ async function readUpdateTreeDigest(tree) {
706
785
  const child = relative ? `${relative}/${name}` : name;
707
786
  await visit(join6(path, name), child);
708
787
  }
709
- const after = await lstat2(path, { bigint: true });
788
+ const after = await lstat(path, { bigint: true });
710
789
  if (!sameTreeEntrySnapshot(before, after))
711
790
  throw new Error("Update tree changed while reading a directory");
712
791
  return;
@@ -714,8 +793,8 @@ async function readUpdateTreeDigest(tree) {
714
793
  if (!before.isFile() || before.nlink !== 1n || before.size < 0n)
715
794
  throw new Error("Update tree contains an unsupported filesystem entry");
716
795
  writeRecord("file", relative, before.mode, before.size);
717
- const noFollow = constants4.O_NOFOLLOW ?? 0;
718
- const file = await open4(path, constants4.O_RDONLY | noFollow | (constants4.O_NONBLOCK ?? 0));
796
+ const noFollow = constants5.O_NOFOLLOW ?? 0;
797
+ const file = await open5(path, constants5.O_RDONLY | noFollow | (constants5.O_NONBLOCK ?? 0));
719
798
  try {
720
799
  const opened = await file.stat({ bigint: true });
721
800
  if (!opened.isFile() || !sameTreeEntrySnapshot(before, opened))
@@ -730,7 +809,7 @@ async function readUpdateTreeDigest(tree) {
730
809
  hash.update(buffer.subarray(0, result.bytesRead));
731
810
  }
732
811
  const afterOpen = await file.stat({ bigint: true });
733
- const afterPath = await lstat2(path, { bigint: true });
812
+ const afterPath = await lstat(path, { bigint: true });
734
813
  if (bytesRead !== before.size || !sameTreeEntrySnapshot(opened, afterOpen) || !sameTreeEntrySnapshot(before, afterPath))
735
814
  throw new Error("Update tree changed while reading a file");
736
815
  } finally {
@@ -738,7 +817,7 @@ async function readUpdateTreeDigest(tree) {
738
817
  }
739
818
  };
740
819
  await visit(tree, "");
741
- const finalRoot = await lstat2(tree, { bigint: true });
820
+ const finalRoot = await lstat(tree, { bigint: true });
742
821
  if (!sameTreeEntrySnapshot(root, finalRoot))
743
822
  throw new Error("Update tree changed while its digest was computed");
744
823
  const count = Buffer.allocUnsafe(8);
@@ -747,10 +826,10 @@ async function readUpdateTreeDigest(tree) {
747
826
  return hash.digest("hex");
748
827
  }
749
828
  function sameUpdateTreeDigest(left, right) {
750
- return typeof left === "string" && typeof right === "string" && /^[0-9a-f]{64}$/.test(left) && left === right;
829
+ return typeof left === "string" && typeof right === "string" && isSha256Hex(left) && left === right;
751
830
  }
752
831
  async function syncDirectoryPath(path) {
753
- const directory = await open4(path, constants4.O_RDONLY);
832
+ const directory = await open5(path, constants5.O_RDONLY);
754
833
  try {
755
834
  await directory.sync();
756
835
  } finally {
@@ -758,12 +837,12 @@ async function syncDirectoryPath(path) {
758
837
  }
759
838
  }
760
839
  async function syncUpdateTree(tree) {
761
- const root = await lstat2(tree);
840
+ const root = await lstat(tree);
762
841
  if (!root.isDirectory())
763
842
  throw new Error("Update tree changed while syncing");
764
843
  const rootDevice = root.dev;
765
844
  const syncEntry = async (path) => {
766
- const info = await lstat2(path);
845
+ const info = await lstat(path);
767
846
  if (info.isSymbolicLink())
768
847
  return;
769
848
  if (info.dev !== rootDevice)
@@ -776,7 +855,7 @@ async function syncUpdateTree(tree) {
776
855
  }
777
856
  if (!info.isFile())
778
857
  throw new Error("Update tree contains an unsupported filesystem entry");
779
- const file = await open4(path, constants4.O_RDONLY | (constants4.O_NOFOLLOW ?? 0));
858
+ const file = await open5(path, constants5.O_RDONLY | (constants5.O_NOFOLLOW ?? 0));
780
859
  try {
781
860
  const opened = await file.stat();
782
861
  if (!opened.isFile())
@@ -787,12 +866,12 @@ async function syncUpdateTree(tree) {
787
866
  }
788
867
  };
789
868
  await syncEntry(tree);
790
- const final = await lstat2(tree);
869
+ const final = await lstat(tree);
791
870
  if (!final.isDirectory() || final.dev !== rootDevice)
792
871
  throw new Error("Update tree changed while syncing");
793
872
  }
794
873
  async function syncUpdateTreeParent(target) {
795
- const directory = await open4(dirname3(target), constants4.O_RDONLY);
874
+ const directory = await open5(dirname3(target), constants5.O_RDONLY);
796
875
  try {
797
876
  await directory.sync();
798
877
  } finally {
@@ -800,14 +879,14 @@ async function syncUpdateTreeParent(target) {
800
879
  }
801
880
  }
802
881
  async function exists(path) {
803
- return lstat2(path).then(() => true, (error) => {
804
- if (error.code === "ENOENT")
882
+ return lstat(path).then(() => true, (error) => {
883
+ if (errorCode(error) === "ENOENT")
805
884
  return false;
806
885
  throw error;
807
886
  });
808
887
  }
809
888
  async function assertDirectory(path) {
810
- const info = await lstat2(path);
889
+ const info = await lstat(path);
811
890
  if (!info.isDirectory() || info.isSymbolicLink())
812
891
  throw new Error("Update exchange requires real directories");
813
892
  }
@@ -846,14 +925,14 @@ async function swapUpdateTreeWhileLocked(options, exchangeOperation = exchangeDi
846
925
 
847
926
  // src/updates/update-launch-gate.ts
848
927
  import { randomUUID as randomUUID4 } from "crypto";
849
- import { constants as constants5 } from "fs";
850
- import { lstat as lstat3, open as open5, rename as rename2, unlink as unlink3 } from "fs/promises";
851
- import { basename as basename4, dirname as dirname5, isAbsolute as isAbsolute7, join as join8, resolve as resolve6 } from "path";
928
+ import { constants as constants6 } from "fs";
929
+ import { open as open6, rename as rename2, unlink as unlink3 } from "fs/promises";
930
+ import { basename as basename4, dirname as dirname5, join as join8 } from "path";
852
931
 
853
932
  // src/updates/update-instance-lease.ts
854
- import { basename as basename3, dirname as dirname4, isAbsolute as isAbsolute6, join as join7, resolve as resolve5 } from "path";
933
+ import { basename as basename3, dirname as dirname4, join as join7 } from "path";
855
934
  function updateInstanceLeasePath(target) {
856
- if (typeof target !== "string" || !isAbsolute6(target) || resolve5(target) !== target || target.includes("\x00"))
935
+ if (!isCanonicalPath(target))
857
936
  throw new Error("Invalid update instance lease target");
858
937
  return join7(dirname4(target), "." + basename3(target) + ".framework-instance-lease.lock");
859
938
  }
@@ -867,15 +946,14 @@ async function tryAcquireUpdateInstanceLease(target, mode) {
867
946
  }
868
947
 
869
948
  // src/updates/update-launch-gate.ts
870
- var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
871
949
  var VERSION = /^\d+\.\d+\.\d+(?:-dev\.\d+)?$/;
872
950
  var MAX_GATE_BYTES = 1024;
873
951
  function validateTarget(target) {
874
- if (typeof target !== "string" || !isAbsolute7(target) || resolve6(target) !== target || target.includes("\x00"))
952
+ if (!isCanonicalPath(target))
875
953
  throw new Error("Invalid update launch gate target");
876
954
  }
877
955
  async function syncDirectory(directoryPath) {
878
- const directory = await open5(directoryPath, constants5.O_RDONLY);
956
+ const directory = await open6(directoryPath, constants6.O_RDONLY);
879
957
  try {
880
958
  await directory.sync();
881
959
  } finally {
@@ -967,7 +1045,7 @@ function helperLeaseState(lease, target, owner) {
967
1045
  return state;
968
1046
  }
969
1047
  function validateGateInput(gate) {
970
- if (typeof gate !== "object" || gate === null || Object.keys(gate).length !== 4 || gate.schema !== 1 || typeof gate.generation !== "string" || !UUID.test(gate.generation) || typeof gate.version !== "string" || !VERSION.test(gate.version) || typeof gate.owner !== "string" || !UUID.test(gate.owner))
1048
+ if (typeof gate !== "object" || gate === null || Object.keys(gate).length !== 4 || gate.schema !== 1 || typeof gate.generation !== "string" || !isUuid(gate.generation) || typeof gate.version !== "string" || !VERSION.test(gate.version) || typeof gate.owner !== "string" || !isUuid(gate.owner))
971
1049
  throw new Error("Invalid update launch gate");
972
1050
  return Object.freeze({
973
1051
  schema: 1,
@@ -985,26 +1063,8 @@ function parseGate(value) {
985
1063
  return validateGateInput(record);
986
1064
  }
987
1065
  async function readUpdateLaunchGate(target) {
988
- const path = updateLaunchGatePath(target);
989
- let info;
990
- try {
991
- info = await lstat3(path);
992
- } catch (error) {
993
- if (error.code === "ENOENT")
994
- return;
995
- throw error;
996
- }
997
- if (!info.isFile() || info.isSymbolicLink() || info.size < 1 || info.size > MAX_GATE_BYTES)
998
- throw new Error("Invalid update launch gate");
999
- const file = await open5(path, constants5.O_RDONLY | (constants5.O_NOFOLLOW ?? 0));
1000
- try {
1001
- const opened = await file.stat();
1002
- if (!opened.isFile() || opened.size < 1 || opened.size > MAX_GATE_BYTES)
1003
- throw new Error("Invalid update launch gate");
1004
- return parseGate(JSON.parse(await file.readFile("utf8")));
1005
- } finally {
1006
- await file.close();
1007
- }
1066
+ const text = await readSmallStateFile(updateLaunchGatePath(target), MAX_GATE_BYTES, "Invalid update launch gate");
1067
+ return text === undefined ? undefined : parseGate(JSON.parse(text));
1008
1068
  }
1009
1069
  function sameGate(left, right) {
1010
1070
  return left?.schema === right.schema && left.generation === right.generation && left.version === right.version && left.owner === right.owner;
@@ -1043,7 +1103,7 @@ async function publishUpdateLaunchGate(target, helperLiveness, input, persistOwn
1043
1103
  const temporary = join8(dirname5(path), `.${basename4(path)}.${gate.generation}.${randomUUID4()}.tmp`);
1044
1104
  let renamed = false;
1045
1105
  try {
1046
- const file = await open5(temporary, constants5.O_WRONLY | constants5.O_CREAT | constants5.O_EXCL, 384);
1106
+ const file = await open6(temporary, constants6.O_WRONLY | constants6.O_CREAT | constants6.O_EXCL, 384);
1047
1107
  try {
1048
1108
  await file.writeFile(JSON.stringify(gate) + `
1049
1109
  `);
@@ -1284,15 +1344,15 @@ async function waitForUpdateLaunchGateWriter(target, expected, options) {
1284
1344
  // src/updates/update-helper.ts
1285
1345
  function parseUpdateHelperRequest(value) {
1286
1346
  const data = value;
1287
- if (typeof value !== "object" || value === null || Array.isArray(value) || data.schema !== 1 && data.schema !== 2 && data.schema !== 3 || typeof data.target !== "string" || !isAbsolute8(data.target) || resolve7(data.target) !== data.target || !["macos-arm64", "windows-x64", "linux-x64", "linux-arm64"].includes(data.platform) || typeof data.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(data.version) || !Array.isArray(data.processes) || data.schema === 2 && (typeof data.generation !== "string" || !/^[0-9a-f-]{36}$/i.test(data.generation) || data.helperOwner !== undefined && (typeof data.helperOwner !== "string" || !/^[0-9a-f-]{36}$/i.test(data.helperOwner))) || data.schema === 3 && (typeof data.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(data.generation) || typeof data.helperOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(data.helperOwner)) || !Number.isSafeInteger(data.processTimeoutMs) || data.processTimeoutMs < 1 || data.processTimeoutMs > 120000)
1347
+ if (typeof value !== "object" || value === null || Array.isArray(value) || data.schema !== 1 && data.schema !== 2 && data.schema !== 3 || !isCanonicalPath(data.target) || !["macos-arm64", "windows-x64", "linux-x64", "linux-arm64"].includes(data.platform) || typeof data.version !== "string" || !/^\d+\.\d+\.\d+(?:-dev\.\d+)?$/.test(data.version) || !Array.isArray(data.processes) || data.schema === 2 && (typeof data.generation !== "string" || !/^[0-9a-f-]{36}$/i.test(data.generation) || data.helperOwner !== undefined && (typeof data.helperOwner !== "string" || !/^[0-9a-f-]{36}$/i.test(data.helperOwner))) || data.schema === 3 && (typeof data.generation !== "string" || !isUuid(data.generation) || typeof data.helperOwner !== "string" || !isUuid(data.helperOwner)) || !Number.isSafeInteger(data.processTimeoutMs) || data.processTimeoutMs < 1 || data.processTimeoutMs > 120000)
1288
1348
  throw new Error("Invalid update helper request");
1289
1349
  return Object.freeze(data);
1290
1350
  }
1291
1351
  async function readRequest(staging) {
1292
- const path = join9(staging, "request.json"), info = await lstat4(path);
1352
+ const path = join9(staging, "request.json"), info = await lstat2(path);
1293
1353
  if (!info.isFile() || info.size < 1 || info.size > 65536)
1294
1354
  throw new Error("Invalid update helper request");
1295
- const file = await open6(path, constants6.O_RDONLY | (constants6.O_NOFOLLOW ?? 0));
1355
+ const file = await open7(path, constants7.O_RDONLY | (constants7.O_NOFOLLOW ?? 0));
1296
1356
  try {
1297
1357
  return parseUpdateHelperRequest(JSON.parse(await file.readFile("utf8")));
1298
1358
  } finally {
@@ -1322,10 +1382,10 @@ async function acknowledgeUpdateCancellation(staging, request) {
1322
1382
  }
1323
1383
  async function updatePathExists(path) {
1324
1384
  try {
1325
- await lstat4(path);
1385
+ await lstat2(path);
1326
1386
  return true;
1327
1387
  } catch (error) {
1328
- if (error.code === "ENOENT")
1388
+ if (errorCode(error) === "ENOENT")
1329
1389
  return false;
1330
1390
  throw error;
1331
1391
  }
@@ -1333,13 +1393,13 @@ async function updatePathExists(path) {
1333
1393
  async function runUpdateHelper(options) {
1334
1394
  const hasBoundIdentity = options.target !== undefined || options.generation !== undefined || options.helperOwner !== undefined;
1335
1395
  if (hasBoundIdentity) {
1336
- if (typeof options.target !== "string" || !isAbsolute8(options.target) || resolve7(options.target) !== options.target || options.target.includes("\x00") || !isAbsolute8(options.staging) || resolve7(options.staging) !== options.staging || options.staging.includes("\x00") || updateStagingFor(options.target) !== options.staging || typeof options.generation !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(options.generation) || typeof options.helperOwner !== "string" || !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(options.helperOwner))
1396
+ if (!isCanonicalPath(options.target) || !isCanonicalPath(options.staging) || updateStagingFor(options.target) !== options.staging || typeof options.generation !== "string" || !isUuid(options.generation) || typeof options.helperOwner !== "string" || !isUuid(options.helperOwner))
1337
1397
  throw new Error("Invalid update helper invocation");
1338
1398
  const liveness = await tryAcquireUpdateHelperLiveness(options.target);
1339
1399
  if (!liveness)
1340
1400
  throw new Error("Update helper liveness is already owned");
1341
1401
  try {
1342
- const stagingInfo = await lstat4(options.staging);
1402
+ const stagingInfo = await lstat2(options.staging);
1343
1403
  if (!stagingInfo.isDirectory() || stagingInfo.isSymbolicLink() || await realpath2(options.staging) !== options.staging)
1344
1404
  throw new Error("Invalid update staging directory");
1345
1405
  const request = await readRequest(options.staging);