Skip to content

undici is leaking into browser environment #45

@kezike

Description

@kezike

Description

@dmitrizagidulin and I have been investigating an issue importing http-client into a browser context. The issue is coming from the undici library, which is not compatible with the browser. We noticed that there are provisions in the package.json to exclude problematic files from browser-compatible exports, but it does not seem to fix the issue. We are indirectly using this library as a second-order dependency in a TypeScript + Next.js application, but it seems we will need to make modifications to this library to get it to work.

Solutions tried

  • Overriding the webpack rules in next.config.js to use the same file redirection that http-client uses in its package.json
  • Instructing TypeScript (via tsconfig.json) to use the same file replacement as http-client:
    {
      "compilerOptions": {
        "baseUrl": "./",
        "paths": {
          "@digitalbazaar/http-client/lib/agentCompatibility.js": ["node_modules/@digitalbazaar/http-client/lib/agentCompatibility-browser.js"],
          "@digitalcredentials/security-document-loader/node_modules/@digitalbazaar/http-client/lib/agentCompatibility.js": ["node_modules/@digitalbazaar/http-client/lib/agentCompatibility-browser.js"]
        }
    }
    
  • Modifying environment-based export directives in package.json
  • Modifying configuration in rollup.config.js

Reproduction steps

  • Install application in a TypeScript + Next.js application with different combinations of solutions listed above
  • Run next dev
  • Observe error

Error stack

This is the undici error:

Screenshot 2024-03-18 at 5 54 01 PM

This is the import trace (you'll notice that agentCompatibility.js was not replaced by agentCompatibility-browser.js, as specified in package.json):

Screenshot 2024-03-18 at 5 55 54 PM

Tooling

  • TypeScript
  • Next.js

Next steps

We think the best approach will involve a direct PR to this repo that fixes browser exports in this setup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions