@@ -16,6 +16,22 @@ const apiUrl = "https://api.jspm.io/";
16
16
const BUILD_POLL_TIME = 5 * 60 * 1000 ;
17
17
const BUILD_POLL_INTERVAL = 5 * 1000 ;
18
18
19
+ interface JspmCache {
20
+ lookupCache : Map < string , Promise < ExactPackage > > ;
21
+ versionsCacheMap : Map < string , string [ ] > ;
22
+ resolveCache : Record <
23
+ string ,
24
+ {
25
+ latest : Promise < ExactPackage | null > ;
26
+ majors : Record < string , Promise < ExactPackage | null > > ;
27
+ minors : Record < string , Promise < ExactPackage | null > > ;
28
+ tags : Record < string , Promise < ExactPackage | null > > ;
29
+ }
30
+ > ;
31
+ cachedErrors : Map < string , Promise < boolean > > ;
32
+ buildRequested : Map < string , Promise < void > > ;
33
+ }
34
+
19
35
export const supportedLayers = [ "default" , "system" ] ;
20
36
21
37
export async function pkgToUrl (
@@ -59,22 +75,20 @@ export function parseUrlPkg(url: string) {
59
75
}
60
76
}
61
77
62
- let resolveCache : Record <
63
- string ,
64
- {
65
- latest : Promise < ExactPackage | null > ;
66
- majors : Record < string , Promise < ExactPackage | null > > ;
67
- minors : Record < string , Promise < ExactPackage | null > > ;
68
- tags : Record < string , Promise < ExactPackage | null > > ;
78
+ function getJspmCache ( resolver : Resolver ) : JspmCache {
79
+ const jspmCache = resolver . context . jspmCache ;
80
+ if ( ! resolver . context . jspmCache ) {
81
+ return resolver . context . jspmCache = {
82
+ lookupCache : new Map ( ) ,
83
+ versionsCacheMap : new Map ( ) ,
84
+ resolveCache : { } ,
85
+ cachedErrors : new Map ( ) ,
86
+ buildRequested : new Map ( ) ,
87
+ } ;
69
88
}
70
- > = { } ;
71
-
72
- export function clearResolveCache ( ) {
73
- resolveCache = { } ;
89
+ return jspmCache ;
74
90
}
75
91
76
- const cachedErrors = new Map ( ) ;
77
-
78
92
async function checkBuildOrError (
79
93
resolver : Resolver ,
80
94
pkgUrl : string ,
@@ -84,6 +98,7 @@ async function checkBuildOrError(
84
98
if ( pcfg ) {
85
99
return true ;
86
100
}
101
+ const { cachedErrors } = getJspmCache ( resolver ) ;
87
102
// no package.json! Check if there's a build error:
88
103
if ( cachedErrors . has ( pkgUrl ) )
89
104
return cachedErrors . get ( pkgUrl ) ;
@@ -104,14 +119,14 @@ async function checkBuildOrError(
104
119
return cachedErrorPromise ;
105
120
}
106
121
107
- const buildRequested = new Map ( ) ;
108
-
109
122
async function ensureBuild ( resolver : Resolver , pkg : ExactPackage , fetchOpts : any ) {
110
123
if ( await checkBuildOrError ( resolver , await pkgToUrl ( pkg , "default" ) , fetchOpts ) )
111
124
return ;
112
125
113
126
const fullName = `${ pkg . name } @${ pkg . version } ` ;
114
127
128
+ const { buildRequested } = getJspmCache ( resolver ) ;
129
+
115
130
// no package.json AND no build error -> post a build request
116
131
// once the build request has been posted, try polling for up to 2 mins
117
132
if ( buildRequested . has ( fullName ) )
@@ -158,6 +173,8 @@ export async function resolveLatestTarget(
158
173
return pkg ;
159
174
}
160
175
176
+ const { resolveCache } = getJspmCache ( this ) ;
177
+
161
178
const cache = ( resolveCache [ target . registry + ":" + target . name ] =
162
179
resolveCache [ target . registry + ":" + target . name ] || {
163
180
latest : null ,
@@ -275,8 +292,6 @@ function pkgToLookupUrl(pkg: ExactPackage, edge = false) {
275
292
} `;
276
293
}
277
294
278
- const lookupCache = new Map ( ) ;
279
-
280
295
async function lookupRange (
281
296
this : Resolver ,
282
297
registry : string ,
@@ -285,6 +300,7 @@ async function lookupRange(
285
300
unstable : boolean ,
286
301
parentUrl ?: string
287
302
) : Promise < ExactPackage | null > {
303
+ const { lookupCache } = getJspmCache ( this ) ;
288
304
const url = pkgToLookupUrl ( { registry, name, version : range } , unstable ) ;
289
305
if ( lookupCache . has ( url ) )
290
306
return lookupCache . get ( url ) ;
@@ -294,7 +310,7 @@ async function lookupRange(
294
310
return { registry, name, version : version . trim ( ) } ;
295
311
} else {
296
312
// not found
297
- const versions = await fetchVersions ( name ) ;
313
+ const versions = await fetchVersions . call ( this , name ) ;
298
314
const semverRange = new SemverRange ( String ( range ) || "*" , unstable ) ;
299
315
const version = semverRange . bestMatch ( versions , unstable ) ;
300
316
@@ -312,9 +328,8 @@ async function lookupRange(
312
328
return lookupPromise ;
313
329
}
314
330
315
- const versionsCacheMap = new Map < string , string [ ] > ( ) ;
316
-
317
- export async function fetchVersions ( name : string ) : Promise < string [ ] > {
331
+ export async function fetchVersions ( this : Resolver , name : string ) : Promise < string [ ] > {
332
+ const { versionsCacheMap } = getJspmCache ( this ) ;
318
333
if ( versionsCacheMap . has ( name ) ) {
319
334
return versionsCacheMap . get ( name ) ;
320
335
}
0 commit comments