"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TarballIntegrityError = exports.BadTarballError = void 0;
const error_1 = __importDefault(require("@pnpm/error"));
const createDownloader_1 = __importStar(require("./createDownloader"));
Object.defineProperty(exports, "TarballIntegrityError", { enumerable: true, get: function () { return createDownloader_1.TarballIntegrityError; } });
const path = require("path");
const fs = require("mz/fs");
const ssri = require("ssri");
var errorTypes_1 = require("./errorTypes");
Object.defineProperty(exports, "BadTarballError", { enumerable: true, get: function () { return errorTypes_1.BadTarballError; } });
function default_1(fetchFromRegistry, getCredentials, opts) {
    const download = createDownloader_1.default(fetchFromRegistry, {
        retry: opts.retry,
    });
    return {
        tarball: fetchFromTarball.bind(null, {
            download,
            getCredentialsByURI: getCredentials,
            offline: opts.offline,
        }),
    };
}
exports.default = default_1;
async function fetchFromTarball(ctx, cafs, resolution, opts) {
    if (resolution.tarball.startsWith('file:')) {
        const tarball = resolvePath(opts.lockfileDir, resolution.tarball.slice(5));
        return fetchFromLocalTarball(cafs, tarball, {
            integrity: resolution.integrity,
            manifest: opts.manifest,
        });
    }
    if (ctx.offline) {
        throw new error_1.default('NO_OFFLINE_TARBALL', `A package is missing from the store but cannot download it in offline mode. The missing package may be downloaded from ${resolution.tarball}.`);
    }
    const auth = resolution.registry ? ctx.getCredentialsByURI(resolution.registry) : undefined;
    return ctx.download(resolution.tarball, {
        auth,
        cafs,
        integrity: resolution.integrity,
        manifest: opts.manifest,
        onProgress: opts.onProgress,
        onStart: opts.onStart,
        registry: resolution.registry,
    });
}
const isAbsolutePath = /^[/]|^[A-Za-z]:/;
function resolvePath(where, spec) {
    if (isAbsolutePath.test(spec))
        return spec;
    return path.resolve(where, spec);
}
async function fetchFromLocalTarball(cafs, tarball, opts) {
    try {
        const tarballStream = fs.createReadStream(tarball);
        const [fetchResult] = (await Promise.all([
            cafs.addFilesFromTarball(tarballStream, opts.manifest),
            opts.integrity && ssri.checkStream(tarballStream, opts.integrity),
        ]));
        return { filesIndex: fetchResult };
    }
    catch (err) {
        const error = new createDownloader_1.TarballIntegrityError({
            attempts: 1,
            algorithm: err['algorithm'],
            expected: err['expected'],
            found: err['found'],
            sri: err['sri'],
            url: tarball,
        });
        error['resource'] = tarball;
        throw error;
    }
}
//# sourceMappingURL=index.js.map