electric-sql / pglite

Lightweight Postgres packaged as WASM into a TypeScript library for the browser, Node.js, Bun and Deno
https://electric-sql.com
Apache License 2.0
4.76k stars 81 forks source link

Calling `execProtocol` when PGlite is not ready #74

Open jgoux opened 2 months ago

jgoux commented 2 months ago

Hey, it's me again. 😄

execProtocol is not checking if the database is ready, which triggers this error when called externally to send wire protocol messages:

TypeError: undefined is not an object (evaluating 'this.emp._malloc')

I see 3 solutions:

In the end, I think execProtocol is exposed for low-level implementations such as pglite-server so I wouldn't do extra work in execProtocol.

samwillis commented 2 months ago

Adding note here for others after we chatted on Discord here: https://discord.com/channels/933657521581858818/1212676471588520006/1225898205292793928

Good idea on making PGlite an EventEmitter, we should do that, and it's needed for when we support pg_notify.

It's currently possible to await pg.waitReady, this is a promise that resolves at the end of the init. We can't wait for that inside execProtocol as execProtocol is itself used inside the init and that would result in a deadlock.

I've purposely not documented execProtocol yet as I want feedback on its design, and whats the best way for it to work for the server simulation use case.

We could have public and privet versions of execProtocol, an internal one without waitRead and a public one with it.

jgoux commented 2 months ago

I missed waitReady! For now, it's more than enough for my use case. 👍