Use @kbn/handlebars and compileAST over handlebars.compile (#218449)

## Summary

Change usage of Handlebars.compile across Kibana to use
`@kbn/handlebars` and `compileAST`

### Note for reviewers:
There should be no change for the rendered output where it's used.
Wherever there were tests, i ensured they were passing after making the
change.

### Checklist

Check the PR satisfies following conditions. 
Reviewers should verify this PR satisfies this list as well.

- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Sid 2025-04-22 11:46:05 +02:00 committed by GitHub
parent 6d69aad802
commit 8c8c6d16ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 12 additions and 12 deletions

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import Handlebars from 'handlebars';
import Handlebars from '@kbn/handlebars';
import { load, dump } from 'js-yaml';
import type { Logger } from '@kbn/core/server';
@ -30,7 +30,7 @@ export function compileTemplate(variables: PackagePolicyConfigRecord, templateSt
let template = getHandlebarsCompiledTemplateCache(templateStr);
if (!template) {
template = handlebars.compile(templateStr, { noEscape: true });
template = handlebars.compileAST(templateStr, { noEscape: true });
setHandlebarsCompiledTemplateCache(templateStr, template);
}

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import Handlebars from 'handlebars';
import Handlebars from '@kbn/handlebars';
import {
getPackageAssetsMapCache,
@ -40,7 +40,7 @@ describe('EPM CacheSession', () => {
it('should not cache handlebars template', () => {
const template1 = `test1: {{test}}`;
setHandlebarsCompiledTemplateCache(template1, handlebars.compile(template1));
setHandlebarsCompiledTemplateCache(template1, handlebars.compileAST(template1));
const cache1 = getHandlebarsCompiledTemplateCache(template1);
expect(cache1).toBeUndefined();
});
@ -85,9 +85,9 @@ describe('EPM CacheSession', () => {
it('should cache handle template', async () => {
function setCache() {
const template1 = `test1: {{test}}`;
setHandlebarsCompiledTemplateCache(template1, handlebars.compile(template1));
setHandlebarsCompiledTemplateCache(template1, handlebars.compileAST(template1));
const template2 = `test2: {{test}}`;
setHandlebarsCompiledTemplateCache(template2, handlebars.compile(template2));
setHandlebarsCompiledTemplateCache(template2, handlebars.compileAST(template2));
}
function getCache() {
const template1 = `test1: {{test}}`;

View file

@ -9,6 +9,8 @@ import { AsyncLocalStorage } from 'async_hooks';
import { LRUCache } from 'lru-cache';
import type { TemplateDelegate } from '@kbn/handlebars';
import type { AssetsMap, PackagePolicyAssetsMap } from '../../../../common/types';
import type { PackageInfo } from '../../../../common';
@ -27,7 +29,7 @@ class CacheSession {
private _agentTemplateAssetsMap?: LRUCache<string, PackagePolicyAssetsMap>;
private _handlebarsCompiledTemplate?: LRUCache<string, HandlebarsTemplateDelegate>;
private _handlebarsCompiledTemplate?: LRUCache<string, TemplateDelegate>;
private _isSpaceAwarenessEnabledCache?: boolean;
@ -60,7 +62,7 @@ class CacheSession {
getHandlebarsCompiledTemplateCache() {
if (!this._handlebarsCompiledTemplate) {
this._handlebarsCompiledTemplate = new LRUCache<string, HandlebarsTemplateDelegate>({
this._handlebarsCompiledTemplate = new LRUCache<string, TemplateDelegate>({
max: HANDLEBARS_COMPILE_TEMPLATE_CACHE_SIZE,
});
}
@ -128,10 +130,7 @@ export function getHandlebarsCompiledTemplateCache(tplStr: string) {
return cacheStore.getStore()?.getHandlebarsCompiledTemplateCache()?.get(tplStr);
}
export function setHandlebarsCompiledTemplateCache(
tplStr: string,
tpl: HandlebarsTemplateDelegate
) {
export function setHandlebarsCompiledTemplateCache(tplStr: string, tpl: TemplateDelegate) {
return cacheStore.getStore()?.getHandlebarsCompiledTemplateCache()?.set(tplStr, tpl);
}

View file

@ -120,5 +120,6 @@
"@kbn/core-security-server",
"@kbn/core-http-server-utils",
"@kbn/core-notifications-browser-mocks",
"@kbn/handlebars",
]
}