"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const error_1 = require("@pnpm/error");
const getCredentialsByURI = require("credentials-by-uri");
const mem = require("mem");
const fs = require("mz/fs");
const path = require("path");
const ssri = require("ssri");
const createDownloader_1 = require("./createDownloader");
function default_1(opts) {
    const download = createDownloader_1.default({
        alwaysAuth: opts.alwaysAuth || false,
        ca: opts.ca,
        cert: opts.cert,
        key: opts.key,
        localAddress: opts.localAddress,
        proxy: opts.httpsProxy || opts.proxy,
        registry: opts.registry,
        retry: {
            factor: opts.fetchRetryFactor,
            maxTimeout: opts.fetchRetryMaxtimeout,
            minTimeout: opts.fetchRetryMintimeout,
            retries: opts.fetchRetries,
        },
        // TODO: cover with tests this option
        // https://github.com/pnpm/pnpm/issues/1062
        strictSSL: typeof opts.strictSsl === 'boolean'
            ? opts.strictSsl
            : true,
        userAgent: opts.userAgent,
    });
    const getCreds = getCredentialsByURI.bind(null, opts.rawConfig);
    return {
        tarball: fetchFromTarball.bind(null, {
            download,
            getCredentialsByURI: mem((registry) => getCreds(registry)),
            offline: opts.offline,
        }),
    };
}
exports.default = default_1;
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) {
        err.attempts = 1;
        err.resource = tarball;
        throw err;
    }
}
//# sourceMappingURL=index.js.map