-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.js
61 lines (52 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const { CookieJar, Cookie } = require('tough-cookie');
/**
* Get a list of cookie objects from a request.jar()
*
* @param jar
*/
module.exports.cookiesFromJar = function(jar) {
return new Promise(resolve => {
jar.store.getAllCookies((err, cookies) => {
if (err) {
console.warn('Failed to get cookies form jar', err);
resolve([]);
} else {
// NOTE: Perform toJSON so we have a plain JS object instead of Cookie instance
resolve(cookies.map(c => c.toJSON()));
}
});
});
};
/**
* Get a request.jar() from a list of cookie objects
*
* @param cookies
*/
module.exports.jarFromCookies = function(cookies) {
let jar;
try {
// For some reason, fromJSON modifies `cookies`. Create a copy first
// just to be sure
const copy = JSON.stringify({ cookies });
jar = CookieJar.fromJSON(copy);
} catch (e) {
console.log('[cookies] Failed to initialize cookie jar', e);
jar = new CookieJar();
}
jar.rejectPublicSuffixes = false;
jar.looseMode = true;
return jar;
};
module.exports.cookieToString = function(cookie) {
// Cookie can either be a plain JS object or Cookie instance
if (!(cookie instanceof Cookie)) {
cookie = Cookie.fromJSON(cookie);
}
let str = cookie.toString();
// tough-cookie toString() doesn't put domain on all the time.
// This hack adds when tough-cookie won't
if (cookie.domain && cookie.hostOnly) {
str += '; Domain=' + cookie.domain;
}
return str;
};