JS @libp2p incompatible w Cloudflare Worker (Wrangler)

Hi all, I’m looking to make a PR to the js-libp2p package due to some compatibility issues with Cloudflare Workers stemming from a particular syntax choice.

The PR would not affect functionality and would merely be a syntaxical update but I wanted to get people’s thoughts on it.

When importing helia and @helia/mfs into a Worker (wrangler v3.47.1, latest) , it panics being unable to resolve the following line in @libp2p/interface/src/events.ts:

import { setMaxListeners as nodeSetMaxListeners } from 'events'

Instead, manually changing it to the following has worked

import { EventEmitter } from 'events';
...
const emitter = new EventEmitter()
emitter.setMaxListeners(n, ...eventTargets);

For additional context on Cloudflare Wrangler and their ability to use NodeJS APIs, see here. They seem to have two approaches - use the APIs directly or polyfill some. For some reason, it struggles to polyfill Node’s EventEmitter API using the current syntax but it can do so just fine with the updated syntax provided above.

Any and all thoughts are much appreciated!

1 Like