UNPKG

@worker-tools/request-cookie-store

Version:

An implementation of the Cookie Store API for request handlers.

105 lines 5.25 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _RequestCookieStore_origin, _RequestCookieStore_map, _RequestCookieStore_changes; export * from 'cookie-store-interface'; import { setCookie, attrsToSetCookie, parseCookieHeader } from './set-cookie.js'; /** * An implementation of the [Cookie Store API](https://wicg.github.io/cookie-store) for request handlers. * * It uses the `Cookie` header of a request to populate the store and * keeps a record of changes that can be exported as a list of `Set-Cookie` headers. * * Note that this is not a polyfill! It is intended as a cookie middleware for Cloudflare Workers, * and perhaps some other uses. */ export class RequestCookieStore { constructor(request) { _RequestCookieStore_origin.set(this, void 0); _RequestCookieStore_map.set(this, new Map()); _RequestCookieStore_changes.set(this, new Map()); const origin = request.headers.get('origin') || request.url; const cookie = request.headers.get('cookie'); if (origin) __classPrivateFieldSet(this, _RequestCookieStore_origin, new URL(origin), "f"); __classPrivateFieldSet(this, _RequestCookieStore_map, parseCookieHeader(cookie), "f"); } get(options) { // FIXME if (typeof options !== 'string') throw Error('Overload not implemented.'); return Promise.resolve(__classPrivateFieldGet(this, _RequestCookieStore_map, "f").has(options) ? { name: options, value: __classPrivateFieldGet(this, _RequestCookieStore_map, "f").get(options) } : null); } getAll(options) { // FIXME if (options != null) throw Error('Overload not implemented.'); return Promise.resolve([...__classPrivateFieldGet(this, _RequestCookieStore_map, "f")].map(([name, value]) => ({ name, value }))); } set(options, value) { const result = setCookie(options, value, __classPrivateFieldGet(this, _RequestCookieStore_origin, "f")); if (!result) return Promise.resolve(); const [attributes, expires] = result; const [[name, val]] = attributes; __classPrivateFieldGet(this, _RequestCookieStore_changes, "f").set(name, attributes); if (expires && expires < new Date()) __classPrivateFieldGet(this, _RequestCookieStore_map, "f").delete(name); else __classPrivateFieldGet(this, _RequestCookieStore_map, "f").set(name, val); return Promise.resolve(); } delete(options) { // FIXME if (typeof options !== 'string') throw Error('Overload not implemented.'); this.set({ name: options, value: '', expires: new Date(0) }); return Promise.resolve(); } /** * Exports the recorded changes to this store as a list of `Set-Cookie` headers. * * Can be passed as the `headers` field when building a new `Response`: * ```ts * new Response(body, { headers: cookieStore.headers }) * ``` */ get headers() { const headers = []; for (const attrs of __classPrivateFieldGet(this, _RequestCookieStore_changes, "f").values()) { headers.push(['Set-Cookie', attrsToSetCookie(attrs)]); } return headers; } /** Exports the entire cookie store as a `cookie` header string */ toCookieString() { return [...__classPrivateFieldGet(this, _RequestCookieStore_map, "f")].map(x => x.join('=')).join('; '); } /** Helper to turn a single `CookieInit` into a `set-cookie` string. * @deprecated Might remove/change name */ static toSetCookie(cookie) { const x = setCookie(cookie); return x ? attrsToSetCookie(x[0]) : ''; } addEventListener(_type, _listener, _options) { throw new Error("Method not implemented."); } dispatchEvent(_event) { throw new Error("Method not implemented."); } removeEventListener(_type, _callback, _options) { throw new Error("Method not implemented."); } } _RequestCookieStore_origin = new WeakMap(), _RequestCookieStore_map = new WeakMap(), _RequestCookieStore_changes = new WeakMap(); //# sourceMappingURL=index.js.map