properly handle errors

This commit is contained in:
Simon Knott 2024-12-16 12:28:18 +01:00
parent 2e019eb746
commit 41aeb0ee56
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -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);