"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 getRegistryAuthInfo = require("registry-auth-token");
const R = require("ramda");
const pLimit = require("p-limit");
const mkdirp = require("mkdirp-promise");
const path = require("path");
const createWriteStreamAtomic = require("fs-write-stream-atomic");
const ssri = require("ssri");
exports.default = (client, opts) => {
    const limit = pLimit(opts.networkConcurrency);
    function getJSON(url) {
        return __awaiter(this, void 0, void 0, function* () {
            return limit(() => new Promise((resolve, reject) => {
                const getOpts = {
                    auth: getAuth(url),
                    fullMetadata: false,
                };
                client.get(url, getOpts, (err, data, raw, res) => {
                    if (err)
                        return reject(err);
                    resolve(data);
                });
            }));
        });
    }
    function download(url, saveto, opts) {
        return limit(() => __awaiter(this, void 0, void 0, function* () {
            yield mkdirp(path.dirname(saveto));
            const auth = getAuth(url) || opts.registry && getAuth(opts.registry);
            return new Promise((resolve, reject) => {
                client.fetch(url, { auth }, (err, res) => __awaiter(this, void 0, void 0, function* () {
                    if (err)
                        return reject(err);
                    const writeStream = createWriteStreamAtomic(saveto);
                    const stream = res
                        .on('response', start)
                        .on('error', reject)
                        .pipe(writeStream)
                        .on('error', reject);
                    if (opts.integrity) {
                        try {
                            yield ssri.checkStream(res, opts.integrity);
                        }
                        catch (err) {
                            reject(err);
                        }
                    }
                    stream.on('finish', resolve);
                    function start(res) {
                        if (res.statusCode !== 200) {
                            return reject(new Error(`Invalid response: ${res.statusCode}`));
                        }
                        if (opts.onStart)
                            opts.onStart();
                        if (opts.onProgress && ('content-length' in res.headers)) {
                            const onProgress = opts.onProgress;
                            let downloaded = 0;
                            let size = +res.headers['content-length'];
                            res.on('data', (chunk) => {
                                downloaded += chunk.length;
                                onProgress(downloaded, size);
                            });
                        }
                    }
                }));
            });
        }));
    }
    function getAuth(url) {
        const authInfo = getRegistryAuthInfo(url, { recursive: true, npmrc: opts.rawNpmConfig });
        if (!authInfo)
            return null;
        switch (authInfo.type) {
            case 'Bearer':
                return {
                    alwaysAuth: opts.alwaysAuth,
                    token: authInfo.token,
                };
            case 'Basic':
                return {
                    alwaysAuth: opts.alwaysAuth,
                    username: authInfo.username,
                    password: authInfo.password,
                };
            default:
                throw new Error(`Unsupported authorization type '${authInfo.type}'`);
        }
    }
    return {
        getJSON: R.memoize(getJSON),
        download,
    };
};
//# sourceMappingURL=got.js.map