cookie-es 1.1.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 +20 -5
- package/dist/index.cjs +124 -9
- package/dist/index.d.cts +74 -3
- package/dist/index.d.mts +74 -3
- package/dist/index.d.ts +74 -3
- package/dist/index.mjs +123 -10
- package/package.json +22 -17
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,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
# cookie-es
|
|
2
|
+
# 🍪 cookie-es
|
|
3
3
|
|
|
4
4
|
<!-- automd:badges bundlejs -->
|
|
5
5
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
<!-- /automd -->
|
|
11
11
|
|
|
12
|
-
|
|
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. 🎁
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
@@ -44,19 +44,34 @@ Import:
|
|
|
44
44
|
**ESM** (Node.js, Bun)
|
|
45
45
|
|
|
46
46
|
```js
|
|
47
|
-
import {
|
|
47
|
+
import {
|
|
48
|
+
parse,
|
|
49
|
+
serialize,
|
|
50
|
+
parseSetCookie,
|
|
51
|
+
splitSetCookieString,
|
|
52
|
+
} from "cookie-es";
|
|
48
53
|
```
|
|
49
54
|
|
|
50
55
|
**CommonJS** (Legacy Node.js)
|
|
51
56
|
|
|
52
57
|
```js
|
|
53
|
-
const {
|
|
58
|
+
const {
|
|
59
|
+
parse,
|
|
60
|
+
serialize,
|
|
61
|
+
parseSetCookie,
|
|
62
|
+
splitSetCookieString,
|
|
63
|
+
} = require("cookie-es");
|
|
54
64
|
```
|
|
55
65
|
|
|
56
66
|
**CDN** (Deno, Bun and Browsers)
|
|
57
67
|
|
|
58
68
|
```js
|
|
59
|
-
import {
|
|
69
|
+
import {
|
|
70
|
+
parse,
|
|
71
|
+
serialize,
|
|
72
|
+
parseSetCookie,
|
|
73
|
+
splitSetCookieString,
|
|
74
|
+
} from "https://esm.sh/cookie-es";
|
|
60
75
|
```
|
|
61
76
|
|
|
62
77
|
<!-- /automd -->
|
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
|
}
|
|
@@ -130,19 +141,123 @@ function serialize(name, value, options) {
|
|
|
130
141
|
function isDate(val) {
|
|
131
142
|
return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
|
|
132
143
|
}
|
|
133
|
-
|
|
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;
|
|
134
151
|
try {
|
|
135
|
-
|
|
152
|
+
value = options?.decode === false ? value : (options?.decode || decodeURIComponent)(value);
|
|
136
153
|
} catch {
|
|
137
|
-
return str;
|
|
138
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;
|
|
139
190
|
}
|
|
140
|
-
function
|
|
141
|
-
|
|
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 };
|
|
142
202
|
}
|
|
143
|
-
|
|
144
|
-
|
|
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;
|
|
145
258
|
}
|
|
146
259
|
|
|
147
260
|
exports.parse = parse;
|
|
261
|
+
exports.parseSetCookie = parseSetCookie;
|
|
148
262
|
exports.serialize = serialize;
|
|
263
|
+
exports.splitSetCookieString = splitSetCookieString;
|
package/dist/index.d.cts
CHANGED
|
@@ -100,14 +100,14 @@ interface CookieSerializeOptions {
|
|
|
100
100
|
*/
|
|
101
101
|
secure?: boolean | undefined;
|
|
102
102
|
/**
|
|
103
|
-
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](
|
|
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
104
|
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
|
|
105
105
|
* `Partitioned` attribute is not set.
|
|
106
106
|
*
|
|
107
107
|
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
108
108
|
* This also means many clients may ignore this attribute until they understand it.
|
|
109
109
|
*
|
|
110
|
-
* More information
|
|
110
|
+
* More information can be found in the [proposal](https://github.com/privacycg/CHIPS).
|
|
111
111
|
*/
|
|
112
112
|
partitioned?: boolean;
|
|
113
113
|
}
|
|
@@ -128,6 +128,10 @@ interface CookieParseOptions {
|
|
|
128
128
|
* cookie value will be returned as the cookie's value.
|
|
129
129
|
*/
|
|
130
130
|
decode?(value: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Custom function to filter parsing specific keys.
|
|
133
|
+
*/
|
|
134
|
+
filter?(key: string): boolean;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
/**
|
|
@@ -138,6 +142,7 @@ interface CookieParseOptions {
|
|
|
138
142
|
* @param [options] object containing parsing options
|
|
139
143
|
*/
|
|
140
144
|
declare function parse(str: string, options?: CookieParseOptions): Record<string, string>;
|
|
145
|
+
|
|
141
146
|
/**
|
|
142
147
|
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
143
148
|
*
|
|
@@ -148,4 +153,70 @@ declare function parse(str: string, options?: CookieParseOptions): Record<string
|
|
|
148
153
|
*/
|
|
149
154
|
declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
150
155
|
|
|
151
|
-
|
|
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
CHANGED
|
@@ -100,14 +100,14 @@ interface CookieSerializeOptions {
|
|
|
100
100
|
*/
|
|
101
101
|
secure?: boolean | undefined;
|
|
102
102
|
/**
|
|
103
|
-
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](
|
|
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
104
|
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
|
|
105
105
|
* `Partitioned` attribute is not set.
|
|
106
106
|
*
|
|
107
107
|
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
108
108
|
* This also means many clients may ignore this attribute until they understand it.
|
|
109
109
|
*
|
|
110
|
-
* More information
|
|
110
|
+
* More information can be found in the [proposal](https://github.com/privacycg/CHIPS).
|
|
111
111
|
*/
|
|
112
112
|
partitioned?: boolean;
|
|
113
113
|
}
|
|
@@ -128,6 +128,10 @@ interface CookieParseOptions {
|
|
|
128
128
|
* cookie value will be returned as the cookie's value.
|
|
129
129
|
*/
|
|
130
130
|
decode?(value: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Custom function to filter parsing specific keys.
|
|
133
|
+
*/
|
|
134
|
+
filter?(key: string): boolean;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
/**
|
|
@@ -138,6 +142,7 @@ interface CookieParseOptions {
|
|
|
138
142
|
* @param [options] object containing parsing options
|
|
139
143
|
*/
|
|
140
144
|
declare function parse(str: string, options?: CookieParseOptions): Record<string, string>;
|
|
145
|
+
|
|
141
146
|
/**
|
|
142
147
|
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
143
148
|
*
|
|
@@ -148,4 +153,70 @@ declare function parse(str: string, options?: CookieParseOptions): Record<string
|
|
|
148
153
|
*/
|
|
149
154
|
declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
150
155
|
|
|
151
|
-
|
|
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
|
@@ -100,14 +100,14 @@ interface CookieSerializeOptions {
|
|
|
100
100
|
*/
|
|
101
101
|
secure?: boolean | undefined;
|
|
102
102
|
/**
|
|
103
|
-
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](
|
|
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
104
|
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
|
|
105
105
|
* `Partitioned` attribute is not set.
|
|
106
106
|
*
|
|
107
107
|
* **note** This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
108
108
|
* This also means many clients may ignore this attribute until they understand it.
|
|
109
109
|
*
|
|
110
|
-
* More information
|
|
110
|
+
* More information can be found in the [proposal](https://github.com/privacycg/CHIPS).
|
|
111
111
|
*/
|
|
112
112
|
partitioned?: boolean;
|
|
113
113
|
}
|
|
@@ -128,6 +128,10 @@ interface CookieParseOptions {
|
|
|
128
128
|
* cookie value will be returned as the cookie's value.
|
|
129
129
|
*/
|
|
130
130
|
decode?(value: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Custom function to filter parsing specific keys.
|
|
133
|
+
*/
|
|
134
|
+
filter?(key: string): boolean;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
/**
|
|
@@ -138,6 +142,7 @@ interface CookieParseOptions {
|
|
|
138
142
|
* @param [options] object containing parsing options
|
|
139
143
|
*/
|
|
140
144
|
declare function parse(str: string, options?: CookieParseOptions): Record<string, string>;
|
|
145
|
+
|
|
141
146
|
/**
|
|
142
147
|
* Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
143
148
|
*
|
|
@@ -148,4 +153,70 @@ declare function parse(str: string, options?: CookieParseOptions): Record<string
|
|
|
148
153
|
*/
|
|
149
154
|
declare function serialize(name: string, value: string, options?: CookieSerializeOptions): string;
|
|
150
155
|
|
|
151
|
-
|
|
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
|
}
|
|
@@ -128,18 +139,120 @@ function serialize(name, value, options) {
|
|
|
128
139
|
function isDate(val) {
|
|
129
140
|
return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
|
|
130
141
|
}
|
|
131
|
-
|
|
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;
|
|
132
149
|
try {
|
|
133
|
-
|
|
150
|
+
value = options?.decode === false ? value : (options?.decode || decodeURIComponent)(value);
|
|
134
151
|
} catch {
|
|
135
|
-
return str;
|
|
136
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;
|
|
137
188
|
}
|
|
138
|
-
function
|
|
139
|
-
|
|
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 };
|
|
140
200
|
}
|
|
141
|
-
|
|
142
|
-
|
|
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;
|
|
143
256
|
}
|
|
144
257
|
|
|
145
|
-
export { parse, serialize };
|
|
258
|
+
export { parse, parseSetCookie, serialize, splitSetCookieString };
|
package/package.json
CHANGED
|
@@ -1,41 +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": "automd && 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-v8": "^
|
|
31
|
-
"automd": "^0.3.
|
|
35
|
+
"@vitest/coverage-v8": "^2.0.3",
|
|
36
|
+
"automd": "^0.3.8",
|
|
32
37
|
"changelogen": "^0.5.5",
|
|
33
|
-
"eslint": "^
|
|
34
|
-
"eslint-config-unjs": "^0.2
|
|
35
|
-
"prettier": "^3.
|
|
36
|
-
"typescript": "^5.
|
|
38
|
+
"eslint": "^9.7.0",
|
|
39
|
+
"eslint-config-unjs": "^0.3.2",
|
|
40
|
+
"prettier": "^3.3.3",
|
|
41
|
+
"typescript": "^5.5.3",
|
|
37
42
|
"unbuild": "^2.0.0",
|
|
38
|
-
"vitest": "^
|
|
43
|
+
"vitest": "^2.0.3"
|
|
39
44
|
},
|
|
40
|
-
"packageManager": "pnpm@
|
|
41
|
-
}
|
|
45
|
+
"packageManager": "pnpm@9.5.0"
|
|
46
|
+
}
|