3 /** @type CookieStore */
4 let Cookies = module.exports;
6 let Cookie = require("tough-cookie");
8 //let FileCookieStore = require("@root/file-cookie-store");
9 //let cookies_store = new FileCookieStore("./cookie.txt", { auto_sync: false });
10 let jar = new Cookie.CookieJar(/*cookies_store*/);
11 jar.setCookieAsync = require("util").promisify(jar.setCookie);
12 jar.getCookiesAsync = require("util").promisify(jar.getCookies);
13 //cookies_store.saveAsync = require("util").promisify(cookies_store.save);
17 * @param {import('http').IncomingMessage} resp
18 * @returns {Promise<void>}
20 Cookies.set = async function _setCookie(url, resp) {
22 if (resp.headers["set-cookie"]) {
23 if (Array.isArray(resp.headers["set-cookie"])) {
24 cookies = resp.headers["set-cookie"].map(Cookie.parse);
26 cookies = [Cookie.parse(resp.headers["set-cookie"])];
30 // let Cookie = //require('set-cookie-parser');
31 // Cookie.parse(resp, { decodeValues: true });
33 cookies.map(async function (cookie) {
34 //console.log('DEBUG cookie:', cookie.toJSON());
35 await jar.setCookieAsync(cookie, url, { now: new Date() });
38 //await cookies_store.saveAsync();
43 * @returns {Promise<String>}
45 Cookies.get = async function _getCookie(url) {
46 return (await jar.getCookiesAsync(url)).toString();