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.

57 lines (56 loc) • 2.14 kB
class DataDescriptor { static DATA_DESCRIPTOR_SIGNATURE = Buffer.from("08074b50", "hex").toReversed(); // The largest possible descriptor: signature (4) + CRC32 (4) + two 8-byte zip64 sizes (16). static MAX_DATA_DESCRIPTOR_SIZE = 24; raw; signaturePresent; uncompressedCrc32; compressedSize; uncompressedSize; constructor(props) { this.raw = props.raw; this.signaturePresent = props.signaturePresent; this.uncompressedCrc32 = props.uncompressedCrc32; this.compressedSize = props.compressedSize; this.uncompressedSize = props.uncompressedSize; } /** * Parse the data descriptor that follows the local file's data. */ static async fromFileHandle(fileHandle, position, isZip64) { const buffer = Buffer.allocUnsafe(this.MAX_DATA_DESCRIPTOR_SIZE); const { bytesRead } = await fileHandle.read({ buffer, position }); const descriptor = buffer.subarray(0, bytesRead); const isSignaturePresent = descriptor.subarray(0, this.DATA_DESCRIPTOR_SIGNATURE.length).equals(this.DATA_DESCRIPTOR_SIGNATURE); let offset = isSignaturePresent ? this.DATA_DESCRIPTOR_SIGNATURE.length : 0; const uncompressedCrc32 = Buffer.from(descriptor.subarray(offset, offset + 4)); offset += 4; const compressedSize = isZip64 ? Number(descriptor.readBigUInt64LE(offset)) : descriptor.readUInt32LE(offset); offset += isZip64 ? 8 : 4; const uncompressedSize = isZip64 ? Number(descriptor.readBigUInt64LE(offset)) : descriptor.readUInt32LE(offset); offset += isZip64 ? 8 : 4; return new DataDescriptor({ raw: Buffer.from(descriptor.subarray(0, offset)), signaturePresent: isSignaturePresent, uncompressedCrc32, compressedSize, uncompressedSize }); } /** * Return the numerical CRC32. */ uncompressedCrc32Number() { return this.uncompressedCrc32.readUInt32LE(); } /** * Return the lowercased and padded CRC32 string. */ uncompressedCrc32String() { return this.uncompressedCrc32Number().toString(16).toLowerCase().padStart(8, "0"); } } export { DataDescriptor as default }; //# sourceMappingURL=dataDescriptor.js.map