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