mirror of
https://github.com/astral-sh/setup-uv.git
synced 2026-03-16 10:05:07 +00:00
Speed up version client by partial response reads
This commit is contained in:
@@ -37,10 +37,13 @@ const mockGetLatestVersionFromNdjson = jest.fn<any>();
|
||||
const mockGetAllVersionsFromNdjson = jest.fn<any>();
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
||||
const mockGetArtifactFromNdjson = jest.fn<any>();
|
||||
// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests.
|
||||
const mockGetHighestSatisfyingVersionFromNdjson = jest.fn<any>();
|
||||
|
||||
jest.unstable_mockModule("../../src/download/versions-client", () => ({
|
||||
getAllVersions: mockGetAllVersionsFromNdjson,
|
||||
getArtifact: mockGetArtifactFromNdjson,
|
||||
getHighestSatisfyingVersion: mockGetHighestSatisfyingVersionFromNdjson,
|
||||
getLatestVersion: mockGetLatestVersionFromNdjson,
|
||||
}));
|
||||
|
||||
@@ -81,6 +84,7 @@ describe("download-version", () => {
|
||||
mockGetLatestVersionFromNdjson.mockReset();
|
||||
mockGetAllVersionsFromNdjson.mockReset();
|
||||
mockGetArtifactFromNdjson.mockReset();
|
||||
mockGetHighestSatisfyingVersionFromNdjson.mockReset();
|
||||
mockGetAllManifestVersions.mockReset();
|
||||
mockGetLatestVersionInManifest.mockReset();
|
||||
mockGetManifestArtifact.mockReset();
|
||||
@@ -102,13 +106,26 @@ describe("download-version", () => {
|
||||
expect(mockGetLatestVersionFromNdjson).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("uses astral-sh/versions to resolve available versions", async () => {
|
||||
mockGetAllVersionsFromNdjson.mockResolvedValue(["0.9.26", "0.9.25"]);
|
||||
it("streams astral-sh/versions to resolve the highest matching version", async () => {
|
||||
mockGetHighestSatisfyingVersionFromNdjson.mockResolvedValue("0.9.26");
|
||||
|
||||
const version = await resolveVersion("^0.9.0", undefined);
|
||||
|
||||
expect(version).toBe("0.9.26");
|
||||
expect(mockGetHighestSatisfyingVersionFromNdjson).toHaveBeenCalledWith(
|
||||
"^0.9.0",
|
||||
);
|
||||
expect(mockGetAllVersionsFromNdjson).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("still loads all versions when resolving the lowest matching version", async () => {
|
||||
mockGetAllVersionsFromNdjson.mockResolvedValue(["0.9.26", "0.9.25"]);
|
||||
|
||||
const version = await resolveVersion("^0.9.0", undefined, "lowest");
|
||||
|
||||
expect(version).toBe("0.9.25");
|
||||
expect(mockGetAllVersionsFromNdjson).toHaveBeenCalledTimes(1);
|
||||
expect(mockGetHighestSatisfyingVersionFromNdjson).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not fall back when astral-sh/versions fails", async () => {
|
||||
|
||||
@@ -12,6 +12,7 @@ const {
|
||||
fetchVersionData,
|
||||
getAllVersions,
|
||||
getArtifact,
|
||||
getHighestSatisfyingVersion,
|
||||
getLatestVersion,
|
||||
parseVersionData,
|
||||
} = await import("../../src/download/versions-client");
|
||||
@@ -21,13 +22,28 @@ const sampleNdjsonResponse = `{"version":"0.9.26","artifacts":[{"platform":"aarc
|
||||
|
||||
const multiVariantNdjsonResponse = `{"version":"0.9.26","artifacts":[{"platform":"aarch64-apple-darwin","variant":"python-managed","url":"https://github.com/astral-sh/uv/releases/download/0.9.26/uv-aarch64-apple-darwin-managed.tar.gz","archive_format":"tar.gz","sha256":"managed-checksum"},{"platform":"aarch64-apple-darwin","variant":"default","url":"https://github.com/astral-sh/uv/releases/download/0.9.26/uv-aarch64-apple-darwin.zip","archive_format":"zip","sha256":"default-checksum"}]}`;
|
||||
|
||||
function createMockStream(chunks: string[]): ReadableStream<Uint8Array> {
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
return new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
for (const chunk of chunks) {
|
||||
controller.enqueue(encoder.encode(chunk));
|
||||
}
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function createMockResponse(
|
||||
ok: boolean,
|
||||
status: number,
|
||||
statusText: string,
|
||||
data: string,
|
||||
chunks: string[] = [data],
|
||||
) {
|
||||
return {
|
||||
body: createMockStream(chunks),
|
||||
ok,
|
||||
status,
|
||||
statusText,
|
||||
@@ -86,6 +102,22 @@ describe("versions-client", () => {
|
||||
|
||||
expect(latest).toBe("0.9.26");
|
||||
});
|
||||
|
||||
it("should stop after the first record when resolving latest", async () => {
|
||||
mockFetch.mockResolvedValue(
|
||||
createMockResponse(
|
||||
true,
|
||||
200,
|
||||
"OK",
|
||||
`${sampleNdjsonResponse}\n{"version":`,
|
||||
[`${sampleNdjsonResponse.split("\n")[0]}\n`, '{"version":'],
|
||||
),
|
||||
);
|
||||
|
||||
const latest = await getLatestVersion();
|
||||
|
||||
expect(latest).toBe("0.9.26");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAllVersions", () => {
|
||||
@@ -100,6 +132,24 @@ describe("versions-client", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getHighestSatisfyingVersion", () => {
|
||||
it("should return the first matching version from the stream", async () => {
|
||||
mockFetch.mockResolvedValue(
|
||||
createMockResponse(
|
||||
true,
|
||||
200,
|
||||
"OK",
|
||||
`${sampleNdjsonResponse}\n{"version":`,
|
||||
[`${sampleNdjsonResponse.split("\n")[0]}\n`, '{"version":'],
|
||||
),
|
||||
);
|
||||
|
||||
const version = await getHighestSatisfyingVersion("^0.9.0");
|
||||
|
||||
expect(version).toBe("0.9.26");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getArtifact", () => {
|
||||
beforeEach(() => {
|
||||
mockFetch.mockResolvedValue(
|
||||
@@ -118,6 +168,27 @@ describe("versions-client", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should stop once the requested version is found", async () => {
|
||||
mockFetch.mockResolvedValue(
|
||||
createMockResponse(
|
||||
true,
|
||||
200,
|
||||
"OK",
|
||||
`${sampleNdjsonResponse}\n{"version":`,
|
||||
[`${sampleNdjsonResponse.split("\n")[0]}\n`, '{"version":'],
|
||||
),
|
||||
);
|
||||
|
||||
const artifact = await getArtifact("0.9.26", "aarch64", "apple-darwin");
|
||||
|
||||
expect(artifact).toEqual({
|
||||
archiveFormat: "tar.gz",
|
||||
sha256:
|
||||
"fcf0a9ea6599c6ae28a4c854ac6da76f2c889354d7c36ce136ef071f7ab9721f",
|
||||
url: "https://github.com/astral-sh/uv/releases/download/0.9.26/uv-aarch64-apple-darwin.tar.gz",
|
||||
});
|
||||
});
|
||||
|
||||
it("should find windows artifact", async () => {
|
||||
const artifact = await getArtifact("0.9.26", "x86_64", "pc-windows-msvc");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user