Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 49f16d7

Browse filesBrowse files
trevnorrisMyles Borins
authored andcommitted
async_wrap: allow some hooks to be optional
Only enforce that the init callback is passed to setupHooks(). The remaining hooks can be optionally passed. Throw if async_wrap.enable() runs before setting the init callback or if setupHooks() is called while async wrap is enabled. Add test to verify calls throw appropriately. PR-URL: #3461 Reviewed-By: Fedor Indutny <fedor@indutny.com>
1 parent f413fae commit 49f16d7
Copy full SHA for 49f16d7

File tree

Expand file treeCollapse file tree

2 files changed

+35
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-5
lines changed
Open diff view settings
Collapse file

‎src/async-wrap.cc‎

Copy file name to clipboardExpand all lines: src/async-wrap.cc
+13-5Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ RetainedObjectInfo* WrapperInfo(uint16_t class_id, Local<Value> wrapper) {
103103

104104
static void EnableHooksJS(const FunctionCallbackInfo<Value>& args) {
105105
Environment* env = Environment::GetCurrent(args);
106+
Local<Function> init_fn = env->async_hooks_init_function();
107+
if (init_fn.IsEmpty() || !init_fn->IsFunction())
108+
return env->ThrowTypeError("init callback is not assigned to a function");
106109
env->async_hooks()->set_enable_callbacks(1);
107110
}
108111

@@ -116,13 +119,18 @@ static void DisableHooksJS(const FunctionCallbackInfo<Value>& args) {
116119
static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
117120
Environment* env = Environment::GetCurrent(args);
118121

119-
CHECK(args[0]->IsFunction());
120-
CHECK(args[1]->IsFunction());
121-
CHECK(args[2]->IsFunction());
122+
if (env->async_hooks()->callbacks_enabled())
123+
return env->ThrowError("hooks should not be set while also enabled");
124+
125+
if (!args[0]->IsFunction())
126+
return env->ThrowTypeError("init callback must be a function");
122127

123128
env->set_async_hooks_init_function(args[0].As<Function>());
124-
env->set_async_hooks_pre_function(args[1].As<Function>());
125-
env->set_async_hooks_post_function(args[2].As<Function>());
129+
130+
if (args[1]->IsFunction())
131+
env->set_async_hooks_pre_function(args[1].As<Function>());
132+
if (args[2]->IsFunction())
133+
env->set_async_hooks_post_function(args[2].As<Function>());
126134
}
127135

128136

Collapse file
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const async_wrap = process.binding('async_wrap');
6+
7+
8+
assert.throws(function() {
9+
async_wrap.setupHooks(null);
10+
}, /init callback must be a function/);
11+
12+
assert.throws(function() {
13+
async_wrap.enable();
14+
}, /init callback is not assigned to a function/);
15+
16+
// Should not throw
17+
async_wrap.setupHooks(() => {});
18+
async_wrap.enable();
19+
20+
assert.throws(function() {
21+
async_wrap.setupHooks(() => {});
22+
}, /hooks should not be set while also enabled/);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.