【问题标题】:nodejs memory buffer synchronized .pipe()nodejs内存缓冲区同步.pipe()
【发布时间】:2015-11-15 04:31:29
【问题描述】:

我正在尝试将 pdfkit 从 png-js 迁移到 pngsj2,因为 png-js 不支持隔行扫描 png。我需要以同步方式加载 PNG 文件。我尝试这样做:

var fs = require('fs'),
    PNG = require('pngjs2').PNG;
var stream = require('stream');
var bufferStream = new stream.PassThrough();
var buf = fs.readFileSync('./logs/rid12.png');
bufferStream.end(buf);
var png = null;
bufferStream.pipe(new PNG())
    .on('parsed', function() {
        console.log("here");
        png = this;
    });

console.log("there",png);

“there”发生在“here”之前,因此 png 为空。是否可以将内存缓冲区通过管道传输到 PNG 解析器,这样我就不必制作回调架构?

【问题讨论】:

    标签: node.js stream buffer pdfkit


    【解决方案1】:

    没有办法使异步方法同步。

    有诸如https://github.com/scriby/asyncblock之类的库可以伪造它。

    asyncblock(function(flow) {
    
        var png = null
        flow.sync( bufferStream.pipe(new PNG())
          .on('parsed', function() {
            console.log("here");
            png = this;
            flow.callback()
          })
        );
    
       // png not null
    });
    

    【讨论】:

    • 我不这么认为。大概可以调用(new PNG())的一些方法,但是不知道是哪一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多