add eslint + prettier

reformat code with prettier
This commit is contained in:
jkhsjdhjs 2020-06-17 15:04:32 +02:00
parent 120ce5c6c6
commit c4b04ed3dc
Signed by: jkhsjdhjs
GPG Key ID: BAC6ADBAB7D576CC
13 changed files with 1889 additions and 445 deletions

15
.eslintrc.json Normal file
View File

@ -0,0 +1,15 @@
{
"env": {
"es2020": true,
"node": true
},
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"plugins": ["prettier"],
"parserOptions": {
"ecmaVersion": 11,
"sourceType": "module"
},
"rules": {
"prettier/prettier": "error"
}
}

6
.prettierrc.json Normal file
View File

@ -0,0 +1,6 @@
{
"tabWidth": 4,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid"
}

View File

@ -1,17 +1,17 @@
language: node_js language: node_js
node_js: node_js:
- node - node
- lts/* - lts/*
- 11.14.0 - 11.14.0
script: script:
- node --experimental-modules test/index.mjs - npm test
deploy: deploy:
provider: npm provider: npm
email: npm@totally.rip email: npm@totally.rip
api_key: api_key:
secure: 49x2b6FaWwTCfRXXl2CaQyADZ5cvWkxS/QHRjWOJaOyPVO0M/6H23EWGxu3xttUfKHv+RADrrWKiOnJhnOVD08wO/kpC3jizKQa7D3z08hLmt8lNf6I+gt3VjUnaM1JvEAtGruJcx0aUvbqEVEJ5EehPgE+4/IGCcYC0gF+xEU1ButyQprQspw333jAEBj+0EbwV3K8Tr8CdlpZHN2wh8uzSHGg1m6jMbVZcamI3uo1INoRzhGt5Fd4t5xPOfUYyFPMopCnXX8cwed5O3lgx8nFzhS7kSl8lJrCkhzrqmx/84s1mssEj6hvz6bH5zRwj9Om3wr/1N/Q76sP4Tf6kwbpMXZR/+1dUCgbdSV7XGqaUSDzsuOPUW9+cC/RBF0O0Cr/EfmvJx4Uy5pW/OcCAYBmmMxaupEkW583oGm3lTe66UW9x866j5HyG/z2ElsEDBJscpB2niB4pa71g+ypiJcWydruozOiejS+/Edy/cfM/JPmJv1BTpr8IntWKKsriVcrLXo1p+RMRAS1ZJzg+bU0wuBWTK4EbLE8663VBg4oTd9E1pwvpt5pnFKopeYtVLDX0Vtvd4+XQaUMf80Dx6mLrS1hWyLrt+fe1LX+1gNhwZFLDM0FZnnAaszzxVBAJlpD3F4hz7Jb6Q2ZRvNWP8DCNcJPtlcimSb8usk1eTDE= secure: 49x2b6FaWwTCfRXXl2CaQyADZ5cvWkxS/QHRjWOJaOyPVO0M/6H23EWGxu3xttUfKHv+RADrrWKiOnJhnOVD08wO/kpC3jizKQa7D3z08hLmt8lNf6I+gt3VjUnaM1JvEAtGruJcx0aUvbqEVEJ5EehPgE+4/IGCcYC0gF+xEU1ButyQprQspw333jAEBj+0EbwV3K8Tr8CdlpZHN2wh8uzSHGg1m6jMbVZcamI3uo1INoRzhGt5Fd4t5xPOfUYyFPMopCnXX8cwed5O3lgx8nFzhS7kSl8lJrCkhzrqmx/84s1mssEj6hvz6bH5zRwj9Om3wr/1N/Q76sP4Tf6kwbpMXZR/+1dUCgbdSV7XGqaUSDzsuOPUW9+cC/RBF0O0Cr/EfmvJx4Uy5pW/OcCAYBmmMxaupEkW583oGm3lTe66UW9x866j5HyG/z2ElsEDBJscpB2niB4pa71g+ypiJcWydruozOiejS+/Edy/cfM/JPmJv1BTpr8IntWKKsriVcrLXo1p+RMRAS1ZJzg+bU0wuBWTK4EbLE8663VBg4oTd9E1pwvpt5pnFKopeYtVLDX0Vtvd4+XQaUMf80Dx6mLrS1hWyLrt+fe1LX+1gNhwZFLDM0FZnnAaszzxVBAJlpD3F4hz7Jb6Q2ZRvNWP8DCNcJPtlcimSb8usk1eTDE=
on: on:
branch: master branch: master
tags: true tags: true
repo: jkhsjdhjs/node-fetch-cookies repo: jkhsjdhjs/node-fetch-cookies

130
README.md
View File

@ -5,9 +5,10 @@ It supports reading/writing from/to a JSON cookie jar and keeps cookies in memor
### For upgrading from 1.2.x or below to 1.3.x or above, please read the [breaking API changes](#130-breaking-api-changes). ### For upgrading from 1.2.x or below to 1.3.x or above, please read the [breaking API changes](#130-breaking-api-changes).
## Usage Examples ## Usage Examples
### with file... ### with file...
```javascript ```javascript
import {fetch, CookieJar} from "node-fetch-cookies"; import {fetch, CookieJar} from "node-fetch-cookies";
@ -27,6 +28,7 @@ import {fetch, CookieJar} from "node-fetch-cookies";
``` ```
### ...or without ### ...or without
```javascript ```javascript
import {fetch, CookieJar} from "node-fetch-cookies"; import {fetch, CookieJar} from "node-fetch-cookies";
@ -40,125 +42,155 @@ import {fetch, CookieJar} from "node-fetch-cookies";
}); });
// do some requests you require login for // do some requests you require login for
response = await fetch(cookieJar, "https://example.com/api/admin/drop-all-databases"); response = await fetch(
cookieJar,
"https://example.com/api/admin/drop-all-databases"
);
// and optionally log out again // and optionally log out again
response = await fetch(cookieJar, "https://example.com/api/logout"); response = await fetch(cookieJar, "https://example.com/api/logout");
})(); })();
``` ```
## Documentation ## Documentation
### async fetch(cookieJars, url[, options]) ### async fetch(cookieJars, url[, options])
- `cookieJars` A [CookieJar](#class-cookiejar) instance, an array of CookieJar instances or null, if you don't want to send or store cookies.
- `url` and `options` as in https://github.com/bitinn/node-fetch#fetchurl-options - `cookieJars` A [CookieJar](#class-cookiejar) instance, an array of CookieJar instances or null, if you don't want to send or store cookies.
- `url` and `options` as in https://github.com/bitinn/node-fetch#fetchurl-options
Returns a Promise resolving to a [Response](https://github.com/bitinn/node-fetch#class-response) instance on success. Returns a Promise resolving to a [Response](https://github.com/bitinn/node-fetch#class-response) instance on success.
### Class: CookieJar ### Class: CookieJar
A class that stores cookies. A class that stores cookies.
#### Properties #### Properties
- `flags` The read/write flags as specified below.
- `file` The path of the cookie jar on the disk. - `flags` The read/write flags as specified below.
- `cookies` A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) mapping hostnames to maps, which map cookie names to the respective [Cookie](#class-cookie) instance. - `file` The path of the cookie jar on the disk.
- `cookies` A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) mapping hostnames to maps, which map cookie names to the respective [Cookie](#class-cookie) instance.
#### new CookieJar([file, flags = `rw`, cookies, cookieIgnoreCallback]) #### new CookieJar([file, flags = `rw`, cookies, cookieIgnoreCallback])
- `file` An optional string containing a relative or absolute path to the file on the disk to use.
- `flags` An optional string specifying whether cookies should be read and/or written from/to the jar when passing it as parameter to [fetch](#fetchcookiejar-url-options). Default: `rw` - `file` An optional string containing a relative or absolute path to the file on the disk to use.
- `r`: only read from this jar - `flags` An optional string specifying whether cookies should be read and/or written from/to the jar when passing it as parameter to [fetch](#fetchcookiejar-url-options). Default: `rw`
- `w`: only write to this jar - `r`: only read from this jar
- `rw` or `wr`: read/write from/to this jar - `w`: only write to this jar
- `cookies` An optional initializer for the cookie jar - either an array of [Cookie](#class-cookie) instances or a single Cookie instance. - `rw` or `wr`: read/write from/to this jar
- `cookieIgnoreCallback(cookie, reason)` An optional callback function which will be called when a cookie is ignored instead of added to the cookie jar. - `cookies` An optional initializer for the cookie jar - either an array of [Cookie](#class-cookie) instances or a single Cookie instance.
- `cookie` The cookie string - `cookieIgnoreCallback(cookie, reason)` An optional callback function which will be called when a cookie is ignored instead of added to the cookie jar.
- `reason` A string containing the reason why the cookie has been ignored - `cookie` The cookie string
- `reason` A string containing the reason why the cookie has been ignored
#### addCookie(cookie[, fromURL]) #### addCookie(cookie[, fromURL])
Adds a cookie to the jar. Adds a cookie to the jar.
- `cookie` A [Cookie](#class-cookie) instance to add to the cookie jar.
Alternatively this can also be a string, for example a serialized cookie received from a website. - `cookie` A [Cookie](#class-cookie) instance to add to the cookie jar.
In this case `fromURL` must be specified. Alternatively this can also be a string, for example a serialized cookie received from a website.
- `fromURL` The url a cookie has been received from. In this case `fromURL` must be specified.
- `fromURL` The url a cookie has been received from.
Returns `true` if the cookie has been added successfully. Returns `false` otherwise. Returns `true` if the cookie has been added successfully. Returns `false` otherwise.
If the parser throws a [CookieParseError](#class-cookieparseerror) it will be caught and a warning will be printed to console. If the parser throws a [CookieParseError](#class-cookieparseerror) it will be caught and a warning will be printed to console.
#### domains() #### domains()
Returns an iterator over all domains currently stored cookies for. Returns an iterator over all domains currently stored cookies for.
#### *cookiesDomain(domain) #### \*cookiesDomain(domain)
Returns an iterator over all cookies currently stored for `domain`. Returns an iterator over all cookies currently stored for `domain`.
#### *cookiesValid(withSession) #### \*cookiesValid(withSession)
Returns an iterator over all valid (non-expired) cookies.
- `withSession`: A boolean. Iterator will include session cookies if set to `true`. Returns an iterator over all valid (non-expired) cookies.
- `withSession`: A boolean. Iterator will include session cookies if set to `true`.
#### \*cookiesAll()
#### *cookiesAll()
Returns an iterator over all cookies currently stored. Returns an iterator over all cookies currently stored.
#### *cookiesValidForRequest(requestURL) #### \*cookiesValidForRequest(requestURL)
Returns an iterator over all cookies valid for a request to `url`. Returns an iterator over all cookies valid for a request to `url`.
#### deleteExpired(sessionEnded) #### deleteExpired(sessionEnded)
Removes all expired cookies from the jar. Removes all expired cookies from the jar.
- `sessionEnded`: A boolean. Also removes session cookies if set to `true`.
- `sessionEnded`: A boolean. Also removes session cookies if set to `true`.
#### async load([file = this.file]) #### async load([file = this.file])
Reads cookies from `file` on the disk and adds the contained cookies. Reads cookies from `file` on the disk and adds the contained cookies.
- `file`: Path to the file where the cookies should be saved. Default: `this.file`, the file that has been passed to the constructor.
- `file`: Path to the file where the cookies should be saved. Default: `this.file`, the file that has been passed to the constructor.
#### async save([file = this.file]) #### async save([file = this.file])
Saves the cookie jar to `file` on the disk. Only non-expired non-session cookies are saved. Saves the cookie jar to `file` on the disk. Only non-expired non-session cookies are saved.
- `file`: Path to the file where the cookies should be saved. Default: `this.file`, the file that has been passed to the constructor.
- `file`: Path to the file where the cookies should be saved. Default: `this.file`, the file that has been passed to the constructor.
### Class: Cookie ### Class: Cookie
An abstract representation of a cookie. An abstract representation of a cookie.
#### Properties #### Properties
- `name` The identifier of the cookie.
- `value` The value of the cookie. - `name` The identifier of the cookie.
- `expiry` A [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object of the cookies expiry date or `null`, if the cookie expires with the session. - `value` The value of the cookie.
- `domain` The domain the cookie is valid for. - `expiry` A [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object of the cookies expiry date or `null`, if the cookie expires with the session.
- `path` The path the cookie is valid for. - `domain` The domain the cookie is valid for.
- `secure` A boolean value representing the cookie's secure attribute. If set the cookie will only be used for `https` requests. - `path` The path the cookie is valid for.
- `subdomains` A boolean value specifying whether the cookie should be used for requests to subdomains of `domain` or not. - `secure` A boolean value representing the cookie's secure attribute. If set the cookie will only be used for `https` requests.
- `subdomains` A boolean value specifying whether the cookie should be used for requests to subdomains of `domain` or not.
#### new Cookie(str, requestURL) #### new Cookie(str, requestURL)
Creates a cookie instance from the string representation of a cookie as send by a webserver. Creates a cookie instance from the string representation of a cookie as send by a webserver.
- `str` The string representation of a cookie.
- `url` The url the cookie has been received from. - `str` The string representation of a cookie.
- `url` The url the cookie has been received from.
Will throw a `CookieParseError` if `str` couldn't be parsed. Will throw a `CookieParseError` if `str` couldn't be parsed.
#### static fromObject(obj) #### static fromObject(obj)
Creates a cookie instance from an already existing object with the same properties. Creates a cookie instance from an already existing object with the same properties.
#### serialize() #### serialize()
Serializes the cookie, transforming it to `name=value` so it can be used in requests. Serializes the cookie, transforming it to `name=value` so it can be used in requests.
#### hasExpired(sessionEnded) #### hasExpired(sessionEnded)
Returns whether the cookie has expired or not. Returns whether the cookie has expired or not.
- `sessionEnded`: A boolean that specifies whether the current session has ended, meaning if set to `true`, the function will return `true` for session cookies.
- `sessionEnded`: A boolean that specifies whether the current session has ended, meaning if set to `true`, the function will return `true` for session cookies.
#### isValidForRequest(requestURL) #### isValidForRequest(requestURL)
Returns whether the cookie is valid for a request to `url`. Returns whether the cookie is valid for a request to `url`.
### Class: CookieParseError ### Class: CookieParseError
The Error that is thrown when the cookie parser located in the constructor of the [Cookie](#class-cookie) class is unable to parse the input. The Error that is thrown when the cookie parser located in the constructor of the [Cookie](#class-cookie) class is unable to parse the input.
## 1.3.0 Breaking API Changes ## 1.3.0 Breaking API Changes
- `new CookieJar(flags, file, cookies)` has been changed to `new CookieJar(file, flags = "rw", cookies)`.
`new CookieJar("rw")` can now be written as `new CookieJar()`, `new CookieJar("rw", "jar.json")` can now be written as `new CookieJar("jar.json")`.
This change has been introduced to simplify the usage of this library, since `rw` is used for `flags` in most cases anyways.
- `CookieJar.addFromFile(file)` has been renamed to the async function `async CookieJar.load([file = this.file])`, which uses the fsPromises API for non-blocking cookie loading.
The default value for `file` is the file passed to the constructor.
- `CookieJar.save(file)` was moved to `async CookieJar.save([file = this.file])` now also uses the fsPromises API.
- `new CookieJar()` now doesn't load cookies from the specified file anymore. To do so, call `await CookieJar.load()` after creating the CookieJar.
**NOTE: `CookieJar.load()` will throw an error if the cookie jar doesn't exist or doesn't contain valid JSON!**
- `new CookieJar(flags, file, cookies)` has been changed to `new CookieJar(file, flags = "rw", cookies)`.
`new CookieJar("rw")` can now be written as `new CookieJar()`, `new CookieJar("rw", "jar.json")` can now be written as `new CookieJar("jar.json")`.
This change has been introduced to simplify the usage of this library, since `rw` is used for `flags` in most cases anyways.
- `CookieJar.addFromFile(file)` has been renamed to the async function `async CookieJar.load([file = this.file])`, which uses the fsPromises API for non-blocking cookie loading.
The default value for `file` is the file passed to the constructor.
- `CookieJar.save(file)` was moved to `async CookieJar.save([file = this.file])` now also uses the fsPromises API.
- `new CookieJar()` now doesn't load cookies from the specified file anymore. To do so, call `await CookieJar.load()` after creating the CookieJar.
**NOTE: `CookieJar.load()` will throw an error if the cookie jar doesn't exist or doesn't contain valid JSON!**
## License ## License
This project is licensed under the MIT license, see [LICENSE](LICENSE). This project is licensed under the MIT license, see [LICENSE](LICENSE).

1176
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,34 +1,40 @@
{ {
"name": "node-fetch-cookies", "name": "node-fetch-cookies",
"version": "1.3.6", "version": "1.3.6",
"description": "node-fetch wrapper that adds support for cookie-jars", "description": "node-fetch wrapper that adds support for cookie-jars",
"main": "src/index.mjs", "main": "src/index.mjs",
"engines": { "engines": {
"node": ">=11.14.0" "node": ">=11.14.0"
}, },
"files": [ "files": [
"src/" "src/"
], ],
"scripts": { "scripts": {
"test": "node test/index.mjs" "test": "npx eslint --ext mjs . && npx prettier --check package.json package-lock.json .travis.yml .prettierrc.json .eslintrc.json README.md && node --experimental-modules test/index.mjs"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/jkhsjdhjs/node-fetch-cookies.git" "url": "git+https://github.com/jkhsjdhjs/node-fetch-cookies.git"
}, },
"keywords": [ "keywords": [
"cookie", "cookie",
"cookie-jar", "cookie-jar",
"node-fetch", "node-fetch",
"fetch" "fetch"
], ],
"author": "jkhsjdhjs", "author": "jkhsjdhjs",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://github.com/jkhsjdhjs/node-fetch-cookies/issues" "url": "https://github.com/jkhsjdhjs/node-fetch-cookies/issues"
}, },
"homepage": "https://github.com/jkhsjdhjs/node-fetch-cookies#readme", "homepage": "https://github.com/jkhsjdhjs/node-fetch-cookies#readme",
"dependencies": { "dependencies": {
"node-fetch": "^2.6.0" "node-fetch": "^2.6.0"
} },
"devDependencies": {
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"prettier": "2.0.5"
}
} }

View File

@ -6,43 +6,55 @@ import {paramError, CookieParseError} from "./errors.mjs";
export default class CookieJar { export default class CookieJar {
constructor(file, flags = "rw", cookies, cookieIgnoreCallback) { constructor(file, flags = "rw", cookies, cookieIgnoreCallback) {
this.cookies = new Map(); this.cookies = new Map();
if(file && typeof file !== "string") if (file && typeof file !== "string")
throw paramError("Second", "file", "new CookieJar()", "string"); throw paramError("Second", "file", "new CookieJar()", "string");
if(typeof flags !== "string") if (typeof flags !== "string")
throw paramError("First", "flags", "new CookieJar()", "string"); throw paramError("First", "flags", "new CookieJar()", "string");
if(Array.isArray(cookies)) { if (Array.isArray(cookies)) {
if(!cookies.every(c => c instanceof Cookie)) if (!cookies.every(c => c instanceof Cookie))
throw paramError("Third", "cookies", "new CookieJar()", "[Cookie]"); throw paramError(
"Third",
"cookies",
"new CookieJar()",
"[Cookie]"
);
cookies.forEach(cookie => this.addCookie(cookie)); cookies.forEach(cookie => this.addCookie(cookie));
} } else if (cookies instanceof Cookie) this.addCookie(cookies);
else if(cookies instanceof Cookie) else if (cookies)
this.addCookie(cookies); throw paramError("Third", "cookies", "new CookieJar()", [
else if(cookies) "[Cookie]",
throw paramError("Third", "cookies", "new CookieJar()", ["[Cookie]", "Cookie"]); "Cookie"
if(cookieIgnoreCallback && typeof cookieIgnoreCallback !== "function") ]);
throw paramError("Fourth", "cookieIgnoreCallback", "new CookieJar()", "function"); if (cookieIgnoreCallback && typeof cookieIgnoreCallback !== "function")
throw paramError(
"Fourth",
"cookieIgnoreCallback",
"new CookieJar()",
"function"
);
this.file = file; this.file = file;
this.flags = flags; this.flags = flags;
this.cookieIgnoreCallback = cookieIgnoreCallback; this.cookieIgnoreCallback = cookieIgnoreCallback;
} }
addCookie(cookie, fromURL) { addCookie(cookie, fromURL) {
if(typeof cookie === "string") { if (typeof cookie === "string") {
try { try {
cookie = new Cookie(cookie, fromURL); cookie = new Cookie(cookie, fromURL);
} } catch (error) {
catch(error) { if (error instanceof CookieParseError) {
if(error instanceof CookieParseError) { if (this.cookieIgnoreCallback)
if(this.cookieIgnoreCallback)
this.cookieIgnoreCallback(cookie, error.message); this.cookieIgnoreCallback(cookie, error.message);
return false; return false;
} }
throw error; throw error;
} }
} } else if (!(cookie instanceof Cookie))
else if(!(cookie instanceof Cookie)) throw paramError("First", "cookie", "CookieJar.addCookie()", [
throw paramError("First", "cookie", "CookieJar.addCookie()", ["string", "Cookie"]); "string",
if(!this.cookies.get(cookie.domain)) "Cookie"
]);
if (!this.cookies.get(cookie.domain))
this.cookies.set(cookie.domain, new Map()); this.cookies.set(cookie.domain, new Map());
this.cookies.get(cookie.domain).set(cookie.name, cookie); this.cookies.get(cookie.domain).set(cookie.name, cookie);
return true; return true;
@ -51,30 +63,29 @@ export default class CookieJar {
return this.cookies.keys(); return this.cookies.keys();
} }
*cookiesDomain(domain) { *cookiesDomain(domain) {
for(const cookie of (this.cookies.get(domain) || []).values()) for (const cookie of (this.cookies.get(domain) || []).values())
yield cookie; yield cookie;
} }
*cookiesValid(withSession) { *cookiesValid(withSession) {
for(const cookie of this.cookiesAll()) for (const cookie of this.cookiesAll())
if(!cookie.hasExpired(!withSession)) if (!cookie.hasExpired(!withSession)) yield cookie;
yield cookie;
} }
*cookiesAll() { *cookiesAll() {
for(const domain of this.domains()) for (const domain of this.domains()) yield* this.cookiesDomain(domain);
yield* this.cookiesDomain(domain);
} }
*cookiesValidForRequest(requestURL) { *cookiesValidForRequest(requestURL) {
const namesYielded = new Set(), const namesYielded = new Set(),
domains = url domains = url
.parse(requestURL) .parse(requestURL)
.hostname .hostname.split(".")
.split(".")
.map((_, i, a) => a.slice(i).join(".")) .map((_, i, a) => a.slice(i).join("."))
.slice(0, -1); .slice(0, -1);
for(const domain of domains) { for (const domain of domains) {
for(const cookie of this.cookiesDomain(domain)) { for (const cookie of this.cookiesDomain(domain)) {
if(cookie.isValidForRequest(requestURL) if (
&& !namesYielded.has(cookie.name)) { cookie.isValidForRequest(requestURL) &&
!namesYielded.has(cookie.name)
) {
namesYielded.add(cookie.name); namesYielded.add(cookie.name);
yield cookie; yield cookie;
} }
@ -87,14 +98,19 @@ export default class CookieJar {
validCookies.forEach(c => this.addCookie(c)); validCookies.forEach(c => this.addCookie(c));
} }
async load(file = this.file) { async load(file = this.file) {
if(typeof file !== "string") if (typeof file !== "string")
throw new Error("No file has been specified for this cookie jar!"); throw new Error("No file has been specified for this cookie jar!");
JSON.parse(await fs.readFile(file)).forEach(c => this.addCookie(Cookie.fromObject(c))); JSON.parse(await fs.readFile(file)).forEach(c =>
this.addCookie(Cookie.fromObject(c))
);
} }
async save(file = this.file) { async save(file = this.file) {
if(typeof file !== "string") if (typeof file !== "string")
throw new Error("No file has been specified for this cookie jar!"); throw new Error("No file has been specified for this cookie jar!");
// only save cookies that haven't expired // only save cookies that haven't expired
await fs.writeFile(this.file, JSON.stringify([...this.cookiesValid(false)])); await fs.writeFile(
this.file,
JSON.stringify([...this.cookiesValid(false)])
);
} }
}; }

View File

@ -4,7 +4,10 @@ import {paramError, CookieParseError} from "./errors.mjs";
const validateHostname = (cookieHostname, requestHostname, subdomains) => { const validateHostname = (cookieHostname, requestHostname, subdomains) => {
cookieHostname = cookieHostname.toLowerCase(); cookieHostname = cookieHostname.toLowerCase();
requestHostname = requestHostname.toLowerCase(); requestHostname = requestHostname.toLowerCase();
if(requestHostname === cookieHostname || (subdomains && requestHostname.endsWith("." + cookieHostname))) if (
requestHostname === cookieHostname ||
(subdomains && requestHostname.endsWith("." + cookieHostname))
)
return true; return true;
return false; return false;
}; };
@ -12,16 +15,14 @@ const validateHostname = (cookieHostname, requestHostname, subdomains) => {
const validatePath = (cookiePath, requestPath) => { const validatePath = (cookiePath, requestPath) => {
cookiePath = decodeURIComponent(cookiePath).toLowerCase(); cookiePath = decodeURIComponent(cookiePath).toLowerCase();
requestPath = decodeURIComponent(requestPath).toLowerCase(); requestPath = decodeURIComponent(requestPath).toLowerCase();
if(cookiePath.endsWith("/")) if (cookiePath.endsWith("/")) cookiePath = cookiePath.slice(0, -1);
cookiePath = cookiePath.slice(0, -1); if (requestPath.endsWith("/")) requestPath = requestPath.slice(0, -1);
if(requestPath.endsWith("/"))
requestPath = requestPath.slice(0, -1);
return (requestPath + "/").startsWith(cookiePath + "/"); return (requestPath + "/").startsWith(cookiePath + "/");
}; };
const splitN = (str, sep, n) => { const splitN = (str, sep, n) => {
const splitted = str.split(sep); const splitted = str.split(sep);
if(n < splitted.length - 1) { if (n < splitted.length - 1) {
splitted[n] = splitted.slice(n).join(sep); splitted[n] = splitted.slice(n).join(sep);
splitted.splice(n + 1); splitted.splice(n + 1);
} }
@ -30,9 +31,9 @@ const splitN = (str, sep, n) => {
export default class Cookie { export default class Cookie {
constructor(str, requestURL) { constructor(str, requestURL) {
if(typeof str !== "string") if (typeof str !== "string")
throw paramError("First", "str", "new Cookie()", "string"); throw paramError("First", "str", "new Cookie()", "string");
if(typeof requestURL !== "string") if (typeof requestURL !== "string")
throw paramError("Second", "requestURL", "new Cookie()", "string"); throw paramError("Second", "requestURL", "new Cookie()", "string");
// check if url is valid // check if url is valid
@ -40,95 +41,134 @@ export default class Cookie {
const splitted = str.split("; "); const splitted = str.split("; ");
[this.name, this.value] = splitN(splitted[0], "=", 1); [this.name, this.value] = splitN(splitted[0], "=", 1);
if(!this.name) if (!this.name)
throw new CookieParseError("Invalid cookie name \"" + this.name + "\"!"); throw new CookieParseError(
if(this.value.startsWith("\"") && this.value.endsWith("\"")) 'Invalid cookie name "' + this.name + '"!'
);
if (this.value.startsWith('"') && this.value.endsWith('"'))
this.value = this.value.slice(1, -1); this.value = this.value.slice(1, -1);
const parsedURL = url.parse(requestURL); const parsedURL = url.parse(requestURL);
for(let i = 1; i < splitted.length; i++) { for (let i = 1; i < splitted.length; i++) {
let [k, v] = splitN(splitted[i], "=", 1); let [k, v] = splitN(splitted[i], "=", 1);
k = k.toLowerCase(); k = k.toLowerCase();
if(v) { if (v) {
if(k === "expires") { if (k === "expires") {
if(this.expiry) // max-age has precedence over expires if (this.expiry)
// max-age has precedence over expires
continue; continue;
if(!/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2}[ -](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ -]\d{2,4} \d{2}:\d{2}:\d{2} GMT$/.test(v) if (
|| (this.expiry = new Date(v)).toString() === "Invalid Date" !/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d{2}[ -](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ -]\d{2,4} \d{2}:\d{2}:\d{2} GMT$/.test(
|| this.expiry.getTime() < 0) v
throw new CookieParseError("Invalid value for Expires \"" + v + "\"!"); ) ||
} (this.expiry = new Date(v)).toString() ===
else if(k === "max-age") { "Invalid Date" ||
this.expiry.getTime() < 0
)
throw new CookieParseError(
'Invalid value for Expires "' + v + '"!'
);
} else if (k === "max-age") {
const seconds = ~~+v; const seconds = ~~+v;
if(seconds.toString() !== v) if (seconds.toString() !== v)
throw new CookieParseError("Invalid value for Max-Age \"" + v + "\"!"); throw new CookieParseError(
'Invalid value for Max-Age "' + v + '"!'
);
this.expiry = new Date(); this.expiry = new Date();
this.expiry.setSeconds(this.expiry.getSeconds() + seconds); this.expiry.setSeconds(this.expiry.getSeconds() + seconds);
} } else if (k === "domain") {
else if(k === "domain") { if (v.startsWith(".")) v = v.substring(1);
if(v.startsWith(".")) if (!validateHostname(parsedURL.hostname, v, true))
v = v.substring(1); throw new CookieParseError(
if(!validateHostname(parsedURL.hostname, v, true)) 'Invalid value for Domain "' +
throw new CookieParseError("Invalid value for Domain \"" + v + "\": cookie was received from \"" + parsedURL.hostname + "\"!"); v +
'": cookie was received from "' +
parsedURL.hostname +
'"!'
);
this.domain = v; this.domain = v;
this.subdomains = true; this.subdomains = true;
} } else if (k === "path") this.path = v;
else if(k === "path") else if (k === "samesite")
this.path = v; // only relevant for cross site requests, so not for us
else if(k === "samesite") // only relevant for cross site requests, so not for us
continue; continue;
else else
throw new CookieParseError("Invalid key \"" + k + "\" with value \"" + v + "\" specified!"); throw new CookieParseError(
} 'Invalid key "' +
else { k +
if(k === "secure") '" with value "' +
this.secure = true; v +
else if(k === "httponly") // only relevant for browsers '" specified!'
);
} else {
if (k === "secure") this.secure = true;
else if (k === "httponly")
// only relevant for browsers
continue; continue;
else else
throw new CookieParseError("Invalid key \"" + k + "\" specified!"); throw new CookieParseError(
'Invalid key "' + k + '" specified!'
);
} }
} }
if(this.name.toLowerCase().startsWith("__secure-") && (!this.secure || parsedURL.protocol !== "https:")) if (
throw new CookieParseError("Cookie has \"__Secure-\" prefix but \"Secure\" isn't set or the cookie is not set via https!"); this.name.toLowerCase().startsWith("__secure-") &&
if(this.name.toLowerCase().startsWith("__host-") && (!this.secure || parsedURL.protocol !== "https:" || this.domain || this.path !== "/")) (!this.secure || parsedURL.protocol !== "https:")
throw new CookieParseError("Cookie has \"__Host-\" prefix but \"Secure\" isn't set, the cookie is not set via https, \"Domain\" is set or \"Path\" is not equal to \"/\"!"); )
throw new CookieParseError(
'Cookie has "__Secure-" prefix but "Secure" isn\'t set or the cookie is not set via https!'
);
if (
this.name.toLowerCase().startsWith("__host-") &&
(!this.secure ||
parsedURL.protocol !== "https:" ||
this.domain ||
this.path !== "/")
)
throw new CookieParseError(
'Cookie has "__Host-" prefix but "Secure" isn\'t set, the cookie is not set via https, "Domain" is set or "Path" is not equal to "/"!'
);
// assign defaults // assign defaults
if(!this.domain) { if (!this.domain) {
this.domain = parsedURL.hostname; this.domain = parsedURL.hostname;
this.subdomains = false; this.subdomains = false;
} }
if(!this.path) if (!this.path) this.path = "/";
this.path = "/"; if (!this.secure) this.secure = false;
if(!this.secure) if (!this.expiry) this.expiry = null;
this.secure = false;
if(!this.expiry)
this.expiry = null;
} }
static fromObject(obj) { static fromObject(obj) {
let c = Object.assign(Object.create(this.prototype), obj); let c = Object.assign(Object.create(this.prototype), obj);
if(typeof c.expiry === "string") if (typeof c.expiry === "string") c.expiry = new Date(c.expiry);
c.expiry = new Date(c.expiry);
return c; return c;
} }
serialize() { serialize() {
return this.name + "=" + this.value; return this.name + "=" + this.value;
} }
hasExpired(sessionEnded) { hasExpired(sessionEnded) {
return sessionEnded && this.expiry === null || this.expiry && this.expiry < new Date(); return (
(sessionEnded && this.expiry === null) ||
(this.expiry && this.expiry < new Date())
);
} }
isValidForRequest(requestURL) { isValidForRequest(requestURL) {
if(this.hasExpired(false)) if (this.hasExpired(false)) return false;
return false;
const parsedURL = url.parse(requestURL); const parsedURL = url.parse(requestURL);
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:" if (
|| this.secure && parsedURL.protocol !== "https:" (parsedURL.protocol !== "http:" &&
|| !validateHostname(this.domain, parsedURL.hostname, this.subdomains) parsedURL.protocol !== "https:") ||
|| !validatePath(this.path, parsedURL.pathname)) (this.secure && parsedURL.protocol !== "https:") ||
!validateHostname(
this.domain,
parsedURL.hostname,
this.subdomains
) ||
!validatePath(this.path, parsedURL.pathname)
)
return false; return false;
return true; return true;
} }
}; }

View File

@ -3,10 +3,15 @@ export class CookieParseError extends Error {
super(...args); super(...args);
this.name = "CookieParseError"; this.name = "CookieParseError";
} }
}; }
export function paramError(position, paramName, functionName, validTypes) { export function paramError(position, paramName, functionName, validTypes) {
validTypes = [validTypes].flat().map(t => "\"" + t + "\""); validTypes = [validTypes].flat().map(t => '"' + t + '"');
validTypes = validTypes.slice(0, -1).join(", ") + (validTypes.length > 1 ? " or " : "") + validTypes.slice(-1); validTypes =
return new TypeError(`${position} parameter "${paramName}" passed to "${functionName}" is not of type ${validTypes}!`); validTypes.slice(0, -1).join(", ") +
}; (validTypes.length > 1 ? " or " : "") +
validTypes.slice(-1);
return new TypeError(
`${position} parameter "${paramName}" passed to "${functionName}" is not of type ${validTypes}!`
);
}

View File

@ -6,56 +6,64 @@ import {paramError, CookieParseError} from "./errors.mjs";
const redirectStatus = new Set([301, 302, 303, 307, 308]); const redirectStatus = new Set([301, 302, 303, 307, 308]);
async function fetch(cookieJars, url, options) { async function fetch(cookieJars, url, options) {
let cookies = ""; let cookies = "";
const addValidFromJars = jars => { const addValidFromJars = jars => {
// since multiple cookie jars can be passed, filter duplicates by using a set of cookie names // since multiple cookie jars can be passed, filter duplicates by using a set of cookie names
const set = new Set(); const set = new Set();
jars.flatMap(jar => [...jar.cookiesValidForRequest(url)]) jars.flatMap(jar => [...jar.cookiesValidForRequest(url)]).forEach(
.forEach(cookie => { cookie => {
if(set.has(cookie.name)) if (set.has(cookie.name)) return;
return;
set.add(cookie.name); set.add(cookie.name);
cookies += cookie.serialize() + "; "; cookies += cookie.serialize() + "; ";
}); }
);
}; };
if(cookieJars) { if (cookieJars) {
if(Array.isArray(cookieJars) && cookieJars.every(c => c instanceof CookieJar)) if (
Array.isArray(cookieJars) &&
cookieJars.every(c => c instanceof CookieJar)
)
addValidFromJars(cookieJars.filter(jar => jar.flags.includes("r"))); addValidFromJars(cookieJars.filter(jar => jar.flags.includes("r")));
else if(cookieJars instanceof CookieJar) else if (cookieJars instanceof CookieJar)
if(cookieJars.flags.includes("r")) if (cookieJars.flags.includes("r")) addValidFromJars([cookieJars]);
addValidFromJars([cookieJars]); else
else throw paramError("First", "cookieJars", "fetch", [
throw paramError("First", "cookieJars", "fetch", ["CookieJar", "[CookieJar]"]); "CookieJar",
"[CookieJar]"
]);
} }
if(cookies) { if (cookies) {
if(!options) if (!options) options = {};
options = {}; if (!options.headers) options.headers = {};
if(!options.headers)
options.headers = {};
options.headers.cookie = cookies.slice(0, -2); options.headers.cookie = cookies.slice(0, -2);
} }
const wantFollow = !options || !options.redirect || options.redirect === 'follow'; const wantFollow =
if(wantFollow) { !options || !options.redirect || options.redirect === "follow";
if (wantFollow) {
if (!options) options = {}; if (!options) options = {};
options.redirect = 'manual'; options.redirect = "manual";
} }
const result = await _fetch(url, options); const result = await _fetch(url, options);
// I cannot use headers.get() here because it joins the cookies to a string // I cannot use headers.get() here because it joins the cookies to a string
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"]; cookies =
if(cookies && cookieJars) { result.headers[Object.getOwnPropertySymbols(result.headers)[0]][
if(Array.isArray(cookieJars)) { "set-cookie"
];
if (cookies && cookieJars) {
if (Array.isArray(cookieJars)) {
cookieJars cookieJars
.filter(jar => jar.flags.includes("w")) .filter(jar => jar.flags.includes("w"))
.forEach(jar => cookies.forEach(c => jar.addCookie(c, url))); .forEach(jar => cookies.forEach(c => jar.addCookie(c, url)));
} } else if (
else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("w")) cookieJars instanceof CookieJar &&
cookieJars.flags.includes("w")
)
cookies.forEach(c => cookieJars.addCookie(c, url)); cookies.forEach(c => cookieJars.addCookie(c, url));
} }
if(wantFollow && redirectStatus.has(result.status)) { if (wantFollow && redirectStatus.has(result.status)) {
const location = result.headers.get('Location'); const location = result.headers.get("Location");
options.redirect = 'follow'; options.redirect = "follow";
return fetch(cookieJars, location, options); return fetch(cookieJars, location, options);
} }
return result; return result;

View File

@ -4,282 +4,412 @@ import {CookieParseError} from "../src/errors.mjs";
export default Test => [ export default Test => [
new Test("new Cookie() / cookie parser", () => { new Test("new Cookie() / cookie parser", () => {
const inputs = [ const inputs = [
[ // type error [
// type error
123, 123,
"" ""
], ],
[ // type error [
// type error
"id=a3fWa", "id=a3fWa",
123 123
], ],
[ // type error invalid url [
// type error invalid url
"id=a3fWa", "id=a3fWa",
"" ""
], ],
[ // type error invalid url [
// type error invalid url
"id=a3fWa", "id=a3fWa",
"github.com" "github.com"
], ],
// TODO: fix this test case, parser shouldn't allow it. it is currently ignored // TODO: fix this test case, parser shouldn't allow it. it is currently ignored
[ // type error invalid url [
"id=a3fWa", // type error invalid url
"id=a3fWa",
"https:abc/abs" "https:abc/abs"
], ],
[ // cookie parse error invalid cookie name [
// cookie parse error invalid cookie name
"", "",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 21 Oct 2015 07:28: GMT; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28: GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 21 Onv 2015 07:28:00 GMT; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 21 Onv 2015 07:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 21 Oct 20151 07:28:00 GMT; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 21 Oct 20151 07:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 32 Oct 2015 07:28:00 GMT; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 32 Oct 2015 07:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 21 Oct 2015 25:28:00 GMT; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 21 Oct 2015 25:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 21 Oct 2015 07:61:00 GMT; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 21 Oct 2015 07:61:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 UTC; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 UTC; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT+2; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT+2; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=San, 21 Onv 2015 07:28:00 GMT; Secure; HttpOnly", "id=a3fWa; Expires=San, 21 Onv 2015 07:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Expires=Wed, 31 Dec 1969 07:28:00 GMT; Secure; HttpOnly", "id=a3fWa; Expires=Wed, 31 Dec 1969 07:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid value for expires [
// cookie parse error invalid value for expires
"id=a3fWa; Max-Age=121252a; Secure; HttpOnly", "id=a3fWa; Max-Age=121252a; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid key secur [
// cookie parse error invalid key secur
"id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secur; HttpOnly", "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secur; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error invalid key HttpOly with value 2 [
// cookie parse error invalid key HttpOly with value 2
"id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOly=2", "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOly=2",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error not set via https [
// cookie parse error not set via https
"__Secure-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly", "__Secure-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly",
"http://github.com" "http://github.com"
], ],
[ // cookie parse error secure not set [
// cookie parse error secure not set
"__Secure-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; HttpOnly", "__Secure-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error secure not set [
// cookie parse error secure not set
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; HttpOnly; Path=/", "__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; HttpOnly; Path=/",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error not set via https [
// cookie parse error not set via https
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/", "__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/",
"http://github.com" "http://github.com"
], ],
[ // cookie parse error domain is set [
// cookie parse error domain is set
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Domain=github.com; Path=/", "__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Domain=github.com; Path=/",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error path is not set [
// cookie parse error path is not set
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly", "__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error path is not equal to / [
// cookie parse error path is not equal to /
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/lel/", "__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/lel/",
"https://github.com" "https://github.com"
], ],
[ // cookie parse error domain is not a subdomain [
// cookie parse error domain is not a subdomain
"id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Domain=github.com", "id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Domain=github.com",
"https://gist.github.com" "https://gist.github.com"
], ],
[ // cookie parse error domain is not a subdomain [
// cookie parse error domain is not a subdomain
"id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Domain=npmjs.com", "id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Domain=npmjs.com",
"https://gist.github.com" "https://gist.github.com"
], ],
[ // success [
// success
"__Secure-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly", "__Secure-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly",
"https://github.com" "https://github.com"
], ],
[ // success [
// success
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/", "__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/",
"https://github.com" "https://github.com"
], ],
[ // success [
// success
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2099 20:28:33 GMT; Secure; HttpOnly; Path=/", "__Host-id=a3fWa; Expires=Wed, 21 Nov 2099 20:28:33 GMT; Secure; HttpOnly; Path=/",
"https://github.com" "https://github.com"
], ],
[ // success [
// success
"id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/lul/; Domain=.usercontent.github.com", "id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/lul/; Domain=.usercontent.github.com",
"https://github.com" "https://github.com"
], ],
[ // success [
// success
"id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; SameSite=Strict; Path=/lul/; Domain=usercontent.github.com", "id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; SameSite=Strict; Path=/lul/; Domain=usercontent.github.com",
"https://github.com" "https://github.com"
], ],
[ // success max-age takes precendence over expires [
// success max-age takes precendence over expires
"id=a3fWa; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=1000", "id=a3fWa; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=1000",
"https://github.com" "https://github.com"
], ],
[ // success max-age takes precendence over expires [
"id=\"a3fWa\"; Max-Age=1000; Expires=Thu, 01 Jan 1970 00:00:00 GMT", // success max-age takes precendence over expires
'id="a3fWa"; Max-Age=1000; Expires=Thu, 01 Jan 1970 00:00:00 GMT',
"https://github.com" "https://github.com"
], ]
]; ];
const catchErrorTest = (input, catchFnc) => { const catchErrorTest = (input, catchFnc) => {
try { try {
new Cookie(...input); new Cookie(...input);
return false; return false;
} } catch (error) {
catch(error) {
return catchFnc(error); return catchFnc(error);
} }
}; };
const catchErrorTypeMessageTest = (input, type, message) => catchErrorTest(input, e => e instanceof type && e.message === message); const catchErrorTypeMessageTest = (input, type, message) =>
catchErrorTest(
input,
e => e instanceof type && e.message === message
);
const compareCookieProps = (input, expiryFnc, properties) => { const compareCookieProps = (input, expiryFnc, properties) => {
const cookie = new Cookie(...input); const cookie = new Cookie(...input);
return Object.entries(properties).every(([prop, value]) => cookie[prop] === value) return (
&& expiryFnc(cookie.expiry); Object.entries(properties).every(
([prop, value]) => cookie[prop] === value
) && expiryFnc(cookie.expiry)
);
}; };
return inputs.slice(0, 3).every(input => catchErrorTest(input, e => e instanceof TypeError)) return (
inputs
.slice(0, 3)
.every(input =>
catchErrorTest(input, e => e instanceof TypeError)
) &&
// cookies[4] is the test case that is ignored for now
// cookies[4] is the test case that is ignored for now catchErrorTypeMessageTest(
inputs[5],
&& catchErrorTypeMessageTest(inputs[5], CookieParseError, "Invalid cookie name \"\"!") CookieParseError,
&& catchErrorTypeMessageTest(inputs[6], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 07:28: GMT\"!") 'Invalid cookie name ""!'
&& catchErrorTypeMessageTest(inputs[7], CookieParseError, "Invalid value for Expires \"Wed, 21 Onv 2015 07:28:00 GMT\"!") ) &&
&& catchErrorTypeMessageTest(inputs[8], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 20151 07:28:00 GMT\"!") catchErrorTypeMessageTest(
&& catchErrorTypeMessageTest(inputs[9], CookieParseError, "Invalid value for Expires \"Wed, 32 Oct 2015 07:28:00 GMT\"!") inputs[6],
&& catchErrorTypeMessageTest(inputs[10], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 25:28:00 GMT\"!") CookieParseError,
&& catchErrorTypeMessageTest(inputs[11], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 07:61:00 GMT\"!") 'Invalid value for Expires "Wed, 21 Oct 2015 07:28: GMT"!'
&& catchErrorTypeMessageTest(inputs[12], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 07:28:00 UTC\"!") ) &&
&& catchErrorTypeMessageTest(inputs[13], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 07:28:00 GMT+2\"!") catchErrorTypeMessageTest(
&& catchErrorTypeMessageTest(inputs[14], CookieParseError, "Invalid value for Expires \"San, 21 Onv 2015 07:28:00 GMT\"!") inputs[7],
&& catchErrorTypeMessageTest(inputs[15], CookieParseError, "Invalid value for Expires \"Wed, 31 Dec 1969 07:28:00 GMT\"!") CookieParseError,
&& catchErrorTypeMessageTest(inputs[16], CookieParseError, "Invalid value for Max-Age \"121252a\"!") 'Invalid value for Expires "Wed, 21 Onv 2015 07:28:00 GMT"!'
&& catchErrorTypeMessageTest(inputs[17], CookieParseError, "Invalid key \"secur\" specified!") ) &&
&& catchErrorTypeMessageTest(inputs[18], CookieParseError, "Invalid key \"httpoly\" with value \"2\" specified!") catchErrorTypeMessageTest(
inputs[8],
&& inputs.slice(19, 21).every(input => catchErrorTypeMessageTest(input, CookieParseError, "Cookie has \"__Secure-\" prefix but \"Secure\" isn't set or the cookie is not set via https!")) CookieParseError,
'Invalid value for Expires "Wed, 21 Oct 20151 07:28:00 GMT"!'
&& inputs.slice(21, 26).every(input => catchErrorTypeMessageTest(input, CookieParseError, "Cookie has \"__Host-\" prefix but \"Secure\" isn't set, the cookie is not set via https, \"Domain\" is set or \"Path\" is not equal to \"/\"!")) ) &&
catchErrorTypeMessageTest(
&& catchErrorTypeMessageTest(inputs[26], CookieParseError, "Invalid value for Domain \"github.com\": cookie was received from \"gist.github.com\"!") inputs[9],
&& catchErrorTypeMessageTest(inputs[27], CookieParseError, "Invalid value for Domain \"npmjs.com\": cookie was received from \"gist.github.com\"!") CookieParseError,
'Invalid value for Expires "Wed, 32 Oct 2015 07:28:00 GMT"!'
&& compareCookieProps( ) &&
inputs[28], catchErrorTypeMessageTest(
exp => exp.getTime() === new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(), inputs[10],
{ CookieParseError,
name: "__Secure-id", 'Invalid value for Expires "Wed, 21 Oct 2015 25:28:00 GMT"!'
value: "a3fWa", ) &&
secure: true, catchErrorTypeMessageTest(
domain: "github.com", inputs[11],
subdomains: false, CookieParseError,
path: "/" 'Invalid value for Expires "Wed, 21 Oct 2015 07:61:00 GMT"!'
} ) &&
) catchErrorTypeMessageTest(
inputs[12],
&& compareCookieProps( CookieParseError,
inputs[29], 'Invalid value for Expires "Wed, 21 Oct 2015 07:28:00 UTC"!'
exp => exp.getTime() === new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(), ) &&
{ catchErrorTypeMessageTest(
name: "__Host-id", inputs[13],
value: "a3fWa", CookieParseError,
secure: true, 'Invalid value for Expires "Wed, 21 Oct 2015 07:28:00 GMT+2"!'
domain: "github.com", ) &&
subdomains: false, catchErrorTypeMessageTest(
path: "/" inputs[14],
} CookieParseError,
) 'Invalid value for Expires "San, 21 Onv 2015 07:28:00 GMT"!'
) &&
&& compareCookieProps( catchErrorTypeMessageTest(
inputs[30], inputs[15],
exp => exp.getTime() === new Date("Wed, 21 Nov 2099 20:28:33 GMT").getTime(), CookieParseError,
{ 'Invalid value for Expires "Wed, 31 Dec 1969 07:28:00 GMT"!'
name: "__Host-id", ) &&
value: "a3fWa", catchErrorTypeMessageTest(
secure: true, inputs[16],
domain: "github.com", CookieParseError,
subdomains: false, 'Invalid value for Max-Age "121252a"!'
path: "/" ) &&
} catchErrorTypeMessageTest(
) inputs[17],
CookieParseError,
&& compareCookieProps( 'Invalid key "secur" specified!'
inputs[31], ) &&
exp => exp.getTime() === new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(), catchErrorTypeMessageTest(
{ inputs[18],
name: "id", CookieParseError,
value: "a3fWa", 'Invalid key "httpoly" with value "2" specified!'
secure: true, ) &&
domain: "usercontent.github.com", inputs
subdomains: true, .slice(19, 21)
path: "/lul/" .every(input =>
} catchErrorTypeMessageTest(
) input,
CookieParseError,
&& compareCookieProps( 'Cookie has "__Secure-" prefix but "Secure" isn\'t set or the cookie is not set via https!'
inputs[32], )
exp => exp.getTime() === new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(), ) &&
{ inputs
name: "id", .slice(21, 26)
value: "a3fWa", .every(input =>
secure: true, catchErrorTypeMessageTest(
domain: "usercontent.github.com", input,
subdomains: true, CookieParseError,
path: "/lul/" 'Cookie has "__Host-" prefix but "Secure" isn\'t set, the cookie is not set via https, "Domain" is set or "Path" is not equal to "/"!'
} )
) ) &&
catchErrorTypeMessageTest(
&& compareCookieProps( inputs[26],
inputs[33], CookieParseError,
exp => exp.getTime() > new Date("Thu, 01 Jan 1970 00:00:00 GMT").getTime(), 'Invalid value for Domain "github.com": cookie was received from "gist.github.com"!'
{ ) &&
name: "id", catchErrorTypeMessageTest(
value: "a3fWa", inputs[27],
secure: false, CookieParseError,
domain: "github.com", 'Invalid value for Domain "npmjs.com": cookie was received from "gist.github.com"!'
subdomains: false, ) &&
path: "/" compareCookieProps(
} inputs[28],
) exp =>
exp.getTime() ===
&& compareCookieProps( new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(),
inputs[34], {
exp => exp.getTime() > new Date("Thu, 01 Jan 1970 00:00:00 GMT").getTime(), name: "__Secure-id",
{ value: "a3fWa",
name: "id", secure: true,
value: "a3fWa", domain: "github.com",
secure: false, subdomains: false,
domain: "github.com", path: "/"
subdomains: false, }
path: "/" ) &&
} compareCookieProps(
inputs[29],
exp =>
exp.getTime() ===
new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(),
{
name: "__Host-id",
value: "a3fWa",
secure: true,
domain: "github.com",
subdomains: false,
path: "/"
}
) &&
compareCookieProps(
inputs[30],
exp =>
exp.getTime() ===
new Date("Wed, 21 Nov 2099 20:28:33 GMT").getTime(),
{
name: "__Host-id",
value: "a3fWa",
secure: true,
domain: "github.com",
subdomains: false,
path: "/"
}
) &&
compareCookieProps(
inputs[31],
exp =>
exp.getTime() ===
new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(),
{
name: "id",
value: "a3fWa",
secure: true,
domain: "usercontent.github.com",
subdomains: true,
path: "/lul/"
}
) &&
compareCookieProps(
inputs[32],
exp =>
exp.getTime() ===
new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(),
{
name: "id",
value: "a3fWa",
secure: true,
domain: "usercontent.github.com",
subdomains: true,
path: "/lul/"
}
) &&
compareCookieProps(
inputs[33],
exp =>
exp.getTime() >
new Date("Thu, 01 Jan 1970 00:00:00 GMT").getTime(),
{
name: "id",
value: "a3fWa",
secure: false,
domain: "github.com",
subdomains: false,
path: "/"
}
) &&
compareCookieProps(
inputs[34],
exp =>
exp.getTime() >
new Date("Thu, 01 Jan 1970 00:00:00 GMT").getTime(),
{
name: "id",
value: "a3fWa",
secure: false,
domain: "github.com",
subdomains: false,
path: "/"
}
)
); );
}), }),
new Test("static Cookie.fromObject()", () => { new Test("static Cookie.fromObject()", () => {
@ -307,24 +437,34 @@ export default Test => [
const cookies = inputs.map(i => Cookie.fromObject(i)); const cookies = inputs.map(i => Cookie.fromObject(i));
return cookies[0].name === "testname" return (
&& cookies[0].value === "somevalue" cookies[0].name === "testname" &&
&& cookies[0].domain === "github.com" cookies[0].value === "somevalue" &&
&& cookies[0].path === "/" cookies[0].domain === "github.com" &&
&& cookies[0].expiry.getTime() === new Date(inputs[0].expiry).getTime() cookies[0].path === "/" &&
&& cookies[0].secure === false cookies[0].expiry.getTime() ===
&& cookies[0].subdomains === false new Date(inputs[0].expiry).getTime() &&
cookies[0].secure === false &&
&& cookies[1].name === "testname" cookies[0].subdomains === false &&
&& cookies[1].value === "lul" cookies[1].name === "testname" &&
&& cookies[1].domain === "somedomain.tld" cookies[1].value === "lul" &&
&& cookies[1].path === "/lel/" cookies[1].domain === "somedomain.tld" &&
&& cookies[1].expiry.getTime() === date.getTime() cookies[1].path === "/lel/" &&
&& cookies[1].secure === true cookies[1].expiry.getTime() === date.getTime() &&
&& cookies[1].subdomains === true; cookies[1].secure === true &&
cookies[1].subdomains === true
);
}), }),
new Test("Cookie.serialize()", () => { new Test("Cookie.serialize()", () => {
return new Cookie("abc=def; Expires=Wed, 20 Oct 2018 08:08:08 GMT; Path=/", "https://somedomain.tld").serialize() === "abc=def" return (
&& new Cookie("jkhsd231=ajkshdgi", "https://somedomain.tld").serialize() === "jkhsd231=ajkshdgi"; new Cookie(
"abc=def; Expires=Wed, 20 Oct 2018 08:08:08 GMT; Path=/",
"https://somedomain.tld"
).serialize() === "abc=def" &&
new Cookie(
"jkhsd231=ajkshdgi",
"https://somedomain.tld"
).serialize() === "jkhsd231=ajkshdgi"
);
}) })
]; ];

View File

@ -8,20 +8,32 @@ export default Test => [
const validTypes = ["lol", "lel", "lul", "lal"]; const validTypes = ["lol", "lel", "lul", "lal"];
const errors = [ const errors = [
paramError(position, paramName, functionName, validTypes[0]), paramError(position, paramName, functionName, validTypes[0]),
paramError(position, paramName, functionName, validTypes.slice(0, 2)), paramError(
position,
paramName,
functionName,
validTypes.slice(0, 2)
),
paramError(position, paramName, functionName, validTypes) paramError(position, paramName, functionName, validTypes)
]; ];
return errors.every(e => e instanceof TypeError) return (
&& errors.every(e => e.name === "TypeError") errors.every(e => e instanceof TypeError) &&
&& errors[0].message === "something parameter \"some_param\" passed to \"some_func\" is not of type \"lol\"!" errors.every(e => e.name === "TypeError") &&
&& errors[1].message === "something parameter \"some_param\" passed to \"some_func\" is not of type \"lol\" or \"lel\"!" errors[0].message ===
&& errors[2].message === "something parameter \"some_param\" passed to \"some_func\" is not of type \"lol\", \"lel\", \"lul\" or \"lal\"!"; 'something parameter "some_param" passed to "some_func" is not of type "lol"!' &&
errors[1].message ===
'something parameter "some_param" passed to "some_func" is not of type "lol" or "lel"!' &&
errors[2].message ===
'something parameter "some_param" passed to "some_func" is not of type "lol", "lel", "lul" or "lal"!'
);
}), }),
new Test("class CookieParseError", () => { new Test("class CookieParseError", () => {
const message = "this is a test error"; const message = "this is a test error";
const error = new CookieParseError(message); const error = new CookieParseError(message);
return error instanceof CookieParseError return (
&& error.name === "CookieParseError" error instanceof CookieParseError &&
&& error.message === message; error.name === "CookieParseError" &&
error.message === message
);
}) })
]; ];

View File

@ -3,43 +3,51 @@ import errors from "./errors.mjs";
class Test { class Test {
constructor(name, fnc) { constructor(name, fnc) {
this.name = name this.name = name;
this.fnc = fnc this.fnc = fnc;
} }
async runTest() { async runTest() {
return this.fnc(); return this.fnc();
} }
} }
const tests = [ const tests = [cookie, errors].flatMap(t => t(Test));
cookie,
errors
].flatMap(t => t(Test));
(async () => { (async () => {
console.log("running tests..."); console.log("running tests...");
const testResults = await Promise.all(tests.map(async t => { const testResults = await Promise.all(
try { tests.map(async t => {
t.result = await t.runTest(); try {
if(t.result !== !!t.result) { t.result = await t.runTest();
if (t.result !== !!t.result) {
t.result = false;
console.error("test did not return a boolean: " + t.name);
}
} catch (error) {
console.error(
"uncaught error in test: " + t.name + "\n",
error
);
t.result = false; t.result = false;
console.error("test did not return a boolean: " + t.name);
} }
} return t;
catch(error) { })
console.error("uncaught error in test: " + t.name + "\n", error); );
t.result = false;
}
return t;
}));
testResults.forEach(t => { testResults.forEach(t => {
if(t.result) if (t.result) console.info("✔ " + t.name);
console.info("✔ " + t.name); else console.warn("✘ " + t.name);
else
console.warn("✘ " + t.name);
}); });
const succeededTests = testResults.map(t => t.result).reduce((a, b) => a + b); const succeededTests = testResults
.map(t => t.result)
.reduce((a, b) => a + b);
const success = succeededTests === testResults.length; const success = succeededTests === testResults.length;
(success ? console.info : console.warn)((success ? "✔" : "✘") + " " + succeededTests + "/" + testResults.length + " tests successful"); (success ? console.info : console.warn)(
(success ? "✔" : "✘") +
" " +
succeededTests +
"/" +
testResults.length +
" tests successful"
);
!success && process.exit(1); !success && process.exit(1);
})(); })();