Skip to content

Commit

Permalink
Add type check for onclose callback (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
cemremengu authored and mcollina committed Feb 7, 2019
1 parent 92f03ec commit 2d6a224
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ Boot.prototype.after = function (func) {
Boot.prototype.onClose = function (func) {
// this is used to distinguish between onClose and close handlers
// because they share the same queue but must be called with different signatures

if (typeof func !== 'function') {
throw new Error('not a function')
}

func[this._isOnCloseHandlerKey] = true
this._closeQ.unshift(func, callback.bind(this))

Expand Down
11 changes: 11 additions & 0 deletions test/close.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,14 @@ test('close with async onClose handlers', (t) => {
})
})
})

test('onClose callback must be a function', (t) => {
t.plan(1)

const app = boot()

app.use(function (server, opts, done) {
t.throws(() => app.onClose({}), { message: 'not a function' })
done()
})
})

0 comments on commit 2d6a224

Please sign in to comment.