fixes to get it working
This commit is contained in:
parent
d92f8d3564
commit
f6c3ba8a61
|
@ -18,14 +18,14 @@ export default class CookieJar {
|
||||||
}
|
}
|
||||||
else if(cookies instanceof Cookie)
|
else if(cookies instanceof Cookie)
|
||||||
this.cookies.set(cookies.name, cookies);
|
this.cookies.set(cookies.name, cookies);
|
||||||
else
|
else if(cookies)
|
||||||
throw new TypeError("Third parameter is neither an array nor a cookie!");
|
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))
|
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")
|
if(typeof c === "string")
|
||||||
c = new Cookie(c);
|
c = new Cookie(c, fromURL);
|
||||||
this.cookies.set(c.name, c);
|
this.cookies.set(c.name, c);
|
||||||
}
|
}
|
||||||
forEach(callback) {
|
forEach(callback) {
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
import urlParser from "url";
|
import urlParser from "url";
|
||||||
|
|
||||||
const validateHostname = (cookieHostname, requestHostname, subdomains) => {
|
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 true;
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
const validatePath = (cookiePath, requestPath) => {
|
const validatePath = (cookiePath, requestPath) => {
|
||||||
|
cookiePath = cookiePath.toLowerCase();
|
||||||
|
requestPath = requestPath.toLowerCase();
|
||||||
if(cookiePath.endsWith("/"))
|
if(cookiePath.endsWith("/"))
|
||||||
cookiePath = cookiePath.slice(0, -1);
|
cookiePath = cookiePath.slice(0, -1);
|
||||||
if(requestPath.endsWith("/"))
|
if(requestPath.endsWith("/"))
|
||||||
|
@ -32,14 +36,14 @@ export default class Cookie {
|
||||||
if(k === "expires") {
|
if(k === "expires") {
|
||||||
if(this.expiry) // max-age has precedence over expires
|
if(this.expiry) // max-age has precedence over expires
|
||||||
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{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 TypeError("Invalid value for Expires \"" + v + "\"!");
|
||||||
}
|
}
|
||||||
else if(k === "max-age") {
|
else if(k === "max-age") {
|
||||||
const seconds = parseInt(v);
|
const seconds = parseInt(v);
|
||||||
if(seconds.toString() !== 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 = new Date();
|
||||||
this.expiry.setSeconds(this.expiry.getSeconds() + seconds);
|
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 \"/\"!");
|
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) {
|
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() {
|
serialize() {
|
||||||
return this.name + "=" + this.value;
|
return this.name + "=" + this.value;
|
||||||
}
|
}
|
||||||
isValidForRequest(url) {
|
isValidForRequest(url) {
|
||||||
if(this.expiry && this.expiry > new Date())
|
if(this.expiry && this.expiry < new Date())
|
||||||
return false;
|
return false;
|
||||||
const parsedURL = urlParser.parse(url);
|
const parsedURL = urlParser.parse(url);
|
||||||
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:")
|
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:")
|
||||||
|
|
|
@ -9,16 +9,30 @@ export default {
|
||||||
cookieJars.forEach(jar => {
|
cookieJars.forEach(jar => {
|
||||||
if(!jar.flags.includes("r"))
|
if(!jar.flags.includes("r"))
|
||||||
return;
|
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")) {
|
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
|
else
|
||||||
throw new TypeError("Third paramter is neither a cookie jar nor an array of cookie jars!");
|
throw new TypeError("Third paramter is neither a cookie jar nor an array of cookie jars!");
|
||||||
if(cookies.length !== 0)
|
if(cookies.length !== 0) {
|
||||||
options.headers["Cookie"] = cookies.slice(0, -2);
|
if(!options) {
|
||||||
|
options = {
|
||||||
|
headers: {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if(!options.headers)
|
||||||
|
options.headers = {};
|
||||||
|
options.headers.cookie = cookies.slice(0, -2);
|
||||||
|
}
|
||||||
const result = await fetch(url, options);
|
const result = await fetch(url, options);
|
||||||
// i cannot use headers.get() here because it joins the cookies to a string
|
// i cannot use headers.get() here because it joins the cookies to a string
|
||||||
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
|
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
|
||||||
|
@ -27,13 +41,14 @@ export default {
|
||||||
cookieJars.forEach(jar => {
|
cookieJars.forEach(jar => {
|
||||||
if(!jar.flags.includes("w"))
|
if(!jar.flags.includes("w"))
|
||||||
return;
|
return;
|
||||||
cookies.forEach(c => jar.addCookie(c));
|
cookies.forEach(c => jar.addCookie(c, url));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if(cookieJars.flags.includes("w")) {
|
else if(cookieJars.flags.includes("w")) {
|
||||||
cookies.forEach(c => cookieJars.addCookie(c));
|
cookies.forEach(c => cookieJars.addCookie(c, url));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
},
|
},
|
||||||
CookieJar: CookieJar,
|
CookieJar: CookieJar,
|
||||||
Cookie: Cookie
|
Cookie: Cookie
|
||||||
|
|
Loading…
Reference in New Issue
Block a user