This commit is contained in:
seb
2026-10-26 23:09:23 +01:00
parent 2dea4e86f1
commit 2cc166bbec
4 changed files with 111 additions and 37 deletions

View File

@@ -1,15 +1,64 @@
/**
* SEA Entry point for dashback.
* Injected into the Node.js single executable binary via node --build-sea.
* Embedded inside the Node.js single executable binary.
*
* When launched as a SEA binary, it resolves the project root,
* configures module resolution, and loads src/index.js.
* 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.
*/
const { createRequire } = require('node:module');
const path = require('node:path');
const sea = require('node:sea');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const zlib = require('node:zlib');
const { createRequire } = require('node:module');
function resolveProjectRoot() {
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 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);
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
fs.writeFileSync(targetFile, Buffer.from(b64, 'base64'));
}
// Link host node_modules so dependencies & native addons resolve
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
}
}
// Link host .env if present so config/env.js can find it
const hostEnv = path.resolve(process.cwd(), '.env');
const runtimeEnv = path.join(runtimeDir, '.env');
if (fs.existsSync(hostEnv) && !fs.existsSync(runtimeEnv)) {
try {
fs.symlinkSync(hostEnv, runtimeEnv, 'file');
} catch {
try {
fs.copyFileSync(hostEnv, runtimeEnv);
} catch {}
}
}
return runtimeDir;
}
// Fallback if not running as packed SEA (local development)
if (process.env.DASHBACK_ROOT && fs.existsSync(path.join(process.env.DASHBACK_ROOT, 'src/index.js'))) {
return path.resolve(process.env.DASHBACK_ROOT);
}
@@ -21,19 +70,14 @@ function resolveProjectRoot() {
if (fs.existsSync(parentEntry)) {
return path.resolve(__dirname, '..');
}
const dirEntry = path.join(__dirname, 'src/index.js');
if (fs.existsSync(dirEntry)) {
return __dirname;
}
return process.cwd();
}
const projectRoot = resolveProjectRoot();
const entryFile = path.join(projectRoot, 'src/index.js');
const projectRoot = getProjectRoot();
const entryFile = path.join(projectRoot, 'src', 'index.js');
if (!fs.existsSync(entryFile)) {
console.error(`[SEA] Could not find dashback entry point at ${entryFile}.`);
console.error(`[SEA] Please ensure you are running from the dashback repository root or set DASHBACK_ROOT.`);
process.exit(1);
}