improve session cookie handling
This commit is contained in:
@ -45,9 +45,9 @@ export default class CookieJar {
|
||||
for(const cookie of (this.cookies.get(domain) || []).values())
|
||||
yield cookie;
|
||||
}
|
||||
*cookiesValid() {
|
||||
*cookiesValid(withSession) {
|
||||
for(const cookie of this.cookiesAll())
|
||||
if(!cookie.hasExpired())
|
||||
if(!cookie.hasExpired(!withSession))
|
||||
yield cookie;
|
||||
}
|
||||
*cookiesAll() {
|
||||
@ -73,7 +73,7 @@ export default class CookieJar {
|
||||
}
|
||||
}
|
||||
deleteExpired() {
|
||||
const validCookies = [...this.cookiesValid()];
|
||||
const validCookies = [...this.cookiesValid(false)];
|
||||
this.cookies = new Map();
|
||||
validCookies.forEach(c => this.addCookie(c));
|
||||
}
|
||||
@ -81,6 +81,6 @@ export default class CookieJar {
|
||||
if(typeof this.file !== "string")
|
||||
throw new Error("No file has been specified for this cookie jar!");
|
||||
// only save cookies that haven't expired
|
||||
fs.writeFileSync(this.file, JSON.stringify([...this.cookiesValid()]));
|
||||
fs.writeFileSync(this.file, JSON.stringify([...this.cookiesValid(false)]));
|
||||
}
|
||||
};
|
||||
|
@ -109,11 +109,11 @@ export default class Cookie {
|
||||
serialize() {
|
||||
return this.name + "=" + this.value;
|
||||
}
|
||||
hasExpired() {
|
||||
return this.expiry && this.expiry < new Date();
|
||||
hasExpired(sessionEnded) {
|
||||
return sessionEnded && this.expiry === null || this.expiry < new Date();
|
||||
}
|
||||
isValidForRequest(requestURL) {
|
||||
if(this.hasExpired())
|
||||
if(this.hasExpired(false))
|
||||
return false;
|
||||
const parsedURL = url.parse(requestURL);
|
||||
if(parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:"
|
||||
|
Reference in New Issue
Block a user