Files
holesail-browser/native-host/index.mjs
T
Raven Scott d58a0b6e2d
CI / Build & Test (push) Has been cancelled
first commit
2026-02-27 18:13:59 -05:00

72 lines
1.7 KiB
JavaScript

/**
* Holesail Browser native messaging host entry point.
*
* bare-process/global must be the very first import so that `process` is
* available as a global before any other module runs.
*
* All imports are static so bare-pack can pre-resolve the full module graph.
*/
import 'bare-process/global';
import _messenger from './messenger.js';
import _host from './host.js';
const { createMessenger } = _messenger;
const { handleMessage, cleanup } = _host;
function logErr(msg) {
try {
process.stderr.write(`[holesail-browser-host] ${msg}\n`);
} catch (_) {}
}
const input = process.stdin;
const output = process.stdout;
// On Windows, native messaging requires binary I/O (no CRLF translation).
if (input.setRawMode) {
input.setEncoding?.('binary');
}
if (output.setDefaultEncoding) {
output.setDefaultEncoding?.('binary');
}
const messenger = createMessenger({
input,
output,
onMessage(msg) {
handleMessage(
(response) => messenger.send(response),
msg
).catch((err) => {
logErr(err.stack || err.message);
const id = msg && msg.id;
if (id) {
messenger.send({ id, type: 'response', payload: { ok: false, error: err.message } });
}
});
},
onError(err) {
logErr(err.message);
},
});
function shutdown() {
cleanup();
messenger.destroy();
process.exit(0);
}
process.on('exit', () => cleanup());
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
process.on('unhandledRejection', (reason) => {
logErr('unhandledRejection: ' + (reason && (reason.stack || reason.message || reason)));
});
process.on('uncaughtException', (err) => {
logErr('uncaughtException: ' + (err && (err.stack || err.message)));
});
logErr('ready');