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

Buffer not found when using msgpack-lite in production build #12717

Closed
1 task
anonymous-0545 opened this issue Dec 11, 2024 · 5 comments
Closed
1 task

Buffer not found when using msgpack-lite in production build #12717

anonymous-0545 opened this issue Dec 11, 2024 · 5 comments
Labels
needs repro Issue needs a reproduction

Comments

@anonymous-0545
Copy link

Astro Info

Astro                    v5.0.3
Node                     v23.1.0
System                   macOS (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/mdx
                         @astrojs/sitemap
                         @astrojs/tailwind
                         astro-purgecss

If this issue only occurs in one browser, which browser is a problem?

Chrome

Describe the Bug

Description

The msgpack-lite library works correctly in development but fails in the production build with a "Cannot read properties of undefined (reading 'Buffer')" error.

Error Message

ViewersGrid.astro_astro_type_script_index_0_lang.BIjkZuiz.js:3832 Uncaught TypeError: Cannot read properties of undefined (reading 'Buffer')
    at E_ (ViewersGrid.astro_astro_type_script_index_0_lang.BIjkZuiz.js:3832:65274)

Details

The error occurs in the minified production build where the code looks like:

function E_() {
    if (sc)
        return bi;
    sc = 1,
    bi = i(typeof Buffer < "u" && Buffer) || i(bi.Buffer) || i(typeof window < "u" && window.Buffer) || bi.Buffer;
    function i(e) {
        return e && e.isBuffer && e
    }
    return bi
}

This corresponds to the following source code in development:

// node_modules/msgpack-lite/lib/buffer-global.js
var require_buffer_global = __commonJS({
  "node_modules/msgpack-lite/lib/buffer-global.js"(exports, module) {
    module.exports = c("undefined" !== typeof Buffer && Buffer) || c(exports.Buffer) || c("undefined" !== typeof window && window.Buffer) || exports.Buffer;
    function c(B) {
      return B && B.isBuffer && B;
    }
  }
});

Environment

  • Development: ✅ Works
  • Production Build: ❌ Fails

What's the expected result?

It should be loaded as expected.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-zm7nbaeo?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 11, 2024
@ascorbic
Copy link
Contributor

I can't reproduce this. Your repro builds fine for me. Can you check and update the instructions so that we can reproduce it.

@ascorbic ascorbic added needs repro Issue needs a reproduction and removed needs triage Issue needs to be triaged labels Dec 11, 2024
Copy link
Contributor

Hello @anonymous-0545. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

@anonymous-0545
Copy link
Author

anonymous-0545 commented Dec 11, 2024

I can't reproduce this. Your repro builds fine for me. Can you check and update the instructions so that we can reproduce it.

Hello @ascorbic , thank you so much for the prompt response. I think I was not clear enough, build and serve don't have an issue but if you open the console you can see the error on the repro. I'm not using any functionality of the library on repro, just importing it on the index page but it fails on the runtime of the build. Here is the error when you visit the homepage of the dist folder you can see the following error on the browser console:

index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'Buffer')
    at me (index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1:119)
    at index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1:3792
    at H (index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1:4784)
    at Sr (index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1:4856)
    at Nr (index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1:15473)
    at be (index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1:18519)
    at Ae (index.astro_astro_type_script_index_0_lang.BRafoyqh.js:1:18872)
    at Ye (index.astro_astro_type_script_index_0_lang.BRafoyqh.js:22:2083)
    at index.astro_astro_type_script_index_0_lang.BRafoyqh.js:22:2215

In other words, it builds without an error however, when serving the produced dist it throws the above error when it reaches the script in runtime. Please let me know if I can make any further clarifications.

@ascorbic
Copy link
Contributor

ascorbic commented Dec 12, 2024

It looks like it's related to how the library is trying to load Buffer. It looks like Vite is getting in a mess when minifying this code:

https://github.com/kawanet/msgpack-lite/blob/master/lib/buffer-global.js#L3-L7

It works in dev because it's not minified. There may be some Vite settings you could use to fix this but I'm not sure. I tried a couple of things without much luck. It comes down to the fact that this is a really old library that isn't playing nicely with a modern bundler.

@anonymous-0545
Copy link
Author

I see, I didn't notice that is not actively maintained and migrated to tiny-msgpack, and the issue is resolved, thanks.

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

No branches or pull requests

2 participants