summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/har/har-importer.js
blob: 2246a29086a5c20cb8a2b96431eefeb33184bc83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  TIMING_KEYS,
} = require("resource://devtools/client/netmonitor/src/constants.js");
const {
  getUrlDetails,
} = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");

var guid = 0;

/**
 * This object is responsible for importing HAR file. See HAR spec:
 * https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html
 * http://www.softwareishard.com/blog/har-12-spec/
 */
var HarImporter = function (actions) {
  this.actions = actions;
};

HarImporter.prototype = {
  /**
   * This is the main method used to import HAR data.
   */
  import(har) {
    const json = JSON.parse(har);
    this.doImport(json);
  },

  doImport(har) {
    this.actions.clearRequests();

    // Helper map for pages.
    const pages = new Map();
    har.log.pages.forEach(page => {
      pages.set(page.id, page);
    });

    // Iterate all entries/requests and generate state.
    har.log.entries.forEach(entry => {
      const requestId = String(++guid);
      const startedMs = Date.parse(entry.startedDateTime);

      // Add request
      this.actions.addRequest(
        requestId,
        {
          startedMs,
          method: entry.request.method,
          url: entry.request.url,
          urlDetails: getUrlDetails(entry.request.url),
          isXHR: false,
          cause: {
            loadingDocumentUri: "",
            stackTraceAvailable: false,
            type: "",
          },
          fromCache: false,
          fromServiceWorker: false,
        },
        false
      );

      // Update request
      const data = {
        requestHeaders: {
          headers: entry.request.headers,
          headersSize: entry.request.headersSize,
          rawHeaders: "",
        },
        responseHeaders: {
          headers: entry.response.headers,
          headersSize: entry.response.headersSize,
          rawHeaders: "",
        },
        requestCookies: entry.request.cookies,
        responseCookies: entry.response.cookies,
        requestPostData: {
          postData: entry.request.postData || {},
          postDataDiscarded: false,
        },
        responseContent: {
          content: entry.response.content,
          contentDiscarded: false,
        },
        eventTimings: {
          timings: entry.timings,
        },
        totalTime: TIMING_KEYS.reduce((sum, type) => {
          const time = entry.timings[type];
          return typeof time != "undefined" && time != -1 ? sum + time : sum;
        }, 0),

        httpVersion: entry.request.httpVersion,
        contentSize: entry.response.content.size,
        mimeType: entry.response.content.mimeType,
        remoteAddress: entry.serverIPAddress,
        remotePort: entry.connection,
        status: entry.response.status,
        statusText: entry.response.statusText,
        transferredSize: entry.response.bodySize,
        securityState: entry._securityState,

        // Avoid auto-fetching data from the backend
        eventTimingsAvailable: false,
        requestCookiesAvailable: false,
        requestHeadersAvailable: false,
        responseContentAvailable: false,
        responseStartAvailable: false,
        responseCookiesAvailable: false,
        responseHeadersAvailable: false,
        securityInfoAvailable: false,
        requestPostDataAvailable: false,
      };

      if (entry.cache.afterRequest) {
        const { afterRequest } = entry.cache;
        data.responseCache = {
          cache: {
            expires: afterRequest.expires,
            fetchCount: afterRequest.fetchCount,
            lastFetched: afterRequest.lastFetched,
            // TODO: eTag support, see Bug 1799844.
            // eTag: afterRequest.eTag,
            _dataSize: afterRequest._dataSize,
            _lastModified: afterRequest._lastModified,
            _device: afterRequest._device,
          },
        };
      }

      this.actions.updateRequest(requestId, data, false);

      // Page timing markers
      const pageTimings = pages.get(entry.pageref)?.pageTimings;
      let onContentLoad = (pageTimings && pageTimings.onContentLoad) || 0;
      let onLoad = (pageTimings && pageTimings.onLoad) || 0;

      // Set 0 as the default value
      onContentLoad = onContentLoad != -1 ? onContentLoad : 0;
      onLoad = onLoad != -1 ? onLoad : 0;

      // Add timing markers
      if (onContentLoad > 0) {
        this.actions.addTimingMarker({
          name: "dom-interactive",
          time: startedMs + onContentLoad,
        });
      }

      if (onLoad > 0) {
        this.actions.addTimingMarker({
          name: "dom-complete",
          time: startedMs + onLoad,
        });
      }
    });
  },
};

// Exports from this module
exports.HarImporter = HarImporter;