UNPKG

6.17 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __exportStar = (this && this.__exportStar) || function(m, exports) {
14 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15};
16var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
17 if (kind === "m") throw new TypeError("Private method is not writable");
18 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
19 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");
20 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
21};
22var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
23 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
24 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");
25 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
26};
27var _RequestCookieStore_origin, _RequestCookieStore_map, _RequestCookieStore_changes;
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.RequestCookieStore = void 0;
30__exportStar(require("cookie-store-interface"), exports);
31const set_cookie_js_1 = require("./set-cookie.js");
32/**
33 * An implementation of the [Cookie Store API](https://wicg.github.io/cookie-store) for request handlers.
34 *
35 * It uses the `Cookie` header of a request to populate the store and
36 * keeps a record of changes that can be exported as a list of `Set-Cookie` headers.
37 *
38 * Note that this is not a polyfill! It is intended as a cookie middleware for Cloudflare Workers,
39 * and perhaps some other uses.
40 */
41class RequestCookieStore {
42 constructor(request) {
43 _RequestCookieStore_origin.set(this, void 0);
44 _RequestCookieStore_map.set(this, new Map());
45 _RequestCookieStore_changes.set(this, new Map());
46 const origin = request.headers.get('origin') || request.url;
47 const cookie = request.headers.get('cookie');
48 if (origin)
49 __classPrivateFieldSet(this, _RequestCookieStore_origin, new URL(origin), "f");
50 __classPrivateFieldSet(this, _RequestCookieStore_map, (0, set_cookie_js_1.parseCookieHeader)(cookie), "f");
51 }
52 get(options) {
53 // FIXME
54 if (typeof options !== 'string')
55 throw Error('Overload not implemented.');
56 return Promise.resolve(__classPrivateFieldGet(this, _RequestCookieStore_map, "f").has(options)
57 ? { name: options, value: __classPrivateFieldGet(this, _RequestCookieStore_map, "f").get(options) }
58 : null);
59 }
60 getAll(options) {
61 // FIXME
62 if (options != null)
63 throw Error('Overload not implemented.');
64 return Promise.resolve([...__classPrivateFieldGet(this, _RequestCookieStore_map, "f")].map(([name, value]) => ({ name, value })));
65 }
66 set(options, value) {
67 const result = (0, set_cookie_js_1.setCookie)(options, value, __classPrivateFieldGet(this, _RequestCookieStore_origin, "f"));
68 if (!result)
69 return Promise.resolve();
70 const [attributes, expires] = result;
71 const [[name, val]] = attributes;
72 __classPrivateFieldGet(this, _RequestCookieStore_changes, "f").set(name, attributes);
73 if (expires && expires < new Date())
74 __classPrivateFieldGet(this, _RequestCookieStore_map, "f").delete(name);
75 else
76 __classPrivateFieldGet(this, _RequestCookieStore_map, "f").set(name, val);
77 return Promise.resolve();
78 }
79 delete(options) {
80 // FIXME
81 if (typeof options !== 'string')
82 throw Error('Overload not implemented.');
83 this.set({ name: options, value: '', expires: new Date(0) });
84 return Promise.resolve();
85 }
86 /**
87 * Exports the recorded changes to this store as a list of `Set-Cookie` headers.
88 *
89 * Can be passed as the `headers` field when building a new `Response`:
90 * ```ts
91 * new Response(body, { headers: cookieStore.headers })
92 * ```
93 */
94 get headers() {
95 const headers = [];
96 for (const attrs of __classPrivateFieldGet(this, _RequestCookieStore_changes, "f").values()) {
97 headers.push(['Set-Cookie', (0, set_cookie_js_1.attrsToSetCookie)(attrs)]);
98 }
99 return headers;
100 }
101 /** Exports the entire cookie store as a `cookie` header string */
102 toCookieString() {
103 return [...__classPrivateFieldGet(this, _RequestCookieStore_map, "f")].map(x => x.join('=')).join('; ');
104 }
105 /** Helper to turn a single `CookieInit` into a `set-cookie` string.
106 * @deprecated Might remove/change name */
107 static toSetCookie(cookie) {
108 const x = (0, set_cookie_js_1.setCookie)(cookie);
109 return x ? (0, set_cookie_js_1.attrsToSetCookie)(x[0]) : '';
110 }
111 addEventListener(_type, _listener, _options) {
112 throw new Error("Method not implemented.");
113 }
114 dispatchEvent(_event) {
115 throw new Error("Method not implemented.");
116 }
117 removeEventListener(_type, _callback, _options) {
118 throw new Error("Method not implemented.");
119 }
120}
121exports.RequestCookieStore = RequestCookieStore;
122_RequestCookieStore_origin = new WeakMap(), _RequestCookieStore_map = new WeakMap(), _RequestCookieStore_changes = new WeakMap();
123//# sourceMappingURL=index.js.map
\No newline at end of file