"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocalOrRemoteStream = getLocalOrRemoteStream;
exports.decodeURIComponentNoThrow = decodeURIComponentNoThrow;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const common_1 = require("./types/common");
async function getLocalOrRemoteStream(uri, out) {
    if ((0, common_1.isURL)(uri)) {
        const result = await (0, common_1.createRemoteStream)(uri);
        return {
            totalBytes: +(result.headers.get('Content-Length') || 0),
            stream: result.body,
        };
    }
    else {
        const filename = path_1.default.isAbsolute(uri) ? uri : path_1.default.join(out, uri);
        return {
            totalBytes: fs_1.default.statSync(filename).size,
            stream: fs_1.default.createReadStream(filename),
        };
    }
}
function decodeURIComponentNoThrow(uri) {
    try {
        return decodeURIComponent(uri);
    }
    catch (e) {
        // avoid throwing exception on a failure to decode URI component
        return uri;
    }
}
