{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/zip-iterator/src/lib/Zip.ts"],"sourcesContent":["import { Reader } from 'zip';\n\nimport fs from 'fs';\nimport { Readable } from 'stream';\nimport zlib from 'zlib';\n\nconst decodeDateTime = (date, time) => new Date((date >>> 9) + 1980, ((date >>> 5) & 15) - 1, date & 31, (time >>> 11) & 31, (time >>> 5) & 63, (time & 63) * 2);\n\ninterface Source {\n  read(start, length): Buffer;\n}\n\ninterface ReaderT {\n  _source: Source;\n}\n\nexport default class Zip extends Reader {\n  constructor(fd) {\n    super(fd);\n\n    // patch pos\n    (this as unknown as ReaderT)._source.read = (start, length) => {\n      const result = Buffer.alloc(length);\n      let pos = 0;\n      while (length > 0) {\n        const toRead = Math.min(length, 8192);\n        fs.readSync(fd, result, pos, toRead, start);\n        length -= toRead;\n        start += toRead;\n        pos += toRead;\n      }\n      return result;\n    };\n  }\n\n  iterator() {\n    const stream = this as Reader;\n\n    // find the end record and read it\n    stream.locateEndOfCentralDirectoryRecord();\n    const endRecord = stream.readEndOfCentralDirectoryRecord();\n\n    // seek to the beginning of the central directory\n    stream.seek(endRecord.central_dir_offset);\n\n    let count = endRecord.central_dir_disk_records;\n\n    return {\n      next: () => {\n        if (count-- === 0) throw 'stop-iteration';\n\n        // read the central directory header\n        const centralHeader = stream.readCentralDirectoryFileHeader();\n\n        // save our new position so we can restore it\n        const saved = stream.position();\n\n        // seek to the local header and read it\n        stream.seek(centralHeader.local_file_header_offset);\n        const localHeader = stream.readLocalFileHeader();\n\n        // dont read the content just save the position for later use\n        const start = stream.position();\n\n        // seek back to the next central directory header\n        stream.seek(saved);\n\n        return {\n          localHeader: localHeader,\n          stream: stream,\n          start: start,\n          centralHeader: centralHeader,\n          lastModified: () => decodeDateTime(localHeader.last_mod_file_date, localHeader.last_mod_file_time),\n          getStream: () => {\n            let offset = start;\n            let remaining = centralHeader.compressed_size;\n            let res = new Readable();\n            res._read = function (size) {\n              if (remaining <= 0) return this.push(null); // done\n              if (size > remaining) size = remaining; // clamp\n              const bookmark = stream.position(); // save\n              stream.seek(offset);\n              const chunk = stream.read(size);\n              remaining -= size;\n              offset += size;\n              stream.seek(bookmark); // restore\n              this.push(chunk);\n            };\n            if (centralHeader.compression_method !== 0) res = res.pipe(zlib.createInflateRaw());\n            return res;\n          },\n        };\n      },\n    };\n  }\n}\n"],"names":["Zip","decodeDateTime","date","time","Date","fd","_source","read","start","length","result","Buffer","alloc","pos","toRead","Math","min","fs","readSync","iterator","stream","locateEndOfCentralDirectoryRecord","endRecord","readEndOfCentralDirectoryRecord","seek","central_dir_offset","count","central_dir_disk_records","next","centralHeader","readCentralDirectoryFileHeader","saved","position","local_file_header_offset","localHeader","readLocalFileHeader","lastModified","last_mod_file_date","last_mod_file_time","getStream","offset","remaining","compressed_size","res","Readable","_read","size","push","bookmark","chunk","compression_method","pipe","zlib","createInflateRaw","Reader"],"mappings":";;;;;;;eAgBqBA;;;mBAhBE;yDAER;sBACU;2DACR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEjB,IAAMC,iBAAiB,SAACC,MAAMC;WAAS,IAAIC,KAAK,AAACF,CAAAA,SAAS,CAAA,IAAK,MAAM,AAAC,CAAA,AAACA,SAAS,IAAK,EAAC,IAAK,GAAGA,OAAO,IAAI,AAACC,SAAS,KAAM,IAAI,AAACA,SAAS,IAAK,IAAI,AAACA,CAAAA,OAAO,EAAC,IAAK;;AAU/I,IAAA,AAAMH,oBAAN;;cAAMA;aAAAA,IACPK,EAAE;gCADKL;;gBAEjB,kBAFiBA;YAEXK;;QAGN,MAA6BC,OAAO,CAACC,IAAI,GAAG,SAACC,OAAOC;YAClD,IAAMC,SAASC,OAAOC,KAAK,CAACH;YAC5B,IAAII,MAAM;YACV,MAAOJ,SAAS,EAAG;gBACjB,IAAMK,SAASC,KAAKC,GAAG,CAACP,QAAQ;gBAChCQ,WAAE,CAACC,QAAQ,CAACb,IAAIK,Q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