Enhancement: forward cookies from request (#1804)

This commit is contained in:
James Waters 2023-09-09 00:50:32 +01:00 committed by GitHub
parent 63f952509e
commit d4edd432d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View file

@ -37,3 +37,27 @@ export function addCookieToJar(url, headers) {
cookieJar.setCookieSync(cookies[i], url.toString(), { ignoreError: true });
}
}
export function importCookieHeader(url, cookieHeader) {
const cookies = cookieHeader.split(';');
for (let i = 0; i < cookies.length; i += 1) {
const [key, value] = cookies[i].trim().split('=');
// If there's an existing cookie with a matching key for this url,
// we want to update it. Otherwise, we add a new cookie
let existingCookie;
try {
existingCookie = cookieJar.getCookiesSync(url).find(existing => existing.key === key);
} catch (e) {
console.debug(`Failed to get cookies for ${url}: ${e}`)
}
if (existingCookie) {
existingCookie.value = value;
} else {
cookieJar.setCookieSync(new Cookie({
key, value
}), url.toString(), { ignoreError: true });
}
}
}