This commit is contained in:
seb
2026-10-27 00:24:55 +01:00
parent b6f68f04a4
commit 41c532e2ca
4 changed files with 95 additions and 41 deletions

View File

@@ -2,9 +2,8 @@
* SEA Entry point for dashback.
* Embedded inside the Node.js single executable binary.
*
* When built with embedded assets, it unpacks the bundled src/ and package.json
* to a runtime directory and links the host node_modules, so native C++ addons
* and dependencies resolve seamlessly without needing src/ in the filesystem.
* Unpacks the bundled application, sibling packages, and JavaScript dependencies
* to a runtime directory, and links only the native C++ addons from the host.
*/
const sea = require('node:sea');
const fs = require('node:fs');
@@ -17,28 +16,36 @@ function getProjectRoot() {
// If embedded application payload is present in SEA
if (sea.isSea && sea.isSea() && sea.getAssetKeys && sea.getAssetKeys().includes('app-payload.bin')) {
const runtimeDir = process.env.DASHBACK_RUNTIME_DIR || path.join(os.tmpdir(), 'dashback-runtime');
const markerFile = path.join(runtimeDir, '.unpacked');
const compressed = Buffer.from(sea.getRawAsset('app-payload.bin'));
const json = zlib.inflateSync(compressed).toString('utf8');
const files = JSON.parse(json);
if (!fs.existsSync(markerFile)) {
const compressed = Buffer.from(sea.getRawAsset('app-payload.bin'));
const json = zlib.inflateSync(compressed).toString('utf8');
const files = JSON.parse(json);
for (const [relPath, b64] of Object.entries(files)) {
const targetFile = relPath === 'package.json'
? path.join(runtimeDir, 'package.json')
: path.join(runtimeDir, 'src', relPath);
for (const [relPath, b64] of Object.entries(files)) {
const targetFile = path.join(runtimeDir, relPath);
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
fs.writeFileSync(targetFile, Buffer.from(b64, 'base64'));
}
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
fs.writeFileSync(targetFile, Buffer.from(b64, 'base64'));
fs.writeFileSync(markerFile, '1');
}
// Link host node_modules so dependencies & native addons resolve
// Link host native addons into runtime node_modules
const hostNodeModules = path.resolve(process.cwd(), 'node_modules');
const runtimeNodeModules = path.join(runtimeDir, 'node_modules');
if (fs.existsSync(hostNodeModules) && !fs.existsSync(runtimeNodeModules)) {
try {
fs.symlinkSync(hostNodeModules, runtimeNodeModules, 'junction');
} catch {
// Ignore symlink failure if already exists
fs.mkdirSync(runtimeNodeModules, { recursive: true });
const NATIVE_ADDON_DIRS = ['better-sqlite3', 'canvas', 'sharp', '@img'];
for (const addon of NATIVE_ADDON_DIRS) {
const hostAddon = path.join(hostNodeModules, addon);
const targetAddon = path.join(runtimeNodeModules, addon);
if (fs.existsSync(hostAddon) && !fs.existsSync(targetAddon)) {
try {
fs.mkdirSync(path.dirname(targetAddon), { recursive: true });
fs.symlinkSync(hostAddon, targetAddon, 'junction');
} catch {}
}
}