UNPKG

rexuws

Version:

An express-like framework built on top of uWebsocket.js aims at simple codebase and high performance

230 lines (229 loc) 7.69 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const accepts_1 = __importDefault(require("accepts")); const type_is_1 = __importDefault(require("type-is")); const qs = __importStar(require("querystring")); const symbol_1 = require("./utils/symbol"); const textDecoder = new TextDecoder(); class Request { constructor(req, opts) { this.accepted = []; this._headers = {}; this._hasCalledGetHeader = false; this._hasCookieParser = false; this.originalReq = req; this[symbol_1.GET_HEADER] = req.getHeader.bind(req); this[symbol_1.GET_PARAMS] = req.getParameter.bind(req); this[symbol_1.GET_METHOD] = req.getMethod.bind(req); this[symbol_1.GET_QUERY] = req.getQuery.bind(req); this[symbol_1.FOR_EACH] = req.forEach.bind(req); this[symbol_1.GET_URL] = req.getUrl.bind(req); if (opts) { const { paramsMap, forceInit, cookieParser, baseUrl } = opts; this.parametersMap = paramsMap; if (forceInit) { if (typeof forceInit === 'object') { const keys = Object.keys(forceInit); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const hasValue = forceInit[key]; if (hasValue) { this[key]; } } } if (typeof forceInit === 'boolean') { this.headers; this.url; this.query; this.params; this.method; } } if (cookieParser) { this._hasCookieParser = true; } this._baseUrl = baseUrl; } this.header = this.get; } get body() { if (this._parsedBody) { return this._parsedBody; } return this.raw; } set body(data) { this._parsedBody = data; } get headers() { if (this._hasCalledGetHeader) return this._headers; const headers = {}; this[symbol_1.FOR_EACH]((k, v) => { headers[k] = v; }); this._headers = headers; this._hasCalledGetHeader = true; return headers; } get method() { if (this._method) return this._method; this._method = this[symbol_1.GET_METHOD](); return this._method; } get query() { if (this._query) { return this._query; } this._query = qs.parse(this[symbol_1.GET_QUERY]()); return this._query; } get url() { if (this._url) return this._url; this._url = this[symbol_1.GET_URL](); return this._url; } get params() { if (this._params) return this._params; this._params = this.parametersMap && this.parametersMap?.length > 0 ? this.parametersMap.reduce((acc, cur, idx) => { acc[cur] = this[symbol_1.GET_PARAMS](idx); return acc; }, {}) : {}; return this._params; } get ip() { return textDecoder.decode(this[symbol_1.FROM_RES].getRemoteAddressAsText()); } get ips() { const fwd = this.get('X-Forwarded-For'); return fwd ? [fwd, this.ip] : [this.ip]; } get hostname() { let host = this.get('X-Forwarded-Host'); if (!host) { this.get('Host'); } else if (host.indexOf(',') === -1) { host = host.substring(0, host.indexOf(',')).trimRight(); } if (!host) return; const index = host[0] === '[' ? host.indexOf(']') + 1 : 0; // eslint-disable-next-line consistent-return return index !== -1 ? host.substring(0, index) : host; } get cookies() { if (this._hasCookieParser) { if (this._cookies) return this._cookies; // Get from headers const { cookie } = this.headers; if (!cookie) { this._cookies = {}; return {}; } const result = {}; // Parse cookies const parts = cookie.split(';'); for (let i = 0; i < parts.length; i++) { const val = parts[i].trim().split('='); if (val.length === 2) { result[val[0]] = val[1]; } } this._cookies = result; return result; } if (this._headers) { return this._headers.cookie; } return this[symbol_1.GET_HEADER]('cookie'); } get baseUrl() { return this._baseUrl; } get originalUrl() { if (this._originalUrl) return this._originalUrl; if (!this._baseUrl) return this.url; const escapedUrl = this.url.replace(this._baseUrl, ''); this._originalUrl = escapedUrl; return escapedUrl; } accepts(...type) { const accept = accepts_1.default(this); return accept.types.apply(accept, arguments); } acceptsCharsets(...charset) { const accept = accepts_1.default(this); return accept.charsets.apply(accept, arguments); } acceptsEncodings(...encoding) { const accept = accepts_1.default(this); return accept.encodings.apply(accept, arguments); } acceptsLanguages(...charset) { const accept = accepts_1.default(this); return accept.languages.apply(accept, arguments); } // eslint-disable-next-line class-methods-use-this get(name) { if (!name) { throw new TypeError('name argument is required to req.get'); } if (typeof name !== 'string') { throw new TypeError('name must be a string to req.get'); } const lc = name.toLowerCase(); switch (lc) { case 'referer': case 'referrer': return this.headers.referer || this.headers.referrer; default: return this.headers[lc]; } } is(type) { let arr; // support flattened arguments if (!Array.isArray(type)) { arr = new Array(arguments.length); for (let i = 0; i < arr.length; i++) { arr[i] = arguments[i]; } } arr = type; return type_is_1.default(this, arr); } } exports.default = Request;