Skip to content

Commit

Permalink
Merge pull request #188 from microsoft/bug/refetch-allowed
Browse files Browse the repository at this point in the history
Don’t cache allowedPackageJsonDependencies indefinitely
  • Loading branch information
andrewbranch authored Jan 4, 2021
2 parents 9fea425 + d19d55d commit 3cc9505
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
37 changes: 17 additions & 20 deletions packages/definitions-parser/src/lib/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { joinPaths, readFileSync } from "@definitelytyped/utils";
import { getUrlContentsAsString } from "./utils";
import { getUrlContentsAsString, withCache } from "./utils";

const root = joinPaths(__dirname, "..", "..");
export const dataDirPath = joinPaths(root, "data");
Expand All @@ -15,23 +15,20 @@ export const scopeName = "types";

const allowedPackageJsonDependenciesUrl =
"https://raw.githubusercontent.com/microsoft/DefinitelyTyped-tools/master/packages/definitions-parser/allowedPackageJsonDependencies.txt";
let allowedPackageJsonDependencies: Promise<ReadonlySet<string>>;
export async function getAllowedPackageJsonDependencies(): Promise<ReadonlySet<string>> {
return (
allowedPackageJsonDependencies ||
(allowedPackageJsonDependencies = new Promise<ReadonlySet<string>>(async resolve => {
let raw = readFileSync(joinPaths(root, "allowedPackageJsonDependencies.txt"));
if (process.env.NODE_ENV !== "test") {
try {
raw = await getUrlContentsAsString(allowedPackageJsonDependenciesUrl);
} catch (err) {
console.error(
"Getting the latest allowedPackageJsonDependencies.txt from GitHub failed. Falling back to local copy.\n" +
err.message
);
}

export const getAllowedPackageJsonDependencies = withCache(60 * 60 * 1000, () => {
return new Promise<ReadonlySet<string>>(async resolve => {
let raw = readFileSync(joinPaths(root, "allowedPackageJsonDependencies.txt"));
if (process.env.NODE_ENV !== "test") {
try {
raw = await getUrlContentsAsString(allowedPackageJsonDependenciesUrl);
} catch (err) {
console.error(
"Getting the latest allowedPackageJsonDependencies.txt from GitHub failed. Falling back to local copy.\n" +
err.message
);
}
resolve(new Set(raw.split(/\r?\n/)));
}))
);
}
}
resolve(new Set(raw.split(/\r?\n/)));
});
});
12 changes: 12 additions & 0 deletions packages/definitions-parser/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ export function getUrlContentsAsString(url: string): Promise<string> {
.on("error", reject);
});
}

export function withCache<T>(expiresInMs: number, getValue: () => Promise<T>): () => Promise<T> {
let value: T | undefined;
let resolvedAt: number | undefined;
return async () => {
if (resolvedAt === undefined || Date.now() - resolvedAt > expiresInMs) {
value = await getValue();
resolvedAt = Date.now();
}
return value!;
};
}

0 comments on commit 3cc9505

Please sign in to comment.