@edge-runtime/cookies
Advanced tools
Comparing version 2.0.0-beta.34 to 2.0.0-beta.35
@@ -114,3 +114,4 @@ // Type definitions for cookie 0.5 | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#dictdef-cookielistitem CookieListItem} as specified by W3C. | ||
* {@link https://wicg.github.io/cookie-store/#dictdef-cookielistitem CookieListItem} | ||
* as specified by W3C. | ||
*/ | ||
@@ -124,46 +125,18 @@ interface CookieListItem extends Pick<CookieSerializeOptions, 'domain' | 'path' | 'expires' | 'secure' | 'sameSite'> { | ||
/** | ||
* Extends {@link CookieListItem} with the `httpOnly`, `maxAge` and `priority` properties. | ||
* Superset of {@link CookieListItem} extending it with | ||
* the `httpOnly`, `maxAge` and `priority` properties. | ||
*/ | ||
declare type Cookie = CookieListItem & Pick<CookieSerializeOptions, 'httpOnly' | 'maxAge' | 'priority'>; | ||
declare type ResponseCookie = CookieListItem & Pick<CookieSerializeOptions, 'httpOnly' | 'maxAge' | 'priority'>; | ||
/** | ||
* Loose implementation of the experimental [Cookie Store API](https://wicg.github.io/cookie-store/#dictdef-cookie) | ||
* The main difference is `ResponseCookies` methods do not return a Promise. | ||
* Subset of {@link CookieListItem}, only containing `name` and `value` | ||
* since other cookie attributes aren't be available on a `Request`. | ||
*/ | ||
declare class ResponseCookies { | ||
#private; | ||
constructor(response: Response); | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-get CookieStore#get} without the Promise. | ||
*/ | ||
get(...args: [key: string] | [options: Cookie]): Cookie | undefined; | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-getAll CookieStore#getAll} without the Promise. | ||
*/ | ||
getAll(...args: [key: string] | [options: Cookie] | [undefined]): Cookie[]; | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-set CookieStore#set} without the Promise. | ||
*/ | ||
set(...args: [key: string, value: string, cookie?: Partial<Cookie>] | [options: Cookie]): this; | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-delete CookieStore#delete} without the Promise. | ||
*/ | ||
delete(...args: [key: string] | [options: Cookie]): this; | ||
/** | ||
* Uses {@link ResponseCookies.get} to return only the cookie value. | ||
*/ | ||
getValue(...args: [key: string] | [options: Cookie]): string | undefined; | ||
/** | ||
* Uses {@link ResponseCookies.delete} to invalidate all cookies matching the given name. | ||
* If no name is provided, all cookies are invalidated. | ||
*/ | ||
clear(...args: [key: string] | [options: Cookie] | [undefined]): this; | ||
} | ||
declare type RequestCookie = Pick<CookieListItem, 'name' | 'value'>; | ||
/** | ||
* A class for manipulating {@link Request} cookies. | ||
* A class for manipulating {@link Request} cookies (`Cookie` header). | ||
*/ | ||
declare class RequestCookies { | ||
#private; | ||
constructor(request: Request); | ||
constructor(requestHeaders: Headers); | ||
[Symbol.iterator](): IterableIterator<[string, string]>; | ||
@@ -174,6 +147,6 @@ /** | ||
get size(): number; | ||
get(...args: [name: string] | [Cookie]): string | undefined; | ||
getAll(...args: [name: string] | [Cookie] | [undefined]): [string, string][]; | ||
get(...args: [name: string] | [RequestCookie]): string | undefined; | ||
getAll(...args: [name: string] | [RequestCookie] | []): [string, string][]; | ||
has(name: string): boolean; | ||
set(...args: [key: string, value: string] | [options: Cookie]): this; | ||
set(...args: [key: string, value: string] | [options: RequestCookie]): this; | ||
/** | ||
@@ -191,2 +164,28 @@ * Delete the cookies matching the passed name or names in the request. | ||
export { Cookie, CookieListItem, RequestCookies, ResponseCookies }; | ||
/** | ||
* A class for manipulating {@link Response} cookies (`Set-Cookie` header). | ||
* Loose implementation of the experimental [Cookie Store API](https://wicg.github.io/cookie-store/#dictdef-cookie) | ||
* The main difference is `ResponseCookies` methods do not return a Promise. | ||
*/ | ||
declare class ResponseCookies { | ||
#private; | ||
constructor(responseHeaders: Headers); | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-get CookieStore#get} without the Promise. | ||
*/ | ||
get(...args: [key: string] | [options: ResponseCookie]): ResponseCookie | undefined; | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-getAll CookieStore#getAll} without the Promise. | ||
*/ | ||
getAll(...args: [key: string] | [options: ResponseCookie] | []): ResponseCookie[]; | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-set CookieStore#set} without the Promise. | ||
*/ | ||
set(...args: [key: string, value: string, cookie?: Partial<ResponseCookie>] | [options: ResponseCookie]): this; | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-delete CookieStore#delete} without the Promise. | ||
*/ | ||
delete(...args: [key: string] | [options: ResponseCookie]): this; | ||
} | ||
export { CookieListItem, RequestCookie, RequestCookies, ResponseCookie, ResponseCookies }; |
@@ -50,27 +50,14 @@ "use strict"; | ||
// src/cached.ts | ||
function cached(generate) { | ||
let cache = void 0; | ||
return (key) => { | ||
if ((cache == null ? void 0 : cache.key) !== key) { | ||
cache = { key, value: generate(key) }; | ||
} | ||
return cache.value; | ||
}; | ||
} | ||
// src/serialize.ts | ||
function serialize(cookie) { | ||
function serialize(c) { | ||
const attrs = [ | ||
cookie.path ? `Path=${cookie.path}` : "", | ||
cookie.expires ? `Expires=${cookie.expires.toUTCString()}` : "", | ||
cookie.maxAge ? `Max-Age=${cookie.maxAge}` : "", | ||
cookie.domain ? `Domain=${cookie.domain}` : "", | ||
cookie.secure ? "Secure" : "", | ||
cookie.httpOnly ? "HttpOnly" : "", | ||
cookie.sameSite ? `SameSite=${cookie.sameSite}` : "" | ||
"path" in c && c.path && `Path=${c.path}`, | ||
"expires" in c && c.expires && `Expires=${c.expires.toUTCString()}`, | ||
"maxAge" in c && c.maxAge && `Max-Age=${c.maxAge}`, | ||
"domain" in c && c.domain && `Domain=${c.domain}`, | ||
"secure" in c && c.secure && "Secure", | ||
"httpOnly" in c && c.httpOnly && "HttpOnly", | ||
"sameSite" in c && c.sameSite && `SameSite=${c.sameSite}` | ||
].filter(Boolean); | ||
return `${cookie.name}=${encodeURIComponent( | ||
cookie.value ?? "" | ||
)}; ${attrs.join("; ")}`; | ||
return `${c.name}=${encodeURIComponent(c.value ?? "")}; ${attrs.join("; ")}`; | ||
} | ||
@@ -119,60 +106,76 @@ function parseCookieString(cookie) { | ||
function parseSameSite(string) { | ||
string = string.toLowerCase(); | ||
return SAME_SITE.includes(string) ? string : void 0; | ||
} | ||
// src/response-cookies.ts | ||
// src/cached.ts | ||
function cached(generate) { | ||
let cache = void 0; | ||
return (key) => { | ||
if ((cache == null ? void 0 : cache.key) !== key) { | ||
cache = { key, value: generate(key) }; | ||
} | ||
return cache.value; | ||
}; | ||
} | ||
// src/request-cookies.ts | ||
var _headers, _cache, _parsed, parsed_fn; | ||
var ResponseCookies = class { | ||
constructor(response) { | ||
var RequestCookies = class { | ||
constructor(requestHeaders) { | ||
__privateAdd(this, _parsed); | ||
__privateAdd(this, _headers, void 0); | ||
__privateAdd(this, _cache, cached(() => { | ||
const headers = __privateGet(this, _headers).getAll("set-cookie"); | ||
const map = /* @__PURE__ */ new Map(); | ||
for (const header of headers) { | ||
const parsed = parseSetCookieString(header); | ||
if (parsed) { | ||
map.set(parsed.name, parsed); | ||
} | ||
} | ||
return map; | ||
__privateAdd(this, _cache, cached((header) => { | ||
const parsed = header ? parseCookieString(header) : /* @__PURE__ */ new Map(); | ||
return parsed; | ||
})); | ||
__privateSet(this, _headers, response.headers); | ||
__privateSet(this, _headers, requestHeaders); | ||
} | ||
[Symbol.iterator]() { | ||
return __privateMethod(this, _parsed, parsed_fn).call(this)[Symbol.iterator](); | ||
} | ||
get size() { | ||
return __privateMethod(this, _parsed, parsed_fn).call(this).size; | ||
} | ||
get(...args) { | ||
const key = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return __privateMethod(this, _parsed, parsed_fn).call(this).get(key); | ||
const name = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return __privateMethod(this, _parsed, parsed_fn).call(this).get(name); | ||
} | ||
getAll(...args) { | ||
var _a; | ||
const all = Array.from(__privateMethod(this, _parsed, parsed_fn).call(this).values()); | ||
const all = Array.from(__privateMethod(this, _parsed, parsed_fn).call(this)); | ||
if (!args.length) { | ||
return all; | ||
} | ||
const key = typeof args[0] === "string" ? args[0] : (_a = args[0]) == null ? void 0 : _a.name; | ||
return all.filter((c) => c.name === key); | ||
const name = typeof args[0] === "string" ? args[0] : (_a = args[0]) == null ? void 0 : _a.name; | ||
return all.filter(([n]) => n === name); | ||
} | ||
has(name) { | ||
return __privateMethod(this, _parsed, parsed_fn).call(this).has(name); | ||
} | ||
set(...args) { | ||
const [name, value, cookie] = args.length === 1 ? [args[0].name, args[0].value, args[0]] : args; | ||
const [name, value] = args.length === 1 ? [args[0].name, args[0].value] : args; | ||
const map = __privateMethod(this, _parsed, parsed_fn).call(this); | ||
map.set(name, normalizeCookie({ name, value, ...cookie })); | ||
replace(map, __privateGet(this, _headers)); | ||
map.set(name, value); | ||
__privateGet(this, _headers).set( | ||
"cookie", | ||
Array.from(map).map(([name2, value2]) => serialize({ name: name2, value: value2 })).join("; ") | ||
); | ||
return this; | ||
} | ||
delete(...args) { | ||
const name = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return this.set({ name, value: "", expires: new Date(0) }); | ||
delete(names) { | ||
const map = __privateMethod(this, _parsed, parsed_fn).call(this); | ||
const result = !Array.isArray(names) ? map.delete(names) : names.map((name) => map.delete(name)); | ||
__privateGet(this, _headers).set( | ||
"cookie", | ||
Array.from(map).map(([name, value]) => serialize({ name, value })).join("; ") | ||
); | ||
return result; | ||
} | ||
getValue(...args) { | ||
var _a; | ||
return (_a = this.get(...args)) == null ? void 0 : _a.value; | ||
} | ||
clear(...args) { | ||
var _a; | ||
const key = typeof args[0] === "string" ? args[0] : (_a = args[0]) == null ? void 0 : _a.name; | ||
this.getAll(key).forEach((c) => this.delete(c)); | ||
clear() { | ||
this.delete(Array.from(__privateMethod(this, _parsed, parsed_fn).call(this).keys())); | ||
return this; | ||
} | ||
[Symbol.for("edge-runtime.inspect.custom")]() { | ||
return `ResponseCookies ${JSON.stringify( | ||
return `RequestCookies ${JSON.stringify( | ||
Object.fromEntries(__privateMethod(this, _parsed, parsed_fn).call(this)) | ||
@@ -186,81 +189,51 @@ )}`; | ||
parsed_fn = function() { | ||
const allCookies = __privateGet(this, _headers).get("set-cookie"); | ||
return __privateGet(this, _cache).call(this, allCookies); | ||
const header = __privateGet(this, _headers).get("cookie"); | ||
return __privateGet(this, _cache).call(this, header); | ||
}; | ||
function replace(bag, headers) { | ||
headers.delete("set-cookie"); | ||
for (const [, value] of bag) { | ||
const serialized = serialize(value); | ||
headers.append("set-cookie", serialized); | ||
} | ||
} | ||
function normalizeCookie(cookie = { name: "", value: "" }) { | ||
if (cookie.maxAge) { | ||
cookie.expires = new Date(Date.now() + cookie.maxAge * 1e3); | ||
} | ||
if (cookie.path === null || cookie.path === void 0) { | ||
cookie.path = "/"; | ||
} | ||
return cookie; | ||
} | ||
// src/request-cookies.ts | ||
// src/response-cookies.ts | ||
var _headers2, _cache2, _parsed2, parsed_fn2; | ||
var RequestCookies = class { | ||
constructor(request) { | ||
var ResponseCookies = class { | ||
constructor(responseHeaders) { | ||
__privateAdd(this, _parsed2); | ||
__privateAdd(this, _headers2, void 0); | ||
__privateAdd(this, _cache2, cached((header) => { | ||
const parsed = header ? parseCookieString(header) : /* @__PURE__ */ new Map(); | ||
return parsed; | ||
__privateAdd(this, _cache2, cached(() => { | ||
const headers = __privateGet(this, _headers2).getAll("set-cookie"); | ||
const map = /* @__PURE__ */ new Map(); | ||
for (const header of headers) { | ||
const parsed = parseSetCookieString(header); | ||
if (parsed) { | ||
map.set(parsed.name, parsed); | ||
} | ||
} | ||
return map; | ||
})); | ||
__privateSet(this, _headers2, request.headers); | ||
__privateSet(this, _headers2, responseHeaders); | ||
} | ||
[Symbol.iterator]() { | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this)[Symbol.iterator](); | ||
} | ||
get size() { | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this).size; | ||
} | ||
get(...args) { | ||
const name = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this).get(name); | ||
const key = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this).get(key); | ||
} | ||
getAll(...args) { | ||
var _a; | ||
const all = Array.from(__privateMethod(this, _parsed2, parsed_fn2).call(this)); | ||
const all = Array.from(__privateMethod(this, _parsed2, parsed_fn2).call(this).values()); | ||
if (!args.length) { | ||
return all; | ||
} | ||
const name = typeof args[0] === "string" ? args[0] : (_a = args[0]) == null ? void 0 : _a.name; | ||
return all.filter(([n]) => n === name); | ||
const key = typeof args[0] === "string" ? args[0] : (_a = args[0]) == null ? void 0 : _a.name; | ||
return all.filter((c) => c.name === key); | ||
} | ||
has(name) { | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this).has(name); | ||
} | ||
set(...args) { | ||
const [key, value] = args.length === 1 ? [args[0].name, args[0].value, args[0]] : args; | ||
const [name, value, cookie] = args.length === 1 ? [args[0].name, args[0].value, args[0]] : args; | ||
const map = __privateMethod(this, _parsed2, parsed_fn2).call(this); | ||
map.set(key, value); | ||
__privateGet(this, _headers2).set( | ||
"cookie", | ||
Array.from(map).map(([name, value2]) => serialize({ name, value: value2 })).join("; ") | ||
); | ||
map.set(name, normalizeCookie({ name, value, ...cookie })); | ||
replace(map, __privateGet(this, _headers2)); | ||
return this; | ||
} | ||
delete(names) { | ||
const map = __privateMethod(this, _parsed2, parsed_fn2).call(this); | ||
const result = !Array.isArray(names) ? map.delete(names) : names.map((name) => map.delete(name)); | ||
__privateGet(this, _headers2).set( | ||
"cookie", | ||
Array.from(map).map(([name, value]) => serialize({ name, value })).join("; ") | ||
); | ||
return result; | ||
delete(...args) { | ||
const name = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return this.set({ name, value: "", expires: new Date(0) }); | ||
} | ||
clear() { | ||
this.delete(Array.from(__privateMethod(this, _parsed2, parsed_fn2).call(this).keys())); | ||
return this; | ||
} | ||
[Symbol.for("edge-runtime.inspect.custom")]() { | ||
return `RequestCookies ${JSON.stringify( | ||
return `ResponseCookies ${JSON.stringify( | ||
Object.fromEntries(__privateMethod(this, _parsed2, parsed_fn2).call(this)) | ||
@@ -274,5 +247,21 @@ )}`; | ||
parsed_fn2 = function() { | ||
const header = __privateGet(this, _headers2).get("cookie"); | ||
return __privateGet(this, _cache2).call(this, header); | ||
const allCookies = __privateGet(this, _headers2).get("set-cookie"); | ||
return __privateGet(this, _cache2).call(this, allCookies); | ||
}; | ||
function replace(bag, headers) { | ||
headers.delete("set-cookie"); | ||
for (const [, value] of bag) { | ||
const serialized = serialize(value); | ||
headers.append("set-cookie", serialized); | ||
} | ||
} | ||
function normalizeCookie(cookie = { name: "", value: "" }) { | ||
if (cookie.maxAge) { | ||
cookie.expires = new Date(Date.now() + cookie.maxAge * 1e3); | ||
} | ||
if (cookie.path === null || cookie.path === void 0) { | ||
cookie.path = "/"; | ||
} | ||
return cookie; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -279,0 +268,0 @@ 0 && (module.exports = { |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://edge-runtime.vercel.app/packages/cookies", | ||
"version": "2.0.0-beta.34", | ||
"version": "2.0.0-beta.35", | ||
"main": "dist/index.js", | ||
@@ -8,0 +8,0 @@ "module": "dist/index.mjs", |
Sorry, the diff of this file is not supported yet
42104
677