mpetazzoni/sse.js

CustomEvent is not defined

Closed this issue · 9 comments

I think this needs the dom lib right?

No, although if you're using IE it's possible you're lacking support for the CustomEvent constructor:

https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent#Browser_compatibility

But really, you should not be using IE.

To explain this: this issue is ment, when you want to use it serverside.
There the lib is not working at all. For solving this, used jsdom and injected the related dependencies into the module (i know its hakish as anything, but worked ;) )

Ah, yeah. Never intended for this to be used server-side, so never bothered to test that. You can also probably use the custom-event-polyfill instead, which might be more lightweight that the whole jsdom.

Yep, i tried it, but there have been still some issues with that (maybe i did something wrong there). Anyways, the hackish approach works fine for me, so if anyone get into these troubles, he has a solution ;)

@nilsroesel : Hi, I am using this lib server side as well and see the same issue. Would you be kind enough to share how this is implemented. Code snippet much appreciated

@mpetazzoni Any plans to get this working server-side with Node? custom-event-polyfill isn't working.

@0xTomDaniel CustomEvent should be available (as experimental) in more recent versions of Node, without the polyfill. Can you share what you've tried so far, and what errors / issues you're seeing? 🙏🏻

Thanks for the quick reply!

I'm on Node v18.18 (which should be supported as you said according to https://nodejs.org/api/events.html#class-customevent). I'm receiving the following error when running a Mocha test calling a POST SSE endpoint:

ReferenceError: CustomEvent is not defined
at SSE._setReadyState ([REDACTED]/node_modules/sse.js/lib/sse.js:120:17)
at SSE.stream ([REDACTED]/node_modules/sse.js/lib/sse.js:250:10)
at new SSE ([REDACTED]/node_modules/sse.js/lib/sse.js:282:10)

It seems to occur as soon as addEventListener receives a response.

changes:
  - version: v19.0.0
    pr-url: https://github.com/nodejs/node/pull/44860
    description: No longer behind `--experimental-global-customevent` CLI flag.

Seems like with Node v18 you need to run with --experimental-global-customevent ?