mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-14 08:36:22 +08:00
cc6fdef10e
close AF-2177
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import type { CookiesSetDetails } from 'electron';
|
|
|
|
export function parseCookie(
|
|
cookieString: string,
|
|
url: string
|
|
): CookiesSetDetails {
|
|
const [nameValuePair, ...attributes] = cookieString
|
|
.split('; ')
|
|
.map(part => part.trim());
|
|
|
|
const [name, value] = nameValuePair.split('=');
|
|
|
|
const details: CookiesSetDetails = { url, name, value };
|
|
|
|
attributes.forEach(attribute => {
|
|
const [key, val] = attribute.split('=');
|
|
|
|
switch (key.toLowerCase()) {
|
|
case 'domain':
|
|
details.domain = val;
|
|
break;
|
|
case 'path':
|
|
details.path = val;
|
|
break;
|
|
case 'secure':
|
|
details.secure = true;
|
|
break;
|
|
case 'httponly':
|
|
details.httpOnly = true;
|
|
break;
|
|
case 'expires':
|
|
details.expirationDate = new Date(val).getTime() / 1000; // Convert to seconds
|
|
break;
|
|
case 'samesite':
|
|
if (
|
|
['unspecified', 'no_restriction', 'lax', 'strict'].includes(
|
|
val.toLowerCase()
|
|
)
|
|
) {
|
|
details.sameSite = val.toLowerCase() as
|
|
| 'unspecified'
|
|
| 'no_restriction'
|
|
| 'lax'
|
|
| 'strict';
|
|
}
|
|
break;
|
|
default:
|
|
// Handle other cookie attributes if needed
|
|
break;
|
|
}
|
|
});
|
|
|
|
return details;
|
|
}
|