Skip to content

Commit ef233af

Browse files
committed
fixup
1 parent 5ad6238 commit ef233af

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

lib/handler/cache-handler.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
function noop () {}
1111

1212
/**
13-
* Writes a response to a CacheStore and then passes it on to the next handler
13+
* @implements {import('../../types/dispatcher.d.ts').default.DispatchHandler}
1414
*/
1515
class CacheHandler {
1616
/**
@@ -24,7 +24,7 @@ class CacheHandler {
2424
#store
2525

2626
/**
27-
* @type {import('../../types/dispatcher.d.ts').default.DispatchHandlers}
27+
* @type {import('../../types/dispatcher.d.ts').default.DispatchHandler}
2828
*/
2929
#handler
3030

@@ -36,7 +36,7 @@ class CacheHandler {
3636
/**
3737
* @param {import('../../types/cache-interceptor.d.ts').default.CacheHandlerOptions} opts
3838
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} cacheKey
39-
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler
39+
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
4040
*/
4141
constructor (opts, cacheKey, handler) {
4242
const { store } = opts
@@ -154,15 +154,10 @@ class CacheHandler {
154154
this.#handler.onResponseEnd?.(controller, trailers)
155155
}
156156

157-
/**
158-
* @see {DispatchHandlers.onError}
159-
*
160-
* @param {Error} err
161-
*/
162157
onResponseError (controller, err) {
163158
this.#writeStream?.destroy(err)
164159
this.#writeStream = undefined
165-
this.#handler?.onResponseError(controller, err)
160+
this.#handler.onResponseError?.(controller, err)
166161
}
167162
}
168163

lib/handler/cache-revalidation-handler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const assert = require('node:assert')
1313
*
1414
* @see https://www.rfc-editor.org/rfc/rfc9111.html#name-validation
1515
*
16-
* @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandlers} DispatchHandlers
16+
* @implements {import('../../types/dispatcher.d.ts').default.DispatchHandler}
1717
*/
1818
class CacheRevalidationHandler {
1919
#successful = false
@@ -22,15 +22,15 @@ class CacheRevalidationHandler {
2222
*/
2323
#callback
2424
/**
25-
* @type {(import('../../types/dispatcher.d.ts').default.DispatchHandlers)}
25+
* @type {(import('../../types/dispatcher.d.ts').default.DispatchHandler)}
2626
*/
2727
#handler
2828

2929
#context
3030

3131
/**
3232
* @param {(boolean, any) => void} callback Function to call if the cached value is valid
33-
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler
33+
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
3434
*/
3535
constructor (callback, handler) {
3636
if (typeof callback !== 'function') {

lib/interceptor/cache.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const CacheHandler = require('../handler/cache-handler')
77
const MemoryCacheStore = require('../cache/memory-cache-store')
88
const CacheRevalidationHandler = require('../handler/cache-revalidation-handler')
99
const { assertCacheStore, assertCacheMethods, makeCacheKey, parseCacheControlHeader } = require('../util/cache.js')
10+
const { AbortError } = require('../core/errors.js')
1011

1112
/**
12-
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler
13+
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandler} handler
1314
*/
1415
function sendGatewayTimeout (handler) {
1516
let aborted = false
@@ -171,7 +172,7 @@ module.exports = (opts = {}) => {
171172
return stream.errored
172173
},
173174
abort (reason) {
174-
stream.destroy(reason)
175+
stream.destroy(reason ?? new AbortError())
175176
}
176177
}
177178

types/dispatcher.d.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default Dispatcher
1515
/** Dispatcher is the core API used to dispatch requests. */
1616
declare class Dispatcher extends EventEmitter {
1717
/** 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. */
18-
dispatch (options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandlers): boolean
18+
dispatch (options: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandler): boolean
1919
/** Starts two-way communications with the requested resource. */
2020
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>): Promise<Dispatcher.ConnectData<TOpaque>>
2121
connect<TOpaque = null>(options: Dispatcher.ConnectOptions<TOpaque>, callback: (err: Error | null, data: Dispatcher.ConnectData<TOpaque>) => void): void
@@ -213,22 +213,46 @@ declare namespace Dispatcher {
213213
context: object;
214214
}
215215
export type StreamFactory<TOpaque = null> = (data: StreamFactoryData<TOpaque>) => Writable
216-
export interface DispatchHandlers {
216+
217+
export interface DispatchController {
218+
get aborted () : boolean
219+
get paused () : boolean
220+
get reason () : Error | null
221+
abort (reason: Error): void
222+
pause(): void
223+
resume(): void
224+
}
225+
226+
export interface DispatchHandler {
227+
onRequestStart?(controller: DispatchController, context: any)
228+
onResponseStart?(controller: DispatchController, statusCode: number, statusMessage?: string, headers: IncomingHttpHeaders)
229+
onResponseData?(controller: DispatchController, chunk: Buffer)
230+
onResponseEnd?(controller: DispatchController, trailers: IncomingHttpHeaders)
231+
onResponseError?(controller: DispatchController, error: Error)
232+
217233
/** 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. */
234+
/** @deprecated */
218235
onConnect?(abort: (err?: Error) => void): void;
219236
/** Invoked when an error has occurred. */
237+
/** @deprecated */
220238
onError?(err: Error): void;
221239
/** Invoked when request is upgraded either due to a `Upgrade` header or `CONNECT` method. */
240+
/** @deprecated */
222241
onUpgrade?(statusCode: number, headers: Buffer[] | string[] | null, socket: Duplex): void;
223242
/** Invoked when response is received, before headers have been read. **/
243+
/** @deprecated */
224244
onResponseStarted?(): void;
225245
/** Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. */
246+
/** @deprecated */
226247
onHeaders?(statusCode: number, headers: Buffer[], resume: () => void, statusText: string): boolean;
227248
/** Invoked when response payload data is received. */
249+
/** @deprecated */
228250
onData?(chunk: Buffer): boolean;
229251
/** Invoked when response payload and trailers have been received and the request has completed. */
252+
/** @deprecated */
230253
onComplete?(trailers: string[] | null): void;
231254
/** Invoked when a body chunk is sent to the server. May be invoked multiple times for chunked requests */
255+
/** @deprecated */
232256
onBodySent?(chunkSize: number, totalBytesSent: number): void;
233257
}
234258
export type PipelineHandler<TOpaque = null> = (data: PipelineHandlerData<TOpaque>) => Readable

0 commit comments

Comments
 (0)