hapijs / hoek

Node utilities shared among the extended hapi universe
Other
481 stars 171 forks source link

Handle very long timeouts in Hoek.wait() #373

Closed kanongil closed 2 years ago

kanongil commented 2 years ago

This enhances Hoek.wait() to properly handle timeouts of more than 2^31 - 1 ms.

The existing implementation makes no mention of the fact that it is based on setTimeout() and its inherent limitation of 2^31 - 1 ms. As it is right now, any value larger than this (which equates to around 25 days), is treated as if 1 was passed, and triggers a TimeoutOverflowWarning from node:

(node:99363) TimeoutOverflowWarning: 2147483648 does not fit into a 32-bit signed integer.
Timeout duration was set to 1.

While fixing this, I also took the opportunity to support timeout values using BigInt numbers, which I think makes sense for a modern API.

Note that the implementation now returns a never resolving Promise for exceptionally long timeout values (including Infinity), making it work similar to Hoek.block().

Alternatives considered

Update the docs to note the limitation, and immediately throw an error if passed values larger than 2^31 - 1.

The implementation could also be more accurate for longs waits, but it requires manually tracking elapsed time, which does not seem worth the effort. The API should still wait at least the time specified.