@depup/got 14.6.6-depup.1
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/README.md +36 -0
- package/changes.json +30 -0
- package/dist/source/as-promise/index.d.ts +3 -0
- package/dist/source/as-promise/index.js +204 -0
- package/dist/source/as-promise/types.d.ts +37 -0
- package/dist/source/as-promise/types.js +17 -0
- package/dist/source/core/calculate-retry-delay.d.ts +4 -0
- package/dist/source/core/calculate-retry-delay.js +29 -0
- package/dist/source/core/diagnostics-channel.d.ts +89 -0
- package/dist/source/core/diagnostics-channel.js +49 -0
- package/dist/source/core/errors.d.ts +102 -0
- package/dist/source/core/errors.js +147 -0
- package/dist/source/core/index.d.ts +192 -0
- package/dist/source/core/index.js +1339 -0
- package/dist/source/core/options.d.ts +1564 -0
- package/dist/source/core/options.js +1838 -0
- package/dist/source/core/parse-link-header.d.ts +4 -0
- package/dist/source/core/parse-link-header.js +33 -0
- package/dist/source/core/response.d.ts +109 -0
- package/dist/source/core/response.js +41 -0
- package/dist/source/core/timed-out.d.ts +31 -0
- package/dist/source/core/timed-out.js +136 -0
- package/dist/source/core/utils/defer-to-connect.d.ts +9 -0
- package/dist/source/core/utils/defer-to-connect.js +44 -0
- package/dist/source/core/utils/get-body-size.d.ts +2 -0
- package/dist/source/core/utils/get-body-size.js +37 -0
- package/dist/source/core/utils/is-client-request.d.ts +4 -0
- package/dist/source/core/utils/is-client-request.js +4 -0
- package/dist/source/core/utils/is-form-data.d.ts +7 -0
- package/dist/source/core/utils/is-form-data.js +4 -0
- package/dist/source/core/utils/is-unix-socket-url.d.ts +17 -0
- package/dist/source/core/utils/is-unix-socket-url.js +25 -0
- package/dist/source/core/utils/options-to-url.d.ts +12 -0
- package/dist/source/core/utils/options-to-url.js +48 -0
- package/dist/source/core/utils/proxy-events.d.ts +2 -0
- package/dist/source/core/utils/proxy-events.js +15 -0
- package/dist/source/core/utils/timer.d.ts +31 -0
- package/dist/source/core/utils/timer.js +162 -0
- package/dist/source/core/utils/unhandle.d.ts +10 -0
- package/dist/source/core/utils/unhandle.js +20 -0
- package/dist/source/core/utils/url-to-options.d.ts +14 -0
- package/dist/source/core/utils/url-to-options.js +22 -0
- package/dist/source/core/utils/weakable-map.d.ts +7 -0
- package/dist/source/core/utils/weakable-map.js +24 -0
- package/dist/source/create.d.ts +3 -0
- package/dist/source/create.js +188 -0
- package/dist/source/index.d.ts +16 -0
- package/dist/source/index.js +22 -0
- package/dist/source/types.d.ts +314 -0
- package/dist/source/types.js +1 -0
- package/license +9 -0
- package/package.json +197 -0
- package/readme.md +478 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import is from '@sindresorhus/is';
|
|
2
|
+
// A hacky check to prevent circular references.
|
|
3
|
+
function isRequest(x) {
|
|
4
|
+
return is.object(x) && '_onResponse' in x;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
An error to be thrown when a request fails.
|
|
8
|
+
Contains a `code` property with error class code, like `ECONNREFUSED`.
|
|
9
|
+
*/
|
|
10
|
+
export class RequestError extends Error {
|
|
11
|
+
name = 'RequestError';
|
|
12
|
+
code = 'ERR_GOT_REQUEST_ERROR';
|
|
13
|
+
input;
|
|
14
|
+
stack;
|
|
15
|
+
response;
|
|
16
|
+
request;
|
|
17
|
+
timings;
|
|
18
|
+
constructor(message, error, self) {
|
|
19
|
+
super(message, { cause: error });
|
|
20
|
+
Error.captureStackTrace(this, this.constructor);
|
|
21
|
+
if (error.code) {
|
|
22
|
+
this.code = error.code;
|
|
23
|
+
}
|
|
24
|
+
this.input = error.input;
|
|
25
|
+
if (isRequest(self)) {
|
|
26
|
+
Object.defineProperty(this, 'request', {
|
|
27
|
+
enumerable: false,
|
|
28
|
+
value: self,
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, 'response', {
|
|
31
|
+
enumerable: false,
|
|
32
|
+
value: self.response,
|
|
33
|
+
});
|
|
34
|
+
this.options = self.options;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
this.options = self;
|
|
38
|
+
}
|
|
39
|
+
this.timings = this.request?.timings;
|
|
40
|
+
// Recover the original stacktrace
|
|
41
|
+
if (is.string(error.stack) && is.string(this.stack)) {
|
|
42
|
+
const indexOfMessage = this.stack.indexOf(this.message) + this.message.length;
|
|
43
|
+
const thisStackTrace = this.stack.slice(indexOfMessage).split('\n').reverse();
|
|
44
|
+
const errorStackTrace = error.stack.slice(error.stack.indexOf(error.message) + error.message.length).split('\n').reverse();
|
|
45
|
+
// Remove duplicated traces
|
|
46
|
+
while (errorStackTrace.length > 0 && errorStackTrace[0] === thisStackTrace[0]) {
|
|
47
|
+
thisStackTrace.shift();
|
|
48
|
+
}
|
|
49
|
+
this.stack = `${this.stack.slice(0, indexOfMessage)}${thisStackTrace.reverse().join('\n')}${errorStackTrace.reverse().join('\n')}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
An error to be thrown when the server redirects you more than ten times.
|
|
55
|
+
Includes a `response` property.
|
|
56
|
+
*/
|
|
57
|
+
export class MaxRedirectsError extends RequestError {
|
|
58
|
+
name = 'MaxRedirectsError';
|
|
59
|
+
code = 'ERR_TOO_MANY_REDIRECTS';
|
|
60
|
+
constructor(request) {
|
|
61
|
+
super(`Redirected ${request.options.maxRedirects} times. Aborting.`, {}, request);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
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.
|
|
66
|
+
Includes a `response` property.
|
|
67
|
+
*/
|
|
68
|
+
// TODO: Change `HTTPError<T = any>` to `HTTPError<T = unknown>` in the next major version to enforce type usage.
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
70
|
+
export class HTTPError extends RequestError {
|
|
71
|
+
name = 'HTTPError';
|
|
72
|
+
code = 'ERR_NON_2XX_3XX_RESPONSE';
|
|
73
|
+
constructor(response) {
|
|
74
|
+
super(`Request failed with status code ${response.statusCode} (${response.statusMessage}): ${response.request.options.method} ${response.request.options.url.toString()}`, {}, response.request);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
An error to be thrown when a cache method fails.
|
|
79
|
+
For example, if the database goes down or there's a filesystem error.
|
|
80
|
+
*/
|
|
81
|
+
export class CacheError extends RequestError {
|
|
82
|
+
name = 'CacheError';
|
|
83
|
+
constructor(error, request) {
|
|
84
|
+
super(error.message, error, request);
|
|
85
|
+
if (this.code === 'ERR_GOT_REQUEST_ERROR') {
|
|
86
|
+
this.code = 'ERR_CACHE_ACCESS';
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
|
|
92
|
+
*/
|
|
93
|
+
export class UploadError extends RequestError {
|
|
94
|
+
name = 'UploadError';
|
|
95
|
+
constructor(error, request) {
|
|
96
|
+
super(error.message, error, request);
|
|
97
|
+
if (this.code === 'ERR_GOT_REQUEST_ERROR') {
|
|
98
|
+
this.code = 'ERR_UPLOAD';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
An error to be thrown when the request is aborted due to a timeout.
|
|
104
|
+
Includes an `event` and `timings` property.
|
|
105
|
+
*/
|
|
106
|
+
export class TimeoutError extends RequestError {
|
|
107
|
+
name = 'TimeoutError';
|
|
108
|
+
timings;
|
|
109
|
+
event;
|
|
110
|
+
constructor(error, timings, request) {
|
|
111
|
+
super(error.message, error, request);
|
|
112
|
+
this.event = error.event;
|
|
113
|
+
this.timings = timings;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
An error to be thrown when reading from response stream fails.
|
|
118
|
+
*/
|
|
119
|
+
export class ReadError extends RequestError {
|
|
120
|
+
name = 'ReadError';
|
|
121
|
+
constructor(error, request) {
|
|
122
|
+
super(error.message, error, request);
|
|
123
|
+
if (this.code === 'ERR_GOT_REQUEST_ERROR') {
|
|
124
|
+
this.code = 'ERR_READING_RESPONSE_STREAM';
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
An error which always triggers a new retry when thrown.
|
|
130
|
+
*/
|
|
131
|
+
export class RetryError extends RequestError {
|
|
132
|
+
name = 'RetryError';
|
|
133
|
+
code = 'ERR_RETRYING';
|
|
134
|
+
constructor(request) {
|
|
135
|
+
super('Retrying', {}, request);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
An error to be thrown when the request is aborted by AbortController.
|
|
140
|
+
*/
|
|
141
|
+
export class AbortError extends RequestError {
|
|
142
|
+
name = 'AbortError';
|
|
143
|
+
code = 'ERR_ABORTED';
|
|
144
|
+
constructor(request) {
|
|
145
|
+
super('This operation was aborted.', {}, request);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Duplex } from 'node:stream';
|
|
2
|
+
import { type ClientRequest } from 'node:http';
|
|
3
|
+
import type { Socket } from 'node:net';
|
|
4
|
+
import { type Timings } from './utils/timer.js';
|
|
5
|
+
import Options, { type OptionsInit } from './options.js';
|
|
6
|
+
import { type PlainResponse, type Response } from './response.js';
|
|
7
|
+
import { RequestError } from './errors.js';
|
|
8
|
+
type Error = NodeJS.ErrnoException;
|
|
9
|
+
export type Progress = {
|
|
10
|
+
percent: number;
|
|
11
|
+
transferred: number;
|
|
12
|
+
total?: number;
|
|
13
|
+
};
|
|
14
|
+
export type GotEventFunction<T> =
|
|
15
|
+
/**
|
|
16
|
+
`request` event to get the request object of the request.
|
|
17
|
+
|
|
18
|
+
__Tip__: You can use `request` event to abort requests.
|
|
19
|
+
|
|
20
|
+
@example
|
|
21
|
+
```
|
|
22
|
+
import got from 'got';
|
|
23
|
+
|
|
24
|
+
got.stream('https://github.com')
|
|
25
|
+
.on('request', request => setTimeout(() => request.destroy(), 50));
|
|
26
|
+
```
|
|
27
|
+
*/
|
|
28
|
+
((name: 'request', listener: (request: ClientRequest) => void) => T)
|
|
29
|
+
/**
|
|
30
|
+
The `response` event to get the response object of the final request.
|
|
31
|
+
*/
|
|
32
|
+
& (<R extends Response>(name: 'response', listener: (response: R) => void) => T)
|
|
33
|
+
/**
|
|
34
|
+
The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
|
|
35
|
+
*/
|
|
36
|
+
& (<R extends Response, N extends Options>(name: 'redirect', listener: (response: R, nextOptions: N) => void) => T)
|
|
37
|
+
/**
|
|
38
|
+
Progress events for uploading (sending a request) and downloading (receiving a response).
|
|
39
|
+
The `progress` argument is an object like:
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
{
|
|
43
|
+
percent: 0.1,
|
|
44
|
+
transferred: 1024,
|
|
45
|
+
total: 10240
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
If the `content-length` header is missing, `total` will be `undefined`.
|
|
50
|
+
|
|
51
|
+
@example
|
|
52
|
+
```
|
|
53
|
+
import got from 'got';
|
|
54
|
+
|
|
55
|
+
const response = await got('https://sindresorhus.com')
|
|
56
|
+
.on('downloadProgress', progress => {
|
|
57
|
+
// Report download progress
|
|
58
|
+
})
|
|
59
|
+
.on('uploadProgress', progress => {
|
|
60
|
+
// Report upload progress
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
console.log(response);
|
|
64
|
+
```
|
|
65
|
+
*/
|
|
66
|
+
& ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
|
|
67
|
+
/**
|
|
68
|
+
To enable retrying on a Got stream, it is required to have a `retry` handler attached.
|
|
69
|
+
|
|
70
|
+
When this event is emitted, you should reset the stream you were writing to and prepare the body again.
|
|
71
|
+
|
|
72
|
+
See `got.options.retry` for more information.
|
|
73
|
+
*/
|
|
74
|
+
& ((name: 'retry', listener: (retryCount: number, error: RequestError, createRetryStream: (options?: OptionsInit) => Request) => void) => T);
|
|
75
|
+
export type RequestEvents<T> = {
|
|
76
|
+
on: GotEventFunction<T>;
|
|
77
|
+
once: GotEventFunction<T>;
|
|
78
|
+
off: GotEventFunction<T>;
|
|
79
|
+
};
|
|
80
|
+
type UrlType = ConstructorParameters<typeof Options>[0];
|
|
81
|
+
type OptionsType = ConstructorParameters<typeof Options>[1];
|
|
82
|
+
type DefaultsType = ConstructorParameters<typeof Options>[2];
|
|
83
|
+
export default class Request extends Duplex implements RequestEvents<Request> {
|
|
84
|
+
['constructor']: typeof Request;
|
|
85
|
+
_noPipe?: boolean;
|
|
86
|
+
options: Options;
|
|
87
|
+
response?: PlainResponse;
|
|
88
|
+
requestUrl?: URL;
|
|
89
|
+
redirectUrls: URL[];
|
|
90
|
+
retryCount: number;
|
|
91
|
+
_stopReading: boolean;
|
|
92
|
+
private _requestOptions;
|
|
93
|
+
private _stopRetry;
|
|
94
|
+
private _downloadedSize;
|
|
95
|
+
private _uploadedSize;
|
|
96
|
+
private readonly _pipedServerResponses;
|
|
97
|
+
private _request?;
|
|
98
|
+
private _responseSize?;
|
|
99
|
+
private _bodySize?;
|
|
100
|
+
private _unproxyEvents;
|
|
101
|
+
private _isFromCache?;
|
|
102
|
+
private _triggerRead;
|
|
103
|
+
private readonly _jobs;
|
|
104
|
+
private _cancelTimeouts;
|
|
105
|
+
private readonly _removeListeners;
|
|
106
|
+
private _nativeResponse?;
|
|
107
|
+
private _flushed;
|
|
108
|
+
private _aborted;
|
|
109
|
+
private _expectedContentLength?;
|
|
110
|
+
private _compressedBytesCount?;
|
|
111
|
+
private readonly _requestId;
|
|
112
|
+
private _requestInitialized;
|
|
113
|
+
constructor(url: UrlType, options?: OptionsType, defaults?: DefaultsType);
|
|
114
|
+
flush(): Promise<void>;
|
|
115
|
+
_beforeError(error: Error): void;
|
|
116
|
+
_read(): void;
|
|
117
|
+
_write(chunk: unknown, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
|
|
118
|
+
_final(callback: (error?: Error | null) => void): void;
|
|
119
|
+
_destroy(error: Error | null, callback: (error: Error | null) => void): void;
|
|
120
|
+
pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
|
|
121
|
+
end?: boolean;
|
|
122
|
+
}): T;
|
|
123
|
+
unpipe<T extends NodeJS.WritableStream>(destination: T): this;
|
|
124
|
+
private _checkContentLengthMismatch;
|
|
125
|
+
private _finalizeBody;
|
|
126
|
+
private _onResponseBase;
|
|
127
|
+
private _setRawBody;
|
|
128
|
+
private _onResponse;
|
|
129
|
+
private _onRequest;
|
|
130
|
+
private _asyncWrite;
|
|
131
|
+
private _sendBody;
|
|
132
|
+
private _prepareCache;
|
|
133
|
+
private _createCacheableRequest;
|
|
134
|
+
private _makeRequest;
|
|
135
|
+
private _error;
|
|
136
|
+
private _writeRequest;
|
|
137
|
+
/**
|
|
138
|
+
The remote IP address.
|
|
139
|
+
*/
|
|
140
|
+
get ip(): string | undefined;
|
|
141
|
+
/**
|
|
142
|
+
Indicates whether the request has been aborted or not.
|
|
143
|
+
*/
|
|
144
|
+
get isAborted(): boolean;
|
|
145
|
+
get socket(): Socket | undefined;
|
|
146
|
+
/**
|
|
147
|
+
Progress event for downloading (receiving a response).
|
|
148
|
+
*/
|
|
149
|
+
get downloadProgress(): Progress;
|
|
150
|
+
/**
|
|
151
|
+
Progress event for uploading (sending a request).
|
|
152
|
+
*/
|
|
153
|
+
get uploadProgress(): Progress;
|
|
154
|
+
/**
|
|
155
|
+
The object contains the following properties:
|
|
156
|
+
|
|
157
|
+
- `start` - Time when the request started.
|
|
158
|
+
- `socket` - Time when a socket was assigned to the request.
|
|
159
|
+
- `lookup` - Time when the DNS lookup finished.
|
|
160
|
+
- `connect` - Time when the socket successfully connected.
|
|
161
|
+
- `secureConnect` - Time when the socket securely connected.
|
|
162
|
+
- `upload` - Time when the request finished uploading.
|
|
163
|
+
- `response` - Time when the request fired `response` event.
|
|
164
|
+
- `end` - Time when the response fired `end` event.
|
|
165
|
+
- `error` - Time when the request fired `error` event.
|
|
166
|
+
- `abort` - Time when the request fired `abort` event.
|
|
167
|
+
- `phases`
|
|
168
|
+
- `wait` - `timings.socket - timings.start`
|
|
169
|
+
- `dns` - `timings.lookup - timings.socket`
|
|
170
|
+
- `tcp` - `timings.connect - timings.lookup`
|
|
171
|
+
- `tls` - `timings.secureConnect - timings.connect`
|
|
172
|
+
- `request` - `timings.upload - (timings.secureConnect || timings.connect)`
|
|
173
|
+
- `firstByte` - `timings.response - timings.upload`
|
|
174
|
+
- `download` - `timings.end - timings.response`
|
|
175
|
+
- `total` - `(timings.end || timings.error || timings.abort) - timings.start`
|
|
176
|
+
|
|
177
|
+
If something has not been measured yet, it will be `undefined`.
|
|
178
|
+
|
|
179
|
+
__Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
|
|
180
|
+
*/
|
|
181
|
+
get timings(): Timings | undefined;
|
|
182
|
+
/**
|
|
183
|
+
Whether the response was retrieved from the cache.
|
|
184
|
+
*/
|
|
185
|
+
get isFromCache(): boolean | undefined;
|
|
186
|
+
get reusedSocket(): boolean | undefined;
|
|
187
|
+
/**
|
|
188
|
+
Whether the stream is read-only. Returns `true` when `body`, `json`, or `form` options are provided.
|
|
189
|
+
*/
|
|
190
|
+
get isReadonly(): boolean;
|
|
191
|
+
}
|
|
192
|
+
export {};
|