Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace deprecated Buffer apis with new API #381

Merged
merged 5 commits into from
Aug 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ before_script:
node_js:
- '0.10'
- '0.12'
- '4.4'
- '4.5'
- '5.12'
- '6.3'
- '6.4'


script:
Expand Down
2 changes: 1 addition & 1 deletion examples/pass-sha.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var mysql = require('../index.js').createConnection({
user: 'testuser',
//password: 'testpassword'
passwordSha1: Buffer('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex')
passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex')
});
mysql.execute("select ?+1 as qqq, ? as rrr, ? as yyy", [1, null, 3], function(err, rows, fields) {
console.log(err, rows, fields);
Expand Down
9 changes: 5 additions & 4 deletions lib/auth_41.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ server stores sha1(sha1(password)) ( hash_stag2)
*/

var crypto = require('crypto');
var Buffer = require('safe-buffer').Buffer;

function sha1 (msg, msg1, msg2) {
var hash = crypto.createHash('sha1');
Expand All @@ -40,14 +41,14 @@ function sha1 (msg, msg1, msg2) {

function xor (a, b) {
if (!Buffer.isBuffer(a)) {
a = new Buffer(a, 'binary');
a = Buffer.from(a, 'binary');
}

if (!Buffer.isBuffer(b)) {
b = new Buffer(b, 'binary');
b = Buffer.from(b, 'binary');
}

var result = new Buffer(a.length);
var result = Buffer.allocUnsafe(a.length);

for (var i = 0; i < a.length; i++) {
result[i] = (a[i] ^ b[i]);
Expand All @@ -58,7 +59,7 @@ function xor (a, b) {
function token (password, scramble1, scramble2) {
// TODO: use buffers (not sure why strings here)
if (!password) {
return new Buffer(0);
return Buffer.alloc(0);
}
var stage1 = sha1(password);
return module.exports.calculateTokenFromPasswordSha(stage1, scramble1, scramble2);
Expand Down
6 changes: 4 additions & 2 deletions lib/commands/ping.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var Command = require('./command');
var util = require('util');
var Buffer = require('safe-buffer').Buffer;

var Command = require('./command');
var CommandCode = require('../constants/commands');
var Packet = require('../packets/packet');

Expand All @@ -15,7 +17,7 @@ function Ping (callback)
util.inherits(Ping, Command);

Ping.prototype.start = function (packet, connection) {
var ping = new Packet(0, new Buffer([1, 0, 0, 0, CommandCode.PING]), 0, 5);
var ping = new Packet(0, Buffer.from([1, 0, 0, 0, CommandCode.PING]), 0, 5);
connection.writePacket(ping);
return Ping.prototype.pingResponse;
};
Expand Down
5 changes: 3 additions & 2 deletions lib/commands/query.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('fs');
var util = require('util');
var Buffer = require('safe-buffer').Buffer;

var Readable = require('readable-stream');

Expand All @@ -8,7 +9,7 @@ var Packets = require('../packets/index.js');
var compileParser = require('../compile_text_parser.js');
var ServerStatus = require('../constants/server_status.js');

var EmptyPacket = new Packets.Packet(0, new Buffer(4), 0, 4);
var EmptyPacket = new Packets.Packet(0, Buffer.allocUnsafe(4), 0, 4);

function Query (options, callback)
{
Expand Down Expand Up @@ -126,7 +127,7 @@ Query.prototype._streamLocalInfile = function (connection) {
command._localStream.resume();
});
this._localStream.on('data', function (data) {
var dataWithHeader = new Buffer(data.length + 4);
var dataWithHeader = Buffer.allocUnsafe(data.length + 4);
data.copy(dataWithHeader, 4);
connection.writePacket(new Packets.Packet(0, dataWithHeader, 0, dataWithHeader.length));
});
Expand Down
4 changes: 3 additions & 1 deletion lib/commands/quit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var util = require('util');
var Buffer = require('safe-buffer').Buffer;

var Command = require('./command.js');
var CommandCode = require('../constants/commands.js');
var Packet = require('../packets/packet.js');
Expand All @@ -12,7 +14,7 @@ util.inherits(Quit, Command);

Quit.prototype.start = function (packet, connection) {
connection._closing = true;
var quit = new Packet(0, new Buffer([1, 0, 0, 0, CommandCode.QUIT]), 0, 5);
var quit = new Packet(0, Buffer.from([1, 0, 0, 0, CommandCode.QUIT]), 0, 5);
if (this.done) {
this.done();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/compressed_protocol.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Buffer = require('safe-buffer').Buffer;

// connection mixins
// implementation of http://dev.mysql.com/doc/internals/en/compression.html

Expand Down Expand Up @@ -64,7 +66,7 @@ function writeCompressedPacket (packet) {
}

var packetLen = packet.length();
var compressHeader = new Buffer(7);
var compressHeader = Buffer.allocUnsafe(7);

connection.deflateQueue.push(function (task) {
zlib.deflate(packet.buffer, function (err, compressed) {
Expand Down
3 changes: 2 additions & 1 deletion lib/packet_parser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var Buffer = require('safe-buffer').Buffer;
var Packet = require('./packets/packet.js');

function PacketParser (onPacket, packetHeaderLength)
Expand Down Expand Up @@ -69,7 +70,7 @@ PacketParser.prototype.executePayload = function executePayload (chunk) {
var remainingPayload = this.length - this.bufferLength + this.packetHeaderLength - 3;

if (end - start >= remainingPayload) { // last chunk for payload
var payload = new Buffer(this.length + this.packetHeaderLength);
var payload = Buffer.allocUnsafe(this.length + this.packetHeaderLength);
var offset = 3;
for (var i = 0; i < this.buffer.length; ++i) {
this.buffer[i].copy(payload, offset);
Expand Down
3 changes: 2 additions & 1 deletion lib/packets/auth_switch_request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest

var Packet = require('../packets/packet');
var Buffer = require('safe-buffer').Buffer;

function AuthSwitchRequest (opts)
{
Expand All @@ -11,7 +12,7 @@ function AuthSwitchRequest (opts)
AuthSwitchRequest.prototype.toPacket = function ()
{
var length = 6 + this.pluginName.length + this.pluginData.length;
var buffer = new Buffer(length);
var buffer = Buffer.allocUnsafe(length);
var packet = new Packet(0, buffer, 0, length);
packet.offset = 4;
packet.writeInt8(0xfe);
Expand Down
4 changes: 2 additions & 2 deletions lib/packets/auth_switch_request_more_data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest

var Buffer = require('safe-buffer').Buffer;
var Packet = require('../packets/packet');

function AuthSwitchRequestMoreData (data)
Expand All @@ -10,7 +10,7 @@ function AuthSwitchRequestMoreData (data)
AuthSwitchRequestMoreData.prototype.toPacket = function ()
{
var length = 5 + this.data.length;
var buffer = new Buffer(length);
var buffer = Buffer.allocUnsafe(length);
var packet = new Packet(0, buffer, 0, length);
packet.offset = 4;
packet.writeInt8(0x01);
Expand Down
5 changes: 3 additions & 2 deletions lib/packets/auth_switch_response.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest
var Buffer = require('safe-buffer').Buffer;

var Packet = require('../packets/packet');

function AuthSwitchResponse (data)
{
if (!Buffer.isBuffer(data)) {
data = Buffer(data);
data = Buffer.from(data);
}
this.data = data;
}

AuthSwitchResponse.prototype.toPacket = function ()
{
var length = 4 + this.data.length;
var buffer = new Buffer(length);
var buffer = Buffer.allocUnsafe(length);
var packet = new Packet(0, buffer, 0, length);
packet.offset = 4;
packet.writeBuffer(this.data);
Expand Down
4 changes: 2 additions & 2 deletions lib/packets/binlog_dump.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// http://dev.mysql.com/doc/internals/en/com-binlog-dump.html#packet-COM_BINLOG_DUMP

var Buffer = require('safe-buffer').Buffer;
var Packet = require('../packets/packet');
var CommandCodes = require('../constants/commands');

Expand All @@ -19,7 +19,7 @@ BinlogDump.prototype.toPacket = function ()
{
var length = 15 + // TODO: should be ascii?
Buffer.byteLength(this.filename, 'utf8');
var buffer = new Buffer(length);
var buffer = Buffer.allocUnsafe(length);
var packet = new Packet(0, buffer, 0, length);
packet.offset = 4;
packet.writeInt8(CommandCodes.BINLOG_DUMP);
Expand Down
4 changes: 3 additions & 1 deletion lib/packets/change_user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Buffer = require('safe-buffer').Buffer;

var CommandCode = require('../constants/commands.js');
var ClientConstants = require('../constants/client.js');
var Packet = require('../packets/packet.js');
Expand Down Expand Up @@ -82,7 +84,7 @@ ChangeUser.prototype.toPacket = function ()

// dry run: calculate resulting packet length
var p = this.serializeToBuffer(Packet.MockBuffer());
return this.serializeToBuffer(new Buffer(p.offset));
return this.serializeToBuffer(Buffer.allocUnsafe(p.offset));
};

module.exports = ChangeUser;
4 changes: 3 additions & 1 deletion lib/packets/close_statement.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Buffer = require('safe-buffer').Buffer;

var Packet = require('../packets/packet');
var CommandCodes = require('../constants/commands');

Expand All @@ -9,7 +11,7 @@ function CloseStatement (id)
// note: no response sent back
CloseStatement.prototype.toPacket = function ()
{
var packet = new Packet(0, new Buffer(9), 0, 9);
var packet = new Packet(0, Buffer.allocUnsafe(9), 0, 9);
packet.offset = 4;
packet.writeInt8(CommandCodes.STMT_CLOSE);
packet.writeInt32(this.id);
Expand Down
3 changes: 2 additions & 1 deletion lib/packets/column_definition.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var Buffer = require('safe-buffer').Buffer;
var Packet = require('../packets/packet');

// creating JS string is relatively expensive (compared to
Expand Down Expand Up @@ -94,7 +95,7 @@ ColumnDefinition.toPacket = function (column, sequenceId)
length += Packet.lengthCodedStringLength(column[field]);
});

var buffer = new Buffer(length);
var buffer = Buffer.allocUnsafe(length);

function writeField (name) {
packet.writeLengthCodedString(column[name]);
Expand Down
3 changes: 2 additions & 1 deletion lib/packets/execute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var Buffer = require('safe-buffer').Buffer;
var CursorType = require('../constants/cursor');
var CommandCodes = require('../constants/commands');
var Types = require('../constants/types');
Expand Down Expand Up @@ -50,7 +51,7 @@ Execute.prototype.toPacket = function ()
}
}

var buffer = new Buffer(length);
var buffer = Buffer.allocUnsafe(length);
var packet = new Packet(0, buffer, 0, length);
packet.offset = 4;
packet.writeInt8(CommandCodes.STMT_EXECUTE);
Expand Down
7 changes: 3 additions & 4 deletions lib/packets/handshake.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var Buffer = require('safe-buffer').Buffer;
var Packet = require('../packets/packet');

function Handshake (args) {
Expand Down Expand Up @@ -56,7 +57,7 @@ Handshake.prototype.setScrambleData = function (cb) {
Handshake.prototype.toPacket = function (sequenceId) {
var buffer, packet;
var length = 68 + Buffer.byteLength(this.serverVersion, 'utf8');
buffer = new Buffer(length + 4);
buffer = Buffer.alloc(length + 4, 0); // zero fill, 10 bytes filler later needs to contain zeros
packet = new Packet(sequenceId, buffer, 0, length + 4);
packet.offset = 4;
packet.writeInt8(this.protocolVersion);
Expand All @@ -70,9 +71,7 @@ Handshake.prototype.toPacket = function (sequenceId) {
packet.writeInt16(this.statusFlags);
packet.writeInt16(this.capabilityFlags >> 16);
packet.writeInt8(21); // authPluginDataLength
var filler = new Buffer(10);
filler.fill(0);
packet.writeBuffer(filler);
packet.skip(10);
packet.writeBuffer(this.authPluginData2);
packet.writeInt8(0);
packet.writeString('mysql_native_password');
Expand Down
6 changes: 4 additions & 2 deletions lib/packets/handshake_response.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Buffer = require('safe-buffer').Buffer;

var ClientConstants = require('../constants/client.js');
var Charsets = require('../constants/charsets.js');
var Packet = require('../packets/packet.js');
Expand Down Expand Up @@ -88,7 +90,7 @@ HandshakeResponse.prototype.serializeResponse = function (buffer) {
packet.writeInt8(this.authToken.length);
packet.writeBuffer(this.authToken);
} else {
packet.writeNullTerminatedString(Buffer(this.authToken));
packet.writeNullTerminatedString(Buffer.from(this.authToken));
} if (isSet('CONNECT_WITH_DB')) {
packet.writeNullTerminatedString(this.database);
}
Expand Down Expand Up @@ -124,7 +126,7 @@ HandshakeResponse.prototype.toPacket = function ()
// dry run: calculate resulting packet length
var p = this.serializeResponse(Packet.MockBuffer());

return this.serializeResponse(new Buffer(p.offset));
return this.serializeResponse(Buffer.allocUnsafe(p.offset));
};

module.exports = HandshakeResponse;
8 changes: 5 additions & 3 deletions lib/packets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
}
});

var Buffer = require('safe-buffer').Buffer;

// simple packets:
var Packet = require('./packet');
module.exports.Packet = Packet;
Expand All @@ -32,7 +34,7 @@ module.exports.OK.toPacket = function (args) {
var length = 9 + Packet.lengthCodedNumberLength(affectedRows);
length += Packet.lengthCodedNumberLength(insertId);

var buffer = new Buffer(length);
var buffer = Buffer.allocUnsafe(length);
var packet = new Packet(0, buffer, 0, length);
packet.offset = 4;
packet.writeInt8(0);
Expand All @@ -56,7 +58,7 @@ module.exports.EOF.toPacket = function (warnings, statusFlags) {
if (typeof statusFlags == 'undefined') {
statusFlags = 0;
}
var packet = new Packet(0, new Buffer(9), 0, 9);
var packet = new Packet(0, Buffer.allocUnsafe(9), 0, 9);
packet.offset = 4;
packet.writeInt8(0xfe);
packet.writeInt16(warnings);
Expand All @@ -70,7 +72,7 @@ module.exports.Error = function Error () {

module.exports.Error.toPacket = function (args) {
var length = 13 + Buffer.byteLength(args.message, 'utf8');
var packet = new Packet(0, new Buffer(length), 0, length);
var packet = new Packet(0, Buffer.allocUnsafe(length), 0, length);
packet.offset = 4;
packet.writeInt8(0xff);
packet.writeInt16(args.code);
Expand Down
11 changes: 8 additions & 3 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// var BigNumber = require("bignumber.js");
var ErrorCodeToName = require('../constants/errors.js');

var NativeBuffer = require('buffer').Buffer;
var Buffer = require('safe-buffer').Buffer;
var Long = require('long');

function Packet (id, buffer, start, end)
Expand Down Expand Up @@ -731,6 +732,10 @@ Packet.prototype.writeString = function (s) {
return;
}

if (s.length === 0) {
return;
}

var bytes = Buffer.byteLength(s, 'utf8');
this.buffer.write(s, this.offset, bytes, 'utf8');
this.offset += bytes;
Expand Down Expand Up @@ -800,8 +805,8 @@ Packet.prototype.type = function () {

Packet.MockBuffer = function () {
var noop = function () {};
var res = Buffer(0);
for (var op in Buffer.prototype) {
var res = Buffer.alloc(0);
for (var op in NativeBuffer.prototype) {
if (typeof res[op] == 'function') {
res[op] = noop;
}
Expand Down
Loading