Skip to content

Commit 6fb27af

Browse files
ShadowbeetleAndreasMadsen
authored andcommitted
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: #13096 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Andreas Madsen <[email protected]>
1 parent ef71824 commit 6fb27af

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/async_hooks.js

+4-4
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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
require('../common');
3+
4+
// This tests that using falsy values in createHook throws an error.
5+
6+
const assert = require('assert');
7+
const async_hooks = require('async_hooks');
8+
9+
for (const badArg of [0, 1, false, true, null, 'hello']) {
10+
for (const field of ['init', 'before', 'after', 'destroy']) {
11+
assert.throws(() => {
12+
async_hooks.createHook({ [field]: badArg });
13+
}, new RegExp(`^TypeError: ${field} must be a function$`));
14+
}
15+
}

0 commit comments

Comments
 (0)