From 55265b8126d66793033ce9ee34982dee30c85486 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Thu, 24 May 2018 22:30:10 -0400 Subject: [PATCH] Support Node.js 10.x fixes #2003 fixes #2024 closes #2026 fixes #2034 --- .travis.yml | 1 + Changes.md | 1 + appveyor.yml | 1 + lib/protocol/Protocol.js | 14 ++++++------- lib/protocol/Timer.js | 33 ++++++++++++++++++++++++++++++ lib/protocol/sequences/Sequence.js | 9 ++------ 6 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 lib/protocol/Timer.js diff --git a/.travis.yml b/.travis.yml index eca6acf9e..fed3e4dab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ node_js: - "7.10" - "8.11" - "9.8" + - "10.3" env: global: # Necessary to build Node.js 0.6 on Travis CI images diff --git a/Changes.md b/Changes.md index ff64f9a5a..f1a6efb6f 100644 --- a/Changes.md +++ b/Changes.md @@ -10,6 +10,7 @@ you spot any mistakes. * Add new error codes up to MySQL 5.7.21 * Include connection ID in debug output * Support Node.js 9.x +* Support Node.js 10.x #2003 #2024 #2026 #2034 * Update Amazon RDS SSL certificates * Update `bignumber.js` to 4.1.0 * Update `readable-stream` to 2.3.6 diff --git a/appveyor.yml b/appveyor.yml index e23a0c000..ad210122d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,6 +18,7 @@ environment: - nodejs_version: "7.10" - nodejs_version: "8.11" - nodejs_version: "9.8" + - nodejs_version: "10.3" services: - mysql diff --git a/lib/protocol/Protocol.js b/lib/protocol/Protocol.js index fa816e6d6..5db4f56e5 100644 --- a/lib/protocol/Protocol.js +++ b/lib/protocol/Protocol.js @@ -1,7 +1,6 @@ var Parser = require('./Parser'); var Sequences = require('./sequences'); var Packets = require('./packets'); -var Timers = require('timers'); var Stream = require('stream').Stream; var Util = require('util'); var PacketWriter = require('./PacketWriter'); @@ -154,7 +153,7 @@ Protocol.prototype._enqueue = function(sequence) { self._delegateError(err, sequence); }) .on('packet', function(packet) { - Timers.active(sequence); + sequence._timer.active(); self._emitPacket(packet); }) .on('end', function() { @@ -170,7 +169,7 @@ Protocol.prototype._enqueue = function(sequence) { self._delegateError(err, sequence); }) .on('start-tls', function() { - Timers.active(sequence); + sequence._timer.active(); self._connection._startTLS(function(err) { if (err) { // SSL negotiation error are fatal @@ -180,7 +179,7 @@ Protocol.prototype._enqueue = function(sequence) { return; } - Timers.active(sequence); + sequence._timer.active(); sequence._tlsUpgradeCompleteHandler(); }); }); @@ -265,7 +264,7 @@ Protocol.prototype._parsePacket = function() { this._handshakeInitializationPacket = packet; } - Timers.active(sequence); + sequence._timer.active(); if (!sequence[packetName]) { var err = new Error('Received packet in the wrong sequence.'); @@ -322,7 +321,7 @@ Protocol.prototype._determinePacket = function(sequence) { }; Protocol.prototype._dequeue = function(sequence) { - Timers.unenroll(sequence); + sequence._timer.stop(); // No point in advancing the queue, we are dead if (this._fatalError) { @@ -344,8 +343,7 @@ 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.start(sequence._timeout); } if (sequence.constructor === Sequences.ChangeUser) { diff --git a/lib/protocol/Timer.js b/lib/protocol/Timer.js new file mode 100644 index 000000000..45ed0292f --- /dev/null +++ b/lib/protocol/Timer.js @@ -0,0 +1,33 @@ +var Timers = require('timers'); + +module.exports = Timer; +function Timer(object) { + this._object = object; + this._timeout = null; +} + +Timer.prototype.active = function active() { + if (this._timeout) { + if (this._timeout.refresh) { + this._timeout.refresh(); + } else { + Timers.active(this._timeout); + } + } +}; + +Timer.prototype.start = function start(msecs) { + this.stop(); + this._timeout = Timers.setTimeout(this._onTimeout.bind(this), msecs); +}; + +Timer.prototype.stop = function stop() { + if (this._timeout) { + Timers.clearTimeout(this._timeout); + this._timeout = null; + } +}; + +Timer.prototype._onTimeout = function _onTimeout() { + return this._object._onTimeout(); +}; diff --git a/lib/protocol/sequences/Sequence.js b/lib/protocol/sequences/Sequence.js index c9789bb49..de82dc270 100644 --- a/lib/protocol/sequences/Sequence.js +++ b/lib/protocol/sequences/Sequence.js @@ -2,6 +2,7 @@ var Util = require('util'); var EventEmitter = require('events').EventEmitter; var Packets = require('../packets'); var ErrorConstants = require('../constants/errors'); +var Timer = require('../Timer'); // istanbul ignore next: Node.js < 0.10 not covered var listenerCount = EventEmitter.listenerCount @@ -25,13 +26,7 @@ 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; + this._timer = new Timer(this); } Sequence.determinePacket = function(byte) {