UNPKG

6.57 kBTypeScriptView Raw
1/// <reference types="node" resolution-mode="require"/>
2/// <reference types="node" resolution-mode="require"/>
3/// <reference types="node" resolution-mode="require"/>
4/// <reference types="node" resolution-mode="require"/>
5/// <reference types="node" resolution-mode="require"/>
6import { Duplex } from 'node:stream';
7import { type ClientRequest } from 'node:http';
8import type { Socket } from 'node:net';
9import { type Timings } from '@szmarczak/http-timer';
10import Options from './options.js';
11import { type PlainResponse, type Response } from './response.js';
12import { RequestError } from './errors.js';
13type Error = NodeJS.ErrnoException;
14export type Progress = {
15 percent: number;
16 transferred: number;
17 total?: number;
18};
19export type GotEventFunction<T> =
20/**
21`request` event to get the request object of the request.
22
23 __Tip__: You can use `request` event to abort requests.
24
25@example
26```
27import got from 'got';
28
29got.stream('https://github.com')
30 .on('request', request => setTimeout(() => request.destroy(), 50));
31```
32*/
33((name: 'request', listener: (request: ClientRequest) => void) => T)
34/**
35The `response` event to get the response object of the final request.
36*/
37 & (<R extends Response>(name: 'response', listener: (response: R) => void) => T)
38/**
39The `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/**
43Progress events for uploading (sending a request) and downloading (receiving a response).
44The `progress` argument is an object like:
45
46```
47{
48 percent: 0.1,
49 transferred: 1024,
50 total: 10240
51}
52```
53
54If the `content-length` header is missing, `total` will be `undefined`.
55
56@example
57```
58import got from 'got';
59
60const response = await got('https://sindresorhus.com')
61 .on('downloadProgress', progress => {
62 // Report download progress
63 })
64 .on('uploadProgress', progress => {
65 // Report upload progress
66 });
67
68console.log(response);
69```
70*/
71 & ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
72/**
73To enable retrying on a Got stream, it is required to have a `retry` handler attached.
74
75When this event is emitted, you should reset the stream you were writing to and prepare the body again.
76
77See `got.options.retry` for more information.
78*/
79 & ((name: 'retry', listener: (retryCount: number, error: RequestError) => void) => T);
80export type RequestEvents<T> = {
81 on: GotEventFunction<T>;
82 once: GotEventFunction<T>;
83 off: GotEventFunction<T>;
84};
85type UrlType = ConstructorParameters<typeof Options>[0];
86type OptionsType = ConstructorParameters<typeof Options>[1];
87type DefaultsType = ConstructorParameters<typeof Options>[2];
88export 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 The remote IP address.
141 */
142 get ip(): string | undefined;
143 /**
144 Indicates whether the request has been aborted or not.
145 */
146 get isAborted(): boolean;
147 get socket(): Socket | undefined;
148 /**
149 Progress event for downloading (receiving a response).
150 */
151 get downloadProgress(): Progress;
152 /**
153 Progress event for uploading (sending a request).
154 */
155 get uploadProgress(): Progress;
156 /**
157 The object contains the following properties:
158
159 - `start` - Time when the request started.
160 - `socket` - Time when a socket was assigned to the request.
161 - `lookup` - Time when the DNS lookup finished.
162 - `connect` - Time when the socket successfully connected.
163 - `secureConnect` - Time when the socket securely connected.
164 - `upload` - Time when the request finished uploading.
165 - `response` - Time when the request fired `response` event.
166 - `end` - Time when the response fired `end` event.
167 - `error` - Time when the request fired `error` event.
168 - `abort` - Time when the request fired `abort` event.
169 - `phases`
170 - `wait` - `timings.socket - timings.start`
171 - `dns` - `timings.lookup - timings.socket`
172 - `tcp` - `timings.connect - timings.lookup`
173 - `tls` - `timings.secureConnect - timings.connect`
174 - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
175 - `firstByte` - `timings.response - timings.upload`
176 - `download` - `timings.end - timings.response`
177 - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
178
179 If something has not been measured yet, it will be `undefined`.
180
181 __Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
182 */
183 get timings(): Timings | undefined;
184 /**
185 Whether the response was retrieved from the cache.
186 */
187 get isFromCache(): boolean | undefined;
188 get reusedSocket(): boolean | undefined;
189}
190export {};
191
\No newline at end of file