From 1eedb22ef5977100f0a50f9dca1465ab1f9a19d4 Mon Sep 17 00:00:00 2001 From: Vincent Voyer Date: Thu, 2 Apr 2020 13:06:11 +0200 Subject: [PATCH] docs(clone): add promise example --- docs/api-constructor.md | 49 +++++++++++++++++++++++++++++++++++++++++ lib/constructor.js | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/docs/api-constructor.md b/docs/api-constructor.md index bbc61fea..5bb9ea28 100644 --- a/docs/api-constructor.md +++ b/docs/api-constructor.md @@ -89,6 +89,55 @@ readableStream.pipe(pipeline); // secondWritableStream receives auto-rotated, extracted region of readableStream ``` +```javascript +// Create a pipeline that will download an image, resize it and format it to different files +// Using Promises to know when the pipeline is complete +const fs = require("fs"); +const got = require("got"); +const sharpStream = sharp({ + failOnError: false +}); + +const promises = []; + +promises.push( + sharpStream + .clone() + .jpeg({ quality: 100 }) + .toFile("originalFile.jpg") +); + +promises.push( + sharpStream + .clone() + .resize({ width: 500 }) + .jpeg({ quality: 80 }) + .toFile("optimized-500.jpg") +); + +promises.push( + sharpStream + .clone() + .resize({ width: 500 }) + .webp({ quality: 80 }) + .toFile("optimized-500.webp") +); + +// https://github.com/sindresorhus/got#gotstreamurl-options +got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream); + +Promise.all(promises) + .then(res => { console.log("Done!", res); }) + .catch(err => { + console.error("Error processing files, let's clean it up", err); + try { + fs.unlinkSync("originalFile.jpg"); + fs.unlinkSync("optimized-500.jpg"); + fs.unlinkSync("optimized-500.webp"); + } catch (e) {} + }); +``` + Returns **[Sharp][8]** [1]: https://nodejs.org/api/buffer.html diff --git a/lib/constructor.js b/lib/constructor.js index f0c5acba..0acf8243 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -253,6 +253,54 @@ util.inherits(Sharp, stream.Duplex); * // firstWritableStream receives auto-rotated, resized readableStream * // secondWritableStream receives auto-rotated, extracted region of readableStream * + * @example + * // Create a pipeline that will download an image, resize it and format it to different files + * // Using Promises to know when the pipeline is complete + * const fs = require("fs"); + * const got = require("got"); + * const sharpStream = sharp({ + * failOnError: false + * }); + * + * const promises = []; + * + * promises.push( + * sharpStream + * .clone() + * .jpeg({ quality: 100 }) + * .toFile("originalFile.jpg") + * ); + * + * promises.push( + * sharpStream + * .clone() + * .resize({ width: 500 }) + * .jpeg({ quality: 80 }) + * .toFile("optimized-500.jpg") + * ); + * + * promises.push( + * sharpStream + * .clone() + * .resize({ width: 500 }) + * .webp({ quality: 80 }) + * .toFile("optimized-500.webp") + * ); + * + * // https://github.com/sindresorhus/got#gotstreamurl-options + * got.stream("https://www.example.com/some-file.jpg").pipe(sharpStream); + * + * Promise.all(promises) + * .then(res => { console.log("Done!", res); }) + * .catch(err => { + * console.error("Error processing files, let's clean it up", err); + * try { + * fs.unlinkSync("originalFile.jpg"); + * fs.unlinkSync("optimized-500.jpg"); + * fs.unlinkSync("optimized-500.webp"); + * } catch (e) {} + * }); + * * @returns {Sharp} */ function clone () {