├── .gitignore ├── .travis.yml ├── index.js ├── test.js ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.12' 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require('through2') 2 | 3 | module.exports = function limiter (limit) { 4 | var stream = through(write) 5 | 6 | var len = 0 7 | 8 | return stream 9 | 10 | function write (ch, enc, next) { 11 | if (Buffer.isBuffer(ch)) len += ch.length 12 | else len += 1 13 | 14 | if (len >= limit) this.destroy(new Error('Limit exceeded')) 15 | else this.push(ch) 16 | 17 | next() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var limit = require('./') 3 | 4 | test('errors if limit exceeded', function exceeded (t) { 5 | var limiter = limit(5) // 5 byte limit 6 | 7 | limiter.on('error', function errored (error) { 8 | t.ok(error, 'got error') 9 | t.equal(error.message, 'Limit exceeded', 'got correct error message') 10 | t.end() 11 | }) 12 | 13 | limiter.write(new Buffer(6)) 14 | limiter.end() 15 | }) 16 | 17 | test('does not error if limit not exceeded', function okay (t) { 18 | var limiter = limit(5) // 5 byte limit 19 | 20 | limiter.on('error', function errored (error) { 21 | t.ifErr(error, 'did not get error') 22 | }) 23 | 24 | limiter.on('finish', function finished () { 25 | t.ok(true, 'finished without error') 26 | t.end() 27 | }) 28 | 29 | limiter.write(new Buffer(4)) 30 | limiter.end() 31 | }) 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "size-limit-stream", 3 | "version": "1.0.0", 4 | "description": "a through stream that destroys itself if an overall size limit for the combined stream throughput is exceeded", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard && node test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/maxogden/size-limit-stream.git" 12 | }, 13 | "keywords": [ 14 | "stream", 15 | "streams2", 16 | "through2" 17 | ], 18 | "author": "max ogden", 19 | "license": "BSD", 20 | "bugs": { 21 | "url": "https://github.com/maxogden/size-limit-stream/issues" 22 | }, 23 | "homepage": "https://github.com/maxogden/size-limit-stream", 24 | "dependencies": { 25 | "through2": "^0.6.3" 26 | }, 27 | "devDependencies": { 28 | "standard": "^3.3.0", 29 | "tape": "^3.5.0" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # size-limit-stream 2 | 3 | [![NPM](https://nodei.co/npm/size-limit-stream.png)](https://nodei.co/npm/size-limit-stream/) 4 | 5 | [![js-standard-style](https://raw.githubusercontent.com/feross/standard/master/badge.png)](https://github.com/feross/standard) 6 | 7 | [![Build Status](https://travis-ci.org/maxogden/size-limit-stream.svg?branch=master)](https://travis-ci.org/maxogden/size-limit-stream) 8 | 9 | a through stream that destroys itself if an overall size limit for the combined stream throughput is exceeded. useful for e.g. limiting HTTP upload size 10 | 11 | ## usage 12 | 13 | ### `limitStream(limit)` 14 | 15 | returns a through stream 16 | 17 | example: 18 | 19 | ```js 20 | var limiter = limitStream(1024 * 5) // 5kb max 21 | ``` 22 | 23 | ## example 24 | 25 | create a stream that concatenates input, but only if input is less than the limit: 26 | 27 | ```js 28 | var pumpify = require('pumpify') 29 | var concat = require('concat-stream') 30 | var limitStream = require('size-limit-stream') 31 | 32 | function uploadStream (cb) { 33 | var limiter = limitStream(1024 * 5) // 5kb max 34 | var concatter = concat(function concatted (buff) { 35 | cb(null, buff) 36 | }) 37 | 38 | var combined = pumpify(limiter, concatter) 39 | combined.on('error', cb) 40 | 41 | return combined 42 | } 43 | ``` 44 | --------------------------------------------------------------------------------