"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments)).next());
    });
};
const debug_1 = require('../debug');
const debug = debug_1.default('pnpm:fetch');
const fs = require('fs');
const unpackStream = require('unpack-stream');
function createRemoteTarballFetcher(dist, opts) {
    return (target) => fetchFromRemoteTarball(target, dist, opts);
}
exports.createRemoteTarballFetcher = createRemoteTarballFetcher;
function createLocalTarballFetcher(dist) {
    return (target) => fetchFromLocalTarball(target, dist);
}
exports.createLocalTarballFetcher = createLocalTarballFetcher;
/**
 * Fetches a tarball `tarball` and extracts it into `dir`
 */
function fetchFromRemoteTarball(dir, dist, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const stream = yield opts.got.getStream(dist.tarball);
        yield unpackStream.remote(stream, dir, {
            shasum: dist.shasum,
            onStart: () => opts.log('download-start'),
            onProgress: (done, total) => opts.log('downloading', { done, total })
        });
        debug(`finish ${dist.shasum} ${dist.tarball}`);
    });
}
exports.fetchFromRemoteTarball = fetchFromRemoteTarball;
function fetchFromLocalTarball(dir, dist) {
    return unpackStream.local(fs.createReadStream(dist.tarball), dir);
}
exports.fetchFromLocalTarball = fetchFromLocalTarball;
//# sourceMappingURL=fetch.js.map