Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 69c2a8a

Browse files
committed
refactor(connection): rename internal connection => socket
1 parent 8724b83 commit 69c2a8a

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

lib/connection/connection.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Connection extends EventEmitter {
162162
this.queue = [];
163163

164164
// Internal state
165-
this.connection = null;
165+
this.socket = null;
166166
this.writeStream = null;
167167

168168
// Create hash method
@@ -175,14 +175,14 @@ class Connection extends EventEmitter {
175175
}
176176

177177
setSocketTimeout(value) {
178-
if (this.connection) {
179-
this.connection.setTimeout(value);
178+
if (this.socket) {
179+
this.socket.setTimeout(value);
180180
}
181181
}
182182

183183
resetSocketTimeout() {
184-
if (this.connection) {
185-
this.connection.setTimeout(this.socketTimeout);
184+
if (this.socket) {
185+
this.socket.setTimeout(this.socketTimeout);
186186
}
187187
}
188188

@@ -237,11 +237,11 @@ class Connection extends EventEmitter {
237237
}
238238

239239
// clean up existing event handlers
240-
this.connection.removeAllListeners('error');
241-
this.connection.removeAllListeners('timeout');
242-
this.connection.removeAllListeners('close');
243-
this.connection.removeAllListeners('data');
244-
this.connection = undefined;
240+
this.socket.removeAllListeners('error');
241+
this.socket.removeAllListeners('timeout');
242+
this.socket.removeAllListeners('close');
243+
this.socket.removeAllListeners('data');
244+
this.socket = undefined;
245245

246246
return doConnect(this, 4, _options, _errorHandler);
247247
});
@@ -253,28 +253,30 @@ class Connection extends EventEmitter {
253253
* @return {boolean}
254254
*/
255255
unref() {
256-
if (this.connection == null) {
257-
this.once('connect', () => this.connection.unref());
256+
if (this.socket == null) {
257+
this.once('connect', () => this.socket.unref());
258258
return;
259259
}
260260

261-
this.connection.unref();
261+
this.socket.unref();
262262
}
263263

264264
/**
265265
* Destroy connection
266266
* @method
267267
*/
268268
destroy() {
269-
// Set the connections
270-
if (connectionAccounting) deleteConnection(this.id);
271-
if (this.connection) {
269+
if (connectionAccounting) {
270+
deleteConnection(this.id);
271+
}
272+
273+
if (this.socket) {
272274
// Catch posssible exception thrown by node 0.10.x
273275
try {
274-
this.connection.end();
276+
this.socket.end();
275277
} catch (err) {} // eslint-disable-line
276-
// Destroy connection
277-
this.connection.destroy();
278+
279+
this.socket.destroy();
278280
}
279281

280282
this.destroyed = true;
@@ -297,16 +299,16 @@ class Connection extends EventEmitter {
297299
}
298300

299301
// Double check that the connection is not destroyed
300-
if (this.connection.destroyed === false) {
302+
if (this.socket.destroyed === false) {
301303
// Write out the command
302304
if (!Array.isArray(buffer)) {
303-
this.connection.write(buffer, 'binary');
305+
this.socket.write(buffer, 'binary');
304306
return true;
305307
}
306308

307309
// Iterate over all buffers and write them in order to the socket
308310
for (let i = 0; i < buffer.length; i++) {
309-
this.connection.write(buffer[i], 'binary');
311+
this.socket.write(buffer[i], 'binary');
310312
}
311313

312314
return true;
@@ -341,7 +343,7 @@ class Connection extends EventEmitter {
341343
*/
342344
isConnected() {
343345
if (this.destroyed) return false;
344-
return !this.connection.destroyed && this.connection.writable;
346+
return !this.socket.destroyed && this.socket.writable;
345347
}
346348
}
347349

@@ -682,7 +684,7 @@ function merge(options1, options2) {
682684

683685
function makeSSLConnection(self, _options) {
684686
let sslOptions = {
685-
socket: self.connection,
687+
socket: self.socket,
686688
rejectUnauthorized: self.rejectUnauthorized
687689
};
688690

@@ -761,16 +763,14 @@ function makeUnsecureConnection(self, family) {
761763
return connection;
762764
}
763765

764-
function doConnect(self, family, _options, _errorHandler) {
765-
self.connection = self.ssl
766-
? makeSSLConnection(self, _options)
767-
: makeUnsecureConnection(self, family);
766+
function doConnect(conn, family, _options, _errorHandler) {
767+
conn.socket = conn.ssl ? makeSSLConnection(conn, _options) : makeUnsecureConnection(conn, family);
768768

769769
// Add handlers for events
770-
self.connection.once('error', _errorHandler);
771-
self.connection.once('timeout', timeoutHandler(self));
772-
self.connection.once('close', closeHandler(self));
773-
self.connection.on('data', dataHandler(self));
770+
conn.socket.once('error', _errorHandler);
771+
conn.socket.once('timeout', timeoutHandler(conn));
772+
conn.socket.once('close', closeHandler(conn));
773+
conn.socket.on('data', dataHandler(conn));
774774
}
775775

776776
/**

test/tests/functional/connection_tests.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ describe('Connection', function() {
7676
testCase('should connect with no family', {
7777
config: { host: 'localhost' },
7878
connect: connection => {
79-
expect(connection.connection.remotePort).to.equal(server.port);
80-
expect(connection.connection.remoteFamily).to.equal('IPv4');
79+
expect(connection.socket.remotePort).to.equal(server.port);
80+
expect(connection.socket.remoteFamily).to.equal('IPv4');
8181
}
8282
});
8383

8484
testCase('should connect with family=4', {
8585
config: { host: 'localhost', family: 4 },
8686
connect: connection => {
87-
expect(connection.connection.remotePort).to.equal(server.port);
88-
expect(connection.connection.remoteFamily).to.equal('IPv4');
87+
expect(connection.socket.remotePort).to.equal(server.port);
88+
expect(connection.socket.remoteFamily).to.equal('IPv4');
8989
}
9090
});
9191

@@ -101,8 +101,8 @@ describe('Connection', function() {
101101
testCase('should connect with no family', {
102102
config: { host: 'localhost' },
103103
connect: connection => {
104-
expect(connection.connection.remotePort).to.equal(server.port);
105-
expect(connection.connection.remoteFamily).to.equal('IPv6');
104+
expect(connection.socket.remotePort).to.equal(server.port);
105+
expect(connection.socket.remoteFamily).to.equal('IPv6');
106106
}
107107
});
108108

@@ -119,8 +119,8 @@ describe('Connection', function() {
119119
testCase('should connect with family=6', {
120120
config: { host: 'localhost', family: 6 },
121121
connect: connection => {
122-
expect(connection.connection.remotePort).to.equal(server.port);
123-
expect(connection.connection.remoteFamily).to.equal('IPv6');
122+
expect(connection.socket.remotePort).to.equal(server.port);
123+
expect(connection.socket.remoteFamily).to.equal('IPv6');
124124
}
125125
});
126126
});

test/tests/functional/pool_tests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Pool tests', function() {
7171
process.nextTick(() => {
7272
// Now that we are in next tick, connection should still exist, but there
7373
// should be no connect listeners
74-
expect(connection.connection.listenerCount('connect')).to.equal(0);
74+
expect(connection.socket.listenerCount('connect')).to.equal(0);
7575
expect(connections).to.have.lengthOf(1);
7676

7777
pool.destroy();
@@ -86,7 +86,7 @@ describe('Pool tests', function() {
8686

8787
expect(pool.allConnections()).to.have.lengthOf(1);
8888
connection = pool.allConnections()[0];
89-
expect(connection.connection.listenerCount('connect')).to.equal(1);
89+
expect(connection.socket.listenerCount('connect')).to.equal(1);
9090
}
9191
});
9292

0 commit comments

Comments
 (0)