package/package.json000644 0000000502 3560116604 011543 0ustar00000000 000000 { "name": "stdout-stream", "description": "Non-blocking stdout stream", "version": "1.4.1", "repository": "mafintosh/stdout-stream", "devDependencies": { "tape": "~2.12.3" }, "scripts": { "test": "tape test/index.js" }, "dependencies": { "readable-stream": "^2.0.1" }, "license": "MIT" } package/.travis.yml000644 0000000104 3560116604 011364 0ustar00000000 000000 language: node_js node_js: - "0.8" - "0.10" script: "npm test" package/index.js000644 0000001725 3560116604 010732 0ustar00000000 000000 var fs = require('fs'); var Writable = require('readable-stream/writable'); var exists = function(path) { try { return fs.existsSync(path); } catch (err) { return false; } }; module.exports = function() { var s = new Writable({highWaterMark:0}); var cb; var data; var tries = 0; var offset = 0; var write = function() { fs.write(1, data, offset, data.length - offset, null, onwrite); }; var onwrite = function(err, written) { if (err && err.code === 'EPIPE') return cb() if (err && err.code === 'EAGAIN' && tries++ < 30) return setTimeout(write, 10); if (err) return cb(err); tries = 0; if (offset + written >= data.length) return cb(); offset += written; write(); }; s._write = function(_data, enc, _cb) { offset = 0; cb = _cb; data = _data; write(); }; s._isStdio = true; s.isTTY = process.stdout.isTTY; s.on('finish', function() { fs.close(1, function(err) { if (err) s.emit('error', err); }); }); return s; }(); package/LICENSE000644 0000002034 3560116604 010264 0ustar00000000 000000 Copyright 2013 Mathias Buus 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. package/README.md000644 0000001761 3560116604 010544 0ustar00000000 000000 # stdout-stream Non-blocking stdout stream npm install stdout-stream [![build status](http://img.shields.io/travis/mafintosh/level-filesystem.svg?style=flat)](http://travis-ci.org/mafintosh/stdout-stream) ![dat](http://img.shields.io/badge/Development%20sponsored%20by-dat-green.svg?style=flat) ## Rant Try saving this example as `example.js` ``` js console.error('start'); process.stdout.write(new Buffer(1024*1024)); console.error('end'); ``` And run the following program ``` node example.js | sleep 1000 ``` The program will never print `end` since stdout in node currently is blocking - even when its being piped (!). stdout-stream tries to fix this by being a stream that writes to stdout but never blocks ## Usage ``` js var stdout = require('stdout-stream'); stdout.write('hello\n'); // write should NEVER block stdout.write('non-blocking\n') stdout.write('world\n'); ``` `stdout-stream` should behave in the same way as `process.stdout` (i.e. do not end on pipe etc) ## License MIT package/test/fixtures/end.js000644 0000000211 3560116604 013206 0ustar00000000 000000 var stdout = require('../../'); stdout.write('stdout'); stdout.end(function() { setTimeout(function() { process.exit(0); }, 10); });package/test/fixtures/hello-world.js000644 0000000122 3560116604 014671 0ustar00000000 000000 var stdout = require('../../'); stdout.write('hello\n'); stdout.write('world\n');package/test/index.js000644 0000001462 3560116604 011707 0ustar00000000 000000 var tape = require('tape'); var proc = require('child_process'); var path = require('path'); tape('print to stdout', function(t) { proc.exec('"'+process.execPath+'" '+path.join(__dirname,'fixtures','hello-world.js'), function(err, stdout) { t.ok(!err); t.same(stdout,'hello\nworld\n'); t.end(); }); }); tape('end stdout', function(t) { var ch = proc.exec('"'+process.execPath+'" '+path.join(__dirname,'fixtures','end.js')); var buf = []; var processOnExit = false; var stdoutOnEnd = false; ch.stdout.on('data', function(data) { buf.push(data); }); ch.stdout.on('end', function() { t.same(Buffer.concat(buf).toString(), 'stdout'); t.ok(!processOnExit); stdoutOnEnd = true; }); ch.on('exit', function(code) { processOnExit = true; t.ok(stdoutOnEnd); t.same(code, 0); t.end(); }); });