Sha256: 41349a33dc58e1c951ab04c9756875febadcec3a046122f18d4a4111cc2c08da
Contents?: true
Size: 1.22 KB
Versions: 8
Compression:
Stored size: 1.22 KB
Contents
var stream = require('stream') var util = require('util') function ConcatStream(cb) { stream.Stream.call(this) this.writable = true if (cb) this.cb = cb this.body = [] if (this.cb) this.on('error', cb) } util.inherits(ConcatStream, stream.Stream) ConcatStream.prototype.write = function(chunk) { this.body.push(chunk) } 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) { var isArray = Array.isArray(arr) var isTypedArray = arr.toString().match(/Array/) return isArray || isTypedArray } ConcatStream.prototype.getBody = function () { if (this.body.length === 0) return if (typeof(this.body[0]) === "string") return this.body.join('') if (this.isArray(this.body[0])) return this.arrayConcat(this.body) if (typeof(Buffer) !== "undefined" && Buffer.isBuffer(this.body[0])) { return Buffer.concat(this.body) } return this.body } ConcatStream.prototype.end = function() { if (this.cb) this.cb(false, this.getBody()) } module.exports = function(cb) { return new ConcatStream(cb) } module.exports.ConcatStream = ConcatStream
Version data entries
8 entries across 6 versions & 3 rubygems