digitalbazaar / digitalbazaar/http-client

`undici` is leaking into browser environment

Open
#45 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
2
Forks
8
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the browser-compatible export provisions in package.json and compare them with the import trace produced by next dev. Then inspect the referenced next.config.js, tsconfig.json, and rollup.config.js alongside agentCompatibility.js and agentCompatibility-browser.js. Done means a TypeScript and Next.js browser build no longer resolves the undici dependency through the server path.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js, typescript, webpack
Domain
build-system, tooling, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.