playwright-fluent
Version:
Fluent API around playwright
47 lines (46 loc) • 1.85 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.harHeadersToHttpHeaders = exports.getHarResponseContentAs = exports.getHarDataFrom = void 0;
const fs_1 = require("../fs/fs");
const fs_2 = require("fs");
function getHarDataFrom(filepath) {
if (!filepath) {
throw new Error(`HAR filepath has not been setup. Ensure you have called 'recordNetworkActivity({path: <valid file path>})'.`);
}
if ((0, fs_1.fileDoesNotExist)(filepath)) {
throw new Error(`File '${filepath}' does not exist. Ensure you have called 'recordNetworkActivity({path: ${filepath}})' and that you have closed the browser. HAR data is only saved to disk when the browser is closed.`);
}
const harData = JSON.parse((0, fs_2.readFileSync)(filepath, 'utf8'));
return harData;
}
exports.getHarDataFrom = getHarDataFrom;
function getHarResponseContentAs(harResponse) {
var _a;
const base64EncodedContent = (_a = harResponse === null || harResponse === void 0 ? void 0 : harResponse.content) === null || _a === void 0 ? void 0 : _a.text;
if (!base64EncodedContent) {
return {};
}
const rawContent = Buffer.from(base64EncodedContent, 'base64').toString('utf8');
if (!rawContent) {
return {};
}
try {
return JSON.parse(rawContent);
}
catch (error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return rawContent;
}
}
exports.getHarResponseContentAs = getHarResponseContentAs;
function harHeadersToHttpHeaders(headers) {
const keyValueHeaders = {};
if (!Array.isArray(headers)) {
return keyValueHeaders;
}
headers.forEach((header) => {
keyValueHeaders[header.name] = header.value;
});
return keyValueHeaders;
}
exports.harHeadersToHttpHeaders = harHeadersToHttpHeaders;
;