Comments (5)
See the example below.
const fs = require('node:fs');
const { pipeline } = require('node:stream/promises');
const http = require('node:http');
const { request } = require('undici');
const server = http.createServer((req, res) => {
res.writeHead(200);
setTimeout(() => {
res.end();
console.log('Request closed after 10 seconds');
}, 10000);
const writeZeroes = () => {
res.write(Buffer.alloc(1024, 0));
setImmediate(writeZeroes);
};
writeZeroes();
});
server.listen();
const serverUrl = `http://localhost:${server.address().port}`;
console.log(`Server listening on ${serverUrl}`);
async function test() {
const { body } = await request(serverUrl, {
throwOnError: true,
bodyTimeout: 1000,
});
console.log('Reading body...');
await pipeline(body, fs.createWriteStream('/tmp/body-timeout-test'));
console.log('Reading body finished');
}
test();
Output:
Server listening on http://localhost:63483
Reading body...
Request closed after 10 seconds
Reading body finished
Reading the response body should timeout after 1 second, but it does not.
from undici.
Thank you, we will try the AbortSignal, but I think the documentation is a bit misleading:
- bodyTimeout
number | null
(optional) - The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use0
to disable it entirely. Defaults to 300 seconds.
Based on this, I would expect the request to be aborted after the timeout.
from undici.
Can you please include a server as well in your example? Otherwise reproducing it is network dependent.
from undici.
If I'm not mistaken, the bodyTimeout
is only applied on idle situations between receiving body's data.
As you keep writing to the socket in an interval, and for instance the client keeps receiving data from the server, the timeout is reset on every new chunk, delaying its effect.
e.g. if I alter your example like this:
const fs = require('node:fs')
const { pipeline } = require('node:stream/promises')
const http = require('node:http')
const { request } = require('undici')
const server = http.createServer((req, res) => {
res.writeHead(200)
setTimeout(() => {
res.end()
console.log('Request closed after 10 seconds')
}, 10000).unref()
const writeZeroes = () => {
res.write(Buffer.alloc(1024, 0))
setInterval(writeZeroes, 2000)
}
writeZeroes()
})
server.listen(0, () => {
const serverUrl = `http://localhost:${server.address().port}`
console.log(`Server listening on ${serverUrl}`)
async function test () {
console.log('Sending request...')
const { body } = await request(serverUrl, {
throwOnError: true,
bodyTimeout: 1000
})
console.log('Reading body...')
await pipeline(body, fs.createWriteStream('/tmp/body-timeout-test'))
console.log('Reading body finished')
}
test()
})
Then the timeout will effectively apply.
You can use AbortSignal.timeout
to enforce a specific timeout independent of the bodyTimeout
one
from undici.
See what you mean, a PR to support us improving the docs is always welcomed!
from undici.
Related Issues (20)
- Fetch failed with UND_ERR_CONNECT_TIMEOUT undici error on Next.js serverless function HOT 1
- 'interceptors' export doesn't work like in the docs HOT 1
- Nightly tests are failing HOT 4
- publish autobahn test results on the docs site HOT 7
- Nightly tests are failing HOT 2
- Improve support for pre-shared sessions (PSK) HOT 3
- Request constructor: Failed to parse URL from ___ HOT 2
- Broken links in docs webpage HOT 2
- Failing JSON parsing in Node 22 HOT 13
- undici/types/index.d.ts:21:8: Type error TS1192: Module '"[email protected]/node_modules/undici/types/interceptors"' has no default export HOT 6
- Node 22.3.0 regression: undici sends invalid origin header in some cases HOT 2
- fetch arrayBuffer() leaks random data from process memory HOT 18
- build: husky install is deprecated HOT 1
- Ability to externalize WASM was broken HOT 11
- Measure request latency HOT 3
- dns round-robin interceptor + cache HOT 3
- async dispatch handler helper
- Cannot convert argument to a ByteString because the character at index 46 has a value of 65286 which is greater than 255 HOT 2
- Undici.Request and AbortController doesn't work well HOT 22
- The RetryHandler receives a duplicate body when the server does not support Range requests. HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from undici.