@orkestrel/sea 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/dist/src/server/index.cjs +561 -356
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +553 -211
- package/dist/src/server/index.d.ts +553 -211
- package/dist/src/server/index.js +546 -350
- package/dist/src/server/index.js.map +1 -1
- package/package.json +13 -15
package/dist/src/server/index.js
CHANGED
|
@@ -7,19 +7,19 @@ import { Emitter } from "@orkestrel/emitter";
|
|
|
7
7
|
import { getAssetKeys, getRawAsset, isSea } from "node:sea";
|
|
8
8
|
import { isArrayBuffer } from "@orkestrel/contract";
|
|
9
9
|
//#region src/server/constants.ts
|
|
10
|
-
/** SEA sentinel fuse value embedded in the Node.js binary */
|
|
10
|
+
/** Holds the SEA sentinel fuse value embedded in the Node.js binary */
|
|
11
11
|
var SEA_SENTINEL_FUSE = "NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2";
|
|
12
|
-
/**
|
|
12
|
+
/** Names the SEA blob resource in the executable */
|
|
13
13
|
var SEA_BLOB_RESOURCE = "NODE_SEA_BLOB";
|
|
14
|
-
/**
|
|
14
|
+
/** Holds the default Brotli compression quality level (maximum) */
|
|
15
15
|
var DEFAULT_SEA_COMPRESSION_QUALITY = 11;
|
|
16
|
-
/** Windows PE subsystem value
|
|
16
|
+
/** Holds the Windows PE subsystem value for a GUI application (no terminal window) */
|
|
17
17
|
var WINDOWS_SUBSYSTEM_GUI = 2;
|
|
18
|
-
/** Windows PE subsystem value
|
|
18
|
+
/** Holds the Windows PE subsystem value for a console application */
|
|
19
19
|
var WINDOWS_SUBSYSTEM_CONSOLE = 3;
|
|
20
|
-
/**
|
|
20
|
+
/** Names the file extension indicating Brotli compression */
|
|
21
21
|
var BROTLI_EXTENSION = ".br";
|
|
22
|
-
/**
|
|
22
|
+
/** Lists the file extensions Brotli compression skips */
|
|
23
23
|
var SKIP_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
24
24
|
".br",
|
|
25
25
|
".gz",
|
|
@@ -43,51 +43,55 @@ var SKIP_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
|
43
43
|
".zip",
|
|
44
44
|
".tar"
|
|
45
45
|
]);
|
|
46
|
-
/**
|
|
46
|
+
/** Names the default SEA entry point module format when none is specified */
|
|
47
47
|
var DEFAULT_ENTRY_FORMAT = "cjs";
|
|
48
|
-
/**
|
|
49
|
-
var CLIENT_ASSET_KEY_RAW = "client.html";
|
|
50
|
-
/** Asset key for the Brotli-compressed client HTML entry */
|
|
51
|
-
var CLIENT_ASSET_KEY_BR = "client.html.br";
|
|
52
|
-
/** DOS MZ header magic (first 2 bytes of a PE file) */
|
|
48
|
+
/** Holds the DOS MZ header magic (first 2 bytes of a PE file) */
|
|
53
49
|
var PE_MAGIC = 23117;
|
|
54
|
-
/** PE signature
|
|
50
|
+
/** Holds the PE signature, "PE\0\0" as a 32-bit value */
|
|
55
51
|
var PE_SIGNATURE = 17744;
|
|
56
|
-
/** PE32 optional header magic */
|
|
52
|
+
/** Holds the PE32 optional header magic */
|
|
57
53
|
var PE32_MAGIC = 267;
|
|
58
|
-
/** PE32+ (64-bit) optional header magic */
|
|
54
|
+
/** Holds the PE32+ (64-bit) optional header magic */
|
|
59
55
|
var PE32_PLUS_MAGIC = 523;
|
|
60
|
-
/** ELF magic
|
|
56
|
+
/** Holds the ELF magic, 0x7F 'E' 'L' 'F' as a 32-bit big-endian value */
|
|
61
57
|
var ELF_MAGIC = 2135247942;
|
|
62
|
-
/** ELF 64-bit class identifier */
|
|
58
|
+
/** Holds the ELF 64-bit class identifier */
|
|
63
59
|
var ELF_CLASS_64 = 2;
|
|
64
|
-
/** ELF little-endian data encoding */
|
|
60
|
+
/** Holds the ELF little-endian data encoding */
|
|
65
61
|
var ELF_DATA_LSB = 1;
|
|
66
|
-
/** ELF program header type
|
|
62
|
+
/** Holds the ELF program header type for a note segment */
|
|
67
63
|
var ELF_PT_NOTE = 4;
|
|
68
|
-
/**
|
|
64
|
+
/** Holds the ELF program header type for a loadable segment */
|
|
65
|
+
var ELF_PT_LOAD = 1;
|
|
66
|
+
/** Holds the ELF program header type for the program header table itself */
|
|
67
|
+
var ELF_PT_PHDR = 6;
|
|
68
|
+
/** Holds the ELF segment permission flag marking a segment readable */
|
|
69
|
+
var ELF_PF_R = 4;
|
|
70
|
+
/** Holds the page size an injected ELF segment is aligned to */
|
|
71
|
+
var ELF_PAGE_SIZE = 4096;
|
|
72
|
+
/** Holds the Mach-O 64-bit magic (little-endian) */
|
|
69
73
|
var MACHO_MAGIC_64 = 4277009103;
|
|
70
|
-
/** Mach-O LC_SEGMENT_64 load command */
|
|
74
|
+
/** Holds the Mach-O LC_SEGMENT_64 load command */
|
|
71
75
|
var MACHO_LC_SEGMENT_64 = 25;
|
|
72
|
-
/** PE resource type
|
|
76
|
+
/** Holds the PE resource type RT_RCDATA (raw data) */
|
|
73
77
|
var PE_RT_RCDATA = 10;
|
|
74
|
-
/**
|
|
78
|
+
/** Holds the size of IMAGE_RESOURCE_DIRECTORY in bytes */
|
|
75
79
|
var PE_RESOURCE_DIR_SIZE = 16;
|
|
76
|
-
/**
|
|
80
|
+
/** Holds the size of IMAGE_RESOURCE_DIRECTORY_ENTRY in bytes */
|
|
77
81
|
var PE_RESOURCE_ENTRY_SIZE = 8;
|
|
78
|
-
/**
|
|
82
|
+
/** Holds the size of IMAGE_RESOURCE_DATA_ENTRY in bytes */
|
|
79
83
|
var PE_RESOURCE_DATA_ENTRY_SIZE = 16;
|
|
80
|
-
/** PE section header size in bytes */
|
|
84
|
+
/** Holds the PE section header size in bytes */
|
|
81
85
|
var PE_SECTION_HEADER_SIZE = 40;
|
|
82
|
-
/**
|
|
86
|
+
/** Holds the high bit mask for a resource directory entry offset (indicates subdirectory) */
|
|
83
87
|
var PE_RESOURCE_SUBDIR_FLAG = 2147483648;
|
|
84
|
-
/**
|
|
88
|
+
/** Holds the high bit mask for a resource name entry (indicates named vs integer ID) */
|
|
85
89
|
var PE_RESOURCE_NAME_FLAG = 2147483648;
|
|
86
|
-
/**
|
|
90
|
+
/** Marks a section as containing initialized data */
|
|
87
91
|
var PE_SCN_INITIALIZED_DATA = 64;
|
|
88
|
-
/**
|
|
92
|
+
/** Marks a section as readable */
|
|
89
93
|
var PE_SCN_MEM_READ = 1073741824;
|
|
90
|
-
/**
|
|
94
|
+
/** Holds the platform-specific SEA build configurations */
|
|
91
95
|
var SEA_PLATFORMS = Object.freeze({
|
|
92
96
|
win32: Object.freeze({
|
|
93
97
|
executable: "node.exe",
|
|
@@ -122,7 +126,7 @@ var SEA_COMPRESSION_MODE_VALUES = Object.freeze({
|
|
|
122
126
|
//#endregion
|
|
123
127
|
//#region src/server/errors.ts
|
|
124
128
|
/**
|
|
125
|
-
*
|
|
129
|
+
* Represents the coded base error for every failure raised by the SEA build.
|
|
126
130
|
*
|
|
127
131
|
* @remarks
|
|
128
132
|
* Carries a machine-readable {@link SEAErrorCode} and optional `context`
|
|
@@ -131,7 +135,7 @@ var SEA_COMPRESSION_MODE_VALUES = Object.freeze({
|
|
|
131
135
|
* @example
|
|
132
136
|
* ```ts
|
|
133
137
|
* try {
|
|
134
|
-
*
|
|
138
|
+
* sea.execute()
|
|
135
139
|
* } catch (error) {
|
|
136
140
|
* if (isSEAError(error)) {
|
|
137
141
|
* console.error(error.code, error.context)
|
|
@@ -150,10 +154,10 @@ var SEAError = class extends Error {
|
|
|
150
154
|
}
|
|
151
155
|
};
|
|
152
156
|
/**
|
|
153
|
-
*
|
|
157
|
+
* Checks whether a value is a {@link SEAError}.
|
|
154
158
|
*
|
|
155
159
|
* @param value - The value to test
|
|
156
|
-
* @returns
|
|
160
|
+
* @returns True if `value` is a `SEAError`; false otherwise
|
|
157
161
|
*
|
|
158
162
|
* @example
|
|
159
163
|
* ```ts
|
|
@@ -166,7 +170,7 @@ function isSEAError(value) {
|
|
|
166
170
|
return value instanceof SEAError;
|
|
167
171
|
}
|
|
168
172
|
/**
|
|
169
|
-
*
|
|
173
|
+
* Represents an error thrown when a shell command executed through `executeShell` exits non-zero.
|
|
170
174
|
*
|
|
171
175
|
* @remarks
|
|
172
176
|
* Carries the captured `stdout` and `stderr` buffers from the failed process.
|
|
@@ -176,7 +180,7 @@ function isSEAError(value) {
|
|
|
176
180
|
* @example
|
|
177
181
|
* ```ts
|
|
178
182
|
* try {
|
|
179
|
-
*
|
|
183
|
+
* executeShell(['codesign', '--sign', '-', path])
|
|
180
184
|
* } catch (error) {
|
|
181
185
|
* if (isShellError(error)) {
|
|
182
186
|
* console.error(error.stderr.toString())
|
|
@@ -195,10 +199,10 @@ var ShellError = class extends SEAError {
|
|
|
195
199
|
}
|
|
196
200
|
};
|
|
197
201
|
/**
|
|
198
|
-
*
|
|
202
|
+
* Checks whether a value is a {@link ShellError}.
|
|
199
203
|
*
|
|
200
204
|
* @param value - The value to test
|
|
201
|
-
* @returns
|
|
205
|
+
* @returns True if `value` is a `ShellError`; false otherwise
|
|
202
206
|
*
|
|
203
207
|
* @example
|
|
204
208
|
* ```ts
|
|
@@ -211,56 +215,66 @@ function isShellError(value) {
|
|
|
211
215
|
return value instanceof ShellError;
|
|
212
216
|
}
|
|
213
217
|
//#endregion
|
|
214
|
-
//#region src/server/
|
|
218
|
+
//#region src/server/validators.ts
|
|
215
219
|
/**
|
|
216
|
-
*
|
|
220
|
+
* Checks if a value is a valid {@link ExecutableFormat}.
|
|
217
221
|
*
|
|
218
222
|
* @param value - Value to check
|
|
219
|
-
* @returns True
|
|
223
|
+
* @returns True if value is `'pe'`, `'elf'`, or `'macho'`; false otherwise
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* isExecutableFormat('elf') // true
|
|
228
|
+
* isExecutableFormat('coff') // false
|
|
229
|
+
* ```
|
|
220
230
|
*/
|
|
221
231
|
function isExecutableFormat(value) {
|
|
222
232
|
return value === "pe" || value === "elf" || value === "macho";
|
|
223
233
|
}
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/server/helpers.ts
|
|
224
236
|
/**
|
|
225
|
-
*
|
|
237
|
+
* Resolves the effective platform configuration.
|
|
226
238
|
*
|
|
227
239
|
* @param platform - Platform identifier. Default: `process.platform`.
|
|
228
240
|
* @returns Platform configuration, or undefined if unsupported
|
|
229
241
|
*/
|
|
230
|
-
function
|
|
242
|
+
function resolvePlatform(platform) {
|
|
231
243
|
return SEA_PLATFORMS[platform ?? process.platform];
|
|
232
244
|
}
|
|
233
245
|
/**
|
|
234
|
-
*
|
|
246
|
+
* Checks if the current or specified platform is supported for SEA builds.
|
|
235
247
|
*
|
|
236
248
|
* @param platform - Platform identifier to check
|
|
237
|
-
* @returns True
|
|
249
|
+
* @returns True if the platform has a known configuration; false otherwise
|
|
238
250
|
*/
|
|
239
251
|
function isPlatformSupported(platform) {
|
|
240
252
|
return (platform ?? process.platform) in SEA_PLATFORMS;
|
|
241
253
|
}
|
|
242
254
|
/**
|
|
243
|
-
*
|
|
255
|
+
* Asserts that a path exists, throwing a coded {@link SEAError} if not.
|
|
244
256
|
*
|
|
245
257
|
* @param path - Absolute or relative path to check
|
|
246
258
|
* @param message - Error message when path is missing
|
|
247
259
|
* @param code - Error code to throw with. Default: `'STATE'`.
|
|
260
|
+
* @throws SEAError with `code` (default `'STATE'`) when `path` does not exist
|
|
248
261
|
*/
|
|
249
262
|
function ensureExists(path, message, code) {
|
|
250
263
|
if (!existsSync(path)) throw new SEAError(code ?? "STATE", message, { path });
|
|
251
264
|
}
|
|
252
265
|
/**
|
|
253
|
-
*
|
|
266
|
+
* Checks whether a file's extension is outside `SKIP_EXTENSIONS`, so Brotli
|
|
267
|
+
* compression applies to it.
|
|
254
268
|
*
|
|
255
269
|
* @param path - Path to the file
|
|
256
|
-
* @returns True
|
|
270
|
+
* @returns True if the file type benefits from compression; false otherwise
|
|
257
271
|
*/
|
|
258
272
|
function isCompressible(path) {
|
|
259
273
|
const ext = extname(path).toLowerCase();
|
|
260
274
|
return !SKIP_EXTENSIONS.has(ext);
|
|
261
275
|
}
|
|
262
276
|
/**
|
|
263
|
-
*
|
|
277
|
+
* Walks a directory recursively and returns all file paths.
|
|
264
278
|
*
|
|
265
279
|
* @param directory - Directory to walk
|
|
266
280
|
* @param base - Base directory for relative path calculation
|
|
@@ -280,7 +294,7 @@ function walkDirectory(directory, base) {
|
|
|
280
294
|
return result;
|
|
281
295
|
}
|
|
282
296
|
/**
|
|
283
|
-
*
|
|
297
|
+
* Redacts password arguments from a shell command.
|
|
284
298
|
*
|
|
285
299
|
* @param command - Command arguments to redact
|
|
286
300
|
* @returns A copy with values following password flags replaced by a marker
|
|
@@ -298,7 +312,7 @@ function redactCommand(command) {
|
|
|
298
312
|
});
|
|
299
313
|
}
|
|
300
314
|
/**
|
|
301
|
-
*
|
|
315
|
+
* Executes a command synchronously and returns stdout.
|
|
302
316
|
*
|
|
303
317
|
* @param command - Command to execute (first element is the binary)
|
|
304
318
|
* @param options - Shell options (cwd, env, timeout, signal)
|
|
@@ -308,7 +322,7 @@ function redactCommand(command) {
|
|
|
308
322
|
* @throws SEAError with code `'TIMEOUT'` when the command exceeds `options.timeout`
|
|
309
323
|
* @throws ShellError when the command exits with non-zero status
|
|
310
324
|
*/
|
|
311
|
-
function
|
|
325
|
+
function executeShell(command, options) {
|
|
312
326
|
const [cmd, ...args] = command;
|
|
313
327
|
if (cmd === void 0) throw new SEAError("STATE", "Command array must not be empty");
|
|
314
328
|
if (options?.signal?.aborted === true) throw new SEAError("ABORT", "Shell command aborted", { command: redactCommand(command) });
|
|
@@ -329,7 +343,7 @@ function runShell(command, options) {
|
|
|
329
343
|
return Buffer.from(result.stdout, "utf-8");
|
|
330
344
|
}
|
|
331
345
|
/**
|
|
332
|
-
*
|
|
346
|
+
* Computes a size comparison between original and compressed byte counts.
|
|
333
347
|
*
|
|
334
348
|
* @param original - Original byte count
|
|
335
349
|
* @param compressed - Compressed byte count
|
|
@@ -343,12 +357,13 @@ function computeSize(original, compressed) {
|
|
|
343
357
|
};
|
|
344
358
|
}
|
|
345
359
|
/**
|
|
346
|
-
* Brotli-
|
|
360
|
+
* Brotli-compresses a single file, writing the output alongside it.
|
|
347
361
|
*
|
|
348
362
|
* @param input - Absolute path to the source file
|
|
349
363
|
* @param output - Absolute path for the compressed output
|
|
350
|
-
* @param options -
|
|
364
|
+
* @param options - Brotli options (quality, mode)
|
|
351
365
|
* @returns Compression result with size comparison
|
|
366
|
+
* @throws SEAError with code `'OUTPUT'` when `output` is an existing symlink
|
|
352
367
|
*/
|
|
353
368
|
function compressFile(input, output, options) {
|
|
354
369
|
const quality = options?.quality ?? 11;
|
|
@@ -373,10 +388,10 @@ function compressFile(input, output, options) {
|
|
|
373
388
|
};
|
|
374
389
|
}
|
|
375
390
|
/**
|
|
376
|
-
*
|
|
391
|
+
* Compresses all compressible files in a directory tree.
|
|
377
392
|
*
|
|
378
393
|
* @param directory - Absolute path to the directory
|
|
379
|
-
* @param options -
|
|
394
|
+
* @param options - Brotli options (quality, mode)
|
|
380
395
|
* @param progress - Optional callback invoked after each file is compressed
|
|
381
396
|
* @returns Compression manifest with all results
|
|
382
397
|
*/
|
|
@@ -400,39 +415,218 @@ function compressDirectory(directory, options, progress) {
|
|
|
400
415
|
};
|
|
401
416
|
}
|
|
402
417
|
/**
|
|
403
|
-
*
|
|
418
|
+
* Rounds a value up to the next multiple of an alignment boundary.
|
|
419
|
+
*
|
|
420
|
+
* @remarks
|
|
421
|
+
* The shared alignment leaf behind every binary-format writer in this package:
|
|
422
|
+
* PE section and file alignment, ELF note and page alignment, and Mach-O page
|
|
423
|
+
* alignment all round the same way. `alignment` must be a nonzero power of two
|
|
424
|
+
* ({@link isPowerOfTwo} is the guard the PE injector applies before using a
|
|
425
|
+
* value read out of a header as a divisor).
|
|
426
|
+
*
|
|
427
|
+
* @param value - Value to align
|
|
428
|
+
* @param alignment - Alignment boundary to round up to
|
|
429
|
+
* @returns The value rounded up to the next multiple of `alignment`
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```ts
|
|
433
|
+
* alignTo(10, 4) // 12
|
|
434
|
+
* alignTo(4096, 4096) // 4096 — already aligned
|
|
435
|
+
* ```
|
|
436
|
+
*/
|
|
437
|
+
function alignTo(value, alignment) {
|
|
438
|
+
const remainder = value % alignment;
|
|
439
|
+
return remainder === 0 ? value : value + (alignment - remainder);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Reads a 32-bit unsigned little-endian integer from a file descriptor.
|
|
443
|
+
*
|
|
444
|
+
* @param fd - Open file descriptor
|
|
445
|
+
* @param offset - Byte offset to read from
|
|
446
|
+
* @returns The 32-bit value
|
|
447
|
+
* @throws SEAError with code `'FORMAT'` when fewer than four bytes are readable at `offset`
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* ```ts
|
|
451
|
+
* const fd = openSync('app.exe', 'r')
|
|
452
|
+
* readU32(fd, 0x3c) // the PE header offset
|
|
453
|
+
* ```
|
|
454
|
+
*/
|
|
455
|
+
function readU32(fd, offset) {
|
|
456
|
+
const buf = Buffer.alloc(4);
|
|
457
|
+
const read = readSync(fd, buf, 0, 4, offset);
|
|
458
|
+
if (read < 4) throw new SEAError("FORMAT", "Short read", {
|
|
459
|
+
offset,
|
|
460
|
+
expected: 4,
|
|
461
|
+
read
|
|
462
|
+
});
|
|
463
|
+
return buf.readUInt32LE(0);
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Reads a 64-bit unsigned little-endian integer from a file descriptor.
|
|
467
|
+
*
|
|
468
|
+
* @param fd - Open file descriptor
|
|
469
|
+
* @param offset - Byte offset to read from
|
|
470
|
+
* @returns The 64-bit value, as a `bigint`
|
|
471
|
+
* @throws SEAError with code `'FORMAT'` when fewer than eight bytes are readable at `offset`
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```ts
|
|
475
|
+
* const fd = openSync('app', 'r')
|
|
476
|
+
* readU64(fd, 32) // the ELF program header table offset
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
479
|
+
function readU64(fd, offset) {
|
|
480
|
+
const buf = Buffer.alloc(8);
|
|
481
|
+
const read = readSync(fd, buf, 0, 8, offset);
|
|
482
|
+
if (read < 8) throw new SEAError("FORMAT", "Short read", {
|
|
483
|
+
offset,
|
|
484
|
+
expected: 8,
|
|
485
|
+
read
|
|
486
|
+
});
|
|
487
|
+
return buf.readBigUInt64LE(0);
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Writes a 32-bit unsigned little-endian integer to a file descriptor.
|
|
491
|
+
*
|
|
492
|
+
* @remarks
|
|
493
|
+
* Coerces `value` with `>>> 0` before writing, so a negative or out-of-range
|
|
494
|
+
* number lands as its unsigned 32-bit representation rather than throwing.
|
|
495
|
+
*
|
|
496
|
+
* @param fd - Open file descriptor
|
|
497
|
+
* @param offset - Byte offset to write at
|
|
498
|
+
* @param value - The 32-bit value to write
|
|
499
|
+
*
|
|
500
|
+
* @example
|
|
501
|
+
* ```ts
|
|
502
|
+
* const fd = openSync('app.exe', 'r+')
|
|
503
|
+
* writeU32(fd, 0x3c, 0x100)
|
|
504
|
+
* ```
|
|
505
|
+
*/
|
|
506
|
+
function writeU32(fd, offset, value) {
|
|
507
|
+
const buf = Buffer.alloc(4);
|
|
508
|
+
buf.writeUInt32LE(value >>> 0, 0);
|
|
509
|
+
writeSync(fd, buf, 0, 4, offset);
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Writes a 64-bit unsigned little-endian integer to a file descriptor.
|
|
513
|
+
*
|
|
514
|
+
* @param fd - Open file descriptor
|
|
515
|
+
* @param offset - Byte offset to write at
|
|
516
|
+
* @param value - The 64-bit value to write
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* ```ts
|
|
520
|
+
* const fd = openSync('app', 'r+')
|
|
521
|
+
* writeU64(fd, 32, 8192n)
|
|
522
|
+
* ```
|
|
523
|
+
*/
|
|
524
|
+
function writeU64(fd, offset, value) {
|
|
525
|
+
const buf = Buffer.alloc(8);
|
|
526
|
+
buf.writeBigUInt64LE(value, 0);
|
|
527
|
+
writeSync(fd, buf, 0, 8, offset);
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Appends a source file to a target file, streaming in fixed-size chunks.
|
|
531
|
+
*
|
|
532
|
+
* @remarks
|
|
533
|
+
* Reads `source` in `chunk`-sized reads and appends each chunk to `target`, so
|
|
534
|
+
* a multi-hundred-megabyte blob is never fully buffered. A read returning zero
|
|
535
|
+
* bytes ends the copy, which is the normal end-of-file signal for a source that
|
|
536
|
+
* shrank between the size read and the copy.
|
|
537
|
+
*
|
|
538
|
+
* @param target - Path of the file to append to
|
|
539
|
+
* @param source - Path of the file to append
|
|
540
|
+
* @param chunk - Read/append chunk size in bytes. Default: 4 MB.
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* ```ts
|
|
544
|
+
* appendFile('dist/sea/app', 'dist/sea/sea-prep.blob')
|
|
545
|
+
* ```
|
|
546
|
+
*/
|
|
547
|
+
function appendFile(target, source, chunk = 4194304) {
|
|
548
|
+
const fd = openSync(source, "r");
|
|
549
|
+
try {
|
|
550
|
+
const size = statSync(source).size;
|
|
551
|
+
const buffer = Buffer.alloc(Math.min(chunk, size));
|
|
552
|
+
let offset = 0;
|
|
553
|
+
while (offset < size) {
|
|
554
|
+
const toRead = Math.min(chunk, size - offset);
|
|
555
|
+
const bytesRead = readSync(fd, buffer, 0, toRead, offset);
|
|
556
|
+
if (bytesRead === 0) break;
|
|
557
|
+
appendFileSync(target, buffer.subarray(0, bytesRead));
|
|
558
|
+
offset += bytesRead;
|
|
559
|
+
}
|
|
560
|
+
} finally {
|
|
561
|
+
closeSync(fd);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Truncates a string at its first NUL character.
|
|
566
|
+
*
|
|
567
|
+
* @remarks
|
|
568
|
+
* Binary name fields (PE section names, Mach-O segment and section names, ELF
|
|
569
|
+
* note names) are fixed-width and NUL-padded, so the decoded string carries
|
|
570
|
+
* trailing NUL characters that no comparison against a literal name matches.
|
|
571
|
+
*
|
|
572
|
+
* @param value - Decoded fixed-width name field
|
|
573
|
+
* @returns The value up to, but excluding, its first NUL character
|
|
574
|
+
*
|
|
575
|
+
* @example
|
|
576
|
+
* ```ts
|
|
577
|
+
* stripTrailingNulls('.rsrc\0\0\0') // '.rsrc'
|
|
578
|
+
* ```
|
|
579
|
+
*/
|
|
580
|
+
function stripTrailingNulls(value) {
|
|
581
|
+
const index = value.indexOf("\0");
|
|
582
|
+
return index === -1 ? value : value.slice(0, index);
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Checks whether a number is a nonzero power of two.
|
|
404
586
|
*
|
|
405
587
|
* @param value - Number to check
|
|
406
|
-
* @returns True
|
|
588
|
+
* @returns True if the value is a positive power of two; false otherwise
|
|
407
589
|
*/
|
|
408
590
|
function isPowerOfTwo(value) {
|
|
409
591
|
return value > 0 && (value & value - 1) === 0;
|
|
410
592
|
}
|
|
411
593
|
/**
|
|
412
|
-
*
|
|
594
|
+
* Reads the PE header offset from a Windows executable.
|
|
413
595
|
*
|
|
414
596
|
* @param fd - Open file descriptor
|
|
415
597
|
* @returns The offset to the PE signature
|
|
598
|
+
* @throws SEAError with code `'FORMAT'` when fewer than four bytes are readable at `0x3c`
|
|
416
599
|
*/
|
|
417
|
-
function
|
|
600
|
+
function readPEOffset(fd) {
|
|
418
601
|
const buf = Buffer.alloc(4);
|
|
419
|
-
readSync(fd, buf, 0, 4, 60);
|
|
602
|
+
const read = readSync(fd, buf, 0, 4, 60);
|
|
603
|
+
if (read < 4) throw new SEAError("FORMAT", "Short read", {
|
|
604
|
+
offset: 60,
|
|
605
|
+
expected: 4,
|
|
606
|
+
read
|
|
607
|
+
});
|
|
420
608
|
return buf.readUInt32LE(0);
|
|
421
609
|
}
|
|
422
610
|
/**
|
|
423
|
-
*
|
|
611
|
+
* Reads a 16-bit unsigned integer from a file descriptor.
|
|
424
612
|
*
|
|
425
613
|
* @param fd - Open file descriptor
|
|
426
614
|
* @param offset - Byte offset to read from
|
|
427
615
|
* @returns The 16-bit value
|
|
616
|
+
* @throws SEAError with code `'FORMAT'` when fewer than two bytes are readable at `offset`
|
|
428
617
|
*/
|
|
429
618
|
function readU16(fd, offset) {
|
|
430
619
|
const buf = Buffer.alloc(2);
|
|
431
|
-
readSync(fd, buf, 0, 2, offset);
|
|
620
|
+
const read = readSync(fd, buf, 0, 2, offset);
|
|
621
|
+
if (read < 2) throw new SEAError("FORMAT", "Short read", {
|
|
622
|
+
offset,
|
|
623
|
+
expected: 2,
|
|
624
|
+
read
|
|
625
|
+
});
|
|
432
626
|
return buf.readUInt16LE(0);
|
|
433
627
|
}
|
|
434
628
|
/**
|
|
435
|
-
*
|
|
629
|
+
* Writes a 16-bit unsigned integer to a file descriptor.
|
|
436
630
|
*
|
|
437
631
|
* @param fd - Open file descriptor
|
|
438
632
|
* @param offset - Byte offset to write at
|
|
@@ -444,16 +638,16 @@ function writeU16(fd, offset, value) {
|
|
|
444
638
|
writeSync(fd, buf, 0, 2, offset);
|
|
445
639
|
}
|
|
446
640
|
/**
|
|
447
|
-
*
|
|
641
|
+
* Checks if a file is a Windows PE executable.
|
|
448
642
|
*
|
|
449
643
|
* @param path - Path to the file
|
|
450
|
-
* @returns True
|
|
644
|
+
* @returns True if the file has a valid PE signature; false otherwise
|
|
451
645
|
*/
|
|
452
646
|
function isPEExecutable(path) {
|
|
453
647
|
let fd;
|
|
454
648
|
try {
|
|
455
649
|
fd = openSync(path, "r");
|
|
456
|
-
const peOffset =
|
|
650
|
+
const peOffset = readPEOffset(fd);
|
|
457
651
|
const sig = Buffer.alloc(4);
|
|
458
652
|
readSync(fd, sig, 0, 4, peOffset);
|
|
459
653
|
return sig.toString("ascii") === "PE\0\0";
|
|
@@ -464,7 +658,7 @@ function isPEExecutable(path) {
|
|
|
464
658
|
}
|
|
465
659
|
}
|
|
466
660
|
/**
|
|
467
|
-
*
|
|
661
|
+
* Patches the PE subsystem field in a Windows executable.
|
|
468
662
|
*
|
|
469
663
|
* @param path - Path to the executable
|
|
470
664
|
* @param subsystem - Numeric subsystem value (2 = GUI, 3 = Console)
|
|
@@ -472,13 +666,13 @@ function isPEExecutable(path) {
|
|
|
472
666
|
function patchPESubsystem(path, subsystem) {
|
|
473
667
|
const fd = openSync(path, "r+");
|
|
474
668
|
try {
|
|
475
|
-
writeU16(fd,
|
|
669
|
+
writeU16(fd, readPEOffset(fd) + 92, subsystem);
|
|
476
670
|
} finally {
|
|
477
671
|
closeSync(fd);
|
|
478
672
|
}
|
|
479
673
|
}
|
|
480
674
|
/**
|
|
481
|
-
*
|
|
675
|
+
* Removes the Authenticode signature from a PE executable by zeroing
|
|
482
676
|
* the security directory entry in the optional header.
|
|
483
677
|
*
|
|
484
678
|
* @remarks
|
|
@@ -495,7 +689,7 @@ function patchPESubsystem(path, subsystem) {
|
|
|
495
689
|
function stripPESignature(path) {
|
|
496
690
|
const fd = openSync(path, "r+");
|
|
497
691
|
try {
|
|
498
|
-
const peOffset =
|
|
692
|
+
const peOffset = readPEOffset(fd);
|
|
499
693
|
const securityDirOffset = readU16(fd, peOffset + 24) === 523 ? peOffset + 168 : peOffset + 152;
|
|
500
694
|
const dirBuf = Buffer.alloc(8);
|
|
501
695
|
if (readSync(fd, dirBuf, 0, 8, securityDirOffset) === 8) {
|
|
@@ -513,21 +707,21 @@ function stripPESignature(path) {
|
|
|
513
707
|
}
|
|
514
708
|
}
|
|
515
709
|
/**
|
|
516
|
-
*
|
|
710
|
+
* Builds the `signtool sign` argv for signing a Windows executable.
|
|
517
711
|
*
|
|
518
712
|
* @remarks
|
|
519
713
|
* Requires EXACTLY ONE certificate source — `sign.file` (a `.pfx`/`.p12`
|
|
520
714
|
* file, paired with `sign.password` when present) XOR `sign.thumbprint` (a
|
|
521
715
|
* certificate already installed in the Windows store). When `sign.timestamp`
|
|
522
716
|
* is set it is parsed with the `URL` constructor and must be an `http:` or
|
|
523
|
-
* `https:` URL. The returned argv is passed directly to `
|
|
717
|
+
* `https:` URL. The returned argv is passed directly to `executeShell` — never
|
|
524
718
|
* through a shell — so nothing in `sign` can be interpreted as a flag or
|
|
525
719
|
* injected into a command line. `sign.password` is NEVER included in a
|
|
526
720
|
* thrown error's message or `context`.
|
|
527
721
|
*
|
|
528
722
|
* @param sign - Windows signing options
|
|
529
723
|
* @param target - Absolute path to the executable to sign
|
|
530
|
-
* @returns The `signtool` argv, ready for `
|
|
724
|
+
* @returns The `signtool` argv, ready for `executeShell`
|
|
531
725
|
* @throws SEAError with code `'SIGN'` when neither `file` nor `thumbprint` is set
|
|
532
726
|
* @throws SEAError with code `'SIGN'` when both `file` and `thumbprint` are set
|
|
533
727
|
* @throws SEAError with code `'SIGN'` when `timestamp` is not a parseable http(s) URL
|
|
@@ -535,11 +729,11 @@ function stripPESignature(path) {
|
|
|
535
729
|
*
|
|
536
730
|
* @example
|
|
537
731
|
* ```ts
|
|
538
|
-
*
|
|
732
|
+
* buildSignCommand({ thumbprint: 'AABBCCDDEEFF00112233445566778899AABBCCDD' }, 'dist/sea/app.exe')
|
|
539
733
|
* // ['signtool', 'sign', '/fd', 'sha256', '/sha1', 'AABBCCDDEEFF00112233445566778899AABBCCDD', 'dist/sea/app.exe']
|
|
540
734
|
* ```
|
|
541
735
|
*/
|
|
542
|
-
function
|
|
736
|
+
function buildSignCommand(sign, target) {
|
|
543
737
|
const hasFile = sign.file !== void 0;
|
|
544
738
|
const hasThumbprint = sign.thumbprint !== void 0;
|
|
545
739
|
if (!hasFile && !hasThumbprint) throw new SEAError("SIGN", "Windows signing requires exactly one of sign.file or sign.thumbprint");
|
|
@@ -581,7 +775,7 @@ function createSignCommand(sign, target) {
|
|
|
581
775
|
];
|
|
582
776
|
}
|
|
583
777
|
/**
|
|
584
|
-
*
|
|
778
|
+
* Formats a byte count as a human-readable string.
|
|
585
779
|
*
|
|
586
780
|
* @param bytes - Byte count
|
|
587
781
|
* @returns Formatted string (e.g. `"1.23 MB"`, `"456 KB"`)
|
|
@@ -592,7 +786,7 @@ function formatSize(bytes) {
|
|
|
592
786
|
return `${(bytes / 1048576).toFixed(2)} MB`;
|
|
593
787
|
}
|
|
594
788
|
/**
|
|
595
|
-
*
|
|
789
|
+
* Asserts that an asset key is safe to use as a relative filesystem/archive key.
|
|
596
790
|
*
|
|
597
791
|
* @remarks
|
|
598
792
|
* Rejects an empty key, an absolute path, a key containing a backslash, a
|
|
@@ -617,7 +811,7 @@ function ensureSafeKey(key) {
|
|
|
617
811
|
if (key.split("/").includes("..")) throw new SEAError("ASSET", "Asset key must not contain a \"..\" segment", { key });
|
|
618
812
|
}
|
|
619
813
|
/**
|
|
620
|
-
*
|
|
814
|
+
* Asserts that `path` (resolved against `base`) real-path-resolves to a
|
|
621
815
|
* location inside `base`, defeating a symlink escape.
|
|
622
816
|
*
|
|
623
817
|
* @remarks
|
|
@@ -662,7 +856,7 @@ function ensureContained(base, path) {
|
|
|
662
856
|
return real;
|
|
663
857
|
}
|
|
664
858
|
/**
|
|
665
|
-
*
|
|
859
|
+
* Asserts that `name` is a single safe path segment suitable as an output
|
|
666
860
|
* executable base name.
|
|
667
861
|
*
|
|
668
862
|
* @remarks
|
|
@@ -687,7 +881,7 @@ function ensureSafeName(name) {
|
|
|
687
881
|
if (/^[A-Za-z]:/.test(name)) throw new SEAError("ASSET", "Executable name must not be a drive-relative specifier", { name });
|
|
688
882
|
}
|
|
689
883
|
/**
|
|
690
|
-
*
|
|
884
|
+
* Fsyncs a directory to durably persist a prior file rename/create within it.
|
|
691
885
|
*
|
|
692
886
|
* @remarks
|
|
693
887
|
* A `rename`/`create` is only durable once its CONTAINING directory entry is
|
|
@@ -728,7 +922,7 @@ function syncDirectory(path) {
|
|
|
728
922
|
}
|
|
729
923
|
}
|
|
730
924
|
/**
|
|
731
|
-
*
|
|
925
|
+
* Finalizes a built executable by durably flushing it to disk and atomically
|
|
732
926
|
* moving it into place.
|
|
733
927
|
*
|
|
734
928
|
* @remarks
|
|
@@ -766,7 +960,7 @@ function finalizeExecutable(source, target) {
|
|
|
766
960
|
syncDirectory(dirname(target));
|
|
767
961
|
}
|
|
768
962
|
/**
|
|
769
|
-
*
|
|
963
|
+
* Builds the Node.js `--experimental-sea-config` JSON object for a SEA blob.
|
|
770
964
|
*
|
|
771
965
|
* @remarks
|
|
772
966
|
* A pure leaf extracted from the SEA build orchestrator. The `mainFormat`
|
|
@@ -786,10 +980,10 @@ function finalizeExecutable(source, target) {
|
|
|
786
980
|
*
|
|
787
981
|
* @example
|
|
788
982
|
* ```ts
|
|
789
|
-
* const config =
|
|
983
|
+
* const config = buildBlobConfig({ path: 'dist/bin/serve.cjs' }, 'dist/bin/sea-prep.blob', undefined)
|
|
790
984
|
* ```
|
|
791
985
|
*/
|
|
792
|
-
function
|
|
986
|
+
function buildBlobConfig(entry, blob, assets, options) {
|
|
793
987
|
const format = entry.format ?? "cjs";
|
|
794
988
|
const snapshot = options?.snapshot ?? false;
|
|
795
989
|
if (format === "esm" && snapshot) throw new SEAError("BLOB", "useSnapshot cannot be combined with an ESM entry (mainFormat module)");
|
|
@@ -804,17 +998,16 @@ function createBlobConfig(entry, blob, assets, options) {
|
|
|
804
998
|
};
|
|
805
999
|
}
|
|
806
1000
|
/**
|
|
807
|
-
*
|
|
1001
|
+
* Aligns an ELF note component size to its four-byte boundary.
|
|
808
1002
|
*
|
|
809
1003
|
* @param value - Component size to align
|
|
810
1004
|
* @returns The value rounded up to the next multiple of four
|
|
811
1005
|
*/
|
|
812
1006
|
function alignELFNoteSize(value) {
|
|
813
|
-
|
|
814
|
-
return remainder === 0 ? value : value + (4 - remainder);
|
|
1007
|
+
return alignTo(value, 4);
|
|
815
1008
|
}
|
|
816
1009
|
/**
|
|
817
|
-
*
|
|
1010
|
+
* Builds an ELF `PT_NOTE` entry's header bytes (namesz/descsz/type + padded
|
|
818
1011
|
* name) for the SEA blob note, without the blob body itself.
|
|
819
1012
|
*
|
|
820
1013
|
* @remarks
|
|
@@ -833,7 +1026,7 @@ function alignELFNoteSize(value) {
|
|
|
833
1026
|
*
|
|
834
1027
|
* @example
|
|
835
1028
|
* ```ts
|
|
836
|
-
* const { header,
|
|
1029
|
+
* const { header, total } = buildELFNoteHeader('NODE_SEA_BLOB', 4096)
|
|
837
1030
|
* ```
|
|
838
1031
|
*/
|
|
839
1032
|
function buildELFNoteHeader(resource, blobSize) {
|
|
@@ -847,11 +1040,11 @@ function buildELFNoteHeader(resource, blobSize) {
|
|
|
847
1040
|
const alignedDescSize = alignELFNoteSize(blobSize);
|
|
848
1041
|
return {
|
|
849
1042
|
header,
|
|
850
|
-
|
|
1043
|
+
total: header.length + alignedDescSize
|
|
851
1044
|
};
|
|
852
1045
|
}
|
|
853
1046
|
/**
|
|
854
|
-
*
|
|
1047
|
+
* Copies a byte range from one open file descriptor to another, streaming in
|
|
855
1048
|
* fixed-size chunks instead of buffering the whole range in memory.
|
|
856
1049
|
*
|
|
857
1050
|
* @remarks
|
|
@@ -899,7 +1092,7 @@ function copyRange(source, target, start, length, chunk = 4194304) {
|
|
|
899
1092
|
}
|
|
900
1093
|
}
|
|
901
1094
|
/**
|
|
902
|
-
*
|
|
1095
|
+
* Patches the sentinel fuse in a binary from `:0` to `:1`.
|
|
903
1096
|
*
|
|
904
1097
|
* Searches the file in 64 MB chunks with overlap to handle any file size.
|
|
905
1098
|
* The fuse signals to the Node.js runtime that a SEA blob is present.
|
|
@@ -908,6 +1101,9 @@ function copyRange(source, target, start, length, chunk = 4194304) {
|
|
|
908
1101
|
*
|
|
909
1102
|
* @param executable - Absolute path to the executable
|
|
910
1103
|
* @param fuse - Sentinel fuse string (without the `:0` suffix)
|
|
1104
|
+
* @throws SEAError with code `'FUSE'` when the fuse value cannot be read
|
|
1105
|
+
* @throws SEAError with code `'FUSE'` when the fuse carries a value other than `:0` or `:1`
|
|
1106
|
+
* @throws SEAError with code `'FUSE'` when the fuse is not found in the executable
|
|
911
1107
|
*/
|
|
912
1108
|
function patchSentinelFuse(executable, fuse) {
|
|
913
1109
|
const fd = openSync(executable, "r+");
|
|
@@ -950,7 +1146,7 @@ function patchSentinelFuse(executable, fuse) {
|
|
|
950
1146
|
}
|
|
951
1147
|
}
|
|
952
1148
|
/**
|
|
953
|
-
*
|
|
1149
|
+
* Launches the system default browser at an http(s) URL.
|
|
954
1150
|
*
|
|
955
1151
|
* @remarks
|
|
956
1152
|
* A best-effort launch for bundled local-UI apps: dispatches by
|
|
@@ -999,6 +1195,32 @@ function openBrowser(url) {
|
|
|
999
1195
|
}
|
|
1000
1196
|
//#endregion
|
|
1001
1197
|
//#region src/server/injectors/Injector.ts
|
|
1198
|
+
/**
|
|
1199
|
+
* Writes a named resource into a PE, ELF, or Mach-O executable in place.
|
|
1200
|
+
*
|
|
1201
|
+
* @remarks
|
|
1202
|
+
* The format is detected from the file's magic bytes at construction, and each
|
|
1203
|
+
* format takes its own strategy: PE adds an `RT_RCDATA` resource by rebuilding the
|
|
1204
|
+
* resource directory, ELF appends a `PT_NOTE` segment carrying the blob as note
|
|
1205
|
+
* data, and Mach-O appends an `LC_SEGMENT_64` load command with one section. The
|
|
1206
|
+
* blob is streamed from disk in every case, so its size is bounded by the
|
|
1207
|
+
* filesystem rather than by memory.
|
|
1208
|
+
*
|
|
1209
|
+
* @param options - Injector options: the target executable, the resource name, the
|
|
1210
|
+
* blob path, and the optional sentinel fuse and Mach-O segment
|
|
1211
|
+
*
|
|
1212
|
+
* @example
|
|
1213
|
+
* ```ts
|
|
1214
|
+
* const injector = new Injector({
|
|
1215
|
+
* executable: 'dist/sea/myapp.exe',
|
|
1216
|
+
* resource: 'NODE_SEA_BLOB',
|
|
1217
|
+
* blob: 'dist/sea/sea-prep.blob',
|
|
1218
|
+
* fuse: 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
|
|
1219
|
+
* })
|
|
1220
|
+
* injector.format // 'pe' | 'elf' | 'macho'
|
|
1221
|
+
* injector.inject()
|
|
1222
|
+
* ```
|
|
1223
|
+
*/
|
|
1002
1224
|
var Injector = class {
|
|
1003
1225
|
#options;
|
|
1004
1226
|
#format;
|
|
@@ -1013,12 +1235,12 @@ var Injector = class {
|
|
|
1013
1235
|
let peOptionalOffset;
|
|
1014
1236
|
switch (this.#format) {
|
|
1015
1237
|
case "pe":
|
|
1016
|
-
peOptionalOffset = this.#
|
|
1238
|
+
peOptionalOffset = this.#injectPE();
|
|
1017
1239
|
break;
|
|
1018
1240
|
case "elf":
|
|
1019
|
-
this.#
|
|
1241
|
+
this.#injectELF();
|
|
1020
1242
|
break;
|
|
1021
|
-
case "macho": this.#
|
|
1243
|
+
case "macho": this.#injectMachO();
|
|
1022
1244
|
}
|
|
1023
1245
|
if (this.#options.fuse !== void 0) patchSentinelFuse(this.#options.executable, this.#options.fuse);
|
|
1024
1246
|
if (peOptionalOffset !== void 0) this.#patchChecksum(peOptionalOffset);
|
|
@@ -1050,32 +1272,32 @@ var Injector = class {
|
|
|
1050
1272
|
closeSync(fd);
|
|
1051
1273
|
}
|
|
1052
1274
|
}
|
|
1053
|
-
#
|
|
1275
|
+
#injectPE() {
|
|
1054
1276
|
const fd = openSync(this.#options.executable, "r+");
|
|
1055
1277
|
try {
|
|
1056
|
-
const peOffset =
|
|
1057
|
-
const sig =
|
|
1278
|
+
const peOffset = readU32(fd, 60);
|
|
1279
|
+
const sig = readU32(fd, peOffset);
|
|
1058
1280
|
if (sig !== 17744) throw new SEAError("FORMAT", "Invalid PE signature", {
|
|
1059
1281
|
executable: this.#options.executable,
|
|
1060
1282
|
signature: sig
|
|
1061
1283
|
});
|
|
1062
1284
|
const coffOffset = peOffset + 4;
|
|
1063
|
-
const numberOfSections =
|
|
1064
|
-
const optionalHeaderSize =
|
|
1285
|
+
const numberOfSections = readU16(fd, coffOffset + 2);
|
|
1286
|
+
const optionalHeaderSize = readU16(fd, coffOffset + 16);
|
|
1065
1287
|
const optionalOffset = coffOffset + 20;
|
|
1066
|
-
const optMagic =
|
|
1288
|
+
const optMagic = readU16(fd, optionalOffset);
|
|
1067
1289
|
const is64 = optMagic === 523;
|
|
1068
1290
|
if (optMagic !== 267 && optMagic !== 523) throw new SEAError("FORMAT", `Unsupported PE optional header magic: 0x${optMagic.toString(16)}`, {
|
|
1069
1291
|
executable: this.#options.executable,
|
|
1070
1292
|
optionalHeaderMagic: optMagic
|
|
1071
1293
|
});
|
|
1072
|
-
const sectionAlignment =
|
|
1073
|
-
const fileAlignment =
|
|
1294
|
+
const sectionAlignment = readU32(fd, optionalOffset + 32);
|
|
1295
|
+
const fileAlignment = readU32(fd, optionalOffset + 36);
|
|
1074
1296
|
this.#ensureValidPEAlignment(sectionAlignment, fileAlignment);
|
|
1075
1297
|
const sizeOfImageOffset = optionalOffset + 56;
|
|
1076
1298
|
const resourceDirRvaOffset = optionalOffset + (is64 ? 112 : 96) + 16;
|
|
1077
1299
|
const resourceDirSizeOffset = resourceDirRvaOffset + 4;
|
|
1078
|
-
const
|
|
1300
|
+
const existingResourceRVA = readU32(fd, resourceDirRvaOffset);
|
|
1079
1301
|
const sectionTableOffset = optionalOffset + optionalHeaderSize;
|
|
1080
1302
|
const sections = [];
|
|
1081
1303
|
let rsrcSectionIndex = -1;
|
|
@@ -1083,12 +1305,12 @@ var Injector = class {
|
|
|
1083
1305
|
const off = sectionTableOffset + i * 40;
|
|
1084
1306
|
const nameBuf = Buffer.alloc(8);
|
|
1085
1307
|
readSync(fd, nameBuf, 0, 8, off);
|
|
1086
|
-
const name =
|
|
1087
|
-
const virtualSize =
|
|
1088
|
-
const virtualAddress =
|
|
1089
|
-
const rawSize =
|
|
1090
|
-
const rawOffset =
|
|
1091
|
-
const characteristics =
|
|
1308
|
+
const name = stripTrailingNulls(nameBuf.toString("ascii"));
|
|
1309
|
+
const virtualSize = readU32(fd, off + 8);
|
|
1310
|
+
const virtualAddress = readU32(fd, off + 12);
|
|
1311
|
+
const rawSize = readU32(fd, off + 16);
|
|
1312
|
+
const rawOffset = readU32(fd, off + 20);
|
|
1313
|
+
const characteristics = readU32(fd, off + 36);
|
|
1092
1314
|
sections.push({
|
|
1093
1315
|
name,
|
|
1094
1316
|
virtualSize,
|
|
@@ -1098,19 +1320,19 @@ var Injector = class {
|
|
|
1098
1320
|
characteristics,
|
|
1099
1321
|
headerOffset: off
|
|
1100
1322
|
});
|
|
1101
|
-
if (
|
|
1323
|
+
if (existingResourceRVA >= virtualAddress && existingResourceRVA < virtualAddress + Math.max(virtualSize, rawSize)) rsrcSectionIndex = i;
|
|
1102
1324
|
}
|
|
1103
1325
|
const sectionTableEnd = sectionTableOffset + numberOfSections * 40;
|
|
1104
1326
|
const availableHeaderSpace = sections.reduce((min, s) => s.rawOffset > 0 && s.rawOffset < min ? s.rawOffset : min, Infinity) - sectionTableEnd;
|
|
1105
|
-
if (availableHeaderSpace < 40) throw new SEAError("
|
|
1327
|
+
if (availableHeaderSpace < 40) throw new SEAError("ROOM", `No room in PE header for a new section entry (${String(availableHeaderSpace)} bytes available, need ${String(40)})`, {
|
|
1106
1328
|
executable: this.#options.executable,
|
|
1107
1329
|
availableHeaderSpace,
|
|
1108
1330
|
requiredHeaderSpace: 40
|
|
1109
1331
|
});
|
|
1110
1332
|
const existingLeaves = [];
|
|
1111
|
-
if (rsrcSectionIndex !== -1 &&
|
|
1333
|
+
if (rsrcSectionIndex !== -1 && existingResourceRVA !== 0) {
|
|
1112
1334
|
const rsrc = sections[rsrcSectionIndex];
|
|
1113
|
-
if (rsrc !== void 0) this.#parseResourceDirectory(fd, rsrc.rawOffset,
|
|
1335
|
+
if (rsrc !== void 0) this.#parseResourceDirectory(fd, rsrc.rawOffset, existingResourceRVA, 0, existingLeaves, 0, 0, void 0, 0, void 0);
|
|
1114
1336
|
}
|
|
1115
1337
|
const overwrite = this.#options.overwrite !== false;
|
|
1116
1338
|
const upperName = this.#options.resource.toUpperCase();
|
|
@@ -1123,7 +1345,7 @@ var Injector = class {
|
|
|
1123
1345
|
for (let i = 0; i < filteredLeaves.length; i++) {
|
|
1124
1346
|
const leaf = filteredLeaves[i];
|
|
1125
1347
|
if (leaf === void 0) continue;
|
|
1126
|
-
const fileOffset = this.#rvaToFileOffset(leaf.
|
|
1348
|
+
const fileOffset = this.#rvaToFileOffset(leaf.dataRVA, sections);
|
|
1127
1349
|
if (fileOffset >= 0) {
|
|
1128
1350
|
const buf = Buffer.alloc(leaf.dataSize);
|
|
1129
1351
|
readSync(fd, buf, 0, leaf.dataSize, fileOffset);
|
|
@@ -1134,16 +1356,16 @@ var Injector = class {
|
|
|
1134
1356
|
const tree = this.#buildResourceTree(filteredLeaves, upperName, blobSize);
|
|
1135
1357
|
const { directoryBuffer, dataRegionSize } = this.#serializeResourceTree(tree, leafDataMap, blobSize);
|
|
1136
1358
|
const totalRawSize = directoryBuffer.length + dataRegionSize;
|
|
1137
|
-
const alignedRawSize =
|
|
1359
|
+
const alignedRawSize = alignTo(totalRawSize, fileAlignment);
|
|
1138
1360
|
let highestVaEnd = 0;
|
|
1139
1361
|
for (const s of sections) {
|
|
1140
1362
|
const end = s.virtualAddress + Math.max(s.virtualSize, s.rawSize);
|
|
1141
1363
|
if (end > highestVaEnd) highestVaEnd = end;
|
|
1142
1364
|
}
|
|
1143
|
-
const newVa =
|
|
1365
|
+
const newVa = alignTo(highestVaEnd, sectionAlignment);
|
|
1144
1366
|
const newVirtualSize = totalRawSize;
|
|
1145
1367
|
const fileSize = statSync(this.#options.executable).size;
|
|
1146
|
-
const newRawOffset =
|
|
1368
|
+
const newRawOffset = alignTo(fileSize, fileAlignment);
|
|
1147
1369
|
const newHeaderOffset = sectionTableEnd;
|
|
1148
1370
|
const headerBuf = Buffer.alloc(40);
|
|
1149
1371
|
headerBuf.write(".rsrc2", 0, 6, "ascii");
|
|
@@ -1157,28 +1379,27 @@ var Injector = class {
|
|
|
1157
1379
|
headerBuf.writeUInt16LE(0, 34);
|
|
1158
1380
|
headerBuf.writeUInt32LE(64 | PE_SCN_MEM_READ, 36);
|
|
1159
1381
|
writeSync(fd, headerBuf, 0, 40, newHeaderOffset);
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
this.#writeU32(fd, sizeOfImageOffset, newSizeOfImage);
|
|
1382
|
+
writeU16(fd, coffOffset + 2, numberOfSections + 1);
|
|
1383
|
+
writeU32(fd, resourceDirRvaOffset, newVa);
|
|
1384
|
+
writeU32(fd, resourceDirSizeOffset, directoryBuffer.length);
|
|
1385
|
+
writeU32(fd, sizeOfImageOffset, alignTo(newVa + Math.max(newVirtualSize, alignedRawSize), sectionAlignment));
|
|
1165
1386
|
closeSync(fd);
|
|
1166
1387
|
const currentFileSize = statSync(this.#options.executable).size;
|
|
1167
1388
|
if (newRawOffset > currentFileSize) {
|
|
1168
1389
|
const padding = Buffer.alloc(newRawOffset - currentFileSize);
|
|
1169
1390
|
appendFileSync(this.#options.executable, padding);
|
|
1170
1391
|
}
|
|
1171
|
-
this.#
|
|
1392
|
+
this.#fixupDataEntries(directoryBuffer, 0, newVa);
|
|
1172
1393
|
appendFileSync(this.#options.executable, directoryBuffer);
|
|
1173
1394
|
for (let i = 0; i < filteredLeaves.length; i++) {
|
|
1174
1395
|
const data = leafDataMap.get(i);
|
|
1175
1396
|
if (data !== void 0) {
|
|
1176
1397
|
appendFileSync(this.#options.executable, data);
|
|
1177
|
-
const padLen =
|
|
1398
|
+
const padLen = alignTo(data.length, 4) - data.length;
|
|
1178
1399
|
if (padLen > 0) appendFileSync(this.#options.executable, Buffer.alloc(padLen));
|
|
1179
1400
|
}
|
|
1180
1401
|
}
|
|
1181
|
-
|
|
1402
|
+
appendFile(this.#options.executable, this.#options.blob);
|
|
1182
1403
|
const writtenSize = statSync(this.#options.executable).size - newRawOffset;
|
|
1183
1404
|
if (writtenSize < alignedRawSize) appendFileSync(this.#options.executable, Buffer.alloc(alignedRawSize - writtenSize));
|
|
1184
1405
|
return optionalOffset;
|
|
@@ -1191,11 +1412,11 @@ var Injector = class {
|
|
|
1191
1412
|
}
|
|
1192
1413
|
#parseResourceDirectory(fd, sectionFileOffset, sectionRva, dirOffset, leaves, depth, currentTypeId, currentTypeName, currentNameId, currentNameName) {
|
|
1193
1414
|
const absOffset = sectionFileOffset + dirOffset;
|
|
1194
|
-
const totalEntries =
|
|
1415
|
+
const totalEntries = readU16(fd, absOffset + 12) + readU16(fd, absOffset + 14);
|
|
1195
1416
|
for (let i = 0; i < totalEntries; i++) {
|
|
1196
1417
|
const entryOffset = absOffset + 16 + i * 8;
|
|
1197
|
-
const nameOrId =
|
|
1198
|
-
const offsetOrData =
|
|
1418
|
+
const nameOrId = readU32(fd, entryOffset);
|
|
1419
|
+
const offsetOrData = readU32(fd, entryOffset + 4);
|
|
1199
1420
|
let entryId = 0;
|
|
1200
1421
|
let entryName;
|
|
1201
1422
|
if ((nameOrId & 2147483648) !== 0) {
|
|
@@ -1218,9 +1439,9 @@ var Injector = class {
|
|
|
1218
1439
|
this.#parseResourceDirectory(fd, sectionFileOffset, sectionRva, subDirOffset, leaves, depth + 1, typeId, typeName, nameId, nameName);
|
|
1219
1440
|
} else {
|
|
1220
1441
|
const dataEntryFileOffset = sectionFileOffset + offsetOrData;
|
|
1221
|
-
const
|
|
1222
|
-
const dataSize =
|
|
1223
|
-
const codePage =
|
|
1442
|
+
const dataRVA = readU32(fd, dataEntryFileOffset);
|
|
1443
|
+
const dataSize = readU32(fd, dataEntryFileOffset + 4);
|
|
1444
|
+
const codePage = readU32(fd, dataEntryFileOffset + 8);
|
|
1224
1445
|
const language = depth === 2 ? entryId : 0;
|
|
1225
1446
|
leaves.push({
|
|
1226
1447
|
typeId,
|
|
@@ -1229,7 +1450,7 @@ var Injector = class {
|
|
|
1229
1450
|
nameName,
|
|
1230
1451
|
language,
|
|
1231
1452
|
codePage,
|
|
1232
|
-
|
|
1453
|
+
dataRVA,
|
|
1233
1454
|
dataSize
|
|
1234
1455
|
});
|
|
1235
1456
|
}
|
|
@@ -1306,7 +1527,7 @@ var Injector = class {
|
|
|
1306
1527
|
value: str
|
|
1307
1528
|
});
|
|
1308
1529
|
stringPoolSize += 2 + str.length * 2;
|
|
1309
|
-
stringPoolSize =
|
|
1530
|
+
stringPoolSize = alignTo(stringPoolSize, 2);
|
|
1310
1531
|
}
|
|
1311
1532
|
for (const [nameKey, langEntries] of nameMap) {
|
|
1312
1533
|
dirTableSize += 16 + langEntries.length * 8;
|
|
@@ -1317,7 +1538,7 @@ var Injector = class {
|
|
|
1317
1538
|
value: str
|
|
1318
1539
|
});
|
|
1319
1540
|
stringPoolSize += 2 + str.length * 2;
|
|
1320
|
-
stringPoolSize =
|
|
1541
|
+
stringPoolSize = alignTo(stringPoolSize, 2);
|
|
1321
1542
|
}
|
|
1322
1543
|
for (const entry of langEntries) {
|
|
1323
1544
|
allLeafEntries.push({
|
|
@@ -1340,7 +1561,7 @@ var Injector = class {
|
|
|
1340
1561
|
const data = leafDataMap.get(entry.leafIndex);
|
|
1341
1562
|
if (data !== void 0) {
|
|
1342
1563
|
leafFileOffsets.set(entry.leafIndex, dataRegionOffset);
|
|
1343
|
-
dataRegionOffset +=
|
|
1564
|
+
dataRegionOffset += alignTo(data.length, 4);
|
|
1344
1565
|
}
|
|
1345
1566
|
}
|
|
1346
1567
|
const blobDataOffset = dataRegionOffset;
|
|
@@ -1356,7 +1577,7 @@ var Injector = class {
|
|
|
1356
1577
|
buf.writeUInt16LE(value.length, stringPos);
|
|
1357
1578
|
buf.write(value, stringPos + 2, value.length * 2, "utf16le");
|
|
1358
1579
|
stringPos += 2 + value.length * 2;
|
|
1359
|
-
stringPos =
|
|
1580
|
+
stringPos = alignTo(stringPos, 2);
|
|
1360
1581
|
}
|
|
1361
1582
|
const sortedTypeKeys = this.#sortResourceKeys([...tree.keys()]);
|
|
1362
1583
|
const nextDirOffset = 16 + tree.size * 8;
|
|
@@ -1447,10 +1668,7 @@ var Injector = class {
|
|
|
1447
1668
|
blobDataOffset
|
|
1448
1669
|
};
|
|
1449
1670
|
}
|
|
1450
|
-
#
|
|
1451
|
-
this.#fixupDataEntries(directoryBuffer, 0, 0, sectionVa);
|
|
1452
|
-
}
|
|
1453
|
-
#fixupDataEntries(buf, dirOffset, depth, sectionVa) {
|
|
1671
|
+
#fixupDataEntries(buf, dirOffset, sectionVA) {
|
|
1454
1672
|
if (dirOffset + 16 > buf.length) return;
|
|
1455
1673
|
const totalEntries = buf.readUInt16LE(dirOffset + 12) + buf.readUInt16LE(dirOffset + 14);
|
|
1456
1674
|
for (let i = 0; i < totalEntries; i++) {
|
|
@@ -1459,10 +1677,10 @@ var Injector = class {
|
|
|
1459
1677
|
const offsetOrData = buf.readUInt32LE(entryOffset + 4);
|
|
1460
1678
|
if ((offsetOrData & 2147483648) !== 0) {
|
|
1461
1679
|
const subOffset = offsetOrData & ~PE_RESOURCE_SUBDIR_FLAG;
|
|
1462
|
-
this.#fixupDataEntries(buf, subOffset,
|
|
1680
|
+
this.#fixupDataEntries(buf, subOffset, sectionVA);
|
|
1463
1681
|
} else if (offsetOrData + 4 <= buf.length) {
|
|
1464
1682
|
const currentRva = buf.readUInt32LE(offsetOrData);
|
|
1465
|
-
buf.writeUInt32LE(currentRva +
|
|
1683
|
+
buf.writeUInt32LE(currentRva + sectionVA >>> 0, offsetOrData);
|
|
1466
1684
|
}
|
|
1467
1685
|
}
|
|
1468
1686
|
}
|
|
@@ -1491,8 +1709,7 @@ var Injector = class {
|
|
|
1491
1709
|
offset += bytesRead;
|
|
1492
1710
|
}
|
|
1493
1711
|
sum = (sum & 65535) + (sum >>> 16);
|
|
1494
|
-
|
|
1495
|
-
this.#writeU32(fd, checksumOffset, checksum);
|
|
1712
|
+
writeU32(fd, checksumOffset, sum + fileSize >>> 0);
|
|
1496
1713
|
} finally {
|
|
1497
1714
|
closeSync(fd);
|
|
1498
1715
|
}
|
|
@@ -1504,14 +1721,10 @@ var Injector = class {
|
|
|
1504
1721
|
});
|
|
1505
1722
|
return [...named, ...ids];
|
|
1506
1723
|
}
|
|
1507
|
-
#
|
|
1724
|
+
#injectELF() {
|
|
1508
1725
|
const exePath = this.#options.executable;
|
|
1509
1726
|
const resource = this.#options.resource;
|
|
1510
1727
|
const overwrite = this.#options.overwrite !== false;
|
|
1511
|
-
const PT_LOAD = 1;
|
|
1512
|
-
const PT_PHDR = 6;
|
|
1513
|
-
const PF_R = 4;
|
|
1514
|
-
const PAGE = 4096;
|
|
1515
1728
|
let phdrOffset;
|
|
1516
1729
|
let phdrEntrySize;
|
|
1517
1730
|
let phdrCount;
|
|
@@ -1530,61 +1743,67 @@ var Injector = class {
|
|
|
1530
1743
|
executable: exePath,
|
|
1531
1744
|
elfData
|
|
1532
1745
|
});
|
|
1533
|
-
phdrOffset = Number(
|
|
1534
|
-
phdrEntrySize =
|
|
1535
|
-
phdrCount =
|
|
1536
|
-
headers = this.#
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
const
|
|
1746
|
+
phdrOffset = Number(readU64(fd, 32));
|
|
1747
|
+
phdrEntrySize = readU16(fd, 54);
|
|
1748
|
+
phdrCount = readU16(fd, 56);
|
|
1749
|
+
headers = this.#readELFProgramHeaders(fd, phdrOffset, phdrEntrySize, phdrCount);
|
|
1750
|
+
const stale = /* @__PURE__ */ new Set();
|
|
1751
|
+
for (let i = 0; i < headers.length; i++) {
|
|
1752
|
+
const header = headers[i];
|
|
1753
|
+
if (header === void 0 || header.type !== 4) continue;
|
|
1754
|
+
const existingName = this.#readELFNoteName(fd, header.offset, header.filesz);
|
|
1540
1755
|
if (existingName === void 0 || !existingName.startsWith("NODE_SEA")) continue;
|
|
1541
1756
|
if (!overwrite) throw new SEAError("INJECT", `Note with name "${resource}" already exists`, {
|
|
1542
1757
|
executable: exePath,
|
|
1543
1758
|
resource
|
|
1544
1759
|
});
|
|
1545
|
-
|
|
1760
|
+
stale.add(i);
|
|
1546
1761
|
}
|
|
1762
|
+
headers = headers.map((header, index) => stale.has(index) ? {
|
|
1763
|
+
...header,
|
|
1764
|
+
type: 0
|
|
1765
|
+
} : header);
|
|
1547
1766
|
} finally {
|
|
1548
1767
|
closeSync(fd);
|
|
1549
1768
|
}
|
|
1550
1769
|
let maxVaddrEnd = 0;
|
|
1551
1770
|
for (const header of headers) {
|
|
1552
|
-
if (header.type !==
|
|
1771
|
+
if (header.type !== 1) continue;
|
|
1553
1772
|
const end = header.vaddr + header.memsz;
|
|
1554
1773
|
if (end > maxVaddrEnd) maxVaddrEnd = end;
|
|
1555
1774
|
}
|
|
1556
|
-
const regionVaddr =
|
|
1775
|
+
const regionVaddr = alignTo(maxVaddrEnd, ELF_PAGE_SIZE);
|
|
1557
1776
|
const blobSize = statSync(this.#options.blob).size;
|
|
1558
|
-
const { header: noteHeader,
|
|
1559
|
-
const noteAreaSize =
|
|
1777
|
+
const { header: noteHeader, total: noteTotal } = buildELFNoteHeader(resource, blobSize);
|
|
1778
|
+
const noteAreaSize = alignTo(noteTotal, 8);
|
|
1560
1779
|
const newEntryCount = headers.length + 2;
|
|
1561
1780
|
const phtSize = newEntryCount * phdrEntrySize;
|
|
1562
1781
|
const regionSize = noteAreaSize + phtSize;
|
|
1563
1782
|
const fileSize = statSync(exePath).size;
|
|
1564
|
-
const regionStart =
|
|
1783
|
+
const regionStart = alignTo(fileSize, ELF_PAGE_SIZE);
|
|
1565
1784
|
const phtOff = regionStart + noteAreaSize;
|
|
1566
1785
|
const newLoad = {
|
|
1567
|
-
type:
|
|
1568
|
-
flags:
|
|
1786
|
+
type: 1,
|
|
1787
|
+
flags: 4,
|
|
1569
1788
|
offset: regionStart,
|
|
1570
1789
|
vaddr: regionVaddr,
|
|
1571
1790
|
paddr: regionVaddr,
|
|
1572
1791
|
filesz: regionSize,
|
|
1573
1792
|
memsz: regionSize,
|
|
1574
|
-
align:
|
|
1793
|
+
align: ELF_PAGE_SIZE
|
|
1575
1794
|
};
|
|
1576
1795
|
const newNote = {
|
|
1577
1796
|
type: 4,
|
|
1578
|
-
flags:
|
|
1797
|
+
flags: 4,
|
|
1579
1798
|
offset: regionStart,
|
|
1580
1799
|
vaddr: regionVaddr,
|
|
1581
1800
|
paddr: regionVaddr,
|
|
1582
|
-
filesz:
|
|
1583
|
-
memsz:
|
|
1801
|
+
filesz: noteTotal,
|
|
1802
|
+
memsz: noteTotal,
|
|
1584
1803
|
align: 4
|
|
1585
1804
|
};
|
|
1586
1805
|
const finalHeaders = headers.map((header) => {
|
|
1587
|
-
if (header.type !==
|
|
1806
|
+
if (header.type !== 6) return header;
|
|
1588
1807
|
const relVaddr = regionVaddr + (phtOff - regionStart);
|
|
1589
1808
|
return {
|
|
1590
1809
|
...header,
|
|
@@ -1600,43 +1819,43 @@ var Injector = class {
|
|
|
1600
1819
|
for (let i = 0; i < finalHeaders.length; i++) {
|
|
1601
1820
|
const entry = finalHeaders[i];
|
|
1602
1821
|
if (entry === void 0) continue;
|
|
1603
|
-
this.#
|
|
1822
|
+
this.#writeELFProgramHeaderEntry(phtBuffer, i * phdrEntrySize, entry);
|
|
1604
1823
|
}
|
|
1605
1824
|
if (regionStart > fileSize) appendFileSync(exePath, Buffer.alloc(regionStart - fileSize));
|
|
1606
1825
|
appendFileSync(exePath, noteHeader);
|
|
1607
|
-
|
|
1608
|
-
const descPadding =
|
|
1826
|
+
appendFile(exePath, this.#options.blob);
|
|
1827
|
+
const descPadding = alignTo(blobSize, 4) - blobSize;
|
|
1609
1828
|
if (descPadding > 0) appendFileSync(exePath, Buffer.alloc(descPadding));
|
|
1610
|
-
const trailingPad = noteAreaSize -
|
|
1829
|
+
const trailingPad = noteAreaSize - noteTotal;
|
|
1611
1830
|
if (trailingPad > 0) appendFileSync(exePath, Buffer.alloc(trailingPad));
|
|
1612
1831
|
appendFileSync(exePath, phtBuffer);
|
|
1613
1832
|
const fd2 = openSync(exePath, "r+");
|
|
1614
1833
|
try {
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
this.#
|
|
1834
|
+
writeU64(fd2, 32, BigInt(phtOff));
|
|
1835
|
+
writeU16(fd2, 56, newEntryCount);
|
|
1836
|
+
this.#verifyELFNoteMapping(fd2, phtOff, newEntryCount, phdrEntrySize);
|
|
1618
1837
|
} finally {
|
|
1619
1838
|
closeSync(fd2);
|
|
1620
1839
|
}
|
|
1621
1840
|
}
|
|
1622
|
-
#
|
|
1841
|
+
#readELFProgramHeaders(fd, phdrOffset, entrySize, count) {
|
|
1623
1842
|
const headers = [];
|
|
1624
1843
|
for (let i = 0; i < count; i++) {
|
|
1625
1844
|
const off = phdrOffset + i * entrySize;
|
|
1626
1845
|
headers.push({
|
|
1627
|
-
type:
|
|
1628
|
-
flags:
|
|
1629
|
-
offset: Number(
|
|
1630
|
-
vaddr: Number(
|
|
1631
|
-
paddr: Number(
|
|
1632
|
-
filesz: Number(
|
|
1633
|
-
memsz: Number(
|
|
1634
|
-
align: Number(
|
|
1846
|
+
type: readU32(fd, off),
|
|
1847
|
+
flags: readU32(fd, off + 4),
|
|
1848
|
+
offset: Number(readU64(fd, off + 8)),
|
|
1849
|
+
vaddr: Number(readU64(fd, off + 16)),
|
|
1850
|
+
paddr: Number(readU64(fd, off + 24)),
|
|
1851
|
+
filesz: Number(readU64(fd, off + 32)),
|
|
1852
|
+
memsz: Number(readU64(fd, off + 40)),
|
|
1853
|
+
align: Number(readU64(fd, off + 48))
|
|
1635
1854
|
});
|
|
1636
1855
|
}
|
|
1637
1856
|
return headers;
|
|
1638
1857
|
}
|
|
1639
|
-
#
|
|
1858
|
+
#writeELFProgramHeaderEntry(buf, pos, entry) {
|
|
1640
1859
|
buf.writeUInt32LE(entry.type >>> 0, pos);
|
|
1641
1860
|
buf.writeUInt32LE(entry.flags >>> 0, pos + 4);
|
|
1642
1861
|
buf.writeBigUInt64LE(BigInt(entry.offset), pos + 8);
|
|
@@ -1646,22 +1865,22 @@ var Injector = class {
|
|
|
1646
1865
|
buf.writeBigUInt64LE(BigInt(entry.memsz), pos + 40);
|
|
1647
1866
|
buf.writeBigUInt64LE(BigInt(entry.align), pos + 48);
|
|
1648
1867
|
}
|
|
1649
|
-
#
|
|
1868
|
+
#readELFNoteName(fd, offset, size) {
|
|
1650
1869
|
if (size < 12) return void 0;
|
|
1651
|
-
const namesz =
|
|
1652
|
-
const descsz =
|
|
1870
|
+
const namesz = readU32(fd, offset);
|
|
1871
|
+
const descsz = readU32(fd, offset + 4);
|
|
1653
1872
|
if (namesz === 0 || namesz > 256) return void 0;
|
|
1654
|
-
if (12 +
|
|
1873
|
+
if (12 + alignTo(namesz, 4) + alignTo(descsz, 4) > size) return void 0;
|
|
1655
1874
|
const nameBuf = Buffer.alloc(namesz);
|
|
1656
1875
|
readSync(fd, nameBuf, 0, namesz, offset + 12);
|
|
1657
|
-
return
|
|
1876
|
+
return stripTrailingNulls(nameBuf.toString("utf-8"));
|
|
1658
1877
|
}
|
|
1659
|
-
#
|
|
1660
|
-
const headers = this.#
|
|
1878
|
+
#verifyELFNoteMapping(fd, phdrOffset, phdrCount, phdrEntrySize) {
|
|
1879
|
+
const headers = this.#readELFProgramHeaders(fd, phdrOffset, phdrEntrySize, phdrCount);
|
|
1661
1880
|
const loads = headers.filter((h) => h.type === 1);
|
|
1662
1881
|
const notes = headers.filter((h) => h.type === 4);
|
|
1663
1882
|
for (const note of notes) {
|
|
1664
|
-
const name = this.#
|
|
1883
|
+
const name = this.#readELFNoteName(fd, note.offset, note.filesz);
|
|
1665
1884
|
if (name === void 0 || !name.startsWith("NODE_SEA")) continue;
|
|
1666
1885
|
const covering = loads.find((load) => note.offset >= load.offset && note.offset < load.offset + load.filesz);
|
|
1667
1886
|
if (covering === void 0) continue;
|
|
@@ -1672,7 +1891,7 @@ var Injector = class {
|
|
|
1672
1891
|
resource: this.#options.resource
|
|
1673
1892
|
});
|
|
1674
1893
|
}
|
|
1675
|
-
#
|
|
1894
|
+
#injectMachO() {
|
|
1676
1895
|
const exePath = this.#options.executable;
|
|
1677
1896
|
const fileSize = statSync(exePath).size;
|
|
1678
1897
|
let srcFd = openSync(exePath, "r");
|
|
@@ -1722,7 +1941,7 @@ var Injector = class {
|
|
|
1722
1941
|
for (let i = 0; i < commands.length; i++) {
|
|
1723
1942
|
const cmd = commands[i];
|
|
1724
1943
|
if (cmd === void 0 || cmd.type !== 25) continue;
|
|
1725
|
-
const segName =
|
|
1944
|
+
const segName = stripTrailingNulls(buf.subarray(cmd.offset + 8, cmd.offset + 24).toString("ascii"));
|
|
1726
1945
|
if (segName === "__LINKEDIT") linkeditIndex = i;
|
|
1727
1946
|
if (segName === segmentName) existingSegmentIndices.push(i);
|
|
1728
1947
|
}
|
|
@@ -1731,13 +1950,12 @@ var Injector = class {
|
|
|
1731
1950
|
segmentName
|
|
1732
1951
|
});
|
|
1733
1952
|
const linkeditCmd = linkeditIndex === -1 ? void 0 : commands[linkeditIndex];
|
|
1734
|
-
if (linkeditCmd === void 0) throw new SEAError("
|
|
1735
|
-
const
|
|
1736
|
-
const
|
|
1737
|
-
const
|
|
1953
|
+
if (linkeditCmd === void 0) throw new SEAError("ROOM", "Mach-O binary has no __LINKEDIT segment", { executable: exePath });
|
|
1954
|
+
const linkeditOffset = Number(buf.readBigUInt64LE(linkeditCmd.offset + 40));
|
|
1955
|
+
const linkeditSize = Number(buf.readBigUInt64LE(linkeditCmd.offset + 48));
|
|
1956
|
+
const linkeditAddress = buf.readBigUInt64LE(linkeditCmd.offset + 24);
|
|
1738
1957
|
const blobSize = statSync(this.#options.blob).size;
|
|
1739
|
-
const
|
|
1740
|
-
const shift = this.#align(blobSize, pageSize);
|
|
1958
|
+
const shift = alignTo(blobSize, cputype === 16777228 ? 16384 : 4096);
|
|
1741
1959
|
let firstSectionOffset = fileSize;
|
|
1742
1960
|
for (const cmd of commands) {
|
|
1743
1961
|
if (cmd.type !== 25) continue;
|
|
@@ -1754,7 +1972,7 @@ var Injector = class {
|
|
|
1754
1972
|
const removedSize = commands.filter((_c, i) => existingSegmentIndices.includes(i)).reduce((sum, c) => sum + c.size, 0);
|
|
1755
1973
|
const newNcmds = commands.length - existingSegmentIndices.length + 1;
|
|
1756
1974
|
const newSizeofcmds = sizeofcmds - removedSize + newCmdSize;
|
|
1757
|
-
if (headerSize + newSizeofcmds + 16 > firstSectionOffset) throw new SEAError("
|
|
1975
|
+
if (headerSize + newSizeofcmds + 16 > firstSectionOffset) throw new SEAError("ROOM", `Not enough header space for new Mach-O load command (need offset ${String(headerSize + newSizeofcmds + 16)}, first section at ${String(firstSectionOffset)})`, {
|
|
1758
1976
|
executable: exePath,
|
|
1759
1977
|
firstSectionOffset,
|
|
1760
1978
|
requiredOffset: headerSize + newSizeofcmds + 16
|
|
@@ -1763,14 +1981,14 @@ var Injector = class {
|
|
|
1763
1981
|
const cmd = commands[i];
|
|
1764
1982
|
if (cmd === void 0 || existingSegmentIndices.includes(i)) continue;
|
|
1765
1983
|
if (cmd.type === 25) {
|
|
1766
|
-
if (
|
|
1767
|
-
if (buf.readUInt32LE(cmd.offset + 64) > 0) throw new SEAError("
|
|
1768
|
-
buf.writeBigUInt64LE(
|
|
1769
|
-
buf.writeBigUInt64LE(BigInt(
|
|
1984
|
+
if (stripTrailingNulls(buf.subarray(cmd.offset + 8, cmd.offset + 24).toString("ascii")) === "__LINKEDIT") {
|
|
1985
|
+
if (buf.readUInt32LE(cmd.offset + 64) > 0) throw new SEAError("ROOM", "__LINKEDIT segment with sections is not supported", { executable: exePath });
|
|
1986
|
+
buf.writeBigUInt64LE(linkeditAddress + BigInt(shift), cmd.offset + 24);
|
|
1987
|
+
buf.writeBigUInt64LE(BigInt(linkeditOffset + shift), cmd.offset + 40);
|
|
1770
1988
|
}
|
|
1771
1989
|
continue;
|
|
1772
1990
|
}
|
|
1773
|
-
this.#
|
|
1991
|
+
this.#shiftMachOLinkeditOffsets(buf, cmd, linkeditOffset, shift);
|
|
1774
1992
|
}
|
|
1775
1993
|
buf.writeUInt32LE(newNcmds, 16);
|
|
1776
1994
|
buf.writeUInt32LE(newSizeofcmds, 20);
|
|
@@ -1778,9 +1996,9 @@ var Injector = class {
|
|
|
1778
1996
|
cmd.writeUInt32LE(25, 0);
|
|
1779
1997
|
cmd.writeUInt32LE(newCmdSize, 4);
|
|
1780
1998
|
cmd.write(segmentName, 8, 16, "ascii");
|
|
1781
|
-
cmd.writeBigUInt64LE(
|
|
1999
|
+
cmd.writeBigUInt64LE(linkeditAddress, 24);
|
|
1782
2000
|
cmd.writeBigUInt64LE(BigInt(shift), 32);
|
|
1783
|
-
cmd.writeBigUInt64LE(BigInt(
|
|
2001
|
+
cmd.writeBigUInt64LE(BigInt(linkeditOffset), 40);
|
|
1784
2002
|
cmd.writeBigUInt64LE(BigInt(shift), 48);
|
|
1785
2003
|
cmd.writeUInt32LE(1, 56);
|
|
1786
2004
|
cmd.writeUInt32LE(1, 60);
|
|
@@ -1788,9 +2006,9 @@ var Injector = class {
|
|
|
1788
2006
|
cmd.writeUInt32LE(0, 68);
|
|
1789
2007
|
cmd.write(sectionName, 72, 16, "ascii");
|
|
1790
2008
|
cmd.write(segmentName, 88, 16, "ascii");
|
|
1791
|
-
cmd.writeBigUInt64LE(
|
|
2009
|
+
cmd.writeBigUInt64LE(linkeditAddress, 104);
|
|
1792
2010
|
cmd.writeBigUInt64LE(BigInt(blobSize), 112);
|
|
1793
|
-
cmd.writeUInt32LE(
|
|
2011
|
+
cmd.writeUInt32LE(linkeditOffset, 120);
|
|
1794
2012
|
cmd.writeUInt32LE(0, 124);
|
|
1795
2013
|
cmd.writeUInt32LE(0, 128);
|
|
1796
2014
|
cmd.writeUInt32LE(0, 132);
|
|
@@ -1813,14 +2031,14 @@ var Injector = class {
|
|
|
1813
2031
|
destFd = openSync(injTemp, "w", mode);
|
|
1814
2032
|
writeSync(destFd, buf.subarray(0, headerSize));
|
|
1815
2033
|
writeSync(destFd, newLoadCmdsBuf);
|
|
1816
|
-
copyRange(srcFd, destFd, headerSize + newSizeofcmds,
|
|
2034
|
+
copyRange(srcFd, destFd, headerSize + newSizeofcmds, linkeditOffset - (headerSize + newSizeofcmds));
|
|
1817
2035
|
blobFd = openSync(this.#options.blob, "r");
|
|
1818
2036
|
copyRange(blobFd, destFd, 0, blobSize);
|
|
1819
2037
|
closeSync(blobFd);
|
|
1820
2038
|
blobFd = void 0;
|
|
1821
2039
|
const blobPadding = shift - blobSize;
|
|
1822
2040
|
if (blobPadding > 0) writeSync(destFd, Buffer.alloc(blobPadding));
|
|
1823
|
-
copyRange(srcFd, destFd,
|
|
2041
|
+
copyRange(srcFd, destFd, linkeditOffset, linkeditSize);
|
|
1824
2042
|
closeSync(destFd);
|
|
1825
2043
|
destFd = void 0;
|
|
1826
2044
|
closeSync(srcFd);
|
|
@@ -1828,7 +2046,7 @@ var Injector = class {
|
|
|
1828
2046
|
chmodSync(injTemp, mode & 4095);
|
|
1829
2047
|
finalizeExecutable(injTemp, exePath);
|
|
1830
2048
|
injTemp = void 0;
|
|
1831
|
-
this.#
|
|
2049
|
+
this.#verifyMachOSection(exePath, segmentName, sectionName, linkeditOffset, blobSize);
|
|
1832
2050
|
} finally {
|
|
1833
2051
|
for (const fd of [
|
|
1834
2052
|
blobFd,
|
|
@@ -1840,7 +2058,7 @@ var Injector = class {
|
|
|
1840
2058
|
if (injTemp !== void 0) rmSync(injTemp, { force: true });
|
|
1841
2059
|
}
|
|
1842
2060
|
}
|
|
1843
|
-
#
|
|
2061
|
+
#shiftMachOLinkeditOffsets(buf, cmd, threshold, shift) {
|
|
1844
2062
|
let offsets;
|
|
1845
2063
|
switch (cmd.type) {
|
|
1846
2064
|
case 2:
|
|
@@ -1883,7 +2101,7 @@ var Injector = class {
|
|
|
1883
2101
|
if (value >= threshold && value > 0) buf.writeUInt32LE(value + shift, fieldOffset);
|
|
1884
2102
|
}
|
|
1885
2103
|
}
|
|
1886
|
-
#
|
|
2104
|
+
#verifyMachOSection(exePath, segmentName, sectionName, expectedOffset, expectedSize) {
|
|
1887
2105
|
const fd = openSync(exePath, "r");
|
|
1888
2106
|
try {
|
|
1889
2107
|
const header = Buffer.alloc(32);
|
|
@@ -1898,12 +2116,12 @@ var Injector = class {
|
|
|
1898
2116
|
if (cmdType === 25 && cmdSize >= 72) {
|
|
1899
2117
|
const segBuf = Buffer.alloc(cmdSize);
|
|
1900
2118
|
readSync(fd, segBuf, 0, cmdSize, offset);
|
|
1901
|
-
if (
|
|
2119
|
+
if (stripTrailingNulls(segBuf.subarray(8, 24).toString("ascii")) === segmentName) {
|
|
1902
2120
|
const nsects = segBuf.readUInt32LE(64);
|
|
1903
2121
|
for (let s = 0; s < nsects; s++) {
|
|
1904
2122
|
const sectOff = 72 + s * 80;
|
|
1905
2123
|
if (sectOff + 80 > segBuf.length) continue;
|
|
1906
|
-
if (
|
|
2124
|
+
if (stripTrailingNulls(segBuf.subarray(sectOff, sectOff + 16).toString("ascii")) !== sectionName) continue;
|
|
1907
2125
|
const secSize = Number(segBuf.readBigUInt64LE(sectOff + 40));
|
|
1908
2126
|
if (segBuf.readUInt32LE(sectOff + 48) === expectedOffset && secSize === expectedSize) return;
|
|
1909
2127
|
}
|
|
@@ -1920,40 +2138,6 @@ var Injector = class {
|
|
|
1920
2138
|
sectionName
|
|
1921
2139
|
});
|
|
1922
2140
|
}
|
|
1923
|
-
#readU16(fd, offset) {
|
|
1924
|
-
const b = Buffer.alloc(2);
|
|
1925
|
-
readSync(fd, b, 0, 2, offset);
|
|
1926
|
-
return b.readUInt16LE(0);
|
|
1927
|
-
}
|
|
1928
|
-
#readU32(fd, offset) {
|
|
1929
|
-
const b = Buffer.alloc(4);
|
|
1930
|
-
readSync(fd, b, 0, 4, offset);
|
|
1931
|
-
return b.readUInt32LE(0);
|
|
1932
|
-
}
|
|
1933
|
-
#readU64(fd, offset) {
|
|
1934
|
-
const b = Buffer.alloc(8);
|
|
1935
|
-
readSync(fd, b, 0, 8, offset);
|
|
1936
|
-
return b.readBigUInt64LE(0);
|
|
1937
|
-
}
|
|
1938
|
-
#writeU16(fd, offset, value) {
|
|
1939
|
-
const b = Buffer.alloc(2);
|
|
1940
|
-
b.writeUInt16LE(value, 0);
|
|
1941
|
-
writeSync(fd, b, 0, 2, offset);
|
|
1942
|
-
}
|
|
1943
|
-
#writeU32(fd, offset, value) {
|
|
1944
|
-
const b = Buffer.alloc(4);
|
|
1945
|
-
b.writeUInt32LE(value >>> 0, 0);
|
|
1946
|
-
writeSync(fd, b, 0, 4, offset);
|
|
1947
|
-
}
|
|
1948
|
-
#writeU64(fd, offset, value) {
|
|
1949
|
-
const b = Buffer.alloc(8);
|
|
1950
|
-
b.writeBigUInt64LE(value, 0);
|
|
1951
|
-
writeSync(fd, b, 0, 8, offset);
|
|
1952
|
-
}
|
|
1953
|
-
#align(value, alignment) {
|
|
1954
|
-
const remainder = value % alignment;
|
|
1955
|
-
return remainder === 0 ? value : value + (alignment - remainder);
|
|
1956
|
-
}
|
|
1957
2141
|
#ensureValidPEAlignment(sectionAlignment, fileAlignment) {
|
|
1958
2142
|
if (!isPowerOfTwo(sectionAlignment) || !isPowerOfTwo(fileAlignment)) throw new SEAError("FORMAT", "PE section/file alignment must be a nonzero power of two", {
|
|
1959
2143
|
executable: this.#options.executable,
|
|
@@ -1966,35 +2150,35 @@ var Injector = class {
|
|
|
1966
2150
|
fileAlignment
|
|
1967
2151
|
});
|
|
1968
2152
|
}
|
|
1969
|
-
/** Append sourceFile to targetFile by streaming in 4 MB chunks */
|
|
1970
|
-
#appendFile(targetPath, sourcePath) {
|
|
1971
|
-
const chunkSize = 4194304;
|
|
1972
|
-
const srcFd = openSync(sourcePath, "r");
|
|
1973
|
-
try {
|
|
1974
|
-
const srcSize = statSync(sourcePath).size;
|
|
1975
|
-
const chunk = Buffer.alloc(Math.min(chunkSize, srcSize));
|
|
1976
|
-
let offset = 0;
|
|
1977
|
-
while (offset < srcSize) {
|
|
1978
|
-
const toRead = Math.min(chunkSize, srcSize - offset);
|
|
1979
|
-
const bytesRead = readSync(srcFd, chunk, 0, toRead, offset);
|
|
1980
|
-
if (bytesRead === 0) break;
|
|
1981
|
-
appendFileSync(targetPath, chunk.subarray(0, bytesRead));
|
|
1982
|
-
offset += bytesRead;
|
|
1983
|
-
}
|
|
1984
|
-
} finally {
|
|
1985
|
-
closeSync(srcFd);
|
|
1986
|
-
}
|
|
1987
|
-
}
|
|
1988
|
-
#stripTrailingNulls(value) {
|
|
1989
|
-
const idx = value.indexOf(String.fromCharCode(0));
|
|
1990
|
-
return idx === -1 ? value : value.slice(0, idx);
|
|
1991
|
-
}
|
|
1992
2153
|
};
|
|
1993
2154
|
//#endregion
|
|
1994
|
-
//#region src/server/
|
|
2155
|
+
//#region src/server/seas/SEA.ts
|
|
2156
|
+
/**
|
|
2157
|
+
* Runs a Node.js single executable application build to completion.
|
|
2158
|
+
*
|
|
2159
|
+
* @remarks
|
|
2160
|
+
* `execute` compresses the configured directories, generates the SEA blob, copies
|
|
2161
|
+
* the host Node binary, injects the blob with an {@link Injector}, signs the
|
|
2162
|
+
* result where the platform supports it, and moves the finished executable into
|
|
2163
|
+
* place atomically. Each stage reports on `emitter`, and every failure throws a
|
|
2164
|
+
* coded `SEAError` after an `error` event carrying it.
|
|
2165
|
+
*
|
|
2166
|
+
* @param options - SEA build options: the output name, the entry point, the output
|
|
2167
|
+
* directory, and the optional assets, compression, Windows, and blob settings
|
|
2168
|
+
*
|
|
2169
|
+
* @example
|
|
2170
|
+
* ```ts
|
|
2171
|
+
* const sea = new SEA({
|
|
2172
|
+
* name: 'myapp',
|
|
2173
|
+
* entry: { path: 'dist/server/serve.cjs' },
|
|
2174
|
+
* output: 'dist/sea',
|
|
2175
|
+
* })
|
|
2176
|
+
* const result = await sea.execute()
|
|
2177
|
+
* sea.destroy()
|
|
2178
|
+
* ```
|
|
2179
|
+
*/
|
|
1995
2180
|
var SEA = class {
|
|
1996
2181
|
#status = "idle";
|
|
1997
|
-
#destroyed = false;
|
|
1998
2182
|
#options;
|
|
1999
2183
|
#emitter;
|
|
2000
2184
|
constructor(options) {
|
|
@@ -2011,9 +2195,9 @@ var SEA = class {
|
|
|
2011
2195
|
return this.#status;
|
|
2012
2196
|
}
|
|
2013
2197
|
async execute() {
|
|
2014
|
-
if (this.#destroyed) throw new SEAError("STATE", "SEA is destroyed");
|
|
2198
|
+
if (this.#emitter.destroyed) throw new SEAError("STATE", "SEA is destroyed");
|
|
2015
2199
|
if (this.#status === "active") throw new SEAError("STATE", "SEA build already in progress");
|
|
2016
|
-
if (
|
|
2200
|
+
if (resolvePlatform() === void 0) {
|
|
2017
2201
|
const error = new SEAError("PLATFORM", `Unsupported platform: ${process.platform}`, { platform: process.platform });
|
|
2018
2202
|
this.#status = "error";
|
|
2019
2203
|
this.#emitter.emit("error", error);
|
|
@@ -2028,7 +2212,7 @@ var SEA = class {
|
|
|
2028
2212
|
this.#check();
|
|
2029
2213
|
const compression = this.#compress(root);
|
|
2030
2214
|
this.#check();
|
|
2031
|
-
const blob = this.#
|
|
2215
|
+
const blob = this.#buildBlob(root, compression);
|
|
2032
2216
|
this.#check();
|
|
2033
2217
|
const assembled = this.#assemble(root, blob);
|
|
2034
2218
|
const size = statSync(assembled.executable).size;
|
|
@@ -2053,8 +2237,6 @@ var SEA = class {
|
|
|
2053
2237
|
}
|
|
2054
2238
|
}
|
|
2055
2239
|
destroy() {
|
|
2056
|
-
if (this.#destroyed) return;
|
|
2057
|
-
this.#destroyed = true;
|
|
2058
2240
|
this.#emitter.destroy();
|
|
2059
2241
|
}
|
|
2060
2242
|
#check() {
|
|
@@ -2065,16 +2247,12 @@ var SEA = class {
|
|
|
2065
2247
|
ensureSafeName(this.#options.name);
|
|
2066
2248
|
for (const [key, path] of Object.entries(this.#options.assets ?? {})) {
|
|
2067
2249
|
ensureSafeKey(key);
|
|
2068
|
-
ensureSafeKey(path);
|
|
2069
2250
|
ensureContained(base, path);
|
|
2070
2251
|
}
|
|
2071
|
-
for (const path of this.#options.compression?.paths ?? [])
|
|
2072
|
-
ensureSafeKey(path);
|
|
2073
|
-
if (existsSync(resolve(base, path))) ensureContained(base, path);
|
|
2074
|
-
}
|
|
2252
|
+
for (const path of this.#options.compression?.paths ?? []) if (existsSync(resolve(base, path))) ensureContained(base, path);
|
|
2075
2253
|
const sign = this.#options.windows?.sign;
|
|
2076
2254
|
if (sign !== void 0) {
|
|
2077
|
-
|
|
2255
|
+
buildSignCommand(sign, "placeholder");
|
|
2078
2256
|
if (sign.file !== void 0) ensureExists(resolve(base, sign.file), "Certificate file not found", "SIGN");
|
|
2079
2257
|
}
|
|
2080
2258
|
}
|
|
@@ -2123,19 +2301,19 @@ var SEA = class {
|
|
|
2123
2301
|
this.#emitter.emit("compress", manifest);
|
|
2124
2302
|
return manifest;
|
|
2125
2303
|
}
|
|
2126
|
-
#
|
|
2304
|
+
#buildBlob(root, compression) {
|
|
2127
2305
|
const entry = resolve(root, this.#options.entry.path);
|
|
2128
2306
|
ensureExists(entry, `Entry not found: ${this.#options.entry.path}`, "ENTRY");
|
|
2129
2307
|
const output = resolve(root, this.#options.output);
|
|
2130
2308
|
mkdirSync(output, { recursive: true });
|
|
2131
2309
|
const configPath = join(output, "sea-config.json");
|
|
2132
2310
|
const blob = join(output, "sea-prep.blob");
|
|
2133
|
-
const config =
|
|
2311
|
+
const config = buildBlobConfig({
|
|
2134
2312
|
path: entry,
|
|
2135
2313
|
...this.#options.entry.format === void 0 ? {} : { format: this.#options.entry.format }
|
|
2136
|
-
}, blob, this.#
|
|
2314
|
+
}, blob, this.#resolveAssets(root, compression), this.#options.blob);
|
|
2137
2315
|
writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
2138
|
-
|
|
2316
|
+
executeShell([
|
|
2139
2317
|
process.execPath,
|
|
2140
2318
|
"--experimental-sea-config",
|
|
2141
2319
|
configPath
|
|
@@ -2149,7 +2327,7 @@ var SEA = class {
|
|
|
2149
2327
|
return blob;
|
|
2150
2328
|
}
|
|
2151
2329
|
#assemble(root, blob) {
|
|
2152
|
-
const platform =
|
|
2330
|
+
const platform = resolvePlatform();
|
|
2153
2331
|
if (platform === void 0) throw new SEAError("PLATFORM", `Unsupported platform: ${process.platform}`, { platform: process.platform });
|
|
2154
2332
|
const output = resolve(root, this.#options.output);
|
|
2155
2333
|
const ext = process.platform === "win32" ? ".exe" : "";
|
|
@@ -2171,7 +2349,7 @@ var SEA = class {
|
|
|
2171
2349
|
if (platform.remove !== void 0) {
|
|
2172
2350
|
this.#check();
|
|
2173
2351
|
try {
|
|
2174
|
-
|
|
2352
|
+
executeShell([...platform.remove, temp], shell);
|
|
2175
2353
|
} catch (thrown) {
|
|
2176
2354
|
throw new SEAError("SIGN", "Failed to strip existing signature", { cause: thrown instanceof Error ? thrown.message : String(thrown) });
|
|
2177
2355
|
}
|
|
@@ -2188,14 +2366,14 @@ var SEA = class {
|
|
|
2188
2366
|
if (platform.sign !== void 0) {
|
|
2189
2367
|
this.#check();
|
|
2190
2368
|
try {
|
|
2191
|
-
|
|
2369
|
+
executeShell([...platform.sign, temp], shell);
|
|
2192
2370
|
} catch (thrown) {
|
|
2193
2371
|
throw new SEAError("SIGN", "Failed to sign executable", { cause: thrown instanceof Error ? thrown.message : String(thrown) });
|
|
2194
2372
|
}
|
|
2195
2373
|
if (platform.verify !== void 0) {
|
|
2196
2374
|
this.#check();
|
|
2197
2375
|
try {
|
|
2198
|
-
|
|
2376
|
+
executeShell([...platform.verify, temp], shell);
|
|
2199
2377
|
} catch (thrown) {
|
|
2200
2378
|
throw new SEAError("SIGN", "Signature verification failed", { cause: thrown instanceof Error ? thrown.message : String(thrown) });
|
|
2201
2379
|
}
|
|
@@ -2221,12 +2399,12 @@ var SEA = class {
|
|
|
2221
2399
|
const sign = this.#options.windows?.sign;
|
|
2222
2400
|
if (sign !== void 0) {
|
|
2223
2401
|
this.#check();
|
|
2224
|
-
const signArgs =
|
|
2402
|
+
const signArgs = buildSignCommand(sign.file !== void 0 ? {
|
|
2225
2403
|
...sign,
|
|
2226
2404
|
file: resolve(root, sign.file)
|
|
2227
2405
|
} : sign, temp);
|
|
2228
2406
|
try {
|
|
2229
|
-
|
|
2407
|
+
executeShell(signArgs, shell);
|
|
2230
2408
|
} catch {
|
|
2231
2409
|
throw new SEAError("SIGN", "Windows signing failed", { executable: temp });
|
|
2232
2410
|
}
|
|
@@ -2234,7 +2412,7 @@ var SEA = class {
|
|
|
2234
2412
|
if (platform.verify !== void 0) {
|
|
2235
2413
|
this.#check();
|
|
2236
2414
|
try {
|
|
2237
|
-
|
|
2415
|
+
executeShell([...platform.verify, temp], shell);
|
|
2238
2416
|
} catch {
|
|
2239
2417
|
throw new SEAError("SIGN", "Signature verification failed", { executable: temp });
|
|
2240
2418
|
}
|
|
@@ -2265,7 +2443,7 @@ var SEA = class {
|
|
|
2265
2443
|
...terminal === void 0 ? {} : { terminal }
|
|
2266
2444
|
};
|
|
2267
2445
|
}
|
|
2268
|
-
#
|
|
2446
|
+
#resolveAssets(root, compression) {
|
|
2269
2447
|
const outputs = /* @__PURE__ */ new Map();
|
|
2270
2448
|
for (const result of compression?.assets ?? []) outputs.set(result.input, result.output);
|
|
2271
2449
|
const assets = {};
|
|
@@ -2278,6 +2456,23 @@ var SEA = class {
|
|
|
2278
2456
|
};
|
|
2279
2457
|
//#endregion
|
|
2280
2458
|
//#region src/server/assets/Asset.ts
|
|
2459
|
+
/**
|
|
2460
|
+
* Holds one named asset's key, bytes, and compression state.
|
|
2461
|
+
*
|
|
2462
|
+
* @remarks
|
|
2463
|
+
* `compressed` is inferred from a `.br` suffix on the key whenever the input
|
|
2464
|
+
* leaves it unset, so a Brotli output registered under its own name reports itself
|
|
2465
|
+
* correctly without the caller restating it.
|
|
2466
|
+
*
|
|
2467
|
+
* @param input - The asset's key, content bytes, and optional compression flag
|
|
2468
|
+
*
|
|
2469
|
+
* @example
|
|
2470
|
+
* ```ts
|
|
2471
|
+
* const asset = new Asset({ key: 'client.html.br', content: compressedBuffer })
|
|
2472
|
+
* asset.key // 'client.html.br'
|
|
2473
|
+
* asset.compressed // true
|
|
2474
|
+
* ```
|
|
2475
|
+
*/
|
|
2281
2476
|
var Asset = class {
|
|
2282
2477
|
key;
|
|
2283
2478
|
content;
|
|
@@ -2291,24 +2486,41 @@ var Asset = class {
|
|
|
2291
2486
|
//#endregion
|
|
2292
2487
|
//#region src/server/assets/AssetManager.ts
|
|
2293
2488
|
/**
|
|
2294
|
-
*
|
|
2489
|
+
* Collects named assets from a SEA blob or from disk and serves them by key.
|
|
2490
|
+
*
|
|
2491
|
+
* @remarks
|
|
2492
|
+
* Construction registers every asset embedded in the running SEA blob, so an
|
|
2493
|
+
* executable built by this package reaches its assets without touching the
|
|
2494
|
+
* filesystem. Outside SEA mode that step registers nothing, and `load()` reads
|
|
2495
|
+
* the paths `options.assets` configures instead.
|
|
2295
2496
|
*
|
|
2296
|
-
*
|
|
2297
|
-
*
|
|
2298
|
-
*
|
|
2497
|
+
* @param options - Asset manager options: the project root, the key→path mapping
|
|
2498
|
+
* `load()` reads, and the event hooks
|
|
2499
|
+
*
|
|
2500
|
+
* @example
|
|
2501
|
+
* ```ts
|
|
2502
|
+
* const manager = new AssetManager({
|
|
2503
|
+
* root: process.cwd(),
|
|
2504
|
+
* assets: { 'client.html.br': 'dist/client/client.html.br' },
|
|
2505
|
+
* })
|
|
2506
|
+
* manager.load()
|
|
2507
|
+
* manager.asset('client.html.br')
|
|
2508
|
+
* manager.destroy()
|
|
2509
|
+
* ```
|
|
2299
2510
|
*/
|
|
2300
2511
|
var AssetManager = class {
|
|
2301
2512
|
#emitter;
|
|
2302
2513
|
#assets = /* @__PURE__ */ new Map();
|
|
2303
|
-
#keys = [];
|
|
2304
2514
|
#root;
|
|
2515
|
+
#paths;
|
|
2305
2516
|
constructor(options) {
|
|
2306
2517
|
this.#root = options?.root ?? process.cwd();
|
|
2518
|
+
this.#paths = options?.assets ?? {};
|
|
2307
2519
|
this.#emitter = new Emitter({
|
|
2308
2520
|
...options?.on === void 0 ? {} : { on: options.on },
|
|
2309
2521
|
...options?.error === void 0 ? {} : { error: options.error }
|
|
2310
2522
|
});
|
|
2311
|
-
this.#
|
|
2523
|
+
this.#loadSEA();
|
|
2312
2524
|
}
|
|
2313
2525
|
get emitter() {
|
|
2314
2526
|
return this.#emitter;
|
|
@@ -2323,7 +2535,7 @@ var AssetManager = class {
|
|
|
2323
2535
|
return [...this.#assets.values()];
|
|
2324
2536
|
}
|
|
2325
2537
|
keys() {
|
|
2326
|
-
return [...this.#keys];
|
|
2538
|
+
return [...this.#assets.keys()];
|
|
2327
2539
|
}
|
|
2328
2540
|
register(input) {
|
|
2329
2541
|
const items = Array.isArray(input) ? input : [input];
|
|
@@ -2335,46 +2547,31 @@ var AssetManager = class {
|
|
|
2335
2547
|
}
|
|
2336
2548
|
load() {
|
|
2337
2549
|
if (isSea()) return;
|
|
2338
|
-
const
|
|
2339
|
-
const
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
if (!isArrayBuffer(content)) throw new Error("Failed to encode client asset");
|
|
2345
|
-
this.register({
|
|
2346
|
-
key: CLIENT_ASSET_KEY_RAW,
|
|
2347
|
-
content,
|
|
2348
|
-
compressed: false
|
|
2349
|
-
});
|
|
2350
|
-
this.#emitter.emit("load", [CLIENT_ASSET_KEY_RAW]);
|
|
2351
|
-
} catch (thrown) {
|
|
2352
|
-
this.#emitter.emit("error", thrown);
|
|
2550
|
+
const registered = [];
|
|
2551
|
+
for (const [key, path] of Object.entries(this.#paths)) {
|
|
2552
|
+
const resolved = resolve(this.#root, path);
|
|
2553
|
+
if (!existsSync(resolved)) {
|
|
2554
|
+
this.#emitter.emit("error", /* @__PURE__ */ new Error(`Asset not found: ${path}`));
|
|
2555
|
+
continue;
|
|
2353
2556
|
}
|
|
2354
|
-
return;
|
|
2355
|
-
}
|
|
2356
|
-
if (existsSync(builtBrPath)) {
|
|
2357
2557
|
try {
|
|
2358
|
-
const buffer = readFileSync(
|
|
2558
|
+
const buffer = readFileSync(resolved);
|
|
2359
2559
|
const bufferData = buffer.buffer;
|
|
2360
|
-
if (!isArrayBuffer(bufferData)) throw new Error(
|
|
2560
|
+
if (!isArrayBuffer(bufferData)) throw new Error(`Failed to read asset: ${path}`);
|
|
2361
2561
|
const content = bufferData.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
2362
2562
|
this.register({
|
|
2363
|
-
key
|
|
2364
|
-
content
|
|
2365
|
-
compressed: true
|
|
2563
|
+
key,
|
|
2564
|
+
content
|
|
2366
2565
|
});
|
|
2367
|
-
|
|
2566
|
+
registered.push(key);
|
|
2368
2567
|
} catch (thrown) {
|
|
2369
2568
|
this.#emitter.emit("error", thrown);
|
|
2370
2569
|
}
|
|
2371
|
-
return;
|
|
2372
2570
|
}
|
|
2373
|
-
this.#emitter.emit("
|
|
2571
|
+
if (registered.length > 0) this.#emitter.emit("load", registered);
|
|
2374
2572
|
}
|
|
2375
2573
|
clear() {
|
|
2376
2574
|
this.#assets.clear();
|
|
2377
|
-
this.#keys = [];
|
|
2378
2575
|
this.#emitter.emit("clear");
|
|
2379
2576
|
}
|
|
2380
2577
|
destroy() {
|
|
@@ -2383,9 +2580,8 @@ var AssetManager = class {
|
|
|
2383
2580
|
}
|
|
2384
2581
|
#add(asset) {
|
|
2385
2582
|
this.#assets.set(asset.key, asset);
|
|
2386
|
-
if (!this.#keys.includes(asset.key)) this.#keys.push(asset.key);
|
|
2387
2583
|
}
|
|
2388
|
-
#
|
|
2584
|
+
#loadSEA() {
|
|
2389
2585
|
if (isSea()) {
|
|
2390
2586
|
const assetKeys = getAssetKeys();
|
|
2391
2587
|
const registered = [];
|
|
@@ -2404,28 +2600,28 @@ var AssetManager = class {
|
|
|
2404
2600
|
//#endregion
|
|
2405
2601
|
//#region src/server/factories.ts
|
|
2406
2602
|
/**
|
|
2407
|
-
*
|
|
2603
|
+
* Creates a new SEA build orchestrator.
|
|
2408
2604
|
*
|
|
2409
2605
|
* @param options - SEA build options
|
|
2410
2606
|
* @returns a new `SEAInterface`
|
|
2411
2607
|
*
|
|
2412
2608
|
* @example
|
|
2413
2609
|
* ```ts
|
|
2414
|
-
* const
|
|
2610
|
+
* const sea = createSEA({
|
|
2415
2611
|
* name: 'orkestrel',
|
|
2416
2612
|
* entry: { path: 'dist/bin/serve.cjs' },
|
|
2417
2613
|
* output: 'dist/sea',
|
|
2418
2614
|
* assets: { 'index.html.br': 'dist/client/index.html.br' },
|
|
2419
2615
|
* compression: { paths: ['dist/client'] },
|
|
2420
2616
|
* })
|
|
2421
|
-
* const result = await
|
|
2617
|
+
* const result = await sea.execute()
|
|
2422
2618
|
* ```
|
|
2423
2619
|
*/
|
|
2424
2620
|
function createSEA(options) {
|
|
2425
2621
|
return new SEA(options);
|
|
2426
2622
|
}
|
|
2427
2623
|
/**
|
|
2428
|
-
*
|
|
2624
|
+
* Creates a cross-platform binary resource injector.
|
|
2429
2625
|
*
|
|
2430
2626
|
* Detects the executable format (PE, ELF, Mach-O) from the file header
|
|
2431
2627
|
* and injects the blob using pure TypeScript file I/O — no WASM, no
|
|
@@ -2449,7 +2645,7 @@ function createInjector(options) {
|
|
|
2449
2645
|
return new Injector(options);
|
|
2450
2646
|
}
|
|
2451
2647
|
/**
|
|
2452
|
-
*
|
|
2648
|
+
* Creates a single named asset.
|
|
2453
2649
|
*
|
|
2454
2650
|
* @param input - Asset key, content, and optional compression flag
|
|
2455
2651
|
* @returns a new `AssetInterface`
|
|
@@ -2463,7 +2659,7 @@ function createAsset(input) {
|
|
|
2463
2659
|
return new Asset(input);
|
|
2464
2660
|
}
|
|
2465
2661
|
/**
|
|
2466
|
-
*
|
|
2662
|
+
* Creates an asset manager for SEA-embedded or disk-loaded assets.
|
|
2467
2663
|
*
|
|
2468
2664
|
* @param options - Asset manager options (root, event hooks)
|
|
2469
2665
|
* @returns a new `AssetManagerInterface`
|
|
@@ -2478,6 +2674,6 @@ function createAssetManager(options) {
|
|
|
2478
2674
|
return new AssetManager(options);
|
|
2479
2675
|
}
|
|
2480
2676
|
//#endregion
|
|
2481
|
-
export { Asset, AssetManager, BROTLI_EXTENSION,
|
|
2677
|
+
export { Asset, AssetManager, BROTLI_EXTENSION, DEFAULT_ENTRY_FORMAT, DEFAULT_SEA_COMPRESSION_QUALITY, ELF_CLASS_64, ELF_DATA_LSB, ELF_MAGIC, ELF_PAGE_SIZE, ELF_PF_R, ELF_PT_LOAD, ELF_PT_NOTE, ELF_PT_PHDR, Injector, MACHO_LC_SEGMENT_64, MACHO_MAGIC_64, PE32_MAGIC, PE32_PLUS_MAGIC, PE_MAGIC, PE_RESOURCE_DATA_ENTRY_SIZE, PE_RESOURCE_DIR_SIZE, PE_RESOURCE_ENTRY_SIZE, PE_RESOURCE_NAME_FLAG, PE_RESOURCE_SUBDIR_FLAG, PE_RT_RCDATA, PE_SCN_INITIALIZED_DATA, PE_SCN_MEM_READ, PE_SECTION_HEADER_SIZE, PE_SIGNATURE, SEA, SEAError, SEA_BLOB_RESOURCE, SEA_COMPRESSION_MODE_VALUES, SEA_PLATFORMS, SEA_SENTINEL_FUSE, SKIP_EXTENSIONS, ShellError, WINDOWS_SUBSYSTEM_CONSOLE, WINDOWS_SUBSYSTEM_GUI, alignELFNoteSize, alignTo, appendFile, buildBlobConfig, buildELFNoteHeader, buildSignCommand, compressDirectory, compressFile, computeSize, copyRange, createAsset, createAssetManager, createInjector, createSEA, ensureContained, ensureExists, ensureSafeKey, ensureSafeName, executeShell, finalizeExecutable, formatSize, isCompressible, isExecutableFormat, isPEExecutable, isPlatformSupported, isPowerOfTwo, isSEAError, isShellError, openBrowser, patchPESubsystem, patchSentinelFuse, readPEOffset, readU16, readU32, readU64, redactCommand, resolvePlatform, stripPESignature, stripTrailingNulls, syncDirectory, walkDirectory, writeU16, writeU32, writeU64 };
|
|
2482
2678
|
|
|
2483
2679
|
//# sourceMappingURL=index.js.map
|