Skip to content

Commit

Permalink
use fetch api
Browse files Browse the repository at this point in the history
  • Loading branch information
yumiura authored and chearon committed Jan 11, 2025
1 parent 80e94ea commit 1d956b7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
(Unreleased)
==================
### Changed
* Replaced `simple-get ` with ` Node.js builtin` `fetch` (#2309)
### Added
### Fixed

Expand Down
27 changes: 12 additions & 15 deletions lib/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ const bindings = require('./bindings')
const Image = module.exports = bindings.Image
const util = require('util')

// Lazily loaded simple-get
let get

const { GetSource, SetSource } = bindings

Object.defineProperty(Image.prototype, 'src', {
Expand Down Expand Up @@ -47,20 +44,20 @@ Object.defineProperty(Image.prototype, 'src', {
}
}

if (!get) get = require('simple-get')

get.concat({
url: val,
fetch(val, {
method: 'GET',
headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36' }
}, (err, res, data) => {
if (err) return onerror(err)

if (res.statusCode < 200 || res.statusCode >= 300) {
return onerror(new Error(`Server responded with ${res.statusCode}`))
}

setSource(this, data)
})
.then(res => {
if (!res.ok) {
throw new Error(`Server responded with ${res.statusCode}`)
}
return res.arrayBuffer()
})
.then(data => {
setSource(this, Buffer.from(data))
})
.catch(onerror)
} else { // local file path assumed
setSource(this, val)
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
],
"dependencies": {
"node-addon-api": "^7.0.0",
"prebuild-install": "^7.1.1",
"simple-get": "^3.0.3"
"prebuild-install": "^7.1.1"
},
"devDependencies": {
"@types/node": "^10.12.18",
Expand Down

0 comments on commit 1d956b7

Please sign in to comment.