Skip to content

Commit

Permalink
Use Handlerbars RunTime options over Eval handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-beehyv committed Mar 8, 2022
1 parent 4ad320a commit b132d4e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class ConfigurationService {
return certificateTemplate;
}
async addHelpers(key){
let helper = await loadConfigurationValues(key, async() => await configuration.AddHelpers(key));
let helper = await loadConfigurationValues(key, async() => await configuration.addHelpers(key));
updateConfigValues(key,helper);
return helper;
}
Expand Down
7 changes: 4 additions & 3 deletions backend/certificate_api/src/services/pdf_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const configurationService = new ConfigurationService();

async function createPDF(htmlData, data) {
const template = Handlebars.compile(htmlData);
let helpers = Handlebars.compile(await configurationService.addHelpers(HELPERS.CERTIFICATE_HELPER_FUNCTIONS))().toString();
eval(helpers);

let certificate = template(data);
const handlers = Handlebars.compile(await configurationService.addHelpers(HELPERS.CERTIFICATE_HELPER_FUNCTIONS))();
const handlerObj = new Function('return '+handlers)();

This comment has been minimized.

Copy link
@himeshr-egov

himeshr-egov Mar 8, 2022

Collaborator

Joy and i had a discussion, where we thought, it made sense to move out the compilation, if we were willing to sacrifice flexibility of auto-update of handler functions. So please discuss this with platform team once.

This comment has been minimized.

Copy link
@himeshr-egov

himeshr-egov Mar 8, 2022

Collaborator

Discard the line "const handlerObj", if we could make "const handlers" end-up with function directly

let certificate = template(data, {helpers:handlerObj});

const browser = await puppeteer.launch({
headless: true,
//comment to use default
Expand Down
6 changes: 4 additions & 2 deletions default-configuration/etcd/certificateHelperFunctions.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Handlebars.registerHelper("loud", function (aString) { return aString.toString().toUpperCase() });
Handlebars.registerHelper("small", function (aString) { return aString.toString().toLowerCase() });
helper={

This comment has been minimized.

Copy link
@himeshr-egov

himeshr-egov Mar 8, 2022

Collaborator

can't we have the "return" keyword moved here and make it always return a function

loud: function (aString) { return aString.toString().toUpperCase() },
small: function (aString) { return aString.toString().toLowerCase() }
}

0 comments on commit b132d4e

Please sign in to comment.