decorator-handler.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict'
  2. const assert = require('node:assert')
  3. const WrapHandler = require('./wrap-handler')
  4. /**
  5. * @deprecated
  6. */
  7. module.exports = class DecoratorHandler {
  8. #handler
  9. #onCompleteCalled = false
  10. #onErrorCalled = false
  11. #onResponseStartCalled = false
  12. constructor (handler) {
  13. if (typeof handler !== 'object' || handler === null) {
  14. throw new TypeError('handler must be an object')
  15. }
  16. this.#handler = WrapHandler.wrap(handler)
  17. }
  18. onRequestStart (...args) {
  19. this.#handler.onRequestStart?.(...args)
  20. }
  21. onRequestUpgrade (...args) {
  22. assert(!this.#onCompleteCalled)
  23. assert(!this.#onErrorCalled)
  24. return this.#handler.onRequestUpgrade?.(...args)
  25. }
  26. onResponseStart (...args) {
  27. assert(!this.#onCompleteCalled)
  28. assert(!this.#onErrorCalled)
  29. assert(!this.#onResponseStartCalled)
  30. this.#onResponseStartCalled = true
  31. return this.#handler.onResponseStart?.(...args)
  32. }
  33. onResponseData (...args) {
  34. assert(!this.#onCompleteCalled)
  35. assert(!this.#onErrorCalled)
  36. return this.#handler.onResponseData?.(...args)
  37. }
  38. onResponseEnd (...args) {
  39. assert(!this.#onCompleteCalled)
  40. assert(!this.#onErrorCalled)
  41. this.#onCompleteCalled = true
  42. return this.#handler.onResponseEnd?.(...args)
  43. }
  44. onResponseError (...args) {
  45. this.#onErrorCalled = true
  46. return this.#handler.onResponseError?.(...args)
  47. }
  48. /**
  49. * @deprecated
  50. */
  51. onBodySent () {}
  52. }