From f6c3ba8a61879240e98ada4518cc05ab2ab3f5c2 Mon Sep 17 00:00:00 2001 From: jkhsjdhjs Date: Mon, 14 Jan 2019 04:30:38 +0100 Subject: [PATCH] fixes to get it working --- src/cookie-jar.mjs | 8 ++++---- src/cookie.mjs | 15 +++++++++++---- src/index.mjs | 27 +++++++++++++++++++++------ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/cookie-jar.mjs b/src/cookie-jar.mjs index 6da8f52..9bcab6c 100644 --- a/src/cookie-jar.mjs +++ b/src/cookie-jar.mjs @@ -18,14 +18,14 @@ export default class CookieJar { } else if(cookies instanceof Cookie) this.cookies.set(cookies.name, cookies); - else + else if(cookies) throw new TypeError("Third parameter is neither an array nor a cookie!"); if(this.cookies.size === 0 && this.file.length !== 0 && fs.existsSync(this.file)) - this.cookies = new Map(JSON.parse(fs.readFileSync(this.file))); + this.cookies = new Map(JSON.parse(fs.readFileSync(this.file)).map(([k, v]) => [k, Cookie.fromObject(v)])); } - addCookie(c) { + addCookie(c, fromURL) { if(typeof c === "string") - c = new Cookie(c); + c = new Cookie(c, fromURL); this.cookies.set(c.name, c); } forEach(callback) { diff --git a/src/cookie.mjs b/src/cookie.mjs index 81dbff0..e999550 100644 --- a/src/cookie.mjs +++ b/src/cookie.mjs @@ -1,12 +1,16 @@ import urlParser from "url"; const validateHostname = (cookieHostname, requestHostname, subdomains) => { + cookieHostname = cookieHostname.toLowerCase(); + requestHostname = requestHostname.toLowerCase(); if(requestHostname === cookieHostname || (subdomains && requestHostname.endsWith("." + cookieHostname))) return true; return false; }; const validatePath = (cookiePath, requestPath) => { + cookiePath = cookiePath.toLowerCase(); + requestPath = requestPath.toLowerCase(); if(cookiePath.endsWith("/")) cookiePath = cookiePath.slice(0, -1); if(requestPath.endsWith("/")) @@ -32,14 +36,14 @@ export default class Cookie { 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{4} \d{2}:\d{2}:\d{2} GMT$/.test(v) + 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)) === "Invalid Date") throw new TypeError("Invalid value for Expires \"" + v + "\"!"); } else if(k === "max-age") { const seconds = parseInt(v); if(seconds.toString() !== v) - throw new TypeError("Invalid value for Max-Age \"" + v + "\"!") + throw new TypeError("Invalid value for Max-Age \"" + v + "\"!"); this.expiry = new Date(); this.expiry.setSeconds(this.expiry.getSeconds() + seconds); } @@ -78,13 +82,16 @@ export default class Cookie { throw new TypeError("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 \"/\"!"); } static fromObject(obj) { - return Object.assign(Object.create(this.prototype), obj); + let c = Object.assign(Object.create(this.prototype), obj); + if(c.expiry && typeof c.expiry === "string") + c.expiry = new Date(c.expiry); + return c; } serialize() { return this.name + "=" + this.value; } isValidForRequest(url) { - if(this.expiry && this.expiry > new Date()) + if(this.expiry && this.expiry < new Date()) return false; const parsedURL = urlParser.parse(url); if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:") diff --git a/src/index.mjs b/src/index.mjs index 8db2310..e52076d 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -9,16 +9,30 @@ export default { cookieJars.forEach(jar => { if(!jar.flags.includes("r")) return; - jar.forEach(c => c.isValidForRequest(url) && (cookies += c.serialize() + "; ")); + jar.forEach(c => { + if(c.isValidForRequest(url)) + cookies += c.serialize() + "; "; + }); }); } else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("r")) { - cookieJars.forEach(c => c.isValidForRequest(url) && (cookies += c.serialize() + "; ")); + cookieJars.forEach(c => { + if(c.isValidForRequest(url)) + cookies += c.serialize() + "; "; + }); } else throw new TypeError("Third paramter is neither a cookie jar nor an array of cookie jars!"); - if(cookies.length !== 0) - options.headers["Cookie"] = cookies.slice(0, -2); + if(cookies.length !== 0) { + if(!options) { + options = { + headers: {} + }; + } + if(!options.headers) + options.headers = {}; + options.headers.cookie = cookies.slice(0, -2); + } 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"]; @@ -27,13 +41,14 @@ export default { cookieJars.forEach(jar => { if(!jar.flags.includes("w")) return; - cookies.forEach(c => jar.addCookie(c)); + cookies.forEach(c => jar.addCookie(c, url)); }); } else if(cookieJars.flags.includes("w")) { - cookies.forEach(c => cookieJars.addCookie(c)); + cookies.forEach(c => cookieJars.addCookie(c, url)); } } + return result; }, CookieJar: CookieJar, Cookie: Cookie