├── README.md ├── test └── ut │ ├── string-stream.js │ └── catchoutput.js ├── lib └── string-stream.js ├── .gitignore ├── LICENSE └── index.js /README.md: -------------------------------------------------------------------------------- 1 | catch-output.js 2 | =============== 3 | 4 | catch output in node.js 5 | -------------------------------------------------------------------------------- /test/ut/string-stream.js: -------------------------------------------------------------------------------- 1 | var W = require('../../lib/string-stream.js'); 2 | var w = new W(); 3 | w.write('wwww'); 4 | console.log(w.toString()); -------------------------------------------------------------------------------- /test/ut/catchoutput.js: -------------------------------------------------------------------------------- 1 | var c = require('../../'); 2 | 3 | console.log('test'); //system 4 | 5 | //sync 6 | var s = c.catchOutput(function () { 7 | console.log('xxx'); 8 | console.log('innerr'); 9 | }); 10 | 11 | console.log(s.toString()); //custom sync 12 | 13 | //async 14 | c.catchOutput(function (end) { 15 | console.log('right'); 16 | var o = end(); 17 | console.log(o.toString()); //custom async 18 | }); -------------------------------------------------------------------------------- /lib/string-stream.js: -------------------------------------------------------------------------------- 1 | var Stream = require('stream').Stream; 2 | var util = require('util'); 3 | 4 | module.exports = StringStream; 5 | 6 | function StringStream() { 7 | Stream.apply(this); 8 | this.memory = ''; 9 | this.readable = false; 10 | this.writable = true; 11 | } 12 | 13 | util.inherits(StringStream, Stream); 14 | 15 | StringStream.prototype.write = function (s) { 16 | this.memory += s; 17 | }; 18 | 19 | StringStream.prototype.toString = function () { 20 | return this.memory; 21 | }; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | # phpstorm 31 | 32 | /.idea 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 fansekey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var StringStream = require('./lib/string-stream.js'); 2 | var Console = require('console').Console; 3 | 4 | var exports = module.exports; 5 | var writers = {}; 6 | var cur = null; 7 | 8 | Object.defineProperty(global, 'console', { 9 | enumerable : true, 10 | writable : true, 11 | value : console 12 | }); 13 | 14 | exports.ob_start = function (id) { 15 | if (!id) { 16 | id = '__temp_fd'; 17 | } 18 | cur = id; 19 | if (!writers[id]) { 20 | writers[id] = { 21 | _prev: process.stdout, 22 | fd: new StringStream() 23 | }; 24 | } 25 | var fd = writers[id].fd; 26 | process.__defineGetter__('stdout', function () { 27 | return fd; 28 | }); 29 | console = new Console(fd, process.stderr); 30 | }; 31 | 32 | exports.ob_end = function (id) { 33 | if (!id) { 34 | id = cur; 35 | } 36 | var prev = writers[id]._prev; 37 | console = new Console(prev, process.stderr); //reset 38 | process.__defineGetter__('stdout', function () { 39 | return prev; 40 | }); 41 | var fd = writers[id].fd; 42 | delete writers[id]; 43 | return fd; 44 | }; 45 | 46 | exports._isAsync = function (fn) { 47 | var source = fn.toString(); 48 | return /function\s*\(\s*\w+\s*\)/.test(source) 49 | }; 50 | 51 | exports.catchOutput = function (fn) { 52 | this.ob_start(); 53 | var is_async = this._isAsync(fn); 54 | if (!is_async) { 55 | fn(); 56 | return this.ob_end(); 57 | } else { 58 | fn(this.ob_end); 59 | } 60 | }; 61 | 62 | exports.StringStream = StringStream; --------------------------------------------------------------------------------