oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.71k stars 2.72k forks source link

trouble with sharp/jimp #13846

Closed Shawak closed 2 weeks ago

Shawak commented 1 month ago

What version of Bun is running?

1.1.27

What platform is your computer?

debian/alpine docker container

What steps can reproduce the bug?

Upgrade to 1.1.27 and use the jimp package, e.g.:

import Jimp from 'jimp';
import sharp from 'sharp';

  const buffer = await (async () => {
      const response = await fetch(imageLink);
      const arrayBuffer = await response.arrayBuffer();
      return await sharp(arrayBuffer).toFormat('png').toBuffer();
  })();

  const image = await Jimp.read(buffer);
  const imageBuffer = await image
      .invert()
      .greyscale()
      .getBufferAsync(Jimp.MIME_PNG);

What is the expected behavior?

No response

What do you see instead?

Date: Tue Sep 10 2024 07:03:51 GMT+0000 (Coordinated Universal Time)
106 |   }
107 | 
108 |   assert(this._handle, "zlib binding closed");
109 |   let res;
110 |   do {
111 |     res = this._handle.writeSync(
                             ^
TypeError: this._handle.writeSync is not a function. (In 'this._handle.writeSync(flushFlag, chunk, inOff, availInBefore, this._buffer, this._offset, availOutBefore)', 'this._handle.writeSync' is undefined)
      at /home/bun/app/node_modules/pngjs/lib/sync-inflate.js:111:24
      at /home/bun/app/node_modules/pngjs/lib/parser-sync.js:85:20
      at /home/bun/app/node_modules/@jimp/core/dist/utils/image-bitmap.js:157:47

Additional information

No response

nektro commented 2 weeks ago

As of https://github.com/oven-sh/bun/pull/14079, now fails with

1 | (function (entry, fetcher)
    ^
SyntaxError: module '/Users/meghandenny/src/test/node_modules/jimp/dist/esm/index.js' does not have an export named 'default'. Did you mean 'Jimp'?
      at asyncFunctionResume (1:11)
      at promiseReactionJobWithoutPromiseUnwrapAsyncContext (1:11)
      at promiseReactionJob (1:11)

Bun v1.1.30-debug+6aabaa617 (macOS arm64)
nektro commented 2 weeks ago

changing first line to import { Jimp } from "jimp"; fails with

5 |   const response = await fetch(imageLink);
                                            ^
ReferenceError: Can't find variable: imageLink
      at /Users/meghandenny/src/test/index.js:5:41
github-actions[bot] commented 2 weeks ago

Hello @Shawak. Please provide a minimal reproduction using a GitHub repository, Replit, or CodeSandbox. Issues marked with needs repro will be closed if they have no activity within 3 days.

tobias-theobald commented 2 weeks ago

I have created a minimal reproduction on replit. This does work with node/tsx.

Note how the crash actually happens in pngjs but it's inheriting the Inflate class from zlib (a node-native module) and doing something weird with underscore-prefixed methods, a notation which should be reserved for internals. zlib should be supported by bun, that is how I understand the claim that it's not optimized though.

nektro commented 2 weeks ago

excellent, thanks. can confirm this is now fixed by the latest canary and will be available again in 1.1.30