var util = require('util'),

    Store = require('tough-cookie').Store,
    Cookie = require('tough-cookie').Cookie,

    EXECUTION_EVENT_BASE = 'execution.cookies.',
    EVENT_STORE_ACTION = 'store',
    STORE_METHODS = [
        'findCookie', 'findCookies', 'putCookie', 'updateCookie',
        'removeCookie', 'removeCookies', 'removeAllCookies', 'getAllCookies'
    ],
    FUNCTION = 'function',

    arrayProtoSlice = Array.prototype.slice,
    PostmanCookieStore;

PostmanCookieStore = function (id, emitter, timers) {
    Store.call(this);

    this.id = id; // execution identifier
    this.emitter = emitter;
    this.timers = timers;
};

util.inherits(PostmanCookieStore, Store);

// Disable CookieJar's *Sync APIs
PostmanCookieStore.prototype.synchronous = false;

STORE_METHODS.forEach(function (method) {
    PostmanCookieStore.prototype[method] = function () {
        var args = arrayProtoSlice.call(arguments),
            cb = args.pop(),
            callback;

        callback = function (err, cookies) {
            if (typeof cb !== FUNCTION) {
                return;
            }

            if (err) {
                return cb(err);
            }

            if (Array.isArray(cookies)) {
                return cb(err, cookies.map(function (cookie) {
                    return Cookie.fromJSON(cookie);
                }));
            }

            cb(err, cookies && Cookie.fromJSON(cookies));
        };

        this.emitter.dispatch(
            EXECUTION_EVENT_BASE + this.id,
            this.timers.setEvent(callback),
            EVENT_STORE_ACTION,
            method,
            args
        );
    };
});

module.exports = PostmanCookieStore;

@@ -1,12 +1,39 @@
 var 
+_ = require('lodash'),%0A    
 util = r
@@ -796,16 +796,64 @@
 false;%0A%0A
+// attach a common handler to all store methods%0A
 STORE_ME
@@ -955,16 +955,212 @@
     var 
+eventName = EXECUTION_EVENT_BASE + this.id,%0A            args,%0A            eventId,%0A            callback,%0A            requiredArgs;%0A%0A        // fetch all the arguments passed to the method%0A        
 args = a
@@ -1189,17 +1189,18 @@
 guments)
-,
+;%0A
 %0A       
@@ -1204,28 +1204,341 @@
     
-    cb = args.pop(),
+// get the number of parameters expected by the method%0A        // eslint-disable-next-line lodash/path-style%0A        requiredArgs = _.get(Store, %5B'prototype', method, 'length'%5D, 0);%0A%0A        // adjust arguments length based on Store's prototype method%0A        if (args.length %3C requiredArgs) %7B%0A            args.length = requiredArgs;
 %0A   
@@ -1546,42 +1546,221 @@
     
+%7D%0A%0A
     
-callback;%0A%0A        callback = 
+    // move callback out of arguments%0A        // this will be called when timer clears the event%0A        callback = args.pop();%0A%0A        // set event for the callback%0A        eventId = this.timers.setEvent(
 func
@@ -1805,17 +1805,23 @@
 typeof c
-b
+allback
  !== FUN
@@ -1849,50 +1849,251 @@
     
-return;%0A            %7D%0A%0A            if (err
+throw new TypeError('callback is not a function');%0A            %7D%0A%0A            // methods: putCookie, updateCookie, removeCookie, removeCookies,%0A            //          removeAllCookies%0A            // or, onError%0A            if (err %7C%7C !cookies
 ) %7B%0A
@@ -2112,25 +2112,31 @@
     return c
-b
+allback
 (err);%0A     
@@ -2137,32 +2137,83 @@
 %0A            %7D%0A%0A
+            // methods: findCookies, getAllCookies%0A
             if (
@@ -2262,17 +2262,23 @@
 return c
-b
+allback
 (err, co
@@ -2358,16 +2358,43 @@
 cookie);
+ // serialize cookie object
 %0A       
@@ -2438,26 +2438,55 @@
     
-cb(err, cookies &&
+// method: findCookie%0A            callback(err,
  Coo
@@ -2518,16 +2518,17 @@
        %7D
+)
 ;%0A%0A     
@@ -2534,84 +2534,59 @@
     
-this.emitter.dispatch(%0A            EXECUTION_EVENT_BASE + this.id,%0A 
+// finally, dispatch event over the bridge%0A
         
    t
@@ -2585,120 +2585,82 @@
     
-   
 this.
-timers.setEvent(callback),%0A            EVENT_STORE_ACTION,%0A            method,%0A            args%0A        
+emitter.dispatch(eventName, eventId, EVENT_STORE_ACTION, method, args
 );%0A 

Fix store method call on invalid arguments