Skip to content

Commit

Permalink
fix: make EventSource properties enumerable (#2987)
Browse files Browse the repository at this point in the history
* make eventsource properties enumberable

* Use kEnumerableProperty for eventsource immutable fields

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
  • Loading branch information
MattBidewell and Uzlopak authored Mar 23, 2024
1 parent 8fce214 commit bc304ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/web/eventsource/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { parseMIMEType } = require('../fetch/data-url')
const { MessageEvent } = require('../websocket/events')
const { isNetworkError } = require('../fetch/response')
const { delay } = require('./util')
const { kEnumerableProperty } = require('../../core/util')

let experimentalWarned = false

Expand Down Expand Up @@ -459,6 +460,16 @@ const constantsPropertyDescriptors = {
Object.defineProperties(EventSource, constantsPropertyDescriptors)
Object.defineProperties(EventSource.prototype, constantsPropertyDescriptors)

Object.defineProperties(EventSource.prototype, {
close: kEnumerableProperty,
onerror: kEnumerableProperty,
onmessage: kEnumerableProperty,
onopen: kEnumerableProperty,
readyState: kEnumerableProperty,
url: kEnumerableProperty,
withCredentials: kEnumerableProperty
})

webidl.converters.EventSourceInitDict = webidl.dictionaryConverter([
{ key: 'withCredentials', converter: webidl.converters.boolean, defaultValue: false }
])
Expand Down
15 changes: 15 additions & 0 deletions test/eventsource/eventsource-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { EventSource } = require('../..') // assuming the test is in test/eventsource/

test('EventSource.prototype properties are configured correctly', () => {
const props = Object.entries(Object.getOwnPropertyDescriptors(EventSource.prototype))

for (const [key, value] of props) {
if (key !== 'constructor') {
assert(value.enumerable, `${key} is not enumerable`)
}
}
})

0 comments on commit bc304ff

Please sign in to comment.