cookie-es 1.2.2 → 3.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 +121 -43
- package/dist/index.d.mts +217 -195
- package/dist/index.mjs +395 -250
- package/package.json +24 -32
- package/dist/index.cjs +0 -267
- package/dist/index.d.cts +0 -222
- package/dist/index.d.ts +0 -222
package/README.md
CHANGED
|
@@ -1,82 +1,160 @@
|
|
|
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
|
-
|
|
14
|
-
## Usage
|
|
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 TypeScript types. Compliant with [RFC 6265bis](https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html).
|
|
15
14
|
|
|
16
|
-
Install
|
|
17
|
-
|
|
18
|
-
<!-- automd:pm-install -->
|
|
15
|
+
## Install
|
|
19
16
|
|
|
20
17
|
```sh
|
|
21
|
-
# ✨ Auto-detect
|
|
18
|
+
# ✨ Auto-detect (npm, yarn, pnpm, bun, deno)
|
|
22
19
|
npx nypm install cookie-es
|
|
20
|
+
```
|
|
23
21
|
|
|
24
|
-
|
|
25
|
-
npm install cookie-es
|
|
22
|
+
## Import
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
yarn add cookie-es
|
|
24
|
+
**ESM** (Node.js, Bun, Deno)
|
|
29
25
|
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
```js
|
|
27
|
+
import {
|
|
28
|
+
parseCookie,
|
|
29
|
+
parseSetCookie,
|
|
30
|
+
serializeCookie,
|
|
31
|
+
stringifyCookie,
|
|
32
|
+
splitSetCookieString,
|
|
33
|
+
} from "cookie-es";
|
|
34
|
+
```
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
**CDN** (Deno, Bun and Browsers)
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import {
|
|
40
|
+
parseCookie,
|
|
41
|
+
parseSetCookie,
|
|
42
|
+
serializeCookie,
|
|
43
|
+
stringifyCookie,
|
|
44
|
+
splitSetCookieString,
|
|
45
|
+
} from "https://esm.sh/cookie-es";
|
|
35
46
|
```
|
|
36
47
|
|
|
37
|
-
|
|
48
|
+
## API
|
|
38
49
|
|
|
39
|
-
|
|
50
|
+
### `parseCookie(str, options?)`
|
|
40
51
|
|
|
52
|
+
Parse a `Cookie` header string into an object. First occurrence wins for duplicate names.
|
|
41
53
|
|
|
42
|
-
|
|
54
|
+
```js
|
|
55
|
+
parseCookie("foo=bar; equation=E%3Dmc%5E2");
|
|
56
|
+
// { foo: "bar", equation: "E=mc^2" }
|
|
57
|
+
|
|
58
|
+
// Custom decoder
|
|
59
|
+
parseCookie("foo=bar", { decode: (v) => v });
|
|
43
60
|
|
|
44
|
-
|
|
61
|
+
// Only parse specific keys
|
|
62
|
+
parseCookie("a=1; b=2; c=3", { filter: (key) => key !== "b" });
|
|
63
|
+
// { a: "1", c: "3" }
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `parseSetCookie(str, options?)`
|
|
67
|
+
|
|
68
|
+
Parse a `Set-Cookie` header string into an object with all cookie attributes.
|
|
45
69
|
|
|
46
70
|
```js
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
71
|
+
parseSetCookie(
|
|
72
|
+
"id=abc; Domain=example.com; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=3600; Partitioned; Priority=High",
|
|
73
|
+
);
|
|
74
|
+
// {
|
|
75
|
+
// name: "id",
|
|
76
|
+
// value: "abc",
|
|
77
|
+
// domain: "example.com",
|
|
78
|
+
// path: "/",
|
|
79
|
+
// httpOnly: true,
|
|
80
|
+
// secure: true,
|
|
81
|
+
// sameSite: "lax",
|
|
82
|
+
// maxAge: 3600,
|
|
83
|
+
// partitioned: true,
|
|
84
|
+
// priority: "high",
|
|
85
|
+
// }
|
|
53
86
|
```
|
|
54
87
|
|
|
55
|
-
|
|
88
|
+
Supports `decode` option (custom function or `false` to skip decoding). Returns `undefined` for cookies with forbidden names (prototype pollution protection) or when both name and value are empty ([RFC 6265bis](https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html) sec 5.7).
|
|
89
|
+
|
|
90
|
+
### `serializeCookie(name, value, options?)`
|
|
91
|
+
|
|
92
|
+
Serialize a cookie name-value pair into a `Set-Cookie` header string.
|
|
56
93
|
|
|
57
94
|
```js
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
95
|
+
serializeCookie("foo", "bar", { httpOnly: true, secure: true, maxAge: 3600 });
|
|
96
|
+
// "foo=bar; Max-Age=3600; HttpOnly; Secure"
|
|
97
|
+
|
|
98
|
+
// Also accepts a cookie object
|
|
99
|
+
serializeCookie({
|
|
100
|
+
name: "foo",
|
|
101
|
+
value: "bar",
|
|
102
|
+
domain: "example.com",
|
|
103
|
+
path: "/",
|
|
104
|
+
sameSite: "lax",
|
|
105
|
+
});
|
|
106
|
+
// "foo=bar; Domain=example.com; Path=/; SameSite=Lax"
|
|
64
107
|
```
|
|
65
108
|
|
|
66
|
-
|
|
109
|
+
Supported attributes: `maxAge`, `expires`, `domain`, `path`, `httpOnly`, `secure`, `sameSite`, `priority`, `partitioned`. Use `encode` option for custom value encoding (default: `encodeURIComponent`).
|
|
110
|
+
|
|
111
|
+
> [!NOTE]
|
|
112
|
+
> `parse` and `serialize` are available as shorter aliases for `parseCookie` and `serializeCookie`.
|
|
113
|
+
|
|
114
|
+
### `stringifyCookie(cookies, options?)`
|
|
115
|
+
|
|
116
|
+
Stringify a cookies object into an HTTP `Cookie` header string.
|
|
67
117
|
|
|
68
118
|
```js
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
serialize,
|
|
72
|
-
parseSetCookie,
|
|
73
|
-
splitSetCookieString,
|
|
74
|
-
} from "https://esm.sh/cookie-es";
|
|
119
|
+
stringifyCookie({ foo: "bar", baz: "qux" });
|
|
120
|
+
// "foo=bar; baz=qux"
|
|
75
121
|
```
|
|
76
122
|
|
|
77
|
-
|
|
123
|
+
### `splitSetCookieString(input)`
|
|
78
124
|
|
|
125
|
+
Split comma-joined `Set-Cookie` headers into individual strings. Correctly handles commas within cookie attributes like `Expires` dates.
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
splitSetCookieString(
|
|
129
|
+
"foo=bar; Expires=Thu, 01 Jan 2026 00:00:00 GMT, baz=qux",
|
|
130
|
+
);
|
|
131
|
+
// ["foo=bar; Expires=Thu, 01 Jan 2026 00:00:00 GMT", "baz=qux"]
|
|
132
|
+
|
|
133
|
+
// Also accepts an array
|
|
134
|
+
splitSetCookieString(["a=1, b=2", "c=3"]);
|
|
135
|
+
// ["a=1", "b=2", "c=3"]
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Parsing Options
|
|
139
|
+
|
|
140
|
+
### `allowMultiple`
|
|
141
|
+
|
|
142
|
+
By default, when a cookie name appears more than once, only the first value is kept. Set `allowMultiple: true` to collect all values into an array:
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import { parseCookie } from "cookie-es";
|
|
146
|
+
|
|
147
|
+
// Default: first value wins
|
|
148
|
+
parseCookie("foo=a;bar=b;foo=c");
|
|
149
|
+
// => { foo: "a", bar: "b" }
|
|
150
|
+
|
|
151
|
+
// With allowMultiple: duplicates return arrays
|
|
152
|
+
parseCookie("foo=a;bar=b;foo=c", { allowMultiple: true });
|
|
153
|
+
// => { foo: ["a", "c"], bar: "b" }
|
|
154
|
+
```
|
|
79
155
|
|
|
80
156
|
## License
|
|
81
157
|
|
|
82
158
|
[MIT](./LICENSE)
|
|
159
|
+
|
|
160
|
+
Based on [jshttp/cookie](https://github.com/jshttp/cookie) (Roman Shtylman and Douglas Christopher Wilson) and [nfriedly/set-cookie-parser](https://github.com/nfriedly/set-cookie-parser) (Nathan Friedly).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,216 +1,238 @@
|
|
|
1
|
+
//#region src/cookie/types.d.ts
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
+
* Parse options.
|
|
3
4
|
*/
|
|
5
|
+
interface CookieParseOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Specifies a function that will be used to decode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
|
|
8
|
+
* Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode
|
|
9
|
+
* a previously-encoded cookie value into a JavaScript string.
|
|
10
|
+
*
|
|
11
|
+
* The default function is the global `decodeURIComponent`, wrapped in a `try..catch`. If an error
|
|
12
|
+
* is thrown it will return the cookie's original value. If you provide your own encode/decode
|
|
13
|
+
* scheme you must ensure errors are appropriately handled.
|
|
14
|
+
*
|
|
15
|
+
* @default decode
|
|
16
|
+
*/
|
|
17
|
+
decode?: (str: string) => string | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Custom function to filter parsing specific keys.
|
|
20
|
+
*/
|
|
21
|
+
filter?(key: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* When enabled, duplicate cookie names will return an array of values
|
|
24
|
+
* instead of only the first value.
|
|
25
|
+
*/
|
|
26
|
+
allowMultiple?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Cookies object.
|
|
30
|
+
*/
|
|
31
|
+
type Cookies = Record<string, string | undefined>;
|
|
4
32
|
/**
|
|
5
|
-
*
|
|
33
|
+
* Cookies object when `allowMultiple` is enabled.
|
|
6
34
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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;
|
|
35
|
+
type MultiCookies = Record<string, string | string[] | undefined>;
|
|
36
|
+
/**
|
|
37
|
+
* Stringify options.
|
|
38
|
+
*/
|
|
39
|
+
interface CookieStringifyOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
|
|
42
|
+
* Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
|
|
43
|
+
* a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
|
|
44
|
+
*
|
|
45
|
+
* @default encodeURIComponent
|
|
46
|
+
*/
|
|
47
|
+
encode?: (str: string) => string;
|
|
113
48
|
}
|
|
114
49
|
/**
|
|
115
|
-
*
|
|
50
|
+
* Set-Cookie object.
|
|
116
51
|
*/
|
|
117
|
-
interface
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
52
|
+
interface SetCookie {
|
|
53
|
+
/**
|
|
54
|
+
* Specifies the name of the cookie.
|
|
55
|
+
*/
|
|
56
|
+
name: string;
|
|
57
|
+
/**
|
|
58
|
+
* Specifies the string to be the value for the cookie.
|
|
59
|
+
*/
|
|
60
|
+
value: string | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2).
|
|
63
|
+
*
|
|
64
|
+
* The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
|
|
65
|
+
* `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
|
|
66
|
+
* so if both are set, they should point to the same date and time.
|
|
67
|
+
*/
|
|
68
|
+
maxAge?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
|
|
71
|
+
* When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over.
|
|
72
|
+
*
|
|
73
|
+
* The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
|
|
74
|
+
* `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
|
|
75
|
+
* so if both are set, they should point to the same date and time.
|
|
76
|
+
*/
|
|
77
|
+
expires?: Date;
|
|
78
|
+
/**
|
|
79
|
+
* Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
|
|
80
|
+
* When no domain is set, clients consider the cookie to apply to the current domain only.
|
|
81
|
+
*/
|
|
82
|
+
domain?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
|
|
85
|
+
* When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
|
|
86
|
+
*/
|
|
87
|
+
path?: string;
|
|
88
|
+
/**
|
|
89
|
+
* Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
|
|
90
|
+
* When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
|
|
91
|
+
*/
|
|
92
|
+
httpOnly?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
|
|
95
|
+
* When enabled, clients will only send the cookie back if the browser has an HTTPS connection.
|
|
96
|
+
*/
|
|
97
|
+
secure?: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
|
|
100
|
+
* When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
|
|
101
|
+
*
|
|
102
|
+
* This is an attribute that has not yet been fully standardized, and may change in the future.
|
|
103
|
+
* This also means clients may ignore this attribute until they understand it. More information
|
|
104
|
+
* about can be found in [the proposal](https://github.com/privacycg/CHIPS).
|
|
105
|
+
*/
|
|
106
|
+
partitioned?: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
|
|
109
|
+
*
|
|
110
|
+
* - `'low'` will set the `Priority` attribute to `Low`.
|
|
111
|
+
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
|
|
112
|
+
* - `'high'` will set the `Priority` attribute to `High`.
|
|
113
|
+
*
|
|
114
|
+
* More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
|
|
115
|
+
*/
|
|
116
|
+
priority?: "low" | "medium" | "high";
|
|
117
|
+
/**
|
|
118
|
+
* Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
|
|
119
|
+
*
|
|
120
|
+
* - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
|
|
121
|
+
* - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
|
|
122
|
+
* - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
|
|
123
|
+
* - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
|
|
124
|
+
*
|
|
125
|
+
* More information about enforcement levels can be found in [the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
|
|
126
|
+
*/
|
|
127
|
+
sameSite?: boolean | "lax" | "strict" | "none";
|
|
135
128
|
}
|
|
136
|
-
|
|
137
129
|
/**
|
|
138
|
-
*
|
|
139
|
-
|
|
130
|
+
* Backward compatibility serialize options.
|
|
131
|
+
*/
|
|
132
|
+
type CookieSerializeOptions = CookieStringifyOptions & Omit<SetCookie, "name" | "value">;
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/cookie/parse.d.ts
|
|
135
|
+
/**
|
|
136
|
+
* Parse a `Cookie` header.
|
|
140
137
|
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
138
|
+
* Parse the given cookie header string into an object
|
|
139
|
+
* The object has the various cookies as keys(names) => values
|
|
143
140
|
*/
|
|
144
|
-
declare function parse(str: string, options
|
|
145
|
-
|
|
141
|
+
declare function parse(str: string, options: CookieParseOptions & {
|
|
142
|
+
allowMultiple: true;
|
|
143
|
+
}): MultiCookies;
|
|
144
|
+
declare function parse(str: string, options?: CookieParseOptions): Cookies;
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/cookie/serialize.d.ts
|
|
146
147
|
/**
|
|
147
|
-
*
|
|
148
|
+
* Stringifies an object into an HTTP `Cookie` header.
|
|
149
|
+
*/
|
|
150
|
+
declare function stringifyCookie(cookie: Cookies, options?: CookieStringifyOptions): string;
|
|
151
|
+
/**
|
|
152
|
+
* Serialize data into a cookie header.
|
|
153
|
+
*
|
|
154
|
+
* Serialize a name value pair into a cookie string suitable for
|
|
155
|
+
* http headers. An optional options object specifies cookie parameters.
|
|
148
156
|
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
* @param [options] object containing serialization options
|
|
152
|
-
* @throws {TypeError} when `maxAge` options is invalid
|
|
157
|
+
* serialize('foo', 'bar', { httpOnly: true })
|
|
158
|
+
* => "foo=bar; httpOnly"
|
|
153
159
|
*/
|
|
154
|
-
declare function serialize(
|
|
155
|
-
|
|
160
|
+
declare function serialize(cookie: SetCookie, options?: CookieStringifyOptions): string;
|
|
161
|
+
declare function serialize(name: string, val: string, options?: CookieSerializeOptions): string;
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/set-cookie/types.d.ts
|
|
156
164
|
interface SetCookieParseOptions {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Custom decode function to use on cookie values.
|
|
167
|
+
*
|
|
168
|
+
* By default, `decodeURIComponent` is used.
|
|
169
|
+
*
|
|
170
|
+
* **Note:** If decoding fails, the original (undecoded) value will be used
|
|
171
|
+
*/
|
|
172
|
+
decode?: false | ((value: string) => string);
|
|
165
173
|
}
|
|
166
|
-
interface SetCookie {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
174
|
+
interface SetCookie$1 {
|
|
175
|
+
/**
|
|
176
|
+
* Cookie name
|
|
177
|
+
*/
|
|
178
|
+
name: string;
|
|
179
|
+
/**
|
|
180
|
+
* Cookie value
|
|
181
|
+
*/
|
|
182
|
+
value: string;
|
|
183
|
+
/**
|
|
184
|
+
* Cookie path
|
|
185
|
+
*/
|
|
186
|
+
path?: string | undefined;
|
|
187
|
+
/**
|
|
188
|
+
* Absolute expiration date for the cookie
|
|
189
|
+
*/
|
|
190
|
+
expires?: Date | undefined;
|
|
191
|
+
/**
|
|
192
|
+
* Relative max age of the cookie in seconds from when the client receives it (integer or undefined)
|
|
193
|
+
*
|
|
194
|
+
* Note: when using with express's res.cookie() method, multiply maxAge by 1000 to convert to milliseconds
|
|
195
|
+
*/
|
|
196
|
+
maxAge?: number | undefined;
|
|
197
|
+
/**
|
|
198
|
+
* Domain for the cookie,
|
|
199
|
+
* May begin with "." to indicate the named domain or any subdomain of it
|
|
200
|
+
*/
|
|
201
|
+
domain?: string | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* Indicates that this cookie should only be sent over HTTPs
|
|
204
|
+
*/
|
|
205
|
+
secure?: boolean | undefined;
|
|
206
|
+
/**
|
|
207
|
+
* Indicates that this cookie should not be accessible to client-side JavaScript
|
|
208
|
+
*/
|
|
209
|
+
httpOnly?: boolean | undefined;
|
|
210
|
+
/**
|
|
211
|
+
* Indicates a cookie ought not to be sent along with cross-site requests
|
|
212
|
+
*/
|
|
213
|
+
sameSite?: true | false | "lax" | "strict" | "none" | undefined;
|
|
214
|
+
/**
|
|
215
|
+
* Indicates that the cookie should be stored using partitioned storage
|
|
216
|
+
*
|
|
217
|
+
* See https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies
|
|
218
|
+
*/
|
|
219
|
+
partitioned?: boolean | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* Indicates the priority of the cookie
|
|
222
|
+
*
|
|
223
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#prioritylow_medium_high
|
|
224
|
+
*/
|
|
225
|
+
priority?: "low" | "medium" | "high" | undefined;
|
|
226
|
+
[key: string]: unknown;
|
|
207
227
|
}
|
|
208
|
-
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/set-cookie/parse.d.ts
|
|
209
230
|
/**
|
|
210
231
|
* Parse a [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) header string into an object.
|
|
211
232
|
*/
|
|
212
|
-
declare function parseSetCookie(
|
|
213
|
-
|
|
233
|
+
declare function parseSetCookie(str: string, options?: SetCookieParseOptions): SetCookie$1 | undefined;
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/set-cookie/split.d.ts
|
|
214
236
|
/**
|
|
215
237
|
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
|
216
238
|
* that are within a single set-cookie field-value, such as in the Expires portion.
|
|
@@ -218,5 +240,5 @@ declare function parseSetCookie(setCookieValue: string, options?: SetCookieParse
|
|
|
218
240
|
* See https://tools.ietf.org/html/rfc2616#section-4.2
|
|
219
241
|
*/
|
|
220
242
|
declare function splitSetCookieString(cookiesString: string | string[]): string[];
|
|
221
|
-
|
|
222
|
-
export { type CookieParseOptions, type CookieSerializeOptions, type SetCookie, type SetCookieParseOptions, parse, parseSetCookie, serialize, splitSetCookieString };
|
|
243
|
+
//#endregion
|
|
244
|
+
export { type CookieParseOptions, type CookieSerializeOptions, type CookieStringifyOptions, type Cookies, type MultiCookies, type SetCookie, type SetCookieParseOptions, parse, parse as parseCookie, parseSetCookie, serialize, serialize as serializeCookie, splitSetCookieString, stringifyCookie };
|