Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions doc/api/single-executable-applications.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,38 @@ are equal to [`process.execPath`][].
The value of `__dirname` in the injected main script is equal to the directory
name of [`process.execPath`][].

### Using native addons in the injected main script

Native addons can be bundled as assets into the single-executable application
by specifying them in the `assets` field of the configuration file used to
generate the single-executable application preparation blob.
The addon can then be loaded in the injected main script by writing the asset
to a temporary file and loading it with `process.dlopen()`.

```json
{
"main": "/path/to/bundled/script.js",
"output": "/path/to/write/the/generated/blob.blob",
"assets": {
"myaddon.node": "/path/to/myaddon/build/Release/myaddon.node"
}
}
```

```js
// script.js
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { getRawAsset } = require('node:sea');
const addonPath = path.join(os.tmpdir(), 'myaddon.node');
fs.writeFileSync(addonPath, new Uint8Array(getRawAsset('myaddon.node')));
const myaddon = { exports: {} };
process.dlopen(myaddon, addonPath);
console.log(myaddon.exports);
fs.rmSync(addonPath);
```

## Notes

### Single executable application creation process
Expand Down
4 changes: 4 additions & 0 deletions test/node-api/node-api.status
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ prefix node-api
# https://github.com/nodejs/node/issues/43457
test_fatal/test_threads: PASS,FLAKY
test_fatal/test_threads_report: PASS,FLAKY

[$system==linux && $arch==ppc64]
# https://github.com/nodejs/node/issues/59561
test_sea_addon/test: SKIP
Empty file added test/node-api/sea_addon
Empty file.
17 changes: 17 additions & 0 deletions test/node-api/test_sea_addon/binding.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <node_api.h>
#include <string.h>
#include "../../js-native-api/common.h"

static napi_value Method(napi_env env, napi_callback_info info) {
napi_value world;
const char* str = "world";
size_t str_len = strlen(str);
NODE_API_CALL(env, napi_create_string_utf8(env, str, str_len, &world));
return world;
}

NAPI_MODULE_INIT() {
napi_property_descriptor desc = DECLARE_NODE_API_PROPERTY("hello", Method);
NODE_API_CALL(env, napi_define_properties(env, exports, 1, &desc));
return exports;
}
8 changes: 8 additions & 0 deletions test/node-api/test_sea_addon/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.c" ]
}
]
}
75 changes: 75 additions & 0 deletions test/node-api/test_sea_addon/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';
// This tests that SEA can load addons packaged as assets by writing them to disk
// and loading them via process.dlopen().
const common = require('../../common');
const { generateSEA, skipIfSingleExecutableIsNotSupported } = require('../../common/sea');

skipIfSingleExecutableIsNotSupported();

const assert = require('assert');

const tmpdir = require('../../common/tmpdir');
const { copyFileSync, writeFileSync, existsSync, rmSync } = require('fs');
const {
spawnSyncAndExitWithoutError,
spawnSyncAndAssert,
} = require('../../common/child_process');
const { join } = require('path');
const configFile = tmpdir.resolve('sea-config.json');
const seaPrepBlob = tmpdir.resolve('sea-prep.blob');
const outputFile = tmpdir.resolve(process.platform === 'win32' ? 'sea.exe' : 'sea');
tmpdir.refresh();

// Copy test fixture to working directory
const addonPath = join(__dirname, 'build', common.buildType, 'binding.node');
const copiedAddonPath = tmpdir.resolve('binding.node');
copyFileSync(addonPath, copiedAddonPath);
writeFileSync(tmpdir.resolve('sea.js'), `
const sea = require('node:sea');
const fs = require('fs');
const path = require('path');

const addonPath = path.join(process.cwd(), 'hello.node');
fs.writeFileSync(addonPath, new Uint8Array(sea.getRawAsset('hello.node')));
const mod = {exports: {}}
process.dlopen(mod, addonPath);
console.log('hello,', mod.exports.hello());
`, 'utf-8');

writeFileSync(configFile, `
{
"main": "sea.js",
"output": "sea-prep.blob",
"disableExperimentalSEAWarning": true,
"assets": {
"hello.node": "binding.node"
}
}
`, 'utf8');

spawnSyncAndExitWithoutError(
process.execPath,
['--experimental-sea-config', 'sea-config.json'],
{ cwd: tmpdir.path },
);
assert(existsSync(seaPrepBlob));

generateSEA(outputFile, process.execPath, seaPrepBlob);

// Remove the copied addon after it's been packaged into the SEA blob
rmSync(copiedAddonPath, { force: true });

spawnSyncAndAssert(

Check failure on line 62 in test/node-api/test_sea_addon/test.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04-arm)

--- stderr --- [process 206058]: --- stderr --- /home/runner/work/node/node/node/test/.tmp.4529/sea: symbol lookup error: /home/runner/work/node/node/node/test/.tmp.4529/hello.node: undefined symbol: napi_define_properties [process 206058]: --- stdout --- [process 206058]: status = 127, signal = null /home/runner/work/node/node/node/test/common/child_process.js:98 throw error; ^ Error: - process terminated with status 127, expected 0 at Object.<anonymous> (/home/runner/work/node/node/node/test/node-api/test_sea_addon/test.js:62:1) at Module._compile (node:internal/modules/cjs/loader:1737:14) at Object..js (node:internal/modules/cjs/loader:1870:10) at Module.load (node:internal/modules/cjs/loader:1469:32) at Module._load (node:internal/modules/cjs/loader:1289:12) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:238:24) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { options: { env: { JAVA_HOME: '/usr/lib/jvm/temurin-17-jdk-arm64', VCPKG_FORCE_SYSTEM_BINARIES: '1', MEMORY_PRESSURE_WRITE: 'c29tZSAyMDAwMDAgMjAwMDAwMAA=', RUNNER_NAME: 'GitHub Actions 1000278331', GITHUB_REF_NAME: '59582/merge', ACCEPT_EULA: 'Y', DOTNET_SKIP_FIRST_TIME_EXPERIENCE: '1', GITHUB_ENV: '/home/runner/work/_temp/_runner_file_commands/set_env_624a27aa-1b15-46cd-a1f4-090c8f66f89c', GITHUB_REPOSITORY_ID: '27193779', RUNNER_TRACKING_ID: 'github_84d8d893-f071-4288-bc6d-b6f0d4a7864a', SCCACHE_GHA_ENABLED: 'true', NVM_DIR: '/home/runner/.nvm', GITHUB_REPOSITORY_OWNER: 'nodejs', JAVA_HOME_17_X64: '/usr/lib/jvm/temurin-17-jdk-arm64', GITHUB_EVENT_NAME: 'pull_request', SGX_AESM_ADDR: '1', PWD: '/home/runner/work/node/node/node', GITHUB_ACTION_REPOSITORY: '', RUNNER_WORKSPACE: '/home/runner/work/node', GITHUB_WORKFLOW_SHA: 'e95d1abe9d8dbee4268988f0b55042a74198df67', RUNNER_OS: 'Linux', V: '1', CI: 'true', FLAKY_TESTS: 'keep_retrying', LANG: 'C.UTF-8', RUNNER_TEMP: '/home/runner/work/_temp', ENABLE_RUNNER_TRACING: 'true', GITHUB_SERVER_URL: 'https://github.com', GITHUB_API_URL: 'https://api.github.com', MEMORY_PRESSURE_WATCH: '/sys/fs/cgroup/system.slice/hosted-compute-agent.service/memory.pressure', MAKEFLAGS: ' -j1 --no-print-directory -- V=1 TEST_CI_ARGS=-p\\ actions\\ --measure-flakiness\\ 9', PKG_CONFIG_PATH: '/opt/hostedtoolcache/Python/3.12.11/arm64/lib/pkgconfig', Python2_ROOT_DIR: '/opt/hostedtoolcache/Python/3.12.11/arm64', GITHUB_SHA: 'e95d1abe9d8dbee4268988f0b55042a74198df67', GITHUB_ACTOR_ID: '4299420', POWERSHELL_DISTRIBUTION_CHANNEL: 'GitHub-Actions-ubuntu24', SELENIUM_JAR_PATH: '/usr/share/java/selenium-server.jar', pythonLocation: '/opt/hostedtoolcache/Python/3.12.11/arm64', SCCACHE_PATH: '/opt/hostedtoolcache/sccache/0.10.0/arm64/sccache', MFLAGS: '-j1 --no-print-directory', JAVA_HOME_8_X64: '/usr/lib/jvm/temurin-8-jdk-arm64', PIPX_HOME: '/opt/pipx', GITHUB_RUN_ATTEMPT: '4', GITHUB_TRIGGERING_ACTOR: 'joyeecheung', DOTNET_NOLOGO: '1', DOTNET_MULTILEVEL_LOOKUP: '0', ACTIONS_RESULTS_URL: 'https://results-receiver.actions.githubusercontent.com/', JAVA_HOME_11_X64: '/usr/lib/jvm/temurin-11-jdk-arm64', GITHUB_REF: 'refs/pull/59582/merge', GITHUB_RUN_NUMBER: '75328', Python3_ROOT_DIR: '/opt/hostedtoolcache/Python/3.12.11/arm64', GITHUB_JOB: 'test-linux', SHELL: '/bin/sh', LD_LIBRARY_PATH: '/opt/hostedtoolcache/Python/3.12.11/arm64/lib', GITHUB_STATE: '/home/runner/work/_temp/_runner_file_commands/save_state_624a27aa-1b15-46cd-a1f4-090c8f66f89c', SHLVL: '1', ImageVersion: '20250919.34.1', PATH: '/opt/hostedtoolcache/sccache/0.10.0/arm64:/opt/hostedtoolcache/Python/3.12.11/arm64/bin:/opt/hostedtoolcache/Python/3.12.
outputFile,
[],
{
env: {
...process.env,
NODE_DEBUG_NATIVE: 'SEA',
},
cwd: tmpdir.path,
},
{
stdout: /hello, world/,
},
);
Loading