From b8cb3cc3dd280210a29967697c57b0b44d5cb773 Mon Sep 17 00:00:00 2001 From: BenzeneAlcohol Date: Fri, 27 Oct 2023 16:43:16 +0530 Subject: [PATCH] lib: make event static properties non writable and configurable The idl definition for Event makes the properties constant this means that they shouldn't be configurable and writable. However, they were, and this commit fixes that. Fixes: https://github.com/nodejs/node/issues/50417 --- lib/internal/event_target.js | 22 +++++++++++++++++----- test/parallel/test-event-target.js | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-event-target.js diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 0236f3a53c5276..4c67453fea4c59 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -2,6 +2,7 @@ const { ArrayFrom, + ArrayPrototypeReduce, Boolean, Error, FunctionPrototypeCall, @@ -314,11 +315,6 @@ class Event { throw new ERR_INVALID_THIS('Event'); this.#propagationStopped = true; } - - static NONE = 0; - static CAPTURING_PHASE = 1; - static AT_TARGET = 2; - static BUBBLING_PHASE = 3; } ObjectDefineProperties( @@ -354,6 +350,22 @@ ObjectDefineProperties( isTrusted: isTrustedDescriptor, }); +const staticProps = ['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE']; + +ObjectDefineProperties( + Event, + ArrayPrototypeReduce(staticProps, (result, staticProp, index = 0) => { + result[staticProp] = { + __proto__: null, + writable: false, + configurable: false, + enumerable: true, + value: index, + }; + return result; + }, {}), +); + function isCustomEvent(value) { return isEvent(value) && (value?.[kDetail] !== undefined); } diff --git a/test/parallel/test-event-target.js b/test/parallel/test-event-target.js new file mode 100644 index 00000000000000..12246b15ae859c --- /dev/null +++ b/test/parallel/test-event-target.js @@ -0,0 +1,21 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +const eventPhases = { + 'NONE': 0, + 'CAPTURING_PHASE': 1, + 'AT_TARGET': 2, + 'BUBBLING_PHASE': 3 +}; + +for (const [prop, value] of Object.entries(eventPhases)) { + // Check if the value of the property matches the expected value + assert.strictEqual(Event[prop], value, `Expected Event.${prop} to be ${value}, but got ${Event[prop]}`); + + const desc = Object.getOwnPropertyDescriptor(Event, prop); + assert.strictEqual(desc.writable, false, `${prop} should not be writable`); + assert.strictEqual(desc.configurable, false, `${prop} should not be configurable`); + assert.strictEqual(desc.enumerable, true, `${prop} should be enumerable`); +}