UNPKG

flight-path

Version:

Express style router for Fastly Compute@Edge

50 lines (49 loc) 1.71 kB
import cookie from "cookie"; export default class FPRequest { constructor(config, event) { this.config = config; this.event = event; this.clientInfo = {}; this.params = {}; this._cookies = {}; this.cookies = {}; this.clientInfo = event.client; this._headers = event.request.headers; this.method = event.request.method; this.url = new URL(event.request.url); this.query = Object.fromEntries(this.url.searchParams.entries()); if (config.parseCookies && this._headers.has("cookie")) { this._cookies = cookie.parse(this._headers.get("cookie")); } // If a cookie is set on the req object, we need to re-serialize the `cookie` header incase the req is forwarded to an origin this.cookies = new Proxy(this._cookies, { get: (target, key) => { return target[key]; }, set: (target, key, value) => { target[key] = value; this.reSerializeCookies(); return true; } }); } reSerializeCookies() { console.log("reSerializeCookies"); this._headers.set("cookie", Object.entries(this._cookies).map(([key, value]) => `${key}=${value}`).join("; ")); } get headers() { return Object.fromEntries(this._headers.entries()); } setHeader(key, value) { this._headers.set(key, value); } async json() { return await this.event.request.json(); } async text() { return await this.event.request.text(); } async arrayBuffer() { return await this.event.request.arrayBuffer(); } }