如何处理node.js中(cli)输出的(cli)stdout流的字符编码? [英] How do I handle character encoding for stdout stream of (cli) output in/from node.js?

查看:177
本文介绍了如何处理node.js中(cli)输出的(cli)stdout流的字符编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于如何安全地存储和读取Node.js中的 process.stdout 输出感到困惑:



console.log()(等等)的CLI输出是否以特定的字符编码完成?还是未指定格式的原始二进制文件?可以有二进制数据吗? (我不知道)



Node.js是非常面向utf8的,但是JS是UCS2,我不知道该流如何处理它。



和相关:如果我在utf8中将Buffer转换为String(默认值),对流应用string-diff是否安全?请注意,我的差异渲染器将使用 jsenc 进行显示,以便显示不可打印的内容。



用例是希望能够安全地声明/区分CLI快照以验证为各种工具构建的自定义报告器(请注意,这包括发现意外的垃圾/棉绒输出,因此我想从stdio流中获取真正的最终输出)。 / p>

(欢迎提供任何相关建议)

解决方案


stdout.setEncoding('utf8');



然后您可以放心:

stdout.on('数据,函数(数据){console.log(data);});


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)

解决方案

stdout.setEncoding('utf8');

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

这篇关于如何处理node.js中(cli)输出的(cli)stdout流的字符编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆