@@ -14,33 +14,58 @@ async function getObjectsTle(noradId: number) {
1414
1515 if ( ! tleData || isStale ) {
1616 let allTles = ( await kv . get ( "active_tle" ) ) as string | null ;
17- if ( ! allTles ) {
17+ const activeTimestamp : number | undefined = await kv . get ( "active_timestamp_tle" ) ;
18+ const activeIsStale = activeTimestamp ? now - activeTimestamp > config . cacheActiveDuration : true ;
19+
20+ if ( ! allTles || activeIsStale ) {
21+ // Active group is missing or stale — fetch fresh data and re-cache all satellites
1822 await fetchTle ( "active" ) ;
19- }
20- allTles = ( await kv . get ( "active_tle" ) ) as string | null ;
23+ allTles = ( await kv . get ( "active_tle" ) ) as string | null ;
2124
22- // If we still don't have the active TLEs then something went wrong with fetching, so we throw an error
23- if ( ! allTles ) {
24- log . error ( "Failed to fetch active TLEs from Celestrak" ) ;
25- throw new Error ( "Failed to fetch active TLEs from Celestrak" ) ;
26- }
27- const lines = allTles . split ( "\n" ) ;
28- const setPromises : Promise < boolean > [ ] = [ ] ;
29- for ( let i = 0 ; i < lines . length ; i += 3 ) {
30- const idLine = lines [ i + 0 ] ;
31- const tleLine1 = lines [ i + 1 ] ;
32- const tleLine2 = lines [ i + 2 ] ;
33-
34- if ( idLine && tleLine1 && tleLine2 ) {
35- const parsed = tle . parse ( `${ idLine } \n${ tleLine1 } \n${ tleLine2 } ` ) ;
36- const tleString = `${ idLine } \n${ tleLine1 } \n${ tleLine2 } ` ;
37- if ( parsed . number === noradId ) {
38- tleData = tleString ;
25+ // If we still don't have the active TLEs then something went wrong with fetching, so we throw an error
26+ if ( ! allTles ) {
27+ log . error ( "Failed to fetch active TLEs from Celestrak" ) ;
28+ throw new Error ( "Failed to fetch active TLEs from Celestrak" ) ;
29+ }
30+
31+ const lines = allTles . split ( "\n" ) ;
32+ const setPromises : Promise < boolean > [ ] = [ ] ;
33+ for ( let i = 0 ; i < lines . length ; i += 3 ) {
34+ const idLine = lines [ i + 0 ] ;
35+ const tleLine1 = lines [ i + 1 ] ;
36+ const tleLine2 = lines [ i + 2 ] ;
37+
38+ if ( idLine && tleLine1 && tleLine2 ) {
39+ const parsed = tle . parse ( `${ idLine } \n${ tleLine1 } \n${ tleLine2 } ` ) ;
40+ const tleString = `${ idLine } \n${ tleLine1 } \n${ tleLine2 } ` ;
41+ if ( parsed . number === noradId ) {
42+ tleData = tleString ;
43+ }
44+ setPromises . push ( kv . set ( `tle_${ parsed . number } ` , tleString ) ) ;
45+ setPromises . push ( kv . set ( `tle_${ parsed . number } _timestamp` , now ) ) ;
46+ }
47+ }
48+ await Promise . all ( setPromises ) ;
49+ } else {
50+ // Active group is still fresh — just scan for the requested satellite without re-writing everything
51+ log . debug ( `Active TLE group is fresh. Scanning for NORAD ID ${ noradId } without re-caching all satellites.` ) ;
52+ const lines = allTles . split ( "\n" ) ;
53+ for ( let i = 0 ; i < lines . length ; i += 3 ) {
54+ const idLine = lines [ i + 0 ] ;
55+ const tleLine1 = lines [ i + 1 ] ;
56+ const tleLine2 = lines [ i + 2 ] ;
57+
58+ if ( idLine && tleLine1 && tleLine2 ) {
59+ const parsed = tle . parse ( `${ idLine } \n${ tleLine1 } \n${ tleLine2 } ` ) ;
60+ if ( parsed . number === noradId ) {
61+ tleData = `${ idLine } \n${ tleLine1 } \n${ tleLine2 } ` ;
62+ await kv . set ( `tle_${ noradId } ` , tleData ) ;
63+ await kv . set ( `tle_${ noradId } _timestamp` , now ) ;
64+ break ;
65+ }
3966 }
40- setPromises . push ( kv . set ( `tle_${ parsed . number } ` , tleString ) ) ;
4167 }
4268 }
43- await Promise . all ( setPromises ) ;
4469 }
4570
4671 // If we're here then active group doesn't contain the requested NORAD ID, try fetching the TLE directly from Celestrak, but be aware of rate limits so we don't get blocked
@@ -77,6 +102,7 @@ async function getObjectsTle(noradId: number) {
77102
78103 tleData = ( await response . text ( ) ) as string ;
79104 await kv . set ( `tle_${ noradId } ` , tleData ) ;
105+ await kv . set ( `tle_${ noradId } _timestamp` , Date . now ( ) ) ;
80106 await kv . set ( `celestrakTries` , tries + 1 ) ;
81107 await kv . set ( `celestrakLastTry` , Date . now ( ) ) ;
82108 log . debug ( `Successfully fetched TLE for NORAD ID ${ noradId } from Celestrak.` ) ;
0 commit comments