|
| 1 | +import { |
| 2 | + connectionWrite, |
| 3 | + createConnection, |
| 4 | + createConnectionTls, |
| 5 | +} from './connection'; |
| 6 | +import { createSkytable } from './skytable'; |
| 7 | +import { bufferToHandshakeResult, getClientHandshake } from './protocol'; |
| 8 | +import type { |
| 9 | + ConnectionOptions as ConnectionTLSOptions, |
| 10 | + TLSSocket, |
| 11 | +} from 'node:tls'; |
| 12 | +import { Socket } from 'node:net'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Configuration for a client connection (single node) |
| 16 | + */ |
| 17 | +export class Config { |
| 18 | + private username: string; |
| 19 | + private password: string; |
| 20 | + private host: string; |
| 21 | + private port: number; |
| 22 | + private connection: Socket | TLSSocket | undefined; |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new configuration |
| 26 | + * |
| 27 | + * @param username Set the username for authenticating with Skytable |
| 28 | + * @param password Set the password for authenticating with Skytable |
| 29 | + * @param host Set the host to connect to Skytable (defaults to `127.0.0.1`) |
| 30 | + * @param port Set the port to connect to Skytable (defaults to 2003) |
| 31 | + */ |
| 32 | + constructor( |
| 33 | + username: string, |
| 34 | + password: string, |
| 35 | + host: string = 'localhost', |
| 36 | + port: number = 2003, |
| 37 | + ) { |
| 38 | + this.username = username; |
| 39 | + this.password = password; |
| 40 | + this.host = host; |
| 41 | + this.port = port; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get the set username |
| 46 | + * @returns Username set in this configuration |
| 47 | + */ |
| 48 | + getUsername(): string { |
| 49 | + return this.username; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the set password |
| 54 | + * @returns Username set in this configuration |
| 55 | + */ |
| 56 | + getPassword(): string { |
| 57 | + return this.password; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the set host |
| 62 | + * @returns Username set in this configuration |
| 63 | + */ |
| 64 | + getHost(): string { |
| 65 | + return this.host; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the set port |
| 70 | + * @returns Username set in this configuration |
| 71 | + */ |
| 72 | + getPort(): number { |
| 73 | + return this.port; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the connection |
| 78 | + * @returns The current connection |
| 79 | + */ |
| 80 | + getConnection() { |
| 81 | + return this.connection; |
| 82 | + } |
| 83 | + |
| 84 | + async connect() { |
| 85 | + const connect = await createConnection({ |
| 86 | + port: this.port, |
| 87 | + host: this.host, |
| 88 | + }); |
| 89 | + |
| 90 | + const data = await connectionWrite(connect, getClientHandshake(this)); |
| 91 | + |
| 92 | + await bufferToHandshakeResult(data); |
| 93 | + |
| 94 | + this.connection = connect; |
| 95 | + |
| 96 | + return createSkytable(connect); |
| 97 | + } |
| 98 | + |
| 99 | + async connectTSL(options: ConnectionTLSOptions) { |
| 100 | + const connect = await createConnectionTls({ |
| 101 | + port: this.port, |
| 102 | + host: this.host, |
| 103 | + ...options, |
| 104 | + }); |
| 105 | + |
| 106 | + const data = await connectionWrite(connect, getClientHandshake(this)); |
| 107 | + |
| 108 | + await bufferToHandshakeResult(data); |
| 109 | + |
| 110 | + this.connection = connect; |
| 111 | + |
| 112 | + return createSkytable(connect); |
| 113 | + } |
| 114 | + |
| 115 | + async disconnect() { |
| 116 | + if (this.connection) { |
| 117 | + this.connection.destroySoon(); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments