Search code examples
javascriptnode.jsencodingstreamcommand-line-interface

How do I handle character encoding for stdout stream of (cli) output in/from node.js?


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)


Solution

  • stdout.setEncoding('utf8');
    

    Then you can safely:

    stdout.on('data', function(data) { console.log(data); });