Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/node/diagnostics_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import { ERR_INVALID_ARG_TYPE } from 'node-internal:internal_errors';

import { validateObject } from 'node-internal:validators';

const hasSubscribersGetter =
!!Cloudflare.compatibilityFlags['diagnostics_channel_has_subscribers_getter'];

export const { Channel } = diagnosticsChannel;

export function hasSubscribers(name: string | symbol): boolean {
Expand Down Expand Up @@ -109,6 +112,47 @@ export class TracingChannel {
return this[kError];
}

// `hasSubscribers` is installed on the prototype below, gated by the
// `diagnostics_channel_has_subscribers_getter` compatibility flag. When the
// flag is enabled it's a getter property (matching Node.js); when disabled
// it's a method (preserving the original workerd behavior).
static {
if (hasSubscribersGetter) {
Object.defineProperty(this.prototype, 'hasSubscribers', {
get(this: TracingChannel): boolean {
return [
this.start,
this.end,
this.asyncStart,
this.asyncEnd,
this.error,
].some((c) => c != null && (c.hasSubscribers as unknown as boolean));
},
configurable: true,
enumerable: false,
});
} else {
Object.defineProperty(this.prototype, 'hasSubscribers', {
value: function hasSubscribers(this: TracingChannel): boolean {
return [
this.start,
this.end,
this.asyncStart,
this.asyncEnd,
this.error,
].some(
(c) =>
c != null &&
(c.hasSubscribers as unknown as () => boolean).call(c)
);
},
writable: true,
configurable: true,
enumerable: false,
});
}
}

subscribe(subscriptions: TracingChannelSubscriptions): void {
if (subscriptions.start !== undefined)
this[kStart]?.subscribe(subscriptions.start);
Expand Down
9 changes: 7 additions & 2 deletions src/workerd/api/node/diagnostics-channel.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <workerd/api/node/async-hooks.h>
#include <workerd/io/compatibility-date.capnp.h>
#include <workerd/jsg/jsg.h>

#include <kj/map.h>
Expand Down Expand Up @@ -31,8 +32,12 @@ class Channel: public jsg::Object {
jsg::Optional<v8::Local<v8::Value>> maybeReceiver,
jsg::Arguments<jsg::Value> args);

JSG_RESOURCE_TYPE(Channel) {
JSG_METHOD(hasSubscribers);
JSG_RESOURCE_TYPE(Channel, CompatibilityFlags::Reader flags) {
if (flags.getDiagnosticsChannelHasSubscribersGetter()) {
JSG_READONLY_PROTOTYPE_PROPERTY(hasSubscribers, hasSubscribers);
} else {
JSG_METHOD(hasSubscribers);
}
JSG_METHOD(publish);
JSG_METHOD(subscribe);
JSG_METHOD(unsubscribe);
Expand Down
6 changes: 6 additions & 0 deletions src/workerd/api/node/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ wd_test(
data = ["diagnostics-channel-test.js"],
)

wd_test(
src = "diagnostics-channel-getter-test.wd-test",
args = ["--experimental"],
data = ["diagnostics-channel-getter-test.js"],
)

wd_test(
src = "mimetype-test.wd-test",
args = ["--experimental"],
Expand Down
67 changes: 67 additions & 0 deletions src/workerd/api/node/tests/diagnostics-channel-getter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2026 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
//
// Tests the `hasSubscribers` getter behavior of `Channel` and `TracingChannel`
// when the `diagnostics_channel_has_subscribers_getter` compat flag is enabled.
// Legacy method behavior is covered by `diagnostics-channel-test`.

import { ok, strictEqual } from 'node:assert';

import { channel, tracingChannel } from 'node:diagnostics_channel';

export const test_channel_hasSubscribers_is_a_getter = {
async test() {
const ch = channel('getter-test');

// It is a boolean, not a function (matches Node.js).
strictEqual(typeof ch.hasSubscribers, 'boolean');
strictEqual(ch.hasSubscribers, false);

const listener = () => {};
ch.subscribe(listener);
strictEqual(ch.hasSubscribers, true);

ch.unsubscribe(listener);
strictEqual(ch.hasSubscribers, false);

// Defined on the prototype as a read-only getter.
const desc = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(ch),
'hasSubscribers'
);
ok(desc);
strictEqual(typeof desc.get, 'function');
strictEqual(desc.set, undefined);
strictEqual(desc.value, undefined);
},
};

export const test_tracingChannel_hasSubscribers_is_a_getter = {
async test() {
const tc = tracingChannel('tracing-getter-test');

strictEqual(typeof tc.hasSubscribers, 'boolean');
strictEqual(tc.hasSubscribers, false);

const listener = () => {};

// Each sub-channel independently flips the aggregate getter.
for (const sub of ['start', 'end', 'asyncStart', 'asyncEnd', 'error']) {
tc[sub].subscribe(listener);
strictEqual(tc.hasSubscribers, true, `via ${sub}`);
tc[sub].unsubscribe(listener);
strictEqual(tc.hasSubscribers, false, `after unsubscribing ${sub}`);
}

// TracingChannel.hasSubscribers should also be a prototype getter.
const desc = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(tc),
'hasSubscribers'
);
ok(desc);
strictEqual(typeof desc.get, 'function');
strictEqual(desc.set, undefined);
strictEqual(desc.value, undefined);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "diagnostics-channel-getter-test",
worker = (
modules = [
(name = "worker", esModule = embed "diagnostics-channel-getter-test.js")
],
compatibilityFlags = [
"nodejs_compat",
"nodejs_compat_v2",
"diagnostics_channel_has_subscribers_getter",
]
)
),
],
);
51 changes: 51 additions & 0 deletions src/workerd/api/node/tests/diagnostics-channel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,54 @@ export const DiagChannelUnbindDuringRunStores = {
strictEqual(transformCallCount, 1);
},
};

// Legacy-method behavior: when the `diagnostics_channel_has_subscribers_getter`
// compat flag is NOT enabled, `Channel.hasSubscribers` and
// `TracingChannel.hasSubscribers` are registered as methods. The associated
// `.wd-test` opts out of the flag explicitly so that this behavior is exercised
// under both the default and `@all-compat-flags` test variants.
export const test_channel_hasSubscribers_is_a_method = {
async test() {
const ch = channel('method-test');

// It is a method, not a boolean property.
strictEqual(typeof ch.hasSubscribers, 'function');
strictEqual(ch.hasSubscribers(), false);

const listener = () => {};
ch.subscribe(listener);
strictEqual(ch.hasSubscribers(), true);

ch.unsubscribe(listener);
strictEqual(ch.hasSubscribers(), false);

// Defined on the prototype as a method (value descriptor, no getter).
const desc = Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(ch),
'hasSubscribers'
);
ok(desc);
strictEqual(typeof desc.value, 'function');
strictEqual(desc.get, undefined);
strictEqual(desc.set, undefined);
},
};

export const test_tracingChannel_hasSubscribers_is_a_method = {
async test() {
const tc = tracingChannel('tracing-method-test');

strictEqual(typeof tc.hasSubscribers, 'function');
strictEqual(tc.hasSubscribers(), false);

const listener = () => {};

// Each sub-channel independently flips the aggregate method result.
for (const sub of ['start', 'end', 'asyncStart', 'asyncEnd', 'error']) {
tc[sub].subscribe(listener);
strictEqual(tc.hasSubscribers(), true, `via ${sub}`);
tc[sub].unsubscribe(listener);
strictEqual(tc.hasSubscribers(), false, `after unsubscribing ${sub}`);
}
},
};
10 changes: 9 additions & 1 deletion src/workerd/api/node/tests/diagnostics-channel-test.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ const unitTests :Workerd.Config = (
modules = [
(name = "worker", esModule = embed "diagnostics-channel-test.js")
],
compatibilityFlags = ["nodejs_compat", "nodejs_compat_v2"]
compatibilityFlags = [
"nodejs_compat",
"nodejs_compat_v2",
# Explicitly opt out of the `hasSubscribers` getter so this test file
# always exercises the legacy method behavior, even under
# `@all-compat-flags`. The getter behavior is covered by
# `diagnostics-channel-getter-test`.
"no_diagnostics_channel_has_subscribers_getter",
]
)
),
],
Expand Down
16 changes: 14 additions & 2 deletions src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,20 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
# `ctx.version` is much more well defined. The behaviour of this flag will change in the future.

noResizableArrayBufferInBlob @173 :Bool
$compatEnableFlag("no_resizable_array_buffer_in_blob")
$compatDisableFlag("resizable_array_buffer_in_blob");
$compatEnableFlag("no_resizable_array_buffer_in_blob")
$compatDisableFlag("resizable_array_buffer_in_blob");
# When enabled, creating a Blob with a resizable ArrayBuffer will throw a TypeError, matching
# expected spec behavior.

diagnosticsChannelHasSubscribersGetter @174 :Bool
$compatEnableFlag("diagnostics_channel_has_subscribers_getter")
$compatDisableFlag("no_diagnostics_channel_has_subscribers_getter")
$compatEnableDate("2026-05-19");
# Node.js' `diagnostics_channel.Channel.hasSubscribers` and
# `TracingChannel.hasSubscribers` are boolean getter properties, not methods.
# Originally, workerd registered `Channel.hasSubscribers` as a method (so users
# had to call `ch.hasSubscribers()` with parentheses). When this flag is
# enabled, `hasSubscribers` becomes a read-only getter that evaluates directly
# to a boolean, matching Node.js behavior.
# See: https://nodejs.org/dist/latest-v20.x/docs/api/diagnostics_channel.html#channelhassubscribers
}
Loading