pax_global_header00006660000000000000000000000064125665225330014523gustar00rootroot0000000000000052 comment=406ff9a52ad9f80d85b6037e09472ba4a6527576 read-only-stream-2.0.0/000077500000000000000000000000001256652253300147055ustar00rootroot00000000000000read-only-stream-2.0.0/.travis.yml000066400000000000000000000001361256652253300170160ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" before_install: - npm install -g npm@~1.4.6 read-only-stream-2.0.0/LICENSE000066400000000000000000000020611256652253300157110ustar00rootroot00000000000000This software is released under the MIT license: 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. read-only-stream-2.0.0/example/000077500000000000000000000000001256652253300163405ustar00rootroot00000000000000read-only-stream-2.0.0/example/main.js000066400000000000000000000001751256652253300176250ustar00rootroot00000000000000var wrap = require('./wrap.js'); var ro = wrap(); // can't write to `ro` and muck up internal state ro.pipe(process.stdout); read-only-stream-2.0.0/example/wrap.js000066400000000000000000000002721256652253300176500ustar00rootroot00000000000000var through = require('through2'); var readonly = require('../'); module.exports = function () { var stream = through(); stream.end('wooooo\n'); return readonly(stream); }; read-only-stream-2.0.0/index.js000066400000000000000000000014661256652253300163610ustar00rootroot00000000000000var Readable = require('readable-stream').Readable; module.exports = function (stream) { var opts = stream._readableState; if (typeof stream.read !== 'function') { stream = new Readable(opts).wrap(stream); } var ro = new Readable({ objectMode: opts && opts.objectMode }); var waiting = false; stream.on('readable', function () { if (waiting) { waiting = false; ro._read(); } }); ro._read = function () { var buf, reads = 0; while ((buf = stream.read()) !== null) { ro.push(buf); reads ++; } if (reads === 0) waiting = true; }; stream.once('end', function () { ro.push(null) }); stream.on('error', function (err) { ro.emit('error', err) }); return ro; }; read-only-stream-2.0.0/package.json000066400000000000000000000014301256652253300171710ustar00rootroot00000000000000{ "name": "read-only-stream", "version": "2.0.0", "description": "wrap a readable/writable stream to be read-only", "main": "index.js", "dependencies": { "readable-stream": "^2.0.2" }, "devDependencies": { "concat-stream": "^1.4.6", "tape": "^4.2.0", "through2": "^2.0.0", "through": "^2.3.4", "covert": "^1.0.0" }, "scripts": { "test": "tape test/*.js", "coverage": "covert test/*.js" }, "repository": { "type": "git", "url": "git://github.com/substack/read-only-stream.git" }, "homepage": "https://github.com/substack/read-only-stream", "keywords": [ "stream", "readonly" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "license": "MIT" } read-only-stream-2.0.0/readme.markdown000066400000000000000000000025261256652253300177130ustar00rootroot00000000000000# read-only-stream wrap a readable/writable stream to be read-only to prevent mucking up the input side [![build status](https://secure.travis-ci.org/substack/read-only-stream.png)](http://travis-ci.org/substack/read-only-stream) # example Suppose you have a module that uses a readable/writable stream internally but want to expose just the readable part of that internal stream. This is common if you use the writable side internally and expose the readable side as the interface. Now we can write some code like this with a `through` stream internally for convenience: ``` js var through = require('through2'); var readonly = require('read-only-stream'); module.exports = function () { var stream = through(); stream.end('wooooo\n'); return readonly(stream); }; ``` but consumers won't be able to write to the input side and break the api: ``` js var wrap = require('./wrap.js'); var ro = wrap(); // can't write to `ro` and muck up internal state ro.pipe(process.stdout); ``` # methods ``` js var readonly = require('read-only-stream') ``` ## var ro = readonly(stream) Return a readable stream `ro` that wraps the readable/writable `stream` argument given to only expose the readable side. `stream` can be a streams1 or streams2 stream. # install With [npm](https://npmjs.org) do: ``` npm install read-only-stream ``` # license MIT read-only-stream-2.0.0/test/000077500000000000000000000000001256652253300156645ustar00rootroot00000000000000read-only-stream-2.0.0/test/error.js000066400000000000000000000004641256652253300173570ustar00rootroot00000000000000var test = require('tape'); var readonly = require('../'); var through = require('through2'); test('error', function (t) { t.plan(1); var stream = through(); var ro = readonly(stream); ro.on('error', function (err) { t.ok(err); }); stream.emit('error', new Error); }); read-only-stream-2.0.0/test/ro.js000066400000000000000000000007151256652253300166450ustar00rootroot00000000000000var test = require('tape'); var readonly = require('../'); var through = require('through2'); var concat = require('concat-stream'); test('readonly', function (t) { t.plan(2); var stream = through(); stream.write('woo'); var ro = readonly(stream); ro.pipe(concat(function (body) { t.equal(body.toString('utf8'), 'woo'); })); t.throws(function () { ro.write('beep'); }); stream.end(); }); read-only-stream-2.0.0/test/streams1.js000066400000000000000000000006701256652253300177640ustar00rootroot00000000000000var test = require('tape'); var readonly = require('../'); var through = require('through'); var concat = require('concat-stream'); test('streams1', function (t) { t.plan(2); var stream = through(); var ro = readonly(stream); ro.pipe(concat(function (body) { t.equal(body.toString('utf8'), 'woo'); })); t.throws(function () { ro.write('beep'); }); stream.end('woo'); });