Skip to content

Commit 80ea606

Browse files
authored
Merge pull request #25 from MikiDevAHM/fix/export-hash-resolution
fix(export): resolve missing file hashes from local bytes and Modrinth API
2 parents 61fcaeb + fe8460a commit 80ea606

15 files changed

Lines changed: 1032 additions & 126 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Desktop tool for Minecraft modpack developers (Origin Realms Beyond) to sync mod
101101
## mrpack export invariant (Pass 3)
102102
- Every exported/stored file entry carries lowercase hex `sha1` (40) AND `sha512` (128) plus `fileSize` and an allowed HTTPS download, OR goes to `overrides/`, OR the export fails. Never emit a pack without passing the validator.
103103
- Never write hashes by guessing. Hashing goes through the single streaming utility `src/main/export/hash.ts` (`hashFile`).
104-
- Manifest schema version for the hash cache is `HASH_CACHE_SCHEMA_VERSION` (currently 2); legacy entries are migrated lazily in memory during export via `migrateManifestEntries`.
104+
- Manifest schema version for the hash cache is `HASH_CACHE_SCHEMA_VERSION` (currently 3); legacy entries are migrated lazily in memory during export via `migrateManifestEntries` and resolved through local bytes -> stored hash (`/version_files`) -> strict CDN URL (`/version/{id}`), then cross-verified against Modrinth. `source`/`verified` are internal and never written to the pack.
105105
- Atomic export: write to a temp file, validate, then rename. Never leave a partial `.mrpack`.
106106
- Trusted download hosts: `cdn.modrinth.com`, `github.com`, `raw.githubusercontent.com`, `gitlab.com`.
107107
- Run `bun run verify:mrpack <file>` to validate an exported pack offline.

‎docs/mrpack-export.md‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,30 @@ Each `files[]` entry: `path` (forward slashes, no `..`, no leading separator, no
99
Dependencies: `minecraft` + loader key (`forge`, `neoforge`, `fabric-loader`, `quilt-loader`).
1010
Overrides folders: `overrides/`, `client-overrides/`, `server-overrides/`.
1111

12-
## Source of truth
13-
- Local file bytes available: hashes computed from bytes are ground truth. Modrinth API disagreement => `FILE_HASH_MISMATCH`.
14-
- Stored manifest only: use stored `sha1`/`sha512`; never guess.
15-
- Legacy entries (missing one hash): migrate lazily (local file -> hash; else Modrinth API via available hash; else fail `MISSING_FILE_HASH`).
12+
## Source of truth and resolution chain
13+
`migrateManifestEntries` (`src/main/export/migrate.ts`) resolves legacy/partial entries in this order:
14+
1. Both `sha1` + `sha512` already present: use them and cross-verify below.
15+
2. Local file bytes: exact `root/<path>`, then the filename from each download URL under `root/mods`, then the entry `filename`, then a case-insensitive exact match. Only the profile root is read; the versions-repo tree is never used for local bytes.
16+
3. Available hash: batch `POST https://api.modrinth.com/v2/version_files` with `algorithm` matching the stored hash type (`sha1` or `sha512`). The response is an object keyed by the submitted hash.
17+
4. Download URL: parse the strict Modrinth CDN URL (`https://cdn.modrinth.com/data/<project>/versions/<version>/<filename>`), then `GET /version/{versionId}` with `GET /project/{projectId}/version` as fallback, selecting the file whose `url` matches exactly.
18+
5. Otherwise fail with `MISSING_FILE_HASH`, including `causes` and `pathsTried` for diagnostics.
19+
20+
Local bytes are ground truth. If a stored hash disagrees with local bytes the export fails with `FILE_HASH_MISMATCH` (`local-hash-mismatch`); hashes are never guessed.
21+
22+
## Verification
23+
Every real Modrinth CDN URL is cross-checked:
24+
25+
- `sha512` batch `POST /version_files` (chunks of 25), requiring exact URL, `sha1`, `sha512`, and `fileSize` equality.
26+
- URL not served or hash unknown: `FILE_HASH_MISMATCH` with `url-not-served-by-modrinth`.
27+
- Size disagreement: `FILE_HASH_MISMATCH` with `size-mismatch`.
28+
- Network failure or rate limiting: non-network-only entries are exported with `verified: false` plus a warning; network-only entries fail with `MISSING_FILE_HASH` (`modrinth-unreachable` / `modrinth-rate-limited`).
29+
30+
Requests send an identifying `User-Agent: ORB-Modpack-Exporter/<version>`, retry transient failures with backoff, honour `Retry-After` on 429, and abort after 30s.
31+
32+
`source` and `verified` are internal fields; both are stripped from `modrinth.index.json` and release snapshots.
33+
34+
## Hash cache
35+
Path: `<versionsRepo>/.mrpack_hash_cache.json` (gitignored). `HASH_CACHE_SCHEMA_VERSION` is `3`; other schemas are ignored. Entries may carry `files` and `expiresAt`. A persisted `null` is treated as a miss (never a permanent negative cache). The cache is only rewritten when an entry actually changed.
1636

1737
## Validator
1838
`src/main/export/validator.ts` exports `validateMrpackIndex` and `validateZipEntryNames`.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "orb-modpack-exporter",
33
"productName": "ORB Modpack Exporter",
4-
"version": "2.1.7",
4+
"version": "2.1.8",
55
"description": "Collaborate via GitHub, export .mrpack files, and track changes",
66
"main": ".vite/build/index.cjs",
77
"repository": {

‎src/main/export/errors.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export interface MrpackErrorDetail {
1919
detail?: string;
2020
expected?: string;
2121
actual?: string;
22+
causes?: string[];
23+
pathsTried?: string[];
24+
cause?: string;
2225
}
2326

2427
export class MrpackError extends Error {
@@ -30,14 +33,14 @@ export class MrpackError extends Error {
3033
}
3134
}
3235

33-
export function toErrorPayload(e: unknown): { code: string; file?: string; pointer?: string; detail?: string } {
36+
export function toErrorPayload(e: unknown): { code: string; file?: string; pointer?: string; detail?: string; causes?: string[]; pathsTried?: string[]; cause?: string } {
3437
if (e instanceof MrpackError) {
3538
const d = e.detail;
36-
return { code: d.code, file: d.file, pointer: d.pointer, detail: d.detail };
39+
return { code: d.code, file: d.file, pointer: d.pointer, detail: d.detail, causes: d.causes, pathsTried: d.pathsTried, cause: d.cause };
3740
}
3841
if (e && typeof e === 'object' && 'code' in (e as any)) {
3942
const o = e as any;
40-
return { code: String(o.code), file: o.file, pointer: o.pointer, detail: o.detail ?? o.message };
43+
return { code: String(o.code), file: o.file, pointer: o.pointer, detail: o.detail ?? o.message, causes: o.causes, pathsTried: o.pathsTried, cause: o.cause };
4144
}
4245
return { code: 'UNKNOWN', detail: e instanceof Error ? e.message : String(e) };
4346
}

‎src/main/export/hashCache.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import path from 'path';
22
import fs from 'fs';
33

4-
export const HASH_CACHE_SCHEMA_VERSION = 2;
4+
export const HASH_CACHE_SCHEMA_VERSION = 3;
55

66
export interface HashCacheEntry {
77
sha1: string;
88
sha512: string;
99
size: number;
1010
url: string | null;
11+
files?: Array<{ url: string; filename: string; size: number; hashes: { sha1: string; sha512: string } }>;
12+
expiresAt?: number;
1113
}
1214

1315
export class HashCache {
1416
private filePath: string;
1517
private schemaVersion: number;
1618
private entries: Record<string, HashCacheEntry | null>;
19+
private dirty = false;
1720

1821
constructor(filePath: string) {
1922
this.filePath = filePath;
@@ -32,17 +35,27 @@ export class HashCache {
3235

3336
get(key: string): HashCacheEntry | null | undefined {
3437
if (!(key in this.entries)) return undefined;
35-
return this.entries[key];
38+
const v = this.entries[key];
39+
if (v === null) return undefined; // never treat a persisted miss as permanent
40+
if (v && typeof v.expiresAt === 'number' && v.expiresAt > 0 && Date.now() > v.expiresAt) {
41+
delete this.entries[key];
42+
this.dirty = true;
43+
return undefined;
44+
}
45+
return v;
3646
}
3747

3848
set(key: string, value: HashCacheEntry | null): void {
3949
this.entries[key] = value;
50+
this.dirty = true;
4051
}
4152

4253
save(): void {
54+
if (!this.dirty) return;
4355
try {
4456
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
4557
fs.writeFileSync(this.filePath, JSON.stringify({ schemaVersion: this.schemaVersion, entries: this.entries }, null, 2), 'utf-8');
58+
this.dirty = false;
4659
} catch {
4760
// best-effort
4861
}

0 commit comments

Comments
 (0)