Skip to content

Commit

Permalink
return promise if callback is not passed to async methods (#157)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Waterman <[email protected]>
  • Loading branch information
jstewmon and awaterma committed Jan 22, 2020
1 parent dcfdd03 commit d5d6f79
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 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 @@ -1605,6 +1606,20 @@ class CookieJar {
}
CookieJar.fromJSON = CookieJar.deserializeSync;

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

// Use a closure to provide a true imperative API for synchronous stores.
function syncWrap(method) {
return function(...args) {
Expand Down
15 changes: 14 additions & 1 deletion 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 @@ -85,7 +86,6 @@ class MemoryCookieStore extends Store {
Object.keys(domainIndex).forEach(cookiePath => {
if (pathMatch(path, cookiePath)) {
const pathIndex = domainIndex[cookiePath];

for (const key in pathIndex) {
results.push(pathIndex[key]);
}
Expand Down Expand Up @@ -174,4 +174,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.33",
"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 d5d6f79

Please sign in to comment.