UNPKG

@aws-amplify/amplify-appsync-simulator

Version:

An AppSync Simulator to test AppSync API.

86 lines 3.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppSyncSimulatorServer = void 0; const operations_1 = require("./operations"); const http_1 = require("http"); const https_1 = require("https"); const fs_1 = require("fs"); const promise_toolbox_1 = require("promise-toolbox"); const ip_1 = require("ip"); const websocket_subscription_1 = require("./websocket-subscription"); const get_port_1 = __importDefault(require("get-port")); const server_1 = require("./subscription/websocket-server/server"); const BASE_PORT = 8900; const MAX_PORT = 9999; class AppSyncSimulatorServer { constructor(config, simulatorContext) { this.config = config; this.simulatorContext = simulatorContext; this._isHttps = false; this._operationServer = new operations_1.OperationServer(config, simulatorContext); if (!config.httpsConfig) { this._httpServer = (0, http_1.createServer)(this._operationServer.app); } else { try { const sslOptions = { key: (0, fs_1.readFileSync)(config.httpsConfig.sslKeyPath), cert: (0, fs_1.readFileSync)(config.httpsConfig.sslCertPath), }; this._isHttps = true; this._httpServer = (0, https_1.createServer)(sslOptions, this._operationServer.app); } catch (e) { throw new Error(`SSL key and certificate path provided are invalid. ${e.message}`); } } this._realTimeSubscriptionServer = new websocket_subscription_1.AppSyncSimulatorSubscriptionServer(simulatorContext, this._httpServer, server_1.REALTIME_SUBSCRIPTION_PATH); } async start() { let port = this.config.port; await this._realTimeSubscriptionServer.start(); if (!port) { port = await (0, get_port_1.default)({ port: get_port_1.default.makeRange(BASE_PORT, MAX_PORT), }); } else { try { await (0, get_port_1.default)({ port, }); } catch (e) { throw new Error(`Port ${port} is already in use. Please kill the program using this port and restart Mock`); } } this._httpServer.listen(port); await (0, promise_toolbox_1.fromEvent)(this._httpServer, 'listening').then(() => { const protocol = this._isHttps ? 'https' : 'http'; this._url = `${protocol}://${(0, ip_1.address)()}:${port}`; this._localhostUrl = `${protocol}://localhost:${port}`; }); } async stop() { await this._realTimeSubscriptionServer.stop(); this._httpServer.close(); } get url() { return { graphql: this._url, }; } get localhostUrl() { return { graphql: this._localhostUrl, }; } get isHttps() { return this._isHttps; } } exports.AppSyncSimulatorServer = AppSyncSimulatorServer; //# sourceMappingURL=index.js.map