Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior and code when connecting #178

Closed
jacobprall opened this issue Dec 20, 2024 · 0 comments
Closed

Strange behavior and code when connecting #178

jacobprall opened this issue Dec 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jacobprall
Copy link

Current Behavior

I tried to initialize a connection with the driver on my local.

const db = new Database({
  connectionstring: 'sqlitecloud://cpt96oeuik.sqlite.cloud:8860/chinook.sqlite?apikey=<api-key-redacted>',
  verbose: true
})

It kept failing with this output:

SQLiteCloudConnection.connect - error connecting cpt96oeuik.sqlite.cloud:8860 SQLiteCloudError: Connection not established 111 | }
112 | 
113 | /** Custom error reported by SQLiteCloud drivers */
114 | export class SQLiteCloudError extends Error {
115 |   constructor(message: string, args?: Partial<SQLiteCloudError>) {
116 |     super(message)
          ^
SQLiteCloudError: Connection not established
      at new SQLiteCloudError (/Users/colossus/Desktop/code/sqlite-cloud-repos/sqlitecloud-js/src/drivers/types.ts:116:5)
      at transportCommands (/Users/colossus/Desktop/code/sqlite-cloud-repos/sqlitecloud-js/src/drivers/connection-tls.ts:108:28)
      at /Users/colossus/Desktop/code/sqlite-cloud-repos/sqlitecloud-js/src/drivers/connection-tls.ts:73:7
      at handshake (node:net:91:27)

processCommandsFinish - error with no registered callback 111 | }
112 | 
113 | /** Custom error reported by SQLiteCloud drivers */
114 | export class SQLiteCloudError extends Error {
115 |   constructor(message: string, args?: Partial<SQLiteCloudError>) {
116 |     super(message)
          ^
SQLiteCloudError: Connection closed
      at new SQLiteCloudError (/Users/colossus/Desktop/code/sqlite-cloud-repos/sqlitecloud-js/src/drivers/types.ts:116:5)
      at /Users/colossus/Desktop/code/sqlite-cloud-repos/sqlitecloud-js/src/drivers/connection-tls.ts:97:34
      at node:net:12:43

I tracked down the issue to connection-tls. In connection-tls,

// connectTransport()
// ...
    this.socket = connector(connectionOptions, () => {
      if (this.config.verbose) {
        console.debug(`SQLiteCloudTlsConnection - connected to ${this.config.host}, authorized: ${this.socket?.authorized}`)
      }
      this.transportCommands(initializationCommands, error => {
        if (this.config.verbose) {
          console.debug(`SQLiteCloudTlsConnection - initialized connection`)
        }
        callback?.call(this, error)
      })
    })

As you can see, we call transportCommands in the connectTransport method of the SQLiteCloudTlsConnection class.

However, the transport method requires this.socket to be defined here:

  transportCommands(commands: string | SQLiteCloudCommand, callback?: ResultsCallback): this {
    // connection needs to be established?
    if (!this.socket) {
      callback?.call(this, new SQLiteCloudError('Connection not established', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' }))
      return this
    }

// ...

I think this may be the issue.
To replicate, create a file that imports Database and try to instantiate a connection

@jacobprall jacobprall added the bug Something isn't working label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant