Skip to content
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

Replace fetch api #2309

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,7 @@ This release notably changes to using N-API. 🎉
* Remove unused private field `backend` in the `Backend` class. (#2229)
* Add Node.js v20 to CI. (#2237)
* Replaced `dtslint` with `tsd` (#2313)
* Replaced `simple-get ` with ` Node.js builtin` `fetch` (#2309)
### Added
* Added string tags to support class detection
### Fixed
Expand Down
34 changes: 20 additions & 14 deletions lib/image.js
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 @@ -46,21 +43,30 @@ Object.defineProperty(Image.prototype, 'src', {
throw err
}
}

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

get.concat({
url: val,
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)

util
.callbackify(
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",
},
})
)
.then((res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return onerror(new Error(`Server responded with ${res.statusCode}`))
return onerror(
new Error(`Server responded with ${res.statusCode}`)
)
}

return res.arrayBuffer()
})
.then((data) => {
setSource(this, data)
})
.catch((err) => {
onerror(err)
})
} else { // local file path assumed
setSource(this, val)
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
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