tough-cookie 4.1.3 → 5.0.0-rc.1

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.
Files changed (45) hide show
  1. package/README.md +21 -31
  2. package/dist/cookie/canonicalDomain.d.ts +1 -0
  3. package/dist/cookie/canonicalDomain.js +45 -0
  4. package/dist/cookie/constants.d.ts +18 -0
  5. package/dist/cookie/constants.js +24 -0
  6. package/dist/cookie/cookie.d.ts +97 -0
  7. package/dist/cookie/cookie.js +631 -0
  8. package/dist/cookie/cookieCompare.d.ts +3 -0
  9. package/dist/cookie/cookieCompare.js +65 -0
  10. package/dist/cookie/cookieJar.d.ts +75 -0
  11. package/dist/cookie/cookieJar.js +810 -0
  12. package/dist/cookie/defaultPath.d.ts +1 -0
  13. package/dist/cookie/defaultPath.js +30 -0
  14. package/dist/cookie/domainMatch.d.ts +1 -0
  15. package/dist/cookie/domainMatch.js +60 -0
  16. package/dist/cookie/formatDate.d.ts +2 -0
  17. package/dist/cookie/formatDate.js +34 -0
  18. package/dist/cookie/index.d.ts +19 -0
  19. package/dist/cookie/index.js +39 -0
  20. package/dist/cookie/parseDate.d.ts +1 -0
  21. package/dist/cookie/parseDate.js +230 -0
  22. package/dist/cookie/permutePath.d.ts +5 -0
  23. package/dist/cookie/permutePath.js +49 -0
  24. package/dist/getPublicSuffix.d.ts +6 -0
  25. package/dist/getPublicSuffix.js +74 -0
  26. package/dist/memstore.d.ts +31 -0
  27. package/dist/memstore.js +180 -0
  28. package/{lib/store.js → dist/pathMatch.d.ts} +1 -46
  29. package/{lib → dist}/pathMatch.js +21 -22
  30. package/dist/permuteDomain.d.ts +1 -0
  31. package/{lib → dist}/permuteDomain.js +28 -30
  32. package/dist/store.d.ts +52 -0
  33. package/dist/store.js +68 -0
  34. package/dist/utils.d.ts +23 -0
  35. package/dist/utils.js +92 -0
  36. package/dist/validators.d.ts +20 -0
  37. package/{lib → dist}/validators.js +42 -51
  38. package/dist/version.d.ts +1 -0
  39. package/dist/version.js +5 -0
  40. package/package.json +37 -20
  41. package/lib/cookie.js +0 -1756
  42. package/lib/memstore.js +0 -242
  43. package/lib/pubsuffix-psl.js +0 -73
  44. package/lib/utilHelper.js +0 -39
  45. package/lib/version.js +0 -2
package/README.md CHANGED
@@ -9,31 +9,31 @@
9
9
  ## Synopsis
10
10
 
11
11
  ```javascript
12
- var tough = require("tough-cookie");
13
- var Cookie = tough.Cookie;
14
- var cookie = Cookie.parse(header);
15
- cookie.value = "somethingdifferent";
16
- header = cookie.toString();
17
- var cookiejar = new tough.CookieJar();
12
+ var tough = require('tough-cookie')
13
+ var Cookie = tough.Cookie
14
+ var cookie = Cookie.parse(header)
15
+ cookie.value = 'somethingdifferent'
16
+ header = cookie.toString()
17
+ var cookiejar = new tough.CookieJar()
18
18
 
19
19
  // Asynchronous!
20
20
  var cookie = await cookiejar.setCookie(
21
21
  cookie,
22
- "https://currentdomain.example.com/path"
23
- );
24
- var cookies = await cookiejar.getCookies("https://example.com/otherpath");
22
+ 'https://currentdomain.example.com/path',
23
+ )
24
+ var cookies = await cookiejar.getCookies('https://example.com/otherpath')
25
25
 
26
26
  // Or with callbacks!
27
27
  cookiejar.setCookie(
28
28
  cookie,
29
- "https://currentdomain.example.com/path",
29
+ 'https://currentdomain.example.com/path',
30
30
  function (err, cookie) {
31
31
  /* ... */
32
- }
33
- );
34
- cookiejar.getCookies("http://example.com/otherpath", function (err, cookies) {
32
+ },
33
+ )
34
+ cookiejar.getCookies('http://example.com/otherpath', function (err, cookies) {
35
35
  /* ... */
36
- });
36
+ })
37
37
  ```
38
38
 
39
39
  Why the name? NPM modules `cookie`, `cookies` and `cookiejar` were already taken.
@@ -113,8 +113,8 @@ For use with `.sort()`, sorts a list of cookies into the recommended order given
113
113
  ```javascript
114
114
  var cookies = [
115
115
  /* unsorted array of Cookie objects */
116
- ];
117
- cookies = cookies.sort(cookieCompare);
116
+ ]
117
+ cookies = cookies.sort(cookieCompare)
118
118
  ```
119
119
 
120
120
  > **Note**: Since the JavaScript `Date` is limited to a 1-ms precision, cookies within the same millisecond are entirely possible. This is especially true when using the `now` option to `.setCookie()`. The `.creationIndex` property is a per-process global counter, assigned during construction with `new Cookie()`, which preserves the spirit of the RFC sorting: older cookies go first. This works great for `MemoryCookieStore` since `Set-Cookie` headers are parsed in order, but is not so great for distributed systems. Sophisticated `Store`s may wish to set this to some other _logical clock_ so that if cookies A and B are created in the same millisecond, but cookie A is created before cookie B, then `A.creationIndex < B.creationIndex`. If you want to alter the global counter, which you probably _shouldn't_ do, it's stored in `Cookie.cookiesCreated`.
@@ -144,9 +144,9 @@ If options is not an object it is ignored, which means it can be used with [`Arr
144
144
  To process the Set-Cookie header(s) on a node HTTP/HTTPS response:
145
145
 
146
146
  ```javascript
147
- if (Array.isArray(res.headers["set-cookie"]))
148
- cookies = res.headers["set-cookie"].map(Cookie.parse);
149
- else cookies = [Cookie.parse(res.headers["set-cookie"])];
147
+ if (Array.isArray(res.headers['set-cookie']))
148
+ cookies = res.headers['set-cookie'].map(Cookie.parse)
149
+ else cookies = [Cookie.parse(res.headers['set-cookie'])]
150
150
  ```
151
151
 
152
152
  _Note:_ In version 2.3.3, tough-cookie limited the number of spaces before the `=` to 256 characters. This limitation was removed in version 2.3.4.
@@ -220,7 +220,7 @@ Returns the canonicalized `.domain` field. This is lower-cased and punycode ([RF
220
220
 
221
221
  #### `.toJSON()`
222
222
 
223
- For convenience in using `JSON.serialize(cookie)`. Returns a plain-old `Object` that can be JSON-serialized.
223
+ For convenience in using `JSON.stringify(cookie)`. Returns a plain-old `Object` that can be JSON-serialized.
224
224
 
225
225
  Any `Date` properties (such as `.expires`, `.creation`, and `.lastAccessed`) are exported in ISO format (`.toISOString()`).
226
226
 
@@ -390,7 +390,7 @@ Base class for CookieJar stores. Available as `tough.Store`.
390
390
 
391
391
  ### Store API
392
392
 
393
- The storage model for each `CookieJar` instance can be replaced with a custom implementation. The default is `MemoryCookieStore` which can be found in [`lib/memstore.js`](https://github.com/salesforce/tough-cookie/blob/master/lib/memstore.js). The API uses continuation-passing-style to allow for asynchronous stores.
393
+ The storage model for each `CookieJar` instance can be replaced with a custom implementation. The default is `MemoryCookieStore` which can be found in [`lib/memstore.ts`](https://github.com/salesforce/tough-cookie/blob/master/lib/memstore.ts). The API uses continuation-passing-style to allow for asynchronous stores.
394
394
 
395
395
  Stores should inherit from the base `Store` class, which is available as a top-level export.
396
396
 
@@ -470,16 +470,6 @@ Inherits from `Store`.
470
470
 
471
471
  A just-in-memory CookieJar synchronous store implementation, used by default. Despite being a synchronous implementation, it's usable with both the synchronous and asynchronous forms of the `CookieJar` API. Supports serialization, `getAllCookies`, and `removeAllCookies`.
472
472
 
473
- ### Community Cookie Stores
474
-
475
- These are some Store implementations authored and maintained by the community. They aren't official and we don't vouch for them but you may be interested to have a look:
476
-
477
- - [`db-cookie-store`](https://github.com/JSBizon/db-cookie-store): SQL including SQLite-based databases
478
- - [`file-cookie-store`](https://github.com/JSBizon/file-cookie-store): Netscape cookie file format on disk
479
- - [`redis-cookie-store`](https://github.com/benkroeger/redis-cookie-store): Redis
480
- - [`tough-cookie-filestore`](https://github.com/mitsuru/tough-cookie-filestore): JSON on disk
481
- - [`tough-cookie-web-storage-store`](https://github.com/exponentjs/tough-cookie-web-storage-store): DOM localStorage and sessionStorage
482
-
483
473
  ## Serialization Format
484
474
 
485
475
  **NOTE**: If you want to have custom `Cookie` properties serialized, add the property name to `Cookie.serializableProperties`.
@@ -0,0 +1 @@
1
+ export declare function canonicalDomain(str: string | null): string | null;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.canonicalDomain = void 0;
27
+ const punycode = __importStar(require("punycode/punycode.js"));
28
+ const constants_1 = require("./constants");
29
+ // S5.1.2 Canonicalized Host Names
30
+ function canonicalDomain(str) {
31
+ if (str == null) {
32
+ return null;
33
+ }
34
+ let _str = str.trim().replace(/^\./, ''); // S4.1.2.3 & S5.2.3: ignore leading .
35
+ if (constants_1.IP_V6_REGEX_OBJECT.test(_str)) {
36
+ _str = _str.replace('[', '').replace(']', '');
37
+ }
38
+ // convert to IDN if any non-ASCII characters
39
+ // eslint-disable-next-line no-control-regex
40
+ if (/[^\u0001-\u007f]/.test(_str)) {
41
+ _str = punycode.toASCII(_str);
42
+ }
43
+ return _str.toLowerCase();
44
+ }
45
+ exports.canonicalDomain = canonicalDomain;
@@ -0,0 +1,18 @@
1
+ export declare const PrefixSecurityEnum: Readonly<{
2
+ SILENT: "silent";
3
+ STRICT: "strict";
4
+ DISABLED: "unsafe-disabled";
5
+ }>;
6
+ export declare const IP_V6_REGEX_OBJECT: RegExp;
7
+ export interface SerializedCookieJar {
8
+ version: string;
9
+ storeType: string | null;
10
+ rejectPublicSuffixes: boolean;
11
+ [key: string]: unknown;
12
+ cookies: SerializedCookie[];
13
+ }
14
+ export interface SerializedCookie {
15
+ key?: string;
16
+ value?: string;
17
+ [key: string]: unknown;
18
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IP_V6_REGEX_OBJECT = exports.PrefixSecurityEnum = void 0;
4
+ exports.PrefixSecurityEnum = Object.freeze({
5
+ SILENT: 'silent',
6
+ STRICT: 'strict',
7
+ DISABLED: 'unsafe-disabled',
8
+ });
9
+ const IP_V6_REGEX = `
10
+ \\[?(?:
11
+ (?:[a-fA-F\\d]{1,4}:){7}(?:[a-fA-F\\d]{1,4}|:)|
12
+ (?:[a-fA-F\\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|:[a-fA-F\\d]{1,4}|:)|
13
+ (?:[a-fA-F\\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,2}|:)|
14
+ (?:[a-fA-F\\d]{1,4}:){4}(?:(?::[a-fA-F\\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,3}|:)|
15
+ (?:[a-fA-F\\d]{1,4}:){3}(?:(?::[a-fA-F\\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,4}|:)|
16
+ (?:[a-fA-F\\d]{1,4}:){2}(?:(?::[a-fA-F\\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,5}|:)|
17
+ (?:[a-fA-F\\d]{1,4}:){1}(?:(?::[a-fA-F\\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,6}|:)|
18
+ (?::(?:(?::[a-fA-F\\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-fA-F\\d]{1,4}){1,7}|:))
19
+ )(?:%[0-9a-zA-Z]{1,})?\\]?
20
+ `
21
+ .replace(/\s*\/\/.*$/gm, '')
22
+ .replace(/\n/g, '')
23
+ .trim();
24
+ exports.IP_V6_REGEX_OBJECT = new RegExp(`^${IP_V6_REGEX}$`);
@@ -0,0 +1,97 @@
1
+ /*!
2
+ * Copyright (c) 2015-2020, Salesforce.com, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * 1. Redistributions of source code must retain the above copyright notice,
9
+ * this list of conditions and the following disclaimer.
10
+ *
11
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ *
15
+ * 3. Neither the name of Salesforce.com nor the names of its contributors may
16
+ * be used to endorse or promote products derived from this software without
17
+ * specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ * POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+ import type { SerializedCookie } from './constants';
32
+ type ParseCookieOptions = {
33
+ loose?: boolean | undefined;
34
+ };
35
+ declare function parse(str: string, options?: ParseCookieOptions): Cookie | undefined | null;
36
+ declare function fromJSON(str: unknown): Cookie | null;
37
+ type CreateCookieOptions = {
38
+ key?: string;
39
+ value?: string;
40
+ expires?: Date | 'Infinity' | null;
41
+ maxAge?: number | 'Infinity' | '-Infinity';
42
+ domain?: string | null;
43
+ path?: string | null;
44
+ secure?: boolean;
45
+ httpOnly?: boolean;
46
+ extensions?: string[] | null;
47
+ creation?: Date | 'Infinity' | null;
48
+ creationIndex?: number;
49
+ hostOnly?: boolean | null;
50
+ pathIsDefault?: boolean | null;
51
+ lastAccessed?: Date | 'Infinity' | null;
52
+ sameSite?: string | undefined;
53
+ };
54
+ export declare class Cookie {
55
+ key: string | undefined;
56
+ value: string | undefined;
57
+ expires: Date | 'Infinity' | null | undefined;
58
+ maxAge: number | 'Infinity' | '-Infinity' | undefined;
59
+ domain: string | null | undefined;
60
+ path: string | null | undefined;
61
+ secure: boolean | undefined;
62
+ httpOnly: boolean | undefined;
63
+ extensions: string[] | null | undefined;
64
+ creation: Date | 'Infinity' | null;
65
+ creationIndex: number | undefined;
66
+ hostOnly: boolean | null | undefined;
67
+ pathIsDefault: boolean | null | undefined;
68
+ lastAccessed: Date | 'Infinity' | null | undefined;
69
+ sameSite: string | undefined;
70
+ constructor(options?: CreateCookieOptions);
71
+ toJSON(): SerializedCookie;
72
+ clone(): Cookie | null;
73
+ validate(): boolean;
74
+ setExpires(exp: string | Date): void;
75
+ setMaxAge(age: number): void;
76
+ cookieString(): string;
77
+ toString(): string;
78
+ TTL(now?: number): number;
79
+ expiryTime(now?: Date): number | undefined;
80
+ isPersistent(): boolean;
81
+ canonicalizedDomain(): string | null;
82
+ cdomain(): string | null;
83
+ static parse: typeof parse;
84
+ static fromJSON: typeof fromJSON;
85
+ static cookiesCreated: number;
86
+ static sameSiteLevel: {
87
+ readonly strict: 3;
88
+ readonly lax: 2;
89
+ readonly none: 1;
90
+ };
91
+ static sameSiteCanonical: {
92
+ readonly strict: "Strict";
93
+ readonly lax: "Lax";
94
+ };
95
+ static serializableProperties: readonly ["key", "value", "expires", "maxAge", "domain", "path", "secure", "httpOnly", "extensions", "hostOnly", "pathIsDefault", "creation", "lastAccessed", "sameSite"];
96
+ }
97
+ export {};