@cedx/cookies
Advanced tools
Comparing version 8.0.1 to 8.1.0
/** | ||
* An event dispatched when the cookie store has been changed. | ||
*/ | ||
export class CookieEvent extends Event { | ||
/** | ||
* The event type. | ||
*/ | ||
static readonly type: string; | ||
/** | ||
* The changed key. | ||
*/ | ||
key: string; | ||
/** | ||
* The new value. | ||
*/ | ||
newValue: string|null; | ||
/** | ||
* The original value. | ||
*/ | ||
oldValue: string|null; | ||
/** | ||
* Creates a new cookie event. | ||
* @param key The changed key. | ||
* @param oldValue The original value. | ||
* @param newValue The new value. | ||
*/ | ||
constructor(key: string, oldValue?: string|null, newValue?: string|null); | ||
export declare class CookieEvent extends Event { | ||
/** | ||
* The event type. | ||
*/ | ||
static readonly type = "cookie:change"; | ||
/** | ||
* The changed key. | ||
*/ | ||
readonly key: string; | ||
/** | ||
* The new value. | ||
*/ | ||
readonly newValue: string | null; | ||
/** | ||
* The original value. | ||
*/ | ||
readonly oldValue: string | null; | ||
/** | ||
* Creates a new cookie event. | ||
* @param key The changed key. | ||
* @param oldValue The original value. | ||
* @param newValue The new value. | ||
*/ | ||
constructor(key: string, oldValue?: string | null, newValue?: string | null); | ||
} | ||
//# sourceMappingURL=cookie_event.d.ts.map |
@@ -1,22 +0,33 @@ | ||
// An event dispatched when the cookie store has been changed. | ||
export var CookieEvent = (function() { | ||
class CookieEvent extends Event { | ||
// Creates a new storage event. | ||
/** | ||
* An event dispatched when the cookie store has been changed. | ||
*/ | ||
export class CookieEvent extends Event { | ||
/** | ||
* The event type. | ||
*/ | ||
static type = "cookie:change"; | ||
/** | ||
* The changed key. | ||
*/ | ||
key; | ||
/** | ||
* The new value. | ||
*/ | ||
newValue; | ||
/** | ||
* The original value. | ||
*/ | ||
oldValue; | ||
/** | ||
* Creates a new cookie event. | ||
* @param key The changed key. | ||
* @param oldValue The original value. | ||
* @param newValue The new value. | ||
*/ | ||
constructor(key, oldValue = null, newValue = null) { | ||
super(CookieEvent.type); | ||
// The changed key. | ||
this.key = key; | ||
// The new value. | ||
this.newValue = newValue; | ||
// The original value. | ||
this.oldValue = oldValue; | ||
super(CookieEvent.type); | ||
this.key = key; | ||
this.newValue = newValue; | ||
this.oldValue = oldValue; | ||
} | ||
}; | ||
// The event type. | ||
CookieEvent.type = "cookie:change"; | ||
return CookieEvent; | ||
}).call(this); | ||
} |
@@ -1,51 +0,41 @@ | ||
import {SameSite} from "./same_site.js"; | ||
import type { SameSite } from "./same_site.js"; | ||
/** | ||
* Defines the attributes of a HTTP cookie. | ||
*/ | ||
export class CookieOptions { | ||
/** | ||
* The domain for which the cookie is valid. | ||
*/ | ||
domain: string; | ||
/** | ||
* The expiration date and time for the cookie. | ||
*/ | ||
expires: Date|null; | ||
/** | ||
* The maximum duration, in seconds, until the cookie expires. | ||
*/ | ||
maxAge: number; | ||
/** | ||
* The path to which the cookie applies. | ||
*/ | ||
path: string; | ||
/** | ||
* The cross-site requests policy. | ||
*/ | ||
sameSite: SameSite|null; | ||
/** | ||
* Value indicating whether to transmit the cookie over HTTPS only. | ||
*/ | ||
secure: boolean; | ||
/** | ||
* Creates new cookie options. | ||
* @param options An object providing values to initialize this instance. | ||
*/ | ||
constructor(options?: CookieOptionsParams); | ||
/** | ||
* Returns a string representation of this object. | ||
* @returns The string representation of this object. | ||
*/ | ||
toString(): string; | ||
export declare class CookieOptions { | ||
/** | ||
* The domain for which the cookie is valid. | ||
*/ | ||
domain: string; | ||
/** | ||
* The expiration date and time for the cookie. | ||
*/ | ||
expires: Date | null; | ||
/** | ||
* The maximum duration, in seconds, until the cookie expires. | ||
*/ | ||
maxAge: number; | ||
/** | ||
* The path to which the cookie applies. | ||
*/ | ||
path: string; | ||
/** | ||
* The cross-site requests policy. | ||
*/ | ||
sameSite: SameSite | null; | ||
/** | ||
* Value indicating whether to transmit the cookie over HTTPS only. | ||
*/ | ||
secure: boolean; | ||
/** | ||
* Creates new cookie options. | ||
* @param options An object providing values to initialize this instance. | ||
*/ | ||
constructor(options?: CookieOptionsParams); | ||
/** | ||
* Returns a string representation of this object. | ||
* @returns The string representation of this object. | ||
*/ | ||
toString(): string; | ||
} | ||
/** | ||
@@ -55,32 +45,27 @@ * Defines the parameters of a {@link CookieOptions} instance. | ||
export type CookieOptionsParams = Partial<{ | ||
/** | ||
* The domain for which the cookie is valid. | ||
*/ | ||
domain: string; | ||
/** | ||
* The expiration date and time for the cookie. | ||
*/ | ||
expires: Date|null; | ||
/** | ||
* The maximum duration, in seconds, until the cookie expires. | ||
*/ | ||
maxAge: number; | ||
/** | ||
* The path to which the cookie applies. | ||
*/ | ||
path: string; | ||
/** | ||
* The cross-site requests policy. | ||
*/ | ||
sameSite: SameSite|null; | ||
/** | ||
* Value indicating whether to transmit the cookie over HTTPS only. | ||
*/ | ||
secure: boolean; | ||
/** | ||
* The domain for which the cookie is valid. | ||
*/ | ||
domain: string; | ||
/** | ||
* The expiration date and time for the cookie. | ||
*/ | ||
expires: Date | null; | ||
/** | ||
* The maximum duration, in seconds, until the cookie expires. | ||
*/ | ||
maxAge: number; | ||
/** | ||
* The path to which the cookie applies. | ||
*/ | ||
path: string; | ||
/** | ||
* The cross-site requests policy. | ||
*/ | ||
sameSite: SameSite | null; | ||
/** | ||
* Value indicating whether to transmit the cookie over HTTPS only. | ||
*/ | ||
secure: boolean; | ||
}>; | ||
//# sourceMappingURL=cookie_options.d.ts.map |
@@ -1,45 +0,61 @@ | ||
// Defines the attributes of a HTTP cookie. | ||
export var CookieOptions = class CookieOptions { | ||
// Creates new cookie options. | ||
constructor(options = {}) { | ||
var ref, ref1, ref2, ref3, ref4, ref5; | ||
// The domain for which the cookie is valid. | ||
this.domain = (ref = options.domain) != null ? ref : ""; | ||
// The expiration date and time for the cookie. | ||
this.expires = (ref1 = options.expires) != null ? ref1 : null; | ||
// The maximum duration, in seconds, until the cookie expires. | ||
this.maxAge = (ref2 = options.maxAge) != null ? ref2 : -1; | ||
// The path to which the cookie applies. | ||
this.path = (ref3 = options.path) != null ? ref3 : ""; | ||
// The cross-site requests policy. | ||
this.sameSite = (ref4 = options.sameSite) != null ? ref4 : null; | ||
// Value indicating whether to transmit the cookie over HTTPS only. | ||
this.secure = (ref5 = options.secure) != null ? ref5 : false; | ||
} | ||
// Returns a string representation of this object. | ||
toString() { | ||
var value; | ||
value = []; | ||
if (this.domain) { | ||
value.push(`domain=${this.domain}`); | ||
/** | ||
* Defines the attributes of a HTTP cookie. | ||
*/ | ||
export class CookieOptions { | ||
/** | ||
* The domain for which the cookie is valid. | ||
*/ | ||
domain; | ||
/** | ||
* The expiration date and time for the cookie. | ||
*/ | ||
expires; | ||
/** | ||
* The maximum duration, in seconds, until the cookie expires. | ||
*/ | ||
maxAge; | ||
/** | ||
* The path to which the cookie applies. | ||
*/ | ||
path; | ||
/** | ||
* The cross-site requests policy. | ||
*/ | ||
sameSite; | ||
/** | ||
* Value indicating whether to transmit the cookie over HTTPS only. | ||
*/ | ||
secure; | ||
/** | ||
* Creates new cookie options. | ||
* @param options An object providing values to initialize this instance. | ||
*/ | ||
constructor(options = {}) { | ||
this.domain = options.domain ?? ""; | ||
this.expires = options.expires ?? null; | ||
this.maxAge = options.maxAge ?? -1; | ||
this.path = options.path ?? ""; | ||
this.sameSite = options.sameSite ?? null; | ||
this.secure = options.secure ?? false; | ||
} | ||
if (this.expires) { | ||
value.push(`expires=${this.expires.toUTCString()}`); | ||
/** | ||
* Returns a string representation of this object. | ||
* @returns The string representation of this object. | ||
*/ | ||
toString() { | ||
const value = []; | ||
if (this.domain) | ||
value.push(`domain=${this.domain}`); | ||
if (this.expires) | ||
value.push(`expires=${this.expires.toUTCString()}`); | ||
if (this.maxAge >= 0) | ||
value.push(`max-age=${this.maxAge}`); | ||
if (this.path) | ||
value.push(`path=${this.path}`); | ||
if (this.sameSite) | ||
value.push(`samesite=${this.sameSite}`); | ||
if (this.secure) | ||
value.push("secure"); | ||
return value.join("; "); | ||
} | ||
if (this.maxAge >= 0) { | ||
value.push(`max-age=${this.maxAge}`); | ||
} | ||
if (this.path) { | ||
value.push(`path=${this.path}`); | ||
} | ||
if (this.sameSite) { | ||
value.push(`samesite=${this.sameSite}`); | ||
} | ||
if (this.secure) { | ||
value.push("secure"); | ||
} | ||
return value.join("; "); | ||
} | ||
}; | ||
} |
@@ -1,115 +0,98 @@ | ||
import {CookieEvent} from "./cookie_event.js"; | ||
import {CookieOptions, CookieOptionsParams} from "./cookie_options.js"; | ||
import { CookieEvent } from "./cookie_event.js"; | ||
import { CookieOptions, type CookieOptionsParams } from "./cookie_options.js"; | ||
/** | ||
* Provides access to the [HTTP Cookies](https://developer.mozilla.org/docs/Web/HTTP/Cookies). | ||
*/ | ||
export class CookieStore extends EventTarget { | ||
/** | ||
* The map of all cookies. | ||
*/ | ||
static readonly all: Map<string, string>; | ||
/** | ||
* The default cookie options. | ||
*/ | ||
defaults: CookieOptions; | ||
/** | ||
* The keys of this cookie store. | ||
*/ | ||
readonly keys: Set<string>; | ||
/** | ||
* The number of entries in this cookie store. | ||
*/ | ||
readonly length: number; | ||
/** | ||
* Creates a new cookie store. | ||
* @param options An object providing values to initialize this instance. | ||
*/ | ||
constructor(options?: CookieStoreOptions); | ||
/** | ||
* Returns a new iterator that allows iterating the entries of this cookie store. | ||
* @returns An iterator for the entries of this cookie store. | ||
*/ | ||
[Symbol.iterator](): IterableIterator<[string, string]>; | ||
/** | ||
* Removes all entries from this cookie store. | ||
* @param options The cookie options. | ||
*/ | ||
clear(options?: CookieOptionsParams): void; | ||
/** | ||
* Removes the value associated with the specified key. | ||
* @param key The cookie name. | ||
* @param options The cookie options. | ||
* @returns The value associated with the key before it was removed. | ||
*/ | ||
delete(key: string, options?: CookieOptionsParams): string|null; | ||
/** | ||
* Gets the value associated to the specified key. | ||
* @param key The cookie name. | ||
* @returns The cookie value, or `null` if the key does not exist. | ||
*/ | ||
get(key: string): string|null; | ||
/** | ||
* Gets the deserialized value associated with the specified key. | ||
* @param key The cookie name. | ||
* @returns The cookie value, or `null` if the key does not exist or the value cannot be deserialized. | ||
*/ | ||
getObject<T>(key: string): T|null; | ||
/** | ||
* Gets a value indicating whether this cookie store contains the specified key. | ||
* @param key The cookie name. | ||
* @returns `true` if this cookie store contains the specified key, otherwise `false`. | ||
*/ | ||
has(key: string): boolean; | ||
/** | ||
* Registers a function that will be invoked whenever the `change` event is triggered. | ||
* @param listener The event handler to register. | ||
* @returns This instance. | ||
*/ | ||
onChange(listener: (event: CookieEvent) => void): this; | ||
/** | ||
* Associates a given value with the specified key. | ||
* @param key The cookie name. | ||
* @param value The cookie value. | ||
* @param options The cookie options. | ||
* @returns This instance. | ||
* @throws `Error` if the cookie name is invalid. | ||
*/ | ||
set(key: string, value: string, options?: CookieOptionsParams): this; | ||
/** | ||
* Serializes and associates a given `value` with the specified `key`. | ||
* @param key The cookie name. | ||
* @param value The cookie value. | ||
* @param options The cookie options. | ||
* @returns This instance. | ||
*/ | ||
setObject(key: string, value: unknown, options?: CookieOptionsParams): this; | ||
/** | ||
* Returns a JSON representation of this object. | ||
* @returns The JSON representation of this object. | ||
*/ | ||
toJSON(): Array<[string, string]>; | ||
/** | ||
* Returns a string representation of this object. | ||
* @returns The string representation of this object. | ||
*/ | ||
toString(): string; | ||
export declare class CookieStore extends EventTarget { | ||
#private; | ||
/** | ||
* The default cookie options. | ||
*/ | ||
readonly defaults: CookieOptions; | ||
/** | ||
* Creates a new cookie store. | ||
* @param options An object providing values to initialize this instance. | ||
*/ | ||
constructor(options?: CookieStoreOptions); | ||
/** | ||
* The map of all cookies. | ||
*/ | ||
static get all(): Map<string, string>; | ||
/** | ||
* The keys of this cookie store. | ||
*/ | ||
get keys(): Set<string>; | ||
/** | ||
* The number of entries in this cookie store. | ||
*/ | ||
get length(): number; | ||
/** | ||
* Returns a new iterator that allows iterating the entries of this cookie store. | ||
* @returns An iterator for the entries of this cookie store. | ||
*/ | ||
[Symbol.iterator](): IterableIterator<[string, string]>; | ||
/** | ||
* Removes all entries from this cookie store. | ||
* @param options The cookie options. | ||
*/ | ||
clear(options?: CookieOptionsParams): void; | ||
/** | ||
* Removes the value associated with the specified key. | ||
* @param key The cookie name. | ||
* @param options The cookie options. | ||
* @returns The value associated with the key before it was removed. | ||
*/ | ||
delete(key: string, options?: CookieOptionsParams): string | null; | ||
/** | ||
* Gets the value associated to the specified key. | ||
* @param key The cookie name. | ||
* @returns The cookie value, or `null` if the key does not exist. | ||
*/ | ||
get(key: string): string | null; | ||
/** | ||
* Gets the deserialized value associated with the specified key. | ||
* @param key The cookie name. | ||
* @returns The cookie value, or `null` if the key does not exist or the value cannot be deserialized. | ||
*/ | ||
getObject<T>(key: string): T | null; | ||
/** | ||
* Gets a value indicating whether this cookie store contains the specified key. | ||
* @param key The cookie name. | ||
* @returns `true` if this cookie store contains the specified key, otherwise `false`. | ||
*/ | ||
has(key: string): boolean; | ||
/** | ||
* Registers a function that will be invoked whenever the `change` event is triggered. | ||
* @param listener The event handler to register. | ||
* @returns This instance. | ||
*/ | ||
onChange(listener: (event: CookieEvent) => void): this; | ||
/** | ||
* Associates a given value with the specified key. | ||
* @param key The cookie name. | ||
* @param value The cookie value. | ||
* @param options The cookie options. | ||
* @returns This instance. | ||
* @throws `Error` if the cookie name is invalid. | ||
*/ | ||
set(key: string, value: string, options?: CookieOptionsParams): this; | ||
/** | ||
* Serializes and associates a given `value` with the specified `key`. | ||
* @param key The cookie name. | ||
* @param value The cookie value. | ||
* @param options The cookie options. | ||
* @returns This instance. | ||
*/ | ||
setObject(key: string, value: unknown, options?: CookieOptionsParams): this; | ||
/** | ||
* Returns a JSON representation of this object. | ||
* @returns The JSON representation of this object. | ||
*/ | ||
toJSON(): Array<[string, string]>; | ||
/** | ||
* Returns a string representation of this object. | ||
* @returns The string representation of this object. | ||
*/ | ||
toString(): string; | ||
} | ||
/** | ||
@@ -119,12 +102,11 @@ * Defines the options of a {@link CookieStore} instance. | ||
export type CookieStoreOptions = Partial<{ | ||
/** | ||
* The default cookie options. | ||
*/ | ||
defaults: CookieOptionsParams; | ||
/** | ||
* A string prefixed to every key so that it is unique globally in the whole cookie store. | ||
*/ | ||
keyPrefix: string; | ||
/** | ||
* The default cookie options. | ||
*/ | ||
defaults: CookieOptionsParams; | ||
/** | ||
* A string prefixed to every key so that it is unique globally in the whole cookie store. | ||
*/ | ||
keyPrefix: string; | ||
}>; | ||
//# sourceMappingURL=cookie_store.d.ts.map |
@@ -1,184 +0,186 @@ | ||
import { | ||
CookieEvent | ||
} from "./cookie_event.js"; | ||
import { | ||
CookieOptions | ||
} from "./cookie_options.js"; | ||
export var CookieStore = (function() { | ||
// Provides access to the [HTTP Cookies](https://developer.mozilla.org/docs/Web/HTTP/Cookies). | ||
class CookieStore extends EventTarget { | ||
// Creates a new cookie store. | ||
import { CookieEvent } from "./cookie_event.js"; | ||
import { CookieOptions } from "./cookie_options.js"; | ||
/** | ||
* Provides access to the [HTTP Cookies](https://developer.mozilla.org/docs/Web/HTTP/Cookies). | ||
*/ | ||
export class CookieStore extends EventTarget { | ||
/** | ||
* The default cookie options. | ||
*/ | ||
defaults; | ||
/** | ||
* A string prefixed to every key so that it is unique globally in the whole cookie store. | ||
*/ | ||
#keyPrefix; | ||
/** | ||
* Creates a new cookie store. | ||
* @param options An object providing values to initialize this instance. | ||
*/ | ||
constructor(options = {}) { | ||
var ref; | ||
super(); | ||
// The default cookie options. | ||
this.defaults = new CookieOptions(options.defaults); | ||
// A string prefixed to every key so that it is unique globally in the whole cookie store. | ||
this._keyPrefix = (ref = options.keyPrefix) != null ? ref : ""; | ||
super(); | ||
this.defaults = new CookieOptions(options.defaults ?? {}); | ||
this.#keyPrefix = options.keyPrefix ?? ""; | ||
} | ||
// Returns a new iterator that allows iterating the entries of this cookie store. | ||
* [Symbol.iterator]() { | ||
var key, ref; | ||
ref = this.keys; | ||
for (key of ref) { | ||
yield [key, this.get(key)]; | ||
} | ||
/** | ||
* The map of all cookies. | ||
*/ | ||
static get all() { | ||
const map = new Map; | ||
if (document.cookie) | ||
for (const item of document.cookie.split(";")) { | ||
const parts = item.trimStart().split("="); | ||
if (parts.length >= 2) | ||
map.set(parts[0], decodeURIComponent(parts.slice(1).join("="))); | ||
} | ||
return map; | ||
} | ||
// Removes all entries from this cookie store. | ||
/** | ||
* The keys of this cookie store. | ||
*/ | ||
get keys() { | ||
const keys = Array.from(CookieStore.all.keys()); | ||
return new Set(this.#keyPrefix ? keys.filter(key => key.startsWith(this.#keyPrefix)).map(key => key.slice(this.#keyPrefix.length)) : keys); | ||
} | ||
/** | ||
* The number of entries in this cookie store. | ||
*/ | ||
get length() { | ||
return this.keys.size; | ||
} | ||
/** | ||
* Returns a new iterator that allows iterating the entries of this cookie store. | ||
* @returns An iterator for the entries of this cookie store. | ||
*/ | ||
*[Symbol.iterator]() { | ||
for (const key of this.keys) | ||
yield [key, this.get(key)]; | ||
} | ||
/** | ||
* Removes all entries from this cookie store. | ||
* @param options The cookie options. | ||
*/ | ||
clear(options = {}) { | ||
var key, ref, results; | ||
ref = this.keys; | ||
results = []; | ||
for (key of ref) { | ||
results.push(this.delete(key, options)); | ||
} | ||
return results; | ||
for (const key of this.keys) | ||
this.delete(key, options); | ||
} | ||
// Removes the value associated with the specified key. | ||
/** | ||
* Removes the value associated with the specified key. | ||
* @param key The cookie name. | ||
* @param options The cookie options. | ||
* @returns The value associated with the key before it was removed. | ||
*/ | ||
delete(key, options = {}) { | ||
var cookieOptions, oldValue; | ||
oldValue = this.get(key); | ||
cookieOptions = this._getOptions(options); | ||
cookieOptions.expires = new Date(0); | ||
cookieOptions.maxAge = 0; | ||
document.cookie = `${this._buildKey(key)}=; ${cookieOptions}`; | ||
this.dispatchEvent(new CookieEvent(key, oldValue)); | ||
return oldValue; | ||
const oldValue = this.get(key); | ||
const cookieOptions = this.#getOptions(options); | ||
cookieOptions.expires = new Date(0); | ||
cookieOptions.maxAge = 0; | ||
document.cookie = `${this.#buildKey(key)}=; ${cookieOptions}`; | ||
this.dispatchEvent(new CookieEvent(key, oldValue)); | ||
return oldValue; | ||
} | ||
// Gets the value associated to the specified key. | ||
/** | ||
* Gets the value associated to the specified key. | ||
* @param key The cookie name. | ||
* @returns The cookie value, or `null` if the key does not exist. | ||
*/ | ||
get(key) { | ||
var ref; | ||
return (ref = CookieStore.all.get(this._buildKey(key))) != null ? ref : null; | ||
return CookieStore.all.get(this.#buildKey(key)) ?? null; | ||
} | ||
// Gets the deserialized value associated with the specified key. | ||
/** | ||
* Gets the deserialized value associated with the specified key. | ||
* @param key The cookie name. | ||
* @returns The cookie value, or `null` if the key does not exist or the value cannot be deserialized. | ||
*/ | ||
getObject(key) { | ||
var ref; | ||
try { | ||
return JSON.parse((ref = this.get(key)) != null ? ref : ""); | ||
} catch (error) { | ||
return null; | ||
} | ||
try { | ||
return JSON.parse(this.get(key) ?? ""); | ||
} | ||
catch { | ||
return null; | ||
} | ||
} | ||
// Gets a value indicating whether this cookie store contains the specified key. | ||
/** | ||
* Gets a value indicating whether this cookie store contains the specified key. | ||
* @param key The cookie name. | ||
* @returns `true` if this cookie store contains the specified key, otherwise `false`. | ||
*/ | ||
has(key) { | ||
return CookieStore.all.has(this._buildKey(key)); | ||
return CookieStore.all.has(this.#buildKey(key)); | ||
} | ||
// Registers a function that will be invoked whenever the "change" event is triggered. | ||
/** | ||
* Registers a function that will be invoked whenever the `change` event is triggered. | ||
* @param listener The event handler to register. | ||
* @returns This instance. | ||
*/ | ||
onChange(listener) { | ||
this.addEventListener(CookieEvent.type, listener, { | ||
passive: true | ||
}); | ||
return this; | ||
this.addEventListener(CookieEvent.type, listener); | ||
return this; | ||
} | ||
// Associates a given value with the specified key. | ||
/** | ||
* Associates a given value with the specified key. | ||
* @param key The cookie name. | ||
* @param value The cookie value. | ||
* @param options The cookie options. | ||
* @returns This instance. | ||
* @throws `Error` if the cookie name is invalid. | ||
*/ | ||
set(key, value, options = {}) { | ||
var cookie, cookieOptions, oldValue; | ||
if (!key || key.includes("=" || key.includes(";"))) { | ||
throw Error("Invalid cookie name."); | ||
} | ||
cookie = `${this._buildKey(key)}=${encodeURIComponent(value)}`; | ||
cookieOptions = this._getOptions(options).toString(); | ||
if (cookieOptions) { | ||
cookie += `; ${cookieOptions}`; | ||
} | ||
oldValue = this.get(key); | ||
document.cookie = cookie; | ||
this.dispatchEvent(new CookieEvent(key, oldValue, value)); | ||
return this; | ||
if (!key || key.includes("=") || key.includes(";")) | ||
throw Error("Invalid cookie name."); | ||
let cookie = `${this.#buildKey(key)}=${encodeURIComponent(value)}`; | ||
const cookieOptions = this.#getOptions(options).toString(); | ||
if (cookieOptions) | ||
cookie += `; ${cookieOptions}`; | ||
const oldValue = this.get(key); | ||
document.cookie = cookie; | ||
this.dispatchEvent(new CookieEvent(key, oldValue, value)); | ||
return this; | ||
} | ||
// Serializes and associates a given value with the specified key. | ||
/** | ||
* Serializes and associates a given `value` with the specified `key`. | ||
* @param key The cookie name. | ||
* @param value The cookie value. | ||
* @param options The cookie options. | ||
* @returns This instance. | ||
*/ | ||
setObject(key, value, options = {}) { | ||
return this.set(key, JSON.stringify(value), options); | ||
return this.set(key, JSON.stringify(value), options); | ||
} | ||
// Returns a JSON representation of this object. | ||
/** | ||
* Returns a JSON representation of this object. | ||
* @returns The JSON representation of this object. | ||
*/ | ||
toJSON() { | ||
return Array.from(this); | ||
return Array.from(this); | ||
} | ||
// Returns a string representation of this object. | ||
/** | ||
* Returns a string representation of this object. | ||
* @returns The string representation of this object. | ||
*/ | ||
toString() { | ||
if (this._keyPrefix) { | ||
return Array.from(this).map(function([key, value]) { | ||
return `${key}=${encodeURIComponent(value)}`; | ||
}).join("; "); | ||
} else { | ||
return document.cookie; | ||
} | ||
return this.#keyPrefix ? Array.from(this).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join("; ") : document.cookie; | ||
} | ||
// Builds a normalized cookie key from the given key. | ||
_buildKey(key) { | ||
return `${this._keyPrefix}${key}`; | ||
/** | ||
* Builds a normalized cookie key from the given key. | ||
* @param key The original key. | ||
* @returns The normalized cookie key. | ||
*/ | ||
#buildKey(key) { | ||
return `${this.#keyPrefix}${key}`; | ||
} | ||
// Merges the default cookie options with the specified ones. | ||
_getOptions(options) { | ||
var ref, ref1, ref2, ref3, ref4, ref5; | ||
return new CookieOptions({ | ||
domain: (ref = options.domain) != null ? ref : this.defaults.domain, | ||
expires: (ref1 = options.expires) != null ? ref1 : this.defaults.expires, | ||
maxAge: (ref2 = options.maxAge) != null ? ref2 : this.defaults.maxAge, | ||
path: (ref3 = options.path) != null ? ref3 : this.defaults.path, | ||
sameSite: (ref4 = options.sameSite) != null ? ref4 : this.defaults.sameSite, | ||
secure: (ref5 = options.secure) != null ? ref5 : this.defaults.secure | ||
}); | ||
/** | ||
* Merges the default cookie options with the specified ones. | ||
* @param options Some cookie options. | ||
* @returns The merged cookie options. | ||
*/ | ||
#getOptions(options = {}) { | ||
return new CookieOptions({ | ||
domain: options.domain ?? this.defaults.domain, | ||
expires: options.expires ?? this.defaults.expires, | ||
maxAge: options.maxAge ?? this.defaults.maxAge, | ||
path: options.path ?? this.defaults.path, | ||
sameSite: options.sameSite ?? this.defaults.sameSite, | ||
secure: options.secure ?? this.defaults.secure | ||
}); | ||
} | ||
}; | ||
// The map of all cookies. | ||
Object.defineProperty(CookieStore, "all", { | ||
get: function() { | ||
var item, map, parts, ref; | ||
map = new Map(); | ||
if (document.cookie) { | ||
ref = document.cookie.split(";"); | ||
for (item of ref) { | ||
parts = item.trimStart().split("="); | ||
if (parts.length >= 2) { | ||
map.set(parts[0], decodeURIComponent(parts.slice(1).join("="))); | ||
} | ||
} | ||
} | ||
return map; | ||
} | ||
}); | ||
// The keys of this cookie store. | ||
Object.defineProperty(CookieStore.prototype, "keys", { | ||
get: function() { | ||
var keys, length; | ||
keys = Array.from(CookieStore.all.keys()); | ||
({length} = this._keyPrefix); | ||
return new Set(length ? keys.filter((key) => { | ||
return key.startsWith(this._keyPrefix); | ||
}).map(function(key) { | ||
return key.slice(length); | ||
}) : keys); | ||
} | ||
}); | ||
// The number of entries in this cookie store. | ||
Object.defineProperty(CookieStore.prototype, "length", { | ||
get: function() { | ||
return this.keys.size; | ||
} | ||
}); | ||
return CookieStore; | ||
}).call(this); | ||
} |
@@ -5,1 +5,2 @@ export * from "./cookie_event.js"; | ||
export * from "./same_site.js"; | ||
//# sourceMappingURL=index.d.ts.map |
export * from "./cookie_event.js"; | ||
export * from "./cookie_options.js"; | ||
export * from "./cookie_store.js"; | ||
export * from "./same_site.js"; |
/** | ||
* Defines the values of the `SameSite` cookie attribute. | ||
*/ | ||
export const SameSite: Readonly<{ | ||
/** | ||
* Only send cookies for top level navigation requests. | ||
*/ | ||
lax: "lax", | ||
/** | ||
* No restrictions on cross-site requests. | ||
*/ | ||
none: "none", | ||
/** | ||
* Prevents the cookie from being sent to the target site in all cross-site browsing context. | ||
*/ | ||
strict: "strict" | ||
export declare const SameSite: Readonly<{ | ||
/** | ||
* Only send cookies for top level navigation requests. | ||
*/ | ||
lax: "lax"; | ||
/** | ||
* No restrictions on cross-site requests. | ||
*/ | ||
none: "none"; | ||
/** | ||
* Prevents the cookie from being sent to the target site in all cross-site browsing context. | ||
*/ | ||
strict: "strict"; | ||
}>; | ||
/** | ||
@@ -26,1 +22,2 @@ * Defines the values of the `SameSite` cookie attribute. | ||
export type SameSite = typeof SameSite[keyof typeof SameSite]; | ||
//# sourceMappingURL=same_site.d.ts.map |
@@ -1,9 +0,17 @@ | ||
// Defines the values of the `SameSite` cookie attribute. | ||
export var SameSite = Object.freeze({ | ||
// Only send cookies for top level navigation requests. | ||
lax: "lax", | ||
// No restrictions on cross-site requests. | ||
none: "none", | ||
// Prevents the cookie from being sent to the target site in all cross-site browsing context. | ||
strict: "strict" | ||
/** | ||
* Defines the values of the `SameSite` cookie attribute. | ||
*/ | ||
export const SameSite = Object.freeze({ | ||
/** | ||
* Only send cookies for top level navigation requests. | ||
*/ | ||
lax: "lax", | ||
/** | ||
* No restrictions on cross-site requests. | ||
*/ | ||
none: "none", | ||
/** | ||
* Prevents the cookie from being sent to the target site in all cross-site browsing context. | ||
*/ | ||
strict: "strict" | ||
}); |
@@ -9,3 +9,3 @@ { | ||
"type": "module", | ||
"version": "8.0.1", | ||
"version": "8.1.0", | ||
"author": { | ||
@@ -17,14 +17,18 @@ "email": "cedric@belin.io", | ||
"devDependencies": { | ||
"@coffeelint/cli": "^5.2.11", | ||
"@rollup/plugin-node-resolve": "^15.3.0", | ||
"@types/chai": "^5.0.1", | ||
"@types/mocha": "^10.0.9", | ||
"@types/node": "^22.9.0", | ||
"@types/eslint__js": "^8.42.3", | ||
"@types/gulp": "^4.0.17", | ||
"@types/mocha": "^10.0.10", | ||
"@types/node": "^22.13.0", | ||
"@types/serve-handler": "^6.1.4", | ||
"chai": "^5.1.2", | ||
"coffeescript": "^2.7.0", | ||
"mocha": "^10.8.2", | ||
"puppeteer": "^23.7.1", | ||
"rollup": "^4.25.0", | ||
"serve-handler": "^6.1.6" | ||
"esbuild": "^0.24.2", | ||
"globals": "^15.14.0", | ||
"gulp": "^5.0.0", | ||
"mocha": "^11.1.0", | ||
"playwright": "^1.50.1", | ||
"serve-handler": "^6.1.6", | ||
"typedoc": "^0.27.6", | ||
"typescript": "^5.7.3", | ||
"typescript-eslint": "^8.22.0" | ||
}, | ||
@@ -51,5 +55,8 @@ "engines": { | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"test": "cake test" | ||
"test": "gulp test" | ||
} | ||
} |
@@ -9,2 +9,3 @@ # Cookies for JS | ||
- [User guide](https://github.com/cedx/cookies.js/wiki) | ||
- [API reference](https://cedx.github.io/cookies.js) | ||
- [Examples](https://github.com/cedx/cookies.js/tree/main/example) | ||
@@ -11,0 +12,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33398
24
876
18
16
1