properly handle errors
This commit is contained in:
parent
2e019eb746
commit
41aeb0ee56
11
tests/third_party/proxy/index.ts
vendored
11
tests/third_party/proxy/index.ts
vendored
|
|
@ -3,6 +3,7 @@ import * as net from 'net';
|
|||
import * as url from 'url';
|
||||
import * as http from 'http';
|
||||
import * as os from 'os';
|
||||
import { pipeline } from 'stream/promises';
|
||||
|
||||
const pkg = { version: '1.0.0' }
|
||||
|
||||
|
|
@ -475,13 +476,19 @@ function onupgrade(req: http.IncomingMessage, socket: net.Socket, head: Buffer)
|
|||
localAddress: this.localAddress,
|
||||
});
|
||||
|
||||
proxyReq.on('upgrade', function (proxyRes, proxySocket, proxyHead) {
|
||||
proxyReq.on('upgrade', async function (proxyRes, proxySocket, proxyHead) {
|
||||
const header = ['HTTP/1.1 101 Switching Protocols'];
|
||||
for (const [key, value] of Object.entries(proxyRes.headersDistinct))
|
||||
header.push(`${key}: ${value}`);
|
||||
socket.write(header.join('\r\n') + '\r\n\r\n');
|
||||
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
|
||||
proxySocket.pipe(socket).pipe(proxySocket);
|
||||
|
||||
try {
|
||||
await pipeline(proxySocket, socket, proxySocket);
|
||||
} catch (error) {
|
||||
if (error.code !== "ECONNRESET")
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
proxyReq.end(head);
|
||||
|
|
|
|||
Loading…
Reference in a new issue