@kevisual/router 0.0.26-alpha.1 → 0.0.26-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/auto/call-sock.ts +22 -9
package/package.json
CHANGED
package/src/auto/call-sock.ts
CHANGED
|
@@ -14,7 +14,7 @@ type CallSockOptions = {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export const callSock = async (data: QueryData, options: CallSockOptions = {}): Promise<any> => {
|
|
17
|
-
const { socketPath = './app.sock', timeout =
|
|
17
|
+
const { socketPath = './app.sock', timeout = 10000, method = 'POST' } = options;
|
|
18
18
|
|
|
19
19
|
return new Promise((resolve, reject) => {
|
|
20
20
|
const client = createConnection(socketPath);
|
|
@@ -74,6 +74,26 @@ export const callSock = async (data: QueryData, options: CallSockOptions = {}):
|
|
|
74
74
|
|
|
75
75
|
client.on('data', (chunk) => {
|
|
76
76
|
responseData += chunk.toString();
|
|
77
|
+
|
|
78
|
+
// 检查是否收到完整的HTTP响应
|
|
79
|
+
if (responseData.includes('\r\n\r\n')) {
|
|
80
|
+
const [headerSection] = responseData.split('\r\n\r\n');
|
|
81
|
+
const contentLengthMatch = headerSection.match(/content-length:\s*(\d+)/i);
|
|
82
|
+
|
|
83
|
+
if (contentLengthMatch) {
|
|
84
|
+
const expectedLength = parseInt(contentLengthMatch[1]);
|
|
85
|
+
const bodyStart = responseData.indexOf('\r\n\r\n') + 4;
|
|
86
|
+
const currentBodyLength = Buffer.byteLength(responseData.slice(bodyStart), 'utf8');
|
|
87
|
+
|
|
88
|
+
// 如果收到了完整的响应,主动关闭连接
|
|
89
|
+
if (currentBodyLength >= expectedLength) {
|
|
90
|
+
client.end();
|
|
91
|
+
}
|
|
92
|
+
} else if (responseData.includes('\r\n0\r\n\r\n')) {
|
|
93
|
+
// 检查 chunked 编码结束标记
|
|
94
|
+
client.end();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
77
97
|
});
|
|
78
98
|
|
|
79
99
|
client.on('end', () => {
|
|
@@ -139,13 +159,6 @@ function parseHttpResponse(responseData: string) {
|
|
|
139
159
|
};
|
|
140
160
|
}
|
|
141
161
|
|
|
142
|
-
|
|
143
|
-
export const callSockGet = (data: QueryData, options?: Omit<CallSockOptions, 'method'>) => {
|
|
144
|
-
return callSock(data, { ...options, method: 'GET' });
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
export const callSockPost = (data: QueryData, options?: Omit<CallSockOptions, 'method'>) => {
|
|
162
|
+
export const autoCall = (data: QueryData, options?: Omit<CallSockOptions, 'method'>) => {
|
|
148
163
|
return callSock(data, { ...options, method: 'POST' });
|
|
149
164
|
};
|
|
150
|
-
|
|
151
|
-
export const autoCall = callSockPost;
|