Skip to main content

Class: CookieStorage

Defined in: packages/core/src/storage/CookieStorage.ts:43

Storage of cookies, mirroring the cookies to the current request / response at the server side and the document.cookie property at the client side. The storage caches the cookies internally.

Extends​

Constructors​

Constructor​

new CookieStorage(window, request, response): CookieStorage

Defined in: packages/core/src/storage/CookieStorage.ts:182

Initializes the cookie storage.

Parameters​

window​

Window

The window utility.

request​

Request

The current HTTP request.

response​

Response

The current HTTP response.

Returns​

CookieStorage

Example​

cookie.set('cookie', 'value', { expires: 10 }); // cookie expires
// after 10s
cookie.set('cookie'); // delete cookie

Overrides​

Storage.constructor

Accessors​

$dependencies​

Get Signature​

get static $dependencies(): Dependencies

Defined in: packages/core/src/storage/CookieStorage.ts:95

Returns​

Dependencies

Methods​

clear()​

clear(): this

Defined in: packages/core/src/storage/CookieStorage.ts:280

Clears the storage of all entries.

Returns​

this

This storage.

Overrides​

Storage.clear


delete()​

delete(name, options): this

Defined in: packages/core/src/storage/CookieStorage.ts:268

Deletes the cookie identified by the specified name.

Parameters​

name​

string

Name identifying the cookie.

options​

CookieOptions = {}

The cookie options. The domain and path specify the cookie's domain and path. The httpOnly and secure flags set the flags of the same name of the cookie.

Returns​

this

This storage.

Overrides​

Storage.delete


get()​

get(name): string | number | boolean | Date | undefined

Defined in: packages/core/src/storage/CookieStorage.ts:216

Retrieves the value of the entry identified by the specified . The method returns undefined if the entry does not exists.

Entries set to the undefined value can be tested for existence using the link has method.

Parameters​

name​

string

The key identifying the storage entry.

Returns​

string | number | boolean | Date | undefined

The value of the storage entry.

Overrides​

Storage.get


getCookiesStringForCookieHeader()​

getCookiesStringForCookieHeader(url?): string

Defined in: packages/core/src/storage/CookieStorage.ts:318

Returns all cookies in this storage serialized to a string compatible with the Cookie HTTP header.

When url is provided, the method validates the cookie security based on the url and the cookie's domain, path, and secure attributes.

Parameters​

url?​

string

Returns​

string

All cookies in this storage serialized to a string compatible with the Cookie HTTP header.


getExpirationAsDate()​

getExpirationAsDate(expiration): Date

Defined in: packages/core/src/storage/CookieStorage.ts:481

Converts the provided cookie expiration to a Date instance.

Parameters​

expiration​

Cookie expiration in seconds from now, or as a string compatible with the Date constructor.

string | number | Date

Returns​

Date

Cookie expiration as a Date instance.


has()​

has(name): boolean

Defined in: packages/core/src/storage/CookieStorage.ts:207

Returns true if the entry identified by the specified key exists in this storage.

Parameters​

name​

string

The key identifying the storage entry.

Returns​

boolean

true if the storage entry exists.

Overrides​

Storage.has


init()​

init(options, transformFunction): this

Defined in: packages/core/src/storage/CookieStorage.ts:193

This method is used to finalize the initialization of the storage after the dependencies provided through the constructor have been prepared for use.

This method must be invoked only once and it must be the first method invoked on this instance.

Parameters​

options​

CookieOptions = {}

transformFunction​

Returns​

this

This storage.

Overrides​

Storage.init


keys()​

keys(): Iterable<string>

Defined in: packages/core/src/storage/CookieStorage.ts:293

Returns an iterator for traversing the keys in this storage. The order in which the keys are traversed is undefined.

Returns​

Iterable<string>

An iterator for traversing the keys in this storage. The iterator also implements the iterable protocol, returning itself as its own iterator, allowing it to be used in a for..of loop.

Overrides​

Storage.keys


parse()​

parse(): void

Defined in: packages/core/src/storage/CookieStorage.ts:393

Parses cookies from a cookie string and sets the parsed cookies to the internal storage.

The method obtains the cookie string from the request's Cookie HTTP header when used at the server side, and the document.cookie property at the client side.

Returns​

void


parseFromSetCookieHeader()​

parseFromSetCookieHeader(cookiesString, url?): void

Defined in: packages/core/src/storage/CookieStorage.ts:357

Parses cookies from the provided Set-Cookie HTTP header value.

When url is provided, the method validates the cookie security based on the url and the cookie's domain, path, and secure attributes.

The parsed cookies will be set to the internal storage, and the current HTTP response (via the Set-Cookie HTTP header) if at the server side, or the browser (via the document.cookie property).

Parameters​

cookiesString​

The value of the Set-Cookie HTTP header. When there are multiple cookies, the value can be provided as an array of strings.

string | string[]

url?​

string

Returns​

void


recomputeCookieMaxAgeAndExpires()​

recomputeCookieMaxAgeAndExpires(options): void

Defined in: packages/core/src/storage/CookieStorage.ts:459

Recomputes cookie's attributes maxAge and expires between each other.

Parameters​

options​

CookieOptions

Cookie attributes. Only the attributes listed in the type annotation of this field are supported. For documentation and full list of cookie attributes see http://tools.ietf.org/html/rfc2965#page-5

Returns​

void


sanitizeCookieValue()​

sanitizeCookieValue(value): string

Defined in: packages/core/src/storage/CookieStorage.ts:417

Sanitize cookie value by rules in (@see http://tools.ietf.org/html/rfc6265#section-4r.1.1). Erase all invalid characters from cookie value.

Parameters​

value​

Cookie value

string | number | boolean | Date | undefined

Returns​

string

Sanitized value


set()​

set(name, value, options): this

Defined in: packages/core/src/storage/CookieStorage.ts:234

Sets the storage entry identified by the specified key to the provided value. The method creates the entry if it does not exist already.

Parameters​

name​

string

The key identifying the storage entry.

value​

The storage entry value.

string | number | boolean | Date | undefined

options​

CookieOptions = {}

The cookie options. The maxAge is the maximum age in seconds of the cookie before it will be deleted, the expires is an alternative to that, specifying the moment at which the cookie will be discarded. The domain and path specify the cookie's domain and path. The httpOnly and secure flags set the flags of the same name of the cookie.

Returns​

this

This storage.

Overrides​

Storage.set


size()​

size(): number

Defined in: packages/core/src/storage/CookieStorage.ts:302

Returns the number of entries in this storage.

Returns​

number

The number of entries in this storage.

Overrides​

Storage.size


validateCookieSecurity()​

static validateCookieSecurity(cookie, url): boolean

Defined in: packages/core/src/storage/CookieStorage.ts:104

Filters invalid cookies based on the provided url. We try to check validity of the domain based on secure, path and domain definitions.

Parameters​

Cookie

url​

string

Returns​

boolean