Skip to content

Replace fetch api #2309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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