title: nodejs sharp store direct input stream if failed
body: <p>My need is to resize file and store it and if it failed store original file.
  I try to avoid memory leak and get killed.</p>
  <pre class="lang-js prettyprint-override"><code>let file = fs.createWriteStream(path.join(__dirname, '..', 'public', 'images', 'photo-'+evt._id.toString()));
          const transform = sharp().resize(800, 800, {
              fit: sharp.fit.outside})
          .on('error', (e) =&gt; {
              console.log(url);
              console.log(e);
              if (file) file.destroy();
              if (response) response.destroy();
              //TODO direct copy if fail
              //response.pipe(file)
          });
          global.gc();
          /*try {
              await pipeline(response,
                  transform,
                  file);
              await db.collection(&quot;Event&quot;).updateOne({&quot;_id&quot; : evt._id},{ $set : {'photo' : &quot;photo-&quot;+evt._id.toString()} });
          } catch (err) {
              console.log(err);
              await pipeline(response,
                  file);
          }*/
          let end = new Promise(function(resolve, reject) {
              file.on('close', async function() {
                  await db.collection(&quot;Event&quot;).updateOne({&quot;_id&quot; : evt._id},{ $set : {'photo' : &quot;photo-&quot;+evt._id.toString()} });
                  resolve();
              });
              file.on('error', function(err) {
                  console.log(err);
                  reject(err);
                  if (response) response.destroy();
              });
             
              response.pipe(transform).pipe(file);
          });
          await end;
  </code></pre>
  <ol>
  <li>I am not sure if best is to used pipe() with promise or pipeline function ?</li>
  <li>the response.pipe(file) in on('error',...) will failed because resp is already read. So how to proceed ? store to a temporary buffer ? use a Tee stream ? Is there any solution in sharp API ?</li>
  </ol>