From fb5a20415618bcfefa9d8b7069c304c1b4a895ee Mon Sep 17 00:00:00 2001 From: OrenMe Date: Sun, 31 Mar 2019 17:33:48 +0300 Subject: [PATCH] feat: new token evaluator --- src/common/plugins/plugins-config-store.js | 52 ++++---- src/common/plugins/plugins-config.js | 138 ++++++++++----------- src/common/utils/evaluate.js | 86 +++++++++++++ src/kaltura-player.js | 7 +- 4 files changed, 185 insertions(+), 98 deletions(-) diff --git a/src/common/plugins/plugins-config-store.js b/src/common/plugins/plugins-config-store.js index 3ebb6283e..3ee76b14e 100644 --- a/src/common/plugins/plugins-config-store.js +++ b/src/common/plugins/plugins-config-store.js @@ -6,35 +6,35 @@ const defaultConfig: dataStoreType = { youbora: { playerVersion: '{{pVersion}}', playerName: '{{pName}}', - entryId: '{{entryId}}', - entryName: '{{entryName}}', - entryType: '{{entryType}}', - sessionId: '{{sessionId}}', - uiConfId: '{{uiConfId}}' + entryId: '{{sources.id}}', + entryName: '{{sources.metadata.name}}', + entryType: '{{sources.type}}', + sessionId: '{{session.id}}', + uiConfId: '{{provider.uiConfId}}' }, kanalytics: { playerVersion: '{{pVersion}}', playlistId: '{{playlistId}}', - entryId: '{{entryId}}', - entryType: '{{entryType}}', - sessionId: '{{sessionId}}', - ks: '{{ks}}', - uiConfId: '{{uiConfId}}', - partnerId: '{{partnerId}}', + entryId: '{{sources.id}}', + entryType: '{{sources.type}}', + sessionId: '{{session.id}}', + ks: '{{provider.ks}}', + uiConfId: '{{provider.uiConfId}}', + partnerId: '{{provider.partnerId}}', referrer: '{{referrer}}' }, googleAnalytics: { - entryId: '{{entryId}}', + entryId: '{{sources.id}}', entryName: '{{entryName}}', - uiConfId: '{{uiConfId}}', - partnerId: '{{partnerId}}' + uiConfId: '{{provider.uiConfId}}', + partnerId: '{{provider.partnerId}}' }, ottAnalytics: { - entryId: '{{entryId}}', - ks: '{{ks}}', - isAnonymous: '{{isAnonymous}}', - partnerId: '{{partnerId}}', - serviceUrl: '{{serviceUrl}}' + entryId: '{{sources.id}}', + ks: '{{provider.ks}}', + isAnonymous: '{{session.isAnonymous}}', + partnerId: '{{provider.partnerId}}', + serviceUrl: '{{provider.env.serviceUrl}}' }, ima: { playerVersion: '{{pVersion}}', @@ -43,20 +43,20 @@ const defaultConfig: dataStoreType = { kava: { playerVersion: '{{pVersion}}', playerName: '{{pName}}', - partnerId: '{{partnerId}}', + partnerId: '{{provider.partnerId}}', playlistId: '{{playlistId}}', - entryId: '{{entryId}}', - entryType: '{{entryType}}', - sessionId: '{{sessionId}}', - ks: '{{ks}}', - uiConfId: '{{uiConfId}}', + entryId: '{{sources.id}}', + entryType: '{{sources.type}}', + sessionId: '{{session.id}}', + ks: '{{provider.ks}}', + uiConfId: '{{provider.uiConfId}}', referrer: '{{referrer}}' }, comscore: { playerVersion: '{{pVersion}}' }, vr: { - rootElement: '{{domRootElementId}}' + rootElement: '{{targetId}}' } }; diff --git a/src/common/plugins/plugins-config.js b/src/common/plugins/plugins-config.js index 6e4cac012..aa043ab34 100644 --- a/src/common/plugins/plugins-config.js +++ b/src/common/plugins/plugins-config.js @@ -1,7 +1,7 @@ //@flow import {pluginConfig, templateRegex} from './plugins-config-store.js'; -import evaluate from '../utils/evaluate'; -import {getReferrer} from '../utils/kaltura-params'; +import {replace} from '../utils/evaluate'; +// import {getReferrer} from '../utils/kaltura-params'; import {Utils} from '@playkit-js/playkit-js'; /** @@ -36,56 +36,56 @@ const removeUnevaluatedExpression = (obj = {}): Object => * @param {KPOptionsObject} options - the kaltura player options object * @returns {Object} - data model */ -const getModel = (options: KPOptionsObject): Object => { - const dataModel: Object = { - pVersion: __VERSION__, - pName: __NAME__ - }; - if (options.targetId) { - dataModel.domRootElementId = options.targetId; - } - if (options.provider && options.provider.env) { - dataModel['serviceUrl'] = options.provider.env.serviceUrl; - } - const entryDataModel = { - referrer: getReferrer() - }; - if (options.provider) { - Utils.Object.mergeDeep(entryDataModel, { - ks: options.provider.ks, - uiConfId: options.provider.uiConfId, - partnerId: options.provider.partnerId - }); - } - if (options.session) { - Utils.Object.mergeDeep(entryDataModel, { - sessionId: options.session.id, - ks: options.session.ks, - isAnonymous: options.session.isAnonymous, - uiConfId: options.session.uiConfId, - partnerId: options.session.partnerId - }); - } - if (options.sources) { - Utils.Object.mergeDeep(entryDataModel, { - entryId: options.sources.id, - entryName: options.sources.metadata && options.sources.metadata.name, - entryType: options.sources.type - }); - } - if (options.playlist) { - Utils.Object.mergeDeep(entryDataModel, { - playlistId: options.playlist.id - }); - } - Object.keys(entryDataModel).forEach(key => { - if (entryDataModel[key] === undefined) { - delete entryDataModel[key]; - } - }); - Utils.Object.mergeDeep(dataModel, entryDataModel); - return dataModel; -}; +// const getModel = (options: KPOptionsObject): Object => { +// const dataModel: Object = { +// pVersion: __VERSION__, +// pName: __NAME__ +// }; +// if (options.targetId) { +// dataModel.domRootElementId = options.targetId; +// } +// if (options.provider && options.provider.env) { +// dataModel['serviceUrl'] = options.provider.env.serviceUrl; +// } +// const entryDataModel = { +// referrer: getReferrer() +// }; +// if (options.provider) { +// Utils.Object.mergeDeep(entryDataModel, { +// ks: options.provider.ks, +// uiConfId: options.provider.uiConfId, +// partnerId: options.provider.partnerId +// }); +// } +// if (options.session) { +// Utils.Object.mergeDeep(entryDataModel, { +// sessionId: options.session.id, +// ks: options.session.ks, +// isAnonymous: options.session.isAnonymous, +// uiConfId: options.session.uiConfId, +// partnerId: options.session.partnerId +// }); +// } +// if (options.sources) { +// Utils.Object.mergeDeep(entryDataModel, { +// entryId: options.sources.id, +// entryName: options.sources.metadata && options.sources.metadata.name, +// entryType: options.sources.type +// }); +// } +// if (options.playlist) { +// Utils.Object.mergeDeep(entryDataModel, { +// playlistId: options.playlist.id +// }); +// } +// Object.keys(entryDataModel).forEach(key => { +// if (entryDataModel[key] === undefined) { +// delete entryDataModel[key]; +// } +// }); +// Utils.Object.mergeDeep(dataModel, entryDataModel); +// return dataModel; +// }; /** * @param {KPOptionsObject} options - player options @@ -95,27 +95,27 @@ const getModel = (options: KPOptionsObject): Object => { function evaluatePluginsConfig(options: KPOptionsObject): void { if (options.plugins) { pluginConfig.set(options.plugins); - const dataModel = getModel(options); - const evaluatedConfig = evaluate(JSON.stringify(pluginConfig.get()), dataModel); - let evaluatedConfigObj; - try { - evaluatedConfigObj = JSON.parse(evaluatedConfig, function(key) { - try { - return JSON.parse(this[key]); - } catch (e) { - return this[key]; - } - }); - } catch (e) { - evaluatedConfigObj = {}; - } - evaluatedConfigObj = removeUnevaluatedExpression(evaluatedConfigObj); + // const dataModel = getModel(options); + const evaluatedConfig = replace(pluginConfig.get(), {tokens: options}); + // let evaluatedConfigObj; + // try { + // evaluatedConfigObj = JSON.parse(evaluatedConfig, function(key) { + // try { + // return JSON.parse(this[key]); + // } catch (e) { + // return this[key]; + // } + // }); + // } catch (e) { + // evaluatedConfigObj = {}; + // } + // evaluatedConfigObj = removeUnevaluatedExpression(evaluatedConfigObj); options.plugins = removeUnevaluatedExpression(options.plugins); if (options.plugins) { Object.keys(options.plugins).forEach(pluginName => { - if (options.plugins && options.plugins[pluginName]) { - const mergedConfig = Utils.Object.mergeDeep({}, evaluatedConfigObj[pluginName], options.plugins[pluginName]); + if (options.plugins && options.plugins[pluginName] && evaluatedConfig[pluginName]) { + const mergedConfig = Utils.Object.mergeDeep({}, evaluatedConfig[pluginName], options.plugins[pluginName]); if (options.plugins) { options.plugins[pluginName] = mergedConfig; } diff --git a/src/common/utils/evaluate.js b/src/common/utils/evaluate.js index 38bd5c962..fc3dc1ef5 100644 --- a/src/common/utils/evaluate.js +++ b/src/common/utils/evaluate.js @@ -1,5 +1,90 @@ //@flow +/** + * get token value by key + * @param {Object} tokens - tokens dictionary + * @param {string} tokenName - token name + * @param {string} delimiter - token path delimiter + * @returns {string|Object|null} - the resolved token value + */ +function getTokenValue(tokens: Object, tokenName: string, delimiter: string): string | Object | null { + var tmpTokens = tokens; + + if (tokens.hasOwnProperty(tokenName)) { + return tokens[tokenName]; + } + + var tokenNameParts = tokenName.split(delimiter); + + for (var i = 0; i < tokenNameParts.length; i++) { + if (tmpTokens.hasOwnProperty(tokenNameParts[i])) { + tmpTokens = tmpTokens[tokenNameParts[i]]; + } else { + return null; + } + } + + return tmpTokens; +} + +/** + * xxx + * @param {Object | string} target - the target + * @param {Object} options - the tokens + * @returns {Object | string} - the evaluated target + */ +function replace(target: Object | string, options: Object = {tokens: {}, delimiter: '.'}): Object | string { + var regexPattern = `{{(.+?)}}`; + var includeRegExp = new RegExp(regexPattern, 'g'); + var isObject = false; + var text; + + if (typeof target === 'object') { + text = JSON.stringify(target); + isObject = true; + } else if (typeof target === 'string') { + text = target; + } else { + text = target.toString(); + } + + var retVal = text; + var regExpResult; + + while ((regExpResult = includeRegExp.exec(text))) { + var fullMatch = regExpResult[0]; + var tokenName = regExpResult[1]; + var tokenValue = getTokenValue(options.tokens, tokenName, options.delimiter || '.'); + + if (tokenValue === null) { + tokenValue = ''; + } + + if (tokenValue !== null) { + if (typeof tokenValue === 'object') { + tokenValue = JSON.stringify(tokenValue); + tokenValue = tokenValue.replace(/"/g, '\\"'); + } else if (typeof tokenValue === 'string') { + if (tokenValue.indexOf('"') > -1) { + tokenValue = tokenValue.replace(/"/g, '\\"'); + } + } + + retVal = retVal.replace(fullMatch, tokenValue); + } + } + + return isObject + ? JSON.parse(retVal, function(key) { + try { + return JSON.parse(this[key]); + } catch (e) { + return this[key]; + } + }) + : retVal; +} + /** * @private * @param {string} template - The template string to evaluate @@ -25,3 +110,4 @@ function evaluate(template: string, model: Object = {}): string { } export default evaluate; +export {replace}; diff --git a/src/kaltura-player.js b/src/kaltura-player.js index 52c88830e..afeedad52 100644 --- a/src/kaltura-player.js +++ b/src/kaltura-player.js @@ -74,9 +74,10 @@ class KalturaPlayer extends FakeEventTarget { const playerConfig = Utils.Object.copyDeep(mediaConfig); Utils.Object.mergeDeep(playerConfig.sources, this._localPlayer.config.sources); Utils.Object.mergeDeep(playerConfig.session, this._localPlayer.config.session); - Object.keys(this._localPlayer.config.plugins).forEach(name => { - playerConfig.plugins[name] = {}; - }); + Utils.Object.mergeDeep(playerConfig.plugins, this._localPlayer.config.plugins); + // Object.keys(this._localPlayer.config.plugins).forEach(name => { + // playerConfig.plugins[name] = {}; + // }); addKalturaPoster(playerConfig.sources, mediaConfig.sources, this._localPlayer.dimensions); addKalturaParams(this, playerConfig); maybeSetStreamPriority(this, playerConfig);