Skip to content

Commit 14bc3e2

Browse files
committed
domain: runtime deprecate MakeCallback
Users of MakeCallback that adds the domain property to carry context, should start using the async_context variant of MakeCallback or the AsyncResource class. PR-URL: #17417 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent da97a47 commit 14bc3e2

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

doc/api/deprecations.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,15 @@ Type: Runtime
871871
872872
`timers.unenroll()` is deprecated. Please use the publicly documented [`clearTimeout()`][] or [`clearInterval()`][] instead.
873873
874+
<a id="DEP0097"></a>
875+
### DEP0097: MakeCallback with domain property
876+
877+
Type: Runtime
878+
879+
Users of `MakeCallback` that add the `domain` property to carry context,
880+
should start using the `async_context` variant of `MakeCallback` or
881+
`CallbackScope`, or the the high-level `AsyncResource` class.
882+
874883
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
875884
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
876885
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array

lib/domain.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,29 @@ process.setUncaughtExceptionCaptureCallback = function(fn) {
9494
throw err;
9595
};
9696

97+
98+
let sendMakeCallbackDeprecation = false;
99+
function emitMakeCallbackDeprecation() {
100+
if (!sendMakeCallbackDeprecation) {
101+
process.emitWarning(
102+
'Using a domain property in MakeCallback is deprecated. Use the ' +
103+
'async_context variant of MakeCallback or the AsyncResource class ' +
104+
'instead.', 'DeprecationWarning', 'DEP0097');
105+
sendMakeCallbackDeprecation = true;
106+
}
107+
}
108+
97109
function topLevelDomainCallback(cb, ...args) {
98110
const domain = this.domain;
111+
if (exports.active && domain)
112+
emitMakeCallbackDeprecation();
113+
99114
if (domain)
100115
domain.enter();
101116
const ret = Reflect.apply(cb, this, args);
102117
if (domain)
103118
domain.exit();
119+
104120
return ret;
105121
}
106122

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "node.h"
2+
#include "v8.h"
3+
4+
#include <assert.h>
5+
6+
using v8::Function;
7+
using v8::FunctionCallbackInfo;
8+
using v8::Isolate;
9+
using v8::Local;
10+
using v8::Object;
11+
using v8::Value;
12+
13+
namespace {
14+
15+
void MakeCallback(const FunctionCallbackInfo<Value>& args) {
16+
assert(args[0]->IsObject());
17+
assert(args[1]->IsFunction());
18+
Isolate* isolate = args.GetIsolate();
19+
Local<Object> recv = args[0].As<Object>();
20+
Local<Function> method = args[1].As<Function>();
21+
22+
node::MakeCallback(isolate, recv, method, 0, nullptr);
23+
}
24+
25+
void Initialize(Local<Object> exports) {
26+
NODE_SET_METHOD(exports, "makeCallback", MakeCallback);
27+
}
28+
29+
} // namespace
30+
31+
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'binding',
5+
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
6+
'sources': [ 'binding.cc' ]
7+
}
8+
]
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const assert = require('assert');
5+
const domain = require('domain');
6+
const binding = require(`./build/${common.buildType}/binding`);
7+
8+
function makeCallback(object, cb) {
9+
binding.makeCallback(object, () => setImmediate(cb));
10+
}
11+
12+
let latestWarning = null;
13+
process.on('warning', function(warning) {
14+
latestWarning = warning;
15+
});
16+
17+
const d = domain.create();
18+
19+
// When domain is disabled, no warning will be emitted
20+
makeCallback({ domain: d }, common.mustCall(function() {
21+
assert.strictEqual(latestWarning, null);
22+
23+
d.run(common.mustCall(function() {
24+
// No warning will be emitted when no domain property is applied
25+
makeCallback({}, common.mustCall(function() {
26+
assert.strictEqual(latestWarning, null);
27+
28+
// Warning is emitted when domain property is used and domain is enabled
29+
makeCallback({ domain: d }, common.mustCall(function() {
30+
assert.strictEqual(latestWarning.name, 'DeprecationWarning');
31+
assert.strictEqual(latestWarning.code, 'DEP0097');
32+
}));
33+
}));
34+
}));
35+
}));

0 commit comments

Comments
 (0)