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.

PR-URL: https://github.com/nodejs/node/pull/13096
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
This commit is contained in:
Shadowbeetle 2017-05-18 10:09:50 +02:00 committed by Andreas Madsen
parent ef71824740
commit 6fb27af70a
No known key found for this signature in database
GPG Key ID: 2FEE61B3C9E40F20
2 changed files with 19 additions and 4 deletions

View File

@ -77,13 +77,13 @@ function fatalError(e) {
class AsyncHook { class AsyncHook {
constructor({ init, before, after, destroy }) { constructor({ init, before, after, destroy }) {
if (init && typeof init !== 'function') if (init !== undefined && typeof init !== 'function')
throw new TypeError('init must be a 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'); 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'); 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'); throw new TypeError('destroy must be a function');
this[init_symbol] = init; this[init_symbol] = init;

View File

@ -0,0 +1,15 @@
'use strict';
require('../common');
// This tests that using falsy values in createHook throws an error.
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$`));
}
}