Skip to content

Commit

Permalink
refactor: update Buffer constructors to from and alloc
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop official support for Node < 6
  • Loading branch information
Ethan-Arrowood authored and gr2m committed Sep 14, 2018
1 parent 59060d5 commit c94b89d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/common.js
Expand Up @@ -50,7 +50,7 @@ var isBinaryBuffer = function(buffer) {

// Test if the buffer can be reconstructed verbatim from its utf8 encoding.
var utfEncodedBuffer = buffer.toString('utf8');
var reconstructedBuffer = new Buffer(utfEncodedBuffer, 'utf8');
var reconstructedBuffer = Buffer.from(utfEncodedBuffer, 'utf8');
var compareBuffers = function(lhs, rhs) {
if(lhs.length !== rhs.length) {
return false;
Expand Down Expand Up @@ -80,7 +80,7 @@ var isBinaryBuffer = function(buffer) {
var mergeChunks = function(chunks) {

if(_.isEmpty(chunks)) {
return new Buffer(0);
return Buffer.alloc(0);
}

// We assume that all chunks are Buffer objects if the first is buffer object.
Expand Down
2 changes: 1 addition & 1 deletion lib/interceptor.js
Expand Up @@ -405,7 +405,7 @@ Interceptor.prototype.basicAuth = function basicAuth(options) {
var username = options['user'];
var password = options['pass'] || '';
var name = 'authorization';
var value = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
var value = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
this.interceptorMatchHeaders.push({ name: name, value: value });
return this;
};
Expand Down
8 changes: 4 additions & 4 deletions lib/recorder.js
Expand Up @@ -56,7 +56,7 @@ var getBodyFromChunks = function(chunks, headers) {
body: _.map(chunks, function(chunk) {
if(!Buffer.isBuffer(chunk)) {
if (typeof chunk === 'string') {
chunk = new Buffer(chunk);
chunk = Buffer.from(chunk);
} else {
throw new Error('content-encoded responses must all be binary buffers');
}
Expand Down Expand Up @@ -330,7 +330,7 @@ function record(rec_options) {
res.push = function(data) {
if (data) {
if (encoding) {
data = new Buffer(data, encoding);
data = Buffer.from(data, encoding);
}
dataChunks.push(data);
}
Expand Down Expand Up @@ -358,7 +358,7 @@ function record(rec_options) {
if (data) {
debug(thisRecordingId, 'new', proto, 'body chunk');
if (! Buffer.isBuffer(data)) {
data = new Buffer(data, encoding);
data = Buffer.from(data, encoding);
}
bodyChunks.push(data);
}
Expand All @@ -373,7 +373,7 @@ function record(rec_options) {
if (data) {
debug(thisRecordingId, 'new', proto, 'body chunk');
if (! Buffer.isBuffer(data)) {
data = new Buffer(data, encoding);
data = Buffer.from(data, encoding);
}
bodyChunks.push(data);
}
Expand Down
16 changes: 8 additions & 8 deletions lib/request_overrider.js
Expand Up @@ -111,7 +111,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {

/// options.auth
if (options.auth && (! options.headers || ! options.headers.authorization)) {
setHeader(req, 'Authorization', 'Basic ' + (new Buffer(options.auth)).toString('base64'));
setHeader(req, 'Authorization', 'Basic ' + (Buffer.from(options.auth)).toString('base64'));
}

if (! req.connection) {
Expand All @@ -131,7 +131,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
if (!req.aborted) {
if (buffer) {
if (!Buffer.isBuffer(buffer)) {
buffer = new Buffer(buffer, encoding);
buffer = Buffer.from(buffer, encoding);
}
requestBodyBuffers.push(buffer);
}
Expand Down Expand Up @@ -318,13 +318,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
emitError(new Error('Gzip encoding is currently not supported in this version of Node.'));
return;
}
requestBody = String(zlib.gunzipSync(new Buffer(requestBody, 'hex')), 'hex')
requestBody = String(zlib.gunzipSync(Buffer.from(requestBody, 'hex')), 'hex')
} else if (requestBody && common.contentEncoding(req.headers, 'deflate')) {
if (typeof zlib.deflateSync !== 'function') {
emitError(new Error('Deflate encoding is currently not supported in this version of Node.'));
return;
}
requestBody = String(zlib.inflateSync(new Buffer(requestBody, 'hex')), 'hex')
requestBody = String(zlib.inflateSync(Buffer.from(requestBody, 'hex')), 'hex')
}

requestBody = JSON.parse(requestBody);
Expand Down Expand Up @@ -356,7 +356,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}

responseBuffers = _.map(buffers, function(buffer) {
return new Buffer(buffer, 'hex');
return Buffer.from(buffer, 'hex');
});

} else {
Expand All @@ -368,15 +368,15 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
if(isBinaryRequestBodyBuffer && typeof(responseBody) === 'string') {
// Try to create the buffer from the interceptor's body response as hex.
try {
responseBody = new Buffer(responseBody, 'hex');
responseBody = Buffer.from(responseBody, 'hex');
} catch(err) {
debug('exception during Buffer construction from hex data:', responseBody, '-', err);
}

// Creating buffers does not necessarily throw errors, check for difference in size
if (!responseBody || (interceptor.body.length > 0 && responseBody.length === 0)) {
// We fallback on constructing buffer from utf8 representation of the body.
responseBody = new Buffer(interceptor.body, 'utf8');
responseBody = Buffer.from(interceptor.body, 'utf8');
}
}
}
Expand Down Expand Up @@ -446,7 +446,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
});
} else if (responseBody && !Buffer.isBuffer(responseBody)) {
if (typeof responseBody === 'string') {
responseBody = new Buffer(responseBody);
responseBody = Buffer.from(responseBody);
} else {
responseBody = JSON.stringify(responseBody);
response.headers['content-type'] = 'application/json';
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.js
Expand Up @@ -58,7 +58,7 @@ Socket.prototype.applyDelay = function applyDelay(delayMs) {
};

Socket.prototype.getPeerCertificate = function getPeerCertificate() {
return new Buffer((Math.random() * 10000 + Date.now()).toString()).toString('base64');
return Buffer.from((Math.random() * 10000 + Date.now()).toString()).toString('base64');
};

Socket.prototype.destroy = function destroy() {
Expand Down

0 comments on commit c94b89d

Please sign in to comment.