From 836a748d9eb44bb5cdffd25f2794284a11dbe022 Mon Sep 17 00:00:00 2001 From: jkhsjdhjs Date: Mon, 2 Dec 2019 16:00:12 +0100 Subject: [PATCH 1/2] test: change boolean check --- test/index.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.mjs b/test/index.mjs index 9c33aa2..e1d2c04 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -21,7 +21,7 @@ const tests = [ const testResults = await Promise.all(tests.map(async t => { try { t.result = await t.runTest(); - if(typeof t.result !== "boolean") { + if(t.result !== !!t.result) { t.result = false; console.error("test did not return a boolean: " + t.name); } From f18fc559998d68e012e91a52c553c9f712589819 Mon Sep 17 00:00:00 2001 From: jkhsjdhjs Date: Mon, 2 Dec 2019 18:46:46 +0100 Subject: [PATCH 2/2] cookie-jar: make logging ignored cookies optional by adding a callback function to the class attributes readme: update accordingly bump patch version fix #2 --- README.md | 5 ++++- package-lock.json | 2 +- package.json | 2 +- src/cookie-jar.mjs | 21 ++++++++++++--------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 30690c2..266a7d8 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package-lock.json b/package-lock.json index 7aec41c..3c0efa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "node-fetch-cookies", - "version": "1.3.3", + "version": "1.3.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 25b3b54..c1cd0d2 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/cookie-jar.mjs b/src/cookie-jar.mjs index 13912bc..761e3d8 100644 --- a/src/cookie-jar.mjs +++ b/src/cookie-jar.mjs @@ -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;