harTracer.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.HarTracer = void 0;
  6. var _browserContext = require("../browserContext");
  7. var _fetch = require("../fetch");
  8. var _helper = require("../helper");
  9. var network = _interopRequireWildcard(require("../network"));
  10. var _utils = require("../../utils");
  11. var _eventsHelper = require("../../utils/eventsHelper");
  12. var _utilsBundle = require("../../utilsBundle");
  13. var _manualPromise = require("../../utils/manualPromise");
  14. var _userAgent = require("../../utils/userAgent");
  15. var _network2 = require("../../utils/network");
  16. var _frames = require("../frames");
  17. var _mimeType = require("../../utils/mimeType");
  18. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  19. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  20. /**
  21. * Copyright (c) Microsoft Corporation.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. */
  35. const FALLBACK_HTTP_VERSION = 'HTTP/1.1';
  36. class HarTracer {
  37. constructor(context, page, delegate, options) {
  38. this._context = void 0;
  39. this._barrierPromises = new Set();
  40. this._delegate = void 0;
  41. this._options = void 0;
  42. this._pageEntries = new Map();
  43. this._eventListeners = [];
  44. this._started = false;
  45. this._entrySymbol = void 0;
  46. this._baseURL = void 0;
  47. this._page = void 0;
  48. this._context = context;
  49. this._page = page;
  50. this._delegate = delegate;
  51. this._options = options;
  52. if (options.slimMode) {
  53. options.omitSecurityDetails = true;
  54. options.omitCookies = true;
  55. options.omitTiming = true;
  56. options.omitServerIP = true;
  57. options.omitSizes = true;
  58. options.omitPages = true;
  59. }
  60. this._entrySymbol = Symbol('requestHarEntry');
  61. this._baseURL = context instanceof _fetch.APIRequestContext ? context._defaultOptions().baseURL : context._options.baseURL;
  62. }
  63. start(options) {
  64. if (this._started) return;
  65. this._options.omitScripts = options.omitScripts;
  66. this._started = true;
  67. const apiRequest = this._context instanceof _fetch.APIRequestContext ? this._context : this._context.fetchRequest;
  68. this._eventListeners = [_eventsHelper.eventsHelper.addEventListener(apiRequest, _fetch.APIRequestContext.Events.Request, event => this._onAPIRequest(event)), _eventsHelper.eventsHelper.addEventListener(apiRequest, _fetch.APIRequestContext.Events.RequestFinished, event => this._onAPIRequestFinished(event))];
  69. if (this._context instanceof _browserContext.BrowserContext) {
  70. this._eventListeners.push(_eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.Page, page => this._createPageEntryIfNeeded(page)), _eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.Request, request => this._onRequest(request)), _eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.RequestFinished, ({
  71. request,
  72. response
  73. }) => this._onRequestFinished(request, response).catch(() => {})), _eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.RequestFailed, request => this._onRequestFailed(request)), _eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.Response, response => this._onResponse(response)), _eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.RequestAborted, request => this._onRequestAborted(request)), _eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.RequestFulfilled, request => this._onRequestFulfilled(request)), _eventsHelper.eventsHelper.addEventListener(this._context, _browserContext.BrowserContext.Events.RequestContinued, request => this._onRequestContinued(request)));
  74. }
  75. }
  76. _shouldIncludeEntryWithUrl(urlString) {
  77. return !this._options.urlFilter || (0, _network2.urlMatches)(this._baseURL, urlString, this._options.urlFilter);
  78. }
  79. _entryForRequest(request) {
  80. return request[this._entrySymbol];
  81. }
  82. _createPageEntryIfNeeded(page) {
  83. if (!page) return;
  84. if (this._options.omitPages) return;
  85. if (this._page && page !== this._page) return;
  86. let pageEntry = this._pageEntries.get(page);
  87. if (!pageEntry) {
  88. const date = new Date();
  89. pageEntry = {
  90. startedDateTime: date.toISOString(),
  91. id: page.guid,
  92. title: '',
  93. pageTimings: this._options.omitTiming ? {} : {
  94. onContentLoad: -1,
  95. onLoad: -1
  96. }
  97. };
  98. pageEntry[startedDateSymbol] = date;
  99. page.mainFrame().on(_frames.Frame.Events.AddLifecycle, event => {
  100. if (event === 'load') this._onLoad(page, pageEntry);
  101. if (event === 'domcontentloaded') this._onDOMContentLoaded(page, pageEntry);
  102. });
  103. this._pageEntries.set(page, pageEntry);
  104. }
  105. return pageEntry;
  106. }
  107. _onDOMContentLoaded(page, pageEntry) {
  108. const promise = page.mainFrame().evaluateExpression(String(() => {
  109. return {
  110. title: document.title,
  111. domContentLoaded: performance.timing.domContentLoadedEventStart
  112. };
  113. }), {
  114. isFunction: true,
  115. world: 'utility'
  116. }).then(result => {
  117. pageEntry.title = result.title;
  118. if (!this._options.omitTiming) pageEntry.pageTimings.onContentLoad = result.domContentLoaded;
  119. }).catch(() => {});
  120. this._addBarrier(page, promise);
  121. }
  122. _onLoad(page, pageEntry) {
  123. const promise = page.mainFrame().evaluateExpression(String(() => {
  124. return {
  125. title: document.title,
  126. loaded: performance.timing.loadEventStart
  127. };
  128. }), {
  129. isFunction: true,
  130. world: 'utility'
  131. }).then(result => {
  132. pageEntry.title = result.title;
  133. if (!this._options.omitTiming) pageEntry.pageTimings.onLoad = result.loaded;
  134. }).catch(() => {});
  135. this._addBarrier(page, promise);
  136. }
  137. _addBarrier(target, promise) {
  138. if (!target) return null;
  139. if (!this._options.waitForContentOnStop) return;
  140. const race = target.openScope.safeRace(promise);
  141. this._barrierPromises.add(race);
  142. race.then(() => this._barrierPromises.delete(race));
  143. }
  144. _onAPIRequest(event) {
  145. var _event$postData;
  146. if (!this._shouldIncludeEntryWithUrl(event.url.toString())) return;
  147. const harEntry = createHarEntry(event.method, event.url, undefined, this._options);
  148. harEntry._apiRequest = true;
  149. if (!this._options.omitCookies) harEntry.request.cookies = event.cookies;
  150. harEntry.request.headers = Object.entries(event.headers).map(([name, value]) => ({
  151. name,
  152. value
  153. }));
  154. harEntry.request.postData = this._postDataForBuffer(event.postData || null, event.headers['content-type'], this._options.content);
  155. if (!this._options.omitSizes) harEntry.request.bodySize = ((_event$postData = event.postData) === null || _event$postData === void 0 ? void 0 : _event$postData.length) || 0;
  156. event[this._entrySymbol] = harEntry;
  157. if (this._started) this._delegate.onEntryStarted(harEntry);
  158. }
  159. _onAPIRequestFinished(event) {
  160. const harEntry = this._entryForRequest(event.requestEvent);
  161. if (!harEntry) return;
  162. harEntry.response.status = event.statusCode;
  163. harEntry.response.statusText = event.statusMessage;
  164. harEntry.response.httpVersion = event.httpVersion;
  165. harEntry.response.redirectURL = event.headers.location || '';
  166. for (let i = 0; i < event.rawHeaders.length; i += 2) {
  167. harEntry.response.headers.push({
  168. name: event.rawHeaders[i],
  169. value: event.rawHeaders[i + 1]
  170. });
  171. }
  172. harEntry.response.cookies = this._options.omitCookies ? [] : event.cookies.map(c => {
  173. return {
  174. ...c,
  175. expires: c.expires === -1 ? undefined : safeDateToISOString(c.expires)
  176. };
  177. });
  178. const content = harEntry.response.content;
  179. const contentType = event.headers['content-type'];
  180. if (contentType) content.mimeType = contentType;
  181. this._storeResponseContent(event.body, content, 'other');
  182. if (this._started) this._delegate.onEntryFinished(harEntry);
  183. }
  184. _onRequest(request) {
  185. var _request$frame, _request$frame2;
  186. if (!this._shouldIncludeEntryWithUrl(request.url())) return;
  187. const page = (_request$frame = request.frame()) === null || _request$frame === void 0 ? void 0 : _request$frame._page;
  188. if (this._page && page !== this._page) return;
  189. const url = network.parsedURL(request.url());
  190. if (!url) return;
  191. const pageEntry = this._createPageEntryIfNeeded(page);
  192. const harEntry = createHarEntry(request.method(), url, (_request$frame2 = request.frame()) === null || _request$frame2 === void 0 ? void 0 : _request$frame2.guid, this._options);
  193. if (pageEntry) harEntry.pageref = pageEntry.id;
  194. this._recordRequestHeadersAndCookies(harEntry, request.headers());
  195. harEntry.request.postData = this._postDataForRequest(request, this._options.content);
  196. if (!this._options.omitSizes) harEntry.request.bodySize = request.bodySize();
  197. if (request.redirectedFrom()) {
  198. const fromEntry = this._entryForRequest(request.redirectedFrom());
  199. if (fromEntry) fromEntry.response.redirectURL = request.url();
  200. }
  201. request[this._entrySymbol] = harEntry;
  202. (0, _utils.assert)(this._started);
  203. this._delegate.onEntryStarted(harEntry);
  204. }
  205. _recordRequestHeadersAndCookies(harEntry, headers) {
  206. if (!this._options.omitCookies) {
  207. harEntry.request.cookies = [];
  208. for (const header of headers.filter(header => header.name.toLowerCase() === 'cookie')) harEntry.request.cookies.push(...header.value.split(';').map(parseCookie));
  209. }
  210. harEntry.request.headers = headers;
  211. }
  212. _recordRequestOverrides(harEntry, request) {
  213. if (!request._hasOverrides() || !this._options.recordRequestOverrides) return;
  214. harEntry.request.method = request.method();
  215. harEntry.request.url = request.url();
  216. harEntry.request.postData = this._postDataForRequest(request, this._options.content);
  217. this._recordRequestHeadersAndCookies(harEntry, request.headers());
  218. }
  219. async _onRequestFinished(request, response) {
  220. var _request$frame3;
  221. if (!response) return;
  222. const harEntry = this._entryForRequest(request);
  223. if (!harEntry) return;
  224. const page = (_request$frame3 = request.frame()) === null || _request$frame3 === void 0 ? void 0 : _request$frame3._page;
  225. // In WebKit security details and server ip are reported in Network.loadingFinished, so we populate
  226. // it here to not hang in case of long chunked responses, see https://github.com/microsoft/playwright/issues/21182.
  227. if (!this._options.omitServerIP) {
  228. this._addBarrier(page || request.serviceWorker(), response.serverAddr().then(server => {
  229. if (server !== null && server !== void 0 && server.ipAddress) harEntry.serverIPAddress = server.ipAddress;
  230. if (server !== null && server !== void 0 && server.port) harEntry._serverPort = server.port;
  231. }));
  232. }
  233. if (!this._options.omitSecurityDetails) {
  234. this._addBarrier(page || request.serviceWorker(), response.securityDetails().then(details => {
  235. if (details) harEntry._securityDetails = details;
  236. }));
  237. }
  238. const httpVersion = response.httpVersion();
  239. harEntry.request.httpVersion = httpVersion;
  240. harEntry.response.httpVersion = httpVersion;
  241. const compressionCalculationBarrier = this._options.omitSizes ? undefined : {
  242. _encodedBodySize: -1,
  243. _decodedBodySize: -1,
  244. barrier: new _manualPromise.ManualPromise(),
  245. _check: function () {
  246. if (this._encodedBodySize !== -1 && this._decodedBodySize !== -1) {
  247. harEntry.response.content.compression = Math.max(0, this._decodedBodySize - this._encodedBodySize);
  248. this.barrier.resolve();
  249. }
  250. },
  251. setEncodedBodySize: function (encodedBodySize) {
  252. this._encodedBodySize = encodedBodySize;
  253. this._check();
  254. },
  255. setDecodedBodySize: function (decodedBodySize) {
  256. this._decodedBodySize = decodedBodySize;
  257. this._check();
  258. }
  259. };
  260. if (compressionCalculationBarrier) this._addBarrier(page || request.serviceWorker(), compressionCalculationBarrier.barrier);
  261. const promise = response.body().then(buffer => {
  262. if (this._options.omitScripts && request.resourceType() === 'script') {
  263. compressionCalculationBarrier === null || compressionCalculationBarrier === void 0 ? void 0 : compressionCalculationBarrier.setDecodedBodySize(0);
  264. return;
  265. }
  266. const content = harEntry.response.content;
  267. compressionCalculationBarrier === null || compressionCalculationBarrier === void 0 ? void 0 : compressionCalculationBarrier.setDecodedBodySize(buffer.length);
  268. this._storeResponseContent(buffer, content, request.resourceType());
  269. }).catch(() => {
  270. compressionCalculationBarrier === null || compressionCalculationBarrier === void 0 ? void 0 : compressionCalculationBarrier.setDecodedBodySize(0);
  271. }).then(() => {
  272. if (this._started) this._delegate.onEntryFinished(harEntry);
  273. });
  274. this._addBarrier(page || request.serviceWorker(), promise);
  275. // Respose end timing is only available after the response event was received.
  276. const timing = response.timing();
  277. harEntry.timings.receive = response.request()._responseEndTiming !== -1 ? _helper.helper.millisToRoundishMillis(response.request()._responseEndTiming - timing.responseStart) : -1;
  278. this._computeHarEntryTotalTime(harEntry);
  279. if (!this._options.omitSizes) {
  280. this._addBarrier(page || request.serviceWorker(), response.sizes().then(sizes => {
  281. harEntry.response.bodySize = sizes.responseBodySize;
  282. harEntry.response.headersSize = sizes.responseHeadersSize;
  283. harEntry.response._transferSize = sizes.transferSize;
  284. harEntry.request.headersSize = sizes.requestHeadersSize;
  285. compressionCalculationBarrier === null || compressionCalculationBarrier === void 0 ? void 0 : compressionCalculationBarrier.setEncodedBodySize(sizes.responseBodySize);
  286. }));
  287. }
  288. }
  289. async _onRequestFailed(request) {
  290. const harEntry = this._entryForRequest(request);
  291. if (!harEntry) return;
  292. if (request._failureText !== null) harEntry.response._failureText = request._failureText;
  293. this._recordRequestOverrides(harEntry, request);
  294. if (this._started) this._delegate.onEntryFinished(harEntry);
  295. }
  296. _onRequestAborted(request) {
  297. const harEntry = this._entryForRequest(request);
  298. if (harEntry) harEntry._wasAborted = true;
  299. }
  300. _onRequestFulfilled(request) {
  301. const harEntry = this._entryForRequest(request);
  302. if (harEntry) harEntry._wasFulfilled = true;
  303. }
  304. _onRequestContinued(request) {
  305. const harEntry = this._entryForRequest(request);
  306. if (harEntry) harEntry._wasContinued = true;
  307. }
  308. _storeResponseContent(buffer, content, resourceType) {
  309. if (!buffer) {
  310. content.size = 0;
  311. return;
  312. }
  313. if (!this._options.omitSizes) content.size = buffer.length;
  314. if (this._options.content === 'embed') {
  315. // Sometimes, we can receive a font/media file with textual mime type. Browser
  316. // still interprets them correctly, but the 'content-type' header is obviously wrong.
  317. if ((0, _mimeType.isTextualMimeType)(content.mimeType) && resourceType !== 'font') {
  318. content.text = buffer.toString();
  319. } else {
  320. content.text = buffer.toString('base64');
  321. content.encoding = 'base64';
  322. }
  323. } else if (this._options.content === 'attach') {
  324. const sha1 = (0, _utils.calculateSha1)(buffer) + '.' + (_utilsBundle.mime.getExtension(content.mimeType) || 'dat');
  325. if (this._options.includeTraceInfo) content._sha1 = sha1;else content._file = sha1;
  326. if (this._started) this._delegate.onContentBlob(sha1, buffer);
  327. }
  328. }
  329. _onResponse(response) {
  330. var _response$frame;
  331. const harEntry = this._entryForRequest(response.request());
  332. if (!harEntry) return;
  333. const page = (_response$frame = response.frame()) === null || _response$frame === void 0 ? void 0 : _response$frame._page;
  334. const pageEntry = this._createPageEntryIfNeeded(page);
  335. const request = response.request();
  336. // Prefer "response received" time over "request sent" time
  337. // for the purpose of matching requests that were used in a particular snapshot.
  338. // Note that both snapshot time and request time are taken here in the Node process.
  339. if (this._options.includeTraceInfo) harEntry._monotonicTime = (0, _utils.monotonicTime)();
  340. harEntry.response = {
  341. status: response.status(),
  342. statusText: response.statusText(),
  343. httpVersion: response.httpVersion(),
  344. // These are bad values that will be overwritten bellow.
  345. cookies: [],
  346. headers: [],
  347. content: {
  348. size: -1,
  349. mimeType: 'x-unknown'
  350. },
  351. headersSize: -1,
  352. bodySize: -1,
  353. redirectURL: '',
  354. _transferSize: this._options.omitSizes ? undefined : -1
  355. };
  356. if (!this._options.omitTiming) {
  357. const startDateTime = pageEntry ? pageEntry[startedDateSymbol].valueOf() : 0;
  358. const timing = response.timing();
  359. if (pageEntry && startDateTime > timing.startTime) pageEntry.startedDateTime = new Date(timing.startTime).toISOString();
  360. const dns = timing.domainLookupEnd !== -1 ? _helper.helper.millisToRoundishMillis(timing.domainLookupEnd - timing.domainLookupStart) : -1;
  361. const connect = timing.connectEnd !== -1 ? _helper.helper.millisToRoundishMillis(timing.connectEnd - timing.connectStart) : -1;
  362. const ssl = timing.connectEnd !== -1 ? _helper.helper.millisToRoundishMillis(timing.connectEnd - timing.secureConnectionStart) : -1;
  363. const wait = timing.responseStart !== -1 ? _helper.helper.millisToRoundishMillis(timing.responseStart - timing.requestStart) : -1;
  364. const receive = -1;
  365. harEntry.timings = {
  366. dns,
  367. connect,
  368. ssl,
  369. send: 0,
  370. wait,
  371. receive
  372. };
  373. this._computeHarEntryTotalTime(harEntry);
  374. }
  375. this._recordRequestOverrides(harEntry, request);
  376. this._addBarrier(page || request.serviceWorker(), request.rawRequestHeaders().then(headers => {
  377. this._recordRequestHeadersAndCookies(harEntry, headers);
  378. }));
  379. // Record available headers including redirect location in case the tracing is stopped before
  380. // response extra info is received (in Chromium).
  381. this._recordResponseHeaders(harEntry, response.headers());
  382. this._addBarrier(page || request.serviceWorker(), response.rawResponseHeaders().then(headers => {
  383. this._recordResponseHeaders(harEntry, headers);
  384. }));
  385. }
  386. _recordResponseHeaders(harEntry, headers) {
  387. if (!this._options.omitCookies) {
  388. harEntry.response.cookies = headers.filter(header => header.name.toLowerCase() === 'set-cookie').map(header => parseCookie(header.value));
  389. }
  390. harEntry.response.headers = headers;
  391. const contentType = headers.find(header => header.name.toLowerCase() === 'content-type');
  392. if (contentType) harEntry.response.content.mimeType = contentType.value;
  393. }
  394. _computeHarEntryTotalTime(harEntry) {
  395. harEntry.time = [harEntry.timings.dns, harEntry.timings.connect, harEntry.timings.ssl, harEntry.timings.wait, harEntry.timings.receive].reduce((pre, cur) => (cur || -1) > 0 ? cur + pre : pre, 0);
  396. }
  397. async flush() {
  398. await Promise.all(this._barrierPromises);
  399. }
  400. stop() {
  401. this._started = false;
  402. _eventsHelper.eventsHelper.removeEventListeners(this._eventListeners);
  403. this._barrierPromises.clear();
  404. const context = this._context instanceof _browserContext.BrowserContext ? this._context : undefined;
  405. const log = {
  406. version: '1.2',
  407. creator: {
  408. name: 'Playwright',
  409. version: (0, _userAgent.getPlaywrightVersion)()
  410. },
  411. browser: {
  412. name: (context === null || context === void 0 ? void 0 : context._browser.options.name) || '',
  413. version: (context === null || context === void 0 ? void 0 : context._browser.version()) || ''
  414. },
  415. pages: this._pageEntries.size ? Array.from(this._pageEntries.values()) : undefined,
  416. entries: []
  417. };
  418. if (!this._options.omitTiming) {
  419. for (const pageEntry of log.pages || []) {
  420. const startDateTime = pageEntry[startedDateSymbol].valueOf();
  421. if (typeof pageEntry.pageTimings.onContentLoad === 'number' && pageEntry.pageTimings.onContentLoad >= 0) pageEntry.pageTimings.onContentLoad -= startDateTime;else pageEntry.pageTimings.onContentLoad = -1;
  422. if (typeof pageEntry.pageTimings.onLoad === 'number' && pageEntry.pageTimings.onLoad >= 0) pageEntry.pageTimings.onLoad -= startDateTime;else pageEntry.pageTimings.onLoad = -1;
  423. }
  424. }
  425. this._pageEntries.clear();
  426. return log;
  427. }
  428. _postDataForRequest(request, content) {
  429. const postData = request.postDataBuffer();
  430. if (!postData) return;
  431. const contentType = request.headerValue('content-type');
  432. return this._postDataForBuffer(postData, contentType, content);
  433. }
  434. _postDataForBuffer(postData, contentType, content) {
  435. var _contentType;
  436. if (!postData) return;
  437. (_contentType = contentType) !== null && _contentType !== void 0 ? _contentType : contentType = 'application/octet-stream';
  438. const result = {
  439. mimeType: contentType,
  440. text: '',
  441. params: []
  442. };
  443. if (content === 'embed' && contentType !== 'application/octet-stream') result.text = postData.toString();
  444. if (content === 'attach') {
  445. const sha1 = (0, _utils.calculateSha1)(postData) + '.' + (_utilsBundle.mime.getExtension(contentType) || 'dat');
  446. if (this._options.includeTraceInfo) result._sha1 = sha1;else result._file = sha1;
  447. this._delegate.onContentBlob(sha1, postData);
  448. }
  449. if (contentType === 'application/x-www-form-urlencoded') {
  450. const parsed = new URLSearchParams(postData.toString());
  451. for (const [name, value] of parsed.entries()) result.params.push({
  452. name,
  453. value
  454. });
  455. }
  456. return result;
  457. }
  458. }
  459. exports.HarTracer = HarTracer;
  460. function createHarEntry(method, url, frameref, options) {
  461. const harEntry = {
  462. _frameref: options.includeTraceInfo ? frameref : undefined,
  463. _monotonicTime: options.includeTraceInfo ? (0, _utils.monotonicTime)() : undefined,
  464. startedDateTime: new Date().toISOString(),
  465. time: -1,
  466. request: {
  467. method: method,
  468. url: url.toString(),
  469. httpVersion: FALLBACK_HTTP_VERSION,
  470. cookies: [],
  471. headers: [],
  472. queryString: [...url.searchParams].map(e => ({
  473. name: e[0],
  474. value: e[1]
  475. })),
  476. headersSize: -1,
  477. bodySize: -1
  478. },
  479. response: {
  480. status: -1,
  481. statusText: '',
  482. httpVersion: FALLBACK_HTTP_VERSION,
  483. cookies: [],
  484. headers: [],
  485. content: {
  486. size: -1,
  487. mimeType: 'x-unknown'
  488. },
  489. headersSize: -1,
  490. bodySize: -1,
  491. redirectURL: '',
  492. _transferSize: options.omitSizes ? undefined : -1
  493. },
  494. cache: {},
  495. timings: {
  496. send: -1,
  497. wait: -1,
  498. receive: -1
  499. }
  500. };
  501. return harEntry;
  502. }
  503. function parseCookie(c) {
  504. const cookie = {
  505. name: '',
  506. value: ''
  507. };
  508. let first = true;
  509. for (const pair of c.split(/; */)) {
  510. const indexOfEquals = pair.indexOf('=');
  511. const name = indexOfEquals !== -1 ? pair.substr(0, indexOfEquals).trim() : pair.trim();
  512. const value = indexOfEquals !== -1 ? pair.substr(indexOfEquals + 1, pair.length).trim() : '';
  513. if (first) {
  514. first = false;
  515. cookie.name = name;
  516. cookie.value = value;
  517. continue;
  518. }
  519. if (name === 'Domain') cookie.domain = value;
  520. if (name === 'Expires') cookie.expires = safeDateToISOString(value);
  521. if (name === 'HttpOnly') cookie.httpOnly = true;
  522. if (name === 'Max-Age') cookie.expires = safeDateToISOString(Date.now() + +value * 1000);
  523. if (name === 'Path') cookie.path = value;
  524. if (name === 'SameSite') cookie.sameSite = value;
  525. if (name === 'Secure') cookie.secure = true;
  526. }
  527. return cookie;
  528. }
  529. function safeDateToISOString(value) {
  530. try {
  531. return new Date(value).toISOString();
  532. } catch (e) {}
  533. }
  534. const startedDateSymbol = Symbol('startedDate');