From 972ed5784f6a4806c528a41ee6cbdc2a551bc129 Mon Sep 17 00:00:00 2001 From: Volodymyr Kolesnykov Date: Sat, 11 Jan 2025 07:32:56 +0200 Subject: [PATCH] fix(wp): handling of CR characters --- src/bin/vip-wp.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/bin/vip-wp.js b/src/bin/vip-wp.js index 3e6cd1f8b..a7845caca 100755 --- a/src/bin/vip-wp.js +++ b/src/bin/vip-wp.js @@ -6,7 +6,7 @@ import gql from 'graphql-tag'; import readline from 'readline'; import SocketIO from 'socket.io-client'; import IOStream from 'socket.io-stream'; -import { Writable } from 'stream'; +import { Transform, Writable } from 'stream'; import { WPCliCommandOverSSH } from '../commands/wp-ssh'; import API, { API_HOST, disableGlobalGraphQLErrorHandling } from '../lib/api'; @@ -43,14 +43,32 @@ const cancelCommandChar = '\x03'; let currentJob = null; let currentOffset = 0; let commandRunning = false; +const isStdinTty = process.stdin.isTTY; + +const normalizeNewlineStream = new Transform( { + transform( chunk, encoding, callback ) { + callback( null, chunk.toString().replace( /\r/g, '\n' ) ); + }, +} ); const pipeStreamsToProcess = ( { stdin, stdout: outStream } ) => { - process.stdin.pipe( stdin ); + if ( isStdinTty ) { + process.stdin.pipe( normalizeNewlineStream ).pipe( stdin ); + } else { + process.stdin.pipe( stdin ); + } + outStream.pipe( process.stdout ); }; const unpipeStreamsFromProcess = ( { stdin, stdout: outStream } ) => { - process.stdin.unpipe( stdin ); + if ( isStdinTty ) { + process.stdin.unpipe( normalizeNewlineStream ); + normalizeNewlineStream.unpipe( stdin ); + } else { + process.stdin.unpipe( stdin ); + } + outStream.unpipe( process.stdout ); }; @@ -394,6 +412,7 @@ commandWrapper( { terminal: true, prompt: '', historySize: 0, + crlfDelay: Infinity, }; if ( isSubShell ) { @@ -506,16 +525,6 @@ commandWrapper( { } ); } ); - // Fix to re-add the \n character that readline strips when terminal == true - process.stdin.on( 'data', data => { - // only run this in interactive mode for prompts from WP commands - if ( commandRunning && 0 === Buffer.compare( data, Buffer.from( '\r' ) ) ) { - if ( currentJob?.stdinStream ) { - currentJob.stdinStream.write( '\n' ); - } - } - } ); - subShellRl.on( 'SIGINT', async () => { // if we have a 2nd SIGINT, exit immediately if ( countSIGINT >= 1 ) {