Skip to content

Conversation

Ian-I-Chien
Copy link

@Ian-I-Chien Ian-I-Chien commented Jun 24, 2025

fix: reject loadImage when src is null or invalid
Description:

  • To handle non-string/non-Buffer sources while fetching image.
  • Add test cases for '', null, and undefined inputs to loadImage()

Test Result:

npm run test
...
      ✔ rejects when loadImage is called with null
      ✔ rejects when loadImage is called with undefined
      ✔ rejects when loadImage is called with empty string

  291 passing (302ms)
  6 pending

Thanks for contributing!

  • Have you updated CHANGELOG.md?

Fix #2304

@tom00502
Copy link

I'm also encountering this issue. I hope it can be merged.

@Ian-I-Chien Ian-I-Chien force-pushed the reject-null-for-loadImage branch from cf6b80f to ecd20bd Compare June 30, 2025 06:16
index.js Outdated
@@ -22,6 +22,8 @@ function createImageData (array, width, height) {

function loadImage (src) {
return new Promise((resolve, reject) => {
if (!src) return reject(new Error("Invalid image source"));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be placed in a final else branch on line 66 of lib/image.js to fix the whole class of bugs. It isn't just null/undefined that will cause onload to not be called. Then you could mark this as fixes #2525 as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @chearon ,
Thanks for the review.
Would you prefer having the check in both index.js and image.js or only in the image.js ?

Description:
- To handle non-string/non-Buffer sources while fetching image.
- Add test cases for '', null, and undefined inputs to loadImage()

Test Result:
```
npm run test
...
      ✔ rejects when loadImage is called with null
      ✔ rejects when loadImage is called with undefined
      ✔ rejects when loadImage is called with empty string

  291 passing (302ms)
  6 pending
```
@Ian-I-Chien Ian-I-Chien force-pushed the reject-null-for-loadImage branch from ecd20bd to 6186db2 Compare August 29, 2025 08:10
@Ian-I-Chien
Copy link
Author

Hi @chearon ,
I have forced pushed based on your suggestion. Please help review it again.
Thank you.

Ian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

loadImage causes the process to terminate
3 participants