59 lines
2.3 KiB
JavaScript
59 lines
2.3 KiB
JavaScript
|
"use strict";
|
||
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
||
|
}
|
||
|
Object.defineProperty(o, k2, desc);
|
||
|
}) : (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
o[k2] = m[k];
|
||
|
}));
|
||
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||
|
}) : function(o, v) {
|
||
|
o["default"] = v;
|
||
|
});
|
||
|
var __importStar = (this && this.__importStar) || function (mod) {
|
||
|
if (mod && mod.__esModule) return mod;
|
||
|
var result = {};
|
||
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||
|
__setModuleDefault(result, mod);
|
||
|
return result;
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.respondOnWebSocket = void 0;
|
||
|
const headerTransforms = __importStar(require("./header-transforms"));
|
||
|
function writeWebSocketHead(socket, destRes, headers) {
|
||
|
const { httpVersion, statusCode, statusMessage } = destRes;
|
||
|
const resRaw = [`HTTP/${httpVersion} ${statusCode} ${statusMessage}`];
|
||
|
const headersNames = Object.keys(headers);
|
||
|
for (const headerName of headersNames) {
|
||
|
const headerValue = headers[headerName];
|
||
|
if (Array.isArray(headerValue)) {
|
||
|
for (const value of headerValue)
|
||
|
resRaw.push(headerName + ': ' + value);
|
||
|
}
|
||
|
else
|
||
|
resRaw.push(headerName + ': ' + headerValue);
|
||
|
}
|
||
|
resRaw.push('', '');
|
||
|
socket.write(resRaw.join('\r\n'));
|
||
|
}
|
||
|
function respondOnWebSocket(ctx) {
|
||
|
const headers = headerTransforms.forResponse(ctx);
|
||
|
const destRes = ctx.destRes;
|
||
|
const res = ctx.res;
|
||
|
writeWebSocketHead(res, destRes, headers);
|
||
|
const unpipe = () => {
|
||
|
res.unpipe(destRes.socket);
|
||
|
destRes.socket.unpipe(res);
|
||
|
};
|
||
|
destRes.socket.on('end', unpipe);
|
||
|
res.on('end', unpipe);
|
||
|
destRes.socket.pipe(res);
|
||
|
res.pipe(destRes.socket);
|
||
|
}
|
||
|
exports.respondOnWebSocket = respondOnWebSocket;
|