@@ -98,16 +98,16 @@ function install(opts, cb) {
98
98
} ) ;
99
99
100
100
function onlyInstallMissingFiles ( opts , cb ) {
101
- async . series ( [
101
+ async . waterfall ( [
102
102
checksum . bind ( null , opts . to ) ,
103
- etag . bind ( null , opts . from , requestOpts )
104
- ] , function ( error , results ) {
103
+ isUpToDate . bind ( null , opts . from , requestOpts )
104
+ ] , function ( error , isLatest ) {
105
105
if ( error ) {
106
106
return cb ( error ) ;
107
107
}
108
108
109
109
// File already exists. Prevent download/installation.
110
- if ( results [ 0 ] === results [ 1 ] ) {
110
+ if ( isLatest ) {
111
111
logger ( '---' ) ;
112
112
logger ( 'File from ' + opts . from + ' has already been downloaded' ) ;
113
113
expectedRequests -= 1 ;
@@ -391,7 +391,7 @@ function logInstallSummary(logger, paths, urls) {
391
391
392
392
function checksum ( filepath , cb ) {
393
393
if ( ! fs . existsSync ( filepath ) ) {
394
- return cb ( ) ;
394
+ return cb ( null , null ) ;
395
395
}
396
396
397
397
var hash = crypto . createHash ( 'md5' ) ;
@@ -414,13 +414,31 @@ function unquote (str, quoteChar) {
414
414
return str ;
415
415
}
416
416
417
- function etag ( url , requestOpts , cb ) {
418
- request . head ( url , requestOpts ) . on ( 'response' , function ( res ) {
417
+ function isUpToDate ( url , requestOpts , hash , cb ) {
418
+ if ( ! hash ) {
419
+ return cb ( null , false ) ;
420
+ }
421
+
422
+ var query = Object . assign ( { } , requestOpts , {
423
+ url : url ,
424
+ headers : {
425
+ 'If-None-Match' : '"' + hash + '"'
426
+ }
427
+ } ) ;
428
+
429
+ var req = request . get ( query ) ;
430
+ req . on ( 'response' , function ( res ) {
431
+ req . abort ( ) ;
432
+
433
+ if ( res . statusCode === 304 ) {
434
+ return cb ( null , true ) ;
435
+ }
436
+
419
437
if ( res . statusCode !== 200 ) {
420
438
return cb ( new Error ( 'Could not request headers from ' + url + ': ' , res . statusCodestatusCode ) ) ;
421
439
}
422
440
423
- cb ( null , unquote ( res . headers . etag ) ) ;
441
+ cb ( null , false ) ;
424
442
} ) . once ( 'error' , function ( err ) {
425
443
cb ( new Error ( 'Could not request headers from ' + url + ': ' + err ) ) ;
426
444
} ) ;
0 commit comments