tursodatabase / libsql-client-ts

TypeScript/JavaScript client API for libSQL
https://docs.turso.tech/sdk/ts/quickstart
MIT License
289 stars 37 forks source link

Check at runtime if @libsql/client will actually work before importing it #4

Closed CodingDoug closed 1 year ago

CodingDoug commented 1 year ago

When someone unknowingly uses @libsql/client in an environment where nodejs APIs are not available, such as a browser, they get a mostly unhelpful error:

(index):103 Uncaught (in promise) TypeError: promisify is not a function
    at node_modules/.pnpm/better-sqlite3@8.1.0/node_modules/better-sqlite3/lib/methods/backup.js (backup.js:6:18)
    at __require2 (@libsql_client.js?v=282a651a:10:50)
    at node_modules/.pnpm/better-sqlite3@8.1.0/node_modules/better-sqlite3/lib/database.js (database.js:72:29)
    at __require2 (@libsql_client.js?v=282a651a:10:50)
    at node_modules/.pnpm/better-sqlite3@8.1.0/node_modules/better-sqlite3/lib/index.js (index.js:2:18)
    at __require2 (@libsql_client.js?v=282a651a:10:50)
    at node_modules/.pnpm/@libsql+client@0.0.5/node_modules/@libsql/client/lib/lib/driver/SqliteDriver.js (SqliteDriver.js:43:40)
    at __require2 (@libsql_client.js?v=282a651a:10:50)
    at node_modules/.pnpm/@libsql+client@0.0.5/node_modules/@libsql/client/lib/lib/libsql-js.js (libsql-js.js:41:22)
    at __require2 (@libsql_client.js?v=282a651a:10:50)
    at node_modules/.pnpm/@libsql+client@0.0.5/node_modules/@libsql/client/lib/index.js (index.js:17:14)
    at __require2 (@libsql_client.js?v=282a651a:10:50)
    at index.js:17:49
node_modules/.pnpm/better-sqlite3@8.1.0/node_modules/better-sqlite3/lib/methods/backup.js   @   backup.js:6
__require2  @   @libsql_client.js?v=282a651a:10
node_modules/.pnpm/better-sqlite3@8.1.0/node_modules/better-sqlite3/lib/database.js @   database.js:72
__require2  @   @libsql_client.js?v=282a651a:10
node_modules/.pnpm/better-sqlite3@8.1.0/node_modules/better-sqlite3/lib/index.js    @   index.js:2
__require2  @   @libsql_client.js?v=282a651a:10
node_modules/.pnpm/@libsql+client@0.0.5/node_modules/@libsql/client/lib/lib/driver/SqliteDriver.js  @   SqliteDriver.js:43
__require2  @   @libsql_client.js?v=282a651a:10
node_modules/.pnpm/@libsql+client@0.0.5/node_modules/@libsql/client/lib/lib/libsql-js.js    @   libsql-js.js:41
__require2  @   @libsql_client.js?v=282a651a:10
node_modules/.pnpm/@libsql+client@0.0.5/node_modules/@libsql/client/lib/index.js    @   index.js:17
__require2  @   @libsql_client.js?v=282a651a:10
(anonymous)

This is a side-effect of eagerly importing better-sqlite3 which depends on node API. It would be better if we can detect this scenario at runtime before importing it, then produce a more helpful error message telling the developer to use @libsql/client/http instead.

honzasp commented 1 year ago

It is unclear how we could do this, because we have very little control about errors during imports.

Also, the client has been rewritten significantly since this issue was created, so perhaps the problem is no longer so acute? For example, I used a wrong import a few days ago, and I received a quite understandable error message saying that it's not possible to require("fs").

honzasp commented 1 year ago

Doug, feel free to reopen if this is still a problem :)