Compare commits

..

3 commits

Author SHA1 Message Date
53db10f4e5
feat(sync): check which Side a metafile should be downloaded on before downloading it
Some checks failed
Actions / Build and Push Documentation (push) Failing after 16s
2025-02-12 08:21:22 -06:00
6907543fb8
feat(sync): format byte numbers in download logs 2025-02-12 08:20:57 -06:00
0b3f39b8b2
fix(sync): remove type from the HashFormat import in sync.ts 2025-02-12 08:20:10 -06:00

View file

@ -3,7 +3,9 @@ import {
Resource,
type Metafile,
type PackwizIndex,
type HashFormat,
HashFormat,
Side,
isValidSide,
doHashesMatch,
} from "@packwizjs/parser";
import { write } from "bun";
@ -41,22 +43,40 @@ async function downloadFile(
doHashesMatch(hash, hashFormat, dataBuffer);
console.log(`Saving file to ${path.toString()}`);
const fileSize = await write(path.toString(), arrayBuffer);
console.log(`Saved ${fileSize} file to ${path.toString()}`);
console.log(`Saved ${formatBytes(fileSize)} file to ${path.toString()}`);
}
/**
* Converts bytes into a human-readable format with size units.
* @param bytes - The number of bytes.
* @param decimals - The number of decimal places to use.
* @returns Formatted string (e.g., "1.23 MB").
*/
function formatBytes(bytes: number, decimals: number = 2) {
if (bytes === 0) return "0 Bytes";
const k = 1024; // or 1000 for decimal units
const dm = decimals < 0 ? 0 : decimals;
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
/**
* Iterates over the files in a Packwiz index and downloads them concurrently.
* @param index The Packwiz index to iterate over.
* @param side The side to download files for (e.g., "client", "server", or "both").
* @param concurrencyLimit The maximum number of concurrent downloads.
* @returns A promise that resolves when all files have been downloaded.
*/
export async function iteratePackwizIndex(
index: PackwizIndex,
side: Side,
concurrencyLimit: number = 5,
) {
let currentIndex = 0;
let activeDownloads = 0;
const totalFiles = index.files.length;
side = side.toLowerCase() as Side;
return new Promise<void>((resolve, reject) => {
function downloadNextFile() {
@ -81,11 +101,28 @@ export async function iteratePackwizIndex(
if (file.metafile) {
const metafile = await file.parse();
if (!metafile.side || !isValidSide(metafile.side)) {
throw new Error(
`Metafile ${metafile.filename} has invalid Side: ${metafile.side}`,
);
}
if (metafile.side !== Side.Both && metafile.side !== side) {
// Don't throw an error here, this is normal behavior!
console.log(
`Skipping metafile ${metafile.filename} due to side mismatch: '${metafile.side}' (Wanted '${side}' or '${Side.Both}')`,
);
return;
}
hash = metafile.provider.hash;
hashFormat = metafile.provider.hashFormat;
url = metafile.provider.url;
saveLocation = getSaveLocation(file, index, metafile);
} else {
// we don't check non-metafiles' Side because packwiz doesn't store metadata for whether or not to download them on client / server
// so instead, always download them on both sides (assume Side.Both)
const diff = index.location.diff(file.file);
hash = file.hash;
hashFormat = file.hashFormat;