Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs/node#55116 #15

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ jobs:
- 20.13.0 # introduces TC early exit and TC#hasSubscribers()
- 21.0.0 # nothing special, first 21
- 21.x # nothing special, latest 21
- 22.0.0 # introduces TC early exit and TC#hasSubscribers()
- 2.20.0 # introduces TC early exit and TC#hasSubscribers()
- 22.x # nothing special, full support DC
- add me
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
8 changes: 7 additions & 1 deletion checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ module.exports.hasSyncUnsubscribeBug = hasSyncUnsubscribeBug;
function hasTracingChannelHasSubscribers() {
return MAJOR >= 22
|| (MAJOR == 20 && MINOR >= 13);
};
}
module.exports.hasTracingChannelHasSubscribers = hasTracingChannelHasSubscribers;

function hasSubscribersMutationBug() {
// TODO: version TBD
return MAJOR <= 22 && MINOR <= 9;
}
module.exports.hasSubscribersMutationBug = hasSubscribersMutationBug;
4 changes: 4 additions & 0 deletions dc-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ if (!checks.hasTracingChannelHasSubscribers()) {
dc = require('./patch-tracing-channel-has-subscribers.js')(dc);
}

if (!checks.hasSubscribersMutationBug()) {
dc = require('./patch-subscribers-mutation-bug.js')(dc);
}

module.exports = dc;

48 changes: 48 additions & 0 deletions patch-subscribers-mutation-bug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const {
ArrayPrototypePushApply,

Check failure on line 2 in patch-subscribers-mutation-bug.js

View workflow job for this annotation

GitHub Actions / Code Linter

'ArrayPrototypePushApply' is assigned a value but never used
ArrayPrototypeSlice
} = require('./primordials.js');

// The ch.unsubscribe() method doesn't return a value
// Recent versions return if an unsubscribe succeeded
// @see https://github.com/nodejs/node/pull/40433
module.exports = function (unpatched) {
const channels = new WeakSet();

const dc_channel = unpatched.channel;

const dc = { ...unpatched };

dc.channel = function () {
const ch = dc_channel.apply(this, arguments);

if (channels.has(ch)) return ch;

const { subscribe, unsubscribe, publish } = ch;

Check failure on line 21 in patch-subscribers-mutation-bug.js

View workflow job for this annotation

GitHub Actions / Code Linter

'unsubscribe' is assigned a value but never used

ch.subscribe = function () {
this._subscribers = ArrayPrototypeSlice(this._subscribers);

return subscribe.apply(this, arguments);
};

ch.unsubscribe = function () {
// TODO: update me
this._subscribers = ArrayPrototypeSlice(this._subscribers);

return subscribe.apply(this, arguments);
};
Comment on lines +29 to +34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Unexpected unnamed function. (...read more)

It is easier to debug your application code when you avoid anonymous functions so that the stack trace can show you meaningful error messages. This rule enforces all your function to be consistently declared with a name.

View in Datadog  Leave us feedback  Documentation


ch.publish = function () {
const self = Object.assign({}, this);

return publish.apply(self, arguments)

Check failure on line 39 in patch-subscribers-mutation-bug.js

View workflow job for this annotation

GitHub Actions / Code Linter

Missing semicolon
};

channels.add(ch);

return ch;
};
Comment on lines +16 to +45

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Unexpected unnamed function. (...read more)

It is easier to debug your application code when you avoid anonymous functions so that the stack trace can show you meaningful error messages. This rule enforces all your function to be consistently declared with a name.

View in Datadog  Leave us feedback  Documentation


return dc;
};
Comment on lines +9 to +48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Quality Violation

Unexpected unnamed function. (...read more)

It is easier to debug your application code when you avoid anonymous functions so that the stack trace can show you meaningful error messages. This rule enforces all your function to be consistently declared with a name.

View in Datadog  Leave us feedback  Documentation

6 changes: 6 additions & 0 deletions primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const ReflectApply = Reflect.apply;
const PromiseReject = Promise.reject.bind(Promise);
const PromiseResolve = Promise.resolve;
const PromisePrototypeThen = makeCall(Promise.prototype.then);
const ArrayPrototypePush = makeCall(Array.prototype.push);
const ArrayPrototypePushApply = (...args) => Array.prototype.push.apply(...args);
const ArrayPrototypeSlice = makeCall(Array.prototype.slice);
const ArrayPrototypeSplice = makeCall(Array.prototype.splice);
const ArrayPrototypeAt = makeCall(Array.prototype.at || arrayAtPolyfill);
const ObjectDefineProperty = Object.defineProperty;
Expand All @@ -31,6 +34,9 @@ module.exports = {
PromiseReject,
PromiseResolve,
PromisePrototypeThen,
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
ArrayPrototypeAt,
ObjectDefineProperty,
Expand Down
1 change: 1 addition & 0 deletions test/test-diagnostics-channel-sync-unsubscribe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test('test-diagnostics-channel-sync-unsubscribe', (t) => {
const onMessageHandler = common.mustCall(() => dc.unsubscribe(channel_name, onMessageHandler));

dc.subscribe(channel_name, onMessageHandler);
dc.subscribe(channel_name, common.mustCall());

// This must not throw.
dc.channel(channel_name).publish(published_data);
Expand Down
Loading