"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const decompress = require("decompress-maybe");
const tar = require("tar-stream");
const parseJson_1 = require("./parseJson");
async function default_1(addStreamToCafs, _ignore, stream, manifest) {
    const ignore = _ignore ? _ignore : () => false;
    const extract = tar.extract();
    const filesIndex = {};
    await new Promise((resolve, reject) => {
        extract.on('entry', async (header, fileStream, next) => {
            const filename = header.name.substr(header.name.indexOf('/') + 1);
            if (header.type !== 'file' || ignore(filename)) {
                fileStream.resume();
                next();
                return;
            }
            if (filename === 'package.json' && manifest) {
                parseJson_1.parseJsonStream(fileStream, manifest);
            }
            const generatingIntegrity = addStreamToCafs(fileStream, header.mode);
            filesIndex[filename] = {
                generatingIntegrity,
                mode: header.mode,
                size: header.size,
            };
            next();
        });
        // listener
        extract.on('finish', () => resolve());
        extract.on('error', reject);
        // pipe through extractor
        stream
            .on('error', reject)
            .pipe(decompress())
            .on('error', reject).pipe(extract);
    });
    return filesIndex;
}
exports.default = default_1;
//# sourceMappingURL=addFilesFromTarball.js.map