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

☆樱花仙子☆ 提交于 2020-04-13 17:36:52

问题


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:


stdout.setEncoding('utf8');

Then you can safely: stdout.on('data', function(data) { console.log(data); });



来源:https://stackoverflow.com/questions/19694901/how-do-i-handle-character-encoding-for-stdout-stream-of-cli-output-in-from-nod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!