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

Encoder for GIF provides a Promise to getBuffer when a string, Buffer, or Uint8Array is expected #1239

Merged
merged 3 commits into from
Jul 26, 2023

Conversation

stevezac-osu
Copy link
Contributor

@stevezac-osu stevezac-osu commented May 29, 2023

Fixing Issue 980: The 'chunk' argument must be of type string or an Instance of Buffer or Uint8Array. Received an instance of Promise.

What's Changing and Why

On jimp/packages/core/src/utils/image-bitmap.js getBuffer(mime, cb) function, the method is intended to provide a string or a buffer to a callback function so that the callback can consume such buffer (typically with the Write function).

However, occasionally the following error will occur ( see also #980 ):

internal/streams/writable.js:285
      throw new ERR_INVALID_ARG_TYPE(
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Promise
    at new NodeError (internal/errors.js:322:7)
    at WriteStream.Writable.write (internal/streams/writable.js:285:13)
    at WriteStream.<anonymous> (/home/zachs1590/cs464-jimp-testing/node_modules/@jimp/core/dist/index.js:462:16)
    at WriteStream.emit (events.js:400:28)
    at internal/fs/streams.js:340:12
    at FSReqCallback.oncomplete (fs.js:180:23) {
  code: 'ERR_INVALID_ARG_TYPE'

I traced the issue back to the fact that one of the encoders, the GIF encoder, consistently returns a promise rather than a string or a buffer response. See jimp/packages/type-gif/src/index.js line 34.

Rather than solve this issue at the GIF encoder (if that could be solved at all), it seemed like a better idea to make the get buffer function more robust by permitting encoders to return a promise as a valid data type and handle such a response gracefully.

Accordingly, on jimp/packages/core/src/utils/image-bitmap.js getBuffer(mime, cb), I have added

  if (this.constructor.encoders[mime]) {
    const buffer = this.constructor.encoders[mime](this);
    //Typically, buffers return a string or map.  However, the gif library "gifwrap" seemingly returns promises.
    if(buffer instanceof Promise){
      //trigger the callback when the promise has been resolved
      buffer.then((buff) => {
        cb.call(this, null, buff);
      })
    } else {
      cb.call(this, null, buffer);
    }

What else might be affected

n/a

…nstance of Buffer or Uint8Array. Received an instance of Promise.
@stevezac-osu
Copy link
Contributor Author

stevezac-osu commented May 31, 2023

Please note -- I think this solves not only #980, but also #1000 and #1056

@stevezac-osu stevezac-osu changed the title Resolving Issue 980 Encoder for GIF provides a Promise to getBuffer when a string, Buffer, or Uint8Array is expected May 31, 2023
@jasonz1987
Copy link

please merge this pr.

@jbwebtech
Copy link

@stevezac-osu thank you for finding (and fixing!) the issue many of us are experiencing. I implemented your change locally, and it worked great! 👍🎉

@hipstersmoothie hipstersmoothie merged commit 0a404ca into jimp-dev:main Jul 26, 2023
6 of 7 checks passed
@github-actions
Copy link
Contributor

🚀 PR was released in v0.22.10 🚀

@github-actions github-actions bot added the released This issue/pull request has been released. label Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants