|
"use strict"; |
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
|
if (k2 === undefined) k2 = k; |
|
var desc = Object.getOwnPropertyDescriptor(m, k); |
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
|
desc = { enumerable: true, get: function() { return m[k]; } }; |
|
} |
|
Object.defineProperty(o, k2, desc); |
|
}) : (function(o, m, k, k2) { |
|
if (k2 === undefined) k2 = k; |
|
o[k2] = m[k]; |
|
})); |
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
|
Object.defineProperty(o, "default", { enumerable: true, value: v }); |
|
}) : function(o, v) { |
|
o["default"] = v; |
|
}); |
|
var __importStar = (this && this.__importStar) || function (mod) { |
|
if (mod && mod.__esModule) return mod; |
|
var result = {}; |
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
|
__setModuleDefault(result, mod); |
|
return result; |
|
}; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.CookieJar = void 0; |
|
const getPublicSuffix_1 = require("../getPublicSuffix"); |
|
const validators = __importStar(require("../validators")); |
|
const validators_1 = require("../validators"); |
|
const store_1 = require("../store"); |
|
const memstore_1 = require("../memstore"); |
|
const pathMatch_1 = require("../pathMatch"); |
|
const cookie_1 = require("./cookie"); |
|
const utils_1 = require("../utils"); |
|
const canonicalDomain_1 = require("./canonicalDomain"); |
|
const constants_1 = require("./constants"); |
|
const defaultPath_1 = require("./defaultPath"); |
|
const domainMatch_1 = require("./domainMatch"); |
|
const cookieCompare_1 = require("./cookieCompare"); |
|
const version_1 = require("../version"); |
|
const defaultSetCookieOptions = { |
|
loose: false, |
|
sameSiteContext: undefined, |
|
ignoreError: false, |
|
http: true, |
|
}; |
|
const defaultGetCookieOptions = { |
|
http: true, |
|
expire: true, |
|
allPaths: false, |
|
sameSiteContext: undefined, |
|
sort: undefined, |
|
}; |
|
const SAME_SITE_CONTEXT_VAL_ERR = 'Invalid sameSiteContext option for getCookies(); expected one of "strict", "lax", or "none"'; |
|
function getCookieContext(url) { |
|
if (url && |
|
typeof url === 'object' && |
|
'hostname' in url && |
|
typeof url.hostname === 'string' && |
|
'pathname' in url && |
|
typeof url.pathname === 'string' && |
|
'protocol' in url && |
|
typeof url.protocol === 'string') { |
|
return { |
|
hostname: url.hostname, |
|
pathname: url.pathname, |
|
protocol: url.protocol, |
|
}; |
|
} |
|
else if (typeof url === 'string') { |
|
try { |
|
return new URL(decodeURI(url)); |
|
} |
|
catch { |
|
return new URL(url); |
|
} |
|
} |
|
else { |
|
throw new validators_1.ParameterError('`url` argument is not a string or URL.'); |
|
} |
|
} |
|
function checkSameSiteContext(value) { |
|
const context = String(value).toLowerCase(); |
|
if (context === 'none' || context === 'lax' || context === 'strict') { |
|
return context; |
|
} |
|
else { |
|
return undefined; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isSecurePrefixConditionMet(cookie) { |
|
const startsWithSecurePrefix = typeof cookie.key === 'string' && cookie.key.startsWith('__Secure-'); |
|
return !startsWithSecurePrefix || cookie.secure; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isHostPrefixConditionMet(cookie) { |
|
const startsWithHostPrefix = typeof cookie.key === 'string' && cookie.key.startsWith('__Host-'); |
|
return (!startsWithHostPrefix || |
|
Boolean(cookie.secure && |
|
cookie.hostOnly && |
|
cookie.path != null && |
|
cookie.path === '/')); |
|
} |
|
function getNormalizedPrefixSecurity(prefixSecurity) { |
|
const normalizedPrefixSecurity = prefixSecurity.toLowerCase(); |
|
|
|
switch (normalizedPrefixSecurity) { |
|
case constants_1.PrefixSecurityEnum.STRICT: |
|
case constants_1.PrefixSecurityEnum.SILENT: |
|
case constants_1.PrefixSecurityEnum.DISABLED: |
|
return normalizedPrefixSecurity; |
|
default: |
|
return constants_1.PrefixSecurityEnum.SILENT; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CookieJar { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(store, options) { |
|
if (typeof options === 'boolean') { |
|
options = { rejectPublicSuffixes: options }; |
|
} |
|
this.rejectPublicSuffixes = options?.rejectPublicSuffixes ?? true; |
|
this.enableLooseMode = options?.looseMode ?? false; |
|
this.allowSpecialUseDomain = options?.allowSpecialUseDomain ?? true; |
|
this.prefixSecurity = getNormalizedPrefixSecurity(options?.prefixSecurity ?? 'silent'); |
|
this.store = store ?? new memstore_1.MemoryCookieStore(); |
|
} |
|
callSync(fn) { |
|
if (!this.store.synchronous) { |
|
throw new Error('CookieJar store is not synchronous; use async API instead.'); |
|
} |
|
let syncErr = null; |
|
let syncResult = undefined; |
|
try { |
|
fn.call(this, (error, result) => { |
|
syncErr = error; |
|
syncResult = result; |
|
}); |
|
} |
|
catch (err) { |
|
syncErr = err; |
|
} |
|
if (syncErr) |
|
throw syncErr; |
|
return syncResult; |
|
} |
|
|
|
|
|
|
|
setCookie(cookie, url, options, callback) { |
|
if (typeof options === 'function') { |
|
callback = options; |
|
options = undefined; |
|
} |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const cb = promiseCallback.callback; |
|
let context; |
|
try { |
|
if (typeof url === 'string') { |
|
validators.validate(validators.isNonEmptyString(url), callback, (0, utils_1.safeToString)(options)); |
|
} |
|
context = getCookieContext(url); |
|
if (typeof url === 'function') { |
|
return promiseCallback.reject(new Error('No URL was specified')); |
|
} |
|
if (typeof options === 'function') { |
|
options = defaultSetCookieOptions; |
|
} |
|
validators.validate(typeof cb === 'function', cb); |
|
if (!validators.isNonEmptyString(cookie) && |
|
!validators.isObject(cookie) && |
|
cookie instanceof String && |
|
cookie.length == 0) { |
|
return promiseCallback.resolve(undefined); |
|
} |
|
} |
|
catch (err) { |
|
return promiseCallback.reject(err); |
|
} |
|
const host = (0, canonicalDomain_1.canonicalDomain)(context.hostname) ?? null; |
|
const loose = options?.loose || this.enableLooseMode; |
|
let sameSiteContext = null; |
|
if (options?.sameSiteContext) { |
|
sameSiteContext = checkSameSiteContext(options.sameSiteContext); |
|
if (!sameSiteContext) { |
|
return promiseCallback.reject(new Error(SAME_SITE_CONTEXT_VAL_ERR)); |
|
} |
|
} |
|
|
|
if (typeof cookie === 'string' || cookie instanceof String) { |
|
const parsedCookie = cookie_1.Cookie.parse(cookie.toString(), { loose: loose }); |
|
if (!parsedCookie) { |
|
const err = new Error('Cookie failed to parse'); |
|
return options?.ignoreError |
|
? promiseCallback.resolve(undefined) |
|
: promiseCallback.reject(err); |
|
} |
|
cookie = parsedCookie; |
|
} |
|
else if (!(cookie instanceof cookie_1.Cookie)) { |
|
|
|
|
|
const err = new Error('First argument to setCookie must be a Cookie object or string'); |
|
return options?.ignoreError |
|
? promiseCallback.resolve(undefined) |
|
: promiseCallback.reject(err); |
|
} |
|
|
|
const now = options?.now || new Date(); |
|
|
|
|
|
|
|
if (this.rejectPublicSuffixes && cookie.domain) { |
|
try { |
|
const cdomain = cookie.cdomain(); |
|
const suffix = typeof cdomain === 'string' |
|
? (0, getPublicSuffix_1.getPublicSuffix)(cdomain, { |
|
allowSpecialUseDomain: this.allowSpecialUseDomain, |
|
ignoreError: options?.ignoreError, |
|
}) |
|
: null; |
|
if (suffix == null && !constants_1.IP_V6_REGEX_OBJECT.test(cookie.domain)) { |
|
|
|
const err = new Error('Cookie has domain set to a public suffix'); |
|
return options?.ignoreError |
|
? promiseCallback.resolve(undefined) |
|
: promiseCallback.reject(err); |
|
} |
|
|
|
|
|
|
|
|
|
} |
|
catch (err) { |
|
return options?.ignoreError |
|
? promiseCallback.resolve(undefined) |
|
: |
|
promiseCallback.reject(err); |
|
} |
|
} |
|
|
|
if (cookie.domain) { |
|
if (!(0, domainMatch_1.domainMatch)(host ?? undefined, cookie.cdomain() ?? undefined, false)) { |
|
const err = new Error(`Cookie not in this host's domain. Cookie:${cookie.cdomain() ?? 'null'} Request:${host ?? 'null'}`); |
|
return options?.ignoreError |
|
? promiseCallback.resolve(undefined) |
|
: promiseCallback.reject(err); |
|
} |
|
if (cookie.hostOnly == null) { |
|
|
|
cookie.hostOnly = false; |
|
} |
|
} |
|
else { |
|
cookie.hostOnly = true; |
|
cookie.domain = host; |
|
} |
|
|
|
|
|
|
|
if (!cookie.path || cookie.path[0] !== '/') { |
|
cookie.path = (0, defaultPath_1.defaultPath)(context.pathname); |
|
cookie.pathIsDefault = true; |
|
} |
|
|
|
|
|
|
|
if (options?.http === false && cookie.httpOnly) { |
|
const err = new Error("Cookie is HttpOnly and this isn't an HTTP API"); |
|
return options.ignoreError |
|
? promiseCallback.resolve(undefined) |
|
: promiseCallback.reject(err); |
|
} |
|
|
|
if (cookie.sameSite !== 'none' && |
|
cookie.sameSite !== undefined && |
|
sameSiteContext) { |
|
|
|
|
|
|
|
|
|
if (sameSiteContext === 'none') { |
|
const err = new Error('Cookie is SameSite but this is a cross-origin request'); |
|
return options?.ignoreError |
|
? promiseCallback.resolve(undefined) |
|
: promiseCallback.reject(err); |
|
} |
|
} |
|
|
|
const ignoreErrorForPrefixSecurity = this.prefixSecurity === constants_1.PrefixSecurityEnum.SILENT; |
|
const prefixSecurityDisabled = this.prefixSecurity === constants_1.PrefixSecurityEnum.DISABLED; |
|
|
|
if (!prefixSecurityDisabled) { |
|
let errorFound = false; |
|
let errorMsg; |
|
|
|
if (!isSecurePrefixConditionMet(cookie)) { |
|
errorFound = true; |
|
errorMsg = 'Cookie has __Secure prefix but Secure attribute is not set'; |
|
} |
|
else if (!isHostPrefixConditionMet(cookie)) { |
|
|
|
errorFound = true; |
|
errorMsg = |
|
"Cookie has __Host prefix but either Secure or HostOnly attribute is not set or Path is not '/'"; |
|
} |
|
if (errorFound) { |
|
return options?.ignoreError || ignoreErrorForPrefixSecurity |
|
? promiseCallback.resolve(undefined) |
|
: promiseCallback.reject(new Error(errorMsg)); |
|
} |
|
} |
|
const store = this.store; |
|
|
|
|
|
|
|
if (!store.updateCookie) { |
|
store.updateCookie = async function (_oldCookie, newCookie, cb) { |
|
return this.putCookie(newCookie).then(() => cb?.(null), (error) => cb?.(error)); |
|
}; |
|
} |
|
const withCookie = function withCookie(err, oldCookie) { |
|
if (err) { |
|
cb(err); |
|
return; |
|
} |
|
const next = function (err) { |
|
if (err) { |
|
cb(err); |
|
} |
|
else if (typeof cookie === 'string') { |
|
cb(null, undefined); |
|
} |
|
else { |
|
cb(null, cookie); |
|
} |
|
}; |
|
if (oldCookie) { |
|
|
|
|
|
if (options && |
|
'http' in options && |
|
options.http === false && |
|
oldCookie.httpOnly) { |
|
|
|
err = new Error("old Cookie is HttpOnly and this isn't an HTTP API"); |
|
if (options.ignoreError) |
|
cb(null, undefined); |
|
else |
|
cb(err); |
|
return; |
|
} |
|
if (cookie instanceof cookie_1.Cookie) { |
|
cookie.creation = oldCookie.creation; |
|
|
|
cookie.creationIndex = oldCookie.creationIndex; |
|
|
|
cookie.lastAccessed = now; |
|
|
|
store.updateCookie(oldCookie, cookie, next); |
|
} |
|
} |
|
else { |
|
if (cookie instanceof cookie_1.Cookie) { |
|
cookie.creation = cookie.lastAccessed = now; |
|
store.putCookie(cookie, next); |
|
} |
|
} |
|
}; |
|
|
|
store.findCookie(cookie.domain, cookie.path, cookie.key, withCookie); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setCookieSync(cookie, url, options) { |
|
const setCookieFn = options |
|
? this.setCookie.bind(this, cookie, url, options) |
|
: this.setCookie.bind(this, cookie, url); |
|
return this.callSync(setCookieFn); |
|
} |
|
|
|
|
|
|
|
getCookies(url, options, callback) { |
|
|
|
if (typeof options === 'function') { |
|
callback = options; |
|
options = defaultGetCookieOptions; |
|
} |
|
else if (options === undefined) { |
|
options = defaultGetCookieOptions; |
|
} |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const cb = promiseCallback.callback; |
|
let context; |
|
try { |
|
if (typeof url === 'string') { |
|
validators.validate(validators.isNonEmptyString(url), cb, url); |
|
} |
|
context = getCookieContext(url); |
|
validators.validate(validators.isObject(options), cb, (0, utils_1.safeToString)(options)); |
|
validators.validate(typeof cb === 'function', cb); |
|
} |
|
catch (parameterError) { |
|
return promiseCallback.reject(parameterError); |
|
} |
|
const host = (0, canonicalDomain_1.canonicalDomain)(context.hostname); |
|
const path = context.pathname || '/'; |
|
const secure = context.protocol && |
|
(context.protocol == 'https:' || context.protocol == 'wss:'); |
|
let sameSiteLevel = 0; |
|
if (options.sameSiteContext) { |
|
const sameSiteContext = checkSameSiteContext(options.sameSiteContext); |
|
if (sameSiteContext == null) { |
|
return promiseCallback.reject(new Error(SAME_SITE_CONTEXT_VAL_ERR)); |
|
} |
|
sameSiteLevel = cookie_1.Cookie.sameSiteLevel[sameSiteContext]; |
|
if (!sameSiteLevel) { |
|
return promiseCallback.reject(new Error(SAME_SITE_CONTEXT_VAL_ERR)); |
|
} |
|
} |
|
const http = options.http ?? true; |
|
const now = Date.now(); |
|
const expireCheck = options.expire ?? true; |
|
const allPaths = options.allPaths ?? false; |
|
const store = this.store; |
|
function matchingCookie(c) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (c.hostOnly) { |
|
if (c.domain != host) { |
|
return false; |
|
} |
|
} |
|
else { |
|
if (!(0, domainMatch_1.domainMatch)(host ?? undefined, c.domain ?? undefined, false)) { |
|
return false; |
|
} |
|
} |
|
|
|
if (!allPaths && typeof c.path === 'string' && !(0, pathMatch_1.pathMatch)(path, c.path)) { |
|
return false; |
|
} |
|
|
|
|
|
if (c.secure && !secure) { |
|
return false; |
|
} |
|
|
|
|
|
if (c.httpOnly && !http) { |
|
return false; |
|
} |
|
|
|
if (sameSiteLevel) { |
|
let cookieLevel; |
|
if (c.sameSite === 'lax') { |
|
cookieLevel = cookie_1.Cookie.sameSiteLevel.lax; |
|
} |
|
else if (c.sameSite === 'strict') { |
|
cookieLevel = cookie_1.Cookie.sameSiteLevel.strict; |
|
} |
|
else { |
|
cookieLevel = cookie_1.Cookie.sameSiteLevel.none; |
|
} |
|
if (cookieLevel > sameSiteLevel) { |
|
|
|
return false; |
|
} |
|
} |
|
|
|
|
|
const expiryTime = c.expiryTime(); |
|
if (expireCheck && expiryTime && expiryTime <= now) { |
|
store.removeCookie(c.domain, c.path, c.key, () => { }); |
|
return false; |
|
} |
|
return true; |
|
} |
|
store.findCookies(host, allPaths ? null : path, this.allowSpecialUseDomain, (err, cookies) => { |
|
if (err) { |
|
cb(err); |
|
return; |
|
} |
|
if (cookies == null) { |
|
cb(null, []); |
|
return; |
|
} |
|
cookies = cookies.filter(matchingCookie); |
|
|
|
if ('sort' in options && options.sort !== false) { |
|
cookies = cookies.sort(cookieCompare_1.cookieCompare); |
|
} |
|
|
|
const now = new Date(); |
|
for (const cookie of cookies) { |
|
cookie.lastAccessed = now; |
|
} |
|
|
|
cb(null, cookies); |
|
}); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getCookiesSync(url, options) { |
|
return this.callSync(this.getCookies.bind(this, url, options)) ?? []; |
|
} |
|
|
|
|
|
|
|
getCookieString(url, options, callback) { |
|
if (typeof options === 'function') { |
|
callback = options; |
|
options = undefined; |
|
} |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const next = function (err, cookies) { |
|
if (err) { |
|
promiseCallback.callback(err); |
|
} |
|
else { |
|
promiseCallback.callback(null, cookies |
|
?.sort(cookieCompare_1.cookieCompare) |
|
.map((c) => c.cookieString()) |
|
.join('; ')); |
|
} |
|
}; |
|
this.getCookies(url, options, next); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getCookieStringSync(url, options) { |
|
return (this.callSync(options |
|
? this.getCookieString.bind(this, url, options) |
|
: this.getCookieString.bind(this, url)) ?? ''); |
|
} |
|
|
|
|
|
|
|
getSetCookieStrings(url, options, callback) { |
|
if (typeof options === 'function') { |
|
callback = options; |
|
options = undefined; |
|
} |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const next = function (err, cookies) { |
|
if (err) { |
|
promiseCallback.callback(err); |
|
} |
|
else { |
|
promiseCallback.callback(null, cookies?.map((c) => { |
|
return c.toString(); |
|
})); |
|
} |
|
}; |
|
this.getCookies(url, options, next); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getSetCookieStringsSync(url, options = {}) { |
|
return (this.callSync(this.getSetCookieStrings.bind(this, url, options)) ?? []); |
|
} |
|
|
|
|
|
|
|
serialize(callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
let type = this.store.constructor.name; |
|
if (validators.isObject(type)) { |
|
type = null; |
|
} |
|
|
|
const serialized = { |
|
|
|
|
|
|
|
version: `tough-cookie@${version_1.version}`, |
|
|
|
storeType: type, |
|
|
|
rejectPublicSuffixes: this.rejectPublicSuffixes, |
|
enableLooseMode: this.enableLooseMode, |
|
allowSpecialUseDomain: this.allowSpecialUseDomain, |
|
prefixSecurity: getNormalizedPrefixSecurity(this.prefixSecurity), |
|
|
|
cookies: [], |
|
}; |
|
if (typeof this.store.getAllCookies !== 'function') { |
|
return promiseCallback.reject(new Error('store does not support getAllCookies and cannot be serialized')); |
|
} |
|
this.store.getAllCookies((err, cookies) => { |
|
if (err) { |
|
promiseCallback.callback(err); |
|
return; |
|
} |
|
if (cookies == null) { |
|
promiseCallback.callback(null, serialized); |
|
return; |
|
} |
|
serialized.cookies = cookies.map((cookie) => { |
|
|
|
const serializedCookie = cookie.toJSON(); |
|
|
|
delete serializedCookie.creationIndex; |
|
return serializedCookie; |
|
}); |
|
promiseCallback.callback(null, serialized); |
|
}); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
serializeSync() { |
|
return this.callSync((callback) => { |
|
this.serialize(callback); |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
toJSON() { |
|
return this.serializeSync(); |
|
} |
|
|
|
|
|
|
|
|
|
_importCookies(serialized, callback) { |
|
let cookies = undefined; |
|
if (serialized && |
|
typeof serialized === 'object' && |
|
(0, utils_1.inOperator)('cookies', serialized) && |
|
Array.isArray(serialized.cookies)) { |
|
cookies = serialized.cookies; |
|
} |
|
if (!cookies) { |
|
callback(new Error('serialized jar has no cookies array'), undefined); |
|
return; |
|
} |
|
cookies = cookies.slice(); |
|
const putNext = (err) => { |
|
if (err) { |
|
callback(err, undefined); |
|
return; |
|
} |
|
if (Array.isArray(cookies)) { |
|
if (!cookies.length) { |
|
callback(err, this); |
|
return; |
|
} |
|
let cookie; |
|
try { |
|
cookie = cookie_1.Cookie.fromJSON(cookies.shift()); |
|
} |
|
catch (e) { |
|
callback(e instanceof Error ? e : new Error(), undefined); |
|
return; |
|
} |
|
if (cookie === undefined) { |
|
putNext(null); |
|
return; |
|
} |
|
this.store.putCookie(cookie, putNext); |
|
} |
|
}; |
|
putNext(null); |
|
} |
|
|
|
|
|
|
|
_importCookiesSync(serialized) { |
|
this.callSync(this._importCookies.bind(this, serialized)); |
|
} |
|
|
|
|
|
|
|
clone(newStore, callback) { |
|
if (typeof newStore === 'function') { |
|
callback = newStore; |
|
newStore = undefined; |
|
} |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const cb = promiseCallback.callback; |
|
this.serialize((err, serialized) => { |
|
if (err) { |
|
return promiseCallback.reject(err); |
|
} |
|
return CookieJar.deserialize(serialized ?? '', newStore, cb); |
|
}); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
_cloneSync(newStore) { |
|
const cloneFn = newStore && typeof newStore !== 'function' |
|
? this.clone.bind(this, newStore) |
|
: this.clone.bind(this); |
|
return this.callSync((callback) => { |
|
cloneFn(callback); |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cloneSync(newStore) { |
|
if (!newStore) { |
|
return this._cloneSync(); |
|
} |
|
if (!newStore.synchronous) { |
|
throw new Error('CookieJar clone destination store is not synchronous; use async API instead.'); |
|
} |
|
return this._cloneSync(newStore); |
|
} |
|
|
|
|
|
|
|
removeAllCookies(callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const cb = promiseCallback.callback; |
|
const store = this.store; |
|
|
|
|
|
|
|
if (typeof store.removeAllCookies === 'function' && |
|
store.removeAllCookies !== store_1.Store.prototype.removeAllCookies) { |
|
|
|
|
|
store.removeAllCookies(cb); |
|
return promiseCallback.promise; |
|
} |
|
store.getAllCookies((err, cookies) => { |
|
if (err) { |
|
cb(err); |
|
return; |
|
} |
|
if (!cookies) { |
|
cookies = []; |
|
} |
|
if (cookies.length === 0) { |
|
cb(null, undefined); |
|
return; |
|
} |
|
let completedCount = 0; |
|
const removeErrors = []; |
|
|
|
const removeCookieCb = function removeCookieCb(removeErr) { |
|
if (removeErr) { |
|
removeErrors.push(removeErr); |
|
} |
|
completedCount++; |
|
if (completedCount === cookies.length) { |
|
if (removeErrors[0]) |
|
cb(removeErrors[0]); |
|
else |
|
cb(null, undefined); |
|
return; |
|
} |
|
}; |
|
cookies.forEach((cookie) => { |
|
store.removeCookie(cookie.domain, cookie.path, cookie.key, removeCookieCb); |
|
}); |
|
}); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
removeAllCookiesSync() { |
|
this.callSync((callback) => { |
|
|
|
|
|
this.removeAllCookies(callback); |
|
}); |
|
} |
|
|
|
|
|
|
|
static deserialize(strOrObj, store, callback) { |
|
if (typeof store === 'function') { |
|
callback = store; |
|
store = undefined; |
|
} |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
let serialized; |
|
if (typeof strOrObj === 'string') { |
|
try { |
|
serialized = JSON.parse(strOrObj); |
|
} |
|
catch (e) { |
|
return promiseCallback.reject(e instanceof Error ? e : new Error()); |
|
} |
|
} |
|
else { |
|
serialized = strOrObj; |
|
} |
|
const readSerializedProperty = (property) => { |
|
return serialized && |
|
typeof serialized === 'object' && |
|
(0, utils_1.inOperator)(property, serialized) |
|
? serialized[property] |
|
: undefined; |
|
}; |
|
const readSerializedBoolean = (property) => { |
|
const value = readSerializedProperty(property); |
|
return typeof value === 'boolean' ? value : undefined; |
|
}; |
|
const readSerializedString = (property) => { |
|
const value = readSerializedProperty(property); |
|
return typeof value === 'string' ? value : undefined; |
|
}; |
|
const jar = new CookieJar(store, { |
|
rejectPublicSuffixes: readSerializedBoolean('rejectPublicSuffixes'), |
|
looseMode: readSerializedBoolean('enableLooseMode'), |
|
allowSpecialUseDomain: readSerializedBoolean('allowSpecialUseDomain'), |
|
prefixSecurity: getNormalizedPrefixSecurity(readSerializedString('prefixSecurity') ?? 'silent'), |
|
}); |
|
jar._importCookies(serialized, (err) => { |
|
if (err) { |
|
promiseCallback.callback(err); |
|
return; |
|
} |
|
promiseCallback.callback(null, jar); |
|
}); |
|
return promiseCallback.promise; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static deserializeSync(strOrObj, store) { |
|
const serialized = typeof strOrObj === 'string' ? JSON.parse(strOrObj) : strOrObj; |
|
const readSerializedProperty = (property) => { |
|
return serialized && |
|
typeof serialized === 'object' && |
|
(0, utils_1.inOperator)(property, serialized) |
|
? serialized[property] |
|
: undefined; |
|
}; |
|
const readSerializedBoolean = (property) => { |
|
const value = readSerializedProperty(property); |
|
return typeof value === 'boolean' ? value : undefined; |
|
}; |
|
const readSerializedString = (property) => { |
|
const value = readSerializedProperty(property); |
|
return typeof value === 'string' ? value : undefined; |
|
}; |
|
const jar = new CookieJar(store, { |
|
rejectPublicSuffixes: readSerializedBoolean('rejectPublicSuffixes'), |
|
looseMode: readSerializedBoolean('enableLooseMode'), |
|
allowSpecialUseDomain: readSerializedBoolean('allowSpecialUseDomain'), |
|
prefixSecurity: getNormalizedPrefixSecurity(readSerializedString('prefixSecurity') ?? 'silent'), |
|
}); |
|
|
|
if (!jar.store.synchronous) { |
|
throw new Error('CookieJar store is not synchronous; use async API instead.'); |
|
} |
|
jar._importCookiesSync(serialized); |
|
return jar; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static fromJSON(jsonString, store) { |
|
return CookieJar.deserializeSync(jsonString, store); |
|
} |
|
} |
|
exports.CookieJar = CookieJar; |
|
|