"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const check_package_1 = require("@pnpm/check-package");
const logger_1 = require("@pnpm/logger");
const pkgid_to_filename_1 = require("@pnpm/pkgid-to-filename");
const loadJsonFile = require("load-json-file");
const mkdirp = require("mkdirp-promise");
const fs = require("mz/fs");
const PQueue = require("p-queue");
const path = require("path");
const exists = require("path-exists");
const renameOverwrite = require("rename-overwrite");
const rimraf = require("rimraf-then");
const symlinkDir = require("symlink-dir");
const writeJsonFile = require("write-json-file");
const readPkg_1 = require("./fs/readPkg");
const loggers_1 = require("./loggers");
function default_1(resolve, fetchers, opts) {
    opts = opts || {};
    const networkConcurrency = opts.networkConcurrency || 16;
    const requestsQueue = new PQueue({
        concurrency: networkConcurrency,
    });
    requestsQueue['counter'] = 0; // tslint:disable-line
    requestsQueue['concurrency'] = networkConcurrency; // tslint:disable-line
    const fetch = fetcher.bind(null, fetchers);
    return resolveAndFetch.bind(null, {
        fetch,
        fetchingLocker: {},
        requestsQueue,
        resolve,
        storeIndex: opts.storeIndex,
        storePath: opts.storePath,
    });
}
exports.default = default_1;
function resolveAndFetch(ctx, wantedDependency, options) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            let latest;
            let pkg;
            let normalizedPref;
            let resolution = options.shrinkwrapResolution;
            let pkgId = options.currentPkgId;
            if (!resolution || options.update) {
                const resolveResult = yield ctx.requestsQueue.add(() => ctx.resolve(wantedDependency, {
                    defaultTag: options.defaultTag,
                    preferredVersions: options.preferredVersions,
                    prefix: options.prefix,
                    registry: options.registry,
                }), { priority: options.downloadPriority });
                // keep the shrinkwrap resolution when possible
                // to keep the original shasum
                if (pkgId !== resolveResult.id || !resolution) {
                    resolution = resolveResult.resolution;
                }
                pkgId = resolveResult.id;
                pkg = resolveResult.package;
                latest = resolveResult.latest;
                normalizedPref = resolveResult.normalizedPref;
            }
            const id = pkgId;
            loggers_1.progressLogger.debug({ status: 'resolved', pkgId: id, pkg: options.loggedPkg });
            if (resolution.type === 'directory') {
                if (!pkg) {
                    throw new Error(`Couldn't read package.json of local dependency ${wantedDependency.alias ? wantedDependency.alias + '@' : ''}${wantedDependency.pref}`);
                }
                return {
                    body: {
                        cacheByEngine: options.sideEffectsCache ? yield getCacheByEngine(ctx.storePath, id) : new Map(),
                        id,
                        isLocal: true,
                        manifest: pkg,
                        normalizedPref,
                        resolution: resolution,
                    },
                };
            }
            const targetRelative = pkgid_to_filename_1.default(id);
            const target = path.join(ctx.storePath, targetRelative);
            // We can skip fetching the package only if the manifest
            // is present after resolution
            if (options.skipFetch && pkg) {
                return {
                    body: {
                        cacheByEngine: options.sideEffectsCache ? yield getCacheByEngine(ctx.storePath, id) : new Map(),
                        id,
                        inStoreLocation: target,
                        isLocal: false,
                        latest,
                        manifest: pkg,
                        normalizedPref,
                        resolution,
                    },
                };
            }
            if (!ctx.fetchingLocker[id]) {
                ctx.fetchingLocker[id] = fetchToStore({
                    fetch: ctx.fetch,
                    pkg,
                    pkgId: id,
                    prefix: options.prefix,
                    requestsQueue: ctx.requestsQueue,
                    resolution: resolution,
                    storeIndex: ctx.storeIndex,
                    storePath: ctx.storePath,
                    target,
                    targetRelative,
                    verifyStoreIntegrity: options.verifyStoreIntegrity,
                });
            }
            if (pkg) {
                return {
                    body: {
                        cacheByEngine: options.sideEffectsCache ? yield getCacheByEngine(ctx.storePath, id) : new Map(),
                        id,
                        inStoreLocation: target,
                        isLocal: false,
                        latest,
                        manifest: pkg,
                        normalizedPref,
                        resolution,
                    },
                    fetchingFiles: ctx.fetchingLocker[id].fetchingFiles,
                    finishing: ctx.fetchingLocker[id].finishing,
                };
            }
            return {
                body: {
                    cacheByEngine: options.sideEffectsCache ? yield getCacheByEngine(ctx.storePath, id) : new Map(),
                    id,
                    inStoreLocation: target,
                    isLocal: false,
                    latest,
                    normalizedPref,
                    resolution,
                },
                fetchingFiles: ctx.fetchingLocker[id].fetchingFiles,
                fetchingManifest: ctx.fetchingLocker[id].fetchingManifest,
                finishing: ctx.fetchingLocker[id].finishing,
            };
        }
        catch (err) {
            loggers_1.progressLogger.debug({ status: 'error', pkg: options.loggedPkg });
            throw err;
        }
    });
}
function fetchToStore(opts) {
    const fetchingManifest = differed();
    const fetchingFiles = differed();
    const finishing = differed();
    doFetchToStore();
    if (!opts.pkg) {
        return {
            fetchingFiles: fetchingFiles.promise,
            fetchingManifest: fetchingManifest.promise,
            finishing: finishing.promise,
        };
    }
    return {
        fetchingFiles: fetchingFiles.promise,
        finishing: finishing.promise,
    };
    function doFetchToStore() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                loggers_1.progressLogger.debug({
                    pkgId: opts.pkgId,
                    status: 'resolving_content',
                });
                const target = opts.target;
                const linkToUnpacked = path.join(target, 'package');
                // We can safely assume that if there is no data about the package in `store.json` then
                // it is not in the store yet.
                // In case there is record about the package in `store.json`, we check it in the file system just in case
                const targetExists = opts.storeIndex[opts.targetRelative] && (yield exists(path.join(linkToUnpacked, 'package.json')));
                if (targetExists) {
                    // if target exists and it wasn't modified, then no need to refetch it
                    const satisfiedIntegrity = opts.verifyStoreIntegrity
                        ? yield check_package_1.default(linkToUnpacked)
                        : yield loadJsonFile(path.join(path.dirname(linkToUnpacked), 'integrity.json'));
                    if (satisfiedIntegrity) {
                        loggers_1.progressLogger.debug({
                            pkgId: opts.pkgId,
                            status: 'found_in_store',
                        });
                        fetchingFiles.resolve({
                            filenames: Object.keys(satisfiedIntegrity).filter((f) => !satisfiedIntegrity[f].isDir),
                            fromStore: true,
                        });
                        if (!opts.pkg) {
                            readPkg_1.fromDir(linkToUnpacked)
                                .then(fetchingManifest.resolve)
                                .catch(fetchingManifest.reject);
                        }
                        finishing.resolve(undefined);
                        return;
                    }
                    logger_1.default.warn(`Refetching ${target} to store, as it was modified`);
                }
                // We fetch into targetStage directory first and then fs.rename() it to the
                // target directory.
                const targetStage = `${target}_stage`;
                yield rimraf(targetStage);
                let packageIndex = {};
                yield Promise.all([
                    (() => __awaiter(this, void 0, void 0, function* () {
                        // Tarballs are requested first because they are bigger than metadata files.
                        // However, when one line is left available, allow it to be picked up by a metadata request.
                        // This is done in order to avoid situations when tarballs are downloaded in chunks
                        // As much tarballs should be downloaded simultaneously as possible.
                        const priority = (++opts.requestsQueue['counter'] % opts.requestsQueue['concurrency'] === 0 ? -1 : 1) * 1000; // tslint:disable-line
                        packageIndex = yield opts.requestsQueue.add(() => opts.fetch(opts.resolution, targetStage, {
                            cachedTarballLocation: path.join(opts.storePath, opts.pkgId, 'packed.tgz'),
                            onProgress: (downloaded) => {
                                loggers_1.progressLogger.debug({ status: 'fetching_progress', pkgId: opts.pkgId, downloaded });
                            },
                            onStart: (size, attempt) => {
                                loggers_1.progressLogger.debug({ status: 'fetching_started', pkgId: opts.pkgId, size, attempt });
                            },
                            pkgId: opts.pkgId,
                            prefix: opts.prefix,
                        }), { priority });
                    }))(),
                    // removing only the folder with the unpacked files
                    // not touching tarball and integrity.json
                    targetExists && (yield rimraf(path.join(target, 'node_modules'))),
                ]);
                loggers_1.progressLogger.debug({
                    pkgId: opts.pkgId,
                    status: 'fetched',
                });
                // Ideally, fetchingFiles shouldn't care about when integrity is calculated
                //
                // TODO: Move renaming of the stage folder into the fetcher.
                //       It will allow to finish fetchingFiles earlier
                //       than integrity is generated
                if (!targetExists) {
                    if (opts.verifyStoreIntegrity) {
                        const fileIntegrities = yield Promise.all(Object.keys(packageIndex)
                            .map((filename) => packageIndex[filename].generatingIntegrity
                            .then((fileIntegrity) => ({
                            [filename]: {
                                integrity: fileIntegrity,
                                size: packageIndex[filename].size,
                            },
                        }))));
                        const integrity = fileIntegrities
                            .reduce((acc, info) => {
                            Object.assign(acc, info);
                            return acc;
                        }, {});
                        yield writeJsonFile(path.join(target, 'integrity.json'), integrity, { indent: null });
                    }
                    else {
                        // TODO: save only filename: {size}
                        yield writeJsonFile(path.join(target, 'integrity.json'), packageIndex, { indent: null });
                    }
                    finishing.resolve(undefined);
                }
                else {
                    finishing.resolve(undefined);
                }
                let pkg;
                if (opts.pkg) {
                    pkg = opts.pkg;
                }
                else {
                    pkg = yield readPkg_1.fromDir(targetStage);
                    fetchingManifest.resolve(pkg);
                }
                const unpacked = path.join(target, 'node_modules', pkg.name);
                yield mkdirp(path.dirname(unpacked));
                // rename(oldPath, newPath) is an atomic operation, so we do it at the
                // end
                yield renameOverwrite(targetStage, unpacked);
                yield symlinkDir(unpacked, linkToUnpacked);
                fetchingFiles.resolve({
                    filenames: Object.keys(packageIndex).filter((f) => !packageIndex[f].isDir),
                    fromStore: false,
                });
            }
            catch (err) {
                fetchingFiles.reject(err);
                if (!opts.pkg) {
                    fetchingManifest.reject(err);
                }
            }
        });
    }
}
// tslint:disable-next-line
function noop() { }
function differed() {
    let pResolve = noop;
    let pReject = noop;
    const promise = new Promise((resolve, reject) => {
        pResolve = resolve;
        pReject = reject;
    });
    return {
        promise,
        reject: pReject,
        resolve: pResolve,
    };
}
function fetcher(fetcherByHostingType, resolution, target, opts) {
    return __awaiter(this, void 0, void 0, function* () {
        const fetch = fetcherByHostingType[resolution.type || 'tarball'];
        if (!fetch) {
            throw new Error(`Fetching for dependency type "${resolution.type}" is not supported`);
        }
        try {
            return yield fetch(resolution, target, opts);
        }
        catch (err) {
            logger_1.default.error(`Fetching ${opts.pkgId} failed!`);
            throw err;
        }
    });
}
function getCacheByEngine(storePath, id) {
    return __awaiter(this, void 0, void 0, function* () {
        const map = new Map();
        const cacheRoot = path.join(storePath, id, 'side_effects');
        if (!(yield fs.exists(cacheRoot))) {
            return map;
        }
        const dirContents = (yield fs.readdir(cacheRoot)).map((content) => path.join(cacheRoot, content));
        yield Promise.all(dirContents.map((dir) => __awaiter(this, void 0, void 0, function* () {
            if (!(yield fs.lstat(dir)).isDirectory()) {
                return;
            }
            const engineName = path.basename(dir);
            map[engineName] = path.join(dir, 'package');
        })));
        return map;
    });
}
//# sourceMappingURL=packageRequester.js.map