import { IncomingMessage } from 'http'; import { FormUtil } from '../body/form-util'; import { Request } from './http-protocol'; const URL_PARTS = Symbol('urlParts'); const QUERY = Symbol('query'); const ORIGINAL_URL = Symbol('originalUrl'); interface UrlParts { url: string; path: string; search: string; query?: Record; } /** * The request of Cell's server: Node's `IncomingMessage` with `path`, `query`, `protocol`, `hostname`, * `ip`, `originalUrl` and `get()`. Computed when read and kept until `url` changes. A request from another * server (a test, a serverless proxy) gets them with {@link HttpRequest.enhance}. */ export class HttpRequest extends IncomingMessage { declare body: any; declare protected [URL_PARTS]?: UrlParts; declare protected [QUERY]?: Record; declare protected [ORIGINAL_URL]?: string; /** `request` with the members of HttpRequest, by changing its prototype as express did. */ static enhance(request: IncomingMessage): Request { if (!(request instanceof HttpRequest)) { Object.setPrototypeOf(request, HttpRequest.prototype); } return request as unknown as Request; } get path(): string { return this.urlParts().path; } get query(): Record { const assigned = this[QUERY]; if (assigned) { return assigned; } const parts = this.urlParts(); if (!parts.query) { // The query parser of express (qs): five levels of brackets, arrays up to index 20. parts.query = parts.search.length > 1 ? FormUtil.parse(parts.search.slice(1), { depth: 5, arrayLimit: 20, parameterLimit: Infinity }) : {}; } return parts.query; } set query(query: Record) { this[QUERY] = query; } get protocol(): 'http' | 'https' { return (this.socket as { encrypted?: boolean } | null)?.encrypted ? 'https' : 'http'; } get secure(): boolean { return this.protocol === 'https'; } get hostname(): string | undefined { const host = this.headers.host; if (!host) { return undefined; } // [::1]:3000 → [::1] const end = host[0] === '[' ? host.indexOf(']') + 1 : 0; const colon = host.indexOf(':', end); return colon < 0 ? host : host.slice(0, colon); } get ip(): string | undefined { return this.socket?.remoteAddress; } get originalUrl(): string { return this[ORIGINAL_URL] ?? this.url ?? '/'; } set originalUrl(url: string) { this[ORIGINAL_URL] = url; } get(name: 'set-cookie'): string[] | undefined; get(name: string): string | undefined; get(name: string): string | string[] | undefined { const key = name.toLowerCase(); if (key === 'referer' || key === 'referrer') { return this.headers.referrer as string | undefined ?? this.headers.referer; } return this.headers[key]; } header(name: 'set-cookie'): string[] | undefined; header(name: string): string | undefined; header(name: string): string | string[] | undefined { return this.get(name); } protected urlParts(): UrlParts { const url = this.url ?? '/'; let parts = this[URL_PARTS]; if (parts?.url !== url) { parts = HttpRequest.split(url); this[URL_PARTS] = parts; } return parts; } /** `/a?b=1` → path `/a`, search `?b=1`; an absolute URL (a proxy request) keeps only its path. */ protected static split(url: string): UrlParts { if (url[0] !== '/') { try { const parsed = new URL(url); return { url, path: parsed.pathname, search: parsed.search }; } catch { // Not absolute: read it as a path. } } const hash = url.indexOf('#'); const end = hash < 0 ? url.length : hash; const question = url.indexOf('?'); if (question < 0 || question > end) { return { url, path: url.slice(0, end), search: '' }; } return { url, path: url.slice(0, question), search: url.slice(question, end) }; } }