11import { Pool , request } from 'undici'
22import * as semver from 'semver'
3- import { CACHE_TTL , JSDELIVR_CDN_URL , MAX_CONCURRENT_REQUESTS , REQUEST_TIMEOUT } from '../config'
3+ import { JSDELIVR_CDN_URL , MAX_CONCURRENT_REQUESTS , REQUEST_TIMEOUT } from '../config'
44import { getAllPackageData } from './npm-registry'
5- import { persistentCache } from './persistent-cache'
5+ import { packageCache , PackageVersionData } from './cache-manager'
6+ import { ConsoleUtils } from '../ui/utils'
67import { OnBatchReadyCallback } from '../types'
78
89// Create a persistent connection pool for jsDelivr CDN with optimal settings
@@ -19,13 +20,6 @@ const jsdelivrPool = new Pool('https://cdn.jsdelivr.net', {
1920const BATCH_SIZE = 5
2021const BATCH_TIMEOUT_MS = 500
2122
22- // In-memory cache for package data
23- interface CacheEntry {
24- data : { latestVersion : string ; allVersions : string [ ] }
25- timestamp : number
26- }
27- const packageCache = new Map < string , CacheEntry > ( )
28-
2923/**
3024 * Fetches package.json from jsdelivr CDN for a specific version tag using undici pool.
3125 * Uses connection pooling and keep-alive for maximum performance.
@@ -81,8 +75,8 @@ export async function getAllPackageDataFromJsdelivr(
8175 currentVersions ?: Map < string , string > ,
8276 onProgress ?: ( currentPackage : string , completed : number , total : number ) => void ,
8377 onBatchReady ?: OnBatchReadyCallback
84- ) : Promise < Map < string , { latestVersion : string ; allVersions : string [ ] } > > {
85- const packageData = new Map < string , { latestVersion : string ; allVersions : string [ ] } > ( )
78+ ) : Promise < Map < string , PackageVersionData > > {
79+ const packageData = new Map < string , PackageVersionData > ( )
8680
8781 if ( packageNames . length === 0 ) {
8882 return packageData
@@ -92,8 +86,7 @@ export async function getAllPackageDataFromJsdelivr(
9286 let completedCount = 0
9387
9488 // Batch buffer for progressive updates
95- let batchBuffer : Array < { name : string ; data : { latestVersion : string ; allVersions : string [ ] } } > =
96- [ ]
89+ let batchBuffer : Array < { name : string ; data : PackageVersionData } > = [ ]
9790 let batchTimer : NodeJS . Timeout | null = null
9891
9992 // Helper to flush the current batch
@@ -109,10 +102,7 @@ export async function getAllPackageDataFromJsdelivr(
109102 }
110103
111104 // Helper to add package to batch and flush if needed
112- const addToBatch = (
113- packageName : string ,
114- data : { latestVersion : string ; allVersions : string [ ] }
115- ) => {
105+ const addToBatch = ( packageName : string , data : PackageVersionData ) => {
116106 if ( onBatchReady ) {
117107 batchBuffer . push ( { name : packageName , data } )
118108
@@ -130,32 +120,15 @@ export async function getAllPackageDataFromJsdelivr(
130120 const fetchPackageWithFallback = async ( packageName : string ) : Promise < void > => {
131121 const currentVersion = currentVersions ?. get ( packageName )
132122
133- // Try to get from in-memory cache first (fastest)
134- const memoryCached = packageCache . get ( packageName )
135- if ( memoryCached && Date . now ( ) - memoryCached . timestamp < CACHE_TTL ) {
136- packageData . set ( packageName , memoryCached . data )
137- completedCount ++
138- if ( onProgress ) {
139- onProgress ( packageName , completedCount , total )
140- }
141- addToBatch ( packageName , memoryCached . data )
142- return
143- }
144-
145- // Try persistent disk cache (fast, survives restarts)
146- const diskCached = persistentCache . get ( packageName )
147- if ( diskCached ) {
148- // Also populate in-memory cache for subsequent accesses
149- packageCache . set ( packageName , {
150- data : diskCached ,
151- timestamp : Date . now ( ) ,
152- } )
153- packageData . set ( packageName , diskCached )
123+ // Use CacheManager for unified caching (memory + disk)
124+ const cached = packageCache . get ( packageName )
125+ if ( cached ) {
126+ packageData . set ( packageName , cached )
154127 completedCount ++
155128 if ( onProgress ) {
156129 onProgress ( packageName , completedCount , total )
157130 }
158- addToBatch ( packageName , diskCached )
131+ addToBatch ( packageName , cached )
159132 return
160133 }
161134
@@ -187,13 +160,8 @@ export async function getAllPackageDataFromJsdelivr(
187160
188161 if ( result ) {
189162 packageData . set ( packageName , result )
190- // Cache in memory
191- packageCache . set ( packageName , {
192- data : result ,
193- timestamp : Date . now ( ) ,
194- } )
195- // Cache to disk for persistence
196- persistentCache . set ( packageName , result )
163+ // CacheManager handles both memory and disk caching
164+ packageCache . set ( packageName , result )
197165 addToBatch ( packageName , result )
198166 }
199167
@@ -212,18 +180,13 @@ export async function getAllPackageDataFromJsdelivr(
212180 allVersions . push ( majorResult . version )
213181 }
214182
215- const result = {
183+ const result : PackageVersionData = {
216184 latestVersion,
217185 allVersions : allVersions . sort ( semver . rcompare ) ,
218186 }
219187
220- // Cache the result in memory
221- packageCache . set ( packageName , {
222- data : result ,
223- timestamp : Date . now ( ) ,
224- } )
225- // Cache to disk for persistence
226- persistentCache . set ( packageName , result )
188+ // Cache the result using CacheManager (handles both memory and disk)
189+ packageCache . set ( packageName , result )
227190
228191 packageData . set ( packageName , result )
229192 completedCount ++
@@ -240,13 +203,8 @@ export async function getAllPackageDataFromJsdelivr(
240203
241204 if ( result ) {
242205 packageData . set ( packageName , result )
243- // Cache in memory
244- packageCache . set ( packageName , {
245- data : result ,
246- timestamp : Date . now ( ) ,
247- } )
248- // Cache to disk for persistence
249- persistentCache . set ( packageName , result )
206+ // CacheManager handles both memory and disk caching
207+ packageCache . set ( packageName , result )
250208 addToBatch ( packageName , result )
251209 }
252210 } catch ( npmError ) {
@@ -267,11 +225,11 @@ export async function getAllPackageDataFromJsdelivr(
267225 flushBatch ( )
268226
269227 // Flush persistent cache to disk
270- persistentCache . flush ( )
228+ packageCache . flush ( )
271229
272- // Clear the progress line and show completion time if no custom progress handler
230+ // Clear the progress line if no custom progress handler
273231 if ( ! onProgress ) {
274- process . stdout . write ( '\r' + ' ' . repeat ( 80 ) + '\r' )
232+ ConsoleUtils . clearProgress ( )
275233 }
276234
277235 return packageData
0 commit comments