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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnsupportedProtocolError = exports.ReadError = exports.TimeoutError = exports.UploadError = exports.CacheError = exports.HTTPError = exports.MaxRedirectsError = exports.RequestError = exports.setNonEnumerableProperties = exports.knownHookEvents = exports.withoutBody = exports.kIsNormalizedAlready = void 0;
const util_1 = require("util");
const stream_1 = require("stream");
const fs_1 = require("fs");
const url_1 = require("url");
const http = require("http");
const http_1 = require("http");
const https = require("https");
const http_timer_1 = require("@szmarczak/http-timer");
const cacheable_lookup_1 = require("cacheable-lookup");
const CacheableRequest = require("cacheable-request");
const decompressResponse = require("decompress-response");
// @ts-expect-error Missing types
const http2wrapper = require("http2-wrapper");
const lowercaseKeys = require("lowercase-keys");
const is_1 = require("@sindresorhus/is");
const get_body_size_1 = require("./utils/get-body-size");
const is_form_data_1 = require("./utils/is-form-data");
const proxy_events_1 = require("./utils/proxy-events");
const timed_out_1 = require("./utils/timed-out");
const url_to_options_1 = require("./utils/url-to-options");
const options_to_url_1 = require("./utils/options-to-url");
const weakable_map_1 = require("./utils/weakable-map");
const get_buffer_1 = require("./utils/get-buffer");
const dns_ip_version_1 = require("./utils/dns-ip-version");
const is_response_ok_1 = require("./utils/is-response-ok");
const deprecation_warning_1 = require("../utils/deprecation-warning");
const normalize_arguments_1 = require("../as-promise/normalize-arguments");
const calculate_retry_delay_1 = require("./calculate-retry-delay");
let globalDnsCache;
const kRequest = Symbol('request');
const kResponse = Symbol('response');
const kResponseSize = Symbol('responseSize');
const kDownloadedSize = Symbol('downloadedSize');
const kBodySize = Symbol('bodySize');
const kUploadedSize = Symbol('uploadedSize');
const kServerResponsesPiped = Symbol('serverResponsesPiped');
const kUnproxyEvents = Symbol('unproxyEvents');
const kIsFromCache = Symbol('isFromCache');
const kCancelTimeouts = Symbol('cancelTimeouts');
const kStartedReading = Symbol('startedReading');
const kStopReading = Symbol('stopReading');
const kTriggerRead = Symbol('triggerRead');
const kBody = Symbol('body');
const kJobs = Symbol('jobs');
const kOriginalResponse = Symbol('originalResponse');
const kRetryTimeout = Symbol('retryTimeout');
exports.kIsNormalizedAlready = Symbol('isNormalizedAlready');
const supportsBrotli = is_1.default.string(process.versions.brotli);
exports.withoutBody = new Set(['GET', 'HEAD']);
exports.knownHookEvents = [
'init',
'beforeRequest',
'beforeRedirect',
'beforeError',
'beforeRetry',
// Promise-Only
'afterResponse'
];
function validateSearchParameters(searchParameters) {
// eslint-disable-next-line guard-for-in
for (const key in searchParameters) {
const value = searchParameters[key];
if (!is_1.default.string(value) && !is_1.default.number(value) && !is_1.default.boolean(value) && !is_1.default.null_(value) && !is_1.default.undefined(value)) {
throw new TypeError(`The \`searchParams\` value '${String(value)}' must be a string, number, boolean or null`);
}
}
}
function isClientRequest(clientRequest) {
return is_1.default.object(clientRequest) && !('statusCode' in clientRequest);
}
const cacheableStore = new weakable_map_1.default();
const waitForOpenFile = async (file) => new Promise((resolve, reject) => {
const onError = (error) => {
reject(error);
};
// Node.js 12 has incomplete types
if (!file.pending) {
resolve();
}
file.once('error', onError);
file.once('ready', () => {
file.off('error', onError);
resolve();
});
});
const redirectCodes = new Set([300, 301, 302, 303, 304, 307, 308]);
const nonEnumerableProperties = [
'context',
'body',
'json',
'form'
];
exports.setNonEnumerableProperties = (sources, to) => {
// Non enumerable properties shall not be merged
const properties = {};
for (const source of sources) {
if (!source) {
continue;
}
for (const name of nonEnumerableProperties) {
if (!(name in source)) {
continue;
}
properties[name] = {
writable: true,
configurable: true,
enumerable: false,
// @ts-expect-error TS doesn't see the check above
value: source[name]
};
}
}
Object.defineProperties(to, properties);
};
/**
An error to be thrown when a request fails.
Contains a `code` property with error class code, like `ECONNREFUSED`.
*/
class RequestError extends Error {
constructor(message, error, self) {
var _a;
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = 'RequestError';
this.code = error.code;
if (self instanceof Request) {
Object.defineProperty(this, 'request', {
enumerable: false,
value: self
});
Object.defineProperty(this, 'response', {
enumerable: false,
value: self[kResponse]
});
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: self.options
});
}
else {
Object.defineProperty(this, 'options', {
// This fails because of TS 3.7.2 useDefineForClassFields
// Ref: https://github.com/microsoft/TypeScript/issues/34972
enumerable: false,
value: self
});
}
this.timings = (_a = this.request) === null || _a === void 0 ? void 0 : _a.timings;
// Recover the original stacktrace
if (is_1.default.string(error.stack) && is_1.default.string(this.stack)) {
const indexOfMessage = this.stack.indexOf(this.message) + this.message.length;
const thisStackTrace = this.stack.slice(indexOfMessage).split('\n').reverse();
const errorStackTrace = error.stack.slice(error.stack.indexOf(error.message) + error.message.length).split('\n').reverse();
// Remove duplicated traces
while (errorStackTrace.length !== 0 && errorStackTrace[0] === thisStackTrace[0]) {
thisStackTrace.shift();
}
this.stack = `${this.stack.slice(0, indexOfMessage)}${thisStackTrace.reverse().join('\n')}${errorStackTrace.reverse().join('\n')}`;
}
}
}
exports.RequestError = RequestError;
/**
An error to be thrown when the server redirects you more than ten times.
Includes a `response` property.
*/
class MaxRedirectsError extends RequestError {
constructor(request) {
super(`Redirected ${request.options.maxRedirects} times. Aborting.`, {}, request);
this.name = 'MaxRedirectsError';
}
}
exports.MaxRedirectsError = MaxRedirectsError;
/**
An error to be thrown when the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304.
Includes a `response` property.
*/
class HTTPError extends RequestError {
constructor(response) {
super(`Response code ${response.statusCode} (${response.statusMessage})`, {}, response.request);
this.name = 'HTTPError';
}
}
exports.HTTPError = HTTPError;
/**
An error to be thrown when a cache method fails.
For example, if the database goes down or there's a filesystem error.
*/
class CacheError extends RequestError {
constructor(error, request) {
super(error.message, error, request);
this.name = 'CacheError';
}
}
exports.CacheError = CacheError;
/**
An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
*/
class UploadError extends RequestError {
constructor(error, request) {
super(error.message, error, request);
this.name = 'UploadError';
}
}
exports.UploadError = UploadError;
/**
An error to be thrown when the request is aborted due to a timeout.
Includes an `event` and `timings` property.
*/
class TimeoutError extends RequestError {
constructor(error, timings, request) {
super(error.message, error, request);
this.name = 'TimeoutError';
this.event = error.event;
this.timings = timings;
}
}
exports.TimeoutError = TimeoutError;
/**
An error to be thrown when reading from response stream fails.
*/
class ReadError extends RequestError {
constructor(error, request) {
super(error.message, error, request);
this.name = 'ReadError';
}
}
exports.ReadError = ReadError;
/**
An error to be thrown when given an unsupported protocol.
*/
class UnsupportedProtocolError extends RequestError {
constructor(options) {
super(`Unsupported protocol "${options.url.protocol}"`, {}, options);
this.name = 'UnsupportedProtocolError';
}
}
exports.UnsupportedProtocolError = UnsupportedProtocolError;
const proxiedRequestEvents = [
'socket',
'connect',
'continue',
'information',
'upgrade',
'timeout'
];
class Request extends stream_1.Duplex {
constructor(url, options = {}, defaults) {
super({
// This must be false, to enable throwing after destroy
// It is used for retry logic in Promise API
autoDestroy: false,
// It needs to be zero because we're just proxying the data to another stream
highWaterMark: 0
});
this[kDownloadedSize] = 0;
this[kUploadedSize] = 0;
this.requestInitialized = false;
this[kServerResponsesPiped] = new Set();
this.redirects = [];
this[kStopReading] = false;
this[kTriggerRead] = false;
this[kJobs] = [];
this.retryCount = 0;
// TODO: Remove this when targeting Node.js >= 12
this._progressCallbacks = [];
const unlockWrite = () => this._unlockWrite();
const lockWrite = () => this._lockWrite();
this.on('pipe', (source) => {
source.prependListener('data', unlockWrite);
source.on('data', lockWrite);
source.prependListener('end', unlockWrite);
source.on('end', lockWrite);
});
this.on('unpipe', (source) => {
source.off('data', unlockWrite);
source.off('data', lockWrite);
source.off('end', unlockWrite);
source.off('end', lockWrite);
});
this.on('pipe', source => {
if (source instanceof http_1.IncomingMessage) {
this.options.headers = {
...source.headers,
...this.options.headers
};
}
});
const { json, body, form } = options;
if (json || body || form) {
this._lockWrite();
}
if (exports.kIsNormalizedAlready in options) {
this.options = options;
}
else {
try {
// @ts-expect-error Common TypeScript bug saying that `this.constructor` is not accessible
this.options = this.constructor.normalizeArguments(url, options, defaults);
}
catch (error) {
// TODO: Move this to `_destroy()`
if (is_1.default.nodeStream(options.body)) {
options.body.destroy();
}
this.destroy(error);
return;
}
}
(async () => {
var _a;
try {
if (this.options.body instanceof fs_1.ReadStream) {
await waitForOpenFile(this.options.body);
}
const { url: normalizedURL } = this.options;
if (!normalizedURL) {
throw new TypeError('Missing `url` property');
}
this.requestUrl = normalizedURL.toString();
decodeURI(this.requestUrl);
await this._finalizeBody();
await this._makeRequest();
if (this.destroyed) {
(_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.destroy();
return;
}
// Queued writes etc.
for (const job of this[kJobs]) {
job();
}
// Prevent memory leak
this[kJobs].length = 0;
this.requestInitialized = true;
}
catch (error) {
if (error instanceof RequestError) {
this._beforeError(error);
return;
}
// This is a workaround for https://github.com/nodejs/node/issues/33335
if (!this.destroyed) {
this.destroy(error);
}
}
})();
}
static normalizeArguments(url, options, defaults) {
var _a, _b, _c, _d, _e;
const rawOptions = options;
if (is_1.default.object(url) && !is_1.default.urlInstance(url)) {
options = { ...defaults, ...url, ...options };
}
else {
if (url && options && options.url !== undefined) {
throw new TypeError('The `url` option is mutually exclusive with the `input` argument');
}
options = { ...defaults, ...options };
if (url !== undefined) {
options.url = url;
}
if (is_1.default.urlInstance(options.url)) {
options.url = new url_1.URL(options.url.toString());
}
}
// TODO: Deprecate URL options in Got 12.
// Support extend-specific options
if (options.cache === false) {
options.cache = undefined;
}
if (options.dnsCache === false) {
options.dnsCache = undefined;
}
// Nice type assertions
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.method);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.headers);
is_1.assert.any([is_1.default.string, is_1.default.urlInstance, is_1.default.undefined], options.prefixUrl);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.cookieJar);
is_1.assert.any([is_1.default.object, is_1.default.string, is_1.default.undefined], options.searchParams);
is_1.assert.any([is_1.default.object, is_1.default.string, is_1.default.undefined], options.cache);
is_1.assert.any([is_1.default.object, is_1.default.number, is_1.default.undefined], options.timeout);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.context);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.hooks);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.decompress);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.ignoreInvalidCookies);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.followRedirect);
is_1.assert.any([is_1.default.number, is_1.default.undefined], options.maxRedirects);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.throwHttpErrors);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.http2);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.allowGetBody);
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.localAddress);
is_1.assert.any([dns_ip_version_1.isDnsLookupIpVersion, is_1.default.undefined], options.dnsLookupIpVersion);
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.https);
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.rejectUnauthorized);
if (options.https) {
is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.https.rejectUnauthorized);
is_1.assert.any([is_1.default.function_, is_1.default.undefined], options.https.checkServerIdentity);
is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.certificateAuthority);
is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.key);
is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.certificate);
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.https.passphrase);
is_1.assert.any([is_1.default.string, is_1.default.buffer, is_1.default.array, is_1.default.undefined], options.https.pfx);
}
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.cacheOptions);
// `options.method`
if (is_1.default.string(options.method)) {
options.method = options.method.toUpperCase();
}
else {
options.method = 'GET';
}
// `options.headers`
if (options.headers === (defaults === null || defaults === void 0 ? void 0 : defaults.headers)) {
options.headers = { ...options.headers };
}
else {
options.headers = lowercaseKeys({ ...(defaults === null || defaults === void 0 ? void 0 : defaults.headers), ...options.headers });
}
// Disallow legacy `url.Url`
if ('slashes' in options) {
throw new TypeError('The legacy `url.Url` has been deprecated. Use `URL` instead.');
}
// `options.auth`
if ('auth' in options) {
throw new TypeError('Parameter `auth` is deprecated. Use `username` / `password` instead.');
}
// `options.searchParams`
if ('searchParams' in options) {
if (options.searchParams && options.searchParams !== (defaults === null || defaults === void 0 ? void 0 : defaults.searchParams)) {
let searchParameters;
if (is_1.default.string(options.searchParams) || (options.searchParams instanceof url_1.URLSearchParams)) {
searchParameters = new url_1.URLSearchParams(options.searchParams);
}
else {
validateSearchParameters(options.searchParams);
searchParameters = new url_1.URLSearchParams();
// eslint-disable-next-line guard-for-in
for (const key in options.searchParams) {
const value = options.searchParams[key];
if (value === null) {
searchParameters.append(key, '');
}
else if (value !== undefined) {
searchParameters.append(key, value);
}
}
}
// `normalizeArguments()` is also used to merge options
(_a = defaults === null || defaults === void 0 ? void 0 : defaults.searchParams) === null || _a === void 0 ? void 0 : _a.forEach((value, key) => {
// Only use default if one isn't already defined
if (!searchParameters.has(key)) {
searchParameters.append(key, value);
}
});
options.searchParams = searchParameters;
}
}
// `options.username` & `options.password`
options.username = (_b = options.username) !== null && _b !== void 0 ? _b : '';
options.password = (_c = options.password) !== null && _c !== void 0 ? _c : '';
// `options.prefixUrl` & `options.url`
if (is_1.default.undefined(options.prefixUrl)) {
options.prefixUrl = (_d = defaults === null || defaults === void 0 ? void 0 : defaults.prefixUrl) !== null && _d !== void 0 ? _d : '';
}
else {
options.prefixUrl = options.prefixUrl.toString();
if (options.prefixUrl !== '' && !options.prefixUrl.endsWith('/')) {
options.prefixUrl += '/';
}
}
if (is_1.default.string(options.url)) {
if (options.url.startsWith('/')) {
throw new Error('`input` must not start with a slash when using `prefixUrl`');
}
options.url = options_to_url_1.default(options.prefixUrl + options.url, options);
}
else if ((is_1.default.undefined(options.url) && options.prefixUrl !== '') || options.protocol) {
options.url = options_to_url_1.default(options.prefixUrl, options);
}
if (options.url) {
if ('port' in options) {
delete options.port;
}
// Make it possible to change `options.prefixUrl`
let { prefixUrl } = options;
Object.defineProperty(options, 'prefixUrl', {
set: (value) => {
const url = options.url;
if (!url.href.startsWith(value)) {
throw new Error(`Cannot change \`prefixUrl\` from ${prefixUrl} to ${value}: ${url.href}`);
}
options.url = new url_1.URL(value + url.href.slice(prefixUrl.length));
prefixUrl = value;
},
get: () => prefixUrl
});
// Support UNIX sockets
let { protocol } = options.url;
if (protocol === 'unix:') {
protocol = 'http:';
options.url = new url_1.URL(`http://unix${options.url.pathname}${options.url.search}`);
}
// Set search params
if (options.searchParams) {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
options.url.search = options.searchParams.toString();
}
// Protocol check
if (protocol !== 'http:' && protocol !== 'https:') {
throw new UnsupportedProtocolError(options);
}
// Update `username`
if (options.username === '') {
options.username = options.url.username;
}
else {
options.url.username = options.username;
}
// Update `password`
if (options.password === '') {
options.password = options.url.password;
}
else {
options.url.password = options.password;
}
}
// `options.cookieJar`
const { cookieJar } = options;
if (cookieJar) {
let { setCookie, getCookieString } = cookieJar;
is_1.assert.function_(setCookie);
is_1.assert.function_(getCookieString);
/* istanbul ignore next: Horrible `tough-cookie` v3 check */
if (setCookie.length === 4 && getCookieString.length === 0) {
setCookie = util_1.promisify(setCookie.bind(options.cookieJar));
getCookieString = util_1.promisify(getCookieString.bind(options.cookieJar));
options.cookieJar = {
setCookie,
getCookieString: getCookieString
};
}
}
// `options.cache`
const { cache } = options;
if (cache) {
if (!cacheableStore.has(cache)) {
cacheableStore.set(cache, new CacheableRequest(((requestOptions, handler) => {
const result = requestOptions[kRequest](requestOptions, handler);
// TODO: remove this when `cacheable-request` supports async request functions.
if (is_1.default.promise(result)) {
// @ts-expect-error
// We only need to implement the error handler in order to support HTTP2 caching.
// The result will be a promise anyway.
result.once = (event, handler) => {
if (event === 'error') {
result.catch(handler);
}
else if (event === 'abort') {
// The empty catch is needed here in case when
// it rejects before it's `await`ed in `_makeRequest`.
(async () => {
try {
const request = (await result);
request.once('abort', handler);
}
catch (_a) { }
})();
}
else {
/* istanbul ignore next: safety check */
throw new Error(`Unknown HTTP2 promise event: ${event}`);
}
return result;
};
}
return result;
}), cache));
}
}
// `options.cacheOptions`
options.cacheOptions = { ...options.cacheOptions };
// `options.dnsCache`
if (options.dnsCache === true) {
if (!globalDnsCache) {
globalDnsCache = new cacheable_lookup_1.default();
}
options.dnsCache = globalDnsCache;
}
else if (!is_1.default.undefined(options.dnsCache) && !options.dnsCache.lookup) {
throw new TypeError(`Parameter \`dnsCache\` must be a CacheableLookup instance or a boolean, got ${is_1.default(options.dnsCache)}`);
}
// `options.timeout`
if (is_1.default.number(options.timeout)) {
options.timeout = { request: options.timeout };
}
else if (defaults && options.timeout !== defaults.timeout) {
options.timeout = {
...defaults.timeout,
...options.timeout
};
}
else {
options.timeout = { ...options.timeout };
}
// `options.context`
if (!options.context) {
options.context = {};
}
// `options.hooks`
const areHooksDefault = options.hooks === (defaults === null || defaults === void 0 ? void 0 : defaults.hooks);
options.hooks = { ...options.hooks };
for (const event of exports.knownHookEvents) {
if (event in options.hooks) {
if (is_1.default.array(options.hooks[event])) {
// See https://github.com/microsoft/TypeScript/issues/31445#issuecomment-576929044
options.hooks[event] = [...options.hooks[event]];
}
else {
throw new TypeError(`Parameter \`${event}\` must be an Array, got ${is_1.default(options.hooks[event])}`);
}
}
else {
options.hooks[event] = [];
}
}
if (defaults && !areHooksDefault) {
for (const event of exports.knownHookEvents) {
const defaultHooks = defaults.hooks[event];
if (defaultHooks.length > 0) {
// See https://github.com/microsoft/TypeScript/issues/31445#issuecomment-576929044
options.hooks[event] = [
...defaults.hooks[event],
...options.hooks[event]
];
}
}
}
// DNS options
if ('family' in options) {
deprecation_warning_1.default('"options.family" was never documented, please use "options.dnsLookupIpVersion"');
}
// HTTPS options
if (defaults === null || defaults === void 0 ? void 0 : defaults.https) {
options.https = { ...defaults.https, ...options.https };
}
if ('rejectUnauthorized' in options) {
deprecation_warning_1.default('"options.rejectUnauthorized" is now deprecated, please use "options.https.rejectUnauthorized"');
}
if ('checkServerIdentity' in options) {
deprecation_warning_1.default('"options.checkServerIdentity" was never documented, please use "options.https.checkServerIdentity"');
}
if ('ca' in options) {
deprecation_warning_1.default('"options.ca" was never documented, please use "options.https.certificateAuthority"');
}
if ('key' in options) {
deprecation_warning_1.default('"options.key" was never documented, please use "options.https.key"');
}
if ('cert' in options) {
deprecation_warning_1.default('"options.cert" was never documented, please use "options.https.certificate"');
}
if ('passphrase' in options) {
deprecation_warning_1.default('"options.passphrase" was never documented, please use "options.https.passphrase"');
}
if ('pfx' in options) {
deprecation_warning_1.default('"options.pfx" was never documented, please use "options.https.pfx"');
}
// Other options
if ('followRedirects' in options) {
throw new TypeError('The `followRedirects` option does not exist. Use `followRedirect` instead.');
}
if (options.agent) {
for (const key in options.agent) {
if (key !== 'http' && key !== 'https' && key !== 'http2') {
throw new TypeError(`Expected the \`options.agent\` properties to be \`http\`, \`https\` or \`http2\`, got \`${key}\``);
}
}
}
options.maxRedirects = (_e = options.maxRedirects) !== null && _e !== void 0 ? _e : 0;
// Set non-enumerable properties
exports.setNonEnumerableProperties([defaults, rawOptions], options);
return normalize_arguments_1.default(options, defaults);
}
_lockWrite() {
const onLockedWrite = () => {
throw new TypeError('The payload has been already provided');
};
this.write = onLockedWrite;
this.end = onLockedWrite;
}
_unlockWrite() {
this.write = super.write;
this.end = super.end;
}
async _finalizeBody() {
const { options } = this;
const { headers } = options;
const isForm = !is_1.default.undefined(options.form);
const isJSON = !is_1.default.undefined(options.json);
const isBody = !is_1.default.undefined(options.body);
const hasPayload = isForm || isJSON || isBody;
const cannotHaveBody = exports.withoutBody.has(options.method) && !(options.method === 'GET' && options.allowGetBody);
this._cannotHaveBody = cannotHaveBody;
if (hasPayload) {
if (cannotHaveBody) {
throw new TypeError(`The \`${options.method}\` method cannot be used with a body`);
}
if ([isBody, isForm, isJSON].filter(isTrue => isTrue).length > 1) {
throw new TypeError('The `body`, `json` and `form` options are mutually exclusive');
}
if (isBody &&
!(options.body instanceof stream_1.Readable) &&
!is_1.default.string(options.body) &&
!is_1.default.buffer(options.body) &&
!is_form_data_1.default(options.body)) {
throw new TypeError('The `body` option must be a stream.Readable, string or Buffer');
}
if (isForm && !is_1.default.object(options.form)) {
throw new TypeError('The `form` option must be an Object');
}
{
// Serialize body
const noContentType = !is_1.default.string(headers['content-type']);
if (isBody) {
// Special case for https://github.com/form-data/form-data
if (is_form_data_1.default(options.body) && noContentType) {
headers['content-type'] = `multipart/form-data; boundary=${options.body.getBoundary()}`;
}
this[kBody] = options.body;
}
else if (isForm) {
if (noContentType) {
headers['content-type'] = 'application/x-www-form-urlencoded';
}
this[kBody] = (new url_1.URLSearchParams(options.form)).toString();
}
else {
if (noContentType) {
headers['content-type'] = 'application/json';
}
this[kBody] = options.stringifyJson(options.json);
}
const uploadBodySize = await get_body_size_1.default(this[kBody], options.headers);
// See https://tools.ietf.org/html/rfc7230#section-3.3.2
// A user agent SHOULD send a Content-Length in a request message when
// no Transfer-Encoding is sent and the request method defines a meaning
// for an enclosed payload body. For example, a Content-Length header
// field is normally sent in a POST request even when the value is 0
// (indicating an empty payload body). A user agent SHOULD NOT send a
// Content-Length header field when the request message does not contain
// a payload body and the method semantics do not anticipate such a
// body.
if (is_1.default.undefined(headers['content-length']) && is_1.default.undefined(headers['transfer-encoding'])) {
if (!cannotHaveBody && !is_1.default.undefined(uploadBodySize)) {
headers['content-length'] = String(uploadBodySize);
}
}
}
}
else if (cannotHaveBody) {
this._lockWrite();
}
else {
this._unlockWrite();
}
this[kBodySize] = Number(headers['content-length']) || undefined;
}
async _onResponseBase(response) {
const { options } = this;
const { url } = options;
this[kOriginalResponse] = response;
if (options.decompress) {
response = decompressResponse(response);
}
const statusCode = response.statusCode;
const typedResponse = response;
typedResponse.statusMessage = typedResponse.statusMessage ? typedResponse.statusMessage : http.STATUS_CODES[statusCode];
typedResponse.url = options.url.toString();
typedResponse.requestUrl = this.requestUrl;
typedResponse.redirectUrls = this.redirects;
typedResponse.request = this;
typedResponse.isFromCache = response.fromCache || false;
typedResponse.ip = this.ip;
typedResponse.retryCount = this.retryCount;
this[kIsFromCache] = typedResponse.isFromCache;
this[kResponseSize] = Number(response.headers['content-length']) || undefined;
this[kResponse] = response;
response.once('end', () => {
this[kResponseSize] = this[kDownloadedSize];
this.emit('downloadProgress', this.downloadProgress);
});
response.once('error', (error) => {
// Force clean-up, because some packages don't do this.
// TODO: Fix decompress-response
response.destroy();
this._beforeError(new ReadError(error, this));
});
response.once('aborted', () => {
this._beforeError(new ReadError({
name: 'Error',
message: 'The server aborted pending request',
code: 'ECONNRESET'
}, this));
});
this.emit('downloadProgress', this.downloadProgress);
const rawCookies = response.headers['set-cookie'];
if (is_1.default.object(options.cookieJar) && rawCookies) {
let promises = rawCookies.map(async (rawCookie) => options.cookieJar.setCookie(rawCookie, url.toString()));
if (options.ignoreInvalidCookies) {
promises = promises.map(async (p) => p.catch(() => { }));
}
try {
await Promise.all(promises);
}
catch (error) {
this._beforeError(error);
return;
}
}
if (options.followRedirect && response.headers.location && redirectCodes.has(statusCode)) {
// We're being redirected, we don't care about the response.
// It'd be best to abort the request, but we can't because
// we would have to sacrifice the TCP connection. We don't want that.
response.resume();
if (this[kRequest]) {
this[kCancelTimeouts]();
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this[kRequest];
this[kUnproxyEvents]();
}
const shouldBeGet = statusCode === 303 && options.method !== 'GET' && options.method !== 'HEAD';
if (shouldBeGet || !options.methodRewriting) {
// Server responded with "see other", indicating that the resource exists at another location,
// and the client should request it from that location via GET or HEAD.
options.method = 'GET';
if ('body' in options) {
delete options.body;
}
if ('json' in options) {
delete options.json;
}
if ('form' in options) {
delete options.form;
}
this[kBody] = undefined;
delete options.headers['content-length'];
}
if (this.redirects.length >= options.maxRedirects) {
this._beforeError(new MaxRedirectsError(this));
return;
}
try {
// Do not remove. See https://github.com/sindresorhus/got/pull/214
const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();
// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
const redirectUrl = new url_1.URL(redirectBuffer, url);
const redirectString = redirectUrl.toString();
decodeURI(redirectString);
// Redirecting to a different site, clear sensitive data.
if (redirectUrl.hostname !== url.hostname || redirectUrl.port !== url.port) {
if ('host' in options.headers) {
delete options.headers.host;
}
if ('cookie' in options.headers) {
delete options.headers.cookie;
}
if ('authorization' in options.headers) {
delete options.headers.authorization;
}
if (options.username || options.password) {
options.username = '';
options.password = '';
}
}
else {
redirectUrl.username = options.username;
redirectUrl.password = options.password;
}
this.redirects.push(redirectString);
options.url = redirectUrl;
for (const hook of options.hooks.beforeRedirect) {
// eslint-disable-next-line no-await-in-loop
await hook(options, typedResponse);
}
this.emit('redirect', typedResponse, options);
await this._makeRequest();
}
catch (error) {
this._beforeError(error);
return;
}
return;
}
if (options.isStream && options.throwHttpErrors && !is_response_ok_1.isResponseOk(typedResponse)) {
this._beforeError(new HTTPError(typedResponse));
return;
}
response.on('readable', () => {
if (this[kTriggerRead]) {
this._read();
}
});
this.on('resume', () => {
response.resume();
});
this.on('pause', () => {
response.pause();
});
response.once('end', () => {
this.push(null);
});
this.emit('response', response);
for (const destination of this[kServerResponsesPiped]) {
if (destination.headersSent) {
continue;
}
// eslint-disable-next-line guard-for-in
for (const key in response.headers) {
const isAllowed = options.decompress ? key !== 'content-encoding' : true;
const value = response.headers[key];
if (isAllowed) {
destination.setHeader(key, value);
}
}
destination.statusCode = statusCode;
}
}
async _onResponse(response) {
try {
await this._onResponseBase(response);
}
catch (error) {
/* istanbul ignore next: better safe than sorry */
this._beforeError(error);
}
}
_onRequest(request) {
const { options } = this;
const { timeout, url } = options;
http_timer_1.default(request);
this[kCancelTimeouts] = timed_out_1.default(request, timeout, url);
const responseEventName = options.cache ? 'cacheableResponse' : 'response';
request.once(responseEventName, (response) => {
void this._onResponse(response);
});
request.once('error', (error) => {
var _a;
// Force clean-up, because some packages (e.g. nock) don't do this.
request.destroy();
// Node.js <= 12.18.2 mistakenly emits the response `end` first.
(_a = request.res) === null || _a === void 0 ? void 0 : _a.removeAllListeners('end');
error = error instanceof timed_out_1.TimeoutError ? new TimeoutError(error, this.timings, this) : new RequestError(error.message, error, this);
this._beforeError(error);
});
this[kUnproxyEvents] = proxy_events_1.default(request, this, proxiedRequestEvents);
this[kRequest] = request;
this.emit('uploadProgress', this.uploadProgress);
// Send body
const body = this[kBody];
const currentRequest = this.redirects.length === 0 ? this : request;
if (is_1.default.nodeStream(body)) {
body.pipe(currentRequest);
body.once('error', (error) => {
this._beforeError(new UploadError(error, this));
});
}
else {
this._unlockWrite();
if (!is_1.default.undefined(body)) {
this._writeRequest(body, undefined, () => { });
currentRequest.end();
this._lockWrite();
}
else if (this._cannotHaveBody || this._noPipe) {
currentRequest.end();
this._lockWrite();
}
}
this.emit('request', request);
}
async _createCacheableRequest(url, options) {
return new Promise((resolve, reject) => {
// TODO: Remove `utils/url-to-options.ts` when `cacheable-request` is fixed
Object.assign(options, url_to_options_1.default(url));
// `http-cache-semantics` checks this
// TODO: Fix this ignore.
// @ts-expect-error
delete options.url;
let request;
// This is ugly
const cacheRequest = cacheableStore.get(options.cache)(options, async (response) => {
// TODO: Fix `cacheable-response`
response._readableState.autoDestroy = false;
if (request) {
(await request).emit('cacheableResponse', response);
}
resolve(response);
});
// Restore options
options.url = url;
cacheRequest.once('error', reject);
cacheRequest.once('request', async (requestOrPromise) => {
request = requestOrPromise;
resolve(request);
});
});
}
async _makeRequest() {
var _a, _b, _c, _d, _e;
const { options } = this;
const { headers } = options;
for (const key in headers) {
if (is_1.default.undefined(headers[key])) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete headers[key];
}
else if (is_1.default.null_(headers[key])) {
throw new TypeError(`Use \`undefined\` instead of \`null\` to delete the \`${key}\` header`);
}
}
if (options.decompress && is_1.default.undefined(headers['accept-encoding'])) {
headers['accept-encoding'] = supportsBrotli ? 'gzip, deflate, br' : 'gzip, deflate';
}
// Set cookies
if (options.cookieJar) {
const cookieString = await options.cookieJar.getCookieString(options.url.toString());
if (is_1.default.nonEmptyString(cookieString)) {
options.headers.cookie = cookieString;
}
}
for (const hook of options.hooks.beforeRequest) {
// eslint-disable-next-line no-await-in-loop
const result = await hook(options);
if (!is_1.default.undefined(result)) {
// @ts-expect-error Skip the type mismatch to support abstract responses
options.request = () => result;
break;
}
}
if (options.body && this[kBody] !== options.body) {
this[kBody] = options.body;
}
const { agent, request, timeout, url } = options;
if (options.dnsCache && !('lookup' in options)) {
options.lookup = options.dnsCache.lookup;
}
// UNIX sockets
if (url.hostname === 'unix') {
const matches = /(?<socketPath>.+?):(?<path>.+)/.exec(`${url.pathname}${url.search}`);
if (matches === null || matches === void 0 ? void 0 : matches.groups) {
const { socketPath, path } = matches.groups;
Object.assign(options, {
socketPath,
path,
host: ''
});
}
}
const isHttps = url.protocol === 'https:';
// Fallback function
let fallbackFn;
if (options.http2) {
fallbackFn = http2wrapper.auto;
}
else {
fallbackFn = isHttps ? https.request : http.request;
}
const realFn = (_a = options.request) !== null && _a !== void 0 ? _a : fallbackFn;
// Cache support
const fn = options.cache ? this._createCacheableRequest : realFn;
// Pass an agent directly when HTTP2 is disabled
if (agent && !options.http2) {
options.agent = agent[isHttps ? 'https' : 'http'];
}
// Prepare plain HTTP request options
options[kRequest] = realFn;
delete options.request;
// TODO: Fix this ignore.
// @ts-expect-error
delete options.timeout;
const requestOptions = options;
requestOptions.shared = (_b = options.cacheOptions) === null || _b === void 0 ? void 0 : _b.shared;
requestOptions.cacheHeuristic = (_c = options.cacheOptions) === null || _c === void 0 ? void 0 : _c.cacheHeuristic;
requestOptions.immutableMinTimeToLive = (_d = options.cacheOptions) === null || _d === void 0 ? void 0 : _d.immutableMinTimeToLive;
requestOptions.ignoreCargoCult = (_e = options.cacheOptions) === null || _e === void 0 ? void 0 : _e.ignoreCargoCult;
// If `dnsLookupIpVersion` is not present do not override `family`
if (options.dnsLookupIpVersion !== undefined) {
try {
requestOptions.family = dns_ip_version_1.dnsLookupIpVersionToFamily(options.dnsLookupIpVersion);
}
catch (_f) {
throw new Error('Invalid `dnsLookupIpVersion` option value');
}
}
// HTTPS options remapping
if (options.https) {
if ('rejectUnauthorized' in options.https) {
requestOptions.rejectUnauthorized = options.https.rejectUnauthorized;
}
if (options.https.checkServerIdentity) {
requestOptions.checkServerIdentity = options.https.checkServerIdentity;
}
if (options.https.certificateAuthority) {
requestOptions.ca = options.https.certificateAuthority;
}
if (options.https.certificate) {
requestOptions.cert = options.https.certificate;
}
if (options.https.key) {
requestOptions.key = options.https.key;
}
if (options.https.passphrase) {
requestOptions.passphrase = options.https.passphrase;
}
if (options.https.pfx) {
requestOptions.pfx = options.https.pfx;
}
}
try {
let requestOrResponse = await fn(url, requestOptions);
if (is_1.default.undefined(requestOrResponse)) {
requestOrResponse = fallbackFn(url, requestOptions);
}
// Restore options
options.request = request;
options.timeout = timeout;
options.agent = agent;
// HTTPS options restore
if (options.https) {
if ('rejectUnauthorized' in options.https) {
delete requestOptions.rejectUnauthorized;
}
if (options.https.checkServerIdentity) {
// @ts-expect-error - This one will be removed when we remove the alias.
delete requestOptions.checkServerIdentity;
}
if (options.https.certificateAuthority) {
delete requestOptions.ca;
}
if (options.https.certificate) {
delete requestOptions.cert;
}
if (options.https.key) {
delete requestOptions.key;
}
if (options.https.passphrase) {
delete requestOptions.passphrase;
}
if (options.https.pfx) {
delete requestOptions.pfx;
}
}
if (isClientRequest(requestOrResponse)) {
this._onRequest(requestOrResponse);
// Emit the response after the stream has been ended
}
else if (this.writable) {
this.once('finish', () => {
void this._onResponse(requestOrResponse);
});
this._unlockWrite();
this.end();
this._lockWrite();
}
else {
void this._onResponse(requestOrResponse);
}
}
catch (error) {
if (error instanceof CacheableRequest.CacheError) {
throw new CacheError(error, this);
}
throw new RequestError(error.message, error, this);
}
}
async _error(error) {
try {
for (const hook of this.options.hooks.beforeError) {
// eslint-disable-next-line no-await-in-loop
error = await hook(error);
}
}
catch (error_) {
error = new RequestError(error_.message, error_, this);
}
this.destroy(error);
}
_beforeError(error) {
if (this[kStopReading]) {
return;
}
const { options } = this;
const retryCount = this.retryCount + 1;
this[kStopReading] = true;
if (!(error instanceof RequestError)) {
error = new RequestError(error.message, error, this);
}
const typedError = error;
const { response } = typedError;
void (async () => {
if (response && !response.body) {
response.setEncoding(this._readableState.encoding);
try {
response.rawBody = await get_buffer_1.default(response);
response.body = response.rawBody.toString();
}
catch (_a) { }
}
if (this.listenerCount('retry') !== 0) {
let backoff;
try {
let retryAfter;
if (response && 'retry-after' in response.headers) {
retryAfter = Number(response.headers['retry-after']);
if (Number.isNaN(retryAfter)) {
retryAfter = Date.parse(response.headers['retry-after']) - Date.now();
if (retryAfter <= 0) {
retryAfter = 1;
}
}
else {
retryAfter *= 1000;
}
}
backoff = await options.retry.calculateDelay({
attemptCount: retryCount,
retryOptions: options.retry,
error: typedError,
retryAfter,
computedValue: calculate_retry_delay_1.default({
attemptCount: retryCount,
retryOptions: options.retry,
error: typedError,
retryAfter,
computedValue: 0
})
});
}
catch (error_) {
void this._error(new RequestError(error_.message, error_, this));
return;
}
if (backoff) {
const retry = async () => {
try {
for (const hook of this.options.hooks.beforeRetry) {
// eslint-disable-next-line no-await-in-loop
await hook(this.options, typedError, retryCount);
}
}
catch (error_) {
void this._error(new RequestError(error_.message, error, this));
return;
}
// Something forced us to abort the retry
if (this.destroyed) {
return;
}
this.destroy();
this.emit('retry', retryCount, error);
};
this[kRetryTimeout] = setTimeout(retry, backoff);
return;
}
}
void this._error(typedError);
})();
}
_read() {
this[kTriggerRead] = true;
const response = this[kResponse];
if (response && !this[kStopReading]) {
// We cannot put this in the `if` above
// because `.read()` also triggers the `end` event
if (response.readableLength) {
this[kTriggerRead] = false;
}
let data;
while ((data = response.read()) !== null) {
this[kDownloadedSize] += data.length;
this[kStartedReading] = true;
const progress = this.downloadProgress;
if (progress.percent < 1) {
this.emit('downloadProgress', progress);
}
this.push(data);
}
}
}
// Node.js 12 has incorrect types, so the encoding must be a string
_write(chunk, encoding, callback) {
const write = () => {
this._writeRequest(chunk, encoding, callback);
};
if (this.requestInitialized) {
write();
}
else {
this[kJobs].push(write);
}
}
_writeRequest(chunk, encoding, callback) {
if (this[kRequest].destroyed) {
// Probably the `ClientRequest` instance will throw
return;
}
this._progressCallbacks.push(() => {
this[kUploadedSize] += Buffer.byteLength(chunk, encoding);
const progress = this.uploadProgress;
if (progress.percent < 1) {
this.emit('uploadProgress', progress);
}
});
// TODO: What happens if it's from cache? Then this[kRequest] won't be defined.
this[kRequest].write(chunk, encoding, (error) => {
if (!error && this._progressCallbacks.length > 0) {
this._progressCallbacks.shift()();
}
callback(error);
});
}
_final(callback) {
const endRequest = () => {
// FIX: Node.js 10 calls the write callback AFTER the end callback!
while (this._progressCallbacks.length !== 0) {
this._progressCallbacks.shift()();
}
// We need to check if `this[kRequest]` is present,
// because it isn't when we use cache.
if (!(kRequest in this)) {
callback();
return;
}
if (this[kRequest].destroyed) {
callback();
return;
}
this[kRequest].end((error) => {
if (!error) {
this[kBodySize] = this[kUploadedSize];
this.emit('uploadProgress', this.uploadProgress);
this[kRequest].emit('upload-complete');
}
callback(error);
});
};
if (this.requestInitialized) {
endRequest();
}
else {
this[kJobs].push(endRequest);
}
}
_destroy(error, callback) {
var _a;
this[kStopReading] = true;
// Prevent further retries
clearTimeout(this[kRetryTimeout]);
if (kRequest in this) {
this[kCancelTimeouts]();
// TODO: Remove the next `if` when these get fixed:
// - https://github.com/nodejs/node/issues/32851
if (!((_a = this[kResponse]) === null || _a === void 0 ? void 0 : _a.complete)) {
this[kRequest].destroy();
}
}
if (error !== null && !is_1.default.undefined(error) && !(error instanceof RequestError)) {
error = new RequestError(error.message, error, this);
}
callback(error);
}
get _isAboutToError() {
return this[kStopReading];
}
/**
The remote IP address.
*/
get ip() {
var _a;
return (_a = this.socket) === null || _a === void 0 ? void 0 : _a.remoteAddress;
}
/**
Indicates whether the request has been aborted or not.
*/
get aborted() {
var _a, _b, _c;
return ((_b = (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.destroyed) !== null && _b !== void 0 ? _b : this.destroyed) && !((_c = this[kOriginalResponse]) === null || _c === void 0 ? void 0 : _c.complete);
}
get socket() {
var _a, _b;
return (_b = (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket) !== null && _b !== void 0 ? _b : undefined;
}
/**
Progress event for downloading (receiving a response).
*/
get downloadProgress() {
let percent;
if (this[kResponseSize]) {
percent = this[kDownloadedSize] / this[kResponseSize];
}
else if (this[kResponseSize] === this[kDownloadedSize]) {
percent = 1;
}
else {
percent = 0;
}
return {
percent,
transferred: this[kDownloadedSize],
total: this[kResponseSize]
};
}
/**
Progress event for uploading (sending a request).
*/
get uploadProgress() {
let percent;
if (this[kBodySize]) {
percent = this[kUploadedSize] / this[kBodySize];
}
else if (this[kBodySize] === this[kUploadedSize]) {
percent = 1;
}
else {
percent = 0;
}
return {
percent,
transferred: this[kUploadedSize],
total: this[kBodySize]
};
}
/**
The object contains the following properties:
- `start` - Time when the request started.
- `socket` - Time when a socket was assigned to the request.
- `lookup` - Time when the DNS lookup finished.
- `connect` - Time when the socket successfully connected.
- `secureConnect` - Time when the socket securely connected.
- `upload` - Time when the request finished uploading.
- `response` - Time when the request fired `response` event.
- `end` - Time when the response fired `end` event.
- `error` - Time when the request fired `error` event.
- `abort` - Time when the request fired `abort` event.
- `phases`
- `wait` - `timings.socket - timings.start`
- `dns` - `timings.lookup - timings.socket`
- `tcp` - `timings.connect - timings.lookup`
- `tls` - `timings.secureConnect - timings.connect`
- `request` - `timings.upload - (timings.secureConnect || timings.connect)`
- `firstByte` - `timings.response - timings.upload`
- `download` - `timings.end - timings.response`
- `total` - `(timings.end || timings.error || timings.abort) - timings.start`
If something has not been measured yet, it will be `undefined`.
__Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
*/
get timings() {
var _a;
return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.timings;
}
/**
Whether the response was retrieved from the cache.
*/
get isFromCache() {
return this[kIsFromCache];
}
pipe(destination, options) {
if (this[kStartedReading]) {
throw new Error('Failed to pipe. The response has been emitted already.');
}
if (destination instanceof http_1.ServerResponse) {
this[kServerResponsesPiped].add(destination);
}
return super.pipe(destination, options);
}
unpipe(destination) {
if (destination instanceof http_1.ServerResponse) {
this[kServerResponsesPiped].delete(destination);
}
super.unpipe(destination);
return this;
}
}
exports.default = Request;
|