From a5dce87ea3cc8486feab2f3667e61c29e4bdaafd Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Tue, 13 May 2025 10:52:56 -0700 Subject: [PATCH 1/9] lib: avoid event override by assignment --- lib/events.js | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/events.js b/lib/events.js index 5bfe95c84ab198..5970aafd59e030 100644 --- a/lib/events.js +++ b/lib/events.js @@ -330,24 +330,47 @@ EventEmitter.setMaxListeners = EventEmitter.init = function(opts) { if (this._events === undefined || - this._events === ObjectGetPrototypeOf(this)._events) { - this._events = { __proto__: null }; - this._eventsCount = 0; - this[kShapeMode] = false; + this._events === ObjectGetPrototypeOf(this)._events) { + ObjectDefineProperties(this, { + __proto__: null, + _events: { + __proto__: null, + value: { __proto__: null }, + }, + _eventCount: { + __proto__: null, + value: 0, + }, + [kShapeMode]: { + __proto__: null, + value: false, + }, + }); } else { - this[kShapeMode] = true; + ObjectDefineProperty(this, kShapeMode, { + __proto__: null, + value: true, + }); } - this._maxListeners ||= undefined; - + ObjectDefineProperty(this, '_maxListeners', { + __proto__: null, + value: this._maxListeners || undefined, + }); if (opts?.captureRejections) { validateBoolean(opts.captureRejections, 'options.captureRejections'); - this[kCapture] = Boolean(opts.captureRejections); + ObjectDefineProperty(this, kCapture, { + __proto__: null, + value: Boolean(opts.captureRejections), + }); } else { // Assigning the kCapture property directly saves an expensive // prototype lookup in a very sensitive hot path. - this[kCapture] = EventEmitter.prototype[kCapture]; + ObjectDefineProperty(this, kCapture, { + __proto__: null, + value: Boolean(opts.captureRejections), + }); } }; From c7a261a56902ee3b1d4ef16dfd3fb1ba522590f7 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Wed, 18 Jun 2025 17:32:38 -0700 Subject: [PATCH 2/9] fixup! Review suggestion Co-authored-by: Jordan Harband --- lib/events.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/events.js b/lib/events.js index 5970aafd59e030..cab04390ca0fae 100644 --- a/lib/events.js +++ b/lib/events.js @@ -349,7 +349,9 @@ EventEmitter.init = function(opts) { } else { ObjectDefineProperty(this, kShapeMode, { __proto__: null, + configurable: true, value: true, + writable: true, }); } From e57c53b12e9a7b0b1dcdf6b616f95a13ef67c093 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Wed, 18 Jun 2025 17:33:07 -0700 Subject: [PATCH 3/9] fixup! Review suggestion Co-authored-by: Jordan Harband --- lib/events.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/events.js b/lib/events.js index cab04390ca0fae..34c32c5a9a7995 100644 --- a/lib/events.js +++ b/lib/events.js @@ -357,7 +357,9 @@ EventEmitter.init = function(opts) { ObjectDefineProperty(this, '_maxListeners', { __proto__: null, + configurable: true, value: this._maxListeners || undefined, + writable: true, }); if (opts?.captureRejections) { From e7aeac9b652d9f24d9e064f27c7c23768722e150 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Wed, 18 Jun 2025 17:33:32 -0700 Subject: [PATCH 4/9] fixup! Review suggestion Co-authored-by: Jordan Harband --- lib/events.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index 34c32c5a9a7995..ce2b83e82773fe 100644 --- a/lib/events.js +++ b/lib/events.js @@ -366,7 +366,9 @@ EventEmitter.init = function(opts) { validateBoolean(opts.captureRejections, 'options.captureRejections'); ObjectDefineProperty(this, kCapture, { __proto__: null, - value: Boolean(opts.captureRejections), + configurable: true, + value: !!opts.captureRejections, + writable: true, }); } else { // Assigning the kCapture property directly saves an expensive From 640ac9de7a74a319bcb277c8f41525224c8a3a69 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Thu, 19 Jun 2025 15:21:19 -0700 Subject: [PATCH 5/9] fixup! review suggestions Co-authored-by: Jordan Harband --- lib/events.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index ce2b83e82773fe..e01e1b35719ac2 100644 --- a/lib/events.js +++ b/lib/events.js @@ -375,7 +375,9 @@ EventEmitter.init = function(opts) { // prototype lookup in a very sensitive hot path. ObjectDefineProperty(this, kCapture, { __proto__: null, - value: Boolean(opts.captureRejections), + configurable: true, + value: EventEmitter.prototype[kCapture], + writable: true, }); } }; From c60730cebe1bb730e0ad5394abd5c9c5bd433a9f Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Thu, 19 Jun 2025 15:30:25 -0700 Subject: [PATCH 6/9] fixup! Review suggestion Co-authored-by: Jordan Harband --- lib/events.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/events.js b/lib/events.js index e01e1b35719ac2..f1923d7ae79642 100644 --- a/lib/events.js +++ b/lib/events.js @@ -335,15 +335,21 @@ EventEmitter.init = function(opts) { __proto__: null, _events: { __proto__: null, + configurable: true, value: { __proto__: null }, + writable: true, }, _eventCount: { __proto__: null, + configurable: true, value: 0, + writable: true, }, [kShapeMode]: { __proto__: null, + configurable: true, value: false, + writable: true, }, }); } else { From fae8feb5b24a73475a2807d9d1f7376b0be8ab37 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Thu, 19 Jun 2025 18:12:47 -0700 Subject: [PATCH 7/9] fixup! remove unused Boolean --- lib/events.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index f1923d7ae79642..fec3dbb339c9ae 100644 --- a/lib/events.js +++ b/lib/events.js @@ -28,7 +28,6 @@ const { ArrayPrototypeSlice, ArrayPrototypeSplice, ArrayPrototypeUnshift, - Boolean, Error, ErrorCaptureStackTrace, FunctionPrototypeBind, From 577a8219d3bc5c2638c21ca46c393b48d5c2bd31 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Tue, 12 Aug 2025 15:20:01 -0700 Subject: [PATCH 8/9] fixup! merge repair --- lib/events.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/events.js b/lib/events.js index 70f617398dde7a..7ee58c5326acdc 100644 --- a/lib/events.js +++ b/lib/events.js @@ -28,6 +28,7 @@ const { ArrayPrototypeSlice, ArrayPrototypeSplice, ArrayPrototypeUnshift, + AsyncIteratorPrototype, Error, ErrorCaptureStackTrace, FunctionPrototypeBind, From f1958ee5b561d6facdb81da6be2634aa294b15e7 Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Tue, 12 Aug 2025 15:21:41 -0700 Subject: [PATCH 9/9] fixup! restore Boolean --- lib/events.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index 7ee58c5326acdc..9c51f5f2ffd00e 100644 --- a/lib/events.js +++ b/lib/events.js @@ -29,6 +29,7 @@ const { ArrayPrototypeSplice, ArrayPrototypeUnshift, AsyncIteratorPrototype, + Boolean, Error, ErrorCaptureStackTrace, FunctionPrototypeBind, @@ -373,7 +374,7 @@ EventEmitter.init = function(opts) { ObjectDefineProperty(this, kCapture, { __proto__: null, configurable: true, - value: !!opts.captureRejections, + value: Boolean(opts.captureRejections), writable: true, }); } else {