dispatcher.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { URL } from 'url'
  2. import { Duplex, Readable, Writable } from 'stream'
  3. import { EventEmitter } from 'events'
  4. import { Blob } from 'buffer'
  5. import { IncomingHttpHeaders } from './header'
  6. import BodyReadable from './readable'
  7. import { FormData } from './formdata'
  8. import Errors from './errors'
  9. import { Autocomplete } from './utility'
  10. type AbortSignal = unknown
  11. export default Dispatcher
  12. export type UndiciHeaders = Record<string, string | string[]> | IncomingHttpHeaders | string[] | Iterable<[string, string | string[] | undefined]> | null
  13. /** Dispatcher is the core API used to dispatch requests. */
  14. declare class Dispatcher extends EventEmitter {
  15. /** 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. */
  16. dispatch (options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandler): boolean
  17. /** Starts two-way communications with the requested resource. */
  18. connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>): Promise<Dispatcher.ConnectData<TOpaque>>
  19. connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>, callback: (err: Error | null, data: Dispatcher.ConnectData<TOpaque>) => void): void
  20. /** Compose a chain of dispatchers */
  21. compose (dispatchers: Dispatcher.DispatcherComposeInterceptor[]): Dispatcher.ComposedDispatcher
  22. compose (...dispatchers: Dispatcher.DispatcherComposeInterceptor[]): Dispatcher.ComposedDispatcher
  23. /** Performs an HTTP request. */
  24. request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>): Promise<Dispatcher.ResponseData<TOpaque>>
  25. request<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, callback: (err: Error | null, data: Dispatcher.ResponseData<TOpaque>) => void): void
  26. /** For easy use with `stream.pipeline`. */
  27. pipeline<TOpaque = null>(options: Dispatcher.PipelineOptions<TOpaque>, handler: Dispatcher.PipelineHandler<TOpaque>): Duplex
  28. /** A faster version of `Dispatcher.request`. */
  29. stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>): Promise<Dispatcher.StreamData<TOpaque>>
  30. stream<TOpaque = null>(options: Dispatcher.RequestOptions<TOpaque>, factory: Dispatcher.StreamFactory<TOpaque>, callback: (err: Error | null, data: Dispatcher.StreamData<TOpaque>) => void): void
  31. /** Upgrade to a different protocol. */
  32. upgrade (options: Dispatcher.UpgradeOptions): Promise<Dispatcher.UpgradeData>
  33. upgrade (options: Dispatcher.UpgradeOptions, callback: (err: Error | null, data: Dispatcher.UpgradeData) => void): void
  34. /** Closes the client and gracefully waits for enqueued requests to complete before invoking the callback (or returning a promise if no callback is provided). */
  35. close (): Promise<void>
  36. close (callback: () => void): void
  37. /** 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. */
  38. destroy (): Promise<void>
  39. destroy (err: Error | null): Promise<void>
  40. destroy (callback: () => void): void
  41. destroy (err: Error | null, callback: () => void): void
  42. on (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
  43. on (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  44. on (eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  45. on (eventName: 'drain', callback: (origin: URL) => void): this
  46. once (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
  47. once (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  48. once (eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  49. once (eventName: 'drain', callback: (origin: URL) => void): this
  50. off (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
  51. off (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  52. off (eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  53. off (eventName: 'drain', callback: (origin: URL) => void): this
  54. addListener (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
  55. addListener (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  56. addListener (eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  57. addListener (eventName: 'drain', callback: (origin: URL) => void): this
  58. removeListener (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
  59. removeListener (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  60. removeListener (eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  61. removeListener (eventName: 'drain', callback: (origin: URL) => void): this
  62. prependListener (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
  63. prependListener (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  64. prependListener (eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  65. prependListener (eventName: 'drain', callback: (origin: URL) => void): this
  66. prependOnceListener (eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this
  67. prependOnceListener (eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  68. prependOnceListener (eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this
  69. prependOnceListener (eventName: 'drain', callback: (origin: URL) => void): this
  70. listeners (eventName: 'connect'): ((origin: URL, targets: readonly Dispatcher[]) => void)[]
  71. listeners (eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[]
  72. listeners (eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[]
  73. listeners (eventName: 'drain'): ((origin: URL) => void)[]
  74. rawListeners (eventName: 'connect'): ((origin: URL, targets: readonly Dispatcher[]) => void)[]
  75. rawListeners (eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[]
  76. rawListeners (eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[]
  77. rawListeners (eventName: 'drain'): ((origin: URL) => void)[]
  78. emit (eventName: 'connect', origin: URL, targets: readonly Dispatcher[]): boolean
  79. emit (eventName: 'disconnect', origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): boolean
  80. emit (eventName: 'connectionError', origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): boolean
  81. emit (eventName: 'drain', origin: URL): boolean
  82. }
  83. declare namespace Dispatcher {
  84. export interface ComposedDispatcher extends Dispatcher {}
  85. export type Dispatch = Dispatcher['dispatch']
  86. export type DispatcherComposeInterceptor = (dispatch: Dispatch) => Dispatch
  87. export interface DispatchOptions {
  88. origin?: string | URL;
  89. path: string;
  90. method: HttpMethod;
  91. /** Default: `null` */
  92. body?: string | Buffer | Uint8Array | Readable | null | FormData;
  93. /** Default: `null` */
  94. headers?: UndiciHeaders;
  95. /** Query string params to be embedded in the request URL. Default: `null` */
  96. query?: Record<string, any>;
  97. /** 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`. */
  98. idempotent?: boolean;
  99. /** 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. Defaults to `method !== 'HEAD'`. */
  100. blocking?: boolean;
  101. /** Upgrade the request. Should be used to specify the kind of upgrade i.e. `'Websocket'`. Default: `method === 'CONNECT' || null`. */
  102. upgrade?: boolean | string | null;
  103. /** The amount of time, in milliseconds, the parser will wait to receive the complete HTTP headers. Defaults to 300 seconds. */
  104. headersTimeout?: number | null;
  105. /** 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. */
  106. bodyTimeout?: number | null;
  107. /** Whether the request should stablish a keep-alive or not. Default `false` */
  108. reset?: boolean;
  109. /** Whether Undici should throw an error upon receiving a 4xx or 5xx response from the server. Defaults to false */
  110. throwOnError?: boolean;
  111. /** For H2, it appends the expect: 100-continue header, and halts the request body until a 100-continue is received from the remote server */
  112. expectContinue?: boolean;
  113. }
  114. export interface ConnectOptions<TOpaque = null> {
  115. origin: string | URL;
  116. path: string;
  117. /** Default: `null` */
  118. headers?: UndiciHeaders;
  119. /** Default: `null` */
  120. signal?: AbortSignal | EventEmitter | null;
  121. /** This argument parameter is passed through to `ConnectData` */
  122. opaque?: TOpaque;
  123. /** Default: false */
  124. redirectionLimitReached?: boolean;
  125. /** Default: `null` */
  126. responseHeaders?: 'raw' | null;
  127. }
  128. export interface RequestOptions<TOpaque = null> extends DispatchOptions {
  129. /** Default: `null` */
  130. opaque?: TOpaque;
  131. /** Default: `null` */
  132. signal?: AbortSignal | EventEmitter | null;
  133. /** Default: false */
  134. redirectionLimitReached?: boolean;
  135. /** Default: `null` */
  136. onInfo?: (info: { statusCode: number, headers: Record<string, string | string[]> }) => void;
  137. /** Default: `null` */
  138. responseHeaders?: 'raw' | null;
  139. /** Default: `64 KiB` */
  140. highWaterMark?: number;
  141. }
  142. export interface PipelineOptions<TOpaque = null> extends RequestOptions<TOpaque> {
  143. /** `true` if the `handler` will return an object stream. Default: `false` */
  144. objectMode?: boolean;
  145. }
  146. export interface UpgradeOptions {
  147. path: string;
  148. /** Default: `'GET'` */
  149. method?: string;
  150. /** Default: `null` */
  151. headers?: UndiciHeaders;
  152. /** A string of comma separated protocols, in descending preference order. Default: `'Websocket'` */
  153. protocol?: string;
  154. /** Default: `null` */
  155. signal?: AbortSignal | EventEmitter | null;
  156. /** Default: false */
  157. redirectionLimitReached?: boolean;
  158. /** Default: `null` */
  159. responseHeaders?: 'raw' | null;
  160. }
  161. export interface ConnectData<TOpaque = null> {
  162. statusCode: number;
  163. headers: IncomingHttpHeaders;
  164. socket: Duplex;
  165. opaque: TOpaque;
  166. }
  167. export interface ResponseData<TOpaque = null> {
  168. statusCode: number;
  169. headers: IncomingHttpHeaders;
  170. body: BodyReadable & BodyMixin;
  171. trailers: Record<string, string>;
  172. opaque: TOpaque;
  173. context: object;
  174. }
  175. export interface PipelineHandlerData<TOpaque = null> {
  176. statusCode: number;
  177. headers: IncomingHttpHeaders;
  178. opaque: TOpaque;
  179. body: BodyReadable;
  180. context: object;
  181. }
  182. export interface StreamData<TOpaque = null> {
  183. opaque: TOpaque;
  184. trailers: Record<string, string>;
  185. }
  186. export interface UpgradeData<TOpaque = null> {
  187. headers: IncomingHttpHeaders;
  188. socket: Duplex;
  189. opaque: TOpaque;
  190. }
  191. export interface StreamFactoryData<TOpaque = null> {
  192. statusCode: number;
  193. headers: IncomingHttpHeaders;
  194. opaque: TOpaque;
  195. context: object;
  196. }
  197. export type StreamFactory<TOpaque = null> = (data: StreamFactoryData<TOpaque>) => Writable
  198. export interface DispatchController {
  199. get aborted () : boolean
  200. get paused () : boolean
  201. get reason () : Error | null
  202. abort (reason: Error): void
  203. pause(): void
  204. resume(): void
  205. }
  206. export interface DispatchHandler {
  207. onRequestStart?(controller: DispatchController, context: any): void;
  208. onRequestUpgrade?(controller: DispatchController, statusCode: number, headers: IncomingHttpHeaders, socket: Duplex): void;
  209. onResponseStart?(controller: DispatchController, statusCode: number, headers: IncomingHttpHeaders, statusMessage?: string): void;
  210. onResponseData?(controller: DispatchController, chunk: Buffer): void;
  211. onResponseEnd?(controller: DispatchController, trailers: IncomingHttpHeaders): void;
  212. onResponseError?(controller: DispatchController, error: Error): void;
  213. /** 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. */
  214. /** @deprecated */
  215. onConnect?(abort: (err?: Error) => void): void;
  216. /** Invoked when an error has occurred. */
  217. /** @deprecated */
  218. onError?(err: Error): void;
  219. /** Invoked when request is upgraded either due to a `Upgrade` header or `CONNECT` method. */
  220. /** @deprecated */
  221. onUpgrade?(statusCode: number, headers: Buffer[] | string[] | null, socket: Duplex): void;
  222. /** Invoked when response is received, before headers have been read. **/
  223. /** @deprecated */
  224. onResponseStarted?(): void;
  225. /** Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. */
  226. /** @deprecated */
  227. onHeaders?(statusCode: number, headers: Buffer[], resume: () => void, statusText: string): boolean;
  228. /** Invoked when response payload data is received. */
  229. /** @deprecated */
  230. onData?(chunk: Buffer): boolean;
  231. /** Invoked when response payload and trailers have been received and the request has completed. */
  232. /** @deprecated */
  233. onComplete?(trailers: string[] | null): void;
  234. /** Invoked when a body chunk is sent to the server. May be invoked multiple times for chunked requests */
  235. /** @deprecated */
  236. onBodySent?(chunkSize: number, totalBytesSent: number): void;
  237. }
  238. export type PipelineHandler<TOpaque = null> = (data: PipelineHandlerData<TOpaque>) => Readable
  239. export type HttpMethod = Autocomplete<'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'>
  240. /**
  241. * @link https://fetch.spec.whatwg.org/#body-mixin
  242. */
  243. interface BodyMixin {
  244. readonly body?: never;
  245. readonly bodyUsed: boolean;
  246. arrayBuffer(): Promise<ArrayBuffer>;
  247. blob(): Promise<Blob>;
  248. bytes(): Promise<Uint8Array>;
  249. formData(): Promise<never>;
  250. json(): Promise<unknown>;
  251. text(): Promise<string>;
  252. }
  253. export interface DispatchInterceptor {
  254. (dispatch: Dispatch): Dispatch
  255. }
  256. }