From 29130742c95fc5883c636255613636fcec22544f Mon Sep 17 00:00:00 2001 From: Shadowbeetle Date: Thu, 18 May 2017 10:09:50 +0200 Subject: [PATCH] async_hooks: add constructor check to async-hooks This fixes the async_hooks.AsyncHook constructor such that it throws an error when provided with falsy values other than undefined. --- lib/async_hooks.js | 8 ++++---- test/parallel/test-async-wrap-constructor.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-async-wrap-constructor.js diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 867b5eb52da14d..9ff244aad7e6b4 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -77,13 +77,13 @@ function fatalError(e) { class AsyncHook { constructor({ init, before, after, destroy }) { - if (init && typeof init !== 'function') + if (init !== undefined && typeof init !== 'function') throw new TypeError('init must be a function'); - if (before && typeof before !== 'function') + if (before !== undefined && typeof before !== 'function') throw new TypeError('before must be a function'); - if (after && typeof after !== 'function') + if (after !== undefined && typeof after !== 'function') throw new TypeError('after must be a function'); - if (destroy && typeof destroy !== 'function') + if (destroy !== undefined && typeof destroy !== 'function') throw new TypeError('destroy must be a function'); this[init_symbol] = init; diff --git a/test/parallel/test-async-wrap-constructor.js b/test/parallel/test-async-wrap-constructor.js new file mode 100644 index 00000000000000..213691a7808692 --- /dev/null +++ b/test/parallel/test-async-wrap-constructor.js @@ -0,0 +1,12 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const async_hooks = require('async_hooks'); + +for (const badArg of [0, 1, false, true, null, 'hello']) { + for (const field of ['init', 'before', 'after', 'destroy']) { + assert.throws(() => { + async_hooks.createHook({ [field]: badArg }); + }, new RegExp(`^TypeError: ${field} must be a function$`)); + } +}