Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cache latest semvers resolved remotely #42

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/x/async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Mutex } from "https://deno.land/x/[email protected]/mutex.ts";
72 changes: 68 additions & 4 deletions src/dependency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Maybe } from "../lib/types.ts";
import { Mutex } from "../lib/x/async.ts";
import type { Path, SemVerString } from "./types.ts";
import { parseSemVer } from "./semver.ts";
import { parseSemVer, SEMVER_REGEXP } from "./semver.ts";
import { assertExists } from "../lib/std/assert.ts";

export interface Dependency {
name: string;
Expand Down Expand Up @@ -31,6 +33,23 @@ function parseProps(
async function resolveLatestSemVer(
url: URL,
): Promise<Maybe<SemVerString>> {
await LatestSemverCache.lock(url);
const result = await _resolve(url);
LatestSemverCache.unlock(url);
return result;
}

async function _resolve(
url: URL,
): Promise<Maybe<SemVerString>> {
const cached = LatestSemverCache.get(url);
if (cached) {
return cached;
}
if (cached === null) {
// The dependency is already found to be up to date.
return;
}
const props = parseProps(url);
if (!props) {
// The specifier is does not contain a semver.
Expand All @@ -42,7 +61,9 @@ async function resolveLatestSemVer(
`https://registry.npmjs.org/${props.name}`,
);
if (!response.ok) {
throw new Error(`Failed to fetch npm registry: ${response.statusText}`);
throw new Error(
`Failed to fetch npm registry: ${response.statusText}`,
);
}
const json = await response.json();
if (!json["dist-tags"]?.latest) {
Expand All @@ -53,9 +74,10 @@ async function resolveLatestSemVer(
const latestSemVer: string = json["dist-tags"].latest;
if (latestSemVer === props.version) {
// The dependency is up to date
LatestSemverCache.set(url, null);
return;
}
return latestSemVer as SemVerString;
return LatestSemverCache.set(url, latestSemVer as SemVerString);
}
case "http:":
case "https:": {
Expand All @@ -66,14 +88,19 @@ async function resolveLatestSemVer(
await response.arrayBuffer();
if (!response.redirected) {
// The host did not redirect to a url with semver
LatestSemverCache.set(url, null);
return;
}
const specifierWithLatestSemVer = response.url;
if (specifierWithLatestSemVer === url.href) {
// The dependency is up to date
LatestSemverCache.set(url, null);
return;
}
return parseSemVer(specifierWithLatestSemVer);
return LatestSemverCache.set(
url,
parseSemVer(specifierWithLatestSemVer) as SemVerString,
);
}
case "node:":
case "file:":
Expand All @@ -83,3 +110,40 @@ async function resolveLatestSemVer(
return;
}
}

class LatestSemverCache {
static #mutex = new Map<string, Mutex>();
static #cache = new Map<string, SemVerString | null>();

static lock(url: URL): Promise<void> {
const key = this.getKey(url);
const mutex = this.#mutex.get(key) ??
this.#mutex.set(key, new Mutex()).get(key)!;
return mutex.acquire();
}

static unlock(url: URL): void {
const key = this.getKey(url);
const mutex = this.#mutex.get(key);
assertExists(mutex);
mutex.release();
}

static get(url: URL): SemVerString | null | undefined {
const key = this.getKey(url);
return this.#cache.get(key);
}

static set<T extends SemVerString | null>(
url: URL,
semver: T,
): T {
const key = this.getKey(url);
this.#cache.set(key, semver);
return semver;
}

static getKey(url: URL): string {
return url.href.split(SEMVER_REGEXP)[0];
}
}
2 changes: 1 addition & 1 deletion src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Maybe } from "../lib/types.ts";
import type { SemVerString } from "./types.ts";

// Ref: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const SEMVER_REGEXP =
export const SEMVER_REGEXP =
/@v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/g;

export function parseSemVer(
Expand Down