@@ -32,7 +32,8 @@ var paths = {
3232 cancel : 'cancelTest.php' ,
3333 history : 'testlog.php' ,
3434 videoCreation : 'video/create.php' ,
35- videoView : 'video/view.php'
35+ videoView : 'video/view.php' ,
36+ googleCsi : 'google/google_csi.php'
3637} ;
3738
3839var filenames = {
@@ -120,6 +121,18 @@ function get(config, pathname, callback, encoding) {
120121 } ) ;
121122}
122123
124+ // execute callback properly normalizing optional args
125+ function callbackYield ( callback , err , data , options ) {
126+ if ( typeof callback === 'function' ) {
127+ callback . apply ( callback , [ err , data ] . concat ( options . args ) ) ;
128+ }
129+ }
130+
131+ // helper for async parser callback
132+ function asyncParserCallback ( options , err , data ) {
133+ callbackYield ( this , err , data , options ) ;
134+ }
135+
123136// WPT API call wrapper
124137function api ( pathname , callback , query , options ) {
125138 var config ;
@@ -142,7 +155,7 @@ function api(pathname, callback, query, options) {
142155
143156 // dry run: return the API url (string) only
144157 if ( typeof callback === 'function' ) {
145- callback . apply ( this ,
158+ callback . apply ( callback ,
146159 [ undefined , helper . dryRun ( config , pathname ) ]
147160 ) ;
148161 }
@@ -154,19 +167,21 @@ function api(pathname, callback, query, options) {
154167 if ( ! err ) {
155168 try {
156169 if ( options . parser ) {
157- data = options . parser ( data ) ;
170+ // async parser
171+ if ( options . parser . async ) {
172+ return options . parser ( data ,
173+ asyncParserCallback . bind ( callback , options ) ) ;
174+ } else {
175+ data = options . parser ( data ) ;
176+ }
158177 } else {
159178 if ( ! data ) {
160179 data = { } ;
161180 } else if ( info . type === 'application/json' ) {
162181 data = JSON . parse ( data ) ;
163182 } else if ( info . type === 'text/xml' ) {
164- helper . xmlToObj ( data , function ( err , obj ) {
165- if ( typeof callback === 'function' ) {
166- callback . apply ( this , [ err , obj ] . concat ( options . args ) ) ;
167- }
168- } ) ;
169- return ;
183+ return helper . xmlToObj ( data ,
184+ asyncParserCallback . bind ( callback , options ) ) ;
170185 } else if ( info . type === 'text/html' ) {
171186 data = { result : ( reHTMLOutput . exec ( data ) || [ ] ) [ 1 ] } ;
172187 }
@@ -176,10 +191,8 @@ function api(pathname, callback, query, options) {
176191 }
177192 }
178193
179- if ( typeof callback === 'function' ) {
180- callback . apply ( this , [ err , data ] . concat ( options . args ) ) ;
181- }
182- } , options . encoding ) ;
194+ callbackYield ( callback , err , data , options ) ;
195+ } . bind ( this ) , options . encoding ) ;
183196
184197 }
185198
@@ -188,20 +201,32 @@ function api(pathname, callback, query, options) {
188201}
189202
190203// Set the appropriate filename to be requested
191- function setFilename ( input , options ) {
204+ function setFilename ( input , options , doNotDefault ) {
192205 var run , cached ;
193206
194207 options = options || { } ;
195208
196- run = parseInt ( options . run || options . r , 10 ) || 1 ;
209+ // set run and cached with or without defaults
210+ run = parseInt ( options . run || options . r , 10 ) ||
211+ ( doNotDefault ? undefined : 1 ) ;
197212 cached = ( options . repeatView || options . cached || options . c ) ?
198213 filenames . cached : '' ;
214+ // when falsy, check set default accordingly
215+ if ( doNotDefault && ! cached ) {
216+ cached = [ 'repeatView' , 'cached' , 'c' ] . some ( function ( key ) {
217+ return key in options ;
218+ } ) ? '' : undefined ;
219+ }
199220
200221 if ( typeof input === 'string' ) {
201222 return run + cached + '_' + input ;
202223 } else {
203- input . run = run ;
204- input . cached = cached ? 1 : 0 ;
224+ if ( run !== undefined ) {
225+ input . run = run ;
226+ }
227+ if ( cached !== undefined ) {
228+ input . cached = cached ? 1 : 0 ;
229+ }
205230 return input ;
206231 }
207232}
@@ -515,25 +540,29 @@ function getTestInfo(id, options, callback) {
515540}
516541
517542function getHistory ( days , options , callback ) {
543+ var query ;
544+
518545 callback = callback || options ;
519546 options = options === callback ? { } : options ;
520- var query = {
547+ options . parser = options . parser || helper . csvParser ;
548+ query = {
521549 all : 'on' ,
522550 f : 'csv' ,
523551 days : days ? parseInt ( days , 10 ) : 1
524552 } ;
525553
526- return api . call ( this , paths . history , function ( err , data ) {
527- if ( err ) {
528- return callback ( err ) ;
529- } else if ( ! ( data instanceof Buffer ) ) {
530- return callback ( err , data ) ;
531- }
554+ return api . call ( this , paths . history , callback , query , options ) ;
555+ }
532556
533- helper . csvParser ( data , function ( parsed ) {
534- callback ( err , parsed ) ;
535- } ) ;
536- } , query , options ) ;
557+ function getGoogleCsiData ( id , options , callback ) {
558+ var query ;
559+
560+ callback = callback || options ;
561+ options = options === callback ? { } : options ;
562+ options . parser = options . parser || helper . csvParser ;
563+ query = setFilename ( { test : id } , options , true ) ;
564+
565+ return api . call ( this , paths . googleCsi , callback , query , options ) ;
537566}
538567
539568function getWaterfallImage ( id , options , callback ) {
@@ -677,6 +706,7 @@ WebPageTest.prototype = {
677706 getHistory : getHistory ,
678707 getWaterfallImage : getWaterfallImage ,
679708 getScreenshotImage : getScreenshotImage ,
709+ getGoogleCsiData : getGoogleCsiData ,
680710 getEmbedVideoPlayer : getEmbedVideoPlayer ,
681711 createVideo : createVideo ,
682712 scriptToString : WebPageTest . scriptToString ,
@@ -698,6 +728,9 @@ WebPageTest.prototype = {
698728 console : getConsoleLogData ,
699729 testinfo : getTestInfo ,
700730 history : getHistory ,
731+ googlecsi : getGoogleCsiData ,
732+ player : getEmbedVideoPlayer ,
733+ video : createVideo ,
701734 waterfall : getWaterfallImage ,
702735 screenshot : getScreenshotImage
703736} ;
0 commit comments