Skip to main content

Class: CookieStorage

@ima/core.CookieStorage

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.

Hierarchy

Constructors

constructor

new CookieStorage(window, request, response)

Initializes the cookie storage.

Parameters

NameTypeDescription
windowWindowThe window utility.
requestRequestThe current HTTP request.
responseResponseThe current HTTP response.

Example

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

Overrides

Storage.constructor

Defined in

packages/core/src/storage/CookieStorage.ts:111

Properties

#memoParseRawCookies

Private #memoParseRawCookies: MemoizedFn<(rawCookies: undefined | string) => string[]>

Memoized function of private parseRawCookies function

Defined in

packages/core/src/storage/CookieStorage.ts:93


_options

Private _options: CookieOptions

The overriding cookie attribute values.

Defined in

packages/core/src/storage/CookieStorage.ts:68


_request

Private _request: Request

The current HTTP request. This field is used at the server side.

Defined in

packages/core/src/storage/CookieStorage.ts:53


_response

Private _response: Response

The current HTTP response. This field is used at the server side.

Defined in

packages/core/src/storage/CookieStorage.ts:58


_storage

Private _storage: Map<string, Cookie>

The internal storage of entries.

Defined in

packages/core/src/storage/CookieStorage.ts:63


_transformFunction

Private _transformFunction: Object

Transform encode and decode functions for cookie value.

Type declaration

NameType
decode(value: string) => string
encode(value: string) => string

Defined in

packages/core/src/storage/CookieStorage.ts:82


_window

Private _window: Window

The window utility used to determine whether the IMA is being run at the client or at the server.

Defined in

packages/core/src/storage/CookieStorage.ts:48

Accessors

$dependencies

Static get $dependencies(): Dependencies

Returns

Dependencies

Defined in

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

Methods

#extractCookie

Private #extractCookie(cookieString): Cookie & { name?: string }

Extract cookie name, value and options from cookie string.

Parameters

NameTypeDescription
cookieStringstringThe value of the Set-Cookie HTTP header.

Returns

Cookie & { name?: string }

Defined in

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


#extractNameAndValue

Private #extractNameAndValue(pair, pairIndex): [null | string, undefined | null | string | number | boolean | Date]

Extract name and value for defined pair and pair index.

Parameters

NameType
pairstring
pairIndexnumber

Returns

[null | string, undefined | null | string | number | boolean | Date]

Defined in

packages/core/src/storage/CookieStorage.ts:511


#firstLetterToLowerCase

Private #firstLetterToLowerCase(word): string

Creates a copy of the provided word (or text) that has its first character converted to lower case.

Parameters

NameTypeDescription
wordstringThe word (or any text) that should have its first character converted to lower case.

Returns

string

A copy of the provided string with its first character converted to lower case.

Defined in

packages/core/src/storage/CookieStorage.ts:430


#generateCookieString

Private #generateCookieString(name, value, options): string

Generates a string representing the specified cookie, usable either with the document.cookie property or the Set-Cookie HTTP header.

(Note that the Cookie HTTP header uses a slightly different syntax.)

Parameters

NameTypeDescription
namestringThe cookie name.
valueundefined | string | number | boolean | DateThe cookie value, will be converted to string.
optionsCookieOptionsCookie 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

string

A string representing the cookie. Setting this string to the document.cookie property will set the cookie to the browser's cookie storage.

Defined in

packages/core/src/storage/CookieStorage.ts:453


#parseRawCookies

Private #parseRawCookies(rawCookies): string[]

Parameters

NameType
rawCookiesundefined | string

Returns

string[]

Defined in

packages/core/src/storage/CookieStorage.ts:386


clear

clear(): CookieStorage

Clears the storage of all entries.

Returns

CookieStorage

This storage.

Overrides

Storage.clear

Defined in

packages/core/src/storage/CookieStorage.ts:209


delete

delete(name, options?): CookieStorage

Deletes the cookie identified by the specified name.

Parameters

NameTypeDescription
namestringName identifying the cookie.
optionsCookieOptionsThe 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

CookieStorage

This storage.

Overrides

Storage.delete

Defined in

packages/core/src/storage/CookieStorage.ts:197


get

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

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

NameTypeDescription
namestringThe key identifying the storage entry.

Returns

undefined | string | number | boolean | Date

The value of the storage entry.

Overrides

Storage.get

Defined in

packages/core/src/storage/CookieStorage.ts:145


getCookiesStringForCookieHeader

getCookiesStringForCookieHeader(): string

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

Returns

string

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

Defined in

packages/core/src/storage/CookieStorage.ts:244


getExpirationAsDate

getExpirationAsDate(expiration): Date

Converts the provided cookie expiration to a Date instance.

Parameters

NameTypeDescription
expirationstring | number | DateCookie expiration in seconds from now, or as a string compatible with the Date constructor.

Returns

Date

Cookie expiration as a Date instance.

Defined in

packages/core/src/storage/CookieStorage.ts:372


has

has(name): boolean

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

Parameters

NameTypeDescription
namestringThe key identifying the storage entry.

Returns

boolean

true if the storage entry exists.

Overrides

Storage.has

Defined in

packages/core/src/storage/CookieStorage.ts:136


init

init(options?, transformFunction?): CookieStorage

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

NameType
optionsCookieOptions
transformFunctionObject

Returns

CookieStorage

This storage.

Overrides

Storage.init

Defined in

packages/core/src/storage/CookieStorage.ts:122


keys

keys(): Iterable<string>

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

Defined in

packages/core/src/storage/CookieStorage.ts:222


parse

parse(): void

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

Defined in

packages/core/src/storage/CookieStorage.ts:284


parseFromSetCookieHeader

parseFromSetCookieHeader(setCookieHeader): void

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

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

NameTypeDescription
setCookieHeaderstringThe value of the Set-Cookie HTTP header.

Returns

void

Defined in

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


recomputeCookieMaxAgeAndExpires

recomputeCookieMaxAgeAndExpires(options): void

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

Parameters

NameTypeDescription
optionsCookieOptionsCookie 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

Defined in

packages/core/src/storage/CookieStorage.ts:350


sanitizeCookieValue

sanitizeCookieValue(value): string

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

NameTypeDescription
valueundefined | string | number | boolean | DateCookie value

Returns

string

Sanitized value

Defined in

packages/core/src/storage/CookieStorage.ts:308


set

set(name, value, options?): CookieStorage

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

NameTypeDescription
namestringThe key identifying the storage entry.
valueundefined | string | number | boolean | DateThe storage entry value.
optionsCookieOptionsThe 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

CookieStorage

This storage.

Overrides

Storage.set

Defined in

packages/core/src/storage/CookieStorage.ts:163


size

size(): number

Returns the number of entries in this storage.

Returns

number

The number of entries in this storage.

Overrides

Storage.size

Defined in

packages/core/src/storage/CookieStorage.ts:231