make specifying a file for cookie-jars optional
This commit is contained in:
		@@ -33,12 +33,12 @@ A class that stores cookies.
 | 
			
		||||
- `file` The path of the cookie jar on the disk.
 | 
			
		||||
- `cookies` A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) mapping cookie names to their properties.
 | 
			
		||||
 | 
			
		||||
#### new CookieJar(flags, file[, cookies])
 | 
			
		||||
#### new CookieJar(flags[, file, cookies])
 | 
			
		||||
- `flags` A string specifying whether cookies should be read and/or written from/to the jar when passing it as parameter to [fetch](#fetchcookiejar-url-options).
 | 
			
		||||
    - `r`: only read from this jar
 | 
			
		||||
    - `w`: only write to this jar
 | 
			
		||||
    - `rw` or `wr`: read/write from/to this jar
 | 
			
		||||
- `file` A string containing a relative or absolute path to the file on the disk to use.
 | 
			
		||||
- `file` An optional string containing a relative or absolute path to the file on the disk to use.
 | 
			
		||||
- `cookies` An optional initializer for the cookie jar - either an array of [Cookie](#class-cookie) instances or a single Cookie instance.
 | 
			
		||||
 | 
			
		||||
#### addCookie(cookie[, url])
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ export default class CookieJar {
 | 
			
		||||
        this.flags = flags;
 | 
			
		||||
        if(typeof this.flags !== "string")
 | 
			
		||||
            throw new TypeError("First parameter is not a string!");
 | 
			
		||||
        if(typeof this.file !== "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))
 | 
			
		||||
@@ -20,7 +20,7 @@ export default class CookieJar {
 | 
			
		||||
            this.cookies.set(cookies.name, cookies);
 | 
			
		||||
        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))
 | 
			
		||||
        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) {
 | 
			
		||||
@@ -32,6 +32,8 @@ export default class CookieJar {
 | 
			
		||||
        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 => {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user