Skip to content

Commit

Permalink
Merge pull request #92 from tomoyukikashiro/fix/image-size-known-issue
Browse files Browse the repository at this point in the history
fix(image/picutre): ERR_BUFFER_OUT_OF_BOUNDS in image-size
  • Loading branch information
tomoyukikashiro authored Oct 21, 2019
2 parents 661eeec + 94d422c commit c3a2d70
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 102 deletions.
19 changes: 10 additions & 9 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const axios = require('axios')
const fs = require('fs')
const path = require('path')
const sizeOf = require('image-size')
const uriToBuffer = require('data-uri-to-buffer')
const probe = require('probe-image-size')

const normalizeUrl = (url) => {
if (url.startsWith('http')) {
Expand Down Expand Up @@ -48,18 +48,19 @@ const normalizeSrc = (src, srcset) => {
const srcNode = async (cwd, attributes) => {
const { src, alt = '', ...attrs } = attributes
const url = normalizeUrl(normalizeSrc(src, attributes.srcset))
let size = null
let img = null
let size = { width: null, height: null }
if (url.startsWith('http')) {
const remoteOptions = { responseType: 'arraybuffer' }
img = await getRemoteFile(url, remoteOptions)
size = sizeOf(img)
const result = await probe(url)
size = { width: result.width, height: result.height }
} else if (url.startsWith('data:')) {
const buffer = uriToBuffer(url)
size = sizeOf(buffer)
const result = probe.sync(buffer)
size = { width: result.width, height: result.height }
} else {
getRelativeFile(url, cwd)
size = sizeOf(path.join(cwd, url))
const fileStream = fs.createReadStream(path.join(cwd, url))
const result = await probe(fileStream)
size = { width: result.width, height: result.height }
fileStream.destroy()
}
// remove loading attribute
// https://github.com/tomoyukikashiro/html2amp/issues/80
Expand Down
Loading

0 comments on commit c3a2d70

Please sign in to comment.