Made base class for metadata readers that deal with zip

This commit is contained in:
Kir_Antipov 2021-12-07 21:48:52 +03:00
parent 4151c36500
commit ef28fbd8f5
3 changed files with 39 additions and 1 deletions

5
package-lock.json generated
View file

@ -4140,6 +4140,11 @@
"integrity": "sha512-9/IECtNr8dXNmPWmFXepT0/7o5eolGesHUa3mtr0KlgnCvnZxwh2qensKL42JJY2vQKC3nIBXetFAqR+PW1CmA==",
"dev": true
},
"node-stream-zip": {
"version": "1.15.0",
"resolved": "https://registry.npmjs.org/node-stream-zip/-/node-stream-zip-1.15.0.tgz",
"integrity": "sha512-LN4fydt9TqhZhThkZIVQnF9cwjU3qmUH9h78Mx/K7d3VvfRqqwthLwJEUOEL0QPZ0XQmNN7be5Ggit5+4dq3Bw=="
},
"normalize-path": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",

View file

@ -43,6 +43,7 @@
"@actions/github": "^5.0.0",
"fast-glob": "^3.2.7",
"formdata-node": "^4.2.4",
"node-fetch": "^3.0.0"
"node-fetch": "^3.0.0",
"node-stream-zip": "^1.15.0"
}
}

View file

@ -0,0 +1,32 @@
import ModMetadata from "./mod-metadata";
import ModMetadataReader from "./mod-metadata-reader";
import { StreamZipAsync, async as ZipArchive } from "node-stream-zip";
export default abstract class ZippedModMetadataReader<TConfig = Record<string, unknown>> implements ModMetadataReader {
private configEntryName: string;
protected constructor(configEntryName: string) {
this.configEntryName = configEntryName;
}
async readMetadata(modPath: string): Promise<ModMetadata | null> {
let zip = <StreamZipAsync>null;
try {
zip = new ZipArchive({ file: modPath });
const buffer = await zip.entryData(this.configEntryName).catch(_ => <Buffer>null);
if (buffer) {
return this.createMetadataFromConfig(this.loadConfig(buffer));
} else {
return null;
}
} catch {
return null;
} finally {
await zip?.close();
}
}
protected abstract loadConfig(buffer: Buffer): TConfig;
protected abstract createMetadataFromConfig(config: TConfig): ModMetadata;
}