tezx
Version:
TezX is a high-performance, lightweight JavaScript framework designed for speed, scalability, and flexibility. It enables efficient routing, middleware management, and static file serving with minimal configuration. Fully compatible with Node.js, Deno, an
36 lines (35 loc) • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toWebRequest = toWebRequest;
const node_stream_1 = require("node:stream");
function toWebRequest(req, method = "GET") {
const headers = {};
for (const [key, value] of Object.entries(req.headers)) {
if (Array.isArray(value)) {
headers[key] = value.join(", ");
}
else if (typeof value === "string") {
headers[key] = value;
}
}
const isEncrypted = (req.socket && req.socket.encrypted) || false;
const protocol = isEncrypted ? "https:" : "http:";
let host = "localhost";
const hostHeader = req.headers.host;
if (typeof hostHeader === "string") {
host = hostHeader;
}
const urlStr = req.url ?? "/";
const fullUrl = new URL(urlStr, `${protocol}//${host}`);
const hasBody = !["GET", "HEAD"].includes(method.toUpperCase());
const body = hasBody ? node_stream_1.Readable.toWeb(req) : undefined;
const abortController = new AbortController();
req?.once("close", () => abortController.abort());
return new Request(fullUrl.href, {
method,
headers,
body,
signal: abortController.signal,
duplex: hasBody ? "half" : undefined,
});
}