|
| 1 | +import { EventEmitter } from 'events'; |
| 2 | +import { StringDecoder } from 'string_decoder'; |
| 3 | + |
| 4 | +const LINE_ENDING_RE = /\r?\n|\r(?!\n)/; |
| 5 | +const CTRL_C = '\u0003'; |
| 6 | +const CTRL_D = '\u0004'; |
| 7 | + |
| 8 | +/** |
| 9 | + * A proxy for `tty.ReadStream` that allows to read |
| 10 | + * the stream line by line. |
| 11 | + * |
| 12 | + * Each time a newline is encountered the stream wont emit further data |
| 13 | + * untill `.nextLine()` is called. |
| 14 | + * |
| 15 | + * NOTE: the control sequences Ctrl+C and Ctrl+D are not buffered and instead |
| 16 | + * are forwarded regardless. |
| 17 | + * |
| 18 | + * Is possible to disable the "line splitting" by calling `.disableBlockOnNewline()` and |
| 19 | + * re-enable it by calling `.enableBlockOnNewLine()`. |
| 20 | + * |
| 21 | + * If the line splitting is disabled the stream will behave like |
| 22 | + * the proxied `tty.ReadStream`, forwarding all the characters. |
| 23 | + */ |
| 24 | +export class LineByLineInput { |
| 25 | + private _emitter: EventEmitter; |
| 26 | + private _originalInput: NodeJS.ReadStream; |
| 27 | + private _forwarding: boolean; |
| 28 | + private _blockOnNewLineEnabled: boolean; |
| 29 | + private _charQueue: string[]; |
| 30 | + private _decoder: StringDecoder; |
| 31 | + |
| 32 | + constructor(readable: NodeJS.ReadStream) { |
| 33 | + this._emitter = new EventEmitter(); |
| 34 | + this._originalInput = readable; |
| 35 | + this._forwarding = true; |
| 36 | + this._blockOnNewLineEnabled = true; |
| 37 | + this._charQueue = []; |
| 38 | + this._decoder = new StringDecoder('utf-8'); |
| 39 | + |
| 40 | + readable.on('data', this._onData); |
| 41 | + |
| 42 | + const proxy = new Proxy(readable, { |
| 43 | + get: (target: NodeJS.ReadStream, property: string): any => { |
| 44 | + if (typeof property === 'string' && |
| 45 | + !property.startsWith('_') && |
| 46 | + typeof this[property] === 'function' |
| 47 | + ) { |
| 48 | + return this[property].bind(this); |
| 49 | + } |
| 50 | + |
| 51 | + return target[property]; |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + return (proxy as unknown) as LineByLineInput; |
| 56 | + } |
| 57 | + |
| 58 | + on(event: string, handler: (...args: any[]) => void): void { |
| 59 | + if (event === 'data') { |
| 60 | + this._emitter.on('data', handler); |
| 61 | + // we may have buffered data for the first listener |
| 62 | + this._flush(); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + this._originalInput.on(event, handler); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + nextLine(): void { |
| 71 | + this._resumeForwarding(); |
| 72 | + this._flush(); |
| 73 | + } |
| 74 | + |
| 75 | + enableBlockOnNewLine(): void { |
| 76 | + this._blockOnNewLineEnabled = true; |
| 77 | + } |
| 78 | + |
| 79 | + disableBlockOnNewline(): void { |
| 80 | + this._blockOnNewLineEnabled = false; |
| 81 | + this._flush(); |
| 82 | + } |
| 83 | + |
| 84 | + private _onData = (chunk: Buffer): void => { |
| 85 | + if (this._blockOnNewLineEnabled) { |
| 86 | + return this._forwardAndBlockOnNewline(chunk); |
| 87 | + } |
| 88 | + |
| 89 | + return this._forwardWithoutBlocking(chunk); |
| 90 | + }; |
| 91 | + |
| 92 | + private _forwardAndBlockOnNewline(chunk: Buffer): void { |
| 93 | + const chars = this._decoder.write(chunk); |
| 94 | + for (const char of chars) { |
| 95 | + if (this._isCtrlC(char) || this._isCtrlD(char)) { |
| 96 | + this._emitChar(char); |
| 97 | + } else { |
| 98 | + this._charQueue.push(char); |
| 99 | + } |
| 100 | + } |
| 101 | + this._flush(); |
| 102 | + } |
| 103 | + |
| 104 | + private _forwardWithoutBlocking(chunk: Buffer): void { |
| 105 | + // keeps decoding state consistent |
| 106 | + this._decoder.write(chunk); |
| 107 | + this._emitChunk(chunk); |
| 108 | + } |
| 109 | + |
| 110 | + private _pauseForwarding(): void { |
| 111 | + this._forwarding = false; |
| 112 | + } |
| 113 | + |
| 114 | + private _resumeForwarding(): void { |
| 115 | + this._forwarding = true; |
| 116 | + } |
| 117 | + |
| 118 | + private _shouldForward(): boolean { |
| 119 | + // If we are not blocking on new lines |
| 120 | + // we just forward everything as is, |
| 121 | + // otherwise we forward only if the forwarding |
| 122 | + // is not paused. |
| 123 | + |
| 124 | + return !this._blockOnNewLineEnabled || this._forwarding; |
| 125 | + } |
| 126 | + |
| 127 | + private _emitChar(char): void { |
| 128 | + this._emitChunk(Buffer.from(char, 'utf8')); |
| 129 | + } |
| 130 | + |
| 131 | + private _emitChunk(chunk: Buffer): void { |
| 132 | + this._emitter.emit('data', chunk); |
| 133 | + } |
| 134 | + |
| 135 | + private _flush(): void { |
| 136 | + // there is nobody to flush for |
| 137 | + if (this._emitter.listenerCount('data') === 0) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + while ( |
| 142 | + this._charQueue.length && |
| 143 | + this._shouldForward() && |
| 144 | + |
| 145 | + // We don't forward residual characters we could |
| 146 | + // have in the buffer if in the meanwhile something |
| 147 | + // downstream explicitly called pause(), as that may cause |
| 148 | + // unexpected behaviors. |
| 149 | + !this._originalInput.isPaused() |
| 150 | + ) { |
| 151 | + const char = this._charQueue.shift(); |
| 152 | + |
| 153 | + if (this._isLineEnding(char)) { |
| 154 | + this._pauseForwarding(); |
| 155 | + } |
| 156 | + |
| 157 | + this._emitChar(char); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private _isLineEnding(char: string): boolean { |
| 162 | + return LINE_ENDING_RE.test(char); |
| 163 | + } |
| 164 | + |
| 165 | + private _isCtrlD(char: string): boolean { |
| 166 | + return char === CTRL_D; |
| 167 | + } |
| 168 | + |
| 169 | + private _isCtrlC(char: string): boolean { |
| 170 | + return char === CTRL_C; |
| 171 | + } |
| 172 | +} |
0 commit comments