AK-21/Graphite-Industrial-Intelligence
0
1/**2 * This feature allows the distribution of a Node.js application conveniently to a3 * system that does not have Node.js installed.4 *5 * Node.js supports the creation of [single executable applications](https://github.com/nodejs/single-executable) by allowing6 * the injection of a blob prepared by Node.js, which can contain a bundled script,7 * into the `node` binary. During start up, the program checks if anything has been8 * injected. If the blob is found, it executes the script in the blob. Otherwise9 * Node.js operates as it normally does.10 *11 * The single executable application feature currently only supports running a12 * single embedded script using the `CommonJS` module system.13 *14 * Users can create a single executable application from their bundled script15 * with the `node` binary itself and any tool which can inject resources into the16 * binary.17 *18 * Here are the steps for creating a single executable application using one such19 * tool, [postject](https://github.com/nodejs/postject):20 *21 * 1. Create a JavaScript file:22 * ```bash23 * echo 'console.log(`Hello, ${process.argv[2]}!`);' > hello.js24 * ```25 * 2. Create a configuration file building a blob that can be injected into the26 * single executable application (see `Generating single executable preparation blobs` for details):27 * ```bash28 * echo '{ "main": "hello.js", "output": "sea-prep.blob" }' > sea-config.json29 * ```30 * 3. Generate the blob to be injected:31 * ```bash32 * node --experimental-sea-config sea-config.json33 * ```34 * 4. Create a copy of the `node` executable and name it according to your needs:35 * * On systems other than Windows:36 * ```bash37 * cp $(command -v node) hello38 * ```39 * * On Windows:40 * ```text41 * node -e "require('fs').copyFileSync(process.execPath, 'hello.exe')"42 * ```43 * The `.exe` extension is necessary.44 * 5. Remove the signature of the binary (macOS and Windows only):45 * * On macOS:46 * ```bash47 * codesign --remove-signature hello48 * ```49 * * On Windows (optional):50 * [signtool](https://learn.microsoft.com/en-us/windows/win32/seccrypto/signtool) can be used from the installed [Windows SDK](https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/).51 * If this step is52 * skipped, ignore any signature-related warning from postject.53 * ```powershell54 * signtool remove /s hello.exe55 * ```56 * 6. Inject the blob into the copied binary by running `postject` with57 * the following options:58 * * `hello` / `hello.exe` \- The name of the copy of the `node` executable59 * created in step 4.60 * * `NODE_SEA_BLOB` \- The name of the resource / note / section in the binary61 * where the contents of the blob will be stored.62 * * `sea-prep.blob` \- The name of the blob created in step 1.63 * * `--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2` \- The [fuse](https://www.electronjs.org/docs/latest/tutorial/fuses) used by the Node.js project to detect if a file has been64 * injected.65 * * `--macho-segment-name NODE_SEA` (only needed on macOS) - The name of the66 * segment in the binary where the contents of the blob will be67 * stored.68 * To summarize, here is the required command for each platform:69 * * On Linux:70 * ```bash71 * npx postject hello NODE_SEA_BLOB sea-prep.blob \72 * --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b273 * ```74 * * On Windows - PowerShell:75 * ```powershell76 * npx postject hello.exe NODE_SEA_BLOB sea-prep.blob `77 * --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b278 * ```79 * * On Windows - Command Prompt:80 * ```text81 * npx postject hello.exe NODE_SEA_BLOB sea-prep.blob ^82 * --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b283 * ```84 * * On macOS:85 * ```bash86 * npx postject hello NODE_SEA_BLOB sea-prep.blob \87 * --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \88 * --macho-segment-name NODE_SEA89 * ```90 * 7. Sign the binary (macOS and Windows only):91 * * On macOS:92 * ```bash93 * codesign --sign - hello94 * ```95 * * On Windows (optional):96 * A certificate needs to be present for this to work. However, the unsigned97 * binary would still be runnable.98 * ```powershell99 * signtool sign /fd SHA256 hello.exe100 * ```101 * 8. Run the binary:102 * * On systems other than Windows103 * ```console104 * $ ./hello world105 * Hello, world!106 * ```107 * * On Windows108 * ```console109 * $ .\hello.exe world110 * Hello, world!111 * ```112 * @since v19.7.0, v18.16.0113 * @experimental114 * @see [source](https://github.com/nodejs/node/blob/v24.x/src/node_sea.cc)115 */116declare module "node:sea" {117 type AssetKey = string;118 /**119 * @since v20.12.0120 * @return Whether this script is running inside a single-executable application.121 */122 function isSea(): boolean;123 /**124 * This method can be used to retrieve the assets configured to be bundled into the125 * single-executable application at build time.126 * An error is thrown when no matching asset can be found.127 * @since v20.12.0128 */129 function getAsset(key: AssetKey): ArrayBuffer;130 function getAsset(key: AssetKey, encoding: string): string;131 /**132 * Similar to `sea.getAsset()`, but returns the result in a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).133 * An error is thrown when no matching asset can be found.134 * @since v20.12.0135 */136 function getAssetAsBlob(key: AssetKey, options?: {137 type: string;138 }): Blob;139 /**140 * This method can be used to retrieve the assets configured to be bundled into the141 * single-executable application at build time.142 * An error is thrown when no matching asset can be found.143 *144 * Unlike `sea.getRawAsset()` or `sea.getAssetAsBlob()`, this method does not145 * return a copy. Instead, it returns the raw asset bundled inside the executable.146 *147 * For now, users should avoid writing to the returned array buffer. If the148 * injected section is not marked as writable or not aligned properly,149 * writes to the returned array buffer is likely to result in a crash.150 * @since v20.12.0151 */152 function getRawAsset(key: AssetKey): ArrayBuffer;153 /**154 * This method can be used to retrieve an array of all the keys of assets155 * embedded into the single-executable application.156 * An error is thrown when not running inside a single-executable application.157 * @since v24.8.0158 * @returns An array containing all the keys of the assets159 * embedded in the executable. If no assets are embedded, returns an empty array.160 */161 function getAssetKeys(): string[];162}163 