Skip to content

Commit ef6b5d3

Browse files
committed
More refactoring.
1 parent b7fe3dc commit ef6b5d3

File tree

5 files changed

+313
-421
lines changed

5 files changed

+313
-421
lines changed

lib/connection.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import EventEmitter from 'node:events'
33

44
import { SSLError, ConnectionError,ClientStateError, AuthenticationError } from './errors.js'
55
import { Query } from './query.js'
6-
import { FrontendMessage } from './frontend_message.js'
6+
import { StartupMessage, SSLRequestMessage } from './frontend_message.js'
77
import { ParseMessageFromBuffer } from './backend_message.js'
88
import { AuthenticationMethods } from './authentication.js';
99
import { startTLS } from './starttls.js';
@@ -52,7 +52,7 @@ class Connection extends EventEmitter {
5252
this.connection.setKeepAlive(true);
5353
}
5454
if (this.connectionOptions.ssl) {
55-
this.writeMessage(new FrontendMessage.SSLRequest);
55+
this.writeMessage(new SSLRequestMessage);
5656
this.connection.once('data', function(buffer) {
5757
var conn, err, sslOptions;
5858
if ('S' === buffer.toString('utf-8')) {
@@ -110,7 +110,7 @@ class Connection extends EventEmitter {
110110
this._onError(error);
111111
}
112112
if (this.connection.connected) {
113-
this.writeMessage(new FrontendMessage.Terminate());
113+
this.writeMessage(new TerminateMessage());
114114
}
115115
return this.connection.end();
116116
}
@@ -184,7 +184,7 @@ class Connection extends EventEmitter {
184184
});
185185
case AuthenticationMethods.CLEARTEXT_PASSWORD:
186186
case AuthenticationMethods.MD5_PASSWORD:
187-
this.writeMessage(new FrontendMessage.Password(this.connectionOptions.password, msg.method, {
187+
this.writeMessage(new PasswordMessage(this.connectionOptions.password, msg.method, {
188188
salt: msg.salt,
189189
user: this.connectionOptions.user
190190
}));
@@ -195,7 +195,7 @@ class Connection extends EventEmitter {
195195
}
196196

197197
this.connection.on('data', this._onData.bind(this));
198-
this.writeMessage(new FrontendMessage.Startup(this.connectionOptions.user, this.connectionOptions.database));
198+
this.writeMessage(new StartupMessage(this.connectionOptions.user, this.connectionOptions.database));
199199

200200
this.once('ErrorResponse', authenticationFailureHandler);
201201
this.once('Authentication', authenticationHandler);
@@ -312,24 +312,23 @@ class Connection extends EventEmitter {
312312
}
313313

314314
_onData(buffer) {
315-
var bufferedData, message, size;
315+
316316
if (this.incomingData.length === 0) {
317317
this.incomingData = buffer;
318318
} else {
319-
bufferedData = Buffer.alloc(this.incomingData.length + buffer.length);
319+
const bufferedData = Buffer.alloc(this.incomingData.length + buffer.length);
320320
this.incomingData.copy(bufferedData);
321321
buffer.copy(bufferedData, this.incomingData.length);
322322
this.incomingData = bufferedData;
323323
}
324324
while (this.incomingData.length >= 5) {
325-
size = this.incomingData.readUInt32BE(1);
325+
const size = this.incomingData.readUInt32BE(1);
326326
if (size + 1 <= this.incomingData.length) {
327-
message = ParseMessageFromBuffer(this.incomingData.slice(0, size + 1));
328-
console.debug(typeof message.constructor.name);
327+
const msg = ParseMessageFromBuffer(this.incomingData.slice(0, size + 1));
329328
if (this.debug) {
330-
console.debug('<=', message);
329+
console.debug('<=', msg);
331330
}
332-
this.emit(message.constructor.name, message);
331+
this.emit(msg.constructor.name, msg);
333332
this.incomingData = this.incomingData.slice(size + 1);
334333
} else {
335334
break;
@@ -371,7 +370,7 @@ class Connection extends EventEmitter {
371370

372371
writeMessage(msg, callback) {
373372
if (this.debug) {
374-
console.log('=>', msg);
373+
console.debug('=>', msg);
375374
}
376375
return this.connection.write(msg.toBuffer(), callback);
377376
};

lib/connection.test.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,6 @@ import net from 'net'
44

55
import * as Vertica from './vertica'
66

7-
class Connection {
8-
constructor() {
9-
this.busy = false
10-
this.connected = false
11-
this._socket = null
12-
}
13-
14-
static create(connectionOptions, callback) {
15-
const connection = new Connection()
16-
connection._socket = net.createConnection(connectionOptions, () => {
17-
console.log("connect")
18-
connection.connected = true
19-
callback(null, connection)
20-
})
21-
22-
connection._socket.on('error', (err) => {
23-
callback(err, null)
24-
})
25-
26-
return connection
27-
}
28-
}
29-
307
describe.runIf(fs.existsSync('./test/connection.json'))('Vertica.connect', () => {
318
const connectionInfo = JSON.parse(fs.readFileSync('./test/connection.json'))
329

0 commit comments

Comments
 (0)