{"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":["Reader","fs","Readable","zlib","decodeDateTime","date","time","Date","Zip","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","start","lastModified","last_mod_file_date","last_mod_file_time","getStream","offset","remaining","compressed_size","res","_read","size","push","bookmark","chunk","read","compression_method","pipe","createInflateRaw","constructor","fd","_source","length","result","Buffer","alloc","pos","toRead","Math","min","readSync"],"mappings":"AAAA,SAASA,MAAM,QAAQ,MAAM;AAE7B,OAAOC,QAAQ,KAAK;AACpB,SAASC,QAAQ,QAAQ,SAAS;AAClC,OAAOC,UAAU,OAAO;AAExB,MAAMC,iBAAiB,CAACC,MAAMC,OAAS,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,AAAME,MAAN,MAAMA,YAAYR;IAmB/BS,WAAW;QACT,MAAMC,SAAS,IAAI;QAEnB,kCAAkC;QAClCA,OAAOC,iCAAiC;QACxC,MAAMC,YAAYF,OAAOG,+BAA+B;QAExD,iDAAiD;QACjDH,OAAOI,IAAI,CAACF,UAAUG,kBAAkB;QAExC,IAAIC,QAAQJ,UAAUK,wBAAwB;QAE9C,OAAO;YACLC,MAAM;gBACJ,IAAIF,YAAY,GAAG,MAAM;gBAEzB,oCAA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