got 11.5.2 → 11.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/source/as-promise/index.d.ts +1 -3
- package/dist/source/as-promise/index.js +44 -125
- package/dist/source/as-promise/normalize-arguments.d.ts +3 -0
- package/dist/source/as-promise/normalize-arguments.js +78 -0
- package/dist/source/as-promise/parse-body.d.ts +3 -0
- package/dist/source/as-promise/parse-body.js +25 -0
- package/dist/source/as-promise/types.d.ts +206 -24
- package/dist/source/as-promise/types.js +18 -7
- package/dist/source/{as-promise → core}/calculate-retry-delay.d.ts +2 -1
- package/dist/source/core/calculate-retry-delay.js +29 -0
- package/dist/source/core/index.d.ts +822 -5
- package/dist/source/core/index.js +198 -39
- package/dist/source/core/utils/is-response-ok.d.ts +2 -0
- package/dist/source/core/utils/is-response-ok.js +8 -0
- package/dist/source/create.js +16 -7
- package/dist/source/index.js +3 -2
- package/dist/source/types.d.ts +240 -1
- package/package.json +20 -16
- package/readme.md +75 -7
- package/dist/source/as-promise/calculate-retry-delay.js +0 -38
- package/dist/source/as-promise/core.d.ts +0 -13
- package/dist/source/as-promise/core.js +0 -127
|
@@ -25,7 +25,10 @@ const options_to_url_1 = require("./utils/options-to-url");
|
|
|
25
25
|
const weakable_map_1 = require("./utils/weakable-map");
|
|
26
26
|
const get_buffer_1 = require("./utils/get-buffer");
|
|
27
27
|
const dns_ip_version_1 = require("./utils/dns-ip-version");
|
|
28
|
+
const is_response_ok_1 = require("./utils/is-response-ok");
|
|
28
29
|
const deprecation_warning_1 = require("../utils/deprecation-warning");
|
|
30
|
+
const normalize_arguments_1 = require("../as-promise/normalize-arguments");
|
|
31
|
+
const calculate_retry_delay_1 = require("./calculate-retry-delay");
|
|
29
32
|
const globalDnsCache = new cacheable_lookup_1.default();
|
|
30
33
|
const kRequest = Symbol('request');
|
|
31
34
|
const kResponse = Symbol('response');
|
|
@@ -43,10 +46,19 @@ const kTriggerRead = Symbol('triggerRead');
|
|
|
43
46
|
const kBody = Symbol('body');
|
|
44
47
|
const kJobs = Symbol('jobs');
|
|
45
48
|
const kOriginalResponse = Symbol('originalResponse');
|
|
49
|
+
const kRetryTimeout = Symbol('retryTimeout');
|
|
46
50
|
exports.kIsNormalizedAlready = Symbol('isNormalizedAlready');
|
|
47
51
|
const supportsBrotli = is_1.default.string(process.versions.brotli);
|
|
48
52
|
exports.withoutBody = new Set(['GET', 'HEAD']);
|
|
49
|
-
exports.knownHookEvents = [
|
|
53
|
+
exports.knownHookEvents = [
|
|
54
|
+
'init',
|
|
55
|
+
'beforeRequest',
|
|
56
|
+
'beforeRedirect',
|
|
57
|
+
'beforeError',
|
|
58
|
+
'beforeRetry',
|
|
59
|
+
// Promise-Only
|
|
60
|
+
'afterResponse'
|
|
61
|
+
];
|
|
50
62
|
function validateSearchParameters(searchParameters) {
|
|
51
63
|
// eslint-disable-next-line guard-for-in
|
|
52
64
|
for (const key in searchParameters) {
|
|
@@ -103,6 +115,10 @@ exports.setNonEnumerableProperties = (sources, to) => {
|
|
|
103
115
|
}
|
|
104
116
|
Object.defineProperties(to, properties);
|
|
105
117
|
};
|
|
118
|
+
/**
|
|
119
|
+
An error to be thrown when a request fails.
|
|
120
|
+
Contains a `code` property with error class code, like `ECONNREFUSED`.
|
|
121
|
+
*/
|
|
106
122
|
class RequestError extends Error {
|
|
107
123
|
constructor(message, error, self) {
|
|
108
124
|
var _a;
|
|
@@ -149,6 +165,10 @@ class RequestError extends Error {
|
|
|
149
165
|
}
|
|
150
166
|
}
|
|
151
167
|
exports.RequestError = RequestError;
|
|
168
|
+
/**
|
|
169
|
+
An error to be thrown when the server redirects you more than ten times.
|
|
170
|
+
Includes a `response` property.
|
|
171
|
+
*/
|
|
152
172
|
class MaxRedirectsError extends RequestError {
|
|
153
173
|
constructor(request) {
|
|
154
174
|
super(`Redirected ${request.options.maxRedirects} times. Aborting.`, {}, request);
|
|
@@ -156,6 +176,10 @@ class MaxRedirectsError extends RequestError {
|
|
|
156
176
|
}
|
|
157
177
|
}
|
|
158
178
|
exports.MaxRedirectsError = MaxRedirectsError;
|
|
179
|
+
/**
|
|
180
|
+
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.
|
|
181
|
+
Includes a `response` property.
|
|
182
|
+
*/
|
|
159
183
|
class HTTPError extends RequestError {
|
|
160
184
|
constructor(response) {
|
|
161
185
|
super(`Response code ${response.statusCode} (${response.statusMessage})`, {}, response.request);
|
|
@@ -163,6 +187,10 @@ class HTTPError extends RequestError {
|
|
|
163
187
|
}
|
|
164
188
|
}
|
|
165
189
|
exports.HTTPError = HTTPError;
|
|
190
|
+
/**
|
|
191
|
+
An error to be thrown when a cache method fails.
|
|
192
|
+
For example, if the database goes down or there's a filesystem error.
|
|
193
|
+
*/
|
|
166
194
|
class CacheError extends RequestError {
|
|
167
195
|
constructor(error, request) {
|
|
168
196
|
super(error.message, error, request);
|
|
@@ -170,6 +198,9 @@ class CacheError extends RequestError {
|
|
|
170
198
|
}
|
|
171
199
|
}
|
|
172
200
|
exports.CacheError = CacheError;
|
|
201
|
+
/**
|
|
202
|
+
An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
|
|
203
|
+
*/
|
|
173
204
|
class UploadError extends RequestError {
|
|
174
205
|
constructor(error, request) {
|
|
175
206
|
super(error.message, error, request);
|
|
@@ -177,6 +208,10 @@ class UploadError extends RequestError {
|
|
|
177
208
|
}
|
|
178
209
|
}
|
|
179
210
|
exports.UploadError = UploadError;
|
|
211
|
+
/**
|
|
212
|
+
An error to be thrown when the request is aborted due to a timeout.
|
|
213
|
+
Includes an `event` and `timings` property.
|
|
214
|
+
*/
|
|
180
215
|
class TimeoutError extends RequestError {
|
|
181
216
|
constructor(error, timings, request) {
|
|
182
217
|
super(error.message, error, request);
|
|
@@ -186,6 +221,9 @@ class TimeoutError extends RequestError {
|
|
|
186
221
|
}
|
|
187
222
|
}
|
|
188
223
|
exports.TimeoutError = TimeoutError;
|
|
224
|
+
/**
|
|
225
|
+
An error to be thrown when reading from response stream fails.
|
|
226
|
+
*/
|
|
189
227
|
class ReadError extends RequestError {
|
|
190
228
|
constructor(error, request) {
|
|
191
229
|
super(error.message, error, request);
|
|
@@ -193,6 +231,9 @@ class ReadError extends RequestError {
|
|
|
193
231
|
}
|
|
194
232
|
}
|
|
195
233
|
exports.ReadError = ReadError;
|
|
234
|
+
/**
|
|
235
|
+
An error to be thrown when given an unsupported protocol.
|
|
236
|
+
*/
|
|
196
237
|
class UnsupportedProtocolError extends RequestError {
|
|
197
238
|
constructor(options) {
|
|
198
239
|
super(`Unsupported protocol "${options.url.protocol}"`, {}, options);
|
|
@@ -211,6 +252,9 @@ const proxiedRequestEvents = [
|
|
|
211
252
|
class Request extends stream_1.Duplex {
|
|
212
253
|
constructor(url, options = {}, defaults) {
|
|
213
254
|
super({
|
|
255
|
+
// This must be false, to enable throwing after destroy
|
|
256
|
+
// It is used for retry logic in Promise API
|
|
257
|
+
autoDestroy: false,
|
|
214
258
|
// It needs to be zero because we're just proxying the data to another stream
|
|
215
259
|
highWaterMark: 0
|
|
216
260
|
});
|
|
@@ -222,6 +266,7 @@ class Request extends stream_1.Duplex {
|
|
|
222
266
|
this[kStopReading] = false;
|
|
223
267
|
this[kTriggerRead] = false;
|
|
224
268
|
this[kJobs] = [];
|
|
269
|
+
this.retryCount = 0;
|
|
225
270
|
// TODO: Remove this when targeting Node.js >= 12
|
|
226
271
|
this._progressCallbacks = [];
|
|
227
272
|
const unlockWrite = () => this._unlockWrite();
|
|
@@ -279,6 +324,8 @@ class Request extends stream_1.Duplex {
|
|
|
279
324
|
for (const job of this[kJobs]) {
|
|
280
325
|
job();
|
|
281
326
|
}
|
|
327
|
+
// Prevent memory leak
|
|
328
|
+
this[kJobs].length = 0;
|
|
282
329
|
this.requestInitialized = true;
|
|
283
330
|
}
|
|
284
331
|
catch (error) {
|
|
@@ -348,6 +395,7 @@ class Request extends stream_1.Duplex {
|
|
|
348
395
|
is_1.assert.any([is_1.default.string, is_1.default.object, is_1.default.array, is_1.default.undefined], options.https.certificate);
|
|
349
396
|
is_1.assert.any([is_1.default.string, is_1.default.undefined], options.https.passphrase);
|
|
350
397
|
}
|
|
398
|
+
is_1.assert.any([is_1.default.object, is_1.default.undefined], options.cacheOptions);
|
|
351
399
|
// `options.method`
|
|
352
400
|
if (is_1.default.string(options.method)) {
|
|
353
401
|
options.method = options.method.toUpperCase();
|
|
@@ -479,9 +527,7 @@ class Request extends stream_1.Duplex {
|
|
|
479
527
|
getCookieString = util_1.promisify(getCookieString.bind(options.cookieJar));
|
|
480
528
|
options.cookieJar = {
|
|
481
529
|
setCookie,
|
|
482
|
-
|
|
483
|
-
// @ts-expect-error TypeScript thinks that promisifying callback(error, string) will result in Promise<void>
|
|
484
|
-
getCookieString
|
|
530
|
+
getCookieString: getCookieString
|
|
485
531
|
};
|
|
486
532
|
}
|
|
487
533
|
}
|
|
@@ -522,6 +568,8 @@ class Request extends stream_1.Duplex {
|
|
|
522
568
|
}), cache));
|
|
523
569
|
}
|
|
524
570
|
}
|
|
571
|
+
// `options.cacheOptions`
|
|
572
|
+
options.cacheOptions = { ...options.cacheOptions };
|
|
525
573
|
// `options.dnsCache`
|
|
526
574
|
if (options.dnsCache === true) {
|
|
527
575
|
options.dnsCache = globalDnsCache;
|
|
@@ -615,7 +663,7 @@ class Request extends stream_1.Duplex {
|
|
|
615
663
|
options.maxRedirects = (_d = options.maxRedirects) !== null && _d !== void 0 ? _d : 0;
|
|
616
664
|
// Set non-enumerable properties
|
|
617
665
|
exports.setNonEnumerableProperties([defaults, rawOptions], options);
|
|
618
|
-
return options;
|
|
666
|
+
return normalize_arguments_1.default(options, defaults);
|
|
619
667
|
}
|
|
620
668
|
_lockWrite() {
|
|
621
669
|
const onLockedWrite = () => {
|
|
@@ -717,6 +765,7 @@ class Request extends stream_1.Duplex {
|
|
|
717
765
|
typedResponse.request = this;
|
|
718
766
|
typedResponse.isFromCache = response.fromCache || false;
|
|
719
767
|
typedResponse.ip = this.ip;
|
|
768
|
+
typedResponse.retryCount = this.retryCount;
|
|
720
769
|
this[kIsFromCache] = typedResponse.isFromCache;
|
|
721
770
|
this[kResponseSize] = Number(response.headers['content-length']) || undefined;
|
|
722
771
|
this[kResponse] = response;
|
|
@@ -754,7 +803,7 @@ class Request extends stream_1.Duplex {
|
|
|
754
803
|
}
|
|
755
804
|
if (options.followRedirect && response.headers.location && redirectCodes.has(statusCode)) {
|
|
756
805
|
// We're being redirected, we don't care about the response.
|
|
757
|
-
// It'd be
|
|
806
|
+
// It'd be best to abort the request, but we can't because
|
|
758
807
|
// we would have to sacrifice the TCP connection. We don't want that.
|
|
759
808
|
response.resume();
|
|
760
809
|
if (this[kRequest]) {
|
|
@@ -801,7 +850,10 @@ class Request extends stream_1.Duplex {
|
|
|
801
850
|
delete options.headers.authorization;
|
|
802
851
|
}
|
|
803
852
|
if (options.username || options.password) {
|
|
853
|
+
// TODO: Fix this ignore.
|
|
854
|
+
// @ts-expect-error
|
|
804
855
|
delete options.username;
|
|
856
|
+
// @ts-expect-error
|
|
805
857
|
delete options.password;
|
|
806
858
|
}
|
|
807
859
|
}
|
|
@@ -820,16 +872,9 @@ class Request extends stream_1.Duplex {
|
|
|
820
872
|
}
|
|
821
873
|
return;
|
|
822
874
|
}
|
|
823
|
-
|
|
824
|
-
const isOk = (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304;
|
|
825
|
-
if (options.throwHttpErrors && !isOk) {
|
|
826
|
-
// Normally we would have to use `void [await] this._beforeError(error)` everywhere,
|
|
827
|
-
// but since there's `void (async () => { ... })()` inside of it, we don't have to.
|
|
875
|
+
if (options.isStream && options.throwHttpErrors && !is_response_ok_1.isResponseOk(typedResponse)) {
|
|
828
876
|
this._beforeError(new HTTPError(typedResponse));
|
|
829
|
-
|
|
830
|
-
if (this[kStopReading]) {
|
|
831
|
-
return;
|
|
832
|
-
}
|
|
877
|
+
return;
|
|
833
878
|
}
|
|
834
879
|
response.on('readable', () => {
|
|
835
880
|
if (this[kTriggerRead]) {
|
|
@@ -866,6 +911,7 @@ class Request extends stream_1.Duplex {
|
|
|
866
911
|
await this._onResponseBase(response);
|
|
867
912
|
}
|
|
868
913
|
catch (error) {
|
|
914
|
+
/* istanbul ignore next: better safe than sorry */
|
|
869
915
|
this._beforeError(error);
|
|
870
916
|
}
|
|
871
917
|
}
|
|
@@ -903,9 +949,6 @@ class Request extends stream_1.Duplex {
|
|
|
903
949
|
body.once('error', (error) => {
|
|
904
950
|
this._beforeError(new UploadError(error, this));
|
|
905
951
|
});
|
|
906
|
-
body.once('end', () => {
|
|
907
|
-
delete options.body;
|
|
908
|
-
});
|
|
909
952
|
}
|
|
910
953
|
else {
|
|
911
954
|
this._unlockWrite();
|
|
@@ -926,6 +969,8 @@ class Request extends stream_1.Duplex {
|
|
|
926
969
|
// TODO: Remove `utils/url-to-options.ts` when `cacheable-request` is fixed
|
|
927
970
|
Object.assign(options, url_to_options_1.default(url));
|
|
928
971
|
// `http-cache-semantics` checks this
|
|
972
|
+
// TODO: Fix this ignore.
|
|
973
|
+
// @ts-expect-error
|
|
929
974
|
delete options.url;
|
|
930
975
|
let request;
|
|
931
976
|
// This is ugly
|
|
@@ -947,7 +992,7 @@ class Request extends stream_1.Duplex {
|
|
|
947
992
|
});
|
|
948
993
|
}
|
|
949
994
|
async _makeRequest() {
|
|
950
|
-
var _a;
|
|
995
|
+
var _a, _b, _c, _d, _e;
|
|
951
996
|
const { options } = this;
|
|
952
997
|
const { headers } = options;
|
|
953
998
|
for (const key in headers) {
|
|
@@ -1013,14 +1058,20 @@ class Request extends stream_1.Duplex {
|
|
|
1013
1058
|
// Prepare plain HTTP request options
|
|
1014
1059
|
options[kRequest] = realFn;
|
|
1015
1060
|
delete options.request;
|
|
1061
|
+
// TODO: Fix this ignore.
|
|
1062
|
+
// @ts-expect-error
|
|
1016
1063
|
delete options.timeout;
|
|
1017
1064
|
const requestOptions = options;
|
|
1065
|
+
requestOptions.shared = (_b = options.cacheOptions) === null || _b === void 0 ? void 0 : _b.shared;
|
|
1066
|
+
requestOptions.cacheHeuristic = (_c = options.cacheOptions) === null || _c === void 0 ? void 0 : _c.cacheHeuristic;
|
|
1067
|
+
requestOptions.immutableMinTimeToLive = (_d = options.cacheOptions) === null || _d === void 0 ? void 0 : _d.immutableMinTimeToLive;
|
|
1068
|
+
requestOptions.ignoreCargoCult = (_e = options.cacheOptions) === null || _e === void 0 ? void 0 : _e.ignoreCargoCult;
|
|
1018
1069
|
// If `dnsLookupIpVersion` is not present do not override `family`
|
|
1019
1070
|
if (options.dnsLookupIpVersion !== undefined) {
|
|
1020
1071
|
try {
|
|
1021
1072
|
requestOptions.family = dns_ip_version_1.dnsLookupIpVersionToFamily(options.dnsLookupIpVersion);
|
|
1022
1073
|
}
|
|
1023
|
-
catch (
|
|
1074
|
+
catch (_f) {
|
|
1024
1075
|
throw new Error('Invalid `dnsLookupIpVersion` option value');
|
|
1025
1076
|
}
|
|
1026
1077
|
}
|
|
@@ -1060,6 +1111,7 @@ class Request extends stream_1.Duplex {
|
|
|
1060
1111
|
delete requestOptions.rejectUnauthorized;
|
|
1061
1112
|
}
|
|
1062
1113
|
if (options.https.checkServerIdentity) {
|
|
1114
|
+
// @ts-expect-error - This one will be removed when we remove the alias.
|
|
1063
1115
|
delete requestOptions.checkServerIdentity;
|
|
1064
1116
|
}
|
|
1065
1117
|
if (options.https.certificateAuthority) {
|
|
@@ -1098,34 +1150,97 @@ class Request extends stream_1.Duplex {
|
|
|
1098
1150
|
throw new RequestError(error.message, error, this);
|
|
1099
1151
|
}
|
|
1100
1152
|
}
|
|
1153
|
+
async _error(error) {
|
|
1154
|
+
try {
|
|
1155
|
+
for (const hook of this.options.hooks.beforeError) {
|
|
1156
|
+
// eslint-disable-next-line no-await-in-loop
|
|
1157
|
+
error = await hook(error);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
catch (error_) {
|
|
1161
|
+
error = new RequestError(error_.message, error_, this);
|
|
1162
|
+
}
|
|
1163
|
+
this.destroy(error);
|
|
1164
|
+
}
|
|
1101
1165
|
_beforeError(error) {
|
|
1102
|
-
if (this
|
|
1166
|
+
if (this[kStopReading]) {
|
|
1103
1167
|
return;
|
|
1104
1168
|
}
|
|
1169
|
+
const { options } = this;
|
|
1170
|
+
const retryCount = this.retryCount + 1;
|
|
1105
1171
|
this[kStopReading] = true;
|
|
1106
1172
|
if (!(error instanceof RequestError)) {
|
|
1107
1173
|
error = new RequestError(error.message, error, this);
|
|
1108
1174
|
}
|
|
1175
|
+
const typedError = error;
|
|
1176
|
+
const { response } = typedError;
|
|
1109
1177
|
void (async () => {
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
response.setEncoding(this._readableState.encoding);
|
|
1178
|
+
if (response && !response.body) {
|
|
1179
|
+
response.setEncoding(this._readableState.encoding);
|
|
1180
|
+
try {
|
|
1114
1181
|
response.rawBody = await get_buffer_1.default(response);
|
|
1115
|
-
response.body = response.rawBody.toString();
|
|
1116
1182
|
}
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1183
|
+
catch (_a) { }
|
|
1184
|
+
response.body = response.rawBody.toString();
|
|
1185
|
+
}
|
|
1186
|
+
if (this.listenerCount('retry') !== 0) {
|
|
1187
|
+
let backoff;
|
|
1188
|
+
try {
|
|
1189
|
+
let retryAfter;
|
|
1190
|
+
if (response && 'retry-after' in response.headers) {
|
|
1191
|
+
retryAfter = Number(response.headers['retry-after']);
|
|
1192
|
+
if (Number.isNaN(retryAfter)) {
|
|
1193
|
+
retryAfter = Date.parse(response.headers['retry-after']) - Date.now();
|
|
1194
|
+
if (retryAfter <= 0) {
|
|
1195
|
+
retryAfter = 1;
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
else {
|
|
1199
|
+
retryAfter *= 1000;
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
backoff = await options.retry.calculateDelay({
|
|
1203
|
+
attemptCount: retryCount,
|
|
1204
|
+
retryOptions: options.retry,
|
|
1205
|
+
error: typedError,
|
|
1206
|
+
retryAfter,
|
|
1207
|
+
computedValue: calculate_retry_delay_1.default({
|
|
1208
|
+
attemptCount: retryCount,
|
|
1209
|
+
retryOptions: options.retry,
|
|
1210
|
+
error: typedError,
|
|
1211
|
+
retryAfter,
|
|
1212
|
+
computedValue: 0
|
|
1213
|
+
})
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
catch (error_) {
|
|
1217
|
+
void this._error(new RequestError(error_.message, error_, this));
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
if (backoff) {
|
|
1221
|
+
const retry = async () => {
|
|
1222
|
+
try {
|
|
1223
|
+
for (const hook of this.options.hooks.beforeRetry) {
|
|
1224
|
+
// eslint-disable-next-line no-await-in-loop
|
|
1225
|
+
await hook(this.options, typedError, retryCount);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
catch (error_) {
|
|
1229
|
+
void this._error(new RequestError(error_.message, error, this));
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
// Something forced us to abort the retry
|
|
1233
|
+
if (this.destroyed) {
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
this.destroy();
|
|
1237
|
+
this.emit('retry', retryCount, error);
|
|
1238
|
+
};
|
|
1239
|
+
this[kRetryTimeout] = setTimeout(retry, backoff);
|
|
1240
|
+
return;
|
|
1123
1241
|
}
|
|
1124
1242
|
}
|
|
1125
|
-
|
|
1126
|
-
error = new RequestError(error_.message, error_, this);
|
|
1127
|
-
}
|
|
1128
|
-
this.destroy(error);
|
|
1243
|
+
void this._error(typedError);
|
|
1129
1244
|
})();
|
|
1130
1245
|
}
|
|
1131
1246
|
_read() {
|
|
@@ -1212,6 +1327,8 @@ class Request extends stream_1.Duplex {
|
|
|
1212
1327
|
_destroy(error, callback) {
|
|
1213
1328
|
var _a;
|
|
1214
1329
|
this[kStopReading] = true;
|
|
1330
|
+
// Prevent further retries
|
|
1331
|
+
clearTimeout(this[kRetryTimeout]);
|
|
1215
1332
|
if (kRequest in this) {
|
|
1216
1333
|
this[kCancelTimeouts]();
|
|
1217
1334
|
// TODO: Remove the next `if` when these get fixed:
|
|
@@ -1225,10 +1342,19 @@ class Request extends stream_1.Duplex {
|
|
|
1225
1342
|
}
|
|
1226
1343
|
callback(error);
|
|
1227
1344
|
}
|
|
1345
|
+
get _isAboutToError() {
|
|
1346
|
+
return this[kStopReading];
|
|
1347
|
+
}
|
|
1348
|
+
/**
|
|
1349
|
+
The remote IP address.
|
|
1350
|
+
*/
|
|
1228
1351
|
get ip() {
|
|
1229
1352
|
var _a;
|
|
1230
1353
|
return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket.remoteAddress;
|
|
1231
1354
|
}
|
|
1355
|
+
/**
|
|
1356
|
+
Indicates whether the request has been aborted or not.
|
|
1357
|
+
*/
|
|
1232
1358
|
get aborted() {
|
|
1233
1359
|
var _a, _b, _c;
|
|
1234
1360
|
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);
|
|
@@ -1237,6 +1363,9 @@ class Request extends stream_1.Duplex {
|
|
|
1237
1363
|
var _a;
|
|
1238
1364
|
return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.socket;
|
|
1239
1365
|
}
|
|
1366
|
+
/**
|
|
1367
|
+
Progress event for downloading (receiving a response).
|
|
1368
|
+
*/
|
|
1240
1369
|
get downloadProgress() {
|
|
1241
1370
|
let percent;
|
|
1242
1371
|
if (this[kResponseSize]) {
|
|
@@ -1254,6 +1383,9 @@ class Request extends stream_1.Duplex {
|
|
|
1254
1383
|
total: this[kResponseSize]
|
|
1255
1384
|
};
|
|
1256
1385
|
}
|
|
1386
|
+
/**
|
|
1387
|
+
Progress event for uploading (sending a request).
|
|
1388
|
+
*/
|
|
1257
1389
|
get uploadProgress() {
|
|
1258
1390
|
let percent;
|
|
1259
1391
|
if (this[kBodySize]) {
|
|
@@ -1271,16 +1403,43 @@ class Request extends stream_1.Duplex {
|
|
|
1271
1403
|
total: this[kBodySize]
|
|
1272
1404
|
};
|
|
1273
1405
|
}
|
|
1406
|
+
/**
|
|
1407
|
+
The object contains the following properties:
|
|
1408
|
+
|
|
1409
|
+
- `start` - Time when the request started.
|
|
1410
|
+
- `socket` - Time when a socket was assigned to the request.
|
|
1411
|
+
- `lookup` - Time when the DNS lookup finished.
|
|
1412
|
+
- `connect` - Time when the socket successfully connected.
|
|
1413
|
+
- `secureConnect` - Time when the socket securely connected.
|
|
1414
|
+
- `upload` - Time when the request finished uploading.
|
|
1415
|
+
- `response` - Time when the request fired `response` event.
|
|
1416
|
+
- `end` - Time when the response fired `end` event.
|
|
1417
|
+
- `error` - Time when the request fired `error` event.
|
|
1418
|
+
- `abort` - Time when the request fired `abort` event.
|
|
1419
|
+
- `phases`
|
|
1420
|
+
- `wait` - `timings.socket - timings.start`
|
|
1421
|
+
- `dns` - `timings.lookup - timings.socket`
|
|
1422
|
+
- `tcp` - `timings.connect - timings.lookup`
|
|
1423
|
+
- `tls` - `timings.secureConnect - timings.connect`
|
|
1424
|
+
- `request` - `timings.upload - (timings.secureConnect || timings.connect)`
|
|
1425
|
+
- `firstByte` - `timings.response - timings.upload`
|
|
1426
|
+
- `download` - `timings.end - timings.response`
|
|
1427
|
+
- `total` - `(timings.end || timings.error || timings.abort) - timings.start`
|
|
1428
|
+
|
|
1429
|
+
If something has not been measured yet, it will be `undefined`.
|
|
1430
|
+
|
|
1431
|
+
__Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
|
|
1432
|
+
*/
|
|
1274
1433
|
get timings() {
|
|
1275
1434
|
var _a;
|
|
1276
1435
|
return (_a = this[kRequest]) === null || _a === void 0 ? void 0 : _a.timings;
|
|
1277
1436
|
}
|
|
1437
|
+
/**
|
|
1438
|
+
Whether the response was retrieved from the cache.
|
|
1439
|
+
*/
|
|
1278
1440
|
get isFromCache() {
|
|
1279
1441
|
return this[kIsFromCache];
|
|
1280
1442
|
}
|
|
1281
|
-
get _response() {
|
|
1282
|
-
return this[kResponse];
|
|
1283
|
-
}
|
|
1284
1443
|
pipe(destination, options) {
|
|
1285
1444
|
if (this[kStartedReading]) {
|
|
1286
1445
|
throw new Error('Failed to pipe. The response has been emitted already.');
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isResponseOk = void 0;
|
|
4
|
+
exports.isResponseOk = (response) => {
|
|
5
|
+
const { statusCode } = response;
|
|
6
|
+
const limitStatusCode = response.request.options.followRedirect ? 299 : 399;
|
|
7
|
+
return (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304;
|
|
8
|
+
};
|
package/dist/source/create.js
CHANGED
|
@@ -7,11 +7,10 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
9
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
exports.defaultHandler = void 0;
|
|
14
|
-
const p_cancelable_1 = require("p-cancelable");
|
|
15
14
|
const is_1 = require("@sindresorhus/is");
|
|
16
15
|
const as_promise_1 = require("./as-promise");
|
|
17
16
|
const create_rejection_1 = require("./as-promise/create-rejection");
|
|
@@ -25,14 +24,23 @@ const errors = {
|
|
|
25
24
|
MaxRedirectsError: as_promise_1.MaxRedirectsError,
|
|
26
25
|
TimeoutError: as_promise_1.TimeoutError,
|
|
27
26
|
ParseError: as_promise_1.ParseError,
|
|
28
|
-
CancelError:
|
|
27
|
+
CancelError: as_promise_1.CancelError,
|
|
29
28
|
UnsupportedProtocolError: as_promise_1.UnsupportedProtocolError,
|
|
30
29
|
UploadError: as_promise_1.UploadError
|
|
31
30
|
};
|
|
32
31
|
// The `delay` package weighs 10KB (!)
|
|
33
|
-
const delay = async (ms) => new Promise(resolve =>
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const delay = async (ms) => new Promise(resolve => {
|
|
33
|
+
setTimeout(resolve, ms);
|
|
34
|
+
});
|
|
35
|
+
const { normalizeArguments } = core_1.default;
|
|
36
|
+
const mergeOptions = (...sources) => {
|
|
37
|
+
let mergedOptions;
|
|
38
|
+
for (const source of sources) {
|
|
39
|
+
mergedOptions = normalizeArguments(undefined, source, mergedOptions);
|
|
40
|
+
}
|
|
41
|
+
return mergedOptions;
|
|
42
|
+
};
|
|
43
|
+
const getPromiseOrStream = (options) => options.isStream ? new core_1.default(undefined, options) : as_promise_1.default(options);
|
|
36
44
|
const isGotInstance = (value) => ('defaults' in value && 'options' in value.defaults);
|
|
37
45
|
const aliases = [
|
|
38
46
|
'get',
|
|
@@ -218,13 +226,14 @@ const create = (defaults) => {
|
|
|
218
226
|
return got(url, { ...options, method, isStream: true });
|
|
219
227
|
});
|
|
220
228
|
}
|
|
221
|
-
Object.assign(got,
|
|
229
|
+
Object.assign(got, errors);
|
|
222
230
|
Object.defineProperty(got, 'defaults', {
|
|
223
231
|
value: defaults.mutableDefaults ? defaults : deep_freeze_1.default(defaults),
|
|
224
232
|
writable: defaults.mutableDefaults,
|
|
225
233
|
configurable: defaults.mutableDefaults,
|
|
226
234
|
enumerable: true
|
|
227
235
|
});
|
|
236
|
+
got.mergeOptions = mergeOptions;
|
|
228
237
|
return got;
|
|
229
238
|
};
|
|
230
239
|
exports.default = create;
|
package/dist/source/index.js
CHANGED
|
@@ -7,7 +7,7 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
9
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
const url_1 = require("url");
|
|
@@ -116,7 +116,8 @@ const defaults = {
|
|
|
116
116
|
stackAllItems: true
|
|
117
117
|
},
|
|
118
118
|
parseJson: (text) => JSON.parse(text),
|
|
119
|
-
stringifyJson: (object) => JSON.stringify(object)
|
|
119
|
+
stringifyJson: (object) => JSON.stringify(object),
|
|
120
|
+
cacheOptions: {}
|
|
120
121
|
},
|
|
121
122
|
handlers: [create_1.defaultHandler],
|
|
122
123
|
mutableDefaults: false
|