title: How to run Sharp transform on readable http stream and write toFile
body: <p>This question is about the node Sharp library <a href="http://sharp.pixelplumbing.com/en/stable/api-input/" rel="nofollow noreferrer">http://sharp.pixelplumbing.com/en/stable/api-input/</a></p>
  
  <p>Documentation for the constructor says that it can read image data from a stream if you pipe it into the sharp object. "JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present."</p>
  
  <p>I'm trying to pipe a readable stream from a HTTP GET from GCS into the transform, then use <code>toFile</code> to get the transformed image in a file. I get an error from Sharp indicating that it isn't getting the image data input properly. Here is the code which is part of the function that does this workflow.</p>
  
  <pre><code>const avatarId = this.Uuid(),
      sharp = this.Sharp({failOnError: true}),
      instream = this.GoogleCloudStorage.GET(this.GoogleCloudStorage.getName(photo.uri)),
      fileLocation = this.getTempFileLocation()
  
  sharp.rotate().extract({left: x, top: y, width: w, height: w})
  if (w &gt; 600) {
      sharp.resize(600, 600, {fastShrinkOnLoad: false})
      w = 600
  }
  
  instream.pipe(sharp)
  return sharp.jpeg().toFile(fileLocation)
  .then( info =&gt; {
      console.log('editAvatar: sharp done')
      return this.GoogleCloudStorage.POST( avatarId, this.Fs.createReadStream( fileLocation ), this.getMIMEType(info) )
  } )
  </code></pre>
  
  <p><code>GoogleCloudStorage.GET</code> basically returns <code>File.createReadStream</code> on cloud-storage File object. I've tested this GET call in a script, it works. <code>GoogleCloudStorage.POST</code> uses <code>File.createWriteStream</code> and pipes the passed readable stream into it. Hence it needs to receive a readable stream, which is why I'm trying to write the sharp transform to file.</p>
  
  <p>The error output I get:</p>
  
  <pre><code>[02/24/2018 07:41:06.892] Error -- message: Input file is missing or of an unsupported image format -- stack: Error: Input file is missing or of an unsupported image format
  events.js:163
        throw er; // Unhandled 'error' event
        ^
  
  Error: Unexpected data on Writable Stream
      at Sharp._write (/code/api/node_modules/sharp/lib/input.js:114:14)
      at doWrite (_stream_writable.js:329:12)
      at writeOrBuffer (_stream_writable.js:315:5)
      at Sharp.Writable.write (_stream_writable.js:241:11)
      at DestroyableTransform.ondata (/code/api/node_modules/readable-stream/lib/_stream_readable.js:612:20)
      at emitOne (events.js:96:13)
      at DestroyableTransform.emit (events.js:191:7)
      at addChunk (/code/api/node_modules/readable-stream/lib/_stream_readable.js:284:12)
      at readableAddChunk (/code/api/node_modules/readable-stream/lib/_stream_readable.js:271:11)
      at DestroyableTransform.Readable.push (/code/api/node_modules/readable-stream/lib/_stream_readable.js:238:10)
  </code></pre>
  
  <p><strong>Update</strong>: I experimented using <code>File.download</code> instead, and that works. So either I'm missing something with piping the stream into sharp, or sharp has a bug.</p>