From 2d6a2246f6d716444e1e0c8d7f4d46e6e15a84c5 Mon Sep 17 00:00:00 2001 From: Cemre Mengu Date: Fri, 8 Feb 2019 02:47:30 +0300 Subject: [PATCH] Add type check for onclose callback (#80) --- boot.js | 5 +++++ test/close.test.js | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/boot.js b/boot.js index e3c5bd7..69cdf32 100644 --- a/boot.js +++ b/boot.js @@ -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)) diff --git a/test/close.test.js b/test/close.test.js index 76f0e0a..198c53d 100644 --- a/test/close.test.js +++ b/test/close.test.js @@ -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() + }) +})