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
Some checks failed
Actions / Build and Push Documentation (push) Failing after 16s
This commit is contained in:
parent
6907543fb8
commit
53db10f4e5
1 changed files with 22 additions and 0 deletions
|
@ -4,6 +4,8 @@ import {
|
||||||
type Metafile,
|
type Metafile,
|
||||||
type PackwizIndex,
|
type PackwizIndex,
|
||||||
HashFormat,
|
HashFormat,
|
||||||
|
Side,
|
||||||
|
isValidSide,
|
||||||
doHashesMatch,
|
doHashesMatch,
|
||||||
} from "@packwizjs/parser";
|
} from "@packwizjs/parser";
|
||||||
import { write } from "bun";
|
import { write } from "bun";
|
||||||
|
@ -62,16 +64,19 @@ function formatBytes(bytes: number, decimals: number = 2) {
|
||||||
/**
|
/**
|
||||||
* 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() {
|
||||||
|
@ -96,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