Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- '6.0'
- '8.0'
115 changes: 51 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,61 @@
var Writable = require('readable-stream').Writable
var inherits = require('inherits')
var bufferFrom = require('buffer-from')

if (typeof Uint8Array === 'undefined') {
var U8 = require('typedarray').Uint8Array
} else {
var U8 = Uint8Array
}

function ConcatStream(opts, cb) {
if (!(this instanceof ConcatStream)) return new ConcatStream(opts, cb)

if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!opts) opts = {}

var encoding = opts.encoding
var shouldInferEncoding = false
class ConcatStream extends Writable {
constructor (opts, cb) {
super({ objectMode: true })

if (!encoding) {
shouldInferEncoding = true
} else {
encoding = String(encoding).toLowerCase()
if (encoding === 'u8' || encoding === 'uint8') {
encoding = 'uint8array'
if (typeof opts === 'function') {
cb = opts
opts = {}
}
}
if (!opts) opts = {}

Writable.call(this, { objectMode: true })
var encoding = opts.encoding
var shouldInferEncoding = false

this.encoding = encoding
this.shouldInferEncoding = shouldInferEncoding
if (!encoding) {
shouldInferEncoding = true
} else {
encoding = String(encoding).toLowerCase()
if (encoding === 'u8' || encoding === 'uint8') {
encoding = 'uint8array'
}
}

if (cb) this.on('finish', function () { cb(this.getBody()) })
this.body = []
}
this.encoding = encoding
this.shouldInferEncoding = shouldInferEncoding

module.exports = ConcatStream
inherits(ConcatStream, Writable)
if (cb) this.on('finish', function () { cb(this.getBody()) })
this.body = []
}

ConcatStream.prototype._write = function(chunk, enc, next) {
this.body.push(chunk)
next()
}
_write (chunk, enc, next) {
this.body.push(chunk)
next()
}

ConcatStream.prototype.inferEncoding = function (buff) {
var firstBuffer = buff === undefined ? this.body[0] : buff;
if (Buffer.isBuffer(firstBuffer)) return 'buffer'
if (typeof Uint8Array !== 'undefined' && firstBuffer instanceof Uint8Array) return 'uint8array'
if (Array.isArray(firstBuffer)) return 'array'
if (typeof firstBuffer === 'string') return 'string'
if (Object.prototype.toString.call(firstBuffer) === "[object Object]") return 'object'
return 'buffer'
}
inferEncoding (buff) {
var firstBuffer = buff === undefined ? this.body[0] : buff;
if (Buffer.isBuffer(firstBuffer)) return 'buffer'
if (typeof Uint8Array !== 'undefined' && firstBuffer instanceof Uint8Array) return 'uint8array'
if (Array.isArray(firstBuffer)) return 'array'
if (typeof firstBuffer === 'string') return 'string'
if (Object.prototype.toString.call(firstBuffer) === "[object Object]") return 'object'
return 'buffer'
}

ConcatStream.prototype.getBody = function () {
if (!this.encoding && this.body.length === 0) return []
if (this.shouldInferEncoding) this.encoding = this.inferEncoding()
if (this.encoding === 'array') return arrayConcat(this.body)
if (this.encoding === 'string') return stringConcat(this.body)
if (this.encoding === 'buffer') return bufferConcat(this.body)
if (this.encoding === 'uint8array') return u8Concat(this.body)
return this.body
getBody () {
if (!this.encoding && this.body.length === 0) return []
if (this.shouldInferEncoding) this.encoding = this.inferEncoding()
if (this.encoding === 'array') return arrayConcat(this.body)
if (this.encoding === 'string') return stringConcat(this.body)
if (this.encoding === 'buffer') return bufferConcat(this.body)
if (this.encoding === 'uint8array') return u8Concat(this.body)
return this.body
}
}

var isArray = Array.isArray || function (arr) {
return Object.prototype.toString.call(arr) == '[object Array]'
}
module.exports = ConcatStream

function isArrayish (arr) {
return /Array\]$/.test(Object.prototype.toString.call(arr))
Expand All @@ -88,9 +75,9 @@ function stringConcat (parts) {
} else if (Buffer.isBuffer(p)) {
strings.push(p)
} else if (isBufferish(p)) {
strings.push(bufferFrom(p))
strings.push(Buffer.from(p))
} else {
strings.push(bufferFrom(String(p)))
strings.push(Buffer.from(String(p)))
}
}
if (Buffer.isBuffer(parts[0])) {
Expand All @@ -109,9 +96,9 @@ function bufferConcat (parts) {
if (Buffer.isBuffer(p)) {
bufs.push(p)
} else if (isBufferish(p)) {
bufs.push(bufferFrom(p))
bufs.push(Buffer.from(p))
} else {
bufs.push(bufferFrom(String(p)))
bufs.push(Buffer.from(String(p)))
}
}
return Buffer.concat(bufs)
Expand All @@ -129,11 +116,11 @@ function u8Concat (parts) {
var len = 0
for (var i = 0; i < parts.length; i++) {
if (typeof parts[i] === 'string') {
parts[i] = bufferFrom(parts[i])
parts[i] = Buffer.from(parts[i])
}
len += parts[i].length
}
var u8 = new U8(len)
var u8 = new Uint8Array(len)
for (var i = 0, offset = 0; i < parts.length; i++) {
var part = parts[i]
for (var j = 0; j < part.length; j++) {
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"url": "https://github.com/maxogden/concat-stream/issues"
},
"engines": [
"node >= 6.0"
"node >= 8.0"
],
"main": "index.js",
"files": [
Expand All @@ -28,10 +28,7 @@
},
"license": "MIT",
"dependencies": {
"buffer-from": "^1.0.0",
"inherits": "^2.0.3",
"readable-stream": "^3.0.2",
"typedarray": "^0.0.6"
"readable-stream": "^3.4.0"
},
"devDependencies": {
"tape": "^4.6.3"
Expand Down
4 changes: 2 additions & 2 deletions test/array.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var concat = require('../')
var Concat = require('../')
var test = require('tape')

test('array stream', function (t) {
t.plan(1)
var arrays = concat({ encoding: 'array' }, function(out) {
var arrays = new Concat({ encoding: 'array' }, function(out) {
t.deepEqual(out, [1,2,3,4,5,6])
})
arrays.write([1,2,3])
Expand Down
17 changes: 7 additions & 10 deletions test/buffer.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
var concat = require('../')
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) {
var buffers = new Concat(function(out) {
t.ok(Buffer.isBuffer(out))
t.equal(out.toString('utf8'), 'pizza Array is not a stringy cat')
})
buffers.write(bufferFrom('pizza Array is not a ', 'utf8'))
buffers.write(bufferFrom('stringy cat'))
buffers.write(Buffer.from('pizza Array is not a ', 'utf8'))
buffers.write(Buffer.from('stringy cat'))
buffers.end()
})

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

test('type inference works as expected', function(t) {
var stream = concat()
var stream = new Concat()
t.equal(stream.inferEncoding(['hello']), 'array', 'array')
t.equal(stream.inferEncoding(bufferFrom('hello')), 'buffer', 'buffer')
t.equal(stream.inferEncoding(Buffer.from('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
8 changes: 4 additions & 4 deletions test/nothing.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var concat = require('../')
var Concat = require('../')
var test = require('tape')

test('no callback stream', function (t) {
var stream = concat()
var stream = new Concat()
stream.write('space')
stream.end(' cats')
t.end()
})

test('no encoding set, no data', function (t) {
var stream = concat(function(data) {
var stream = new Concat(function(data) {
t.deepEqual(data, [])
t.end()
})
stream.end()
})

test('encoding set to string, no data', function (t) {
var stream = concat({ encoding: 'string' }, function(data) {
var stream = new Concat({ encoding: 'string' }, function(data) {
t.deepEqual(data, '')
t.end()
})
Expand Down
6 changes: 3 additions & 3 deletions test/objects.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var concat = require('../')
var Concat = require('../')
var test = require('tape')

test('writing objects', function (t) {
var stream = concat({encoding: "objects"}, concatted)
var stream = new Concat({encoding: "objects"}, concatted)
function concatted(objs) {
t.equal(objs.length, 2)
t.deepEqual(objs[0], {"foo": "bar"})
Expand All @@ -16,7 +16,7 @@ test('writing objects', function (t) {


test('switch to objects encoding if no encoding specified and objects are written', function (t) {
var stream = concat(concatted)
var stream = new Concat(concatted)
function concatted(objs) {
t.equal(objs.length, 2)
t.deepEqual(objs[0], {"foo": "bar"})
Expand Down
4 changes: 2 additions & 2 deletions test/server/ls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var concat = require('../../')
var Concat = require('../../')
var spawn = require('child_process').spawn
var exec = require('child_process').exec
var test = require('tape')
Expand All @@ -7,7 +7,7 @@ test('ls command', function (t) {
t.plan(1)
var cmd = spawn('ls', [ __dirname ])
cmd.stdout.pipe(
concat(function(out) {
new Concat(function(out) {
exec('ls ' + __dirname, function (err, body) {
t.equal(out.toString('utf8'), body.toString('utf8'))
})
Expand Down
Loading