Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): use max length + iterations for useCookie timeout #24253

Merged
merged 5 commits into from Nov 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 41 additions & 5 deletions packages/nuxt/src/app/composables/cookie.ts
Expand Up @@ -140,10 +140,25 @@ function writeServerCookie (event: H3Event, name: string, value: any, opts: Cook
}
}

/**
* The maximum value allowed on a timeout delay.
*
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value
*/
const MAX_TIMEOUT_DELAY = 2_147_483_647

// custom ref that will update the value to undefined if the cookie expires
function cookieRef<T> (value: T | undefined, delay: number) {
let timeout: NodeJS.Timeout
onScopeDispose(() => { clearTimeout(timeout) })
let interval: NodeJS.Timeout
onScopeDispose(() => {
clearTimeout(timeout)
clearInterval(interval)
})

// Divide delay into intervals to work around the maximum delay value for setTimeout
const intervalsNeeded = Math.max(Math.ceil(delay / MAX_TIMEOUT_DELAY) - 1, 0)
const lastDelay = delay - MAX_TIMEOUT_DELAY * intervalsNeeded
return customRef((track, trigger) => {
return {
get () {
Expand All @@ -152,10 +167,31 @@ function cookieRef<T> (value: T | undefined, delay: number) {
},
set (newValue) {
clearTimeout(timeout)
timeout = setTimeout(() => {
value = undefined
trigger()
}, delay)
clearInterval(interval)

let currentIntervalIdx = 0
if (intervalsNeeded) {
// We need intervals to go around the MAX_TIMEOUT_DELAY of setTimeout
interval = setInterval(() => {
currentIntervalIdx++

if (currentIntervalIdx >= intervalsNeeded) {
// We're on the final interval. Use the remainder as a timeout.
clearInterval(interval)
timeout = setTimeout(() => {
value = undefined
trigger()
}, lastDelay)
}
}, MAX_TIMEOUT_DELAY)
} else {
// The original delay is safe to use in setTimeout
timeout = setTimeout(() => {
value = undefined
trigger()
}, delay)
}

value = newValue
trigger()
}
Expand Down