-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
51 lines (44 loc) · 1.56 KB
/
utils.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var https = require('https')
var url = require('url')
var querystring = require('querystring')
// Just like normal Node.js https request, but supports `url`, `body` and `timeout` params
exports.request = function(options, cb) {
cb = exports.once(cb)
if (options.url) {
var parsedUrl = url.parse(options.url)
options.hostname = parsedUrl.hostname
options.path = parsedUrl.path
delete options.url
}
if (options.body) {
options.method = options.method || 'POST'
options.headers = options.headers || {}
if (typeof options.body != 'string' && !Buffer.isBuffer(options.body)) {
var contentType = options.headers['Content-Type'] || options.headers['content-type']
options.body = contentType == 'application/x-www-form-urlencoded' ?
querystring.stringify(options.body) : JSON.stringify(options.body)
}
var contentLength = options.headers['Content-Length'] || options.headers['content-length']
if (!contentLength) options.headers['Content-Length'] = Buffer.byteLength(options.body)
}
var req = https.request(options, function(res) {
var data = ''
res.setEncoding('utf8')
res.on('error', cb)
res.on('data', function(chunk) { data += chunk })
res.on('end', function() { cb(null, res, data) })
}).on('error', cb)
if (options.timeout != null) {
req.setTimeout(options.timeout)
req.on('timeout', function() { req.abort() })
}
req.end(options.body)
}
exports.once = function(cb) {
var called = false
return function() {
if (called) return
called = true
cb.apply(this, arguments)
}
}