4

I am confused about how to safely store and read the process.stdout output in Node.js:

Is the CLI output of console.log() (and such) done in a specific character encoding? Or is it raw binary of unspecified form? Can there be binary data? (I have no idea)

Node.js is very utf8 oriented, but then JS is UCS2 and I have no idea what the stream does with it.

And related: is it safe to apply a string-diff to the stream if I convert the Buffer to String in utf8 (the default)? Note my diff renderer will use jsenc for display so it shows non-printables.

The use case is that I want to be able to safely assert/diff CLI snapshots to verify my custom reporters I build for various tools (note this includes spotting un-expected trash/lint output, so I want to tap the true final output from the stdio stream).

(any related advise is welcome)

1 Answer 1

7
stdout.setEncoding('utf8');

Then you can safely:

stdout.on('data', function(data) { console.log(data); });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.