Skip to content

Commit b198e8d

Browse files
jasnellmafintosh
authored andcommitted
Replace Buffer constructor with buffer-from (#59)
Adds a dependency on buffer-from
1 parent bb2b8d0 commit b198e8d

File tree

7 files changed

+23
-17
lines changed

7 files changed

+23
-17
lines changed

index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Writable = require('readable-stream').Writable
22
var inherits = require('inherits')
3+
var bufferFrom = require('buffer-from')
34

45
if (typeof Uint8Array === 'undefined') {
56
var U8 = require('typedarray').Uint8Array
@@ -87,9 +88,9 @@ function stringConcat (parts) {
8788
} else if (Buffer.isBuffer(p)) {
8889
strings.push(p)
8990
} else if (isBufferish(p)) {
90-
strings.push(new Buffer(p))
91+
strings.push(bufferFrom(p))
9192
} else {
92-
strings.push(new Buffer(String(p)))
93+
strings.push(bufferFrom(String(p)))
9394
}
9495
}
9596
if (Buffer.isBuffer(parts[0])) {
@@ -108,9 +109,9 @@ function bufferConcat (parts) {
108109
if (Buffer.isBuffer(p)) {
109110
bufs.push(p)
110111
} else if (isBufferish(p)) {
111-
bufs.push(new Buffer(p))
112+
bufs.push(bufferFrom(p))
112113
} else {
113-
bufs.push(new Buffer(String(p)))
114+
bufs.push(bufferFrom(String(p)))
114115
}
115116
}
116117
return Buffer.concat(bufs)
@@ -128,7 +129,7 @@ function u8Concat (parts) {
128129
var len = 0
129130
for (var i = 0; i < parts.length; i++) {
130131
if (typeof parts[i] === 'string') {
131-
parts[i] = new Buffer(parts[i])
132+
parts[i] = bufferFrom(parts[i])
132133
}
133134
len += parts[i].length
134135
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
},
2929
"license": "MIT",
3030
"dependencies": {
31+
"buffer-from": "^1.0.0",
3132
"inherits": "^2.0.3",
32-
"typedarray": "^0.0.6",
33-
"readable-stream": "^2.2.2"
33+
"readable-stream": "^2.2.2",
34+
"typedarray": "^0.0.6"
3435
},
3536
"devDependencies": {
3637
"tape": "^4.6.3"

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var a = new Uint8Array(3)
6262
a[0] = 97; a[1] = 98; a[2] = 99
6363
write.write(a)
6464
write.write('!')
65-
write.end(Buffer('!!1'))
65+
write.end(Buffer.from('!!1'))
6666
```
6767

6868
See `test/` for more examples

test/buffer.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ var concat = require('../')
22
var test = require('tape')
33
var TA = require('typedarray')
44
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
5+
var bufferFrom = require('buffer-from')
56

67
test('buffer stream', function (t) {
78
t.plan(2)
89
var buffers = concat(function(out) {
910
t.ok(Buffer.isBuffer(out))
1011
t.equal(out.toString('utf8'), 'pizza Array is not a stringy cat')
1112
})
12-
buffers.write(new Buffer('pizza Array is not a ', 'utf8'))
13-
buffers.write(new Buffer('stringy cat'))
13+
buffers.write(bufferFrom('pizza Array is not a ', 'utf8'))
14+
buffers.write(bufferFrom('stringy cat'))
1415
buffers.end()
1516
})
1617

@@ -20,7 +21,7 @@ test('buffer mixed writes', function (t) {
2021
t.ok(Buffer.isBuffer(out))
2122
t.equal(out.toString('utf8'), 'pizza Array is not a stringy cat555')
2223
})
23-
buffers.write(new Buffer('pizza'))
24+
buffers.write(bufferFrom('pizza'))
2425
buffers.write(' Array is not a ')
2526
buffers.write([ 115, 116, 114, 105, 110, 103, 121 ])
2627
var u8 = new U8(4)

test/infer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var concat = require('../')
22
var test = require('tape')
3+
var bufferFrom = require('buffer-from')
34

45
test('type inference works as expected', function(t) {
56
var stream = concat()
67
t.equal(stream.inferEncoding(['hello']), 'array', 'array')
7-
t.equal(stream.inferEncoding(new Buffer('hello')), 'buffer', 'buffer')
8+
t.equal(stream.inferEncoding(bufferFrom('hello')), 'buffer', 'buffer')
89
t.equal(stream.inferEncoding(undefined), 'buffer', 'buffer')
910
t.equal(stream.inferEncoding(new Uint8Array(1)), 'uint8array', 'uint8array')
1011
t.equal(stream.inferEncoding('hello'), 'string', 'string')

test/string.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var concat = require('../')
22
var test = require('tape')
33
var TA = require('typedarray')
44
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
5+
var bufferFrom = require('buffer-from')
56

67
test('string -> buffer stream', function (t) {
78
t.plan(2)
@@ -42,7 +43,7 @@ test('string from mixed write encodings', function (t) {
4243
t.equal(out, 'nacho dogs')
4344
})
4445
strings.write('na')
45-
strings.write(new Buffer('cho'))
46+
strings.write(bufferFrom('cho'))
4647
strings.write([ 32, 100 ])
4748
var u8 = new U8(3)
4849
u8[0] = 111; u8[1] = 103; u8[2] = 115;
@@ -55,7 +56,7 @@ test('string from buffers with multibyte characters', function (t) {
5556
t.equal(typeof out, 'string')
5657
t.equal(out, '☃☃☃☃☃☃☃☃')
5758
})
58-
var snowman = new Buffer('☃')
59+
var snowman = bufferFrom('☃')
5960
for (var i = 0; i < 8; i++) {
6061
strings.write(snowman.slice(0, 1))
6162
strings.write(snowman.slice(1))

test/typedarray.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var concat = require('../')
22
var test = require('tape')
33
var TA = require('typedarray')
44
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
5+
var bufferFrom = require('buffer-from')
56

67
test('typed array stream', function (t) {
78
t.plan(2)
@@ -14,7 +15,7 @@ test('typed array stream', function (t) {
1415

1516
var arrays = concat({ encoding: 'Uint8Array' }, function(out) {
1617
t.equal(typeof out.subarray, 'function')
17-
t.deepEqual(new Buffer(out).toString('utf8'), 'abcde fg xyz')
18+
t.deepEqual(bufferFrom(out).toString('utf8'), 'abcde fg xyz')
1819
})
1920
arrays.write(a)
2021
arrays.write(b)
@@ -25,9 +26,9 @@ test('typed array from strings, buffers, and arrays', function (t) {
2526
t.plan(2)
2627
var arrays = concat({ encoding: 'Uint8Array' }, function(out) {
2728
t.equal(typeof out.subarray, 'function')
28-
t.deepEqual(new Buffer(out).toString('utf8'), 'abcde fg xyz')
29+
t.deepEqual(bufferFrom(out).toString('utf8'), 'abcde fg xyz')
2930
})
3031
arrays.write('abcde')
31-
arrays.write(new Buffer(' fg '))
32+
arrays.write(bufferFrom(' fg '))
3233
arrays.end([ 120, 121, 122 ])
3334
})

0 commit comments

Comments
 (0)