How to avoid creating the default Noise instance?
alanshaw opened this issue · 3 comments
alanshaw commented
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:
- Change
const NOISE
to afunction 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). - Add a package export for
"./noise": "./dist/src/noise.js"
so you canimport { 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 🙂
alanshaw commented
Any opinions?
mpetrunic commented
I think we plan to release the breaking change very soon anyways and IMO we can remove that default instance.
mpetrunic commented
🤖 Release is at https://github.com/ChainSafe/js-libp2p-noise/releases/tag/v8.0.0 🌻