Skip to content

Commit

Permalink
Replace Buffer constructor with buffer-from (#59)
Browse files Browse the repository at this point in the history
Adds a dependency on buffer-from
  • Loading branch information
jasnell authored and mafintosh committed Mar 21, 2018
1 parent bb2b8d0 commit b198e8d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Writable = require('readable-stream').Writable
var inherits = require('inherits')
var bufferFrom = require('buffer-from')

if (typeof Uint8Array === 'undefined') {
var U8 = require('typedarray').Uint8Array
Expand Down Expand Up @@ -87,9 +88,9 @@ function stringConcat (parts) {
} else if (Buffer.isBuffer(p)) {
strings.push(p)
} else if (isBufferish(p)) {
strings.push(new Buffer(p))
strings.push(bufferFrom(p))
} else {
strings.push(new Buffer(String(p)))
strings.push(bufferFrom(String(p)))
}
}
if (Buffer.isBuffer(parts[0])) {
Expand All @@ -108,9 +109,9 @@ function bufferConcat (parts) {
if (Buffer.isBuffer(p)) {
bufs.push(p)
} else if (isBufferish(p)) {
bufs.push(new Buffer(p))
bufs.push(bufferFrom(p))
} else {
bufs.push(new Buffer(String(p)))
bufs.push(bufferFrom(String(p)))
}
}
return Buffer.concat(bufs)
Expand All @@ -128,7 +129,7 @@ function u8Concat (parts) {
var len = 0
for (var i = 0; i < parts.length; i++) {
if (typeof parts[i] === 'string') {
parts[i] = new Buffer(parts[i])
parts[i] = bufferFrom(parts[i])
}
len += parts[i].length
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
},
"license": "MIT",
"dependencies": {
"buffer-from": "^1.0.0",
"inherits": "^2.0.3",
"typedarray": "^0.0.6",
"readable-stream": "^2.2.2"
"readable-stream": "^2.2.2",
"typedarray": "^0.0.6"
},
"devDependencies": {
"tape": "^4.6.3"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var a = new Uint8Array(3)
a[0] = 97; a[1] = 98; a[2] = 99
write.write(a)
write.write('!')
write.end(Buffer('!!1'))
write.end(Buffer.from('!!1'))
```

See `test/` for more examples
Expand Down
7 changes: 4 additions & 3 deletions test/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ var concat = require('../')
var test = require('tape')
var TA = require('typedarray')
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
var bufferFrom = require('buffer-from')

test('buffer stream', function (t) {
t.plan(2)
var buffers = concat(function(out) {
t.ok(Buffer.isBuffer(out))
t.equal(out.toString('utf8'), 'pizza Array is not a stringy cat')
})
buffers.write(new Buffer('pizza Array is not a ', 'utf8'))
buffers.write(new Buffer('stringy cat'))
buffers.write(bufferFrom('pizza Array is not a ', 'utf8'))
buffers.write(bufferFrom('stringy cat'))
buffers.end()
})

Expand All @@ -20,7 +21,7 @@ test('buffer mixed writes', function (t) {
t.ok(Buffer.isBuffer(out))
t.equal(out.toString('utf8'), 'pizza Array is not a stringy cat555')
})
buffers.write(new Buffer('pizza'))
buffers.write(bufferFrom('pizza'))
buffers.write(' Array is not a ')
buffers.write([ 115, 116, 114, 105, 110, 103, 121 ])
var u8 = new U8(4)
Expand Down
3 changes: 2 additions & 1 deletion test/infer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var concat = require('../')
var test = require('tape')
var bufferFrom = require('buffer-from')

test('type inference works as expected', function(t) {
var stream = concat()
t.equal(stream.inferEncoding(['hello']), 'array', 'array')
t.equal(stream.inferEncoding(new Buffer('hello')), 'buffer', 'buffer')
t.equal(stream.inferEncoding(bufferFrom('hello')), 'buffer', 'buffer')
t.equal(stream.inferEncoding(undefined), 'buffer', 'buffer')
t.equal(stream.inferEncoding(new Uint8Array(1)), 'uint8array', 'uint8array')
t.equal(stream.inferEncoding('hello'), 'string', 'string')
Expand Down
5 changes: 3 additions & 2 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var concat = require('../')
var test = require('tape')
var TA = require('typedarray')
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
var bufferFrom = require('buffer-from')

test('string -> buffer stream', function (t) {
t.plan(2)
Expand Down Expand Up @@ -42,7 +43,7 @@ test('string from mixed write encodings', function (t) {
t.equal(out, 'nacho dogs')
})
strings.write('na')
strings.write(new Buffer('cho'))
strings.write(bufferFrom('cho'))
strings.write([ 32, 100 ])
var u8 = new U8(3)
u8[0] = 111; u8[1] = 103; u8[2] = 115;
Expand All @@ -55,7 +56,7 @@ test('string from buffers with multibyte characters', function (t) {
t.equal(typeof out, 'string')
t.equal(out, '☃☃☃☃☃☃☃☃')
})
var snowman = new Buffer('☃')
var snowman = bufferFrom('☃')
for (var i = 0; i < 8; i++) {
strings.write(snowman.slice(0, 1))
strings.write(snowman.slice(1))
Expand Down
7 changes: 4 additions & 3 deletions test/typedarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var concat = require('../')
var test = require('tape')
var TA = require('typedarray')
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
var bufferFrom = require('buffer-from')

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

var arrays = concat({ encoding: 'Uint8Array' }, function(out) {
t.equal(typeof out.subarray, 'function')
t.deepEqual(new Buffer(out).toString('utf8'), 'abcde fg xyz')
t.deepEqual(bufferFrom(out).toString('utf8'), 'abcde fg xyz')
})
arrays.write(a)
arrays.write(b)
Expand All @@ -25,9 +26,9 @@ test('typed array from strings, buffers, and arrays', function (t) {
t.plan(2)
var arrays = concat({ encoding: 'Uint8Array' }, function(out) {
t.equal(typeof out.subarray, 'function')
t.deepEqual(new Buffer(out).toString('utf8'), 'abcde fg xyz')
t.deepEqual(bufferFrom(out).toString('utf8'), 'abcde fg xyz')
})
arrays.write('abcde')
arrays.write(new Buffer(' fg '))
arrays.write(bufferFrom(' fg '))
arrays.end([ 120, 121, 122 ])
})

0 comments on commit b198e8d

Please sign in to comment.