@duongtrungnguyen/nestro
Version:
Service registry for Nest JS
163 lines • 6.17 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var utils_exports = {};
__export(utils_exports, {
hasEncryptedConnection: () => hasEncryptedConnection,
isSocketIORequest: () => isSocketIORequest,
isSocketRequest: () => isSocketRequest,
rewriteCookieProperty: () => rewriteCookieProperty,
setupOutgoing: () => setupOutgoing,
setupSocket: () => setupSocket,
urlJoin: () => urlJoin
});
module.exports = __toCommonJS(utils_exports);
var import_url = require("url");
var import_constants = require("./constants");
function setupOutgoing(outgoing = {}, options, req, forward) {
const targetKey = forward || "target";
const target = options[targetKey];
if (!target) {
return outgoing;
}
outgoing.port = getTargetPort(target);
copyTargetProperties(outgoing, target);
outgoing.method = req.method || "GET";
outgoing.headers = req.headers;
if (import_constants.SSL_PROTOCOL_REGEX.test(target.protocol || "")) {
outgoing.rejectUnauthorized = options.secure !== void 0 ? options.secure : true;
}
outgoing.localAddress = options.localAddress;
configureConnectionHeader(outgoing, req);
const originalPath = req.url || "/";
const rewrittenPath = rewritePath(originalPath, req, options);
outgoing.path = buildPath(target, rewrittenPath, options);
if (options.changeOrigin) {
const hostHeader = getHostHeader(outgoing);
if (hostHeader) {
outgoing.headers.host = hostHeader;
}
}
return outgoing;
}
function getTargetPort(target) {
if (target.port) {
return target.port;
}
if (target.port) {
return parseInt(target.port, 10);
}
return import_constants.SSL_PROTOCOL_REGEX.test(target.protocol || "") ? 443 : 80;
}
function copyTargetProperties(outgoing, target) {
const props = ["host", "hostname", "socketPath", "pfx", "key", "passphrase", "cert", "ca", "ciphers", "secureProtocol"];
for (const prop of props) {
if (target[prop] !== void 0) {
outgoing[prop] = target[prop];
}
}
}
function configureConnectionHeader(outgoing, req) {
const isWebSocket = req.headers.upgrade && import_constants.WEBSOCKET_UPGRADE_REGEX.test(req.headers.upgrade.toLowerCase()) && req.headers.connection && import_constants.UPGRADE_HEADER_REGEX.test(req.headers.connection.toLowerCase());
outgoing.headers = outgoing.headers || {};
if (isWebSocket) {
if (!outgoing.headers.connection) {
outgoing.headers.connection = "upgrade";
}
} else if (!outgoing.agent && !outgoing.headers.connection) {
outgoing.headers.connection = "close";
}
}
function buildPath(target, rewrittenPath, options) {
let targetPath = "";
if (options.prependPath !== false) {
targetPath = target.path || target.pathname || "";
}
const url = new import_url.URL(rewrittenPath, "http://localhost");
const pathname = options.toProxy ? rewrittenPath : url.pathname;
const queryString = options.toProxy ? "" : url.search;
const fullPath = joinPaths(targetPath, pathname);
return fullPath + queryString;
}
function rewritePath(path, req, options) {
if (options.ignorePath) return "";
if (!options.pathRewrite) return path;
for (const [pattern, replacement] of Object.entries(options.pathRewrite)) {
const regex = new RegExp(pattern);
if (regex.test(path)) {
return typeof replacement === "function" ? replacement(path, req) : path.replace(regex, replacement);
}
}
return path;
}
function getHostHeader(outgoing) {
if (outgoing.port !== 80 && outgoing.port !== 443) {
return `${outgoing.hostname || outgoing.host}:${outgoing.port}`;
}
return outgoing.hostname || outgoing.host;
}
function joinPaths(...paths) {
if (!paths.length) return "";
const filteredPaths = paths.filter(Boolean);
if (!filteredPaths.length) return "";
return filteredPaths.join("/").replace(/\/+/g, "/").replace(/http:\/|https:\//g, (match) => match.replace(":/", "://"));
}
function setupSocket(socket) {
socket.setTimeout(0);
socket.setNoDelay(true);
socket.setKeepAlive(true, 0);
return socket;
}
function hasEncryptedConnection(req) {
const socket = req.socket;
return Boolean(socket.encrypted || socket.pair);
}
function urlJoin(...args) {
if (!args.length) return "";
const last = args[args.length - 1];
const [path, ...queryParts] = last.split("?");
const paths = [...args.slice(0, -1), path].filter(Boolean);
const joinedPath = joinPaths(...paths);
return queryParts.length > 0 ? `${joinedPath}?${queryParts.join("?")}` : joinedPath;
}
function rewriteCookieProperty(header, config, property) {
if (Array.isArray(header)) {
return header.map((h) => rewriteCookieProperty(h, config, property));
}
const pattern = new RegExp(`(;\\s*${property}=)([^;]+)`, "i");
return header.replace(pattern, (match, prefix, previousValue) => {
const newValue = previousValue in config ? config[previousValue] : config["*"];
return newValue ? `${prefix}${newValue}` : "";
});
}
function isSocketIORequest(req) {
return !!req.url && import_constants.SOCKET_IO_PATH_REGEX.test(req.url);
}
function isSocketRequest(req) {
return !!req.headers.upgrade && req.headers.upgrade.toLowerCase() === import_constants.WEBSOCKET_UPGRADE_HEADER;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
hasEncryptedConnection,
isSocketIORequest,
isSocketRequest,
rewriteCookieProperty,
setupOutgoing,
setupSocket,
urlJoin
});
//# sourceMappingURL=utils.js.map