@worker-tools/middleware
Version:
A suite of standalone HTTP server middlewares for Worker Runtimes.
85 lines • 4.56 kB
JavaScript
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 _MiddlewareCookieStore_promise, _MiddlewareCookieStore_store;
import { ExtendablePromise } from "@worker-tools/extendable-promise";
import { cookiesFrom } from "../cookies.js";
const encode = encodeURIComponent;
function decodeItem(item) {
if (item) {
item.name = decodeURIComponent(item.name);
item.value = decodeURIComponent(item.value);
}
return item;
}
/**
* A more opinionated cookie store implementation that
* - URI-(en|de)codes cookie values and
* - provides a promise that resolves when all async operations associated with this store have settled.
*/
export class MiddlewareCookieStore {
constructor(store, requestDuration) {
_MiddlewareCookieStore_promise.set(this, void 0);
_MiddlewareCookieStore_store.set(this, void 0);
__classPrivateFieldSet(this, _MiddlewareCookieStore_store, store, "f");
__classPrivateFieldSet(this, _MiddlewareCookieStore_promise, new ExtendablePromise(requestDuration), "f");
}
async get(options) {
return decodeItem(await __classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").get(options));
}
async getAll(options) {
return (await __classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").getAll(options)).map(decodeItem);
}
set(name, value) {
let res;
if (typeof name === 'string' && typeof value === 'string') {
res = __classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").set(encode(name), encode(value));
}
else if (name && typeof name === 'object') {
const options = name;
options.name = encode(options.name);
options.value = encode(options.value);
res = __classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").set(options);
}
else
throw Error('Illegal invocation');
__classPrivateFieldGet(this, _MiddlewareCookieStore_promise, "f").waitUntil(res);
return res;
}
delete(options) {
const res = __classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").delete(options);
__classPrivateFieldGet(this, _MiddlewareCookieStore_promise, "f").waitUntil(res);
return res;
}
addEventListener(type, callback, options) {
__classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").addEventListener(type, callback, options);
}
dispatchEvent(event) {
return __classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").dispatchEvent(event);
}
removeEventListener(type, callback, options) {
__classPrivateFieldGet(this, _MiddlewareCookieStore_store, "f").removeEventListener(type, callback, options);
}
/** @deprecated Name of this property might change */
get settled() { return __classPrivateFieldGet(this, _MiddlewareCookieStore_promise, "f").settled; }
/** @deprecated Name of this property might change */
get allSettledPromise() { return Promise.resolve(__classPrivateFieldGet(this, _MiddlewareCookieStore_promise, "f")); }
/**
* If you've made changes to the store and would like to access the current cookies as an object,
* it is provided as a promise here (TODO:)
* @deprecated This method might change names
*/
get updatedCookies() {
return cookiesFrom(this);
}
}
_MiddlewareCookieStore_promise = new WeakMap(), _MiddlewareCookieStore_store = new WeakMap();
//# sourceMappingURL=middleware-cookie-store.js.map