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

Revert "refactor: update Buffer constructors to from and alloc (#1141)" #1144

Merged
merged 1 commit into from
Jun 7, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/common.js
Original file line number Diff line number Diff line change
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 = Buffer.from(utfEncodedBuffer, 'utf8');
var reconstructedBuffer = new Buffer(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 Buffer.alloc(0);
return new Buffer(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
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ Interceptor.prototype.basicAuth = function basicAuth(options) {
var username = options['user'];
var password = options['pass'] || '';
var name = 'authorization';
var value = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
var value = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
this.interceptorMatchHeaders.push({ name: name, value: value });
return this;
};
Expand Down
8 changes: 4 additions & 4 deletions lib/recorder.js
Original file line number Diff line number Diff line change
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 = Buffer.from(chunk);
chunk = new Buffer(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 = Buffer.from(data, encoding);
data = new Buffer(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 = Buffer.from(data, encoding);
data = new Buffer(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 = Buffer.from(data, encoding);
data = new Buffer(data, encoding);
}
bodyChunks.push(data);
}
Expand Down
16 changes: 8 additions & 8 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
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 ' + (Buffer.from(options.auth)).toString('base64'));
setHeader(req, 'Authorization', 'Basic ' + (new Buffer(options.auth)).toString('base64'));
}

if (! req.connection) {
Expand All @@ -132,7 +132,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
if (!req.aborted) {
if (buffer) {
if (!Buffer.isBuffer(buffer)) {
buffer = Buffer.from(buffer, encoding);
buffer = new Buffer(buffer, encoding);
}
requestBodyBuffers.push(buffer);
}
Expand Down Expand Up @@ -319,13 +319,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(Buffer.from(requestBody, 'hex')), 'hex')
requestBody = String(zlib.gunzipSync(new Buffer(requestBody, 'hex')), 'hex')
} else if (requestBody && common.contentEncoding(options.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(Buffer.from(requestBody, 'hex')), 'hex')
requestBody = String(zlib.inflateSync(new Buffer(requestBody, 'hex')), 'hex')
}

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

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

} else {
Expand All @@ -369,15 +369,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 = Buffer.from(responseBody, 'hex');
responseBody = new Buffer(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 = Buffer.from(interceptor.body, 'utf8');
responseBody = new Buffer(interceptor.body, 'utf8');
}
}
}
Expand Down Expand Up @@ -443,7 +443,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
});
} else if (responseBody && !Buffer.isBuffer(responseBody)) {
if (typeof responseBody === 'string') {
responseBody = Buffer.from(responseBody);
responseBody = new Buffer(responseBody);
} else {
responseBody = JSON.stringify(responseBody);
response.headers['content-type'] = 'application/json';
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Socket.prototype.applyDelay = function applyDelay(delayMs) {
};

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

Socket.prototype.destroy = function destroy() {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ tap.test('isBinaryBuffer works', function(t) {
t.false(common.isBinaryBuffer(''));

// Returns true for binary buffers.
t.true(common.isBinaryBuffer(Buffer.from('8001', 'hex')));
t.true(common.isBinaryBuffer(new Buffer('8001', 'hex')));

// Returns false for buffers containing strings.
t.false(common.isBinaryBuffer(Buffer.from('8001', 'utf8')));
t.false(common.isBinaryBuffer(new Buffer('8001', 'utf8')));

t.end();

Expand Down
16 changes: 8 additions & 8 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ test("response pipe", function(t) {
function Constructor() {
events.EventEmitter.call(this);

this.buffer = Buffer.alloc(0);
this.buffer = new Buffer(0);
this.writable = true;
}

Expand All @@ -1642,7 +1642,7 @@ test("response pipe", function(t) {
};

Constructor.prototype.write = function(chunk) {
var buf = Buffer.alloc(this.buffer.length + chunk.length);
var buf = new Buffer(this.buffer.length + chunk.length);

this.buffer.copy(buf);
chunk.copy(buf, this.buffer.length);
Expand Down Expand Up @@ -1682,7 +1682,7 @@ test("response pipe without implicit end", function(t) {
function Constructor() {
events.EventEmitter.call(this);

this.buffer = Buffer.alloc(0);
this.buffer = new Buffer(0);
this.writable = true;
}

Expand All @@ -1693,7 +1693,7 @@ test("response pipe without implicit end", function(t) {
};

Constructor.prototype.write = function(chunk) {
var buf = Buffer.alloc(this.buffer.length + chunk.length);
var buf = new Buffer(this.buffer.length + chunk.length);

this.buffer.copy(buf);
chunk.copy(buf, this.buffer.length);
Expand Down Expand Up @@ -3750,7 +3750,7 @@ test('define() works with binary buffers', function(t) {
t.end();
});

req.write(Buffer.from(nockDef.body, 'hex'));
req.write(new Buffer(nockDef.body, 'hex'));
req.end();

});
Expand Down Expand Up @@ -3873,7 +3873,7 @@ test('sending binary and receiving JSON should work ', function(t) {
mikealRequest({
method: 'POST',
uri: 'http://example.com/some/path',
body: Buffer.from('ffd8ffe000104a46494600010101006000600000ff', 'hex'),
body: new Buffer('ffd8ffe000104a46494600010101006000600000ff', 'hex'),
headers: { 'Accept': 'application/json', 'Content-Length': 23861 }
}, function(err, res, body) {
scope.done();
Expand All @@ -3896,7 +3896,7 @@ test('sending binary and receiving JSON should work ', function(t) {

test('fix #146 - resume() is automatically invoked when the response is drained', function(t) {
var replyLength = 1024 * 1024;
var replyBuffer = Buffer.from((new Array(replyLength + 1)).join("."));
var replyBuffer = new Buffer((new Array(replyLength + 1)).join("."));
t.equal(replyBuffer.length, replyLength);

nock("http://www.abc.com")
Expand Down Expand Up @@ -4434,7 +4434,7 @@ test("match basic authentication header", function(t) {
var username = 'testuser'
, password = 'testpassword'
, authString = username + ":" + password
, encrypted = (Buffer.from(authString)).toString( 'base64' );
, encrypted = (new Buffer(authString)).toString( 'base64' );

var scope = nock('http://www.headdy.com')
.get('/')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_isomorphic_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test("basicAuth match works", function (t) {

return fetch('http://isomorphicfetchland.com/path2', {
headers: {
'Authorization': 'Basic ' + Buffer.from('username:password').toString('base64'),
'Authorization': 'Basic ' + new Buffer('username:password').toString('base64'),
}
}).
then(function (res) {
Expand All @@ -78,7 +78,7 @@ test("basicAuth match works", function (t) {
});

test("matchHeader works", function (t) {
var authorizationHeader = 'Basic ' + Buffer.from('username:password').toString('base64');
var authorizationHeader = 'Basic ' + new Buffer('username:password').toString('base64');

var scope = nock('http://isomorphicfetchland.com').
get('/path2').
Expand Down
2 changes: 1 addition & 1 deletion tests/test_recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ test("respects http.request() consumers", function(t) {
, path:'/' }
;
var req = http.request(options, function (res) {
var buffer = Buffer.from('');
var buffer = new Buffer('');

setTimeout(function () {
res
Expand Down
6 changes: 3 additions & 3 deletions tests/test_s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('works with s3, body < 1024 ^ 2', function (t) {

bucket.putObject({
Key: 'key',
Body: Buffer.alloc(1024 * 1024 - 1), // works
Body: new Buffer(1024 * 1024 - 1), // works
// Body: new Buffer(1024 * 1024), // doesn't work
ContentType: 'binary/octet-stream'
},
Expand Down Expand Up @@ -57,7 +57,7 @@ test('works with s3, body = 10 * 1024 ^ 2', function (t) {

bucket.putObject({
Key: 'key',
Body: Buffer.alloc(10 * 1024 * 1024), // doesn't work
Body: new Buffer(10 * 1024 * 1024), // doesn't work
ContentType: 'binary/octet-stream'
},
function (err, resp) {
Expand Down Expand Up @@ -88,7 +88,7 @@ test('works with s3, body = 16 * 1024 ^ 2', function (t) {

bucket.putObject({
Key: 'key',
Body: Buffer.alloc(16 * 1024 * 1024), // doesn't work
Body: new Buffer(16 * 1024 * 1024), // doesn't work
ContentType: 'binary/octet-stream'
},
function (err, resp) {
Expand Down