UNPKG

maxcso

Version:

💿 maxcso binaries and wrapper for Node.js.

96 lines • 4.77 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()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; import fs from 'node:fs'; import MaxcsoBin from './maxcsoBin.js'; export var CsoFileType; (function (CsoFileType) { CsoFileType["CSO"] = "CISO"; CsoFileType["DAX"] = "DAX"; CsoFileType["ZSO"] = "ZISO"; })(CsoFileType || (CsoFileType = {})); export default { /** * Given a compressed CSO/ZSO/DAX file, get its uncompressed ISO's CRC32. This is fast/cheap to * calculate. */ uncompressedCrc32(options_1) { return __awaiter(this, arguments, void 0, function* (options, attempt = 1) { const output = yield MaxcsoBin.run(['--crc', options.inputFilename], 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.uncompressedCrc32(options, attempt + 1); } const crcMatch = output.replace(/[\n\r]/g, '').match(/ ([\da-f]{8})$/); if (crcMatch === null) { throw new Error('failed to get CRC of uncompressed file'); } return crcMatch[1]; }); }, /** * Given a compressed CSO/ZSO/DAX file, get the information stored in its file header. */ header(inputFilename) { return __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c; const chunks = []; try { for (var _d = true, _e = __asyncValues(fs.createReadStream(inputFilename, { start: 0, end: 24 })), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const chunk = _c; chunks.push(chunk); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } const contents = Buffer.concat(chunks); const signature = contents.subarray(0x0, 4).filter((char) => char !== 0x00).toString(); const fileType = Object.values(CsoFileType).find((value) => value === signature); if (fileType === undefined) { throw new Error('failed to get file type'); } if (fileType === CsoFileType.DAX) { // @see https://github.com/unknownbrackets/maxcso/blob/961f232cf99d546b2b7e704c0ecf3fc5bea52221/src/dax.h return { fileType, uncompressedSize: BigInt(contents.readUInt32LE(0x4)), blockSize: -1, version: contents.readUInt32LE(0x8), indexAlignment: -1, }; } // @see https://docs.fileformat.com/disc-and-media/cso/ return { fileType, uncompressedSize: contents.readBigUInt64LE(0x8), blockSize: contents.readUInt32LE(0x10), version: contents.readInt8(0x14), indexAlignment: contents.readInt8(0x15), }; }); }, }; //# sourceMappingURL=maxcsoInfo.js.map