source: node_modules/undici/types/dispatcher.d.ts

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 13.1 KB
Line 
1import { URL } from 'url'
2import { Duplex, Readable, Writable } from 'stream'
3import { EventEmitter } from 'events'
4import { Blob } from 'buffer'
5import { IncomingHttpHeaders } from './header'
6import BodyReadable from './readable'
7import { FormData } from './formdata'
8import Errors from './errors'
9
10type AbortSignal = unknown;
11
12export default Dispatcher
13
14/** Dispatcher is the core API used to dispatch requests. */
15declare class Dispatcher extends EventEmitter {
16 /** Dispatches a request. This API is expected to evolve through semver-major versions and is less stable than the preceding higher level APIs. It is primarily intended for library developers who implement higher level APIs on top of this. */
17 dispatch(options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandlers): boolean;
18 /** Starts two-way communications with the requested resource. */
19 connect(options: Dispatcher.ConnectOptions): Promise<Dispatcher.ConnectData>;
20 connect(options: Dispatcher.ConnectOptions, callback: (err: Error | null, data: Dispatcher.ConnectData) => void): void;
21 /** Performs an HTTP request. */
22 request(options: Dispatcher.RequestOptions): Promise<Dispatcher.ResponseData>;
23 request(options: Dispatcher.RequestOptions, callback: (err: Error | null, data: Dispatcher.ResponseData) => void): void;
24 /** For easy use with `stream.pipeline`. */
25 pipeline(options: Dispatcher.PipelineOptions, handler: Dispatcher.PipelineHandler): Duplex;
26 /** A faster version of `Dispatcher.request`. */
27 stream(options: Dispatcher.RequestOptions, factory: Dispatcher.StreamFactory): Promise<Dispatcher.StreamData>;
28 stream(options: Dispatcher.RequestOptions, factory: Dispatcher.StreamFactory, callback: (err: Error | null, data: Dispatcher.StreamData) => void): void;
29 /** Upgrade to a different protocol. */
30 upgrade(options: Dispatcher.UpgradeOptions): Promise<Dispatcher.UpgradeData>;
31 upgrade(options: Dispatcher.UpgradeOptions, callback: (err: Error | null, data: Dispatcher.UpgradeData) => void): void;
32 /** Closes the client and gracefully waits for enqueued requests to complete before invoking the callback (or returning a promise if no callback is provided). */
33 close(): Promise<void>;
34 close(callback: () => void): void;
35 /** Destroy the client abruptly with the given err. All the pending and running requests will be asynchronously aborted and error. Waits until socket is closed before invoking the callback (or returning a promise if no callback is provided). Since this operation is asynchronously dispatched there might still be some progress on dispatched requests. */
36 destroy(): Promise<void>;
37 destroy(err: Error | null): Promise<void>;
38 destroy(callback: () => void): void;
39 destroy(err: Error | null, callback: () => void): void;
40
41 on(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
42 on(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
43 on(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
44 on(eventName: 'drain', callback: (origin: URL) => void): this;
45
46
47 once(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
48 once(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
49 once(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
50 once(eventName: 'drain', callback: (origin: URL) => void): this;
51
52
53 off(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
54 off(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
55 off(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
56 off(eventName: 'drain', callback: (origin: URL) => void): this;
57
58
59 addListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
60 addListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
61 addListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
62 addListener(eventName: 'drain', callback: (origin: URL) => void): this;
63
64 removeListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
65 removeListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
66 removeListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
67 removeListener(eventName: 'drain', callback: (origin: URL) => void): this;
68
69 prependListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
70 prependListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
71 prependListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
72 prependListener(eventName: 'drain', callback: (origin: URL) => void): this;
73
74 prependOnceListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
75 prependOnceListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
76 prependOnceListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
77 prependOnceListener(eventName: 'drain', callback: (origin: URL) => void): this;
78
79 listeners(eventName: 'connect'): ((origin: URL, targets: readonly Dispatcher[]) => void)[]
80 listeners(eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
81 listeners(eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
82 listeners(eventName: 'drain'): ((origin: URL) => void)[];
83
84 rawListeners(eventName: 'connect'): ((origin: URL, targets: readonly Dispatcher[]) => void)[]
85 rawListeners(eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
86 rawListeners(eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
87 rawListeners(eventName: 'drain'): ((origin: URL) => void)[];
88
89 emit(eventName: 'connect', origin: URL, targets: readonly Dispatcher[]): boolean;
90 emit(eventName: 'disconnect', origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): boolean;
91 emit(eventName: 'connectionError', origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): boolean;
92 emit(eventName: 'drain', origin: URL): boolean;
93}
94
95declare namespace Dispatcher {
96 export interface DispatchOptions {
97 origin?: string | URL;
98 path: string;
99 method: HttpMethod;
100 /** Default: `null` */
101 body?: string | Buffer | Uint8Array | Readable | null | FormData;
102 /** Default: `null` */
103 headers?: IncomingHttpHeaders | string[] | null;
104 /** Query string params to be embedded in the request URL. Default: `null` */
105 query?: Record<string, any>;
106 /** Whether the requests can be safely retried or not. If `false` the request won't be sent until all preceding requests in the pipeline have completed. Default: `true` if `method` is `HEAD` or `GET`. */
107 idempotent?: boolean;
108 /** Whether the response is expected to take a long time and would end up blocking the pipeline. When this is set to `true` further pipelining will be avoided on the same connection until headers have been received. */
109 blocking?: boolean;
110 /** Upgrade the request. Should be used to specify the kind of upgrade i.e. `'Websocket'`. Default: `method === 'CONNECT' || null`. */
111 upgrade?: boolean | string | null;
112 /** The amount of time, in milliseconds, the parser will wait to receive the complete HTTP headers. Defaults to 300 seconds. */
113 headersTimeout?: number | null;
114 /** The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use 0 to disable it entirely. Defaults to 300 seconds. */
115 bodyTimeout?: number | null;
116 /** Whether the request should stablish a keep-alive or not. Default `false` */
117 reset?: boolean;
118 /** Whether Undici should throw an error upon receiving a 4xx or 5xx response from the server. Defaults to false */
119 throwOnError?: boolean;
120 /** For H2, it appends the expect: 100-continue header, and halts the request body until a 100-continue is received from the remote server*/
121 expectContinue?: boolean;
122 }
123 export interface ConnectOptions {
124 path: string;
125 /** Default: `null` */
126 headers?: IncomingHttpHeaders | string[] | null;
127 /** Default: `null` */
128 signal?: AbortSignal | EventEmitter | null;
129 /** This argument parameter is passed through to `ConnectData` */
130 opaque?: unknown;
131 /** Default: 0 */
132 maxRedirections?: number;
133 /** Default: `null` */
134 responseHeader?: 'raw' | null;
135 }
136 export interface RequestOptions extends DispatchOptions {
137 /** Default: `null` */
138 opaque?: unknown;
139 /** Default: `null` */
140 signal?: AbortSignal | EventEmitter | null;
141 /** Default: 0 */
142 maxRedirections?: number;
143 /** Default: `null` */
144 onInfo?: (info: { statusCode: number, headers: Record<string, string | string[]> }) => void;
145 /** Default: `null` */
146 responseHeader?: 'raw' | null;
147 /** Default: `64 KiB` */
148 highWaterMark?: number;
149 }
150 export interface PipelineOptions extends RequestOptions {
151 /** `true` if the `handler` will return an object stream. Default: `false` */
152 objectMode?: boolean;
153 }
154 export interface UpgradeOptions {
155 path: string;
156 /** Default: `'GET'` */
157 method?: string;
158 /** Default: `null` */
159 headers?: IncomingHttpHeaders | string[] | null;
160 /** A string of comma separated protocols, in descending preference order. Default: `'Websocket'` */
161 protocol?: string;
162 /** Default: `null` */
163 signal?: AbortSignal | EventEmitter | null;
164 /** Default: 0 */
165 maxRedirections?: number;
166 /** Default: `null` */
167 responseHeader?: 'raw' | null;
168 }
169 export interface ConnectData {
170 statusCode: number;
171 headers: IncomingHttpHeaders;
172 socket: Duplex;
173 opaque: unknown;
174 }
175 export interface ResponseData {
176 statusCode: number;
177 headers: IncomingHttpHeaders;
178 body: BodyReadable & BodyMixin;
179 trailers: Record<string, string>;
180 opaque: unknown;
181 context: object;
182 }
183 export interface PipelineHandlerData {
184 statusCode: number;
185 headers: IncomingHttpHeaders;
186 opaque: unknown;
187 body: BodyReadable;
188 context: object;
189 }
190 export interface StreamData {
191 opaque: unknown;
192 trailers: Record<string, string>;
193 }
194 export interface UpgradeData {
195 headers: IncomingHttpHeaders;
196 socket: Duplex;
197 opaque: unknown;
198 }
199 export interface StreamFactoryData {
200 statusCode: number;
201 headers: IncomingHttpHeaders;
202 opaque: unknown;
203 context: object;
204 }
205 export type StreamFactory = (data: StreamFactoryData) => Writable;
206 export interface DispatchHandlers {
207 /** Invoked before request is dispatched on socket. May be invoked multiple times when a request is retried when the request at the head of the pipeline fails. */
208 onConnect?(abort: () => void): void;
209 /** Invoked when an error has occurred. */
210 onError?(err: Error): void;
211 /** Invoked when request is upgraded either due to a `Upgrade` header or `CONNECT` method. */
212 onUpgrade?(statusCode: number, headers: Buffer[] | string[] | null, socket: Duplex): void;
213 /** Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. */
214 onHeaders?(statusCode: number, headers: Buffer[] | string[] | null, resume: () => void, statusText: string): boolean;
215 /** Invoked when response payload data is received. */
216 onData?(chunk: Buffer): boolean;
217 /** Invoked when response payload and trailers have been received and the request has completed. */
218 onComplete?(trailers: string[] | null): void;
219 /** Invoked when a body chunk is sent to the server. May be invoked multiple times for chunked requests */
220 onBodySent?(chunkSize: number, totalBytesSent: number): void;
221 }
222 export type PipelineHandler = (data: PipelineHandlerData) => Readable;
223 export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
224
225 /**
226 * @link https://fetch.spec.whatwg.org/#body-mixin
227 */
228 interface BodyMixin {
229 readonly body?: never; // throws on node v16.6.0
230 readonly bodyUsed: boolean;
231 arrayBuffer(): Promise<ArrayBuffer>;
232 blob(): Promise<Blob>;
233 formData(): Promise<never>;
234 json(): Promise<unknown>;
235 text(): Promise<string>;
236 }
237
238 export interface DispatchInterceptor {
239 (dispatch: Dispatcher['dispatch']): Dispatcher['dispatch']
240 }
241}
Note: See TracBrowser for help on using the repository browser.