1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import { Duplex } from 'node:stream';
|
7 | import { type ClientRequest } from 'node:http';
|
8 | import type { Socket } from 'node:net';
|
9 | import { type Timings } from '@szmarczak/http-timer';
|
10 | import Options from './options.js';
|
11 | import { type PlainResponse, type Response } from './response.js';
|
12 | import { RequestError } from './errors.js';
|
13 | type Error = NodeJS.ErrnoException;
|
14 | export type Progress = {
|
15 | percent: number;
|
16 | transferred: number;
|
17 | total?: number;
|
18 | };
|
19 | export type GotEventFunction<T> =
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | ((name: 'request', listener: (request: ClientRequest) => void) => T)
|
34 |
|
35 |
|
36 |
|
37 | & (<R extends Response>(name: 'response', listener: (response: R) => void) => T)
|
38 | /**
|
39 | The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
|
40 | */
|
41 | & (<R extends Response, N extends Options>(name: 'redirect', listener: (response: R, nextOptions: N) => void) => T)
|
42 | /**
|
43 | Progress events for uploading (sending a request) and downloading (receiving a response).
|
44 | The `progress` argument is an object like:
|
45 |
|
46 | ```
|
47 | {
|
48 | percent: 0.1,
|
49 | transferred: 1024,
|
50 | total: 10240
|
51 | }
|
52 | ```
|
53 |
|
54 | If the `content-length` header is missing, `total` will be `undefined`.
|
55 |
|
56 | @example
|
57 | ```
|
58 | import got from 'got';
|
59 |
|
60 | const response = await got('https:
|
61 | .on('downloadProgress', progress => {
|
62 |
|
63 | })
|
64 | .on('uploadProgress', progress => {
|
65 |
|
66 | });
|
67 |
|
68 | console.log(response);
|
69 | ```
|
70 | */
|
71 | & ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | & ((name: 'retry', listener: (retryCount: number, error: RequestError) => void) => T);
|
80 | export type RequestEvents<T> = {
|
81 | on: GotEventFunction<T>;
|
82 | once: GotEventFunction<T>;
|
83 | off: GotEventFunction<T>;
|
84 | };
|
85 | type UrlType = ConstructorParameters<typeof Options>[0];
|
86 | type OptionsType = ConstructorParameters<typeof Options>[1];
|
87 | type DefaultsType = ConstructorParameters<typeof Options>[2];
|
88 | export default class Request extends Duplex implements RequestEvents<Request> {
|
89 | ['constructor']: typeof Request;
|
90 | _noPipe?: boolean;
|
91 | options: Options;
|
92 | response?: PlainResponse;
|
93 | requestUrl?: URL;
|
94 | redirectUrls: URL[];
|
95 | retryCount: number;
|
96 | private _requestOptions;
|
97 | private _stopRetry;
|
98 | private _downloadedSize;
|
99 | private _uploadedSize;
|
100 | private _stopReading;
|
101 | private readonly _pipedServerResponses;
|
102 | private _request?;
|
103 | private _responseSize?;
|
104 | private _bodySize?;
|
105 | private _unproxyEvents;
|
106 | private _isFromCache?;
|
107 | private _cannotHaveBody;
|
108 | private _triggerRead;
|
109 | private readonly _jobs;
|
110 | private _cancelTimeouts;
|
111 | private readonly _removeListeners;
|
112 | private _nativeResponse?;
|
113 | private _flushed;
|
114 | private _aborted;
|
115 | private _requestInitialized;
|
116 | constructor(url: UrlType, options?: OptionsType, defaults?: DefaultsType);
|
117 | flush(): Promise<void>;
|
118 | _beforeError(error: Error): void;
|
119 | _read(): void;
|
120 | _write(chunk: unknown, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
|
121 | _final(callback: (error?: Error | null) => void): void;
|
122 | _destroy(error: Error | null, callback: (error: Error | null) => void): void;
|
123 | pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
|
124 | end?: boolean;
|
125 | }): T;
|
126 | unpipe<T extends NodeJS.WritableStream>(destination: T): this;
|
127 | private _finalizeBody;
|
128 | private _onResponseBase;
|
129 | private _setRawBody;
|
130 | private _onResponse;
|
131 | private _onRequest;
|
132 | private _asyncWrite;
|
133 | private _sendBody;
|
134 | private _prepareCache;
|
135 | private _createCacheableRequest;
|
136 | private _makeRequest;
|
137 | private _error;
|
138 | private _writeRequest;
|
139 | |
140 |
|
141 |
|
142 | get ip(): string | undefined;
|
143 | |
144 |
|
145 |
|
146 | get isAborted(): boolean;
|
147 | get socket(): Socket | undefined;
|
148 | |
149 |
|
150 |
|
151 | get downloadProgress(): Progress;
|
152 | |
153 |
|
154 |
|
155 | get uploadProgress(): Progress;
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | get timings(): Timings | undefined;
|
184 | |
185 |
|
186 |
|
187 | get isFromCache(): boolean | undefined;
|
188 | get reusedSocket(): boolean | undefined;
|
189 | }
|
190 | export {};
|
191 |
|
\ | No newline at end of file |