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)
|
||||
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) {
|
||||
|
|
|
@ -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:")
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user