From 4e106d45c37b47ae7a9260ae7c86711104322cb0 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Fri, 10 Mar 2017 10:43:09 -0800 Subject: [PATCH] packager: verify validity of TTY before using it Reviewed By: mkonicek Differential Revision: D4689779 fbshipit-source-id: 9bc2c1447bd64ec392adef772b1189a782f83545 --- packages/metro-bundler/src/lib/terminal.js | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/metro-bundler/src/lib/terminal.js b/packages/metro-bundler/src/lib/terminal.js index 531fcc5e..04043db5 100644 --- a/packages/metro-bundler/src/lib/terminal.js +++ b/packages/metro-bundler/src/lib/terminal.js @@ -41,6 +41,20 @@ function chunkString(str: string, size: number): Array { return str.match(new RegExp(`.{1,${size}}`, 'g')) || []; } +/** + * Get the stream as a TTY if it effectively looks like a valid TTY. + */ +function getTTYStream(stream: net$Socket): ?tty.WriteStream { + if ( + stream instanceof tty.WriteStream && + stream.isTTY && + stream.columns >= 1 + ) { + return stream; + } + return null; +} + /** * We don't just print things to the console, sometimes we also want to show * and update progress. This utility just ensures the output stays neat: no @@ -93,19 +107,20 @@ class Terminal { */ _update(): void { const {_statusStr, _stream} = this; + const ttyStream = getTTYStream(_stream); if (_statusStr === this._nextStatusStr && this._logLines.length === 0) { return; } - if (_stream instanceof tty.WriteStream) { - clearStringBackwards(_stream, _statusStr); + if (ttyStream != null) { + clearStringBackwards(ttyStream, _statusStr); } this._logLines.forEach(line => { _stream.write(line); _stream.write('\n'); }); this._logLines = []; - if (_stream instanceof tty.WriteStream) { - this._nextStatusStr = chunkString(this._nextStatusStr, _stream.columns).join('\n'); + if (ttyStream != null) { + this._nextStatusStr = chunkString(this._nextStatusStr, ttyStream.columns).join('\n'); _stream.write(this._nextStatusStr); } this._statusStr = this._nextStatusStr;