forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource-summary.js
114 lines (104 loc) · 4.86 KB
/
resource-summary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @license Copyright 2019 The Lighthouse Authors. All Rights Reserved.
* 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
* 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.
*/
'use strict';
const makeComputedArtifact = require('./computed-artifact.js');
const NetworkRecords = require('./network-records.js');
const URL = require('../lib/url-shim.js');
const NetworkRequest = require('../lib/network-request.js');
const Budget = require('../config/budget.js');
const {Util} = require('../util-commonjs.js');
/** @typedef {{count: number, resourceSize: number, transferSize: number}} ResourceEntry */
class ResourceSummary {
/**
* @param {LH.Artifacts.NetworkRequest} record
* @return {LH.Budget.ResourceType}
*/
static determineResourceType(record) {
if (!record.resourceType) return 'other';
/** @type {Partial<Record<LH.Crdp.Network.ResourceType, LH.Budget.ResourceType>>} */
const requestToResourceType = {
'Stylesheet': 'stylesheet',
'Image': 'image',
'Media': 'media',
'Font': 'font',
'Script': 'script',
'Document': 'document',
};
return requestToResourceType[record.resourceType] || 'other';
}
/**
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
* @param {LH.Artifacts.URL} URLArtifact
* @param {ImmutableObject<LH.Budget[]|null>} budgets
* @return {Record<LH.Budget.ResourceType, ResourceEntry>}
*/
static summarize(networkRecords, URLArtifact, budgets) {
/** @type {Record<LH.Budget.ResourceType, ResourceEntry>} */
const resourceSummary = {
'stylesheet': {count: 0, resourceSize: 0, transferSize: 0},
'image': {count: 0, resourceSize: 0, transferSize: 0},
'media': {count: 0, resourceSize: 0, transferSize: 0},
'font': {count: 0, resourceSize: 0, transferSize: 0},
'script': {count: 0, resourceSize: 0, transferSize: 0},
'document': {count: 0, resourceSize: 0, transferSize: 0},
'other': {count: 0, resourceSize: 0, transferSize: 0},
'total': {count: 0, resourceSize: 0, transferSize: 0},
'third-party': {count: 0, resourceSize: 0, transferSize: 0},
};
const budget = Budget.getMatchingBudget(budgets, URLArtifact.mainDocumentUrl);
/** @type {ReadonlyArray<string>} */
let firstPartyHosts = [];
if (budget?.options?.firstPartyHostnames) {
firstPartyHosts = budget.options.firstPartyHostnames;
} else {
const rootDomain = Util.getRootDomain(URLArtifact.finalUrl);
firstPartyHosts = [`*.${rootDomain}`];
}
networkRecords.filter(record => {
// Ignore favicon.co
// Headless Chrome does not request /favicon.ico, so don't consider this request.
// Makes resource summary consistent across LR / other channels.
const type = this.determineResourceType(record);
if (type === 'other' && record.url.endsWith('/favicon.ico')) {
return false;
}
// Ignore non-network protocols
if (NetworkRequest.isNonNetworkRequest(record)) return false;
return true;
}).forEach((record) => {
const type = this.determineResourceType(record);
resourceSummary[type].count++;
resourceSummary[type].resourceSize += record.resourceSize;
resourceSummary[type].transferSize += record.transferSize;
resourceSummary.total.count++;
resourceSummary.total.resourceSize += record.resourceSize;
resourceSummary.total.transferSize += record.transferSize;
const isFirstParty = firstPartyHosts.some((hostExp) => {
const url = new URL(record.url);
if (hostExp.startsWith('*.')) {
return url.hostname.endsWith(hostExp.slice(2));
}
return url.hostname === hostExp;
});
if (!isFirstParty) {
resourceSummary['third-party'].count++;
resourceSummary['third-party'].resourceSize += record.resourceSize;
resourceSummary['third-party'].transferSize += record.transferSize;
}
});
return resourceSummary;
}
/**
* @param {{URL: LH.Artifacts['URL'], devtoolsLog: LH.DevtoolsLog, budgets: ImmutableObject<LH.Budget[]|null>}} data
* @param {LH.Artifacts.ComputedContext} context
* @return {Promise<Record<LH.Budget.ResourceType,ResourceEntry>>}
*/
static async compute_(data, context) {
const networkRecords = await NetworkRecords.request(data.devtoolsLog, context);
return ResourceSummary.summarize(networkRecords, data.URL, data.budgets);
}
}
module.exports = makeComputedArtifact(ResourceSummary, ['URL', 'devtoolsLog', 'budgets']);