Skip to content

Commit 1ae0fba

Browse files
Config changes
1 parent 7ae5fe7 commit 1ae0fba

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

src/connection.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,10 @@
33
*/
44

55
import { SQLiteCloudConfig, SQLiteCloudError, ErrorCallback, ResultsCallback } from './types'
6-
import { parseConnectionString, parseBoolean } from './utilities'
7-
8-
/** tls.TLSSocket is required to connect using TlsSocketTransport but is only supported in node.js environments */
9-
let tlsSupported = false
10-
import('tls')
11-
.then(() => {
12-
tlsSupported = true
13-
})
14-
.catch(() => {
15-
tlsSupported = false
16-
})
6+
import { parseConnectionString, parseBoolean, isBrowser, isNode } from './utilities'
177

188
/** Default timeout value for queries */
199
export const DEFAULT_TIMEOUT = 300 * 1000
20-
2110
/** Default tls connection port */
2211
export const DEFAULT_PORT = 9960
2312

@@ -60,7 +49,7 @@ export class SQLiteCloudConnection {
6049
protected connect(callback?: ErrorCallback): this {
6150
this.operations.enqueue(done => {
6251
// connect using websocket if tls is not supported or if explicitly requested
63-
if (!tlsSupported || this.config?.websocketOptions?.useWebsocket || this.config?.websocketOptions?.gatewayUrl) {
52+
if (isBrowser || this.config?.useWebsocket || this.config?.gatewayUrl) {
6453
// socket.io transport works in both node.js and browser environments and connects via SQLite Cloud Gateway
6554
import('./transport-ws')
6655
.then(transport => {
@@ -127,6 +116,7 @@ export class SQLiteCloudConnection {
127116
config.sqliteMode = parseBoolean(config.sqliteMode)
128117

129118
if (!config.username || !config.password || !config.host) {
119+
console.error(`SQLiteCloudConnection.validateConfiguration - missing arguments`, config)
130120
throw new SQLiteCloudError('The user, password and host arguments must be specified.', { errorCode: 'ERR_MISSING_ARGS' })
131121
}
132122

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export { SQLiteCloudConfig, SQLCloudRowsetMetadata, SQLiteCloudError, ErrorCallb
88
export { SQLiteCloudRowset, SQLiteCloudRow } from './rowset'
99
export { SQLiteCloudConnection } from './connection'
1010
export { escapeSqlParameter, prepareSql } from './utilities'
11+
export { WebSocketTransport } from './transport-ws'

test/connection-ws.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('connection-ws', () => {
3535

3636
it('should connect with config object string', done => {
3737
const configObj = getChinookConfig()
38-
configObj.websocketOptions = { useWebsocket: true }
38+
configObj.useWebsocket = true
3939
const connection = new SQLiteCloudConnection(configObj)
4040
expect(connection).toBeDefined()
4141
connection.sendCommands('TEST STRING', (error, results) => {
@@ -49,7 +49,7 @@ describe('connection-ws', () => {
4949
const configObj = getChinookConfig()
5050
configObj.connectionString?.replace(configObj.password as string, 'wrongpassword')
5151
configObj.password = 'wrongpassword'
52-
configObj.websocketOptions = { useWebsocket: true }
52+
configObj.useWebsocket = true
5353

5454
// should attemp connection and return error
5555
const connection = new SQLiteCloudConnection(configObj)

test/shared.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ export function getChinookWebsocketConnection(callback?: ResultsCallback, extraC
9494
let chinookConfig = getChinookConfig(CHINOOK_DATABASE_URL, extraConfig)
9595
chinookConfig = {
9696
...chinookConfig,
97-
websocketOptions: {
98-
useWebsocket: true,
99-
gatewayUrl: GATEWAY_URL
100-
}
97+
useWebsocket: true,
98+
gatewayUrl: GATEWAY_URL
10199
}
102100
const chinookConnection = new SQLiteCloudConnection(chinookConfig, callback)
103101
return chinookConnection

test/utilities.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ describe('parseConnectionString', () => {
107107
})
108108
})
109109

110+
it('should parse connection string without database or options', () => {
111+
const connectionString = 'sqlitecloud://user:password@host:1234'
112+
const config = parseConnectionString(connectionString)
113+
114+
expect(config).toEqual({
115+
username: 'user',
116+
password: 'password',
117+
host: 'host',
118+
port: 1234
119+
})
120+
})
121+
122+
it('should parse connection string without port', () => {
123+
const connectionString = 'sqlitecloud://user:password@host'
124+
const config = parseConnectionString(connectionString)
125+
126+
expect(config).toEqual({
127+
username: 'user',
128+
password: 'password',
129+
host: 'host'
130+
})
131+
})
132+
110133
it('should throw SQLiteCloudError if the connection string is invalid', () => {
111134
const connectionString = 'not a valid url'
112135

0 commit comments

Comments
 (0)