From 9476a81a5fc5523e4e17ed84e88c4c6aa6f7849d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 20 Apr 2024 19:11:38 +0200 Subject: [PATCH 1/5] :new: Add validation for noTrim option --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index 338c965..0276ae1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -369,6 +369,7 @@ export class NodeSSH { options.onStderr == null || typeof options.onStderr === 'function', 'options.onStderr must be a valid function', ) + invariant(options.noTrim == null || typeof options.noTrim === 'boolean', 'options.noTrim must be a boolean') let command = givenCommand From dddc67157a59c687bba5c9030697edba36e392c7 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 20 Apr 2024 19:11:47 +0200 Subject: [PATCH 2/5] :shirt: Minor style fix --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0276ae1..96dc3bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -429,8 +429,8 @@ export class NodeSSH { resolve({ code: code != null ? code : null, signal: signal != null ? signal : null, - stdout: stdout, - stderr: stderr, + stdout, + stderr, }) }) }) From a083c3612a0e0d413d17d07af163433a20a4ee6d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 20 Apr 2024 19:13:13 +0200 Subject: [PATCH 3/5] :memo: Document noTrim option in docs --- CHANGELOG.md | 4 ++++ README.md | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3f6962..bd1f53b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +#### Upcoming + +- Add `noTrim` option to exec methods for cases where you do not want node-ssh to automatically trim the outputs. + #### 13.1.0 - Add new methods around sockets forwarding. #460 (Thanks @mat-sz) diff --git a/README.md b/README.md index a41e81d..d71a1b9 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ export interface SSHExecCommandOptions { stdin?: string | stream.Readable execOptions?: ExecOptions encoding?: BufferEncoding + noTrim?: boolean onChannel?: (clientChannel: ClientChannel) => void onStdout?: (chunk: Buffer) => void onStderr?: (chunk: Buffer) => void From aaf404b3c36be75430295f77b83127bab0a28fb2 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 20 Apr 2024 19:17:41 +0200 Subject: [PATCH 4/5] :new: Add tests for new behavior --- test/main-test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/main-test.ts b/test/main-test.ts index 56f91e6..0ee849b 100644 --- a/test/main-test.ts +++ b/test/main-test.ts @@ -494,3 +494,11 @@ sshit('forwards an inbound TCP/IP connection to client with automatically assign connectWithPassword(port, client) }) }) +sshit('has a working noTrim option', async function (t, port, client) { + await connectWithPassword(port, client) + const resultWithTrim = await client.exec('printf', ["\n", "hello", "\n\n\n\n"], {stream: 'stdout'}) + t.is(resultWithTrim, 'hello') + + const resultWithoutTrim = await client.exec('printf', ['\n\n\nhi\n\n\n'], {stream: 'stdout', noTrim: true}) + t.is(resultWithoutTrim, '\n\n\nhi\n\n\n') +}) From 8b49f1a0436112810f9674464fb0a445ac70b33b Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 20 Apr 2024 19:21:28 +0200 Subject: [PATCH 5/5] :bug: Fix tests for noTrim --- test/main-test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/main-test.ts b/test/main-test.ts index 0ee849b..34adc55 100644 --- a/test/main-test.ts +++ b/test/main-test.ts @@ -496,9 +496,9 @@ sshit('forwards an inbound TCP/IP connection to client with automatically assign }) sshit('has a working noTrim option', async function (t, port, client) { await connectWithPassword(port, client) - const resultWithTrim = await client.exec('printf', ["\n", "hello", "\n\n\n\n"], {stream: 'stdout'}) + const resultWithTrim = await client.exec('echo', ["\nhello\n\n\n\n"], {stream: 'stdout'}) t.is(resultWithTrim, 'hello') - const resultWithoutTrim = await client.exec('printf', ['\n\n\nhi\n\n\n'], {stream: 'stdout', noTrim: true}) - t.is(resultWithoutTrim, '\n\n\nhi\n\n\n') + const resultWithoutTrim = await client.exec('echo', ['\n\n\nhi\n\n\n'], {stream: 'stdout', noTrim: true}) + t.is(resultWithoutTrim, '\n\n\nhi\n\n\n\n') })