在注入的主脚本中使用本地插件


🌐 Using native addons in the injected main script

本地插件可以通过在用于生成单文件可执行应用准备块的配置文件的 assets 字段中指定它们,作为资源打包到单文件可执行应用中。然后,可以在注入的主脚本中通过将资源写入临时文件并使用 process.dlopen() 加载它来加载该插件。

🌐 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().

{
  "main": "/path/to/bundled/script.js",
  "output": "/path/to/write/the/generated/blob.blob",
  "assets": {
    "myaddon.node": "/path/to/myaddon/build/Release/myaddon.node"
  }
} 
// 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); 

已知警告:如果单文件可执行应用是由在 Linux arm64 Docker 容器中运行的 postject 生成的,生成的 ELF 二进制文件没有用于加载插件的正确哈希表 和将在 process.dlopen() 上崩溃。请在其他平台上构建单文件可执行应用,或至少在非容器的 Linux arm64 环境中构建,以规避此问题。

🌐 Known caveat: if the single-executable application is produced by postject running on a Linux arm64 docker container, the produced ELF binary does not have the correct hash table to load the addons and will crash on process.dlopen(). Build the single-executable application on other platforms, or at least on a non-container Linux arm64 environment to work around this issue.