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
node_js:
- node
- lts/*
- 11.14.0
- node
- lts/*
- 11.14.0
script:
- node --experimental-modules test/index.mjs
- npm test
deploy:
provider: npm
email: npm@totally.rip
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=
on:
branch: master
tags: true
repo: jkhsjdhjs/node-fetch-cookies
provider: npm
email: npm@totally.rip
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=
on:
branch: master
tags: true
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).
## Usage Examples
### with file...
```javascript
import {fetch, CookieJar} from "node-fetch-cookies";
@ -27,6 +28,7 @@ import {fetch, CookieJar} from "node-fetch-cookies";
```
### ...or without
```javascript
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
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
response = await fetch(cookieJar, "https://example.com/api/logout");
})();
```
## Documentation
### 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.
### Class: CookieJar
A class that stores cookies.
#### Properties
- `flags` The read/write flags as specified below.
- `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.
- `flags` The read/write flags as specified below.
- `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])
- `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`
- `r`: only read from this jar
- `w`: only write to this jar
- `rw` or `wr`: read/write from/to this jar
- `cookies` An optional initializer for the cookie jar - either an array of [Cookie](#class-cookie) instances or a single Cookie instance.
- `cookieIgnoreCallback(cookie, reason)` An optional callback function which will be called when a cookie is ignored instead of added to the cookie jar.
- `cookie` The cookie string
- `reason` A string containing the reason why the cookie has been ignored
- `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`
- `r`: only read from this jar
- `w`: only write to this jar
- `rw` or `wr`: read/write from/to this jar
- `cookies` An optional initializer for the cookie jar - either an array of [Cookie](#class-cookie) instances or a single Cookie instance.
- `cookieIgnoreCallback(cookie, reason)` An optional callback function which will be called when a cookie is ignored instead of added to the cookie jar.
- `cookie` The cookie string
- `reason` A string containing the reason why the cookie has been ignored
#### addCookie(cookie[, fromURL])
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.
In this case `fromURL` must be specified.
- `fromURL` The url a cookie has been received from.
- `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.
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.
If the parser throws a [CookieParseError](#class-cookieparseerror) it will be caught and a warning will be printed to console.
#### domains()
Returns an iterator over all domains currently stored cookies for.
#### *cookiesDomain(domain)
#### \*cookiesDomain(domain)
Returns an iterator over all cookies currently stored for `domain`.
#### *cookiesValid(withSession)
Returns an iterator over all valid (non-expired) cookies.
- `withSession`: A boolean. Iterator will include session cookies if set to `true`.
#### \*cookiesValid(withSession)
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.
#### *cookiesValidForRequest(requestURL)
#### \*cookiesValidForRequest(requestURL)
Returns an iterator over all cookies valid for a request to `url`.
#### deleteExpired(sessionEnded)
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])
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])
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
An abstract representation of a cookie.
#### Properties
- `name` The identifier of the cookie.
- `value` The value 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.
- `domain` The domain the cookie is valid for.
- `path` The path 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.
- `subdomains` A boolean value specifying whether the cookie should be used for requests to subdomains of `domain` or not.
- `name` The identifier of the cookie.
- `value` The value 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.
- `domain` The domain the cookie is valid for.
- `path` The path 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.
- `subdomains` A boolean value specifying whether the cookie should be used for requests to subdomains of `domain` or not.
#### new Cookie(str, requestURL)
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.
#### static fromObject(obj)
Creates a cookie instance from an already existing object with the same properties.
#### serialize()
Serializes the cookie, transforming it to `name=value` so it can be used in requests.
#### hasExpired(sessionEnded)
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)
Returns whether the cookie is valid for a request to `url`.
### 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.
## 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
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",
"version": "1.3.6",
"description": "node-fetch wrapper that adds support for cookie-jars",
"main": "src/index.mjs",
"engines": {
"node": ">=11.14.0"
},
"files": [
"src/"
],
"scripts": {
"test": "node test/index.mjs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jkhsjdhjs/node-fetch-cookies.git"
},
"keywords": [
"cookie",
"cookie-jar",
"node-fetch",
"fetch"
],
"author": "jkhsjdhjs",
"license": "MIT",
"bugs": {
"url": "https://github.com/jkhsjdhjs/node-fetch-cookies/issues"
},
"homepage": "https://github.com/jkhsjdhjs/node-fetch-cookies#readme",
"dependencies": {
"node-fetch": "^2.6.0"
}
"name": "node-fetch-cookies",
"version": "1.3.6",
"description": "node-fetch wrapper that adds support for cookie-jars",
"main": "src/index.mjs",
"engines": {
"node": ">=11.14.0"
},
"files": [
"src/"
],
"scripts": {
"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": {
"type": "git",
"url": "git+https://github.com/jkhsjdhjs/node-fetch-cookies.git"
},
"keywords": [
"cookie",
"cookie-jar",
"node-fetch",
"fetch"
],
"author": "jkhsjdhjs",
"license": "MIT",
"bugs": {
"url": "https://github.com/jkhsjdhjs/node-fetch-cookies/issues"
},
"homepage": "https://github.com/jkhsjdhjs/node-fetch-cookies#readme",
"dependencies": {
"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 {
constructor(file, flags = "rw", cookies, cookieIgnoreCallback) {
this.cookies = new Map();
if(file && typeof file !== "string")
if (file && typeof file !== "string")
throw paramError("Second", "file", "new CookieJar()", "string");
if(typeof flags !== "string")
if (typeof flags !== "string")
throw paramError("First", "flags", "new CookieJar()", "string");
if(Array.isArray(cookies)) {
if(!cookies.every(c => c instanceof Cookie))
throw paramError("Third", "cookies", "new CookieJar()", "[Cookie]");
if (Array.isArray(cookies)) {
if (!cookies.every(c => c instanceof Cookie))
throw paramError(
"Third",
"cookies",
"new CookieJar()",
"[Cookie]"
);
cookies.forEach(cookie => this.addCookie(cookie));
}
else if(cookies instanceof Cookie)
this.addCookie(cookies);
else if(cookies)
throw paramError("Third", "cookies", "new CookieJar()", ["[Cookie]", "Cookie"]);
if(cookieIgnoreCallback && typeof cookieIgnoreCallback !== "function")
throw paramError("Fourth", "cookieIgnoreCallback", "new CookieJar()", "function");
} else if (cookies instanceof Cookie) this.addCookie(cookies);
else if (cookies)
throw paramError("Third", "cookies", "new CookieJar()", [
"[Cookie]",
"Cookie"
]);
if (cookieIgnoreCallback && typeof cookieIgnoreCallback !== "function")
throw paramError(
"Fourth",
"cookieIgnoreCallback",
"new CookieJar()",
"function"
);
this.file = file;
this.flags = flags;
this.cookieIgnoreCallback = cookieIgnoreCallback;
}
addCookie(cookie, fromURL) {
if(typeof cookie === "string") {
if (typeof cookie === "string") {
try {
cookie = new Cookie(cookie, fromURL);
}
catch(error) {
if(error instanceof CookieParseError) {
if(this.cookieIgnoreCallback)
} catch (error) {
if (error instanceof CookieParseError) {
if (this.cookieIgnoreCallback)
this.cookieIgnoreCallback(cookie, error.message);
return false;
}
throw error;
}
}
else if(!(cookie instanceof Cookie))
throw paramError("First", "cookie", "CookieJar.addCookie()", ["string", "Cookie"]);
if(!this.cookies.get(cookie.domain))
} else if (!(cookie instanceof Cookie))
throw paramError("First", "cookie", "CookieJar.addCookie()", [
"string",
"Cookie"
]);
if (!this.cookies.get(cookie.domain))
this.cookies.set(cookie.domain, new Map());
this.cookies.get(cookie.domain).set(cookie.name, cookie);
return true;
@ -51,30 +63,29 @@ export default class CookieJar {
return this.cookies.keys();
}
*cookiesDomain(domain) {
for(const cookie of (this.cookies.get(domain) || []).values())
for (const cookie of (this.cookies.get(domain) || []).values())
yield cookie;
}
*cookiesValid(withSession) {
for(const cookie of this.cookiesAll())
if(!cookie.hasExpired(!withSession))
yield cookie;
for (const cookie of this.cookiesAll())
if (!cookie.hasExpired(!withSession)) yield cookie;
}
*cookiesAll() {
for(const domain of this.domains())
yield* this.cookiesDomain(domain);
for (const domain of this.domains()) yield* this.cookiesDomain(domain);
}
*cookiesValidForRequest(requestURL) {
const namesYielded = new Set(),
domains = url
domains = url
.parse(requestURL)
.hostname
.split(".")
.hostname.split(".")
.map((_, i, a) => a.slice(i).join("."))
.slice(0, -1);
for(const domain of domains) {
for(const cookie of this.cookiesDomain(domain)) {
if(cookie.isValidForRequest(requestURL)
&& !namesYielded.has(cookie.name)) {
for (const domain of domains) {
for (const cookie of this.cookiesDomain(domain)) {
if (
cookie.isValidForRequest(requestURL) &&
!namesYielded.has(cookie.name)
) {
namesYielded.add(cookie.name);
yield cookie;
}
@ -87,14 +98,19 @@ export default class CookieJar {
validCookies.forEach(c => this.addCookie(c));
}
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!");
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) {
if(typeof file !== "string")
if (typeof file !== "string")
throw new Error("No file has been specified for this cookie jar!");
// 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) => {
cookieHostname = cookieHostname.toLowerCase();
requestHostname = requestHostname.toLowerCase();
if(requestHostname === cookieHostname || (subdomains && requestHostname.endsWith("." + cookieHostname)))
if (
requestHostname === cookieHostname ||
(subdomains && requestHostname.endsWith("." + cookieHostname))
)
return true;
return false;
};
@ -12,16 +15,14 @@ const validateHostname = (cookieHostname, requestHostname, subdomains) => {
const validatePath = (cookiePath, requestPath) => {
cookiePath = decodeURIComponent(cookiePath).toLowerCase();
requestPath = decodeURIComponent(requestPath).toLowerCase();
if(cookiePath.endsWith("/"))
cookiePath = cookiePath.slice(0, -1);
if(requestPath.endsWith("/"))
requestPath = requestPath.slice(0, -1);
if (cookiePath.endsWith("/")) cookiePath = cookiePath.slice(0, -1);
if (requestPath.endsWith("/")) requestPath = requestPath.slice(0, -1);
return (requestPath + "/").startsWith(cookiePath + "/");
};
const splitN = (str, sep, n) => {
const splitted = str.split(sep);
if(n < splitted.length - 1) {
if (n < splitted.length - 1) {
splitted[n] = splitted.slice(n).join(sep);
splitted.splice(n + 1);
}
@ -30,9 +31,9 @@ const splitN = (str, sep, n) => {
export default class Cookie {
constructor(str, requestURL) {
if(typeof str !== "string")
if (typeof str !== "string")
throw paramError("First", "str", "new Cookie()", "string");
if(typeof requestURL !== "string")
if (typeof requestURL !== "string")
throw paramError("Second", "requestURL", "new Cookie()", "string");
// check if url is valid
@ -40,95 +41,134 @@ export default class Cookie {
const splitted = str.split("; ");
[this.name, this.value] = splitN(splitted[0], "=", 1);
if(!this.name)
throw new CookieParseError("Invalid cookie name \"" + this.name + "\"!");
if(this.value.startsWith("\"") && this.value.endsWith("\""))
if (!this.name)
throw new CookieParseError(
'Invalid cookie name "' + this.name + '"!'
);
if (this.value.startsWith('"') && this.value.endsWith('"'))
this.value = this.value.slice(1, -1);
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);
k = k.toLowerCase();
if(v) {
if(k === "expires") {
if(this.expiry) // max-age has precedence over expires
if (v) {
if (k === "expires") {
if (this.expiry)
// max-age has precedence over expires
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)
|| (this.expiry = new Date(v)).toString() === "Invalid Date"
|| this.expiry.getTime() < 0)
throw new CookieParseError("Invalid value for Expires \"" + v + "\"!");
}
else if(k === "max-age") {
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
) ||
(this.expiry = new Date(v)).toString() ===
"Invalid Date" ||
this.expiry.getTime() < 0
)
throw new CookieParseError(
'Invalid value for Expires "' + v + '"!'
);
} else if (k === "max-age") {
const seconds = ~~+v;
if(seconds.toString() !== v)
throw new CookieParseError("Invalid value for Max-Age \"" + v + "\"!");
if (seconds.toString() !== v)
throw new CookieParseError(
'Invalid value for Max-Age "' + v + '"!'
);
this.expiry = new Date();
this.expiry.setSeconds(this.expiry.getSeconds() + seconds);
}
else if(k === "domain") {
if(v.startsWith("."))
v = v.substring(1);
if(!validateHostname(parsedURL.hostname, v, true))
throw new CookieParseError("Invalid value for Domain \"" + v + "\": cookie was received from \"" + parsedURL.hostname + "\"!");
} else if (k === "domain") {
if (v.startsWith(".")) v = v.substring(1);
if (!validateHostname(parsedURL.hostname, v, true))
throw new CookieParseError(
'Invalid value for Domain "' +
v +
'": cookie was received from "' +
parsedURL.hostname +
'"!'
);
this.domain = v;
this.subdomains = true;
}
else if(k === "path")
this.path = v;
else if(k === "samesite") // only relevant for cross site requests, so not for us
} else if (k === "path") this.path = v;
else if (k === "samesite")
// only relevant for cross site requests, so not for us
continue;
else
throw new CookieParseError("Invalid key \"" + k + "\" with value \"" + v + "\" specified!");
}
else {
if(k === "secure")
this.secure = true;
else if(k === "httponly") // only relevant for browsers
throw new CookieParseError(
'Invalid key "' +
k +
'" with value "' +
v +
'" specified!'
);
} else {
if (k === "secure") this.secure = true;
else if (k === "httponly")
// only relevant for browsers
continue;
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:"))
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 \"/\"!");
if (
this.name.toLowerCase().startsWith("__secure-") &&
(!this.secure || parsedURL.protocol !== "https:")
)
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
if(!this.domain) {
if (!this.domain) {
this.domain = parsedURL.hostname;
this.subdomains = false;
}
if(!this.path)
this.path = "/";
if(!this.secure)
this.secure = false;
if(!this.expiry)
this.expiry = null;
if (!this.path) this.path = "/";
if (!this.secure) this.secure = false;
if (!this.expiry) this.expiry = null;
}
static fromObject(obj) {
let c = Object.assign(Object.create(this.prototype), obj);
if(typeof c.expiry === "string")
c.expiry = new Date(c.expiry);
if (typeof c.expiry === "string") c.expiry = new Date(c.expiry);
return c;
}
serialize() {
return this.name + "=" + this.value;
}
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) {
if(this.hasExpired(false))
return false;
if (this.hasExpired(false)) return false;
const parsedURL = url.parse(requestURL);
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:"
|| this.secure && parsedURL.protocol !== "https:"
|| !validateHostname(this.domain, parsedURL.hostname, this.subdomains)
|| !validatePath(this.path, parsedURL.pathname))
if (
(parsedURL.protocol !== "http:" &&
parsedURL.protocol !== "https:") ||
(this.secure && parsedURL.protocol !== "https:") ||
!validateHostname(
this.domain,
parsedURL.hostname,
this.subdomains
) ||
!validatePath(this.path, parsedURL.pathname)
)
return false;
return true;
}
};
}

View File

@ -3,10 +3,15 @@ export class CookieParseError extends Error {
super(...args);
this.name = "CookieParseError";
}
};
}
export function paramError(position, paramName, functionName, validTypes) {
validTypes = [validTypes].flat().map(t => "\"" + t + "\"");
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}!`);
};
validTypes = [validTypes].flat().map(t => '"' + t + '"');
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]);
async function fetch(cookieJars, url, options) {
let cookies = "";
const addValidFromJars = jars => {
// since multiple cookie jars can be passed, filter duplicates by using a set of cookie names
const set = new Set();
jars.flatMap(jar => [...jar.cookiesValidForRequest(url)])
.forEach(cookie => {
if(set.has(cookie.name))
return;
jars.flatMap(jar => [...jar.cookiesValidForRequest(url)]).forEach(
cookie => {
if (set.has(cookie.name)) return;
set.add(cookie.name);
cookies += cookie.serialize() + "; ";
});
}
);
};
if(cookieJars) {
if(Array.isArray(cookieJars) && cookieJars.every(c => c instanceof CookieJar))
if (cookieJars) {
if (
Array.isArray(cookieJars) &&
cookieJars.every(c => c instanceof CookieJar)
)
addValidFromJars(cookieJars.filter(jar => jar.flags.includes("r")));
else if(cookieJars instanceof CookieJar)
if(cookieJars.flags.includes("r"))
addValidFromJars([cookieJars]);
else
throw paramError("First", "cookieJars", "fetch", ["CookieJar", "[CookieJar]"]);
else if (cookieJars instanceof CookieJar)
if (cookieJars.flags.includes("r")) addValidFromJars([cookieJars]);
else
throw paramError("First", "cookieJars", "fetch", [
"CookieJar",
"[CookieJar]"
]);
}
if(cookies) {
if(!options)
options = {};
if(!options.headers)
options.headers = {};
if (cookies) {
if (!options) options = {};
if (!options.headers) options.headers = {};
options.headers.cookie = cookies.slice(0, -2);
}
const wantFollow = !options || !options.redirect || options.redirect === 'follow';
if(wantFollow) {
const wantFollow =
!options || !options.redirect || options.redirect === "follow";
if (wantFollow) {
if (!options) options = {};
options.redirect = 'manual';
options.redirect = "manual";
}
const result = await _fetch(url, options);
// I cannot use headers.get() here because it joins the cookies to a string
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
if(cookies && cookieJars) {
if(Array.isArray(cookieJars)) {
cookies =
result.headers[Object.getOwnPropertySymbols(result.headers)[0]][
"set-cookie"
];
if (cookies && cookieJars) {
if (Array.isArray(cookieJars)) {
cookieJars
.filter(jar => jar.flags.includes("w"))
.forEach(jar => cookies.forEach(c => jar.addCookie(c, url)));
}
else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("w"))
} else if (
cookieJars instanceof CookieJar &&
cookieJars.flags.includes("w")
)
cookies.forEach(c => cookieJars.addCookie(c, url));
}
if(wantFollow && redirectStatus.has(result.status)) {
const location = result.headers.get('Location');
options.redirect = 'follow';
if (wantFollow && redirectStatus.has(result.status)) {
const location = result.headers.get("Location");
options.redirect = "follow";
return fetch(cookieJars, location, options);
}
return result;

View File

@ -4,282 +4,412 @@ import {CookieParseError} from "../src/errors.mjs";
export default Test => [
new Test("new Cookie() / cookie parser", () => {
const inputs = [
[ // type error
[
// type error
123,
""
],
[ // type error
[
// type error
"id=a3fWa",
123
],
[ // type error invalid url
[
// type error invalid url
"id=a3fWa",
""
],
[ // type error invalid url
[
// type error invalid url
"id=a3fWa",
"github.com"
],
// 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"
],
[ // cookie parse error invalid cookie name
[
// cookie parse error invalid cookie name
"",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"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",
"https://github.com"
],
[ // cookie parse error invalid value for expires
[
// cookie parse error invalid value for expires
"id=a3fWa; Max-Age=121252a; Secure; HttpOnly",
"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",
"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",
"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",
"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",
"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=/",
"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=/",
"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=/",
"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",
"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/",
"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",
"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",
"https://gist.github.com"
],
[ // success
[
// success
"__Secure-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly",
"https://github.com"
],
[ // success
[
// success
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/",
"https://github.com"
],
[ // success
[
// success
"__Host-id=a3fWa; Expires=Wed, 21 Nov 2099 20:28:33 GMT; Secure; HttpOnly; Path=/",
"https://github.com"
],
[ // success
[
// success
"id=a3fWa; Expires=Wed, 21 Nov 2015 07:28:00 GMT; Secure; HttpOnly; Path=/lul/; Domain=.usercontent.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",
"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",
"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"
],
]
];
const catchErrorTest = (input, catchFnc) => {
try {
new Cookie(...input);
return false;
}
catch(error) {
} catch (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 cookie = new Cookie(...input);
return Object.entries(properties).every(([prop, value]) => cookie[prop] === value)
&& expiryFnc(cookie.expiry);
return (
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], CookieParseError, "Invalid cookie name \"\"!")
&& catchErrorTypeMessageTest(inputs[6], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 07:28: GMT\"!")
&& 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(inputs[9], CookieParseError, "Invalid value for Expires \"Wed, 32 Oct 2015 07:28:00 GMT\"!")
&& catchErrorTypeMessageTest(inputs[10], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 25:28:00 GMT\"!")
&& catchErrorTypeMessageTest(inputs[11], CookieParseError, "Invalid value for Expires \"Wed, 21 Oct 2015 07:61:00 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(inputs[14], CookieParseError, "Invalid value for Expires \"San, 21 Onv 2015 07:28:00 GMT\"!")
&& catchErrorTypeMessageTest(inputs[15], CookieParseError, "Invalid value for Expires \"Wed, 31 Dec 1969 07:28:00 GMT\"!")
&& catchErrorTypeMessageTest(inputs[16], CookieParseError, "Invalid value for Max-Age \"121252a\"!")
&& catchErrorTypeMessageTest(inputs[17], CookieParseError, "Invalid key \"secur\" specified!")
&& catchErrorTypeMessageTest(inputs[18], CookieParseError, "Invalid key \"httpoly\" with value \"2\" specified!")
&& 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!"))
&& 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(inputs[26], CookieParseError, "Invalid value for Domain \"github.com\": cookie was received from \"gist.github.com\"!")
&& catchErrorTypeMessageTest(inputs[27], CookieParseError, "Invalid value for Domain \"npmjs.com\": cookie was received from \"gist.github.com\"!")
&& compareCookieProps(
inputs[28],
exp => exp.getTime() === new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(),
{
name: "__Secure-id",
value: "a3fWa",
secure: true,
domain: "github.com",
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: "/"
}
catchErrorTypeMessageTest(
inputs[5],
CookieParseError,
'Invalid cookie name ""!'
) &&
catchErrorTypeMessageTest(
inputs[6],
CookieParseError,
'Invalid value for Expires "Wed, 21 Oct 2015 07:28: GMT"!'
) &&
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(
inputs[9],
CookieParseError,
'Invalid value for Expires "Wed, 32 Oct 2015 07:28:00 GMT"!'
) &&
catchErrorTypeMessageTest(
inputs[10],
CookieParseError,
'Invalid value for Expires "Wed, 21 Oct 2015 25:28:00 GMT"!'
) &&
catchErrorTypeMessageTest(
inputs[11],
CookieParseError,
'Invalid value for Expires "Wed, 21 Oct 2015 07:61:00 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(
inputs[14],
CookieParseError,
'Invalid value for Expires "San, 21 Onv 2015 07:28:00 GMT"!'
) &&
catchErrorTypeMessageTest(
inputs[15],
CookieParseError,
'Invalid value for Expires "Wed, 31 Dec 1969 07:28:00 GMT"!'
) &&
catchErrorTypeMessageTest(
inputs[16],
CookieParseError,
'Invalid value for Max-Age "121252a"!'
) &&
catchErrorTypeMessageTest(
inputs[17],
CookieParseError,
'Invalid key "secur" specified!'
) &&
catchErrorTypeMessageTest(
inputs[18],
CookieParseError,
'Invalid key "httpoly" with value "2" specified!'
) &&
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!'
)
) &&
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(
inputs[26],
CookieParseError,
'Invalid value for Domain "github.com": cookie was received from "gist.github.com"!'
) &&
catchErrorTypeMessageTest(
inputs[27],
CookieParseError,
'Invalid value for Domain "npmjs.com": cookie was received from "gist.github.com"!'
) &&
compareCookieProps(
inputs[28],
exp =>
exp.getTime() ===
new Date("Wed, 21 Nov 2015 07:28:00 GMT").getTime(),
{
name: "__Secure-id",
value: "a3fWa",
secure: true,
domain: "github.com",
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()", () => {
@ -307,24 +437,34 @@ export default Test => [
const cookies = inputs.map(i => Cookie.fromObject(i));
return cookies[0].name === "testname"
&& cookies[0].value === "somevalue"
&& cookies[0].domain === "github.com"
&& cookies[0].path === "/"
&& cookies[0].expiry.getTime() === new Date(inputs[0].expiry).getTime()
&& cookies[0].secure === false
&& cookies[0].subdomains === false
&& cookies[1].name === "testname"
&& cookies[1].value === "lul"
&& cookies[1].domain === "somedomain.tld"
&& cookies[1].path === "/lel/"
&& cookies[1].expiry.getTime() === date.getTime()
&& cookies[1].secure === true
&& cookies[1].subdomains === true;
return (
cookies[0].name === "testname" &&
cookies[0].value === "somevalue" &&
cookies[0].domain === "github.com" &&
cookies[0].path === "/" &&
cookies[0].expiry.getTime() ===
new Date(inputs[0].expiry).getTime() &&
cookies[0].secure === false &&
cookies[0].subdomains === false &&
cookies[1].name === "testname" &&
cookies[1].value === "lul" &&
cookies[1].domain === "somedomain.tld" &&
cookies[1].path === "/lel/" &&
cookies[1].expiry.getTime() === date.getTime() &&
cookies[1].secure === true &&
cookies[1].subdomains === true
);
}),
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"
&& new Cookie("jkhsd231=ajkshdgi", "https://somedomain.tld").serialize() === "jkhsd231=ajkshdgi";
return (
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 errors = [
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)
];
return errors.every(e => e instanceof TypeError)
&& errors.every(e => e.name === "TypeError")
&& errors[0].message === "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\"!";
return (
errors.every(e => e instanceof TypeError) &&
errors.every(e => e.name === "TypeError") &&
errors[0].message ===
'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", () => {
const message = "this is a test error";
const error = new CookieParseError(message);
return error instanceof CookieParseError
&& error.name === "CookieParseError"
&& error.message === message;
return (
error instanceof CookieParseError &&
error.name === "CookieParseError" &&
error.message === message
);
})
];

View File

@ -3,43 +3,51 @@ import errors from "./errors.mjs";
class Test {
constructor(name, fnc) {
this.name = name
this.fnc = fnc
this.name = name;
this.fnc = fnc;
}
async runTest() {
return this.fnc();
}
}
const tests = [
cookie,
errors
].flatMap(t => t(Test));
const tests = [cookie, errors].flatMap(t => t(Test));
(async () => {
console.log("running tests...");
const testResults = await Promise.all(tests.map(async t => {
try {
t.result = await t.runTest();
if(t.result !== !!t.result) {
const testResults = await Promise.all(
tests.map(async t => {
try {
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;
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;
}
return t;
}));
return t;
})
);
testResults.forEach(t => {
if(t.result)
console.info("✔ " + t.name);
else
console.warn("✘ " + t.name);
if (t.result) console.info("✔ " + 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;
(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);
})();