Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/get-stdin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.0.0
Choose a base ref
...
head repository: sindresorhus/get-stdin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v8.0.0
Choose a head ref
  • 6 commits
  • 7 files changed
  • 3 contributors

Commits on Apr 27, 2019

  1. Add Node.js 12 to testing (#28)

    coreyfarrell authored and sindresorhus committed Apr 27, 2019
    Copy the full SHA
    8a89e1c View commit details

Commits on May 28, 2019

  1. Create funding.yml

    sindresorhus authored May 28, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f78b784 View commit details

Commits on May 31, 2019

  1. Tidelift tasks

    sindresorhus committed May 31, 2019
    Copy the full SHA
    99da4b5 View commit details

Commits on May 12, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    96ebc2e View commit details
  2. Meta tweaks

    sindresorhus committed May 12, 2020
    Copy the full SHA
    541a2b2 View commit details
  3. 8.0.0

    sindresorhus committed May 12, 2020
    Copy the full SHA
    07288e1 View commit details
Showing with 49 additions and 55 deletions.
  1. +4 −0 .github/funding.yml
  2. +3 −0 .github/security.md
  3. +2 −1 .travis.yml
  4. +20 −38 index.js
  5. +1 −1 license
  6. +7 −6 package.json
  7. +12 −9 readme.md
4 changes: 4 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github: sindresorhus
open_collective: sindresorhus
tidelift: npm/get-stdin
custom: https://sindresorhus.com/donate
3 changes: 3 additions & 0 deletions .github/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
58 changes: 20 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,34 @@
'use strict';
const {stdin} = process;

module.exports = () => {
module.exports = async () => {
let result = '';

return new Promise(resolve => {
if (stdin.isTTY) {
resolve(result);
return;
}
if (stdin.isTTY) {
return result;
}

stdin.setEncoding('utf8');
stdin.setEncoding('utf8');

stdin.on('readable', () => {
let chunk;
for await (const chunk of stdin) {
result += chunk;
}

while ((chunk = stdin.read())) {
result += chunk;
}
});

stdin.on('end', () => {
resolve(result);
});
});
return result;
};

module.exports.buffer = () => {
module.exports.buffer = async () => {
const result = [];
let length = 0;

return new Promise(resolve => {
if (stdin.isTTY) {
resolve(Buffer.concat([]));
return;
}

stdin.on('readable', () => {
let chunk;

while ((chunk = stdin.read())) {
result.push(chunk);
length += chunk.length;
}
});

stdin.on('end', () => {
resolve(Buffer.concat(result, length));
});
});
if (stdin.isTTY) {
return Buffer.concat([]);
}

for await (const chunk of stdin) {
result.push(chunk);
length += chunk.length;
}

return Buffer.concat(result, length);
};
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

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:

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "get-stdin",
"version": "7.0.0",
"version": "8.0.0",
"description": "Get stdin as a string or buffer",
"license": "MIT",
"repository": "sindresorhus/get-stdin",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava test.js test-buffer.js && echo unicorns | node test-real.js && tsd"
@@ -30,10 +31,10 @@
"read"
],
"devDependencies": {
"@types/node": "^11.13.4",
"ava": "^1.4.1",
"@types/node": "^13.13.5",
"ava": "^2.4.0",
"delay": "^4.2.0",
"tsd": "^0.7.2",
"tsd": "^0.11.0",
"xo": "^0.24.0"
}
}
21 changes: 12 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# get-stdin [![Build Status](https://travis-ci.org/sindresorhus/get-stdin.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stdin)
# get-stdin [![Build Status](https://travis-ci.com/sindresorhus/get-stdin.svg?branch=master)](https://travis-ci.com/sindresorhus/get-stdin)

> Get [stdin](https://nodejs.org/api/process.html#process_process_stdin) as a string or buffer

## Install

```
$ npm install get-stdin
```


## Usage

```js
@@ -27,7 +25,6 @@ $ echo unicorns | node example.js
unicorns
```


## API

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.
@@ -44,12 +41,18 @@ 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

MIT © [Sindre Sorhus](https://sindresorhus.com)
---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-get-stdin?utm_source=npm-get-stdin&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>