"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 fs = require("fs");
function getFdDir() {
    switch (process.platform) {
        case 'linux':
            return '/proc/self/fd';
        case 'darwin':
            return '/dev/fd';
        default:
            return '';
    }
}
function getOpenFds() {
    const fdDir = getFdDir();
    return new Promise((resolve, reject) => {
        if (!fdDir) {
            reject("Platform unsupported");
        }
        fs.readdir(fdDir, (err, fds) => {
            if (err) {
                reject(err);
            }
            else {
                resolve(fds);
            }
        });
    });
}
exports.getOpenFds = getOpenFds;
function getFileType(path) {
    return new Promise((resolve, reject) => {
        fs.stat(path, (err, stats) => {
            if (err) {
                reject(err);
            }
            else {
                const mode = stats.mode;
                const type = mode & fs.constants.S_IFMT;
                switch (type) {
                    case (fs.constants.S_IFREG):
                        resolve('regular');
                        return;
                    case (fs.constants.S_IFDIR):
                        resolve('directory');
                        return;
                    case (fs.constants.S_IFCHR):
                        resolve('character-device');
                        return;
                    case (fs.constants.S_IFBLK):
                        resolve('block-device');
                        return;
                    case (fs.constants.S_IFIFO):
                        resolve('pipe');
                        return;
                    case (fs.constants.S_IFLNK):
                        resolve('symbolic-link');
                        return;
                    case (fs.constants.S_IFSOCK):
                        resolve('socket');
                        return;
                    default:
                        resolve('unknown');
                        return;
                }
            }
        });
    });
}
exports.getFileType = getFileType;
function getSummary() {
    return __awaiter(this, void 0, void 0, function* () {
        const summary = {
            "regular": 0,
            "directory": 0,
            "character-device": 0,
            "block-device": 0,
            "pipe": 0,
            "symbolic-link": 0,
            "socket": 0,
            "unknown": 0,
            "total": 0
        };
        let fds = [];
        try {
            fds = yield getOpenFds();
        }
        catch (err) { }
        for (let i = 0; i < fds.length; ++i) {
            const file = getFdDir() + '/' + fds[i];
            try {
                const type = yield getFileType(file);
                summary[type]++;
                summary.total++;
            }
            catch (err) { }
        }
        return summary;
    });
}
exports.getSummary = getSummary;
//# sourceMappingURL=openFiles.js.map