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 wele)
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 wele)
Share Improve this question asked Oct 30, 2013 at 22:18 BartvdsBartvds 3,5405 gold badges33 silver badges42 bronze badges1 Answer
Reset to default 7stdout.setEncoding('utf8');
Then you can safely:
stdout.on('data', function(data) { console.log(data); });