rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
80 lines (79 loc) • 3.4 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var writeHTTPMeta_exports = {};
__export(writeHTTPMeta_exports, {
default: () => writeHTTPMeta
});
module.exports = __toCommonJS(writeHTTPMeta_exports);
var import_parseStatus = __toESM(require("./parseStatus"));
var import_parseContent = __toESM(require("./parseContent"));
async function writeHTTPMeta(res, ctx) {
const readyHeaders = {};
const readyCookies = {};
for (const header in ctx.response.headers) {
if (!ctx.response.headers[header])
continue;
const parsed = await (0, import_parseContent.default)(ctx.response.headers[header]);
readyHeaders[header] = parsed.content;
}
for (const cookie in ctx.response.cookies) {
if (!ctx.response.cookies[cookie] || !ctx.response.cookies[cookie].value)
continue;
const infos = ctx.response.cookies[cookie];
const parsed = await (0, import_parseContent.default)(infos.value);
let cookieString = `${cookie}=${encodeURIComponent(parsed.content.toString())}`;
if (infos.domain)
cookieString += `;Domain=${infos.domain}`;
if ("expires" in infos && infos.expires)
cookieString += `;Expires=${infos.expires.toUTCString()}`;
if ("maxAge" in infos && infos.maxAge !== void 0)
cookieString += `;Max-Age=${infos.maxAge}`;
if (infos.httpOnly)
cookieString += `;HttpOnly`;
if (infos.path)
cookieString += `;Path=${infos.path}`;
if (infos.sameSite)
cookieString += `;SameSite=${infos.sameSite[0].toUpperCase() + infos.sameSite.slice(1)}`;
if (infos.secure)
cookieString += `;Secure`;
readyCookies[cookie] = cookieString;
}
return () => {
if (!ctx.isAborted)
res.writeStatus((0, import_parseStatus.default)(ctx.response.status, ctx.response.statusMessage));
for (const header in readyHeaders) {
if (!ctx.isAborted)
res.writeHeader(header, readyHeaders[header]);
}
for (const cookie in readyCookies) {
if (!ctx.isAborted)
res.writeHeader("set-cookie", readyCookies[cookie]);
}
};
}