From 7727b8702af8156b27501dd9f3b415dd96901e48 Mon Sep 17 00:00:00 2001 From: ryjiang Date: Fri, 18 Oct 2024 13:52:55 +0800 Subject: [PATCH] fix build Signed-off-by: ryjiang --- server/src/app.ts | 18 ++++++++++++------ server/src/middleware/index.ts | 14 ++++++++------ server/src/socket.ts | 22 +++++++++++++++------- server/src/utils/Helper.ts | 6 +++++- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/server/src/app.ts b/server/src/app.ts index cdc18559..aaeedd44 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -18,7 +18,7 @@ import { ErrorMiddleware, ReqHeaderMiddleware, } from './middleware'; -import { CLIENT_TTL, SimpleQueue } from './utils'; +import { CLIENT_TTL, SimpleQueue, isElectron } from './utils'; import { getIp } from './utils/Network'; import { DescribeIndexRes, MilvusClient } from './types'; import { initWebSocket } from './socket'; @@ -73,7 +73,9 @@ app.use(express.json({ limit: '150MB' })); // TransformResInterceptor app.use(TransformResMiddleware); // LoggingInterceptor -app.use(LoggingMiddleware); +if (!isElectron()) { + app.use(LoggingMiddleware); +} // All headers operations app.use(ReqHeaderMiddleware); // use router @@ -96,8 +98,12 @@ const PORT = process.env.SERVER_PORT || 3000; // start server server.listen(PORT, () => { - const ips = getIp(); - ips.forEach(ip => { - console.info(chalk.cyanBright(`Attu server started: http://${ip}:${PORT}`)); - }); + if (!isElectron()) { + const ips = getIp(); + ips.forEach(ip => { + console.info( + chalk.cyanBright(`Attu server started: http://${ip}:${PORT}`) + ); + }); + } }); diff --git a/server/src/middleware/index.ts b/server/src/middleware/index.ts index 8e5ce8df..918e7383 100644 --- a/server/src/middleware/index.ts +++ b/server/src/middleware/index.ts @@ -1,7 +1,7 @@ import { Request, Response, NextFunction } from 'express'; import morgan from 'morgan'; import chalk from 'chalk'; -import { MILVUS_CLIENT_ID, HTTP_STATUS_CODE } from '../utils'; +import { MILVUS_CLIENT_ID, HTTP_STATUS_CODE, isElectron } from '../utils'; import { HttpError } from 'http-errors'; import HttpErrors from 'http-errors'; import { clientCache } from '../app'; @@ -77,11 +77,13 @@ export const ErrorMiddleware = ( next: NextFunction ) => { const statusCode = err.statusCode || 500; - console.log( - chalk.blue.bold(req.method, req.url), - chalk.magenta.bold(statusCode), - chalk.red.bold(err) - ); + if (!isElectron()) { + console.log( + chalk.blue.bold(req.method, req.url), + chalk.magenta.bold(statusCode), + chalk.red.bold(err) + ); + } // Boolean property that indicates if the app sent HTTP headers for the response. // Here to prevent sending response after header has been sent. if (res.headersSent) { diff --git a/server/src/socket.ts b/server/src/socket.ts index d45bf176..6df1a3a5 100644 --- a/server/src/socket.ts +++ b/server/src/socket.ts @@ -2,7 +2,7 @@ import { Server, Socket } from 'socket.io'; import * as http from 'http'; import chalk from 'chalk'; -import { WS_EVENTS } from './utils'; +import { WS_EVENTS, isElectron } from './utils'; export let io: Server; export let clients = new Map(); @@ -19,23 +19,31 @@ export function initWebSocket(server: http.Server) { // register client socket.on(WS_EVENTS.REGISTER, (clientId: string) => { clients.set(clientId, socket); - console.info(chalk.green(`ws client connected ${clientId}`)); + if (!isElectron()) { + console.info(chalk.green(`ws client connected ${clientId}`)); + } socket.on('disconnect', () => { - console.info(chalk.green(`ws client disconnected ${clientId}`)); + if (!isElectron()) { + console.info(chalk.green(`ws client disconnected ${clientId}`)); + } clients.delete(clientId); }); socket.on('error', (error: Error) => { - console.error( - chalk.red(`ws client error ${clientId}: ${error.message}`) - ); + if (!isElectron()) { + console.error( + chalk.red(`ws client error ${clientId}: ${error.message}`) + ); + } }); }); }); // Handle server-level errors io.on('error', (error: Error) => { - console.error(chalk.red(`ws server error: ${error.message}`)); + if (!isElectron()) { + console.error(chalk.red(`ws server error: ${error.message}`)); + } }); } diff --git a/server/src/utils/Helper.ts b/server/src/utils/Helper.ts index fa85ef31..39120b52 100644 --- a/server/src/utils/Helper.ts +++ b/server/src/utils/Helper.ts @@ -169,4 +169,8 @@ export const getKeyValueListFromJsonString = (json: string): KeyValuePair[] => { } }; -export const cloneObj = (obj: any) => JSON.parse(JSON.stringify(obj)); \ No newline at end of file +export const cloneObj = (obj: any) => JSON.parse(JSON.stringify(obj)); + +export const isElectron = () => { + return process.versions && !!process.versions.electron; +};