From 36a933a672d24eb40829b1cf6a20c92503d37c9a Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sun, 29 Sep 2024 19:49:56 +0800 Subject: [PATCH] try to fix socket api --- src/main/core/mihomoApi.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 14634703..8b5bf5d0 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -34,11 +34,11 @@ async function mihomoHttp(method: HttpMethod, path: string, data?: object): P const client = net.connect( process.platform === 'win32' ? mihomoPipe : join(mihomoWorkDir(), mihomoUnix) ) - client.on('data', function (res) { + const parseResult = (str: string): void => { try { - const data = res.toString().split('\r\n\r\n')[1] + const data = str.split('\r\n\r\n')[1] const json = trimJson(data) - if (res.toString().includes('HTTP/1.1 4') || res.toString().includes('HTTP/1.1 5')) { + if (str.includes('HTTP/1.1 4') || str.includes('HTTP/1.1 5')) { reject(json ? JSON.parse(json) : data) } else { resolve(json ? JSON.parse(json) : undefined) @@ -48,6 +48,17 @@ async function mihomoHttp(method: HttpMethod, path: string, data?: object): P } finally { client.end() } + } + let buffer = '' + client.on('data', function (res) { + if (res.toString().includes('Transfer-Encoding: chunked') || buffer !== '') { + buffer += res.toString() + if (buffer.endsWith('\r\n\r\n')) { + parseResult(buffer) + } + } else { + parseResult(res.toString()) + } }) client.on('error', function (error) { reject(error)