UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

121 lines (120 loc) 3.54 kB
"use strict"; 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 Base_exports = {}; __export(Base_exports, { default: () => Base }); module.exports = __toCommonJS(Base_exports); class Base { /** * Initializes a new Instance of a Web Context * @since 7.0.0 */ constructor(controller, localContext) { /** * Context Variables that are available everywhere in the requests lifespan * @since 1.2.1 */ this["@"] = {}; this.ctx = localContext; this.ctg = controller["globalContext"]; this.controller = controller; this.headers = localContext.headers; this.cookies = localContext.cookies; this.params = localContext.params; this.queries = localContext.queries; this.fragments = localContext.fragments; this.headers["modifyFn"] = (event, key, data) => { switch (event) { case "set": { localContext.response.headers[key] = data; return; } case "delete": { localContext.response.headers[key] = void 0; return; } case "clear": { let keys = 0; for (const cKey in localContext.response.headers) { if (!key.includes(cKey)) { localContext.response.headers[key] = void 0; keys++; } } return keys; } } }; this.cookies["modifyFn"] = (event, key, data) => { switch (event) { case "set": { localContext.response.cookies[key] = data; return; } case "delete": { localContext.response.cookies[key] = { value: "r", maxAge: 0 }; return; } case "clear": { let keys = 0; for (const [cKey] of localContext.cookies) { if (!key.includes(cKey)) { localContext.response.cookies[cKey] = { value: "r", maxAge: 0 }; keys++; } } return keys; } } }; let hostIp; if (this.ctx.isProxy && this.ctx.headers.has(this.ctg.options.proxy.header)) hostIp = this.ctx.headers.get(this.ctg.options.proxy.header, "").split(",")[0].trim(); else hostIp = this.ctx.remoteAddress.split(":")[0]; this.client = { userAgent: localContext.headers.get("user-agent", ""), port: parseInt(localContext.remoteAddress.split(":")[1]), ip: hostIp }; this.url = localContext.url; this.domain = localContext.headers.get("host", ""); } /** * Set a Custom Variable * @example * ``` * ctr.setCustom('hello', 123) * * ctr["@"].hello // 123 * ``` * @since 1.2.1 */ setCustom(name, value) { this["@"][name] = value; return this; } }