Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
try to fix socket api
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Sep 29, 2024
1 parent 323e583 commit 36a933a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/core/mihomoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ async function mihomoHttp<T>(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)
Expand All @@ -48,6 +48,17 @@ async function mihomoHttp<T>(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)
Expand Down

0 comments on commit 36a933a

Please sign in to comment.