Skip to content

Commit 5b1fac0

Browse files
authored
Merge pull request #589 from sidorares/safe-buffer-fixes
Safe buffer fixes
2 parents e1a740f + cb83849 commit 5b1fac0

File tree

6 files changed

+19
-23
lines changed

6 files changed

+19
-23
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ notifications:
2424
email: false
2525

2626
script:
27-
- docker run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_DATABASE=test -p 33306:3306 mysql:5.7
27+
- nvm ls-remote
28+
- docker run -d -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_DATABASE=test -p 33306:3306 mysql:5.7
2829
- MYSQL_PORT=33306 node tools/wait-up.js
2930
- node --version
3031
- yarn --version

lib/parsers/string.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1+
var Buffer = require('safe-buffer').Buffer;
12
var Iconv = require('iconv-lite');
23

3-
var NODE_ENCODING = [
4-
'ascii',
5-
'utf8',
6-
'utf16le',
7-
'ucs2',
8-
'base64',
9-
'latin1',
10-
'binary',
11-
'hex'
12-
].reduce(function (map, item) {
13-
map[item] = Buffer.isEncoding(item);
14-
return map;
15-
}, {});
16-
174
exports.decode = function(buffer, encoding, options) {
18-
if (NODE_ENCODING[encoding]) {
5+
if (Buffer.isEncoding(encoding)) {
196
return buffer.toString(encoding);
207
}
218

@@ -28,7 +15,7 @@ exports.decode = function(buffer, encoding, options) {
2815
};
2916

3017
exports.encode = function(string, encoding, options) {
31-
if (NODE_ENCODING[encoding]) {
18+
if (Buffer.isEncoding(encoding)) {
3219
return Buffer.from(string, encoding);
3320
}
3421

lib/pool.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ Pool.prototype.query = function(sql, values, cb) {
165165
Pool.prototype.execute = function(sql, values, cb) {
166166
var useNamedPlaceholders = this.config.connectionConfig.namedPlaceholders;
167167

168+
// TODO construct execute command first here and pass it to connection.execute
169+
// so that polymorphic arguments logic is there in one place
170+
if (typeof values == 'function') {
171+
cb = values;
172+
values = [];
173+
}
174+
168175
this.getConnection(function(err, conn) {
169176
if (err) {
170177
return cb(err);

test/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports.SqlString = require('sqlstring');
1111
module.exports.config = config;
1212

1313
module.exports.waitDatabaseReady = function(callback) {
14-
const tryConnect = () => {
14+
const tryConnect = function() {
1515
const conn = module.exports.createConnection();
1616
conn.on('error', function(err) {
1717
console.log(err);

test/integration/connection/encoding/test-non-bmp-chars.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ var pileOfPoo = '💩';
77

88
var connection = common.createConnection({ charset: 'UTF8_GENERAL_CI' });
99
connection.query('select "💩"', function(err, rows, fields) {
10+
assert.ifError(err);
1011
assert.equal(fields[0].name, pileOfPoo);
1112
assert.equal(rows[0][fields[0].name], pileOfPoo);
1213
connection.end();
1314
});
1415

1516
var connection2 = common.createConnection({ charset: 'UTF8MB4_GENERAL_CI' });
1617
connection2.query('select "💩"', function(err, rows, fields) {
18+
assert.ifError(err);
1719
assert.equal(fields[0].name, '?');
1820
assert.equal(rows[0]['?'], pileOfPoo);
1921
connection2.end();

test/run.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ process.env.TZ = 'UTC';
1313

1414
require('urun')(__dirname, options);
1515

16-
17-
process.on('exit', (code) => {
18-
console.log(`About to exit with code: ${code}`);
16+
process.on('exit', function(code) {
17+
console.log('About to exit with code: ' + code);
1918
});
2019

21-
process.on('unhandledRejection', (reason) => {
20+
process.on('unhandledRejection', function(reason) {
2221
console.log('unhandledRejection', reason);
2322
});
2423

25-
process.on('uncaughtException', (err) => {
24+
process.on('uncaughtException', function(err) {
2625
console.log('uncaughtException', err);
2726
});

0 commit comments

Comments
 (0)