Skip to content

Commit 787c808

Browse files
mvdnbrkJustinBeckwith
authored andcommitted
Fix: Removes usage of deprecated Buffer constructor. (axios#1555) (axios#1622)
1 parent d74238e commit 787c808

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lib/adapters/http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ module.exports = function httpAdapter(config) {
3939
if (Buffer.isBuffer(data)) {
4040
// Nothing to do...
4141
} else if (utils.isArrayBuffer(data)) {
42-
data = new Buffer(new Uint8Array(data));
42+
data = Buffer.from(new Uint8Array(data));
4343
} else if (utils.isString(data)) {
44-
data = new Buffer(data, 'utf-8');
44+
data = Buffer.from(data, 'utf-8');
4545
} else {
4646
return reject(createError(
4747
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
@@ -124,7 +124,7 @@ module.exports = function httpAdapter(config) {
124124

125125
// Basic proxy authorization
126126
if (proxy.auth) {
127-
var base64 = new Buffer(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
127+
var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
128128
options.headers['Proxy-Authorization'] = 'Basic ' + base64;
129129
}
130130
}

test/unit/adapters/http.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ module.exports = {
181181
var user = 'foo';
182182
var headers = { Authorization: 'Bearer 1234' };
183183
axios.get('http://' + user + '@localhost:4444/', { headers: headers }).then(function (res) {
184-
var base64 = new Buffer(user + ':', 'utf8').toString('base64');
184+
var base64 = Buffer.from(user + ':', 'utf8').toString('base64');
185185
test.equal(res.data, 'Basic ' + base64);
186186
test.done();
187187
});
@@ -195,7 +195,7 @@ module.exports = {
195195
var auth = { username: 'foo', password: 'bar' };
196196
var headers = { Authorization: 'Bearer 1234' };
197197
axios.get('http://localhost:4444/', { auth: auth, headers: headers }).then(function (res) {
198-
var base64 = new Buffer('foo:bar', 'utf8').toString('base64');
198+
var base64 = Buffer.from('foo:bar', 'utf8').toString('base64');
199199
test.equal(res.data, 'Basic ' + base64);
200200
test.done();
201201
});
@@ -288,7 +288,7 @@ module.exports = {
288288
},
289289

290290
testBuffer: function(test) {
291-
var buf = new Buffer(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes)
291+
var buf = Buffer.from(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes)
292292
buf.fill('x');
293293
server = http.createServer(function (req, res) {
294294
test.equal(req.headers['content-length'], buf.length.toString());
@@ -437,7 +437,7 @@ module.exports = {
437437
}
438438
}
439439
}).then(function(res) {
440-
var base64 = new Buffer('user:pass', 'utf8').toString('base64');
440+
var base64 = Buffer.from('user:pass', 'utf8').toString('base64');
441441
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');
442442
test.done();
443443
});
@@ -473,7 +473,7 @@ module.exports = {
473473
process.env.http_proxy = 'http://user:pass@localhost:4000/';
474474

475475
axios.get('http://localhost:4444/').then(function(res) {
476-
var base64 = new Buffer('user:pass', 'utf8').toString('base64');
476+
var base64 = Buffer.from('user:pass', 'utf8').toString('base64');
477477
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy set by process.env.http_proxy');
478478
test.done();
479479
});
@@ -519,7 +519,7 @@ module.exports = {
519519
'Proxy-Authorization': 'Basic abc123'
520520
}
521521
}).then(function(res) {
522-
var base64 = new Buffer('user:pass', 'utf8').toString('base64');
522+
var base64 = Buffer.from('user:pass', 'utf8').toString('base64');
523523
test.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');
524524
test.done();
525525
});

0 commit comments

Comments
 (0)