Compare commits
3 commits
9cbf22e22a
...
53db10f4e5
Author | SHA1 | Date | |
---|---|---|---|
53db10f4e5 | |||
6907543fb8 | |||
0b3f39b8b2 |
1 changed files with 39 additions and 2 deletions
|
@ -3,7 +3,9 @@ import {
|
||||||
Resource,
|
Resource,
|
||||||
type Metafile,
|
type Metafile,
|
||||||
type PackwizIndex,
|
type PackwizIndex,
|
||||||
type HashFormat,
|
HashFormat,
|
||||||
|
Side,
|
||||||
|
isValidSide,
|
||||||
doHashesMatch,
|
doHashesMatch,
|
||||||
} from "@packwizjs/parser";
|
} from "@packwizjs/parser";
|
||||||
import { write } from "bun";
|
import { write } from "bun";
|
||||||
|
@ -41,22 +43,40 @@ async function downloadFile(
|
||||||
doHashesMatch(hash, hashFormat, dataBuffer);
|
doHashesMatch(hash, hashFormat, dataBuffer);
|
||||||
console.log(`Saving file to ${path.toString()}`);
|
console.log(`Saving file to ${path.toString()}`);
|
||||||
const fileSize = await write(path.toString(), arrayBuffer);
|
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.
|
* Iterates over the files in a Packwiz index and downloads them concurrently.
|
||||||
* @param index The Packwiz index to iterate over.
|
* @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.
|
* @param concurrencyLimit The maximum number of concurrent downloads.
|
||||||
* @returns A promise that resolves when all files have been downloaded.
|
* @returns A promise that resolves when all files have been downloaded.
|
||||||
*/
|
*/
|
||||||
export async function iteratePackwizIndex(
|
export async function iteratePackwizIndex(
|
||||||
index: PackwizIndex,
|
index: PackwizIndex,
|
||||||
|
side: Side,
|
||||||
concurrencyLimit: number = 5,
|
concurrencyLimit: number = 5,
|
||||||
) {
|
) {
|
||||||
let currentIndex = 0;
|
let currentIndex = 0;
|
||||||
let activeDownloads = 0;
|
let activeDownloads = 0;
|
||||||
const totalFiles = index.files.length;
|
const totalFiles = index.files.length;
|
||||||
|
side = side.toLowerCase() as Side;
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
function downloadNextFile() {
|
function downloadNextFile() {
|
||||||
|
@ -81,11 +101,28 @@ export async function iteratePackwizIndex(
|
||||||
|
|
||||||
if (file.metafile) {
|
if (file.metafile) {
|
||||||
const metafile = await file.parse();
|
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;
|
hash = metafile.provider.hash;
|
||||||
hashFormat = metafile.provider.hashFormat;
|
hashFormat = metafile.provider.hashFormat;
|
||||||
url = metafile.provider.url;
|
url = metafile.provider.url;
|
||||||
saveLocation = getSaveLocation(file, index, metafile);
|
saveLocation = getSaveLocation(file, index, metafile);
|
||||||
} else {
|
} 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);
|
const diff = index.location.diff(file.file);
|
||||||
hash = file.hash;
|
hash = file.hash;
|
||||||
hashFormat = file.hashFormat;
|
hashFormat = file.hashFormat;
|
||||||
|
|
Loading…
Add table
Reference in a new issue