Skip to content

Commit

Permalink
Use simple-get for fetching images
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Apr 1, 2019
1 parent 39aa8ab commit 2cc758f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
(Unreleased)
==================
### Changed
* Use [simple-get](https://github.com/feross/simple-get) for fetching images, now supports redirects (#1398)
### Added
### Fixed

Expand Down
19 changes: 8 additions & 11 deletions lib/image.js
Expand Up @@ -12,8 +12,7 @@

const bindings = require('./bindings')
const Image = module.exports = bindings.Image
const http = require("http")
const https = require("https")
const get = require('simple-get')
const util = require('util')

const {GetSource, SetSource} = bindings;
Expand Down Expand Up @@ -46,17 +45,15 @@ Object.defineProperty(Image.prototype, 'src', {
}
}

const type = /^\s*https:\/\//.test(val) ? https : http
type.get(val, res => {
if (res.statusCode !== 200) {
get.concat(val, (err, res, data) => {
if (err) return onerror(err)

if (res.statusCode < 200 || res.statusCode >= 300) {
return onerror(new Error(`Server responded with ${res.statusCode}`))
}
const buffers = []
res.on('data', buffer => buffers.push(buffer))
res.on('end', () => {
setSource(this, Buffer.concat(buffers));
})
}).on('error', onerror)

setSource(this, data)
})
} else { // local file path assumed
setSource(this, val);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -49,7 +49,8 @@
"types": "types",
"dependencies": {
"nan": "^2.12.1",
"node-pre-gyp": "^0.11.0"
"node-pre-gyp": "^0.11.0",
"simple-get": "^3.0.3"
},
"devDependencies": {
"@types/node": "^10.12.18",
Expand Down

0 comments on commit 2cc758f

Please sign in to comment.