pubkey / rxdb

A fast, local first, reactive Database for JavaScript Applications https://rxdb.info/
https://rxdb.info/
Apache License 2.0
21.57k stars 1.06k forks source link

WebRTC replication plugin throw error in vite env #5841

Closed croatialu closed 6 months ago

croatialu commented 7 months ago

code:

  replicateWebRTC(
    {
      collection: database.users,
      topic: 'my-users-pool',
      connectionHandlerCreator: getConnectionHandlerSimplePeer({}),
      pull: {},
      push: {
      }
    }
  );

error: image

cleech commented 7 months ago

I'm not trying to rule out an issue here, but as a data point I'm using the WebRTC replication with vite as a build tool without problems. Currently using rxdb 15.6.0 and vite 5.2.2

pubkey commented 7 months ago

Seems to come from RxDB and not from simple-peer. Can you enable source maps?

croatialu commented 7 months ago

I'm not trying to rule out an issue here, but as a data point I'm using the WebRTC replication with vite as a build tool without problems. Currently using rxdb 15.6.0 and vite 5.2.2

https://stackblitz.com/edit/vitejs-rxdb-replication-webrtc?file=src%2Fdb%2Fplugins%2Freplication-webrtc%2Fconnection-handler-simple-peer.ts

I tried to reproduce this problem in a new project ("vite": "^5.2.6", "rxdb": "^15.16.0").

https://www.npmjs.com/package/readable-stream#usage-in-browsers In the readable-stream introduction, "Polyfills are no longer required since version 4.2.0."

The version used in simple-peer is ^3.6.0 (https://github.com/feross/simple-peer/blob/f1a492d1999ce727fa87193ebdea20ac89c1fc6d/package.json#L20

My thoughts: The problem may be caused by vite not configuring polyfill by default or using the wrong polyfill when building the simple-peer, and there is a problem when building (warning in the console). If we use simplepeer-min.js directly, which is a built product with polyfill inside, we don't have this problem.

On the whole, Cause of the problem: Some modules used in "simple-peer" lack corresponding polyfill or have problems with polyfill when vite is built. Causing vite to issue a warning.

Problem solution:

  1. The polyfill of the vite configuration module, such as readable-stream, etc. (What if I used a different build tool?)
  2. Directly use simplepeer-min.js with polyfill. (https://github.com/feross/simple-peer/blob/f1a492d1999ce727fa87193ebdea20ac89c1fc6d/package.json#L63)
croatialu commented 7 months ago

Seems to come from RxDB and not from simple-peer. Can you enable source maps?

I think it's a matter of polyfill when building, you can try this: https://stackblitz.com/edit/vitejs-rxdb-replication-webrtc?file=src%2Fdb%2Fplugins%2Freplication-webrtc%2Fconnection-handler-simple-peer.ts

pubkey commented 7 months ago

I merged your PR, release will come in the next days.

pubkey commented 6 months ago

Please reopen if still broken in the latest version.