Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return promise if callback not passed to async methods #157

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move to more modern syntax; won't this effect our dependents? I think if we are targeting older platforms, we may need to talk more about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Destructuring assignment is supported in node 6+.

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