STDIN 上的 readFileSync 与 node.js 中的 readFile - 字符编码?

readFileSync on STDIN vs readFile in node.js - char encoding?

我正在尝试从 STDIN 读取文件,用 paper.js 处理它并 return 结果。

为了到达那里,我首先尝试通过像这样从 fs 读取文件来做到这一点:

const Project = require('paper').Project
var fs = require('fs');
let p = new Project() 

fs.readFile( process.argv[2], function (err, data) {
    if (err) { throw err; }
    
    p.importSVG(data.toString(), { onError: function(e){ console.log(e) } })
    console.log(p.exportSVG({ asString: true }));
});

效果很好。

然后我尝试从 STDIN 获取数据(cat foo_02.svg | node swiss.js - 是的,毫无意义地使用 cat,但仅作为示例),如下所示:

const Project = require('paper').Project
var fs = require('fs');
let p = new Project() 

const data = fs.readFileSync(process.stdin.fd).toString();

p.importSVG(data, { onError: function(e){ console.log(e) } })
console.log(p.exportSVG({ asString: true }));

这似乎破坏了数据。

a console.log(data) 添加在显示这个(截断):

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0"
viewBox="0,0,0,0">
  <defs>
    <clipPath id="clip-1">
      <rect x="0" y="0" width="2074" height="748" fill="none">
      </rect>
    </clipPath>
  </defs>
  <g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1"
  stroke-linecap="butt" stroke-linejoin="miter"
  stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0"
  font-family="none" font-weight="none" font-size="none"
  text-anchor="none" style="mix-blend-mode: normal">
    <g clip-path="url(#clip-1)">
      <g fill="#f98673" font-family="sans-serif"
      font-weight="normal" font-size="12" text-anchor="start"></g>
      <path d="M1042.7,104.3c-1.6,1.3 -3.3,9.5 -2.8,14.1c0.3,3.6 4.7,10.3 7.2,11.1c1,0.3 3.6,2.7 5.8,5.3c9,10.7 12.1,14.7 12.1,15.9c0,0.7 0.6,1.6 1.3,2c0.8,0.4 2,2 2.8,3.4c2.3,4.2 7,11.3 8.3,12.4c0.7,0.5 1.8,1.9 2.6,3.1c2.1,2.9 10,12.3 12.8,14.9c1.2,1.2 2.2,2.7 2.2,3.2c0,0.5 3.6,4.4 8,8.8c4.4,4.3 9.9,10.5 12.1,13.8c4.5,6.6 14,18.7 17.9,22.8c2.8,2.9 3.9,4.2 12.3,14.7c3.1,4 8.2,10.3 11.2,14.1c3,3.7 5.5,7.3 5.5,7.8c0,0.5 0.8,1.8 1.8,2.9c0.9,1 3.1,3.8 4.9,6.2c3.1,4.4 11.4,13.8 18.8,21.7c6.4,6.7 9.3,10.4 14.4,17.9c4.2,6.3 9.1,15.6 9.1,17.2c0,0.3 1.4,2.5 3,4.9c3.7,5.4 3.7,6.7 0.3,11.3c-3.8,5 -21.8,27.1 -24.7,30.2c-1.3,1.4 -3.3,4.6 -4.6,7.2c-1.3,2.7 -2.7,4.8 -3,4.8c-0.4,0 -2,1.9 -3.5,4.3c-3.7,5.6 -8.1,11.1 -13,16.3c-7.8,8.2 -9.3,10.1 -21.8,25.9c-16,20.2 -15.9,20.1 -19.5,26c-1.6,2.8 -5.2,8.7 -8.1,13.3c-2.8,4.6 -5.1,8.7 -5.1,9c0,0.4 -1.8,2.9 -4,5.7c-2.2,2.7

而最终的 console.log 结果(也被截断):

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0"
viewBox="0,0,0,0">
  <defs>
    <clipPath id="clip-1">
      <rect x="0" y="0" transform="scale(0,0)" width="0" height="0"
      fill="none"></rect>
    </clipPath>
    <clipPath id="clip-2">
      <rect x="-1037" y="-374" transform="scale(0,0)" width="2074"
      height="748" id="clip-1" fill="none"></rect>
    </clipPath>
  </defs>
  <g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1"
  stroke-linecap="butt" stroke-linejoin="miter"
  stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0"
  font-family="none" font-weight="none" font-size="none"
  text-anchor="none" style="mix-blend-mode: normal">
    <g clip-path="url(#clip-1)">
      <g>
        <g clip-path="url(#clip-2)">
          <g fill="#f98673" font-family="sans-serif"
          font-weight="normal" font-size="12" text-anchor="start">
          </g>
          <path d="M0,0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0zM0,0v0v0v0v0v0v0v0v0v0v0v0v

这里有一些隐式编码吗?致力于 linux/bash/node v10.19.0.

我无法使用另一个 svg 文件重现它,例如 https://upload.wikimedia.org/wikipedia/commons/2/21/Speaker_Icon.svg

你能link你的svg文件吗?

我建议您尝试使用以下代码,因为 stdin 上的 readFile 无法正常工作 (https://github.com/nodejs/node-v0.x-archive/issues/7412)

const Project = require('paper').Project
var fs = require('fs');
let p = new Project() 

var chunks = [];
process.stdin
    .on("data", function(chunk) { chunks.push(chunk); })
    .on("end", function() {

        console.log(chunks.join("").toString());
        p.importSVG(chunks.join("").toString(), { onError: function(e){ console.log(e) } })
        console.log(p.exportSVG({ asString: true }));
    })
    .setEncoding("utf8");