Sharp.js not piping to stream.Writable?

798 Views Asked by At

Found on "sharp": "^0.26.0"

For some reason this does not work:

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)

No output

Yet this for some reason does work:

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))
>>> 0
>>> 1
>>> 2

The readable stream is pushed a new chunk of string data on a 1 second interval using setInterval().

Am I not exposing a functionality to a Writable stream to sharp that I should be? The docs say that this set up should work :)

Update

Added in suggestions from @Matt's comment.

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)

No Output

1

There are 1 best solutions below

0
On

The chunk of data being pushed to the readableStream was not a valid image format for sharp to convert to png. Adding error event listeners onto the png transformer displayed the correct errors.