@@ -77,7 +77,7 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
77
77
if ( ! path . startsWith ( "/_npm/" ) ) throw new Error ( `invalid npm path: ${ path } ` ) ;
78
78
const filePath = join ( root , ".observablehq" , "cache" , path ) ;
79
79
if ( existsSync ( filePath ) ) return filePath ;
80
- let promise = npmRequests . get ( path ) ;
80
+ let promise = npmRequests . get ( filePath ) ;
81
81
if ( promise ) return promise ; // coalesce concurrent requests
82
82
promise = ( async function ( ) {
83
83
const specifier = extractNpmSpecifier ( path ) ;
@@ -96,8 +96,8 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
96
96
}
97
97
return filePath ;
98
98
} ) ( ) ;
99
- promise . catch ( ( ) => { } ) . then ( ( ) => npmRequests . delete ( path ) ) ;
100
- npmRequests . set ( path , promise ) ;
99
+ promise . catch ( ( ) => { } ) . then ( ( ) => npmRequests . delete ( filePath ) ) ;
100
+ npmRequests . set ( filePath , promise ) ;
101
101
return promise ;
102
102
}
103
103
@@ -177,9 +177,7 @@ export async function getDependencyResolver(
177
177
} ;
178
178
}
179
179
180
- let npmVersionCache : Promise < Map < string , string [ ] > > ;
181
-
182
- async function initializeNpmVersionCache ( root : string ) : typeof npmVersionCache {
180
+ async function initializeNpmVersionCache ( root : string ) : Promise < Map < string , string [ ] > > {
183
181
const cache = new Map < string , string [ ] > ( ) ;
184
182
const cacheDir = join ( root , ".observablehq" , "cache" , "_npm" ) ;
185
183
try {
@@ -207,12 +205,19 @@ async function initializeNpmVersionCache(root: string): typeof npmVersionCache {
207
205
return cache ;
208
206
}
209
207
208
+ const npmVersionCaches = new Map < string , Promise < Map < string , string [ ] > > > ( ) ;
210
209
const npmVersionRequests = new Map < string , Promise < string > > ( ) ;
211
210
211
+ function getNpmVersionCache ( root : string ) : Promise < Map < string , string [ ] > > {
212
+ let cache = npmVersionCaches . get ( root ) ;
213
+ if ( ! cache ) npmVersionCaches . set ( root , ( cache = initializeNpmVersionCache ( root ) ) ) ;
214
+ return cache ;
215
+ }
216
+
212
217
async function resolveNpmVersion ( root : string , specifier : NpmSpecifier ) : Promise < string > {
213
218
const { name, range} = specifier ;
214
219
if ( range && / ^ \d + \. \d + \. \d + ( [ - + ] .* ) ? $ / . test ( range ) ) return range ; // exact version specified
215
- const cache = await ( npmVersionCache ??= initializeNpmVersionCache ( root ) ) ;
220
+ const cache = await getNpmVersionCache ( root ) ;
216
221
const versions = cache . get ( specifier . name ) ;
217
222
if ( versions ) for ( const version of versions ) if ( ! range || satisfies ( version , range ) ) return version ;
218
223
const href = `https://data.jsdelivr.com/v1/packages/npm/${ name } /resolved${ range ? `?specifier=${ range } ` : "" } ` ;
0 commit comments