Handle undici module availability more robustly - #102
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting
| let undiciRequest; | ||
| let UndiciAgent; | ||
| let UndiciProxyAgent; | ||
|
|
||
| try { | ||
| ({ request: undiciRequest, Agent: UndiciAgent, ProxyAgent: UndiciProxyAgent } = require('node:undici')); | ||
| } catch (nodeUndiciError) { | ||
| try { | ||
| ({ request: undiciRequest, Agent: UndiciAgent, ProxyAgent: UndiciProxyAgent } = require('undici')); | ||
| } catch (npmUndiciError) { | ||
| const combinedError = new Error( | ||
| 'Failed to load undici. Ensure you are running on Node.js v18+ or install the "undici" package.', | ||
| ); | ||
| combinedError.details = { | ||
| nodeUndiciError: { | ||
| message: nodeUndiciError instanceof Error ? nodeUndiciError.message : String(nodeUndiciError), | ||
| code: nodeUndiciError && typeof nodeUndiciError === 'object' ? nodeUndiciError.code : undefined, | ||
| }, | ||
| npmUndiciError: { | ||
| message: npmUndiciError instanceof Error ? npmUndiciError.message : String(npmUndiciError), | ||
| code: npmUndiciError && typeof npmUndiciError === 'object' ? npmUndiciError.code : undefined, | ||
| }, | ||
| }; | ||
| throw combinedError; | ||
| } | ||
| } |
There was a problem hiding this comment.
Guard against missing ProxyAgent in built‑in undici
The new loading logic prefers require('node:undici') and only falls back to the npm package if the require itself throws. On many Node 18 LTS builds the built‑in node:undici export does not include ProxyAgent, so the destructuring succeeds but UndiciProxyAgent becomes undefined. When a proxy is configured and getDispatcherForUrl attempts new UndiciProxyAgent({ uri }), the server will now throw TypeError: UndiciProxyAgent is not a constructor at runtime, whereas the previous code always used the npm dependency that ships with ProxyAgent. Consider detecting missing exports and falling back to the npm module when ProxyAgent (or other required symbols) is undefined.
Useful? React with 👍 / 👎.
c7f2145 to
c8636e3
Compare
Summary
undiciimplementation when availableundicipackage and surface a descriptive error if neither can be loadedTesting
https://chatgpt.com/codex/tasks/task_e_68ebb8701774832d9460683c4dc96c24