pax_global_header00006660000000000000000000000064127115213240014510gustar00rootroot0000000000000052 comment=321cf242ef6d130bb2e59c0565a61ded5dd2673f block-stream-0.0.9/000077500000000000000000000000001271152132400141015ustar00rootroot00000000000000block-stream-0.0.9/.gitignore000066400000000000000000000000551271152132400160710ustar00rootroot00000000000000.nyc_output nyc_output node_modules coverage block-stream-0.0.9/.travis.yml000066400000000000000000000001641271152132400162130ustar00rootroot00000000000000language: node_js node_js: - '0.8' - '0.10' - '0.12' - 'iojs' before_install: - npm install -g npm@latest block-stream-0.0.9/LICENCE000066400000000000000000000024461271152132400150740ustar00rootroot00000000000000Copyright (c) Isaac Z. Schlueter All rights reserved. The BSD License Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. block-stream-0.0.9/LICENSE000066400000000000000000000013751271152132400151140ustar00rootroot00000000000000The ISC License Copyright (c) Isaac Z. Schlueter and Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. block-stream-0.0.9/README.md000066400000000000000000000005611271152132400153620ustar00rootroot00000000000000# block-stream A stream of blocks. Write data into it, and it'll output data in buffer blocks the size you specify, padding with zeroes if necessary. ```javascript var block = new BlockStream(512) fs.createReadStream("some-file").pipe(block) block.pipe(fs.createWriteStream("block-file")) ``` When `.end()` or `.flush()` is called, it'll pad the block with zeroes. block-stream-0.0.9/bench/000077500000000000000000000000001271152132400151605ustar00rootroot00000000000000block-stream-0.0.9/bench/block-stream-pause.js000066400000000000000000000040311271152132400212120ustar00rootroot00000000000000var BlockStream = require("../block-stream.js") var blockSizes = [16, 25, 1024] , writeSizes = [4, 8, 15, 16, 17, 64, 100] , writeCounts = [1, 10, 100] , tap = require("tap") writeCounts.forEach(function (writeCount) { blockSizes.forEach(function (blockSize) { writeSizes.forEach(function (writeSize) { tap.test("writeSize=" + writeSize + " blockSize="+blockSize + " writeCount="+writeCount, function (t) { var f = new BlockStream(blockSize, {nopad: true }) var actualChunks = 0 var actualBytes = 0 var timeouts = 0 f.on("data", function (c) { timeouts ++ actualChunks ++ actualBytes += c.length // make sure that no data gets corrupted, and basic sanity var before = c.toString() // simulate a slow write operation f.pause() setTimeout(function () { timeouts -- var after = c.toString() t.equal(after, before, "should not change data") // now corrupt it, to find leaks. for (var i = 0; i < c.length; i ++) { c[i] = "x".charCodeAt(0) } f.resume() }, 100) }) f.on("end", function () { // round up to the nearest block size var expectChunks = Math.ceil(writeSize * writeCount * 2 / blockSize) var expectBytes = writeSize * writeCount * 2 t.equal(actualBytes, expectBytes, "bytes=" + expectBytes + " writeSize=" + writeSize) t.equal(actualChunks, expectChunks, "chunks=" + expectChunks + " writeSize=" + writeSize) // wait for all the timeout checks to finish, then end the test setTimeout(function WAIT () { if (timeouts > 0) return setTimeout(WAIT) t.end() }, 100) }) for (var i = 0; i < writeCount; i ++) { var a = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) a[j] = "a".charCodeAt(0) var b = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) b[j] = "b".charCodeAt(0) f.write(a) f.write(b) } f.end() }) }) }) }) block-stream-0.0.9/bench/block-stream.js000066400000000000000000000037661271152132400201150ustar00rootroot00000000000000var BlockStream = require("../block-stream.js") var blockSizes = [16, 25, 1024] , writeSizes = [4, 8, 15, 16, 17, 64, 100] , writeCounts = [1, 10, 100] , tap = require("tap") writeCounts.forEach(function (writeCount) { blockSizes.forEach(function (blockSize) { writeSizes.forEach(function (writeSize) { tap.test("writeSize=" + writeSize + " blockSize="+blockSize + " writeCount="+writeCount, function (t) { var f = new BlockStream(blockSize, {nopad: true }) var actualChunks = 0 var actualBytes = 0 var timeouts = 0 f.on("data", function (c) { timeouts ++ actualChunks ++ actualBytes += c.length // make sure that no data gets corrupted, and basic sanity var before = c.toString() // simulate a slow write operation setTimeout(function () { timeouts -- var after = c.toString() t.equal(after, before, "should not change data") // now corrupt it, to find leaks. for (var i = 0; i < c.length; i ++) { c[i] = "x".charCodeAt(0) } }, 100) }) f.on("end", function () { // round up to the nearest block size var expectChunks = Math.ceil(writeSize * writeCount * 2 / blockSize) var expectBytes = writeSize * writeCount * 2 t.equal(actualBytes, expectBytes, "bytes=" + expectBytes + " writeSize=" + writeSize) t.equal(actualChunks, expectChunks, "chunks=" + expectChunks + " writeSize=" + writeSize) // wait for all the timeout checks to finish, then end the test setTimeout(function WAIT () { if (timeouts > 0) return setTimeout(WAIT) t.end() }, 100) }) for (var i = 0; i < writeCount; i ++) { var a = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) a[j] = "a".charCodeAt(0) var b = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) b[j] = "b".charCodeAt(0) f.write(a) f.write(b) } f.end() }) }) }) }) block-stream-0.0.9/bench/dropper-pause.js000066400000000000000000000040161271152132400203050ustar00rootroot00000000000000var BlockStream = require("dropper") var blockSizes = [16, 25, 1024] , writeSizes = [4, 8, 15, 16, 17, 64, 100] , writeCounts = [1, 10, 100] , tap = require("tap") writeCounts.forEach(function (writeCount) { blockSizes.forEach(function (blockSize) { writeSizes.forEach(function (writeSize) { tap.test("writeSize=" + writeSize + " blockSize="+blockSize + " writeCount="+writeCount, function (t) { var f = new BlockStream(blockSize, {nopad: true }) var actualChunks = 0 var actualBytes = 0 var timeouts = 0 f.on("data", function (c) { timeouts ++ actualChunks ++ actualBytes += c.length // make sure that no data gets corrupted, and basic sanity var before = c.toString() // simulate a slow write operation f.pause() setTimeout(function () { timeouts -- var after = c.toString() t.equal(after, before, "should not change data") // now corrupt it, to find leaks. for (var i = 0; i < c.length; i ++) { c[i] = "x".charCodeAt(0) } f.resume() }, 100) }) f.on("end", function () { // round up to the nearest block size var expectChunks = Math.ceil(writeSize * writeCount * 2 / blockSize) var expectBytes = writeSize * writeCount * 2 t.equal(actualBytes, expectBytes, "bytes=" + expectBytes + " writeSize=" + writeSize) t.equal(actualChunks, expectChunks, "chunks=" + expectChunks + " writeSize=" + writeSize) // wait for all the timeout checks to finish, then end the test setTimeout(function WAIT () { if (timeouts > 0) return setTimeout(WAIT) t.end() }, 100) }) for (var i = 0; i < writeCount; i ++) { var a = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) a[j] = "a".charCodeAt(0) var b = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) b[j] = "b".charCodeAt(0) f.write(a) f.write(b) } f.end() }) }) }) }) block-stream-0.0.9/bench/dropper.js000066400000000000000000000037531271152132400172010ustar00rootroot00000000000000var BlockStream = require("dropper") var blockSizes = [16, 25, 1024] , writeSizes = [4, 8, 15, 16, 17, 64, 100] , writeCounts = [1, 10, 100] , tap = require("tap") writeCounts.forEach(function (writeCount) { blockSizes.forEach(function (blockSize) { writeSizes.forEach(function (writeSize) { tap.test("writeSize=" + writeSize + " blockSize="+blockSize + " writeCount="+writeCount, function (t) { var f = new BlockStream(blockSize, {nopad: true }) var actualChunks = 0 var actualBytes = 0 var timeouts = 0 f.on("data", function (c) { timeouts ++ actualChunks ++ actualBytes += c.length // make sure that no data gets corrupted, and basic sanity var before = c.toString() // simulate a slow write operation setTimeout(function () { timeouts -- var after = c.toString() t.equal(after, before, "should not change data") // now corrupt it, to find leaks. for (var i = 0; i < c.length; i ++) { c[i] = "x".charCodeAt(0) } }, 100) }) f.on("end", function () { // round up to the nearest block size var expectChunks = Math.ceil(writeSize * writeCount * 2 / blockSize) var expectBytes = writeSize * writeCount * 2 t.equal(actualBytes, expectBytes, "bytes=" + expectBytes + " writeSize=" + writeSize) t.equal(actualChunks, expectChunks, "chunks=" + expectChunks + " writeSize=" + writeSize) // wait for all the timeout checks to finish, then end the test setTimeout(function WAIT () { if (timeouts > 0) return setTimeout(WAIT) t.end() }, 100) }) for (var i = 0; i < writeCount; i ++) { var a = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) a[j] = "a".charCodeAt(0) var b = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) b[j] = "b".charCodeAt(0) f.write(a) f.write(b) } f.end() }) }) }) }) block-stream-0.0.9/block-stream.js000066400000000000000000000146331271152132400170310ustar00rootroot00000000000000// write data to it, and it'll emit data in 512 byte blocks. // if you .end() or .flush(), it'll emit whatever it's got, // padded with nulls to 512 bytes. module.exports = BlockStream var Stream = require("stream").Stream , inherits = require("inherits") , assert = require("assert").ok , debug = process.env.DEBUG ? console.error : function () {} function BlockStream (size, opt) { this.writable = this.readable = true this._opt = opt || {} this._chunkSize = size || 512 this._offset = 0 this._buffer = [] this._bufferLength = 0 if (this._opt.nopad) this._zeroes = false else { this._zeroes = new Buffer(this._chunkSize) for (var i = 0; i < this._chunkSize; i ++) { this._zeroes[i] = 0 } } } inherits(BlockStream, Stream) BlockStream.prototype.write = function (c) { // debug(" BS write", c) if (this._ended) throw new Error("BlockStream: write after end") if (c && !Buffer.isBuffer(c)) c = new Buffer(c + "") if (c.length) { this._buffer.push(c) this._bufferLength += c.length } // debug("pushed onto buffer", this._bufferLength) if (this._bufferLength >= this._chunkSize) { if (this._paused) { // debug(" BS paused, return false, need drain") this._needDrain = true return false } this._emitChunk() } return true } BlockStream.prototype.pause = function () { // debug(" BS pausing") this._paused = true } BlockStream.prototype.resume = function () { // debug(" BS resume") this._paused = false return this._emitChunk() } BlockStream.prototype.end = function (chunk) { // debug("end", chunk) if (typeof chunk === "function") cb = chunk, chunk = null if (chunk) this.write(chunk) this._ended = true this.flush() } BlockStream.prototype.flush = function () { this._emitChunk(true) } BlockStream.prototype._emitChunk = function (flush) { // debug("emitChunk flush=%j emitting=%j paused=%j", flush, this._emitting, this._paused) // emit a chunk if (flush && this._zeroes) { // debug(" BS push zeroes", this._bufferLength) // push a chunk of zeroes var padBytes = (this._bufferLength % this._chunkSize) if (padBytes !== 0) padBytes = this._chunkSize - padBytes if (padBytes > 0) { // debug("padBytes", padBytes, this._zeroes.slice(0, padBytes)) this._buffer.push(this._zeroes.slice(0, padBytes)) this._bufferLength += padBytes // debug(this._buffer[this._buffer.length - 1].length, this._bufferLength) } } if (this._emitting || this._paused) return this._emitting = true // debug(" BS entering loops") var bufferIndex = 0 while (this._bufferLength >= this._chunkSize && (flush || !this._paused)) { // debug(" BS data emission loop", this._bufferLength) var out , outOffset = 0 , outHas = this._chunkSize while (outHas > 0 && (flush || !this._paused) ) { // debug(" BS data inner emit loop", this._bufferLength) var cur = this._buffer[bufferIndex] , curHas = cur.length - this._offset // debug("cur=", cur) // debug("curHas=%j", curHas) // If it's not big enough to fill the whole thing, then we'll need // to copy multiple buffers into one. However, if it is big enough, // then just slice out the part we want, to save unnecessary copying. // Also, need to copy if we've already done some copying, since buffers // can't be joined like cons strings. if (out || curHas < outHas) { out = out || new Buffer(this._chunkSize) cur.copy(out, outOffset, this._offset, this._offset + Math.min(curHas, outHas)) } else if (cur.length === outHas && this._offset === 0) { // shortcut -- cur is exactly long enough, and no offset. out = cur } else { // slice out the piece of cur that we need. out = cur.slice(this._offset, this._offset + outHas) } if (curHas > outHas) { // means that the current buffer couldn't be completely output // update this._offset to reflect how much WAS written this._offset += outHas outHas = 0 } else { // output the entire current chunk. // toss it away outHas -= curHas outOffset += curHas bufferIndex ++ this._offset = 0 } } this._bufferLength -= this._chunkSize assert(out.length === this._chunkSize) // debug("emitting data", out) // debug(" BS emitting, paused=%j", this._paused, this._bufferLength) this.emit("data", out) out = null } // debug(" BS out of loops", this._bufferLength) // whatever is left, it's not enough to fill up a block, or we're paused this._buffer = this._buffer.slice(bufferIndex) if (this._paused) { // debug(" BS paused, leaving", this._bufferLength) this._needsDrain = true this._emitting = false return } // if flushing, and not using null-padding, then need to emit the last // chunk(s) sitting in the queue. We know that it's not enough to // fill up a whole block, because otherwise it would have been emitted // above, but there may be some offset. var l = this._buffer.length if (flush && !this._zeroes && l) { if (l === 1) { if (this._offset) { this.emit("data", this._buffer[0].slice(this._offset)) } else { this.emit("data", this._buffer[0]) } } else { var outHas = this._bufferLength , out = new Buffer(outHas) , outOffset = 0 for (var i = 0; i < l; i ++) { var cur = this._buffer[i] , curHas = cur.length - this._offset cur.copy(out, outOffset, this._offset) this._offset = 0 outOffset += curHas this._bufferLength -= curHas } this.emit("data", out) } // truncate this._buffer.length = 0 this._bufferLength = 0 this._offset = 0 } // now either drained or ended // debug("either draining, or ended", this._bufferLength, this._ended) // means that we've flushed out all that we can so far. if (this._needDrain) { // debug("emitting drain", this._bufferLength) this._needDrain = false this.emit("drain") } if ((this._bufferLength === 0) && this._ended && !this._endEmitted) { // debug("emitting end", this._bufferLength) this._endEmitted = true this.emit("end") } this._emitting = false // debug(" BS no longer emitting", flush, this._paused, this._emitting, this._bufferLength, this._chunkSize) } block-stream-0.0.9/package.json000066400000000000000000000010371271152132400163700ustar00rootroot00000000000000{ "author": "Isaac Z. Schlueter (http://blog.izs.me/)", "name": "block-stream", "description": "a stream of blocks", "version": "0.0.9", "repository": { "type": "git", "url": "git://github.com/isaacs/block-stream.git" }, "engines": { "node": "0.4 || >=0.5.8" }, "main": "block-stream.js", "dependencies": { "inherits": "~2.0.0" }, "devDependencies": { "tap": "^5.7.1" }, "scripts": { "test": "tap test/*.js --cov" }, "license": "ISC", "files": [ "block-stream.js" ] } block-stream-0.0.9/test/000077500000000000000000000000001271152132400150605ustar00rootroot00000000000000block-stream-0.0.9/test/basic.js000066400000000000000000000013321271152132400164760ustar00rootroot00000000000000var tap = require("tap") , BlockStream = require("../block-stream.js") tap.test("basic test", function (t) { var b = new BlockStream(16) var fs = require("fs") var fstr = fs.createReadStream(__filename, {encoding: "utf8"}) fstr.pipe(b) var stat t.doesNotThrow(function () { stat = fs.statSync(__filename) }, "stat should not throw") var totalBytes = 0 b.on("data", function (c) { t.equal(c.length, 16, "chunks should be 16 bytes long") t.type(c, Buffer, "chunks should be buffer objects") totalBytes += c.length }) b.on("end", function () { var expectedBytes = stat.size + (16 - stat.size % 16) t.equal(totalBytes, expectedBytes, "Should be multiple of 16") t.end() }) }) block-stream-0.0.9/test/nopad-thorough.js000066400000000000000000000040041271152132400203520ustar00rootroot00000000000000var BlockStream = require("../block-stream.js") var blockSizes = [16]//, 25]//, 1024] , writeSizes = [4, 15, 16, 17, 64 ]//, 64, 100] , writeCounts = [1, 10]//, 100] , tap = require("tap") writeCounts.forEach(function (writeCount) { blockSizes.forEach(function (blockSize) { writeSizes.forEach(function (writeSize) { tap.test("writeSize=" + writeSize + " blockSize="+blockSize + " writeCount="+writeCount, function (t) { var f = new BlockStream(blockSize, {nopad: true }) var actualChunks = 0 var actualBytes = 0 var timeouts = 0 f.on("data", function (c) { timeouts ++ actualChunks ++ actualBytes += c.length // make sure that no data gets corrupted, and basic sanity var before = c.toString() // simulate a slow write operation setTimeout(function () { timeouts -- var after = c.toString() t.equal(after, before, "should not change data") // now corrupt it, to find leaks. for (var i = 0; i < c.length; i ++) { c[i] = "x".charCodeAt(0) } }, 100) }) f.on("end", function () { // round up to the nearest block size var expectChunks = Math.ceil(writeSize * writeCount * 2 / blockSize) var expectBytes = writeSize * writeCount * 2 t.equal(actualBytes, expectBytes, "bytes=" + expectBytes + " writeSize=" + writeSize) t.equal(actualChunks, expectChunks, "chunks=" + expectChunks + " writeSize=" + writeSize) // wait for all the timeout checks to finish, then end the test setTimeout(function WAIT () { if (timeouts > 0) return setTimeout(WAIT) t.end() }, 100) }) for (var i = 0; i < writeCount; i ++) { var a = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) a[j] = "a".charCodeAt(0) var b = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) b[j] = "b".charCodeAt(0) f.write(a) f.write(b) } f.end() }) }) }) }) block-stream-0.0.9/test/nopad.js000066400000000000000000000022561271152132400165240ustar00rootroot00000000000000var BlockStream = require("../") var tap = require("tap") tap.test("don't pad, small writes", function (t) { var f = new BlockStream(16, { nopad: true }) t.plan(1) f.on("data", function (c) { t.equal(c.toString(), "abc", "should get 'abc'") }) f.on("end", function () { t.end() }) f.write(new Buffer("a")) f.write(new Buffer("b")) f.write(new Buffer("c")) f.end() }) tap.test("don't pad, exact write", function (t) { var f = new BlockStream(16, { nopad: true }) t.plan(1) var first = true f.on("data", function (c) { if (first) { first = false t.equal(c.toString(), "abcdefghijklmnop", "first chunk") } else { t.fail("should only get one") } }) f.on("end", function () { t.end() }) f.end(new Buffer("abcdefghijklmnop")) }) tap.test("don't pad, big write", function (t) { var f = new BlockStream(16, { nopad: true }) t.plan(2) var first = true f.on("data", function (c) { if (first) { first = false t.equal(c.toString(), "abcdefghijklmnop", "first chunk") } else { t.equal(c.toString(), "q") } }) f.on("end", function () { t.end() }) f.end(new Buffer("abcdefghijklmnopq")) }) block-stream-0.0.9/test/pause-resume.js000066400000000000000000000041641271152132400200360ustar00rootroot00000000000000var BlockStream = require("../block-stream.js") var blockSizes = [16] , writeSizes = [15, 16, 17] , writeCounts = [1, 10]//, 100] , tap = require("tap") writeCounts.forEach(function (writeCount) { blockSizes.forEach(function (blockSize) { writeSizes.forEach(function (writeSize) { tap.test("writeSize=" + writeSize + " blockSize="+blockSize + " writeCount="+writeCount, function (t) { var f = new BlockStream(blockSize) var actualChunks = 0 var actualBytes = 0 var timeouts = 0 var paused = false f.on("data", function (c) { timeouts ++ t.notOk(paused, "should not be paused when emitting data") actualChunks ++ actualBytes += c.length // make sure that no data gets corrupted, and basic sanity var before = c.toString() // simulate a slow write operation paused = true f.pause() process.nextTick(function () { var after = c.toString() t.equal(after, before, "should not change data") // now corrupt it, to find leaks. for (var i = 0; i < c.length; i ++) { c[i] = "x".charCodeAt(0) } paused = false f.resume() timeouts -- }) }) f.on("end", function () { // round up to the nearest block size var expectChunks = Math.ceil(writeSize * writeCount * 2 / blockSize) var expectBytes = expectChunks * blockSize t.equal(actualBytes, expectBytes, "bytes=" + expectBytes + " writeSize=" + writeSize) t.equal(actualChunks, expectChunks, "chunks=" + expectChunks + " writeSize=" + writeSize) // wait for all the timeout checks to finish, then end the test setTimeout(function WAIT () { if (timeouts > 0) return setTimeout(WAIT) t.end() }, 200) }) for (var i = 0; i < writeCount; i ++) { var a = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) a[j] = "a".charCodeAt(0) var b = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) b[j] = "b".charCodeAt(0) f.write(a) f.write(b) } f.end() }) }) }) }) block-stream-0.0.9/test/thorough.js000066400000000000000000000037621271152132400172650ustar00rootroot00000000000000var BlockStream = require("../block-stream.js") var blockSizes = [16]//, 25]//, 1024] , writeSizes = [4, 15, 16, 17, 64 ]//, 64, 100] , writeCounts = [1, 10]//, 100] , tap = require("tap") writeCounts.forEach(function (writeCount) { blockSizes.forEach(function (blockSize) { writeSizes.forEach(function (writeSize) { tap.test("writeSize=" + writeSize + " blockSize="+blockSize + " writeCount="+writeCount, function (t) { var f = new BlockStream(blockSize) var actualChunks = 0 var actualBytes = 0 var timeouts = 0 f.on("data", function (c) { timeouts ++ actualChunks ++ actualBytes += c.length // make sure that no data gets corrupted, and basic sanity var before = c.toString() // simulate a slow write operation setTimeout(function () { timeouts -- var after = c.toString() t.equal(after, before, "should not change data") // now corrupt it, to find leaks. for (var i = 0; i < c.length; i ++) { c[i] = "x".charCodeAt(0) } }, 100) }) f.on("end", function () { // round up to the nearest block size var expectChunks = Math.ceil(writeSize * writeCount * 2 / blockSize) var expectBytes = expectChunks * blockSize t.equal(actualBytes, expectBytes, "bytes=" + expectBytes + " writeSize=" + writeSize) t.equal(actualChunks, expectChunks, "chunks=" + expectChunks + " writeSize=" + writeSize) // wait for all the timeout checks to finish, then end the test setTimeout(function WAIT () { if (timeouts > 0) return setTimeout(WAIT) t.end() }, 100) }) for (var i = 0; i < writeCount; i ++) { var a = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) a[j] = "a".charCodeAt(0) var b = new Buffer(writeSize); for (var j = 0; j < writeSize; j ++) b[j] = "b".charCodeAt(0) f.write(a) f.write(b) } f.end() }) }) }) }) block-stream-0.0.9/test/two-stream.js000066400000000000000000000026451271152132400175270ustar00rootroot00000000000000var t = require('tap'), BlockStream = require("../block-stream.js"), isize = 0, tsize = 0, fsize = 0, psize = 0, i = 0, filter = null, paper = null, stack = null, // a source data buffer tsize = 1 * 1024; // <- 1K stack = new Buffer( tsize ); for ( ; i < tsize; i++) stack[i] = "x".charCodeAt(0); isize = 1 * 1024; // <- initial packet size with 4K no bug! fsize = 2 * 1024 ; // <- first block-stream size psize = Math.ceil( isize / 6 ); // <- second block-stream size fexpected = Math.ceil( tsize / fsize ); // <- packets expected for first pexpected = Math.ceil( tsize / psize ); // <- packets expected for second filter = new BlockStream( fsize, { nopad : true } ); paper = new BlockStream( psize, { nopad : true } ); var fcounter = 0; filter.on( 'data', function (c) { // verify that they're not null-padded for (var i = 0; i < c.length; i ++) { t.strictEqual(c[i], "x".charCodeAt(0)) } ++fcounter; } ); var pcounter = 0; paper.on( 'data', function (c) { // verify that they're not null-padded for (var i = 0; i < c.length; i ++) { t.strictEqual(c[i], "x".charCodeAt(0)) } ++pcounter; } ); filter.pipe( paper ); filter.on( 'end', function () { t.strictEqual( fcounter, fexpected ); } ); paper.on( 'end', function () { t.strictEqual( pcounter, pexpected ); } ); for ( i = 0, j = isize; j <= tsize; j += isize ) { filter.write( stack.slice( j - isize, j ) ); } filter.end();