UNPKG

@splitsoftware/splitio-commons

Version:
55 lines (54 loc) 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.releaseApiKey = exports.validateAndTrackApiKey = exports.usedKeysMap = exports.validateApiKey = void 0; var constants_1 = require("../../logger/constants"); var lang_1 = require("../lang"); var item = 'sdk_key'; // @TODO replace ApiKey with SdkKey in function names /** validates the given SDK key */ function validateApiKey(log, maybeSdkKey) { var sdkKey = false; if (maybeSdkKey == undefined) { // eslint-disable-line eqeqeq log.error(constants_1.ERROR_NULL, [constants_1.LOG_PREFIX_INSTANTIATION, item]); } else if ((0, lang_1.isString)(maybeSdkKey)) { if (maybeSdkKey.length > 0) sdkKey = maybeSdkKey; else log.error(constants_1.ERROR_EMPTY, [constants_1.LOG_PREFIX_INSTANTIATION, item]); } else { log.error(constants_1.ERROR_INVALID, [constants_1.LOG_PREFIX_INSTANTIATION, item]); } return sdkKey; } exports.validateApiKey = validateApiKey; // Exported for telemetry exports.usedKeysMap = {}; /** validates the given SDK key and also warns if it is in use */ function validateAndTrackApiKey(log, maybeSdkKey) { var sdkKey = validateApiKey(log, maybeSdkKey); // If sdkKey is correct, we'll save it as the instance creation should work. if (sdkKey) { if (!exports.usedKeysMap[sdkKey]) { // If this key is not present, only warning scenarios is that we have factories for other keys. exports.usedKeysMap[sdkKey] = 1; if (Object.keys(exports.usedKeysMap).length > 1) { log.warn(constants_1.WARN_SDK_KEY, ['an instance of the Split factory']); } } else { log.warn(constants_1.WARN_SDK_KEY, ["".concat(exports.usedKeysMap[sdkKey], " factory/ies with this SDK Key")]); exports.usedKeysMap[sdkKey]++; } } return sdkKey; } exports.validateAndTrackApiKey = validateAndTrackApiKey; function releaseApiKey(sdkKey) { if (exports.usedKeysMap[sdkKey]) exports.usedKeysMap[sdkKey]--; if (exports.usedKeysMap[sdkKey] === 0) delete exports.usedKeysMap[sdkKey]; } exports.releaseApiKey = releaseApiKey;