Skip to content

Commit 04b23fb

Browse files
committed
Support Node.js 11.x
1 parent 60fdb02 commit 04b23fb

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ node_js:
1515
- "8.13"
1616
- "9.11"
1717
- "10.13"
18+
- "11.2"
1819
env:
1920
global:
2021
# Necessary to build Node.js 0.6 on Travis CI images

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ you spot any mistakes.
1111
* Fix no password support for old password protocol
1212
* Remove special case for handshake in determine packet code
1313
* Small performance improvement starting command sequence
14+
* Support Node.js 11.x
1415

1516
## v2.16.0 (2018-07-17)
1617

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ environment:
2020
- nodejs_version: "8.13"
2121
- nodejs_version: "9.11"
2222
- nodejs_version: "10.13"
23+
- nodejs_version: "11.2"
2324

2425
services:
2526
- mysql

lib/Connection.js

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -277,52 +277,52 @@ Connection.prototype.format = function(sql, values) {
277277
if (tls.TLSSocket) {
278278
// 0.11+ environment
279279
Connection.prototype._startTLS = function _startTLS(onSecure) {
280-
var connection = this;
281-
var secureContext = tls.createSecureContext({
282-
ca : this.config.ssl.ca,
283-
cert : this.config.ssl.cert,
284-
ciphers : this.config.ssl.ciphers,
285-
key : this.config.ssl.key,
286-
passphrase : this.config.ssl.passphrase
287-
});
288-
289-
// "unpipe"
290-
this._socket.removeAllListeners('data');
291-
this._protocol.removeAllListeners('data');
292-
293-
// socket <-> encrypted
294-
var rejectUnauthorized = this.config.ssl.rejectUnauthorized;
295-
var secureEstablished = false;
296-
var secureSocket = new tls.TLSSocket(this._socket, {
297-
rejectUnauthorized : rejectUnauthorized,
298-
requestCert : true,
299-
secureContext : secureContext,
300-
isServer : false
301-
});
280+
var connection = this;
302281

303-
// error handler for secure socket
304-
secureSocket.on('_tlsError', function(err) {
305-
if (secureEstablished) {
306-
connection._handleNetworkError(err);
307-
} else {
282+
createSecureContext(this.config, function (err, secureContext) {
283+
if (err) {
308284
onSecure(err);
285+
return;
309286
}
310-
});
311287

312-
// cleartext <-> protocol
313-
secureSocket.pipe(this._protocol);
314-
this._protocol.on('data', function(data) {
315-
secureSocket.write(data);
316-
});
288+
// "unpipe"
289+
connection._socket.removeAllListeners('data');
290+
connection._protocol.removeAllListeners('data');
291+
292+
// socket <-> encrypted
293+
var rejectUnauthorized = connection.config.ssl.rejectUnauthorized;
294+
var secureEstablished = false;
295+
var secureSocket = new tls.TLSSocket(connection._socket, {
296+
rejectUnauthorized : rejectUnauthorized,
297+
requestCert : true,
298+
secureContext : secureContext,
299+
isServer : false
300+
});
317301

318-
secureSocket.on('secure', function() {
319-
secureEstablished = true;
302+
// error handler for secure socket
303+
secureSocket.on('_tlsError', function(err) {
304+
if (secureEstablished) {
305+
connection._handleNetworkError(err);
306+
} else {
307+
onSecure(err);
308+
}
309+
});
320310

321-
onSecure(rejectUnauthorized ? this.ssl.verifyError() : null);
322-
});
311+
// cleartext <-> protocol
312+
secureSocket.pipe(connection._protocol);
313+
connection._protocol.on('data', function(data) {
314+
secureSocket.write(data);
315+
});
323316

324-
// start TLS communications
325-
secureSocket._start();
317+
secureSocket.on('secure', function() {
318+
secureEstablished = true;
319+
320+
onSecure(rejectUnauthorized ? this.ssl.verifyError() : null);
321+
});
322+
323+
// start TLS communications
324+
secureSocket._start();
325+
});
326326
};
327327
} else {
328328
// pre-0.11 environment
@@ -458,6 +458,25 @@ Connection.prototype._implyConnect = function() {
458458
}
459459
};
460460

461+
function createSecureContext (config, cb) {
462+
var context = null;
463+
var error = null;
464+
465+
try {
466+
context = tls.createSecureContext({
467+
ca : config.ssl.ca,
468+
cert : config.ssl.cert,
469+
ciphers : config.ssl.ciphers,
470+
key : config.ssl.key,
471+
passphrase : config.ssl.passphrase
472+
});
473+
} catch (err) {
474+
error = err;
475+
}
476+
477+
cb(error, context);
478+
}
479+
461480
function unwrapFromDomain(fn) {
462481
return function () {
463482
var domains = [];

0 commit comments

Comments
 (0)