Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Sep 22, 2024
1 parent 33172c3 commit b9cd97c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
32 changes: 21 additions & 11 deletions lib/handler/decorator-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,31 @@ module.exports = class DecoratorHandler {

onRequestStart (reserved, abort) {
if (this.#handler.onRequestStart) {
this.#handler.onRequestStart(null, abort)
this.#handler.onRequestStart(reserved, abort)
}

if (this.#handler.onConnect) {
this.#handler.onConnect(abort)
}
}

onResponseStart (resume) {
this.#resume = resume

if (this.#handler.onResponseStart) {
return this.#handler.onResponseStart(resume)
}

this.#resume = resume

return true
}

onResponseHeaders (statusCode, headers) {
onResponseHeaders (headers, statusCode) {
if (this.#handler.onResponseHeaders) {
this.#handler.onResponseHeaders(headers, statusCode)
}

if (this.#handler.onConnect) {
this.#handler.onConnect(statusCode, toRawHeaders(headers), this.#resume)
if (this.#handler.onHeaders) {
this.#handler.onHeaders(statusCode, toRawHeaders(headers), this.#resume)
}

return true
Expand Down Expand Up @@ -136,21 +140,27 @@ module.exports = class DecoratorHandler {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

let paused
let ret

if (this.#handler.onResponseStart) {
paused ||= this.#handler.onResponseStart(args[2]) === false
if (this.#handler.onResponseStart(args[2]) === false) {
// TODO (fix): Strictly speaking we should not call onRepsonseHeaders
// after this...
ret = false
}
}

if (this.#handler.onResponseHeaders) {
paused ||= this.#handler.onResponseHeaders(util.parseHeaders(args[1]), args[0]) === false
if (this.#handler.onResponseHeaders(util.parseHeaders(args[1]), args[0]) === false) {
ret = false
}
}

if (this.#handler.onHeaders) {
paused ||= this.#handler.onHeaders(...args) === false
ret ??= this.#handler.onHeaders(...args)
}

return paused
return ret
}

onData (...args) {
Expand Down
26 changes: 26 additions & 0 deletions test/decorator-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ describe('DecoratorHandler', () => {
t.equal(typeof decorator[method], 'function')
})

test(`should delegate ${method}-method`, (t) => {
t = tspl(t, { plan: 1 })
const handler = { [method]: () => method }
const decorator = new DecoratorHandler(handler)
t.equal(decorator[method](), method)
})

test(`should delegate ${method}-method with arguments`, (t) => {
t = tspl(t, { plan: 1 })
const handler = { [method]: (...args) => args }
const decorator = new DecoratorHandler(handler)
t.deepStrictEqual(decorator[method](1, 2, 3), [1, 2, 3])
})

test(`can be extended and should delegate ${method}-method`, (t) => {
t = tspl(t, { plan: 1 })

class ExtendedHandler extends DecoratorHandler {
[method] () {
return method
}
}
const decorator = new ExtendedHandler({})
t.equal(decorator[method](), method)
})

test(`calling the method ${method}-method should not throw if the method is not defined in the handler`, (t) => {
t = tspl(t, { plan: 1 })
const decorator = new DecoratorHandler({})
Expand Down
3 changes: 1 addition & 2 deletions test/issue-2065.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ test('undici.request with a FormData body should set content-length header', asy
const body = new FormData()
body.set('file', new File(['abc'], 'abc.txt'))

const res = await request(`http://localhost:${server.address().port}`, {
await request(`http://localhost:${server.address().port}`, {
method: 'POST',
body
})
await res.body.dump()
})
1 change: 0 additions & 1 deletion test/stream-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ test('IncomingMessage', async (t) => {
body: 'hello world'
}, (err, data) => {
t.ifError(err)
data.body.resume()
})
})
})
Expand Down
12 changes: 6 additions & 6 deletions types/dispatcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ declare namespace Dispatcher {
}
export type StreamFactory<TOpaque = null> = (data: StreamFactoryData<TOpaque>) => Writable
export interface DispatchHandlers {
onRequestStart(reserved: null, abort: (err?: Error) => void): void;
onResponseStart(resume: () => void): boolean;
onResponseHeaders(headers: Record<string, string>, statusCode: number): boolean;
onResponseData(chunk: Buffer): boolean;
onResponseEnd(trailers: Record<string, string>): void;
onResponseError(err: Error) : void;
onRequestStart?(reserved: null, abort: (err?: Error) => void): void;
onResponseStart?(resume: () => void): boolean;
onResponseHeaders?(headers: Record<string, string>, statusCode: number): boolean;
onResponseData?(chunk: Buffer): boolean;
onResponseEnd?(trailers: Record<string, string>): void;
onResponseError?(err: Error) : void;
/** @deprecated 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. */
onConnect?(abort: (err?: Error) => void): void;
/** @deprecated Invoked when an error has occurred. */
Expand Down

0 comments on commit b9cd97c

Please sign in to comment.