在代理文件流时,Windows 命名管道会卡死
// Create ~2MB test file
async function createTestFile() {
await mkdir(TEST_DIR, { recursive: true })
const content = 'x'.repeat(2_000_000) // 2MB
await writeFile(TEST_FILE, content)
console.log(Created ${content.length} byte test file\n)
}
// Server that streams a file const fileServer = createServer(async (req, res) => { const stats = await stat(TEST_FILE) res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': stats.size, }) createReadStream(TEST_FILE).pipe(res) })
const proxyUndici = createServer(async (req, res) => { console.log('[Proxy] Received request, fetching from file server via undici...')
const agent = new Agent({ connect: { socketPath } }) const response = await fetch('http://localhost/', { dispatcher: agent })
console.log([Proxy] Got response: ${response.status} ${response.statusText})
console.log([Proxy] Headers:, Object.fromEntries(response.headers))
res.writeHead(response.status, Object.fromEntries(response.headers))
const reader = response.body.getReader() let chunks = 0 let totalBytes = 0
内容来源: nodejs/undici