Ciro Santilli OurBigBook.com  Sponsor €¥ 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
nodejs/stdin_pipe_interactive.js
#!/usr/bin/env node

// https://stackoverflow.com/questions/30441025/read-all-text-from-stdin-to-a-string/54565854#54565854
// Works on both pipe and interactive input. Ctrl + C works and exits program.
// TODO requires Ctrl + D to be pressed twice if interactive input does not end in newline.

const fs = require('fs')

async function read(stream) {
  const chunks = [];
  for await (const chunk of stream) {
    chunks.push(chunk)
  }
  return Buffer.concat(chunks).toString('utf8');
}

(async () => {
const ret = await read(process.stdin)
console.log(`Read: ${typeof(ret)}\n${ret}`)
})()