@worker-tools/request-cookie-store
Version:
An implementation of the Cookie Store API for request handlers.
123 lines • 6.17 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
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;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequestCookieStore = void 0;
__exportStar(require("cookie-store-interface"), exports);
const set_cookie_js_1 = require("./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.
*/
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, (0, set_cookie_js_1.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 = (0, set_cookie_js_1.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', (0, set_cookie_js_1.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 = (0, set_cookie_js_1.setCookie)(cookie);
return x ? (0, set_cookie_js_1.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.");
}
}
exports.RequestCookieStore = RequestCookieStore;
_RequestCookieStore_origin = new WeakMap(), _RequestCookieStore_map = new WeakMap(), _RequestCookieStore_changes = new WeakMap();
//# sourceMappingURL=index.js.map
;