|
| 1 | +import { cookie, deleteCookie } from '../helpers'; |
| 2 | + |
| 3 | + |
| 4 | +/** |
| 5 | + * Cookie storage interface for reading and writing cookies. |
| 6 | + */ |
| 7 | +export interface CookieStorage { |
| 8 | + /** |
| 9 | + * Get the value of a cookie |
| 10 | + * |
| 11 | + * @param name - The name of the cookie |
| 12 | + * @returns The cookie value |
| 13 | + */ |
| 14 | + getCookie(name: string): string; |
| 15 | + |
| 16 | + /** |
| 17 | + * Set a cookie |
| 18 | + * |
| 19 | + * @param name - The cookie name (required) |
| 20 | + * @param value - The cookie value |
| 21 | + * @param ttl - The cookie Time To Live (seconds) |
| 22 | + * @param path - The cookies path |
| 23 | + * @param domain - The cookies domain |
| 24 | + * @param samesite - The cookies samesite attribute |
| 25 | + * @param secure - Boolean to specify if cookie should be secure |
| 26 | + * @returns true if the cookie was set, false otherwise |
| 27 | + */ |
| 28 | + setCookie( |
| 29 | + name: string, |
| 30 | + value?: string, |
| 31 | + ttl?: number, |
| 32 | + path?: string, |
| 33 | + domain?: string, |
| 34 | + samesite?: string, |
| 35 | + secure?: boolean |
| 36 | + ): boolean; |
| 37 | + |
| 38 | + /** |
| 39 | + * Delete a cookie |
| 40 | + * |
| 41 | + * @param name - The cookie name |
| 42 | + * @param domainName - The cookie domain name |
| 43 | + * @param sameSite - The cookie same site attribute |
| 44 | + * @param secure - Boolean to specify if cookie should be secure |
| 45 | + */ |
| 46 | + deleteCookie(name: string, path?: string, domainName?: string, sameSite?: string, secure?: boolean): void; |
| 47 | +} |
| 48 | + |
| 49 | +export interface AsyncCookieStorage extends CookieStorage { |
| 50 | + /** |
| 51 | + * Clear the cookie storage cache (does not delete any cookies) |
| 52 | + */ |
| 53 | + clearCache(): void; |
| 54 | + |
| 55 | + /** |
| 56 | + * Write all pending cookies. |
| 57 | + */ |
| 58 | + flush(): void; |
| 59 | +} |
| 60 | + |
| 61 | +interface Cookie { |
| 62 | + getValue: () => string; |
| 63 | + setValue: (value?: string, ttl?: number, path?: string, domain?: string, samesite?: string, secure?: boolean) => boolean; |
| 64 | + deleteValue: (path?: string, domainName?: string, sameSite?: string, secure?: boolean) => void; |
| 65 | + flush: () => void; |
| 66 | +} |
| 67 | + |
| 68 | +function newCookie(name: string): Cookie { |
| 69 | + let flushTimer: ReturnType<typeof setTimeout> | undefined; |
| 70 | + let lastSetValueArgs: Parameters<typeof setValue> | undefined; |
| 71 | + let cacheExpireAt: Date | undefined; |
| 72 | + let flushed = true; |
| 73 | + const flushTimeout = 10; // milliseconds |
| 74 | + const maxCacheTtl = 0.05; // seconds |
| 75 | + |
| 76 | + function getValue(): string { |
| 77 | + // Note: we can't cache the cookie value as we don't know the expiration date |
| 78 | + if (lastSetValueArgs && (!cacheExpireAt || cacheExpireAt > new Date())) { |
| 79 | + return lastSetValueArgs[0] ?? cookie(name); |
| 80 | + } |
| 81 | + return cookie(name); |
| 82 | + } |
| 83 | + |
| 84 | + function setValue(value?: string, ttl?: number, path?: string, domain?: string, samesite?: string, secure?: boolean): boolean { |
| 85 | + lastSetValueArgs = [value, ttl, path, domain, samesite, secure]; |
| 86 | + flushed = false; |
| 87 | + |
| 88 | + // throttle setting the cookie |
| 89 | + if (flushTimer === undefined) { |
| 90 | + flushTimer = setTimeout(() => { |
| 91 | + flushTimer = undefined; |
| 92 | + flush(); |
| 93 | + }, flushTimeout); |
| 94 | + } |
| 95 | + |
| 96 | + cacheExpireAt = new Date(Date.now() + Math.min(maxCacheTtl, ttl ?? maxCacheTtl) * 1000); |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + function deleteValue(path?: string, domainName?: string, sameSite?: string, secure?: boolean): void { |
| 101 | + lastSetValueArgs = undefined; |
| 102 | + flushed = true; |
| 103 | + |
| 104 | + // cancel setting the cookie |
| 105 | + if (flushTimer !== undefined) { |
| 106 | + clearTimeout(flushTimer); |
| 107 | + flushTimer = undefined; |
| 108 | + } |
| 109 | + |
| 110 | + deleteCookie(name, path, domainName, sameSite, secure); |
| 111 | + } |
| 112 | + |
| 113 | + function flush(): void { |
| 114 | + if (flushTimer !== undefined) { |
| 115 | + clearTimeout(flushTimer); |
| 116 | + flushTimer = undefined; |
| 117 | + } |
| 118 | + |
| 119 | + if (flushed) { |
| 120 | + return; |
| 121 | + } |
| 122 | + flushed = true; |
| 123 | + |
| 124 | + if (lastSetValueArgs !== undefined) { |
| 125 | + const [value, ttl, path, domain, samesite, secure] = lastSetValueArgs; |
| 126 | + cookie(name, value, ttl, path, domain, samesite, secure); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return { |
| 131 | + getValue, |
| 132 | + setValue, |
| 133 | + deleteValue, |
| 134 | + flush, |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Create a new async cookie storage |
| 140 | + * |
| 141 | + * @returns A new cookie storage |
| 142 | + */ |
| 143 | +export function newCookieStorage(): AsyncCookieStorage { |
| 144 | + let cache: Record<string, Cookie> = {}; |
| 145 | + |
| 146 | + function getOrInitCookie(name: string): Cookie { |
| 147 | + if (!cache[name]) { |
| 148 | + cache[name] = newCookie(name); |
| 149 | + } |
| 150 | + return cache[name]; |
| 151 | + } |
| 152 | + |
| 153 | + function getCookie(name: string): string { |
| 154 | + return getOrInitCookie(name).getValue(); |
| 155 | + } |
| 156 | + |
| 157 | + function setCookie( |
| 158 | + name: string, |
| 159 | + value?: string, |
| 160 | + ttl?: number, |
| 161 | + path?: string, |
| 162 | + domain?: string, |
| 163 | + samesite?: string, |
| 164 | + secure?: boolean |
| 165 | + ): boolean { |
| 166 | + return getOrInitCookie(name).setValue(value, ttl, path, domain, samesite, secure); |
| 167 | + } |
| 168 | + |
| 169 | + function deleteCookie(name: string, path?: string, domainName?: string, sameSite?: string, secure?: boolean): void { |
| 170 | + getOrInitCookie(name).deleteValue(path, domainName, sameSite, secure); |
| 171 | + } |
| 172 | + |
| 173 | + function clearCache(): void { |
| 174 | + cache = {}; |
| 175 | + } |
| 176 | + |
| 177 | + function flush(): void { |
| 178 | + for (const cookie of Object.values(cache)) { |
| 179 | + cookie.flush(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return { |
| 184 | + getCookie, |
| 185 | + setCookie, |
| 186 | + deleteCookie, |
| 187 | + clearCache, |
| 188 | + flush, |
| 189 | + }; |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * Cookie storage instance with asynchronous cookie writes |
| 194 | + */ |
| 195 | +export const asyncCookieStorage = newCookieStorage(); |
| 196 | + |
| 197 | +/** |
| 198 | + * Cookie storage instance with synchronous cookie writes |
| 199 | + */ |
| 200 | +export const syncCookieStorage: CookieStorage = { |
| 201 | + getCookie: cookie, |
| 202 | + setCookie: (name, value, ttl, path, domain, samesite, secure) => { |
| 203 | + cookie(name, value, ttl, path, domain, samesite, secure); |
| 204 | + return document.cookie.indexOf(`${name}=`) !== -1; |
| 205 | + }, |
| 206 | + deleteCookie |
| 207 | +}; |
0 commit comments