tough-cookie 5.0.0 → 5.1.0-rc.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 +18 -8
- package/dist/cookie/cookie.d.ts +11 -0
- package/dist/cookie/cookie.js +30 -0
- package/dist/cookie/cookieJar.d.ts +1 -1
- package/dist/cookie/cookieJar.js +1 -1
- package/dist/pathMatch.js +1 -2
- package/dist/utils.js +2 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +20 -16
package/README.md
CHANGED
|
@@ -22,12 +22,16 @@ yarn add tough-cookie
|
|
|
22
22
|
import { Cookie, CookieJar } from 'tough-cookie'
|
|
23
23
|
|
|
24
24
|
// parse a `Cookie` request header
|
|
25
|
-
const reqCookies = 'ID=298zf09hf012fh2; csrf=u32t4o3tb3gg43; _gat=1'
|
|
25
|
+
const reqCookies = 'ID=298zf09hf012fh2; csrf=u32t4o3tb3gg43; _gat=1'
|
|
26
|
+
.split(';')
|
|
27
|
+
.map(Cookie.parse)
|
|
26
28
|
// generate a `Cookie` request header
|
|
27
|
-
const cookieHeader = reqCookies.map(cookie => cookie.cookieString()).join(';')
|
|
29
|
+
const cookieHeader = reqCookies.map((cookie) => cookie.cookieString()).join(';')
|
|
28
30
|
|
|
29
31
|
// parse a Set-Cookie response header
|
|
30
|
-
const resCookie = Cookie.parse(
|
|
32
|
+
const resCookie = Cookie.parse(
|
|
33
|
+
'foo=bar; Domain=example.com; Path=/; Expires=Tue, 21 Oct 2025 00:00:00 GMT',
|
|
34
|
+
)
|
|
31
35
|
// generate a Set-Cookie response header
|
|
32
36
|
const setCookieHeader = cookie.toString()
|
|
33
37
|
|
|
@@ -58,8 +62,14 @@ import { CookieJar } from 'tough-cookie'
|
|
|
58
62
|
const cookieJar = new CookieJar() // uses the in-memory store by default
|
|
59
63
|
|
|
60
64
|
// storing cookies with various SameSite attributes
|
|
61
|
-
await cookieJar.setCookie(
|
|
62
|
-
|
|
65
|
+
await cookieJar.setCookie(
|
|
66
|
+
'strict=authorized; SameSite=strict',
|
|
67
|
+
'http://example.com/index.html',
|
|
68
|
+
)
|
|
69
|
+
await cookieJar.setCookie(
|
|
70
|
+
'lax=okay; SameSite=lax',
|
|
71
|
+
'http://example.com/index.html',
|
|
72
|
+
)
|
|
63
73
|
await cookieJar.setCookie('normal=whatever', 'http://example.com/index.html')
|
|
64
74
|
|
|
65
75
|
// retrieving cookies using a SameSite context
|
|
@@ -105,7 +115,7 @@ You can define this functionality by passing in the `prefixSecurity` option to `
|
|
|
105
115
|
import { CookieJar, MemoryCookieStore } from 'tough-cookie'
|
|
106
116
|
|
|
107
117
|
const cookieJar = new CookieJar(new MemoryCookieStore(), {
|
|
108
|
-
prefixSecurity: 'silent'
|
|
118
|
+
prefixSecurity: 'silent',
|
|
109
119
|
})
|
|
110
120
|
|
|
111
121
|
// this cookie will be silently ignored since the url is insecure (http)
|
|
@@ -132,8 +142,8 @@ for older versions of node, and we will do so in consultation with our community
|
|
|
132
142
|
|
|
133
143
|
[npm-badge]: https://img.shields.io/npm/v/tough-cookie.svg?style=flat
|
|
134
144
|
[npm-repo]: https://www.npmjs.com/package/tough-cookie
|
|
135
|
-
[ci-badge]: https://github.com/salesforce/tough-cookie/actions/workflows/
|
|
136
|
-
[ci-url]: https://github.com/salesforce/tough-cookie/actions/workflows/
|
|
145
|
+
[ci-badge]: https://github.com/salesforce/tough-cookie/actions/workflows/ci.yaml/badge.svg
|
|
146
|
+
[ci-url]: https://github.com/salesforce/tough-cookie/actions/workflows/ci.yaml
|
|
137
147
|
[rfc6265-badge]: https://img.shields.io/badge/RFC-6265-flat?labelColor=000000&color=666666
|
|
138
148
|
[rfc6265-tracker]: https://datatracker.ietf.org/doc/rfc6265/
|
|
139
149
|
[rfc6265bis-badge]: https://img.shields.io/badge/RFC-6265bis-flat?labelColor=000000&color=666666
|
package/dist/cookie/cookie.d.ts
CHANGED
|
@@ -217,6 +217,17 @@ export declare class Cookie {
|
|
|
217
217
|
* @param now - can be used to provide a time offset (instead of {@link Cookie.lastAccessed}) to use when calculating the "Max-Age" value
|
|
218
218
|
*/
|
|
219
219
|
expiryTime(now?: Date): number | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* Similar to {@link Cookie.expiryTime}, computes the absolute unix-epoch milliseconds that this cookie expires and returns it as a Date.
|
|
222
|
+
*
|
|
223
|
+
* The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The {@link Cookie.lastAccessed} attribute
|
|
224
|
+
* (or the `now` parameter if given) is used to offset the {@link Cookie.maxAge} attribute.
|
|
225
|
+
*
|
|
226
|
+
* If Expires ({@link Cookie.expires}) is set, that's returned.
|
|
227
|
+
*
|
|
228
|
+
* @param now - can be used to provide a time offset (instead of {@link Cookie.lastAccessed}) to use when calculating the "Max-Age" value
|
|
229
|
+
*/
|
|
230
|
+
expiryDate(now?: Date): Date | undefined;
|
|
220
231
|
/**
|
|
221
232
|
* Indicates if the cookie has been persisted to a store or not.
|
|
222
233
|
* @public
|
package/dist/cookie/cookie.js
CHANGED
|
@@ -675,6 +675,36 @@ class Cookie {
|
|
|
675
675
|
}
|
|
676
676
|
return this.expires ? this.expires.getTime() : undefined;
|
|
677
677
|
}
|
|
678
|
+
/**
|
|
679
|
+
* Similar to {@link Cookie.expiryTime}, computes the absolute unix-epoch milliseconds that this cookie expires and returns it as a Date.
|
|
680
|
+
*
|
|
681
|
+
* The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The {@link Cookie.lastAccessed} attribute
|
|
682
|
+
* (or the `now` parameter if given) is used to offset the {@link Cookie.maxAge} attribute.
|
|
683
|
+
*
|
|
684
|
+
* If Expires ({@link Cookie.expires}) is set, that's returned.
|
|
685
|
+
*
|
|
686
|
+
* @param now - can be used to provide a time offset (instead of {@link Cookie.lastAccessed}) to use when calculating the "Max-Age" value
|
|
687
|
+
*/
|
|
688
|
+
expiryDate(now) {
|
|
689
|
+
const millisec = this.expiryTime(now);
|
|
690
|
+
if (millisec == Infinity) {
|
|
691
|
+
// The 31-bit value of 2147483647000 was chosen to be the MAX_TIME representable
|
|
692
|
+
// in tough-cookie though MDN states that the actual maximum value for a Date is 8.64e15.
|
|
693
|
+
// I'm guessing this is due to the Y2038 problem that would affect systems that store
|
|
694
|
+
// unix time as 32-bit integers.
|
|
695
|
+
// See:
|
|
696
|
+
// - https://github.com/salesforce/tough-cookie/commit/0616f70bf725e00c63d442544ad230c4f8b23357
|
|
697
|
+
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_epoch_timestamps_and_invalid_date
|
|
698
|
+
// - https://en.wikipedia.org/wiki/Year_2038_problem
|
|
699
|
+
return new Date(2147483647000);
|
|
700
|
+
}
|
|
701
|
+
else if (millisec == -Infinity) {
|
|
702
|
+
return new Date(0);
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
return millisec == undefined ? undefined : new Date(millisec);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
678
708
|
/**
|
|
679
709
|
* Indicates if the cookie has been persisted to a store or not.
|
|
680
710
|
* @public
|
|
@@ -310,7 +310,7 @@ export declare class CookieJar {
|
|
|
310
310
|
* @param url - The domain to store the cookie with.
|
|
311
311
|
* @param options - Configuration settings to use when retrieving the cookies.
|
|
312
312
|
*/
|
|
313
|
-
getCookies(url: string | URL, options?: GetCookiesOptions
|
|
313
|
+
getCookies(url: string | URL, options?: GetCookiesOptions): Promise<Cookie[]>;
|
|
314
314
|
/**
|
|
315
315
|
* @internal No doc because this is an overload that supports the implementation
|
|
316
316
|
*/
|
package/dist/cookie/cookieJar.js
CHANGED
|
@@ -527,7 +527,7 @@ class CookieJar {
|
|
|
527
527
|
// deferred from S5.3
|
|
528
528
|
// non-RFC: allow retention of expired cookies by choice
|
|
529
529
|
const expiryTime = c.expiryTime();
|
|
530
|
-
if (expireCheck && expiryTime && expiryTime <= now) {
|
|
530
|
+
if (expireCheck && expiryTime != undefined && expiryTime <= now) {
|
|
531
531
|
store.removeCookie(c.domain, c.path, c.key, () => { }); // result ignored
|
|
532
532
|
return false;
|
|
533
533
|
}
|
package/dist/pathMatch.js
CHANGED
|
@@ -32,8 +32,7 @@ function pathMatch(reqPath, cookiePath) {
|
|
|
32
32
|
// " o The cookie-path is a prefix of the request-path, and the first
|
|
33
33
|
// character of the request-path that is not included in the cookie- path
|
|
34
34
|
// is a %x2F ("/") character."
|
|
35
|
-
if (
|
|
36
|
-
reqPath[cookiePath.length] === '/') {
|
|
35
|
+
if (reqPath.startsWith(cookiePath) && reqPath[cookiePath.length] === '/') {
|
|
37
36
|
return true;
|
|
38
37
|
}
|
|
39
38
|
}
|
package/dist/utils.js
CHANGED
|
@@ -29,7 +29,8 @@ const safeToStringImpl = (val, seenArrays = new WeakSet()) => {
|
|
|
29
29
|
return Array.isArray(val)
|
|
30
30
|
? // Arrays have a weird custom toString that we need to replicate
|
|
31
31
|
safeArrayToString(val, seenArrays)
|
|
32
|
-
:
|
|
32
|
+
: // eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
33
|
+
String(val);
|
|
33
34
|
}
|
|
34
35
|
else {
|
|
35
36
|
// This case should just be objects with null prototype, so we can just use Object#toString
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/package.json
CHANGED
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"RFC6265",
|
|
76
76
|
"RFC2965"
|
|
77
77
|
],
|
|
78
|
-
"version": "5.0.0",
|
|
78
|
+
"version": "5.1.0-rc.0",
|
|
79
79
|
"homepage": "https://github.com/salesforce/tough-cookie",
|
|
80
80
|
"repository": {
|
|
81
81
|
"type": "git",
|
|
@@ -92,21 +92,25 @@
|
|
|
92
92
|
"!__tests__"
|
|
93
93
|
],
|
|
94
94
|
"scripts": {
|
|
95
|
-
"
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"
|
|
100
|
-
"version": "
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
"
|
|
105
|
-
"
|
|
106
|
-
"
|
|
107
|
-
"
|
|
108
|
-
"
|
|
109
|
-
"
|
|
95
|
+
"build": "npm run _build:clean && npm run _build:compile",
|
|
96
|
+
"lint": "npm run _lint:check",
|
|
97
|
+
"prepack": "npm run build",
|
|
98
|
+
"prepare-pr": "npm test && npm run _api:update && npm run _docs:generate && npm run _format:fix && npm run _lint:fix",
|
|
99
|
+
"test": "npm run build && npm run _test:ts && npm run _test:legacy",
|
|
100
|
+
"version": "npm run _version:generate && npm run prepare-pr && git add --renormalize .",
|
|
101
|
+
"_api:check": "api-extractor run --verbose",
|
|
102
|
+
"_api:update": "api-extractor run --verbose --local",
|
|
103
|
+
"_build:clean": "rm -rf dist",
|
|
104
|
+
"_build:compile": "tsc",
|
|
105
|
+
"_docs:generate": "api-documenter markdown --input-folder ./tmp --output-folder ./api/docs",
|
|
106
|
+
"_docs:fix": "prettier ./api/docs --write",
|
|
107
|
+
"_format:check": "prettier . --check",
|
|
108
|
+
"_format:fix": "prettier . --write",
|
|
109
|
+
"_lint:check": "eslint .",
|
|
110
|
+
"_lint:fix": "eslint . --fix",
|
|
111
|
+
"_test:legacy": "./test/scripts/vows.js test/*_test.js",
|
|
112
|
+
"_test:ts": "jest",
|
|
113
|
+
"_version:generate": "genversion --template version-template.ejs --force lib/version.ts"
|
|
110
114
|
},
|
|
111
115
|
"//": "We only support node 18+, but v16 still works. We won't block v16 until it becomes a burden.",
|
|
112
116
|
"engines": {
|