Skip to content

Commit

Permalink
async_hooks: add constructor check to async-hooks
Browse files Browse the repository at this point in the history
This fixes the async_hooks.AsyncHook constructor such that it throws an
error when provided with falsy values other than undefined.

PR-URL: nodejs#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>
  • Loading branch information
Shadowbeetle authored and AndreasMadsen committed May 21, 2017
1 parent ef71824 commit 6fb27af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-async-wrap-constructor.js
Original file line number Diff line number Diff line change
@@ -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$`));
}
}

0 comments on commit 6fb27af

Please sign in to comment.