Skip to content

Commit 277836e

Browse files
author
Alex Gaetano Padula
committed
correct tls connectivity logic
1 parent 166648f commit 277836e

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

main.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,32 @@ class Client {
3232

3333
Connect() {
3434
return new Promise((resolve, reject) => {
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");
37-
38-
this.socket.on('data', function (data) {
39-
if (data.toString().startsWith("0")) {
40-
resolve("Connected to CursusDB cluster successfully.")
41-
} else {
42-
reject(data.toString())
43-
}
35+
if(this.tls) {
36+
this.socket = tls.connect({ host: this.host, port: this.port }, () => {
37+
this.socket.write("Authentication: " + Buffer.from( this.username + "\\0" + this.password).toString('base64') +"\r\n");
38+
39+
this.socket.on('data', function (data) {
40+
if (data.toString().startsWith("0")) {
41+
resolve("Connected to CursusDB cluster successfully.")
42+
} else {
43+
reject(data.toString())
44+
}
45+
});
46+
});
47+
} else {
48+
this.socket = net.createConnection({ host: this.host, port: this.port }, () => {
49+
this.socket.write("Authentication: " + Buffer.from( this.username + "\\0" + this.password).toString('base64') +"\r\n");
50+
51+
this.socket.on('data', function (data) {
52+
if (data.toString().startsWith("0")) {
53+
resolve("Connected to CursusDB cluster successfully.")
54+
} else {
55+
reject(data.toString())
56+
}
57+
});
4458
});
45-
});
59+
}
60+
4661
})
4762
}
4863

0 commit comments

Comments
 (0)