Files
scriptc/tests/fixtures/event-loop/stdio-readable.mjs
T
Chris Tate fb3806ce42 Search TypedArrays, read stdin through 'readable', and assign constructor-function globals
- TypedArray indexOf, lastIndexOf and includes on every element kind, typed
  and checked-dynamic: strict equality (SameValueZero for includes) and
  fromIndex as ToIntegerOrInfinity. A Buffer reached through a Uint8Array
  value keeps Buffer's search.
- process.stdin 'readable' with read() and read(size), including sizes
  above the read-ahead mark and input a once('readable') listener leaves
  for a later 'data' listener. for await over process.stdin yields Buffers.
- Module bindings holding `new F()` of a constructor function can be read
  and reassigned from other functions.
2026-10-23 03:54:26 -05:00

20 lines
559 B
JavaScript

// process.stdin in paused mode through 'readable': each event drains the
// buffer with read(), one more event follows EOF, and 'end' comes after the
// read that finds the buffer empty.
const input = process.stdin;
const events = [];
let text = '';
input.on('readable', () => {
let reads = 0;
let chunk;
while ((chunk = input.read()) !== null) {
text += String(chunk);
reads++;
}
events.push(`readable ${reads}`);
});
input.on('end', () => {
console.log(events.join(', '));
console.log(JSON.stringify(text), input.readableLength);
});