pax_global_header00006660000000000000000000000064127415434570014526gustar00rootroot0000000000000052 comment=c4ac12861e3097c3d59d2043d94077f2728a837a node-stream-meter-1.0.4/000077500000000000000000000000001274154345700150605ustar00rootroot00000000000000node-stream-meter-1.0.4/.gitignore000066400000000000000000000000301274154345700170410ustar00rootroot00000000000000node_modules .tern-port node-stream-meter-1.0.4/LICENSE000066400000000000000000000021061274154345700160640ustar00rootroot00000000000000(The MIT License) Copyright (c) Bryce B. Baril 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. node-stream-meter-1.0.4/README.md000066400000000000000000000046241274154345700163450ustar00rootroot00000000000000Stream Meter ============ [![NPM](https://nodei.co/npm/stream-meter.png)](https://nodei.co/npm/stream-meter/) [![david-dm](https://david-dm.org/brycebaril/node-stream-meter.png)](https://david-dm.org/brycebaril/node-stream-meter/) [![david-dm](https://david-dm.org/brycebaril/node-stream-meter/dev-status.png)](https://david-dm.org/brycebaril/node-stream-meter#info=devDependencies/) Stream Meter is a... uh, meter for streams. It is a streams2 Transform stream that passes through content, but counts the number of bytes it forwards. However, give it a size in bytes and it will abort as soon as that threshold is passed. This is useful for capping your [hyperquest](http://npm.im/hyperquest) or http/https clients or servers content size. ``` npm install stream-meter ``` Examples: ```javascript var meter = require("stream-meter") // make an un-capped meter var m = meter() process.stdin.pipe(m).pipe(process.stdout) setTimeout(function () { // Log how much we saw in a couple seconds. console.log(m.bytes) }, 2000) // this will abort (with an Error) in the frame where 1024 bytes is reached process.stdin.pipe(meter(1024)).pipe(process.stdout) // create a 1024 byte-capped meter var m = meter(1024) m.on("error", function (e) { // log the error but don't kill the process console.log(e.message) }) process.stdin.pipe(m).pipe(process.stdout) ``` ```javascript var hyperquest = require("hyperquest") var req = hyperquest("https://raw.github.com/mranney/node_redis/master/index.js") var meter = require("stream-meter")(1024) meter.on("error", function (e) { console.log(e.message) console.log("Read %s bytes", meter.bytes) }) req.pipe(meter).pipe(process.stderr) ``` ```bash $ node hypermeter.js 2> /dev/null Stream exceeded specified max of 1024 bytes. Read 7377 bytes ``` Usage ===== ```javascript var meter = require("stream-meter") var stream = meter(size) stream.on("error", function (e) { // handle the meter aborting the stream }) // read the bytes processed by the meter and passed through to any subsequent streams. var size = stream.bytes ``` See test/index.js for additional examples. Options ======= size ---- Size (in bytes) to trigger the stream to abort. It will complete whatever frame it aborted in, so the size streamed will still be >= size but no more than size + highWaterMark Properties ========== bytes ----- Number of bytes handled and passed through the meter. LICENSE ======= MIT node-stream-meter-1.0.4/index.js000066400000000000000000000011621274154345700165250ustar00rootroot00000000000000module.exports = Meter var util = require("util") var Transform = require("stream").Transform if (!Transform) { Transform = require("readable-stream/transform") } function Meter(maxBytes) { if (!(this instanceof Meter)) return new Meter(maxBytes) Transform.call(this) this.bytes = 0 this.maxBytes = maxBytes || Number.MAX_VALUE } util.inherits(Meter, Transform) Meter.prototype._transform = function (chunk, encoding, cb) { this.bytes += chunk.length this.push(chunk) if (this.bytes > this.maxBytes) { return cb(new Error("Stream exceeded specified max of " + this.maxBytes + " bytes.")) } cb() }node-stream-meter-1.0.4/package.json000066400000000000000000000014421274154345700173470ustar00rootroot00000000000000{ "name": "stream-meter", "version": "1.0.4", "description": "A stream meter that both counts the bytes piped through it, and can optionally abort on a max size. (e.g. limit a http request size)", "main": "index.js", "directories": { "test": "test" }, "dependencies": { "readable-stream": "^2.1.4" }, "devDependencies": { "concat-stream": "^1.5.1", "stream-spigot": "^3.0.3", "tape": "^4.6.0" }, "scripts": { "test": "node test/" }, "repository": { "type": "git", "url": "https://github.com/brycebaril/node-stream-meter.git" }, "keywords": [ "streams2", "streams", "meter", "abort" ], "author": "Bryce B. Baril", "license": "MIT", "bugs": { "url": "https://github.com/brycebaril/node-stream-meter/issues" } } node-stream-meter-1.0.4/test/000077500000000000000000000000001274154345700160375ustar00rootroot00000000000000node-stream-meter-1.0.4/test/index.js000066400000000000000000000023021274154345700175010ustar00rootroot00000000000000var test = require("tape").test var spigot = require("stream-spigot") var concat = require("concat-stream") var meter // Stats test("load", function (t) { t.plan(1) meter = require("../") t.ok(meter, "loaded module") }) test("no max (passthrough)", function (t) { t.plan(2) var m = meter() var content = "ABCD1234" function match(d) { t.equals(d.toString(), content) t.equals(m.bytes, 8) } spigot([content]).pipe(m).pipe(concat(match)) }) test("under max", function (t) { t.plan(2) var m = meter(100) var content = "ABCD1234" function match(d) { t.equals(d.toString(), content) t.equals(m.bytes, 8) } spigot([content]).pipe(m).pipe(concat(match)) }) test("stops at meter", function (t) { t.plan(3) var chunks = 0 function match(d) { t.fail() } var c = concat(match) var m = meter(10) m.on("error", function (e) { t.ok(e.message, "Stream exceeded specified max of 10 bytes.") // 12 because read frame is 4, so the 3rd read will put it over the max at 12 bytes t.equals(c.getBody().toString(), "ABCDEFGHIJKL") t.equals(m.bytes, 12) }) spigot(["ABCD", "EFGH", "IJKL", "MNOP", "QRST", "UVWX", "YZ"]).pipe(m).pipe(c) })