Skip to content

Commit

Permalink
return promise if callback is not passed to async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jstewmon committed May 7, 2019
1 parent e6675db commit 58daa88
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
15 changes: 15 additions & 0 deletions lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const Store = require("./store").Store;
const MemoryCookieStore = require("./memstore").MemoryCookieStore;
const pathMatch = require("./pathMatch").pathMatch;
const VERSION = require("./version");
const { fromCallback } = require("universalify");

// From RFC6265 S4.1.1
// note that it excludes \x3B ";"
Expand Down Expand Up @@ -1433,6 +1434,20 @@ class CookieJar {
}
CookieJar.fromJSON = CookieJar.deserializeSync;

[
"_importCookies",
"clone",
"getCookies",
"getCookieString",
"getSetCookieStrings",
"removeAllCookies",
"serialize",
"setCookie"
].forEach(name => {
CookieJar.prototype[name] = fromCallback(CookieJar.prototype[name]);
});
fromCallback(CookieJar.deserialize);

// Use a closure to provide a true imperative API for synchronous stores.
function syncWrap(method) {
return function(...args) {
Expand Down
14 changes: 14 additions & 0 deletions lib/memstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
const { fromCallback } = require("universalify");
const Store = require("./store").Store;
const permuteDomain = require("./permuteDomain").permuteDomain;
const pathMatch = require("./pathMatch").pathMatch;
Expand Down Expand Up @@ -170,4 +171,17 @@ class MemoryCookieStore extends Store {
}
}

[
"findCookie",
"findCookies",
"putCookie",
"updateCookie",
"removeCookie",
"removeCookies",
"removeAllCookies",
"getAllCookies"
].forEach(name => {
MemoryCookieStore[name] = fromCallback(MemoryCookieStore.prototype[name]);
});

exports.MemoryCookieStore = MemoryCookieStore;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"dependencies": {
"ip-regex": "^2.1.0",
"psl": "^1.1.28",
"punycode": "^2.1.1"
"punycode": "^2.1.1",
"universalify": "^0.1.2"
}
}
76 changes: 73 additions & 3 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
const util = require("util");
const vows = require("vows");
const assert = require("assert");
const async = require("async");
const tough = require("../lib/cookie");
const Cookie = tough.Cookie;
const CookieJar = tough.CookieJar;
const Store = tough.Store;
const MemoryCookieStore = tough.MemoryCookieStore;

const atNow = Date.now();

Expand Down Expand Up @@ -84,6 +81,79 @@ vows
}
}
})
.addBatch({
"CookieJar Promises": {
topic: () => new CookieJar(),
setCookie: {
topic(jar) {
jar
.setCookie("foo=bar", "http://example.com")
.then(c => this.callback(null, c), this.callback);
},
"resolves to a Cookie"(cookie) {
assert.ok(cookie instanceof Cookie);
assert.strictEqual(cookie.key, "foo");
assert.strictEqual(cookie.value, "bar");
}
},
getCookies: {
topic(jar) {
jar
.getCookies("http://example.com")
.then(cookies => this.callback(null, cookies), this.callback);
},
"resolves to an array of cookies"(cookies) {
assert.ok(Array.isArray(cookies), "not an array");
assert.ok(cookies.length > 0, "array is empty");
for (const cookie of cookies) {
assert.ok(cookie instanceof Cookie, "not instanceof Cookie");
}
}
},
getCookieString: {
topic(jar) {
jar
.getCookieString("http://example.com")
.then(cookies => this.callback(null, cookies), this.callback);
},
"resolves to a string"(cookies) {
assert.ok(typeof cookies === "string", "not a string");
}
},
getSetCookieStrings: {
topic(jar) {
jar
.getSetCookieStrings("http://example.com")
.then(cookies => this.callback(null, cookies), this.callback);
},
"resolves to a an array of strings"(cookies) {
assert.ok(Array.isArray(cookies), "not an array");
assert.ok(cookies.length > 0, "array is empty");
for (const cookie of cookies) {
assert.ok(typeof cookie === "string", "not a string");
}
}
},
removeAllCookies: {
topic(jar) {
jar.removeAllCookies().then(this.callback, this.callback);
},
"resolves to undefined"(arg) {
assert.ok(arg === undefined, "was not undefined");
}
},
serialize: {
topic(jar) {
jar
.serialize()
.then(data => this.callback(null, data), this.callback);
},
"resolves to an object"(data) {
assert.ok(data instanceof Object, "not an object");
}
}
}
})
.addBatch({
"expiry option": {
topic: function() {
Expand Down

0 comments on commit 58daa88

Please sign in to comment.