tinify-nodejs icon indicating copy to clipboard operation
tinify-nodejs copied to clipboard

stream support

Open sarkistlt opened this issue 7 years ago • 4 comments

any plans on supporting stream? buffer is cool, but what if I just don't want to load the whole file, and just want to stream it from my API to swift or s3 object storage. Would be supper useful to be able pipe to stream

sarkistlt avatar May 08 '18 05:05 sarkistlt

There are no update plans for the clients right now but I'm happy to note your request and share the idea with the development team.

michielverkoijen avatar May 08 '18 08:05 michielverkoijen

I've just tested it with node.js (6.15.1), and you should be able to use the default streams functionality:

import request from 'request';

const r = request.post('https://api:[email protected]/shrink', (err, resp, body) => {
    const obj = JSON.parse(body);

    request.get(obj.output.url); // <== your compressed file url, also pipeable
});

stream.pipe(r);

HydraOrc avatar Dec 05 '18 13:12 HydraOrc

@HydraOrc hm, didn't thought about directly using API url, looks good, will try, thanks! But I think you need pipe request.get stream, instead of r, if you want to stream compressed file to write stream distention.

sarkistlt avatar Dec 05 '18 17:12 sarkistlt

@sarkistlt I mean that at first you should pipe your stream to the post request (r in the case above) to send your data to the tinify api server. And after that in a callback you could also pipe the request.get(obj.output.url) to write the compressed result to any destination you want to. In my case it is writing a file to a GridFS storage and it works perfectly, but it could be something else.

For example: request.get(obj.output.url).pipe(fs.createWriteStream('filename.txt'));

HydraOrc avatar Dec 06 '18 10:12 HydraOrc