remove unnecessary code from cookie jar class

This commit is contained in:
jkhsjdhjs 2019-07-24 16:45:04 +02:00
parent 5b4105322d
commit d7087a0322
Signed by: jkhsjdhjs
GPG Key ID: BAC6ADBAB7D576CC

View File

@ -1,27 +1,8 @@
import fs from "fs";
import Cookie from "./cookie";
export default class CookieJar {
constructor(flags, file, cookies) {
constructor() {
this.cookies = new Map();
this.file = file;
this.flags = flags;
if(typeof this.flags !== "string")
throw new TypeError("First parameter is not a string!");
if(this.file && typeof this.file !== "string")
throw new TypeError("Second parameter is not a string!");
if(Array.isArray(cookies)) {
if(!cookies.every(c => c instanceof Cookie))
throw new TypeError("Third parameter is not an array of cookies!");
else
cookies.forEach(cookie => this.cookies.set(cookie.name, cookie));
}
else if(cookies instanceof Cookie)
this.cookies.set(cookies.name, cookies);
else if(cookies)
throw new TypeError("Third parameter is neither an array nor a cookie!");
if(this.file && this.cookies.size === 0 && this.file.length !== 0 && fs.existsSync(this.file))
this.cookies = new Map(JSON.parse(fs.readFileSync(this.file)).map(([k, v]) => [k, Cookie.fromObject(v)]));
}
addCookie(c, fromURL) {
if(typeof c === "string")
@ -31,15 +12,4 @@ export default class CookieJar {
forEach(callback) {
this.cookies.forEach(callback);
}
save() {
if(typeof this.file !== "string")
throw new Error("No file has been specified for this cookie jar!");
// only save cookies that haven't expired
let cookiesToSave = new Map();
this.forEach(cookie => {
if(!cookie.hasExpired())
cookiesToSave.set(cookie.name, cookie);
});
fs.writeFileSync(this.file, JSON.stringify([...cookiesToSave]));
}
};