Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sqlitecloud/drivers",
"version": "1.0.417",
"version": "1.0.421",
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
25 changes: 17 additions & 8 deletions src/drivers/connection-tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
* connection-tls.ts - connection via tls socket and sqlitecloud protocol
*/

import { type SQLiteCloudConfig, SQLiteCloudError, type ErrorCallback, type ResultsCallback, SQLiteCloudCommand } from './types'
import { SQLiteCloudConnection } from './connection'
import { getInitializationCommands } from './utilities'
import {
bufferEndsWith,
CMD_COMPRESSED,
CMD_ROWSET_CHUNK,
decompressBuffer,
formatCommand,
hasCommandLength,
parseCommandLength,
popData,
decompressBuffer,
parseRowsetChunks,
CMD_COMPRESSED,
CMD_ROWSET_CHUNK,
bufferEndsWith,
popData,
ROWSET_CHUNKS_END
} from './protocol'
import { type ErrorCallback, type ResultsCallback, SQLiteCloudCommand, type SQLiteCloudConfig, SQLiteCloudError } from './types'
import { getInitializationCommands } from './utilities'

// explicitly importing buffer library to allow cross-platform support by replacing it
import { Buffer } from 'buffer'
Expand Down Expand Up @@ -77,6 +77,10 @@ export class SQLiteCloudTlsConnection extends SQLiteCloudConnection {
callback?.call(this, error)
})
})
this.socket.setKeepAlive(true)
// disable Nagle algorithm because we want our writes to be sent ASAP
// https://brooker.co.za/blog/2024/05/09/nagle.html
this.socket.setNoDelay(true)

this.socket.on('data', data => {
this.processCommandsData(data)
Expand All @@ -97,6 +101,11 @@ export class SQLiteCloudTlsConnection extends SQLiteCloudConnection {
this.processCommandsFinish(new SQLiteCloudError('Connection closed', { errorCode: 'ERR_CONNECTION_CLOSED' }))
})

this.socket.on('timeout', () => {
this.close()
this.processCommandsFinish(new SQLiteCloudError('Connection ened due to timeout', { errorCode: 'ERR_CONNECTION_TIMEOUT' }))
})

return this
}

Expand Down Expand Up @@ -241,7 +250,7 @@ export class SQLiteCloudTlsConnection extends SQLiteCloudConnection {
}

/** Disconnect immediately, release connection, no events. */
close(): this {
public close(): this {
if (this.socket) {
this.socket.removeAllListeners()
this.socket.destroy()
Expand Down
27 changes: 21 additions & 6 deletions src/drivers/connection-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* transport-ws.ts - handles low level communication with sqlitecloud server via socket.io websocket
*/

import { SQLiteCloudConfig, SQLiteCloudError, ErrorCallback, ResultsCallback, SQLiteCloudCommand, SQLiteCloudDataTypes } from './types'
import { SQLiteCloudRowset } from './rowset'
import { SQLiteCloudConnection } from './connection'
import { io, Socket } from 'socket.io-client'
import { SQLiteCloudConnection } from './connection'
import { SQLiteCloudRowset } from './rowset'
import { ErrorCallback, ResultsCallback, SQLiteCloudCommand, SQLiteCloudConfig, SQLiteCloudError } from './types'

/**
* Implementation of TransportConnection that connects to the database indirectly
Expand All @@ -19,7 +19,7 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection {

/** True if connection is open */
get connected(): boolean {
return !!this.socket
return !!(this.socket && this.socket?.connected)
}

/* Opens a connection with the server and sends the initialization commands. Will throw in case of errors. */
Expand All @@ -32,11 +32,25 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection {
const connectionstring = this.config.connectionstring as string
const gatewayUrl = this.config?.gatewayurl || `${this.config.host === 'localhost' ? 'ws' : 'wss'}://${this.config.host as string}:4000`
this.socket = io(gatewayUrl, { auth: { token: connectionstring } })

this.socket.on('connect', () => {
callback?.call(this, null)
})

this.socket.on('disconnect', (reason) => {
this.close()
callback?.call(this, new SQLiteCloudError('Disconnected', { errorCode: 'ERR_CONNECTION_DISCONNECTED', cause: reason }))
})

this.socket.on('error', (error: Error) => {
this.close()
callback?.call(this, new SQLiteCloudError('Connection error', { errorCode: 'ERR_CONNECTION_ERROR', cause: error }))
})
}
callback?.call(this, null)
} catch (error) {
callback?.call(this, error as Error)
}

return this
}

Expand Down Expand Up @@ -78,11 +92,12 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection {
public close(): this {
console.assert(this.socket !== null, 'SQLiteCloudWebsocketConnection.close - connection already closed')
if (this.socket) {
this.socket?.removeAllListeners()
this.socket?.close()
this.socket = undefined
}

this.operations.clear()
this.socket = undefined
return this
}
}
Expand Down
Loading
Loading