Socket
Socket
Sign inDemoInstall

concat-stream

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

concat-stream - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

test/array.js

118

index.js

@@ -1,52 +0,104 @@

var stream = require('stream')
var bops = require('bops')
var util = require('util')
var Writable = require('stream').Writable
var inherits = require('inherits')
var TA = require('typedarray')
var U8 = typeof Uint8Array !== 'undefined' ? Uint8Array : TA.Uint8Array
function ConcatStream(cb) {
stream.Stream.call(this)
this.writable = true
if (cb) this.cb = cb
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 = String(opts.encoding || 'buffer').toLowerCase()
if (encoding === 'u8' || encoding === 'uint8') {
encoding = 'uint8array'
}
Writable.call(this, { objectMode: true })
this.encoding = encoding
if (cb) this.on('finish', function () { cb(this.getBody()) })
this.body = []
this.on('error', function(err) {
// no-op
})
}
util.inherits(ConcatStream, stream.Stream)
module.exports = ConcatStream
inherits(ConcatStream, Writable)
ConcatStream.prototype.write = function(chunk) {
this.emit('data', chunk)
ConcatStream.prototype._write = function(chunk, enc, next) {
this.body.push(chunk)
next()
}
ConcatStream.prototype.destroy = function() {}
ConcatStream.prototype.arrayConcat = function(arrs) {
if (arrs.length === 0) return []
if (arrs.length === 1) return arrs[0]
return arrs.reduce(function (a, b) { return a.concat(b) })
ConcatStream.prototype.getBody = function () {
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
}
ConcatStream.prototype.isArray = function(arr) {
var isArray = Array.isArray || function (arr) {
return Object.prototype.toString.call(arr) == '[object Array]'
}
ConcatStream.prototype.getBody = function () {
if (this.body.length === 0) return bops.from(0)
if (typeof(this.body[0]) === "string") return this.body.join('')
if (this.isArray(this.body[0])) return this.arrayConcat(this.body)
if (bops.is(this.body[0])) return bops.join(this.body)
return this.body
function isArrayish (arr) {
return /Array\]$/.test(Object.prototype.toString.call(arr))
}
ConcatStream.prototype.end = function(chunk) {
if (chunk) this.write(chunk)
this.emit('end')
if (this.cb) this.cb(this.getBody())
function stringConcat (parts) {
var strings = []
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.toString('utf8'))
} else {
strings.push(Buffer(p).toString('utf8'))
}
}
return strings.join('')
}
module.exports = function(cb) {
return new ConcatStream(cb)
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)
}
module.exports.ConcatStream = ConcatStream
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
}

@@ -10,3 +10,3 @@ {

],
"version": "1.2.1",
"version": "1.3.0",
"author": "Max Ogden <max@maxogden.com>",

@@ -25,8 +25,28 @@ "repository": {

"scripts": {
"test": "node test.js"
"test": "tape test/*.js test/server/*.js"
},
"license": "MIT",
"dependencies": {
"bops": "0.0.6"
"inherits": "~2.0.1",
"typedarray": "~0.0.5"
},
"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
```sh
$ npm install concat-stream
```
Writable stream that concatenates strings or binary data and calls a callback with the result. Not a transform stream -- more of a stream sink.
then
[![NPM](https://nodei.co/npm/concat-stream.png)](https://nodei.co/npm/concat-stream/)
[![browser support](https://ci.testling.com/maxogden/node-concat-stream.png)](https://ci.testling.com/maxogden/node-concat-stream)
### examples
```js

@@ -22,3 +24,3 @@ var concat = require('concat-stream')

```js
var write = concat(function(data) {})
var write = concat({ encoding: 'array' }, function(data) {})
write.write([1,2,3])

@@ -40,2 +42,34 @@ write.write([4,5,6])

MIT LICENSE
or if you want a Uint8Array, you can have those too!
```js
var write = concat({ encoding: 'u8' }, 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'))
```
# 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.
Use `opts.encoding` to control what format `data` should be:
* 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
# license
MIT LICENSE
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc