|
2 | 2 | * connection.ts - handles low level communication with sqlitecloud server |
3 | 3 | */ |
4 | 4 |
|
5 | | -import { SQLiteCloudConfig, SQLCloudRowsetMetadata, SQLiteCloudError, SQLiteCloudDataTypes, ErrorCallback, ResultsCallback } from './types' |
6 | | -import { SQLiteCloudRowset } from './rowset' |
| 5 | +import { SQLiteCloudConfig, SQLiteCloudError, ErrorCallback, ResultsCallback } from './types' |
7 | 6 | import { parseConnectionString, parseBoolean } from './utilities' |
8 | 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 | + }) |
| 17 | + |
9 | 18 | /** Default timeout value for queries */ |
10 | 19 | export const DEFAULT_TIMEOUT = 300 * 1000 |
11 | 20 |
|
@@ -49,22 +58,36 @@ export class SQLiteCloudConnection { |
49 | 58 |
|
50 | 59 | protected connect(callback?: ErrorCallback): this { |
51 | 60 | 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 | + }) |
55 | 76 | } 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 | + }) |
58 | 90 | } |
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 | | - }) |
68 | 91 | }) |
69 | 92 |
|
70 | 93 | return this |
@@ -264,3 +287,30 @@ export interface ConnectionTransport { |
264 | 287 | /** Disconnect from server, release transport. */ |
265 | 288 | close(): this |
266 | 289 | } |
| 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 | +} |
0 commit comments