[Pkg-javascript-commits] [node-get-stdin] 02/11: Imported Upstream version 5.0.1
Jonathan Horn
jonathanh-guest at moszumanska.debian.org
Thu Jun 2 18:50:43 UTC 2016
This is an automated email from the git hooks/post-receive script.
jonathanh-guest pushed a commit to branch master
in repository node-get-stdin.
commit ebdb4d5dce2954f8277f7e666f73840b420109f3
Author: Jonathan Ulrich Horn <debian at autoit4you.de>
Date: Thu Jun 2 19:20:26 2016 +0200
Imported Upstream version 5.0.1
---
.editorconfig | 2 +-
.jshintrc | 13 ------------
.travis.yml | 4 +++-
index.js | 63 ++++++++++++++++++++++++++++++----------------------------
package.json | 22 ++++++++++++--------
readme.md | 29 ++++++++++++++++++---------
test-buffer.js | 27 -------------------------
test-real.js | 7 ++-----
test.js | 41 ++++++++++++++++++++++++++------------
9 files changed, 101 insertions(+), 107 deletions(-)
diff --git a/.editorconfig b/.editorconfig
index 86c8f59..8f9d77e 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -7,7 +7,7 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
-[package.json]
+[{package.json,*.yml}]
indent_style = space
indent_size = 2
diff --git a/.jshintrc b/.jshintrc
deleted file mode 100644
index 804f8af..0000000
--- a/.jshintrc
+++ /dev/null
@@ -1,13 +0,0 @@
-{
- "node": true,
- "esnext": true,
- "bitwise": true,
- "camelcase": true,
- "curly": true,
- "immed": true,
- "newcap": true,
- "noarg": true,
- "undef": true,
- "unused": "vars",
- "strict": true
-}
diff --git a/.travis.yml b/.travis.yml
index 244b7e8..aea1274 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,3 +1,5 @@
+sudo: false
language: node_js
node_js:
- - '0.10'
+ - 'stable'
+ - '0.12'
diff --git a/index.js b/index.js
index 0f1aeb3..2083294 100644
--- a/index.js
+++ b/index.js
@@ -1,49 +1,52 @@
'use strict';
+var stdin = process.stdin;
-module.exports = function (cb) {
- var stdin = process.stdin;
+module.exports = function () {
var ret = '';
- if (stdin.isTTY) {
- setImmediate(cb, '');
- return;
- }
+ return new Promise(function (resolve) {
+ if (stdin.isTTY) {
+ resolve(ret);
+ return;
+ }
- stdin.setEncoding('utf8');
+ stdin.setEncoding('utf8');
- stdin.on('readable', function () {
- var chunk;
+ stdin.on('readable', function () {
+ var chunk;
- while (chunk = stdin.read()) {
- ret += chunk;
- }
- });
+ while ((chunk = stdin.read())) {
+ ret += chunk;
+ }
+ });
- stdin.on('end', function () {
- cb(ret);
+ stdin.on('end', function () {
+ resolve(ret);
+ });
});
};
-module.exports.buffer = function (cb) {
- var stdin = process.stdin;
+module.exports.buffer = function () {
var ret = [];
var len = 0;
- if (stdin.isTTY) {
- setImmediate(cb, new Buffer(''));
- return;
- }
+ return new Promise(function (resolve) {
+ if (stdin.isTTY) {
+ resolve(new Buffer(''));
+ return;
+ }
- stdin.on('readable', function () {
- var chunk;
+ stdin.on('readable', function () {
+ var chunk;
- while (chunk = stdin.read()) {
- ret.push(chunk);
- len += chunk.length;
- }
- });
+ while ((chunk = stdin.read())) {
+ ret.push(chunk);
+ len += chunk.length;
+ }
+ });
- stdin.on('end', function () {
- cb(Buffer.concat(ret, len));
+ stdin.on('end', function () {
+ resolve(Buffer.concat(ret, len));
+ });
});
};
diff --git a/package.json b/package.json
index 9a3e42a..df7cb59 100644
--- a/package.json
+++ b/package.json
@@ -1,19 +1,19 @@
{
"name": "get-stdin",
- "version": "4.0.1",
- "description": "Easier stdin",
+ "version": "5.0.1",
+ "description": "Get stdin as a string or buffer",
"license": "MIT",
"repository": "sindresorhus/get-stdin",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus at gmail.com",
- "url": "http://sindresorhus.com"
+ "url": "sindresorhus.com"
},
"engines": {
- "node": ">=0.10.0"
+ "node": ">=0.12.0"
},
"scripts": {
- "test": "node test.js && node test-buffer.js && echo unicorns | node test-real.js"
+ "test": "xo && ava test.js && echo unicorns | ava test-real.js"
},
"files": [
"index.js"
@@ -26,10 +26,16 @@
"buffer",
"stream",
"process",
- "stream"
+ "read"
],
"devDependencies": {
- "ava": "0.0.4",
- "buffer-equal": "0.0.1"
+ "ava": "*",
+ "buffer-equals": "^1.0.3",
+ "xo": "*"
+ },
+ "xo": {
+ "ignores": [
+ "test.js"
+ ]
}
}
diff --git a/readme.md b/readme.md
index bc1d32a..95fcbaa 100644
--- a/readme.md
+++ b/readme.md
@@ -1,11 +1,11 @@
# get-stdin [![Build Status](https://travis-ci.org/sindresorhus/get-stdin.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stdin)
-> Easier stdin
+> Get [stdin](https://nodejs.org/api/process.html#process_process_stdin) as a string or buffer
## Install
-```sh
+```
$ npm install --save get-stdin
```
@@ -14,15 +14,15 @@ $ npm install --save get-stdin
```js
// example.js
-var stdin = require('get-stdin');
+const getStdin = require('get-stdin');
-stdin(function (data) {
- console.log(data);
- //=> unicorns
+getStdin().then(str => {
+ console.log(str);
+ //=> 'unicorns'
});
```
-```sh
+```
$ echo unicorns | node example.js
unicorns
```
@@ -30,14 +30,25 @@ unicorns
## API
-### stdin(callback)
+Both methods returns a promise that is resolved when the `end` event fires on the `stdin` stream, indicating that there is no more data to be read.
+
+### getStdin()
Get `stdin` as a string.
-### stdin.buffer(callback)
+In a TTY context, a promise that resolves to an empty string is returned.
+
+### getStdin.buffer()
Get `stdin` as a buffer.
+In a TTY context, a promise that resolves to an empty buffer is returned.
+
+
+## Related
+
+- [get-stream](https://github.com/sindresorhus/get-stream) - Get a stream as a string or buffer
+
## License
diff --git a/test-buffer.js b/test-buffer.js
deleted file mode 100644
index 799e907..0000000
--- a/test-buffer.js
+++ /dev/null
@@ -1,27 +0,0 @@
-'use strict';
-var test = require('ava');
-var bufferEqual = require('buffer-equal');
-var stdin = require('./');
-
-test('get stdin as a buffer', function (t) {
- t.plan(2);
- process.stdin.isTTY = false;
-
- stdin.buffer(function (data) {
- t.assert(bufferEqual(data, new Buffer('unicorns')));
- t.assert(data.toString().trim() === 'unicorns');
- });
-
- process.stdin.push(new Buffer('unicorns'));
- process.stdin.emit('end');
-});
-
-test('get empty buffer when no stdin', function (t) {
- t.plan(1);
- process.stdin.isTTY = true;
-
- stdin.buffer(function (data) {
- t.assert(bufferEqual(data, new Buffer('')));
- });
-});
-
diff --git a/test-real.js b/test-real.js
index c6b63d3..6241fd5 100644
--- a/test-real.js
+++ b/test-real.js
@@ -1,6 +1,3 @@
-'use strict';
-var stdin = require('./');
+import fn from './';
-stdin(function (data) {
- process.exit(data ? 0 : 1);
-});
+fn().then(data => process.exit(data ? 0 : 1));
diff --git a/test.js b/test.js
index b2734f7..50b2557 100644
--- a/test.js
+++ b/test.js
@@ -1,24 +1,39 @@
-'use strict';
-var test = require('ava');
-var stdin = require('./');
+import test from 'ava';
+import bufferEquals from 'buffer-equals';
+import fn from './';
-test('get stdin', function (t) {
- t.plan(1);
+test.serial('get stdin', async t => {
process.stdin.isTTY = false;
- stdin(function (data) {
- t.assert(data.trim() === 'unicorns');
+ setImmediate(() => {
+ process.stdin.push('unicorns');
+ process.stdin.emit('end');
});
- process.stdin.push('unicorns');
- process.stdin.emit('end');
+ t.is((await fn()).trim(), 'unicorns');
});
-test('get empty string when no stdin', function (t) {
- t.plan(1);
+test.serial('get empty string when no stdin', async t => {
process.stdin.isTTY = true;
+ t.is(await fn(), '');
+});
+
+test.serial('get stdin as a buffer', t => {
+ process.stdin.isTTY = false;
- stdin(function (data) {
- t.assert(data === '');
+ const promise = fn.buffer(data => {
+ t.true(bufferEquals(data, new Buffer('unicorns')));
+ t.is(data.toString().trim(), 'unicorns');
});
+
+ process.stdin.push(new Buffer('unicorns'));
+ process.stdin.emit('end');
+
+ return promise;
+});
+
+test.serial('get empty buffer when no stdin', async t => {
+ process.stdin.isTTY = true;
+
+ t.true(bufferEquals(await fn.buffer(), new Buffer('')));
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-get-stdin.git
More information about the Pkg-javascript-commits
mailing list