@@ -194,103 +194,102 @@ class CacheHeaders extends Audit {
194
194
* @param {LH.Audit.Context } context
195
195
* @return {Promise<LH.Audit.Product> }
196
196
*/
197
- static audit ( artifacts , context ) {
197
+ static async audit ( artifacts , context ) {
198
198
const devtoolsLogs = artifacts . devtoolsLogs [ Audit . DEFAULT_PASS ] ;
199
- return NetworkRecords . request ( devtoolsLogs , context ) . then ( records => {
200
- const results = [ ] ;
201
- let totalWastedBytes = 0 ;
202
-
203
- for ( const record of records ) {
204
- if ( ! CacheHeaders . isCacheableAsset ( record ) ) continue ;
205
-
206
- /** @type {Map<string, string> } */
207
- const headers = new Map ( ) ;
208
- for ( const header of record . responseHeaders || [ ] ) {
209
- if ( headers . has ( header . name . toLowerCase ( ) ) ) {
210
- const previousHeaderValue = headers . get ( header . name . toLowerCase ( ) ) ;
211
- headers . set ( header . name . toLowerCase ( ) ,
212
- `${ previousHeaderValue } , ${ header . value } ` ) ;
213
- } else {
214
- headers . set ( header . name . toLowerCase ( ) , header . value ) ;
215
- }
216
- }
217
-
218
- const cacheControl = parseCacheControl ( headers . get ( 'cache-control' ) ) ;
219
- if ( this . shouldSkipRecord ( headers , cacheControl ) ) {
220
- continue ;
199
+ const records = await NetworkRecords . request ( devtoolsLogs , context ) ;
200
+ const results = [ ] ;
201
+ let totalWastedBytes = 0 ;
202
+
203
+ for ( const record of records ) {
204
+ if ( ! CacheHeaders . isCacheableAsset ( record ) ) continue ;
205
+
206
+ /** @type {Map<string, string> } */
207
+ const headers = new Map ( ) ;
208
+ for ( const header of record . responseHeaders || [ ] ) {
209
+ if ( headers . has ( header . name . toLowerCase ( ) ) ) {
210
+ const previousHeaderValue = headers . get ( header . name . toLowerCase ( ) ) ;
211
+ headers . set ( header . name . toLowerCase ( ) ,
212
+ `${ previousHeaderValue } , ${ header . value } ` ) ;
213
+ } else {
214
+ headers . set ( header . name . toLowerCase ( ) , header . value ) ;
221
215
}
216
+ }
222
217
223
- // Ignore if cacheLifetimeInSeconds is a nonpositive number.
224
- let cacheLifetimeInSeconds = CacheHeaders . computeCacheLifetimeInSeconds (
225
- headers , cacheControl ) ;
226
- if ( cacheLifetimeInSeconds !== null &&
227
- ( ! Number . isFinite ( cacheLifetimeInSeconds ) || cacheLifetimeInSeconds <= 0 ) ) {
228
- continue ;
229
- }
230
- cacheLifetimeInSeconds = cacheLifetimeInSeconds || 0 ;
231
-
232
- // Ignore assets whose cache lifetime is already high enough
233
- const cacheHitProbability = CacheHeaders . getCacheHitProbability ( cacheLifetimeInSeconds ) ;
234
- if ( cacheHitProbability > IGNORE_THRESHOLD_IN_PERCENT ) continue ;
235
-
236
- const url = UrlUtils . elideDataURI ( record . url ) ;
237
- const totalBytes = record . transferSize || 0 ;
238
- const wastedBytes = ( 1 - cacheHitProbability ) * totalBytes ;
239
-
240
- totalWastedBytes += wastedBytes ;
241
-
242
- // Include cacheControl info (if it exists) per url as a diagnostic.
243
- /** @type {LH.Audit.Details.DebugData|undefined } */
244
- let debugData ;
245
- if ( cacheControl ) {
246
- debugData = {
247
- type : 'debugdata' ,
248
- ...cacheControl ,
249
- } ;
250
- }
218
+ const cacheControl = parseCacheControl ( headers . get ( 'cache-control' ) ) ;
219
+ if ( this . shouldSkipRecord ( headers , cacheControl ) ) {
220
+ continue ;
221
+ }
251
222
252
- results . push ( {
253
- url,
254
- debugData,
255
- cacheLifetimeMs : cacheLifetimeInSeconds * 1000 ,
256
- cacheHitProbability,
257
- totalBytes,
258
- wastedBytes,
259
- } ) ;
223
+ // Ignore if cacheLifetimeInSeconds is a nonpositive number.
224
+ let cacheLifetimeInSeconds = CacheHeaders . computeCacheLifetimeInSeconds (
225
+ headers , cacheControl ) ;
226
+ if ( cacheLifetimeInSeconds !== null &&
227
+ ( ! Number . isFinite ( cacheLifetimeInSeconds ) || cacheLifetimeInSeconds <= 0 ) ) {
228
+ continue ;
229
+ }
230
+ cacheLifetimeInSeconds = cacheLifetimeInSeconds || 0 ;
231
+
232
+ // Ignore assets whose cache lifetime is already high enough
233
+ const cacheHitProbability = CacheHeaders . getCacheHitProbability ( cacheLifetimeInSeconds ) ;
234
+ if ( cacheHitProbability > IGNORE_THRESHOLD_IN_PERCENT ) continue ;
235
+
236
+ const url = UrlUtils . elideDataURI ( record . url ) ;
237
+ const totalBytes = record . transferSize || 0 ;
238
+ const wastedBytes = ( 1 - cacheHitProbability ) * totalBytes ;
239
+
240
+ totalWastedBytes += wastedBytes ;
241
+
242
+ // Include cacheControl info (if it exists) per url as a diagnostic.
243
+ /** @type {LH.Audit.Details.DebugData|undefined } */
244
+ let debugData ;
245
+ if ( cacheControl ) {
246
+ debugData = {
247
+ type : 'debugdata' ,
248
+ ...cacheControl ,
249
+ } ;
260
250
}
261
251
262
- results . sort ( ( a , b ) => {
263
- return a . cacheLifetimeMs - b . cacheLifetimeMs ||
264
- b . totalBytes - a . totalBytes ||
265
- a . url . localeCompare ( b . url ) ;
252
+ results . push ( {
253
+ url,
254
+ debugData,
255
+ cacheLifetimeMs : cacheLifetimeInSeconds * 1000 ,
256
+ cacheHitProbability,
257
+ totalBytes,
258
+ wastedBytes,
266
259
} ) ;
260
+ }
267
261
268
- const score = Audit . computeLogNormalScore (
269
- { p10 : context . options . p10 , median : context . options . median } ,
270
- totalWastedBytes
271
- ) ;
272
-
273
- /** @type {LH.Audit.Details.Table['headings'] } */
274
- const headings = [
275
- { key : 'url' , valueType : 'url' , label : str_ ( i18n . UIStrings . columnURL ) } ,
276
- // TODO(i18n): pre-compute localized duration
277
- { key : 'cacheLifetimeMs' , valueType : 'ms' , label : str_ ( i18n . UIStrings . columnCacheTTL ) ,
278
- displayUnit : 'duration' } ,
279
- { key : 'totalBytes' , valueType : 'bytes' , label : str_ ( i18n . UIStrings . columnTransferSize ) ,
280
- displayUnit : 'kb' , granularity : 1 } ,
281
- ] ;
282
-
283
- const summary = { wastedBytes : totalWastedBytes } ;
284
- const details = Audit . makeTableDetails ( headings , results , summary ) ;
285
-
286
- return {
287
- score,
288
- numericValue : totalWastedBytes ,
289
- numericUnit : 'byte' ,
290
- displayValue : str_ ( UIStrings . displayValue , { itemCount : results . length } ) ,
291
- details,
292
- } ;
262
+ results . sort ( ( a , b ) => {
263
+ return a . cacheLifetimeMs - b . cacheLifetimeMs ||
264
+ b . totalBytes - a . totalBytes ||
265
+ a . url . localeCompare ( b . url ) ;
293
266
} ) ;
267
+
268
+ const score = Audit . computeLogNormalScore (
269
+ { p10 : context . options . p10 , median : context . options . median } ,
270
+ totalWastedBytes
271
+ ) ;
272
+
273
+ /** @type {LH.Audit.Details.Table['headings'] } */
274
+ const headings = [
275
+ { key : 'url' , valueType : 'url' , label : str_ ( i18n . UIStrings . columnURL ) } ,
276
+ // TODO(i18n): pre-compute localized duration
277
+ { key : 'cacheLifetimeMs' , valueType : 'ms' , label : str_ ( i18n . UIStrings . columnCacheTTL ) ,
278
+ displayUnit : 'duration' } ,
279
+ { key : 'totalBytes' , valueType : 'bytes' , label : str_ ( i18n . UIStrings . columnTransferSize ) ,
280
+ displayUnit : 'kb' , granularity : 1 } ,
281
+ ] ;
282
+
283
+ const summary = { wastedBytes : totalWastedBytes } ;
284
+ const details = Audit . makeTableDetails ( headings , results , summary ) ;
285
+
286
+ return {
287
+ score,
288
+ numericValue : totalWastedBytes ,
289
+ numericUnit : 'byte' ,
290
+ displayValue : str_ ( UIStrings . displayValue , { itemCount : results . length } ) ,
291
+ details,
292
+ } ;
294
293
}
295
294
}
296
295
0 commit comments