Skip to content

Commit e2f3d29

Browse files
committed
diagnostics_channel: return original thenable
This makes tracePromise return the original thenable to allow custom thenable types to retain their methods rather than producing the chained result type.
1 parent 5b6091c commit e2f3d29

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

lib/diagnostics_channel.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const {
1010
ObjectDefineProperty,
1111
ObjectGetPrototypeOf,
1212
ObjectSetPrototypeOf,
13+
Promise,
14+
PromisePrototypeThen,
1315
ReflectApply,
1416
SafeFinalizationRegistry,
1517
SafeMap,
@@ -26,6 +28,9 @@ const {
2628
} = require('internal/validators');
2729

2830
const { triggerUncaughtException } = internalBinding('errors');
31+
const { isPromise } = require('internal/util/types');
32+
33+
const PromisePrototype = Promise.prototype;
2934

3035
const dc_binding = internalBinding('diagnostics_channel');
3136
const { subscribers: subscriberCounts } = dc_binding;
@@ -369,16 +374,20 @@ class TracingChannel {
369374

370375
const { start, end, asyncStart, asyncEnd, error } = this;
371376

372-
function reject(err) {
377+
function onReject(err) {
373378
context.error = err;
374379
error.publish(context);
375380
asyncStart.publish(context);
376381
// TODO: Is there a way to have asyncEnd _after_ the continuation?
377382
asyncEnd.publish(context);
383+
}
384+
385+
function onRejectWithRethrow(err) {
386+
onReject(err);
378387
throw err;
379388
}
380389

381-
function resolve(result) {
390+
function onResolve(result) {
382391
context.result = result;
383392
asyncStart.publish(context);
384393
// TODO: Is there a way to have asyncEnd _after_ the continuation?
@@ -396,7 +405,17 @@ class TracingChannel {
396405
context.result = result;
397406
return result;
398407
}
399-
return result.then(resolve, reject);
408+
// isPromise() matches sub-classes, but we need to match only direct
409+
// instances of the native Promise type to safely use PromisePrototypeThen.
410+
if (isPromise(result) && ObjectGetPrototypeOf(result) === PromisePrototype) {
411+
return PromisePrototypeThen(result, onResolve, onRejectWithRethrow);
412+
}
413+
// For non-native thenables, subscribe to the result but return the
414+
// original thenable so the consumer can continue handling it directly.
415+
// Non-native thenables don't have unhandledRejection tracking, so
416+
// swallowing the rejection here doesn't change existing behaviour.
417+
result.then(onResolve, onReject);
418+
return result;
400419
} catch (err) {
401420
context.error = err;
402421
error.publish(context);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const dc = require('diagnostics_channel');
5+
const assert = require('assert');
6+
7+
class SpoofedPromise extends Promise {
8+
customMethod() {
9+
return 'works';
10+
}
11+
}
12+
13+
const channel = dc.tracingChannel('test');
14+
15+
const expectedResult = { foo: 'bar' };
16+
const input = { foo: 'bar' };
17+
const thisArg = { baz: 'buz' };
18+
19+
function check(found) {
20+
assert.strictEqual(found, input);
21+
}
22+
23+
function checkAsync(found) {
24+
check(found);
25+
assert.strictEqual(found.error, undefined);
26+
assert.deepStrictEqual(found.result, expectedResult);
27+
}
28+
29+
const handlers = {
30+
start: common.mustCall(check),
31+
end: common.mustCall(check),
32+
asyncStart: common.mustCall(checkAsync),
33+
asyncEnd: common.mustCall(checkAsync),
34+
error: common.mustNotCall()
35+
};
36+
37+
channel.subscribe(handlers);
38+
39+
let innerPromise;
40+
41+
const result = channel.tracePromise(common.mustCall(function() {
42+
innerPromise = SpoofedPromise.resolve(expectedResult);
43+
// Spoof the constructor to try to trick the brand check
44+
innerPromise.constructor = Promise;
45+
return innerPromise;
46+
}), input, thisArg);
47+
48+
// Despite the spoofed constructor, the original subclass instance should be
49+
// returned directly so that custom methods remain accessible.
50+
assert(result instanceof SpoofedPromise);
51+
assert.strictEqual(result, innerPromise);
52+
assert.strictEqual(result.customMethod(), 'works');

test/parallel/test-diagnostics-channel-tracing-channel-promise-thenable.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class ResolvedThenable {
1212
then(resolve) {
1313
return new ResolvedThenable(resolve(this.#result));
1414
}
15+
customMethod() {
16+
return this.#result;
17+
}
1518
}
1619

1720
const channel = dc.tracingChannel('test');
@@ -49,7 +52,10 @@ const result = channel.tracePromise(common.mustCall(function(value) {
4952
}), input, thisArg, expectedResult);
5053

5154
assert(result instanceof ResolvedThenable);
52-
assert.notStrictEqual(result, innerThenable);
55+
// With branching then, the original thenable is returned directly so that
56+
// extra methods defined on it remain accessible to the caller.
57+
assert.strictEqual(result, innerThenable);
58+
assert.deepStrictEqual(result.customMethod(), expectedResult);
5359
result.then(common.mustCall((value) => {
5460
assert.deepStrictEqual(value, expectedResult);
5561
}));

0 commit comments

Comments
 (0)