@budibase/string-templates 2.21.9 → 2.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/iife.d.ts ADDED
@@ -0,0 +1 @@
1
+ export function iifeWrapper(script: any): string;
package/dist/index.d.ts CHANGED
@@ -16,4 +16,5 @@ export function convertToJS(hbs: any): string;
16
16
  import { FIND_ANY_HBS_REGEX } from "./utilities";
17
17
  import { helpersToRemoveForJs } from "./helpers/list";
18
18
  export function defaultJSSetup(): void;
19
- export { setJSRunner, setOnErrorLog, FIND_ANY_HBS_REGEX, JsErrorTimeout, helpersToRemoveForJs };
19
+ import { iifeWrapper } from "./iife";
20
+ export { setJSRunner, setOnErrorLog, FIND_ANY_HBS_REGEX, JsErrorTimeout, helpersToRemoveForJs, iifeWrapper };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@budibase/string-templates",
3
- "version": "2.21.9",
3
+ "version": "2.22.0",
4
4
  "description": "Handlebars wrapper for Budibase templating.",
5
5
  "main": "src/index.js",
6
6
  "module": "dist/bundle.mjs",
@@ -12,7 +12,8 @@
12
12
  "import": "./dist/bundle.mjs"
13
13
  },
14
14
  "./package.json": "./package.json",
15
- "./test/utils": "./test/utils.js"
15
+ "./test/utils": "./test/utils.js",
16
+ "./iife": "./src/iife.js"
16
17
  },
17
18
  "files": [
18
19
  "dist",
@@ -46,5 +47,5 @@
46
47
  "rollup-plugin-terser": "^7.0.2",
47
48
  "typescript": "5.2.2"
48
49
  },
49
- "gitHead": "901d6ae4116d0199b2a68e5be38c8dc5658b48e7"
50
+ "gitHead": "45f438318a63b0f60cbee8cef5115ca7947f356b"
50
51
  }
@@ -2,6 +2,7 @@ const { atob, isBackendService, isJSAllowed } = require("../utilities")
2
2
  const cloneDeep = require("lodash.clonedeep")
3
3
  const { LITERAL_MARKER } = require("../helpers/constants")
4
4
  const { getJsHelperList } = require("./list")
5
+ const { iifeWrapper } = require("../iife")
5
6
 
6
7
  // The method of executing JS scripts depends on the bundle being built.
7
8
  // This setter is used in the entrypoint (either index.js or index.mjs).
@@ -48,14 +49,36 @@ module.exports.processJS = (handlebars, context) => {
48
49
  try {
49
50
  // Wrap JS in a function and immediately invoke it.
50
51
  // This is required to allow the final `return` statement to be valid.
51
- const js = `(function(){${atob(handlebars)}})();`
52
+ const js = iifeWrapper(atob(handlebars))
53
+
54
+ // Transform snippets into an object for faster access, and cache previously
55
+ // evaluated snippets
56
+ let snippetMap = {}
57
+ let snippetCache = {}
58
+ for (let snippet of context.snippets || []) {
59
+ snippetMap[snippet.name] = snippet.code
60
+ }
52
61
 
53
62
  // Our $ context function gets a value from context.
54
63
  // We clone the context to avoid mutation in the binding affecting real
55
64
  // app context.
65
+ const clonedContext = cloneDeep({ ...context, snippets: null })
56
66
  const sandboxContext = {
57
- $: path => getContextValue(path, cloneDeep(context)),
67
+ $: path => getContextValue(path, clonedContext),
58
68
  helpers: getJsHelperList(),
69
+
70
+ // Proxy to evaluate snippets when running in the browser
71
+ snippets: new Proxy(
72
+ {},
73
+ {
74
+ get: function (_, name) {
75
+ if (!(name in snippetCache)) {
76
+ snippetCache[name] = eval(iifeWrapper(snippetMap[name]))
77
+ }
78
+ return snippetCache[name]
79
+ },
80
+ }
81
+ ),
59
82
  }
60
83
 
61
84
  // Create a sandbox with our context and run the JS
package/src/iife.js ADDED
@@ -0,0 +1,3 @@
1
+ module.exports.iifeWrapper = script => {
2
+ return `(function(){\n${script}\n})();`
3
+ }
package/src/index.js CHANGED
@@ -3,6 +3,7 @@ const handlebars = require("handlebars")
3
3
  const { registerAll, registerMinimum } = require("./helpers/index")
4
4
  const processors = require("./processors")
5
5
  const { atob, btoa, isBackendService } = require("./utilities")
6
+ const { iifeWrapper } = require("./iife")
6
7
  const manifest = require("../manifest.json")
7
8
  const {
8
9
  FIND_HBS_REGEX,
@@ -426,3 +427,4 @@ function defaultJSSetup() {
426
427
  defaultJSSetup()
427
428
 
428
429
  module.exports.defaultJSSetup = defaultJSSetup
430
+ module.exports.iifeWrapper = iifeWrapper