got 12.6.1 → 14.0.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/types.d.ts +1 -1
- package/dist/source/core/errors.js +8 -48
- package/dist/source/core/index.d.ts +4 -4
- package/dist/source/core/index.js +93 -215
- package/dist/source/core/options.d.ts +19 -19
- package/dist/source/core/options.js +16 -34
- package/dist/source/core/parse-link-header.js +1 -1
- package/dist/source/core/response.d.ts +0 -1
- package/dist/source/core/response.js +3 -1
- package/dist/source/core/timed-out.d.ts +1 -0
- package/dist/source/core/timed-out.js +12 -21
- package/dist/source/core/utils/is-client-request.d.ts +1 -0
- package/dist/source/core/utils/is-unix-socket-url.d.ts +0 -1
- package/dist/source/core/utils/options-to-url.d.ts +0 -1
- package/dist/source/core/utils/options-to-url.js +0 -2
- package/dist/source/core/utils/url-to-options.d.ts +1 -1
- package/dist/source/core/utils/weakable-map.js +2 -12
- package/dist/source/index.js +1 -0
- package/dist/source/types.d.ts +1 -2
- package/license +1 -1
- package/package.json +49 -51
- package/readme.md +6 -25
|
@@ -3,7 +3,7 @@ import type { Buffer } from 'node:buffer';
|
|
|
3
3
|
import type PCancelable from 'p-cancelable';
|
|
4
4
|
import { RequestError } from '../core/errors.js';
|
|
5
5
|
import type Request from '../core/index.js';
|
|
6
|
-
import type
|
|
6
|
+
import { type RequestEvents } from '../core/index.js';
|
|
7
7
|
import type { Response } from '../core/response.js';
|
|
8
8
|
/**
|
|
9
9
|
An error to be thrown when the request is aborted with `.cancel()`.
|
|
@@ -8,44 +8,14 @@ An error to be thrown when a request fails.
|
|
|
8
8
|
Contains a `code` property with error class code, like `ECONNREFUSED`.
|
|
9
9
|
*/
|
|
10
10
|
export class RequestError extends Error {
|
|
11
|
+
input;
|
|
12
|
+
code;
|
|
13
|
+
stack;
|
|
14
|
+
response;
|
|
15
|
+
request;
|
|
16
|
+
timings;
|
|
11
17
|
constructor(message, error, self) {
|
|
12
18
|
super(message);
|
|
13
|
-
Object.defineProperty(this, "input", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
configurable: true,
|
|
16
|
-
writable: true,
|
|
17
|
-
value: void 0
|
|
18
|
-
});
|
|
19
|
-
Object.defineProperty(this, "code", {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
configurable: true,
|
|
22
|
-
writable: true,
|
|
23
|
-
value: void 0
|
|
24
|
-
});
|
|
25
|
-
Object.defineProperty(this, "stack", {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
configurable: true,
|
|
28
|
-
writable: true,
|
|
29
|
-
value: void 0
|
|
30
|
-
});
|
|
31
|
-
Object.defineProperty(this, "response", {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
configurable: true,
|
|
34
|
-
writable: true,
|
|
35
|
-
value: void 0
|
|
36
|
-
});
|
|
37
|
-
Object.defineProperty(this, "request", {
|
|
38
|
-
enumerable: true,
|
|
39
|
-
configurable: true,
|
|
40
|
-
writable: true,
|
|
41
|
-
value: void 0
|
|
42
|
-
});
|
|
43
|
-
Object.defineProperty(this, "timings", {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
configurable: true,
|
|
46
|
-
writable: true,
|
|
47
|
-
value: void 0
|
|
48
|
-
});
|
|
49
19
|
Error.captureStackTrace(this, this.constructor);
|
|
50
20
|
this.name = 'RequestError';
|
|
51
21
|
this.code = error.code ?? 'ERR_GOT_REQUEST_ERROR';
|
|
@@ -127,20 +97,10 @@ An error to be thrown when the request is aborted due to a timeout.
|
|
|
127
97
|
Includes an `event` and `timings` property.
|
|
128
98
|
*/
|
|
129
99
|
export class TimeoutError extends RequestError {
|
|
100
|
+
timings;
|
|
101
|
+
event;
|
|
130
102
|
constructor(error, timings, request) {
|
|
131
103
|
super(error.message, error, request);
|
|
132
|
-
Object.defineProperty(this, "timings", {
|
|
133
|
-
enumerable: true,
|
|
134
|
-
configurable: true,
|
|
135
|
-
writable: true,
|
|
136
|
-
value: void 0
|
|
137
|
-
});
|
|
138
|
-
Object.defineProperty(this, "event", {
|
|
139
|
-
enumerable: true,
|
|
140
|
-
configurable: true,
|
|
141
|
-
writable: true,
|
|
142
|
-
value: void 0
|
|
143
|
-
});
|
|
144
104
|
this.name = 'TimeoutError';
|
|
145
105
|
this.event = error.event;
|
|
146
106
|
this.timings = timings;
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
/// <reference types="node" resolution-mode="require"/>
|
|
4
4
|
/// <reference types="node" resolution-mode="require"/>
|
|
5
5
|
/// <reference types="node" resolution-mode="require"/>
|
|
6
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
6
7
|
import { Duplex } from 'node:stream';
|
|
7
|
-
import {
|
|
8
|
-
import type { ClientRequest } from 'node:http';
|
|
8
|
+
import { type ClientRequest } from 'node:http';
|
|
9
9
|
import type { Socket } from 'node:net';
|
|
10
|
-
import type
|
|
10
|
+
import { type Timings } from '@szmarczak/http-timer';
|
|
11
11
|
import Options from './options.js';
|
|
12
12
|
import { type PlainResponse, type Response } from './response.js';
|
|
13
13
|
import { RequestError } from './errors.js';
|
|
@@ -107,7 +107,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
|
|
|
107
107
|
private _isFromCache?;
|
|
108
108
|
private _cannotHaveBody;
|
|
109
109
|
private _triggerRead;
|
|
110
|
-
private _jobs;
|
|
110
|
+
private readonly _jobs;
|
|
111
111
|
private _cancelTimeouts;
|
|
112
112
|
private readonly _removeListeners;
|
|
113
113
|
private _nativeResponse?;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import process from 'node:process';
|
|
2
2
|
import { Buffer } from 'node:buffer';
|
|
3
3
|
import { Duplex } from 'node:stream';
|
|
4
|
-
import { URL, URLSearchParams } from 'node:url';
|
|
5
4
|
import http, { ServerResponse } from 'node:http';
|
|
6
5
|
import timer from '@szmarczak/http-timer';
|
|
7
6
|
import CacheableRequest, { CacheError as CacheableCacheError, } from 'cacheable-request';
|
|
8
7
|
import decompressResponse from 'decompress-response';
|
|
9
8
|
import is from '@sindresorhus/is';
|
|
10
|
-
import
|
|
9
|
+
import { getStreamAsBuffer } from 'get-stream';
|
|
11
10
|
import { FormDataEncoder, isFormData as isFormDataLike } from 'form-data-encoder';
|
|
12
11
|
import getBodySize from './utils/get-body-size.js';
|
|
13
12
|
import isFormData from './utils/is-form-data.js';
|
|
@@ -21,7 +20,6 @@ import { isResponseOk } from './response.js';
|
|
|
21
20
|
import isClientRequest from './utils/is-client-request.js';
|
|
22
21
|
import isUnixSocketURL from './utils/is-unix-socket-url.js';
|
|
23
22
|
import { RequestError, ReadError, MaxRedirectsError, HTTPError, TimeoutError, UploadError, CacheError, AbortError, } from './errors.js';
|
|
24
|
-
const { buffer: getBuffer } = getStream;
|
|
25
23
|
const supportsBrotli = is.string(process.versions.brotli);
|
|
26
24
|
const methodsWithoutBody = new Set(['GET', 'HEAD']);
|
|
27
25
|
const cacheableStore = new WeakableMap();
|
|
@@ -35,6 +33,34 @@ const proxiedRequestEvents = [
|
|
|
35
33
|
];
|
|
36
34
|
const noop = () => { };
|
|
37
35
|
export default class Request extends Duplex {
|
|
36
|
+
// @ts-expect-error - Ignoring for now.
|
|
37
|
+
['constructor'];
|
|
38
|
+
_noPipe;
|
|
39
|
+
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/9568
|
|
40
|
+
options;
|
|
41
|
+
response;
|
|
42
|
+
requestUrl;
|
|
43
|
+
redirectUrls;
|
|
44
|
+
retryCount;
|
|
45
|
+
_stopRetry;
|
|
46
|
+
_downloadedSize;
|
|
47
|
+
_uploadedSize;
|
|
48
|
+
_stopReading;
|
|
49
|
+
_pipedServerResponses;
|
|
50
|
+
_request;
|
|
51
|
+
_responseSize;
|
|
52
|
+
_bodySize;
|
|
53
|
+
_unproxyEvents;
|
|
54
|
+
_isFromCache;
|
|
55
|
+
_cannotHaveBody;
|
|
56
|
+
_triggerRead;
|
|
57
|
+
_cancelTimeouts;
|
|
58
|
+
_removeListeners;
|
|
59
|
+
_nativeResponse;
|
|
60
|
+
_flushed;
|
|
61
|
+
_aborted;
|
|
62
|
+
// We need this because `this._request` if `undefined` when using cache
|
|
63
|
+
_requestInitialized;
|
|
38
64
|
constructor(url, options, defaults) {
|
|
39
65
|
super({
|
|
40
66
|
// Don't destroy immediately, as the error may be emitted on unsuccessful retry
|
|
@@ -42,159 +68,6 @@ export default class Request extends Duplex {
|
|
|
42
68
|
// It needs to be zero because we're just proxying the data to another stream
|
|
43
69
|
highWaterMark: 0,
|
|
44
70
|
});
|
|
45
|
-
// @ts-expect-error - Ignoring for now.
|
|
46
|
-
Object.defineProperty(this, 'constructor', {
|
|
47
|
-
enumerable: true,
|
|
48
|
-
configurable: true,
|
|
49
|
-
writable: true,
|
|
50
|
-
value: void 0
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(this, "_noPipe", {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
configurable: true,
|
|
55
|
-
writable: true,
|
|
56
|
-
value: void 0
|
|
57
|
-
});
|
|
58
|
-
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/9568
|
|
59
|
-
Object.defineProperty(this, "options", {
|
|
60
|
-
enumerable: true,
|
|
61
|
-
configurable: true,
|
|
62
|
-
writable: true,
|
|
63
|
-
value: void 0
|
|
64
|
-
});
|
|
65
|
-
Object.defineProperty(this, "response", {
|
|
66
|
-
enumerable: true,
|
|
67
|
-
configurable: true,
|
|
68
|
-
writable: true,
|
|
69
|
-
value: void 0
|
|
70
|
-
});
|
|
71
|
-
Object.defineProperty(this, "requestUrl", {
|
|
72
|
-
enumerable: true,
|
|
73
|
-
configurable: true,
|
|
74
|
-
writable: true,
|
|
75
|
-
value: void 0
|
|
76
|
-
});
|
|
77
|
-
Object.defineProperty(this, "redirectUrls", {
|
|
78
|
-
enumerable: true,
|
|
79
|
-
configurable: true,
|
|
80
|
-
writable: true,
|
|
81
|
-
value: void 0
|
|
82
|
-
});
|
|
83
|
-
Object.defineProperty(this, "retryCount", {
|
|
84
|
-
enumerable: true,
|
|
85
|
-
configurable: true,
|
|
86
|
-
writable: true,
|
|
87
|
-
value: void 0
|
|
88
|
-
});
|
|
89
|
-
Object.defineProperty(this, "_stopRetry", {
|
|
90
|
-
enumerable: true,
|
|
91
|
-
configurable: true,
|
|
92
|
-
writable: true,
|
|
93
|
-
value: void 0
|
|
94
|
-
});
|
|
95
|
-
Object.defineProperty(this, "_downloadedSize", {
|
|
96
|
-
enumerable: true,
|
|
97
|
-
configurable: true,
|
|
98
|
-
writable: true,
|
|
99
|
-
value: void 0
|
|
100
|
-
});
|
|
101
|
-
Object.defineProperty(this, "_uploadedSize", {
|
|
102
|
-
enumerable: true,
|
|
103
|
-
configurable: true,
|
|
104
|
-
writable: true,
|
|
105
|
-
value: void 0
|
|
106
|
-
});
|
|
107
|
-
Object.defineProperty(this, "_stopReading", {
|
|
108
|
-
enumerable: true,
|
|
109
|
-
configurable: true,
|
|
110
|
-
writable: true,
|
|
111
|
-
value: void 0
|
|
112
|
-
});
|
|
113
|
-
Object.defineProperty(this, "_pipedServerResponses", {
|
|
114
|
-
enumerable: true,
|
|
115
|
-
configurable: true,
|
|
116
|
-
writable: true,
|
|
117
|
-
value: void 0
|
|
118
|
-
});
|
|
119
|
-
Object.defineProperty(this, "_request", {
|
|
120
|
-
enumerable: true,
|
|
121
|
-
configurable: true,
|
|
122
|
-
writable: true,
|
|
123
|
-
value: void 0
|
|
124
|
-
});
|
|
125
|
-
Object.defineProperty(this, "_responseSize", {
|
|
126
|
-
enumerable: true,
|
|
127
|
-
configurable: true,
|
|
128
|
-
writable: true,
|
|
129
|
-
value: void 0
|
|
130
|
-
});
|
|
131
|
-
Object.defineProperty(this, "_bodySize", {
|
|
132
|
-
enumerable: true,
|
|
133
|
-
configurable: true,
|
|
134
|
-
writable: true,
|
|
135
|
-
value: void 0
|
|
136
|
-
});
|
|
137
|
-
Object.defineProperty(this, "_unproxyEvents", {
|
|
138
|
-
enumerable: true,
|
|
139
|
-
configurable: true,
|
|
140
|
-
writable: true,
|
|
141
|
-
value: void 0
|
|
142
|
-
});
|
|
143
|
-
Object.defineProperty(this, "_isFromCache", {
|
|
144
|
-
enumerable: true,
|
|
145
|
-
configurable: true,
|
|
146
|
-
writable: true,
|
|
147
|
-
value: void 0
|
|
148
|
-
});
|
|
149
|
-
Object.defineProperty(this, "_cannotHaveBody", {
|
|
150
|
-
enumerable: true,
|
|
151
|
-
configurable: true,
|
|
152
|
-
writable: true,
|
|
153
|
-
value: void 0
|
|
154
|
-
});
|
|
155
|
-
Object.defineProperty(this, "_triggerRead", {
|
|
156
|
-
enumerable: true,
|
|
157
|
-
configurable: true,
|
|
158
|
-
writable: true,
|
|
159
|
-
value: void 0
|
|
160
|
-
});
|
|
161
|
-
Object.defineProperty(this, "_cancelTimeouts", {
|
|
162
|
-
enumerable: true,
|
|
163
|
-
configurable: true,
|
|
164
|
-
writable: true,
|
|
165
|
-
value: void 0
|
|
166
|
-
});
|
|
167
|
-
Object.defineProperty(this, "_removeListeners", {
|
|
168
|
-
enumerable: true,
|
|
169
|
-
configurable: true,
|
|
170
|
-
writable: true,
|
|
171
|
-
value: void 0
|
|
172
|
-
});
|
|
173
|
-
Object.defineProperty(this, "_nativeResponse", {
|
|
174
|
-
enumerable: true,
|
|
175
|
-
configurable: true,
|
|
176
|
-
writable: true,
|
|
177
|
-
value: void 0
|
|
178
|
-
});
|
|
179
|
-
Object.defineProperty(this, "_flushed", {
|
|
180
|
-
enumerable: true,
|
|
181
|
-
configurable: true,
|
|
182
|
-
writable: true,
|
|
183
|
-
value: void 0
|
|
184
|
-
});
|
|
185
|
-
Object.defineProperty(this, "_aborted", {
|
|
186
|
-
enumerable: true,
|
|
187
|
-
configurable: true,
|
|
188
|
-
writable: true,
|
|
189
|
-
value: void 0
|
|
190
|
-
});
|
|
191
|
-
// We need this because `this._request` if `undefined` when using cache
|
|
192
|
-
Object.defineProperty(this, "_requestInitialized", {
|
|
193
|
-
enumerable: true,
|
|
194
|
-
configurable: true,
|
|
195
|
-
writable: true,
|
|
196
|
-
value: void 0
|
|
197
|
-
});
|
|
198
71
|
this._downloadedSize = 0;
|
|
199
72
|
this._uploadedSize = 0;
|
|
200
73
|
this._stopReading = false;
|
|
@@ -268,7 +141,7 @@ export default class Request extends Duplex {
|
|
|
268
141
|
else {
|
|
269
142
|
this.options.signal.addEventListener('abort', abort);
|
|
270
143
|
this._removeListeners = () => {
|
|
271
|
-
this.options.signal
|
|
144
|
+
this.options.signal?.removeEventListener('abort', abort);
|
|
272
145
|
};
|
|
273
146
|
}
|
|
274
147
|
}
|
|
@@ -314,7 +187,7 @@ export default class Request extends Duplex {
|
|
|
314
187
|
void (async () => {
|
|
315
188
|
// Node.js parser is really weird.
|
|
316
189
|
// It emits post-request Parse Errors on the same instance as previous request. WTF.
|
|
317
|
-
// Therefore we need to check if it has been destroyed as well.
|
|
190
|
+
// Therefore, we need to check if it has been destroyed as well.
|
|
318
191
|
//
|
|
319
192
|
// Furthermore, Node.js 16 `response.destroy()` doesn't immediately destroy the socket,
|
|
320
193
|
// but makes the response unreadable. So we additionally need to check `response.readable`.
|
|
@@ -631,76 +504,79 @@ export default class Request extends Duplex {
|
|
|
631
504
|
if (this.isAborted) {
|
|
632
505
|
return;
|
|
633
506
|
}
|
|
634
|
-
if (
|
|
507
|
+
if (response.headers.location && redirectCodes.has(statusCode)) {
|
|
635
508
|
// We're being redirected, we don't care about the response.
|
|
636
509
|
// It'd be best to abort the request, but we can't because
|
|
637
510
|
// we would have to sacrifice the TCP connection. We don't want that.
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
this.
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
this._request = undefined;
|
|
646
|
-
const updatedOptions = new Options(undefined, undefined, this.options);
|
|
647
|
-
const serverRequestedGet = statusCode === 303 && updatedOptions.method !== 'GET' && updatedOptions.method !== 'HEAD';
|
|
648
|
-
const canRewrite = statusCode !== 307 && statusCode !== 308;
|
|
649
|
-
const userRequestedGet = updatedOptions.methodRewriting && canRewrite;
|
|
650
|
-
if (serverRequestedGet || userRequestedGet) {
|
|
651
|
-
updatedOptions.method = 'GET';
|
|
652
|
-
updatedOptions.body = undefined;
|
|
653
|
-
updatedOptions.json = undefined;
|
|
654
|
-
updatedOptions.form = undefined;
|
|
655
|
-
delete updatedOptions.headers['content-length'];
|
|
656
|
-
}
|
|
657
|
-
try {
|
|
658
|
-
// We need this in order to support UTF-8
|
|
659
|
-
const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();
|
|
660
|
-
const redirectUrl = new URL(redirectBuffer, url);
|
|
661
|
-
if (!isUnixSocketURL(url) && isUnixSocketURL(redirectUrl)) {
|
|
662
|
-
this._beforeError(new RequestError('Cannot redirect to UNIX socket', {}, this));
|
|
511
|
+
const shouldFollow = typeof options.followRedirect === 'function' ? options.followRedirect(typedResponse) : options.followRedirect;
|
|
512
|
+
if (shouldFollow) {
|
|
513
|
+
response.resume();
|
|
514
|
+
this._cancelTimeouts();
|
|
515
|
+
this._unproxyEvents();
|
|
516
|
+
if (this.redirectUrls.length >= options.maxRedirects) {
|
|
517
|
+
this._beforeError(new MaxRedirectsError(this));
|
|
663
518
|
return;
|
|
664
519
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
520
|
+
this._request = undefined;
|
|
521
|
+
const updatedOptions = new Options(undefined, undefined, this.options);
|
|
522
|
+
const serverRequestedGet = statusCode === 303 && updatedOptions.method !== 'GET' && updatedOptions.method !== 'HEAD';
|
|
523
|
+
const canRewrite = statusCode !== 307 && statusCode !== 308;
|
|
524
|
+
const userRequestedGet = updatedOptions.methodRewriting && canRewrite;
|
|
525
|
+
if (serverRequestedGet || userRequestedGet) {
|
|
526
|
+
updatedOptions.method = 'GET';
|
|
527
|
+
updatedOptions.body = undefined;
|
|
528
|
+
updatedOptions.json = undefined;
|
|
529
|
+
updatedOptions.form = undefined;
|
|
530
|
+
delete updatedOptions.headers['content-length'];
|
|
531
|
+
}
|
|
532
|
+
try {
|
|
533
|
+
// We need this in order to support UTF-8
|
|
534
|
+
const redirectBuffer = Buffer.from(response.headers.location, 'binary').toString();
|
|
535
|
+
const redirectUrl = new URL(redirectBuffer, url);
|
|
536
|
+
if (!isUnixSocketURL(url) && isUnixSocketURL(redirectUrl)) {
|
|
537
|
+
this._beforeError(new RequestError('Cannot redirect to UNIX socket', {}, this));
|
|
538
|
+
return;
|
|
669
539
|
}
|
|
670
|
-
|
|
671
|
-
|
|
540
|
+
// Redirecting to a different site, clear sensitive data.
|
|
541
|
+
if (redirectUrl.hostname !== url.hostname || redirectUrl.port !== url.port) {
|
|
542
|
+
if ('host' in updatedOptions.headers) {
|
|
543
|
+
delete updatedOptions.headers.host;
|
|
544
|
+
}
|
|
545
|
+
if ('cookie' in updatedOptions.headers) {
|
|
546
|
+
delete updatedOptions.headers.cookie;
|
|
547
|
+
}
|
|
548
|
+
if ('authorization' in updatedOptions.headers) {
|
|
549
|
+
delete updatedOptions.headers.authorization;
|
|
550
|
+
}
|
|
551
|
+
if (updatedOptions.username || updatedOptions.password) {
|
|
552
|
+
updatedOptions.username = '';
|
|
553
|
+
updatedOptions.password = '';
|
|
554
|
+
}
|
|
672
555
|
}
|
|
673
|
-
|
|
674
|
-
|
|
556
|
+
else {
|
|
557
|
+
redirectUrl.username = updatedOptions.username;
|
|
558
|
+
redirectUrl.password = updatedOptions.password;
|
|
675
559
|
}
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
560
|
+
this.redirectUrls.push(redirectUrl);
|
|
561
|
+
updatedOptions.prefixUrl = '';
|
|
562
|
+
updatedOptions.url = redirectUrl;
|
|
563
|
+
for (const hook of updatedOptions.hooks.beforeRedirect) {
|
|
564
|
+
// eslint-disable-next-line no-await-in-loop
|
|
565
|
+
await hook(updatedOptions, typedResponse);
|
|
679
566
|
}
|
|
567
|
+
this.emit('redirect', updatedOptions, typedResponse);
|
|
568
|
+
this.options = updatedOptions;
|
|
569
|
+
await this._makeRequest();
|
|
680
570
|
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
}
|
|
685
|
-
this.redirectUrls.push(redirectUrl);
|
|
686
|
-
updatedOptions.prefixUrl = '';
|
|
687
|
-
updatedOptions.url = redirectUrl;
|
|
688
|
-
for (const hook of updatedOptions.hooks.beforeRedirect) {
|
|
689
|
-
// eslint-disable-next-line no-await-in-loop
|
|
690
|
-
await hook(updatedOptions, typedResponse);
|
|
571
|
+
catch (error) {
|
|
572
|
+
this._beforeError(error);
|
|
573
|
+
return;
|
|
691
574
|
}
|
|
692
|
-
this.emit('redirect', updatedOptions, typedResponse);
|
|
693
|
-
this.options = updatedOptions;
|
|
694
|
-
await this._makeRequest();
|
|
695
|
-
}
|
|
696
|
-
catch (error) {
|
|
697
|
-
this._beforeError(error);
|
|
698
575
|
return;
|
|
699
576
|
}
|
|
700
|
-
return;
|
|
701
577
|
}
|
|
702
578
|
// `HTTPError`s always have `error.response.body` defined.
|
|
703
|
-
// Therefore we cannot retry if `options.throwHttpErrors` is false.
|
|
579
|
+
// Therefore, we cannot retry if `options.throwHttpErrors` is false.
|
|
704
580
|
// On the last retry, if `options.throwHttpErrors` is false, we would need to return the body,
|
|
705
581
|
// but that wouldn't be possible since the body would be already read in `error.response.body`.
|
|
706
582
|
if (options.isStream && options.throwHttpErrors && !isResponseOk(typedResponse)) {
|
|
@@ -750,7 +626,10 @@ export default class Request extends Duplex {
|
|
|
750
626
|
}
|
|
751
627
|
try {
|
|
752
628
|
// Errors are emitted via the `error` event
|
|
753
|
-
const rawBody = await
|
|
629
|
+
const rawBody = await getStreamAsBuffer(from);
|
|
630
|
+
// TODO: Switch to this:
|
|
631
|
+
// let rawBody = await from.toArray();
|
|
632
|
+
// rawBody = Buffer.concat(rawBody);
|
|
754
633
|
// On retry Request is destroyed with no error, therefore the above will successfully resolve.
|
|
755
634
|
// So in order to check if this was really successfull, we need to check if it has been properly ended.
|
|
756
635
|
if (!this.isAborted) {
|
|
@@ -844,7 +723,6 @@ export default class Request extends Duplex {
|
|
|
844
723
|
// We only need to implement the error handler in order to support HTTP2 caching.
|
|
845
724
|
// The result will be a promise anyway.
|
|
846
725
|
// @ts-expect-error ignore
|
|
847
|
-
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
848
726
|
result.once = (event, handler) => {
|
|
849
727
|
if (event === 'error') {
|
|
850
728
|
(async () => {
|
|
@@ -8,18 +8,14 @@
|
|
|
8
8
|
/// <reference types="node" resolution-mode="require"/>
|
|
9
9
|
/// <reference types="node" resolution-mode="require"/>
|
|
10
10
|
import type { Buffer } from 'node:buffer';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import http from 'node:http';
|
|
14
|
-
import https from 'node:https';
|
|
11
|
+
import { checkServerIdentity, type SecureContextOptions, type DetailedPeerCertificate } from 'node:tls';
|
|
12
|
+
import https, { type RequestOptions as HttpsRequestOptions, type Agent as HttpsAgent } from 'node:https';
|
|
13
|
+
import http, { type Agent as HttpAgent, type ClientRequest } from 'node:http';
|
|
15
14
|
import type { Readable } from 'node:stream';
|
|
16
15
|
import type { Socket } from 'node:net';
|
|
17
|
-
import type { SecureContextOptions, DetailedPeerCertificate } from 'node:tls';
|
|
18
|
-
import type { Agent as HttpAgent, ClientRequest } from 'node:http';
|
|
19
|
-
import type { RequestOptions as HttpsRequestOptions, Agent as HttpsAgent } from 'node:https';
|
|
20
16
|
import CacheableLookup from 'cacheable-lookup';
|
|
21
17
|
import http2wrapper, { type ClientHttp2Session } from 'http2-wrapper';
|
|
22
|
-
import type
|
|
18
|
+
import { type FormDataLike } from 'form-data-encoder';
|
|
23
19
|
import type { StorageAdapter } from 'cacheable-request';
|
|
24
20
|
import type ResponseLike from 'responselike';
|
|
25
21
|
import type { IncomingMessageWithTimings } from '@szmarczak/http-timer';
|
|
@@ -567,7 +563,7 @@ export type OptionsInit = Except<Partial<InternalsType>, 'hooks' | 'retry'> & {
|
|
|
567
563
|
};
|
|
568
564
|
export default class Options {
|
|
569
565
|
private _unixOptions?;
|
|
570
|
-
private _internals;
|
|
566
|
+
private readonly _internals;
|
|
571
567
|
private _merging;
|
|
572
568
|
private readonly _init;
|
|
573
569
|
constructor(input?: string | URL | OptionsInit, options?: OptionsInit, defaults?: Options);
|
|
@@ -741,8 +737,6 @@ export default class Options {
|
|
|
741
737
|
/**
|
|
742
738
|
You can abort the `request` using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
|
|
743
739
|
|
|
744
|
-
*Requires Node.js 16 or later.*
|
|
745
|
-
|
|
746
740
|
@example
|
|
747
741
|
```
|
|
748
742
|
import got from 'got';
|
|
@@ -758,8 +752,8 @@ export default class Options {
|
|
|
758
752
|
}, 100);
|
|
759
753
|
```
|
|
760
754
|
*/
|
|
761
|
-
get signal():
|
|
762
|
-
set signal(value:
|
|
755
|
+
get signal(): AbortSignal | undefined;
|
|
756
|
+
set signal(value: AbortSignal | undefined);
|
|
763
757
|
/**
|
|
764
758
|
Ignore invalid cookies instead of throwing an error.
|
|
765
759
|
Only useful when the `cookieJar` option has been set. Not recommended.
|
|
@@ -844,15 +838,17 @@ export default class Options {
|
|
|
844
838
|
get hooks(): Hooks;
|
|
845
839
|
set hooks(value: Hooks);
|
|
846
840
|
/**
|
|
847
|
-
|
|
841
|
+
Whether redirect responses should be followed automatically.
|
|
842
|
+
|
|
843
|
+
Optionally, pass a function to dynamically decide based on the response object.
|
|
848
844
|
|
|
849
845
|
Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`.
|
|
850
846
|
This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4). You can optionally turn on this behavior also for other redirect codes - see `methodRewriting`.
|
|
851
847
|
|
|
852
848
|
@default true
|
|
853
849
|
*/
|
|
854
|
-
get followRedirect(): boolean;
|
|
855
|
-
set followRedirect(value: boolean);
|
|
850
|
+
get followRedirect(): boolean | ((response: PlainResponse) => boolean);
|
|
851
|
+
set followRedirect(value: boolean | ((response: PlainResponse) => boolean));
|
|
856
852
|
get followRedirects(): unknown;
|
|
857
853
|
set followRedirects(_value: unknown);
|
|
858
854
|
/**
|
|
@@ -1138,6 +1134,7 @@ export default class Options {
|
|
|
1138
1134
|
username: string;
|
|
1139
1135
|
password: string;
|
|
1140
1136
|
json: unknown;
|
|
1137
|
+
followRedirect: boolean | ((response: PlainResponse) => boolean);
|
|
1141
1138
|
retry: Partial<RetryOptions>;
|
|
1142
1139
|
agent: Agents;
|
|
1143
1140
|
h2session: http2wrapper.ClientHttp2Session | undefined;
|
|
@@ -1147,7 +1144,7 @@ export default class Options {
|
|
|
1147
1144
|
form: Record<string, any> | undefined;
|
|
1148
1145
|
url: string | URL | undefined;
|
|
1149
1146
|
cookieJar: PromiseCookieJar | ToughCookieJar | undefined;
|
|
1150
|
-
signal:
|
|
1147
|
+
signal: AbortSignal | undefined;
|
|
1151
1148
|
ignoreInvalidCookies: boolean;
|
|
1152
1149
|
searchParams: string | SearchParameters | URLSearchParams | undefined;
|
|
1153
1150
|
dnsLookup: {
|
|
@@ -1161,7 +1158,6 @@ export default class Options {
|
|
|
1161
1158
|
dnsCache: boolean | CacheableLookup | undefined;
|
|
1162
1159
|
context: Record<string, unknown>;
|
|
1163
1160
|
hooks: Hooks;
|
|
1164
|
-
followRedirect: boolean;
|
|
1165
1161
|
maxRedirects: number;
|
|
1166
1162
|
cache: string | boolean | StorageAdapter | undefined;
|
|
1167
1163
|
throwHttpErrors: boolean;
|
|
@@ -1212,7 +1208,7 @@ export default class Options {
|
|
|
1212
1208
|
(hostname: string, options: import("cacheable-lookup").LookupOptions, callback: (error: NodeJS.ErrnoException | null, address: string, family: import("cacheable-lookup").IPFamily) => void): void;
|
|
1213
1209
|
} | undefined;
|
|
1214
1210
|
family: DnsLookupIpVersion;
|
|
1215
|
-
agent: false |
|
|
1211
|
+
agent: false | http.Agent | Agents | undefined;
|
|
1216
1212
|
setHost: boolean;
|
|
1217
1213
|
method: Method;
|
|
1218
1214
|
maxHeaderSize: number | undefined;
|
|
@@ -1236,6 +1232,10 @@ export default class Options {
|
|
|
1236
1232
|
socketPath?: string | undefined;
|
|
1237
1233
|
uniqueHeaders?: (string | string[])[] | undefined;
|
|
1238
1234
|
joinDuplicateHeaders?: boolean | undefined;
|
|
1235
|
+
ALPNCallback?: ((arg: {
|
|
1236
|
+
servername: string;
|
|
1237
|
+
protocols: string[];
|
|
1238
|
+
}) => string | undefined) | undefined;
|
|
1239
1239
|
clientCertEngine?: string | undefined;
|
|
1240
1240
|
privateKeyEngine?: string | undefined;
|
|
1241
1241
|
privateKeyIdentifier?: string | undefined;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import process from 'node:process';
|
|
2
2
|
import { promisify, inspect } from 'node:util';
|
|
3
|
-
import { URL, URLSearchParams } from 'node:url';
|
|
4
3
|
import { checkServerIdentity } from 'node:tls';
|
|
5
4
|
// DO NOT use destructuring for `https.request` and `http.request` as it's not compatible with `nock`.
|
|
6
|
-
import http from 'node:http';
|
|
7
5
|
import https from 'node:https';
|
|
6
|
+
import http from 'node:http';
|
|
8
7
|
import is, { assert } from '@sindresorhus/is';
|
|
9
8
|
import lowercaseKeys from 'lowercase-keys';
|
|
10
9
|
import CacheableLookup from 'cacheable-lookup';
|
|
@@ -181,7 +180,7 @@ const defaultInternals = {
|
|
|
181
180
|
setHost: true,
|
|
182
181
|
maxHeaderSize: undefined,
|
|
183
182
|
signal: undefined,
|
|
184
|
-
enableUnixSockets:
|
|
183
|
+
enableUnixSockets: false,
|
|
185
184
|
};
|
|
186
185
|
const cloneInternals = (internals) => {
|
|
187
186
|
const { hooks, retry } = internals;
|
|
@@ -296,31 +295,11 @@ const init = (options, withOptions, self) => {
|
|
|
296
295
|
}
|
|
297
296
|
};
|
|
298
297
|
export default class Options {
|
|
298
|
+
_unixOptions;
|
|
299
|
+
_internals;
|
|
300
|
+
_merging;
|
|
301
|
+
_init;
|
|
299
302
|
constructor(input, options, defaults) {
|
|
300
|
-
Object.defineProperty(this, "_unixOptions", {
|
|
301
|
-
enumerable: true,
|
|
302
|
-
configurable: true,
|
|
303
|
-
writable: true,
|
|
304
|
-
value: void 0
|
|
305
|
-
});
|
|
306
|
-
Object.defineProperty(this, "_internals", {
|
|
307
|
-
enumerable: true,
|
|
308
|
-
configurable: true,
|
|
309
|
-
writable: true,
|
|
310
|
-
value: void 0
|
|
311
|
-
});
|
|
312
|
-
Object.defineProperty(this, "_merging", {
|
|
313
|
-
enumerable: true,
|
|
314
|
-
configurable: true,
|
|
315
|
-
writable: true,
|
|
316
|
-
value: void 0
|
|
317
|
-
});
|
|
318
|
-
Object.defineProperty(this, "_init", {
|
|
319
|
-
enumerable: true,
|
|
320
|
-
configurable: true,
|
|
321
|
-
writable: true,
|
|
322
|
-
value: void 0
|
|
323
|
-
});
|
|
324
303
|
assert.any([is.string, is.urlInstance, is.object, is.undefined], input);
|
|
325
304
|
assert.any([is.object, is.undefined], options);
|
|
326
305
|
assert.any([is.object, is.undefined], defaults);
|
|
@@ -408,7 +387,12 @@ export default class Options {
|
|
|
408
387
|
throw new Error(`Unexpected option: ${key}`);
|
|
409
388
|
}
|
|
410
389
|
// @ts-expect-error Type 'unknown' is not assignable to type 'never'.
|
|
411
|
-
|
|
390
|
+
const value = options[key];
|
|
391
|
+
if (value === undefined) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
// @ts-expect-error Type 'unknown' is not assignable to type 'never'.
|
|
395
|
+
this[key] = value;
|
|
412
396
|
push = true;
|
|
413
397
|
}
|
|
414
398
|
if (push) {
|
|
@@ -764,8 +748,6 @@ export default class Options {
|
|
|
764
748
|
/**
|
|
765
749
|
You can abort the `request` using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
|
|
766
750
|
|
|
767
|
-
*Requires Node.js 16 or later.*
|
|
768
|
-
|
|
769
751
|
@example
|
|
770
752
|
```
|
|
771
753
|
import got from 'got';
|
|
@@ -781,11 +763,9 @@ export default class Options {
|
|
|
781
763
|
}, 100);
|
|
782
764
|
```
|
|
783
765
|
*/
|
|
784
|
-
// TODO: Replace `any` with `AbortSignal` when targeting Node 16.
|
|
785
766
|
get signal() {
|
|
786
767
|
return this._internals.signal;
|
|
787
768
|
}
|
|
788
|
-
// TODO: Replace `any` with `AbortSignal` when targeting Node 16.
|
|
789
769
|
set signal(value) {
|
|
790
770
|
assert.object(value);
|
|
791
771
|
this._internals.signal = value;
|
|
@@ -1000,7 +980,9 @@ export default class Options {
|
|
|
1000
980
|
}
|
|
1001
981
|
}
|
|
1002
982
|
/**
|
|
1003
|
-
|
|
983
|
+
Whether redirect responses should be followed automatically.
|
|
984
|
+
|
|
985
|
+
Optionally, pass a function to dynamically decide based on the response object.
|
|
1004
986
|
|
|
1005
987
|
Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`.
|
|
1006
988
|
This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4). You can optionally turn on this behavior also for other redirect codes - see `methodRewriting`.
|
|
@@ -1011,7 +993,7 @@ export default class Options {
|
|
|
1011
993
|
return this._internals.followRedirect;
|
|
1012
994
|
}
|
|
1013
995
|
set followRedirect(value) {
|
|
1014
|
-
assert.boolean
|
|
996
|
+
assert.any([is.boolean, is.function_], value);
|
|
1015
997
|
this._internals.followRedirect = value;
|
|
1016
998
|
}
|
|
1017
999
|
get followRedirects() {
|
|
@@ -6,7 +6,7 @@ export default function parseLinkHeader(link) {
|
|
|
6
6
|
const [rawUriReference, ...rawLinkParameters] = item.split(';');
|
|
7
7
|
const trimmedUriReference = rawUriReference.trim();
|
|
8
8
|
// eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
|
|
9
|
-
if (trimmedUriReference[0] !== '<' || trimmedUriReference
|
|
9
|
+
if (trimmedUriReference[0] !== '<' || trimmedUriReference.at(-1) !== '>') {
|
|
10
10
|
throw new Error(`Invalid format of the Link header reference: ${trimmedUriReference}`);
|
|
11
11
|
}
|
|
12
12
|
const reference = trimmedUriReference.slice(1, -1);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
3
|
import type { Buffer } from 'node:buffer';
|
|
4
|
-
import type { URL } from 'node:url';
|
|
5
4
|
import type { IncomingMessageWithTimings, Timings } from '@szmarczak/http-timer';
|
|
6
5
|
import { RequestError } from './errors.js';
|
|
7
6
|
import type { ParseJsonFunction, ResponseType } from './options.js';
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { RequestError } from './errors.js';
|
|
2
2
|
export const isResponseOk = (response) => {
|
|
3
3
|
const { statusCode } = response;
|
|
4
|
-
const
|
|
4
|
+
const { followRedirect } = response.request.options;
|
|
5
|
+
const shouldFollow = typeof followRedirect === 'function' ? followRedirect(response) : followRedirect;
|
|
6
|
+
const limitStatusCode = shouldFollow ? 299 : 399;
|
|
5
7
|
return (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304;
|
|
6
8
|
};
|
|
7
9
|
/**
|
|
@@ -3,20 +3,11 @@ import unhandler from './utils/unhandle.js';
|
|
|
3
3
|
const reentry = Symbol('reentry');
|
|
4
4
|
const noop = () => { };
|
|
5
5
|
export class TimeoutError extends Error {
|
|
6
|
+
event;
|
|
7
|
+
code;
|
|
6
8
|
constructor(threshold, event) {
|
|
7
9
|
super(`Timeout awaiting '${event}' for ${threshold}ms`);
|
|
8
|
-
|
|
9
|
-
enumerable: true,
|
|
10
|
-
configurable: true,
|
|
11
|
-
writable: true,
|
|
12
|
-
value: event
|
|
13
|
-
});
|
|
14
|
-
Object.defineProperty(this, "code", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
configurable: true,
|
|
17
|
-
writable: true,
|
|
18
|
-
value: void 0
|
|
19
|
-
});
|
|
10
|
+
this.event = event;
|
|
20
11
|
this.name = 'TimeoutError';
|
|
21
12
|
this.code = 'ETIMEDOUT';
|
|
22
13
|
}
|
|
@@ -55,13 +46,13 @@ export default function timedOut(request, delays, options) {
|
|
|
55
46
|
throw error;
|
|
56
47
|
}
|
|
57
48
|
});
|
|
58
|
-
if (
|
|
49
|
+
if (delays.request !== undefined) {
|
|
59
50
|
const cancelTimeout = addTimeout(delays.request, timeoutHandler, 'request');
|
|
60
51
|
once(request, 'response', (response) => {
|
|
61
52
|
once(response, 'end', cancelTimeout);
|
|
62
53
|
});
|
|
63
54
|
}
|
|
64
|
-
if (
|
|
55
|
+
if (delays.socket !== undefined) {
|
|
65
56
|
const { socket } = delays;
|
|
66
57
|
const socketTimeoutHandler = () => {
|
|
67
58
|
timeoutHandler(socket, 'socket');
|
|
@@ -74,17 +65,17 @@ export default function timedOut(request, delays, options) {
|
|
|
74
65
|
request.removeListener('timeout', socketTimeoutHandler);
|
|
75
66
|
});
|
|
76
67
|
}
|
|
77
|
-
const hasLookup =
|
|
78
|
-
const hasConnect =
|
|
79
|
-
const hasSecureConnect =
|
|
80
|
-
const hasSend =
|
|
68
|
+
const hasLookup = delays.lookup !== undefined;
|
|
69
|
+
const hasConnect = delays.connect !== undefined;
|
|
70
|
+
const hasSecureConnect = delays.secureConnect !== undefined;
|
|
71
|
+
const hasSend = delays.send !== undefined;
|
|
81
72
|
if (hasLookup || hasConnect || hasSecureConnect || hasSend) {
|
|
82
73
|
once(request, 'socket', (socket) => {
|
|
83
74
|
const { socketPath } = request;
|
|
84
75
|
/* istanbul ignore next: hard to test */
|
|
85
76
|
if (socket.connecting) {
|
|
86
77
|
const hasPath = Boolean(socketPath ?? net.isIP(hostname ?? host ?? '') !== 0);
|
|
87
|
-
if (hasLookup && !hasPath &&
|
|
78
|
+
if (hasLookup && !hasPath && socket.address().address === undefined) {
|
|
88
79
|
const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
|
|
89
80
|
once(socket, 'lookup', cancelTimeout);
|
|
90
81
|
}
|
|
@@ -122,13 +113,13 @@ export default function timedOut(request, delays, options) {
|
|
|
122
113
|
}
|
|
123
114
|
});
|
|
124
115
|
}
|
|
125
|
-
if (
|
|
116
|
+
if (delays.response !== undefined) {
|
|
126
117
|
once(request, 'upload-complete', () => {
|
|
127
118
|
const cancelTimeout = addTimeout(delays.response, timeoutHandler, 'response');
|
|
128
119
|
once(request, 'response', cancelTimeout);
|
|
129
120
|
});
|
|
130
121
|
}
|
|
131
|
-
if (
|
|
122
|
+
if (delays.read !== undefined) {
|
|
132
123
|
once(request, 'response', (response) => {
|
|
133
124
|
const cancelTimeout = addTimeout(delays.read, timeoutHandler, 'read');
|
|
134
125
|
once(response, 'end', cancelTimeout);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
3
|
import type { Writable, Readable } from 'node:stream';
|
|
3
4
|
import type { ClientRequest } from 'node:http';
|
|
4
5
|
declare function isClientRequest(clientRequest: Writable | Readable): clientRequest is ClientRequest;
|
|
@@ -1,17 +1,7 @@
|
|
|
1
1
|
export default class WeakableMap {
|
|
2
|
+
weakMap;
|
|
3
|
+
map;
|
|
2
4
|
constructor() {
|
|
3
|
-
Object.defineProperty(this, "weakMap", {
|
|
4
|
-
enumerable: true,
|
|
5
|
-
configurable: true,
|
|
6
|
-
writable: true,
|
|
7
|
-
value: void 0
|
|
8
|
-
});
|
|
9
|
-
Object.defineProperty(this, "map", {
|
|
10
|
-
enumerable: true,
|
|
11
|
-
configurable: true,
|
|
12
|
-
writable: true,
|
|
13
|
-
value: void 0
|
|
14
|
-
});
|
|
15
5
|
this.weakMap = new WeakMap();
|
|
16
6
|
this.map = new Map();
|
|
17
7
|
}
|
package/dist/source/index.js
CHANGED
package/dist/source/types.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
3
|
import type { Buffer } from 'node:buffer';
|
|
4
|
-
import type { URL } from 'node:url';
|
|
5
4
|
import type { CancelableRequest } from './as-promise/types.js';
|
|
6
5
|
import type { Response } from './core/response.js';
|
|
7
6
|
import type Options from './core/options.js';
|
|
8
|
-
import type
|
|
7
|
+
import { type PaginationOptions, type OptionsInit } from './core/options.js';
|
|
9
8
|
import type Request from './core/index.js';
|
|
10
9
|
type Except<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
|
|
11
10
|
type Merge<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
|
package/license
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "got",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "14.0.0",
|
|
4
4
|
"description": "Human-friendly and powerful HTTP request library for Node.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/got",
|
|
7
7
|
"funding": "https://github.com/sindresorhus/got?sponsor=1",
|
|
8
8
|
"type": "module",
|
|
9
|
-
"exports":
|
|
10
|
-
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./dist/source/index.d.ts",
|
|
11
|
+
"default": "./dist/source/index.js"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
11
14
|
"engines": {
|
|
12
|
-
"node": ">=
|
|
15
|
+
"node": ">=20"
|
|
13
16
|
},
|
|
14
17
|
"scripts": {
|
|
15
|
-
"test": "xo && tsc --noEmit && ava",
|
|
18
|
+
"test": "xo && tsc --noEmit && NODE_OPTIONS='--import=tsx/esm' ava",
|
|
16
19
|
"release": "np",
|
|
17
20
|
"build": "del-cli dist && tsc",
|
|
18
21
|
"prepare": "npm run build"
|
|
@@ -45,64 +48,61 @@
|
|
|
45
48
|
"ky"
|
|
46
49
|
],
|
|
47
50
|
"dependencies": {
|
|
48
|
-
"@sindresorhus/is": "^
|
|
51
|
+
"@sindresorhus/is": "^6.1.0",
|
|
49
52
|
"@szmarczak/http-timer": "^5.0.1",
|
|
50
53
|
"cacheable-lookup": "^7.0.0",
|
|
51
|
-
"cacheable-request": "^10.2.
|
|
54
|
+
"cacheable-request": "^10.2.14",
|
|
52
55
|
"decompress-response": "^6.0.0",
|
|
53
|
-
"form-data-encoder": "^
|
|
54
|
-
"get-stream": "^
|
|
55
|
-
"http2-wrapper": "^2.1
|
|
56
|
+
"form-data-encoder": "^4.0.2",
|
|
57
|
+
"get-stream": "^8.0.1",
|
|
58
|
+
"http2-wrapper": "^2.2.1",
|
|
56
59
|
"lowercase-keys": "^3.0.0",
|
|
57
|
-
"p-cancelable": "^
|
|
60
|
+
"p-cancelable": "^4.0.1",
|
|
58
61
|
"responselike": "^3.0.0"
|
|
59
62
|
},
|
|
60
63
|
"devDependencies": {
|
|
61
64
|
"@hapi/bourne": "^3.0.0",
|
|
62
|
-
"@sindresorhus/tsconfig": "^
|
|
63
|
-
"@sinonjs/fake-timers": "^
|
|
64
|
-
"@types/benchmark": "^2.1.
|
|
65
|
-
"@types/express": "^4.17.
|
|
66
|
-
"@types/node": "^
|
|
67
|
-
"@types/pem": "^1.
|
|
68
|
-
"@types/
|
|
69
|
-
"@types/
|
|
70
|
-
"@types/
|
|
71
|
-
"@types/
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"ava": "^5.2.0",
|
|
75
|
-
"axios": "^0.27.2",
|
|
65
|
+
"@sindresorhus/tsconfig": "^5.0.0",
|
|
66
|
+
"@sinonjs/fake-timers": "^11.2.2",
|
|
67
|
+
"@types/benchmark": "^2.1.5",
|
|
68
|
+
"@types/express": "^4.17.21",
|
|
69
|
+
"@types/node": "^20.10.0",
|
|
70
|
+
"@types/pem": "^1.14.4",
|
|
71
|
+
"@types/readable-stream": "^4.0.9",
|
|
72
|
+
"@types/request": "^2.48.12",
|
|
73
|
+
"@types/sinon": "^17.0.2",
|
|
74
|
+
"@types/sinonjs__fake-timers": "^8.1.5",
|
|
75
|
+
"ava": "^5.3.1",
|
|
76
|
+
"axios": "^1.6.2",
|
|
76
77
|
"benchmark": "^2.1.4",
|
|
77
78
|
"bluebird": "^3.7.2",
|
|
78
79
|
"body-parser": "^1.20.2",
|
|
79
80
|
"create-cert": "^1.0.6",
|
|
80
81
|
"create-test-server": "^3.0.1",
|
|
81
|
-
"del-cli": "^5.
|
|
82
|
-
"delay": "^
|
|
83
|
-
"express": "^4.
|
|
82
|
+
"del-cli": "^5.1.0",
|
|
83
|
+
"delay": "^6.0.0",
|
|
84
|
+
"express": "^4.18.2",
|
|
84
85
|
"form-data": "^4.0.0",
|
|
85
|
-
"formdata-node": "^
|
|
86
|
-
"nock": "^13.
|
|
87
|
-
"node-fetch": "^3.2
|
|
88
|
-
"np": "^
|
|
86
|
+
"formdata-node": "^6.0.3",
|
|
87
|
+
"nock": "^13.4.0",
|
|
88
|
+
"node-fetch": "^3.3.2",
|
|
89
|
+
"np": "^9.0.0",
|
|
89
90
|
"nyc": "^15.1.0",
|
|
90
|
-
"p-event": "^
|
|
91
|
-
"pem": "^1.14.
|
|
92
|
-
"pify": "^6.
|
|
93
|
-
"readable-stream": "^4.2
|
|
91
|
+
"p-event": "^6.0.0",
|
|
92
|
+
"pem": "^1.14.8",
|
|
93
|
+
"pify": "^6.1.0",
|
|
94
|
+
"readable-stream": "^4.4.2",
|
|
94
95
|
"request": "^2.88.2",
|
|
95
|
-
"sinon": "^
|
|
96
|
+
"sinon": "^17.0.1",
|
|
96
97
|
"slow-stream": "0.0.4",
|
|
97
|
-
"tempy": "^3.
|
|
98
|
+
"tempy": "^3.1.0",
|
|
98
99
|
"then-busboy": "^5.2.1",
|
|
99
|
-
"tough-cookie": "4.1.
|
|
100
|
-
"
|
|
101
|
-
"type-fest": "^
|
|
102
|
-
"typescript": "
|
|
103
|
-
"xo": "^0.
|
|
100
|
+
"tough-cookie": "^4.1.3",
|
|
101
|
+
"tsx": "^4.6.0",
|
|
102
|
+
"type-fest": "^4.8.2",
|
|
103
|
+
"typescript": "^5.3.2",
|
|
104
|
+
"xo": "^0.56.0"
|
|
104
105
|
},
|
|
105
|
-
"sideEffects": false,
|
|
106
106
|
"ava": {
|
|
107
107
|
"files": [
|
|
108
108
|
"test/*"
|
|
@@ -111,9 +111,7 @@
|
|
|
111
111
|
"extensions": {
|
|
112
112
|
"ts": "module"
|
|
113
113
|
},
|
|
114
|
-
"
|
|
115
|
-
"--loader=ts-node/esm"
|
|
116
|
-
]
|
|
114
|
+
"workerThreads": false
|
|
117
115
|
},
|
|
118
116
|
"nyc": {
|
|
119
117
|
"reporter": [
|
|
@@ -135,10 +133,7 @@
|
|
|
135
133
|
"rules": {
|
|
136
134
|
"@typescript-eslint/no-empty-function": "off",
|
|
137
135
|
"n/no-deprecated-api": "off",
|
|
138
|
-
"n/prefer-global/url": "off",
|
|
139
|
-
"n/prefer-global/url-search-params": "off",
|
|
140
136
|
"@typescript-eslint/no-implicit-any-catch": "off",
|
|
141
|
-
"unicorn/prefer-node-protocol": "off",
|
|
142
137
|
"ava/assertion-arguments": "off",
|
|
143
138
|
"@typescript-eslint/no-unsafe-member-access": "off",
|
|
144
139
|
"@typescript-eslint/no-unsafe-return": "off",
|
|
@@ -146,8 +141,11 @@
|
|
|
146
141
|
"@typescript-eslint/no-unsafe-call": "off",
|
|
147
142
|
"@typescript-eslint/await-thenable": "off",
|
|
148
143
|
"@typescript-eslint/no-redundant-type-constituents": "off",
|
|
144
|
+
"@typescript-eslint/no-unsafe-argument": "off",
|
|
145
|
+
"@typescript-eslint/promise-function-async": "off",
|
|
149
146
|
"no-lone-blocks": "off",
|
|
150
|
-
"unicorn/no-await-expression-member": "off"
|
|
147
|
+
"unicorn/no-await-expression-member": "off",
|
|
148
|
+
"unicorn/prefer-event-target": "off"
|
|
151
149
|
}
|
|
152
150
|
},
|
|
153
151
|
"runkitExampleFilename": "./documentation/examples/runkit-example.js"
|
package/readme.md
CHANGED
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
|
|
81
81
|
---
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
**You probably want [Ky](https://github.com/sindresorhus/ky) instead, by the same people. It's smaller, works in the browser too, and is more stable since it's built upon [`Fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).**
|
|
84
84
|
|
|
85
85
|
---
|
|
86
86
|
|
|
@@ -92,9 +92,9 @@ For browser usage, we recommend [Ky](https://github.com/sindresorhus/ky) by the
|
|
|
92
92
|
npm install got
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
-
**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you will have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)
|
|
95
|
+
**Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you will have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). Please don't open issues for questions regarding CommonJS / ESM.
|
|
96
96
|
|
|
97
|
-
**Got v11
|
|
97
|
+
**Got v11 is no longer maintained and we will not accept any backport requests.**
|
|
98
98
|
|
|
99
99
|
## Take a peek
|
|
100
100
|
|
|
@@ -124,7 +124,7 @@ For advanced JSON usage, check out the [`parseJson`](documentation/2-options.md#
|
|
|
124
124
|
|
|
125
125
|
## Highlights
|
|
126
126
|
|
|
127
|
-
- [Used by
|
|
127
|
+
- [Used by 10K+ packages and 5M+ repos](https://github.com/sindresorhus/got/network/dependents)
|
|
128
128
|
- [Actively maintained](https://github.com/sindresorhus/got/graphs/contributors)
|
|
129
129
|
- [Trusted by many companies](#widely-used)
|
|
130
130
|
|
|
@@ -187,11 +187,6 @@ By default, Got will retry on failure. To disable this option, set [`options.ret
|
|
|
187
187
|
- [`got-scraping`](https://github.com/apify/got-scraping) - Got wrapper specifically designed for web scraping purposes
|
|
188
188
|
- [`got-ssrf`](https://github.com/JaneJeon/got-ssrf) - Got wrapper to protect server-side requests against SSRF attacks
|
|
189
189
|
|
|
190
|
-
### Legacy
|
|
191
|
-
|
|
192
|
-
- [travis-got](https://github.com/samverschueren/travis-got) - Got convenience wrapper to interact with the Travis API
|
|
193
|
-
- [graphql-got](https://github.com/kevva/graphql-got) - Got convenience wrapper to interact with GraphQL
|
|
194
|
-
|
|
195
190
|
## Comparison
|
|
196
191
|
|
|
197
192
|
| | `got` | [`node-fetch`][n0] | [`ky`][k0] | [`axios`][a0] | [`superagent`][s0] |
|
|
@@ -203,7 +198,7 @@ By default, Got will retry on failure. To disable this option, set [`options.ret
|
|
|
203
198
|
| Pagination API | :heavy_check_mark: | :x: | :x: | :x: | :x: |
|
|
204
199
|
| Request cancelation | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
205
200
|
| RFC compliant caching | :heavy_check_mark: | :x: | :x: | :x: | :x: |
|
|
206
|
-
| Cookies (out-of-box) | :heavy_check_mark: | :x: | :x: | :x: | :x: |
|
|
201
|
+
| Cookies (out-of-the-box) | :heavy_check_mark: | :x: | :x: | :x: | :x: |
|
|
207
202
|
| Follows redirects | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
208
203
|
| Retries on failure | :heavy_check_mark: | :x: | :heavy_check_mark: | :x: | :heavy_check_mark: |
|
|
209
204
|
| Progress events | :heavy_check_mark: | :x: | :heavy_check_mark:\*\*\* | Browser only | :heavy_check_mark: |
|
|
@@ -393,14 +388,6 @@ By default, Got will retry on failure. To disable this option, set [`options.ret
|
|
|
393
388
|
---|---
|
|
394
389
|
[Sindre Sorhus](https://sindresorhus.com) | [Szymon Marczak](https://github.com/szmarczak)
|
|
395
390
|
|
|
396
|
-
###### Former
|
|
397
|
-
|
|
398
|
-
- [Vsevolod Strukchinsky](https://github.com/floatdrop)
|
|
399
|
-
- [Alexander Tesfamichael](https://github.com/alextes)
|
|
400
|
-
- [Brandon Smith](https://github.com/brandon93s)
|
|
401
|
-
- [Luke Childs](https://github.com/lukechilds)
|
|
402
|
-
- [Giovanni Minotti](https://github.com/Giotino)
|
|
403
|
-
|
|
404
391
|
<a name="widely-used"></a>
|
|
405
392
|
## These amazing companies are using Got
|
|
406
393
|
|
|
@@ -436,7 +423,7 @@ By default, Got will retry on failure. To disable this option, set [`options.ret
|
|
|
436
423
|
</td>
|
|
437
424
|
<td align="center">
|
|
438
425
|
<a href="https://github.com/renovatebot/renovate">
|
|
439
|
-
<img width="150" valign="middle" src="https://camo.githubusercontent.com/
|
|
426
|
+
<img width="150" valign="middle" src="https://camo.githubusercontent.com/7c2dc41a8407d4cfa700f762a1abf46b232858ae7e3a2bf5aee7d9f36416127c/68747470733a2f2f6170702e72656e6f76617465626f742e636f6d2f696d616765732f72656e6f766174655f3636305f3232302e6a7067">
|
|
440
427
|
</a>
|
|
441
428
|
</td>
|
|
442
429
|
<td align="center">
|
|
@@ -519,9 +506,3 @@ By default, Got will retry on failure. To disable this option, set [`options.ret
|
|
|
519
506
|
> Got has been a crucial component of Apify's scraping for years. We use it to extract data from billions of web pages every month, and we really appreciate the powerful API and extensibility, which allowed us to build our own specialized HTTP client on top of Got. The support has always been stellar too.
|
|
520
507
|
>
|
|
521
508
|
> — <a href="https://github.com/mnmkng">Ondra Urban</a>
|
|
522
|
-
|
|
523
|
-
## For enterprise
|
|
524
|
-
|
|
525
|
-
Available as part of the Tidelift Subscription.
|
|
526
|
-
|
|
527
|
-
The maintainers of `got` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-got?utm_source=npm-got&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|