ChainSafe/js-libp2p-noise

How to avoid creating the default Noise instance?

alanshaw opened this issue · 3 comments

In Cloudflare workers you cannot call some crypto functions outside of a request:

Error: Some functionality, such as asynchronous I/O (fetch, Cache API, KV), timeouts (setTimeout, setInterval), and generating random values (crypto.getRandomValues, crypto.subtle.generateKey), can only be performed while handling a request.

This error occurs when importing the module because a default instance is created up front (the NOISE export).

I've considered a couple of solutions:

  1. Change const NOISE to a function NOISE() that lazily creates (and caches) the default instance when called. This would be a BREAKING CHANGE for consumers (albeit an easy one to migrate to).
  2. Add a package export for "./noise": "./dist/src/noise.js" so you can import { Noise } from '@chainsafe/libp2p-noise/noise' to avoid the creation of the default instance altogether. Not a breaking change but a bit clunky.

Let me know what you think or if you have any better solutions 🙂

Any opinions?

I think we plan to release the breaking change very soon anyways and IMO we can remove that default instance.