index.js
6.6 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict';
var http = require('http');
var https = require('https');
var urlLib = require('url');
var util = require('util');
var zlib = require('zlib');
var querystring = require('querystring');
var objectAssign = require('object-assign');
var infinityAgent = require('infinity-agent');
var duplexify = require('duplexify');
var isStream = require('is-stream');
var readAllStream = require('read-all-stream');
var timedOut = require('timed-out');
var prependHttp = require('prepend-http');
var lowercaseKeys = require('lowercase-keys');
var isRedirect = require('is-redirect');
var NestedErrorStacks = require('nested-error-stacks');
function GotError(message, nested) {
NestedErrorStacks.call(this, message, nested);
objectAssign(this, nested, {nested: this.nested});
}
util.inherits(GotError, NestedErrorStacks);
GotError.prototype.name = 'GotError';
function got(url, opts, cb) {
if (typeof url !== 'string' && typeof url !== 'object') {
throw new GotError('Parameter `url` must be a string or object, not ' + typeof url);
}
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = objectAssign(
{
protocol: 'http:'
},
typeof url === 'string' ? urlLib.parse(prependHttp(url)) : url,
opts
);
opts.headers = objectAssign({
'user-agent': 'https://github.com/sindresorhus/got',
'accept-encoding': 'gzip,deflate'
}, lowercaseKeys(opts.headers));
if (opts.pathname) {
opts.path = opts.pathname;
}
if (opts.query) {
if (typeof opts.query !== 'string') {
opts.query = querystring.stringify(opts.query);
}
opts.path = opts.pathname + '?' + opts.query;
delete opts.query;
}
var encoding = opts.encoding;
var body = opts.body;
var json = opts.json;
var timeout = opts.timeout;
var proxy;
var redirectCount = 0;
delete opts.encoding;
delete opts.body;
delete opts.json;
delete opts.timeout;
if (json) {
opts.headers.accept = opts.headers.accept || 'application/json';
}
if (body) {
if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream.readable(body)) {
throw new GotError('options.body must be a ReadableStream, string or Buffer');
}
opts.method = opts.method || 'POST';
if (!opts.headers['content-length'] && !opts.headers['transfer-encoding'] && !isStream.readable(body)) {
var length = typeof body === 'string' ? Buffer.byteLength(body) : body.length;
opts.headers['content-length'] = length;
}
}
opts.method = opts.method || 'GET';
// returns a proxy stream to the response
// if no callback has been provided
if (!cb) {
proxy = duplexify();
// forward errors on the stream
cb = function (err, data, response) {
proxy.emit('error', err, data, response);
};
}
if (proxy && json) {
throw new GotError('got can not be used as stream when options.json is used');
}
function get(opts, cb) {
var fn = opts.protocol === 'https:' ? https : http;
var url = urlLib.format(opts);
if (opts.agent === undefined) {
opts.agent = infinityAgent[fn === https ? 'https' : 'http'].globalAgent;
if (process.version.indexOf('v0.10') === 0 && fn === https && (
typeof opts.ca !== 'undefined' ||
typeof opts.cert !== 'undefined' ||
typeof opts.ciphers !== 'undefined' ||
typeof opts.key !== 'undefined' ||
typeof opts.passphrase !== 'undefined' ||
typeof opts.pfx !== 'undefined' ||
typeof opts.rejectUnauthorized !== 'undefined')) {
opts.agent = new infinityAgent.https.Agent({
ca: opts.ca,
cert: opts.cert,
ciphers: opts.ciphers,
key: opts.key,
passphrase: opts.passphrase,
pfx: opts.pfx,
rejectUnauthorized: opts.rejectUnauthorized
});
}
}
var req = fn.request(opts, function (response) {
var statusCode = response.statusCode;
var res = response;
// auto-redirect only for GET and HEAD methods
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
// discard response
res.resume();
if (++redirectCount > 10) {
cb(new GotError('Redirected 10 times. Aborting.'), undefined, res);
return;
}
var redirectUrl = urlLib.resolve(url, res.headers.location);
var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl));
if (opts.agent === infinityAgent.http.globalAgent && redirectOpts.protocol === 'https:' && opts.protocol === 'http:') {
redirectOpts.agent = undefined;
}
if (proxy) {
proxy.emit('redirect', res, redirectOpts);
}
get(redirectOpts, cb);
return;
}
if (proxy) {
proxy.emit('response', res);
}
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
res = res.pipe(zlib.createUnzip());
}
if (statusCode < 200 || statusCode > 299) {
readAllStream(res, encoding, function (err, data) {
err = new GotError(opts.method + ' ' + url + ' response code is ' + statusCode + ' (' + http.STATUS_CODES[statusCode] + ')', err);
err.code = statusCode;
if (data && json) {
try {
data = JSON.parse(data);
} catch (e) {
err = new GotError('Parsing ' + url + ' response failed', new GotError(e.message, err));
}
}
cb(err, data, response);
});
return;
}
// pipe the response to the proxy if in proxy mode
if (proxy) {
proxy.setReadable(res);
return;
}
readAllStream(res, encoding, function (err, data) {
if (err) {
err = new GotError('Reading ' + url + ' response failed', err);
} else if (json && statusCode !== 204) {
// only parse json if the option is enabled, and the response
// is not a 204 (empty reponse)
try {
data = JSON.parse(data);
} catch (e) {
err = new GotError('Parsing ' + url + ' response failed', e);
}
}
cb(err, data, response);
});
}).once('error', function (err) {
cb(new GotError('Request to ' + url + ' failed', err));
});
if (timeout) {
timedOut(req, timeout);
}
if (!proxy) {
if (isStream.readable(body)) {
body.pipe(req);
} else {
req.end(body);
}
return;
}
if (body) {
proxy.write = function () {
throw new Error('got\'s stream is not writable when options.body is used');
};
if (isStream.readable(body)) {
body.pipe(req);
} else {
req.end(body);
}
return;
}
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
proxy.setWritable(req);
return;
}
req.end();
}
get(opts, cb);
return proxy;
}
[
'get',
'post',
'put',
'patch',
'head',
'delete'
].forEach(function (el) {
got[el] = function (url, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return got(url, objectAssign({}, opts, {method: el.toUpperCase()}), cb);
};
});
module.exports = got;