UNPKG

6.29 kBTypeScriptView Raw
1import { Duplex } from 'node:stream';
2import { type ClientRequest } from 'node:http';
3import type { Socket } from 'node:net';
4import { type Timings } from '@szmarczak/http-timer';
5import Options from './options.js';
6import { type PlainResponse, type Response } from './response.js';
7import { RequestError } from './errors.js';
8type Error = NodeJS.ErrnoException;
9export type Progress = {
10 percent: number;
11 transferred: number;
12 total?: number;
13};
14export 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```
22import got from 'got';
23
24got.stream('https://github.com')
25 .on('request', request => setTimeout(() => request.destroy(), 50));
26```
27*/
28((name: 'request', listener: (request: ClientRequest) => void) => T)
29/**
30The `response` event to get the response object of the final request.
31*/
32 & (<R extends Response>(name: 'response', listener: (response: R) => void) => T)
33/**
34The `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/**
38Progress events for uploading (sending a request) and downloading (receiving a response).
39The `progress` argument is an object like:
40
41```
42{
43 percent: 0.1,
44 transferred: 1024,
45 total: 10240
46}
47```
48
49If the `content-length` header is missing, `total` will be `undefined`.
50
51@example
52```
53import got from 'got';
54
55const 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
63console.log(response);
64```
65*/
66 & ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
67/**
68To enable retrying on a Got stream, it is required to have a `retry` handler attached.
69
70When this event is emitted, you should reset the stream you were writing to and prepare the body again.
71
72See `got.options.retry` for more information.
73*/
74 & ((name: 'retry', listener: (retryCount: number, error: RequestError) => void) => T);
75export type RequestEvents<T> = {
76 on: GotEventFunction<T>;
77 once: GotEventFunction<T>;
78 off: GotEventFunction<T>;
79};
80type UrlType = ConstructorParameters<typeof Options>[0];
81type OptionsType = ConstructorParameters<typeof Options>[1];
82type DefaultsType = ConstructorParameters<typeof Options>[2];
83export 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 private _requestOptions;
92 private _stopRetry;
93 private _downloadedSize;
94 private _uploadedSize;
95 private _stopReading;
96 private readonly _pipedServerResponses;
97 private _request?;
98 private _responseSize?;
99 private _bodySize?;
100 private _unproxyEvents;
101 private _isFromCache?;
102 private _cannotHaveBody;
103 private _triggerRead;
104 private readonly _jobs;
105 private _cancelTimeouts;
106 private readonly _removeListeners;
107 private _nativeResponse?;
108 private _flushed;
109 private _aborted;
110 private _requestInitialized;
111 constructor(url: UrlType, options?: OptionsType, defaults?: DefaultsType);
112 flush(): Promise<void>;
113 _beforeError(error: Error): void;
114 _read(): void;
115 _write(chunk: unknown, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
116 _final(callback: (error?: Error | null) => void): void;
117 _destroy(error: Error | null, callback: (error: Error | null) => void): void;
118 pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
119 end?: boolean;
120 }): T;
121 unpipe<T extends NodeJS.WritableStream>(destination: T): this;
122 private _finalizeBody;
123 private _onResponseBase;
124 private _setRawBody;
125 private _onResponse;
126 private _onRequest;
127 private _asyncWrite;
128 private _sendBody;
129 private _prepareCache;
130 private _createCacheableRequest;
131 private _makeRequest;
132 private _error;
133 private _writeRequest;
134 /**
135 The remote IP address.
136 */
137 get ip(): string | undefined;
138 /**
139 Indicates whether the request has been aborted or not.
140 */
141 get isAborted(): boolean;
142 get socket(): Socket | undefined;
143 /**
144 Progress event for downloading (receiving a response).
145 */
146 get downloadProgress(): Progress;
147 /**
148 Progress event for uploading (sending a request).
149 */
150 get uploadProgress(): Progress;
151 /**
152 The object contains the following properties:
153
154 - `start` - Time when the request started.
155 - `socket` - Time when a socket was assigned to the request.
156 - `lookup` - Time when the DNS lookup finished.
157 - `connect` - Time when the socket successfully connected.
158 - `secureConnect` - Time when the socket securely connected.
159 - `upload` - Time when the request finished uploading.
160 - `response` - Time when the request fired `response` event.
161 - `end` - Time when the response fired `end` event.
162 - `error` - Time when the request fired `error` event.
163 - `abort` - Time when the request fired `abort` event.
164 - `phases`
165 - `wait` - `timings.socket - timings.start`
166 - `dns` - `timings.lookup - timings.socket`
167 - `tcp` - `timings.connect - timings.lookup`
168 - `tls` - `timings.secureConnect - timings.connect`
169 - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
170 - `firstByte` - `timings.response - timings.upload`
171 - `download` - `timings.end - timings.response`
172 - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
173
174 If something has not been measured yet, it will be `undefined`.
175
176 __Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
177 */
178 get timings(): Timings | undefined;
179 /**
180 Whether the response was retrieved from the cache.
181 */
182 get isFromCache(): boolean | undefined;
183 get reusedSocket(): boolean | undefined;
184}
185export {};
186
\No newline at end of file