Skip to content

Commit 439463c

Browse files
Merge pull request #28 from juliandescottes/data-urls
Filter out data URLs from HAR generation
2 parents 877c848 + f0bc412 commit 439463c

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/har-recorder.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ class HarRecorder {
310310
return undefined;
311311
}
312312

313+
_isDataURL(url) {
314+
return url.startsWith("data:");
315+
}
316+
313317
_log(message) {
314318
if (this._debugLogs) {
315319
console.log(`[har-recorder] ${message}`);
@@ -320,6 +324,13 @@ class HarRecorder {
320324
const id = params.request.request + "-" + params.redirectCount;
321325
const url = params.request.url;
322326

327+
if (this._isDataURL(url)) {
328+
// Currently timings for data URLs are completely incorrect and it is not
329+
// clear if data URLs are relevant for HAR export.
330+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1916651
331+
return;
332+
}
333+
323334
this._log(
324335
`Event "beforeRequestSent" for url: ${this._shortUrl(url)} (id: ${id})`,
325336
);
@@ -439,6 +450,14 @@ class HarRecorder {
439450
_onResponseCompleted(params) {
440451
const id = params.request.request + "-" + params.redirectCount;
441452
const url = params.request.url;
453+
454+
if (this._isDataURL(url)) {
455+
// Currently timings for data URLs are completely incorrect and it is not
456+
// clear if data URLs are relevant for HAR export.
457+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1916651
458+
return;
459+
}
460+
442461
this._log(
443462
`Event "responseCompleted" for url: ${this._shortUrl(url)} (id: ${id})`,
444463
);

test/har-recorder-dataurl.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
* http://creativecommons.org/publicdomain/zero/1.0/ */
3+
4+
const { HarRecorder } = require("..");
5+
const { getMockEvents } = require("./resources/mock-events");
6+
7+
test("HarRecorder generates har export without data URLs", () => {
8+
const recorder = new HarRecorder({ browser: "browser", version: "version" });
9+
10+
recorder.startRecording();
11+
12+
const requestId = 33;
13+
const startTime = Date.now();
14+
const {
15+
beforeRequestSentEvent,
16+
domContentLoadedEvent,
17+
loadEvent,
18+
responseCompletedEvent,
19+
} = getMockEvents(startTime, { requestId });
20+
21+
// Create 1 additional event for a data url, starting 10 ms before the "main" events.
22+
const dataUrlEvents = getMockEvents(startTime - 10);
23+
const dataUrl = "data:text/html,foo";
24+
dataUrlEvents.beforeRequestSentEvent.params.request.url = dataUrl;
25+
dataUrlEvents.responseCompletedEvent.params.request.url = dataUrl;
26+
27+
recorder.recordEvent(dataUrlEvents.beforeRequestSentEvent);
28+
recorder.recordEvent(dataUrlEvents.responseCompletedEvent);
29+
recorder.recordEvent(beforeRequestSentEvent);
30+
recorder.recordEvent(responseCompletedEvent);
31+
recorder.recordEvent(domContentLoadedEvent);
32+
recorder.recordEvent(loadEvent);
33+
34+
const harExport = recorder.stopRecording();
35+
36+
expect(harExport).toBeDefined();
37+
expect(harExport.log).toBeDefined();
38+
39+
// We should only have one page and one entry, data URL event should be
40+
// filtered out.
41+
expect(harExport.log.pages.length).toBe(1);
42+
expect(harExport.log.entries.length).toBe(1);
43+
});

0 commit comments

Comments
 (0)