Skip to content

Commit 26ea2ec

Browse files
Dynamic module imports for browser vs node env
1 parent ff78949 commit 26ea2ec

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

src/connection.ts

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
* connection.ts - handles low level communication with sqlitecloud server
33
*/
44

5-
import { SQLiteCloudConfig, SQLCloudRowsetMetadata, SQLiteCloudError, SQLiteCloudDataTypes, ErrorCallback, ResultsCallback } from './types'
6-
import { SQLiteCloudRowset } from './rowset'
5+
import { SQLiteCloudConfig, SQLiteCloudError, ErrorCallback, ResultsCallback } from './types'
76
import { parseConnectionString, parseBoolean } from './utilities'
87

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+
})
17+
918
/** Default timeout value for queries */
1019
export const DEFAULT_TIMEOUT = 300 * 1000
1120

@@ -49,22 +58,36 @@ export class SQLiteCloudConnection {
4958

5059
protected connect(callback?: ErrorCallback): this {
5160
this.operations.enqueue(done => {
52-
if (false) {
53-
const transport = require('./transport-ws')
54-
this.transport = new transport.WebSocketTransport()
61+
// connect using websocket if tls is not supported or if explicitly requested
62+
if (!tlsSupported || this.config?.websocketOptions?.useWebsocket || this.config?.websocketOptions?.gatewayUrl) {
63+
// socket.io transport works in both node.js and browser environments and connects via SQLite Cloud Gateway
64+
import('./transport-ws')
65+
.then(transport => {
66+
this.transport = new transport.WebSocketTransport()
67+
this.transport.connect(this.config, error => {
68+
if (error) this.close()
69+
callback?.call(this, error)
70+
done(error)
71+
})
72+
})
73+
.catch(error => {
74+
done(error)
75+
})
5576
} else {
56-
const transport = require('./transport-tls')
57-
this.transport = new transport.TlsSocketTransport()
77+
// tls sockets work only in node.js environments
78+
import('./transport-tls')
79+
.then(transport => {
80+
this.transport = new transport.TlsSocketTransport()
81+
this.transport.connect(this.config, error => {
82+
if (error) this.close()
83+
callback?.call(this, error)
84+
done(error)
85+
})
86+
})
87+
.catch(error => {
88+
done(error)
89+
})
5890
}
59-
60-
// ask transport layer to connect
61-
this.transport?.connect(this.config, error => {
62-
if (error) {
63-
this.close()
64-
}
65-
callback?.call(this, error)
66-
done(error)
67-
})
6891
})
6992

7093
return this
@@ -264,3 +287,30 @@ export interface ConnectionTransport {
264287
/** Disconnect from server, release transport. */
265288
close(): this
266289
}
290+
291+
//
292+
// utilities
293+
//
294+
295+
async function loadTlsModuleIfSupported() {
296+
// Check if running in Node.js
297+
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
298+
try {
299+
// Dynamically import the 'tls' module
300+
const tls = await import('tls')
301+
302+
// Check if TLSSocket is available
303+
if (tls && tls.TLSSocket) {
304+
console.log('tls.TLSSocket is supported. Module loaded.')
305+
// Module is supported, you can use it here
306+
// For example, return it or perform further operations
307+
return tls
308+
}
309+
} catch (error) {
310+
console.error('tls module is not supported in this environment.')
311+
}
312+
} else {
313+
console.log('Not running in a Node.js environment.')
314+
}
315+
return null
316+
}

src/transport-tls.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class TlsSocketTransport implements ConnectionTransport {
217217
this.socket?.write(commands, 'utf8', () => {
218218
socketTimeout = setTimeout(() => {
219219
const timeoutError = new SQLiteCloudError('Request timed out', { cause: anonimizeCommand(commands) })
220-
this.log(`Request timed out, config.timeout is ${this.config?.timeout as number}ms`, timeoutError)
220+
console.debug(`Request timed out, config.timeout is ${this.config?.timeout as number}ms`, timeoutError)
221221
finish(timeoutError)
222222
}, this.config?.timeout)
223223
this.socket?.on('data', readData)

test/connection-ws.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* connection-ws.test.ts - test connection via socket.io based gateway
33
*/
44

5-
import { SQLiteCloudWebsocketConnection } from '../src/index'
65
import { SQLiteCloudError } from '../src/index'
76
import { SQLiteCloudConnection, anonimizeCommand } from '../src/connection'
87
import {

0 commit comments

Comments
 (0)