|
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.MemoryCookieStore = void 0; |
|
const pathMatch_1 = require("./pathMatch"); |
|
const permuteDomain_1 = require("./permuteDomain"); |
|
const store_1 = require("./store"); |
|
const utils_1 = require("./utils"); |
|
|
|
|
|
|
|
|
|
|
|
class MemoryCookieStore extends store_1.Store { |
|
|
|
|
|
|
|
constructor() { |
|
super(); |
|
this.synchronous = true; |
|
this.idx = Object.create(null); |
|
} |
|
|
|
|
|
|
|
findCookie(domain, path, key, callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
if (domain == null || path == null || key == null) { |
|
return promiseCallback.resolve(undefined); |
|
} |
|
const result = this.idx[domain]?.[path]?.[key]; |
|
return promiseCallback.resolve(result); |
|
} |
|
|
|
|
|
|
|
findCookies(domain, path, allowSpecialUseDomain = false, callback) { |
|
if (typeof allowSpecialUseDomain === 'function') { |
|
callback = allowSpecialUseDomain; |
|
|
|
|
|
allowSpecialUseDomain = true; |
|
} |
|
const results = []; |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
if (!domain) { |
|
return promiseCallback.resolve([]); |
|
} |
|
let pathMatcher; |
|
if (!path) { |
|
|
|
pathMatcher = function matchAll(domainIndex) { |
|
for (const curPath in domainIndex) { |
|
const pathIndex = domainIndex[curPath]; |
|
for (const key in pathIndex) { |
|
const value = pathIndex[key]; |
|
if (value) { |
|
results.push(value); |
|
} |
|
} |
|
} |
|
}; |
|
} |
|
else { |
|
pathMatcher = function matchRFC(domainIndex) { |
|
|
|
|
|
for (const cookiePath in domainIndex) { |
|
if ((0, pathMatch_1.pathMatch)(path, cookiePath)) { |
|
const pathIndex = domainIndex[cookiePath]; |
|
for (const key in pathIndex) { |
|
const value = pathIndex[key]; |
|
if (value) { |
|
results.push(value); |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
} |
|
const domains = (0, permuteDomain_1.permuteDomain)(domain, allowSpecialUseDomain) || [domain]; |
|
const idx = this.idx; |
|
domains.forEach((curDomain) => { |
|
const domainIndex = idx[curDomain]; |
|
if (!domainIndex) { |
|
return; |
|
} |
|
pathMatcher(domainIndex); |
|
}); |
|
return promiseCallback.resolve(results); |
|
} |
|
|
|
|
|
|
|
putCookie(cookie, callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const { domain, path, key } = cookie; |
|
|
|
if (domain == null || path == null || key == null) { |
|
return promiseCallback.resolve(undefined); |
|
} |
|
const domainEntry = this.idx[domain] ?? |
|
Object.create(null); |
|
this.idx[domain] = domainEntry; |
|
const pathEntry = domainEntry[path] ?? |
|
Object.create(null); |
|
domainEntry[path] = pathEntry; |
|
pathEntry[key] = cookie; |
|
return promiseCallback.resolve(undefined); |
|
} |
|
|
|
|
|
|
|
updateCookie(_oldCookie, newCookie, callback) { |
|
|
|
|
|
|
|
|
|
if (callback) |
|
this.putCookie(newCookie, callback); |
|
else |
|
return this.putCookie(newCookie); |
|
} |
|
|
|
|
|
|
|
removeCookie(domain, path, key, callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
delete this.idx[domain]?.[path]?.[key]; |
|
return promiseCallback.resolve(undefined); |
|
} |
|
|
|
|
|
|
|
removeCookies(domain, path, callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const domainEntry = this.idx[domain]; |
|
if (domainEntry) { |
|
if (path) { |
|
|
|
delete domainEntry[path]; |
|
} |
|
else { |
|
|
|
delete this.idx[domain]; |
|
} |
|
} |
|
return promiseCallback.resolve(undefined); |
|
} |
|
|
|
|
|
|
|
removeAllCookies(callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
this.idx = Object.create(null); |
|
return promiseCallback.resolve(undefined); |
|
} |
|
|
|
|
|
|
|
getAllCookies(callback) { |
|
const promiseCallback = (0, utils_1.createPromiseCallback)(callback); |
|
const cookies = []; |
|
const idx = this.idx; |
|
const domains = Object.keys(idx); |
|
domains.forEach((domain) => { |
|
const domainEntry = idx[domain] ?? {}; |
|
const paths = Object.keys(domainEntry); |
|
paths.forEach((path) => { |
|
const pathEntry = domainEntry[path] ?? {}; |
|
const keys = Object.keys(pathEntry); |
|
keys.forEach((key) => { |
|
const keyEntry = pathEntry[key]; |
|
if (keyEntry != null) { |
|
cookies.push(keyEntry); |
|
} |
|
}); |
|
}); |
|
}); |
|
|
|
|
|
cookies.sort((a, b) => { |
|
return (a.creationIndex || 0) - (b.creationIndex || 0); |
|
}); |
|
return promiseCallback.resolve(cookies); |
|
} |
|
} |
|
exports.MemoryCookieStore = MemoryCookieStore; |
|
|