@@ -2,6 +2,28 @@ import {getHeader} from "../../helpers/har";
2
2
import { Entry , Header } from "../../typing/har" ;
3
3
import { WaterfallEntry } from "../../typing/waterfall" ;
4
4
5
+ function parseAndFormat < S , T > ( source : S , parseFn : ( ( _ : S ) => T ) , formatFn : ( ( _ : T ) => string ) ) : string {
6
+ if ( typeof source === "undefined" ) {
7
+ return undefined ;
8
+ }
9
+ const parsed = parseFn ( source ) ;
10
+ if ( typeof parsed === "undefined" ) {
11
+ return undefined ;
12
+ }
13
+ return formatFn ( parsed ) ;
14
+ }
15
+
16
+ function parseNonNegative ( n : number ) : number {
17
+ if ( n < 0 ) {
18
+ return undefined ;
19
+ }
20
+ return n ;
21
+ }
22
+
23
+ function formatMilliseconds ( millis : number ) : string {
24
+ return `${ millis } ms` ;
25
+ }
26
+
5
27
let ifValueDefined = ( value : number , fn : ( _ : number ) => any ) => {
6
28
if ( ! isFinite ( value ) || value <= 0 ) {
7
29
return undefined ;
@@ -154,16 +176,17 @@ function parseResponseDetails(harEntry: Entry): KvTuple[] {
154
176
function parseTimings ( entry : WaterfallEntry ) : KvTuple [ ] {
155
177
const timings = entry . rawResource . timings ;
156
178
157
- // FIXME should only filter -1 values here, 0 is a valid timing.
179
+ const optionalTiming = ( timing ?: number ) => parseAndFormat ( timing , parseNonNegative , formatMilliseconds ) ;
180
+
158
181
return [
159
- [ "Total" , ` ${ entry . total } ms` ] ,
160
- [ "Blocked" , formatTime ( timings [ " blocked" ] ) ] ,
161
- [ "DNS" , formatTime ( timings [ " dns" ] ) ] ,
162
- [ "Connect" , formatTime ( timings [ " connect" ] ) ] ,
163
- [ "SSL (TLS)" , formatTime ( timings [ " ssl" ] ) ] ,
164
- [ "Send" , formatTime ( timings [ " send" ] ) ] ,
165
- [ "Wait" , formatTime ( timings [ " wait" ] ) ] ,
166
- [ "Receive" , formatTime ( timings [ " receive" ] ) ] ,
182
+ [ "Total" , formatMilliseconds ( entry . total ) ] ,
183
+ [ "Blocked" , optionalTiming ( timings . blocked ) ] ,
184
+ [ "DNS" , optionalTiming ( timings . dns ) ] ,
185
+ [ "Connect" , optionalTiming ( timings . connect ) ] ,
186
+ [ "SSL (TLS)" , optionalTiming ( timings . ssl ) ] ,
187
+ [ "Send" , formatMilliseconds ( timings . send ) ] ,
188
+ [ "Wait" , formatMilliseconds ( timings . wait ) ] ,
189
+ [ "Receive" , formatMilliseconds ( timings . receive ) ] ,
167
190
] ;
168
191
}
169
192
0 commit comments