feat(parser): implement hashing
All checks were successful
Actions / Build and Push Documentation (push) Successful in 16s
All checks were successful
Actions / Build and Push Documentation (push) Successful in 16s
This commit is contained in:
parent
f302ec0d31
commit
422fe6dbd3
5 changed files with 57 additions and 4 deletions
2
bun.lock
2
bun.lock
|
@ -18,7 +18,7 @@
|
|||
},
|
||||
"packages/parser": {
|
||||
"name": "@packwizjs/parser",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.4",
|
||||
"dependencies": {
|
||||
"@types/semver": "^7.5.8",
|
||||
"semver": "^7.7.1",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "@packwizjs/parser",
|
||||
"module": "src/index.ts",
|
||||
"version": "1.0.4",
|
||||
"version": "1.1.0",
|
||||
"type": "module",
|
||||
"description": "A simple library for parsing Packwiz TOML files",
|
||||
"author": {
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
import { createHash } from "crypto";
|
||||
//import { murmur2 } from "murmurhash2";
|
||||
|
||||
/**
|
||||
* The hash formats allowed by Packwiz.
|
||||
*/
|
||||
|
@ -37,3 +40,22 @@ export function isValidHashFormat(hashFormat: string): HashFormat {
|
|||
throw new Error(`Invalid index file hash format: ${hashFormat}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function doHashesMatch(
|
||||
hash: string,
|
||||
hashFormat: HashFormat,
|
||||
data: Buffer,
|
||||
): boolean {
|
||||
let computedHash;
|
||||
if (hashFormat === HashFormat.murmur2) {
|
||||
console.log(
|
||||
"WARNING: Murmur2 hash checking is not implemented yet! Skipping hash checking.",
|
||||
);
|
||||
return true;
|
||||
//computedHash = murmur2(data.toString(), 0).toString(16);
|
||||
} else {
|
||||
computedHash = createHash(hashFormat).update(data).digest("hex");
|
||||
}
|
||||
console.log(`Comparing ${hashFormat} hashes: ${hash} vs ${computedHash}`);
|
||||
return hash === computedHash;
|
||||
}
|
||||
|
|
|
@ -14,4 +14,8 @@ export {
|
|||
CurseForgeProvider,
|
||||
} from "./provider";
|
||||
export { Side, isValidSide } from "./enums/side";
|
||||
export { HashFormat, isValidHashFormat } from "./enums/hash-format";
|
||||
export {
|
||||
HashFormat,
|
||||
isValidHashFormat,
|
||||
doHashesMatch,
|
||||
} from "./enums/hash-format";
|
||||
|
|
|
@ -6,7 +6,11 @@ import {
|
|||
CurseForgeProvider,
|
||||
GitHubProvider,
|
||||
} from "./provider";
|
||||
import { HashFormat, isValidHashFormat } from "./enums/hash-format";
|
||||
import {
|
||||
HashFormat,
|
||||
isValidHashFormat,
|
||||
doHashesMatch,
|
||||
} from "./enums/hash-format";
|
||||
import { isValidSide, type Side } from "./enums/side";
|
||||
import * as semver from "semver";
|
||||
|
||||
|
@ -90,6 +94,16 @@ export class IndexFileEntry {
|
|||
);
|
||||
}
|
||||
const fileContent = await this.file.fetchContents();
|
||||
if (!fileContent) {
|
||||
throw new Error("Unable to fetch metafile contents!");
|
||||
}
|
||||
|
||||
if (!doHashesMatch(this.hash, this.hashFormat, Buffer.from(fileContent))) {
|
||||
throw new Error(
|
||||
`Hash of metafile ${this.file.toString()} does not match!`,
|
||||
);
|
||||
}
|
||||
|
||||
const parsed = toml.parse(fileContent);
|
||||
const side = isValidSide(parsed.side);
|
||||
|
||||
|
@ -254,6 +268,19 @@ export async function parsePackwiz(filePath: string): Promise<Packwiz> {
|
|||
const parsedPackwizFile = toml.parse(await packwizFile.fetchContents());
|
||||
|
||||
const indexFile = packwizFile.parent.join(parsedPackwizFile.index.file);
|
||||
|
||||
if (
|
||||
!doHashesMatch(
|
||||
parsedPackwizFile.index.hash,
|
||||
parsedPackwizFile.index["hash-format"],
|
||||
Buffer.from(await indexFile.fetchContents()),
|
||||
)
|
||||
) {
|
||||
throw new Error(
|
||||
`Hash of index file ${indexFile.toString()} does not match!`,
|
||||
);
|
||||
}
|
||||
|
||||
const parsedIndexFile = toml.parse(await indexFile.fetchContents());
|
||||
const indexHashFormat = isValidHashFormat(parsedIndexFile["hash-format"]);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue