tinify / tinify-nodejs

Node.js client for the Tinify API.
https://tinypng.com/developers
MIT License
414 stars 74 forks source link

stream support #16

Open sarkistlt opened 6 years ago

sarkistlt commented 6 years ago

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

michielverkoijen commented 6 years ago

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.

HydraOrc commented 5 years ago

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:your_key@api.tinify.com/shrink', (err, resp, body) => {
    const obj = JSON.parse(body);

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

stream.pipe(r);
sarkistlt commented 5 years ago

@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.

HydraOrc commented 5 years ago

@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'));