Skip to content

Commit ad9c960

Browse files
authored
Update option documentation (#186)
1 parent c4a2597 commit ad9c960

File tree

2 files changed

+47
-72
lines changed

2 files changed

+47
-72
lines changed

README.md

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const cookie = require("cookie");
2121

2222
### cookie.parse(str, options)
2323

24-
Parse an HTTP `Cookie` header string and returning an object of all cookie name-value pairs.
24+
Parse a HTTP `Cookie` header string and returning an object of all cookie name-value pairs.
2525
The `str` argument is the string representing a `Cookie` header value and `options` is an
2626
optional object containing additional parsing options.
2727

@@ -36,15 +36,13 @@ const cookies = cookie.parse("foo=bar; equation=E%3Dmc%5E2");
3636

3737
##### decode
3838

39-
Specifies a function that will be used to decode a cookie's value. Since the value of a cookie
40-
has a limited character set (and must be a simple string), this function can be used to decode
39+
Specifies a function that will be used to decode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
40+
Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode
4141
a previously-encoded cookie value into a JavaScript string.
4242

43-
The default function is the global `decodeURIComponent`, which will decode any URL-encoded
44-
sequences into their byte representations.
45-
46-
If an error is thrown from this function, the original, non-decoded cookie value will
47-
be returned as the cookie's value.
43+
The default function is the global `decodeURIComponent`, wrapped in a `try..catch`. If an error
44+
is thrown it will return the cookie's original value. If you provide your own encode/decode
45+
scheme you must ensure errors are appropriately handled.
4846

4947
### cookie.serialize(name, value, options)
5048

@@ -63,17 +61,15 @@ const setCookie = cookie.serialize("foo", "bar");
6361

6462
##### encode
6563

66-
Specifies a function that will be used to encode a cookie's value. Since value of a cookie
67-
has a limited character set (and must be a simple string), this function can be used to encode
68-
a value into a string suited for a cookie's value.
64+
Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
65+
Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
66+
a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
6967

70-
The default function is the global `encodeURIComponent`, which will encode a JavaScript string
71-
into UTF-8 byte sequences and then URL-encode any that fall outside of the cookie range.
68+
The default function is the global `encodeURIComponent`.
7269

7370
##### maxAge
7471

7572
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).
76-
The given number will be converted to an integer by rounding down. By default, no maximum age is set.
7773

7874
The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
7975
`maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
@@ -82,8 +78,7 @@ so if both are set, they should point to the same date and time.
8278
##### expires
8379

8480
Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
85-
By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and
86-
will delete it on a condition like exiting a web browser application.
81+
When no expiration is set clients consider this a "non-persistent cookie" and delete it the current session is over.
8782

8883
The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
8984
`maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
@@ -92,49 +87,41 @@ so if both are set, they should point to the same date and time.
9287
##### domain
9388

9489
Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
95-
By default, no domain is set, and most clients will consider the cookie to apply to only the current domain.
90+
When no domain is set clients consider the cookie to apply to the current domain only.
9691

9792
##### path
9893

99-
Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4). By default, the path
100-
is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
94+
Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
95+
When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
10196

10297
##### httpOnly
10398

104-
Specifies the `boolean` value for the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6). When truthy,
105-
the `HttpOnly` attribute is set, otherwise it is not. By default, the `HttpOnly` attribute is not set.
106-
107-
Be careful when setting this to `true`, as compliant clients will not allow client-side
108-
JavaScript to see the cookie in `document.cookie`.
99+
Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
100+
When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
109101

110102
##### secure
111103

112-
Specifies the `boolean` value for the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5). When truthy,
113-
the `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
114-
115-
Be careful when setting this to `true`, as compliant clients will not send the cookie back to
116-
the server in the future if the browser does not have an HTTPS connection.
104+
Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
105+
When enabled, clients will only send the cookie back if the browser has a HTTPS connection.
117106

118107
##### partitioned
119108

120-
Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/)
121-
attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
122-
`Partitioned` attribute is not set.
109+
Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
110+
When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
123111

124112
This is an attribute that has not yet been fully standardized, and may change in the future.
125-
This also means many clients may ignore this attribute until they understand it. More information
113+
This also means clients may ignore this attribute until they understand it. More information
126114
about can be found in [the proposal](https://github.com/privacycg/CHIPS).
127115

128116
##### priority
129117

130-
Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
118+
Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
131119

132120
- `'low'` will set the `Priority` attribute to `Low`.
133121
- `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
134122
- `'high'` will set the `Priority` attribute to `High`.
135123

136-
More information about the different priority levels can be found in
137-
[the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
124+
More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
138125

139126
##### sameSite
140127

@@ -145,8 +132,7 @@ Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ie
145132
- `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
146133
- `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
147134

148-
More information about the different enforcement levels can be found in
149-
[the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
135+
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).
150136

151137
## Example
152138

src/index.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ const NullObject = /* @__PURE__ */ (() => {
7171
*/
7272
export interface ParseOptions {
7373
/**
74-
* Specifies a function that will be used to decode a cookie's value. Since
75-
* the value of a cookie has a limited character set (and must be a simple
76-
* string), this function can be used to decode a previously-encoded cookie
77-
* value into a JavaScript string.
74+
* Specifies a function that will be used to decode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
75+
* Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode
76+
* a previously-encoded cookie value into a JavaScript string.
7877
*
79-
* Note: if an error is thrown from this function, the original, non-decoded
80-
* cookie value will be returned as the cookie's value.
78+
* The default function is the global `decodeURIComponent`, wrapped in a `try..catch`. If an error
79+
* is thrown it will return the cookie's original value. If you provide your own encode/decode
80+
* scheme you must ensure errors are appropriately handled.
8181
*
82-
* @default decodeURIComponent
82+
* @default decode
8383
*/
8484
decode?: (str: string) => string | undefined;
8585
}
@@ -155,16 +155,15 @@ function endIndex(str: string, index: number, min: number) {
155155
*/
156156
export interface SerializeOptions {
157157
/**
158-
* Specifies a function that will be used to encode a cookie's value. Since
159-
* value of a cookie has a limited character set (and must be a simple string),
160-
* this function can be used to encode a value into a string suited for a cookie's value.
158+
* Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
159+
* Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
160+
* a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
161161
*
162162
* @default encodeURIComponent
163163
*/
164164
encode?: (str: string) => string;
165165
/**
166166
* 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).
167-
* The given number will be converted to an integer by rounding down. By default, no maximum age is set.
168167
*
169168
* The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
170169
* `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
@@ -173,8 +172,7 @@ export interface SerializeOptions {
173172
maxAge?: number;
174173
/**
175174
* Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
176-
* By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and
177-
* will delete it on a condition like exiting a web browser application.
175+
* When no expiration is set clients consider this a "non-persistent cookie" and delete it the current session is over.
178176
*
179177
* The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
180178
* `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
@@ -183,37 +181,30 @@ export interface SerializeOptions {
183181
expires?: Date;
184182
/**
185183
* Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
186-
* By default, no domain is set, and most clients will consider the cookie to apply to only the current domain.
184+
* When no domain is set clients consider the cookie to apply to the current domain only.
187185
*/
188186
domain?: string;
189187
/**
190-
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4). By default, the path
191-
* is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
188+
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
189+
* When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
192190
*/
193191
path?: string;
194192
/**
195-
* Specifies the `boolean` value for the [`HttpOnly` `Set-Cookie` attribute][rfc-6265-5.2.6]. When truthy,
196-
* the `HttpOnly` attribute is set, otherwise it is not. By default, the `HttpOnly` attribute is not set.
197-
*
198-
* Be careful when setting this to `true`, as compliant clients will not allow client-side
199-
* JavaScript to see the cookie in `document.cookie`.
193+
* Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
194+
* When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
200195
*/
201196
httpOnly?: boolean;
202197
/**
203-
* Specifies the `boolean` value for the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5). When truthy,
204-
* the `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
205-
*
206-
* Be careful when setting this to `true`, as compliant clients will not send the cookie back to
207-
* the server in the future if the browser does not have an HTTPS connection.
198+
* Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
199+
* When enabled, clients will only send the cookie back if the browser has a HTTPS connection.
208200
*/
209201
secure?: boolean;
210202
/**
211-
* Specifies the `boolean` value for the [`Partitioned` `Set-Cookie`](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/)
212-
* attribute. When truthy, the `Partitioned` attribute is set, otherwise it is not. By default, the
213-
* `Partitioned` attribute is not set.
203+
* Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
204+
* When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
214205
*
215206
* This is an attribute that has not yet been fully standardized, and may change in the future.
216-
* This also means many clients may ignore this attribute until they understand it. More information
207+
* This also means clients may ignore this attribute until they understand it. More information
217208
* about can be found in [the proposal](https://github.com/privacycg/CHIPS).
218209
*/
219210
partitioned?: boolean;
@@ -224,8 +215,7 @@ export interface SerializeOptions {
224215
* - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
225216
* - `'high'` will set the `Priority` attribute to `High`.
226217
*
227-
* More information about the different priority levels can be found in
228-
* [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
218+
* More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
229219
*/
230220
priority?: "low" | "medium" | "high";
231221
/**
@@ -236,8 +226,7 @@ export interface SerializeOptions {
236226
* - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
237227
* - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
238228
*
239-
* More information about the different enforcement levels can be found in
240-
* [the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
229+
* 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).
241230
*/
242231
sameSite?: boolean | "lax" | "strict" | "none";
243232
}

0 commit comments

Comments
 (0)