Skip to content

Commit 6571b87

Browse files
committed
chore: use arrows stage 2
1 parent e55abc7 commit 6571b87

15 files changed

+64
-68
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ script:
3131
- docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_DATABASE=test -p 33306:3306 mysql:5.7
3232
- MYSQL_PORT=33306 node tools/wait-up.js
3333
- yarn --version
34+
- if [ "$LINT" = "1" ]; then yarn run lint; fi
3435
- MYSQL_PORT=33306 yarn run test:raw

appveyor.yml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ before_test:
3232

3333
test_script:
3434
- mysql --version
35+
- if "%LINT%"=="1" (yarn run lint)
3536
- node test/run.js
3637
- SET MYSQL_USE_COMPRESSION=1
3738
- node test/run.js

benchmarks/ping-pong-client.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
const net = require('net');
44
let count = 0;
55
const byte = Buffer.from([0x33]);
6-
function pong() {
6+
function pong(connection) {
77
count++;
8-
this.write(byte);
8+
connection.write(byte);
99
}
1010

1111
const c = net.connect(3334);
1212
c.setNoDelay(true);
13-
c.ondata = pong;
14-
pong.apply(c);
13+
c.ondata = () => pong(c);
14+
pong(c);
1515

1616
setInterval(() => {
1717
console.log(count);

benchmarks/ping-pong-server.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
const net = require('net');
44

55
const byte = Buffer.from([0x33]);
6-
function pong() {
7-
this.write(byte);
6+
function pong(conn) {
7+
conn.write(byte);
88
}
99

1010
net
1111
.createServer(s => {
1212
s.setNoDelay(true);
13-
s.ondata = pong;
13+
s.ondata = () => pong(s);
1414
})
1515
.listen(3334);

benchmarks/ping-pong-uv.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ function pong(sock) {
99
writeReq.oncomplete = noop;
1010
}
1111

12-
function ping() {
12+
function ping(conn) {
1313
count++;
14-
pong(this);
14+
pong(conn);
1515
}
1616

1717
const port = 3334;
@@ -25,7 +25,7 @@ req.oncomplete = function() {
2525
console.log('connected');
2626
pong(client);
2727
};
28-
client.onread = ping;
28+
client.onread = () => ping(client);
2929
client.readStart();
3030

3131
setInterval(() => {

lib/compressed_protocol.js

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const zlib = require('zlib');
77
const PacketParser = require('./packet_parser.js');
88

99
function handleCompressedPacket(packet) {
10+
// eslint-disable-next-line consistent-this, no-invalid-this
1011
const connection = this;
1112
const deflatedLength = packet.readInt24();
1213
const body = packet.readBuffer();
@@ -44,13 +45,15 @@ function writeCompressed(buffer) {
4445
if (buffer.length > MAX_COMPRESSED_LENGTH) {
4546
for (start = 0; start < buffer.length; start += MAX_COMPRESSED_LENGTH) {
4647
writeCompressed.call(
48+
// eslint-disable-next-line no-invalid-this
4749
this,
4850
buffer.slice(start, start + MAX_COMPRESSED_LENGTH)
4951
);
5052
}
5153
return;
5254
}
5355

56+
// eslint-disable-next-line no-invalid-this, consistent-this
5457
const connection = this;
5558

5659
let packetLen = buffer.length;

lib/packets/execute.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function toParameter(value, encoding) {
2222
let type = Types.VAR_STRING;
2323
let length;
2424
let writer = function(value) {
25+
// eslint-disable-next-line no-invalid-this
2526
return Packet.prototype.writeLengthCodedString.call(this, value, encoding);
2627
};
2728
if (value !== null) {
@@ -77,7 +78,6 @@ class Execute {
7778
}
7879

7980
toPacket() {
80-
const self = this;
8181
// TODO: don't try to calculate packet length in advance, allocate some big buffer in advance (header + 256 bytes?)
8282
// and copy + reallocate if not enough
8383
// 0 + 4 - length, seqId
@@ -92,7 +92,7 @@ class Execute {
9292
length += 1; // new-params-bound-flag
9393
length += 2 * this.parameters.length; // type byte for each parameter if new-params-bound-flag is set
9494
parameters = this.parameters.map(value =>
95-
toParameter(value, self.encoding)
95+
toParameter(value, this.encoding)
9696
);
9797
length += parameters.reduce(
9898
(accumulator, parameter) => accumulator + parameter.length,

lib/packets/handshake.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ class Handshake {
1515
}
1616

1717
setScrambleData(cb) {
18-
const self = this;
1918
require('crypto').randomBytes(20, (err, data) => {
2019
if (err) {
2120
cb(err);
2221
return;
2322
}
24-
self.authPluginData1 = data.slice(0, 8);
25-
self.authPluginData2 = data.slice(8, 20);
23+
this.authPluginData1 = data.slice(0, 8);
24+
this.authPluginData2 = data.slice(8, 20);
2625
cb();
2726
});
2827
}

lib/pool_cluster.js

+10-18
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,15 @@ const EventEmitter = require('events').EventEmitter;
88
* Selector
99
*/
1010
const makeSelector = {
11-
RR: () => {
11+
RR() {
1212
let index = 0;
13-
return clusterIds => {
14-
if (index >= clusterIds.length) {
15-
index = 0;
16-
}
17-
18-
const clusterId = clusterIds[index++];
19-
20-
return clusterId;
21-
};
13+
return clusterIds => clusterIds[index++ % clusterIds.length];
2214
},
23-
RANDOM: () => {
15+
RANDOM() {
2416
return clusterIds =>
2517
clusterIds[Math.floor(Math.random() * clusterIds.length)];
2618
},
27-
ORDER: () => {
19+
ORDER() {
2820
return clusterIds => clusterIds[0];
2921
}
3022
};
@@ -182,20 +174,20 @@ class PoolCluster extends EventEmitter {
182174
}
183175

184176
_getConnection(node, cb) {
185-
const self = this;
186-
node.pool.getConnection(function(err, connection) {
177+
node.pool.getConnection((err, connection) => {
187178
if (err) {
188-
self._increaseErrorCount(node);
189-
if (self._canRetry) {
190-
self.emit('warn', err);
179+
this._increaseErrorCount(node);
180+
if (this._canRetry) {
181+
// REVIEW: this seems wrong?
182+
this.emit('warn', err);
191183
// eslint-disable-next-line no-console
192184
console.warn('[Error] PoolCluster : ' + err);
193185
return cb(null, 'retry');
194186
} else {
195187
return cb(err);
196188
}
197189
} else {
198-
self._decreaseErrorCount(node);
190+
this._decreaseErrorCount(node);
199191
}
200192
connection._clusterId = node.id;
201193
return cb(null, connection);

lib/pool_connection.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class PoolConnection extends Connection {
99
// When a fatal error occurs the connection's protocol ends, which will cause
1010
// the connection to end as well, thus we only need to watch for the end event
1111
// and we will be notified of disconnects.
12-
this.on('end', function() {
12+
// REVIEW: Moved to `once`
13+
this.once('end', () => {
1314
this._removeFromPool();
1415
});
15-
this.on('error', function() {
16+
this.once('error', () => {
1617
this._removeFromPool();
1718
});
1819
}
@@ -44,7 +45,7 @@ class PoolConnection extends Connection {
4445

4546
destroy() {
4647
this._removeFromPool();
47-
return Connection.prototype.destroy.apply(this, arguments);
48+
super.destroy();
4849
}
4950

5051
_removeFromPool() {

package-lock.json

+7-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
"author": "Andrey Sidorov <sidorares@yandex.ru>",
4646
"license": "MIT",
4747
"dependencies": {
48-
"denque": "1.4.0",
48+
"denque": "^1.4.0",
4949
"generate-function": "^2.3.1",
5050
"iconv-lite": "^0.4.24",
5151
"long": "^4.0.0",
52-
"lru-cache": "4.1.3",
53-
"named-placeholders": "1.1.1",
54-
"seq-queue": "0.0.5",
55-
"sqlstring": "2.3.1"
52+
"lru-cache": "^4.1.3",
53+
"named-placeholders": "^1.1.2",
54+
"seq-queue": "^0.0.5",
55+
"sqlstring": "^2.3.1"
5656
},
5757
"devDependencies": {
5858
"assert-diff": "^2.0.2",

test/common.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ module.exports.SqlString = require('sqlstring');
2525
module.exports.config = config;
2626

2727
module.exports.waitDatabaseReady = function(callback) {
28+
const start = Date.now();
2829
const tryConnect = function() {
2930
const conn = module.exports.createConnection();
30-
conn.on('error', err => {
31-
console.log(err);
31+
conn.once('error', err => {
32+
if (err.code !== 'PROTOCOL_CONNECTION_LOST' && err.code !== 'ETIMEDOUT') {
33+
console.log('Unexpected error waiting for connection', err);
34+
}
35+
try {
36+
conn.close();
37+
} catch (err) {
38+
// ignore
39+
}
3240
console.log('not ready');
3341
setTimeout(tryConnect, 1000);
3442
});
35-
conn.on('connect', () => {
36-
console.log('ready!');
43+
conn.once('connect', () => {
44+
console.log(`ready after ${Date.now() - start}ms!`);
3745
conn.close();
3846
callback();
3947
});

test/integration/promise-wrappers/test-promise-wrappers.js

+4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ function testEventsConnect() {
145145
);
146146
}
147147

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

169170
doneEventsConnect = events === 5;
170171
});
172+
/* eslint-enable no-invalid-this */
171173

172174
conn.connection.emit('error', new Error());
173175
conn.connection.emit('drain');
@@ -295,6 +297,7 @@ function testEventsPool() {
295297
);
296298
}
297299

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

315318
doneEventsPool = events === 4;
316319
});
320+
/* eslint-enable no-invalid-this */
317321

318322
pool.pool.emit('acquire');
319323
pool.pool.emit('connection');

test/integration/test-namedPlaceholders.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test('Test namedPlaceholder as command parameter', {
1212
'Enabled in connection config, disabled in query command': () => {
1313
// enabled in initial config, disable in test
1414
const c = createConnection({ namedPlaceholders: true });
15-
c.query({ sql: query, namedPlaceholders: false }, values, function(err) {
15+
c.query({ sql: query, namedPlaceholders: false }, values, err => {
1616
if (!err || !err.sqlMessage.match(/right syntax to use near ':named'/)) {
1717
assert.fail(
1818
'Expected err.sqlMessage to contain "right syntax to use near \':named\'" sqlMessage: ' +
@@ -24,21 +24,15 @@ test('Test namedPlaceholder as command parameter', {
2424
},
2525
'Disabled in connection config, enable query command': () => {
2626
const c = createConnection({ namedPlaceholders: false });
27-
c.query({ sql: query, namedPlaceholders: true }, values, function(
28-
err,
29-
rows
30-
) {
27+
c.query({ sql: query, namedPlaceholders: true }, values, (err, rows) => {
3128
assert.ifError(err);
3229
assert.equal(rows[0].result, 1);
3330
c.end();
3431
});
3532
},
3633
'Disabled in connection config, enable execute command': () => {
3734
const c = createConnection({ namedPlaceholders: false });
38-
c.execute({ sql: query, namedPlaceholders: true }, values, function(
39-
err,
40-
rows
41-
) {
35+
c.execute({ sql: query, namedPlaceholders: true }, values, (err, rows) => {
4236
assert.ifError(err);
4337
assert.equal(rows[0].result, 1);
4438
c.end();

0 commit comments

Comments
 (0)