| 47 | // Returns true on timeout, false on end. Used by -p mode to distinguish a |
| 48 | // real pipe producer from an inherited-but-idle parent stdin. |
| 49 | export function peekForStdinData( |
| 50 | stream: NodeJS.EventEmitter, |
| 51 | ms: number, |
| 52 | ): Promise<boolean> { |
| 53 | return new Promise<boolean>(resolve => { |
| 54 | const done = (timedOut: boolean) => { |
| 55 | clearTimeout(peek) |
| 56 | stream.off('end', onEnd) |
| 57 | stream.off('data', onFirstData) |
| 58 | void resolve(timedOut) |
| 59 | } |
| 60 | const onEnd = () => done(false) |
| 61 | const onFirstData = () => clearTimeout(peek) |
| 62 | // eslint-disable-next-line no-restricted-syntax -- not a sleep: races timeout against stream end/data events |
| 63 | const peek = setTimeout(done, ms, true) |
| 64 | stream.once('end', onEnd) |
| 65 | stream.once('data', onFirstData) |
| 66 | }) |
| 67 | } |