cookie-es 1.2.1 → 2.0.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 +9 -39
- package/dist/index.mjs +5 -1
- package/package.json +15 -24
- package/dist/index.cjs +0 -263
- package/dist/index.d.cts +0 -222
- package/dist/index.d.ts +0 -222
package/README.md
CHANGED
|
@@ -1,47 +1,29 @@
|
|
|
1
|
-
|
|
2
1
|
# 🍪 cookie-es
|
|
3
2
|
|
|
4
|
-
<!-- automd:badges bundlejs -->
|
|
3
|
+
<!-- automd:badges bundlejs packagephobia codecov -->
|
|
5
4
|
|
|
6
5
|
[](https://npmjs.com/package/cookie-es)
|
|
7
|
-
[](https://
|
|
6
|
+
[](https://npm.chart.dev/cookie-es)
|
|
8
7
|
[](https://bundlejs.com/?q=cookie-es)
|
|
8
|
+
[](https://packagephobia.com/result?p=cookie-es)
|
|
9
|
+
[](https://codecov.io/gh/unjs/cookie-es)
|
|
9
10
|
|
|
10
11
|
<!-- /automd -->
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
ESM ready [`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/cookie) and [set-cookie-parser](https://github.com/nfriedly/set-cookie-parser) with built-in types.
|
|
13
14
|
|
|
14
15
|
## Usage
|
|
15
16
|
|
|
16
17
|
Install:
|
|
17
18
|
|
|
18
|
-
<!-- automd:pm-install -->
|
|
19
|
-
|
|
20
19
|
```sh
|
|
21
|
-
# ✨ Auto-detect
|
|
20
|
+
# ✨ Auto-detect (npm, yarn, pnpm, bun, deno)
|
|
22
21
|
npx nypm install cookie-es
|
|
23
|
-
|
|
24
|
-
# npm
|
|
25
|
-
npm install cookie-es
|
|
26
|
-
|
|
27
|
-
# yarn
|
|
28
|
-
yarn add cookie-es
|
|
29
|
-
|
|
30
|
-
# pnpm
|
|
31
|
-
pnpm install cookie-es
|
|
32
|
-
|
|
33
|
-
# bun
|
|
34
|
-
bun install cookie-es
|
|
35
22
|
```
|
|
36
23
|
|
|
37
|
-
<!-- /automd-->
|
|
38
|
-
|
|
39
24
|
Import:
|
|
40
25
|
|
|
41
|
-
|
|
42
|
-
<!-- automd:jsimport cdn cjs src=./src/index.ts -->
|
|
43
|
-
|
|
44
|
-
**ESM** (Node.js, Bun)
|
|
26
|
+
**ESM** (Node.js, Bun, Deno)
|
|
45
27
|
|
|
46
28
|
```js
|
|
47
29
|
import {
|
|
@@ -52,17 +34,6 @@ import {
|
|
|
52
34
|
} from "cookie-es";
|
|
53
35
|
```
|
|
54
36
|
|
|
55
|
-
**CommonJS** (Legacy Node.js)
|
|
56
|
-
|
|
57
|
-
```js
|
|
58
|
-
const {
|
|
59
|
-
parse,
|
|
60
|
-
serialize,
|
|
61
|
-
parseSetCookie,
|
|
62
|
-
splitSetCookieString,
|
|
63
|
-
} = require("cookie-es");
|
|
64
|
-
```
|
|
65
|
-
|
|
66
37
|
**CDN** (Deno, Bun and Browsers)
|
|
67
38
|
|
|
68
39
|
```js
|
|
@@ -74,9 +45,8 @@ import {
|
|
|
74
45
|
} from "https://esm.sh/cookie-es";
|
|
75
46
|
```
|
|
76
47
|
|
|
77
|
-
<!-- /automd -->
|
|
78
|
-
|
|
79
|
-
|
|
80
48
|
## License
|
|
81
49
|
|
|
82
50
|
[MIT](./LICENSE)
|
|
51
|
+
|
|
52
|
+
Based on [jshttp/cookie](https://github.com/jshttp/cookie) (Roman Shtylman and hristopher Wilson) and [nfriedly/set-cookie-parser](https://github.com/nfriedly/set-cookie-parser) (Nathan Friedly).
|
package/dist/index.mjs
CHANGED
|
@@ -19,6 +19,10 @@ function parse(str, options) {
|
|
|
19
19
|
continue;
|
|
20
20
|
}
|
|
21
21
|
const key = str.slice(index, eqIdx).trim();
|
|
22
|
+
if (opt?.filter && !opt?.filter(key)) {
|
|
23
|
+
index = endIdx + 1;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
22
26
|
if (void 0 === obj[key]) {
|
|
23
27
|
let val = str.slice(eqIdx + 1, endIdx).trim();
|
|
24
28
|
if (val.codePointAt(0) === 34) {
|
|
@@ -249,7 +253,7 @@ function splitSetCookieString(cookiesString) {
|
|
|
249
253
|
}
|
|
250
254
|
}
|
|
251
255
|
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
|
|
252
|
-
cookiesStrings.push(cookiesString.slice(start
|
|
256
|
+
cookiesStrings.push(cookiesString.slice(start));
|
|
253
257
|
}
|
|
254
258
|
}
|
|
255
259
|
return cookiesStrings;
|
package/package.json
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cookie-es",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"repository": "unjs/cookie-es",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
9
|
-
"
|
|
10
|
-
|
|
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
|
-
}
|
|
18
|
-
}
|
|
9
|
+
"types": "./dist/index.d.mts",
|
|
10
|
+
"default": "./dist/index.mjs"
|
|
19
11
|
},
|
|
20
|
-
"
|
|
21
|
-
"module": "./dist/index.mjs",
|
|
22
|
-
"types": "./dist/index.d.cts",
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
23
13
|
"files": [
|
|
24
14
|
"dist"
|
|
25
15
|
],
|
|
@@ -32,15 +22,16 @@
|
|
|
32
22
|
"test": "pnpm lint && vitest run --coverage"
|
|
33
23
|
},
|
|
34
24
|
"devDependencies": {
|
|
35
|
-
"@
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"eslint
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
25
|
+
"@types/node": "^22.13.5",
|
|
26
|
+
"@vitest/coverage-v8": "^3.0.7",
|
|
27
|
+
"automd": "^0.4.0",
|
|
28
|
+
"changelogen": "^0.6.0",
|
|
29
|
+
"eslint": "^9.21.0",
|
|
30
|
+
"eslint-config-unjs": "^0.4.2",
|
|
31
|
+
"prettier": "^3.5.2",
|
|
32
|
+
"typescript": "^5.7.3",
|
|
33
|
+
"unbuild": "^3.5.0",
|
|
34
|
+
"vitest": "^3.0.7"
|
|
44
35
|
},
|
|
45
|
-
"packageManager": "pnpm@
|
|
36
|
+
"packageManager": "pnpm@10.5.2"
|
|
46
37
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function parse(str, options) {
|
|
4
|
-
if (typeof str !== "string") {
|
|
5
|
-
throw new TypeError("argument str must be a string");
|
|
6
|
-
}
|
|
7
|
-
const obj = {};
|
|
8
|
-
const opt = options || {};
|
|
9
|
-
const dec = opt.decode || decode;
|
|
10
|
-
let index = 0;
|
|
11
|
-
while (index < str.length) {
|
|
12
|
-
const eqIdx = str.indexOf("=", index);
|
|
13
|
-
if (eqIdx === -1) {
|
|
14
|
-
break;
|
|
15
|
-
}
|
|
16
|
-
let endIdx = str.indexOf(";", index);
|
|
17
|
-
if (endIdx === -1) {
|
|
18
|
-
endIdx = str.length;
|
|
19
|
-
} else if (endIdx < eqIdx) {
|
|
20
|
-
index = str.lastIndexOf(";", eqIdx - 1) + 1;
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
const key = str.slice(index, eqIdx).trim();
|
|
24
|
-
if (void 0 === obj[key]) {
|
|
25
|
-
let val = str.slice(eqIdx + 1, endIdx).trim();
|
|
26
|
-
if (val.codePointAt(0) === 34) {
|
|
27
|
-
val = val.slice(1, -1);
|
|
28
|
-
}
|
|
29
|
-
obj[key] = tryDecode(val, dec);
|
|
30
|
-
}
|
|
31
|
-
index = endIdx + 1;
|
|
32
|
-
}
|
|
33
|
-
return obj;
|
|
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]+$/;
|
|
47
|
-
function serialize(name, value, options) {
|
|
48
|
-
const opt = options || {};
|
|
49
|
-
const enc = opt.encode || encodeURIComponent;
|
|
50
|
-
if (typeof enc !== "function") {
|
|
51
|
-
throw new TypeError("option encode is invalid");
|
|
52
|
-
}
|
|
53
|
-
if (!fieldContentRegExp.test(name)) {
|
|
54
|
-
throw new TypeError("argument name is invalid");
|
|
55
|
-
}
|
|
56
|
-
const encodedValue = enc(value);
|
|
57
|
-
if (encodedValue && !fieldContentRegExp.test(encodedValue)) {
|
|
58
|
-
throw new TypeError("argument val is invalid");
|
|
59
|
-
}
|
|
60
|
-
let str = name + "=" + encodedValue;
|
|
61
|
-
if (void 0 !== opt.maxAge && opt.maxAge !== null) {
|
|
62
|
-
const maxAge = opt.maxAge - 0;
|
|
63
|
-
if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) {
|
|
64
|
-
throw new TypeError("option maxAge is invalid");
|
|
65
|
-
}
|
|
66
|
-
str += "; Max-Age=" + Math.floor(maxAge);
|
|
67
|
-
}
|
|
68
|
-
if (opt.domain) {
|
|
69
|
-
if (!fieldContentRegExp.test(opt.domain)) {
|
|
70
|
-
throw new TypeError("option domain is invalid");
|
|
71
|
-
}
|
|
72
|
-
str += "; Domain=" + opt.domain;
|
|
73
|
-
}
|
|
74
|
-
if (opt.path) {
|
|
75
|
-
if (!fieldContentRegExp.test(opt.path)) {
|
|
76
|
-
throw new TypeError("option path is invalid");
|
|
77
|
-
}
|
|
78
|
-
str += "; Path=" + opt.path;
|
|
79
|
-
}
|
|
80
|
-
if (opt.expires) {
|
|
81
|
-
if (!isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) {
|
|
82
|
-
throw new TypeError("option expires is invalid");
|
|
83
|
-
}
|
|
84
|
-
str += "; Expires=" + opt.expires.toUTCString();
|
|
85
|
-
}
|
|
86
|
-
if (opt.httpOnly) {
|
|
87
|
-
str += "; HttpOnly";
|
|
88
|
-
}
|
|
89
|
-
if (opt.secure) {
|
|
90
|
-
str += "; Secure";
|
|
91
|
-
}
|
|
92
|
-
if (opt.priority) {
|
|
93
|
-
const priority = typeof opt.priority === "string" ? opt.priority.toLowerCase() : opt.priority;
|
|
94
|
-
switch (priority) {
|
|
95
|
-
case "low": {
|
|
96
|
-
str += "; Priority=Low";
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
case "medium": {
|
|
100
|
-
str += "; Priority=Medium";
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
case "high": {
|
|
104
|
-
str += "; Priority=High";
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
default: {
|
|
108
|
-
throw new TypeError("option priority is invalid");
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (opt.sameSite) {
|
|
113
|
-
const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
114
|
-
switch (sameSite) {
|
|
115
|
-
case true: {
|
|
116
|
-
str += "; SameSite=Strict";
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
case "lax": {
|
|
120
|
-
str += "; SameSite=Lax";
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
case "strict": {
|
|
124
|
-
str += "; SameSite=Strict";
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
case "none": {
|
|
128
|
-
str += "; SameSite=None";
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
default: {
|
|
132
|
-
throw new TypeError("option sameSite is invalid");
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (opt.partitioned) {
|
|
137
|
-
str += "; Partitioned";
|
|
138
|
-
}
|
|
139
|
-
return str;
|
|
140
|
-
}
|
|
141
|
-
function isDate(val) {
|
|
142
|
-
return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
|
|
143
|
-
}
|
|
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;
|
|
151
|
-
try {
|
|
152
|
-
value = options?.decode === false ? value : (options?.decode || decodeURIComponent)(value);
|
|
153
|
-
} catch {
|
|
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;
|
|
190
|
-
}
|
|
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 };
|
|
202
|
-
}
|
|
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;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
exports.parse = parse;
|
|
261
|
-
exports.parseSetCookie = parseSetCookie;
|
|
262
|
-
exports.serialize = serialize;
|
|
263
|
-
exports.splitSetCookieString = splitSetCookieString;
|
package/dist/index.d.cts
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
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?: true | false | "lax" | "strict" | "none" | 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
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
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?: true | false | "lax" | "strict" | "none" | 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 };
|