Skip to content

Commit def3c83

Browse files
committed
async_hooks: add type checking for AsyncResource type
throw `TypeError` if `type` is not provided or is not a string
1 parent 73b8272 commit def3c83

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/async_hooks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ function triggerAsyncId() {
234234

235235
class AsyncResource {
236236
constructor(type, triggerAsyncId = initTriggerId()) {
237+
if (typeof type !== 'string')
238+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'type', 'string');
239+
237240
// Unlike emitInitScript, AsyncResource doesn't supports null as the
238241
// triggerAsyncId.
239242
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < -1) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const async_hooks = require('async_hooks');
6+
const { AsyncResource } = async_hooks;
7+
const { spawn } = require('child_process');
8+
9+
const initHooks = require('./init-hooks');
10+
11+
if (process.argv[2] === 'child') {
12+
initHooks().enable();
13+
14+
class Foo extends AsyncResource {
15+
constructor(type) {
16+
super(type, async_hooks.executionAsyncId());
17+
}
18+
}
19+
20+
[null, undefined, 1, Date, {}, []].forEach((i) => {
21+
common.expectsError(() => new Foo(i), {
22+
code: 'ERR_INVALID_ARG_TYPE',
23+
type: TypeError
24+
});
25+
});
26+
27+
} else {
28+
const args = process.argv.slice(1).concat('child');
29+
spawn(process.execPath, args)
30+
.on('close', common.mustCall((code) => {
31+
// No error because the type was defaulted
32+
assert.strictEqual(code, 0);
33+
}));
34+
}

0 commit comments

Comments
 (0)