1717* You should have received a copy of the GNU General Public License
1818* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919 */
20- import net from 'net'
20+ import net from 'node: net'
2121import tls from 'tls'
2222
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+ }
2332
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 ( ) {
4134 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" ) ;
4437
45- cluster . socket . on ( 'data' , function ( data ) {
38+ this . socket . on ( 'data' , function ( data ) {
4639 if ( data . toString ( ) . startsWith ( "0" ) ) {
47- resolve ( cluster )
40+ resolve ( "Connected to CursusDB cluster successfully." )
4841 } else {
4942 reject ( data . toString ( ) )
5043 }
5144 } ) ;
52-
53- } ) ;
54-
55- } )
45+ } ) ;
46+ } )
47+ }
5648
57-
58-
59-
60- }
61-
62- async function query ( queryString ) {
49+ Query ( queryString ) {
6350 return new Promise ( ( resolve , reject ) => {
64- cluster . socket . write ( queryString + "\r\n" ) ;
51+ this . socket . write ( queryString + "\r\n" ) ;
6552
66- cluster . socket . on ( 'data' , function ( data ) {
53+ this . socket . on ( 'data' , function ( data ) {
6754 resolve ( data . toString ( ) )
6855 } ) ;
6956
7057 } )
71-
72- }
58+ }
7359
74- async function close ( ) {
75- cluster . socket . end ( )
76-
77- }
60+ Close ( ) {
61+ this . socket . end ( )
62+ }
7863
64+ }
7965
8066
81- export default cluster
67+ export default Client
0 commit comments