Skip to content

Commit

Permalink
fix: clear version cache on each generator instantiation (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Sep 14, 2024
1 parent fb91f9b commit 3687db1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/providers/esmsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function resolveLatestTarget(
parentUrl: string
): Promise<ExactPackage | null> {
const { registry, name, range, unstable } = target;
const versions = await fetchVersions(name);
const versions = await fetchVersions.call(this, name);
const semverRange = new SemverRange(String(range) || "*", unstable);
const version = semverRange.bestMatch(versions, unstable);
if (version) {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/jsdelivr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function resolveLatestTarget(
parentUrl: string
): Promise<ExactPackage | null> {
const { registry, name, range, unstable } = target;
const versions = await fetchVersions(name);
const versions = await fetchVersions.call(this, name);
const semverRange = new SemverRange(String(range) || "*", unstable);
const version = semverRange.bestMatch(versions, unstable);
if (version) {
Expand Down
57 changes: 36 additions & 21 deletions src/providers/jspm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ const apiUrl = "https://api.jspm.io/";
const BUILD_POLL_TIME = 5 * 60 * 1000;
const BUILD_POLL_INTERVAL = 5 * 1000;

interface JspmCache {
lookupCache: Map<string, Promise<ExactPackage>>;
versionsCacheMap: Map<string, string[]>;
resolveCache: Record<
string,
{
latest: Promise<ExactPackage | null>;
majors: Record<string, Promise<ExactPackage | null>>;
minors: Record<string, Promise<ExactPackage | null>>;
tags: Record<string, Promise<ExactPackage | null>>;
}
>;
cachedErrors: Map<string, Promise<boolean>>;
buildRequested: Map<string, Promise<void>>;
}

export const supportedLayers = ["default", "system"];

export async function pkgToUrl(
Expand Down Expand Up @@ -59,22 +75,20 @@ export function parseUrlPkg(url: string) {
}
}

let resolveCache: Record<
string,
{
latest: Promise<ExactPackage | null>;
majors: Record<string, Promise<ExactPackage | null>>;
minors: Record<string, Promise<ExactPackage | null>>;
tags: Record<string, Promise<ExactPackage | null>>;
function getJspmCache (resolver: Resolver): JspmCache {
const jspmCache = resolver.context.jspmCache;
if (!resolver.context.jspmCache) {
return resolver.context.jspmCache = {
lookupCache: new Map(),
versionsCacheMap: new Map(),
resolveCache: {},
cachedErrors: new Map(),
buildRequested: new Map(),
};
}
> = {};

export function clearResolveCache() {
resolveCache = {};
return jspmCache;
}

const cachedErrors = new Map();

async function checkBuildOrError(
resolver: Resolver,
pkgUrl: string,
Expand All @@ -84,6 +98,7 @@ async function checkBuildOrError(
if (pcfg) {
return true;
}
const { cachedErrors } = getJspmCache(resolver);
// no package.json! Check if there's a build error:
if (cachedErrors.has(pkgUrl))
return cachedErrors.get(pkgUrl);
Expand All @@ -104,14 +119,14 @@ async function checkBuildOrError(
return cachedErrorPromise;
}

const buildRequested = new Map();

async function ensureBuild(resolver: Resolver, pkg: ExactPackage, fetchOpts: any) {
if (await checkBuildOrError(resolver, await pkgToUrl(pkg, "default"), fetchOpts))
return;

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

const { buildRequested } = getJspmCache(resolver);

// no package.json AND no build error -> post a build request
// once the build request has been posted, try polling for up to 2 mins
if (buildRequested.has(fullName))
Expand Down Expand Up @@ -158,6 +173,8 @@ export async function resolveLatestTarget(
return pkg;
}

const { resolveCache } = getJspmCache(this);

const cache = (resolveCache[target.registry + ":" + target.name] =
resolveCache[target.registry + ":" + target.name] || {
latest: null,
Expand Down Expand Up @@ -275,8 +292,6 @@ function pkgToLookupUrl(pkg: ExactPackage, edge = false) {
}`;
}

const lookupCache = new Map();

async function lookupRange(
this: Resolver,
registry: string,
Expand All @@ -285,6 +300,7 @@ async function lookupRange(
unstable: boolean,
parentUrl?: string
): Promise<ExactPackage | null> {
const { lookupCache } = getJspmCache(this);
const url = pkgToLookupUrl({ registry, name, version: range }, unstable);
if (lookupCache.has(url))
return lookupCache.get(url);
Expand All @@ -294,7 +310,7 @@ async function lookupRange(
return { registry, name, version: version.trim() };
} else {
// not found
const versions = await fetchVersions(name);
const versions = await fetchVersions.call(this, name);
const semverRange = new SemverRange(String(range) || "*", unstable);
const version = semverRange.bestMatch(versions, unstable);

Expand All @@ -312,9 +328,8 @@ async function lookupRange(
return lookupPromise;
}

const versionsCacheMap = new Map<string, string[]>();

export async function fetchVersions(name: string): Promise<string[]> {
export async function fetchVersions(this: Resolver, name: string): Promise<string[]> {
const { versionsCacheMap } = getJspmCache(this);
if (versionsCacheMap.has(name)) {
return versionsCacheMap.get(name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/skypack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function resolveLatestTarget(
parentUrl: string
): Promise<ExactPackage | null> {
const { registry, name, range, unstable } = target;
const versions = await fetchVersions(name);
const versions = await fetchVersions.call(this, name);
const semverRange = new SemverRange(String(range) || "*", unstable);
const version = semverRange.bestMatch(versions, unstable);
if (version) {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/unpkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function resolveLatestTarget(
parentUrl: string
): Promise<ExactPackage | null> {
const { registry, name, range, unstable } = target;
const versions = await fetchVersions(name);
const versions = await fetchVersions.call(this, name);
const semverRange = new SemverRange(String(range) || "*", unstable);
const version = semverRange.bestMatch(versions, unstable);
if (version) {
Expand Down
6 changes: 4 additions & 2 deletions src/trace/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class Resolver {
traceCjs: boolean;
traceTs: boolean;
traceSystem: boolean;
context: Record<string, any>;
constructor({
env,
log,
Expand Down Expand Up @@ -132,6 +133,7 @@ export class Resolver {
this.traceCjs = traceCjs;
this.traceTs = traceTs;
this.traceSystem = traceSystem;
this.context = {};
}

addCustomProvider(name: string, provider: Provider) {
Expand Down Expand Up @@ -359,8 +361,8 @@ export class Resolver {
const resolveLatestTarget = getProvider(
provider,
this.providers
).resolveLatestTarget.bind(this);
const pkg = await resolveLatestTarget(latestTarget, layer, parentUrl);
).resolveLatestTarget;
const pkg = await resolveLatestTarget.call(this, latestTarget, layer, parentUrl);
if (pkg) return pkg;

if (provider === "nodemodules") {
Expand Down

0 comments on commit 3687db1

Please sign in to comment.