diff --git a/.travis.yml b/.travis.yml index 7c6a8283f2..390958ad53 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,4 +31,5 @@ script: - docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_DATABASE=test -p 33306:3306 mysql:5.7 - MYSQL_PORT=33306 node tools/wait-up.js - yarn --version + - if [ "$LINT" = "1" ]; then yarn run lint; fi - MYSQL_PORT=33306 yarn run test:raw diff --git a/appveyor.yml b/appveyor.yml index a90c96a26c..5547cbc8bc 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -32,6 +32,7 @@ before_test: test_script: - mysql --version + - if "%LINT%"=="1" (yarn run lint) - node test/run.js - SET MYSQL_USE_COMPRESSION=1 - node test/run.js diff --git a/benchmarks/ping-pong-client.js b/benchmarks/ping-pong-client.js index 39874be6ac..02cc3b4a40 100644 --- a/benchmarks/ping-pong-client.js +++ b/benchmarks/ping-pong-client.js @@ -3,15 +3,15 @@ const net = require('net'); let count = 0; const byte = Buffer.from([0x33]); -function pong() { +function pong(connection) { count++; - this.write(byte); + connection.write(byte); } const c = net.connect(3334); c.setNoDelay(true); -c.ondata = pong; -pong.apply(c); +c.ondata = () => pong(c); +pong(c); setInterval(() => { console.log(count); diff --git a/benchmarks/ping-pong-server.js b/benchmarks/ping-pong-server.js index 342d90e1f3..b2cabfcc76 100644 --- a/benchmarks/ping-pong-server.js +++ b/benchmarks/ping-pong-server.js @@ -3,13 +3,13 @@ const net = require('net'); const byte = Buffer.from([0x33]); -function pong() { - this.write(byte); +function pong(conn) { + conn.write(byte); } net .createServer(s => { s.setNoDelay(true); - s.ondata = pong; + s.ondata = () => pong(s); }) .listen(3334); diff --git a/benchmarks/ping-pong-uv.js b/benchmarks/ping-pong-uv.js index ef2fc1d1d0..6e96eb7ef4 100644 --- a/benchmarks/ping-pong-uv.js +++ b/benchmarks/ping-pong-uv.js @@ -9,9 +9,9 @@ function pong(sock) { writeReq.oncomplete = noop; } -function ping() { +function ping(conn) { count++; - pong(this); + pong(conn); } const port = 3334; @@ -25,7 +25,7 @@ req.oncomplete = function() { console.log('connected'); pong(client); }; -client.onread = ping; +client.onread = () => ping(client); client.readStart(); setInterval(() => { diff --git a/lib/compressed_protocol.js b/lib/compressed_protocol.js index 0620194272..67a7c39c32 100644 --- a/lib/compressed_protocol.js +++ b/lib/compressed_protocol.js @@ -7,6 +7,7 @@ const zlib = require('zlib'); const PacketParser = require('./packet_parser.js'); function handleCompressedPacket(packet) { + // eslint-disable-next-line consistent-this, no-invalid-this const connection = this; const deflatedLength = packet.readInt24(); const body = packet.readBuffer(); @@ -44,6 +45,7 @@ function writeCompressed(buffer) { if (buffer.length > MAX_COMPRESSED_LENGTH) { for (start = 0; start < buffer.length; start += MAX_COMPRESSED_LENGTH) { writeCompressed.call( + // eslint-disable-next-line no-invalid-this this, buffer.slice(start, start + MAX_COMPRESSED_LENGTH) ); @@ -51,6 +53,7 @@ function writeCompressed(buffer) { return; } + // eslint-disable-next-line no-invalid-this, consistent-this const connection = this; let packetLen = buffer.length; diff --git a/lib/packets/execute.js b/lib/packets/execute.js index f104097840..9e36af0d92 100644 --- a/lib/packets/execute.js +++ b/lib/packets/execute.js @@ -22,6 +22,7 @@ function toParameter(value, encoding) { let type = Types.VAR_STRING; let length; let writer = function(value) { + // eslint-disable-next-line no-invalid-this return Packet.prototype.writeLengthCodedString.call(this, value, encoding); }; if (value !== null) { @@ -77,7 +78,6 @@ class Execute { } toPacket() { - const self = this; // TODO: don't try to calculate packet length in advance, allocate some big buffer in advance (header + 256 bytes?) // and copy + reallocate if not enough // 0 + 4 - length, seqId @@ -92,7 +92,7 @@ class Execute { length += 1; // new-params-bound-flag length += 2 * this.parameters.length; // type byte for each parameter if new-params-bound-flag is set parameters = this.parameters.map(value => - toParameter(value, self.encoding) + toParameter(value, this.encoding) ); length += parameters.reduce( (accumulator, parameter) => accumulator + parameter.length, diff --git a/lib/packets/handshake.js b/lib/packets/handshake.js index 74b2efd506..777f73fc6f 100644 --- a/lib/packets/handshake.js +++ b/lib/packets/handshake.js @@ -15,14 +15,13 @@ class Handshake { } setScrambleData(cb) { - const self = this; require('crypto').randomBytes(20, (err, data) => { if (err) { cb(err); return; } - self.authPluginData1 = data.slice(0, 8); - self.authPluginData2 = data.slice(8, 20); + this.authPluginData1 = data.slice(0, 8); + this.authPluginData2 = data.slice(8, 20); cb(); }); } diff --git a/lib/pool_cluster.js b/lib/pool_cluster.js index aba4f2590f..465709539b 100644 --- a/lib/pool_cluster.js +++ b/lib/pool_cluster.js @@ -8,23 +8,15 @@ const EventEmitter = require('events').EventEmitter; * Selector */ const makeSelector = { - RR: () => { + RR() { let index = 0; - return clusterIds => { - if (index >= clusterIds.length) { - index = 0; - } - - const clusterId = clusterIds[index++]; - - return clusterId; - }; + return clusterIds => clusterIds[index++ % clusterIds.length]; }, - RANDOM: () => { + RANDOM() { return clusterIds => clusterIds[Math.floor(Math.random() * clusterIds.length)]; }, - ORDER: () => { + ORDER() { return clusterIds => clusterIds[0]; } }; @@ -182,12 +174,12 @@ class PoolCluster extends EventEmitter { } _getConnection(node, cb) { - const self = this; - node.pool.getConnection(function(err, connection) { + node.pool.getConnection((err, connection) => { if (err) { - self._increaseErrorCount(node); - if (self._canRetry) { - self.emit('warn', err); + this._increaseErrorCount(node); + if (this._canRetry) { + // REVIEW: this seems wrong? + this.emit('warn', err); // eslint-disable-next-line no-console console.warn('[Error] PoolCluster : ' + err); return cb(null, 'retry'); @@ -195,7 +187,7 @@ class PoolCluster extends EventEmitter { return cb(err); } } else { - self._decreaseErrorCount(node); + this._decreaseErrorCount(node); } connection._clusterId = node.id; return cb(null, connection); diff --git a/lib/pool_connection.js b/lib/pool_connection.js index ac51c56e2e..be49a73ac9 100644 --- a/lib/pool_connection.js +++ b/lib/pool_connection.js @@ -9,10 +9,11 @@ class PoolConnection extends Connection { // When a fatal error occurs the connection's protocol ends, which will cause // the connection to end as well, thus we only need to watch for the end event // and we will be notified of disconnects. - this.on('end', function() { + // REVIEW: Moved to `once` + this.once('end', () => { this._removeFromPool(); }); - this.on('error', function() { + this.once('error', () => { this._removeFromPool(); }); } @@ -44,7 +45,7 @@ class PoolConnection extends Connection { destroy() { this._removeFromPool(); - return Connection.prototype.destroy.apply(this, arguments); + super.destroy(); } _removeFromPool() { diff --git a/package-lock.json b/package-lock.json index 708dabfbb3..fcfe6cd5f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -600,7 +600,7 @@ "dependencies": { "pify": { "version": "2.3.0", - "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=", "dev": true } @@ -1237,7 +1237,7 @@ "dependencies": { "pify": { "version": "2.3.0", - "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=", "dev": true } @@ -1399,7 +1399,7 @@ "dependencies": { "ansi-escapes": { "version": "3.1.0", - "resolved": "http://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.1.0.tgz", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.1.0.tgz", "integrity": "sha512-UgAb8H9D41AQnu/PbWlCofQVcnV4Gs2bBJi9eZPxfU/hgglFh3SMDMENRIqdr7H6XFnXdoknctFByVsCOotTVw==", "dev": true }, @@ -2155,18 +2155,11 @@ "dev": true }, "named-placeholders": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.1.tgz", - "integrity": "sha1-O3oNJiA910s6nfTJz7gnsvuQfmQ=", + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.2.tgz", + "integrity": "sha512-wiFWqxoLL3PGVReSZpjLVxyJ1bRqe+KKJVbr4hGs1KWfTZTQyezHFBbuKj9hsizHyGV2ne7EMjHdxEGAybD5SA==", "requires": { - "lru-cache": "2.5.0" - }, - "dependencies": { - "lru-cache": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.5.0.tgz", - "integrity": "sha1-2COIrpyWC+y+oMc7uet5tsbOmus=" - } + "lru-cache": "^4.1.3" } }, "nanomatch": { diff --git a/package.json b/package.json index 9c3f77b90b..11cc020836 100644 --- a/package.json +++ b/package.json @@ -45,14 +45,14 @@ "author": "Andrey Sidorov ", "license": "MIT", "dependencies": { - "denque": "1.4.0", + "denque": "^1.4.0", "generate-function": "^2.3.1", "iconv-lite": "^0.4.24", "long": "^4.0.0", - "lru-cache": "4.1.3", - "named-placeholders": "1.1.1", - "seq-queue": "0.0.5", - "sqlstring": "2.3.1" + "lru-cache": "^4.1.3", + "named-placeholders": "^1.1.2", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.1" }, "devDependencies": { "assert-diff": "^2.0.2", diff --git a/test/common.js b/test/common.js index 5e3ceae223..5512d54387 100644 --- a/test/common.js +++ b/test/common.js @@ -25,15 +25,23 @@ module.exports.SqlString = require('sqlstring'); module.exports.config = config; module.exports.waitDatabaseReady = function(callback) { + const start = Date.now(); const tryConnect = function() { const conn = module.exports.createConnection(); - conn.on('error', err => { - console.log(err); + conn.once('error', err => { + if (err.code !== 'PROTOCOL_CONNECTION_LOST' && err.code !== 'ETIMEDOUT') { + console.log('Unexpected error waiting for connection', err); + } + try { + conn.close(); + } catch (err) { + // ignore + } console.log('not ready'); setTimeout(tryConnect, 1000); }); - conn.on('connect', () => { - console.log('ready!'); + conn.once('connect', () => { + console.log(`ready after ${Date.now() - start}ms!`); conn.close(); callback(); }); diff --git a/test/integration/promise-wrappers/test-promise-wrappers.js b/test/integration/promise-wrappers/test-promise-wrappers.js index d79f6e5a3e..dc6d649a36 100644 --- a/test/integration/promise-wrappers/test-promise-wrappers.js +++ b/test/integration/promise-wrappers/test-promise-wrappers.js @@ -145,6 +145,7 @@ function testEventsConnect() { ); } + /* eslint-disable no-invalid-this */ conn .once('error', function() { assert.equal(this, conn); @@ -168,6 +169,7 @@ function testEventsConnect() { doneEventsConnect = events === 5; }); + /* eslint-enable no-invalid-this */ conn.connection.emit('error', new Error()); conn.connection.emit('drain'); @@ -295,6 +297,7 @@ function testEventsPool() { ); } + /* eslint-disable no-invalid-this */ pool .once('acquire', function() { assert.equal(this, pool); @@ -314,6 +317,7 @@ function testEventsPool() { doneEventsPool = events === 4; }); + /* eslint-enable no-invalid-this */ pool.pool.emit('acquire'); pool.pool.emit('connection'); diff --git a/test/integration/test-namedPlaceholders.js b/test/integration/test-namedPlaceholders.js index 9f0f3423e8..2a8e050154 100644 --- a/test/integration/test-namedPlaceholders.js +++ b/test/integration/test-namedPlaceholders.js @@ -12,7 +12,7 @@ test('Test namedPlaceholder as command parameter', { 'Enabled in connection config, disabled in query command': () => { // enabled in initial config, disable in test const c = createConnection({ namedPlaceholders: true }); - c.query({ sql: query, namedPlaceholders: false }, values, function(err) { + c.query({ sql: query, namedPlaceholders: false }, values, err => { if (!err || !err.sqlMessage.match(/right syntax to use near ':named'/)) { assert.fail( 'Expected err.sqlMessage to contain "right syntax to use near \':named\'" sqlMessage: ' + @@ -24,10 +24,7 @@ test('Test namedPlaceholder as command parameter', { }, 'Disabled in connection config, enable query command': () => { const c = createConnection({ namedPlaceholders: false }); - c.query({ sql: query, namedPlaceholders: true }, values, function( - err, - rows - ) { + c.query({ sql: query, namedPlaceholders: true }, values, (err, rows) => { assert.ifError(err); assert.equal(rows[0].result, 1); c.end(); @@ -35,10 +32,7 @@ test('Test namedPlaceholder as command parameter', { }, 'Disabled in connection config, enable execute command': () => { const c = createConnection({ namedPlaceholders: false }); - c.execute({ sql: query, namedPlaceholders: true }, values, function( - err, - rows - ) { + c.execute({ sql: query, namedPlaceholders: true }, values, (err, rows) => { assert.ifError(err); assert.equal(rows[0].result, 1); c.end();