22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5- import { NAVIGATION_TIMING_FIELDS , OPTIONAL_NAVIGATION_TIMING_FIELDS } from './timing-fields' ;
5+ import {
6+ NAVIGATION_TIMING_FIELDS ,
7+ OPTIONAL_NAVIGATION_TIMING_FIELDS ,
8+ } from './timing-fields' ;
69
710const L2TimingsMap = {
8- ' navigationStart' : 'startTime' ,
9- ' domLoading' : 'domContentLoadedEventStart'
10- }
11+ navigationStart : 'startTime' ,
12+ domLoading : 'domContentLoadedEventStart' ,
13+ } ;
1114
1215const TimingVersions = {
1316 L2 : 'L2' ,
1417 L1 : 'L1' ,
15- UNKNOWN : ''
16- }
18+ UNKNOWN : '' ,
19+ } ;
1720
1821class NavigationTiming {
1922 init ( opts ) {
20-
2123 // A performance api must be provided
2224 if ( ! opts || ! opts . performance ) {
23- throw new Error ( 'opts.performance is required!' )
25+ throw new Error ( 'opts.performance is required!' ) ;
2426 }
2527 this . performance = opts . performance ;
2628 this . useL1Timings = opts . useL1Timings ;
2729 }
2830
29- getTimingVersion ( ) {
30- const version = this . getL2Timings ( ) ? TimingVersions . L2 :
31- this . getL1Timings ( ) ? TimingVersions . L1 :
32- TimingVersions . UNKNOWN ;
31+ getTimingVersion ( ) {
32+ const version = this . getL2Timings ( )
33+ ? TimingVersions . L2
34+ : this . getL1Timings ( )
35+ ? TimingVersions . L1
36+ : TimingVersions . UNKNOWN ;
3337 return version ;
3438 }
3539
3640 getL2Timings ( ) {
3741 if (
3842 ! ! this . performance &&
3943 ! ! this . performance . getEntriesByType &&
40- ! ! this . performance . getEntriesByType ( 'navigation' ) )
41- {
42- return this . performance . getEntriesByType ( 'navigation' ) [ 0 ]
44+ ! ! this . performance . getEntriesByType ( 'navigation' )
45+ ) {
46+ return this . performance . getEntriesByType ( 'navigation' ) [ 0 ] ;
4347 }
4448 }
4549
@@ -48,14 +52,13 @@ class NavigationTiming {
4852 }
4953
5054 diff ( ) {
51-
5255 // If we are using our fallback performance api (ie window.performance
5356 // doesn't exist), don't return anything.
5457 if ( this . performance . unreliable === true ) {
5558 return undefined ;
5659 }
5760
58- const diff = { }
61+ const diff = { } ;
5962 const l2Timings = this . getL2Timings ( ) ;
6063 const l1Timings = this . getL1Timings ( ) ;
6164
@@ -64,11 +67,13 @@ class NavigationTiming {
6467 for ( const key in NAVIGATION_TIMING_FIELDS ) {
6568 const timing = l1Timings [ key ] ;
6669
67- if ( timing === 0 && OPTIONAL_NAVIGATION_TIMING_FIELDS . indexOf ( key ) >= 0 ) {
70+ if (
71+ timing === 0 &&
72+ OPTIONAL_NAVIGATION_TIMING_FIELDS . indexOf ( key ) >= 0
73+ ) {
6874 // A time value of 0 for certain fields indicates a non-applicable value. Set to null.
6975 diff [ key ] = null ;
70- }
71- else {
76+ } else {
7277 // Compute the delta relative to navigation start. This removes any
7378 // ambiguity around what the 'start' or 'baseTime' time is. Since we
7479 // are sure the current set of navigation timings were created using
@@ -78,7 +83,7 @@ class NavigationTiming {
7883 }
7984 }
8085
81- function diffL2 ( ) {
86+ function diffL2 ( ) {
8287 // If we have level 2 timings we can almost return the timings directly. We just have massage
8388 // a couple fields to keep it backwards compatible.
8489 for ( const key in NAVIGATION_TIMING_FIELDS ) {
@@ -90,18 +95,22 @@ class NavigationTiming {
9095 // Case for testing. We should always try to use l2, but if explicitly requested use L1.
9196 if ( this . useL1Timings && l1Timings ) {
9297 diffL1 ( ) ;
93- }
94- else if ( l2Timings ) {
98+ } else if ( l2Timings ) {
9599 diffL2 ( ) ;
96- }
97- else if ( l1Timings ) {
100+ } else if ( l1Timings ) {
98101 diffL1 ( ) ;
99102 }
100103
101- // We shouldn't see any negative values. If we do something went very wrong.
102- // We will use -11111 as a magic number to ensure a sentry error is captured,
103- // and it's easy to spot.
104104 for ( const key in NAVIGATION_TIMING_FIELDS ) {
105+ // We expect that all values are integer values, Chrome will produce floating
106+ // point values that we must convert.
107+ if ( typeof diff [ key ] === 'number' ) {
108+ diff [ key ] = parseInt ( diff [ key ] ) ;
109+ }
110+
111+ // We shouldn't see any negative values. If we do something went very wrong.
112+ // We will use -11111 as a magic number to ensure a sentry error is captured,
113+ // and it's easy to spot.
105114 if ( diff [ key ] < 0 ) {
106115 diff [ key ] = - 11111 ;
107116 }
0 commit comments