@orkestrel/sea 0.0.3 → 0.0.5

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.
@@ -92,8 +92,6 @@ var PE_SCN_MEM_READ = 1073741824;
92
92
  var SEA_PLATFORMS = Object.freeze({
93
93
  win32: Object.freeze({
94
94
  executable: "node.exe",
95
- remove: void 0,
96
- sign: void 0,
97
95
  verify: Object.freeze([
98
96
  "signtool",
99
97
  "verify",
@@ -114,12 +112,7 @@ var SEA_PLATFORMS = Object.freeze({
114
112
  "--strict"
115
113
  ])
116
114
  }),
117
- linux: Object.freeze({
118
- executable: "node",
119
- remove: void 0,
120
- sign: void 0,
121
- verify: void 0
122
- })
115
+ linux: Object.freeze({ executable: "node" })
123
116
  });
124
117
  /** Maps a {@link SEACompressionMode} to its numeric Brotli mode value */
125
118
  var SEA_COMPRESSION_MODE_VALUES = Object.freeze({
@@ -154,7 +147,7 @@ var SEAError = class extends Error {
154
147
  super(message);
155
148
  this.name = "SEAError";
156
149
  this.code = code;
157
- this.context = context;
150
+ if (context !== void 0) this.context = context;
158
151
  }
159
152
  };
160
153
  /**
@@ -288,6 +281,24 @@ function walkDirectory(directory, base) {
288
281
  return result;
289
282
  }
290
283
  /**
284
+ * Redact password arguments from a shell command.
285
+ *
286
+ * @param command - Command arguments to redact
287
+ * @returns A copy with values following password flags replaced by a marker
288
+ */
289
+ function redactCommand(command) {
290
+ const secretFlags = /* @__PURE__ */ new Set([
291
+ "/p",
292
+ "-p",
293
+ "--password"
294
+ ]);
295
+ return command.map((token, index) => {
296
+ const previous = command[index - 1];
297
+ if (previous !== void 0 && secretFlags.has(previous.toLowerCase())) return "***";
298
+ return token;
299
+ });
300
+ }
301
+ /**
291
302
  * Run a command synchronously and return stdout.
292
303
  *
293
304
  * @param command - Command to execute (first element is the binary)
@@ -299,18 +310,6 @@ function walkDirectory(directory, base) {
299
310
  * @throws ShellError when the command exits with non-zero status
300
311
  */
301
312
  function runShell(command, options) {
302
- function redactCommand(argv) {
303
- const secretFlags = /* @__PURE__ */ new Set([
304
- "/p",
305
- "-p",
306
- "--password"
307
- ]);
308
- return argv.map((token, index) => {
309
- const previous = argv[index - 1];
310
- if (previous !== void 0 && secretFlags.has(previous.toLowerCase())) return "***";
311
- return token;
312
- });
313
- }
314
313
  const [cmd, ...args] = command;
315
314
  if (cmd === void 0) throw new SEAError("STATE", "Command array must not be empty");
316
315
  if (options?.signal?.aborted === true) throw new SEAError("ABORT", "Shell command aborted", { command: redactCommand(command) });
@@ -414,6 +413,15 @@ function compressDirectory(directory, options, progress) {
414
413
  };
415
414
  }
416
415
  /**
416
+ * Check whether a number is a nonzero power of two.
417
+ *
418
+ * @param value - Number to check
419
+ * @returns True when the value is a positive power of two
420
+ */
421
+ function isPowerOfTwo(value) {
422
+ return value > 0 && (value & value - 1) === 0;
423
+ }
424
+ /**
417
425
  * Parse the PE header offset from a Windows executable.
418
426
  *
419
427
  * @param fd - Open file descriptor
@@ -511,7 +519,8 @@ function stripPESignature(path) {
511
519
  if (certOffset <= fileSize && certOffset + certSize === fileSize) (0, node_fs.ftruncateSync)(fd, certOffset);
512
520
  }
513
521
  }
514
- (0, node_fs.writeSync)(fd, Buffer.alloc(8), 0, 8, securityDirOffset);
522
+ const zeroes = Buffer.alloc(8);
523
+ (0, node_fs.writeSync)(fd, zeroes, 0, 8, securityDirOffset);
515
524
  } finally {
516
525
  (0, node_fs.closeSync)(fd);
517
526
  }
@@ -592,8 +601,8 @@ function createSignCommand(sign, target) {
592
601
  */
593
602
  function formatSize(bytes) {
594
603
  if (bytes < 1024) return `${String(bytes)} B`;
595
- if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
596
- return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
604
+ if (bytes < 1048576) return `${(bytes / 1024).toFixed(1)} KB`;
605
+ return `${(bytes / 1048576).toFixed(2)} MB`;
597
606
  }
598
607
  /**
599
608
  * Assert that an asset key is safe to use as a relative filesystem/archive key.
@@ -808,6 +817,16 @@ function createBlobConfig(entry, blob, assets, options) {
808
817
  };
809
818
  }
810
819
  /**
820
+ * Align an ELF note component size to its four-byte boundary.
821
+ *
822
+ * @param value - Component size to align
823
+ * @returns The value rounded up to the next multiple of four
824
+ */
825
+ function alignELFNoteSize(value) {
826
+ const remainder = value % 4;
827
+ return remainder === 0 ? value : value + (4 - remainder);
828
+ }
829
+ /**
811
830
  * Build an ELF `PT_NOTE` entry's header bytes (namesz/descsz/type + padded
812
831
  * name) for the SEA blob note, without the blob body itself.
813
832
  *
@@ -831,18 +850,14 @@ function createBlobConfig(entry, blob, assets, options) {
831
850
  * ```
832
851
  */
833
852
  function buildELFNoteHeader(resource, blobSize) {
834
- const alignTo4 = (value) => {
835
- const remainder = value % 4;
836
- return remainder === 0 ? value : value + (4 - remainder);
837
- };
838
853
  const nameBytes = Buffer.from(`${resource}\0`, "utf-8");
839
- const alignedNameSize = alignTo4(nameBytes.length);
854
+ const alignedNameSize = alignELFNoteSize(nameBytes.length);
840
855
  const header = Buffer.alloc(12 + alignedNameSize);
841
856
  header.writeUInt32LE(nameBytes.length, 0);
842
857
  header.writeUInt32LE(blobSize, 4);
843
858
  header.writeUInt32LE(0, 8);
844
859
  nameBytes.copy(header, 12);
845
- const alignedDescSize = alignTo4(blobSize);
860
+ const alignedDescSize = alignELFNoteSize(blobSize);
846
861
  return {
847
862
  header,
848
863
  entryTotal: header.length + alignedDescSize
@@ -877,7 +892,7 @@ function buildELFNoteHeader(resource, blobSize) {
877
892
  * copyRange(srcFd, dstFd, 1024, 4096)
878
893
  * ```
879
894
  */
880
- function copyRange(source, target, start, length, chunk = 4 * 1024 * 1024) {
895
+ function copyRange(source, target, start, length, chunk = 4194304) {
881
896
  if (length <= 0) return;
882
897
  const bufferSize = Math.min(chunk, length);
883
898
  const buffer = Buffer.alloc(bufferSize);
@@ -911,7 +926,7 @@ function patchSentinelFuse(executable, fuse) {
911
926
  const fd = (0, node_fs.openSync)(executable, "r+");
912
927
  try {
913
928
  const needle = Buffer.from(fuse, "utf-8");
914
- const chunkSize = 64 * 1024 * 1024;
929
+ const chunkSize = 67108864;
915
930
  const readSize = chunkSize + needle.length;
916
931
  const buffer = Buffer.alloc(readSize);
917
932
  let offset = 0;
@@ -1016,9 +1031,7 @@ var Injector = class {
1016
1031
  case "elf":
1017
1032
  this.#injectElf();
1018
1033
  break;
1019
- case "macho":
1020
- this.#injectMacho();
1021
- break;
1034
+ case "macho": this.#injectMacho();
1022
1035
  }
1023
1036
  if (this.#options.fuse !== void 0) patchSentinelFuse(this.#options.executable, this.#options.fuse);
1024
1037
  if (peOptionalOffset !== void 0) this.#patchChecksum(peOptionalOffset);
@@ -1471,12 +1484,13 @@ var Injector = class {
1471
1484
  const fd = (0, node_fs.openSync)(this.#options.executable, "r+");
1472
1485
  try {
1473
1486
  const fileSize = (0, node_fs.statSync)(this.#options.executable).size;
1474
- const chunkSize = 4 * 1024 * 1024;
1487
+ const chunkSize = 4194304;
1475
1488
  const chunk = Buffer.alloc(Math.min(chunkSize, fileSize));
1476
1489
  let sum = 0;
1477
1490
  let offset = 0;
1478
1491
  while (offset < fileSize) {
1479
- const bytesRead = (0, node_fs.readSync)(fd, chunk, 0, Math.min(chunkSize, fileSize - offset), offset);
1492
+ const toRead = Math.min(chunkSize, fileSize - offset);
1493
+ const bytesRead = (0, node_fs.readSync)(fd, chunk, 0, toRead, offset);
1480
1494
  if (bytesRead === 0) break;
1481
1495
  for (let i = 0; i < bytesRead; i += 2) {
1482
1496
  const absolute = offset + i;
@@ -1840,32 +1854,30 @@ var Injector = class {
1840
1854
  }
1841
1855
  }
1842
1856
  #shiftMachoLinkeditOffsets(buf, cmd, threshold, shift) {
1843
- const bump = (fieldOffset) => {
1844
- if (fieldOffset + 4 > buf.length) return;
1845
- const value = buf.readUInt32LE(fieldOffset);
1846
- if (value >= threshold && value > 0) buf.writeUInt32LE(value + shift, fieldOffset);
1847
- };
1848
- const base = cmd.offset;
1857
+ let offsets;
1849
1858
  switch (cmd.type) {
1850
1859
  case 2:
1851
- bump(base + 8);
1852
- bump(base + 16);
1860
+ offsets = [8, 16];
1853
1861
  break;
1854
1862
  case 11:
1855
- bump(base + 32);
1856
- bump(base + 40);
1857
- bump(base + 48);
1858
- bump(base + 56);
1859
- bump(base + 64);
1860
- bump(base + 72);
1863
+ offsets = [
1864
+ 32,
1865
+ 40,
1866
+ 48,
1867
+ 56,
1868
+ 64,
1869
+ 72
1870
+ ];
1861
1871
  break;
1862
1872
  case 34:
1863
1873
  case 2147483682:
1864
- bump(base + 8);
1865
- bump(base + 16);
1866
- bump(base + 24);
1867
- bump(base + 32);
1868
- bump(base + 40);
1874
+ offsets = [
1875
+ 8,
1876
+ 16,
1877
+ 24,
1878
+ 32,
1879
+ 40
1880
+ ];
1869
1881
  break;
1870
1882
  case 29:
1871
1883
  case 30:
@@ -1873,9 +1885,15 @@ var Injector = class {
1873
1885
  case 41:
1874
1886
  case 2147483699:
1875
1887
  case 2147483700:
1876
- bump(base + 8);
1888
+ offsets = [8];
1877
1889
  break;
1878
- default: break;
1890
+ default: return;
1891
+ }
1892
+ for (const offset of offsets) {
1893
+ const fieldOffset = cmd.offset + offset;
1894
+ if (fieldOffset + 4 > buf.length) continue;
1895
+ const value = buf.readUInt32LE(fieldOffset);
1896
+ if (value >= threshold && value > 0) buf.writeUInt32LE(value + shift, fieldOffset);
1879
1897
  }
1880
1898
  }
1881
1899
  #verifyMachoSection(exePath, segmentName, sectionName, expectedOffset, expectedSize) {
@@ -1950,7 +1968,6 @@ var Injector = class {
1950
1968
  return remainder === 0 ? value : value + (alignment - remainder);
1951
1969
  }
1952
1970
  #ensureValidPEAlignment(sectionAlignment, fileAlignment) {
1953
- const isPowerOfTwo = (n) => n > 0 && (n & n - 1) === 0;
1954
1971
  if (!isPowerOfTwo(sectionAlignment) || !isPowerOfTwo(fileAlignment)) throw new SEAError("FORMAT", "PE section/file alignment must be a nonzero power of two", {
1955
1972
  executable: this.#options.executable,
1956
1973
  sectionAlignment,
@@ -1964,14 +1981,15 @@ var Injector = class {
1964
1981
  }
1965
1982
  /** Append sourceFile to targetFile by streaming in 4 MB chunks */
1966
1983
  #appendFile(targetPath, sourcePath) {
1967
- const chunkSize = 4 * 1024 * 1024;
1984
+ const chunkSize = 4194304;
1968
1985
  const srcFd = (0, node_fs.openSync)(sourcePath, "r");
1969
1986
  try {
1970
1987
  const srcSize = (0, node_fs.statSync)(sourcePath).size;
1971
1988
  const chunk = Buffer.alloc(Math.min(chunkSize, srcSize));
1972
1989
  let offset = 0;
1973
1990
  while (offset < srcSize) {
1974
- const bytesRead = (0, node_fs.readSync)(srcFd, chunk, 0, Math.min(chunkSize, srcSize - offset), offset);
1991
+ const toRead = Math.min(chunkSize, srcSize - offset);
1992
+ const bytesRead = (0, node_fs.readSync)(srcFd, chunk, 0, toRead, offset);
1975
1993
  if (bytesRead === 0) break;
1976
1994
  (0, node_fs.appendFileSync)(targetPath, chunk.subarray(0, bytesRead));
1977
1995
  offset += bytesRead;
@@ -1995,8 +2013,8 @@ var SEA = class {
1995
2013
  constructor(options) {
1996
2014
  this.#options = options;
1997
2015
  this.#emitter = new _orkestrel_emitter.Emitter({
1998
- on: options.on,
1999
- error: options.error
2016
+ ...options.on === void 0 ? {} : { on: options.on },
2017
+ ...options.error === void 0 ? {} : { error: options.error }
2000
2018
  });
2001
2019
  }
2002
2020
  get emitter() {
@@ -2033,10 +2051,10 @@ var SEA = class {
2033
2051
  platform: process.platform,
2034
2052
  size,
2035
2053
  duration,
2036
- compression,
2037
2054
  signed: assembled.signed,
2038
2055
  stripped: assembled.stripped,
2039
- terminal: assembled.terminal
2056
+ ...compression === void 0 ? {} : { compression },
2057
+ ...assembled.terminal === void 0 ? {} : { terminal: assembled.terminal }
2040
2058
  };
2041
2059
  this.#status = "done";
2042
2060
  this.#emitter.emit("complete", result);
@@ -2082,7 +2100,8 @@ var SEA = class {
2082
2100
  const directories = [];
2083
2101
  let total = 0;
2084
2102
  for (const path of compression.paths) {
2085
- if (!(0, node_fs.existsSync)((0, node_path.resolve)(root, path))) continue;
2103
+ const candidate = (0, node_path.resolve)(root, path);
2104
+ if (!(0, node_fs.existsSync)(candidate)) continue;
2086
2105
  this.#check();
2087
2106
  const directory = ensureContained(root, path);
2088
2107
  directories.push(directory);
@@ -2126,7 +2145,7 @@ var SEA = class {
2126
2145
  const blob = (0, node_path.join)(output, "sea-prep.blob");
2127
2146
  const config = createBlobConfig({
2128
2147
  path: entry,
2129
- format: this.#options.entry.format
2148
+ ...this.#options.entry.format === void 0 ? {} : { format: this.#options.entry.format }
2130
2149
  }, blob, this.#assets(root), this.#options.blob);
2131
2150
  (0, node_fs.writeFileSync)(configPath, JSON.stringify(config, null, 2));
2132
2151
  runShell([
@@ -2135,7 +2154,7 @@ var SEA = class {
2135
2154
  configPath
2136
2155
  ], {
2137
2156
  cwd: output,
2138
- signal: this.#options.signal
2157
+ ...this.#options.signal === void 0 ? {} : { signal: this.#options.signal }
2139
2158
  });
2140
2159
  if (!(0, node_fs.existsSync)(blob)) throw new SEAError("BLOB", "SEA blob generation failed — blob not found", { blob });
2141
2160
  this.#emitter.emit("blob", blob);
@@ -2146,8 +2165,10 @@ var SEA = class {
2146
2165
  if (platform === void 0) throw new SEAError("PLATFORM", `Unsupported platform: ${process.platform}`, { platform: process.platform });
2147
2166
  const output = (0, node_path.resolve)(root, this.#options.output);
2148
2167
  const ext = process.platform === "win32" ? ".exe" : "";
2149
- const finalOutput = (0, node_path.join)(output, process.platform === "win32" ? `${this.#options.name}.exe` : this.#options.name);
2168
+ const name = process.platform === "win32" ? `${this.#options.name}.exe` : this.#options.name;
2169
+ const finalOutput = (0, node_path.join)(output, name);
2150
2170
  const temp = (0, node_path.join)(output, `.${this.#options.name}-${(0, node_crypto.randomUUID)()}.tmp${ext}`);
2171
+ const shell = this.#options.signal === void 0 ? {} : { signal: this.#options.signal };
2151
2172
  let signed = false;
2152
2173
  let stripped = false;
2153
2174
  let terminal;
@@ -2159,7 +2180,7 @@ var SEA = class {
2159
2180
  if (platform.remove !== void 0) {
2160
2181
  this.#check();
2161
2182
  try {
2162
- runShell([...platform.remove, temp], { signal: this.#options.signal });
2183
+ runShell([...platform.remove, temp], shell);
2163
2184
  } catch (thrown) {
2164
2185
  throw new SEAError("SIGN", "Failed to strip existing signature", { cause: thrown instanceof Error ? thrown.message : String(thrown) });
2165
2186
  }
@@ -2176,14 +2197,14 @@ var SEA = class {
2176
2197
  if (platform.sign !== void 0) {
2177
2198
  this.#check();
2178
2199
  try {
2179
- runShell([...platform.sign, temp], { signal: this.#options.signal });
2200
+ runShell([...platform.sign, temp], shell);
2180
2201
  } catch (thrown) {
2181
2202
  throw new SEAError("SIGN", "Failed to sign executable", { cause: thrown instanceof Error ? thrown.message : String(thrown) });
2182
2203
  }
2183
2204
  if (platform.verify !== void 0) {
2184
2205
  this.#check();
2185
2206
  try {
2186
- runShell([...platform.verify, temp], { signal: this.#options.signal });
2207
+ runShell([...platform.verify, temp], shell);
2187
2208
  } catch (thrown) {
2188
2209
  throw new SEAError("SIGN", "Signature verification failed", { cause: thrown instanceof Error ? thrown.message : String(thrown) });
2189
2210
  }
@@ -2214,7 +2235,7 @@ var SEA = class {
2214
2235
  file: (0, node_path.resolve)(root, sign.file)
2215
2236
  } : sign, temp);
2216
2237
  try {
2217
- runShell(signArgs, { signal: this.#options.signal });
2238
+ runShell(signArgs, shell);
2218
2239
  } catch {
2219
2240
  throw new SEAError("SIGN", "Windows signing failed", { executable: temp });
2220
2241
  }
@@ -2222,7 +2243,7 @@ var SEA = class {
2222
2243
  if (platform.verify !== void 0) {
2223
2244
  this.#check();
2224
2245
  try {
2225
- runShell([...platform.verify, temp], { signal: this.#options.signal });
2246
+ runShell([...platform.verify, temp], shell);
2226
2247
  } catch {
2227
2248
  throw new SEAError("SIGN", "Signature verification failed", { executable: temp });
2228
2249
  }
@@ -2250,7 +2271,7 @@ var SEA = class {
2250
2271
  executable: finalOutput,
2251
2272
  signed,
2252
2273
  stripped,
2253
- terminal
2274
+ ...terminal === void 0 ? {} : { terminal }
2254
2275
  };
2255
2276
  }
2256
2277
  #assets(root) {
@@ -2288,8 +2309,8 @@ var AssetManager = class {
2288
2309
  constructor(options) {
2289
2310
  this.#root = options?.root ?? process.cwd();
2290
2311
  this.#emitter = new _orkestrel_emitter.Emitter({
2291
- on: options?.on,
2292
- error: options?.error
2312
+ ...options?.on === void 0 ? {} : { on: options.on },
2313
+ ...options?.error === void 0 ? {} : { error: options.error }
2293
2314
  });
2294
2315
  this.#loadSea();
2295
2316
  }
@@ -2498,6 +2519,7 @@ exports.SKIP_EXTENSIONS = SKIP_EXTENSIONS;
2498
2519
  exports.ShellError = ShellError;
2499
2520
  exports.WINDOWS_SUBSYSTEM_CONSOLE = WINDOWS_SUBSYSTEM_CONSOLE;
2500
2521
  exports.WINDOWS_SUBSYSTEM_GUI = WINDOWS_SUBSYSTEM_GUI;
2522
+ exports.alignELFNoteSize = alignELFNoteSize;
2501
2523
  exports.buildELFNoteHeader = buildELFNoteHeader;
2502
2524
  exports.compressDirectory = compressDirectory;
2503
2525
  exports.compressFile = compressFile;
@@ -2519,6 +2541,7 @@ exports.isCompressible = isCompressible;
2519
2541
  exports.isExecutableFormat = isExecutableFormat;
2520
2542
  exports.isPEExecutable = isPEExecutable;
2521
2543
  exports.isPlatformSupported = isPlatformSupported;
2544
+ exports.isPowerOfTwo = isPowerOfTwo;
2522
2545
  exports.isSEAError = isSEAError;
2523
2546
  exports.isShellError = isShellError;
2524
2547
  exports.openBrowser = openBrowser;
@@ -2527,6 +2550,7 @@ exports.patchPESubsystem = patchPESubsystem;
2527
2550
  exports.patchSentinelFuse = patchSentinelFuse;
2528
2551
  exports.platformConfig = platformConfig;
2529
2552
  exports.readU16 = readU16;
2553
+ exports.redactCommand = redactCommand;
2530
2554
  exports.runShell = runShell;
2531
2555
  exports.stripPESignature = stripPESignature;
2532
2556
  exports.syncDirectory = syncDirectory;