nodeca / probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
MIT License
979 stars 77 forks source link

Gotcha: probe-image-size does not work with gzip encoded streams #28

Closed ithinkihaveacat closed 5 years ago

ithinkihaveacat commented 5 years ago

If a gzip encoded stream is requested via the accept-encoding header, probe-image-size fails with unrecognized file format:

const probe = require("probe-image-size");
probe(
  "https://www.washingtonpost.com/graphics/2018/lifestyle/magazine/amp-stories/tangent-big-change-means-big-risk/img/Tangent_Rebrand_1.jpg",
  { headers: { "accept-encoding": "gzip" }
  }).then(console.log, console.error);

Actual result:

{ ProbeError: unrecognized file format
    at ParserStream.parserEnd (/Users/stillers/workspace/ampbench/amp-story/linter/node_modules/probe-image-size/stream.js:29:24)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
    at ParserStream.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)
  name: 'ProbeError',
  message: 'unrecognized file format',
  code: 'ECONTENT' }

Expected result (without accept-encoding: gzip):

{ width: 1162,
  height: 2066,
  type: 'jpg',
  mime: 'image/jpeg',
  wUnits: 'px',
  hUnits: 'px',
  url: 'https://www.washingtonpost.com/graphics/2018/lifestyle/magazine/amp-stories/tangent-big-change-means-big-risk/img/Tangent_Rebrand_1.jpg' }

I suppose this is more or less working as intended, but it might be useful to mention this in the README somewhere. (Or perhaps this issue itself is sufficient "documentation".)

(One reason why you might end up piping accept-encoding headers to probe-image-size is if the original input was something like Chrome DevTools' "Copy as cURL" function, which bulk copies all headers.)

puzrin commented 5 years ago

I'm not sure if this worth additional note in readme, but feel free to make PR if you wish, it will be accepted. Just try to keep your note short.

PS. See https://github.com/nodeca/probe-image-size/issues/16 first. AFAIK, gzipped content should be supported, and problem CAN be on server side. But anyway... nobody cares... because no real need to request gripped images.