lighthouse 11.3.0-dev.20231129 → 11.3.0-dev.20231201

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.
package/shared/util.js CHANGED
@@ -77,6 +77,23 @@ class Util {
77
77
  return details;
78
78
  }
79
79
 
80
+ /**
81
+ * Given the entity classification dataset and a URL, identify the entity.
82
+ * @param {string} url
83
+ * @param {LH.Result.Entities=} entities
84
+ * @return {LH.Result.LhrEntity|string}
85
+ */
86
+ static getEntityFromUrl(url, entities) {
87
+ // If it's a pre-v10 LHR, we don't have entities, so match against the root-ish domain
88
+ if (!entities) {
89
+ return Util.getPseudoRootDomain(url);
90
+ }
91
+
92
+ const entity = entities.find(e => e.origins.find(origin => url.startsWith(origin)));
93
+ // This fallback case would be unexpected, but leaving for safety.
94
+ return entity || Util.getPseudoRootDomain(url);
95
+ }
96
+
80
97
  /**
81
98
  * Split a string by markdown code spans (enclosed in `backticks`), splitting
82
99
  * into segments that were enclosed in backticks (marked as `isCode === true`)
@@ -292,11 +309,12 @@ class Util {
292
309
 
293
310
  /**
294
311
  * Gets the tld of a domain
312
+ * This function is used only while rendering pre-10.0 LHRs.
295
313
  *
296
314
  * @param {string} hostname
297
315
  * @return {string} tld
298
316
  */
299
- static getTld(hostname) {
317
+ static getPseudoTld(hostname) {
300
318
  const tlds = hostname.split('.').slice(-2);
301
319
 
302
320
  if (!listOfTlds.includes(tlds[0])) {
@@ -308,12 +326,16 @@ class Util {
308
326
 
309
327
  /**
310
328
  * Returns a primary domain for provided hostname (e.g. www.example.com -> example.com).
329
+ * As it doesn't consult the Public Suffix List, it can sometimes lose detail.
330
+ * See the `listOfTlds` comment above for more.
331
+ * This function is used only while rendering pre-10.0 LHRs. See UrlUtils.getRootDomain
332
+ * for the current method that makes use of PSL.
311
333
  * @param {string|URL} url hostname or URL object
312
334
  * @return {string}
313
335
  */
314
- static getRootDomain(url) {
336
+ static getPseudoRootDomain(url) {
315
337
  const hostname = Util.createOrReturnURL(url).hostname;
316
- const tld = Util.getTld(hostname);
338
+ const tld = Util.getPseudoTld(hostname);
317
339
 
318
340
  // tld is .com or .co.uk which means we means that length is 1 to big
319
341
  // .com => 2 & .co.uk => 3