Files
setup-uv/src/download/checksum/checksum.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.8 KiB
TypeScript
Raw Normal View History

import * as crypto from "node:crypto";
2025-08-12 22:12:10 +02:00
import * as fs from "node:fs";
2024-08-23 23:58:26 +02:00
2024-09-05 08:06:45 -04:00
import * as core from "@actions/core";
import type { Architecture, Platform } from "../../utils/platforms";
2025-08-12 22:12:10 +02:00
import { KNOWN_CHECKSUMS } from "./known-checksums";
2024-08-23 23:58:26 +02:00
export async function validateChecksum(
checkSum: string | undefined,
downloadPath: string,
arch: Architecture,
platform: Platform,
2024-09-05 08:06:45 -04:00
version: string,
2024-08-23 23:58:26 +02:00
): Promise<void> {
2025-08-12 22:12:10 +02:00
let isValid: boolean | undefined;
2024-09-05 08:06:45 -04:00
if (checkSum !== undefined && checkSum !== "") {
isValid = await validateFileCheckSum(downloadPath, checkSum);
2024-08-23 23:58:26 +02:00
} else {
core.debug("Checksum not provided. Checking known checksums.");
2024-09-05 08:06:45 -04:00
const key = `${arch}-${platform}-${version}`;
2024-08-23 23:58:26 +02:00
if (key in KNOWN_CHECKSUMS) {
2024-09-05 08:06:45 -04:00
const knownChecksum = KNOWN_CHECKSUMS[`${arch}-${platform}-${version}`];
core.debug(`Checking checksum for ${arch}-${platform}-${version}.`);
isValid = await validateFileCheckSum(downloadPath, knownChecksum);
2024-08-23 23:58:26 +02:00
} else {
2024-09-05 08:06:45 -04:00
core.debug(`No known checksum found for ${key}.`);
2024-08-23 23:58:26 +02:00
}
}
if (isValid === false) {
2024-09-05 08:06:45 -04:00
throw new Error(`Checksum for ${downloadPath} did not match ${checkSum}.`);
2024-08-23 23:58:26 +02:00
}
if (isValid === true) {
core.debug(`Checksum for ${downloadPath} is valid.`);
}
2024-08-23 23:58:26 +02:00
}
async function validateFileCheckSum(
filePath: string,
2024-09-05 08:06:45 -04:00
expected: string,
2024-08-23 23:58:26 +02:00
): Promise<boolean> {
return new Promise((resolve, reject) => {
2024-09-05 08:06:45 -04:00
const hash = crypto.createHash("sha256");
const stream = fs.createReadStream(filePath);
stream.on("error", (err) => reject(err));
stream.on("data", (chunk) => hash.update(chunk));
stream.on("end", () => {
const actual = hash.digest("hex");
resolve(actual === expected);
});
});
2024-08-23 23:58:26 +02:00
}
export function isknownVersion(version: string): boolean {
2024-09-05 08:06:45 -04:00
const pattern = new RegExp(`^.*-.*-${version}$`);
return Object.keys(KNOWN_CHECKSUMS).some((key) => pattern.test(key));
2024-08-23 23:58:26 +02:00
}