UNPKG

ayakashi

Version:

The next generation web scraping framework

219 lines (218 loc) 6.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DbCookieStore = void 0; const tough_cookie_1 = require("tough-cookie"); const sequelize_1 = require("sequelize"); const async_1 = require("async"); const dayjs_1 = __importDefault(require("dayjs")); class DbCookieStore extends tough_cookie_1.Store { constructor(sessionDb, CookieModel) { super(); this.sessionDb = sessionDb; this.CookieModel = CookieModel; } findCookie(domain, path, key, cb) { this.CookieModel.findOne({ where: { [sequelize_1.Op.and]: [{ domain: domain }, { path: path }, { key: key }] } }) .then(function (cookieInstance) { if (cookieInstance) { cb(null, cookieInstance.get({ plain: true })); } else { cb(null, null); } }) .catch(function (err) { cb(err, null); }); } findCookies(domain, path, cb) { let where; if (path === null) { where = { domain: domain }; } else { where = { [sequelize_1.Op.and]: [{ domain: domain }, { path: path }] }; } this.CookieModel.findAll({ where: where }) .then(function (cookieInstances) { if (cookieInstances && cookieInstances.length > 0) { cb(null, cookieInstances.map(c => c.get({ plain: true }))); } else { cb(null, []); } }) .catch(function (err) { cb(err, []); }); } putCookie(cookie, callback) { async_1.retry({ times: 10, interval: 100 }, (cb) => { this.CookieModel.findOrCreate({ where: { [sequelize_1.Op.and]: [{ domain: cookie.domain }, { path: cookie.path }, { key: cookie.key }] }, defaults: { key: cookie.key, value: cookie.value, expires: dayjs_1.default(cookie.expires).isValid() ? cookie.expires : null, maxAge: cookie.maxAge || null, domain: cookie.domain, path: cookie.path, secure: cookie.secure, httpOnly: cookie.httpOnly, hostOnly: cookie.hostOnly, creation: cookie.creation, lastAccessed: cookie.lastAccessed } }) .then(([cookieInstance, created]) => { if (created) { return cb(null); } this.CookieModel.update({ key: cookie.key, value: cookie.value, expires: dayjs_1.default(cookie.expires).isValid() ? cookie.expires : null, maxAge: cookie.maxAge || null, domain: cookie.domain, path: cookie.path, secure: cookie.secure, httpOnly: cookie.httpOnly, hostOnly: cookie.hostOnly, creation: cookie.creation, lastAccessed: cookie.lastAccessed }, { where: { id: cookieInstance.id } }) .then(function () { cb(null); }) .catch(function (err) { cb(err); }); }) .catch(function (err) { cb(err); }); }, function (err) { callback(err || null); }); } updateCookie(oldCookie, newCookie, cb) { this.CookieModel.update({ value: newCookie.value, lastAccessed: newCookie.lastAccessed }, { where: { [sequelize_1.Op.and]: [{ domain: oldCookie.domain }, { path: oldCookie.path }, { key: oldCookie.key }] } }) .then(function () { cb(null); }) .catch(function (err) { cb(err); }); } removeCookie(domain, path, key, cb) { this.CookieModel.destroy({ where: { [sequelize_1.Op.and]: [{ domain: domain }, { path: path }, { key: key }] } }) .then(function () { cb(null); }) .catch(function (err) { cb(err); }); } removeCookies(domain, path, cb) { this.CookieModel.destroy({ where: { [sequelize_1.Op.and]: [{ domain: domain }, { path: path }] } }) .then(function () { cb(null); }) .catch(function (err) { cb(err); }); } removeAllCookies(cb) { this.CookieModel.destroy() .then(function () { cb(null); }) .catch(function (err) { cb(err); }); } getAllCookies(cb) { this.CookieModel.findAll() .then(function (cookieInstances) { if (cookieInstances && cookieInstances.length > 0) { cb(null, cookieInstances.map(c => c.get({ plain: true }))); } else { cb(null, []); } }) .catch(function (err) { cb(err, []); }); } } exports.DbCookieStore = DbCookieStore;