merge latest node-fetch-cookies version

This commit is contained in:
jkhsjdhjs 2019-08-15 23:14:23 +02:00
parent bd2009cf72
commit 0c767b3fee
Signed by: jkhsjdhjs
GPG Key ID: BAC6ADBAB7D576CC
5 changed files with 134 additions and 60 deletions

4
package-lock.json generated
View File

@ -1,5 +1,5 @@
{ {
"name": "node-fetch-cookies", "name": "flumm-fetch-cookies",
"version": "1.0.6", "version": "1.1.0",
"lockfileVersion": 1 "lockfileVersion": 1
} }

View File

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

View File

@ -1,15 +1,69 @@
import Cookie from "./cookie"; import Cookie from "./cookie";
import url from "url";
export default class CookieJar { export default class CookieJar {
constructor() { constructor() {
this.cookies = new Map(); this.cookies = new Map();
} }
addCookie(c, fromURL) { addCookie(c, fromURL) {
if(typeof c === "string") if(typeof c === "string") {
try {
c = new Cookie(c, fromURL); c = new Cookie(c, fromURL);
this.cookies.set(c.name, c);
} }
forEach(callback) { catch(error) {
this.cookies.forEach(callback); if(error.name === "CookieParseError") {
console.warn("Ignored cookie: " + c);
console.warn("Reason: " + error.message);
return false;
}
else
throw error;
}
}
else if(!(c instanceof Cookie))
throw new TypeError("First parameter is neither a string nor a cookie!");
if(!this.cookies.get(c.domain))
this.cookies.set(c.domain, new Map());
this.cookies.get(c.domain).set(c.name, c);
return true;
}
domains() {
return this.cookies.keys();
}
*cookiesDomain(domain) {
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;
}
*cookiesAll() {
for(const domain of this.domains())
yield* this.cookiesDomain(domain);
}
*cookiesValidForRequest(requestURL) {
const namesYielded = [],
domains = url
.parse(requestURL)
.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.every(name => name !== cookie.name)) {
namesYielded.push(cookie.name);
yield cookie;
}
}
}
}
deleteExpired(sessionEnded) {
const validCookies = [...this.cookiesValid(!sessionEnded)];
this.cookies = new Map();
validCookies.forEach(c => this.addCookie(c));
} }
}; };

View File

@ -1,4 +1,11 @@
import urlParser from "url"; import url from "url";
class CookieParseError extends Error {
constructor(...args) {
super(...args);
this.name = "CookieParseError";
}
}
const validateHostname = (cookieHostname, requestHostname, subdomains) => { const validateHostname = (cookieHostname, requestHostname, subdomains) => {
cookieHostname = cookieHostname.toLowerCase(); cookieHostname = cookieHostname.toLowerCase();
@ -9,8 +16,8 @@ const validateHostname = (cookieHostname, requestHostname, subdomains) => {
}; };
const validatePath = (cookiePath, requestPath) => { const validatePath = (cookiePath, requestPath) => {
cookiePath = cookiePath.toLowerCase(); cookiePath = decodeURIComponent(cookiePath).toLowerCase();
requestPath = 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("/")) if(requestPath.endsWith("/"))
@ -18,18 +25,31 @@ const validatePath = (cookiePath, requestPath) => {
return (requestPath + "/").startsWith(cookiePath + "/"); return (requestPath + "/").startsWith(cookiePath + "/");
}; };
const splitN = (str, sep, n) => {
const splitted = str.split(sep);
if(n < splitted.length - 1) {
splitted[n] = splitted.slice(n).join(sep);
splitted.splice(n + 1);
}
return splitted;
};
export default class Cookie { export default class Cookie {
constructor(str, url) { constructor(str, requestURL) {
if(typeof str !== "string") if(typeof str !== "string")
throw new TypeError("Input not a string"); throw new TypeError("First parameter is not a string!");
const splitted = str.split("; "); const splitted = str.split("; ");
[this.name, this.value] = splitted[0].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.value.startsWith("\"") && this.value.endsWith("\""))
this.value = this.value.slice(1, -1); 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] = splitted[i].split("="); let [k, v] = splitN(splitted[i], "=", 1);
k = k.toLowerCase(); k = k.toLowerCase();
if(v) { if(v) {
if(k === "expires") { if(k === "expires") {
@ -37,28 +57,29 @@ export default class Cookie {
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(!/^(?: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") || (this.expiry = new Date(v)) === "Invalid Date")
throw new TypeError("Invalid value for Expires \"" + v + "\"!"); throw new CookieParseError("Invalid value for Expires \"" + v + "\"!");
} }
else if(k === "max-age") { else if(k === "max-age") {
const seconds = parseInt(v); const seconds = ~~+v;
if(seconds.toString() !== v) if(seconds.toString() !== v)
throw new TypeError("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(".")) if(v.startsWith("."))
v = v.substring(1); 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.domain = v;
this.subdomains = true; this.subdomains = true;
} }
else if(k === "path") { else if(k === "path")
this.path = v; this.path = v;
}
else if(k === "samesite") // 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 TypeError("Invalid key \"" + k + "\" specified!"); throw new CookieParseError("Invalid key \"" + k + "\" with value \"" + v + "\" specified!");
} }
else { else {
if(k === "secure") if(k === "secure")
@ -66,43 +87,41 @@ export default class Cookie {
else if(k === "httponly") // only relevant for browsers else if(k === "httponly") // only relevant for browsers
continue; continue;
else else
throw new TypeError("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 && 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 = urlParser.parse(url).hostname; this.domain = parsedURL.hostname;
this.subdomains = false; this.subdomains = false;
} }
if(!this.path) if(!this.path)
this.path = "/"; this.path = "/";
if(this.name.toLowerCase().startsWith("__secure-") && (!this.secure || !url.toLowerCase().startsWith("https:"))) if(!this.secure)
throw new TypeError("Cookie has \"__Secure-\" prefix but \"Secure\" isn't set or the cookie is not set via https!"); this.secure = false;
if(this.name.toLowerCase().startsWith("__host-") && (!this.secure || !url.toLowerCase().startsWith("https:") || this.domain || this.path !== "/")) if(!this.expiry)
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 \"/\"!"); this.expiry = null;
}
static fromObject(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() { serialize() {
return this.name + "=" + this.value; return this.name + "=" + this.value;
} }
hasExpired() { hasExpired(sessionEnded) {
return this.expiry && this.expiry < new Date(); return sessionEnded && this.expiry === null || this.expiry < new Date();
} }
isValidForRequest(url) { isValidForRequest(requestURL) {
if(this.hasExpired()) if(this.hasExpired(false))
return false; return false;
const parsedURL = urlParser.parse(url); const parsedURL = url.parse(requestURL);
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:") if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:"
return false; || this.secure && parsedURL.protocol !== "https:"
if(this.secure && parsedURL.protocol !== "https:") || !validateHostname(this.domain, parsedURL.hostname, this.subdomains)
return false; || !validatePath(this.path, parsedURL.pathname))
if(!validateHostname(this.domain, parsedURL.hostname, this.subdomains))
return false;
if(!validatePath(this.path, parsedURL.pathname))
return false; return false;
return true; return true;
} }

View File

@ -1,28 +1,29 @@
import fetch from "./fetch"; import fetch from "./fetch";
import CookieJar from "./cookie-jar"; import CookieJar from "./cookie-jar";
import Cookie from "./cookie";
const cookieJar = new CookieJar(); const cookieJar = new CookieJar();
export default async function cookieFetch(url, options) { export default async function cookieFetch(url, options) {
let cookies = ""; let cookies = "";
cookieJar.forEach(c => { [...cookieJar.cookiesValidForRequest(url)]
if(c.isValidForRequest(url)) .filter((v, i, a) => a.slice(0, i).every(c => c.name !== v.name)) // filter cookies with duplicate names
cookies += c.serialize() + "; "; .forEach(c => cookies += c.serialize() + "; ");
});
if(cookies.length !== 0) { if(cookies) {
if(!options) { if(!options)
options = { options = {};
headers: {} if(!options.headers)
};
}
else if(!options.headers)
options.headers = {}; options.headers = {};
options.headers.cookie = cookies.slice(0, -2); options.headers.cookie = cookies.slice(0, -2);
} }
const result = await fetch(url, options); const result = await fetch(url, options);
cookies = result.headers["set-cookie"];
if(cookies) cookies = result.headers["set-cookie"] || [];
cookies.forEach(c => cookieJar.addCookie(c, url)); cookies.forEach(c => cookieJar.addCookie(c, url));
// delete expired cookies after each request
cookieJar.deleteExpired(false);
return result; return result;
} }