UNPKG

@pureweb/platform-streaming-agent

Version:

The PureWeb platform streaming agent enables your game to communicate and stream through the PureWeb Platform

346 lines (345 loc) 13.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Configuration = exports.DefaultConfiguration = void 0; const platform_sdk_1 = require("@pureweb/platform-sdk"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const MAXIMUM_RUNTIME = 8 * 3600; // 8 hours default maximum runtime in seconds exports.DefaultConfiguration = { streamerPort: 8888, shutdownOnDisconnect: true, logLevel: 'info', lingerTimeoutSeconds: -1, rendezvousTimeoutSeconds: -1, streamerTimeoutSeconds: -1, reconnectTimeoutMilliseconds: 30000, connectionTimeout: 900, claimedTimeoutSeconds: 900 }; class Configuration { constructor() { this._streamerPort = exports.DefaultConfiguration.streamerPort; this._shutdownOnDisconnect = exports.DefaultConfiguration.shutdownOnDisconnect; this._logLevel = exports.DefaultConfiguration.logLevel; this._lingerTimeoutSeconds = exports.DefaultConfiguration.lingerTimeoutSeconds; this._rendezvousTimeoutSeconds = exports.DefaultConfiguration.rendezvousTimeoutSeconds; this._reconnectTimeoutMilliseconds = exports.DefaultConfiguration.reconnectTimeoutMilliseconds; this._streamerTimeoutSeconds = exports.DefaultConfiguration.streamerTimeoutSeconds; this._claimedTimeoutSeconds = exports.DefaultConfiguration.claimedTimeoutSeconds; } get agentToken() { return this._agentToken; } set agentToken(value) { this._agentToken = value; } get streamerPort() { return this._streamerPort; } set streamerPort(value) { this._streamerPort = value; } get sidecarPort() { return this._sidecarPort; } set sidecarPort(value) { this._sidecarPort = value; } get leaseUrl() { return this._leaseUrl; } set leaseUrl(value) { this._leaseUrl = value; } get shutdownOnDisconnect() { return this._shutdownOnDisconnect; } set shutdownOnDisconnect(value) { this._shutdownOnDisconnect = value; } get connectionTimeout() { return this._connectionTimeout; } set connectionTimeout(value) { this._connectionTimeout = value; } get logLevel() { return this._logLevel; } set logLevel(value) { this._logLevel = value; } get lingerTimeoutSeconds() { return this._lingerTimeoutSeconds; } set lingerTimeoutSeconds(value) { this._lingerTimeoutSeconds = value; } get rendezvousTimeoutSeconds() { return this._rendezvousTimeoutSeconds; } set rendezvousTimeoutSeconds(value) { this._rendezvousTimeoutSeconds = value; } get reconnectTimeoutMilliseconds() { return this._reconnectTimeoutMilliseconds; } set reconnectTimeoutMilliseconds(value) { this._reconnectTimeoutMilliseconds = value; } get ueVersion() { return this._ueVersion; } set ueVersion(value) { this._ueVersion = value; } get configFile() { return this._configFile; } set configFile(value) { this._configFile = value; } get streamService() { return this._streamService; } set streamService(value) { this._streamService = value; } get maximumRuntime() { return this._maximumRuntime; } set maximumRuntime(value) { this._maximumRuntime = value; } get standbyMode() { return this._bStandbyMode; } set standbyMode(value) { this._bStandbyMode = value; } get streamReady() { return this._bStreamReady; } set streamReady(value) { this._bStreamReady = value; } get streamerTimeoutSeconds() { return this._streamerTimeoutSeconds; } set streamerTimeoutSeconds(value) { this._streamerTimeoutSeconds = value; } get claimedTimeoutSeconds() { return this._claimedTimeoutSeconds; } set claimedTimeoutSeconds(value) { this._streamerTimeoutSeconds = value; } deleteFile(filePath) { const resolvedPath = path_1.default.resolve(filePath); try { if (fs_1.default.existsSync(resolvedPath)) { fs_1.default.unlinkSync(resolvedPath); } } catch (error) { // just log. Not critical. console.error(`Error deleting file at ${resolvedPath}:`, error); } } readConfigurationFile(filePath) { if (filePath === undefined) { return {}; } const absolutePath = path_1.default.resolve(filePath); try { const fileContent = fs_1.default.readFileSync(absolutePath, 'utf-8'); return JSON.parse(fileContent); } catch (error) { console.error(`Error reading or parsing provided configuration file at ${filePath}:`, error); throw error; } finally { this.deleteFile(absolutePath); } } // eslint-disable-next-line @typescript-eslint/no-explicit-any init(cmdArguments, processEnviromentVars) { if (cmdArguments.configFile) { this.configFile = cmdArguments.configFile; } else if (processEnviromentVars.PUREWEB_CONFIGURATION_FILE) { this.configFile = processEnviromentVars.PUREWEB_CONFIGURATION_FILE; } const fileConfiguration = this.readConfigurationFile(this.configFile); try { if (cmdArguments.agentToken) { this.agentToken = new platform_sdk_1.AgentToken(cmdArguments.agentToken); } else if (processEnviromentVars.PUREWEB_AGENT_TOKEN) { this.agentToken = new platform_sdk_1.AgentToken(processEnviromentVars.PUREWEB_AGENT_TOKEN); } else if (fileConfiguration.agentToken) { this.agentToken = new platform_sdk_1.AgentToken(fileConfiguration.agentToken); } else { throw { name: ' MissingTokenError', message: `An agent token is always required` }; } if (this.agentToken) { if (this.agentToken.isExpired) { throw { name: ' ExpiredToken', message: `The provided agent token has expired` }; } else if (!this.agentToken.isValid()) { throw { name: ' InvalidToken', message: `The provided agent token is not valid` }; } } if (cmdArguments.streamerPort) { this.streamerPort = cmdArguments.streamerPort; } else if (processEnviromentVars.PUREWEB_STREAMER_PORT) { this.streamerPort = Number(processEnviromentVars.PUREWEB_STREAMER_PORT); } else if (fileConfiguration.streamerPort) { this.streamerPort = fileConfiguration.streamerPort; } if (cmdArguments.sidecarPort) { this.sidecarPort = cmdArguments.sidecarPort; } else if (processEnviromentVars.PUREWEB_SIDECAR_PORT) { this.sidecarPort = Number(processEnviromentVars.PUREWEB_SIDECAR_PORT); } else if (fileConfiguration.sidecarPort) { this.sidecarPort = fileConfiguration.sidecarPort; } if (cmdArguments.leaseUrl) { this.leaseUrl = cmdArguments.leaseUrl; } else if (processEnviromentVars.PUREWEB_LEASE_URL) { this.leaseUrl = processEnviromentVars.PUREWEB_LEASE_URL; } else if (fileConfiguration.leaseUrl) { this.leaseUrl = fileConfiguration.leaseUrl; } if (cmdArguments.shutdownOnDisconnect) { this.shutdownOnDisconnect = JSON.parse(cmdArguments.shutdownOnDisconnect); } else if (processEnviromentVars.PUREWEB_SHUTDOWN_ON_DISCONNECT) { this.shutdownOnDisconnect = JSON.parse(processEnviromentVars.PUREWEB_SHUTDOWN_ON_DISCONNECT); } else if (fileConfiguration.shutdownOnDisconnect) { this.shutdownOnDisconnect = fileConfiguration.shutdownOnDisconnect; } if (cmdArguments.connectionTimeout) { this.connectionTimeout = cmdArguments.connectionTimeout; } else if (processEnviromentVars.PUREWEB_CONNECTION_TIMEOUT) { this.connectionTimeout = Number(processEnviromentVars.PUREWEB_CONNECTION_TIMEOUT); } else if (fileConfiguration.connectionTimeout) { this.connectionTimeout = fileConfiguration.connectionTimeout; } if (cmdArguments.logLevel) { this.logLevel = cmdArguments.logLevel; } else if (processEnviromentVars.PUREWEB_LOG_LEVEL) { this.logLevel = processEnviromentVars.PUREWEB_LOG_LEVEL; } else if (fileConfiguration.logLevel) { this.logLevel = fileConfiguration.logLevel; } if (cmdArguments.lingerTimeoutSeconds) { this.lingerTimeoutSeconds = cmdArguments.lingerTimeoutSeconds; } else if (processEnviromentVars.PUREWEB_LINGER_TIMEOUT) { this.lingerTimeoutSeconds = Number(processEnviromentVars.PUREWEB_LINGER_TIMEOUT); } else if (fileConfiguration.lingerTimeoutSeconds) { this.lingerTimeoutSeconds = fileConfiguration.lingerTimeoutSeconds; } if (cmdArguments.rendezvousTimeoutSeconds) { this.rendezvousTimeoutSeconds = cmdArguments.rendezvousTimeoutSeconds; } else if (processEnviromentVars.PUREWEB_RENDEZVOUS_TIMEOUT) { this.rendezvousTimeoutSeconds = Number(processEnviromentVars.PUREWEB_RENDEZVOUS_TIMEOUT); } else if (fileConfiguration.rendezvousTimeoutSeconds) { this.rendezvousTimeoutSeconds = fileConfiguration.rendezvousTimeoutSeconds; } if (cmdArguments.reconnectTimeoutMilliseconds) { this.reconnectTimeoutMilliseconds = cmdArguments.reconnectTimeoutMilliseconds; } else if (processEnviromentVars.PUREWEB_RECONNECT_TIMEOUT) { this.reconnectTimeoutMilliseconds = Number(processEnviromentVars.PUREWEB_RECONNECT_TIMEOUT); } else if (fileConfiguration.reconnectTimeoutMilliseconds) { this.reconnectTimeoutMilliseconds = fileConfiguration.reconnectTimeoutMilliseconds; } if (cmdArguments.unrealEngineVersion) { this.ueVersion = cmdArguments.unrealEngineVersion; } else if (processEnviromentVars.UE_VERSION) { this.ueVersion = processEnviromentVars.UE_VERSION; } else if (fileConfiguration.ueVersion) { this.ueVersion = fileConfiguration.ueVersion; } if (cmdArguments.streamService) { this.streamService = cmdArguments.streamService; } else if (processEnviromentVars.PUREWEB_STREAM_SERVICE) { this.streamService = processEnviromentVars.PUREWEB_STREAM_SERVICE; } else if (fileConfiguration.streamService) { this.streamService = fileConfiguration.streamService; } if (cmdArguments.maximumRuntime) { this.maximumRuntime = cmdArguments.maximumRuntime; } else if (processEnviromentVars.PUREWEB_MAXIMUM_RUNTIME) { this.maximumRuntime = Number(processEnviromentVars.PUREWEB_MAXIMUM_RUNTIME); } else if (fileConfiguration.maximumRuntime) { this.maximumRuntime = fileConfiguration.maximumRuntime; } this.maximumRuntime = this.maximumRuntime || MAXIMUM_RUNTIME; if (processEnviromentVars.PUREWEB_STANDBY_MODE === 'true') { this.standbyMode = true; } else { this.standbyMode = false; } if (processEnviromentVars.PUREWEB_STREAM_READY === 'true') { this.streamReady = true; } else { this.streamReady = false; } if (cmdArguments.streamerTimeoutSeconds) { this.streamerTimeoutSeconds = cmdArguments.streamerTimeoutSeconds; } else if (processEnviromentVars.PUREWEB_STREAMER_TIMEOUT) { this.streamerTimeoutSeconds = Number(processEnviromentVars.PUREWEB_STREAMER_TIMEOUT); } else if (fileConfiguration.streamerTimeoutSeconds) { this.streamerTimeoutSeconds = fileConfiguration.streamerTimeoutSeconds; } if (cmdArguments.claimedTimeoutSeconds) { this.claimedTimeoutSeconds = cmdArguments.claimedTimeoutSeconds; } else if (processEnviromentVars.PUREWEB_CLAIMED_TIMEOUT) { this.claimedTimeoutSeconds = Number(processEnviromentVars.PUREWEB_CLAIMED_TIMEOUT); } else if (fileConfiguration.claimedTimeoutSeconds) { this.claimedTimeoutSeconds = fileConfiguration.claimedTimeoutSeconds; } } catch (e) { throw `Invalid Streaming Agent configuration: ${e.name} - ${e.message}`; } } } exports.Configuration = Configuration;