UNPKG

chdman

Version:

💿 chdman binaries and wrapper for Node.js.

108 lines • 6.67 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import util from 'node:util'; import fs from 'node:fs'; import ChdmanBin from './chdmanBin.js'; import { CHDType, CHDCompressionAlgorithm } from './common.js'; export default { /** * Return info about a CHD file. */ info(options_1) { return __awaiter(this, arguments, void 0, function* (options, attempt = 1) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u; try { yield util.promisify(fs.stat)(options.inputFilename); } catch (_v) { throw new Error(`input file doesn't exist: ${options.inputFilename}`); } const output = yield ChdmanBin.run([ 'info', '--input', options.inputFilename, '--verbose', ], options); // Try to detect failures, and then retry them automatically if (!output.trim() && attempt <= 3) { yield new Promise((resolve) => { setTimeout(resolve, Math.random() * (Math.pow(2, (attempt - 1)) * 20)); }); return this.info(options, attempt + 1); } const parsedKeys = new Map(); for (const line of output.split(/\r?\n/)) { const split = line.split(/^([^ ][^:]+): +(.+)$/); if (split.length === 4) { parsedKeys.set(split[1].toUpperCase(), split[2]); } } const metadata = [...output.matchAll(/metadata: +(.+)\r?\n +(.+)/gi)] .map((match, index_) => { var _a, _b, _c, _d, _e, _f, _g, _h; const tag = (_c = (_b = (_a = match[1].match(/tag='([\d a-z]+)'/i)) === null || _a === void 0 ? void 0 : _a.at(1)) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : ''; const index = Number.parseInt((_e = (_d = match[1].match(/index=(\d+)/i)) === null || _d === void 0 ? void 0 : _d.at(1)) !== null && _e !== void 0 ? _e : String(index_), 10); const length = Number.parseInt((_h = (_g = (_f = match[1].match(/length=([\d,]+)/i)) === null || _f === void 0 ? void 0 : _f.at(1)) === null || _g === void 0 ? void 0 : _g.replace(/,/g, '')) !== null && _h !== void 0 ? _h : '0', 10); return { tag, index, length, data: match[2], }; }); const metadataTags = new Set(metadata.map((m) => m.tag)); let type = CHDType.RAW; if (metadataTags.has('GDDD')) { type = CHDType.HARD_DISK; } else if (metadataTags.has('CHCD') || metadataTags.has('CHTR') || metadataTags.has('CHT2')) { type = CHDType.CD_ROM; } else if (metadataTags.has('CHGT') || metadataTags.has('CHGD')) { type = CHDType.GD_ROM; } else if (metadataTags.has('DVD')) { type = CHDType.DVD_ROM; } const chdInfo = { inputFile: (_a = parsedKeys.get('INPUT FILE')) !== null && _a !== void 0 ? _a : options.inputFilename, fileVersion: Number.parseInt((_b = parsedKeys.get('FILE VERSION')) !== null && _b !== void 0 ? _b : '0', 10), logicalSize: Number.parseInt((_d = (_c = parsedKeys.get('LOGICAL SIZE')) === null || _c === void 0 ? void 0 : _c.replace(/\D+/g, '')) !== null && _d !== void 0 ? _d : '0', 10), hunkSize: Number.parseInt((_f = (_e = parsedKeys.get('HUNK SIZE')) === null || _e === void 0 ? void 0 : _e.replace(/\D+/g, '')) !== null && _f !== void 0 ? _f : '0', 10), totalHunks: Number.parseInt((_h = (_g = parsedKeys.get('TOTAL HUNKS')) === null || _g === void 0 ? void 0 : _g.replace(/\D+/g, '')) !== null && _h !== void 0 ? _h : '0', 10), unitSize: Number.parseInt((_k = (_j = parsedKeys.get('UNIT SIZE')) === null || _j === void 0 ? void 0 : _j.replace(/\D+/g, '')) !== null && _k !== void 0 ? _k : '0', 10), totalUnits: Number.parseInt((_m = (_l = parsedKeys.get('TOTAL UNITS')) === null || _l === void 0 ? void 0 : _l.replace(/\D+/g, '')) !== null && _m !== void 0 ? _m : '0', 10), compression: [...((_o = parsedKeys.get('COMPRESSION')) !== null && _o !== void 0 ? _o : '').matchAll(/([a-z]{4})( \([^(]+\))?/g)] .map((match) => match[1]) .map((compressionType) => Object.keys(CHDCompressionAlgorithm) .map((enumKey) => { const compressionAlgo = enumKey; return CHDCompressionAlgorithm[compressionAlgo]; }) .find((enumValue) => enumValue === compressionType)) .filter((enumValue) => enumValue !== undefined), chdSize: Number.parseInt((_q = (_p = parsedKeys.get('CHD SIZE')) === null || _p === void 0 ? void 0 : _p.replace(/\D+/g, '')) !== null && _q !== void 0 ? _q : '0', 10), ratio: Number.parseFloat((_s = (_r = parsedKeys.get('RATIO')) === null || _r === void 0 ? void 0 : _r.replace(/[^\d.]+/g, '')) !== null && _s !== void 0 ? _s : '0'), sha1: (_t = parsedKeys.get('SHA1')) !== null && _t !== void 0 ? _t : '', dataSha1: (_u = parsedKeys.get('DATA SHA1')) !== null && _u !== void 0 ? _u : '', metadata, type, }; // Try to detect failures, and then retry them automatically if (chdInfo.fileVersion === 0 && attempt <= 3) { yield new Promise((resolve) => { setTimeout(resolve, Math.random() * (Math.pow(2, (attempt - 1)) * 20)); }); return this.info(options, attempt + 1); } return chdInfo; }); }, }; //# sourceMappingURL=chdmanInfo.js.map