@authless/puppeteer-extra-plugin-har
Version:
> This is a plugin for [puppeteer-extra](https://github.com/berstend/puppeteer-extra) that allows you to create a HAR file from your page.
110 lines • 4.78 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Har = void 0;
const puppeteer_extra_plugin_1 = require("puppeteer-extra-plugin");
const puppeteer_har_1 = __importDefault(require("@authless/puppeteer-har"));
class Har extends puppeteer_extra_plugin_1.PuppeteerExtraPlugin {
constructor(config = {}) {
super(config);
/* eslint-disable-next-line no-empty-function */
this.harCallback = () => __awaiter(this, void 0, void 0, function* () { });
this.targetMap = new Map();
if (typeof config.callback !== 'undefined') {
this.harCallback = config.callback;
}
}
/* eslint-disable-next-line class-methods-use-this */
get name() {
return 'har';
}
static getTargetId(target) {
var _a;
const targetUrl = target.url();
const targetId = (_a = target._targetInfo) === null || _a === void 0 ? void 0 : _a.targetId;
if (typeof targetId !== 'string') {
throw new Error(`failed to get target id for target: ${targetUrl} (type: ${target.type()})`);
}
return targetId;
}
static getBrowserProcessId(target) {
var _a;
const targetUrl = target.url();
const browser = target.browser();
const processId = `${(_a = browser._process) === null || _a === void 0 ? void 0 : _a.pid}`;
if (typeof processId !== 'string') {
throw new Error(`failed to get browser process id for target: ${targetUrl} (type: ${target.type()})`);
}
return processId;
}
static getHarId(target) {
return `${this.getBrowserProcessId(target)}-${this.getTargetId(target)}`;
}
startRecording(target) {
return __awaiter(this, void 0, void 0, function* () {
const harId = Har.getHarId(target);
const page = yield target.page();
const har = new puppeteer_har_1.default(page);
yield har.start();
this.debug(`recording HAR [ID: ${harId}]`);
this.targetMap.set(harId, har);
});
}
finishRecording(harId) {
return __awaiter(this, void 0, void 0, function* () {
const harInstance = this.targetMap.get(harId);
if (harInstance instanceof puppeteer_har_1.default) {
this.targetMap.delete(harId);
try {
let start = Date.now();
const harObject = yield harInstance.stop();
this.debug(`recorded HAR [ID: ${harId}]. (serialized in ${Date.now() - start}ms)`);
yield this.harCallback(null, { har: harObject, harId });
}
catch (error) {
yield this.harCallback(error, null);
}
}
});
}
onTargetCreated(target) {
return __awaiter(this, void 0, void 0, function* () {
if (target.type() === 'page') {
yield this.startRecording(target);
}
});
}
onTargetDestroyed(target) {
return __awaiter(this, void 0, void 0, function* () {
if (target.type() === 'page') {
const harId = Har.getHarId(target);
yield this.finishRecording(harId);
}
});
}
onDisconnected() {
return __awaiter(this, void 0, void 0, function* () {
if (this.targetMap.size > 0) {
this.debug.extend('disconnect')(`open recordings discovered: ${this.targetMap.size}`);
const openHarIds = Array.from(this.targetMap.keys());
yield Promise.all(openHarIds.map((harId) => __awaiter(this, void 0, void 0, function* () {
/* eslint-disable-next-line no-return-await */
return yield this.finishRecording(harId);
})));
}
});
}
}
exports.Har = Har;
//# sourceMappingURL=har.js.map