Skip to content

Commit 9cfe50a

Browse files
committed
switch from new Buffer() to Buffer.from()
avoids deprecation failures in nodejs 10 Fixes #256
1 parent 2ca2136 commit 9cfe50a

11 files changed

+32
-32
lines changed

examples/multipart-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var needle = require('./../');
22

33
var url = 'http://posttestserver.com/post.php?dir=needle';
44

5-
var black_pixel = new Buffer("R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=", 'base64');
5+
var black_pixel = Buffer.from("R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=", 'base64');
66

77
var data = {
88
foo: 'bar',

lib/auth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function md5(string) {
2121

2222
function basic(user, pass) {
2323
var str = typeof pass == 'undefined' ? user : [user, pass].join(':');
24-
return 'Basic ' + new Buffer(str).toString('base64');
24+
return 'Basic ' + Buffer.from(str).toString('base64');
2525
}
2626

2727
////////////////////

lib/multipart.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function generate_part(name, part, boundary, callback) {
7373
}
7474

7575
return_part += '\r\n\r\n';
76-
return_part += new Buffer(String(part.value), 'utf8').toString('binary');
76+
return_part += Buffer.from(String(part.value), 'utf8').toString('binary');
7777
append();
7878

7979
}

lib/needle.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ Needle.prototype.start = function() {
347347
: json ? JSON.stringify(data) : stringify(data);
348348

349349
// ensure we have a buffer so bytecount is correct.
350-
body = new Buffer(body, config.encoding);
350+
body = Buffer.from(body, config.encoding);
351351
}
352352

353353
}
@@ -641,7 +641,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
641641
var chunk;
642642
while ((chunk = this.read()) !== null) {
643643
// We're either pushing buffers or objects, never strings.
644-
if (typeof chunk == 'string') chunk = new Buffer(chunk);
644+
if (typeof chunk == 'string') chunk = Buffer.from(chunk);
645645

646646
// Push all chunks to resp.body. We'll bind them in resp.end().
647647
resp.body.push(chunk);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@
6565
},
6666
"license": "MIT",
6767
"engines": {
68-
"node": ">= 0.10.x"
68+
"node": ">= 4.5.0"
6969
}
7070
}

test/basic_auth_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('Basic Auth', function() {
1919

2020
var get_auth = function(header) {
2121
var token = header.split(/\s+/).pop();
22-
return token && new Buffer(token, 'base64').toString().split(':');
22+
return token && Buffer.from(token, 'base64').toString().split(':');
2323
}
2424

2525
describe('when neither username or password are passed', function() {

test/cookies_spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('cookies', function() {
129129
needle.get(TEST_HOST + ':' + ALL_COOKIES_TEST_PORT, function(error, response) {
130130
should.not.exist(error)
131131
var whatever = 'efbfbdefbfbdefbfbdefbfbdefbfbd2defbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbdefbfbd';
132-
new Buffer(response.cookies.geo_city).toString('hex').should.eql(whatever)
132+
Buffer.from(response.cookies.geo_city).toString('hex').should.eql(whatever)
133133
done();
134134
});
135135
})
@@ -302,4 +302,4 @@ describe('cookies', function() {
302302
});
303303
});
304304
});
305-
});
305+
});

test/output_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe('with output option', function() {
180180

181181
describe('for a binary file', function() {
182182

183-
var pixel = new Buffer("base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs", "base64");
183+
var pixel = Buffer.from("base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs", "base64");
184184

185185
before(function() {
186186
handler = function(req, res) {

test/post_data_spec.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ multiparts.push(['Content-Disposition: form-data; name=\"foo\"'])
1010
multiparts.push(['\r\nbar\r\n----------------------NODENEEDLEHTTPCLIENT--'])
1111
// multiparts.push(['Content-Disposition: form-data; name=\"test\"'])
1212
// multiparts.push(['\r\næµè¯\r\n----------------------NODENEEDLEHTTPCLIENT--'])
13-
// multiparts.push(['\r\n' + new Buffer('测试').toString() + '\r\n----------------------NODENEEDLEHTTPCLIENT--'])
13+
// multiparts.push(['\r\n' + Buffer.from('测试').toString() + '\r\n----------------------NODENEEDLEHTTPCLIENT--'])
1414

1515

1616
describe('post data (e.g. request body)', function() {
@@ -150,7 +150,7 @@ describe('post data (e.g. request body)', function() {
150150
spy.called.should.be.true;
151151

152152
spy.args[0][0].should.be.an.instanceof(String);
153-
new Buffer(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
153+
Buffer.from(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
154154
done();
155155
})
156156
})
@@ -187,7 +187,7 @@ describe('post data (e.g. request body)', function() {
187187
post({ foo: 'bar', test: '测试' }, { multipart: true }, function(err, resp) {
188188
spy.called.should.be.true;
189189
spy.args[0][0].should.be.an.instanceof(String);
190-
new Buffer(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
190+
Buffer.from(spy.args[0][0]).toString('hex').should.eql('2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d22666f6f220d0a0d0a6261720d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e540d0a436f6e74656e742d446973706f736974696f6e3a20666f726d2d646174613b206e616d653d2274657374220d0a0d0ac3a6c2b5c28bc3a8c2afc2950d0a2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d4e4f44454e4545444c4548545450434c49454e542d2d')
191191
done();
192192
})
193193
})
@@ -761,14 +761,14 @@ describe('post data (e.g. request body)', function() {
761761
it('sends request', function(done) {
762762
spystub_request();
763763

764-
get(new Buffer('foobar'), { json: false }, function(err, resp) {
764+
get(Buffer.from('foobar'), { json: false }, function(err, resp) {
765765
check_request('get');
766766
done();
767767
})
768768
})
769769

770770
it('sets Content-Type header', function(done) {
771-
get(new Buffer('foobar'), { json: false }, function(err, resp) {
771+
get(Buffer.from('foobar'), { json: false }, function(err, resp) {
772772
// should.not.exist(resp.body.headers['content-type']);
773773
resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
774774

@@ -777,15 +777,15 @@ describe('post data (e.g. request body)', function() {
777777
})
778778

779779
it('doesnt change default Accept header', function(done) {
780-
get(new Buffer('foobar'), { json: false }, function(err, resp) {
780+
get(Buffer.from('foobar'), { json: false }, function(err, resp) {
781781
// resp.body contains 'header' and 'body', mirroring what we sent
782782
resp.body.headers['accept'].should.equal('*/*');
783783
done();
784784
})
785785
})
786786

787787
it('writes as buffer', function(done) {
788-
get(new Buffer('foobar'), { json: false }, function(err, resp) {
788+
get(Buffer.from('foobar'), { json: false }, function(err, resp) {
789789
spy.called.should.be.true;
790790
spy.args[0][0].should.be.an.instanceof(Buffer);
791791
spy.args[0][0].toString().should.equal('foobar');
@@ -801,28 +801,28 @@ describe('post data (e.g. request body)', function() {
801801
it('sends request, without setting a querystring', function(done) {
802802
spystub_request();
803803

804-
get(new Buffer('foobar'), { json: true }, function(err, resp) {
804+
get(Buffer.from('foobar'), { json: true }, function(err, resp) {
805805
check_request('get');
806806
done();
807807
})
808808
})
809809

810810
it('sets Content-Type header', function(done) {
811-
get(new Buffer('foobar'), { json: true }, function(err, resp) {
811+
get(Buffer.from('foobar'), { json: true }, function(err, resp) {
812812
resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
813813
done();
814814
})
815815
})
816816

817817
it('set Accept header to application/json', function(done) {
818-
get(new Buffer('foobar'), { json: true }, function(err, resp) {
818+
get(Buffer.from('foobar'), { json: true }, function(err, resp) {
819819
resp.body.headers['accept'].should.equal('application/json');
820820
done();
821821
})
822822
})
823823

824824
it('writes JSON.stringify version of object', function(done) {
825-
get(new Buffer('foobar'), { json: true }, function(err, resp) {
825+
get(Buffer.from('foobar'), { json: true }, function(err, resp) {
826826
spy.called.should.be.true;
827827
spy.args[0][0].toString().should.eql('foobar')
828828
resp.body.body.should.eql('foobar');
@@ -841,29 +841,29 @@ describe('post data (e.g. request body)', function() {
841841
it('sends request', function(done) {
842842
spystub_request();
843843

844-
post(new Buffer('foobar'), { json: false }, function(err, resp) {
844+
post(Buffer.from('foobar'), { json: false }, function(err, resp) {
845845
check_request('post');
846846
done();
847847
})
848848
})
849849

850850
it('sets Content-Type header to www-form-urlencoded', function(done) {
851-
post(new Buffer('foobar'), { json: false }, function(err, resp) {
851+
post(Buffer.from('foobar'), { json: false }, function(err, resp) {
852852
resp.body.headers['content-type'].should.equal('application/x-www-form-urlencoded');
853853
done();
854854
})
855855
})
856856

857857
it('doesnt change default Accept header', function(done) {
858-
post(new Buffer('foobar'), { json: false }, function(err, resp) {
858+
post(Buffer.from('foobar'), { json: false }, function(err, resp) {
859859
// resp.body contains 'header' and 'body', mirroring what we sent
860860
resp.body.headers['accept'].should.equal('*/*');
861861
done();
862862
})
863863
})
864864

865865
it('writes as buffer', function(done) {
866-
post(new Buffer('foobar'), { json: false }, function(err, resp) {
866+
post(Buffer.from('foobar'), { json: false }, function(err, resp) {
867867
spy.called.should.be.true;
868868
spy.args[0][0].should.be.an.instanceof(Buffer);
869869
spy.args[0][0].toString().should.equal('foobar');
@@ -879,28 +879,28 @@ describe('post data (e.g. request body)', function() {
879879
it('sends request', function(done) {
880880
spystub_request();
881881

882-
post(new Buffer('foobar'), { json: true }, function(err, resp) {
882+
post(Buffer.from('foobar'), { json: true }, function(err, resp) {
883883
check_request('post');
884884
done();
885885
})
886886
})
887887

888888
it('sets Content-Type header', function(done) {
889-
post(new Buffer('foobar'), { json: true }, function(err, resp) {
889+
post(Buffer.from('foobar'), { json: true }, function(err, resp) {
890890
resp.body.headers['content-type'].should.equal('application/json; charset=utf-8');
891891
done();
892892
})
893893
})
894894

895895
it('set Accept header to application/json', function(done) {
896-
post(new Buffer('foobar'), { json: true }, function(err, resp) {
896+
post(Buffer.from('foobar'), { json: true }, function(err, resp) {
897897
resp.body.headers['accept'].should.equal('application/json');
898898
done();
899899
})
900900
})
901901

902902
it('passes raw buffer (assuming its a JSON string beneath)', function(done) {
903-
post(new Buffer('foobar'), { json: true }, function(err, resp) {
903+
post(Buffer.from('foobar'), { json: true }, function(err, resp) {
904904
spy.called.should.be.true;
905905
spy.args[0][0].toString().should.eql('foobar')
906906
resp.body.body.should.eql('foobar');
@@ -1018,4 +1018,4 @@ describe('post data (e.g. request body)', function() {
10181018

10191019
})
10201020

1021-
})
1021+
})

test/proxy_spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('proxy option', function() {
5151

5252
function get_auth(header) {
5353
var token = header.split(/\s+/).pop();
54-
return token && new Buffer(token, 'base64').toString().split(':');
54+
return token && Buffer.from(token, 'base64').toString().split(':');
5555
}
5656

5757
function no_proxy_auth(done) {

test/utils/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function multipart_post(url){
4646
var data = 'Plain text data.\nLorem ipsum dolor sit amet.\nBla bla bla.\n';
4747
fs.writeFileSync(filename, data);
4848

49-
var black_pixel = new Buffer("data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=".replace(/^data:image\/\w+;base64,/, ""), "base64");
49+
var black_pixel = Buffer.from("data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=".replace(/^data:image\/\w+;base64,/, ""), "base64");
5050

5151
var data = {
5252
foo: 'bar',

0 commit comments

Comments
 (0)