const tough = require('tough-cookie');
const { MemoryCookieStore } = require('tough-cookie/lib/memstore');
const fs = require('fs');

function iterateProperties(obj, cb) {
    for(let elem in obj) {
        if(obj.hasOwnProperty(elem)) {
            cb(elem, obj[elem])
        }
    }
}

class FileStore extends MemoryCookieStore {
    constructor(file) {
        super();
        this.file = file;
        this.load();
    }

    createSaveCallback(callback) {
        return (err) => {
            if(err) {
                callback(err);
            } else {
                this.save(callback);
            }
        };
    }

    putCookie(cookie, callback) {
        super.putCookie(cookie, this.createSaveCallback(callback));
    }

    removeCookie(domain, path, key, callback) {
        super.removeCookie(domain, path, key, this.createSaveCallback(callback));
    }

    removeCookies(domain, path, callback) {
        super.removeCookies(domain, path, this.createSaveCallback(callback));
    }

    load() {
        const data = fs.readFileSync(this.file, 'utf8');
        if (data.length >= 2) {
            if (data[0] == '{' && data[data.length - 1] == '}') {
                let json = data && data.length > 0 ? JSON.parse(data) : {};
                iterateProperties(json, (domain, paths) => {
                    iterateProperties(paths, (path, cookies) => {
                        iterateProperties(cookies, (cookieName, cookie) => {
                            json[domain][path][cookieName] = tough.fromJSON(JSON.stringify(cookie));
                        });
                    });
                });

                this.idx = json;
            } else {
                LOG.warn("Encountered old cookies. Ignoring.");
                this.idx = {};
                this.save(() => {
                });
            }
        } else {
            this.idx = {};
        }
    }

    save(callback) {
        const data = JSON.stringify(this.idx);
        fs.writeFile(this.file, data, (err) => {
            if(err) {
                throw err;
            } else {
                callback();
            }
        });
    }
}

module.exports = FileStore;
@@ -1128,16 +1128,17 @@
 ta%5B0%5D ==
+=
  '%7B' && 
@@ -1165,16 +1165,17 @@
 %5D ==
+=
  '%7D') %7B%0A
     
@@ -1166,24 +1166,50 @@
  === '%7D') %7B%0A
+                try %7B%0A    
             
@@ -1280,32 +1280,36 @@
                 
+    
 iteratePropertie
@@ -1349,32 +1349,36 @@
                 
+   
+ 
 iteratePropertie
@@ -1399,32 +1399,36 @@
 , cookies) =%3E %7B%0A
+    
                 
@@ -1516,16 +1516,20 @@
         
+  
+  
 json%5Bdom
@@ -1609,36 +1609,44 @@
                 
-%7D);%0A
+    %7D);%0A    
                 
@@ -1645,32 +1645,36 @@
             %7D);%0A
+    
                 
@@ -1686,32 +1686,36 @@
                 
+    
 this.idx = json;
@@ -1715,16 +1715,177 @@
 = json;%0A
+                %7D catch (exception) %7B%0A                    LOG.error(%22Error reading cookies: %22 + exception);%0A                    this.idx = %7B%7D;%0A                %7D%0A
         

Make loading cookies a little more robust against possible corruption