UNPKG

6.72 kBTypeScriptView Raw
1/// <reference types="node" />
2import { Duplex } from 'node:stream';
3import { URL } from 'node:url';
4import { ServerResponse } from 'node:http';
5import type { ClientRequest } from 'node:http';
6import type { Socket } from 'node:net';
7import CacheableRequest from 'cacheable-request';
8import type { Timings } from '@szmarczak/http-timer';
9import type ResponseLike from 'responselike';
10import Options from './options.js';
11import { Response } from './response.js';
12import { RequestError } from './errors.js';
13import type { PlainResponse } from './response.js';
14import type { NativeRequestOptions } from './options.js';
15declare type Error = NodeJS.ErrnoException;
16export interface Progress {
17 percent: number;
18 transferred: number;
19 total?: number;
20}
21export declare type GotEventFunction<T> =
22/**
23`request` event to get the request object of the request.
24
25 __Tip__: You can use `request` event to abort requests.
26
27@example
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```
58const response = await got('https://sindresorhus.com')
59 .on('downloadProgress', progress => {
60 // Report download progress
61 })
62 .on('uploadProgress', progress => {
63 // Report upload progress
64 });
65
66console.log(response);
67```
68*/
69 & ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
70/**
71To enable retrying on a Got stream, it is required to have a `retry` handler attached.
72
73When this event is emitted, you should reset the stream you were writing to and prepare the body again.
74
75See `got.options.retry` for more information.
76*/
77 & ((name: 'retry', listener: (retryCount: number, error: RequestError) => void) => T);
78export interface RequestEvents<T> {
79 on: GotEventFunction<T>;
80 once: GotEventFunction<T>;
81}
82export declare type CacheableRequestFunction = (options: string | URL | NativeRequestOptions, cb?: (response: ServerResponse | ResponseLike) => void) => CacheableRequest.Emitter;
83declare type UrlType = ConstructorParameters<typeof Options>[0];
84declare type OptionsType = ConstructorParameters<typeof Options>[1];
85declare type DefaultsType = ConstructorParameters<typeof Options>[2];
86export default class Request extends Duplex implements RequestEvents<Request> {
87 ['constructor']: typeof Request;
88 _noPipe?: boolean;
89 options: Options;
90 response?: PlainResponse;
91 requestUrl?: URL;
92 redirectUrls: URL[];
93 retryCount: number;
94 private _requestOptions;
95 private _stopRetry;
96 private _downloadedSize;
97 private _uploadedSize;
98 private _stopReading;
99 private readonly _pipedServerResponses;
100 private _request?;
101 private _responseSize?;
102 private _bodySize?;
103 private _unproxyEvents;
104 private _isFromCache?;
105 private _cannotHaveBody;
106 private _triggerRead;
107 private _jobs;
108 private _cancelTimeouts;
109 private _nativeResponse?;
110 private _flushed;
111 private _aborted;
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 _lockWrite;
125 private _unlockWrite;
126 private _finalizeBody;
127 private _onResponseBase;
128 private _setRawBody;
129 private _onResponse;
130 private _onRequest;
131 private _asyncWrite;
132 private _sendBody;
133 private _prepareCache;
134 private _createCacheableRequest;
135 private _makeRequest;
136 private _error;
137 private _writeRequest;
138 /**
139 The remote IP address.
140 */
141 get ip(): string | undefined;
142 /**
143 Indicates whether the request has been aborted or not.
144 */
145 get isAborted(): boolean;
146 get socket(): Socket | undefined;
147 /**
148 Progress event for downloading (receiving a response).
149 */
150 get downloadProgress(): Progress;
151 /**
152 Progress event for uploading (sending a request).
153 */
154 get uploadProgress(): Progress;
155 /**
156 The object contains the following properties:
157
158 - `start` - Time when the request started.
159 - `socket` - Time when a socket was assigned to the request.
160 - `lookup` - Time when the DNS lookup finished.
161 - `connect` - Time when the socket successfully connected.
162 - `secureConnect` - Time when the socket securely connected.
163 - `upload` - Time when the request finished uploading.
164 - `response` - Time when the request fired `response` event.
165 - `end` - Time when the response fired `end` event.
166 - `error` - Time when the request fired `error` event.
167 - `abort` - Time when the request fired `abort` event.
168 - `phases`
169 - `wait` - `timings.socket - timings.start`
170 - `dns` - `timings.lookup - timings.socket`
171 - `tcp` - `timings.connect - timings.lookup`
172 - `tls` - `timings.secureConnect - timings.connect`
173 - `request` - `timings.upload - (timings.secureConnect || timings.connect)`
174 - `firstByte` - `timings.response - timings.upload`
175 - `download` - `timings.end - timings.response`
176 - `total` - `(timings.end || timings.error || timings.abort) - timings.start`
177
178 If something has not been measured yet, it will be `undefined`.
179
180 __Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
181 */
182 get timings(): Timings | undefined;
183 /**
184 Whether the response was retrieved from the cache.
185 */
186 get isFromCache(): boolean | undefined;
187 get reusedSocket(): boolean | undefined;
188}
189export {};
190
\No newline at end of file