Skip to content

Commit ed6b142

Browse files
author
Michael Mrowetz
committed
#162 handle HARs with no page property
1 parent 11200f0 commit ed6b142

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

Diff for: src/ts/transformers/har.ts

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { roundNumber } from "../helpers/misc";
22
import { toInt } from "../helpers/parse";
3-
import { Entry, Har, PageTimings } from "../typing/har";
3+
import {
4+
Entry,
5+
Har,
6+
Page,
7+
PageTimings,
8+
} from "../typing/har";
49
import {
510
Mark,
611
TimingType,
@@ -28,10 +33,11 @@ import {
2833
export function transformDoc(harData: Har): WaterfallDocs {
2934
// make sure it's the *.log base node
3035
let data = (harData["log"] !== undefined ? harData["log"] : harData) as Har;
31-
console.log("HAR created by %s(%s) %s page(s)", data.creator.name, data.creator.version, data.pages.length);
36+
const pages = getPages(data);
37+
console.log("HAR created by %s(%s) %s page(s)", data.creator.name, data.creator.version, pages.length);
3238

3339
return {
34-
pages: data.pages.map((_page, i) => this.transformPage(data, i)),
40+
pages: pages.map((_page, i) => this.transformPage(data, i)),
3541
};
3642
}
3743

@@ -57,6 +63,25 @@ function toWaterFallEntry(entry: Entry, index: number, startRelative: number, is
5763
);
5864
}
5965

66+
/** retuns the page or a mock page object */
67+
function getPages(data: Har) {
68+
if (data.pages && data.pages.length > 0) {
69+
return data.pages;
70+
}
71+
const statedTime = data.entries.reduce((earliest, curr) => {
72+
const currDate = Date.parse(curr.startedDateTime);
73+
const earliestDate = Date.parse(earliest);
74+
return earliestDate < currDate ? earliest : curr.startedDateTime;
75+
}, data.entries[0].startedDateTime);
76+
console.log(statedTime);
77+
return [{
78+
id: "",
79+
pageTimings: {},
80+
startedDateTime: data.entries[0].startedDateTime,
81+
title: "n/a",
82+
} as Page];
83+
}
84+
6085
/**
6186
* Transforms a HAR object into the format needed to render the PerfCascade
6287
* @param {Har} harData - HAR document
@@ -67,16 +92,22 @@ export function transformPage(harData: Har, pageIndex: number = 0): WaterfallDat
6792
// make sure it's the *.log base node
6893
let data = (harData["log"] !== undefined ? harData["log"] : harData) as Har;
6994

70-
const currPage = data.pages[pageIndex];
95+
const pages = getPages(data);
96+
const currPage = pages[pageIndex];
7197
const pageStartTime = new Date(currPage.startedDateTime).getTime();
7298
const pageTimings = currPage.pageTimings;
7399

74-
console.log("%s: %s of %s page(s)", currPage.title, pageIndex + 1, data.pages.length);
100+
console.log("%s: %s of %s page(s)", currPage.title, pageIndex + 1, pages.length);
75101

76102
let doneTime = 0;
77103
const isTLS = documentIsSecure(data.entries);
78104
const entries = data.entries
79-
.filter((entry) => entry.pageref === currPage.id)
105+
.filter((entry) => {
106+
if (pages.length === 1 && currPage.id === "") {
107+
return true;
108+
}
109+
return entry.pageref === currPage.id;
110+
})
80111
.map((entry, index) => {
81112
const startRelative = new Date(entry.startedDateTime).getTime() - pageStartTime;
82113
doneTime = Math.max(doneTime, startRelative + entry.time);

0 commit comments

Comments
 (0)