Skip to content

fix: support native Event and CustomEvent and avoid overwriting globals - #58

Open
JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:fix/events-compatibility
Open

JeanMeche wants to merge 1 commit into
angular:mainfrom
JeanMeche:fix/events-compatibility

Conversation

@JeanMeche

Copy link
Copy Markdown
Member

Description

Modern Node.js (v18+) provides native standard implementations of Event, CustomEvent, and EventTarget.

When @angular/platform-server imports Domino and copies domino.impl onto globalThis, Domino's internal Event and CustomEvent constructors overwrite Node's native globals. This causes issues for libraries extending Node's native EventTarget (such as p-queue v10), because Node's native EventTarget.prototype.dispatchEvent() enforces an internal brand check requiring the event to be an instance of Node's native Event, throwing:

TypeError [ERR_INVALID_ARG_TYPE]: The "event" argument must be an instance of Event. Received an instance of CustomEvent

Furthermore, Domino's EventTarget.prototype._dispatchEvent previously rejected native Event instances because it required event._initialized === true and directly assigned to getter-only properties (target, currentTarget, isTrusted, eventPhase).

Changes

  1. Prefer globalThis.Event / CustomEvent: Updated lib/events.js to defer to globalThis event constructors when available in the environment.
  2. Native Event Dispatching: Updated lib/EventTarget.js to:
    • Accept native events by only checking event._initialized === false.
    • Use Object.defineProperty to assign target, currentTarget, isTrusted, and eventPhase on events with read-only prototype getters.
    • Respect cancelBubble and stopImmediatePropagation() on native events.
  3. Legacy createEvent compatibility: Fallback to passing an empty type to constructor if new constructor() throws due to argument length.

Fixes angular/angular#71977

- Defer to `globalThis.Event` and `globalThis.CustomEvent` (as well as UIEvent and MouseEvent) when available in the runtime environment.
- Allow `EventTarget.prototype._dispatchEvent` to dispatch native `Event` and `CustomEvent` objects by properly defining properties (such as `target`, `currentTarget`, `isTrusted`, `eventPhase`) without failing on read-only getters.
- Support native `stopPropagation()` and `stopImmediatePropagation()`.

Fixes angular/angular#71977
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.

Angular's global patching in @angular/platform-server causes issues for other code expecting node's native event types

1 participant