Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
proxySocket.on('error', onOutgoingError);
// Allow us to listen when the websocket has completed
proxySocket.on('end', function () {
server.emit('close', proxyRes, proxySocket, proxyHead);
});
// The pipe below will end proxySocket if socket closes cleanly, but not
// if it errors (eg, vanishes from the net and starts returning
// EHOSTUNREACH). We need to do that explicitly.
socket.on('error', function () {
proxySocket.end();
});
common.setupSocket(proxySocket);
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
// Remark: Handle writing the headers to the socket when switching protocols
// Also handles when a header is an array
socket.write(createHttpHeader(
'HTTP/1.1 101 Switching Protocols',
proxyRes.headers
));
let proxyStream = proxySocket;
if (options.createWsServerTransformStream) {
const wsServerTransformStream = options.createWsServerTransformStream(
req,
proxyReq,
const createHttpHeader = function(line, headers) {
return Object.keys(headers).reduce(function (head, key) {
const value = headers[key];
if (!Array.isArray(value)) {
head.push(`${key}: ${value}`);
return head;
}
for (let i = 0; i < value.length; i++) {
head.push(`${key}: ${value[i]}`);
}
return head;
}, [line])
.join(CRLF) + `${CRLF}${CRLF}`;
};
common.setupSocket(socket);
if (head && head.length) socket.unshift(head);
const protocol = common.isSSL.test(options.target.protocol) ? https : http;
const proxyReq = protocol.request(
common.setupOutgoing(options.ssl || {}, options, req)
);
// Enable developers to modify the proxyReq before headers are sent
if (server) {
server.emit('proxyReqWs', proxyReq, req, socket, options, head);
}
// Error Handler
proxyReq.on('error', onOutgoingError);