Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.

Commit 3687db1

Browse files
authored
fix: clear version cache on each generator instantiation (#382)
1 parent fb91f9b commit 3687db1

File tree

6 files changed

+44
-27
lines changed

6 files changed

+44
-27
lines changed

src/providers/esmsh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function resolveLatestTarget(
8585
parentUrl: string
8686
): Promise<ExactPackage | null> {
8787
const { registry, name, range, unstable } = target;
88-
const versions = await fetchVersions(name);
88+
const versions = await fetchVersions.call(this, name);
8989
const semverRange = new SemverRange(String(range) || "*", unstable);
9090
const version = semverRange.bestMatch(versions, unstable);
9191
if (version) {

src/providers/jsdelivr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function resolveLatestTarget(
2929
parentUrl: string
3030
): Promise<ExactPackage | null> {
3131
const { registry, name, range, unstable } = target;
32-
const versions = await fetchVersions(name);
32+
const versions = await fetchVersions.call(this, name);
3333
const semverRange = new SemverRange(String(range) || "*", unstable);
3434
const version = semverRange.bestMatch(versions, unstable);
3535
if (version) {

src/providers/jspm.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ const apiUrl = "https://api.jspm.io/";
1616
const BUILD_POLL_TIME = 5 * 60 * 1000;
1717
const BUILD_POLL_INTERVAL = 5 * 1000;
1818

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+
1935
export const supportedLayers = ["default", "system"];
2036

2137
export async function pkgToUrl(
@@ -59,22 +75,20 @@ export function parseUrlPkg(url: string) {
5975
}
6076
}
6177

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+
};
6988
}
70-
> = {};
71-
72-
export function clearResolveCache() {
73-
resolveCache = {};
89+
return jspmCache;
7490
}
7591

76-
const cachedErrors = new Map();
77-
7892
async function checkBuildOrError(
7993
resolver: Resolver,
8094
pkgUrl: string,
@@ -84,6 +98,7 @@ async function checkBuildOrError(
8498
if (pcfg) {
8599
return true;
86100
}
101+
const { cachedErrors } = getJspmCache(resolver);
87102
// no package.json! Check if there's a build error:
88103
if (cachedErrors.has(pkgUrl))
89104
return cachedErrors.get(pkgUrl);
@@ -104,14 +119,14 @@ async function checkBuildOrError(
104119
return cachedErrorPromise;
105120
}
106121

107-
const buildRequested = new Map();
108-
109122
async function ensureBuild(resolver: Resolver, pkg: ExactPackage, fetchOpts: any) {
110123
if (await checkBuildOrError(resolver, await pkgToUrl(pkg, "default"), fetchOpts))
111124
return;
112125

113126
const fullName = `${pkg.name}@${pkg.version}`;
114127

128+
const { buildRequested } = getJspmCache(resolver);
129+
115130
// no package.json AND no build error -> post a build request
116131
// once the build request has been posted, try polling for up to 2 mins
117132
if (buildRequested.has(fullName))
@@ -158,6 +173,8 @@ export async function resolveLatestTarget(
158173
return pkg;
159174
}
160175

176+
const { resolveCache } = getJspmCache(this);
177+
161178
const cache = (resolveCache[target.registry + ":" + target.name] =
162179
resolveCache[target.registry + ":" + target.name] || {
163180
latest: null,
@@ -275,8 +292,6 @@ function pkgToLookupUrl(pkg: ExactPackage, edge = false) {
275292
}`;
276293
}
277294

278-
const lookupCache = new Map();
279-
280295
async function lookupRange(
281296
this: Resolver,
282297
registry: string,
@@ -285,6 +300,7 @@ async function lookupRange(
285300
unstable: boolean,
286301
parentUrl?: string
287302
): Promise<ExactPackage | null> {
303+
const { lookupCache } = getJspmCache(this);
288304
const url = pkgToLookupUrl({ registry, name, version: range }, unstable);
289305
if (lookupCache.has(url))
290306
return lookupCache.get(url);
@@ -294,7 +310,7 @@ async function lookupRange(
294310
return { registry, name, version: version.trim() };
295311
} else {
296312
// not found
297-
const versions = await fetchVersions(name);
313+
const versions = await fetchVersions.call(this, name);
298314
const semverRange = new SemverRange(String(range) || "*", unstable);
299315
const version = semverRange.bestMatch(versions, unstable);
300316

@@ -312,9 +328,8 @@ async function lookupRange(
312328
return lookupPromise;
313329
}
314330

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);
318333
if (versionsCacheMap.has(name)) {
319334
return versionsCacheMap.get(name);
320335
}

src/providers/skypack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function resolveLatestTarget(
2828
parentUrl: string
2929
): Promise<ExactPackage | null> {
3030
const { registry, name, range, unstable } = target;
31-
const versions = await fetchVersions(name);
31+
const versions = await fetchVersions.call(this, name);
3232
const semverRange = new SemverRange(String(range) || "*", unstable);
3333
const version = semverRange.bestMatch(versions, unstable);
3434
if (version) {

src/providers/unpkg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function resolveLatestTarget(
2929
parentUrl: string
3030
): Promise<ExactPackage | null> {
3131
const { registry, name, range, unstable } = target;
32-
const versions = await fetchVersions(name);
32+
const versions = await fetchVersions.call(this, name);
3333
const semverRange = new SemverRange(String(range) || "*", unstable);
3434
const version = semverRange.bestMatch(versions, unstable);
3535
if (version) {

src/trace/resolver.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export class Resolver {
104104
traceCjs: boolean;
105105
traceTs: boolean;
106106
traceSystem: boolean;
107+
context: Record<string, any>;
107108
constructor({
108109
env,
109110
log,
@@ -132,6 +133,7 @@ export class Resolver {
132133
this.traceCjs = traceCjs;
133134
this.traceTs = traceTs;
134135
this.traceSystem = traceSystem;
136+
this.context = {};
135137
}
136138

137139
addCustomProvider(name: string, provider: Provider) {
@@ -359,8 +361,8 @@ export class Resolver {
359361
const resolveLatestTarget = getProvider(
360362
provider,
361363
this.providers
362-
).resolveLatestTarget.bind(this);
363-
const pkg = await resolveLatestTarget(latestTarget, layer, parentUrl);
364+
).resolveLatestTarget;
365+
const pkg = await resolveLatestTarget.call(this, latestTarget, layer, parentUrl);
364366
if (pkg) return pkg;
365367

366368
if (provider === "nodemodules") {

0 commit comments

Comments
 (0)