cookie-jar: make logging ignored cookies optional

by adding a callback function to the class attributes
readme: update accordingly
bump patch version
fix #2
This commit is contained in:
jkhsjdhjs 2019-12-02 18:46:46 +01:00
parent 836a748d9e
commit f18fc55999
Signed by: jkhsjdhjs
GPG Key ID: BAC6ADBAB7D576CC
4 changed files with 18 additions and 12 deletions

View File

@ -64,13 +64,16 @@ A class that stores cookies.
- `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])
#### 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
#### addCookie(cookie[, fromURL])
Adds a cookie to the jar.

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "node-fetch-cookies",
"version": "1.3.3",
"version": "1.3.4",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "node-fetch-cookies",
"version": "1.3.3",
"version": "1.3.4",
"description": "node-fetch wrapper that adds support for cookie-jars",
"main": "src/index.mjs",
"engines": {

View File

@ -4,14 +4,11 @@ import Cookie from "./cookie.mjs";
import {paramError, CookieParseError} from "./errors.mjs";
export default class CookieJar {
constructor(file, flags = "rw", cookies) {
this.flags = flags;
this.file = file;
this.cookies = new Map();
if(typeof this.flags !== "string")
throw paramError("First", "flags", "new CookieJar()", "string");
if(this.file && typeof this.file !== "string")
constructor(file, flags = "rw", cookies, cookieIgnoreCallback) {
if(file && typeof file !== "string")
throw paramError("Second", "file", "new CookieJar()", "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]");
@ -21,6 +18,12 @@ export default class CookieJar {
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.cookies = new Map();
this.cookieIgnoreCallback = cookieIgnoreCallback;
}
addCookie(cookie, fromURL) {
if(typeof cookie === "string") {
@ -29,8 +32,8 @@ export default class CookieJar {
}
catch(error) {
if(error instanceof CookieParseError) {
console.warn("Ignored cookie: " + cookie);
console.warn("Reason: " + error.message);
if(this.cookieIgnoreCallback)
this.cookieIgnoreCallback(cookie, error.message);
return false;
}
throw error;