@@ -4,6 +4,7 @@ import { checksum } from './utils'
44import { isBun } from './universal/utils'
55
66import type { Handler , HookContainer , LifeCycleStore } from './types'
7+ import { isCloudflareWorker } from './adapter/cloudflare-worker'
78
89export namespace Sucrose {
910 export interface Inference {
@@ -21,6 +22,18 @@ export namespace Sucrose {
2122 export interface LifeCycle extends Partial < LifeCycleStore > {
2223 handler ?: Handler
2324 }
25+
26+ export interface Settings {
27+ /**
28+ * If no sucrose usage is found in time
29+ * it's likely that server is either idle or
30+ * no new compilation is happening
31+ * clear the cache to free up memory
32+ *
33+ * @default 4 * 60 * 1000 + 55 * 1000 (4 minutes 55 seconds)
34+ */
35+ gcTime ?: number | null
36+ }
2437}
2538
2639/**
@@ -579,7 +592,11 @@ export const isContextPassToFunction = (
579592let pendingGC : number | undefined
580593let caches = < Record < number , Sucrose . Inference > > { }
581594
582- export const clearSucroseCache = ( delay = 0 ) => {
595+ export const clearSucroseCache = ( delay : Sucrose . Settings [ 'gcTime' ] ) => {
596+ // Can't setTimeout outside fetch in Cloudflare Worker
597+ if ( delay === null || isCloudflareWorker ( ) ) return
598+ if ( delay === undefined ) delay = 4 * 60 * 1000 + 55 * 1000
599+
583600 if ( pendingGC ) clearTimeout ( pendingGC )
584601
585602 pendingGC = setTimeout ( ( ) => {
@@ -616,7 +633,8 @@ export const sucrose = (
616633 url : false ,
617634 route : false ,
618635 path : false
619- }
636+ } ,
637+ settings : Sucrose . Settings = { }
620638) : Sucrose . Inference => {
621639 const events = < ( Handler | HookContainer ) [ ] > [ ]
622640
@@ -653,7 +671,7 @@ export const sucrose = (
653671 // it's likely that server is either idle or
654672 // no new compilation is happening
655673 // Clear the cache to free up memory
656- clearSucroseCache ( 4 * 60 * 1000 + 55 * 1000 )
674+ clearSucroseCache ( settings . gcTime )
657675
658676 const fnInference : Sucrose . Inference = {
659677 query : false ,
0 commit comments