-
Notifications
You must be signed in to change notification settings - Fork 9
/
Teepee.js
1106 lines (1024 loc) · 35.2 KB
/
Teepee.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global JSON, setTimeout, setImmediate */
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const urlModule = require('url');
const fs = require('fs');
const zlib = require('zlib');
const HttpError = require('httperrors');
const SocketError = require('socketerrors');
const DnsError = require('dnserrors');
const createError = require('createerror');
const os = require('os');
const passError = require('passerror');
const isStream = require('is-stream');
const FormData = require('form-data');
const http = require('http');
const https = require('https');
const SelfRedirectError = createError({ name: 'SelfRedirect' });
const omit = require('lodash.omit');
const uniq = require('lodash.uniq');
const clone = require('lodash.clone');
const defaults = require('lodash.defaults');
function isContentTypeJson(contentType) {
return /^application\/json\b|\+json\b/i.test(contentType);
}
function resolveCertKeyOrCa(value) {
if (typeof value === 'string') {
return fs.readFileSync(value.replace(/\{hostname\}/g, os.hostname()));
} else if (Array.isArray(value)) {
// An array of ca file names
return value.map(resolveCertKeyOrCa);
} else {
return value;
}
}
function safeDecodeURIComponent(str) {
try {
return decodeURIComponent(str);
} catch (e) {
// Assume URIError: URI malformed (percent encoded octets that don't decode as UTF-8)
return str;
}
}
/*
* config.url {String} The base url for all requests
* config.headers {Object} (optional) Default headers to send for every request (headers passed to request take precedence).
* config.numRetries {Number} (optional) The number of times to retry an operation if it fails due to a non-HTTP error such
* as a socket timeout. Defaults to 0.
* config.timeout {Number} (optional) The maximum number of milliseconds to wait for the request to complete. If combined with retry,
* the timeout will apply to the individual request, not the sequence of requests.
* config.rejectUnauthorized {Boolean} (optional) Whether to consider an HTTPS request failed if the remote cert doesn't validate.
* config.agent {Object} (optional) The HTTP/HTTPS agent to use. Defaults to use the global agent for the given protocol.
* Pass true to create a new agent for the Teepee instance using the default constructors.
* config.maxSockets {Number} (optional) The maximum number of simultaneous connections to support. Only used if config.agent isn't provided.
* config.keepAlive {Number} (optional) Passed to the Agent constructor. Only used if config.agent isn't provided.
* config.keepAliveMsecs {Number} (optional) Passed to the Agent constructor. Only used if config.agent isn't provided.
* config.maxSockets {Number} (optional) Passed to the Agent constructor. Only used if config.agent isn't provided.
* config.maxFreeSockets {Number} (optional) Passed to the Agent constructor. Only used if config.agent isn't provided.
* config.cert {Buffer} (optional) The certificate to use. Only used if config.agent isn't provided.
* config.key {Buffer} (optional) The certificate key to use. Only used if config.agent isn't provided.
* config.ca {Buffer} (optional) The certificate authority (CA) to use. Only used if config.agent isn't provided.
*/
function Teepee(config) {
if (!(this instanceof Teepee)) {
// Invoked without new, shorthand for issuing a request
const args = Array.prototype.slice.call(arguments);
let teepee;
if (
typeof args[0] === 'string' ||
(args[0] && typeof args[0] === 'object')
) {
teepee = new Teepee(args.shift());
}
return teepee.request.apply(teepee, args);
}
EventEmitter.call(this);
if (typeof config === 'string') {
config = { url: config };
}
this._userSuppliedConfigOptionNames = [];
if (config) {
Object.keys(config).forEach(function(key) {
const value = config[key];
if (typeof value !== 'undefined') {
if (key === 'agent' && typeof value !== 'boolean') {
const protocol =
config.url && /^https:/.test(config.url) ? 'https' : 'http';
this.agentByProtocol = this.agentByProtocol || {};
this.agentByProtocol[protocol] = value;
} else if (key === 'cert' || key === 'key' || key === 'ca') {
this[key] = resolveCertKeyOrCa(value);
this._userSuppliedConfigOptionNames.push(key);
} else if (typeof this[key] === 'undefined') {
this[key] = value;
this._userSuppliedConfigOptionNames.push(key);
} else {
// Ignore unsupported property that would overwrite or shadow for a built-in property or method
}
}
}, this);
}
if (typeof this.url === 'string') {
if (/^[A-Z]+ /.test(this.url)) {
const urlFragments = this.url.split(' ');
if (urlFragments.length > 1) {
this.method = urlFragments.shift();
this.url = urlFragments.join(' ');
}
}
}
}
util.inherits(Teepee, EventEmitter);
Teepee.prototype.subsidiary = function(config) {
const subsidiary = new this.constructor(config);
if (this.headers) {
if (subsidiary.headers) {
defaults(subsidiary.headers, this.headers);
} else {
subsidiary.headers = clone(this.headers);
}
}
if (this.query) {
if (subsidiary.query) {
defaults(subsidiary.query, this.query);
} else {
subsidiary.query = clone(this.query);
}
}
// Make sure that the subsidiary will get the same object so all agents are shared:
this.agentByProtocol = this.agentByProtocol || {};
const that = this;
const subsidiaryEmit = subsidiary.emit;
subsidiary.emit = function() {
subsidiaryEmit.apply(this, arguments);
that.emit.apply(that, arguments);
};
defaults(subsidiary, this);
if (this._userSuppliedConfigOptionNames.length > 0) {
if (subsidiary._userSuppliedConfigOptionNames.length > 0) {
subsidiary._userSuppliedConfigOptionNames = uniq(
this._userSuppliedConfigOptionNames.concat(
subsidiary._userSuppliedConfigOptionNames
)
);
} else {
subsidiary._userSuppliedConfigOptionNames = this._userSuppliedConfigOptionNames;
}
}
return subsidiary;
};
Teepee.prototype.extractNonRequestOptions = obj => {
const result = {};
if (obj) {
Object.keys(obj).forEach(key => {
if (
key !== 'method' &&
key !== 'headers' &&
key !== 'path' &&
key !== 'query' &&
key !== 'streamRows' &&
key !== 'eventEmitter' &&
key !== 'url' &&
key !== 'path'
) {
result[key] = obj[key];
}
});
}
return result;
};
Teepee.prototype.preprocessQueryStringParameterValue = (
queryStringParameterValue,
queryStringParameterName
) => queryStringParameterValue;
Teepee.prototype.stringifyJsonRequestBody = JSON.stringify;
Teepee.prototype._addQueryStringToUrl = function(url, query) {
if (typeof query !== 'undefined') {
if (typeof query === 'string') {
if (query.length > 0) {
url += (url.indexOf('?') === -1 ? '?' : '&') + query;
}
} else {
// Assume object
const params = [];
Object.keys(query).forEach(function(key) {
const value = query[key];
if (Array.isArray(value)) {
// Turn query: {foo: ['a', 'b']} into ?foo=a&foo=b
value.forEach(function(valueArrayItem) {
params.push(
`${encodeURIComponent(key)}=${encodeURIComponent(
this.preprocessQueryStringParameterValue(valueArrayItem, key)
)}`
);
}, this);
} else if (typeof value !== 'undefined') {
params.push(
`${encodeURIComponent(key)}=${encodeURIComponent(
this.preprocessQueryStringParameterValue(value, key)
)}`
);
}
}, this);
if (params.length > 0) {
url += (url.indexOf('?') === -1 ? '?' : '&') + params.join('&');
}
}
}
return url;
};
Teepee.prototype.getPlaceholderValue = function(
placeholderName,
requestOptions
) {
if (typeof requestOptions[placeholderName] !== 'undefined') {
return requestOptions[placeholderName];
} else {
const type = typeof this[placeholderName];
if (type === 'undefined') {
return `{${placeholderName}}`;
} else {
const value = this[placeholderName];
if (typeof value === 'function') {
return value.call(this, requestOptions, placeholderName);
} else {
return String(value);
}
}
}
};
Teepee.prototype.expandUrl = function(url, requestOptions) {
requestOptions = requestOptions || {};
const that = this;
let expandedUrl = url.replace(
/\{((?:[^{}]+|\{\w+\})*)\}/g,
($0, placeholderName) => {
if (/^\w+$/.test(placeholderName)) {
return that.getPlaceholderValue(placeholderName, requestOptions, $0);
} else {
const methodName = `__placeholder_fn_${placeholderName}`;
if (!that[methodName]) {
// eslint-disable-next-line no-new-func
that[methodName] = new Function(
'requestOptions',
`return ${placeholderName.replace(
/\{(\w+)\}/g,
'this.getPlaceholderValue("$1", requestOptions)'
)};`
);
}
return that[methodName](requestOptions);
}
}
);
if (!/^[a-z+]+:\/\//i.test(expandedUrl)) {
expandedUrl = `http://${expandedUrl}`;
}
return expandedUrl;
};
Teepee.prototype.getAgent = function(protocol) {
if (!this.agentByProtocol) {
this.agentByProtocol = {};
}
if (
this.agent ||
this.Agent ||
this.AgentByProtocol ||
(this.agentByProtocol && this.agentByProtocol[protocol])
) {
if (!this.agentByProtocol[protocol]) {
const agentOptions = {};
// Pass all instance variables that originate from the user-supplied config object to the Agent constructor:
this._userSuppliedConfigOptionNames.forEach(function setAgentOptions(
userSuppliedConfigOptionName
) {
const value = this[userSuppliedConfigOptionName];
if (typeof value !== 'undefined') {
agentOptions[userSuppliedConfigOptionName] = value;
}
},
this);
const Agent =
this.Agent ||
(this.AgentByProtocol && this.AgentByProtocol[protocol]) ||
(protocol === 'https' ? https : http).Agent;
this.agentByProtocol[protocol] = new Agent(agentOptions);
}
return this.agentByProtocol[protocol];
}
};
Teepee.prototype.resolveUrl = function(baseUrl, url, options) {
if (url && /^https?:\/\//.test(url)) {
return this.expandUrl(url, options);
} else if (baseUrl) {
baseUrl = this.expandUrl(baseUrl, options);
if (typeof url === 'string') {
if (/^\/\//.test(url) || /^\.\.?(?:$|\/)/.test(url)) {
// Protocol-relative or relative starting with a . or .. fragment, resolve it:
// eslint-disable-next-line node/no-deprecated-api
return urlModule.resolve(baseUrl, url);
} else {
// Borrowed from request: Handle all cases to make sure that there's only one slash between the baseUrl and url:
const baseUrlEndsWithSlash =
baseUrl.lastIndexOf('/') === baseUrl.length - 1;
const urlStartsWithSlash = url.indexOf('/') === 0;
if (baseUrlEndsWithSlash && urlStartsWithSlash) {
return baseUrl + url.slice(1);
} else if (baseUrlEndsWithSlash || urlStartsWithSlash) {
return baseUrl + url;
} else if (url === '') {
return baseUrl;
} else {
return `${baseUrl}/${url}`;
}
}
} else {
return baseUrl;
}
} else {
throw new Error(
'An absolute request url must be given when no base url is available'
);
}
};
/*
* Perform a request
*
* options.headers {Object} (optional) The HTTP headers for the request.
* options.path {String} (optional) The path relative to the base url.
* options.query {Object} (optional) Query parameters, will be run through encodeURIComponent, array values supported
* options.body {String|Object|Buffer|Stream} (optional) What to send. Streams are streamed, objects will be serialized as JSON,
* and buffers are sent as-is.
* options.numRetries {Number} (optional) The number of times to retry an operation if it fails due to a non-HTTP error such
* as a socket timeout. Defaults to the numRetries parameter given to the constructor
* (which defaults to 0). Has no effect with the onResponse and streamRows options.
*/
Teepee.prototype.request = function(url, options, cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
} else if (typeof url === 'function') {
cb = url;
url = undefined;
options = undefined;
}
if (typeof url === 'string') {
if (options && typeof options === 'object') {
options.url = url;
url = undefined;
} else if (typeof options === 'undefined') {
options = { url };
url = undefined;
} else {
throw new Error(
`Teepee#request: options cannot be passed as ${typeof options}`
);
}
} else if (url && typeof url === 'object') {
options = url;
url = undefined;
}
options = options || {};
let numRetriesLeft = options.streamRows
? 0
: typeof options.numRetries !== 'undefined'
? options.numRetries
: this.numRetries || 0;
const retryDelayMilliseconds =
typeof options.retryDelayMilliseconds !== 'undefined'
? options.retryDelayMilliseconds
: this.retryDelayMilliseconds || 0;
const timeout =
typeof options.timeout !== 'undefined' ? options.timeout : this.timeout;
let username =
typeof options.username !== 'undefined' ? options.username : this.username;
let password =
typeof options.password !== 'undefined' ? options.password : this.password;
let body = typeof options.body !== 'undefined' ? options.body : this.body;
let retry =
typeof options.retry !== 'undefined' ? options.retry : this.retry || [];
const rejectUnauthorized =
typeof options.rejectUnauthorized !== 'undefined'
? options.rejectUnauthorized
: this.rejectUnauthorized;
const headers = {};
// Gotcha: A query specified as a string overrides this.query
const query =
typeof options.query === 'string'
? options.query
: { ...this.query, ...options.query };
let method = options.method;
let requestUrl =
typeof options.path === 'string' ? options.path : options.url;
const autoDecodeJson =
typeof options.json !== 'undefined'
? options.json !== false
: this.json !== false; // Defaults to true
const headerObjs = [this.headers, options.headers];
if (options && options.formData) {
if (typeof body !== 'undefined') {
throw new Error(
'Teepee#request: The "body" and "formData" options are not supported together'
);
}
body = new FormData();
Object.keys(options.formData).forEach(name => {
let value = options.formData[name];
let partOptions = {};
if (isStream.readable(value) && value.path) {
partOptions.filename = value.path;
} else if (typeof value === 'object' && !Buffer.isBuffer(value)) {
partOptions = Object.assign({}, value);
value = partOptions.value;
delete partOptions.value;
if (partOptions.fileName) {
partOptions.filename = partOptions.fileName;
delete partOptions.fileName;
}
}
body.append(name, value, partOptions);
});
headerObjs.push(body.getHeaders());
}
headerObjs.forEach(headersObj => {
if (headersObj) {
Object.keys(headersObj).forEach(headerName => {
const headerValue = headersObj[headerName];
if (typeof headerValue === 'undefined') {
return;
}
headers[headerName.toLowerCase()] = headerValue;
});
}
});
if (typeof retry !== 'undefined' && !Array.isArray(retry)) {
retry = [retry];
}
if (typeof requestUrl === 'string') {
if (/^[A-Z]+ /.test(requestUrl)) {
const requestPathFragments = requestUrl.split(' ');
// Error out if they conflict?
method = method || requestPathFragments.shift();
requestUrl = requestPathFragments.join(' ');
}
}
method = method || this.method || 'GET';
if (Buffer.isBuffer(body)) {
headers['content-length'] = body.length; // Disables chunked encoding for buffered body or strings
} else if (typeof body === 'string') {
headers['content-length'] = Buffer.byteLength(body); // Disables chunked encoding for string body
} else if (typeof body === 'object') {
if (typeof body.pipe === 'function') {
// Hack to prevent the response handling code from discarding the response
body._teepeePipeDue = true;
} else {
body = this.stringifyJsonRequestBody(body);
headers['content-type'] = headers['content-type'] || 'application/json';
headers['content-length'] = Buffer.byteLength(body); // Disables chunked encoding for json body
}
} else if (!body) {
headers['content-length'] = 0; // Disables chunked encoding if there is no body
}
url = this._addQueryStringToUrl(
this.resolveUrl(this.url, requestUrl, options),
query
);
let auth;
// https://github.com/joyent/node/issues/25353 url.parse() fails if auth contain a colon,
// parse it separately:
url = url.replace(
/^([a-z+-]+:\/\/)([^:@/]+(?::[^@/]*?))@/i,
($0, before, _auth) => {
auth = _auth;
return before;
}
);
// new urlModule.URL does not accept a url without a hostname
// eslint-disable-next-line node/no-deprecated-api
const urlObj = urlModule.parse(url);
if (!urlObj) {
throw new Error(`Invalid url: ${url}`);
}
const protocol = urlObj.protocol.replace(/:$/, '');
const host = urlObj.hostname;
let port = urlObj.port;
const path = urlObj.pathname;
const queryString = urlObj.search || '';
if (!('host' in headers) && (host || port)) {
headers.host = (host || '') + (port ? `:${port}` : '');
}
if (port !== undefined && port !== null) {
port = parseInt(port, 10);
} else if (protocol === 'https') {
port = 443;
} else {
port = 80;
}
if (
typeof auth === 'string' &&
auth.length > 0 &&
!('authorization' in headers)
) {
const authFragments = auth.split(':');
username = username || safeDecodeURIComponent(authFragments.shift());
if (authFragments.length > 0) {
password = safeDecodeURIComponent(authFragments.join(':'));
}
}
if (username) {
headers.authorization = `Basic ${Buffer.from(
username + (password ? `:${password}` : ''),
'utf-8'
).toString('base64')}`;
}
let requestOptions = {
protocol,
host,
port,
method,
path: path + queryString,
headers,
rejectUnauthorized
};
const agent = this.getAgent(protocol);
if (agent) {
requestOptions.agent = agent;
} else {
['cert', 'key', 'ca'].forEach(function setRequestOptions(key) {
if (this[key]) {
requestOptions[key] = this[key];
}
}, this);
}
let that = this;
let currentRequest;
let currentResponse;
let responseError;
let responseBodyChunks;
const eventEmitter = new EventEmitter();
function disposeRequestOrResponse(obj) {
if (obj) {
obj.removeAllListeners();
obj.on('error', () => {});
}
}
function cleanUp() {
responseBodyChunks = null;
disposeRequestOrResponse(currentRequest);
currentRequest = null;
disposeRequestOrResponse(currentResponse);
currentResponse = null;
responseError = undefined;
}
let promise;
eventEmitter.then = function() {
if (cb && !promise) {
throw new Error('You cannot use .then() and a callback at the same time');
} else {
if (!promise) {
promise = new Promise((resolve, reject) => {
if (currentRequest) {
throw new Error(
'.then() must be called in the same tick as the request is initiated'
);
}
cb = (err, response, body) => {
if (err) {
reject(err);
} else {
resolve(response);
}
};
});
}
return promise.then.apply(promise, arguments);
}
};
eventEmitter.done = false;
eventEmitter.abort = () => {
eventEmitter.done = true;
if (currentRequest) {
currentRequest.abort();
cleanUp();
eventEmitter.removeAllListeners();
requestOptions = null;
that = null;
}
};
eventEmitter.error = err => {
if (!eventEmitter.done) {
eventEmitter.done = true;
// if what came up was a plain error convert it to an httpError
if (!err.statusCode) {
if (SocketError.supports(err)) {
err = new SocketError(err);
} else if (DnsError.supports(err)) {
err = new DnsError(err);
} else {
// convert to a 500 internal server error
err = new HttpError[500](err.message);
}
}
that.emit('failedRequest', {
url,
requestOptions,
response: currentResponse,
err,
numRetriesLeft
});
if (cb) {
// Could we pass 'response' as an argument to this method always instead, so we don't need the pseudo-global currentResponse variable?
cb(err, currentResponse, currentResponse && currentResponse.body);
} else if (
eventEmitter.listeners('error').length > 0 ||
eventEmitter.listeners('response').length === 0
) {
eventEmitter.emit('error', err);
}
setImmediate(() => {
cleanUp();
eventEmitter.removeAllListeners();
requestOptions = null;
that = null;
});
}
};
eventEmitter.success = response => {
if (!eventEmitter.done) {
eventEmitter.done = true;
that.emit('successfulRequest', {
url,
requestOptions,
response
});
eventEmitter.emit('end');
if (cb) {
cb(null, response, response && response.body);
}
setImmediate(() => {
cleanUp();
eventEmitter.removeAllListeners();
requestOptions = null;
that = null;
});
}
};
if (this.preprocessRequestOptions) {
this.preprocessRequestOptions(
requestOptions,
options,
passError(eventEmitter.error, dispatchRequest)
);
} else {
setImmediate(dispatchRequest);
}
function dispatchRequest() {
if (currentRequest) {
disposeRequestOrResponse(currentRequest);
}
currentRequest = (requestOptions.protocol === 'https'
? https
: http
).request(omit(requestOptions, 'protocol'));
that.emit('request', { requestOptions, url });
if (currentResponse) {
disposeRequestOrResponse(currentResponse);
}
currentResponse = null;
responseError = undefined;
if (eventEmitter.listeners('request').length > 0) {
numRetriesLeft = 0;
eventEmitter.emit('request', currentRequest, requestOptions, url);
}
let requestBody = body;
if (typeof requestBody === 'function') {
requestBody = requestBody();
}
if (requestBody && typeof requestBody.pipe === 'function') {
if (typeof body !== 'function') {
numRetriesLeft = 0;
}
requestBody.pipe(currentRequest);
} else {
currentRequest.end(requestBody);
}
// Added request socket connection timeout handling.
// Needed to handle busy servers which may not respond on unestablishment of socket connection
if (
options &&
options.socketTimeout &&
typeof options.socketTimeout === 'number'
) {
currentRequest.on('socket', socket => {
socket.setTimeout(options.socketTimeout);
socket.on('timeout', () => {
socket.end();
cleanUp();
handleRequestError(new SocketError.ETIMEDOUT());
});
});
}
if (typeof timeout === 'number') {
currentRequest.setTimeout(timeout, () => {
// This callback will be added as a one time listener for the 'timeout' event.
currentRequest.destroy();
cleanUp();
handleRequestError(new SocketError.ETIMEDOUT());
});
}
function retryUponError(err) {
cleanUp();
numRetriesLeft -= 1;
setTimeout(() => {
that.emit('retriedRequest', {
requestOptions,
err,
numRetriesLeft,
url
});
dispatchRequest();
}, retryDelayMilliseconds);
}
function handleRequestError(err) {
disposeRequestOrResponse(currentRequest);
if (eventEmitter.done) {
return;
}
// Non-HTTP error (ECONNRESET, ETIMEDOUT, etc.)
// Try again (up to numRetriesLeft times). Warning: This doesn't work when piping into the returned request,
// so please specify numRetriesLeft:0 if you intend to do that.
if (numRetriesLeft > 0) {
return retryUponError(err);
} else {
eventEmitter.error(err);
}
}
currentRequest
.once('error', handleRequestError)
.once('response', function handleResponse(response) {
currentResponse = response;
let hasEnded = false;
response.once('end', () => {
hasEnded = true;
});
if (eventEmitter.done) {
return;
}
function returnSuccessOrError(err) {
err = err || responseError;
if (err) {
eventEmitter.error(err, response);
} else {
if (hasEnded) {
eventEmitter.success(response);
} else {
response.once('end', () => {
eventEmitter.success(response);
});
// Avoid "Cannot switch to old mode now" error when a pipe has been added:
if (
(typeof response._readableState.pipesCount !== 'number' ||
response._readableState.pipesCount === 0) &&
!response._readableState.pipes
) {
response.resume();
}
}
}
}
function shouldRetryOnErrorStatusCode(statusCode) {
return retry.some(retryEntry => {
if (retryEntry === 'httpError' || retryEntry === statusCode) {
return true;
} else if (
typeof retryEntry === 'string' &&
retryEntry.length === 3 &&
/\d/.test(retryEntry.charAt(0))
) {
const statusCodeString = String(statusCode);
if (
retryEntry.replace(
/x/g,
($0, index) => statusCodeString[index]
) === statusCodeString
) {
return true;
}
}
});
}
response.requestOptions = requestOptions;
response.url = url;
response.cacheInfo = { headers: {} };
let responseBodyMustBeDisposedUnlessPiped = false;
if (response.statusCode === 301 || response.statusCode === 302) {
if (numRetriesLeft > 0 && retry.indexOf('selfRedirect') !== -1) {
const redirectTargetUrl = new urlModule.URL(
response.headers.location,
url
).href;
if (
redirectTargetUrl.replace(/#.*$/, '') === url.replace(/#.*$/, '')
) {
response.once('error', () => {});
response.resume();
return retryUponError(
new SelfRedirectError({
data: { location: response.headers.location }
})
);
} else {
responseBodyMustBeDisposedUnlessPiped = true;
}
}
} else if (response.statusCode >= 400) {
responseError = new HttpError(response.statusCode);
if (
numRetriesLeft > 0 &&
shouldRetryOnErrorStatusCode(response.statusCode)
) {
response.once('error', () => {});
response.resume();
return retryUponError(responseError);
}
} else if (response.statusCode === 304) {
response.cacheInfo.notModified = true;
body = null;
responseBodyMustBeDisposedUnlessPiped = true;
}
[
'last-modified',
'etag',
'expires',
'cache-control',
'content-type'
].forEach(headerName => {
if (headerName in response.headers) {
response.cacheInfo.headers[headerName] =
response.headers[headerName];
}
});
eventEmitter.emit('response', response, responseError);
if (responseError) {
if (!cb) {
eventEmitter.error(responseError);
}
} else {
eventEmitter.emit('success', response);
}
let responseBodyMustBeBuffered =
eventEmitter.listeners('responseBody').length > 0 || responseError;
if (cb) {
responseBodyMustBeBuffered = true;
eventEmitter.once('responseBody', () => {
returnSuccessOrError();
});
// Under these specific circumstances we can retry when the request times out while we're streaming the response:
if (typeof timeout === 'number' && numRetriesLeft > 0) {
currentRequest.removeAllListeners('timeout');
currentRequest.once('timeout', () => {
// Emitted if the socket times out from inactivity. This is only to notify that the socket has been idle. The user must manually close the connection.
eventEmitter.removeAllListeners('responseBody');
currentRequest.destroy();
numRetriesLeft -= 1;
retryUponError(new SocketError.ETIMEDOUT());
});
}
} else {
numRetriesLeft = 0;
if (responseError) {
if (responseBodyMustBeBuffered) {
eventEmitter.once('responseBody', () => {
setImmediate(() => {
returnSuccessOrError();
});
});
} else {
response.once('end', returnSuccessOrError);
// Avoid "Cannot switch to old mode now" error when a pipe has been added:
if (
(typeof response._readableState.pipesCount !== 'number' ||
response._readableState.pipesCount === 0) &&
!response._readableState.pipes
) {
response.resume();
}
}
} else if (!responseBodyMustBeBuffered) {
response.once('end', returnSuccessOrError);
}
}
if (responseBodyMustBeBuffered) {
responseBodyChunks = [];
let responseBodyStream = response;
const contentEncoding = response.headers['content-encoding'];
if (contentEncoding === 'gzip' || contentEncoding === 'deflate') {
const decoder = new zlib[
contentEncoding === 'gzip' ? 'Gunzip' : 'Inflate'
]();
decoder.once('error', returnSuccessOrError);
responseBodyStream = responseBodyStream.pipe(decoder);
}
responseBodyStream
.on('data', function handleBodyChunk(responseBodyChunk) {
responseBodyChunks.push(responseBodyChunk);
})
.once('error', returnSuccessOrError)
.once('end', function handleEnd() {
disposeRequestOrResponse(currentRequest);
currentRequest = null;
response.body = Buffer.concat(responseBodyChunks);
if (
isContentTypeJson(response.headers['content-type']) &&
response.req.method !== 'HEAD' &&
autoDecodeJson
) {
// 'HEAD' requests have blank response
try {
response.body = JSON.parse(response.body.toString('utf-8'));
} catch (e) {
return eventEmitter.error(
new HttpError.BadGateway(
'Error parsing JSON response body'
),
response
);