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

[Bug?]: vinxi build produces code that uses wrong APIs (import fetch from "node-fetch") #1664

Open
2 tasks done
nbrugger-tgm opened this issue Oct 28, 2024 · 4 comments
Open
2 tasks done
Labels
bug Something isn't working vinxi related to vinxi

Comments

@nbrugger-tgm
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

When importing and using APIs like import fetch from "node-fetch"; the types, and APIs that are used at runtime WILDLY differ between npm run dev and npm run build && node .output/server/index.mjs.

Observed differences:

  1. using await fetch(myUrl, { compress: false });
    • run dev: the response returned is as expected not decompressed when using .arrayBuffer()
    • run build: the response ignores the compress option and decompresses the response of .arrayBuffer()
  2. prototype of NodeJS.ReadableStream (console.log(await fetch(myUrl).body)
    • run dev
      PassThrough {
          _transform: [Function],
          .... many more props .....
          find: [Function],
      }
      
    • run build
      [ReadableStream] {
         locked: [Getter],
         cancel: [Function: cancel],
         getReader: [Function: getReader],
         pipeThrough: [Function: pipeThrough],
         pipeTo: [Function: pipeTo],
         tee: [Function: tee],
         values: [Function: values]
       }
      

In general it seems that at runtime (npm run build) import fetch from "node-fetch"; the fetch is replaced with the browser/DOM fetch API which does not happen at npm run dev.

Expected behavior 🤔

When importing import fetch from "node-fetch"; the output and behaviour of the API in npm run dev and npm run build is completely the same. While implementation might change behind the scene on building behaviour and API contracts shall not be broken.

Steps to reproduce 🕹

Steps:

  1. go to https://stackblitz.com/edit/github-qd1tfd?file=src%2Froutes%2Findex.ts
  2. Clone it to your laptop: the reproducer does not work on stackblitz since the app runs seemingly in a web-worker and seems NOT to use "real" node-apis
  3. run npm install && npm run dev goto localhost:3000 see the Content size + body prototype
  4. run npm run build && node .output/server/index.mjs repeat and see the difference

Context 🔦

There are features in the node-fetch fetch not present in the browser / dom fetch (the compress option).

Your environment 🌎

@solidjs/start: 1.0.9

(see the stackblitz from reproducer)
@nbrugger-tgm nbrugger-tgm added the bug Something isn't working label Oct 28, 2024
@ryansolid
Copy link
Member

ryansolid commented Oct 31, 2024

I see.. I wonder if this was to try to homogenize all the fetch on the web version.. I don't think Start does this but Vinxi might be. Actually the fact this happens in build makes me possibly suspicious of Nitro here.

@ryansolid ryansolid added the vinxi related to vinxi label Oct 31, 2024
@ryoid
Copy link
Contributor

ryoid commented Nov 27, 2024

To share some information to help trace this, it might be coming from unenv which Nitro uses. I've experienced similar behaviour in build.

You may consider doing this to circumvent the automatic replacement

export default defineConfig({
  server: {
    alias: {
      "node-fetch": "node-fetch",
    },
  },
});

@nbrugger-tgm
Copy link
Author

Where would I have to do this? vinxi.js? Vite plugin?

@ryoid
Copy link
Contributor

ryoid commented Nov 27, 2024

Where would I have to do this? vinxi.js? Vite plugin?

In app.config.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vinxi related to vinxi
Projects
None yet
Development

No branches or pull requests

3 participants