Skip to content

Commit

Permalink
chore: use arrows stage 2
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchick committed Nov 16, 2018
1 parent e55abc7 commit 6571b87
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 68 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/ping-pong-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/ping-pong-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
6 changes: 3 additions & 3 deletions benchmarks/ping-pong-uv.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function pong(sock) {
writeReq.oncomplete = noop;
}

function ping() {
function ping(conn) {
count++;
pong(this);
pong(conn);
}

const port = 3334;
Expand All @@ -25,7 +25,7 @@ req.oncomplete = function() {
console.log('connected');
pong(client);
};
client.onread = ping;
client.onread = () => ping(client);
client.readStart();

setInterval(() => {
Expand Down
3 changes: 3 additions & 0 deletions lib/compressed_protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -44,13 +45,15 @@ 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)
);
}
return;
}

// eslint-disable-next-line no-invalid-this, consistent-this
const connection = this;

let packetLen = buffer.length;
Expand Down
4 changes: 2 additions & 2 deletions lib/packets/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions lib/packets/handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
28 changes: 10 additions & 18 deletions lib/pool_cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
};
Expand Down Expand Up @@ -182,20 +174,20 @@ 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');
} else {
return cb(err);
}
} else {
self._decreaseErrorCount(node);
this._decreaseErrorCount(node);
}
connection._clusterId = node.id;
return cb(null, connection);
Expand Down
7 changes: 4 additions & 3 deletions lib/pool_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down Expand Up @@ -44,7 +45,7 @@ class PoolConnection extends Connection {

destroy() {
this._removeFromPool();
return Connection.prototype.destroy.apply(this, arguments);
super.destroy();
}

_removeFromPool() {
Expand Down
21 changes: 7 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
"author": "Andrey Sidorov <sidorares@yandex.ru>",
"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",
Expand Down
16 changes: 12 additions & 4 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
4 changes: 4 additions & 0 deletions test/integration/promise-wrappers/test-promise-wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function testEventsConnect() {
);
}

/* eslint-disable no-invalid-this */
conn
.once('error', function() {
assert.equal(this, conn);
Expand All @@ -168,6 +169,7 @@ function testEventsConnect() {

doneEventsConnect = events === 5;
});
/* eslint-enable no-invalid-this */

conn.connection.emit('error', new Error());
conn.connection.emit('drain');
Expand Down Expand Up @@ -295,6 +297,7 @@ function testEventsPool() {
);
}

/* eslint-disable no-invalid-this */
pool
.once('acquire', function() {
assert.equal(this, pool);
Expand All @@ -314,6 +317,7 @@ function testEventsPool() {

doneEventsPool = events === 4;
});
/* eslint-enable no-invalid-this */

pool.pool.emit('acquire');
pool.pool.emit('connection');
Expand Down
12 changes: 3 additions & 9 deletions test/integration/test-namedPlaceholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ' +
Expand All @@ -24,21 +24,15 @@ 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();
});
},
'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();
Expand Down

0 comments on commit 6571b87

Please sign in to comment.