From e95ccdd20623115bfd7d47df14a3250d6066a9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Di=C3=B3genes=20Fernandes?= Date: Wed, 17 Dec 2025 12:29:27 -0300 Subject: [PATCH] Streaming when using `tofu_wrapper` (#75) Signed-off-by: Diogenes Fernandes --- dist/index1.js | 15 +++++++-------- wrapper/lib/output-listener.js | 7 +++++-- wrapper/test/output-listener.test.js | 8 ++++++-- wrapper/tofu.js | 8 ++------ 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/dist/index1.js b/dist/index1.js index 13eba0d..1d72fb6 100755 --- a/dist/index1.js +++ b/dist/index1.js @@ -25899,7 +25899,8 @@ module.exports = { * * @example * // Instantiate a new listener - * const listener = new OutputListener(); + * // stream is used to write the data before waiting for the listener to complete + * const listener = new OutputListener(stream); * // Register listener against STDOUT stream * await exec.exec('ls', ['-ltr'], { * listeners: { @@ -25910,12 +25911,14 @@ module.exports = { * console.log(listener.contents); */ class OutputListener { - constructor () { + constructor (stream) { this._buff = []; + this._stream = stream; } get listener () { const listen = function listen (data) { + this._stream.write(data); this._buff.push(data); }; return listen.bind(this); @@ -27889,8 +27892,8 @@ async function checkTofu () { await checkTofu(); // Create listeners to receive output (in memory) as well - const stdout = new OutputListener(); - const stderr = new OutputListener(); + const stdout = new OutputListener(process.stdout); + const stderr = new OutputListener(process.stderr); const listeners = { stdout: stdout.listener, stderr: stderr.listener @@ -27905,10 +27908,6 @@ async function checkTofu () { }; const exitCode = await exec(pathToCLI, args, options); - // Pass-through stdout/err as `exec` won't due to `silent: true` option - process.stdout.write(stdout.contents); - process.stderr.write(stderr.contents); - // Set outputs, result, exitcode, and stderr core.setOutput('stdout', stdout.contents); core.setOutput('stderr', stderr.contents); diff --git a/wrapper/lib/output-listener.js b/wrapper/lib/output-listener.js index 9686dba..e77075e 100644 --- a/wrapper/lib/output-listener.js +++ b/wrapper/lib/output-listener.js @@ -9,7 +9,8 @@ * * @example * // Instantiate a new listener - * const listener = new OutputListener(); + * // stream is used to write the data before waiting for the listener to complete + * const listener = new OutputListener(stream); * // Register listener against STDOUT stream * await exec.exec('ls', ['-ltr'], { * listeners: { @@ -20,12 +21,14 @@ * console.log(listener.contents); */ class OutputListener { - constructor () { + constructor (stream) { this._buff = []; + this._stream = stream; } get listener () { const listen = function listen (data) { + this._stream.write(data); this._buff.push(data); }; return listen.bind(this); diff --git a/wrapper/test/output-listener.test.js b/wrapper/test/output-listener.test.js index 229c3cf..538c3e1 100644 --- a/wrapper/test/output-listener.test.js +++ b/wrapper/test/output-listener.test.js @@ -4,14 +4,18 @@ */ const OutputListener = require('../lib/output-listener'); - +const { PassThrough } = require('stream'); describe('output-listener', () => { it('receives and exposes data', () => { - const listener = new OutputListener(); + const stream = new PassThrough(); + const listener = new OutputListener(stream); const listen = listener.listener; listen(Buffer.from('foo')); + expect(stream.read()).toEqual(Buffer.from('foo')); listen(Buffer.from('bar')); + expect(stream.read()).toEqual(Buffer.from('bar')); listen(Buffer.from('baz')); + expect(stream.read()).toEqual(Buffer.from('baz')); expect(listener.contents).toEqual('foobarbaz'); }); }); diff --git a/wrapper/tofu.js b/wrapper/tofu.js index a3f690a..2a6be5e 100755 --- a/wrapper/tofu.js +++ b/wrapper/tofu.js @@ -22,8 +22,8 @@ async function checkTofu () { await checkTofu(); // Create listeners to receive output (in memory) as well - const stdout = new OutputListener(); - const stderr = new OutputListener(); + const stdout = new OutputListener(process.stdout); + const stderr = new OutputListener(process.stderr); const listeners = { stdout: stdout.listener, stderr: stderr.listener @@ -38,10 +38,6 @@ async function checkTofu () { }; const exitCode = await exec(pathToCLI, args, options); - // Pass-through stdout/err as `exec` won't due to `silent: true` option - process.stdout.write(stdout.contents); - process.stderr.write(stderr.contents); - // Set outputs, result, exitcode, and stderr core.setOutput('stdout', stdout.contents); core.setOutput('stderr', stderr.contents);