Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

join buffers correctly

  • Loading branch information...
commit 3b4a1f5426c5db138a588ae371bc66da17564851 1 parent 16326d2
@maxogden authored
View
9 index.js
@@ -1,5 +1,6 @@
var stream = require('stream')
var util = require('util')
+var bufferjoiner = require('bufferjoiner')
function ConcatStream(cb) {
stream.Stream.call(this)
@@ -26,7 +27,13 @@ ConcatStream.prototype.getBody = function () {
})
return first
}
- if (this.body.length === 1) return this.body[0]
+ if (typeof(Buffer) !== "undefined" && Buffer.isBuffer(this.body[0])) {
+ var buffs = new BufferJoiner()
+ this.body.forEach(function(buf) {
+ buffs.add(buf)
+ })
+ return buffs.join()
+ }
return this.body
}
View
6 node_modules/bufferjoiner/.npmignore
@@ -0,0 +1,6 @@
+docs/
+examples/
+test/
+lib-cov/
+coverage.html
+README.md
View
5 node_modules/bufferjoiner/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.6
+ - 0.8
View
22 node_modules/bufferjoiner/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Kilian Ciuffolo, me@nailik.org
+
+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.
View
17 node_modules/bufferjoiner/Makefile
@@ -0,0 +1,17 @@
+REPORTER = spec
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js $(OPT) --reporter $(REPORTER)
+
+test-bail:
+ @rm -rf lib-cov
+ @jscoverage lib lib-cov
+ $(MAKE) test OPT=--bail
+
+test-cov:
+ @rm -rf lib-cov
+ @jscoverage lib lib-cov
+ @BUFFER_JOINER_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
+ @open -g coverage.html
+
+.PHONY: test test-bail test-cov
View
3  node_modules/bufferjoiner/index.js
@@ -0,0 +1,3 @@
+module.exports = process.env.BUFFER_JOINER_COV
+ ? require('./lib-cov/bufferjoiner')
+ : require('./lib/bufferjoiner')
View
72 node_modules/bufferjoiner/lib/bufferjoiner.js
@@ -0,0 +1,72 @@
+/**
+ * Setup BufferJoiner.
+ *
+ * @api public
+ */
+
+var BufferJoiner = module.exports = function BufferJoiner() {
+ if (!(this instanceof BufferJoiner)) {
+ return new BufferJoiner()
+ }
+
+ Object.defineProperty(this, '_buffersList', {
+ value: [],
+ writable: true,
+ enumerable: false,
+ configurable: false
+ })
+ Object.defineProperty(this, '_length', {
+ value: 0,
+ writable: true,
+ enumerable: false,
+ configurable: false
+ })
+}
+
+/**
+ * Return `this._length`
+ *
+ * @api public
+ */
+
+BufferJoiner.prototype.__defineGetter__('length', function length() {
+ return this._length
+})
+
+/**
+ * Add buffer into the buffers list.
+ *
+ * @param {Buffer} buffer
+ * @api public
+ */
+
+BufferJoiner.prototype.add = function add(buffer) {
+ this._buffersList.push(buffer)
+ this._length += buffer.length
+ return this
+}
+
+/**
+ * Return a joined version of the buffers list
+ * add reset status. If `reAdd` return value
+ * will be readded to the buffers list.
+ *
+ * @param {Boolean} reAdd
+ * @api public
+ */
+
+BufferJoiner.prototype.join = function join(reAdd) {
+ var result = new Buffer(this._length)
+ var lastFreeIndex = 0
+ var buffer
+
+ while (buffer = this._buffersList.shift()) {
+ buffer.copy(result, lastFreeIndex)
+ lastFreeIndex += buffer.length
+ }
+
+ this._length = 0
+ reAdd && this.add(result)
+
+ return result
+}
View
43 node_modules/bufferjoiner/package.json
@@ -0,0 +1,43 @@
+{
+ "author": {
+ "name": "Kilian Ciuffolo",
+ "email": "me@nailik.org",
+ "url": "http://nailik.org"
+ },
+ "name": "bufferjoiner",
+ "main": "./",
+ "description": "A nodejs binary buffer utility",
+ "keywords": [
+ "buffer",
+ "merge",
+ "join",
+ "binary"
+ ],
+ "version": "0.1.3",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/kilianc/node-bufferjoiner.git"
+ },
+ "devDependencies": {
+ "mocha": "1.3.x",
+ "chai": "1.0.x"
+ },
+ "_npmUser": {
+ "name": "maxogden",
+ "email": "max@maxogden.com"
+ },
+ "_id": "bufferjoiner@0.1.3",
+ "dependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "_engineSupported": true,
+ "_npmVersion": "1.1.21",
+ "_nodeVersion": "v0.8.0",
+ "_defaultsLoaded": true,
+ "dist": {
+ "shasum": "3fa544cc5728b455fefbc708218bfca900eb9481"
+ },
+ "_from": "bufferjoiner@0.1.3"
+}
View
5 package.json
@@ -1,7 +1,7 @@
{ "name" : "concat-stream"
, "description" : "writable stream that concatenates strings or data and calls a callback with the result"
, "tags" : ["stream", "simple", "util", "utility"]
-, "version" : "0.0.4"
+, "version" : "0.0.5"
, "author" : "Max Ogden <max@maxogden.com>"
, "repository" :
{ "type" : "git"
@@ -10,4 +10,7 @@
, "bugs" :
{ "url" : "http://github.com/maxogden/node-concat-stream/issues" }
, "engines" : ["node >= 0.4.0"]
+, "dependencies": {
+ "bufferjoiner": "0.1.3"
+ }
}
View
8 readme.md
@@ -19,5 +19,13 @@ works with arrays too!
write.write([4,5,6])
write.end()
// data will be [1,2,3,4,5,6] in the above callback
+
+works with buffers too! can't believe the deals!
+
+ var write = concat(function(err, 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
MIT LICENSE
Please sign in to comment.
Something went wrong with that request. Please try again.