@edge-runtime/cookies
Advanced tools
Comparing version 1.1.0-beta.33 to 2.0.0-beta.34
@@ -113,17 +113,48 @@ // Type definitions for cookie 0.5 | ||
interface Options extends CookieSerializeOptions { | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#dictdef-cookielistitem CookieListItem} as specified by W3C. | ||
*/ | ||
interface CookieListItem extends Pick<CookieSerializeOptions, 'domain' | 'path' | 'expires' | 'secure' | 'sameSite'> { | ||
/** A string with the name of a cookie. */ | ||
name: string; | ||
/** A string containing the value of the cookie. */ | ||
value: string; | ||
} | ||
/** | ||
* Extends {@link CookieListItem} with the `httpOnly`, `maxAge` and `priority` properties. | ||
*/ | ||
declare type Cookie = 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. | ||
*/ | ||
declare class ResponseCookies { | ||
private readonly headers; | ||
#private; | ||
constructor(response: Response); | ||
private cache; | ||
private parsed; | ||
set(key: string, value: string, options?: Options): this; | ||
delete(key: string): this; | ||
get(key: string): string | undefined; | ||
getWithOptions(key: string): { | ||
value: string | undefined; | ||
options: Options; | ||
}; | ||
/** | ||
* {@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; | ||
} | ||
@@ -135,22 +166,25 @@ | ||
declare class RequestCookies { | ||
private readonly headers; | ||
#private; | ||
constructor(request: Request); | ||
[Symbol.iterator](): IterableIterator<[string, string]>; | ||
/** | ||
* Delete all the cookies in the cookies in the request | ||
*/ | ||
clear(): void; | ||
/** | ||
* The amount of cookies received from the client | ||
*/ | ||
get size(): number; | ||
[Symbol.iterator](): IterableIterator<[string, string]>; | ||
private cache; | ||
private parsed; | ||
get(name: string): string | undefined; | ||
get(...args: [name: string] | [Cookie]): string | undefined; | ||
getAll(...args: [name: string] | [Cookie] | [undefined]): [string, string][]; | ||
has(name: string): boolean; | ||
set(name: string, value: string): this; | ||
delete(names: string[]): boolean[]; | ||
delete(name: string): boolean; | ||
set(...args: [key: string, value: string] | [options: Cookie]): this; | ||
/** | ||
* Delete the cookies matching the passed name or names in the request. | ||
*/ | ||
delete( | ||
/** Name or names of the cookies to be deleted */ | ||
names: string | string[]): boolean | boolean[]; | ||
/** | ||
* Delete all the cookies in the cookies in the request. | ||
*/ | ||
clear(): this; | ||
} | ||
export { Options, RequestCookies, ResponseCookies }; | ||
export { Cookie, CookieListItem, RequestCookies, ResponseCookies }; |
@@ -19,2 +19,24 @@ "use strict"; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __accessCheck = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet = (obj, member, getter) => { | ||
__accessCheck(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateSet = (obj, member, value, setter) => { | ||
__accessCheck(obj, member, "write to private field"); | ||
setter ? setter.call(obj, value) : member.set(obj, value); | ||
return value; | ||
}; | ||
var __privateMethod = (obj, member, method) => { | ||
__accessCheck(obj, member, "access private method"); | ||
return method; | ||
}; | ||
@@ -41,14 +63,15 @@ // src/index.ts | ||
// src/serialize.ts | ||
function serialize(name, value, options) { | ||
const { expires, maxAge, domain, path, secure, httpOnly, sameSite } = options; | ||
function serialize(cookie) { | ||
const attrs = [ | ||
path ? `Path=${path}` : "", | ||
expires ? `Expires=${expires.toUTCString()}` : "", | ||
maxAge ? `Max-Age=${maxAge}` : "", | ||
domain ? `Domain=${domain}` : "", | ||
secure ? "Secure" : "", | ||
httpOnly ? "HttpOnly" : "", | ||
sameSite ? `SameSite=${sameSite}` : "" | ||
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}` : "" | ||
].filter(Boolean); | ||
return `${name}=${encodeURIComponent(value)}; ${attrs.join("; ")}`; | ||
return `${cookie.name}=${encodeURIComponent( | ||
cookie.value ?? "" | ||
)}; ${attrs.join("; ")}`; | ||
} | ||
@@ -73,3 +96,5 @@ function parseCookieString(cookie) { | ||
); | ||
const options = { | ||
const cookie = { | ||
name, | ||
value: decodeURIComponent(value), | ||
domain, | ||
@@ -83,7 +108,3 @@ ...expires && { expires: new Date(expires) }, | ||
}; | ||
return { | ||
name, | ||
value: decodeURIComponent(value), | ||
attributes: compact(options) | ||
}; | ||
return compact(cookie); | ||
} | ||
@@ -105,6 +126,9 @@ function compact(t) { | ||
// src/response-cookies.ts | ||
var _headers, _cache, _parsed, parsed_fn; | ||
var ResponseCookies = class { | ||
constructor(response) { | ||
this.cache = cached((_key) => { | ||
const headers = this.headers.getAll("set-cookie"); | ||
__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(); | ||
@@ -114,94 +138,114 @@ for (const header of headers) { | ||
if (parsed) { | ||
map.set(parsed.name, { | ||
value: parsed.value, | ||
options: parsed == null ? void 0 : parsed.attributes | ||
}); | ||
map.set(parsed.name, parsed); | ||
} | ||
} | ||
return map; | ||
}); | ||
this.headers = response.headers; | ||
})); | ||
__privateSet(this, _headers, response.headers); | ||
} | ||
parsed() { | ||
const allCookies = this.headers.get("set-cookie"); | ||
return this.cache(allCookies); | ||
get(...args) { | ||
const key = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return __privateMethod(this, _parsed, parsed_fn).call(this).get(key); | ||
} | ||
set(key, value, options) { | ||
const map = this.parsed(); | ||
map.set(key, { value, options: normalizeCookieOptions(options || {}) }); | ||
replace(map, this.headers); | ||
getAll(...args) { | ||
var _a; | ||
const all = Array.from(__privateMethod(this, _parsed, parsed_fn).call(this).values()); | ||
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); | ||
} | ||
set(...args) { | ||
const [name, value, cookie] = args.length === 1 ? [args[0].name, args[0].value, args[0]] : args; | ||
const map = __privateMethod(this, _parsed, parsed_fn).call(this); | ||
map.set(name, normalizeCookie({ name, value, ...cookie })); | ||
replace(map, __privateGet(this, _headers)); | ||
return this; | ||
} | ||
delete(key) { | ||
return this.set(key, "", { expires: new Date(0) }); | ||
delete(...args) { | ||
const name = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return this.set({ name, value: "", expires: new Date(0) }); | ||
} | ||
get(key) { | ||
getValue(...args) { | ||
var _a; | ||
return (_a = this.getWithOptions(key)) == null ? void 0 : _a.value; | ||
return (_a = this.get(...args)) == null ? void 0 : _a.value; | ||
} | ||
getWithOptions(key) { | ||
const element = this.parsed().get(key); | ||
return { value: element == null ? void 0 : element.value, options: (element == null ? void 0 : element.options) ?? {} }; | ||
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)); | ||
return this; | ||
} | ||
[Symbol.for("edge-runtime.inspect.custom")]() { | ||
return `ResponseCookies ${JSON.stringify( | ||
Object.fromEntries(this.parsed()) | ||
Object.fromEntries(__privateMethod(this, _parsed, parsed_fn).call(this)) | ||
)}`; | ||
} | ||
}; | ||
_headers = new WeakMap(); | ||
_cache = new WeakMap(); | ||
_parsed = new WeakSet(); | ||
parsed_fn = function() { | ||
const allCookies = __privateGet(this, _headers).get("set-cookie"); | ||
return __privateGet(this, _cache).call(this, allCookies); | ||
}; | ||
function replace(bag, headers) { | ||
headers.delete("set-cookie"); | ||
for (const [key, { value, options }] of bag) { | ||
const serialized = serialize(key, value, options); | ||
for (const [, value] of bag) { | ||
const serialized = serialize(value); | ||
headers.append("set-cookie", serialized); | ||
} | ||
} | ||
var normalizeCookieOptions = (options) => { | ||
options = Object.assign({}, options); | ||
if (options.maxAge) { | ||
options.expires = new Date(Date.now() + options.maxAge * 1e3); | ||
function normalizeCookie(cookie = { name: "", value: "" }) { | ||
if (cookie.maxAge) { | ||
cookie.expires = new Date(Date.now() + cookie.maxAge * 1e3); | ||
} | ||
if (options.path === null || options.path === void 0) { | ||
options.path = "/"; | ||
if (cookie.path === null || cookie.path === void 0) { | ||
cookie.path = "/"; | ||
} | ||
return options; | ||
}; | ||
return cookie; | ||
} | ||
// src/request-cookies.ts | ||
var _headers2, _cache2, _parsed2, parsed_fn2; | ||
var RequestCookies = class { | ||
constructor(request) { | ||
this.cache = cached((header) => { | ||
__privateAdd(this, _parsed2); | ||
__privateAdd(this, _headers2, void 0); | ||
__privateAdd(this, _cache2, cached((header) => { | ||
const parsed = header ? parseCookieString(header) : /* @__PURE__ */ new Map(); | ||
return parsed; | ||
}); | ||
this.headers = request.headers; | ||
})); | ||
__privateSet(this, _headers2, request.headers); | ||
} | ||
clear() { | ||
this.delete([...this.parsed().keys()]); | ||
[Symbol.iterator]() { | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this)[Symbol.iterator](); | ||
} | ||
[Symbol.for("edge-runtime.inspect.custom")]() { | ||
return `RequestCookies ${JSON.stringify(Object.fromEntries(this.parsed()))}`; | ||
} | ||
get size() { | ||
return this.parsed().size; | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this).size; | ||
} | ||
[Symbol.iterator]() { | ||
return this.parsed()[Symbol.iterator](); | ||
get(...args) { | ||
const name = typeof args[0] === "string" ? args[0] : args[0].name; | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this).get(name); | ||
} | ||
parsed() { | ||
const header = this.headers.get("cookie"); | ||
return this.cache(header); | ||
getAll(...args) { | ||
var _a; | ||
const all = Array.from(__privateMethod(this, _parsed2, parsed_fn2).call(this)); | ||
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); | ||
} | ||
get(name) { | ||
return this.parsed().get(name); | ||
} | ||
has(name) { | ||
return this.parsed().has(name); | ||
return __privateMethod(this, _parsed2, parsed_fn2).call(this).has(name); | ||
} | ||
set(name, value) { | ||
const map = this.parsed(); | ||
map.set(name, value); | ||
this.headers.set( | ||
set(...args) { | ||
const [key, value] = 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", | ||
[...map].map(([key, value2]) => serialize(key, value2, {})).join("; ") | ||
Array.from(map).map(([name, value2]) => serialize({ name, value: value2 })).join("; ") | ||
); | ||
@@ -211,11 +255,27 @@ return this; | ||
delete(names) { | ||
const map = this.parsed(); | ||
const map = __privateMethod(this, _parsed2, parsed_fn2).call(this); | ||
const result = !Array.isArray(names) ? map.delete(names) : names.map((name) => map.delete(name)); | ||
this.headers.set( | ||
__privateGet(this, _headers2).set( | ||
"cookie", | ||
[...map].map(([key, value]) => serialize(key, value, {})).join("; ") | ||
Array.from(map).map(([name, value]) => serialize({ name, value })).join("; ") | ||
); | ||
return result; | ||
} | ||
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( | ||
Object.fromEntries(__privateMethod(this, _parsed2, parsed_fn2).call(this)) | ||
)}`; | ||
} | ||
}; | ||
_headers2 = new WeakMap(); | ||
_cache2 = new WeakMap(); | ||
_parsed2 = new WeakSet(); | ||
parsed_fn2 = function() { | ||
const header = __privateGet(this, _headers2).get("cookie"); | ||
return __privateGet(this, _cache2).call(this, header); | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -222,0 +282,0 @@ 0 && (module.exports = { |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://edge-runtime.vercel.app/packages/cookies", | ||
"version": "1.1.0-beta.33", | ||
"version": "2.0.0-beta.34", | ||
"main": "dist/index.js", | ||
@@ -23,4 +23,4 @@ "module": "dist/index.mjs", | ||
"devDependencies": { | ||
"@edge-runtime/format": "1.1.0-beta.33", | ||
"@edge-runtime/jest-environment": "1.1.0-beta.33", | ||
"@edge-runtime/format": "1.1.0-beta.34", | ||
"@edge-runtime/jest-environment": "1.1.0-beta.36", | ||
"@types/cookie": "0.5.1", | ||
@@ -27,0 +27,0 @@ "tsup": "6" |
Sorry, the diff of this file is not supported yet
42598
700