@mojojs/core
Version:
Real-time web framework
36 lines • 1.02 kB
JavaScript
import { format } from 'node:url';
import tough from 'tough-cookie';
/**
* Cookie jar class.
*/
export class CookieJar {
constructor() {
/**
* Cookie storage.
*/
this.storage = new tough.CookieJar();
}
/**
* Load cookies from storage.
*/
async loadCookies(url) {
const cookies = await this.storage.getCookies(this._cookieURL(url));
if (cookies.length > 0)
return cookies.map(cookie => cookie.cookieString()).join('; ');
return null;
}
/**
* Store cookies.
*/
async storeCookies(url, headers) {
const cookieURL = this._cookieURL(url);
for (const cookie of headers.map(value => tough.Cookie.parse(value))) {
if (cookie !== undefined)
await this.storage.setCookie(cookie, cookieURL);
}
}
_cookieURL(currentURL) {
return format(currentURL, { auth: false, fragment: false, search: false });
}
}
//# sourceMappingURL=cookie-jar.js.map