Skip to content

Commit 31c9d9c

Browse files
Merge pull request #738 from tediousjs/arthur/fix-deprecation-warning
Fix `tls.createSecurePair` deprecation warning
2 parents 82214ab + 638a804 commit 31c9d9c

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"big-number": "0.3.1",
4545
"bl": "^1.2.2",
4646
"depd": "^1.1.2",
47+
"native-duplexpair": "^1.0.0",
4748
"iconv-lite": "^0.4.23",
4849
"punycode": "^2.1.0",
4950
"readable-stream": "^2.3.6",

src/message-io.js

+25-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const tls = require('tls');
22
const crypto = require('crypto');
3+
const DuplexPair = require('native-duplexpair');
34
const EventEmitter = require('events').EventEmitter;
45
const Transform = require('readable-stream').Transform;
56

@@ -58,6 +59,8 @@ module.exports = class MessageIO extends EventEmitter {
5859
this._packetSize = _packetSize;
5960
this.debug = debug;
6061

62+
this.tlsNegotiationComplete = false;
63+
6164
this.packetStream = new ReadablePacketStream();
6265
this.packetStream.on('data', (packet) => {
6366
this.logPacket('Received', packet);
@@ -83,47 +86,37 @@ module.exports = class MessageIO extends EventEmitter {
8386
startTls(credentialsDetails, hostname, trustServerCertificate) {
8487
const credentials = tls.createSecureContext ? tls.createSecureContext(credentialsDetails) : crypto.createCredentials(credentialsDetails);
8588

86-
this.securePair = tls.createSecurePair(credentials);
87-
this.tlsNegotiationComplete = false;
88-
89-
this.securePair.on('secure', () => {
90-
const cipher = this.securePair.cleartext.getCipher();
89+
const duplexpair = new DuplexPair();
90+
const securePair = this.securePair = {
91+
cleartext: tls.connect({
92+
socket: duplexpair.socket1,
93+
servername: hostname,
94+
secureContext: credentials,
95+
rejectUnauthorized: !trustServerCertificate
96+
}),
97+
encrypted: duplexpair.socket2
98+
};
9199

92-
if (!trustServerCertificate) {
93-
let verifyError = this.securePair.ssl.verifyError();
94-
95-
// Verify that server's identity matches it's certificate's names
96-
if (!verifyError) {
97-
verifyError = tls.checkServerIdentity(hostname, this.securePair.cleartext.getPeerCertificate());
98-
}
99-
100-
if (verifyError) {
101-
this.securePair.destroy();
102-
this.socket.destroy(verifyError);
103-
return;
104-
}
100+
// If an error happens in the TLS layer, there is nothing we can do about it.
101+
// Forward the error to the socket so the connection gets properly cleaned up.
102+
securePair.cleartext.on('error', (err) => {
103+
// Streams in node.js versions before 8.0.0 don't support `.destroy`
104+
if (typeof securePair.encrypted.destroy === 'function') {
105+
securePair.encrypted.destroy();
105106
}
107+
this.socket.destroy(err);
108+
});
106109

110+
securePair.cleartext.on('secureConnect', () => {
111+
const cipher = securePair.cleartext.getCipher();
107112
this.debug.log('TLS negotiated (' + cipher.name + ', ' + cipher.version + ')');
108-
this.emit('secure', this.securePair.cleartext);
113+
this.emit('secure', securePair.cleartext);
109114
this.encryptAllFutureTraffic();
110115
});
111116

112-
this.securePair.encrypted.on('data', (data) => {
117+
securePair.encrypted.on('data', (data) => {
113118
this.sendMessage(TYPE.PRELOGIN, data);
114119
});
115-
116-
// If an error happens in the TLS layer, there is nothing we can do about it.
117-
// Forward the error to the socket so the connection gets properly cleaned up.
118-
this.securePair.cleartext.on('error', (err) => {
119-
this.socket.destroy(err);
120-
});
121-
122-
// On Node >= 0.12, the encrypted stream automatically starts spewing out
123-
// data once we attach a `data` listener. But on Node <= 0.10.x, this is not
124-
// the case. We need to kick the cleartext stream once to get the
125-
// encrypted end of the secure pair to emit the TLS handshake data.
126-
this.securePair.cleartext.write('');
127120
}
128121

129122
encryptAllFutureTraffic() {

test/integration/connection-retry-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ exports['connection retry tests'] = {
7474
const config = getConfig();
7575
config.options.connectTimeout = config.options.connectionRetryInterval / 2;
7676

77-
const clock = this.sinon.useFakeTimers();
77+
const clock = this.sinon.useFakeTimers('setTimeout');
7878

7979
test.expect(1);
8080

0 commit comments

Comments
 (0)