mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
* fix(cli): keep LAN access for sessions that outlive their runner on macOS macOS attributes Local Network decisions to a process's responsible process, which a child inherits from its spawner. Sessions spawned by a runner that launchd started (directly, or through the npm launcher bin/hapi.cjs) inherit the runner's or the launcher's pid. Once that process exits (upgrade, version handoff) while the sessions are kept, every new process in their trees that connects to a LAN address fails with EHOSTUNREACH. When the spawner may hand its responsibility on, detached spawnHappyCLI children (sessions, runner handoff, runner start) are started with HAPI_DISCLAIM_RESPONSIBILITY=<spawner pid>. In a direct child of that pid, the entry hook removes the variable from the libc environment and re-executes process.execPath in place via posix_spawn(POSIX_SPAWN_SETEXEC) with responsibility_spawnattrs_setdisclaim, so the child becomes its own responsible process. The pid, stdio, process group, cwd and the rest of the environment are unchanged, so the runner's ChildProcess tracking keeps working. The spawner may hand its responsibility on when it is its own responsible process, or when its responsible process is its direct parent, is the npm launcher (which now passes HAPI_LAUNCHER_PID) and is itself its own responsible process. A spawner started from Terminal or over SSH keeps the inherited responsibility and with it the implicit LAN allowance macOS grants there. The lookup uses responsibility_get_pid_responsible_for_pid through bun:ffi; when it is unavailable the spawn is not disclaimed and this is logged once. Binaries without the hook ignore the variable, and a copy that leaks into a rolled-back tree is ignored because its parent is not the spawner. The private disclaim symbol may disappear: the hook then execs without it, and any other failure continues in-process; the reason is written to the hapi log. * fix(cli): print --help and --version when HAPI_HOME cannot be created The entry point imported runtime/responsibility to read the npm launcher's pid. That module imports the logger, whose configuration creates HAPI_HOME, so `hapi --help` and `hapi --version` failed before printing when that directory could not be created. Move the launcher pid into a dependency-free module, read it from there, and make the optional disclaim fallback note best effort. Add an entry point test that runs both flags with an unwritable HAPI_HOME.
196 lines
5.7 KiB
JavaScript
Executable File
196 lines
5.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const { execFileSync } = require('child_process');
|
|
const path = require('path');
|
|
const { version: PACKAGE_VERSION } = require('../package.json');
|
|
|
|
const platform = process.platform;
|
|
const arch = process.arch;
|
|
const RELEASE_URL = 'https://github.com/tiann/hapi/releases';
|
|
const OFFICIAL_NPM_REGISTRY = 'https://registry.npmjs.org';
|
|
const SUPPORTED_PLATFORMS = [
|
|
{
|
|
key: 'darwin-arm64',
|
|
label: 'darwin-arm64 (macOS Apple Silicon)',
|
|
},
|
|
{
|
|
key: 'darwin-x64',
|
|
label: 'darwin-x64 (macOS Intel)',
|
|
},
|
|
{
|
|
key: 'linux-arm64',
|
|
label: 'linux-arm64',
|
|
},
|
|
{
|
|
key: 'linux-x64',
|
|
label: 'linux-x64',
|
|
},
|
|
{
|
|
key: 'win32-x64',
|
|
label: 'win32-x64',
|
|
},
|
|
];
|
|
|
|
function getPlatformKey(platformName = platform, archName = arch) {
|
|
return `${platformName}-${archName}`;
|
|
}
|
|
|
|
function isSupportedPlatform(platformName = platform, archName = arch) {
|
|
return SUPPORTED_PLATFORMS.some((item) => item.key === getPlatformKey(platformName, archName));
|
|
}
|
|
|
|
function getBinaryPath(platformName = platform, archName = arch) {
|
|
const pkgName = `@twsxtd/hapi-${platformName}-${archName}`;
|
|
let pkgPath;
|
|
|
|
try {
|
|
// Try to find the platform-specific package
|
|
pkgPath = require.resolve(`${pkgName}/package.json`);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
|
|
// A failed optional dependency upgrade can leave an older package on disk.
|
|
// Node resolves that package even though it does not satisfy our pinned version.
|
|
const installedVersion = require(pkgPath).version;
|
|
if (installedVersion !== PACKAGE_VERSION) {
|
|
throw new Error(
|
|
`Platform package version mismatch: ${pkgName}\n` +
|
|
`Expected ${PACKAGE_VERSION}, but found ${installedVersion ?? 'unknown'}.\n` +
|
|
'Refusing to run a platform binary from a different HAPI release.'
|
|
);
|
|
}
|
|
|
|
const binName = platformName === 'win32' ? 'hapi.exe' : 'hapi';
|
|
return path.join(path.dirname(pkgPath), 'bin', binName);
|
|
}
|
|
|
|
function formatCommand(binPath, args) {
|
|
return [binPath, ...args].map((arg) => JSON.stringify(String(arg))).join(' ');
|
|
}
|
|
|
|
function normalizeExecError(error) {
|
|
return {
|
|
status: typeof error?.status === 'number' ? error.status : null,
|
|
signal: typeof error?.signal === 'string' ? error.signal : null,
|
|
message: error?.message ? String(error.message) : null,
|
|
};
|
|
}
|
|
|
|
function reportExecutionFailure(error, binPath, args, log = console.error) {
|
|
const { status, signal, message } = normalizeExecError(error);
|
|
|
|
log(`Failed to execute: ${formatCommand(binPath, args)}`);
|
|
|
|
if (signal) {
|
|
log(`Binary terminated by signal ${signal}.`);
|
|
}
|
|
|
|
if (status !== null) {
|
|
log(`Binary exited with status ${status}.`);
|
|
}
|
|
|
|
if (message) {
|
|
log(message);
|
|
}
|
|
|
|
return { status, signal };
|
|
}
|
|
|
|
function reportUnsupportedPlatform(platformName = platform, archName = arch, log = console.error) {
|
|
log(`Unsupported platform: ${platformName}-${archName}`);
|
|
log('');
|
|
log('Supported platforms:');
|
|
for (const item of SUPPORTED_PLATFORMS) {
|
|
log(` - ${item.label}`);
|
|
}
|
|
log('');
|
|
log('You can download the binary manually from:');
|
|
log(` ${RELEASE_URL}`);
|
|
}
|
|
|
|
function reportReinstall(log = console.error) {
|
|
log('Try reinstalling with the official npm registry:');
|
|
log('Use the same package manager as your current installation:');
|
|
log(` npm install -g @twsxtd/hapi@${PACKAGE_VERSION} --registry=${OFFICIAL_NPM_REGISTRY}`);
|
|
log('For Bun, remove the launcher first to retry skipped optional dependencies:');
|
|
log(` bun remove -g @twsxtd/hapi && bun add -g @twsxtd/hapi@${PACKAGE_VERSION} --registry=${OFFICIAL_NPM_REGISTRY}`);
|
|
log('');
|
|
log('Or download the binary manually from:');
|
|
log(` ${RELEASE_URL}`);
|
|
}
|
|
|
|
function reportMissingPlatformPackage(platformName = platform, archName = arch, log = console.error) {
|
|
const platformPackage = `@twsxtd/hapi-${platformName}-${archName}`;
|
|
log(`Missing platform package: ${platformPackage}`);
|
|
log('');
|
|
log(`Detected platform ${platformName}-${archName} is supported, but the platform binary package was not installed.`);
|
|
log('This may happen when using a registry mirror that has not synced all optionalDependencies.');
|
|
log('');
|
|
reportReinstall(log);
|
|
}
|
|
|
|
function main() {
|
|
if (!isSupportedPlatform()) {
|
|
reportUnsupportedPlatform();
|
|
process.exit(1);
|
|
}
|
|
|
|
let binPath;
|
|
try {
|
|
binPath = getBinaryPath();
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
console.error('');
|
|
reportReinstall();
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!binPath) {
|
|
reportMissingPlatformPackage();
|
|
process.exit(1);
|
|
}
|
|
|
|
const args = process.argv.slice(2);
|
|
|
|
try {
|
|
// The binary may hand this launcher's macOS responsibility on to the
|
|
// processes it detaches (see cli/src/runtime/responsibility.ts).
|
|
execFileSync(binPath, args, {
|
|
stdio: 'inherit',
|
|
env: { ...process.env, HAPI_LAUNCHER_PID: String(process.pid) },
|
|
});
|
|
} catch (error) {
|
|
const { status, signal } = reportExecutionFailure(error, binPath, args);
|
|
|
|
if (status !== null) {
|
|
process.exit(status);
|
|
}
|
|
|
|
if (signal) {
|
|
try {
|
|
process.kill(process.pid, signal);
|
|
} catch {
|
|
// ignore unsupported/invalid signal names on this platform
|
|
}
|
|
}
|
|
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
module.exports = {
|
|
formatCommand,
|
|
getPlatformKey,
|
|
getBinaryPath,
|
|
isSupportedPlatform,
|
|
normalizeExecError,
|
|
reportExecutionFailure,
|
|
reportMissingPlatformPackage,
|
|
reportUnsupportedPlatform,
|
|
};
|