UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature toggles service. It provides different strategies for handling feature toggles.

112 lines 4.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProxyService = void 0; const unleash_client_1 = require("unleash-client"); const proxy_1 = require("../proxy"); const api_token_1 = require("../types/models/api-token"); const frontend_settings_1 = require("../types/settings/frontend-settings"); const util_1 = require("../util"); const error_1 = require("../error"); const assert_1 = __importDefault(require("assert")); const date_fns_1 = require("date-fns"); class ProxyService { constructor(config, stores, services) { this.clients = new Map(); this.config = config; this.logger = config.getLogger('services/proxy-service.ts'); this.stores = stores; this.services = services; this.timer = setInterval(() => this.fetchFrontendSettings(), (0, date_fns_1.minutesToMilliseconds)(2)).unref(); } async getProxyFeatures(token, context) { const client = await this.clientForProxyToken(token); const definitions = client.getFeatureToggleDefinitions() || []; return definitions .filter((feature) => client.isEnabled(feature.name, context)) .map((feature) => ({ name: feature.name, enabled: Boolean(feature.enabled), variant: client.forceGetVariant(feature.name, context), impressionData: Boolean(feature.impressionData), })); } async getAllProxyFeatures(token, context) { const client = await this.clientForProxyToken(token); const definitions = client.getFeatureToggleDefinitions() || []; return definitions.map((feature) => ({ name: feature.name, enabled: Boolean(feature.enabled), variant: client.forceGetVariant(feature.name, context), impressionData: Boolean(feature.impressionData), })); } async registerProxyMetrics(token, metrics, ip) { ProxyService.assertExpectedTokenType(token); const environment = this.services.clientMetricsServiceV2.resolveMetricsEnvironment(token, metrics); await this.services.clientMetricsServiceV2.registerClientMetrics({ ...metrics, environment }, ip); } async clientForProxyToken(token) { ProxyService.assertExpectedTokenType(token); if (!this.clients.has(token.secret)) { this.clients.set(token.secret, await this.createClientForProxyToken(token)); } return this.clients.get(token.secret); } async createClientForProxyToken(token) { const repository = new proxy_1.ProxyRepository(this.config, this.stores, this.services, token); const client = await (0, unleash_client_1.startUnleash)({ appName: 'proxy', url: 'unused', storageProvider: new unleash_client_1.InMemStorageProvider(), disableMetrics: true, repository, }); client.on(unleash_client_1.UnleashEvents.Error, (error) => { this.logger.error(error); }); return client; } deleteClientForProxyToken(secret) { this.clients.delete(secret); } stopAll() { this.clients.forEach((client) => client.destroy()); } static assertExpectedTokenType({ type }) { (0, assert_1.default)(type === api_token_1.ApiTokenType.FRONTEND || type === api_token_1.ApiTokenType.ADMIN); } async setFrontendSettings(value, createdBy) { const error = (0, util_1.validateOrigins)(value.frontendApiOrigins); if (error) { throw new error_1.BadDataError(error); } await this.services.settingService.insert(frontend_settings_1.frontendSettingsKey, value, createdBy); } async fetchFrontendSettings() { try { this.cachedFrontendSettings = await this.services.settingService.get(frontend_settings_1.frontendSettingsKey, { frontendApiOrigins: this.config.frontendApiOrigins, }); } catch (error) { this.logger.debug('Unable to fetch frontend settings'); } return this.cachedFrontendSettings; } async getFrontendSettings(useCache = true) { if (useCache && this.cachedFrontendSettings) { return this.cachedFrontendSettings; } return this.fetchFrontendSettings(); } destroy() { clearInterval(this.timer); this.timer = null; } } exports.ProxyService = ProxyService; //# sourceMappingURL=proxy-service.js.map