cookie-es 1.0.0 → 1.2.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/LICENSE +21 -17
- package/README.md +58 -8
- package/dist/index.cjs +145 -18
- package/dist/index.d.cts +222 -0
- package/dist/index.d.mts +222 -0
- package/dist/index.d.ts +83 -1
- package/dist/index.mjs +144 -19
- package/package.json +24 -18
package/LICENSE
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
+
Cookie-es copyright (c) Pooya Parsa <pooya@pi0.io>
|
|
4
|
+
|
|
5
|
+
Cookie parsing based on https://github.com/jshttp/cookie
|
|
3
6
|
Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
|
|
4
7
|
Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
'Software'), to deal in the Software without restriction, including
|
|
9
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
-
permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
-
the following conditions:
|
|
9
|
+
Set-Cookie parsing based on https://github.com/nfriedly/set-cookie-parser
|
|
10
|
+
Copyright (c) 2015 Nathan Friedly <nathan@nfriedly.com> (http://nfriedly.com/)
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
24
21
|
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,32 +1,82 @@
|
|
|
1
1
|
|
|
2
|
-
# cookie-es
|
|
2
|
+
# 🍪 cookie-es
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
<!-- automd:badges bundlejs -->
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
[](https://npmjs.com/package/cookie-es)
|
|
7
|
+
[](https://npmjs.com/package/cookie-es)
|
|
8
|
+
[](https://bundlejs.com/?q=cookie-es)
|
|
9
|
+
|
|
10
|
+
<!-- /automd -->
|
|
11
|
+
|
|
12
|
+
🍪 [`Cookie`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie) and [`Set-Cookie`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) parser and serializer based on [cookie](https://github.com/jshttp/cookiee) and [set-cookie-parser](https://github.com/nfriedly/set-cookie-parser) with dual ESM/CJS exports and bundled types. 🎁
|
|
7
13
|
|
|
8
14
|
## Usage
|
|
9
15
|
|
|
10
16
|
Install:
|
|
11
17
|
|
|
18
|
+
<!-- automd:pm-install -->
|
|
19
|
+
|
|
12
20
|
```sh
|
|
21
|
+
# ✨ Auto-detect
|
|
22
|
+
npx nypm install cookie-es
|
|
23
|
+
|
|
13
24
|
# npm
|
|
14
|
-
npm
|
|
25
|
+
npm install cookie-es
|
|
15
26
|
|
|
16
27
|
# yarn
|
|
17
28
|
yarn add cookie-es
|
|
29
|
+
|
|
30
|
+
# pnpm
|
|
31
|
+
pnpm install cookie-es
|
|
32
|
+
|
|
33
|
+
# bun
|
|
34
|
+
bun install cookie-es
|
|
18
35
|
```
|
|
19
36
|
|
|
37
|
+
<!-- /automd-->
|
|
38
|
+
|
|
20
39
|
Import:
|
|
21
40
|
|
|
41
|
+
|
|
42
|
+
<!-- automd:jsimport cdn cjs src=./src/index.ts -->
|
|
43
|
+
|
|
44
|
+
**ESM** (Node.js, Bun)
|
|
45
|
+
|
|
22
46
|
```js
|
|
23
|
-
|
|
24
|
-
|
|
47
|
+
import {
|
|
48
|
+
parse,
|
|
49
|
+
serialize,
|
|
50
|
+
parseSetCookie,
|
|
51
|
+
splitSetCookieString,
|
|
52
|
+
} from "cookie-es";
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**CommonJS** (Legacy Node.js)
|
|
25
56
|
|
|
26
|
-
|
|
27
|
-
const {
|
|
57
|
+
```js
|
|
58
|
+
const {
|
|
59
|
+
parse,
|
|
60
|
+
serialize,
|
|
61
|
+
parseSetCookie,
|
|
62
|
+
splitSetCookieString,
|
|
63
|
+
} = require("cookie-es");
|
|
28
64
|
```
|
|
29
65
|
|
|
66
|
+
**CDN** (Deno, Bun and Browsers)
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import {
|
|
70
|
+
parse,
|
|
71
|
+
serialize,
|
|
72
|
+
parseSetCookie,
|
|
73
|
+
splitSetCookieString,
|
|
74
|
+
} from "https://esm.sh/cookie-es";
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
<!-- /automd -->
|
|
78
|
+
|
|
79
|
+
|
|
30
80
|
## License
|
|
31
81
|
|
|
32
82
|
[MIT](./LICENSE)
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const fieldContentRegExp = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
|
|
4
3
|
function parse(str, options) {
|
|
5
4
|
if (typeof str !== "string") {
|
|
6
5
|
throw new TypeError("argument str must be a string");
|
|
@@ -33,9 +32,21 @@ function parse(str, options) {
|
|
|
33
32
|
}
|
|
34
33
|
return obj;
|
|
35
34
|
}
|
|
35
|
+
function decode(str) {
|
|
36
|
+
return str.includes("%") ? decodeURIComponent(str) : str;
|
|
37
|
+
}
|
|
38
|
+
function tryDecode(str, decode2) {
|
|
39
|
+
try {
|
|
40
|
+
return decode2(str);
|
|
41
|
+
} catch {
|
|
42
|
+
return str;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const fieldContentRegExp = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
|
|
36
47
|
function serialize(name, value, options) {
|
|
37
48
|
const opt = options || {};
|
|
38
|
-
const enc = opt.encode ||
|
|
49
|
+
const enc = opt.encode || encodeURIComponent;
|
|
39
50
|
if (typeof enc !== "function") {
|
|
40
51
|
throw new TypeError("option encode is invalid");
|
|
41
52
|
}
|
|
@@ -81,56 +92,172 @@ function serialize(name, value, options) {
|
|
|
81
92
|
if (opt.priority) {
|
|
82
93
|
const priority = typeof opt.priority === "string" ? opt.priority.toLowerCase() : opt.priority;
|
|
83
94
|
switch (priority) {
|
|
84
|
-
case "low":
|
|
95
|
+
case "low": {
|
|
85
96
|
str += "; Priority=Low";
|
|
86
97
|
break;
|
|
87
|
-
|
|
98
|
+
}
|
|
99
|
+
case "medium": {
|
|
88
100
|
str += "; Priority=Medium";
|
|
89
101
|
break;
|
|
90
|
-
|
|
102
|
+
}
|
|
103
|
+
case "high": {
|
|
91
104
|
str += "; Priority=High";
|
|
92
105
|
break;
|
|
93
|
-
|
|
106
|
+
}
|
|
107
|
+
default: {
|
|
94
108
|
throw new TypeError("option priority is invalid");
|
|
109
|
+
}
|
|
95
110
|
}
|
|
96
111
|
}
|
|
97
112
|
if (opt.sameSite) {
|
|
98
113
|
const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
99
114
|
switch (sameSite) {
|
|
100
|
-
case true:
|
|
115
|
+
case true: {
|
|
101
116
|
str += "; SameSite=Strict";
|
|
102
117
|
break;
|
|
103
|
-
|
|
118
|
+
}
|
|
119
|
+
case "lax": {
|
|
104
120
|
str += "; SameSite=Lax";
|
|
105
121
|
break;
|
|
106
|
-
|
|
122
|
+
}
|
|
123
|
+
case "strict": {
|
|
107
124
|
str += "; SameSite=Strict";
|
|
108
125
|
break;
|
|
109
|
-
|
|
126
|
+
}
|
|
127
|
+
case "none": {
|
|
110
128
|
str += "; SameSite=None";
|
|
111
129
|
break;
|
|
112
|
-
|
|
130
|
+
}
|
|
131
|
+
default: {
|
|
113
132
|
throw new TypeError("option sameSite is invalid");
|
|
133
|
+
}
|
|
114
134
|
}
|
|
115
135
|
}
|
|
136
|
+
if (opt.partitioned) {
|
|
137
|
+
str += "; Partitioned";
|
|
138
|
+
}
|
|
116
139
|
return str;
|
|
117
140
|
}
|
|
118
141
|
function isDate(val) {
|
|
119
142
|
return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
|
|
120
143
|
}
|
|
121
|
-
|
|
144
|
+
|
|
145
|
+
function parseSetCookie(setCookieValue, options) {
|
|
146
|
+
const parts = (setCookieValue || "").split(";").filter((str) => typeof str === "string" && !!str.trim());
|
|
147
|
+
const nameValuePairStr = parts.shift() || "";
|
|
148
|
+
const parsed = _parseNameValuePair(nameValuePairStr);
|
|
149
|
+
const name = parsed.name;
|
|
150
|
+
let value = parsed.value;
|
|
122
151
|
try {
|
|
123
|
-
|
|
152
|
+
value = options?.decode === false ? value : (options?.decode || decodeURIComponent)(value);
|
|
124
153
|
} catch {
|
|
125
|
-
return str;
|
|
126
154
|
}
|
|
155
|
+
const cookie = {
|
|
156
|
+
name,
|
|
157
|
+
value
|
|
158
|
+
};
|
|
159
|
+
for (const part of parts) {
|
|
160
|
+
const sides = part.split("=");
|
|
161
|
+
const partKey = (sides.shift() || "").trimStart().toLowerCase();
|
|
162
|
+
const partValue = sides.join("=");
|
|
163
|
+
switch (partKey) {
|
|
164
|
+
case "expires": {
|
|
165
|
+
cookie.expires = new Date(partValue);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case "max-age": {
|
|
169
|
+
cookie.maxAge = Number.parseInt(partValue, 10);
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case "secure": {
|
|
173
|
+
cookie.secure = true;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
case "httponly": {
|
|
177
|
+
cookie.httpOnly = true;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case "samesite": {
|
|
181
|
+
cookie.sameSite = partValue;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
default: {
|
|
185
|
+
cookie[partKey] = partValue;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return cookie;
|
|
127
190
|
}
|
|
128
|
-
function
|
|
129
|
-
|
|
191
|
+
function _parseNameValuePair(nameValuePairStr) {
|
|
192
|
+
let name = "";
|
|
193
|
+
let value = "";
|
|
194
|
+
const nameValueArr = nameValuePairStr.split("=");
|
|
195
|
+
if (nameValueArr.length > 1) {
|
|
196
|
+
name = nameValueArr.shift();
|
|
197
|
+
value = nameValueArr.join("=");
|
|
198
|
+
} else {
|
|
199
|
+
value = nameValuePairStr;
|
|
200
|
+
}
|
|
201
|
+
return { name, value };
|
|
130
202
|
}
|
|
131
|
-
|
|
132
|
-
|
|
203
|
+
|
|
204
|
+
function splitSetCookieString(cookiesString) {
|
|
205
|
+
if (Array.isArray(cookiesString)) {
|
|
206
|
+
return cookiesString.flatMap((c) => splitSetCookieString(c));
|
|
207
|
+
}
|
|
208
|
+
if (typeof cookiesString !== "string") {
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
const cookiesStrings = [];
|
|
212
|
+
let pos = 0;
|
|
213
|
+
let start;
|
|
214
|
+
let ch;
|
|
215
|
+
let lastComma;
|
|
216
|
+
let nextStart;
|
|
217
|
+
let cookiesSeparatorFound;
|
|
218
|
+
const skipWhitespace = () => {
|
|
219
|
+
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
|
|
220
|
+
pos += 1;
|
|
221
|
+
}
|
|
222
|
+
return pos < cookiesString.length;
|
|
223
|
+
};
|
|
224
|
+
const notSpecialChar = () => {
|
|
225
|
+
ch = cookiesString.charAt(pos);
|
|
226
|
+
return ch !== "=" && ch !== ";" && ch !== ",";
|
|
227
|
+
};
|
|
228
|
+
while (pos < cookiesString.length) {
|
|
229
|
+
start = pos;
|
|
230
|
+
cookiesSeparatorFound = false;
|
|
231
|
+
while (skipWhitespace()) {
|
|
232
|
+
ch = cookiesString.charAt(pos);
|
|
233
|
+
if (ch === ",") {
|
|
234
|
+
lastComma = pos;
|
|
235
|
+
pos += 1;
|
|
236
|
+
skipWhitespace();
|
|
237
|
+
nextStart = pos;
|
|
238
|
+
while (pos < cookiesString.length && notSpecialChar()) {
|
|
239
|
+
pos += 1;
|
|
240
|
+
}
|
|
241
|
+
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
|
|
242
|
+
cookiesSeparatorFound = true;
|
|
243
|
+
pos = nextStart;
|
|
244
|
+
cookiesStrings.push(cookiesString.slice(start, lastComma));
|
|
245
|
+
start = pos;
|
|
246
|
+
} else {
|
|
247
|
+
pos = lastComma + 1;
|
|
248
|
+
}
|
|
249
|
+
} else {
|
|
250
|
+
pos += 1;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
|
|
254
|
+
cookiesStrings.push(cookiesString.slice(start, cookiesString.length));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return cookiesStrings;
|
|
133
258
|
}
|
|
134
259
|
|
|
135
260
|
exports.parse = parse;
|
|
261
|
+
exports.parseSetCookie = parseSetCookie;
|
|
136
262
|
exports.serialize = serialize;
|
|
263
|
+
exports.splitSetCookieString = splitSetCookieString;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic HTTP cookie parser and serializer for HTTP servers.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Additional serialization options
|
|
6
|
+
*/
|
|
7
|
+
interface CookieSerializeOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no
|
|
10
|
+
* domain is set, and most clients will consider the cookie to apply to only
|
|
11
|
+
* the current domain.
|
|
12
|
+
*/
|
|
13
|
+
domain?: string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Specifies a function that will be used to encode a cookie's value. Since
|
|
16
|
+
* value of a cookie has a limited character set (and must be a simple
|
|
17
|
+
* string), this function can be used to encode a value into a string suited
|
|
18
|
+
* for a cookie's value.
|
|
19
|
+
*
|
|
20
|
+
* The default function is the global `encodeURIComponent`, which will
|
|
21
|
+
* encode a JavaScript string into UTF-8 byte sequences and then URL-encode
|
|
22
|
+
* any that fall outside of the cookie range.
|
|
23
|
+
*/
|
|
24
|
+
encode?(value: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default,
|
|
27
|
+
* no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
|
|
28
|
+
* it on a condition like exiting a web browser application.
|
|
29
|
+
*
|
|
30
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
|
|
31
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
32
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
33
|
+
* point to the same date and time.
|
|
34
|
+
*/
|
|
35
|
+
expires?: Date | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}.
|
|
38
|
+
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
|
|
39
|
+
* default, the `HttpOnly` attribute is not set.
|
|
40
|
+
*
|
|
41
|
+
* *Note* be careful when setting this to true, as compliant clients will
|
|
42
|
+
* not allow client-side JavaScript to see the cookie in `document.cookie`.
|
|
43
|
+
*/
|
|
44
|
+
httpOnly?: boolean | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Specifies the number (in seconds) to be the value for the `Max-Age`
|
|
47
|
+
* `Set-Cookie` attribute. The given number will be converted to an integer
|
|
48
|
+
* by rounding down. By default, no maximum age is set.
|
|
49
|
+
*
|
|
50
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
|
|
51
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
52
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
53
|
+
* point to the same date and time.
|
|
54
|
+
*/
|
|
55
|
+
maxAge?: number | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}.
|
|
58
|
+
* By default, the path is considered the "default path".
|
|
59
|
+
*/
|
|
60
|
+
path?: string | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute][rfc-west-cookie-priority-00-4.1].
|
|
63
|
+
*
|
|
64
|
+
* - `'low'` will set the `Priority` attribute to `Low`.
|
|
65
|
+
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
|
|
66
|
+
* - `'high'` will set the `Priority` attribute to `High`.
|
|
67
|
+
*
|
|
68
|
+
* More information about the different priority levels can be found in
|
|
69
|
+
* [the specification][rfc-west-cookie-priority-00-4.1].
|
|
70
|
+
*
|
|
71
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
72
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
73
|
+
*/
|
|
74
|
+
priority?: "low" | "medium" | "high" | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
|
|
77
|
+
*
|
|
78
|
+
* - `true` will set the `SameSite` attribute to `Strict` for strict same
|
|
79
|
+
* site enforcement.
|
|
80
|
+
* - `false` will not set the `SameSite` attribute.
|
|
81
|
+
* - `'lax'` will set the `SameSite` attribute to Lax for lax same site
|
|
82
|
+
* enforcement.
|
|
83
|
+
* - `'strict'` will set the `SameSite` attribute to Strict for strict same
|
|
84
|
+
* site enforcement.
|
|
85
|
+
* - `'none'` will set the SameSite attribute to None for an explicit
|
|
86
|
+
* cross-site cookie.
|
|
87
|
+
*
|
|
88
|
+
* More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
|
|
89
|
+
*
|
|
90
|
+
* *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
|
|
91
|
+
*/
|
|
92
|
+
sameSite?: true | false | "lax" | "strict" | "none" | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
|
|
95
|
+
* `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
|
|
96
|
+
*
|
|
97
|
+
* *Note* be careful when setting this to `true`, as compliant clients will
|
|
98
|
+
* not send the cookie back to the server in the future if the browser does
|
|
99
|
+
* not have an HTTPS connection.
|
|
100
|
+
*/
|
|
101
|
+
secure?: boolean | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1)
|
|
104
|
+
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
|
|
105
|
+
* `Partitioned` attribute is not set.
|
|
106
|
+
*
|
|
107
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
108
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
109
|
+
*
|
|
110
|
+
* More information can be found in the [proposal](https://github.com/privacycg/CHIPS).
|
|
111
|
+
*/
|
|
112
|
+
partitioned?: boolean;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Additional parsing options
|
|
116
|
+
*/
|
|
117
|
+
interface CookieParseOptions {
|
|
118
|
+
/**
|
|
119
|
+
* Specifies a function that will be used to decode a cookie's value. Since
|
|
120
|
+
* the value of a cookie has a limited character set (and must be a simple
|
|
121
|
+
* string), this function can be used to decode a previously-encoded cookie
|
|
122
|
+
* value into a JavaScript string or other object.
|
|
123
|
+
*
|
|
124
|
+
* The default function is the global `decodeURIComponent`, which will decode
|
|
125
|
+
* any URL-encoded sequences into their byte representations.
|
|
126
|
+
*
|
|
127
|
+
* *Note* if an error is thrown from this function, the original, non-decoded
|
|
128
|
+
* cookie value will be returned as the cookie's value.
|
|
129
|
+
*/
|
|
130
|
+
decode?(value: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Custom function to filter parsing specific keys.
|
|
133
|
+
*/
|
|
134
|
+
filter?(key: string): boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Parse an HTTP Cookie header string and returning an object of all cookie
|
|
139
|
+
* name-value pairs.
|
|
140
|
+
*
|
|
141
|
+
* @param str the string representing a `Cookie` header value
|
|
142
|
+
* @param [options] object containing parsing options
|
|
143
|
+
*/
|
|
144
|
+
declare function parse(str: string, options?: CookieParseOptions): Record<string, string>;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
148
|
+
*
|
|
149
|
+
* @param name the name for the cookie
|
|
150
|
+
* @param value value to set the cookie to
|
|
151
|
+
* @param [options] object containing serialization options
|
|
152
|
+
* @throws {TypeError} when `maxAge` options is invalid
|
|
153
|
+
*/
|
|
154
|
+
declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
155
|
+
|
|
156
|
+
interface SetCookieParseOptions {
|
|
157
|
+
/**
|
|
158
|
+
* Custom decode function to use on cookie values.
|
|
159
|
+
*
|
|
160
|
+
* By default, `decodeURIComponent` is used.
|
|
161
|
+
*
|
|
162
|
+
* **Note:** If decoding fails, the original (undecoded) value will be used
|
|
163
|
+
*/
|
|
164
|
+
decode?: false | ((value: string) => string);
|
|
165
|
+
}
|
|
166
|
+
interface SetCookie {
|
|
167
|
+
/**
|
|
168
|
+
* Cookie name
|
|
169
|
+
*/
|
|
170
|
+
name: string;
|
|
171
|
+
/**
|
|
172
|
+
* Cookie value
|
|
173
|
+
*/
|
|
174
|
+
value: string;
|
|
175
|
+
/**
|
|
176
|
+
* Cookie path
|
|
177
|
+
*/
|
|
178
|
+
path?: string | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* Absolute expiration date for the cookie
|
|
181
|
+
*/
|
|
182
|
+
expires?: Date | undefined;
|
|
183
|
+
/**
|
|
184
|
+
* Relative max age of the cookie in seconds from when the client receives it (integer or undefined)
|
|
185
|
+
*
|
|
186
|
+
* Note: when using with express's res.cookie() method, multiply maxAge by 1000 to convert to milliseconds
|
|
187
|
+
*/
|
|
188
|
+
maxAge?: number | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* Domain for the cookie,
|
|
191
|
+
* May begin with "." to indicate the named domain or any subdomain of it
|
|
192
|
+
*/
|
|
193
|
+
domain?: string | undefined;
|
|
194
|
+
/**
|
|
195
|
+
* Indicates that this cookie should only be sent over HTTPs
|
|
196
|
+
*/
|
|
197
|
+
secure?: boolean | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Indicates that this cookie should not be accessible to client-side JavaScript
|
|
200
|
+
*/
|
|
201
|
+
httpOnly?: boolean | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* Indicates a cookie ought not to be sent along with cross-site requests
|
|
204
|
+
*/
|
|
205
|
+
sameSite?: string | undefined;
|
|
206
|
+
[key: string]: unknown;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.
|
|
211
|
+
*/
|
|
212
|
+
declare function parseSetCookie(setCookieValue: string, options?: SetCookieParseOptions): SetCookie;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
|
216
|
+
* that are within a single set-cookie field-value, such as in the Expires portion.
|
|
217
|
+
*
|
|
218
|
+
* See https://tools.ietf.org/html/rfc2616#section-4.2
|
|
219
|
+
*/
|
|
220
|
+
declare function splitSetCookieString(cookiesString: string | string[]): string[];
|
|
221
|
+
|
|
222
|
+
export { type CookieParseOptions, type CookieSerializeOptions, type SetCookie, type SetCookieParseOptions, parse, parseSetCookie, serialize, splitSetCookieString };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic HTTP cookie parser and serializer for HTTP servers.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Additional serialization options
|
|
6
|
+
*/
|
|
7
|
+
interface CookieSerializeOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.3|Domain Set-Cookie attribute}. By default, no
|
|
10
|
+
* domain is set, and most clients will consider the cookie to apply to only
|
|
11
|
+
* the current domain.
|
|
12
|
+
*/
|
|
13
|
+
domain?: string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Specifies a function that will be used to encode a cookie's value. Since
|
|
16
|
+
* value of a cookie has a limited character set (and must be a simple
|
|
17
|
+
* string), this function can be used to encode a value into a string suited
|
|
18
|
+
* for a cookie's value.
|
|
19
|
+
*
|
|
20
|
+
* The default function is the global `encodeURIComponent`, which will
|
|
21
|
+
* encode a JavaScript string into UTF-8 byte sequences and then URL-encode
|
|
22
|
+
* any that fall outside of the cookie range.
|
|
23
|
+
*/
|
|
24
|
+
encode?(value: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Specifies the `Date` object to be the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.1|`Expires` `Set-Cookie` attribute}. By default,
|
|
27
|
+
* no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
|
|
28
|
+
* it on a condition like exiting a web browser application.
|
|
29
|
+
*
|
|
30
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
|
|
31
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
32
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
33
|
+
* point to the same date and time.
|
|
34
|
+
*/
|
|
35
|
+
expires?: Date | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.6|`HttpOnly` `Set-Cookie` attribute}.
|
|
38
|
+
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
|
|
39
|
+
* default, the `HttpOnly` attribute is not set.
|
|
40
|
+
*
|
|
41
|
+
* *Note* be careful when setting this to true, as compliant clients will
|
|
42
|
+
* not allow client-side JavaScript to see the cookie in `document.cookie`.
|
|
43
|
+
*/
|
|
44
|
+
httpOnly?: boolean | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Specifies the number (in seconds) to be the value for the `Max-Age`
|
|
47
|
+
* `Set-Cookie` attribute. The given number will be converted to an integer
|
|
48
|
+
* by rounding down. By default, no maximum age is set.
|
|
49
|
+
*
|
|
50
|
+
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
|
|
51
|
+
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
|
|
52
|
+
* possible not all clients by obey this, so if both are set, they should
|
|
53
|
+
* point to the same date and time.
|
|
54
|
+
*/
|
|
55
|
+
maxAge?: number | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Specifies the value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.4|`Path` `Set-Cookie` attribute}.
|
|
58
|
+
* By default, the path is considered the "default path".
|
|
59
|
+
*/
|
|
60
|
+
path?: string | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute][rfc-west-cookie-priority-00-4.1].
|
|
63
|
+
*
|
|
64
|
+
* - `'low'` will set the `Priority` attribute to `Low`.
|
|
65
|
+
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
|
|
66
|
+
* - `'high'` will set the `Priority` attribute to `High`.
|
|
67
|
+
*
|
|
68
|
+
* More information about the different priority levels can be found in
|
|
69
|
+
* [the specification][rfc-west-cookie-priority-00-4.1].
|
|
70
|
+
*
|
|
71
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
72
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
73
|
+
*/
|
|
74
|
+
priority?: "low" | "medium" | "high" | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Specifies the boolean or string to be the value for the {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|`SameSite` `Set-Cookie` attribute}.
|
|
77
|
+
*
|
|
78
|
+
* - `true` will set the `SameSite` attribute to `Strict` for strict same
|
|
79
|
+
* site enforcement.
|
|
80
|
+
* - `false` will not set the `SameSite` attribute.
|
|
81
|
+
* - `'lax'` will set the `SameSite` attribute to Lax for lax same site
|
|
82
|
+
* enforcement.
|
|
83
|
+
* - `'strict'` will set the `SameSite` attribute to Strict for strict same
|
|
84
|
+
* site enforcement.
|
|
85
|
+
* - `'none'` will set the SameSite attribute to None for an explicit
|
|
86
|
+
* cross-site cookie.
|
|
87
|
+
*
|
|
88
|
+
* More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
|
|
89
|
+
*
|
|
90
|
+
* *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
|
|
91
|
+
*/
|
|
92
|
+
sameSite?: true | false | "lax" | "strict" | "none" | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
|
|
95
|
+
* `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
|
|
96
|
+
*
|
|
97
|
+
* *Note* be careful when setting this to `true`, as compliant clients will
|
|
98
|
+
* not send the cookie back to the server in the future if the browser does
|
|
99
|
+
* not have an HTTPS connection.
|
|
100
|
+
*/
|
|
101
|
+
secure?: boolean | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1)
|
|
104
|
+
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
|
|
105
|
+
* `Partitioned` attribute is not set.
|
|
106
|
+
*
|
|
107
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
108
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
109
|
+
*
|
|
110
|
+
* More information can be found in the [proposal](https://github.com/privacycg/CHIPS).
|
|
111
|
+
*/
|
|
112
|
+
partitioned?: boolean;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Additional parsing options
|
|
116
|
+
*/
|
|
117
|
+
interface CookieParseOptions {
|
|
118
|
+
/**
|
|
119
|
+
* Specifies a function that will be used to decode a cookie's value. Since
|
|
120
|
+
* the value of a cookie has a limited character set (and must be a simple
|
|
121
|
+
* string), this function can be used to decode a previously-encoded cookie
|
|
122
|
+
* value into a JavaScript string or other object.
|
|
123
|
+
*
|
|
124
|
+
* The default function is the global `decodeURIComponent`, which will decode
|
|
125
|
+
* any URL-encoded sequences into their byte representations.
|
|
126
|
+
*
|
|
127
|
+
* *Note* if an error is thrown from this function, the original, non-decoded
|
|
128
|
+
* cookie value will be returned as the cookie's value.
|
|
129
|
+
*/
|
|
130
|
+
decode?(value: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Custom function to filter parsing specific keys.
|
|
133
|
+
*/
|
|
134
|
+
filter?(key: string): boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Parse an HTTP Cookie header string and returning an object of all cookie
|
|
139
|
+
* name-value pairs.
|
|
140
|
+
*
|
|
141
|
+
* @param str the string representing a `Cookie` header value
|
|
142
|
+
* @param [options] object containing parsing options
|
|
143
|
+
*/
|
|
144
|
+
declare function parse(str: string, options?: CookieParseOptions): Record<string, string>;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
148
|
+
*
|
|
149
|
+
* @param name the name for the cookie
|
|
150
|
+
* @param value value to set the cookie to
|
|
151
|
+
* @param [options] object containing serialization options
|
|
152
|
+
* @throws {TypeError} when `maxAge` options is invalid
|
|
153
|
+
*/
|
|
154
|
+
declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
155
|
+
|
|
156
|
+
interface SetCookieParseOptions {
|
|
157
|
+
/**
|
|
158
|
+
* Custom decode function to use on cookie values.
|
|
159
|
+
*
|
|
160
|
+
* By default, `decodeURIComponent` is used.
|
|
161
|
+
*
|
|
162
|
+
* **Note:** If decoding fails, the original (undecoded) value will be used
|
|
163
|
+
*/
|
|
164
|
+
decode?: false | ((value: string) => string);
|
|
165
|
+
}
|
|
166
|
+
interface SetCookie {
|
|
167
|
+
/**
|
|
168
|
+
* Cookie name
|
|
169
|
+
*/
|
|
170
|
+
name: string;
|
|
171
|
+
/**
|
|
172
|
+
* Cookie value
|
|
173
|
+
*/
|
|
174
|
+
value: string;
|
|
175
|
+
/**
|
|
176
|
+
* Cookie path
|
|
177
|
+
*/
|
|
178
|
+
path?: string | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* Absolute expiration date for the cookie
|
|
181
|
+
*/
|
|
182
|
+
expires?: Date | undefined;
|
|
183
|
+
/**
|
|
184
|
+
* Relative max age of the cookie in seconds from when the client receives it (integer or undefined)
|
|
185
|
+
*
|
|
186
|
+
* Note: when using with express's res.cookie() method, multiply maxAge by 1000 to convert to milliseconds
|
|
187
|
+
*/
|
|
188
|
+
maxAge?: number | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* Domain for the cookie,
|
|
191
|
+
* May begin with "." to indicate the named domain or any subdomain of it
|
|
192
|
+
*/
|
|
193
|
+
domain?: string | undefined;
|
|
194
|
+
/**
|
|
195
|
+
* Indicates that this cookie should only be sent over HTTPs
|
|
196
|
+
*/
|
|
197
|
+
secure?: boolean | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Indicates that this cookie should not be accessible to client-side JavaScript
|
|
200
|
+
*/
|
|
201
|
+
httpOnly?: boolean | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* Indicates a cookie ought not to be sent along with cross-site requests
|
|
204
|
+
*/
|
|
205
|
+
sameSite?: string | undefined;
|
|
206
|
+
[key: string]: unknown;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.
|
|
211
|
+
*/
|
|
212
|
+
declare function parseSetCookie(setCookieValue: string, options?: SetCookieParseOptions): SetCookie;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
|
216
|
+
* that are within a single set-cookie field-value, such as in the Expires portion.
|
|
217
|
+
*
|
|
218
|
+
* See https://tools.ietf.org/html/rfc2616#section-4.2
|
|
219
|
+
*/
|
|
220
|
+
declare function splitSetCookieString(cookiesString: string | string[]): string[];
|
|
221
|
+
|
|
222
|
+
export { type CookieParseOptions, type CookieSerializeOptions, type SetCookie, type SetCookieParseOptions, parse, parseSetCookie, serialize, splitSetCookieString };
|
package/dist/index.d.ts
CHANGED
|
@@ -99,6 +99,17 @@ interface CookieSerializeOptions {
|
|
|
99
99
|
* not have an HTTPS connection.
|
|
100
100
|
*/
|
|
101
101
|
secure?: boolean | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](https://datatracker.ietf.org/doc/html/draft-cutler-httpbis-partitioned-cookies#section-2.1)
|
|
104
|
+
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
|
|
105
|
+
* `Partitioned` attribute is not set.
|
|
106
|
+
*
|
|
107
|
+
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
108
|
+
* This also means many clients may ignore this attribute until they understand it.
|
|
109
|
+
*
|
|
110
|
+
* More information can be found in the [proposal](https://github.com/privacycg/CHIPS).
|
|
111
|
+
*/
|
|
112
|
+
partitioned?: boolean;
|
|
102
113
|
}
|
|
103
114
|
/**
|
|
104
115
|
* Additional parsing options
|
|
@@ -117,6 +128,10 @@ interface CookieParseOptions {
|
|
|
117
128
|
* cookie value will be returned as the cookie's value.
|
|
118
129
|
*/
|
|
119
130
|
decode?(value: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Custom function to filter parsing specific keys.
|
|
133
|
+
*/
|
|
134
|
+
filter?(key: string): boolean;
|
|
120
135
|
}
|
|
121
136
|
|
|
122
137
|
/**
|
|
@@ -127,6 +142,7 @@ interface CookieParseOptions {
|
|
|
127
142
|
* @param [options] object containing parsing options
|
|
128
143
|
*/
|
|
129
144
|
declare function parse(str: string, options?: CookieParseOptions): Record<string, string>;
|
|
145
|
+
|
|
130
146
|
/**
|
|
131
147
|
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
132
148
|
*
|
|
@@ -137,4 +153,70 @@ declare function parse(str: string, options?: CookieParseOptions): Record<string
|
|
|
137
153
|
*/
|
|
138
154
|
declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
139
155
|
|
|
140
|
-
|
|
156
|
+
interface SetCookieParseOptions {
|
|
157
|
+
/**
|
|
158
|
+
* Custom decode function to use on cookie values.
|
|
159
|
+
*
|
|
160
|
+
* By default, `decodeURIComponent` is used.
|
|
161
|
+
*
|
|
162
|
+
* **Note:** If decoding fails, the original (undecoded) value will be used
|
|
163
|
+
*/
|
|
164
|
+
decode?: false | ((value: string) => string);
|
|
165
|
+
}
|
|
166
|
+
interface SetCookie {
|
|
167
|
+
/**
|
|
168
|
+
* Cookie name
|
|
169
|
+
*/
|
|
170
|
+
name: string;
|
|
171
|
+
/**
|
|
172
|
+
* Cookie value
|
|
173
|
+
*/
|
|
174
|
+
value: string;
|
|
175
|
+
/**
|
|
176
|
+
* Cookie path
|
|
177
|
+
*/
|
|
178
|
+
path?: string | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* Absolute expiration date for the cookie
|
|
181
|
+
*/
|
|
182
|
+
expires?: Date | undefined;
|
|
183
|
+
/**
|
|
184
|
+
* Relative max age of the cookie in seconds from when the client receives it (integer or undefined)
|
|
185
|
+
*
|
|
186
|
+
* Note: when using with express's res.cookie() method, multiply maxAge by 1000 to convert to milliseconds
|
|
187
|
+
*/
|
|
188
|
+
maxAge?: number | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* Domain for the cookie,
|
|
191
|
+
* May begin with "." to indicate the named domain or any subdomain of it
|
|
192
|
+
*/
|
|
193
|
+
domain?: string | undefined;
|
|
194
|
+
/**
|
|
195
|
+
* Indicates that this cookie should only be sent over HTTPs
|
|
196
|
+
*/
|
|
197
|
+
secure?: boolean | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Indicates that this cookie should not be accessible to client-side JavaScript
|
|
200
|
+
*/
|
|
201
|
+
httpOnly?: boolean | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* Indicates a cookie ought not to be sent along with cross-site requests
|
|
204
|
+
*/
|
|
205
|
+
sameSite?: string | undefined;
|
|
206
|
+
[key: string]: unknown;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.
|
|
211
|
+
*/
|
|
212
|
+
declare function parseSetCookie(setCookieValue: string, options?: SetCookieParseOptions): SetCookie;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
|
216
|
+
* that are within a single set-cookie field-value, such as in the Expires portion.
|
|
217
|
+
*
|
|
218
|
+
* See https://tools.ietf.org/html/rfc2616#section-4.2
|
|
219
|
+
*/
|
|
220
|
+
declare function splitSetCookieString(cookiesString: string | string[]): string[];
|
|
221
|
+
|
|
222
|
+
export { type CookieParseOptions, type CookieSerializeOptions, type SetCookie, type SetCookieParseOptions, parse, parseSetCookie, serialize, splitSetCookieString };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const fieldContentRegExp = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
|
|
2
1
|
function parse(str, options) {
|
|
3
2
|
if (typeof str !== "string") {
|
|
4
3
|
throw new TypeError("argument str must be a string");
|
|
@@ -31,9 +30,21 @@ function parse(str, options) {
|
|
|
31
30
|
}
|
|
32
31
|
return obj;
|
|
33
32
|
}
|
|
33
|
+
function decode(str) {
|
|
34
|
+
return str.includes("%") ? decodeURIComponent(str) : str;
|
|
35
|
+
}
|
|
36
|
+
function tryDecode(str, decode2) {
|
|
37
|
+
try {
|
|
38
|
+
return decode2(str);
|
|
39
|
+
} catch {
|
|
40
|
+
return str;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const fieldContentRegExp = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
|
|
34
45
|
function serialize(name, value, options) {
|
|
35
46
|
const opt = options || {};
|
|
36
|
-
const enc = opt.encode ||
|
|
47
|
+
const enc = opt.encode || encodeURIComponent;
|
|
37
48
|
if (typeof enc !== "function") {
|
|
38
49
|
throw new TypeError("option encode is invalid");
|
|
39
50
|
}
|
|
@@ -79,55 +90,169 @@ function serialize(name, value, options) {
|
|
|
79
90
|
if (opt.priority) {
|
|
80
91
|
const priority = typeof opt.priority === "string" ? opt.priority.toLowerCase() : opt.priority;
|
|
81
92
|
switch (priority) {
|
|
82
|
-
case "low":
|
|
93
|
+
case "low": {
|
|
83
94
|
str += "; Priority=Low";
|
|
84
95
|
break;
|
|
85
|
-
|
|
96
|
+
}
|
|
97
|
+
case "medium": {
|
|
86
98
|
str += "; Priority=Medium";
|
|
87
99
|
break;
|
|
88
|
-
|
|
100
|
+
}
|
|
101
|
+
case "high": {
|
|
89
102
|
str += "; Priority=High";
|
|
90
103
|
break;
|
|
91
|
-
|
|
104
|
+
}
|
|
105
|
+
default: {
|
|
92
106
|
throw new TypeError("option priority is invalid");
|
|
107
|
+
}
|
|
93
108
|
}
|
|
94
109
|
}
|
|
95
110
|
if (opt.sameSite) {
|
|
96
111
|
const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
97
112
|
switch (sameSite) {
|
|
98
|
-
case true:
|
|
113
|
+
case true: {
|
|
99
114
|
str += "; SameSite=Strict";
|
|
100
115
|
break;
|
|
101
|
-
|
|
116
|
+
}
|
|
117
|
+
case "lax": {
|
|
102
118
|
str += "; SameSite=Lax";
|
|
103
119
|
break;
|
|
104
|
-
|
|
120
|
+
}
|
|
121
|
+
case "strict": {
|
|
105
122
|
str += "; SameSite=Strict";
|
|
106
123
|
break;
|
|
107
|
-
|
|
124
|
+
}
|
|
125
|
+
case "none": {
|
|
108
126
|
str += "; SameSite=None";
|
|
109
127
|
break;
|
|
110
|
-
|
|
128
|
+
}
|
|
129
|
+
default: {
|
|
111
130
|
throw new TypeError("option sameSite is invalid");
|
|
131
|
+
}
|
|
112
132
|
}
|
|
113
133
|
}
|
|
134
|
+
if (opt.partitioned) {
|
|
135
|
+
str += "; Partitioned";
|
|
136
|
+
}
|
|
114
137
|
return str;
|
|
115
138
|
}
|
|
116
139
|
function isDate(val) {
|
|
117
140
|
return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
|
|
118
141
|
}
|
|
119
|
-
|
|
142
|
+
|
|
143
|
+
function parseSetCookie(setCookieValue, options) {
|
|
144
|
+
const parts = (setCookieValue || "").split(";").filter((str) => typeof str === "string" && !!str.trim());
|
|
145
|
+
const nameValuePairStr = parts.shift() || "";
|
|
146
|
+
const parsed = _parseNameValuePair(nameValuePairStr);
|
|
147
|
+
const name = parsed.name;
|
|
148
|
+
let value = parsed.value;
|
|
120
149
|
try {
|
|
121
|
-
|
|
150
|
+
value = options?.decode === false ? value : (options?.decode || decodeURIComponent)(value);
|
|
122
151
|
} catch {
|
|
123
|
-
return str;
|
|
124
152
|
}
|
|
153
|
+
const cookie = {
|
|
154
|
+
name,
|
|
155
|
+
value
|
|
156
|
+
};
|
|
157
|
+
for (const part of parts) {
|
|
158
|
+
const sides = part.split("=");
|
|
159
|
+
const partKey = (sides.shift() || "").trimStart().toLowerCase();
|
|
160
|
+
const partValue = sides.join("=");
|
|
161
|
+
switch (partKey) {
|
|
162
|
+
case "expires": {
|
|
163
|
+
cookie.expires = new Date(partValue);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case "max-age": {
|
|
167
|
+
cookie.maxAge = Number.parseInt(partValue, 10);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "secure": {
|
|
171
|
+
cookie.secure = true;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case "httponly": {
|
|
175
|
+
cookie.httpOnly = true;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
case "samesite": {
|
|
179
|
+
cookie.sameSite = partValue;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
default: {
|
|
183
|
+
cookie[partKey] = partValue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return cookie;
|
|
125
188
|
}
|
|
126
|
-
function
|
|
127
|
-
|
|
189
|
+
function _parseNameValuePair(nameValuePairStr) {
|
|
190
|
+
let name = "";
|
|
191
|
+
let value = "";
|
|
192
|
+
const nameValueArr = nameValuePairStr.split("=");
|
|
193
|
+
if (nameValueArr.length > 1) {
|
|
194
|
+
name = nameValueArr.shift();
|
|
195
|
+
value = nameValueArr.join("=");
|
|
196
|
+
} else {
|
|
197
|
+
value = nameValuePairStr;
|
|
198
|
+
}
|
|
199
|
+
return { name, value };
|
|
128
200
|
}
|
|
129
|
-
|
|
130
|
-
|
|
201
|
+
|
|
202
|
+
function splitSetCookieString(cookiesString) {
|
|
203
|
+
if (Array.isArray(cookiesString)) {
|
|
204
|
+
return cookiesString.flatMap((c) => splitSetCookieString(c));
|
|
205
|
+
}
|
|
206
|
+
if (typeof cookiesString !== "string") {
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
const cookiesStrings = [];
|
|
210
|
+
let pos = 0;
|
|
211
|
+
let start;
|
|
212
|
+
let ch;
|
|
213
|
+
let lastComma;
|
|
214
|
+
let nextStart;
|
|
215
|
+
let cookiesSeparatorFound;
|
|
216
|
+
const skipWhitespace = () => {
|
|
217
|
+
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
|
|
218
|
+
pos += 1;
|
|
219
|
+
}
|
|
220
|
+
return pos < cookiesString.length;
|
|
221
|
+
};
|
|
222
|
+
const notSpecialChar = () => {
|
|
223
|
+
ch = cookiesString.charAt(pos);
|
|
224
|
+
return ch !== "=" && ch !== ";" && ch !== ",";
|
|
225
|
+
};
|
|
226
|
+
while (pos < cookiesString.length) {
|
|
227
|
+
start = pos;
|
|
228
|
+
cookiesSeparatorFound = false;
|
|
229
|
+
while (skipWhitespace()) {
|
|
230
|
+
ch = cookiesString.charAt(pos);
|
|
231
|
+
if (ch === ",") {
|
|
232
|
+
lastComma = pos;
|
|
233
|
+
pos += 1;
|
|
234
|
+
skipWhitespace();
|
|
235
|
+
nextStart = pos;
|
|
236
|
+
while (pos < cookiesString.length && notSpecialChar()) {
|
|
237
|
+
pos += 1;
|
|
238
|
+
}
|
|
239
|
+
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
|
|
240
|
+
cookiesSeparatorFound = true;
|
|
241
|
+
pos = nextStart;
|
|
242
|
+
cookiesStrings.push(cookiesString.slice(start, lastComma));
|
|
243
|
+
start = pos;
|
|
244
|
+
} else {
|
|
245
|
+
pos = lastComma + 1;
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
pos += 1;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
|
|
252
|
+
cookiesStrings.push(cookiesString.slice(start, cookiesString.length));
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return cookiesStrings;
|
|
131
256
|
}
|
|
132
257
|
|
|
133
|
-
export { parse, serialize };
|
|
258
|
+
export { parse, parseSetCookie, serialize, splitSetCookieString };
|
package/package.json
CHANGED
|
@@ -1,40 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cookie-es",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"repository": "unjs/cookie-es",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.cts",
|
|
16
|
+
"default": "./dist/index.cjs"
|
|
17
|
+
}
|
|
13
18
|
}
|
|
14
19
|
},
|
|
15
20
|
"main": "./dist/index.cjs",
|
|
16
21
|
"module": "./dist/index.mjs",
|
|
17
|
-
"types": "./dist/index.d.
|
|
22
|
+
"types": "./dist/index.d.cts",
|
|
18
23
|
"files": [
|
|
19
24
|
"dist"
|
|
20
25
|
],
|
|
21
26
|
"scripts": {
|
|
22
27
|
"build": "unbuild",
|
|
23
|
-
"dev": "vitest",
|
|
24
|
-
"lint": "eslint --cache
|
|
25
|
-
"lint:fix": "eslint --cache
|
|
28
|
+
"dev": "vitest --coverage",
|
|
29
|
+
"lint": "eslint --cache . && prettier -c src test",
|
|
30
|
+
"lint:fix": "automd && eslint --cache . --fix && prettier -c src test -w",
|
|
26
31
|
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",
|
|
27
32
|
"test": "pnpm lint && vitest run --coverage"
|
|
28
33
|
},
|
|
29
34
|
"devDependencies": {
|
|
30
|
-
"@vitest/coverage-
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"eslint
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
35
|
+
"@vitest/coverage-v8": "^2.0.3",
|
|
36
|
+
"automd": "^0.3.8",
|
|
37
|
+
"changelogen": "^0.5.5",
|
|
38
|
+
"eslint": "^9.7.0",
|
|
39
|
+
"eslint-config-unjs": "^0.3.2",
|
|
40
|
+
"prettier": "^3.3.3",
|
|
41
|
+
"typescript": "^5.5.3",
|
|
42
|
+
"unbuild": "^2.0.0",
|
|
43
|
+
"vitest": "^2.0.3"
|
|
38
44
|
},
|
|
39
|
-
"packageManager": "pnpm@
|
|
40
|
-
}
|
|
45
|
+
"packageManager": "pnpm@9.5.0"
|
|
46
|
+
}
|