lighthouse 12.0.0 → 12.1.0-dev.20240618

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/cli/run.js +5 -38
  2. package/cli/test/smokehouse/lighthouse-runners/bundle.js +1 -1
  3. package/cli/test/smokehouse/smokehouse.js +11 -1
  4. package/core/audits/accessibility/aria-allowed-role.js +7 -9
  5. package/core/audits/byte-efficiency/byte-efficiency-audit.d.ts +2 -2
  6. package/core/audits/byte-efficiency/byte-efficiency-audit.js +3 -3
  7. package/core/audits/byte-efficiency/offscreen-images.js +1 -1
  8. package/core/audits/byte-efficiency/render-blocking-resources.d.ts +3 -3
  9. package/core/audits/byte-efficiency/render-blocking-resources.js +10 -10
  10. package/core/audits/dobetterweb/uses-http2.d.ts +2 -2
  11. package/core/audits/dobetterweb/uses-http2.js +5 -5
  12. package/core/audits/long-tasks.d.ts +7 -6
  13. package/core/audits/long-tasks.js +5 -4
  14. package/core/audits/metrics/first-meaningful-paint.d.ts +1 -17
  15. package/core/audits/metrics/first-meaningful-paint.js +5 -47
  16. package/core/audits/metrics/interactive.d.ts +1 -1
  17. package/core/audits/metrics/interactive.js +1 -1
  18. package/core/audits/predictive-perf.js +0 -6
  19. package/core/audits/prioritize-lcp-image.d.ts +1 -1
  20. package/core/audits/prioritize-lcp-image.js +2 -2
  21. package/core/audits/redirects.js +2 -3
  22. package/core/audits/third-party-facades.js +1 -1
  23. package/core/audits/third-party-summary.js +1 -1
  24. package/core/audits/uses-rel-preconnect.js +2 -2
  25. package/core/audits/uses-rel-preload.js +9 -9
  26. package/core/computed/critical-request-chains.js +3 -3
  27. package/core/computed/document-urls.js +3 -2
  28. package/core/computed/load-simulator.d.ts +4 -4
  29. package/core/computed/load-simulator.js +3 -54
  30. package/core/computed/main-resource.js +3 -2
  31. package/core/computed/metrics/interactive.d.ts +6 -0
  32. package/core/computed/metrics/interactive.js +2 -2
  33. package/core/computed/metrics/lantern-first-contentful-paint.d.ts +4 -4
  34. package/core/computed/metrics/lantern-first-contentful-paint.js +3 -3
  35. package/core/computed/metrics/lantern-interactive.d.ts +4 -4
  36. package/core/computed/metrics/lantern-interactive.js +6 -6
  37. package/core/computed/metrics/lantern-largest-contentful-paint.d.ts +4 -4
  38. package/core/computed/metrics/lantern-largest-contentful-paint.js +3 -3
  39. package/core/computed/metrics/lantern-max-potential-fid.d.ts +4 -4
  40. package/core/computed/metrics/lantern-max-potential-fid.js +3 -3
  41. package/core/computed/metrics/lantern-metric.d.ts +21 -2
  42. package/core/computed/metrics/lantern-metric.js +39 -4
  43. package/core/computed/metrics/lantern-speed-index.d.ts +4 -4
  44. package/core/computed/metrics/lantern-speed-index.js +4 -4
  45. package/core/computed/metrics/lantern-total-blocking-time.d.ts +4 -4
  46. package/core/computed/metrics/lantern-total-blocking-time.js +3 -3
  47. package/core/computed/metrics/timing-summary.js +0 -6
  48. package/core/computed/metrics/total-blocking-time.js +3 -1
  49. package/core/computed/navigation-insights.d.ts +2 -20
  50. package/core/computed/network-analysis.d.ts +0 -5
  51. package/core/computed/network-analysis.js +2 -42
  52. package/core/computed/page-dependency-graph.d.ts +5 -3
  53. package/core/computed/page-dependency-graph.js +17 -7
  54. package/core/computed/processed-navigation.d.ts +1 -1
  55. package/core/computed/tbt-impact-tasks.js +4 -2
  56. package/core/computed/trace-engine-result.d.ts +0 -11
  57. package/core/computed/trace-engine-result.js +5 -19
  58. package/core/config/constants.d.ts +28 -53
  59. package/core/config/constants.js +2 -43
  60. package/core/gather/driver/navigation.js +1 -1
  61. package/core/gather/driver/network-monitor.d.ts +2 -1
  62. package/core/gather/driver/network-monitor.js +4 -4
  63. package/core/gather/driver/target-manager.d.ts +1 -1
  64. package/core/gather/driver.d.ts +0 -12
  65. package/core/gather/driver.js +1 -26
  66. package/core/gather/gatherers/inspector-issues.js +1 -0
  67. package/core/gather/gatherers/root-causes.js +0 -1
  68. package/core/gather/session.d.ts +5 -3
  69. package/core/gather/session.js +22 -1
  70. package/core/lib/asset-saver.d.ts +13 -9
  71. package/core/lib/asset-saver.js +77 -30
  72. package/core/lib/bf-cache-strings.js +2 -2
  73. package/core/lib/lantern/{base-node.d.ts → BaseNode.d.ts} +7 -9
  74. package/core/lib/lantern/{base-node.js → BaseNode.js} +6 -6
  75. package/core/lib/lantern/BaseNode.test.js +385 -0
  76. package/core/lib/lantern/CpuNode.d.ts +43 -0
  77. package/core/lib/lantern/{cpu-node.js → CpuNode.js} +8 -9
  78. package/core/lib/lantern/{lantern-error.d.ts → LanternError.d.ts} +1 -1
  79. package/core/lib/lantern/{metric.d.ts → Metric.d.ts} +23 -25
  80. package/core/lib/lantern/{metric.js → Metric.js} +14 -16
  81. package/core/lib/lantern/{network-node.d.ts → NetworkNode.d.ts} +6 -7
  82. package/core/lib/lantern/{network-node.js → NetworkNode.js} +7 -9
  83. package/core/lib/lantern/PageDependencyGraph.d.ts +82 -0
  84. package/core/lib/lantern/{page-dependency-graph.js → PageDependencyGraph.js} +185 -50
  85. package/core/lib/lantern/PageDependencyGraph.test.js +654 -0
  86. package/core/{computed/metrics/tbt-utils.d.ts → lib/lantern/TBTUtils.d.ts} +1 -1
  87. package/core/lib/lantern/TBTUtils.test.d.ts +2 -0
  88. package/core/lib/lantern/TBTUtils.test.js +130 -0
  89. package/core/lib/lantern/TraceEngineComputationData.d.ts +25 -0
  90. package/core/lib/lantern/TraceEngineComputationData.js +466 -0
  91. package/core/lib/lantern/lantern.d.ts +19 -7
  92. package/core/lib/lantern/lantern.js +21 -1
  93. package/core/lib/lantern/metrics/{first-contentful-paint.d.ts → FirstContentfulPaint.d.ts} +11 -11
  94. package/core/lib/lantern/metrics/{first-contentful-paint.js → FirstContentfulPaint.js} +11 -13
  95. package/core/lib/lantern/metrics/FirstContentfulPaint.test.js +54 -0
  96. package/core/lib/lantern/metrics/Interactive.d.ts +20 -0
  97. package/core/lib/lantern/metrics/{interactive.js → Interactive.js} +23 -26
  98. package/core/lib/lantern/metrics/Interactive.test.js +56 -0
  99. package/core/lib/lantern/metrics/LargestContentfulPaint.d.ts +19 -0
  100. package/core/lib/lantern/metrics/{largest-contentful-paint.js → LargestContentfulPaint.js} +16 -19
  101. package/core/lib/lantern/metrics/LargestContentfulPaint.test.js +42 -0
  102. package/core/lib/lantern/metrics/MaxPotentialFID.d.ts +24 -0
  103. package/core/lib/lantern/metrics/{max-potential-fid.js → MaxPotentialFID.js} +12 -13
  104. package/core/lib/lantern/metrics/MetricTestUtils.d.ts +19 -0
  105. package/core/lib/lantern/metrics/MetricTestUtils.js +48 -0
  106. package/core/lib/lantern/metrics/{speed-index.d.ts → SpeedIndex.d.ts} +7 -13
  107. package/core/lib/lantern/metrics/{speed-index.js → SpeedIndex.js} +14 -16
  108. package/core/lib/lantern/metrics/SpeedIndex.test.js +83 -0
  109. package/core/lib/lantern/metrics/TotalBlockingTime.d.ts +25 -0
  110. package/core/lib/lantern/metrics/{total-blocking-time.js → TotalBlockingTime.js} +13 -16
  111. package/core/lib/lantern/metrics/__snapshots__/FirstContentfulPaint.test.js.snap +11 -0
  112. package/core/lib/lantern/metrics/__snapshots__/Interactive.test.js.snap +17 -0
  113. package/core/lib/lantern/metrics/metrics.d.ts +9 -0
  114. package/core/lib/lantern/metrics/metrics.js +16 -0
  115. package/core/lib/lantern/{simulator/connection-pool.d.ts → simulation/ConnectionPool.d.ts} +15 -15
  116. package/core/lib/lantern/{simulator/connection-pool.js → simulation/ConnectionPool.js} +25 -25
  117. package/core/lib/lantern/simulation/ConnectionPool.test.js +195 -0
  118. package/core/lib/lantern/simulation/Constants.d.ts +52 -0
  119. package/core/lib/lantern/simulation/Constants.js +48 -0
  120. package/core/lib/lantern/{simulator/dns-cache.d.ts → simulation/DNSCache.d.ts} +3 -3
  121. package/core/lib/lantern/{simulator/dns-cache.js → simulation/DNSCache.js} +2 -2
  122. package/core/lib/lantern/simulation/DNSCache.test.js +72 -0
  123. package/core/lib/lantern/{simulator/network-analyzer.d.ts → simulation/NetworkAnalyzer.d.ts} +34 -21
  124. package/core/lib/lantern/{simulator/network-analyzer.js → simulation/NetworkAnalyzer.js} +106 -57
  125. package/core/lib/lantern/simulation/NetworkAnalyzer.test.js +475 -0
  126. package/core/lib/lantern/{simulator/simulator-timing-map.d.ts → simulation/SimulationTimingMap.d.ts} +7 -7
  127. package/core/lib/lantern/{simulator/simulator-timing-map.js → simulation/SimulationTimingMap.js} +5 -6
  128. package/core/lib/lantern/{simulator/simulator.d.ts → simulation/Simulator.d.ts} +21 -19
  129. package/core/lib/lantern/{simulator/simulator.js → simulation/Simulator.js} +100 -45
  130. package/core/lib/lantern/simulation/Simulator.test.js +434 -0
  131. package/core/lib/lantern/simulation/TCPConnection.test.d.ts +2 -0
  132. package/core/lib/lantern/simulation/TCPConnection.test.js +374 -0
  133. package/core/lib/lantern/{simulator/tcp-connection.d.ts → simulation/TcpConnection.d.ts} +2 -2
  134. package/core/lib/lantern/{simulator/tcp-connection.js → simulation/TcpConnection.js} +1 -1
  135. package/core/lib/lantern/simulation/simulation.d.ts +21 -0
  136. package/core/lib/lantern/simulation/simulation.js +28 -0
  137. package/core/lib/lantern/types/lantern.d.ts +145 -20
  138. package/core/lib/lantern-trace-saver.d.ts +4 -4
  139. package/core/lib/lantern-trace-saver.js +4 -4
  140. package/core/lib/navigation-error.js +3 -3
  141. package/core/lib/network-recorder.js +2 -2
  142. package/core/lib/network-request.d.ts +29 -3
  143. package/core/lib/network-request.js +20 -13
  144. package/core/lib/tracehouse/trace-processor.d.ts +0 -2
  145. package/core/lib/tracehouse/trace-processor.js +1 -31
  146. package/core/lib/traces/metric-trace-events.js +0 -5
  147. package/dist/report/bundle.esm.js +1 -1
  148. package/dist/report/flow.js +1 -1
  149. package/dist/report/standalone.js +1 -1
  150. package/package.json +14 -13
  151. package/report/renderer/performance-category-renderer.js +1 -1
  152. package/shared/localization/locales/ar-XB.json +43 -4
  153. package/shared/localization/locales/ar.json +43 -4
  154. package/shared/localization/locales/bg.json +43 -4
  155. package/shared/localization/locales/ca.json +44 -5
  156. package/shared/localization/locales/cs.json +43 -4
  157. package/shared/localization/locales/da.json +43 -4
  158. package/shared/localization/locales/de.json +43 -4
  159. package/shared/localization/locales/el.json +43 -4
  160. package/shared/localization/locales/en-GB.json +42 -3
  161. package/shared/localization/locales/en-US.json +4 -4
  162. package/shared/localization/locales/en-XA.json +43 -4
  163. package/shared/localization/locales/en-XL.json +4 -4
  164. package/shared/localization/locales/es-419.json +43 -4
  165. package/shared/localization/locales/es.json +42 -3
  166. package/shared/localization/locales/fi.json +43 -4
  167. package/shared/localization/locales/fil.json +42 -3
  168. package/shared/localization/locales/fr.json +42 -3
  169. package/shared/localization/locales/he.json +48 -9
  170. package/shared/localization/locales/hi.json +43 -4
  171. package/shared/localization/locales/hr.json +43 -4
  172. package/shared/localization/locales/hu.json +42 -3
  173. package/shared/localization/locales/id.json +42 -3
  174. package/shared/localization/locales/it.json +43 -4
  175. package/shared/localization/locales/ja.json +42 -3
  176. package/shared/localization/locales/ko.json +43 -4
  177. package/shared/localization/locales/lt.json +42 -3
  178. package/shared/localization/locales/lv.json +43 -4
  179. package/shared/localization/locales/nl.json +44 -5
  180. package/shared/localization/locales/no.json +42 -3
  181. package/shared/localization/locales/pl.json +43 -4
  182. package/shared/localization/locales/pt-PT.json +43 -4
  183. package/shared/localization/locales/pt.json +43 -4
  184. package/shared/localization/locales/ro.json +43 -4
  185. package/shared/localization/locales/ru.json +43 -4
  186. package/shared/localization/locales/sk.json +43 -4
  187. package/shared/localization/locales/sl.json +42 -3
  188. package/shared/localization/locales/sr-Latn.json +43 -4
  189. package/shared/localization/locales/sr.json +43 -4
  190. package/shared/localization/locales/sv.json +42 -3
  191. package/shared/localization/locales/ta.json +42 -3
  192. package/shared/localization/locales/te.json +43 -4
  193. package/shared/localization/locales/th.json +44 -5
  194. package/shared/localization/locales/tr.json +43 -4
  195. package/shared/localization/locales/uk.json +43 -4
  196. package/shared/localization/locales/vi.json +43 -4
  197. package/shared/localization/locales/zh-HK.json +43 -4
  198. package/shared/localization/locales/zh-TW.json +43 -4
  199. package/shared/localization/locales/zh.json +43 -4
  200. package/third-party/chromium-synchronization/inspector-issueAdded-types-test.js +1 -0
  201. package/tsconfig-all.json +1 -0
  202. package/tsconfig.json +11 -7
  203. package/types/artifacts.d.ts +23 -25
  204. package/types/gatherer.d.ts +2 -3
  205. package/types/lhr/lhr.d.ts +2 -0
  206. package/core/computed/metrics/first-meaningful-paint.d.ts +0 -21
  207. package/core/computed/metrics/first-meaningful-paint.js +0 -44
  208. package/core/computed/metrics/lantern-first-meaningful-paint.d.ts +0 -25
  209. package/core/computed/metrics/lantern-first-meaningful-paint.js +0 -43
  210. package/core/lib/lantern/cpu-node.d.ts +0 -44
  211. package/core/lib/lantern/metrics/first-meaningful-paint.d.ts +0 -6
  212. package/core/lib/lantern/metrics/first-meaningful-paint.js +0 -64
  213. package/core/lib/lantern/metrics/interactive.d.ts +0 -26
  214. package/core/lib/lantern/metrics/largest-contentful-paint.d.ts +0 -19
  215. package/core/lib/lantern/metrics/max-potential-fid.d.ts +0 -30
  216. package/core/lib/lantern/metrics/total-blocking-time.d.ts +0 -31
  217. package/core/lib/lantern/page-dependency-graph.d.ts +0 -58
  218. /package/core/lib/lantern/{lantern-error.js → LanternError.js} +0 -0
  219. /package/core/{computed/metrics/tbt-utils.js → lib/lantern/TBTUtils.js} +0 -0
@@ -0,0 +1,130 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2024 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import * as Lantern from './lantern.js';
8
+
9
+ const {calculateSumOfBlockingTime} = Lantern.TBTUtils;
10
+
11
+ describe('TotalBlockingTime utils', () => {
12
+ it('reports 0 when no task is longer than 50ms', () => {
13
+ const events = [
14
+ {start: 1000, end: 1050, duration: 50},
15
+ {start: 2000, end: 2010, duration: 10},
16
+ ];
17
+
18
+ const fcpTimeMs = 500;
19
+ const interactiveTimeMs = 4000;
20
+
21
+ expect(
22
+ calculateSumOfBlockingTime(events, fcpTimeMs, interactiveTimeMs)
23
+ ).toBe(0);
24
+ });
25
+
26
+ it('only looks at tasks within FCP and TTI', () => {
27
+ const events = [
28
+ {start: 1000, end: 1060, duration: 60},
29
+ {start: 2000, end: 2100, duration: 100},
30
+ {start: 2300, end: 2450, duration: 150},
31
+ {start: 2600, end: 2800, duration: 200},
32
+ ];
33
+
34
+ const fcpTimeMs = 1500;
35
+ const interactiveTimeMs = 2500;
36
+
37
+ expect(
38
+ calculateSumOfBlockingTime(events, fcpTimeMs, interactiveTimeMs)
39
+ ).toBe(150);
40
+ });
41
+
42
+ it('clips before finding blocking regions', () => {
43
+ const fcpTimeMs = 150;
44
+ const interactiveTimeMs = 300;
45
+
46
+ const events = [
47
+ // The clipping is done first, so the task becomes [150, 200] after clipping and contributes
48
+ // 0ms of blocking time. This is in contrast to first calculating the blocking region ([100,
49
+ // 200]) and then clipping at FCP (150ms), which yields 50ms blocking time.
50
+ {start: 50, end: 200, duration: 150},
51
+ // Similarly, the task is first clipped above to be [240, 300], and then contributes 10ms
52
+ // blocking time.
53
+ {start: 240, end: 460, duration: 120},
54
+ ];
55
+
56
+ expect(
57
+ calculateSumOfBlockingTime(events, fcpTimeMs, interactiveTimeMs)
58
+ ).toBe(10); // 0ms + 10ms.
59
+ });
60
+
61
+ // TTI can happen in the middle of a task, for example, if TTI is at FMP which occurs as part
62
+ // of a larger task, or in the lantern case where we use estimate TTI using a different graph
63
+ // from the one used to estimate TBT.
64
+ it('clips properly if TTI falls in the middle of a task', () => {
65
+ const fcpTimeMs = 1000;
66
+ const interactiveTimeMs = 2000;
67
+
68
+ expect(
69
+ calculateSumOfBlockingTime(
70
+ [{start: 1951, end: 2100, duration: 149}],
71
+ fcpTimeMs,
72
+ interactiveTimeMs
73
+ )
74
+ ).toBe(0); // Duration after clipping is 49, which is < 50.
75
+ expect(
76
+ calculateSumOfBlockingTime(
77
+ [{start: 1950, end: 2100, duration: 150}],
78
+ fcpTimeMs,
79
+ interactiveTimeMs
80
+ )
81
+ ).toBe(0); // Duration after clipping is 50, so time after 50ms is 0ms.
82
+ expect(
83
+ calculateSumOfBlockingTime(
84
+ [{start: 1949, end: 2100, duration: 151}],
85
+ fcpTimeMs,
86
+ interactiveTimeMs
87
+ )
88
+ ).toBe(1); // Duration after clipping is 51, so time after 50ms is 1ms.
89
+ });
90
+
91
+ it('clips properly if FCP falls in the middle of a task', () => {
92
+ const fcpTimeMs = 1000;
93
+ const interactiveTimeMs = 2000;
94
+
95
+ expect(
96
+ calculateSumOfBlockingTime(
97
+ [{start: 900, end: 1049, duration: 149}],
98
+ fcpTimeMs,
99
+ interactiveTimeMs
100
+ )
101
+ ).toBe(0); // Duration after clipping is 49, which is < 50.
102
+ expect(
103
+ calculateSumOfBlockingTime(
104
+ [{start: 900, end: 1050, duration: 150}],
105
+ fcpTimeMs,
106
+ interactiveTimeMs
107
+ )
108
+ ).toBe(0); // Duration after clipping is 50, so time after 50ms is 0ms.
109
+ expect(
110
+ calculateSumOfBlockingTime(
111
+ [{start: 900, end: 1051, duration: 151}],
112
+ fcpTimeMs,
113
+ interactiveTimeMs
114
+ )
115
+ ).toBe(1); // Duration after clipping is 51, so time after 50ms is 1ms.
116
+ });
117
+
118
+ // This can happen in the lantern metric case, where we use the optimistic
119
+ // TTI and pessimistic FCP.
120
+ it('returns 0 if interactiveTime is earlier than FCP', () => {
121
+ const fcpTimeMs = 2050;
122
+ const interactiveTimeMs = 1050;
123
+
124
+ const events = [{start: 500, end: 3000, duration: 2500}];
125
+
126
+ expect(
127
+ calculateSumOfBlockingTime(events, fcpTimeMs, interactiveTimeMs)
128
+ ).toBe(0);
129
+ });
130
+ });
@@ -0,0 +1,25 @@
1
+ export type MetricName = import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricName;
2
+ export type MetricScore = import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricScore;
3
+ /** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricName} MetricName */
4
+ /** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricScore} MetricScore */
5
+ /**
6
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
7
+ * @return {Lantern.Simulation.ProcessedNavigation}
8
+ */
9
+ export function createProcessedNavigation(traceEngineData: TraceEngine.Handlers.Types.TraceParseData): Lantern.Simulation.ProcessedNavigation;
10
+ /**
11
+ * @param {Lantern.Trace} trace
12
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
13
+ * @return {Lantern.NetworkRequest[]}
14
+ */
15
+ export function createNetworkRequests(trace: Lantern.Trace, traceEngineData: TraceEngine.Handlers.Types.TraceParseData): Lantern.NetworkRequest[];
16
+ /**
17
+ * @param {Lantern.NetworkRequest[]} requests
18
+ * @param {Lantern.Trace} trace
19
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
20
+ * @param {Lantern.Simulation.URL=} URL
21
+ */
22
+ export function createGraph(requests: Lantern.NetworkRequest[], trace: Lantern.Trace, traceEngineData: TraceEngine.Handlers.Types.TraceParseData, URL?: Lantern.Simulation.URL | undefined): import("./PageDependencyGraph.js").Node;
23
+ import * as TraceEngine from '@paulirish/trace_engine';
24
+ import * as Lantern from './lantern.js';
25
+ //# sourceMappingURL=TraceEngineComputationData.d.ts.map
@@ -0,0 +1,466 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2024 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ import * as TraceEngine from '@paulirish/trace_engine';
8
+ import * as Protocol from '@paulirish/trace_engine/generated/protocol.js';
9
+
10
+ import * as Lantern from './lantern.js';
11
+
12
+ /** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricName} MetricName */
13
+ /** @typedef {import('@paulirish/trace_engine/models/trace/handlers/PageLoadMetricsHandler.js').MetricScore} MetricScore */
14
+
15
+ /**
16
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
17
+ * @return {Lantern.Simulation.ProcessedNavigation}
18
+ */
19
+ function createProcessedNavigation(traceEngineData) {
20
+ const Meta = traceEngineData.Meta;
21
+ const frameId = Meta.mainFrameId;
22
+ const scoresByNav = traceEngineData.PageLoadMetrics.metricScoresByFrameId.get(frameId);
23
+ if (!scoresByNav) {
24
+ throw new Error('missing metric scores for main frame');
25
+ }
26
+
27
+ const lastNavigationId = Meta.mainFrameNavigations.at(-1)?.args.data?.navigationId;
28
+ const scores = lastNavigationId && scoresByNav.get(lastNavigationId);
29
+ if (!scores) {
30
+ throw new Error('missing metric scores for specified navigation');
31
+ }
32
+
33
+ /** @param {MetricName} metric */
34
+ const getTimestampOrUndefined = metric => {
35
+ const metricScore = scores.get(metric);
36
+ if (!metricScore?.event) return;
37
+ return metricScore.event.ts;
38
+ };
39
+ /** @param {MetricName} metric */
40
+ const getTimestamp = metric => {
41
+ const metricScore = scores.get(metric);
42
+ if (!metricScore?.event) throw new Error(`missing metric: ${metric}`);
43
+ return metricScore.event.ts;
44
+ };
45
+ // TODO: should use `MetricName.LCP`, but it is a const enum.
46
+ const FCP = /** @type {MetricName} */('FCP');
47
+ const LCP = /** @type {MetricName} */('LCP');
48
+ return {
49
+ timestamps: {
50
+ firstContentfulPaint: getTimestamp(FCP),
51
+ largestContentfulPaint: getTimestampOrUndefined(LCP),
52
+ },
53
+ };
54
+ }
55
+
56
+ /**
57
+ * @param {URL|string} url
58
+ */
59
+ function createParsedUrl(url) {
60
+ if (typeof url === 'string') {
61
+ url = new URL(url);
62
+ }
63
+ return {
64
+ scheme: url.protocol.split(':')[0],
65
+ // Intentional, DevTools uses different terminology
66
+ host: url.hostname,
67
+ securityOrigin: url.origin,
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Returns a map of `pid` -> `tid[]`.
73
+ * @param {Lantern.Trace} trace
74
+ * @return {Map<number, number[]>}
75
+ */
76
+ function findWorkerThreads(trace) {
77
+ // TODO: WorkersHandler in TraceEngine needs to be updated to also include `pid` (only had `tid`).
78
+ const workerThreads = new Map();
79
+ const workerCreationEvents = ['ServiceWorker thread', 'DedicatedWorker thread'];
80
+
81
+ for (const event of trace.traceEvents) {
82
+ if (event.name !== 'thread_name' || !event.args.name) {
83
+ continue;
84
+ }
85
+ if (!workerCreationEvents.includes(event.args.name)) {
86
+ continue;
87
+ }
88
+
89
+ const tids = workerThreads.get(event.pid);
90
+ if (tids) {
91
+ tids.push(event.tid);
92
+ } else {
93
+ workerThreads.set(event.pid, [event.tid]);
94
+ }
95
+ }
96
+
97
+ return workerThreads;
98
+ }
99
+
100
+ /**
101
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
102
+ * @param {Map<number, number[]>} workerThreads
103
+ * @param {import('@paulirish/trace_engine/models/trace/types/TraceEvents.js').SyntheticNetworkRequest} request
104
+ * @return {Lantern.NetworkRequest=}
105
+ */
106
+ function createLanternRequest(traceEngineData, workerThreads, request) {
107
+ if (request.args.data.connectionId === undefined ||
108
+ request.args.data.connectionReused === undefined) {
109
+ throw new Error('Trace is too old');
110
+ }
111
+
112
+ let url;
113
+ try {
114
+ url = new URL(request.args.data.url);
115
+ } catch (e) {
116
+ return;
117
+ }
118
+
119
+ const timing = request.args.data.timing ? {
120
+ // These two timings are not included in the trace.
121
+ workerFetchStart: -1,
122
+ workerRespondWithSettled: -1,
123
+ ...request.args.data.timing,
124
+ } : undefined;
125
+
126
+ const networkRequestTime = timing ?
127
+ timing.requestTime * 1000 :
128
+ request.args.data.syntheticData.downloadStart / 1000;
129
+
130
+ let fromWorker = false;
131
+ const tids = workerThreads.get(request.pid);
132
+ if (tids?.includes(request.tid)) {
133
+ fromWorker = true;
134
+ }
135
+
136
+ // TraceEngine collects worker thread ids in a different manner than `workerThreads` does.
137
+ // AFAIK these should be equivalent, but in case they are not let's also check this for now.
138
+ if (traceEngineData.Workers.workerIdByThread.has(request.tid)) {
139
+ fromWorker = true;
140
+ }
141
+
142
+ // typescript const enums.... gotta stop using those ...
143
+ const Other = /** @type {Protocol.Network.InitiatorType.Other} */ ('other');
144
+
145
+ // `initiator` in the trace does not contain the stack trace for JS-initiated
146
+ // requests. Instead, that is stored in the `stackTrace` property of the SyntheticNetworkRequest.
147
+ // There are some minor differences in the fields, accounted for here.
148
+ // Most importantly, there seems to be fewer frames in the trace than the equivalent
149
+ // events over the CDP. This results in less accuracy in determining the initiator request,
150
+ // which means less edges in the graph, which mean worse results.
151
+ // TODO: Should fix in Chromium.
152
+ /** @type {Lantern.NetworkRequest['initiator']} */
153
+ const initiator = request.args.data.initiator ?? {type: Other};
154
+ if (request.args.data.stackTrace) {
155
+ const callFrames = request.args.data.stackTrace.map(f => {
156
+ return {
157
+ scriptId: /** @type {Protocol.Runtime.ScriptId} */(String(f.scriptId)),
158
+ url: f.url,
159
+ lineNumber: f.lineNumber - 1,
160
+ columnNumber: f.columnNumber - 1,
161
+ functionName: f.functionName,
162
+ };
163
+ });
164
+ initiator.stack = {callFrames};
165
+ // Note: there is no `parent` to set ...
166
+ }
167
+
168
+ let resourceType = request.args.data.resourceType;
169
+ if (request.args.data.initiator?.fetchType === 'xmlhttprequest') {
170
+ // @ts-expect-error yes XHR is a valid ResourceType. TypeScript const enums are so unhelpful.
171
+ resourceType = 'XHR';
172
+ } else if (request.args.data.initiator?.fetchType === 'fetch') {
173
+ // @ts-expect-error yes Fetch is a valid ResourceType. TypeScript const enums are so unhelpful.
174
+ resourceType = 'Fetch';
175
+ }
176
+
177
+ // TODO: set decodedBodyLength for data urls in Trace Engine.
178
+ let resourceSize = request.args.data.decodedBodyLength ?? 0;
179
+ if (url.protocol === 'data:' && resourceSize === 0) {
180
+ const needle = 'base64,';
181
+ const index = url.pathname.indexOf(needle);
182
+ if (index !== -1) {
183
+ resourceSize = atob(url.pathname.substring(index + needle.length)).length;
184
+ }
185
+ }
186
+
187
+ return {
188
+ rawRequest: request,
189
+ requestId: request.args.data.requestId,
190
+ connectionId: request.args.data.connectionId,
191
+ connectionReused: request.args.data.connectionReused,
192
+ url: request.args.data.url,
193
+ protocol: request.args.data.protocol,
194
+ parsedURL: createParsedUrl(url),
195
+ documentURL: request.args.data.requestingFrameUrl,
196
+ rendererStartTime: request.ts / 1000,
197
+ networkRequestTime,
198
+ responseHeadersEndTime: request.args.data.syntheticData.downloadStart / 1000,
199
+ networkEndTime: request.args.data.syntheticData.finishTime / 1000,
200
+ transferSize: request.args.data.encodedDataLength,
201
+ resourceSize,
202
+ fromDiskCache: request.args.data.syntheticData.isDiskCached,
203
+ fromMemoryCache: request.args.data.syntheticData.isMemoryCached,
204
+ isLinkPreload: request.args.data.isLinkPreload,
205
+ finished: request.args.data.finished,
206
+ failed: request.args.data.failed,
207
+ statusCode: request.args.data.statusCode,
208
+ initiator,
209
+ timing,
210
+ resourceType,
211
+ mimeType: request.args.data.mimeType,
212
+ priority: request.args.data.priority,
213
+ frameId: request.args.data.frame,
214
+ fromWorker,
215
+ // Set later.
216
+ redirects: undefined,
217
+ redirectSource: undefined,
218
+ redirectDestination: undefined,
219
+ initiatorRequest: undefined,
220
+ };
221
+ }
222
+
223
+ /**
224
+ * @param {Lantern.NetworkRequest} request The request to find the initiator of
225
+ * @param {Map<string, Lantern.NetworkRequest[]>} requestsByURL
226
+ * @return {Lantern.NetworkRequest|null}
227
+ */
228
+ function chooseInitiatorRequest(request, requestsByURL) {
229
+ if (request.redirectSource) {
230
+ return request.redirectSource;
231
+ }
232
+
233
+ const initiatorURL = Lantern.PageDependencyGraph.getNetworkInitiators(request)[0];
234
+ let candidates = requestsByURL.get(initiatorURL) || [];
235
+ // The (valid) initiator must come before the initiated request.
236
+ candidates = candidates.filter(c => {
237
+ return c.responseHeadersEndTime <= request.rendererStartTime &&
238
+ c.finished && !c.failed;
239
+ });
240
+ if (candidates.length > 1) {
241
+ // Disambiguate based on prefetch. Prefetch requests have type 'Other' and cannot
242
+ // initiate requests, so we drop them here.
243
+ const nonPrefetchCandidates = candidates.filter(
244
+ cand => cand.resourceType !== Lantern.NetworkRequestTypes.Other);
245
+ if (nonPrefetchCandidates.length) {
246
+ candidates = nonPrefetchCandidates;
247
+ }
248
+ }
249
+ if (candidates.length > 1) {
250
+ // Disambiguate based on frame. It's likely that the initiator comes from the same frame.
251
+ const sameFrameCandidates = candidates.filter(cand => cand.frameId === request.frameId);
252
+ if (sameFrameCandidates.length) {
253
+ candidates = sameFrameCandidates;
254
+ }
255
+ }
256
+ if (candidates.length > 1 && request.initiator.type === 'parser') {
257
+ // Filter to just Documents when initiator type is parser.
258
+ const documentCandidates = candidates.filter(cand =>
259
+ cand.resourceType === Lantern.NetworkRequestTypes.Document);
260
+ if (documentCandidates.length) {
261
+ candidates = documentCandidates;
262
+ }
263
+ }
264
+ if (candidates.length > 1) {
265
+ // If all real loads came from successful preloads (url preloaded and
266
+ // loads came from the cache), filter to link rel=preload request(s).
267
+ const linkPreloadCandidates = candidates.filter(c => c.isLinkPreload);
268
+ if (linkPreloadCandidates.length) {
269
+ const nonPreloadCandidates = candidates.filter(c => !c.isLinkPreload);
270
+ const allPreloaded = nonPreloadCandidates.every(c => c.fromDiskCache || c.fromMemoryCache);
271
+ if (nonPreloadCandidates.length && allPreloaded) {
272
+ candidates = linkPreloadCandidates;
273
+ }
274
+ }
275
+ }
276
+
277
+ // Only return an initiator if the result is unambiguous.
278
+ return candidates.length === 1 ? candidates[0] : null;
279
+ }
280
+
281
+ /**
282
+ * @param {Lantern.NetworkRequest[]} lanternRequests
283
+ */
284
+ function linkInitiators(lanternRequests) {
285
+ /** @type {Map<string, Lantern.NetworkRequest[]>} */
286
+ const requestsByURL = new Map();
287
+ for (const request of lanternRequests) {
288
+ const requests = requestsByURL.get(request.url) || [];
289
+ requests.push(request);
290
+ requestsByURL.set(request.url, requests);
291
+ }
292
+
293
+ for (const request of lanternRequests) {
294
+ const initiatorRequest = chooseInitiatorRequest(request, requestsByURL);
295
+ if (initiatorRequest) {
296
+ request.initiatorRequest = initiatorRequest;
297
+ }
298
+ }
299
+ }
300
+
301
+ /**
302
+ * @param {Lantern.Trace} trace
303
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
304
+ * @return {Lantern.NetworkRequest[]}
305
+ */
306
+ function createNetworkRequests(trace, traceEngineData) {
307
+ const workerThreads = findWorkerThreads(trace);
308
+
309
+ /** @type {Lantern.NetworkRequest[]} */
310
+ const lanternRequests = [];
311
+ for (const request of traceEngineData.NetworkRequests.byTime) {
312
+ const lanternRequest = createLanternRequest(traceEngineData, workerThreads, request);
313
+ if (lanternRequest) {
314
+ lanternRequests.push(lanternRequest);
315
+ }
316
+ }
317
+
318
+ // TraceEngine consolidates all redirects into a single request object, but lantern needs
319
+ // an entry for each redirected request.
320
+ for (const request of [...lanternRequests]) {
321
+ if (!request.rawRequest) continue;
322
+
323
+ const redirects = request.rawRequest.args.data.redirects;
324
+ if (!redirects.length) continue;
325
+
326
+ const requestChain = [];
327
+ for (const redirect of redirects) {
328
+ const redirectedRequest = structuredClone(request);
329
+
330
+ redirectedRequest.networkRequestTime = redirect.ts / 1000;
331
+ redirectedRequest.rendererStartTime = redirectedRequest.networkRequestTime;
332
+
333
+ redirectedRequest.networkEndTime = (redirect.ts + redirect.dur) / 1000;
334
+ redirectedRequest.responseHeadersEndTime = redirectedRequest.networkEndTime;
335
+
336
+ redirectedRequest.timing = {
337
+ requestTime: redirectedRequest.networkRequestTime / 1000,
338
+ receiveHeadersStart: redirectedRequest.responseHeadersEndTime,
339
+ receiveHeadersEnd: redirectedRequest.responseHeadersEndTime,
340
+ proxyStart: -1,
341
+ proxyEnd: -1,
342
+ dnsStart: -1,
343
+ dnsEnd: -1,
344
+ connectStart: -1,
345
+ connectEnd: -1,
346
+ sslStart: -1,
347
+ sslEnd: -1,
348
+ sendStart: -1,
349
+ sendEnd: -1,
350
+ workerStart: -1,
351
+ workerReady: -1,
352
+ workerFetchStart: -1,
353
+ workerRespondWithSettled: -1,
354
+ pushStart: -1,
355
+ pushEnd: -1,
356
+ };
357
+
358
+ redirectedRequest.url = redirect.url;
359
+ redirectedRequest.parsedURL = createParsedUrl(redirect.url);
360
+ // TODO: TraceEngine is not retaining the actual status code.
361
+ redirectedRequest.statusCode = 302;
362
+ redirectedRequest.resourceType = undefined;
363
+ // TODO: TraceEngine is not retaining transfer size of redirected request.
364
+ redirectedRequest.transferSize = 400;
365
+ requestChain.push(redirectedRequest);
366
+ lanternRequests.push(redirectedRequest);
367
+ }
368
+ requestChain.push(request);
369
+
370
+ for (let i = 0; i < requestChain.length; i++) {
371
+ const request = requestChain[i];
372
+ if (i > 0) {
373
+ request.redirectSource = requestChain[i - 1];
374
+ request.redirects = requestChain.slice(0, i);
375
+ }
376
+ if (i !== requestChain.length - 1) {
377
+ request.redirectDestination = requestChain[i + 1];
378
+ }
379
+ }
380
+
381
+ // Apply the `:redirect` requestId convention: only redirects[0].requestId is the actual
382
+ // requestId, all the rest have n occurences of `:redirect` as a suffix.
383
+ for (let i = 1; i < requestChain.length; i++) {
384
+ requestChain[i].requestId = `${requestChain[i - 1].requestId}:redirect`;
385
+ }
386
+ }
387
+
388
+ linkInitiators(lanternRequests);
389
+
390
+ // This would already be sorted by rendererStartTime, if not for the redirect unwrapping done
391
+ // above.
392
+ return lanternRequests.sort((a, b) => a.rendererStartTime - b.rendererStartTime);
393
+ }
394
+
395
+ /**
396
+ * @param {Lantern.Trace} trace
397
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
398
+ * @return {Lantern.TraceEvent[]}
399
+ */
400
+ function collectMainThreadEvents(trace, traceEngineData) {
401
+ const Meta = traceEngineData.Meta;
402
+ const mainFramePids = Meta.mainFrameNavigations.length
403
+ ? new Set(Meta.mainFrameNavigations.map(nav => nav.pid))
404
+ : Meta.topLevelRendererIds;
405
+
406
+ const rendererPidToTid = new Map();
407
+ for (const pid of mainFramePids) {
408
+ const threads = Meta.threadsInProcess.get(pid) ?? [];
409
+
410
+ let found = false;
411
+ for (const [tid, thread] of threads) {
412
+ if (thread.args.name === 'CrRendererMain') {
413
+ rendererPidToTid.set(pid, tid);
414
+ found = true;
415
+ break;
416
+ }
417
+ }
418
+
419
+ if (found) continue;
420
+
421
+ // `CrRendererMain` can be missing if chrome is launched with the `--single-process` flag.
422
+ // In this case, page tasks will be run in the browser thread.
423
+ for (const [tid, thread] of threads) {
424
+ if (thread.args.name === 'CrBrowserMain') {
425
+ rendererPidToTid.set(pid, tid);
426
+ found = true;
427
+ break;
428
+ }
429
+ }
430
+ }
431
+
432
+ return trace.traceEvents.filter(e => rendererPidToTid.get(e.pid) === e.tid);
433
+ }
434
+
435
+ /**
436
+ * @param {Lantern.NetworkRequest[]} requests
437
+ * @param {Lantern.Trace} trace
438
+ * @param {TraceEngine.Handlers.Types.TraceParseData} traceEngineData
439
+ * @param {Lantern.Simulation.URL=} URL
440
+ */
441
+ function createGraph(requests, trace, traceEngineData, URL) {
442
+ const mainThreadEvents = collectMainThreadEvents(trace, traceEngineData);
443
+
444
+ // URL defines the initial request that the Lantern graph starts at (the root node) and the
445
+ // main document request. These are equal if there are no redirects.
446
+ if (!URL) {
447
+ URL = {
448
+ requestedUrl: requests[0].url,
449
+ mainDocumentUrl: '',
450
+ };
451
+
452
+ let request = requests[0];
453
+ while (request.redirectDestination) {
454
+ request = request.redirectDestination;
455
+ }
456
+ URL.mainDocumentUrl = request.url;
457
+ }
458
+
459
+ return Lantern.PageDependencyGraph.createGraph(mainThreadEvents, requests, URL);
460
+ }
461
+
462
+ export {
463
+ createProcessedNavigation,
464
+ createNetworkRequests,
465
+ createGraph,
466
+ };
@@ -1,8 +1,20 @@
1
- /**
2
- * @license
3
- * Copyright 2024 Google LLC
4
- * SPDX-License-Identifier: Apache-2.0
5
- */
6
- /** @type {LH.Util.SelfMap<LH.Crdp.Network.ResourceType>} */
7
- export const NetworkRequestTypes: LH.Util.SelfMap<LH.Crdp.Network.ResourceType>;
1
+ export { BaseNode } from "./BaseNode.js";
2
+ export { CPUNode } from "./CpuNode.js";
3
+ export { LanternError as Error } from "./LanternError.js";
4
+ export { Metric } from "./Metric.js";
5
+ export { NetworkNode } from "./NetworkNode.js";
6
+ export { PageDependencyGraph } from "./PageDependencyGraph.js";
7
+ export * as Metrics from "./metrics/metrics.js";
8
+ export * as Simulation from "./simulation/simulation.js";
9
+ export * as TBTUtils from "./TBTUtils.js";
10
+ export * as TraceEngineComputationData from "./TraceEngineComputationData.js";
11
+ export type NetworkRequest<T = any> = Lantern.NetworkRequest<T>;
12
+ export type ResourcePriority = Lantern.ResourcePriority;
13
+ export type ResourceTiming = Lantern.ResourceTiming;
14
+ export type ResourceType = Lantern.ResourceType;
15
+ export type Trace = Lantern.Trace;
16
+ export type TraceEvent = Lantern.TraceEvent;
17
+ /** @type {Lantern.Util.SelfMap<Lantern.ResourceType>} */
18
+ export const NetworkRequestTypes: Lantern.Util.SelfMap<Lantern.ResourceType>;
19
+ import * as Lantern from './types/lantern.js';
8
20
  //# sourceMappingURL=lantern.d.ts.map
@@ -4,7 +4,9 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
 
7
- /** @type {LH.Util.SelfMap<LH.Crdp.Network.ResourceType>} */
7
+ import * as Lantern from './types/lantern.js';
8
+
9
+ /** @type {Lantern.Util.SelfMap<Lantern.ResourceType>} */
8
10
  const NetworkRequestTypes = {
9
11
  XHR: 'XHR',
10
12
  Fetch: 'Fetch',
@@ -26,6 +28,24 @@ const NetworkRequestTypes = {
26
28
  Prefetch: 'Prefetch',
27
29
  };
28
30
 
31
+ export {BaseNode} from './BaseNode.js';
32
+ export {CPUNode} from './CpuNode.js';
33
+ export {LanternError as Error} from './LanternError.js';
34
+ export {Metric} from './Metric.js';
35
+ export {NetworkNode} from './NetworkNode.js';
36
+ export {PageDependencyGraph} from './PageDependencyGraph.js';
37
+ export * as Metrics from './metrics/metrics.js';
38
+ export * as Simulation from './simulation/simulation.js';
39
+ export * as TBTUtils from './TBTUtils.js';
40
+ export * as TraceEngineComputationData from './TraceEngineComputationData.js';
41
+
42
+ /** @template [T=any] @typedef {Lantern.NetworkRequest<T>} NetworkRequest */
43
+ /** @typedef {Lantern.ResourcePriority} ResourcePriority */
44
+ /** @typedef {Lantern.ResourceTiming} ResourceTiming */
45
+ /** @typedef {Lantern.ResourceType} ResourceType */
46
+ /** @typedef {Lantern.Trace} Trace */
47
+ /** @typedef {Lantern.TraceEvent} TraceEvent */
48
+
29
49
  export {
30
50
  NetworkRequestTypes,
31
51
  };