cookie-es 1.2.2 → 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 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
  [![npm version](https://img.shields.io/npm/v/cookie-es)](https://npmjs.com/package/cookie-es)
7
- [![npm downloads](https://img.shields.io/npm/dm/cookie-es)](https://npmjs.com/package/cookie-es)
6
+ [![npm downloads](https://img.shields.io/npm/dm/cookie-es)](https://npm.chart.dev/cookie-es)
8
7
  [![bundle size](https://img.shields.io/bundlejs/size/cookie-es)](https://bundlejs.com/?q=cookie-es)
8
+ [![install size](https://badgen.net/packagephobia/install/cookie-es)](https://packagephobia.com/result?p=cookie-es)
9
+ [![codecov](https://img.shields.io/codecov/c/gh/unjs/cookie-es)](https://codecov.io/gh/unjs/cookie-es)
9
10
 
10
11
  <!-- /automd -->
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/cookie) and [set-cookie-parser](https://github.com/nfriedly/set-cookie-parser) with dual ESM/CJS exports and bundled types. 🎁
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
@@ -253,7 +253,7 @@ function splitSetCookieString(cookiesString) {
253
253
  }
254
254
  }
255
255
  if (!cookiesSeparatorFound || pos >= cookiesString.length) {
256
- cookiesStrings.push(cookiesString.slice(start, cookiesString.length));
256
+ cookiesStrings.push(cookiesString.slice(start));
257
257
  }
258
258
  }
259
259
  return cookiesStrings;
package/package.json CHANGED
@@ -1,25 +1,15 @@
1
1
  {
2
2
  "name": "cookie-es",
3
- "version": "1.2.2",
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
- "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
- }
18
- }
9
+ "types": "./dist/index.d.mts",
10
+ "default": "./dist/index.mjs"
19
11
  },
20
- "main": "./dist/index.cjs",
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
- "@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"
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@9.5.0"
36
+ "packageManager": "pnpm@10.5.2"
46
37
  }
package/dist/index.cjs DELETED
@@ -1,267 +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 (opt?.filter && !opt?.filter(key)) {
25
- index = endIdx + 1;
26
- continue;
27
- }
28
- if (void 0 === obj[key]) {
29
- let val = str.slice(eqIdx + 1, endIdx).trim();
30
- if (val.codePointAt(0) === 34) {
31
- val = val.slice(1, -1);
32
- }
33
- obj[key] = tryDecode(val, dec);
34
- }
35
- index = endIdx + 1;
36
- }
37
- return obj;
38
- }
39
- function decode(str) {
40
- return str.includes("%") ? decodeURIComponent(str) : str;
41
- }
42
- function tryDecode(str, decode2) {
43
- try {
44
- return decode2(str);
45
- } catch {
46
- return str;
47
- }
48
- }
49
-
50
- const fieldContentRegExp = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
51
- function serialize(name, value, options) {
52
- const opt = options || {};
53
- const enc = opt.encode || encodeURIComponent;
54
- if (typeof enc !== "function") {
55
- throw new TypeError("option encode is invalid");
56
- }
57
- if (!fieldContentRegExp.test(name)) {
58
- throw new TypeError("argument name is invalid");
59
- }
60
- const encodedValue = enc(value);
61
- if (encodedValue && !fieldContentRegExp.test(encodedValue)) {
62
- throw new TypeError("argument val is invalid");
63
- }
64
- let str = name + "=" + encodedValue;
65
- if (void 0 !== opt.maxAge && opt.maxAge !== null) {
66
- const maxAge = opt.maxAge - 0;
67
- if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) {
68
- throw new TypeError("option maxAge is invalid");
69
- }
70
- str += "; Max-Age=" + Math.floor(maxAge);
71
- }
72
- if (opt.domain) {
73
- if (!fieldContentRegExp.test(opt.domain)) {
74
- throw new TypeError("option domain is invalid");
75
- }
76
- str += "; Domain=" + opt.domain;
77
- }
78
- if (opt.path) {
79
- if (!fieldContentRegExp.test(opt.path)) {
80
- throw new TypeError("option path is invalid");
81
- }
82
- str += "; Path=" + opt.path;
83
- }
84
- if (opt.expires) {
85
- if (!isDate(opt.expires) || Number.isNaN(opt.expires.valueOf())) {
86
- throw new TypeError("option expires is invalid");
87
- }
88
- str += "; Expires=" + opt.expires.toUTCString();
89
- }
90
- if (opt.httpOnly) {
91
- str += "; HttpOnly";
92
- }
93
- if (opt.secure) {
94
- str += "; Secure";
95
- }
96
- if (opt.priority) {
97
- const priority = typeof opt.priority === "string" ? opt.priority.toLowerCase() : opt.priority;
98
- switch (priority) {
99
- case "low": {
100
- str += "; Priority=Low";
101
- break;
102
- }
103
- case "medium": {
104
- str += "; Priority=Medium";
105
- break;
106
- }
107
- case "high": {
108
- str += "; Priority=High";
109
- break;
110
- }
111
- default: {
112
- throw new TypeError("option priority is invalid");
113
- }
114
- }
115
- }
116
- if (opt.sameSite) {
117
- const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
118
- switch (sameSite) {
119
- case true: {
120
- str += "; SameSite=Strict";
121
- break;
122
- }
123
- case "lax": {
124
- str += "; SameSite=Lax";
125
- break;
126
- }
127
- case "strict": {
128
- str += "; SameSite=Strict";
129
- break;
130
- }
131
- case "none": {
132
- str += "; SameSite=None";
133
- break;
134
- }
135
- default: {
136
- throw new TypeError("option sameSite is invalid");
137
- }
138
- }
139
- }
140
- if (opt.partitioned) {
141
- str += "; Partitioned";
142
- }
143
- return str;
144
- }
145
- function isDate(val) {
146
- return Object.prototype.toString.call(val) === "[object Date]" || val instanceof Date;
147
- }
148
-
149
- function parseSetCookie(setCookieValue, options) {
150
- const parts = (setCookieValue || "").split(";").filter((str) => typeof str === "string" && !!str.trim());
151
- const nameValuePairStr = parts.shift() || "";
152
- const parsed = _parseNameValuePair(nameValuePairStr);
153
- const name = parsed.name;
154
- let value = parsed.value;
155
- try {
156
- value = options?.decode === false ? value : (options?.decode || decodeURIComponent)(value);
157
- } catch {
158
- }
159
- const cookie = {
160
- name,
161
- value
162
- };
163
- for (const part of parts) {
164
- const sides = part.split("=");
165
- const partKey = (sides.shift() || "").trimStart().toLowerCase();
166
- const partValue = sides.join("=");
167
- switch (partKey) {
168
- case "expires": {
169
- cookie.expires = new Date(partValue);
170
- break;
171
- }
172
- case "max-age": {
173
- cookie.maxAge = Number.parseInt(partValue, 10);
174
- break;
175
- }
176
- case "secure": {
177
- cookie.secure = true;
178
- break;
179
- }
180
- case "httponly": {
181
- cookie.httpOnly = true;
182
- break;
183
- }
184
- case "samesite": {
185
- cookie.sameSite = partValue;
186
- break;
187
- }
188
- default: {
189
- cookie[partKey] = partValue;
190
- }
191
- }
192
- }
193
- return cookie;
194
- }
195
- function _parseNameValuePair(nameValuePairStr) {
196
- let name = "";
197
- let value = "";
198
- const nameValueArr = nameValuePairStr.split("=");
199
- if (nameValueArr.length > 1) {
200
- name = nameValueArr.shift();
201
- value = nameValueArr.join("=");
202
- } else {
203
- value = nameValuePairStr;
204
- }
205
- return { name, value };
206
- }
207
-
208
- function splitSetCookieString(cookiesString) {
209
- if (Array.isArray(cookiesString)) {
210
- return cookiesString.flatMap((c) => splitSetCookieString(c));
211
- }
212
- if (typeof cookiesString !== "string") {
213
- return [];
214
- }
215
- const cookiesStrings = [];
216
- let pos = 0;
217
- let start;
218
- let ch;
219
- let lastComma;
220
- let nextStart;
221
- let cookiesSeparatorFound;
222
- const skipWhitespace = () => {
223
- while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
224
- pos += 1;
225
- }
226
- return pos < cookiesString.length;
227
- };
228
- const notSpecialChar = () => {
229
- ch = cookiesString.charAt(pos);
230
- return ch !== "=" && ch !== ";" && ch !== ",";
231
- };
232
- while (pos < cookiesString.length) {
233
- start = pos;
234
- cookiesSeparatorFound = false;
235
- while (skipWhitespace()) {
236
- ch = cookiesString.charAt(pos);
237
- if (ch === ",") {
238
- lastComma = pos;
239
- pos += 1;
240
- skipWhitespace();
241
- nextStart = pos;
242
- while (pos < cookiesString.length && notSpecialChar()) {
243
- pos += 1;
244
- }
245
- if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
246
- cookiesSeparatorFound = true;
247
- pos = nextStart;
248
- cookiesStrings.push(cookiesString.slice(start, lastComma));
249
- start = pos;
250
- } else {
251
- pos = lastComma + 1;
252
- }
253
- } else {
254
- pos += 1;
255
- }
256
- }
257
- if (!cookiesSeparatorFound || pos >= cookiesString.length) {
258
- cookiesStrings.push(cookiesString.slice(start, cookiesString.length));
259
- }
260
- }
261
- return cookiesStrings;
262
- }
263
-
264
- exports.parse = parse;
265
- exports.parseSetCookie = parseSetCookie;
266
- exports.serialize = serialize;
267
- 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 };