"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const es6_promisify_1 = require("es6-promisify");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const fsReadFile = es6_promisify_1.promisify(fs_1.default.readFile);
const fsWriteFile = es6_promisify_1.promisify(fs_1.default.writeFile);
const fsExists = (p) => new Promise(resolve => {
    fs_1.default.exists(p, exists => {
        resolve(exists);
    });
});
class CertificateStorage {
    constructor(storagePath) {
        this.storagePath = storagePath;
    }
    has(domain) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield fsExists(this.getCertPath(domain));
        });
    }
    get(domain) {
        return __awaiter(this, void 0, void 0, function* () {
            const cert = {
                cert: '',
                key: '',
            };
            cert.key = yield fsReadFile(this.getKeyPath(domain), { encoding: 'utf-8' });
            cert.cert = yield fsReadFile(this.getCertPath(domain), {
                encoding: 'utf-8',
            });
            return cert;
        });
    }
    set(domain, cert) {
        return __awaiter(this, void 0, void 0, function* () {
            yield fsWriteFile(this.getCertPath(domain), cert.cert, {
                encoding: 'utf-8',
            });
            yield fsWriteFile(this.getKeyPath(domain), cert.key, { encoding: 'utf-8' });
        });
    }
    getRoot() {
        return __awaiter(this, void 0, void 0, function* () {
            const dir = path_1.default.join(this.storagePath, 'root');
            const root = {
                cert: yield fsReadFile(path_1.default.join(dir, 'zproxy.crt.pem'), {
                    encoding: 'utf-8',
                }),
                key: yield fsReadFile(path_1.default.join(dir, 'zproxy.key.pem'), {
                    encoding: 'utf-8',
                }),
            };
            return root;
        });
    }
    getCertPath(domain) {
        return path_1.default.join(this.storagePath, `${domain}.crt`);
    }
    getKeyPath(domain) {
        return path_1.default.join(this.storagePath, `${domain}.key`);
    }
}
exports.CertificateStorage = CertificateStorage;
//# sourceMappingURL=index.js.map