Skip to content

Accept a null source in MessageEventInit - #7470

Merged
jasnell merged 1 commit into
cloudflare:mainfrom
danielmlr:fix/message-event-init-null-source
Sep 23, 2026
Merged

jasnell merged 1 commit into
cloudflare:mainfrom
danielmlr:fix/message-event-init-null-source

Conversation

@danielmlr

Copy link
Copy Markdown
Contributor

Lets new MessageEvent() accept a null source again, as the HTML spec allows (MessageEventSource? source = null).

Since #7053 the initializer types source as optional but not nullable, so null throws TypeError: Incorrect type for the 'source' field on 'Initializer'. Every message event a WebSocket or EventSource fires has a null source, so copying one with new MessageEvent(event.type, event) throws too. partysocket copies events that way, and emdash patches it to leave the source out.

A port or an omitted source is unchanged. Before #7053 the initializer ignored the source altogether, so accepting null again needs no compatibility flag.

messageEventNullSource in events-test.js covers an explicit null, a copy of a user-constructed event and a copy of a WebSocketPair message event. Without the fix it fails with the TypeError above in the @, @all-compat-flags and @all-autogates variants; with it, bazel test //src/... passes on macOS arm64 (1951 tests, 8 skipped).

The HTML spec declares `MessageEventSource? source = null` in
MessageEventInit, but the initializer typed source as
jsg::Optional<jsg::Ref<MessagePort>>, which rejects null with a
TypeError. Message events fired by a WebSocket or an EventSource have
a null source, so copying one with `new MessageEvent(event.type, event)`
threw. partysocket copies events that way.

Compatibility: no flag. An omitted source and a MessagePort source
behave as before; the only change is that null no longer throws. Before
79e36b4 the initializer read nothing but data, so code passing a null
source worked until then, and this restores that.
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@danielmlr

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Sep 22, 2026
@danielmlr
danielmlr marked this pull request as ready for review September 22, 2026 18:42
@danielmlr
danielmlr requested review from a team as code owners September 22, 2026 18:42
@danielmlr
danielmlr requested a review from tpmmorris September 22, 2026 18:42

@tpmmorris tpmmorris left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

lgtm

Comment thread src/workerd/api/events.c++
@jasnell
jasnell merged commit c3f0074 into cloudflare:main Sep 23, 2026
56 of 60 checks passed
@danielmlr
danielmlr deleted the fix/message-event-init-null-source branch September 23, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants