@orkestrel/sea 0.0.14 → 0.0.15

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.
@@ -7,19 +7,28 @@ 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
- /** Holds the SEA sentinel fuse value embedded in the Node.js binary */
10
+ /**
11
+ * Holds the SEA sentinel fuse value embedded in the Node.js binary,
12
+ * `NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2`.
13
+ */
11
14
  var SEA_SENTINEL_FUSE = "NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2";
12
- /** Names the SEA blob resource in the executable */
15
+ /** Names the SEA blob resource in the executable, `NODE_SEA_BLOB`. */
13
16
  var SEA_BLOB_RESOURCE = "NODE_SEA_BLOB";
14
- /** Holds the default Brotli compression quality level (maximum) */
17
+ /** Holds the default Brotli compression quality level, 11, the maximum Brotli accepts. */
15
18
  var DEFAULT_SEA_COMPRESSION_QUALITY = 11;
16
- /** Holds the Windows PE subsystem value for a GUI application (no terminal window) */
19
+ /**
20
+ * Holds the Windows PE subsystem value for a GUI application, 2, which launches
21
+ * without a terminal window.
22
+ */
17
23
  var WINDOWS_SUBSYSTEM_GUI = 2;
18
- /** Holds the Windows PE subsystem value for a console application */
24
+ /** Holds the Windows PE subsystem value for a console application, 3. */
19
25
  var WINDOWS_SUBSYSTEM_CONSOLE = 3;
20
- /** Names the file extension indicating Brotli compression */
26
+ /** Names the file extension indicating Brotli compression, `.br`. */
21
27
  var BROTLI_EXTENSION = ".br";
22
- /** Lists the file extensions Brotli compression skips */
28
+ /**
29
+ * Lists the file extensions Brotli compression skips, the already-compressed
30
+ * archive, image, font, and media formats.
31
+ */
23
32
  var SKIP_EXTENSIONS = /* @__PURE__ */ new Set([
24
33
  ".br",
25
34
  ".gz",
@@ -43,55 +52,67 @@ var SKIP_EXTENSIONS = /* @__PURE__ */ new Set([
43
52
  ".zip",
44
53
  ".tar"
45
54
  ]);
46
- /** Names the default SEA entry point module format when none is specified */
55
+ /** Names the default SEA entry point module format when none is specified, `cjs`. */
47
56
  var DEFAULT_ENTRY_FORMAT = "cjs";
48
- /** Holds the DOS MZ header magic (first 2 bytes of a PE file) */
57
+ /** Holds the DOS MZ header magic, 0x5a4d, the first two bytes of a PE file. */
49
58
  var PE_MAGIC = 23117;
50
- /** Holds the PE signature, "PE\0\0" as a 32-bit value */
59
+ /** Holds the PE signature, 0x00004550, the bytes `PE\0\0` read as a 32-bit value. */
51
60
  var PE_SIGNATURE = 17744;
52
- /** Holds the PE32 optional header magic */
61
+ /** Holds the PE32 optional header magic, 0x10b. */
53
62
  var PE32_MAGIC = 267;
54
- /** Holds the PE32+ (64-bit) optional header magic */
63
+ /** Holds the PE32+ optional header magic, 0x20b, the 64-bit form. */
55
64
  var PE32_PLUS_MAGIC = 523;
56
- /** Holds the ELF magic, 0x7F 'E' 'L' 'F' as a 32-bit big-endian value */
65
+ /**
66
+ * Holds the ELF magic, 0x7f454c46, the bytes 0x7F `E` `L` `F` read as a 32-bit
67
+ * big-endian value.
68
+ */
57
69
  var ELF_MAGIC = 2135247942;
58
- /** Holds the ELF 64-bit class identifier */
70
+ /** Holds the ELF 64-bit class identifier, 2. */
59
71
  var ELF_CLASS_64 = 2;
60
- /** Holds the ELF little-endian data encoding */
72
+ /** Holds the ELF little-endian data encoding, 1. */
61
73
  var ELF_DATA_LSB = 1;
62
- /** Holds the ELF program header type for a note segment */
74
+ /** Holds the ELF program header type for a note segment, 4. */
63
75
  var ELF_PT_NOTE = 4;
64
- /** Holds the ELF program header type for a loadable segment */
76
+ /** Holds the ELF program header type for a loadable segment, 1. */
65
77
  var ELF_PT_LOAD = 1;
66
- /** Holds the ELF program header type for the program header table itself */
78
+ /** Holds the ELF program header type for the program header table itself, 6. */
67
79
  var ELF_PT_PHDR = 6;
68
- /** Holds the ELF segment permission flag marking a segment readable */
80
+ /** Holds the ELF segment permission flag marking a segment readable, 4. */
69
81
  var ELF_PF_R = 4;
70
- /** Holds the page size an injected ELF segment is aligned to */
82
+ /** Holds the page size an injected ELF segment is aligned to, 0x1000. */
71
83
  var ELF_PAGE_SIZE = 4096;
72
- /** Holds the Mach-O 64-bit magic (little-endian) */
84
+ /** Holds the Mach-O 64-bit magic, 0xfeedfacf, in little-endian byte order. */
73
85
  var MACHO_MAGIC_64 = 4277009103;
74
- /** Holds the Mach-O LC_SEGMENT_64 load command */
86
+ /** Holds the Mach-O `LC_SEGMENT_64` load command, 0x19. */
75
87
  var MACHO_LC_SEGMENT_64 = 25;
76
- /** Holds the PE resource type RT_RCDATA (raw data) */
88
+ /** Holds the PE resource type `RT_RCDATA` for raw data, 10. */
77
89
  var PE_RT_RCDATA = 10;
78
- /** Holds the size of IMAGE_RESOURCE_DIRECTORY in bytes */
90
+ /** Holds the size of `IMAGE_RESOURCE_DIRECTORY` in bytes, 16. */
79
91
  var PE_RESOURCE_DIR_SIZE = 16;
80
- /** Holds the size of IMAGE_RESOURCE_DIRECTORY_ENTRY in bytes */
92
+ /** Holds the size of `IMAGE_RESOURCE_DIRECTORY_ENTRY` in bytes, 8. */
81
93
  var PE_RESOURCE_ENTRY_SIZE = 8;
82
- /** Holds the size of IMAGE_RESOURCE_DATA_ENTRY in bytes */
94
+ /** Holds the size of `IMAGE_RESOURCE_DATA_ENTRY` in bytes, 16. */
83
95
  var PE_RESOURCE_DATA_ENTRY_SIZE = 16;
84
- /** Holds the PE section header size in bytes */
96
+ /** Holds the PE section header size in bytes, 40. */
85
97
  var PE_SECTION_HEADER_SIZE = 40;
86
- /** Holds the high bit mask for a resource directory entry offset (indicates subdirectory) */
98
+ /**
99
+ * Holds the high bit mask marking a resource directory entry offset as a
100
+ * subdirectory, 0x80000000.
101
+ */
87
102
  var PE_RESOURCE_SUBDIR_FLAG = 2147483648;
88
- /** Holds the high bit mask for a resource name entry (indicates named vs integer ID) */
103
+ /**
104
+ * Holds the high bit mask marking a resource entry as named rather than
105
+ * integer-identified, 0x80000000.
106
+ */
89
107
  var PE_RESOURCE_NAME_FLAG = 2147483648;
90
- /** Marks a section as containing initialized data */
108
+ /** Marks a section as containing initialized data, 0x00000040. */
91
109
  var PE_SCN_INITIALIZED_DATA = 64;
92
- /** Marks a section as readable */
110
+ /** Marks a section as readable, 0x40000000. */
93
111
  var PE_SCN_MEM_READ = 1073741824;
94
- /** Holds the platform-specific SEA build configurations */
112
+ /**
113
+ * Holds the platform-specific SEA build configurations, keyed by
114
+ * `process.platform` for `win32`, `darwin`, and `linux`.
115
+ */
95
116
  var SEA_PLATFORMS = Object.freeze({
96
117
  win32: Object.freeze({
97
118
  executable: "node.exe",
@@ -117,7 +138,10 @@ var SEA_PLATFORMS = Object.freeze({
117
138
  }),
118
139
  linux: Object.freeze({ executable: "node" })
119
140
  });
120
- /** Maps a {@link SEACompressionMode} to its numeric Brotli mode value */
141
+ /**
142
+ * Maps a {@link SEACompressionMode} to its numeric Brotli mode value: `generic`
143
+ * to 0, `text` to 1, and `font` to 2.
144
+ */
121
145
  var SEA_COMPRESSION_MODE_VALUES = Object.freeze({
122
146
  generic: 0,
123
147
  text: 1,
@@ -217,7 +241,7 @@ function isShellError(value) {
217
241
  //#endregion
218
242
  //#region src/server/validators.ts
219
243
  /**
220
- * Checks if a value is a valid {@link ExecutableFormat}.
244
+ * Checks whether a value is a valid {@link ExecutableFormat}.
221
245
  *
222
246
  * @param value - Value to check
223
247
  * @returns True if value is `'pe'`, `'elf'`, or `'macho'`; false otherwise
@@ -243,7 +267,7 @@ function resolvePlatform(platform) {
243
267
  return SEA_PLATFORMS[platform ?? process.platform];
244
268
  }
245
269
  /**
246
- * Checks if the current or specified platform is supported for SEA builds.
270
+ * Checks whether the current or named platform is supported for SEA builds.
247
271
  *
248
272
  * @param platform - Platform identifier to check
249
273
  * @returns True if the platform has a known configuration; false otherwise
@@ -274,7 +298,8 @@ function isCompressible(path) {
274
298
  return !SKIP_EXTENSIONS.has(ext);
275
299
  }
276
300
  /**
277
- * Walks a directory recursively and returns all file paths.
301
+ * Walks a directory recursively and returns every file path it finds, relative to
302
+ * the base and skipping symlinks.
278
303
  *
279
304
  * @param directory - Directory to walk
280
305
  * @param base - Base directory for relative path calculation
@@ -294,7 +319,8 @@ function walkDirectory(directory, base) {
294
319
  return result;
295
320
  }
296
321
  /**
297
- * Redacts password arguments from a shell command.
322
+ * Redacts password arguments from a shell command, so the command is safe to
323
+ * include in an error message.
298
324
  *
299
325
  * @param command - Command arguments to redact
300
326
  * @returns A copy with values following password flags replaced by a marker
@@ -562,7 +588,7 @@ function appendFile(target, source, chunk = 4194304) {
562
588
  }
563
589
  }
564
590
  /**
565
- * Truncates a string at its first NUL character.
591
+ * Truncates a NUL-padded binary name field at its first NUL character.
566
592
  *
567
593
  * @remarks
568
594
  * Binary name fields (PE section names, Mach-O segment and section names, ELF
@@ -638,7 +664,7 @@ function writeU16(fd, offset, value) {
638
664
  writeSync(fd, buf, 0, 2, offset);
639
665
  }
640
666
  /**
641
- * Checks if a file is a Windows PE executable.
667
+ * Checks whether a file is a Windows PE executable.
642
668
  *
643
669
  * @param path - Path to the file
644
670
  * @returns True if the file has a valid PE signature; false otherwise
@@ -678,11 +704,11 @@ function patchPESubsystem(path, subsystem) {
678
704
  * @remarks
679
705
  * If the certificate overlay described by the security directory sits at the
680
706
  * very end of the file (the common case for `signtool`-signed binaries), the
681
- * file is truncated to the certificate's start FIRST so the overlay bytes are
707
+ * file is truncated to the certificate's start first so the overlay bytes are
682
708
  * discarded rather than left as dead weight for a downstream injector to
683
709
  * bury mid-file. The directory read is bounds-checked so a malformed or
684
- * truncated security directory cannot throw unexpectedly — it is simply
685
- * skipped and only the directory entry is zeroed.
710
+ * truncated security directory cannot throw unexpectedly — it is skipped
711
+ * and only the directory entry is zeroed.
686
712
  *
687
713
  * @param path - Path to the executable
688
714
  */
@@ -710,13 +736,13 @@ function stripPESignature(path) {
710
736
  * Builds the `signtool sign` argv for signing a Windows executable.
711
737
  *
712
738
  * @remarks
713
- * Requires EXACTLY ONE certificate source — `sign.file` (a `.pfx`/`.p12`
739
+ * Requires exactly one certificate source — `sign.file` (a `.pfx`/`.p12`
714
740
  * file, paired with `sign.password` when present) XOR `sign.thumbprint` (a
715
741
  * certificate already installed in the Windows store). When `sign.timestamp`
716
742
  * is set it is parsed with the `URL` constructor and must be an `http:` or
717
743
  * `https:` URL. The returned argv is passed directly to `executeShell` — never
718
744
  * through a shell — so nothing in `sign` can be interpreted as a flag or
719
- * injected into a command line. `sign.password` is NEVER included in a
745
+ * injected into a command line. `sign.password` is never included in a
720
746
  * thrown error's message or `context`.
721
747
  *
722
748
  * @param sign - Windows signing options
@@ -778,7 +804,7 @@ function buildSignCommand(sign, target) {
778
804
  * Formats a byte count as a human-readable string.
779
805
  *
780
806
  * @param bytes - Byte count
781
- * @returns Formatted string (e.g. `"1.23 MB"`, `"456 KB"`)
807
+ * @returns Formatted string (for example `"1.23 MB"`, `"456 KB"`)
782
808
  */
783
809
  function formatSize(bytes) {
784
810
  if (bytes < 1024) return `${String(bytes)} B`;
@@ -790,7 +816,7 @@ function formatSize(bytes) {
790
816
  *
791
817
  * @remarks
792
818
  * Rejects an empty key, an absolute path, a key containing a backslash, a
793
- * Windows drive-relative specifier (e.g. `'C:foo'`), or any `/`-separated
819
+ * Windows drive-relative specifier (for example `'C:foo'`), or any `/`-separated
794
820
  * segment equal to `'..'` — all of which could escape the intended asset root.
795
821
  *
796
822
  * @param key - Asset key to validate
@@ -818,10 +844,10 @@ function ensureSafeKey(key) {
818
844
  * Resolves `path` against `base`, then dereferences both the resolved path
819
845
  * and `base` with `realpathSync` and requires the real resolved path to
820
846
  * equal the real base or begin with the real base plus a path separator.
821
- * Dereferencing BOTH sides means a symlinked base itself (e.g. macOS `/tmp`
847
+ * Dereferencing both sides means a symlinked base itself (for example macOS `/tmp`
822
848
  * -> `/private/tmp`) still matches, while a symlink inside the tree that
823
849
  * points outside the real base is correctly rejected. A `realpathSync`
824
- * failure (e.g. `ENOENT`) is wrapped as a coded `SEAError` rather than
850
+ * failure (for example `ENOENT`) is wrapped as a coded `SEAError` rather than
825
851
  * leaking the raw Node error.
826
852
  *
827
853
  * @param base - Absolute path to the containing root
@@ -861,8 +887,8 @@ function ensureContained(base, path) {
861
887
  *
862
888
  * @remarks
863
889
  * Rejects an empty name, `'.'`, `'..'`, a name containing a `/` or `\`
864
- * separator, an absolute path, or a Windows drive-relative specifier (e.g.
865
- * `'C:foo'`) — all of which could redirect the output executable outside
890
+ * separator, an absolute path, or a Windows drive-relative specifier (for
891
+ * example `'C:foo'`) — all of which could redirect the output executable outside
866
892
  * the intended output directory.
867
893
  *
868
894
  * @param name - Output executable base name to validate
@@ -884,14 +910,14 @@ function ensureSafeName(name) {
884
910
  * Fsyncs a directory to durably persist a prior file rename/create within it.
885
911
  *
886
912
  * @remarks
887
- * A `rename`/`create` is only durable once its CONTAINING directory entry is
913
+ * A `rename`/`create` is only durable once its containing directory entry is
888
914
  * flushed — fsyncing the file itself is not enough. `path` is the directory
889
915
  * to fsync (callers pass `dirname(target)`, not the file itself). On Windows
890
916
  * there is no directory file handle to fsync, so this is a no-op there (the
891
917
  * platform lacks the primitive, not a failure). Some filesystems/platforms
892
918
  * return a benign errno for a directory fsync attempt (`EINVAL`, `ENOTSUP`,
893
919
  * `EISDIR`, `EPERM`, `EACCES`) — those are treated as "fsync unsupported
894
- * here" and swallowed; anything else (e.g. `ENOENT`, meaning the directory
920
+ * here" and swallowed; anything else (for example `ENOENT`, meaning the directory
895
921
  * itself is missing) is a genuine failure and is thrown as a coded `SEAError`.
896
922
  *
897
923
  * @param path - Directory path to fsync
@@ -965,7 +991,7 @@ function finalizeExecutable(source, target) {
965
991
  * @remarks
966
992
  * A pure leaf extracted from the SEA build orchestrator. The `mainFormat`
967
993
  * field (`'commonjs' | 'module'`) exists only in Node >= 25.7 — for a `'cjs'`
968
- * entry (the default) `mainFormat` is OMITTED entirely so the config still
994
+ * entry (the default) `mainFormat` is omitted entirely so the config still
969
995
  * builds on older Node hosts where `'commonjs'` is already the implicit
970
996
  * default; for an `'esm'` entry `mainFormat: 'module'` is set explicitly
971
997
  * (this requires a Node >= 25.7 build host). Node also documents that
@@ -1020,7 +1046,7 @@ function alignELFNoteSize(value) {
1020
1046
  * (`strncmp(name, "NODE_SEA", 8)`), so `resource` is written in full but
1021
1047
  * only needs to begin with those 8 bytes to be found at runtime.
1022
1048
  *
1023
- * @param resource - Note name (SEA resource identifier, e.g. `NODE_SEA_BLOB`)
1049
+ * @param resource - Note name (SEA resource identifier, for example `NODE_SEA_BLOB`)
1024
1050
  * @param blobSize - Size in bytes of the SEA blob that will follow the header
1025
1051
  * @returns The note header bytes and the total on-disk size of header + blob
1026
1052
  *
@@ -1094,10 +1120,11 @@ function copyRange(source, target, start, length, chunk = 4194304) {
1094
1120
  /**
1095
1121
  * Patches the sentinel fuse in a binary from `:0` to `:1`.
1096
1122
  *
1097
- * Searches the file in 64 MB chunks with overlap to handle any file size.
1098
- * The fuse signals to the Node.js runtime that a SEA blob is present.
1099
- * This is normally handled by the Injector but is also available standalone
1100
- * when using native PE resource injection.
1123
+ * @remarks
1124
+ * Searches the file in 64 MB chunks with overlap to handle any file size. The fuse
1125
+ * signals to the Node.js runtime that a SEA blob is present. The `Injector` class
1126
+ * normally applies this patch, and it stays available standalone for a build that
1127
+ * injects the PE resource natively instead.
1101
1128
  *
1102
1129
  * @param executable - Absolute path to the executable
1103
1130
  * @param fuse - Sentinel fuse string (without the `:0` suffix)
@@ -1158,7 +1185,7 @@ function patchSentinelFuse(executable, fuse) {
1158
1185
  * outlives the host app and never keeps it alive, and it swallows the async
1159
1186
  * host fault an absent opener binary raises. Only `http:` and `https:` URLs
1160
1187
  * are accepted — any other scheme (including a string that merely looks like a
1161
- * CLI flag, e.g. `'-e ...'`) fails to parse as an http(s) URL and is rejected
1188
+ * CLI flag, for example `'-e ...'`) fails to parse as an http(s) URL and is rejected
1162
1189
  * before anything is spawned.
1163
1190
  *
1164
1191
  * @param url - Absolute http or https URL to open
@@ -2600,32 +2627,43 @@ var AssetManager = class {
2600
2627
  //#endregion
2601
2628
  //#region src/server/factories.ts
2602
2629
  /**
2603
- * Creates a new SEA build orchestrator.
2630
+ * Creates a SEA build orchestrator over the given options and returns it as a
2631
+ * `SEAInterface`, the published contract a caller holds instead of the `SEA` class.
2604
2632
  *
2605
2633
  * @param options - SEA build options
2606
2634
  * @returns a new `SEAInterface`
2607
2635
  *
2608
- * @example
2636
+ * @example Build a single executable
2609
2637
  * ```ts
2638
+ * import { createSEA, formatSize } from '@orkestrel/sea'
2639
+ *
2610
2640
  * const sea = createSEA({
2611
- * name: 'orkestrel',
2612
- * entry: { path: 'dist/bin/serve.cjs' },
2613
- * output: 'dist/sea',
2614
- * assets: { 'index.html.br': 'dist/client/index.html.br' },
2615
- * compression: { paths: ['dist/client'] },
2641
+ * name: 'myapp',
2642
+ * entry: { path: 'dist/server/serve.cjs' },
2643
+ * output: 'dist/sea',
2644
+ * assets: { 'model.gguf': 'models/model.gguf' },
2645
+ * compression: { paths: ['dist/app/browser'], mode: 'text' },
2646
+ * windows: { terminal: false },
2647
+ * timeout: 30_000,
2616
2648
  * })
2649
+ *
2617
2650
  * const result = await sea.execute()
2651
+ * process.stdout.write(
2652
+ * `${result.executable} ${formatSize(result.size)} ${String(result.duration)}ms\n`,
2653
+ * )
2618
2654
  * ```
2619
2655
  */
2620
2656
  function createSEA(options) {
2621
2657
  return new SEA(options);
2622
2658
  }
2623
2659
  /**
2624
- * Creates a cross-platform binary resource injector.
2660
+ * Creates a resource injector bound to one target executable and returns it as an
2661
+ * `InjectorInterface`, with the executable's format already detected from its header.
2625
2662
  *
2626
- * Detects the executable format (PE, ELF, Mach-O) from the file header
2627
- * and injects the blob using pure TypeScript file I/O — no WASM, no
2628
- * external tools.
2663
+ * @remarks
2664
+ * The format detected at construction — PE, ELF, or Mach-O — selects the strategy
2665
+ * `inject` takes. Every write runs through pure TypeScript file I/O, so the injector
2666
+ * needs no WASM and no external tool.
2629
2667
  *
2630
2668
  * @param options - Injector options
2631
2669
  * @returns a new `InjectorInterface`
@@ -2645,7 +2683,9 @@ function createInjector(options) {
2645
2683
  return new Injector(options);
2646
2684
  }
2647
2685
  /**
2648
- * Creates a single named asset.
2686
+ * Creates one named asset from a key and a content buffer and returns it as an
2687
+ * `AssetInterface`, with `compressed` inferred from the key where the input leaves
2688
+ * it unset.
2649
2689
  *
2650
2690
  * @param input - Asset key, content, and optional compression flag
2651
2691
  * @returns a new `AssetInterface`
@@ -2659,7 +2699,8 @@ function createAsset(input) {
2659
2699
  return new Asset(input);
2660
2700
  }
2661
2701
  /**
2662
- * Creates an asset manager for SEA-embedded or disk-loaded assets.
2702
+ * Creates an asset collection and returns it as an `AssetManagerInterface`, already
2703
+ * carrying whatever assets the running SEA blob embeds.
2663
2704
  *
2664
2705
  * @param options - Asset manager options (root, event hooks)
2665
2706
  * @returns a new `AssetManagerInterface`