remove unnecessary code from cookie jar class
This commit is contained in:
parent
5b4105322d
commit
d7087a0322
|
@ -1,27 +1,8 @@
|
||||||
import fs from "fs";
|
|
||||||
import Cookie from "./cookie";
|
import Cookie from "./cookie";
|
||||||
|
|
||||||
export default class CookieJar {
|
export default class CookieJar {
|
||||||
constructor(flags, file, cookies) {
|
constructor() {
|
||||||
this.cookies = new Map();
|
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) {
|
addCookie(c, fromURL) {
|
||||||
if(typeof c === "string")
|
if(typeof c === "string")
|
||||||
|
@ -31,15 +12,4 @@ export default class CookieJar {
|
||||||
forEach(callback) {
|
forEach(callback) {
|
||||||
this.cookies.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]));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user