UNPKG

igir

Version:

🕹 A zero-setup ROM collection manager that sorts, filters, extracts or archives, patches, and reports on collections of any size on any OS.

115 lines (114 loc) • 3.57 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; import { Expose, instanceToPlain, plainToInstance } from "class-transformer"; import FsReadTransform from "../../streams/fsReadTransform.js"; import StreamUtil from "../../utils/streamUtil.js"; import FileChecksums from "./fileChecksums.js"; const _ROMPadding = class _ROMPadding { static POSSIBLE_FILL_BYTES = [0, 255]; paddedSize; fillByte; crc32; md5; sha1; sha256; constructor(props) { this.paddedSize = props?.paddedSize ?? 0; this.fillByte = props?.fillByte ?? 0; this.crc32 = props?.crc32; this.md5 = props?.md5; this.sha1 = props?.sha1; this.sha256 = props?.sha256; } static getKnownFillBytesCount() { return this.POSSIBLE_FILL_BYTES.length; } /** * Construct a {@link ROMPadding} from a plain object — the inverse of {@link toROMPaddingProps}. */ static fileOfObject(obj) { return plainToInstance(this, obj, { enableImplicitConversion: true, excludeExtraneousValues: true }); } /** * Serialize this padding into a plain object suitable for persistence. */ toROMPaddingProps() { return instanceToPlain(this, { exposeUnsetFields: false }); } getPaddedSize() { return this.paddedSize; } getFillByte() { return this.fillByte; } getCrc32() { return this.crc32; } getMd5() { return this.md5; } getSha1() { return this.sha1; } getSha256() { return this.sha256; } /** * Compute the set of {@link ROMPadding} entries describing how a file would look if padded * up to the next power-of-two size with each of the known fill bytes. */ static async paddingsFromFile(file, callback) { const paddedSize = Math.pow(2, Math.ceil(Math.log(file.getSize()) / Math.log(2))); if (paddedSize === file.getSize()) { return []; } if (callback !== void 0) { callback(0, paddedSize); } return await file.createReadStream(async (readable) => { const readableWithCallback = callback === void 0 ? readable : StreamUtil.withTransforms(readable, new FsReadTransform(callback)); const splitStreams = StreamUtil.split(readableWithCallback, this.POSSIBLE_FILL_BYTES.length); return await Promise.all( this.POSSIBLE_FILL_BYTES.map(async (fillByte, idx) => { const paddedStream = StreamUtil.padEnd(splitStreams[idx], paddedSize, fillByte); const checksums = await FileChecksums.hashStream(paddedStream, file.getChecksumBitmask()); return new _ROMPadding({ paddedSize, fillByte, ...checksums }); }) ); }); } }; __decorateClass([ Expose() ], _ROMPadding.prototype, "paddedSize", 2); __decorateClass([ Expose() ], _ROMPadding.prototype, "fillByte", 2); __decorateClass([ Expose() ], _ROMPadding.prototype, "crc32", 2); __decorateClass([ Expose() ], _ROMPadding.prototype, "md5", 2); __decorateClass([ Expose() ], _ROMPadding.prototype, "sha1", 2); __decorateClass([ Expose() ], _ROMPadding.prototype, "sha256", 2); let ROMPadding = _ROMPadding; export { ROMPadding as default }; //# sourceMappingURL=romPadding.js.map