UNPKG

@thi.ng/server

Version:

Minimal HTTP server with declarative routing, static file serving and freely extensible via pre/post interceptors

44 lines (43 loc) 977 B
const parseCoookies = (rawCookies = "", { domain = "localhost", path = "/", now = Date.now() } = {}) => { const res = {}; let currK; let currV; for (const part of rawCookies.split(";")) { let [name, value] = part.split("=").map((x) => x.trim()); value = decodeURIComponent(value); switch (name.toLowerCase()) { case "domain": if (value !== "." && !domain.endsWith(value)) currK = null; break; case "expires": if (Date.parse(value) < now) currK = null; break; case "path": if (!path.startsWith(value)) currK = null; break; case "maxage": case "httponly": case "priority": case "samesite": case "secure": break; default: if (currK && currV) { res[currK] = currV; } currK = name; currV = value; } } if (currK && currV) { res[currK] = currV; } return res; }; export { parseCoookies };