diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 4e32422a5e7720..eb545885b80e5e 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -22,6 +22,7 @@ const { const { kEmptyObject, kEnumerableProperty, + setOwnProperty, } = require('internal/util'); const { @@ -302,15 +303,15 @@ function setupPortReferencing(port, eventEmitter, eventName) { if (name === eventName) removeListener(eventEmitter.listenerCount(name)); }); const origNewListener = eventEmitter[kNewListener]; - eventEmitter[kNewListener] = function(size, type, ...args) { + setOwnProperty(eventEmitter, kNewListener, function(size, type, ...args) { if (type === eventName) newListener(size - 1); return ReflectApply(origNewListener, this, arguments); - }; + }); const origRemoveListener = eventEmitter[kRemoveListener]; - eventEmitter[kRemoveListener] = function(size, type, ...args) { + setOwnProperty(eventEmitter, kRemoveListener, function(size, type, ...args) { if (type === eventName) removeListener(size); return ReflectApply(origRemoveListener, this, arguments); - }; + }); function newListener(size) { if (size === 0) { diff --git a/test/parallel/test-worker-message-channel.js b/test/parallel/test-worker-message-channel.js index 8f66ef87e6e3d4..1d7550753932e7 100644 --- a/test/parallel/test-worker-message-channel.js +++ b/test/parallel/test-worker-message-channel.js @@ -3,6 +3,9 @@ const common = require('../common'); const assert = require('assert'); const { MessageChannel, MessagePort, Worker } = require('worker_threads'); +// Asserts that freezing the EventTarget prototype does not make the internal throw. +Object.freeze(EventTarget.prototype); + { const channel = new MessageChannel();