[Pkg-javascript-commits] [node-concat-stream] 01/07: Imported Upstream version 1.5.1

Ross Gammon ross-guest at moszumanska.debian.org
Sun Nov 8 16:47:55 UTC 2015


This is an automated email from the git hooks/post-receive script.

ross-guest pushed a commit to branch master
in repository node-concat-stream.

commit c8161621254d95018fd4988bf0c87e6d7188d807
Author: Ross Gammon <rossgammon at mail.dk>
Date:   Fri Oct 23 17:49:58 2015 +0200

    Imported Upstream version 1.5.1
---
 .gitignore         |   1 +
 .travis.yml        |   3 ++
 LICENSE            |  24 ++++++++++
 index.js           | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json       |  54 +++++++++++++++++++++
 readme.md          | 100 +++++++++++++++++++++++++++++++++++++++
 test/array.js      |  12 +++++
 test/buffer.js     |  31 ++++++++++++
 test/infer.js      |  15 ++++++
 test/nothing.js    |  25 ++++++++++
 test/objects.js    |  29 ++++++++++++
 test/server/ls.js  |  16 +++++++
 test/string.js     |  76 ++++++++++++++++++++++++++++++
 test/typedarray.js |  33 +++++++++++++
 14 files changed, 555 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b512c09
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..dd5ab9b
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+  - '0.12'
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..99c130e
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,24 @@
+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.
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..b55ae7e
--- /dev/null
+++ b/index.js
@@ -0,0 +1,136 @@
+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 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
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..5c3de72
--- /dev/null
+++ b/package.json
@@ -0,0 +1,54 @@
+{
+  "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 <max at maxogden.com>",
+  "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"
+    ]
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..1a16af9
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,100 @@
+# 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
diff --git a/test/array.js b/test/array.js
new file mode 100644
index 0000000..86e7dd4
--- /dev/null
+++ b/test/array.js
@@ -0,0 +1,12 @@
+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()
+})
diff --git a/test/buffer.js b/test/buffer.js
new file mode 100644
index 0000000..d28f5f9
--- /dev/null
+++ b/test/buffer.js
@@ -0,0 +1,31 @@
+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()
+})
diff --git a/test/infer.js b/test/infer.js
new file mode 100644
index 0000000..91ab933
--- /dev/null
+++ b/test/infer.js
@@ -0,0 +1,15 @@
+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()
+})
diff --git a/test/nothing.js b/test/nothing.js
new file mode 100644
index 0000000..6ac6049
--- /dev/null
+++ b/test/nothing.js
@@ -0,0 +1,25 @@
+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()
+})
diff --git a/test/objects.js b/test/objects.js
new file mode 100644
index 0000000..ad921ed
--- /dev/null
+++ b/test/objects.js
@@ -0,0 +1,29 @@
+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()
+})
diff --git a/test/server/ls.js b/test/server/ls.js
new file mode 100644
index 0000000..3258d8d
--- /dev/null
+++ b/test/server/ls.js
@@ -0,0 +1,16 @@
+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'))
+      })
+    })
+  )
+})
diff --git a/test/string.js b/test/string.js
new file mode 100644
index 0000000..218c522
--- /dev/null
+++ b/test/string.js
@@ -0,0 +1,76 @@
+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(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()
+})
diff --git a/test/typedarray.js b/test/typedarray.js
new file mode 100644
index 0000000..ee07110
--- /dev/null
+++ b/test/typedarray.js
@@ -0,0 +1,33 @@
+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(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 ])
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-concat-stream.git



More information about the Pkg-javascript-commits mailing list