"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseServerFactory = void 0;
const fs_1 = require("fs");
const http_1 = require("http");
const https_1 = require("https");
const LogUtil_1 = require("../logging/LogUtil");
/**
 * Creates an HTTP(S) server native Node.js `http`/`https` modules.
 *
 * Will apply a {@link ServerConfigurator} to the server,
 * which should be used to attach listeners.
 */
class BaseServerFactory {
    constructor(configurator, options = { https: false }) {
        this.logger = (0, LogUtil_1.getLoggerFor)(this);
        this.configurator = configurator;
        this.options = { ...options };
    }
    /**
     * Creates an HTTP(S) server.
     */
    async createServer() {
        const options = this.createServerOptions();
        const server = this.options.https ? (0, https_1.createServer)(options) : (0, http_1.createServer)(options);
        await this.configurator.handleSafe(server);
        return server;
    }
    createServerOptions() {
        const options = { ...this.options };
        for (const id of ['key', 'cert', 'pfx']) {
            const val = options[id];
            if (val) {
                options[id] = (0, fs_1.readFileSync)(val, 'utf8');
            }
        }
        return options;
    }
}
exports.BaseServerFactory = BaseServerFactory;
//# sourceMappingURL=BaseServerFactory.js.map