
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@eomm/set-huge-timeout
Advanced tools
setTimeout
handles
delay as a 32-bit signed integer. This causes an integer overflow when using delays larger
than 2,147,483,647 ms (about 24.8 days), resulting in the timeout being executed immediately.
This package bypass this limit using a recursive setTimeout
call hiding the complexity!
npm install @eomm/set-huge-timeout
Use it as a normal setTimeout
call, but without caring about the limit!
Note that the returned value is an object instead of a Timeout
object.
The object has two properties:
timeout
: This is the reference to the timeout, so you can clear it using clearTimeout
emitter
: This is an EventEmitter
that emits some utility events:
reschedule
: Emitted when the timeout is rescheduledKeep in mind that the timeout
property is updated at every internal reschedule, so you can
clear the timeout at any time only if you read the timeoutReference.timeout
property, otherwise
you may read an old reference.
const { setHugeTimeout } = require('@eomm/set-huge-timeout')
const oneYear = 31_536_000_000
const timeoutReference = setHugeTimeout((message) => {
console.log(message)
}, oneYear, 'Hello, world!')
timeoutReference.emitter.on('reschedule', (timeLeft) => {
console.log(`Rescheduled to ${timeLeft}`)
})
// Clear the timeout
clearTimeout(timeoutReference.timeout)
This module has a test mode that allows to ease the testing of huge timeouts.
Here is an example using Sinon Fake Timers:
const FakeTimers = require('@sinonjs/fake-timers')
const HugeTimeout = require('@eomm/set-huge-timeout')
const { setHugeTimeout } = HugeTimeout
const clock = FakeTimers.createClock()
// Overwrite the internal setTimeout with the fake one.
// All the huge timeouts will be handled by the fake clock
// after this call.
hugeTimeout.test.overwriteSetTimeout(clock.setTimeout)
const waitMs = 2_147_483_999
// This timeout will be handled by the fake clock
setHugeTimeout(() => {
console.log('Timeout!')
}, waitMs)
// Move the clock forward
clock.tick(waitMs) // Output: Timeout!
// Restore the original setTimeout when it's not needed anymore
hugeTimeout.test.restore()
Copyright Manuel Spigolon, Licensed under MIT.
FAQs
setTimeout without the 32 bit int boundaries
The npm package @eomm/set-huge-timeout receives a total of 1 weekly downloads. As such, @eomm/set-huge-timeout popularity was classified as not popular.
We found that @eomm/set-huge-timeout demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.