make hashformat a string enum

This commit is contained in:
cswimr 2025-02-08 10:01:52 -06:00
parent f4e20ef240
commit 6b6a5f3cfb
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087
3 changed files with 52 additions and 21 deletions

15
src/hashFormat.ts Normal file
View file

@ -0,0 +1,15 @@
export enum HashFormat {
"sha256" = "sha256",
"sha512" = "sha512",
"sha1" = "sha1",
"md5" = "md5",
"murmur2" = "murmur2",
}
export function isValidHashFormat(hashFormat: string): HashFormat {
if (hashFormat in HashFormat) {
return HashFormat[hashFormat as keyof typeof HashFormat];
} else {
throw new Error("Invalid index file hash format!");
}
}

View file

@ -6,6 +6,7 @@ import {
CurseForgeProvider, CurseForgeProvider,
GitHubProvider, GitHubProvider,
} from "./provider"; } from "./provider";
import { HashFormat, isValidHashFormat } from "./hashFormat";
export interface Metafile { export interface Metafile {
name: string; name: string;
@ -25,18 +26,20 @@ export interface Option {
* Represents a file entry in the Packwiz index. * Represents a file entry in the Packwiz index.
*/ */
export class IndexFileEntry { export class IndexFileEntry {
readonly file: Resource;
readonly hash: string;
readonly metafile: boolean;
constructor( constructor(
file: string | Resource, readonly file: Resource,
hash: string, readonly hash: string,
metafile: boolean = false, readonly hashFormat: HashFormat,
readonly alias?: string,
readonly metafile: boolean = false,
readonly preserve: boolean = false,
) { ) {
this.file = file instanceof Resource ? file : new Resource(file); this.file = file instanceof Resource ? file : new Resource(file);
this.hash = hash; this.hash = hash;
this.hashFormat = isValidHashFormat(hashFormat);
this.alias = alias;
this.metafile = metafile; this.metafile = metafile;
this.preserve = preserve;
} }
async parse(): Promise<Metafile> { async parse(): Promise<Metafile> {
@ -67,7 +70,7 @@ export class IndexFileEntry {
if (parsed.update?.modrinth) { if (parsed.update?.modrinth) {
return new ModrinthProvider( return new ModrinthProvider(
parsed.download.hash, parsed.download.hash,
parsed.download["hash-format"], isValidHashFormat(parsed.download["hash-format"]),
new Resource(parsed.download.url), new Resource(parsed.download.url),
parsed.update.modrinth["mod-id"], parsed.update.modrinth["mod-id"],
parsed.update.modrinth["version"], parsed.update.modrinth["version"],
@ -75,7 +78,7 @@ export class IndexFileEntry {
} else if (parsed.update?.curseforge) { } else if (parsed.update?.curseforge) {
return new CurseForgeProvider( return new CurseForgeProvider(
parsed.download.hash, parsed.download.hash,
parsed.download["hash-format"], isValidHashFormat(parsed.download["hash-format"]),
parsed.download.mode, parsed.download.mode,
parsed.update.curseforge["file-id"], parsed.update.curseforge["file-id"],
parsed.update.curseforge["project-id"], parsed.update.curseforge["project-id"],
@ -83,7 +86,7 @@ export class IndexFileEntry {
} else if (parsed.update?.github) { } else if (parsed.update?.github) {
return new GitHubProvider( return new GitHubProvider(
parsed.download.hash, parsed.download.hash,
parsed.download["hash-format"], isValidHashFormat(parsed.download["hash-format"]),
new Resource(parsed.download.url), new Resource(parsed.download.url),
parsed.update.github.branch, parsed.update.github.branch,
parsed.update.github.regex, parsed.update.github.regex,
@ -93,7 +96,7 @@ export class IndexFileEntry {
} else if (parsed.download) { } else if (parsed.download) {
return new UrlProvider( return new UrlProvider(
parsed.download.hash, parsed.download.hash,
parsed.download["hash-format"], isValidHashFormat(parsed.download["hash-format"]),
new Resource(parsed.download.url), new Resource(parsed.download.url),
); );
} else { } else {
@ -107,7 +110,7 @@ export class IndexFileEntry {
*/ */
export interface PackwizIndex { export interface PackwizIndex {
location: Resource; location: Resource;
hashFormat: string; hashFormat: HashFormat;
files: IndexFileEntry[]; files: IndexFileEntry[];
} }
@ -125,16 +128,28 @@ export async function parsePackwizIndex(
): Promise<PackwizIndex> { ): Promise<PackwizIndex> {
const indexFile = new Resource(indexFilePath); const indexFile = new Resource(indexFilePath);
const parsed = toml.parse(await indexFile.fetchContents()); const parsed = toml.parse(await indexFile.fetchContents());
const hashFormat = isValidHashFormat(parsed["hash-format"]);
return { return {
location: indexFile, location: indexFile,
hashFormat: parsed["hash-format"], hashFormat: hashFormat,
files: parsed.files.map( files: parsed.files.map(
(file: any) => (file: {
new IndexFileEntry( file: string;
hash: string;
hashFormat: HashFormats;
alias?: string;
metafile?: boolean;
preserve?: boolean;
}) => {
return new IndexFileEntry(
indexFile.parent.join(file.file), indexFile.parent.join(file.file),
file.hash, file.hash,
file.metafile, file.hashFormat || hashFormat,
), file.alias,
file.metafile ?? false,
file.preserve ?? false,
);
},
), ),
}; };
} }

View file

@ -1,9 +1,10 @@
import Resource from "./resource"; import Resource from "./resource";
import { HashFormat } from "./hashFormat";
export class UrlProvider { export class UrlProvider {
constructor( constructor(
public hash: string, public hash: string,
public hashFormat: string, public hashFormat: HashFormat,
public url: Resource, public url: Resource,
) {} ) {}
} }
@ -11,7 +12,7 @@ export class UrlProvider {
export class ModrinthProvider extends UrlProvider { export class ModrinthProvider extends UrlProvider {
constructor( constructor(
hash: string, hash: string,
hashFormat: string, hashFormat: HashFormat,
url: Resource, url: Resource,
public modId: string, public modId: string,
public versionId: string, public versionId: string,
@ -23,7 +24,7 @@ export class ModrinthProvider extends UrlProvider {
export class GitHubProvider extends UrlProvider { export class GitHubProvider extends UrlProvider {
constructor( constructor(
hash: string, hash: string,
hashFormat: string, hashFormat: HashFormat,
url: Resource, url: Resource,
public branch: string, public branch: string,
public regex: string, public regex: string,
@ -37,7 +38,7 @@ export class GitHubProvider extends UrlProvider {
export class CurseForgeProvider extends UrlProvider { export class CurseForgeProvider extends UrlProvider {
constructor( constructor(
hash: string, hash: string,
hashFormat: string, hashFormat: HashFormat,
public mode: string, public mode: string,
public fileId: number, public fileId: number,
public projectId: number, public projectId: number,