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