87 lines
12 KiB
JavaScript
87 lines
12 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.CdpCookieProvider = void 0;
|
||
|
const url_1 = require("url");
|
||
|
const base_1 = require("../test-run/cookies/base");
|
||
|
const match_collection_1 = __importDefault(require("../utils/match-collection"));
|
||
|
const get_active_client_1 = require("./utils/get-active-client");
|
||
|
const MAX_TIMESTAMP = 8640000000000000;
|
||
|
class CdpCookieProvider extends base_1.CookieProviderBase {
|
||
|
async _getCdpClient() {
|
||
|
const browserConnection = this.testRun.browserConnection;
|
||
|
return (0, get_active_client_1.getActiveClient)(browserConnection);
|
||
|
}
|
||
|
async initialize() {
|
||
|
return this.deleteCookies();
|
||
|
}
|
||
|
async getCookies(externalCookies) {
|
||
|
const client = await this._getCdpClient();
|
||
|
const { cookies } = await client.Storage.getCookies({});
|
||
|
return (0, match_collection_1.default)(cookies, externalCookies).map(this._cdpCookieToExternalCookie);
|
||
|
}
|
||
|
async setCookies(cookies, url) {
|
||
|
const client = await this._getCdpClient();
|
||
|
const { hostname = '', pathname = '/' } = url ? new url_1.URL(url) : {};
|
||
|
await client.Network.setCookies({
|
||
|
cookies: cookies.map(cookie => this._cookieOptionToCdpCookieParam(cookie, hostname, pathname)),
|
||
|
});
|
||
|
}
|
||
|
async deleteCookies(cookies = [], urls = []) {
|
||
|
const client = await this._getCdpClient();
|
||
|
if (!cookies || !cookies.length)
|
||
|
return client.Network.clearBrowserCookies();
|
||
|
const parsedUrls = this._parseUrls(urls);
|
||
|
let existingCookies = await this.getCookies([]);
|
||
|
if (parsedUrls.length) {
|
||
|
existingCookies = existingCookies.filter(cookie => parsedUrls
|
||
|
.find(url => url.domain === cookie.domain && url.path === cookie.path));
|
||
|
}
|
||
|
existingCookies = (0, match_collection_1.default)(existingCookies, cookies);
|
||
|
for (const cookie of existingCookies) {
|
||
|
await client.Network.deleteCookies({
|
||
|
name: cookie.name,
|
||
|
domain: cookie.domain,
|
||
|
path: cookie.path,
|
||
|
});
|
||
|
}
|
||
|
return void 0;
|
||
|
}
|
||
|
_cdpCookieToExternalCookie(cookie) {
|
||
|
var _a;
|
||
|
return {
|
||
|
name: cookie.name,
|
||
|
value: cookie.value,
|
||
|
domain: cookie.domain,
|
||
|
maxAge: void 0,
|
||
|
path: cookie.path,
|
||
|
expires: void 0,
|
||
|
secure: cookie.secure,
|
||
|
httpOnly: cookie.httpOnly,
|
||
|
sameSite: (_a = cookie.sameSite) !== null && _a !== void 0 ? _a : 'none',
|
||
|
};
|
||
|
}
|
||
|
_cookieOptionToCdpCookieParam(cookie, hostname, pathname) {
|
||
|
var _a, _b, _c;
|
||
|
return {
|
||
|
name: cookie.name,
|
||
|
value: cookie.value,
|
||
|
domain: (_a = cookie.domain) !== null && _a !== void 0 ? _a : hostname,
|
||
|
path: (_b = cookie.path) !== null && _b !== void 0 ? _b : pathname,
|
||
|
secure: cookie.secure,
|
||
|
httpOnly: false,
|
||
|
sameSite: cookie.sameSite,
|
||
|
expires: ((_c = cookie.expires) === null || _c === void 0 ? void 0 : _c.getTime()) || MAX_TIMESTAMP,
|
||
|
};
|
||
|
}
|
||
|
_parseUrls(urls) {
|
||
|
return urls.map(url => {
|
||
|
const { hostname, pathname } = new url_1.URL(url);
|
||
|
return { domain: hostname, path: pathname };
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
exports.CdpCookieProvider = CdpCookieProvider;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29va2llLXByb3ZpZGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3Byb3h5bGVzcy9jb29raWUtcHJvdmlkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBSUEsNkJBQTBCO0FBRT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