首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sharp.js不给stream.Writable管道吗?

Sharp.js不给stream.Writable管道吗?
EN

Stack Overflow用户
提问于 2020-09-03 03:19:09
回答 1查看 346关注 0票数 0

在“sharp”上找到:"^0.26.0“

由于某些原因,此无法工作

代码语言:javascript
复制
const readableStream = new stream.Readable({
    objectMode: true,
    read() {}
})

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

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

var png = sharp().png()
var fileswitch = new FileSwitch();
readableStream.pipe(png).pipe(fileswitch)

无输出

然而,出于某种原因,这确实有效:

代码语言:javascript
复制
const readableStream = new stream.Readable({
    objectMode: true,
    read() {}
})

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

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

var png = sharp().png()
var fileswitch = new FileSwitch();
readableStream.pipe(png.pipe(fileswitch))
代码语言:javascript
复制
>>> 0
>>> 1
>>> 2

使用setInterval()将可读流按1秒间隔推送新的字符串数据块。

难道我没有把一个功能暴露给一个可写的流,让它变得尖锐吗?文档说,这个设置应该可以工作:)

更新

添加了来自@Matt评论的建议。

代码语言:javascript
复制
const readableStream = new stream.Readable({
    objectMode: true,
    read() {}
})

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

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

readableStream.on('error', listener => {
    console.log(listener)
})

readableStream.on('warning', listener => {
    console.log(listener)
})

var png = sharp().png()
var fileswitch = new FileSwitch();
readableStream.pipe(png).pipe(fileswitch)

无输出

EN

回答 1

Stack Overflow用户

发布于 2020-09-03 15:57:30

被推送到readableStream的数据块不是用于sharp转换为png的有效图像格式。将错误事件侦听器添加到png转换器显示正确的错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63716286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档