Skip to content

Commit 2913074

Browse files
committed
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.
1 parent 6933419 commit 2913074

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/async_hooks.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ function fatalError(e) {
7777

7878
class AsyncHook {
7979
constructor({ init, before, after, destroy }) {
80-
if (init && typeof init !== 'function')
80+
if (init !== undefined && typeof init !== 'function')
8181
throw new TypeError('init must be a function');
82-
if (before && typeof before !== 'function')
82+
if (before !== undefined && typeof before !== 'function')
8383
throw new TypeError('before must be a function');
84-
if (after && typeof after !== 'function')
84+
if (after !== undefined && typeof after !== 'function')
8585
throw new TypeError('after must be a function');
86-
if (destroy && typeof destroy !== 'function')
86+
if (destroy !== undefined && typeof destroy !== 'function')
8787
throw new TypeError('destroy must be a function');
8888

8989
this[init_symbol] = init;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const async_hooks = require('async_hooks');
5+
6+
for (const badArg of [0, 1, false, true, null, 'hello']) {
7+
for (const field of ['init', 'before', 'after', 'destroy']) {
8+
assert.throws(() => {
9+
async_hooks.createHook({ [field]: badArg });
10+
}, new RegExp(`^TypeError: ${field} must be a function$`));
11+
}
12+
}

0 commit comments

Comments
 (0)