index.js 497 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const {stdin} = process;
  3. module.exports = async () => {
  4. let result = '';
  5. if (stdin.isTTY) {
  6. return result;
  7. }
  8. stdin.setEncoding('utf8');
  9. for await (const chunk of stdin) {
  10. result += chunk;
  11. }
  12. return result;
  13. };
  14. module.exports.buffer = async () => {
  15. const result = [];
  16. let length = 0;
  17. if (stdin.isTTY) {
  18. return Buffer.concat([]);
  19. }
  20. for await (const chunk of stdin) {
  21. result.push(chunk);
  22. length += chunk.length;
  23. }
  24. return Buffer.concat(result, length);
  25. };