Skip to content

Commit

Permalink
feat: implemented shim for stream._handle.setBlocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed May 14, 2016
0 parents commit 6bde0c0
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
.nyc_output
14 changes: 14 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2016, Contributors

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# set-blocking

[![Build Status](https://travis-ci.org/yargs/set-blocking.svg)](https://travis-ci.org/yargs/set-blocking)
[![NPM version](https://img.shields.io/npm/v/set-blocking.svg)](https://www.npmjs.com/package/set-blocking)
[![Coverage Status](https://coveralls.io/repos/yargs/set-blocking/badge.svg?branch=)](https://coveralls.io/r/yargs/set-blocking?branch=master)
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)

set blocking `stdio` and `stderr` ensuring that terminal output does not truncate

```js
const setBlocking = require('set-blocking')
setBlocking(true)
console.log(someLargeStringToOutput)
```

## License

ISC
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function (blocking) {
[process.stdout, process.stderr].forEach(function(stream){
if (stream._handle && typeof stream._handle.setBlocking === 'function') {
stream._handle.setBlocking(blocking)
}
})
}
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "set-blocking",
"version": "1.0.0",
"description": "set blocking stdio and stderr ensuring that terminal output does not truncate",
"main": "index.js",
"scripts": {
"test": "nyc mocha ./test/*.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/yargs/set-blocking.git"
},
"keywords": [
"flush",
"terminal",
"blocking",
"stdio",
"stderr"
],
"author": "Ben Coe <[email protected]>",
"license": "ISC",
"bugs": {
"url": "https://github.com/yargs/set-blocking/issues"
},
"homepage": "https://github.com/yargs/set-blocking#readme",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"nyc": "^6.4.4",
"standard": "^7.0.1",
"standard-version": "^2.2.1"
},
"files": [
"index.js",
"LICENSE.txt"
]
}
15 changes: 15 additions & 0 deletions test/fixtures/yargs-497-stderr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node

// see: https://github.com/yargs/yargs/issues/497
var buffer = ''
for (var i = 0; i < 3000; i++) {
buffer += 'line ' + i + '\n'
}

var setBlocking = require('../../')
setBlocking(true)

console.error(buffer)
process.exit(1)

console.log('should not execute')
15 changes: 15 additions & 0 deletions test/fixtures/yargs-497-stdout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node

// see: https://github.com/yargs/yargs/issues/497
var buffer = ''
for (var i = 0; i < 3000; i++) {
buffer += 'line ' + i + '\n'
}

var setBlocking = require('../../')
setBlocking(true)

console.log(buffer)
process.exit(0)

console.log('should not execute')
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* global describe, should */

var exec = require('child_process').exec
var setBlocking = require('../')

require('chai').should()

describe('setBlocking', function () {
// see: https://github.com/yargs/yargs/issues/497
it('does not truncate text printed to stdout when process.exit() is called', function (done) {
exec('./test/fixtures/yargs-497-stdout.js', function (err, stdout, stderr) {
if (err) return done(err)
stdout.should.match(/line 2999/)
return done()
})
})

it('does not truncate text printed to stderr when process.exit() is called', function (done) {
exec('./test/fixtures/yargs-497-stderr.js', function (err, stdout, stderr) {
stderr.should.match(/line 2999/)
return done()
})
})
})

0 comments on commit 6bde0c0

Please sign in to comment.