From 0c21b2971288cdfa9cbc30373f60b63b431f01ae Mon Sep 17 00:00:00 2001 From: InNoobWeTrust Date: Tue, 27 Feb 2024 18:21:08 +0700 Subject: [PATCH] feat(nitro-node): add default event handlers for nitro process --- nitro-node/src/nitro.ts | 25 ++++++++++++++++++++++++- nitro-node/test/nitro-process.test.ts | 4 ++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/nitro-node/src/nitro.ts b/nitro-node/src/nitro.ts index f31fbe284..97a7d5339 100644 --- a/nitro-node/src/nitro.ts +++ b/nitro-node/src/nitro.ts @@ -1,5 +1,6 @@ import fs from "node:fs"; import path from "node:path"; +import net from "node:net"; import stream from "node:stream"; import { ChildProcessWithoutNullStreams, spawn } from "node:child_process"; import tcpPortUsed from "tcp-port-used"; @@ -54,7 +55,29 @@ const getNitroProcessInfo = (subprocess: any): NitroProcessInfo => ({ }); const getCurrentNitroProcessInfo = () => getNitroProcessInfo(subprocess); // Default event handler: do nothing -let processEventHandler: NitroProcessEventHandler = {}; +let processEventHandler: NitroProcessEventHandler = { + close: (code: number, signal: string) => { + log(`[NITRO]::Debug: Nitro process closed with code ${code} and signal ${signal}`); + }, + disconnect: () => { + log("[NITRO]::Debug: Nitro process disconnected"); + }, + error: (e: Error) => { + log(`[NITRO]::Error: Nitro process error: ${JSON.stringify(e)}`); + }, + exit: (code: number, signal: string) => { + log(`[NITRO]::Debug: Nitro process exited with code ${code} and signal ${signal}`); + }, + message: ( + message: object, + sendHandle: net.Socket | net.Server | undefined, + ) => { + log(`[NITRO]::Debug: Nitro process message: ${JSON.stringify(message)}`); + }, + spawn: () => { + log("[NITRO]::Debug: Nitro process spawned"); + }, +}; // Default stdio handler: log stdout and stderr let processStdioHandler: NitroProcessStdioHanler = { stdout: (stdout: stream.Readable | null | undefined) => { diff --git a/nitro-node/test/nitro-process.test.ts b/nitro-node/test/nitro-process.test.ts index 511009049..ef4b50d7b 100644 --- a/nitro-node/test/nitro-process.test.ts +++ b/nitro-node/test/nitro-process.test.ts @@ -237,8 +237,8 @@ describe("Manage nitro process", () => { // Stop nitro await stopModel(); }, - // Set timeout to 1 minutes - 1 * 60 * 1000, + // Set timeout to 5 minutes + 5 * 60 * 1000, ); describe("search model file by magic number", () => { // Rename model file before test