improve session cookie handling

This commit is contained in:
2019-08-15 01:06:22 +02:00
parent 9f498864bb
commit 7e7a19403c
3 changed files with 15 additions and 15 deletions

View File

@ -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)]));
}
};

View File

@ -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:"