Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new token evaluator #229

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/common/plugins/plugins-config-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}',
Expand All @@ -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}}'
}
};

Expand Down
138 changes: 69 additions & 69 deletions src/common/plugins/plugins-config.js
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
86 changes: 86 additions & 0 deletions src/common/utils/evaluate.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,3 +110,4 @@ function evaluate(template: string, model: Object = {}): string {
}

export default evaluate;
export {replace};
7 changes: 4 additions & 3 deletions src/kaltura-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down