From f36059bda89213aa3b8b0de162174d9c7fbf3e68 Mon Sep 17 00:00:00 2001 From: cemremengu Date: Thu, 7 Feb 2019 13:02:22 +0300 Subject: [PATCH] Add type check for onclose callback --- 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() + }) +})