pax_global_header 0000666 0000000 0000000 00000000064 13026000106 0014476 g ustar 00root root 0000000 0000000 52 comment=e482281642c1e011fc158f5749ef40a71c77a426
concat-stream-1.6.0/ 0000775 0000000 0000000 00000000000 13026000106 0014242 5 ustar 00root root 0000000 0000000 concat-stream-1.6.0/.gitignore 0000664 0000000 0000000 00000000014 13026000106 0016225 0 ustar 00root root 0000000 0000000 node_modules concat-stream-1.6.0/.travis.yml 0000664 0000000 0000000 00000000046 13026000106 0016353 0 ustar 00root root 0000000 0000000 language: node_js
node_js:
- '0.12'
concat-stream-1.6.0/LICENSE 0000664 0000000 0000000 00000002074 13026000106 0015252 0 ustar 00root root 0000000 0000000 The MIT License
Copyright (c) 2013 Max Ogden
Permission is hereby granted, free of charge,
to any person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. concat-stream-1.6.0/collaborators.md 0000664 0000000 0000000 00000000542 13026000106 0017433 0 ustar 00root root 0000000 0000000 ## Collaborators
concat-stream is only possible due to the excellent work of the following collaborators:
concat-stream-1.6.0/index.js 0000664 0000000 0000000 00000007227 13026000106 0015717 0 ustar 00root root 0000000 0000000 var Writable = require('readable-stream').Writable
var inherits = require('inherits')
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
if (!encoding) {
shouldInferEncoding = true
} else {
encoding = String(encoding).toLowerCase()
if (encoding === 'u8' || encoding === 'uint8') {
encoding = 'uint8array'
}
}
Writable.call(this, { objectMode: true })
this.encoding = encoding
this.shouldInferEncoding = shouldInferEncoding
if (cb) this.on('finish', function () { cb(this.getBody()) })
this.body = []
}
module.exports = ConcatStream
inherits(ConcatStream, Writable)
ConcatStream.prototype._write = function(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'
}
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
}
var isArray = Array.isArray || function (arr) {
return Object.prototype.toString.call(arr) == '[object Array]'
}
function isArrayish (arr) {
return /Array\]$/.test(Object.prototype.toString.call(arr))
}
function isBufferish (p) {
return typeof p === 'string' || isArrayish(p) || (p && typeof p.subarray === 'function')
}
function stringConcat (parts) {
var strings = []
var needsToString = false
for (var i = 0; i < parts.length; i++) {
var p = parts[i]
if (typeof p === 'string') {
strings.push(p)
} else if (Buffer.isBuffer(p)) {
strings.push(p)
} else if (isBufferish(p)) {
strings.push(new Buffer(p))
} else {
strings.push(new Buffer(String(p)))
}
}
if (Buffer.isBuffer(parts[0])) {
strings = Buffer.concat(strings)
strings = strings.toString('utf8')
} else {
strings = strings.join('')
}
return strings
}
function bufferConcat (parts) {
var bufs = []
for (var i = 0; i < parts.length; i++) {
var p = parts[i]
if (Buffer.isBuffer(p)) {
bufs.push(p)
} else if (isBufferish(p)) {
bufs.push(new Buffer(p))
} else {
bufs.push(new Buffer(String(p)))
}
}
return Buffer.concat(bufs)
}
function arrayConcat (parts) {
var res = []
for (var i = 0; i < parts.length; i++) {
res.push.apply(res, parts[i])
}
return res
}
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])
}
len += parts[i].length
}
var u8 = new U8(len)
for (var i = 0, offset = 0; i < parts.length; i++) {
var part = parts[i]
for (var j = 0; j < part.length; j++) {
u8[offset++] = part[j]
}
}
return u8
}
concat-stream-1.6.0/package.json 0000664 0000000 0000000 00000002206 13026000106 0016530 0 ustar 00root root 0000000 0000000 {
"name": "concat-stream",
"version": "1.6.0",
"description": "writable stream that concatenates strings or binary data and calls a callback with the result",
"tags": [
"stream",
"simple",
"util",
"utility"
],
"author": "Max Ogden ",
"repository": {
"type": "git",
"url": "http://github.com/maxogden/concat-stream.git"
},
"bugs": {
"url": "http://github.com/maxogden/concat-stream/issues"
},
"engines": [
"node >= 0.8"
],
"main": "index.js",
"files": [
"index.js"
],
"scripts": {
"test": "tape test/*.js test/server/*.js"
},
"license": "MIT",
"dependencies": {
"inherits": "^2.0.3",
"typedarray": "^0.0.6",
"readable-stream": "^2.2.2"
},
"devDependencies": {
"tape": "^4.6.3"
},
"testling": {
"files": "test/*.js",
"browsers": [
"ie/8..latest",
"firefox/17..latest",
"firefox/nightly",
"chrome/22..latest",
"chrome/canary",
"opera/12..latest",
"opera/next",
"safari/5.1..latest",
"ipad/6.0..latest",
"iphone/6.0..latest",
"android-browser/4.2..latest"
]
}
}
concat-stream-1.6.0/readme.md 0000664 0000000 0000000 00000006661 13026000106 0016032 0 ustar 00root root 0000000 0000000 # concat-stream
Writable stream that concatenates all the data from a stream and calls a callback with the result. Use this when you want to collect all the data from a stream into a single buffer.
[](https://travis-ci.org/maxogden/concat-stream)
[](https://nodei.co/npm/concat-stream/)
### description
Streams emit many buffers. If you want to collect all of the buffers, and when the stream ends concatenate all of the buffers together and receive a single buffer then this is the module for you.
Only use this if you know you can fit all of the output of your stream into a single Buffer (e.g. in RAM).
There are also `objectMode` streams that emit things other than Buffers, and you can concatenate these too. See below for details.
## Related
`concat-stream` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one.
### examples
#### Buffers
```js
var fs = require('fs')
var concat = require('concat-stream')
var readStream = fs.createReadStream('cat.png')
var concatStream = concat(gotPicture)
readStream.on('error', handleError)
readStream.pipe(concatStream)
function gotPicture(imageBuffer) {
// imageBuffer is all of `cat.png` as a node.js Buffer
}
function handleError(err) {
// handle your error appropriately here, e.g.:
console.error(err) // print the error to STDERR
process.exit(1) // exit program with non-zero exit code
}
```
#### Arrays
```js
var write = concat(function(data) {})
write.write([1,2,3])
write.write([4,5,6])
write.end()
// data will be [1,2,3,4,5,6] in the above callback
```
#### Uint8Arrays
```js
var write = concat(function(data) {})
var a = new Uint8Array(3)
a[0] = 97; a[1] = 98; a[2] = 99
write.write(a)
write.write('!')
write.end(Buffer('!!1'))
```
See `test/` for more examples
# methods
```js
var concat = require('concat-stream')
```
## var writable = concat(opts={}, cb)
Return a `writable` stream that will fire `cb(data)` with all of the data that
was written to the stream. Data can be written to `writable` as strings,
Buffers, arrays of byte integers, and Uint8Arrays.
By default `concat-stream` will give you back the same data type as the type of the first buffer written to the stream. Use `opts.encoding` to set what format `data` should be returned as, e.g. if you if you don't want to rely on the built-in type checking or for some other reason.
* `string` - get a string
* `buffer` - get back a Buffer
* `array` - get an array of byte integers
* `uint8array`, `u8`, `uint8` - get back a Uint8Array
* `object`, get back an array of Objects
If you don't specify an encoding, and the types can't be inferred (e.g. you write things that aren't in the list above), it will try to convert concat them into a `Buffer`.
If nothing is written to `writable` then `data` will be an empty array `[]`.
# error handling
`concat-stream` does not handle errors for you, so you must handle errors on whatever streams you pipe into `concat-stream`. This is a general rule when programming with node.js streams: always handle errors on each and every stream. Since `concat-stream` is not itself a stream it does not emit errors.
We recommend using [`end-of-stream`](https://npmjs.org/end-of-stream) or [`pump`](https://npmjs.org/pump) for writing error tolerant stream code.
# license
MIT LICENSE
concat-stream-1.6.0/test/ 0000775 0000000 0000000 00000000000 13026000106 0015221 5 ustar 00root root 0000000 0000000 concat-stream-1.6.0/test/array.js 0000664 0000000 0000000 00000000420 13026000106 0016671 0 ustar 00root root 0000000 0000000 var concat = require('../')
var test = require('tape')
test('array stream', function (t) {
t.plan(1)
var arrays = concat({ encoding: 'array' }, function(out) {
t.deepEqual(out, [1,2,3,4,5,6])
})
arrays.write([1,2,3])
arrays.write([4,5,6])
arrays.end()
})
concat-stream-1.6.0/test/buffer.js 0000664 0000000 0000000 00000001653 13026000106 0017035 0 ustar 00root root 0000000 0000000 var concat = require('../')
var test = require('tape')
var TA = require('typedarray')
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
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.end()
})
test('buffer mixed writes', 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 cat555')
})
buffers.write(new Buffer('pizza'))
buffers.write(' Array is not a ')
buffers.write([ 115, 116, 114, 105, 110, 103, 121 ])
var u8 = new U8(4)
u8[0] = 32; u8[1] = 99; u8[2] = 97; u8[3] = 116
buffers.write(u8)
buffers.write(555)
buffers.end()
})
concat-stream-1.6.0/test/infer.js 0000664 0000000 0000000 00000001232 13026000106 0016660 0 ustar 00root root 0000000 0000000 var concat = require('../')
var test = require('tape')
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(undefined), 'buffer', 'buffer')
t.equal(stream.inferEncoding(new Uint8Array(1)), 'uint8array', 'uint8array')
t.equal(stream.inferEncoding('hello'), 'string', 'string')
t.equal(stream.inferEncoding(''), 'string', 'string')
t.equal(stream.inferEncoding({hello: "world"}), 'object', 'object')
t.equal(stream.inferEncoding(1), 'buffer', 'buffer')
t.end()
})
concat-stream-1.6.0/test/nothing.js 0000664 0000000 0000000 00000000776 13026000106 0017237 0 ustar 00root root 0000000 0000000 var concat = require('../')
var test = require('tape')
test('no callback stream', function (t) {
var stream = concat()
stream.write('space')
stream.end(' cats')
t.end()
})
test('no encoding set, no data', function (t) {
var stream = 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) {
t.deepEqual(data, '')
t.end()
})
stream.end()
})
concat-stream-1.6.0/test/objects.js 0000664 0000000 0000000 00000001363 13026000106 0017213 0 ustar 00root root 0000000 0000000 var concat = require('../')
var test = require('tape')
test('writing objects', function (t) {
var stream = concat({encoding: "objects"}, concatted)
function concatted(objs) {
t.equal(objs.length, 2)
t.deepEqual(objs[0], {"foo": "bar"})
t.deepEqual(objs[1], {"baz": "taco"})
}
stream.write({"foo": "bar"})
stream.write({"baz": "taco"})
stream.end()
t.end()
})
test('switch to objects encoding if no encoding specified and objects are written', function (t) {
var stream = concat(concatted)
function concatted(objs) {
t.equal(objs.length, 2)
t.deepEqual(objs[0], {"foo": "bar"})
t.deepEqual(objs[1], {"baz": "taco"})
}
stream.write({"foo": "bar"})
stream.write({"baz": "taco"})
stream.end()
t.end()
})
concat-stream-1.6.0/test/server/ 0000775 0000000 0000000 00000000000 13026000106 0016527 5 ustar 00root root 0000000 0000000 concat-stream-1.6.0/test/server/ls.js 0000664 0000000 0000000 00000000633 13026000106 0017505 0 ustar 00root root 0000000 0000000 var concat = require('../../')
var spawn = require('child_process').spawn
var exec = require('child_process').exec
var test = require('tape')
test('ls command', function (t) {
t.plan(1)
var cmd = spawn('ls', [ __dirname ])
cmd.stdout.pipe(
concat(function(out) {
exec('ls ' + __dirname, function (err, body) {
t.equal(out.toString('utf8'), body.toString('utf8'))
})
})
)
})
concat-stream-1.6.0/test/string.js 0000664 0000000 0000000 00000004134 13026000106 0017067 0 ustar 00root root 0000000 0000000 var concat = require('../')
var test = require('tape')
var TA = require('typedarray')
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
test('string -> buffer stream', function (t) {
t.plan(2)
var strings = concat({ encoding: 'buffer'}, function(out) {
t.ok(Buffer.isBuffer(out))
t.equal(out.toString('utf8'), 'nacho dogs')
})
strings.write("nacho ")
strings.write("dogs")
strings.end()
})
test('string stream', function (t) {
t.plan(2)
var strings = concat({ encoding: 'string' }, function(out) {
t.equal(typeof out, 'string')
t.equal(out, 'nacho dogs')
})
strings.write("nacho ")
strings.write("dogs")
strings.end()
})
test('end chunk', function (t) {
t.plan(1)
var endchunk = concat({ encoding: 'string' }, function(out) {
t.equal(out, 'this is the end')
})
endchunk.write("this ")
endchunk.write("is the ")
endchunk.end("end")
})
test('string from mixed write encodings', function (t) {
t.plan(2)
var strings = concat({ encoding: 'string' }, function(out) {
t.equal(typeof out, 'string')
t.equal(out, 'nacho dogs')
})
strings.write('na')
strings.write(new Buffer('cho'))
strings.write([ 32, 100 ])
var u8 = new U8(3)
u8[0] = 111; u8[1] = 103; u8[2] = 115;
strings.end(u8)
})
test('string from buffers with multibyte characters', function (t) {
t.plan(2)
var strings = concat({ encoding: 'string' }, function(out) {
t.equal(typeof out, 'string')
t.equal(out, '☃☃☃☃☃☃☃☃')
})
var snowman = new Buffer('☃')
for (var i = 0; i < 8; i++) {
strings.write(snowman.slice(0, 1))
strings.write(snowman.slice(1))
}
strings.end()
})
test('string infer encoding with empty string chunk', function (t) {
t.plan(2)
var strings = concat(function(out) {
t.equal(typeof out, 'string')
t.equal(out, 'nacho dogs')
})
strings.write("")
strings.write("nacho ")
strings.write("dogs")
strings.end()
})
test('to string numbers', function (t) {
var write = concat(function (str) {
t.equal(str, 'a1000')
t.end()
})
write.write('a')
write.write(1000)
write.end()
})
concat-stream-1.6.0/test/typedarray.js 0000664 0000000 0000000 00000001766 13026000106 0017755 0 ustar 00root root 0000000 0000000 var concat = require('../')
var test = require('tape')
var TA = require('typedarray')
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
test('typed array stream', function (t) {
t.plan(2)
var a = new U8(5)
a[0] = 97; a[1] = 98; a[2] = 99; a[3] = 100; a[4] = 101;
var b = new U8(3)
b[0] = 32; b[1] = 102; b[2] = 103;
var c = new U8(4)
c[0] = 32; c[1] = 120; c[2] = 121; c[3] = 122;
var arrays = concat({ encoding: 'Uint8Array' }, function(out) {
t.equal(typeof out.subarray, 'function')
t.deepEqual(new Buffer(out).toString('utf8'), 'abcde fg xyz')
})
arrays.write(a)
arrays.write(b)
arrays.end(c)
})
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')
})
arrays.write('abcde')
arrays.write(new Buffer(' fg '))
arrays.end([ 120, 121, 122 ])
})