question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Emit multiple data chunks from output Streams (was: Proper streaming)

See original GitHub issue

It would be very cool if sharp could support proper streaming. As it is now, it will always emit exactly one data event which is the whole image. It kind of defeats the purposes of streams.

As I see it there is two ways to implement this.

  1. The hard, but robust way.

    Every time that libvips have data that should be written, it should call a function that we have defined. We then use this data to generate a data-event on the stream. It would be really cool if libvips could provide a state-machine that has a synchronous function that gives the next chunk. That way pause and unpause would work perfectly.

  2. The quick and easy fix.

    We can also create a named pipe and tell libvips to write it to that file via vips_image_write_to_file. To the user we would then return a ReadStream as returned by fs.createReadStream(path).

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:4
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
gmaclennancommented, Feb 26, 2016

@jcupitt great work on the streams branch, a shame it could never be merged. Just adding my support for any work on true streams, which would allow significantly less memory usage since right now sharp needs to load the source image into memory, and then hold the whole output buffer in memory, before writing out.

1reaction
summeracommented, Apr 1, 2020

Yes, after there’s a version of sharp that depends on the future libvips v8.9.0 we can look at the best way of handling its new Stream API.

A possible first implementation could involve accepting file descriptors as input/output, although that is likely to have to be via new API and not the standard, idiomatic Node.js Stream API (which will still be supported using the existing implementation).

A more complete solution might be to create custom libvips Stream objects and use these for passing data chunks to/from Node.js, but there are multi-threading considerations e.g. libvips will be running on a different thread to V8.

I’m planning to migrate the internals of sharp to use Node.js’s newish N-API first - see #1282 - sadly I don’t see anything in N-API for Node.js Stream objects, plus this migration will remove access to things like Node.js’ StreamBase class.

@lovell is this still on the roadmap given sharp is now using libvips 8.9?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I get a NodeJS Transform stream to emit multiple ...
Okay fixed it. This solution was to not do multiple tranny.write s but to do the data transformation within the tranny.
Read more >
Understanding Streams in Node.js - NodeSource
It simply reads chunks of data from an input stream and writes to the destination using write() . This function returns a boolean...
Read more >
Events and Streams in Node.js - codeburst
The most important events on a readable stream are: The data event, which is emitted whenever the stream passes a chunk of data...
Read more >
Node.js Streams: Everything you need to know - freeCodeCamp
A stream for the grep output const wc = . ... The data event, which is emitted whenever the stream passes a chunk...
Read more >
Stream | Node.js v19.3.0 Documentation
If a call to stream.write(chunk) returns false , the 'drain' event will be emitted when it is appropriate to resume writing data to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found