|
| 1 | +/** |
| 2 | + * connection-ws.ts - handles low level communication with sqlitecloud server via socket.io websocket |
| 3 | + */ |
| 4 | + |
| 5 | +import { SQLiteCloudConfig, SQLCloudRowsetMetadata, SQLiteCloudError, SQLiteCloudDataTypes, ErrorCallback, ResultsCallback } from './types' |
| 6 | +import { SQLiteCloudRowset } from './rowset' |
| 7 | +import { SQLiteCloudConnection, DEFAULT_TIMEOUT, DEFAULT_PORT } from './connection' |
| 8 | +import { anonimizeError, anonimizeCommand } from './connection' |
| 9 | + |
| 10 | +import { io, Socket, SocketOptions } from 'socket.io-client' |
| 11 | + |
| 12 | +/** |
| 13 | + * Implementation of SQLiteCloudConnection that connects directly to the database via tls socket and raw, binary protocol. |
| 14 | + * SQLiteCloud low-level connection, will do messaging, handle socket, authentication, etc. |
| 15 | + * A connection socket is established when the connection is created and closed when the connection is closed. |
| 16 | + * All operations are serialized by waiting for any pending operations to complete. Once a connection is closed, |
| 17 | + * it cannot be reopened and you must create a new connection. |
| 18 | + */ |
| 19 | +export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection { |
| 20 | + /** Parse and validate provided connectionString or configuration */ |
| 21 | + constructor(config: SQLiteCloudConfig | string, callback?: ErrorCallback) { |
| 22 | + super(config, callback) |
| 23 | + } |
| 24 | + |
| 25 | + /** Currently opened tls socket used to communicated with SQLiteCloud server */ |
| 26 | + private socket?: Socket |
| 27 | + |
| 28 | + // |
| 29 | + // public properties |
| 30 | + // |
| 31 | + |
| 32 | + /** True if connection is open */ |
| 33 | + public get connected(): boolean { |
| 34 | + return !!this.socket |
| 35 | + } |
| 36 | + |
| 37 | + // |
| 38 | + // private methods |
| 39 | + // |
| 40 | + |
| 41 | + /* Opens a connection with the server and sends the initialization commands. Will throw in case of errors. */ |
| 42 | + protected connect(callback?: ErrorCallback): this { |
| 43 | + this.operations.enqueue(done => { |
| 44 | + try { |
| 45 | + // connection established while we were waiting in line? |
| 46 | + console.assert(!this.connected, 'Connection already established') |
| 47 | + if (!this.socket) { |
| 48 | + const host = this.config.host as string |
| 49 | + // const connectionString = this.config.connectionString as string |
| 50 | + const gatewayUrl = 'ws://localhost:4000' |
| 51 | + //const gatewayUrl = `ws://${host}:4000` |
| 52 | + const connectionString = 'sqlitecloud://admin:uN3ARhdcKQ@og0wjec-m.sqlite.cloud:8860/chinook.db' |
| 53 | + this.socket = io(gatewayUrl, { auth: { token: connectionString } }) |
| 54 | + } |
| 55 | + callback?.call(this, null) |
| 56 | + done(null) |
| 57 | + } catch (error) { |
| 58 | + callback?.call(this, error as Error) |
| 59 | + done(error as Error) |
| 60 | + } |
| 61 | + }) |
| 62 | + return this |
| 63 | + } |
| 64 | + |
| 65 | + /** Will send a command immediately (no queueing), return the rowset/result or throw an error */ |
| 66 | + protected processCommands(commands: string, callback?: ResultsCallback): this { |
| 67 | + // connection needs to be established? |
| 68 | + if (!this.socket) { |
| 69 | + callback?.call(this, new SQLiteCloudError('Connection not established', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' })) |
| 70 | + return this |
| 71 | + } |
| 72 | + |
| 73 | + this.socket.emit('v1/sql', { sql: commands, row: 'array' }, (response: any) => { |
| 74 | + if (response?.error) { |
| 75 | + const error = new SQLiteCloudError(response.error.detail, { ...response.error }) |
| 76 | + callback?.call(this, error) |
| 77 | + } else { |
| 78 | + console.debug(`SQLiteCloudWebsocketConnection.processCommands - response: ${JSON.stringify(response)}`) |
| 79 | + const { data, metadata } = response |
| 80 | + if (data && metadata) { |
| 81 | + if (metadata.numberOfRows !== undefined && metadata.numberOfColumns !== undefined && metadata.columns !== undefined) { |
| 82 | + // we can recreate a SQLiteCloudRowset from the response |
| 83 | + const rowset = new SQLiteCloudRowset(metadata, data.flat()) |
| 84 | + callback?.call(this, null, rowset) |
| 85 | + return |
| 86 | + } |
| 87 | + } |
| 88 | + callback?.call(this, null, response?.data) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + return this |
| 93 | + } |
| 94 | + |
| 95 | + // |
| 96 | + // public methods |
| 97 | + // |
| 98 | + |
| 99 | + /** Disconnect from server, release connection. */ |
| 100 | + public close(): this { |
| 101 | + console.assert(this.socket !== null, 'SQLiteCloudWsConnection.close - connection already closed') |
| 102 | + if (this.socket) { |
| 103 | + this.socket?.close() |
| 104 | + this.socket = undefined |
| 105 | + } |
| 106 | + this.operations.clear() |
| 107 | + this.socket = undefined |
| 108 | + return this |
| 109 | + } |
| 110 | +} |
0 commit comments