pax_global_header00006660000000000000000000000064126110143120014501gustar00rootroot0000000000000052 comment=522adc12d82f57c691a5f946fbc8ba08718dcdcb concat-stream-1.5.1/000077500000000000000000000000001261101431200142455ustar00rootroot00000000000000concat-stream-1.5.1/.gitignore000066400000000000000000000000141261101431200162300ustar00rootroot00000000000000node_modulesconcat-stream-1.5.1/.travis.yml000066400000000000000000000000461261101431200163560ustar00rootroot00000000000000language: node_js node_js: - '0.12' concat-stream-1.5.1/LICENSE000066400000000000000000000020741261101431200152550ustar00rootroot00000000000000The 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.5.1/index.js000066400000000000000000000067741261101431200157300ustar00rootroot00000000000000var 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 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 { strings.push(Buffer(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 (typeof p === 'string' || isArrayish(p) || (p && typeof p.subarray === 'function')) { bufs.push(Buffer(p)) } else bufs.push(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] = 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.5.1/package.json000066400000000000000000000022061261101431200165330ustar00rootroot00000000000000{ "name": "concat-stream", "version": "1.5.1", "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.1", "typedarray": "~0.0.5", "readable-stream": "~2.0.0" }, "devDependencies": { "tape": "~2.3.2" }, "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.5.1/readme.md000066400000000000000000000065411261101431200160320ustar00rootroot00000000000000# 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. [![Build Status](https://travis-ci.org/maxogden/concat-stream.svg?branch=master)](https://travis-ci.org/maxogden/concat-stream) [![NPM](https://nodei.co/npm/concat-stream.png)](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 `stream-each` 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`. # 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.5.1/test/000077500000000000000000000000001261101431200152245ustar00rootroot00000000000000concat-stream-1.5.1/test/array.js000066400000000000000000000004201261101431200166740ustar00rootroot00000000000000var 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.5.1/test/buffer.js000066400000000000000000000016531261101431200170400ustar00rootroot00000000000000var 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.5.1/test/infer.js000066400000000000000000000012321261101431200166630ustar00rootroot00000000000000var 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.5.1/test/nothing.js000066400000000000000000000007761261101431200172420ustar00rootroot00000000000000var 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.5.1/test/objects.js000066400000000000000000000013631261101431200172160ustar00rootroot00000000000000var 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.5.1/test/server/000077500000000000000000000000001261101431200165325ustar00rootroot00000000000000concat-stream-1.5.1/test/server/ls.js000066400000000000000000000006331261101431200175100ustar00rootroot00000000000000var 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.5.1/test/string.js000066400000000000000000000036501261101431200170740ustar00rootroot00000000000000var 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(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() }) concat-stream-1.5.1/test/typedarray.js000066400000000000000000000017521261101431200177530ustar00rootroot00000000000000var 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(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(Buffer(out).toString('utf8'), 'abcde fg xyz') }) arrays.write('abcde') arrays.write(Buffer(' fg ')) arrays.end([ 120, 121, 122 ]) })