-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path_read.js
98 lines (82 loc) · 2.61 KB
/
_read.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var http = require('http')
var https = require('https')
var url = require('url')
var qs = require('querystring')
module.exports = function _read(httpMethod, options, callback) {
// deep copy options
options = JSON.parse(JSON.stringify(options))
// alias body = data
if (options.body && !options.data) {
options.data = options.body
}
// require options.url or fail noisily
if (!options.url) {
throw Error('options.url required')
}
// setup promise if there is no callback
var promise
if (!callback) {
promise = new Promise(function(res, rej) {
callback = function(err, result) {
err ? rej(err) : res(result)
}
})
}
// parse out the options from options.url
var opts = url.parse(options.url)
var method = opts.protocol === 'https:' ? https.request : http.request
var defaultContentType = 'application/json; charset=utf-8'
// check for additional query params
if (options.data) {
var isSearch = !!opts.search
options.url += (isSearch? '&' : '?') + qs.stringify(options.data)
opts = url.parse(options.url)
}
// add timeout if it exists
if (options.timeout) {
opts.timeout = options.timeout
}
// wrangle defaults
opts.method = httpMethod
opts.headers = options.headers || {}
opts.headers['user-agent'] = opts.headers['user-agent'] || opts.headers['User-Agent'] || 'tiny-http'
opts.headers['content-type'] = opts.headers['content-type'] || opts.headers['Content-Type'] || defaultContentType
// make a request
var req = method(opts, function _res(res) {
var raw = [] // keep our buffers here
var ok = res.statusCode >= 200 && res.statusCode < 303
res.on('data', function _data(chunk) {
raw.push(chunk)
})
res.on('end', function _end() {
var err = null
var result = null
var isJSON = res.headers['content-type'] &&
(res.headers['content-type'].startsWith('application/json') ||
res.headers['content-type'].match(/^application\/.*json/))
try {
result = Buffer.concat(raw)
if (!options.buffer) {
var strRes = result.toString()
result = strRes && isJSON ? JSON.parse(strRes) : strRes
}
}
catch(e) {
err = e
}
if (!ok) {
err = Error('GET failed with: ' + res.statusCode)
err.raw = res
err.body = isJSON? JSON.stringify(result) : result.toString()
err.statusCode = res.statusCode
callback(err)
}
else {
callback(err, {body:result, headers:res.headers})
}
})
})
req.on('error', callback)
req.end()
return promise
}