From 20b235ccc8c0003ecebd468b33f1509ab6c2bd97 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 25 Nov 2024 10:48:19 +0100 Subject: [PATCH] fixup --- docs/docs/api/Dispatcher.md | 14 ++++++-------- lib/handler/unwrap-handler.js | 2 +- lib/handler/wrap-handler.js | 2 +- types/dispatcher.d.ts | 1 + 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/docs/docs/api/Dispatcher.md b/docs/docs/api/Dispatcher.md index b85008eb14c..7f4ddab2be4 100644 --- a/docs/docs/api/Dispatcher.md +++ b/docs/docs/api/Dispatcher.md @@ -205,14 +205,12 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo #### Parameter: `DispatchHandler` -* **onConnect** `(abort: () => void, context: object) => void` - 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. -* **onError** `(error: Error) => void` - Invoked when an error has occurred. May not throw. -* **onUpgrade** `(statusCode: number, headers: Buffer[], socket: Duplex) => void` (optional) - Invoked when request is upgraded. Required if `DispatchOptions.upgrade` is defined or `DispatchOptions.method === 'CONNECT'`. -* **onResponseStarted** `() => void` (optional) - Invoked when response is received, before headers have been read. -* **onHeaders** `(statusCode: number, headers: Buffer[], resume: () => void, statusText: string) => boolean` - Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. Not required for `upgrade` requests. -* **onData** `(chunk: Buffer) => boolean` - Invoked when response payload data is received. Not required for `upgrade` requests. -* **onComplete** `(trailers: Buffer[]) => void` - Invoked when response payload and trailers have been received and the request has completed. Not required for `upgrade` requests. -* **onBodySent** `(chunk: string | Buffer | Uint8Array) => void` - Invoked when a body chunk is sent to the server. Not required. For a stream or iterable body this will be invoked for every chunk. For other body types, it will be invoked once after the body is sent. +* **onRequestStart** `(controller: DispatchController, context: object) => void` - 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. +* **onRequestUpgrade** `(controller: DispatchController, statusCode: number, headers: Record, socket: Duplex) => void` (optional) - Invoked when request is upgraded. Required if `DispatchOptions.upgrade` is defined or `DispatchOptions.method === 'CONNECT'`. +* **onResponseStart** `(controller: DispatchController, statusCode: number, statusMessage?: string, headers: Record) => boolean` - Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. Not required for `upgrade` requests. +* **onResponseData** `(controller: DispatchController, chunk: Buffer) => boolean` - Invoked when response payload data is received. Not required for `upgrade` requests. +* **onResponseEnd** `(controller: DispatchController, trailers: Record) => void` - Invoked when response payload and trailers have been received and the request has completed. Not required for `upgrade` requests. +* **onResponseError** `(error: Error) => void` - Invoked when an error has occurred. May not throw. #### Example 1 - Dispatch GET request diff --git a/lib/handler/unwrap-handler.js b/lib/handler/unwrap-handler.js index 4b5d87a5d7d..3b38ac264d0 100644 --- a/lib/handler/unwrap-handler.js +++ b/lib/handler/unwrap-handler.js @@ -68,7 +68,7 @@ module.exports = class UnwrapHandler { } onUpgrade (statusCode, rawHeaders, socket) { - this.#handler.onRequestUpgrade?.(statusCode, parseHeaders(rawHeaders), socket) + this.#handler.onRequestUpgrade?.(this.#controller, statusCode, parseHeaders(rawHeaders), socket) } onHeaders (statusCode, rawHeaders, resume, statusMessage) { diff --git a/lib/handler/wrap-handler.js b/lib/handler/wrap-handler.js index baa202dc479..3bf49f1be16 100644 --- a/lib/handler/wrap-handler.js +++ b/lib/handler/wrap-handler.js @@ -50,7 +50,7 @@ module.exports = class WrapHandler { this.#handler.onConnect?.((reason) => controller.abort(reason), context) } - onRequestUpgrade (statusCode, headers, socket) { + onRequestUpgrade (controller, statusCode, headers, socket) { const rawHeaders = [] for (const [key, val] of Object.entries(headers)) { // TODO (fix): What if val is Array diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 06d2d5d8651..f441d8f8a30 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -225,6 +225,7 @@ declare namespace Dispatcher { export interface DispatchHandler { onRequestStart?(controller: DispatchController, context: any) + onRequestUpgrade?(controller: DispatchController, statusCode: number, headers: IncomingHttpHeaders, socket: Duplex): void; onResponseStart?(controller: DispatchController, statusCode: number, statusMessage?: string, headers: IncomingHttpHeaders) onResponseData?(controller: DispatchController, chunk: Buffer) onResponseEnd?(controller: DispatchController, trailers: IncomingHttpHeaders)