rust-libp2p icon indicating copy to clipboard operation
rust-libp2p copied to clipboard

feat: enable `websocket-websys` in NodeJS environments.

Open indietyp opened this issue 2 years ago • 2 comments

Description

This enables websocket-websys in NodeJS environments through the following mechanisms:

  1. WebContext has a new Unknown variant, which is only initialized if setInterval and clearInterval are available
  2. dial() will panic if WebSocket is unavailable. To circumvent any speed issues, the value is cached in an atomic and retrieved when asked multiple times.

(2) is essential, as otherwise, we try to access an object that doesn't exist; from my experience, this will make NodeJS do weird things, halting execution immediately of the whole script, returning success, and swallowing any error.

Fixes #5024

Change checklist

  • [x] I have performed a self-review of my own code
  • [x] I have made corresponding changes to the documentation
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [x] A changelog entry has been made in the appropriate crates

(3) is a bit hard here, as far as I know we don't yet really have a test harness for both web workers and nodejs environments, is that correct?

indietyp avatar Dec 22 '23 09:12 indietyp

This also fixes another bug that was present (unintentionally?) where the code would correctly detect the worker scope but then refer to the globals instead of the worker scope.

indietyp avatar Dec 22 '23 09:12 indietyp

This pull request has merge conflicts. Could you please resolve them @indietyp? 🙏

mergify[bot] avatar Apr 15 '24 09:04 mergify[bot]