Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for nodejs 10 compatibility. #2026

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -14,6 +14,7 @@ node_js:
- "7.10"
- "8.10"
- "9.8"
- "10.2"
env:
global:
# Necessary to build Node.js 0.6 on Travis CI images
Expand Down
25 changes: 17 additions & 8 deletions lib/protocol/Protocol.js
@@ -1,7 +1,7 @@
var Parser = require('./Parser');
var Sequences = require('./sequences');
var Packets = require('./packets');
var Timers = require('timers');
var Timers = require('./Timers');
var Stream = require('stream').Stream;
var Util = require('util');
var PacketWriter = require('./PacketWriter');
Expand Down Expand Up @@ -154,7 +154,9 @@ Protocol.prototype._enqueue = function(sequence) {
self._delegateError(err, sequence);
})
.on('packet', function(packet) {
Timers.active(sequence);
if (sequence._timer) {
sequence._timer.refresh();
}
self._emitPacket(packet);
})
.on('end', function() {
Expand All @@ -170,7 +172,9 @@ Protocol.prototype._enqueue = function(sequence) {
self._delegateError(err, sequence);
})
.on('start-tls', function() {
Timers.active(sequence);
if (sequence._timer) {
sequence._timer.refresh();
}
self._connection._startTLS(function(err) {
if (err) {
// SSL negotiation error are fatal
Expand All @@ -180,7 +184,9 @@ Protocol.prototype._enqueue = function(sequence) {
return;
}

Timers.active(sequence);
if (sequence._timer) {
sequence._timer.refresh();
}
sequence._tlsUpgradeCompleteHandler();
});
});
Expand Down Expand Up @@ -265,7 +271,9 @@ Protocol.prototype._parsePacket = function() {
this._handshakeInitializationPacket = packet;
}

Timers.active(sequence);
if (sequence._timer) {
sequence._timer.refresh();
}

if (!sequence[packetName]) {
var err = new Error('Received packet in the wrong sequence.');
Expand Down Expand Up @@ -322,7 +330,7 @@ Protocol.prototype._determinePacket = function(sequence) {
};

Protocol.prototype._dequeue = function(sequence) {
Timers.unenroll(sequence);
sequence._timer = Timers.clearTimeout(sequence._timer);

// No point in advancing the queue, we are dead
if (this._fatalError) {
Expand All @@ -344,8 +352,9 @@ Protocol.prototype._dequeue = function(sequence) {

Protocol.prototype._startSequence = function(sequence) {
if (sequence._timeout > 0 && isFinite(sequence._timeout)) {
Timers.enroll(sequence, sequence._timeout);
Timers.active(sequence);
sequence._timer = Timers.setTimeout(function() {
sequence._onTimeout();
}, sequence._timeout);
}

if (sequence.constructor === Sequences.ChangeUser) {
Expand Down
45 changes: 45 additions & 0 deletions lib/protocol/Timers.js
@@ -0,0 +1,45 @@
var Timers = require('timers');

function hasRefresh() {
var testTimer = setTimeout(function() {}, 100);
var result = typeof testTimer.refresh === 'function';

clearTimeout(testTimer);

return result;
}

if (hasRefresh()) {
module.exports.setTimeout = setTimeout;
module.exports.clearTimeout = clearTimeout;
} else {
module.exports.setTimeout = function setTimeout(callback, after) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function.');
}

var args = Array.prototype.slice.call(arguments, 2);
var timer = {
_idleNext : null,
_idlePrev : null,
_idleStart : null,
_idleTimeout : -1,
_repeat : null,
_onTimeout : function _onTimeout() {
callback.apply(this, args);
},
refresh: function refresh() {
Timers.active(this);
}
};

Timers.enroll(timer, after);
Timers.active(timer);
return timer;
};
module.exports.clearTimeout = function clearTimeout(timer) {
if (timer) {
Timers.unenroll(timer);
}
};
}
7 changes: 0 additions & 7 deletions lib/protocol/sequences/Sequence.js
Expand Up @@ -25,13 +25,6 @@ function Sequence(options, callback) {
this._callSite = null;
this._ended = false;
this._timeout = options.timeout;

// For Timers
this._idleNext = null;
this._idlePrev = null;
this._idleStart = null;
this._idleTimeout = -1;
this._repeat = null;
}

Sequence.determinePacket = function(byte) {
Expand Down
11 changes: 9 additions & 2 deletions test/unit/connection/test-connection-ssl-ciphers.js
@@ -1,10 +1,17 @@
var assert = require('assert');
var common = require('../../common');
var tls = require('tls');
var tlsCipher = 'RC4-SHA';

if (typeof tls.getCiphers === 'function') {
tlsCipher = tls.getCiphers()[0].toUpperCase();
}

var connection = common.createConnection({
port : common.fakeServerPort,
ssl : {
ca : common.getSSLConfig().ca,
ciphers : 'RC4-SHA'
ciphers : tlsCipher
}
});

Expand All @@ -17,7 +24,7 @@ server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);
assert.equal(rows.length, 1);
assert.equal(rows[0].Variable_name, 'Ssl_cipher');
assert.equal(rows[0].Value, 'RC4-SHA');
assert.equal(rows[0].Value, tlsCipher);

connection.destroy();
server.destroy();
Expand Down