Skip to content

Commit 6bde0c0

Browse files
author
Benjamin Coe
committed
feat: implemented shim for stream._handle.setBlocking
0 parents  commit 6bde0c0

File tree

8 files changed

+133
-0
lines changed

8 files changed

+133
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.DS_Store
3+
.nyc_output

LICENSE.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Copyright (c) 2016, Contributors
2+
3+
Permission to use, copy, modify, and/or distribute this software
4+
for any purpose with or without fee is hereby granted, provided
5+
that the above copyright notice and this permission notice
6+
appear in all copies.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10+
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
11+
LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# set-blocking
2+
3+
[![Build Status](https://travis-ci.org/yargs/set-blocking.svg)](https://travis-ci.org/yargs/set-blocking)
4+
[![NPM version](https://img.shields.io/npm/v/set-blocking.svg)](https://www.npmjs.com/package/set-blocking)
5+
[![Coverage Status](https://coveralls.io/repos/yargs/set-blocking/badge.svg?branch=)](https://coveralls.io/r/yargs/set-blocking?branch=master)
6+
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
7+
8+
set blocking `stdio` and `stderr` ensuring that terminal output does not truncate
9+
10+
```js
11+
const setBlocking = require('set-blocking')
12+
setBlocking(true)
13+
console.log(someLargeStringToOutput)
14+
```
15+
16+
## License
17+
18+
ISC

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function (blocking) {
2+
[process.stdout, process.stderr].forEach(function(stream){
3+
if (stream._handle && typeof stream._handle.setBlocking === 'function') {
4+
stream._handle.setBlocking(blocking)
5+
}
6+
})
7+
}

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "set-blocking",
3+
"version": "1.0.0",
4+
"description": "set blocking stdio and stderr ensuring that terminal output does not truncate",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "nyc mocha ./test/*.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/yargs/set-blocking.git"
12+
},
13+
"keywords": [
14+
"flush",
15+
"terminal",
16+
"blocking",
17+
"stdio",
18+
"stderr"
19+
],
20+
"author": "Ben Coe <[email protected]>",
21+
"license": "ISC",
22+
"bugs": {
23+
"url": "https://github.com/yargs/set-blocking/issues"
24+
},
25+
"homepage": "https://github.com/yargs/set-blocking#readme",
26+
"devDependencies": {
27+
"chai": "^3.5.0",
28+
"mocha": "^2.4.5",
29+
"nyc": "^6.4.4",
30+
"standard": "^7.0.1",
31+
"standard-version": "^2.2.1"
32+
},
33+
"files": [
34+
"index.js",
35+
"LICENSE.txt"
36+
]
37+
}

test/fixtures/yargs-497-stderr.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
3+
// see: https://github.com/yargs/yargs/issues/497
4+
var buffer = ''
5+
for (var i = 0; i < 3000; i++) {
6+
buffer += 'line ' + i + '\n'
7+
}
8+
9+
var setBlocking = require('../../')
10+
setBlocking(true)
11+
12+
console.error(buffer)
13+
process.exit(1)
14+
15+
console.log('should not execute')

test/fixtures/yargs-497-stdout.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env node
2+
3+
// see: https://github.com/yargs/yargs/issues/497
4+
var buffer = ''
5+
for (var i = 0; i < 3000; i++) {
6+
buffer += 'line ' + i + '\n'
7+
}
8+
9+
var setBlocking = require('../../')
10+
setBlocking(true)
11+
12+
console.log(buffer)
13+
process.exit(0)
14+
15+
console.log('should not execute')

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* global describe, should */
2+
3+
var exec = require('child_process').exec
4+
var setBlocking = require('../')
5+
6+
require('chai').should()
7+
8+
describe('setBlocking', function () {
9+
// see: https://github.com/yargs/yargs/issues/497
10+
it('does not truncate text printed to stdout when process.exit() is called', function (done) {
11+
exec('./test/fixtures/yargs-497-stdout.js', function (err, stdout, stderr) {
12+
if (err) return done(err)
13+
stdout.should.match(/line 2999/)
14+
return done()
15+
})
16+
})
17+
18+
it('does not truncate text printed to stderr when process.exit() is called', function (done) {
19+
exec('./test/fixtures/yargs-497-stderr.js', function (err, stdout, stderr) {
20+
stderr.should.match(/line 2999/)
21+
return done()
22+
})
23+
})
24+
})

0 commit comments

Comments
 (0)