Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve DecoratorHandler #3079

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/handler/decorator-handler.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
'use strict'

module.exports = class DecoratorHandler {
#handler

constructor (handler) {
this.handler = handler
if (typeof handler !== 'object' || handler === null) {
throw new TypeError('handler must be an object')
}
this.#handler = handler
}

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

onError (...args) {
return this.handler.onError(...args)
return this.#handler.onError?.(...args)
}

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

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

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

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

onComplete (...args) {
return this.handler.onComplete(...args)
return this.#handler.onComplete?.(...args)
}

onBodySent (...args) {
return this.handler.onBodySent(...args)
return this.#handler.onBodySent?.(...args)
}
}
74 changes: 74 additions & 0 deletions test/decorator-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { describe, test } = require('node:test')
const DecoratorHandler = require('../lib/handler/decorator-handler')

const methods = [
'onConnect',
'onError',
'onUpgrade',
'onHeaders',
'onResponseStarted',
'onData',
'onComplete',
'onBodySent'
]

describe('DecoratorHandler', () => {
test('should throw if provided handler is not an object', (t) => {
t = tspl(t, { plan: 4 })
t.throws(() => new DecoratorHandler(null), new TypeError('handler must be an object'))
t.throws(() => new DecoratorHandler('string'), new TypeError('handler must be an object'))

t.throws(() => new DecoratorHandler(null), new TypeError('handler must be an object'))
t.throws(() => new DecoratorHandler('string'), new TypeError('handler must be an object'))
})

test('should not expose the handler', (t) => {
t = tspl(t, { plan: 1 })
const handler = {}
const decorator = new DecoratorHandler(handler)
t.strictEqual(Object.keys(decorator).length, 0)
})

methods.forEach((method) => {
test(`should have delegate ${method}-method`, (t) => {
t = tspl(t, { plan: 1 })
const decorator = new 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({})
t.doesNotThrow(() => decorator[method]())
})
})
})