forked from usebruno/bruno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request usebruno#2820 from matthewdickinson/feature/cli-co…
…okies Added cookie support to CLI requests
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const { Cookie, CookieJar } = require('tough-cookie'); | ||
const each = require('lodash/each'); | ||
|
||
const cookieJar = new CookieJar(); | ||
|
||
const addCookieToJar = (setCookieHeader, requestUrl) => { | ||
const cookie = Cookie.parse(setCookieHeader, { loose: true }); | ||
cookieJar.setCookieSync(cookie, requestUrl, { | ||
ignoreError: true // silently ignore things like parse errors and invalid domains | ||
}); | ||
}; | ||
|
||
const getCookiesForUrl = (url) => { | ||
return cookieJar.getCookiesSync(url); | ||
}; | ||
|
||
const getCookieStringForUrl = (url) => { | ||
const cookies = getCookiesForUrl(url); | ||
|
||
if (!Array.isArray(cookies) || !cookies.length) { | ||
return ''; | ||
} | ||
|
||
const validCookies = cookies.filter((cookie) => !cookie.expires || cookie.expires > Date.now()); | ||
|
||
return validCookies.map((cookie) => cookie.cookieString()).join('; '); | ||
}; | ||
|
||
const getDomainsWithCookies = () => { | ||
return new Promise((resolve, reject) => { | ||
const domainCookieMap = {}; | ||
|
||
cookieJar.store.getAllCookies((err, cookies) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
cookies.forEach((cookie) => { | ||
if (!domainCookieMap[cookie.domain]) { | ||
domainCookieMap[cookie.domain] = [cookie]; | ||
} else { | ||
domainCookieMap[cookie.domain].push(cookie); | ||
} | ||
}); | ||
|
||
const domains = Object.keys(domainCookieMap); | ||
const domainsWithCookies = []; | ||
|
||
each(domains, (domain) => { | ||
const cookies = domainCookieMap[domain]; | ||
const validCookies = cookies.filter((cookie) => !cookie.expires || cookie.expires > Date.now()); | ||
|
||
if (validCookies.length) { | ||
domainsWithCookies.push({ | ||
domain, | ||
cookies: validCookies, | ||
cookieString: validCookies.map((cookie) => cookie.cookieString()).join('; ') | ||
}); | ||
} | ||
}); | ||
|
||
resolve(domainsWithCookies); | ||
}); | ||
}); | ||
}; | ||
|
||
const deleteCookiesForDomain = (domain) => { | ||
return new Promise((resolve, reject) => { | ||
cookieJar.store.removeCookies(domain, null, (err) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
return resolve(); | ||
}); | ||
}); | ||
}; | ||
|
||
const saveCookies = (url, headers) => { | ||
let setCookieHeaders = []; | ||
if (headers['set-cookie']) { | ||
setCookieHeaders = Array.isArray(headers['set-cookie']) | ||
? headers['set-cookie'] | ||
: [headers['set-cookie']]; | ||
for (let setCookieHeader of setCookieHeaders) { | ||
if (typeof setCookieHeader === 'string' && setCookieHeader.length) { | ||
addCookieToJar(setCookieHeader, url); | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
addCookieToJar, | ||
getCookiesForUrl, | ||
getCookieStringForUrl, | ||
getDomainsWithCookies, | ||
deleteCookiesForDomain, | ||
saveCookies | ||
}; |