-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
config-plugin.js
250 lines (220 loc) · 7.48 KB
/
config-plugin.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as i18n from '../lib/i18n/i18n.js';
/**
* @param {unknown} arr
* @return {arr is Array<Record<string, unknown>>}
*/
function isArrayOfUnknownObjects(arr) {
return Array.isArray(arr) && arr.every(isObjectOfUnknownProperties);
}
/**
* @param {unknown} val
* @return {val is Record<string, unknown>}
*/
function isObjectOfUnknownProperties(val) {
return typeof val === 'object' && val !== null && !Array.isArray(val);
}
/**
* @param {unknown} str
* @return {str is LH.Gatherer.GatherMode}
*/
function objectIsGatherMode(str) {
if (typeof str !== 'string') return false;
return str === 'navigation' || str === 'timespan' || str === 'snapshot';
}
/**
* @param {unknown} arr
* @return {arr is Array<LH.Gatherer.GatherMode>}
*/
function isArrayOfGatherModes(arr) {
if (!Array.isArray(arr)) return false;
return arr.every(objectIsGatherMode);
}
/**
* Asserts that obj has no own properties, throwing a nice error message if it does.
* Plugin and object name are included for nicer logging.
* @param {Record<string, unknown>} obj
* @param {string} pluginName
* @param {string=} objectName
*/
function assertNoExcessProperties(obj, pluginName, objectName = '') {
if (objectName) {
objectName += ' ';
}
const invalidKeys = Object.keys(obj);
if (invalidKeys.length > 0) {
const keys = invalidKeys.join(', ');
throw new Error(`${pluginName} has unrecognized ${objectName}properties: [${keys}]`);
}
}
/**
* A set of methods for extracting and validating a Lighthouse plugin config.
*/
class ConfigPlugin {
/**
* Extract and validate the list of AuditDefns added by the plugin (or undefined
* if no additional audits are being added by the plugin).
* @param {unknown} auditsJson
* @param {string} pluginName
* @return {Array<{path: string}>|undefined}
*/
static _parseAuditsList(auditsJson, pluginName) {
// Plugin audits aren't required (relying on LH default audits) so fall back to [].
if (auditsJson === undefined) {
return undefined;
} else if (!isArrayOfUnknownObjects(auditsJson)) {
throw new Error(`${pluginName} has an invalid audits array.`);
}
return auditsJson.map(auditDefnJson => {
const {path, ...invalidRest} = auditDefnJson;
assertNoExcessProperties(invalidRest, pluginName, 'audit');
if (typeof path !== 'string') {
throw new Error(`${pluginName} has a missing audit path.`);
}
return {
path,
};
});
}
/**
* Extract and validate the list of category AuditRefs added by the plugin.
* @param {unknown} auditRefsJson
* @param {string} pluginName
* @return {Array<LH.Config.AuditRefJson>}
*/
static _parseAuditRefsList(auditRefsJson, pluginName) {
if (!isArrayOfUnknownObjects(auditRefsJson)) {
throw new Error(`${pluginName} has no valid auditsRefs.`);
}
return auditRefsJson.map(auditRefJson => {
const {id, weight, group, ...invalidRest} = auditRefJson;
assertNoExcessProperties(invalidRest, pluginName, 'auditRef');
if (typeof id !== 'string') {
throw new Error(`${pluginName} has an invalid auditRef id.`);
}
if (typeof weight !== 'number') {
throw new Error(`${pluginName} has an invalid auditRef weight.`);
}
if (typeof group !== 'string' && typeof group !== 'undefined') {
throw new Error(`${pluginName} has an invalid auditRef group.`);
}
const prependedGroup = group ? `${pluginName}-${group}` : group;
return {
id,
weight,
group: prependedGroup,
};
});
}
/**
* Extract and validate the category added by the plugin.
* @param {unknown} categoryJson
* @param {string} pluginName
* @return {LH.Config.CategoryJson}
*/
static _parseCategory(categoryJson, pluginName) {
if (!isObjectOfUnknownProperties(categoryJson)) {
throw new Error(`${pluginName} has no valid category.`);
}
const {
title,
description,
manualDescription,
auditRefs: auditRefsJson,
supportedModes,
...invalidRest
} = categoryJson;
assertNoExcessProperties(invalidRest, pluginName, 'category');
if (!i18n.isStringOrIcuMessage(title)) {
throw new Error(`${pluginName} has an invalid category tile.`);
}
if (!i18n.isStringOrIcuMessage(description) && description !== undefined) {
throw new Error(`${pluginName} has an invalid category description.`);
}
if (!i18n.isStringOrIcuMessage(manualDescription) && manualDescription !== undefined) {
throw new Error(`${pluginName} has an invalid category manualDescription.`);
}
if (!isArrayOfGatherModes(supportedModes) && supportedModes !== undefined) {
throw new Error(
`${pluginName} supportedModes must be an array, ` +
`valid array values are "navigation", "timespan", and "snapshot".`
);
}
const auditRefs = ConfigPlugin._parseAuditRefsList(auditRefsJson, pluginName);
return {
title,
auditRefs,
description: description,
manualDescription: manualDescription,
supportedModes,
};
}
/**
* Extract and validate groups JSON added by the plugin.
* @param {unknown} groupsJson
* @param {string} pluginName
* @return {Record<string, LH.Config.GroupJson>|undefined}
*/
static _parseGroups(groupsJson, pluginName) {
if (groupsJson === undefined) {
return undefined;
}
if (!isObjectOfUnknownProperties(groupsJson)) {
throw new Error(`${pluginName} groups json is not defined as an object.`);
}
const groups = Object.entries(groupsJson);
/** @type {Record<string, LH.Config.GroupJson>} */
const parsedGroupsJson = {};
groups.forEach(([groupId, groupJson]) => {
if (!isObjectOfUnknownProperties(groupJson)) {
throw new Error(`${pluginName} has a group not defined as an object.`);
}
const {title, description, ...invalidRest} = groupJson;
assertNoExcessProperties(invalidRest, pluginName, 'group');
if (!i18n.isStringOrIcuMessage(title)) {
throw new Error(`${pluginName} has an invalid group title.`);
}
if (!i18n.isStringOrIcuMessage(description) && description !== undefined) {
throw new Error(`${pluginName} has an invalid group description.`);
}
parsedGroupsJson[`${pluginName}-${groupId}`] = {
title,
description,
};
});
return parsedGroupsJson;
}
/**
* Extracts and validates a config from the provided plugin input, throwing
* if it deviates from the expected object shape.
* @param {unknown} pluginJson
* @param {string} pluginName
* @return {LH.Config}
*/
static parsePlugin(pluginJson, pluginName) {
// Clone to prevent modifications of original and to deactivate any live properties.
pluginJson = JSON.parse(JSON.stringify(pluginJson));
if (!isObjectOfUnknownProperties(pluginJson)) {
throw new Error(`${pluginName} is not defined as an object.`);
}
const {
audits: pluginAuditsJson,
category: pluginCategoryJson,
groups: pluginGroupsJson,
...invalidRest
} = pluginJson;
assertNoExcessProperties(invalidRest, pluginName);
return {
audits: ConfigPlugin._parseAuditsList(pluginAuditsJson, pluginName),
categories: {
[pluginName]: ConfigPlugin._parseCategory(pluginCategoryJson, pluginName),
},
groups: ConfigPlugin._parseGroups(pluginGroupsJson, pluginName),
};
}
}
export default ConfigPlugin;