Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/api/api-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,14 @@ class PipelineHandler extends AsyncResource {
}

onConnect (abort, context) {
const { ret, res } = this
const { res } = this

if (this.reason) {
abort(this.reason)
return
}

assert(!res, 'pipeline cannot be retried')
assert(!ret.destroyed)

this.abort = abort
this.context = context
Expand Down
2 changes: 2 additions & 0 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const { headerNameLowerCasedRecord } = require('./constants')
const invalidPathRegex = /[^\u0021-\u00ff]/

const kHandler = Symbol('handler')
const noop = () => {}

class Request {
constructor (origin, {
Expand Down Expand Up @@ -296,6 +297,7 @@ class Request {
}
this.aborted = true


return this[kHandler].onError(error)
}

Expand Down
32 changes: 32 additions & 0 deletions lib/handler/decorator-handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
'use strict'

const assert = require('node:assert')
const noop = () => {}

module.exports = class DecoratorHandler {
#handler
#onConnectCalled = false
#onCompleteCalled = false
#onErrorCalled = false

constructor (handler) {
if (typeof handler !== 'object' || handler === null) {
Expand All @@ -11,34 +17,60 @@ module.exports = class DecoratorHandler {
}

onConnect (...args) {
this.#onConnectCalled = true
return this.#handler.onConnect?.(...args)
}

onError (...args) {
if (!this.#onConnectCalled) {
this.#onConnectCalled = true
this.#handler.onConnect?.(noop)
}

this.#onErrorCalled = true
return this.#handler.onError?.(...args)
}

onUpgrade (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onUpgrade?.(...args)
}

onResponseStarted (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onResponseStarted?.(...args)
}

onHeaders (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onHeaders?.(...args)
}

onData (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onData?.(...args)
}

onComplete (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

this.#onCompleteCalled = true
return this.#handler.onComplete?.(...args)
}

onBodySent (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onBodySent?.(...args)
}
}