Giter Site home page Giter Site logo

get-stdin's Introduction

get-stdin

Get stdin as a string or buffer

Install

npm install get-stdin

Usage

// example.js
import getStdin from 'get-stdin';

console.log(await getStdin());
//=> 'unicorns'
$ 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.

getStdin()

Get stdin as a string.

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.

Tip

You can now accomplish this natively in Node.js using streamConsumers.text() or streamConsumers.buffer():

// example.js
import {text} from 'node:stream/consumers';

console.log(await text(stream))
//=> 'unicorns'
$ echo unicorns | node example.js
unicorns

Related

get-stdin's People

Contributors

bendingbender avatar chalker avatar coreyfarrell avatar ethan-arrowood avatar fisker avatar javiercejudo avatar kevva avatar lukechilds avatar markstos avatar ntwb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

get-stdin's Issues

Unexpected reserved word error when building in CI

node_modules/stylelint/node_modules/get-stdin/index.js:13
for await (const chunk of stdin) {

SyntaxError: Unexpected reserved word

I've just started getting the following error in my Jenkins builds. Unsure why this has just become an issue.

[REGRESSION] `get-stdin` can no longer be `require`d

My scripts now throw:

let getStdin = require('get-stdin');
node:internal/modules/cjs/loader:1126
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/local/lib/node_modules/get-stdin/index.js
require() of ES modules is not supported.
require() of /usr/local/lib/node_modules/get-stdin/index.js from /Users/evar/scripts/javascript/readtime.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/local/lib/node_modules/get-stdin/package.json.

    at new NodeError (node:internal/errors:363:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1126:13)
    at Module.load (node:internal/modules/cjs/loader:989:32)
    at Function.Module._load (node:internal/modules/cjs/loader:829:14)
    at Module.require (node:internal/modules/cjs/loader:1013:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (/Users/evar/scripts/javascript/readtime.js:4:16)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
    at Module.load (node:internal/modules/cjs/loader:989:32) {
  code: 'ERR_REQUIRE_ESM'
}

When executable is spawned from node if there is no stdin getStdin() never resolves

If there is no file option set my CLI app falls back to getStdin().

I check if the result of the file read or getStdin() is falsey and if so I show the help message.

This works fine when used for real in a shell but when I try to write a test for it, it just hangs. It seems if I spawn my CLI app from node, get-stdin() returns a promise that never resolves. For example:

$ node dist/cli.js

  Usage: htconvert [options]

  Options:

    -h, --help              output usage information
    -V, --version           output the version number
    -f, --file [.htaccess]  File containing .htaccess redirects

But if I run this from node:

var exec = require('child_process').exec;
exec('node dist/cli.js').stdout.pipe(process.stdout);

I get nothing.

Not sure if possibly related to #1. Is this expected behaviour or a bug?

Console returning error "stdin is not a tty" when running example

I'm trying to use this package for some code I am writing. I have installed it with npm. I have some code identical to the example on the readme.

const getStdin = require('get-stdin');

(async () => {
  console.log(await getStdin());
})();

However, when I run echo unicorn | node example.js, I am getting the error stdin is not a tty. What can I do to fix this?

Node version: 10.15.3
Git Bash: 4.4.23(1)
PC: Windows x86_64

Terminal like interface

Does there exists a terminal like interface for taking input .
As in,

read -s

in terminal doesnt show input while entering .

"Implement me. Unknown stdin file type!" error thrown when running headless process in win32

Hi there - I encountered this while attempting to run PostCSS commands via the PostCSS-CLI command line implementation.

The exact error is as shown:

internal/process/stdio.js:82

        throw new Error('Implement me. Unknown stdin file type!');
        ^
    Error: Implement me. Unknown stdin file type!
    at process.getStdin [as stdin] (internal/process/stdio.js:82:15)
    at Object.<anonymous> (C:\Users\myusername\AppData\Local\Microsoft\VisualStudio\14.0\Extensions\5xs3acz0.hfk\Resources\nodejs\node_modules\get-stdin\index.js:2:20)

    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)

    at Object.<anonymous> (C:\Users\myusername\AppData\Local\Microsoft\VisualStudio\14.0\Extensions\5xs3acz0.hfk\Resources\nodejs\node_modules\postcss-cli\index.js:6:15)

I found a similar issue in Angular here: angular/angular-cli#4870

They solved it like this:
angular/angular-cli@4af7a42

Let me know if you need more information - I am very new to Node so you might need to simplify any questions you have for me.

Reading JSON File from stdin

How do i read JSON file by using this package. I am using following command to get stdin but using this package it does-not work.
cat airlines/british_airways/example_input.json | tsc && node dist/index.js

Break line present at the end

I'm using get-sdtdin at regexgen-cli and I noticed that an \n is always present at the final of the input:

getStdin().then(stdin => {
  console.log(stdin)
  console.log([].concat(stdin))
})
echo hola | regexgen
hola

[ 'hola\n' ]

Allow setting encoding

This is useful for getting binary data through stdin. We can fetch everything as Buffer and then convert it to a string if no other encoding is specified.

Add note about `node:stream/consumers`

Added in v16.7.0, there's now a node:stream/consumers consumers.text(stream) function that returns a promise similar to response.text() from the fetch() API.

https://nodejs.org/api/webstreams.html#utility-consumers

ex:

echo hi | node -e 'require("node:stream/consumers").text(process.stdin).then(console.log)'

I think it would be a good idea to add a note (not necessarily a recommendation one way or the other; just a a note) to inform users of this alternative native Node.js way to get stdin as a string.

This is just an idea and you may not want to add this and instead keep the readme very deliberately bare. idk. This is just a suggestion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.