-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
src: add initial support for single executable applications #45038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 73 commits into
nodejs:main
from
RaisinTen:add-initial-support-for-single-executable-applications
Feb 18, 2023
Merged
Changes from all commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
6559b90
src: add initial support for single executable applications
RaisinTen 444ca79
test: fix `'node': No such file or directory` error on CI
RaisinTen a30e3fc
test: fix `Cannot find module '../common'` error on Jenkins CI
RaisinTen 5caad76
test: skip test on failing platforms
RaisinTen 1c5968d
test: make sure that double backslashes in Windows paths aren't removed
RaisinTen 290d69f
src: move argv replacement to another function
RaisinTen da1b134
doc: fix info about using notes on ELF
RaisinTen f08ca63
test: test code signing on Windows
RaisinTen afc5d75
lib,test: emit experimental warning
RaisinTen da74406
fixup! src: move argv replacement to another function
RaisinTen 412d592
test: skip on debug builds on Linux
RaisinTen 10b0fcd
test: skip on shared builds
RaisinTen 3addc19
test: skip on --without-ssl and --shared-openssl
RaisinTen 232ece5
test: skip on test-ibm-rhel8-s390x-1
RaisinTen f24e37c
test: skip on smartos
RaisinTen 8126bb3
test: skip when cross-compiling
RaisinTen 3c5c2cd
src: speed up case where no resource is present
RaisinTen b47b68d
src: use custom fuse string
RaisinTen 2fe91c6
test: do not skip on asan builds
RaisinTen e0a9af3
src,doc,test: remove :0 from sentinel fuse string
RaisinTen 665e5f8
test: skip on asan builds
RaisinTen fe6d488
src: change segment name from `__POSTJECT` to `NODE_JS` on macOS
RaisinTen 8c5ba96
test: only run test on Ubuntu for Linux
RaisinTen 2855679
test: skip on non-x64 Linux archictectures
RaisinTen 9daeaf0
test: add back ubuntu check for linux
RaisinTen bf4e519
src: call IsSingleExecutable() before FindSingleExecutableCode()
RaisinTen a4ac355
lib: expose require without fs access
RaisinTen 13dd503
src: wrap macho_segment_name assignment into a pre-processor for macOS
RaisinTen ea4a139
test: detect Ubuntu by parsing '/etc/os-release'
RaisinTen 5664222
test: set utf8 encoding while reading
RaisinTen 3a42a29
fixup! test: detect Ubuntu by parsing '/etc/os-release'
RaisinTen 0eb3dee
Apply suggestions from code review
RaisinTen 925775c
test: fix lint error
RaisinTen e841b26
doc: use "`node` binary" instead of just "binary"
RaisinTen aca1db7
doc: add note about linux support
RaisinTen e51708d
src: use external string
RaisinTen 2ad8e30
src: move all SEA code to per_process::sea in node_sea.cc
RaisinTen 2c6bb38
src: add TODO for non-ASCII character inputs
RaisinTen eceb1f7
src: create sea binding
RaisinTen 732b4e0
src: add a TODO to reuse LoadEnvironment
RaisinTen a1b2643
src: use UnionBytes insteda of extending from ExternalOneByteStringSi…
RaisinTen b62393b
src: avoid storing sea statics globally
RaisinTen 4d118cf
doc: move the second para above the first one
RaisinTen b040491
doc: https://github.com/nodejs/node/pull/45038#discussion_r1100745618
RaisinTen b4ab7f0
doc: https://github.com/nodejs/node/pull/45038#discussion_r1100746143
RaisinTen 17a019d
build: add TODO comment to node.gyp
RaisinTen ec052a1
doc: https://github.com/nodejs/node/pull/45038#discussion_r1101352130
RaisinTen c3c1ace
test: skip asan build only on linux
RaisinTen 2aee17f
test: move sea script to test/fixtures
RaisinTen d711de1
test: use execFileSync instead of execSync
RaisinTen e8819fb
Update test/parallel/test-single-executable-application.js
RaisinTen 5c7bdfd
Apply suggestions from code review
RaisinTen 44930d7
src: remove per_process nesting for the sea namespace
RaisinTen 1747349
fixup! Apply suggestions from code review
RaisinTen 308418e
doc: move technical details below
RaisinTen daa8f4d
src: fix bug from UnionBytes going out of scope
RaisinTen 132bfcd
src: add TODO for using 1 byte strings for ASCII-only sources
RaisinTen a0117da
Update doc/api/single-executable-applications.md
RaisinTen dfa4272
Update doc/api/single-executable-applications.md
RaisinTen aef7e26
Update doc/api/single-executable-applications.md
RaisinTen d63ebf1
Update doc/api/single-executable-applications.md
RaisinTen 6f6b7c0
Update doc/api/single-executable-applications.md
RaisinTen 04842d0
Update doc/api/single-executable-applications.md
RaisinTen aaa4b1b
fixup! Update doc/api/single-executable-applications.md
RaisinTen 0ad4016
Update doc/api/single-executable-applications.md
RaisinTen 7ebf905
doc: add explanation to clarify the steps
RaisinTen eecf141
build: use postect-api.h from deps
RaisinTen 8201ef9
Apply suggestions from code review
RaisinTen c276f2e
fixup! Apply suggestions from code review
RaisinTen 7fff038
doc: clarify platform support
RaisinTen 824945b
test: use fixtures helper for postject CLI path
RaisinTen b4a22ba
src: explain POSTJECT_SENTINEL_FUSE macro
RaisinTen e454d1b
test: skip on --with-intl=system-icu
RaisinTen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # Single executable applications | ||
|
|
||
| <!--introduced_in=REPLACEME--> | ||
|
|
||
| > Stability: 1 - Experimental: This feature is being designed and will change. | ||
|
|
||
| <!-- source_link=lib/internal/main/single_executable_application.js --> | ||
|
|
||
| This feature allows the distribution of a Node.js application conveniently to a | ||
| system that does not have Node.js installed. | ||
|
|
||
| Node.js supports the creation of [single executable applications][] by allowing | ||
| the injection of a JavaScript file into the `node` binary. During start up, the | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| program checks if anything has been injected. If the script is found, it | ||
| executes its contents. Otherwise Node.js operates as it normally does. | ||
|
|
||
| The single executable application feature only supports running a single | ||
| embedded [CommonJS][] file. | ||
|
|
||
| A bundled JavaScript file can be turned into a single executable application | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| with any tool which can inject resources into the `node` binary. | ||
|
|
||
| Here are the steps for creating a single executable application using one such | ||
| tool, [postject][]: | ||
|
|
||
| 1. Create a JavaScript file: | ||
| ```console | ||
| $ echo 'console.log(`Hello, ${process.argv[2]}!`);' > hello.js | ||
| ``` | ||
|
|
||
| 2. Create a copy of the `node` executable and name it according to your needs: | ||
| ```console | ||
| $ cp $(command -v node) hello | ||
| ``` | ||
|
|
||
| 3. Inject the JavaScript file into the copied binary by running `postject` with | ||
| the following options: | ||
|
|
||
| * `hello` - The name of the copy of the `node` executable created in step 2. | ||
| * `NODE_JS_CODE` - The name of the resource / note / section in the binary | ||
| where the contents of the JavaScript file will be stored. | ||
| * `hello.js` - The name of the JavaScript file created in step 1. | ||
| * `--sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2` - The | ||
| [fuse][] used by the Node.js project to detect if a file has been injected. | ||
| * `--macho-segment-name NODE_JS` (only needed on macOS) - The name of the | ||
| segment in the binary where the contents of the JavaScript file will be | ||
| stored. | ||
|
|
||
| To summarize, here is the required command for each platform: | ||
|
|
||
| * On systems other than macOS: | ||
| ```console | ||
| $ npx postject hello NODE_JS_CODE hello.js \ | ||
| --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 | ||
| ``` | ||
|
|
||
| * On macOS: | ||
| ```console | ||
| $ npx postject hello NODE_JS_CODE hello.js \ | ||
| --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \ | ||
| --macho-segment-name NODE_JS | ||
| ``` | ||
|
|
||
| 4. Run the binary: | ||
| ```console | ||
| $ ./hello world | ||
| Hello, world! | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| ### `require(id)` in the injected module is not file based | ||
|
|
||
| `require()` in the injected module is not the same as the [`require()`][] | ||
| available to modules that are not injected. It also does not have any of the | ||
| properties that non-injected [`require()`][] has except [`require.main`][]. It | ||
| can only be used to load built-in modules. Attempting to load a module that can | ||
| only be found in the file system will throw an error. | ||
|
|
||
| Instead of relying on a file based `require()`, users can bundle their | ||
| application into a standalone JavaScript file to inject into the executable. | ||
| This also ensures a more deterministic dependency graph. | ||
|
|
||
| However, if a file based `require()` is still needed, that can also be achieved: | ||
|
|
||
| ```js | ||
| const { createRequire } = require('node:module'); | ||
| require = createRequire(__filename); | ||
| ``` | ||
|
|
||
| ### `__filename` and `module.filename` in the injected module | ||
|
|
||
| The values of `__filename` and `module.filename` in the injected module are | ||
| equal to [`process.execPath`][]. | ||
|
|
||
| ### `__dirname` in the injected module | ||
|
|
||
| The value of `__dirname` in the injected module is equal to the directory name | ||
| of [`process.execPath`][]. | ||
|
|
||
| ### Single executable application creation process | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| A tool aiming to create a single executable Node.js application must | ||
| inject the contents of a JavaScript file into: | ||
|
|
||
| * a resource named `NODE_JS_CODE` if the `node` binary is a [PE][] file | ||
| * a section named `NODE_JS_CODE` in the `NODE_JS` segment if the `node` binary | ||
| is a [Mach-O][] file | ||
| * a note named `NODE_JS_CODE` if the `node` binary is an [ELF][] file | ||
|
|
||
| Search the binary for the | ||
| `NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2:0` [fuse][] string and flip the | ||
| last character to `1` to indicate that a resource has been injected. | ||
|
|
||
| ### Platform support | ||
|
|
||
| Single-executable support is tested regularly on CI only on the following | ||
| platforms: | ||
|
|
||
| * Windows | ||
| * macOS | ||
| * Linux (AMD64 only) | ||
|
|
||
| This is due to a lack of better tools to generate single-executables that can be | ||
| used to test this feature on other platforms. | ||
|
|
||
| Suggestions for other resource injection tools/workflows are welcomed. Please | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| start a discussion at <https://github.com/nodejs/single-executable/discussions> | ||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| to help us document them. | ||
|
|
||
| [CommonJS]: modules.md#modules-commonjs-modules | ||
| [ELF]: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format | ||
| [Mach-O]: https://en.wikipedia.org/wiki/Mach-O | ||
| [PE]: https://en.wikipedia.org/wiki/Portable_Executable | ||
| [`process.execPath`]: process.md#processexecpath | ||
| [`require()`]: modules.md#requireid | ||
| [`require.main`]: modules.md#accessing-the-main-module | ||
| [fuse]: https://www.electronjs.org/docs/latest/tutorial/fuses | ||
| [postject]: https://github.com/nodejs/postject | ||
| [single executable applications]: https://github.com/nodejs/single-executable | ||
81 changes: 81 additions & 0 deletions
81
doc/contributing/maintaining-single-executable-application-support.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Maintaining Single Executable Applications support | ||
|
|
||
| Support for [single executable applications][] is one of the key technical | ||
| priorities identified for the success of Node.js. | ||
|
|
||
RaisinTen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## High level strategy | ||
|
|
||
| From the [Next-10 discussions][] there are 2 approaches the project believes are | ||
| important to support: | ||
|
|
||
| ### Compile with Node.js into executable | ||
|
|
||
| This is the approach followed by [boxednode][]. | ||
|
|
||
| No additional code within the Node.js project is needed to support the | ||
| option of compiling a bundled application along with Node.js into a single | ||
| executable application. | ||
|
|
||
| ### Bundle into existing Node.js executable | ||
|
|
||
| This is the approach followed by [pkg][]. | ||
|
|
||
| The project does not plan to provide the complete solution but instead the key | ||
| elements which are required in the Node.js executable in order to enable | ||
| bundling with the pre-built Node.js binaries. This includes: | ||
|
|
||
| * Looking for a segment within the executable that holds bundled code. | ||
| * Running the bundled code when such a segment is found. | ||
|
|
||
| It is left up to external tools/solutions to: | ||
|
|
||
| * Bundle code into a single script. | ||
| * Generate a command line with appropriate options. | ||
| * Add a segment to an existing Node.js executable which contains | ||
| the command line and appropriate headers. | ||
| * Re-generate or removing signatures on the resulting executable | ||
| * Provide a virtual file system, and hooking it in if needed to | ||
| support native modules or reading file contents. | ||
|
|
||
| However, the project also maintains a separate tool, [postject][], for injecting | ||
| arbitrary read-only resources into the binary such as those needed for bundling | ||
| the application into the runtime. | ||
|
|
||
| ## Planning | ||
|
|
||
| Planning for this feature takes place in the [single-executable repository][]. | ||
|
|
||
| ## Upcoming features | ||
|
|
||
| Currently, only running a single embedded CommonJS file is supported but support | ||
| for the following features are in the list of work we'd like to get to: | ||
|
|
||
| * Running an embedded ESM file. | ||
| * Running an archive of multiple files. | ||
| * Embedding [Node.js CLI options][] into the binary. | ||
| * [XCOFF][] executable format. | ||
| * Run tests on Linux architectures/distributions other than AMD64 Ubuntu. | ||
|
|
||
| ## Disabling single executable application support | ||
|
|
||
| To disable single executable application support, build Node.js with the | ||
| `--disable-single-executable-application` configuration option. | ||
|
|
||
| ## Implementation | ||
|
|
||
| When built with single executable application support, the Node.js process uses | ||
| [`postject-api.h`][] to check if the `NODE_JS_CODE` section exists in the | ||
| binary. If it is found, it passes the buffer to | ||
| [`single_executable_application.js`][], which executes the contents of the | ||
| embedded script. | ||
|
|
||
| [Next-10 discussions]: https://github.com/nodejs/next-10/blob/main/meetings/summit-nov-2021.md#single-executable-applications | ||
| [Node.js CLI options]: https://nodejs.org/api/cli.html | ||
| [XCOFF]: https://www.ibm.com/docs/en/aix/7.2?topic=formats-xcoff-object-file-format | ||
| [`postject-api.h`]: https://github.com/nodejs/node/blob/71951a0e86da9253d7c422fa2520ee9143e557fa/test/fixtures/postject-copy/node_modules/postject/dist/postject-api.h | ||
| [`single_executable_application.js`]: https://github.com/nodejs/node/blob/main/lib/internal/main/single_executable_application.js | ||
| [boxednode]: https://github.com/mongodb-js/boxednode | ||
| [pkg]: https://github.com/vercel/pkg | ||
| [postject]: https://github.com/nodejs/postject | ||
| [single executable applications]: https://github.com/nodejs/node/blob/main/doc/contributing/technical-priorities.md#single-executable-applications | ||
| [single-executable repository]: https://github.com/nodejs/single-executable | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| 'use strict'; | ||
| const { | ||
| prepareMainThreadExecution, | ||
| markBootstrapComplete, | ||
| } = require('internal/process/pre_execution'); | ||
| const { getSingleExecutableCode } = internalBinding('sea'); | ||
| const { emitExperimentalWarning } = require('internal/util'); | ||
| const { Module, wrapSafe } = require('internal/modules/cjs/loader'); | ||
| const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors'); | ||
|
|
||
| prepareMainThreadExecution(false, true); | ||
| markBootstrapComplete(); | ||
|
|
||
| emitExperimentalWarning('Single executable application'); | ||
|
|
||
| // This is roughly the same as: | ||
| // | ||
| // const mod = new Module(filename); | ||
| // mod._compile(contents, filename); | ||
| // | ||
| // but the code has been duplicated because currently there is no way to set the | ||
| // value of require.main to module. | ||
| // | ||
| // TODO(RaisinTen): Find a way to deduplicate this. | ||
|
|
||
| const filename = process.execPath; | ||
| const contents = getSingleExecutableCode(); | ||
| const compiledWrapper = wrapSafe(filename, contents); | ||
|
|
||
| const customModule = new Module(filename, null); | ||
| customModule.filename = filename; | ||
| customModule.paths = Module._nodeModulePaths(customModule.path); | ||
|
|
||
| const customExports = customModule.exports; | ||
|
|
||
| function customRequire(path) { | ||
| if (!Module.isBuiltin(path)) { | ||
| throw new ERR_UNKNOWN_BUILTIN_MODULE(path); | ||
| } | ||
|
|
||
| return require(path); | ||
| } | ||
|
|
||
| customRequire.main = customModule; | ||
|
|
||
| const customFilename = customModule.filename; | ||
|
|
||
| const customDirname = customModule.path; | ||
|
|
||
| compiledWrapper( | ||
| customExports, | ||
| customRequire, | ||
| customModule, | ||
| customFilename, | ||
| customDirname); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.