import * as plugins from './plugins.js'; import { FlexFramedTransport } from './classes.flexframedtransport.js'; import { FlexService } from './classes.flexservice.js'; import { createFlexChildShutdown } from './functions.flexchildshutdown.js'; import { type TFlexChildMessage, type TFlexParentMessage, flexIpcDisposeTimeoutMs, parseFlexChildMessage, parseFlexParentMessage, } from './interfaces.flexipc.js'; const readable = plugins.fs.createReadStream('', { fd: 3, autoClose: true }); const writable = plugins.fs.createWriteStream('', { fd: 4, autoClose: true }); const transport = new FlexFramedTransport({ readable, writable, parseIncoming: parseFlexParentMessage, validateOutgoing: parseFlexChildMessage, }); let service!: FlexService; const closeDescriptors = (): void => { readable.destroy(); writable.destroy(); }; const shutdown = createFlexChildShutdown({ dispose: async () => service.dispose(), closeDescriptors, getExitCode: () => typeof process.exitCode === 'number' ? process.exitCode : undefined, setExitCode: (exitCodeArg) => { process.exitCode = exitCodeArg; }, forceExit: (exitCodeArg) => process.exit(exitCodeArg), timeoutMs: flexIpcDisposeTimeoutMs, }); service = new FlexService({ transport, onFatalError: () => shutdown(true), }); process.once('SIGTERM', () => shutdown(false)); process.once('SIGINT', () => shutdown(false)); process.once('uncaughtException', () => shutdown(true)); process.once('unhandledRejection', () => shutdown(true)); try { await service.start(); } catch { shutdown(true); }