Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,32 @@ const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const zlib = require('zlib');
const { request: undiciRequest, Agent: UndiciAgent, ProxyAgent: UndiciProxyAgent } = require('undici');
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;
}
}
Comment on lines +10 to +35

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

const { getProxyForUrl } = require('proxy-from-env');
require('dotenv').config();
const {
Expand Down