File size: 12,196 Bytes
5fae594 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
import type { SerializedCookie } from './constants';
/**
* Optional configuration to be used when parsing cookies.
* @public
*/
export interface ParseCookieOptions {
/**
* If `true` then keyless cookies like `=abc` and `=` which are not RFC-compliant will be parsed.
*/
loose?: boolean | undefined;
}
/**
* Configurable values that can be set when creating a {@link Cookie}.
* @public
*/
export interface CreateCookieOptions {
/** {@inheritDoc Cookie.key} */
key?: string;
/** {@inheritDoc Cookie.value} */
value?: string;
/** {@inheritDoc Cookie.expires} */
expires?: Date | 'Infinity' | null;
/** {@inheritDoc Cookie.maxAge} */
maxAge?: number | 'Infinity' | '-Infinity' | null;
/** {@inheritDoc Cookie.domain} */
domain?: string | null;
/** {@inheritDoc Cookie.path} */
path?: string | null;
/** {@inheritDoc Cookie.secure} */
secure?: boolean;
/** {@inheritDoc Cookie.httpOnly} */
httpOnly?: boolean;
/** {@inheritDoc Cookie.extensions} */
extensions?: string[] | null;
/** {@inheritDoc Cookie.creation} */
creation?: Date | 'Infinity' | null;
/** {@inheritDoc Cookie.hostOnly} */
hostOnly?: boolean | null;
/** {@inheritDoc Cookie.pathIsDefault} */
pathIsDefault?: boolean | null;
/** {@inheritDoc Cookie.lastAccessed} */
lastAccessed?: Date | 'Infinity' | null;
/** {@inheritDoc Cookie.sameSite} */
sameSite?: string | undefined;
}
/**
* An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to a user's web browser.
* It is defined in {@link https://www.rfc-editor.org/rfc/rfc6265.html | RFC6265}.
* @public
*/
export declare class Cookie {
/**
* The name or key of the cookie
*/
key: string;
/**
* The value of the cookie
*/
value: string;
/**
* The 'Expires' attribute of the cookie
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.1 | RFC6265 Section 5.2.1}).
*/
expires: Date | 'Infinity' | null;
/**
* The 'Max-Age' attribute of the cookie
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.2 | RFC6265 Section 5.2.2}).
*/
maxAge: number | 'Infinity' | '-Infinity' | null;
/**
* The 'Domain' attribute of the cookie represents the domain the cookie belongs to
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.3 | RFC6265 Section 5.2.3}).
*/
domain: string | null;
/**
* The 'Path' attribute of the cookie represents the path of the cookie
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.4 | RFC6265 Section 5.2.4}).
*/
path: string | null;
/**
* The 'Secure' flag of the cookie indicates if the scope of the cookie is
* limited to secure channels (e.g.; HTTPS) or not
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.5 | RFC6265 Section 5.2.5}).
*/
secure: boolean;
/**
* The 'HttpOnly' flag of the cookie indicates if the cookie is inaccessible to
* client scripts or not
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.6 | RFC6265 Section 5.2.6}).
*/
httpOnly: boolean;
/**
* Contains attributes which are not part of the defined spec but match the `extension-av` syntax
* defined in Section 4.1.1 of RFC6265
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-4.1.1 | RFC6265 Section 4.1.1}).
*/
extensions: string[] | null;
/**
* Set to the date and time when a Cookie is initially stored or a matching cookie is
* received that replaces an existing cookie
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 Section 5.3}).
*
* Also used to maintain ordering among cookies. Among cookies that have equal-length path fields,
* cookies with earlier creation-times are listed before cookies with later creation-times
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.4 | RFC6265 Section 5.4}).
*/
creation: Date | 'Infinity' | null;
/**
* A global counter used to break ordering ties between two cookies that have equal-length path fields
* and the same creation-time.
*/
creationIndex: number;
/**
* A boolean flag indicating if a cookie is a host-only cookie (i.e.; when the request's host exactly
* matches the domain of the cookie) or not
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 Section 5.3}).
*/
hostOnly: boolean | null;
/**
* A boolean flag indicating if a cookie had no 'Path' attribute and the default path
* was used
* (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.4 | RFC6265 Section 5.2.4}).
*/
pathIsDefault: boolean | null;
/**
* Set to the date and time when a cookie was initially stored ({@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 Section 5.3}) and updated whenever
* the cookie is retrieved from the {@link CookieJar} ({@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.4 | RFC6265 Section 5.4}).
*/
lastAccessed: Date | 'Infinity' | null;
/**
* The 'SameSite' attribute of a cookie as defined in RFC6265bis
* (See {@link https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-13.html#section-5.2 | RFC6265bis (v13) Section 5.2 }).
*/
sameSite: string | undefined;
/**
* Create a new Cookie instance.
* @public
* @param options - The attributes to set on the cookie
*/
constructor(options?: CreateCookieOptions);
/**
* For convenience in using `JSON.stringify(cookie)`. Returns a plain-old Object that can be JSON-serialized.
*
* @remarks
* - Any `Date` properties (such as {@link Cookie.expires}, {@link Cookie.creation}, and {@link Cookie.lastAccessed}) are exported in ISO format (`Date.toISOString()`).
*
* - Custom Cookie properties are discarded. In tough-cookie 1.x, since there was no {@link Cookie.toJSON} method explicitly defined, all enumerable properties were captured.
* If you want a property to be serialized, add the property name to {@link Cookie.serializableProperties}.
*/
toJSON(): SerializedCookie;
/**
* Does a deep clone of this cookie, implemented exactly as `Cookie.fromJSON(cookie.toJSON())`.
* @public
*/
clone(): Cookie | undefined;
/**
* Validates cookie attributes for semantic correctness. Useful for "lint" checking any `Set-Cookie` headers you generate.
* For now, it returns a boolean, but eventually could return a reason string.
*
* @remarks
* Works for a few things, but is by no means comprehensive.
*
* @beta
*/
validate(): boolean;
/**
* Sets the 'Expires' attribute on a cookie.
*
* @remarks
* When given a `string` value it will be parsed with {@link parseDate}. If the value can't be parsed as a cookie date
* then the 'Expires' attribute will be set to `"Infinity"`.
*
* @param exp - the new value for the 'Expires' attribute of the cookie.
*/
setExpires(exp: string | Date): void;
/**
* Sets the 'Max-Age' attribute (in seconds) on a cookie.
*
* @remarks
* Coerces `-Infinity` to `"-Infinity"` and `Infinity` to `"Infinity"` so it can be serialized to JSON.
*
* @param age - the new value for the 'Max-Age' attribute (in seconds).
*/
setMaxAge(age: number): void;
/**
* Encodes to a `Cookie` header value (specifically, the {@link Cookie.key} and {@link Cookie.value} properties joined with "=").
* @public
*/
cookieString(): string;
/**
* Encodes to a `Set-Cookie header` value.
* @public
*/
toString(): string;
/**
* Computes the TTL relative to now (milliseconds).
*
* @remarks
* - `Infinity` is returned for cookies without an explicit expiry
*
* - `0` is returned if the cookie is expired.
*
* - Otherwise a time-to-live in milliseconds is returned.
*
* @param now - passing an explicit value is mostly used for testing purposes since this defaults to the `Date.now()`
* @public
*/
TTL(now?: number): number;
/**
* Computes the absolute unix-epoch milliseconds that this cookie expires.
*
* The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The {@link Cookie.lastAccessed} attribute
* (or the `now` parameter if given) is used to offset the {@link Cookie.maxAge} attribute.
*
* If Expires ({@link Cookie.expires}) is set, that's returned.
*
* @param now - can be used to provide a time offset (instead of {@link Cookie.lastAccessed}) to use when calculating the "Max-Age" value
*/
expiryTime(now?: Date): number | undefined;
/**
* Indicates if the cookie has been persisted to a store or not.
* @public
*/
isPersistent(): boolean;
/**
* Calls {@link canonicalDomain} with the {@link Cookie.domain} property.
* @public
*/
canonicalizedDomain(): string | undefined;
/**
* Alias for {@link Cookie.canonicalizedDomain}
* @public
*/
cdomain(): string | undefined;
/**
* Parses a string into a Cookie object.
*
* @remarks
* Note: when parsing a `Cookie` header it must be split by ';' before each Cookie string can be parsed.
*
* @example
* ```
* // parse a `Set-Cookie` header
* const setCookieHeader = 'a=bcd; Expires=Tue, 18 Oct 2011 07:05:03 GMT'
* const cookie = Cookie.parse(setCookieHeader)
* cookie.key === 'a'
* cookie.value === 'bcd'
* cookie.expires === new Date(Date.parse('Tue, 18 Oct 2011 07:05:03 GMT'))
* ```
*
* @example
* ```
* // parse a `Cookie` header
* const cookieHeader = 'name=value; name2=value2; name3=value3'
* const cookies = cookieHeader.split(';').map(Cookie.parse)
* cookies[0].name === 'name'
* cookies[0].value === 'value'
* cookies[1].name === 'name2'
* cookies[1].value === 'value2'
* cookies[2].name === 'name3'
* cookies[2].value === 'value3'
* ```
*
* @param str - The `Set-Cookie` header or a Cookie string to parse.
* @param options - Configures `strict` or `loose` mode for cookie parsing
*/
static parse(str: string, options?: ParseCookieOptions): Cookie | undefined;
/**
* Does the reverse of {@link Cookie.toJSON}.
*
* @remarks
* Any Date properties (such as .expires, .creation, and .lastAccessed) are parsed via Date.parse, not tough-cookie's parseDate, since ISO timestamps are being handled at this layer.
*
* @example
* ```
* const json = JSON.stringify({
* key: 'alpha',
* value: 'beta',
* domain: 'example.com',
* path: '/foo',
* expires: '2038-01-19T03:14:07.000Z',
* })
* const cookie = Cookie.fromJSON(json)
* cookie.key === 'alpha'
* cookie.value === 'beta'
* cookie.domain === 'example.com'
* cookie.path === '/foo'
* cookie.expires === new Date(Date.parse('2038-01-19T03:14:07.000Z'))
* ```
*
* @param str - An unparsed JSON string or a value that has already been parsed as JSON
*/
static fromJSON(str: unknown): Cookie | undefined;
private static cookiesCreated;
/**
* @internal
*/
static sameSiteLevel: {
readonly strict: 3;
readonly lax: 2;
readonly none: 1;
};
/**
* @internal
*/
static sameSiteCanonical: {
readonly strict: "Strict";
readonly lax: "Lax";
};
/**
* Cookie properties that will be serialized when using {@link Cookie.fromJSON} and {@link Cookie.toJSON}.
* @public
*/
static serializableProperties: readonly ["key", "value", "expires", "maxAge", "domain", "path", "secure", "httpOnly", "extensions", "hostOnly", "pathIsDefault", "creation", "lastAccessed", "sameSite"];
}
|