concat-stream
Advanced tools
Comparing version 1.3.1 to 1.4.0
27
index.js
@@ -15,9 +15,18 @@ var Writable = require('stream').Writable | ||
var encoding = String(opts.encoding || 'buffer').toLowerCase() | ||
if (encoding === 'u8' || encoding === 'uint8') { | ||
encoding = 'uint8array' | ||
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 | ||
@@ -36,3 +45,15 @@ if (cb) this.on('finish', function () { cb(this.getBody()) }) | ||
ConcatStream.prototype.inferEncoding = function () { | ||
var firstBuffer = this.body[0] | ||
if (!firstBuffer) return 'buffer' | ||
if (Buffer.isBuffer(firstBuffer)) return 'buffer' | ||
if (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.shouldInferEncoding) this.encoding = this.inferEncoding() | ||
if (this.encoding === 'array') return arrayConcat(this.body) | ||
@@ -39,0 +60,0 @@ if (this.encoding === 'string') return stringConcat(this.body) |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"author": "Max Ogden <max@maxogden.com>", | ||
@@ -21,3 +21,3 @@ "repository": { | ||
"engines": [ | ||
"node >= 0.8.0" | ||
"node >= 0.10.0" | ||
], | ||
@@ -24,0 +24,0 @@ "main": "index.js", |
@@ -11,2 +11,4 @@ # concat-stream | ||
#### Buffers | ||
```js | ||
@@ -17,3 +19,5 @@ var concat = require('concat-stream') | ||
var read = fs.createReadStream('readme.md') | ||
var write = concat(function(data) {}) | ||
var write = concat(function(data) { | ||
// data is all of readme.md as a Buffer | ||
}) | ||
@@ -23,6 +27,6 @@ read.pipe(write) | ||
works with arrays too! | ||
#### Arrays | ||
```js | ||
var write = concat({ encoding: 'array' }, function(data) {}) | ||
var write = concat(function(data) {}) | ||
write.write([1,2,3]) | ||
@@ -34,16 +38,6 @@ write.write([4,5,6]) | ||
works with buffers too! can't believe the deals! | ||
#### Uint8Arrays | ||
```js | ||
var write = concat(function(data) {}) | ||
write.write(new Buffer('hello ')) | ||
write.write(new Buffer('world')) | ||
write.end() | ||
// data will be a buffer that toString()s to 'hello world' in the above callback | ||
``` | ||
or if you want a Uint8Array, you can have those too! | ||
```js | ||
var write = concat({ encoding: 'u8' }, function(data) {}) | ||
var a = new Uint8Array(3) | ||
@@ -56,2 +50,4 @@ a[0] = 97; a[1] = 98; a[2] = 99 | ||
See `test/` for more examples | ||
# methods | ||
@@ -69,11 +65,14 @@ | ||
Use `opts.encoding` to control what format `data` should be: | ||
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 (this is the default encoding) | ||
* array - get an array of byte integers | ||
* uint8array, u8, uint8 - get back a Uint8Array | ||
* `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 int he list above), it will try to convert concat them into a `Buffer`. | ||
# license | ||
MIT LICENSE |
@@ -16,1 +16,15 @@ var concat = require('../') | ||
}) | ||
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() | ||
}) |
@@ -8,3 +8,3 @@ var concat = require('../') | ||
t.plan(2) | ||
var strings = concat(function(out) { | ||
var strings = concat({ encoding: 'buffer'}, function(out) { | ||
t.ok(Buffer.isBuffer(out)) | ||
@@ -11,0 +11,0 @@ t.equal(out.toString('utf8'), 'nacho dogs') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12363
273
73