forked from evanlucas/remote-file-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (29 loc) · 792 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict'
var request = require('request')
module.exports = function(options, cb) {
if ('string' === typeof options) {
options = {
uri: options
}
}
options = options || {}
options.method = 'HEAD'
options.followAllRedirects = true
options.followOriginalHttpMethod = true
request(options, function(err, res, body) {
if (err) return cb(err)
const code = res.statusCode
if (code >= 400) {
return cb(new Error('Received invalid status code: ' + code))
}
var len = res.headers['content-length']
if (!len) {
return cb(new Error('Unable to determine file size'))
}
len = +len
if (len !== len) {
return cb(new Error('Invalid Content-Length received'))
}
cb(null, +res.headers['content-length'])
})
}