Skip to content

Commit 62032a0

Browse files
committed
fix
1 parent 6f690c1 commit 62032a0

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

lib/image.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const bindings = require('./bindings')
1414
const Image = module.exports = bindings.Image
1515
const util = require('util')
1616

17+
// Lazily loaded fetch
18+
let fetch = global.fetch || require('node-fetch')
19+
1720
const { GetSource, SetSource } = bindings
1821

1922
Object.defineProperty(Image.prototype, 'src', {
@@ -43,30 +46,21 @@ Object.defineProperty(Image.prototype, 'src', {
4346
throw err
4447
}
4548
}
46-
util
47-
.callbackify(
48-
fetch(val, {
49-
method: 'GET',
50-
headers: {
51-
"User-Agent":
52-
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36",
53-
},
54-
})
55-
)
56-
.then((res) => {
57-
if (res.statusCode < 200 || res.statusCode >= 300) {
58-
return onerror(
59-
new Error(`Server responded with ${res.statusCode}`)
60-
)
61-
}
62-
return res.arrayBuffer()
63-
})
64-
.then((data) => {
65-
setSource(this, data)
66-
})
67-
.catch((err) => {
68-
onerror(err)
49+
50+
fetch(val, {
51+
method: 'GET',
52+
headers: { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36' }
6953
})
54+
.then(res => {
55+
if (!res.ok) {
56+
throw new Error(`Server responded with ${res.statusCode}`)
57+
}
58+
return res.arrayBuffer()
59+
})
60+
.then(data => {
61+
setSource(this, Buffer.from(data))
62+
})
63+
.catch(onerror)
7064
} else { // local file path assumed
7165
setSource(this, val)
7266
}

0 commit comments

Comments
 (0)