Skip to content

Latest commit

 

History

History
622 lines (269 loc) · 9.73 KB

tough-cookie.cookie.md

File metadata and controls

622 lines (269 loc) · 9.73 KB

Home > tough-cookie > Cookie

Cookie class

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 RFC6265.

Signature:

export declare class Cookie 

Constructors

Constructor

Modifiers

Description

(constructor)(options)

Create a new Cookie instance.

Properties

Property

Modifiers

Type

Description

creation

Date | 'Infinity' | 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 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 RFC6265 Section 5.4).

creationIndex

number

A global counter used to break ordering ties between two cookies that have equal-length path fields and the same creation-time.

domain

string | null

The 'Domain' attribute of the cookie represents the domain the cookie belongs to (See RFC6265 Section 5.2.3).

expires

Date | 'Infinity' | null

The 'Expires' attribute of the cookie (See RFC6265 Section 5.2.1).

extensions

string[] | null

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 RFC6265 Section 4.1.1).

hostOnly

boolean | null

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 RFC6265 Section 5.3).

httpOnly

boolean

The 'HttpOnly' flag of the cookie indicates if the cookie is inaccessible to client scripts or not (See RFC6265 Section 5.2.6).

key

string

The name or key of the cookie

lastAccessed

Date | 'Infinity' | null

Set to the date and time when a cookie was initially stored (RFC6265 Section 5.3) and updated whenever the cookie is retrieved from the CookieJar (RFC6265 Section 5.4).

maxAge

number | 'Infinity' | '-Infinity' | null

The 'Max-Age' attribute of the cookie (See RFC6265 Section 5.2.2).

path

string | null

The 'Path' attribute of the cookie represents the path of the cookie (See RFC6265 Section 5.2.4).

pathIsDefault

boolean | null

A boolean flag indicating if a cookie had no 'Path' attribute and the default path was used (See RFC6265 Section 5.2.4).

sameSite

string | undefined

The 'SameSite' attribute of a cookie as defined in RFC6265bis (See RFC6265bis (v13) Section 5.2).

secure

boolean

The 'Secure' flag of the cookie indicates if the scope of the cookie is limited to secure channels (e.g.; HTTPS) or not (See RFC6265 Section 5.2.5).

serializableProperties

static

readonly ["key", "value", "expires", "maxAge", "domain", "path", "secure", "httpOnly", "extensions", "hostOnly", "pathIsDefault", "creation", "lastAccessed", "sameSite"]

Cookie properties that will be serialized when using Cookie.fromJSON() and Cookie.toJSON().

value

string

The value of the cookie

Methods

Method

Modifiers

Description

canonicalizedDomain()

Calls canonicalDomain() with the Cookie.domain property.

cdomain()

Alias for Cookie.canonicalizedDomain()

clone()

Does a deep clone of this cookie, implemented exactly as Cookie.fromJSON(cookie.toJSON()).

cookieString()

Encodes to a Cookie header value (specifically, the Cookie.key and Cookie.value properties joined with "=").

expiryDate(now)

Similar to Cookie.expiryTime(), computes the absolute unix-epoch milliseconds that this cookie expires and returns it as a Date.

The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The Cookie.lastAccessed attribute (or the now parameter if given) is used to offset the Cookie.maxAge attribute.

If Expires (Cookie.expires) is set, that's returned.

expiryTime(now)

Computes the absolute unix-epoch milliseconds that this cookie expires.

The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The Cookie.lastAccessed attribute (or the now parameter if given) is used to offset the Cookie.maxAge attribute.

If Expires (Cookie.expires) is set, that's returned.

fromJSON(str)

static

Does the reverse of Cookie.toJSON().

isPersistent()

Indicates if the cookie has been persisted to a store or not.

parse(str, options)

static

Parses a string into a Cookie object.

setExpires(exp)

Sets the 'Expires' attribute on a cookie.

setMaxAge(age)

Sets the 'Max-Age' attribute (in seconds) on a cookie.

toJSON()

For convenience in using JSON.stringify(cookie). Returns a plain-old Object that can be JSON-serialized.

toString()

Encodes to a Set-Cookie header value.

TTL(now)

Computes the TTL relative to now (milliseconds).

validate()

(BETA) 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.