为什么将更多数据推送到流中会破坏代码?

问题描述

我一直在弄乱nodejs流和sharp.js,并被告知这个问题与库无关,而是与nodejs流本身有关?我很困惑我的代码失败了,为什么为什么失败了。

[email protected] [email protected]

const stream = require('stream');
const sharp = require('sharp');

const svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svgjs="http://svgjs.com/svgjs" width="1080" height="720"><rect width="100" height="100" x="100" y="100"></rect><circle r="50" cx="50" cy="50" fill="blue"></circle></svg>'

const readableStream = new stream.Readable({
    objectMode: false,read() {}
})

class FileSwitch extends stream.Writable {
    constructor(options) {
        super(options)
        this.frame = 0
    }

    _write(chunk,encoding,next) {
        console.log(this.frame)
        this.frame++
        next()
    }
}

var fileswitch = new FileSwitch();
var png = sharp().png().on('error',err => {
    console.log(err)
})

readableStream.pipe(png).pipe(fileswitch) // Works with just one call to readableStream.push()
// readableStream.pipe(fileswitch) // Working with both

readableStream.push(Buffer.from(svg)) // png works with just one.
// readableStream.push(Buffer.from(svg)) // Breaks when a second is in put in.
readableStream.push(null)

输出对应于对包含readableStream.push()的管道进行两次调用png的情况。

[Error: Input buffer has corrupt header: glib: XML parse error: Error domain 1 code 5 on line 1 column 273 of data: Extra content at the end of the document


]

按注释中的概述将两个缓冲区对象推到readablStream上时,通过Sharp进行传递的代码不起作用,但是仅包含fileswitch的管道则起作用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)