lighthouse 9.5.0-dev.20230123 → 9.5.0-dev.20230125

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 (167) hide show
  1. package/.codecov.yml +0 -5
  2. package/core/audits/accessibility/accesskeys.js +1 -1
  3. package/core/audits/accessibility/aria-allowed-attr.js +1 -1
  4. package/core/audits/accessibility/aria-command-name.js +1 -1
  5. package/core/audits/accessibility/aria-hidden-body.js +1 -1
  6. package/core/audits/accessibility/aria-hidden-focus.js +1 -1
  7. package/core/audits/accessibility/aria-input-field-name.js +1 -1
  8. package/core/audits/accessibility/aria-meter-name.js +1 -1
  9. package/core/audits/accessibility/aria-progressbar-name.js +1 -1
  10. package/core/audits/accessibility/aria-required-attr.js +1 -1
  11. package/core/audits/accessibility/aria-required-children.js +1 -1
  12. package/core/audits/accessibility/aria-required-parent.js +1 -1
  13. package/core/audits/accessibility/aria-roles.js +1 -1
  14. package/core/audits/accessibility/aria-toggle-field-name.js +1 -1
  15. package/core/audits/accessibility/aria-tooltip-name.js +1 -1
  16. package/core/audits/accessibility/aria-treeitem-name.js +1 -1
  17. package/core/audits/accessibility/aria-valid-attr-value.js +1 -1
  18. package/core/audits/accessibility/aria-valid-attr.js +1 -1
  19. package/core/audits/accessibility/button-name.js +1 -1
  20. package/core/audits/accessibility/bypass.js +1 -1
  21. package/core/audits/accessibility/color-contrast.js +1 -1
  22. package/core/audits/accessibility/definition-list.js +1 -1
  23. package/core/audits/accessibility/dlitem.js +1 -1
  24. package/core/audits/accessibility/document-title.js +1 -1
  25. package/core/audits/accessibility/duplicate-id-active.js +1 -1
  26. package/core/audits/accessibility/duplicate-id-aria.js +1 -1
  27. package/core/audits/accessibility/form-field-multiple-labels.js +1 -1
  28. package/core/audits/accessibility/frame-title.js +1 -1
  29. package/core/audits/accessibility/heading-order.js +1 -1
  30. package/core/audits/accessibility/html-has-lang.js +1 -1
  31. package/core/audits/accessibility/html-lang-valid.js +1 -1
  32. package/core/audits/accessibility/image-alt.js +1 -1
  33. package/core/audits/accessibility/input-image-alt.js +1 -1
  34. package/core/audits/accessibility/label.js +1 -1
  35. package/core/audits/accessibility/link-name.js +1 -1
  36. package/core/audits/accessibility/list.js +1 -1
  37. package/core/audits/accessibility/listitem.js +1 -1
  38. package/core/audits/accessibility/meta-refresh.js +1 -1
  39. package/core/audits/accessibility/meta-viewport.js +1 -1
  40. package/core/audits/accessibility/object-alt.js +1 -1
  41. package/core/audits/accessibility/tabindex.js +1 -1
  42. package/core/audits/accessibility/td-headers-attr.js +1 -1
  43. package/core/audits/accessibility/th-has-data-cells.js +1 -1
  44. package/core/audits/accessibility/valid-lang.js +1 -1
  45. package/core/audits/accessibility/video-caption.js +1 -1
  46. package/core/audits/audit.js +1 -1
  47. package/core/audits/byte-efficiency/legacy-javascript.d.ts +1 -0
  48. package/core/audits/byte-efficiency/legacy-javascript.js +7 -3
  49. package/core/audits/dobetterweb/uses-http2.d.ts +5 -2
  50. package/core/audits/dobetterweb/uses-http2.js +12 -7
  51. package/core/audits/network-requests.js +7 -6
  52. package/core/audits/third-party-facades.d.ts +3 -2
  53. package/core/audits/third-party-facades.js +12 -9
  54. package/core/audits/third-party-summary.d.ts +7 -6
  55. package/core/audits/third-party-summary.js +16 -14
  56. package/core/audits/valid-source-maps.d.ts +5 -3
  57. package/core/audits/valid-source-maps.js +11 -9
  58. package/core/computed/entity-classification.d.ts +29 -0
  59. package/core/computed/entity-classification.js +102 -0
  60. package/core/computed/metrics/responsiveness.js +5 -2
  61. package/core/computed/page-dependency-graph.js +1 -1
  62. package/core/computed/resource-summary.d.ts +4 -2
  63. package/core/computed/resource-summary.js +8 -5
  64. package/core/gather/gatherers/accessibility.js +3 -1
  65. package/core/lib/network-request.d.ts +2 -0
  66. package/core/lib/network-request.js +3 -0
  67. package/core/lib/stack-packs.js +4 -0
  68. package/core/lib/tracehouse/trace-processor.d.ts +14 -5
  69. package/core/lib/tracehouse/trace-processor.js +85 -30
  70. package/core/lib/traces/metric-trace-events.js +1 -1
  71. package/core/lib/url-utils.js +1 -1
  72. package/core/runner.d.ts +10 -0
  73. package/core/runner.js +48 -0
  74. package/dist/report/bundle.esm.js +724 -687
  75. package/dist/report/flow.js +25 -15
  76. package/dist/report/standalone.js +25 -15
  77. package/flow-report/src/common.tsx +1 -1
  78. package/flow-report/src/i18n/i18n.tsx +17 -16
  79. package/flow-report/src/sidebar/sidebar.tsx +2 -2
  80. package/flow-report/src/summary/category.tsx +6 -6
  81. package/flow-report/src/summary/summary.tsx +2 -2
  82. package/package.json +7 -8
  83. package/report/renderer/category-renderer.js +19 -17
  84. package/report/renderer/crc-details-renderer.js +6 -6
  85. package/report/renderer/details-renderer.js +7 -6
  86. package/report/renderer/dom.js +1 -1
  87. package/report/renderer/element-screenshot-renderer.js +3 -3
  88. package/report/renderer/performance-category-renderer.js +17 -15
  89. package/report/renderer/pwa-category-renderer.js +5 -4
  90. package/report/renderer/report-globals.d.ts +21 -0
  91. package/report/renderer/report-globals.js +49 -0
  92. package/report/renderer/report-renderer.js +22 -20
  93. package/report/renderer/report-ui-features.js +9 -8
  94. package/report/renderer/report-utils.d.ts +116 -0
  95. package/report/renderer/{util.js → report-utils.js} +81 -429
  96. package/report/renderer/snippet-renderer.js +3 -2
  97. package/report/test/renderer/category-renderer-test.js +9 -4
  98. package/report/test/renderer/crc-details-renderer-test.js +7 -3
  99. package/report/test/renderer/details-renderer-test.js +7 -3
  100. package/report/test/renderer/dom-test.js +7 -3
  101. package/report/test/renderer/element-screenshot-renderer-test.js +8 -4
  102. package/report/test/renderer/performance-category-renderer-test.js +14 -9
  103. package/report/test/renderer/pwa-category-renderer-test.js +10 -5
  104. package/report/test/renderer/report-renderer-test.js +2 -2
  105. package/report/test/renderer/report-ui-features-test.js +3 -3
  106. package/report/test/renderer/{util-test.js → report-utils-test.js} +36 -234
  107. package/report/test/renderer/snippet-renderer-test.js +7 -3
  108. package/shared/localization/format.js +5 -2
  109. package/shared/localization/locales/ar-XB.json +50 -50
  110. package/shared/localization/locales/ar.json +50 -50
  111. package/shared/localization/locales/bg.json +50 -50
  112. package/shared/localization/locales/ca.json +50 -50
  113. package/shared/localization/locales/cs.json +50 -50
  114. package/shared/localization/locales/da.json +50 -50
  115. package/shared/localization/locales/de.json +50 -50
  116. package/shared/localization/locales/el.json +50 -50
  117. package/shared/localization/locales/en-GB.json +50 -50
  118. package/shared/localization/locales/en-US.json +128 -98
  119. package/shared/localization/locales/en-XA.json +50 -50
  120. package/shared/localization/locales/en-XL.json +128 -98
  121. package/shared/localization/locales/es-419.json +50 -50
  122. package/shared/localization/locales/es.json +50 -50
  123. package/shared/localization/locales/fi.json +50 -50
  124. package/shared/localization/locales/fil.json +50 -50
  125. package/shared/localization/locales/fr.json +50 -50
  126. package/shared/localization/locales/he.json +50 -50
  127. package/shared/localization/locales/hi.json +50 -50
  128. package/shared/localization/locales/hr.json +50 -50
  129. package/shared/localization/locales/hu.json +50 -50
  130. package/shared/localization/locales/id.json +50 -50
  131. package/shared/localization/locales/it.json +50 -50
  132. package/shared/localization/locales/ja.json +50 -50
  133. package/shared/localization/locales/ko.json +50 -50
  134. package/shared/localization/locales/lt.json +50 -50
  135. package/shared/localization/locales/lv.json +50 -50
  136. package/shared/localization/locales/nl.json +50 -50
  137. package/shared/localization/locales/no.json +50 -50
  138. package/shared/localization/locales/pl.json +50 -50
  139. package/shared/localization/locales/pt-PT.json +50 -50
  140. package/shared/localization/locales/pt.json +50 -50
  141. package/shared/localization/locales/ro.json +50 -50
  142. package/shared/localization/locales/ru.json +50 -50
  143. package/shared/localization/locales/sk.json +50 -50
  144. package/shared/localization/locales/sl.json +50 -50
  145. package/shared/localization/locales/sr-Latn.json +50 -50
  146. package/shared/localization/locales/sr.json +50 -50
  147. package/shared/localization/locales/sv.json +50 -50
  148. package/shared/localization/locales/ta.json +50 -50
  149. package/shared/localization/locales/te.json +50 -50
  150. package/shared/localization/locales/th.json +50 -50
  151. package/shared/localization/locales/tr.json +50 -50
  152. package/shared/localization/locales/uk.json +50 -50
  153. package/shared/localization/locales/vi.json +50 -50
  154. package/shared/localization/locales/zh-HK.json +50 -50
  155. package/shared/localization/locales/zh-TW.json +50 -50
  156. package/shared/localization/locales/zh.json +50 -50
  157. package/shared/test/util-test.js +214 -0
  158. package/shared/util.d.ts +122 -0
  159. package/shared/util.js +332 -0
  160. package/tsconfig.json +0 -2
  161. package/types/artifacts.d.ts +21 -2
  162. package/types/lhr/lhr.d.ts +33 -0
  163. package/core/lib/minify-trace.d.ts +0 -6
  164. package/core/lib/minify-trace.js +0 -172
  165. package/core/util.cjs +0 -845
  166. package/core/util.d.cts +0 -342
  167. package/report/renderer/util.d.ts +0 -342
@@ -0,0 +1,214 @@
1
+ /**
2
+ * @license Copyright 2017 The Lighthouse Authors. All Rights Reserved.
3
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
5
+ */
6
+
7
+ import assert from 'assert/strict';
8
+
9
+ import {Util} from '../util.js';
10
+
11
+ describe('util helpers', () => {
12
+ describe('getTld', () => {
13
+ it('returns the correct tld', () => {
14
+ assert.equal(Util.getTld('example.com'), '.com');
15
+ assert.equal(Util.getTld('example.co.uk'), '.co.uk');
16
+ assert.equal(Util.getTld('example.com.br'), '.com.br');
17
+ assert.equal(Util.getTld('example.tokyo.jp'), '.jp');
18
+ });
19
+ });
20
+
21
+ describe('getRootDomain', () => {
22
+ it('returns the correct rootDomain from a string', () => {
23
+ assert.equal(Util.getRootDomain('https://www.example.com/index.html'), 'example.com');
24
+ assert.equal(Util.getRootDomain('https://example.com'), 'example.com');
25
+ assert.equal(Util.getRootDomain('https://www.example.co.uk'), 'example.co.uk');
26
+ assert.equal(Util.getRootDomain('https://example.com.br/app/'), 'example.com.br');
27
+ assert.equal(Util.getRootDomain('https://example.tokyo.jp'), 'tokyo.jp');
28
+ assert.equal(Util.getRootDomain('https://sub.example.com'), 'example.com');
29
+ assert.equal(Util.getRootDomain('https://sub.example.tokyo.jp'), 'tokyo.jp');
30
+ assert.equal(Util.getRootDomain('http://localhost'), 'localhost');
31
+ assert.equal(Util.getRootDomain('http://localhost:8080'), 'localhost');
32
+ });
33
+
34
+ it('returns the correct rootDomain from an URL object', () => {
35
+ assert.equal(Util.getRootDomain(new URL('https://www.example.com/index.html')), 'example.com');
36
+ assert.equal(Util.getRootDomain(new URL('https://example.com')), 'example.com');
37
+ assert.equal(Util.getRootDomain(new URL('https://www.example.co.uk')), 'example.co.uk');
38
+ assert.equal(Util.getRootDomain(new URL('https://example.com.br/app/')), 'example.com.br');
39
+ assert.equal(Util.getRootDomain(new URL('https://example.tokyo.jp')), 'tokyo.jp');
40
+ assert.equal(Util.getRootDomain(new URL('https://sub.example.com')), 'example.com');
41
+ assert.equal(Util.getRootDomain(new URL('https://sub.example.tokyo.jp')), 'tokyo.jp');
42
+ assert.equal(Util.getRootDomain(new URL('http://localhost')), 'localhost');
43
+ assert.equal(Util.getRootDomain(new URL('http://localhost:8080')), 'localhost');
44
+ });
45
+ });
46
+
47
+ describe('#splitMarkdownCodeSpans', () => {
48
+ it('handles strings with no backticks in them', () => {
49
+ expect(Util.splitMarkdownCodeSpans('regular text')).toEqual([
50
+ {isCode: false, text: 'regular text'},
51
+ ]);
52
+ });
53
+
54
+ it('does not split on a single backtick', () => {
55
+ expect(Util.splitMarkdownCodeSpans('regular `text')).toEqual([
56
+ {isCode: false, text: 'regular `text'},
57
+ ]);
58
+ });
59
+
60
+ it('splits on backticked code', () => {
61
+ expect(Util.splitMarkdownCodeSpans('regular `code` text')).toEqual([
62
+ {isCode: false, text: 'regular '},
63
+ {isCode: true, text: 'code'},
64
+ {isCode: false, text: ' text'},
65
+ ]);
66
+ });
67
+
68
+ it('splits on backticked code at the beginning of the string', () => {
69
+ expect(Util.splitMarkdownCodeSpans('`start code` regular text')).toEqual([
70
+ {isCode: true, text: 'start code'},
71
+ {isCode: false, text: ' regular text'},
72
+ ]);
73
+ });
74
+
75
+ it('splits on backticked code at the end of the string', () => {
76
+ expect(Util.splitMarkdownCodeSpans('regular text `end code`')).toEqual([
77
+ {isCode: false, text: 'regular text '},
78
+ {isCode: true, text: 'end code'},
79
+ ]);
80
+ });
81
+
82
+ it('does not split on a single backtick after split out backticked code', () => {
83
+ expect(Util.splitMarkdownCodeSpans('regular text `code` and more `text')).toEqual([
84
+ {isCode: false, text: 'regular text '},
85
+ {isCode: true, text: 'code'},
86
+ {isCode: false, text: ' and more `text'},
87
+ ]);
88
+ });
89
+
90
+ it('splits on two instances of backticked code', () => {
91
+ expect(Util.splitMarkdownCodeSpans('regular text `code` more text `and more code`')).toEqual([
92
+ {isCode: false, text: 'regular text '},
93
+ {isCode: true, text: 'code'},
94
+ {isCode: false, text: ' more text '},
95
+ {isCode: true, text: 'and more code'},
96
+ ]);
97
+ });
98
+
99
+ it('splits on two directly adjacent instances of backticked code', () => {
100
+ // eslint-disable-next-line max-len
101
+ expect(Util.splitMarkdownCodeSpans('regular text `first code``second code` end text')).toEqual([
102
+ {isCode: false, text: 'regular text '},
103
+ {isCode: true, text: 'first code'},
104
+ {isCode: true, text: 'second code'},
105
+ {isCode: false, text: ' end text'},
106
+ ]);
107
+ });
108
+
109
+ it('handles text only within backticks', () => {
110
+ expect(Util.splitMarkdownCodeSpans('`first code``second code`')).toEqual([
111
+ {isCode: true, text: 'first code'},
112
+ {isCode: true, text: 'second code'},
113
+ ]);
114
+ });
115
+
116
+ it('splits on two instances of backticked code separated by only a space', () => {
117
+ // eslint-disable-next-line max-len
118
+ expect(Util.splitMarkdownCodeSpans('`first code` `second code`')).toEqual([
119
+ {isCode: true, text: 'first code'},
120
+ {isCode: false, text: ' '},
121
+ {isCode: true, text: 'second code'},
122
+ ]);
123
+ });
124
+ });
125
+
126
+ describe('#splitMarkdownLink', () => {
127
+ it('handles strings with no links in them', () => {
128
+ expect(Util.splitMarkdownLink('some text')).toEqual([
129
+ {isLink: false, text: 'some text'},
130
+ ]);
131
+ });
132
+
133
+ it('does not split on an incomplete markdown link', () => {
134
+ expect(Util.splitMarkdownLink('some [not link text](text')).toEqual([
135
+ {isLink: false, text: 'some [not link text](text'},
136
+ ]);
137
+ });
138
+
139
+ it('splits on a markdown link', () => {
140
+ expect(Util.splitMarkdownLink('some [link text](https://example.com) text')).toEqual([
141
+ {isLink: false, text: 'some '},
142
+ {isLink: true, text: 'link text', linkHref: 'https://example.com'},
143
+ {isLink: false, text: ' text'},
144
+ ]);
145
+ });
146
+
147
+ it('splits on an http markdown link', () => {
148
+ expect(Util.splitMarkdownLink('you should [totally click here](http://never-mitm.com) now')).toEqual([
149
+ {isLink: false, text: 'you should '},
150
+ {isLink: true, text: 'totally click here', linkHref: 'http://never-mitm.com'},
151
+ {isLink: false, text: ' now'},
152
+ ]);
153
+ });
154
+
155
+ it('does not split on a non-http/https link', () => {
156
+ expect(Util.splitMarkdownLink('some [link text](ftp://example.com) text')).toEqual([
157
+ {isLink: false, text: 'some [link text](ftp://example.com) text'},
158
+ ]);
159
+ });
160
+
161
+ it('does not split on a malformed markdown link', () => {
162
+ expect(Util.splitMarkdownLink('some [link ]text](https://example.com')).toEqual([
163
+ {isLink: false, text: 'some [link ]text](https://example.com'},
164
+ ]);
165
+
166
+ expect(Util.splitMarkdownLink('some [link text] (https://example.com')).toEqual([
167
+ {isLink: false, text: 'some [link text] (https://example.com'},
168
+ ]);
169
+ });
170
+
171
+ it('does not split on empty link text', () => {
172
+ expect(Util.splitMarkdownLink('some [](https://example.com) empty link')).toEqual([
173
+ {isLink: false, text: 'some [](https://example.com) empty link'},
174
+ ]);
175
+ });
176
+
177
+ it('splits on a markdown link at the beginning of a string', () => {
178
+ expect(Util.splitMarkdownLink('[link text](https://example.com) end text')).toEqual([
179
+ {isLink: true, text: 'link text', linkHref: 'https://example.com'},
180
+ {isLink: false, text: ' end text'},
181
+ ]);
182
+ });
183
+
184
+ it('splits on a markdown link at the end of a string', () => {
185
+ expect(Util.splitMarkdownLink('start text [link text](https://example.com)')).toEqual([
186
+ {isLink: false, text: 'start text '},
187
+ {isLink: true, text: 'link text', linkHref: 'https://example.com'},
188
+ ]);
189
+ });
190
+
191
+ it('handles a string consisting only of a markdown link', () => {
192
+ expect(Util.splitMarkdownLink(`[I'm only a link](https://example.com)`)).toEqual([
193
+ {isLink: true, text: `I'm only a link`, linkHref: 'https://example.com'},
194
+ ]);
195
+ });
196
+
197
+ it('handles a string starting and ending with a markdown link', () => {
198
+ expect(Util.splitMarkdownLink('[first link](https://first.com) other text [second link](https://second.com)')).toEqual([
199
+ {isLink: true, text: 'first link', linkHref: 'https://first.com'},
200
+ {isLink: false, text: ' other text '},
201
+ {isLink: true, text: 'second link', linkHref: 'https://second.com'},
202
+ ]);
203
+ });
204
+
205
+ it('handles a string with adjacent markdown links', () => {
206
+ expect(Util.splitMarkdownLink('start text [first link](https://first.com)[second link](https://second.com) and scene')).toEqual([
207
+ {isLink: false, text: 'start text '},
208
+ {isLink: true, text: 'first link', linkHref: 'https://first.com'},
209
+ {isLink: true, text: 'second link', linkHref: 'https://second.com'},
210
+ {isLink: false, text: ' and scene'},
211
+ ]);
212
+ });
213
+ });
214
+ });
@@ -0,0 +1,122 @@
1
+ export type SnippetValue = import('../types/lhr/audit-details').default.SnippetValue;
2
+ export class Util {
3
+ static get RATINGS(): {
4
+ PASS: {
5
+ label: string;
6
+ minScore: number;
7
+ };
8
+ AVERAGE: {
9
+ label: string;
10
+ minScore: number;
11
+ };
12
+ FAIL: {
13
+ label: string;
14
+ };
15
+ ERROR: {
16
+ label: string;
17
+ };
18
+ };
19
+ static get PASS_THRESHOLD(): number;
20
+ static get MS_DISPLAY_VALUE(): string;
21
+ /**
22
+ * If LHR is older than 10.0 it will not have the `finalDisplayedUrl` property.
23
+ * Old LHRs should have the `finalUrl` property which will work fine for the report.
24
+ *
25
+ * @param {LH.Result} lhr
26
+ */
27
+ static getFinalDisplayedUrl(lhr: LH.Result): string;
28
+ /**
29
+ * If LHR is older than 10.0 it will not have the `mainDocumentUrl` property.
30
+ * Old LHRs should have the `finalUrl` property which is the same as `mainDocumentUrl`.
31
+ *
32
+ * @param {LH.Result} lhr
33
+ */
34
+ static getMainDocumentUrl(lhr: LH.Result): string | undefined;
35
+ /**
36
+ * @param {LH.Result} lhr
37
+ * @return {LH.Result.FullPageScreenshot=}
38
+ */
39
+ static getFullPageScreenshot(lhr: LH.Result): LH.Result.FullPageScreenshot | undefined;
40
+ /**
41
+ * Split a string by markdown code spans (enclosed in `backticks`), splitting
42
+ * into segments that were enclosed in backticks (marked as `isCode === true`)
43
+ * and those that outside the backticks (`isCode === false`).
44
+ * @param {string} text
45
+ * @return {Array<{isCode: true, text: string}|{isCode: false, text: string}>}
46
+ */
47
+ static splitMarkdownCodeSpans(text: string): Array<{
48
+ isCode: true;
49
+ text: string;
50
+ } | {
51
+ isCode: false;
52
+ text: string;
53
+ }>;
54
+ /**
55
+ * Split a string on markdown links (e.g. [some link](https://...)) into
56
+ * segments of plain text that weren't part of a link (marked as
57
+ * `isLink === false`), and segments with text content and a URL that did make
58
+ * up a link (marked as `isLink === true`).
59
+ * @param {string} text
60
+ * @return {Array<{isLink: true, text: string, linkHref: string}|{isLink: false, text: string}>}
61
+ */
62
+ static splitMarkdownLink(text: string): Array<{
63
+ isLink: true;
64
+ text: string;
65
+ linkHref: string;
66
+ } | {
67
+ isLink: false;
68
+ text: string;
69
+ }>;
70
+ /**
71
+ * @param {URL} parsedUrl
72
+ * @param {{numPathParts?: number, preserveQuery?: boolean, preserveHost?: boolean}=} options
73
+ * @return {string}
74
+ */
75
+ static getURLDisplayName(parsedUrl: URL, options?: {
76
+ numPathParts?: number | undefined;
77
+ preserveQuery?: boolean | undefined;
78
+ preserveHost?: boolean | undefined;
79
+ } | undefined): string;
80
+ /**
81
+ * Split a URL into a file, hostname and origin for easy display.
82
+ * @param {string} url
83
+ * @return {{file: string, hostname: string, origin: string}}
84
+ */
85
+ static parseURL(url: string): {
86
+ file: string;
87
+ hostname: string;
88
+ origin: string;
89
+ };
90
+ /**
91
+ * @param {string|URL} value
92
+ * @return {!URL}
93
+ */
94
+ static createOrReturnURL(value: string | URL): URL;
95
+ /**
96
+ * Gets the tld of a domain
97
+ *
98
+ * @param {string} hostname
99
+ * @return {string} tld
100
+ */
101
+ static getTld(hostname: string): string;
102
+ /**
103
+ * Returns a primary domain for provided hostname (e.g. www.example.com -> example.com).
104
+ * @param {string|URL} url hostname or URL object
105
+ * @return {string}
106
+ */
107
+ static getRootDomain(url: string | URL): string;
108
+ /**
109
+ * Returns only lines that are near a message, or the first few lines if there are
110
+ * no line messages.
111
+ * @param {SnippetValue['lines']} lines
112
+ * @param {SnippetValue['lineMessages']} lineMessages
113
+ * @param {number} surroundingLineCount Number of lines to include before and after
114
+ * the message. If this is e.g. 2 this function might return 5 lines.
115
+ */
116
+ static filterRelevantLines(lines: SnippetValue['lines'], lineMessages: SnippetValue['lineMessages'], surroundingLineCount: number): {
117
+ content: string;
118
+ lineNumber: number;
119
+ truncated?: boolean | undefined;
120
+ }[];
121
+ }
122
+ //# sourceMappingURL=util.d.ts.map
package/shared/util.js ADDED
@@ -0,0 +1,332 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2017 The Lighthouse Authors. All Rights Reserved.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS-IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ /** @typedef {import('../types/lhr/audit-details').default.SnippetValue} SnippetValue */
19
+
20
+ const ELLIPSIS = '\u2026';
21
+ const NBSP = '\xa0';
22
+ const PASS_THRESHOLD = 0.9;
23
+
24
+ const RATINGS = {
25
+ PASS: {label: 'pass', minScore: PASS_THRESHOLD},
26
+ AVERAGE: {label: 'average', minScore: 0.5},
27
+ FAIL: {label: 'fail'},
28
+ ERROR: {label: 'error'},
29
+ };
30
+
31
+ // 25 most used tld plus one domains (aka public suffixes) from http archive.
32
+ // @see https://github.com/GoogleChrome/lighthouse/pull/5065#discussion_r191926212
33
+ // The canonical list is https://publicsuffix.org/learn/ but we're only using subset to conserve bytes
34
+ const listOfTlds = [
35
+ 'com', 'co', 'gov', 'edu', 'ac', 'org', 'go', 'gob', 'or', 'net', 'in', 'ne', 'nic', 'gouv',
36
+ 'web', 'spb', 'blog', 'jus', 'kiev', 'mil', 'wi', 'qc', 'ca', 'bel', 'on',
37
+ ];
38
+
39
+ class Util {
40
+ static get RATINGS() {
41
+ return RATINGS;
42
+ }
43
+
44
+ static get PASS_THRESHOLD() {
45
+ return PASS_THRESHOLD;
46
+ }
47
+
48
+ static get MS_DISPLAY_VALUE() {
49
+ return `%10d${NBSP}ms`;
50
+ }
51
+
52
+ /**
53
+ * If LHR is older than 10.0 it will not have the `finalDisplayedUrl` property.
54
+ * Old LHRs should have the `finalUrl` property which will work fine for the report.
55
+ *
56
+ * @param {LH.Result} lhr
57
+ */
58
+ static getFinalDisplayedUrl(lhr) {
59
+ if (lhr.finalDisplayedUrl) return lhr.finalDisplayedUrl;
60
+ if (lhr.finalUrl) return lhr.finalUrl;
61
+ throw new Error('Could not determine final displayed URL');
62
+ }
63
+
64
+ /**
65
+ * If LHR is older than 10.0 it will not have the `mainDocumentUrl` property.
66
+ * Old LHRs should have the `finalUrl` property which is the same as `mainDocumentUrl`.
67
+ *
68
+ * @param {LH.Result} lhr
69
+ */
70
+ static getMainDocumentUrl(lhr) {
71
+ return lhr.mainDocumentUrl || lhr.finalUrl;
72
+ }
73
+
74
+ /**
75
+ * @param {LH.Result} lhr
76
+ * @return {LH.Result.FullPageScreenshot=}
77
+ */
78
+ static getFullPageScreenshot(lhr) {
79
+ if (lhr.fullPageScreenshot) {
80
+ return lhr.fullPageScreenshot;
81
+ }
82
+
83
+ // Prior to 10.0.
84
+ const details = /** @type {LH.Result.FullPageScreenshot=} */ (
85
+ lhr.audits['full-page-screenshot']?.details);
86
+ return details;
87
+ }
88
+
89
+ /**
90
+ * Split a string by markdown code spans (enclosed in `backticks`), splitting
91
+ * into segments that were enclosed in backticks (marked as `isCode === true`)
92
+ * and those that outside the backticks (`isCode === false`).
93
+ * @param {string} text
94
+ * @return {Array<{isCode: true, text: string}|{isCode: false, text: string}>}
95
+ */
96
+ static splitMarkdownCodeSpans(text) {
97
+ /** @type {Array<{isCode: true, text: string}|{isCode: false, text: string}>} */
98
+ const segments = [];
99
+
100
+ // Split on backticked code spans.
101
+ const parts = text.split(/`(.*?)`/g);
102
+ for (let i = 0; i < parts.length; i ++) {
103
+ const text = parts[i];
104
+
105
+ // Empty strings are an artifact of splitting, not meaningful.
106
+ if (!text) continue;
107
+
108
+ // Alternates between plain text and code segments.
109
+ const isCode = i % 2 !== 0;
110
+ segments.push({
111
+ isCode,
112
+ text,
113
+ });
114
+ }
115
+
116
+ return segments;
117
+ }
118
+
119
+ /**
120
+ * Split a string on markdown links (e.g. [some link](https://...)) into
121
+ * segments of plain text that weren't part of a link (marked as
122
+ * `isLink === false`), and segments with text content and a URL that did make
123
+ * up a link (marked as `isLink === true`).
124
+ * @param {string} text
125
+ * @return {Array<{isLink: true, text: string, linkHref: string}|{isLink: false, text: string}>}
126
+ */
127
+ static splitMarkdownLink(text) {
128
+ /** @type {Array<{isLink: true, text: string, linkHref: string}|{isLink: false, text: string}>} */
129
+ const segments = [];
130
+
131
+ const parts = text.split(/\[([^\]]+?)\]\((https?:\/\/.*?)\)/g);
132
+ while (parts.length) {
133
+ // Shift off the same number of elements as the pre-split and capture groups.
134
+ const [preambleText, linkText, linkHref] = parts.splice(0, 3);
135
+
136
+ if (preambleText) { // Skip empty text as it's an artifact of splitting, not meaningful.
137
+ segments.push({
138
+ isLink: false,
139
+ text: preambleText,
140
+ });
141
+ }
142
+
143
+ // Append link if there are any.
144
+ if (linkText && linkHref) {
145
+ segments.push({
146
+ isLink: true,
147
+ text: linkText,
148
+ linkHref,
149
+ });
150
+ }
151
+ }
152
+
153
+ return segments;
154
+ }
155
+
156
+ /**
157
+ * @param {URL} parsedUrl
158
+ * @param {{numPathParts?: number, preserveQuery?: boolean, preserveHost?: boolean}=} options
159
+ * @return {string}
160
+ */
161
+ static getURLDisplayName(parsedUrl, options) {
162
+ // Closure optional properties aren't optional in tsc, so fallback needs undefined values.
163
+ options = options || {numPathParts: undefined, preserveQuery: undefined,
164
+ preserveHost: undefined};
165
+ const numPathParts = options.numPathParts !== undefined ? options.numPathParts : 2;
166
+ const preserveQuery = options.preserveQuery !== undefined ? options.preserveQuery : true;
167
+ const preserveHost = options.preserveHost || false;
168
+
169
+ let name;
170
+
171
+ if (parsedUrl.protocol === 'about:' || parsedUrl.protocol === 'data:') {
172
+ // Handle 'about:*' and 'data:*' URLs specially since they have no path.
173
+ name = parsedUrl.href;
174
+ } else {
175
+ name = parsedUrl.pathname;
176
+ const parts = name.split('/').filter(part => part.length);
177
+ if (numPathParts && parts.length > numPathParts) {
178
+ name = ELLIPSIS + parts.slice(-1 * numPathParts).join('/');
179
+ }
180
+
181
+ if (preserveHost) {
182
+ name = `${parsedUrl.host}/${name.replace(/^\//, '')}`;
183
+ }
184
+ if (preserveQuery) {
185
+ name = `${name}${parsedUrl.search}`;
186
+ }
187
+ }
188
+
189
+ const MAX_LENGTH = 64;
190
+ if (parsedUrl.protocol !== 'data:') {
191
+ // Always elide hexadecimal hash
192
+ name = name.replace(/([a-f0-9]{7})[a-f0-9]{13}[a-f0-9]*/g, `$1${ELLIPSIS}`);
193
+ // Also elide other hash-like mixed-case strings
194
+ name = name.replace(/([a-zA-Z0-9-_]{9})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9-_]{10,}/g,
195
+ `$1${ELLIPSIS}`);
196
+ // Also elide long number sequences
197
+ name = name.replace(/(\d{3})\d{6,}/g, `$1${ELLIPSIS}`);
198
+ // Merge any adjacent ellipses
199
+ name = name.replace(/\u2026+/g, ELLIPSIS);
200
+
201
+ // Elide query params first
202
+ if (name.length > MAX_LENGTH && name.includes('?')) {
203
+ // Try to leave the first query parameter intact
204
+ name = name.replace(/\?([^=]*)(=)?.*/, `?$1$2${ELLIPSIS}`);
205
+
206
+ // Remove it all if it's still too long
207
+ if (name.length > MAX_LENGTH) {
208
+ name = name.replace(/\?.*/, `?${ELLIPSIS}`);
209
+ }
210
+ }
211
+ }
212
+
213
+ // Elide too long names next
214
+ if (name.length > MAX_LENGTH) {
215
+ const dotIndex = name.lastIndexOf('.');
216
+ if (dotIndex >= 0) {
217
+ name = name.slice(0, MAX_LENGTH - 1 - (name.length - dotIndex)) +
218
+ // Show file extension
219
+ `${ELLIPSIS}${name.slice(dotIndex)}`;
220
+ } else {
221
+ name = name.slice(0, MAX_LENGTH - 1) + ELLIPSIS;
222
+ }
223
+ }
224
+
225
+ return name;
226
+ }
227
+
228
+ /**
229
+ * Split a URL into a file, hostname and origin for easy display.
230
+ * @param {string} url
231
+ * @return {{file: string, hostname: string, origin: string}}
232
+ */
233
+ static parseURL(url) {
234
+ const parsedUrl = new URL(url);
235
+ return {
236
+ file: Util.getURLDisplayName(parsedUrl),
237
+ hostname: parsedUrl.hostname,
238
+ origin: parsedUrl.origin,
239
+ };
240
+ }
241
+
242
+ /**
243
+ * @param {string|URL} value
244
+ * @return {!URL}
245
+ */
246
+ static createOrReturnURL(value) {
247
+ if (value instanceof URL) {
248
+ return value;
249
+ }
250
+
251
+ return new URL(value);
252
+ }
253
+
254
+ /**
255
+ * Gets the tld of a domain
256
+ *
257
+ * @param {string} hostname
258
+ * @return {string} tld
259
+ */
260
+ static getTld(hostname) {
261
+ const tlds = hostname.split('.').slice(-2);
262
+
263
+ if (!listOfTlds.includes(tlds[0])) {
264
+ return `.${tlds[tlds.length - 1]}`;
265
+ }
266
+
267
+ return `.${tlds.join('.')}`;
268
+ }
269
+
270
+ /**
271
+ * Returns a primary domain for provided hostname (e.g. www.example.com -> example.com).
272
+ * @param {string|URL} url hostname or URL object
273
+ * @return {string}
274
+ */
275
+ static getRootDomain(url) {
276
+ const hostname = Util.createOrReturnURL(url).hostname;
277
+ const tld = Util.getTld(hostname);
278
+
279
+ // tld is .com or .co.uk which means we means that length is 1 to big
280
+ // .com => 2 & .co.uk => 3
281
+ const splitTld = tld.split('.');
282
+
283
+ // get TLD + root domain
284
+ return hostname.split('.').slice(-splitTld.length).join('.');
285
+ }
286
+
287
+ /**
288
+ * Returns only lines that are near a message, or the first few lines if there are
289
+ * no line messages.
290
+ * @param {SnippetValue['lines']} lines
291
+ * @param {SnippetValue['lineMessages']} lineMessages
292
+ * @param {number} surroundingLineCount Number of lines to include before and after
293
+ * the message. If this is e.g. 2 this function might return 5 lines.
294
+ */
295
+ static filterRelevantLines(lines, lineMessages, surroundingLineCount) {
296
+ if (lineMessages.length === 0) {
297
+ // no lines with messages, just return the first bunch of lines
298
+ return lines.slice(0, surroundingLineCount * 2 + 1);
299
+ }
300
+
301
+ const minGapSize = 3;
302
+ const lineNumbersToKeep = new Set();
303
+ // Sort messages so we can check lineNumbersToKeep to see how big the gap to
304
+ // the previous line is.
305
+ lineMessages = lineMessages.sort((a, b) => (a.lineNumber || 0) - (b.lineNumber || 0));
306
+ lineMessages.forEach(({lineNumber}) => {
307
+ let firstSurroundingLineNumber = lineNumber - surroundingLineCount;
308
+ let lastSurroundingLineNumber = lineNumber + surroundingLineCount;
309
+
310
+ while (firstSurroundingLineNumber < 1) {
311
+ // make sure we still show (surroundingLineCount * 2 + 1) lines in total
312
+ firstSurroundingLineNumber++;
313
+ lastSurroundingLineNumber++;
314
+ }
315
+ // If only a few lines would be omitted normally then we prefer to include
316
+ // extra lines to avoid the tiny gap
317
+ if (lineNumbersToKeep.has(firstSurroundingLineNumber - minGapSize - 1)) {
318
+ firstSurroundingLineNumber -= minGapSize;
319
+ }
320
+ for (let i = firstSurroundingLineNumber; i <= lastSurroundingLineNumber; i++) {
321
+ const surroundingLineNumber = i;
322
+ lineNumbersToKeep.add(surroundingLineNumber);
323
+ }
324
+ });
325
+
326
+ return lines.filter(line => lineNumbersToKeep.has(line.lineNumber));
327
+ }
328
+ }
329
+
330
+ export {
331
+ Util,
332
+ };
package/tsconfig.json CHANGED
@@ -16,7 +16,6 @@
16
16
  "cli/**/*.js",
17
17
  "core/**/*.js",
18
18
  "core/index.cjs",
19
- "core/util.cjs",
20
19
  "clients/**/*.js",
21
20
  "build/**/*.js",
22
21
  "./types/**/*.d.ts",
@@ -82,7 +81,6 @@
82
81
  "core/test/lib/median-run-test.js",
83
82
  "core/test/lib/minification-estimator-test.js",
84
83
  "core/test/lib/minify-devtoolslog-test.js",
85
- "core/test/lib/minify-trace-test.js",
86
84
  "core/test/lib/network-recorder-test.js",
87
85
  "core/test/lib/network-request-test.js",
88
86
  "core/test/lib/page-functions-test.js",