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

Remove all instances of new Buffer() #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": false
}
2 changes: 1 addition & 1 deletion lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ OutgoingRequest.prototype._start = function _start(stream, options) {
delete headers.host;

if (options.auth) {
headers.authorization = 'Basic ' + new Buffer(options.auth).toString('base64');
headers.authorization = 'Basic ' + Buffer.from(options.auth).toString('base64');
}

headers[':scheme'] = options.protocol.slice(0, -1);
Expand Down
14 changes: 7 additions & 7 deletions lib/protocol/compressor.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function entryFromPair(pair) {
var DEFAULT_HEADER_TABLE_LIMIT = 4096;

function size(entry) {
return (new Buffer(entry[0] + entry[1], 'utf8')).length + 32;
return (Buffer.from(entry[0] + entry[1], 'utf8')).length + 32;
}

// The `add(index, entry)` can be used to [manage the header table][tablemgmt]:
Expand Down Expand Up @@ -396,7 +396,7 @@ HeaderSetCompressor.prototype._flush = function _flush(callback) {
HeaderSetCompressor.integer = function writeInteger(I, N) {
var limit = Math.pow(2,N) - 1;
if (I < limit) {
return [new Buffer([I])];
return [Buffer.from([I])];
}

var bytes = [];
Expand All @@ -418,7 +418,7 @@ HeaderSetCompressor.integer = function writeInteger(I, N) {
I = Q;
}

return [new Buffer(bytes)];
return [Buffer.from(bytes)];
};

// The inverse algorithm:
Expand Down Expand Up @@ -530,7 +530,7 @@ HuffmanTable.prototype.encode = function encode(buffer) {
add(this.codes[256] >> (this.lengths[256] - space));
}

return new Buffer(result);
return Buffer.from(result);
};

HuffmanTable.prototype.decode = function decode(buffer) {
Expand All @@ -552,7 +552,7 @@ HuffmanTable.prototype.decode = function decode(buffer) {
}
}

return new Buffer(result);
return Buffer.from(result);
};

// The initializer arrays for the Huffman tables are generated with feeding the tables from the
Expand Down Expand Up @@ -852,7 +852,7 @@ HuffmanTable.huffmanTable = new HuffmanTable([
// +---+---+---+---+---+---+---+---+

HeaderSetCompressor.string = function writeString(str) {
str = new Buffer(str, 'utf8');
str = Buffer.from(str, 'utf8');

var huffman = HuffmanTable.huffmanTable.encode(str);
if (huffman.length < str.length) {
Expand Down Expand Up @@ -1341,7 +1341,7 @@ function concat(buffers) {
size += buffers[i].length;
}

var concatenated = new Buffer(size);
var concatenated = Buffer.alloc(size);
for (var cursor = 0, j = 0; j < buffers.length; cursor += buffers[j].length, j++) {
buffers[j].copy(concatenated, cursor);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ Connection.prototype._generatePingId = function _generatePingId() {
// Sending a ping and calling `callback` when the answer arrives
Connection.prototype.ping = function ping(callback) {
var id = this._generatePingId();
var data = new Buffer(id, 'hex');
var data = Buffer.from(id, 'hex');
this._pings[id] = callback;

this._log.debug({ data: data }, 'Sending PING.');
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Endpoint.prototype = Object.create(Duplex.prototype, { constructor: { value: End
// Handshake
// ---------

var CLIENT_PRELUDE = new Buffer('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n');
var CLIENT_PRELUDE = Buffer.from('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n');

// Writing the client header is simple and synchronous.
Endpoint.prototype._writePrelude = function _writePrelude() {
Expand Down
32 changes: 16 additions & 16 deletions lib/protocol/framer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Deserializer.prototype = Object.create(Transform.prototype, { constructor: { val
// payload). The `_cursor` is used to track the progress.
Deserializer.prototype._next = function(size) {
this._cursor = 0;
this._buffer = new Buffer(size);
this._buffer = Buffer.alloc(size);
this._waitingForHeader = !this._waitingForHeader;
if (this._waitingForHeader) {
this._frame = {};
Expand Down Expand Up @@ -204,7 +204,7 @@ var genericAttributes = ['type', 'flags', 'stream'];
var typeSpecificAttributes = {};

Serializer.commonHeader = function writeCommonHeader(frame, buffers) {
var headerBuffer = new Buffer(COMMON_HEADER_SIZE);
var headerBuffer = Buffer.alloc(COMMON_HEADER_SIZE);

var size = 0;
for (var i = 0; i < buffers.length; i++) {
Expand Down Expand Up @@ -363,7 +363,7 @@ typeSpecificAttributes.HEADERS = ['priorityDependency', 'priorityWeight', 'exclu

Serializer.HEADERS = function writeHeadersPriority(frame, buffers) {
if (frame.flags.PRIORITY) {
var buffer = new Buffer(5);
var buffer = Buffer.alloc(5);
assert((0 <= frame.priorityDependency) && (frame.priorityDependency <= 0x7fffffff), frame.priorityDependency);
buffer.writeUInt32BE(frame.priorityDependency, 0);
if (frame.exclusiveDependency) {
Expand Down Expand Up @@ -397,7 +397,7 @@ Deserializer.HEADERS = function readHeadersPriority(buffer, frame) {
}

if (frame.flags.PRIORITY) {
var dependencyData = new Buffer(4);
var dependencyData = Buffer.alloc(4);
buffer.copy(dependencyData, 0, dataOffset, dataOffset + 4);
dataOffset += 4;
frame.exclusiveDependency = !!(dependencyData[0] & 0x80);
Expand Down Expand Up @@ -442,7 +442,7 @@ typeSpecificAttributes.PRIORITY = ['priorityDependency', 'priorityWeight', 'excl
// The payload of a PRIORITY frame contains an exclusive bit, a 31-bit dependency, and an 8-bit weight

Serializer.PRIORITY = function writePriority(frame, buffers) {
var buffer = new Buffer(5);
var buffer = Buffer.alloc(5);
assert((0 <= frame.priorityDependency) && (frame.priorityDependency <= 0x7fffffff), frame.priorityDependency);
buffer.writeUInt32BE(frame.priorityDependency, 0);
if (frame.exclusiveDependency) {
Expand All @@ -459,7 +459,7 @@ Deserializer.PRIORITY = function readPriority(buffer, frame) {
// PRIORITY frames are 5 bytes long. Bad peer!
return 'FRAME_SIZE_ERROR';
}
var dependencyData = new Buffer(4);
var dependencyData = Buffer.alloc(4);
buffer.copy(dependencyData, 0, 0, 4);
frame.exclusiveDependency = !!(dependencyData[0] & 0x80);
dependencyData[0] &= 0x7f;
Expand Down Expand Up @@ -490,7 +490,7 @@ typeSpecificAttributes.RST_STREAM = ['error'];
// code (see Error Codes). The error code indicates why the stream is being terminated.

Serializer.RST_STREAM = function writeRstStream(frame, buffers) {
var buffer = new Buffer(4);
var buffer = Buffer.alloc(4);
var code = errorCodes.indexOf(frame.error);
assert((0 <= code) && (code <= 0xffffffff), code);
buffer.writeUInt32BE(code, 0);
Expand Down Expand Up @@ -555,7 +555,7 @@ Serializer.SETTINGS = function writeSettings(frame, buffers) {
});
assert(settingsLeft.length === 0, 'Unknown settings: ' + settingsLeft.join(', '));

var buffer = new Buffer(settings.length * 6);
var buffer = Buffer.alloc(settings.length * 6);
for (var i = 0; i < settings.length; i++) {
buffer.writeUInt16BE(settings[i].id & 0xffff, i*6);
buffer.writeUInt32BE(settings[i].value, i*6 + 2);
Expand Down Expand Up @@ -651,7 +651,7 @@ typeSpecificAttributes.PUSH_PROMISE = ['promised_stream', 'headers', 'data'];
// additional context for the stream.

Serializer.PUSH_PROMISE = function writePushPromise(frame, buffers) {
var buffer = new Buffer(4);
var buffer = Buffer.alloc(4);

var promised_stream = frame.promised_stream;
assert((0 <= promised_stream) && (promised_stream <= 0x7fffffff), promised_stream);
Expand Down Expand Up @@ -747,7 +747,7 @@ typeSpecificAttributes.GOAWAY = ['last_stream', 'error'];
// closing the connection.

Serializer.GOAWAY = function writeGoaway(frame, buffers) {
var buffer = new Buffer(8);
var buffer = Buffer.alloc(8);

var last_stream = frame.last_stream;
assert((0 <= last_stream) && (last_stream <= 0x7fffffff), last_stream);
Expand Down Expand Up @@ -798,7 +798,7 @@ typeSpecificAttributes.WINDOW_UPDATE = ['window_size'];
// reserved.

Serializer.WINDOW_UPDATE = function writeWindowUpdate(frame, buffers) {
var buffer = new Buffer(4);
var buffer = Buffer.alloc(4);

var window_size = frame.window_size;
assert((0 < window_size) && (window_size <= 0x7fffffff), window_size);
Expand Down Expand Up @@ -888,7 +888,7 @@ function hexencode(s) {
for (var i = 0; i < s.length; i++) {
if (!istchar(s[i])) {
t += '%';
t += new Buffer(s[i]).toString('hex');
t += Buffer.from(s[i]).toString('hex');
} else {
t += s[i];
}
Expand All @@ -897,17 +897,17 @@ function hexencode(s) {
}

Serializer.ALTSVC = function writeAltSvc(frame, buffers) {
var buffer = new Buffer(2);
var buffer = Buffer.alloc(2);
buffer.writeUInt16BE(frame.origin.length, 0);
buffers.push(buffer);
buffers.push(new Buffer(frame.origin, 'ascii'));
buffers.push(Buffer.from(frame.origin, 'ascii'));

var fieldValue = hexencode(frame.protocolID) + '="' + frame.host + ':' + frame.port + '"';
if (frame.maxAge !== 86400) { // 86400 is the default
fieldValue += "; ma=" + frame.maxAge;
}

buffers.push(new Buffer(fieldValue, 'ascii'));
buffers.push(Buffer.from(fieldValue, 'ascii'));
};

function stripquotes(s) {
Expand Down Expand Up @@ -1031,7 +1031,7 @@ function unescape(s) {
hexvalue += s[i];
}
if (hexvalue.length > 0) {
t += new Buffer(hexvalue, 'hex').toString();
t += Buffer.from(hexvalue, 'hex').toString();
} else {
t += '%';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ Stream.prototype._send = function _send() {
// flag on the last frame. If there's no frame in the queue, or if it doesn't support this flag,
// then we create a 0 length DATA frame. We could do this all the time, but putting the flag on an
// existing frame is a nice optimization.
var emptyBuffer = new Buffer(0);
var emptyBuffer = Buffer.alloc(0);
Stream.prototype._finishing = function _finishing() {
var endFrame = {
type: 'DATA',
Expand Down
Loading