Skip to content
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
64 changes: 58 additions & 6 deletions lib/cookies.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Cookie } from 'tough-cookie';

export function formatCookie(cookie) {
let expiresISO;

Expand All @@ -23,12 +21,66 @@ export function formatCookie(cookie) {
}

function parseCookie(cookieString) {
let cookie = Cookie.parse(cookieString);
if (!cookie) {
return;
const parts = cookieString.split(';');
const firstPart = parts.shift();
if (firstPart === undefined) return;

const eqIndex = firstPart.indexOf('=');
if (eqIndex === -1) return;

const name = firstPart.slice(0, eqIndex).trim();
if (!name) return;

const cookie = {
name,
value: firstPart.slice(eqIndex + 1).trim(),
path: undefined,
domain: undefined,
expires: undefined,
httpOnly: false,
secure: false
};

for (const part of parts) {
const trimmed = part.trim();
if (!trimmed) continue;
const idx = trimmed.indexOf('=');
const attrName = (
idx === -1 ? trimmed : trimmed.slice(0, idx)
).toLowerCase();
const attrValue = idx === -1 ? '' : trimmed.slice(idx + 1).trim();

switch (attrName) {
case 'path': {
cookie.path = attrValue || undefined;
break;
}
case 'domain': {
// tough-cookie strips a leading '.'; preserve that behavior
cookie.domain = attrValue.startsWith('.')
? attrValue.slice(1) || undefined
: attrValue || undefined;
break;
}
case 'expires': {
const date = new Date(attrValue);
if (!Number.isNaN(date.getTime())) {
cookie.expires = date.toISOString();
}
break;
}
case 'httponly': {
cookie.httpOnly = true;
break;
}
case 'secure': {
cookie.secure = true;
break;
}
}
}

return formatCookie(cookie);
return cookie;
}

function splitAndParse(header, divider) {
Expand Down
Loading