17
17
* You should have received a copy of the GNU General Public License
18
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
*/
20
- import net from 'net'
20
+ import net from 'node: net'
21
21
import tls from 'tls'
22
22
23
+ // CursusDB Cluster Client class
24
+ class Client {
25
+ constructor ( host , port , username , password , tls ) {
26
+ this . host = host ;
27
+ this . port = port ;
28
+ this . username = username ;
29
+ this . password = password ;
30
+ this . tls = tls ;
31
+ }
23
32
24
- const cluster = {
25
- tls : false ,
26
- socket : undefined ,
27
- connect : connect ,
28
- query : query ,
29
- close : close ,
30
- }
31
- /* Connect
32
- ** host - cluster host
33
- ** port - cluster port
34
- ** username - database user username
35
- ** password - database user password
36
- ** tls bool
37
- */
38
- async function connect ( host , port , username , password , tlsIn ) {
39
- cluster . tls = tlsIn
40
- cluster . socket = tlsIn ? new tls . TLSSocket ( ) : new net . Socket ( )
33
+ Connect ( ) {
41
34
return new Promise ( ( resolve , reject ) => {
42
- cluster . socket . connect ( port , host , function ( ) {
43
- cluster . socket . write ( "Authentication: " + Buffer . from ( username + "\\0" + password ) . toString ( 'base64' ) + "\r\n" ) ;
35
+ this . socket = ( this . tls ? tls : net ) . createConnection ( { host : this . host , port : this . port } , ( ) => {
36
+ this . socket . write ( "Authentication: " + Buffer . from ( this . username + "\\0" + this . password ) . toString ( 'base64' ) + "\r\n" ) ;
44
37
45
- cluster . socket . on ( 'data' , function ( data ) {
38
+ this . socket . on ( 'data' , function ( data ) {
46
39
if ( data . toString ( ) . startsWith ( "0" ) ) {
47
- resolve ( cluster )
40
+ resolve ( "Connected to CursusDB cluster successfully." )
48
41
} else {
49
42
reject ( data . toString ( ) )
50
43
}
51
44
} ) ;
52
-
53
- } ) ;
54
-
55
- } )
45
+ } ) ;
46
+ } )
47
+ }
56
48
57
-
58
-
59
-
60
- }
61
-
62
- async function query ( queryString ) {
49
+ Query ( queryString ) {
63
50
return new Promise ( ( resolve , reject ) => {
64
- cluster . socket . write ( queryString + "\r\n" ) ;
51
+ this . socket . write ( queryString + "\r\n" ) ;
65
52
66
- cluster . socket . on ( 'data' , function ( data ) {
53
+ this . socket . on ( 'data' , function ( data ) {
67
54
resolve ( data . toString ( ) )
68
55
} ) ;
69
56
70
57
} )
71
-
72
- }
58
+ }
73
59
74
- async function close ( ) {
75
- cluster . socket . end ( )
76
-
77
- }
60
+ Close ( ) {
61
+ this . socket . end ( )
62
+ }
78
63
64
+ }
79
65
80
66
81
- export default cluster
67
+ export default Client
0 commit comments