ngx-cookie-service 22.0.0 → 22.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -6
- package/fesm2022/ngx-cookie-service.mjs +177 -211
- package/fesm2022/ngx-cookie-service.mjs.map +1 -1
- package/package.json +2 -2
- package/types/ngx-cookie-service.d.ts +202 -132
package/README.md
CHANGED
|
@@ -148,6 +148,9 @@ server.get('*', (req, res) => {
|
|
|
148
148
|
use `REQUEST.cookies` to access the
|
|
149
149
|
cookies in SSR. Then proceed to use `ngx-cookie-service` as usual.
|
|
150
150
|
4. See the [sample repo](https://github.com/pavankjadda/angular-ssr-docker) for more details.
|
|
151
|
+
5. On the server, `get()`, `check()` and `getAll()` also see cookies set or deleted earlier while rendering the same
|
|
152
|
+
request, whatever their `path` or `domain`. Cookies with `httpOnly: true` can only be set on the server; in the
|
|
153
|
+
browser the call is skipped with a console warning.
|
|
151
154
|
|
|
152
155
|
## Demo
|
|
153
156
|
|
|
@@ -212,17 +215,27 @@ to `Lax`.
|
|
|
212
215
|
Read [this](https://stackoverflow.com/a/1063760) and [this](https://stackoverflow.com/a/17777005/1007003) StackOverflow
|
|
213
216
|
answer for a more in-depth explanation.
|
|
214
217
|
|
|
215
|
-
**Important:** Browsers do not accept cookies flagged sameSite = 'None'
|
|
216
|
-
will override the secure flag to true if sameSite='None'.
|
|
218
|
+
**Important:** Browsers do not accept cookies flagged sameSite = 'None' or partitioned unless the secure flag is set as
|
|
219
|
+
well. CookieService will override the secure flag to true if sameSite='None' or partitioned is true.
|
|
220
|
+
|
|
221
|
+
If `path` or `domain` contains `;` or a control character, the cookie is not written or deleted, and a warning is
|
|
222
|
+
logged to the console. An invalid `expires` or `sameSite` value is ignored with a warning.
|
|
217
223
|
|
|
218
224
|
## delete( name: string, path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax'): void;
|
|
219
225
|
|
|
226
|
+
## delete( name: string, options?: { path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'None' | 'Strict', partitioned?: boolean }): void;
|
|
227
|
+
|
|
220
228
|
```typescript
|
|
221
229
|
cookieService.delete('test');
|
|
230
|
+
cookieService.delete('test', { path: '/', secure: true, sameSite: 'None', partitioned: true });
|
|
222
231
|
```
|
|
223
232
|
|
|
224
|
-
Deletes a cookie with the specified `name`.
|
|
225
|
-
|
|
233
|
+
Deletes a cookie with the specified `name`. The attributes must match the ones the cookie was set with: `path` and
|
|
234
|
+
`domain` select the cookie, and `partitioned: true` is needed to delete a Partitioned cookie.
|
|
235
|
+
|
|
236
|
+
It is best practice to always define a path. If you are unsure about the path value, use `'/'`. Without a path, the
|
|
237
|
+
browser uses the directory of the URL the page was loaded at, so in a single-page app the result depends on the URL the
|
|
238
|
+
user first landed on.
|
|
226
239
|
|
|
227
240
|
**Important:** For security reasons, it is not possible to delete cookies for other domains. Browsers do not allow this.
|
|
228
241
|
Read [this](https://stackoverflow.com/a/1063760) and [this](https://stackoverflow.com/a/17777005/1007003) StackOverflow
|
|
@@ -230,12 +243,15 @@ answer for a more in-depth explanation.
|
|
|
230
243
|
|
|
231
244
|
## deleteAll( path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax' ): void;
|
|
232
245
|
|
|
246
|
+
## deleteAll( options?: { path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'None' | 'Strict', partitioned?: boolean }): void;
|
|
247
|
+
|
|
233
248
|
```typescript
|
|
234
249
|
cookieService.deleteAll();
|
|
250
|
+
cookieService.deleteAll({ path: '/' });
|
|
235
251
|
```
|
|
236
252
|
|
|
237
|
-
Deletes all cookies that can currently be accessed
|
|
238
|
-
the path value, use `'/'`.
|
|
253
|
+
Deletes all cookies that can currently be accessed, using the same attributes for each one. It is best practice to
|
|
254
|
+
always define a path. If you are unsure about the path value, use `'/'`.
|
|
239
255
|
|
|
240
256
|
# FAQ
|
|
241
257
|
|
|
@@ -1,212 +1,178 @@
|
|
|
1
|
-
import { isPlatformBrowser } from
|
|
2
|
-
import * as i0 from
|
|
3
|
-
import {
|
|
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
|
-
* @param secure Is the Cookie secure
|
|
177
|
-
* @param sameSite Is the cookie same site
|
|
178
|
-
*
|
|
179
|
-
* @author: Stepan Suvorov
|
|
180
|
-
* @since: 1.0.0
|
|
181
|
-
*/
|
|
182
|
-
deleteAll(path, domain, secure, sameSite = 'Lax') {
|
|
183
|
-
if (!this.documentIsAccessible) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
const cookies = this.getAll();
|
|
187
|
-
for (const cookieName in cookies) {
|
|
188
|
-
if (cookies.hasOwnProperty(cookieName)) {
|
|
189
|
-
this.delete(cookieName, path, domain, secure, sameSite);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: CookieService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
194
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: CookieService, providedIn: 'root' }); }
|
|
195
|
-
}
|
|
196
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: CookieService, decorators: [{
|
|
197
|
-
type: Injectable,
|
|
198
|
-
args: [{
|
|
199
|
-
providedIn: 'root',
|
|
200
|
-
}]
|
|
201
|
-
}], ctorParameters: () => [] });
|
|
202
|
-
|
|
203
|
-
/*
|
|
204
|
-
* Public API Surface of ngx-cookie-service
|
|
205
|
-
*/
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Generated bundle index. Do not edit.
|
|
209
|
-
*/
|
|
210
|
-
|
|
1
|
+
import { isPlatformBrowser } from "@angular/common";
|
|
2
|
+
import * as i0 from "@angular/core";
|
|
3
|
+
import { DOCUMENT, Injectable, PLATFORM_ID, inject } from "@angular/core";
|
|
4
|
+
const SAME_SITE_VALUES = [
|
|
5
|
+
"Lax",
|
|
6
|
+
"Strict",
|
|
7
|
+
"None"
|
|
8
|
+
];
|
|
9
|
+
var CookieService = class CookieService {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.document = inject(DOCUMENT);
|
|
12
|
+
this.platformId = inject(PLATFORM_ID);
|
|
13
|
+
this.documentIsAccessible = isPlatformBrowser(this.platformId);
|
|
14
|
+
}
|
|
15
|
+
static getCookieRegExp(name) {
|
|
16
|
+
const escapedName = name.replace(/([[\]{}()|=;+?,.*^$\\])/gi, "\\$1");
|
|
17
|
+
return new RegExp("(?:^" + escapedName + "|;\\s*" + escapedName + ")=(.*?)(?:;|$)");
|
|
18
|
+
}
|
|
19
|
+
static safeDecodeURIComponent(encodedURIComponent) {
|
|
20
|
+
try {
|
|
21
|
+
return decodeURIComponent(encodedURIComponent);
|
|
22
|
+
} catch {
|
|
23
|
+
return encodedURIComponent;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
check(name) {
|
|
27
|
+
if (!this.documentIsAccessible) return false;
|
|
28
|
+
name = encodeURIComponent(name);
|
|
29
|
+
return CookieService.getCookieRegExp(name).test(this.document.cookie);
|
|
30
|
+
}
|
|
31
|
+
get(name) {
|
|
32
|
+
if (this.check(name)) {
|
|
33
|
+
name = encodeURIComponent(name);
|
|
34
|
+
const result = CookieService.getCookieRegExp(name).exec(this.document.cookie);
|
|
35
|
+
return result?.[1] ? CookieService.safeDecodeURIComponent(result[1]) : "";
|
|
36
|
+
} else return "";
|
|
37
|
+
}
|
|
38
|
+
getAll() {
|
|
39
|
+
if (!this.documentIsAccessible) return {};
|
|
40
|
+
const cookies = {};
|
|
41
|
+
const document = this.document;
|
|
42
|
+
if (document.cookie && document.cookie !== "") document.cookie.split(";").forEach((currentCookie) => {
|
|
43
|
+
const trimmedCookie = currentCookie.trim();
|
|
44
|
+
if (!trimmedCookie) return;
|
|
45
|
+
const equalIndex = trimmedCookie.indexOf("=");
|
|
46
|
+
if (equalIndex === -1) return;
|
|
47
|
+
const cookieName = CookieService.safeDecodeURIComponent(trimmedCookie.substring(0, equalIndex));
|
|
48
|
+
if (Object.hasOwn(cookies, cookieName)) return;
|
|
49
|
+
Object.defineProperty(cookies, cookieName, {
|
|
50
|
+
value: CookieService.safeDecodeURIComponent(trimmedCookie.substring(equalIndex + 1)),
|
|
51
|
+
enumerable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
configurable: true
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
return cookies;
|
|
57
|
+
}
|
|
58
|
+
set(name, value, expiresOrOptions, path, domain, secure, sameSite, partitioned) {
|
|
59
|
+
if (!this.documentIsAccessible) return;
|
|
60
|
+
if (typeof expiresOrOptions !== "object" || expiresOrOptions === null || expiresOrOptions instanceof Date) {
|
|
61
|
+
const optionsBody = {
|
|
62
|
+
expires: expiresOrOptions,
|
|
63
|
+
path,
|
|
64
|
+
domain,
|
|
65
|
+
secure,
|
|
66
|
+
sameSite: sameSite || "Lax",
|
|
67
|
+
partitioned
|
|
68
|
+
};
|
|
69
|
+
this.set(name, value, optionsBody);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const options = CookieService.validateOptions(name, expiresOrOptions);
|
|
73
|
+
if (!options) return;
|
|
74
|
+
let cookieString = encodeURIComponent(name) + "=" + encodeURIComponent(value) + ";";
|
|
75
|
+
if (options.expires) cookieString += "Expires=" + options.expires.toUTCString() + ";";
|
|
76
|
+
if (options.path) cookieString += "Path=" + options.path + ";";
|
|
77
|
+
if (options.domain) cookieString += "Domain=" + options.domain + ";";
|
|
78
|
+
if (options.sameSite === "None" && !options.secure) {
|
|
79
|
+
options.secure = true;
|
|
80
|
+
console.warn(`[ngx-cookie-service] Cookie ${name} was forced with secure flag because sameSite=None.More details : https://github.com/stevermeister/ngx-cookie-service/issues/86#issuecomment-597720130`);
|
|
81
|
+
}
|
|
82
|
+
if (options.partitioned && !options.secure) {
|
|
83
|
+
options.secure = true;
|
|
84
|
+
console.warn(`[ngx-cookie-service] Cookie ${name} was forced with secure flag because Partitioned cookies must be Secure.`);
|
|
85
|
+
}
|
|
86
|
+
if (options.secure) cookieString += "Secure;";
|
|
87
|
+
if (!options.sameSite) options.sameSite = "Lax";
|
|
88
|
+
cookieString += "SameSite=" + options.sameSite + ";";
|
|
89
|
+
if (options.partitioned) cookieString += "Partitioned;";
|
|
90
|
+
this.document.cookie = cookieString;
|
|
91
|
+
}
|
|
92
|
+
delete(name, pathOrOptions, domain, secure, sameSite = "Lax") {
|
|
93
|
+
if (!this.documentIsAccessible) return;
|
|
94
|
+
const options = CookieService.toDeleteOptions(pathOrOptions, domain, secure, sameSite);
|
|
95
|
+
this.set(name, "", {
|
|
96
|
+
...options,
|
|
97
|
+
expires: /* @__PURE__ */ new Date("Thu, 01 Jan 1970 00:00:01 GMT")
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
deleteAll(pathOrOptions, domain, secure, sameSite = "Lax") {
|
|
101
|
+
if (!this.documentIsAccessible) return;
|
|
102
|
+
for (const cookieName of Object.keys(this.getAll())) if (typeof pathOrOptions === "object" && pathOrOptions !== null) this.delete(cookieName, pathOrOptions);
|
|
103
|
+
else this.delete(cookieName, pathOrOptions, domain, secure, sameSite);
|
|
104
|
+
}
|
|
105
|
+
static validateOptions(name, options = {}) {
|
|
106
|
+
const { expires, ...valid } = options;
|
|
107
|
+
const result = valid;
|
|
108
|
+
for (const attribute of ["path", "domain"]) {
|
|
109
|
+
const attributeValue = result[attribute];
|
|
110
|
+
if (typeof attributeValue === "string" && CookieService.hasUnsafeCharacter(attributeValue)) {
|
|
111
|
+
console.warn(`[ngx-cookie-service] Cookie ${name} was skipped because its ${attribute} contains ";" or a control character.`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (expires) {
|
|
116
|
+
const date = typeof expires === "number" ? new Date(Date.now() + expires * 1e3 * 60 * 60 * 24) : expires;
|
|
117
|
+
if (date instanceof Date && Number.isFinite(date.getTime())) result.expires = date;
|
|
118
|
+
else console.warn(`[ngx-cookie-service] Cookie ${name}: ignoring invalid expires value "${String(expires)}".`);
|
|
119
|
+
}
|
|
120
|
+
if (result.sameSite !== void 0 && result.sameSite !== null) {
|
|
121
|
+
const sameSite = SAME_SITE_VALUES.find((value) => value.toLowerCase() === String(result.sameSite).toLowerCase());
|
|
122
|
+
if (sameSite) result.sameSite = sameSite;
|
|
123
|
+
else {
|
|
124
|
+
console.warn(`[ngx-cookie-service] Cookie ${name}: ignoring invalid sameSite value "${String(result.sameSite)}".`);
|
|
125
|
+
delete result.sameSite;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
static hasUnsafeCharacter(value) {
|
|
131
|
+
return [...value].some((character) => {
|
|
132
|
+
const code = character.charCodeAt(0);
|
|
133
|
+
return character === ";" || code < 32 || code === 127;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
static toDeleteOptions(pathOrOptions, domain, secure, sameSite) {
|
|
137
|
+
if (typeof pathOrOptions === "object" && pathOrOptions !== null) return { ...pathOrOptions };
|
|
138
|
+
return {
|
|
139
|
+
path: pathOrOptions,
|
|
140
|
+
domain,
|
|
141
|
+
secure,
|
|
142
|
+
sameSite
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
static {
|
|
146
|
+
this.ɵfac = i0.ɵɵngDeclareFactory({
|
|
147
|
+
minVersion: "12.0.0",
|
|
148
|
+
version: "22.2.2",
|
|
149
|
+
ngImport: i0,
|
|
150
|
+
type: CookieService,
|
|
151
|
+
deps: [],
|
|
152
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
static {
|
|
156
|
+
this.ɵprov = i0.ɵɵngDeclareInjectable({
|
|
157
|
+
minVersion: "12.0.0",
|
|
158
|
+
version: "22.2.2",
|
|
159
|
+
ngImport: i0,
|
|
160
|
+
type: CookieService,
|
|
161
|
+
providedIn: "root"
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
166
|
+
minVersion: "12.0.0",
|
|
167
|
+
version: "22.2.2",
|
|
168
|
+
ngImport: i0,
|
|
169
|
+
type: CookieService,
|
|
170
|
+
decorators: [{
|
|
171
|
+
type: Injectable,
|
|
172
|
+
args: [{ providedIn: "root" }]
|
|
173
|
+
}],
|
|
174
|
+
ctorParameters: () => []
|
|
175
|
+
});
|
|
211
176
|
export { CookieService };
|
|
212
|
-
|
|
177
|
+
|
|
178
|
+
//# sourceMappingURL=ngx-cookie-service.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ngx-cookie-service.mjs","sources":["../../../projects/ngx-cookie-service/src/lib/cookie.service.ts","../../../projects/ngx-cookie-service/src/public-api.ts","../../../projects/ngx-cookie-service/src/ngx-cookie-service.ts"],"sourcesContent":["import { isPlatformBrowser } from '@angular/common';\nimport { DOCUMENT, inject, Injectable, PLATFORM_ID } from '@angular/core';\n\nexport type SameSite = 'Lax' | 'None' | 'Strict';\n\nexport interface CookieOptions {\n expires?: number | Date;\n path?: string;\n domain?: string;\n secure?: boolean;\n sameSite?: SameSite;\n partitioned?: boolean;\n}\n\n@Injectable({\n providedIn: 'root',\n})\nexport class CookieService {\n private readonly documentIsAccessible: boolean;\n private readonly document = inject<Document>(DOCUMENT);\n private readonly platformId = inject<object>(PLATFORM_ID);\n\n constructor() {\n this.documentIsAccessible = isPlatformBrowser(this.platformId);\n }\n\n /**\n * Get cookie Regular Expression\n *\n * @param name Cookie name\n * @returns property RegExp\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n private static getCookieRegExp(name: string): RegExp {\n const escapedName: string = name.replace(/([[\\]{}()|=;+?,.*^$\\\\])/gi, '\\\\$1');\n\n return new RegExp('(?:^' + escapedName + '|;\\\\s*' + escapedName + ')=(.*?)(?:;|$)');\n }\n\n /**\n * Gets the decoded version of an encoded component of a Uniform Resource Identifier (URI).\n *\n * @param encodedURIComponent A value representing an encoded URI component.\n *\n * @returns The decoded version of an encoded component of a Uniform Resource Identifier (URI).\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n private static safeDecodeURIComponent(encodedURIComponent: string): string {\n try {\n return decodeURIComponent(encodedURIComponent);\n } catch {\n // probably it is not uri encoded. return as is\n return encodedURIComponent;\n }\n }\n\n /**\n * Return `true` if {@link Document} is accessible, otherwise return `false`\n *\n * @param name Cookie name\n * @returns boolean - whether cookie with specified name exists\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n check(name: string): boolean {\n if (!this.documentIsAccessible) {\n return false;\n }\n name = encodeURIComponent(name);\n const regExp = CookieService.getCookieRegExp(name);\n return regExp.test(this.document.cookie);\n }\n\n /**\n * Get cookies by name\n *\n * @param name Cookie name\n * @returns property value\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n get(name: string): string {\n if (this.check(name)) {\n name = encodeURIComponent(name);\n const regExp: RegExp = CookieService.getCookieRegExp(name);\n const result = regExp.exec(this.document.cookie);\n return result?.[1] ? CookieService.safeDecodeURIComponent(result[1]) : '';\n } else {\n return '';\n }\n }\n\n /**\n * Get all cookies in JSON format\n *\n * @returns all the cookies in json\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n getAll(): Record<string, string> {\n if (!this.documentIsAccessible) {\n return {};\n }\n\n const cookies: Record<string, string> = {};\n const document: Document = this.document;\n\n if (document.cookie && document.cookie !== '') {\n document.cookie.split(';').forEach((currentCookie: string) => {\n const [cookieName, cookieValue] = currentCookie.split('=');\n cookies[CookieService.safeDecodeURIComponent(cookieName.replace(/^ /, ''))] = CookieService.safeDecodeURIComponent(cookieValue);\n });\n }\n\n return cookies;\n }\n\n /**\n * Set cookie based on provided information\n *\n * @param name Cookie name\n * @param value Cookie value\n * @param expires Number of days until the cookies expires or an actual `Date`\n * @param path Cookie path\n * @param domain Cookie domain\n * @param secure Secure flag\n * @param partitioned Partitioned flag\n * @param sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n set(\n name: string,\n value: string,\n expires?: CookieOptions['expires'],\n path?: CookieOptions['path'],\n domain?: CookieOptions['domain'],\n secure?: CookieOptions['secure'],\n sameSite?: SameSite,\n partitioned?: CookieOptions['partitioned']\n ): void;\n\n /**\n * Set cookie based on provided information\n *\n * Cookie's parameters:\n * <pre>\n * expires Number of days until the cookies expires or an actual `Date`\n * path Cookie path\n * domain Cookie domain\n * secure flag\n * sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`\n * </pre>\n *\n * @param name Cookie name\n * @param value Cookie value\n * @param options Body with cookie's params\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n set(name: string, value: string, options?: CookieOptions): void;\n\n set(\n name: string,\n value: string,\n expiresOrOptions?: CookieOptions['expires'] | CookieOptions,\n path?: CookieOptions['path'],\n domain?: CookieOptions['domain'],\n secure?: CookieOptions['secure'],\n sameSite?: SameSite,\n partitioned?: CookieOptions['partitioned']\n ): void {\n if (!this.documentIsAccessible) {\n return;\n }\n\n if (typeof expiresOrOptions === 'number' || expiresOrOptions instanceof Date || path || domain || secure || sameSite) {\n const optionsBody = {\n expires: expiresOrOptions as CookieOptions['expires'],\n path,\n domain,\n secure,\n sameSite: sameSite || 'Lax',\n partitioned,\n };\n\n this.set(name, value, optionsBody);\n return;\n }\n\n let cookieString: string = encodeURIComponent(name) + '=' + encodeURIComponent(value) + ';';\n\n const options = expiresOrOptions ? expiresOrOptions : {};\n\n if (options.expires) {\n if (typeof options.expires === 'number') {\n const dateExpires: Date = new Date(new Date().getTime() + options.expires * 1000 * 60 * 60 * 24);\n\n cookieString += 'Expires=' + dateExpires.toUTCString() + ';';\n } else {\n cookieString += 'Expires=' + options.expires.toUTCString() + ';';\n }\n }\n\n if (options.path) {\n cookieString += 'Path=' + options.path + ';';\n }\n\n if (options.domain) {\n cookieString += 'Domain=' + options.domain + ';';\n }\n\n if (options.secure === false && options.sameSite === 'None') {\n options.secure = true;\n console.warn(\n `[ngx-cookie-service] Cookie ${name} was forced with secure flag because sameSite=None.` +\n `More details : https://github.com/stevermeister/ngx-cookie-service/issues/86#issuecomment-597720130`\n );\n }\n if (options.secure) {\n cookieString += 'Secure;';\n }\n\n if (!options.sameSite) {\n options.sameSite = 'Lax';\n }\n\n cookieString += 'SameSite=' + options.sameSite + ';';\n\n if (options.partitioned) {\n cookieString += 'Partitioned;';\n }\n\n this.document.cookie = cookieString;\n }\n\n /**\n * Delete cookie by name at given path and domain. If not path is not specified, cookie at '/' path will be deleted.\n *\n * @param name Cookie name\n * @param path Cookie path\n * @param domain Cookie domain\n * @param secure Cookie secure flag\n * @param sameSite Cookie sameSite flag - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n delete(name: string, path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite: SameSite = 'Lax'): void {\n if (!this.documentIsAccessible) {\n return;\n }\n const expiresDate = new Date('Thu, 01 Jan 1970 00:00:01 GMT');\n this.set(name, '', { expires: expiresDate, path, domain, secure, sameSite });\n }\n\n /**\n * Delete all cookies at given path and domain. If not path is not specified, all cookies at '/' path will be deleted.\n *\n * @param path Cookie path\n * @param domain Cookie domain\n * @param secure Is the Cookie secure\n * @param sameSite Is the cookie same site\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n deleteAll(path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite: SameSite = 'Lax'): void {\n if (!this.documentIsAccessible) {\n return;\n }\n\n const cookies: any = this.getAll();\n\n for (const cookieName in cookies) {\n if (cookies.hasOwnProperty(cookieName)) {\n this.delete(cookieName, path, domain, secure, sameSite);\n }\n }\n }\n}\n","/*\n * Public API Surface of ngx-cookie-service\n */\n\nexport * from './lib/cookie.service';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MAiBa,aAAa,CAAA;AAKxB,IAAA,WAAA,GAAA;AAHiB,QAAA,IAAA,CAAA,QAAQ,GAAG,MAAM,CAAW,QAAQ,CAAC;AACrC,QAAA,IAAA,CAAA,UAAU,GAAG,MAAM,CAAS,WAAW,CAAC;QAGvD,IAAI,CAAC,oBAAoB,GAAG,iBAAiB,CAAC,IAAI,CAAC,UAAU,CAAC;IAChE;AAEA;;;;;;;;AAQG;IACK,OAAO,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AA,QAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,aAAa,cAFZ,MAAM,EAAA,CAAA,CAAA;;2FAEP,aAAa,EAAA,UAAA,EAAA,CAAA;kBAHzB,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;AChBD;;AAEG;;ACFH;;AAEG;;;;"}
|
|
1
|
+
{"version":3,"file":"ngx-cookie-service.mjs","names":[],"sources":["../../../projects/ngx-cookie-service/src/lib/cookie.service.ts"],"sourcesContent":["import { isPlatformBrowser } from '@angular/common';\nimport { DOCUMENT, inject, Injectable, PLATFORM_ID } from '@angular/core';\n\nexport type SameSite = 'Lax' | 'None' | 'Strict';\n\nexport interface CookieOptions {\n expires?: number | Date;\n path?: string;\n domain?: string;\n secure?: boolean;\n sameSite?: SameSite;\n partitioned?: boolean;\n}\n\n/**\n * Attributes that identify the cookie to delete. They must match the attributes the cookie was set with.\n */\nexport type DeleteCookieOptions = Omit<CookieOptions, 'expires'>;\n\n/**\n * Cookie options after validation: invalid values are removed and `expires` is always a `Date`.\n */\ntype ValidCookieOptions = Omit<CookieOptions, 'expires'> & { expires?: Date };\n\n/**\n * The `SameSite` values browsers accept, in their canonical spelling.\n */\nconst SAME_SITE_VALUES: readonly SameSite[] = ['Lax', 'Strict', 'None'];\n\n@Injectable({\n providedIn: 'root',\n})\nexport class CookieService {\n private readonly documentIsAccessible: boolean;\n private readonly document = inject<Document>(DOCUMENT);\n private readonly platformId = inject<object>(PLATFORM_ID);\n\n constructor() {\n this.documentIsAccessible = isPlatformBrowser(this.platformId);\n }\n\n /**\n * Get cookie Regular Expression\n *\n * @param name Cookie name\n * @returns property RegExp\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n private static getCookieRegExp(name: string): RegExp {\n const escapedName: string = name.replace(/([[\\]{}()|=;+?,.*^$\\\\])/gi, '\\\\$1');\n\n return new RegExp('(?:^' + escapedName + '|;\\\\s*' + escapedName + ')=(.*?)(?:;|$)');\n }\n\n /**\n * Gets the decoded version of an encoded component of a Uniform Resource Identifier (URI).\n *\n * @param encodedURIComponent A value representing an encoded URI component.\n *\n * @returns The decoded version of an encoded component of a Uniform Resource Identifier (URI).\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n private static safeDecodeURIComponent(encodedURIComponent: string): string {\n try {\n return decodeURIComponent(encodedURIComponent);\n } catch {\n // probably it is not uri encoded. return as is\n return encodedURIComponent;\n }\n }\n\n /**\n * Return `true` if {@link Document} is accessible, otherwise return `false`\n *\n * @param name Cookie name\n * @returns boolean - whether cookie with specified name exists\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n check(name: string): boolean {\n if (!this.documentIsAccessible) {\n return false;\n }\n name = encodeURIComponent(name);\n const regExp = CookieService.getCookieRegExp(name);\n return regExp.test(this.document.cookie);\n }\n\n /**\n * Get cookies by name\n *\n * @param name Cookie name\n * @returns property value\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n get(name: string): string {\n if (this.check(name)) {\n name = encodeURIComponent(name);\n const regExp: RegExp = CookieService.getCookieRegExp(name);\n const result = regExp.exec(this.document.cookie);\n return result?.[1] ? CookieService.safeDecodeURIComponent(result[1]) : '';\n } else {\n return '';\n }\n }\n\n /**\n * Get all cookies in JSON format\n *\n * @returns all the cookies in json\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n getAll(): Record<string, string> {\n if (!this.documentIsAccessible) {\n return {};\n }\n\n const cookies: Record<string, string> = {};\n const document: Document = this.document;\n\n if (document.cookie && document.cookie !== '') {\n document.cookie.split(';').forEach((currentCookie: string) => {\n const trimmedCookie = currentCookie.trim();\n if (!trimmedCookie) {\n return;\n }\n // Split on the first '=' only, so values containing '=' (e.g. base64 padding) are preserved\n const equalIndex = trimmedCookie.indexOf('=');\n if (equalIndex === -1) {\n // Cookie without value (invalid, but handle gracefully)\n return;\n }\n const cookieName = CookieService.safeDecodeURIComponent(trimmedCookie.substring(0, equalIndex));\n // Browsers list the most specific cookie first when several share a name. Keep that one, as get() does.\n if (Object.hasOwn(cookies, cookieName)) {\n return;\n }\n // defineProperty stores names such as \"__proto__\" as normal entries instead of touching the object's prototype\n Object.defineProperty(cookies, cookieName, {\n value: CookieService.safeDecodeURIComponent(trimmedCookie.substring(equalIndex + 1)),\n enumerable: true,\n writable: true,\n configurable: true,\n });\n });\n }\n\n return cookies;\n }\n\n /**\n * Set cookie based on provided information\n *\n * @param name Cookie name\n * @param value Cookie value\n * @param expires Number of days until the cookies expires or an actual `Date`\n * @param path Cookie path\n * @param domain Cookie domain\n * @param secure Secure flag\n * @param partitioned Partitioned flag\n * @param sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n set(\n name: string,\n value: string,\n expires?: CookieOptions['expires'],\n path?: CookieOptions['path'],\n domain?: CookieOptions['domain'],\n secure?: CookieOptions['secure'],\n sameSite?: SameSite,\n partitioned?: CookieOptions['partitioned']\n ): void;\n\n /**\n * Set cookie based on provided information\n *\n * Cookie's parameters:\n * <pre>\n * expires Number of days until the cookies expires or an actual `Date`\n * path Cookie path\n * domain Cookie domain\n * secure flag\n * sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`\n * </pre>\n *\n * @param name Cookie name\n * @param value Cookie value\n * @param options Body with cookie's params\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n set(name: string, value: string, options?: CookieOptions): void;\n\n set(\n name: string,\n value: string,\n expiresOrOptions?: CookieOptions['expires'] | CookieOptions,\n path?: CookieOptions['path'],\n domain?: CookieOptions['domain'],\n secure?: CookieOptions['secure'],\n sameSite?: SameSite,\n partitioned?: CookieOptions['partitioned']\n ): void {\n if (!this.documentIsAccessible) {\n return;\n }\n\n // Anything but an options object means the positional form, so no positional argument (e.g. a lone `partitioned`) is dropped\n if (typeof expiresOrOptions !== 'object' || expiresOrOptions === null || expiresOrOptions instanceof Date) {\n const optionsBody = {\n expires: expiresOrOptions as CookieOptions['expires'],\n path,\n domain,\n secure,\n sameSite: sameSite || 'Lax',\n partitioned,\n };\n\n this.set(name, value, optionsBody);\n return;\n }\n\n // validateOptions() returns a copy, so the defaults applied below don't leak back into the caller's object\n const options = CookieService.validateOptions(name, expiresOrOptions);\n if (!options) {\n return;\n }\n\n let cookieString: string = encodeURIComponent(name) + '=' + encodeURIComponent(value) + ';';\n\n if (options.expires) {\n cookieString += 'Expires=' + options.expires.toUTCString() + ';';\n }\n\n if (options.path) {\n cookieString += 'Path=' + options.path + ';';\n }\n\n if (options.domain) {\n cookieString += 'Domain=' + options.domain + ';';\n }\n\n if (options.sameSite === 'None' && !options.secure) {\n options.secure = true;\n console.warn(\n `[ngx-cookie-service] Cookie ${name} was forced with secure flag because sameSite=None.` +\n `More details : https://github.com/stevermeister/ngx-cookie-service/issues/86#issuecomment-597720130`\n );\n }\n if (options.partitioned && !options.secure) {\n options.secure = true;\n console.warn(`[ngx-cookie-service] Cookie ${name} was forced with secure flag because Partitioned cookies must be Secure.`);\n }\n if (options.secure) {\n cookieString += 'Secure;';\n }\n\n if (!options.sameSite) {\n options.sameSite = 'Lax';\n }\n\n cookieString += 'SameSite=' + options.sameSite + ';';\n\n if (options.partitioned) {\n cookieString += 'Partitioned;';\n }\n\n this.document.cookie = cookieString;\n }\n\n /**\n * Delete cookie by name at given path and domain.\n *\n * If `path` is not specified, no `Path` attribute is written and the browser uses its default path: the directory of the URL\n * the page was loaded at (not the current router URL). To delete a cookie that was set with `path: '/'`, pass `'/'` here too.\n *\n * @param name Cookie name\n * @param path Cookie path\n * @param domain Cookie domain\n * @param secure Cookie secure flag\n * @param sameSite Cookie sameSite flag - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n delete(name: string, path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite): void;\n\n /**\n * Delete cookie by name. The options must match the attributes the cookie was set with: `path` and `domain` select the\n * cookie, and `partitioned: true` is required to delete a Partitioned cookie.\n *\n * If `path` is not specified, no `Path` attribute is written and the browser uses its default path: the directory of the URL\n * the page was loaded at (not the current router URL). To delete a cookie that was set with `path: '/'`, pass `'/'` here too.\n *\n * @param name Cookie name\n * @param options Cookie attributes: `path`, `domain`, `secure`, `sameSite` and `partitioned`\n *\n * @author: Pavankumar Jadda\n * @since: 22.1.0\n */\n delete(name: string, options?: DeleteCookieOptions): void;\n\n delete(\n name: string,\n pathOrOptions?: CookieOptions['path'] | DeleteCookieOptions,\n domain?: CookieOptions['domain'],\n secure?: CookieOptions['secure'],\n sameSite: SameSite = 'Lax'\n ): void {\n if (!this.documentIsAccessible) {\n return;\n }\n const options = CookieService.toDeleteOptions(pathOrOptions, domain, secure, sameSite);\n this.set(name, '', { ...options, expires: new Date('Thu, 01 Jan 1970 00:00:01 GMT') });\n }\n\n /**\n * Delete all cookies at given path and domain.\n *\n * If `path` is not specified, the browser's default path is used for each cookie, as described for {@link delete}.\n *\n * @param path Cookie path\n * @param domain Cookie domain\n * @param secure Is the Cookie secure\n * @param sameSite Is the cookie same site\n *\n * @author: Stepan Suvorov\n * @since: 1.0.0\n */\n deleteAll(path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite): void;\n\n /**\n * Delete all cookies that can currently be accessed, using the same attributes for each one.\n *\n * If `path` is not specified, the browser's default path is used for each cookie, as described for {@link delete}.\n *\n * @param options Cookie attributes: `path`, `domain`, `secure`, `sameSite` and `partitioned`\n *\n * @author: Pavankumar Jadda\n * @since: 22.1.0\n */\n deleteAll(options?: DeleteCookieOptions): void;\n\n deleteAll(\n pathOrOptions?: CookieOptions['path'] | DeleteCookieOptions,\n domain?: CookieOptions['domain'],\n secure?: CookieOptions['secure'],\n sameSite: SameSite = 'Lax'\n ): void {\n if (!this.documentIsAccessible) {\n return;\n }\n\n // Object.keys() rather than for...in with cookies.hasOwnProperty(): a cookie named \"hasOwnProperty\" would shadow that method\n for (const cookieName of Object.keys(this.getAll())) {\n // Call delete() the same way deleteAll() was called, so overrides and spies of the positional form keep working\n if (typeof pathOrOptions === 'object' && pathOrOptions !== null) {\n this.delete(cookieName, pathOrOptions);\n } else {\n this.delete(cookieName, pathOrOptions, domain, secure, sameSite);\n }\n }\n }\n\n /**\n * Returns a validated copy of the options, or `undefined` if the cookie must not be written.\n *\n * A `path` or `domain` containing `;` or a control character could inject attributes or change the cookie's scope, so the\n * whole write is skipped. An invalid `expires` is dropped, which makes a session cookie as browsers already did, and an\n * unknown `sameSite` falls back to the `Lax` default. Each case logs a warning. A numeric `expires` (days\n * from now) is converted to a `Date`, and `sameSite` is matched case-insensitively.\n *\n * @param name Cookie name, used in warnings\n * @param options Cookie options\n * @returns A validated copy of the options, or `undefined` to skip the write\n *\n * @author: Pavankumar Jadda\n * @since: 22.1.0\n */\n private static validateOptions(name: string, options: CookieOptions = {}): ValidCookieOptions | undefined {\n const { expires, ...valid }: CookieOptions = options;\n const result: ValidCookieOptions = valid;\n\n for (const attribute of ['path', 'domain'] as const) {\n const attributeValue = result[attribute];\n if (typeof attributeValue === 'string' && CookieService.hasUnsafeCharacter(attributeValue)) {\n console.warn(`[ngx-cookie-service] Cookie ${name} was skipped because its ${attribute} contains \";\" or a control character.`);\n return undefined;\n }\n }\n if (expires) {\n const date = typeof expires === 'number' ? new Date(Date.now() + expires * 1000 * 60 * 60 * 24) : expires;\n if (date instanceof Date && Number.isFinite(date.getTime())) {\n result.expires = date;\n } else {\n console.warn(`[ngx-cookie-service] Cookie ${name}: ignoring invalid expires value \"${String(expires)}\".`);\n }\n }\n if (result.sameSite !== undefined && result.sameSite !== null) {\n const sameSite = SAME_SITE_VALUES.find((value) => value.toLowerCase() === String(result.sameSite).toLowerCase());\n if (sameSite) {\n result.sameSite = sameSite;\n } else {\n console.warn(`[ngx-cookie-service] Cookie ${name}: ignoring invalid sameSite value \"${String(result.sameSite)}\".`);\n delete result.sameSite;\n }\n }\n return result;\n }\n\n /**\n * Whether a cookie attribute value contains `;` or an ASCII control character, which would end the attribute early or\n * corrupt the header.\n *\n * @param value Attribute value\n * @returns `true` if the value is unsafe to write\n *\n * @author: Pavankumar Jadda\n * @since: 22.1.0\n */\n private static hasUnsafeCharacter(value: string): boolean {\n return [...value].some((character) => {\n const code = character.charCodeAt(0);\n return character === ';' || code < 0x20 || code === 0x7f;\n });\n }\n\n /**\n * Builds delete options from either an options object or the positional `path`, `domain`, `secure` and `sameSite` arguments.\n *\n * @param pathOrOptions Either a DeleteCookieOptions object or the cookie path\n * @param domain Cookie domain\n * @param secure Cookie secure flag\n * @param sameSite Cookie sameSite flag\n * @returns A new DeleteCookieOptions object\n *\n * @author: Pavankumar Jadda\n * @since: 22.1.0\n */\n private static toDeleteOptions(\n pathOrOptions?: CookieOptions['path'] | DeleteCookieOptions,\n domain?: CookieOptions['domain'],\n secure?: CookieOptions['secure'],\n sameSite?: SameSite\n ): DeleteCookieOptions {\n if (typeof pathOrOptions === 'object' && pathOrOptions !== null) {\n return { ...pathOrOptions };\n }\n return { path: pathOrOptions, domain, secure, sameSite };\n }\n}\n"],"mappings":";;;AA2BA,MAAM,mBAAwC;CAAC;CAAO;CAAU;AAAM;AAKtE,IAAa,gBAAb,MAAa,cAAa;CAKxB,cAAA;EAHiB,KAAA,WAAW,OAAiB,QAAQ;EACpC,KAAA,aAAa,OAAe,WAAW;EAGtD,KAAK,uBAAuB,kBAAkB,KAAK,UAAU;CAC/D;CAWQ,OAAO,gBAAgB,MAAY;EACzC,MAAM,cAAsB,KAAK,QAAQ,6BAA6B,MAAM;EAE5E,OAAO,IAAI,OAAO,SAAS,cAAc,WAAW,cAAc,gBAAgB;CACpF;CAYQ,OAAO,uBAAuB,qBAA2B;EAC/D,IAAI;GACF,OAAO,mBAAmB,mBAAmB;EAC/C,QAAQ;GAEN,OAAO;EACT;CACF;CAWA,MAAM,MAAY;EAChB,IA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A,YAAA;GAAA,SAAA;GAAA,UAAA;GAAA,MAAA;GAAa,MAAA,CAAA;GAAA,QAAA,GAAA,gBAAA;EAAA,CAAA;CAAA;CAAb;EAAA,KAAA,QAAA,GAAA,sBAAA;GAAA,YAAA;GAAA,SAAA;GAAA,UAAA;GAAA,MAAA;GAAa,YAFZ;EAAM,CAAA;CAAA;;AAEP,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAa,YAAA,CAAA;EAHzB,MAAA;EAAW,MAAA,CAAA,EACV,YAAY,OACb,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ngx-cookie-service",
|
|
3
3
|
"description": "Angular cookie service",
|
|
4
|
-
"version": "22.
|
|
4
|
+
"version": "22.1.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Stepan Suvorov <stevermeister@gmail.com>",
|
|
7
7
|
"keywords": [
|
|
@@ -113,4 +113,4 @@
|
|
|
113
113
|
},
|
|
114
114
|
"sideEffects": false,
|
|
115
115
|
"type": "module"
|
|
116
|
-
}
|
|
116
|
+
}
|
|
@@ -1,133 +1,203 @@
|
|
|
1
|
-
import * as i0 from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
partitioned?: boolean;
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
export type SameSite = 'Lax' | 'None' | 'Strict';
|
|
3
|
+
export interface CookieOptions {
|
|
4
|
+
expires?: number | Date;
|
|
5
|
+
path?: string;
|
|
6
|
+
domain?: string;
|
|
7
|
+
secure?: boolean;
|
|
8
|
+
sameSite?: SameSite;
|
|
9
|
+
partitioned?: boolean;
|
|
11
10
|
}
|
|
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
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Attributes that identify the cookie to delete. They must match the attributes the cookie was set with.
|
|
13
|
+
*/
|
|
14
|
+
export type DeleteCookieOptions = Omit<CookieOptions, 'expires'>;
|
|
15
|
+
export declare class CookieService {
|
|
16
|
+
private readonly documentIsAccessible;
|
|
17
|
+
private readonly document;
|
|
18
|
+
private readonly platformId;
|
|
19
|
+
constructor();
|
|
20
|
+
/**
|
|
21
|
+
* Get cookie Regular Expression
|
|
22
|
+
*
|
|
23
|
+
* @param name Cookie name
|
|
24
|
+
* @returns property RegExp
|
|
25
|
+
*
|
|
26
|
+
* @author: Stepan Suvorov
|
|
27
|
+
* @since: 1.0.0
|
|
28
|
+
*/
|
|
29
|
+
private static getCookieRegExp;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the decoded version of an encoded component of a Uniform Resource Identifier (URI).
|
|
32
|
+
*
|
|
33
|
+
* @param encodedURIComponent A value representing an encoded URI component.
|
|
34
|
+
*
|
|
35
|
+
* @returns The decoded version of an encoded component of a Uniform Resource Identifier (URI).
|
|
36
|
+
*
|
|
37
|
+
* @author: Stepan Suvorov
|
|
38
|
+
* @since: 1.0.0
|
|
39
|
+
*/
|
|
40
|
+
private static safeDecodeURIComponent;
|
|
41
|
+
/**
|
|
42
|
+
* Return `true` if {@link Document} is accessible, otherwise return `false`
|
|
43
|
+
*
|
|
44
|
+
* @param name Cookie name
|
|
45
|
+
* @returns boolean - whether cookie with specified name exists
|
|
46
|
+
*
|
|
47
|
+
* @author: Stepan Suvorov
|
|
48
|
+
* @since: 1.0.0
|
|
49
|
+
*/
|
|
50
|
+
check(name: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Get cookies by name
|
|
53
|
+
*
|
|
54
|
+
* @param name Cookie name
|
|
55
|
+
* @returns property value
|
|
56
|
+
*
|
|
57
|
+
* @author: Stepan Suvorov
|
|
58
|
+
* @since: 1.0.0
|
|
59
|
+
*/
|
|
60
|
+
get(name: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* Get all cookies in JSON format
|
|
63
|
+
*
|
|
64
|
+
* @returns all the cookies in json
|
|
65
|
+
*
|
|
66
|
+
* @author: Stepan Suvorov
|
|
67
|
+
* @since: 1.0.0
|
|
68
|
+
*/
|
|
69
|
+
getAll(): Record<string, string>;
|
|
70
|
+
/**
|
|
71
|
+
* Set cookie based on provided information
|
|
72
|
+
*
|
|
73
|
+
* @param name Cookie name
|
|
74
|
+
* @param value Cookie value
|
|
75
|
+
* @param expires Number of days until the cookies expires or an actual `Date`
|
|
76
|
+
* @param path Cookie path
|
|
77
|
+
* @param domain Cookie domain
|
|
78
|
+
* @param secure Secure flag
|
|
79
|
+
* @param partitioned Partitioned flag
|
|
80
|
+
* @param sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`
|
|
81
|
+
*
|
|
82
|
+
* @author: Stepan Suvorov
|
|
83
|
+
* @since: 1.0.0
|
|
84
|
+
*/
|
|
85
|
+
set(name: string, value: string, expires?: CookieOptions['expires'], path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite, partitioned?: CookieOptions['partitioned']): void;
|
|
86
|
+
/**
|
|
87
|
+
* Set cookie based on provided information
|
|
88
|
+
*
|
|
89
|
+
* Cookie's parameters:
|
|
90
|
+
* <pre>
|
|
91
|
+
* expires Number of days until the cookies expires or an actual `Date`
|
|
92
|
+
* path Cookie path
|
|
93
|
+
* domain Cookie domain
|
|
94
|
+
* secure flag
|
|
95
|
+
* sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`
|
|
96
|
+
* </pre>
|
|
97
|
+
*
|
|
98
|
+
* @param name Cookie name
|
|
99
|
+
* @param value Cookie value
|
|
100
|
+
* @param options Body with cookie's params
|
|
101
|
+
*
|
|
102
|
+
* @author: Stepan Suvorov
|
|
103
|
+
* @since: 1.0.0
|
|
104
|
+
*/
|
|
105
|
+
set(name: string, value: string, options?: CookieOptions): void;
|
|
106
|
+
/**
|
|
107
|
+
* Delete cookie by name at given path and domain.
|
|
108
|
+
*
|
|
109
|
+
* If `path` is not specified, no `Path` attribute is written and the browser uses its default path: the directory of the URL
|
|
110
|
+
* the page was loaded at (not the current router URL). To delete a cookie that was set with `path: '/'`, pass `'/'` here too.
|
|
111
|
+
*
|
|
112
|
+
* @param name Cookie name
|
|
113
|
+
* @param path Cookie path
|
|
114
|
+
* @param domain Cookie domain
|
|
115
|
+
* @param secure Cookie secure flag
|
|
116
|
+
* @param sameSite Cookie sameSite flag - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
|
|
117
|
+
*
|
|
118
|
+
* @author: Stepan Suvorov
|
|
119
|
+
* @since: 1.0.0
|
|
120
|
+
*/
|
|
121
|
+
delete(name: string, path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite): void;
|
|
122
|
+
/**
|
|
123
|
+
* Delete cookie by name. The options must match the attributes the cookie was set with: `path` and `domain` select the
|
|
124
|
+
* cookie, and `partitioned: true` is required to delete a Partitioned cookie.
|
|
125
|
+
*
|
|
126
|
+
* If `path` is not specified, no `Path` attribute is written and the browser uses its default path: the directory of the URL
|
|
127
|
+
* the page was loaded at (not the current router URL). To delete a cookie that was set with `path: '/'`, pass `'/'` here too.
|
|
128
|
+
*
|
|
129
|
+
* @param name Cookie name
|
|
130
|
+
* @param options Cookie attributes: `path`, `domain`, `secure`, `sameSite` and `partitioned`
|
|
131
|
+
*
|
|
132
|
+
* @author: Pavankumar Jadda
|
|
133
|
+
* @since: 22.1.0
|
|
134
|
+
*/
|
|
135
|
+
delete(name: string, options?: DeleteCookieOptions): void;
|
|
136
|
+
/**
|
|
137
|
+
* Delete all cookies at given path and domain.
|
|
138
|
+
*
|
|
139
|
+
* If `path` is not specified, the browser's default path is used for each cookie, as described for {@link delete}.
|
|
140
|
+
*
|
|
141
|
+
* @param path Cookie path
|
|
142
|
+
* @param domain Cookie domain
|
|
143
|
+
* @param secure Is the Cookie secure
|
|
144
|
+
* @param sameSite Is the cookie same site
|
|
145
|
+
*
|
|
146
|
+
* @author: Stepan Suvorov
|
|
147
|
+
* @since: 1.0.0
|
|
148
|
+
*/
|
|
149
|
+
deleteAll(path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite): void;
|
|
150
|
+
/**
|
|
151
|
+
* Delete all cookies that can currently be accessed, using the same attributes for each one.
|
|
152
|
+
*
|
|
153
|
+
* If `path` is not specified, the browser's default path is used for each cookie, as described for {@link delete}.
|
|
154
|
+
*
|
|
155
|
+
* @param options Cookie attributes: `path`, `domain`, `secure`, `sameSite` and `partitioned`
|
|
156
|
+
*
|
|
157
|
+
* @author: Pavankumar Jadda
|
|
158
|
+
* @since: 22.1.0
|
|
159
|
+
*/
|
|
160
|
+
deleteAll(options?: DeleteCookieOptions): void;
|
|
161
|
+
/**
|
|
162
|
+
* Returns a validated copy of the options, or `undefined` if the cookie must not be written.
|
|
163
|
+
*
|
|
164
|
+
* A `path` or `domain` containing `;` or a control character could inject attributes or change the cookie's scope, so the
|
|
165
|
+
* whole write is skipped. An invalid `expires` is dropped, which makes a session cookie as browsers already did, and an
|
|
166
|
+
* unknown `sameSite` falls back to the `Lax` default. Each case logs a warning. A numeric `expires` (days
|
|
167
|
+
* from now) is converted to a `Date`, and `sameSite` is matched case-insensitively.
|
|
168
|
+
*
|
|
169
|
+
* @param name Cookie name, used in warnings
|
|
170
|
+
* @param options Cookie options
|
|
171
|
+
* @returns A validated copy of the options, or `undefined` to skip the write
|
|
172
|
+
*
|
|
173
|
+
* @author: Pavankumar Jadda
|
|
174
|
+
* @since: 22.1.0
|
|
175
|
+
*/
|
|
176
|
+
private static validateOptions;
|
|
177
|
+
/**
|
|
178
|
+
* Whether a cookie attribute value contains `;` or an ASCII control character, which would end the attribute early or
|
|
179
|
+
* corrupt the header.
|
|
180
|
+
*
|
|
181
|
+
* @param value Attribute value
|
|
182
|
+
* @returns `true` if the value is unsafe to write
|
|
183
|
+
*
|
|
184
|
+
* @author: Pavankumar Jadda
|
|
185
|
+
* @since: 22.1.0
|
|
186
|
+
*/
|
|
187
|
+
private static hasUnsafeCharacter;
|
|
188
|
+
/**
|
|
189
|
+
* Builds delete options from either an options object or the positional `path`, `domain`, `secure` and `sameSite` arguments.
|
|
190
|
+
*
|
|
191
|
+
* @param pathOrOptions Either a DeleteCookieOptions object or the cookie path
|
|
192
|
+
* @param domain Cookie domain
|
|
193
|
+
* @param secure Cookie secure flag
|
|
194
|
+
* @param sameSite Cookie sameSite flag
|
|
195
|
+
* @returns A new DeleteCookieOptions object
|
|
196
|
+
*
|
|
197
|
+
* @author: Pavankumar Jadda
|
|
198
|
+
* @since: 22.1.0
|
|
199
|
+
*/
|
|
200
|
+
private static toDeleteOptions;
|
|
201
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<CookieService, never>;
|
|
202
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
|
|
203
|
+
}
|