Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d5d6f79

Browse files
jstewmonawaterma
andcommitted
return promise if callback is not passed to async methods (#157)
Co-authored-by: Andrew Waterman <[email protected]>
1 parent dcfdd03 commit d5d6f79

File tree

4 files changed

+104
-5
lines changed

4 files changed

+104
-5
lines changed

lib/cookie.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const Store = require("./store").Store;
3838
const MemoryCookieStore = require("./memstore").MemoryCookieStore;
3939
const pathMatch = require("./pathMatch").pathMatch;
4040
const VERSION = require("./version");
41+
const { fromCallback } = require("universalify");
4142

4243
// From RFC6265 S4.1.1
4344
// note that it excludes \x3B ";"
@@ -1605,6 +1606,20 @@ class CookieJar {
16051606
}
16061607
CookieJar.fromJSON = CookieJar.deserializeSync;
16071608

1609+
[
1610+
"_importCookies",
1611+
"clone",
1612+
"getCookies",
1613+
"getCookieString",
1614+
"getSetCookieStrings",
1615+
"removeAllCookies",
1616+
"serialize",
1617+
"setCookie"
1618+
].forEach(name => {
1619+
CookieJar.prototype[name] = fromCallback(CookieJar.prototype[name]);
1620+
});
1621+
CookieJar.deserialize = fromCallback(CookieJar.deserialize);
1622+
16081623
// Use a closure to provide a true imperative API for synchronous stores.
16091624
function syncWrap(method) {
16101625
return function(...args) {

lib/memstore.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131
"use strict";
32+
const { fromCallback } = require("universalify");
3233
const Store = require("./store").Store;
3334
const permuteDomain = require("./permuteDomain").permuteDomain;
3435
const pathMatch = require("./pathMatch").pathMatch;
@@ -85,7 +86,6 @@ class MemoryCookieStore extends Store {
8586
Object.keys(domainIndex).forEach(cookiePath => {
8687
if (pathMatch(path, cookiePath)) {
8788
const pathIndex = domainIndex[cookiePath];
88-
8989
for (const key in pathIndex) {
9090
results.push(pathIndex[key]);
9191
}
@@ -174,4 +174,17 @@ class MemoryCookieStore extends Store {
174174
}
175175
}
176176

177+
[
178+
"findCookie",
179+
"findCookies",
180+
"putCookie",
181+
"updateCookie",
182+
"removeCookie",
183+
"removeCookies",
184+
"removeAllCookies",
185+
"getAllCookies"
186+
].forEach(name => {
187+
MemoryCookieStore[name] = fromCallback(MemoryCookieStore.prototype[name]);
188+
});
189+
177190
exports.MemoryCookieStore = MemoryCookieStore;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"dependencies": {
8181
"ip-regex": "^2.1.0",
8282
"psl": "^1.1.33",
83-
"punycode": "^2.1.1"
83+
"punycode": "^2.1.1",
84+
"universalify": "^0.1.2"
8485
}
8586
}

test/api_test.js

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@
2929
* POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131
"use strict";
32-
const util = require("util");
3332
const vows = require("vows");
3433
const assert = require("assert");
3534
const async = require("async");
3635
const tough = require("../lib/cookie");
3736
const Cookie = tough.Cookie;
3837
const CookieJar = tough.CookieJar;
39-
const Store = tough.Store;
40-
const MemoryCookieStore = tough.MemoryCookieStore;
4138

4239
const atNow = Date.now();
4340

@@ -84,6 +81,79 @@ vows
8481
}
8582
}
8683
})
84+
.addBatch({
85+
"CookieJar Promises": {
86+
topic: () => new CookieJar(),
87+
setCookie: {
88+
topic(jar) {
89+
jar
90+
.setCookie("foo=bar", "http://example.com")
91+
.then(c => this.callback(null, c), this.callback);
92+
},
93+
"resolves to a Cookie"(cookie) {
94+
assert.ok(cookie instanceof Cookie);
95+
assert.strictEqual(cookie.key, "foo");
96+
assert.strictEqual(cookie.value, "bar");
97+
}
98+
},
99+
getCookies: {
100+
topic(jar) {
101+
jar
102+
.getCookies("http://example.com")
103+
.then(cookies => this.callback(null, cookies), this.callback);
104+
},
105+
"resolves to an array of cookies"(cookies) {
106+
assert.ok(Array.isArray(cookies), "not an array");
107+
assert.ok(cookies.length > 0, "array is empty");
108+
for (const cookie of cookies) {
109+
assert.ok(cookie instanceof Cookie, "not instanceof Cookie");
110+
}
111+
}
112+
},
113+
getCookieString: {
114+
topic(jar) {
115+
jar
116+
.getCookieString("http://example.com")
117+
.then(cookies => this.callback(null, cookies), this.callback);
118+
},
119+
"resolves to a string"(cookies) {
120+
assert.ok(typeof cookies === "string", "not a string");
121+
}
122+
},
123+
getSetCookieStrings: {
124+
topic(jar) {
125+
jar
126+
.getSetCookieStrings("http://example.com")
127+
.then(cookies => this.callback(null, cookies), this.callback);
128+
},
129+
"resolves to a an array of strings"(cookies) {
130+
assert.ok(Array.isArray(cookies), "not an array");
131+
assert.ok(cookies.length > 0, "array is empty");
132+
for (const cookie of cookies) {
133+
assert.ok(typeof cookie === "string", "not a string");
134+
}
135+
}
136+
},
137+
removeAllCookies: {
138+
topic(jar) {
139+
jar.removeAllCookies().then(this.callback, this.callback);
140+
},
141+
"resolves to undefined"(arg) {
142+
assert.ok(arg === undefined, "was not undefined");
143+
}
144+
},
145+
serialize: {
146+
topic(jar) {
147+
jar
148+
.serialize()
149+
.then(data => this.callback(null, data), this.callback);
150+
},
151+
"resolves to an object"(data) {
152+
assert.ok(data instanceof Object, "not an object");
153+
}
154+
}
155+
}
156+
})
87157
.addBatch({
88158
"expiry option": {
89159
topic: function() {

0 commit comments

Comments
 (0)