Skip to content

Commit

Permalink
first pass at streams2 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Dec 24, 2013
1 parent 1e7932e commit 280ca96
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
73 changes: 33 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
var stream = require('stream')
var bops = require('bops')
var util = require('util')

function ConcatStream(cb) {
stream.Stream.call(this)
this.writable = true
if (cb) this.cb = cb
var Writable = require('stream').Writable
var inherits = require('inherits')

function ConcatStream(cb, opts) {
if (!(this instanceof ConcatStream)) return new ConcatStream(cb, opts)
Writable.call(this, { objectMode: true })
if (!opts) opts = {}
if (cb) this.on('finished', function () { cb(this.getBody()) })
this.mode = opts.mode || 'buffer'
this.body = []
this.on('error', function(err) {
// no-op
})
}

util.inherits(ConcatStream, stream.Stream)

ConcatStream.prototype.write = function(chunk) {
this.emit('data', chunk)
this.body.push(chunk)
}

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.isArray = function(arr) {
return Object.prototype.toString.call(arr) == '[object Array]'
module.exports = ConcatStream
inherits(ConcatStream, Writable)

ConcatStream.prototype._write = function(chunk, enc, next) {
if (this.mode !== 'buffer') {
this.body.push(chunk)
}
else if (Buffer.isBuffer(chunk)) {
this.body.push(chunk)
}
else if (typeof chunk === 'string' || isArrayish(chunk)) {
this.body.push(Buffer(chunk))
}
else {
this.body.push(Buffer(String(chunk)))
}
next()
}

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)
if (this.mode === 'array') return this.body
if (this.mode === 'string') return this.body.join('')
if (this.mode === 'buffer') return Buffer.concat(this.body)
return this.body
}

ConcatStream.prototype.end = function(chunk) {
if (chunk) this.write(chunk)
this.emit('end')
if (this.cb) this.cb(this.getBody())
var isArray = Array.isArray || function (arr) {
return Object.prototype.toString.call(arr) == '[object Array]'
}

module.exports = function(cb) {
return new ConcatStream(cb)
function isArrayish (arr) {
return /Array\]$/.test(Object.prototype.toString.call(arr))
}

module.exports.ConcatStream = ConcatStream
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
},
"license": "MIT",
"dependencies": {
"bops": "0.0.6"
"inherits": "~2.0.1"
}
}

0 comments on commit 280ca96

Please sign in to comment.