@pdfme/common 5.5.10-dev.13 → 5.5.10-dev.135

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.
Files changed (103) hide show
  1. package/package.json +30 -55
  2. package/set-version.js +9 -4
  3. package/src/expression.ts +14 -5
  4. package/src/helper.ts +9 -3
  5. package/src/types.ts +20 -15
  6. package/tsconfig.build.json +14 -0
  7. package/tsconfig.json +14 -4
  8. package/vite.config.mts +33 -0
  9. package/dist/cjs/__tests__/dynamicTemplate.test.js +0 -337
  10. package/dist/cjs/__tests__/dynamicTemplate.test.js.map +0 -1
  11. package/dist/cjs/__tests__/expression.test.js +0 -474
  12. package/dist/cjs/__tests__/expression.test.js.map +0 -1
  13. package/dist/cjs/__tests__/helper.test.js +0 -647
  14. package/dist/cjs/__tests__/helper.test.js.map +0 -1
  15. package/dist/cjs/__tests__/pluginRegistry.test.js +0 -83
  16. package/dist/cjs/__tests__/pluginRegistry.test.js.map +0 -1
  17. package/dist/cjs/src/constants.js +0 -27
  18. package/dist/cjs/src/constants.js.map +0 -1
  19. package/dist/cjs/src/dynamicTemplate.js +0 -230
  20. package/dist/cjs/src/dynamicTemplate.js.map +0 -1
  21. package/dist/cjs/src/expression.js +0 -463
  22. package/dist/cjs/src/expression.js.map +0 -1
  23. package/dist/cjs/src/helper.js +0 -277
  24. package/dist/cjs/src/helper.js.map +0 -1
  25. package/dist/cjs/src/index.js +0 -43
  26. package/dist/cjs/src/index.js.map +0 -1
  27. package/dist/cjs/src/pluginRegistry.js +0 -33
  28. package/dist/cjs/src/pluginRegistry.js.map +0 -1
  29. package/dist/cjs/src/schema.js +0 -190
  30. package/dist/cjs/src/schema.js.map +0 -1
  31. package/dist/cjs/src/types.js +0 -3
  32. package/dist/cjs/src/types.js.map +0 -1
  33. package/dist/cjs/src/version.js +0 -5
  34. package/dist/cjs/src/version.js.map +0 -1
  35. package/dist/esm/__tests__/dynamicTemplate.test.js +0 -302
  36. package/dist/esm/__tests__/dynamicTemplate.test.js.map +0 -1
  37. package/dist/esm/__tests__/expression.test.js +0 -472
  38. package/dist/esm/__tests__/expression.test.js.map +0 -1
  39. package/dist/esm/__tests__/helper.test.js +0 -612
  40. package/dist/esm/__tests__/helper.test.js.map +0 -1
  41. package/dist/esm/__tests__/pluginRegistry.test.js +0 -81
  42. package/dist/esm/__tests__/pluginRegistry.test.js.map +0 -1
  43. package/dist/esm/src/constants.js +0 -24
  44. package/dist/esm/src/constants.js.map +0 -1
  45. package/dist/esm/src/dynamicTemplate.js +0 -226
  46. package/dist/esm/src/dynamicTemplate.js.map +0 -1
  47. package/dist/esm/src/expression.js +0 -426
  48. package/dist/esm/src/expression.js.map +0 -1
  49. package/dist/esm/src/helper.js +0 -252
  50. package/dist/esm/src/helper.js.map +0 -1
  51. package/dist/esm/src/index.js +0 -8
  52. package/dist/esm/src/index.js.map +0 -1
  53. package/dist/esm/src/pluginRegistry.js +0 -29
  54. package/dist/esm/src/pluginRegistry.js.map +0 -1
  55. package/dist/esm/src/schema.js +0 -187
  56. package/dist/esm/src/schema.js.map +0 -1
  57. package/dist/esm/src/types.js +0 -2
  58. package/dist/esm/src/types.js.map +0 -1
  59. package/dist/esm/src/version.js +0 -2
  60. package/dist/esm/src/version.js.map +0 -1
  61. package/dist/node/__tests__/dynamicTemplate.test.js +0 -337
  62. package/dist/node/__tests__/dynamicTemplate.test.js.map +0 -1
  63. package/dist/node/__tests__/expression.test.js +0 -474
  64. package/dist/node/__tests__/expression.test.js.map +0 -1
  65. package/dist/node/__tests__/helper.test.js +0 -647
  66. package/dist/node/__tests__/helper.test.js.map +0 -1
  67. package/dist/node/__tests__/pluginRegistry.test.js +0 -83
  68. package/dist/node/__tests__/pluginRegistry.test.js.map +0 -1
  69. package/dist/node/src/constants.js +0 -27
  70. package/dist/node/src/constants.js.map +0 -1
  71. package/dist/node/src/dynamicTemplate.js +0 -230
  72. package/dist/node/src/dynamicTemplate.js.map +0 -1
  73. package/dist/node/src/expression.js +0 -463
  74. package/dist/node/src/expression.js.map +0 -1
  75. package/dist/node/src/helper.js +0 -277
  76. package/dist/node/src/helper.js.map +0 -1
  77. package/dist/node/src/index.js +0 -43
  78. package/dist/node/src/index.js.map +0 -1
  79. package/dist/node/src/pluginRegistry.js +0 -33
  80. package/dist/node/src/pluginRegistry.js.map +0 -1
  81. package/dist/node/src/schema.js +0 -190
  82. package/dist/node/src/schema.js.map +0 -1
  83. package/dist/node/src/types.js +0 -3
  84. package/dist/node/src/types.js.map +0 -1
  85. package/dist/node/src/version.js +0 -5
  86. package/dist/node/src/version.js.map +0 -1
  87. package/dist/types/__tests__/dynamicTemplate.test.d.ts +0 -1
  88. package/dist/types/__tests__/expression.test.d.ts +0 -1
  89. package/dist/types/__tests__/helper.test.d.ts +0 -1
  90. package/dist/types/__tests__/pluginRegistry.test.d.ts +0 -1
  91. package/dist/types/src/constants.d.ts +0 -20
  92. package/dist/types/src/dynamicTemplate.d.ts +0 -27
  93. package/dist/types/src/expression.d.ts +0 -6
  94. package/dist/types/src/helper.d.ts +0 -36
  95. package/dist/types/src/index.d.ts +0 -9
  96. package/dist/types/src/pluginRegistry.d.ts +0 -5
  97. package/dist/types/src/schema.d.ts +0 -820
  98. package/dist/types/src/types.d.ts +0 -181
  99. package/dist/types/src/version.d.ts +0 -1
  100. package/eslint.config.mjs +0 -22
  101. package/tsconfig.cjs.json +0 -10
  102. package/tsconfig.esm.json +0 -11
  103. package/tsconfig.node.json +0 -11
@@ -1,226 +0,0 @@
1
- import { cloneDeep, isBlankPdf } from './helper.js';
2
- /** Floating point tolerance for comparisons */
3
- const EPSILON = 0.01;
4
- /** Calculate the content height of a page (drawable area excluding padding) */
5
- const getContentHeight = (basePdf) => basePdf.height - basePdf.padding[0] - basePdf.padding[2];
6
- /** Get the input value for a schema */
7
- const getSchemaValue = (schema, input) => (schema.readOnly ? schema.content : input?.[schema.name]) || '';
8
- /**
9
- * Normalize schemas within a single page into layout items.
10
- * Returns items sorted by Y coordinate with their order preserved.
11
- */
12
- function normalizePageSchemas(pageSchemas, paddingTop) {
13
- const items = [];
14
- const orderMap = new Map();
15
- pageSchemas.forEach((schema, index) => {
16
- // Guard against negative Y position when schema.y < paddingTop
17
- // Prevents "Cannot read properties of undefined (reading 'push')" error
18
- const localY = Math.max(0, schema.position.y - paddingTop);
19
- items.push({
20
- schema: cloneDeep(schema),
21
- baseY: localY,
22
- height: schema.height,
23
- dynamicHeights: [schema.height], // Will be updated later
24
- });
25
- orderMap.set(schema.name, index);
26
- });
27
- // Sort by Y coordinate (preserve original order for same position)
28
- items.sort((a, b) => {
29
- if (Math.abs(a.baseY - b.baseY) > EPSILON) {
30
- return a.baseY - b.baseY;
31
- }
32
- return (orderMap.get(a.schema.name) ?? 0) - (orderMap.get(b.schema.name) ?? 0);
33
- });
34
- return { items, orderMap };
35
- }
36
- /**
37
- * Place rows on pages, splitting across pages as needed.
38
- * @returns The final global Y coordinate after placement
39
- */
40
- function placeRowsOnPages(schema, dynamicHeights, startGlobalY, contentHeight, paddingTop, pages) {
41
- let currentRowIndex = 0;
42
- let currentPageIndex = Math.floor(startGlobalY / contentHeight);
43
- let currentYInPage = startGlobalY % contentHeight;
44
- if (currentYInPage < 0)
45
- currentYInPage = 0;
46
- let actualGlobalEndY = 0;
47
- const isSplittable = dynamicHeights.length > 1;
48
- while (currentRowIndex < dynamicHeights.length) {
49
- // Ensure page exists
50
- while (pages.length <= currentPageIndex)
51
- pages.push([]);
52
- const spaceLeft = contentHeight - currentYInPage;
53
- const rowHeight = dynamicHeights[currentRowIndex];
54
- // If row doesn't fit, move to next page
55
- if (rowHeight > spaceLeft + EPSILON) {
56
- const isAtPageStart = Math.abs(spaceLeft - contentHeight) <= EPSILON;
57
- if (!isAtPageStart) {
58
- currentPageIndex++;
59
- currentYInPage = 0;
60
- continue;
61
- }
62
- // Force placement for oversized rows that don't fit even on a fresh page
63
- }
64
- // Pack as many rows as possible on this page
65
- let chunkHeight = 0;
66
- const startRowIndex = currentRowIndex;
67
- while (currentRowIndex < dynamicHeights.length) {
68
- const h = dynamicHeights[currentRowIndex];
69
- if (currentYInPage + chunkHeight + h <= contentHeight + EPSILON) {
70
- chunkHeight += h;
71
- currentRowIndex++;
72
- }
73
- else {
74
- break;
75
- }
76
- }
77
- // Don't leave header alone on a page without any data rows
78
- // If only header fits and there are data rows remaining, move everything to next page
79
- // BUT: if already at page top, don't move (prevents infinite loop when data row is too large)
80
- const isAtPageTop = currentYInPage <= EPSILON;
81
- if (isSplittable &&
82
- startRowIndex === 0 &&
83
- currentRowIndex === 1 &&
84
- dynamicHeights.length > 1 &&
85
- !isAtPageTop) {
86
- currentRowIndex = 0;
87
- currentPageIndex++;
88
- currentYInPage = 0;
89
- continue;
90
- }
91
- // Force at least one row to prevent infinite loop
92
- if (currentRowIndex === startRowIndex) {
93
- chunkHeight += dynamicHeights[currentRowIndex];
94
- currentRowIndex++;
95
- }
96
- // Create schema for this chunk
97
- const newSchema = {
98
- ...schema,
99
- height: chunkHeight,
100
- position: { ...schema.position, y: currentYInPage + paddingTop },
101
- };
102
- // Set bodyRange for splittable elements
103
- // dynamicHeights[0] = header row, dynamicHeights[1] = body[0]
104
- // So subtract 1 to convert to body index
105
- if (isSplittable) {
106
- newSchema.__bodyRange = {
107
- start: startRowIndex === 0 ? 0 : startRowIndex - 1,
108
- end: currentRowIndex - 1,
109
- };
110
- newSchema.__isSplit = startRowIndex > 0;
111
- }
112
- pages[currentPageIndex].push(newSchema);
113
- // Update position
114
- currentYInPage += chunkHeight;
115
- if (currentYInPage >= contentHeight - EPSILON) {
116
- currentPageIndex++;
117
- currentYInPage = 0;
118
- }
119
- actualGlobalEndY = currentPageIndex * contentHeight + currentYInPage;
120
- }
121
- return actualGlobalEndY;
122
- }
123
- /** Sort elements within each page by their original order */
124
- function sortPagesByOrder(pages, orderMap) {
125
- pages.forEach((page) => {
126
- page.sort((a, b) => (orderMap.get(a.name) ?? 0) - (orderMap.get(b.name) ?? 0));
127
- });
128
- }
129
- /** Remove trailing empty pages */
130
- function removeTrailingEmptyPages(pages) {
131
- while (pages.length > 1 && pages[pages.length - 1].length === 0) {
132
- pages.pop();
133
- }
134
- }
135
- /**
136
- * Process a single template page that has dynamic content.
137
- * Uses the same layout algorithm as the original implementation,
138
- * but scoped to a single page's schemas.
139
- */
140
- function processDynamicPage(items, orderMap, contentHeight, paddingTop) {
141
- const pages = [];
142
- let totalYOffset = 0;
143
- for (const item of items) {
144
- const currentGlobalStartY = item.baseY + totalYOffset;
145
- const actualGlobalEndY = placeRowsOnPages(item.schema, item.dynamicHeights, currentGlobalStartY, contentHeight, paddingTop, pages);
146
- // Update offset: difference between actual and original end position
147
- const originalGlobalEndY = item.baseY + item.height;
148
- totalYOffset = actualGlobalEndY - originalGlobalEndY;
149
- }
150
- sortPagesByOrder(pages, orderMap);
151
- removeTrailingEmptyPages(pages);
152
- return pages;
153
- }
154
- /**
155
- * Process a template containing tables with dynamic heights
156
- * and generate a new template with proper page breaks.
157
- *
158
- * Processing is done page-by-page:
159
- * - Pages with height changes are processed with full layout calculations
160
- * - Pages without height changes are copied as-is (no offset propagation between pages)
161
- *
162
- * This reduces computation cost by:
163
- * 1. Limiting layout calculations to pages that need them
164
- * 2. Avoiding cross-page offset propagation for static pages
165
- */
166
- export const getDynamicTemplate = async (arg) => {
167
- const { template, input, options, _cache, getDynamicHeights } = arg;
168
- const basePdf = template.basePdf;
169
- if (!isBlankPdf(basePdf)) {
170
- return template;
171
- }
172
- const contentHeight = getContentHeight(basePdf);
173
- const paddingTop = basePdf.padding[0];
174
- const resultPages = [];
175
- const PARALLEL_LIMIT = 10;
176
- // Process each template page independently
177
- for (let pageIndex = 0; pageIndex < template.schemas.length; pageIndex++) {
178
- const pageSchemas = template.schemas[pageIndex];
179
- // Normalize this page's schemas
180
- const { items, orderMap } = normalizePageSchemas(pageSchemas, paddingTop);
181
- // Calculate dynamic heights for this page's schemas with concurrency limit
182
- for (let i = 0; i < items.length; i += PARALLEL_LIMIT) {
183
- const chunk = items.slice(i, i + PARALLEL_LIMIT);
184
- const chunkResults = await Promise.all(chunk.map((item) => {
185
- const value = getSchemaValue(item.schema, input);
186
- return getDynamicHeights(value, {
187
- schema: item.schema,
188
- basePdf,
189
- options,
190
- _cache,
191
- }).then((heights) => (heights.length === 0 ? [0] : heights));
192
- }));
193
- // Update items with calculated heights
194
- for (let j = 0; j < chunkResults.length; j++) {
195
- items[i + j].dynamicHeights = chunkResults[j];
196
- }
197
- }
198
- // Process all pages independently (no cross-page offset propagation)
199
- const processedPages = processDynamicPage(items, orderMap, contentHeight, paddingTop);
200
- resultPages.push(...processedPages);
201
- }
202
- removeTrailingEmptyPages(resultPages);
203
- // Check if anything changed - return original template if not
204
- if (resultPages.length === template.schemas.length) {
205
- let unchanged = true;
206
- for (let i = 0; i < resultPages.length && unchanged; i++) {
207
- if (resultPages[i].length !== template.schemas[i].length) {
208
- unchanged = false;
209
- break;
210
- }
211
- for (let j = 0; j < resultPages[i].length && unchanged; j++) {
212
- const orig = template.schemas[i][j];
213
- const result = resultPages[i][j];
214
- if (Math.abs(orig.height - result.height) > EPSILON ||
215
- Math.abs(orig.position.y - result.position.y) > EPSILON) {
216
- unchanged = false;
217
- }
218
- }
219
- }
220
- if (unchanged) {
221
- return template;
222
- }
223
- }
224
- return { basePdf, schemas: resultPages };
225
- };
226
- //# sourceMappingURL=dynamicTemplate.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dynamicTemplate.js","sourceRoot":"","sources":["../../../src/dynamicTemplate.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEpD,+CAA+C;AAC/C,MAAM,OAAO,GAAG,IAAI,CAAC;AAyBrB,+EAA+E;AAC/E,MAAM,gBAAgB,GAAG,CAAC,OAAiB,EAAU,EAAE,CACrD,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,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AC,CAAC"}
@@ -1,426 +0,0 @@
1
- import * as acorn from 'acorn';
2
- const expressionCache = new Map();
3
- const parseDataCache = new Map();
4
- const parseData = (data) => {
5
- const key = JSON.stringify(data);
6
- if (parseDataCache.has(key)) {
7
- return parseDataCache.get(key);
8
- }
9
- const parsed = Object.fromEntries(Object.entries(data).map(([key, value]) => {
10
- if (typeof value === 'string') {
11
- try {
12
- const parsedValue = JSON.parse(value);
13
- return [key, parsedValue];
14
- }
15
- catch {
16
- return [key, value];
17
- }
18
- }
19
- return [key, value];
20
- }));
21
- parseDataCache.set(key, parsed);
22
- return parsed;
23
- };
24
- const padZero = (num) => String(num).padStart(2, '0');
25
- const formatDate = (date) => `${date.getFullYear()}/${padZero(date.getMonth() + 1)}/${padZero(date.getDate())}`;
26
- const formatDateTime = (date) => `${formatDate(date)} ${padZero(date.getHours())}:${padZero(date.getMinutes())}`;
27
- // Safe assign function that prevents prototype pollution
28
- const safeAssign = (target, ...sources) => {
29
- if (target == null) {
30
- throw new TypeError('Cannot convert undefined or null to object');
31
- }
32
- const to = { ...target };
33
- for (const source of sources) {
34
- if (source != null) {
35
- for (const key in source) {
36
- // Skip prototype pollution keys
37
- if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
38
- continue;
39
- }
40
- // Only copy own properties
41
- if (Object.prototype.hasOwnProperty.call(source, key)) {
42
- to[key] = source[key];
43
- }
44
- }
45
- }
46
- }
47
- return to;
48
- };
49
- // Create a safe copy of Object with dangerous methods excluded
50
- const safeObject = {
51
- keys: Object.keys,
52
- values: Object.values,
53
- entries: Object.entries,
54
- fromEntries: Object.fromEntries,
55
- is: Object.is,
56
- hasOwnProperty: Object.hasOwnProperty,
57
- assign: safeAssign, // Safe version of Object.assign
58
- // The following methods are excluded due to security concerns:
59
- // - Side effects: create, freeze, seal (can still be used for attacks)
60
- // - Prototype access: getOwnPropertyDescriptor, getPrototypeOf, setPrototypeOf,
61
- // defineProperty, defineProperties, getOwnPropertyNames, getOwnPropertySymbols
62
- };
63
- const allowedGlobals = {
64
- Math,
65
- String,
66
- Number,
67
- Boolean,
68
- Array,
69
- Object: safeObject,
70
- Date,
71
- JSON,
72
- isNaN,
73
- parseFloat,
74
- parseInt,
75
- decodeURI,
76
- decodeURIComponent,
77
- encodeURI,
78
- encodeURIComponent,
79
- };
80
- const validateAST = (node) => {
81
- switch (node.type) {
82
- case 'Literal':
83
- case 'Identifier':
84
- break;
85
- case 'BinaryExpression':
86
- case 'LogicalExpression': {
87
- const binaryNode = node;
88
- validateAST(binaryNode.left);
89
- validateAST(binaryNode.right);
90
- break;
91
- }
92
- case 'UnaryExpression': {
93
- const unaryNode = node;
94
- validateAST(unaryNode.argument);
95
- break;
96
- }
97
- case 'ConditionalExpression': {
98
- const condNode = node;
99
- validateAST(condNode.test);
100
- validateAST(condNode.consequent);
101
- validateAST(condNode.alternate);
102
- break;
103
- }
104
- case 'MemberExpression': {
105
- const memberNode = node;
106
- validateAST(memberNode.object);
107
- if (memberNode.computed) {
108
- validateAST(memberNode.property);
109
- }
110
- else {
111
- const propName = memberNode.property.name;
112
- if (['constructor', '__proto__', 'prototype'].includes(propName)) {
113
- throw new Error('Access to prohibited property');
114
- }
115
- // Block prototype pollution methods
116
- if (['__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__'].includes(propName)) {
117
- throw new Error(`Access to prohibited method: ${propName}`);
118
- }
119
- const prohibitedMethods = ['toLocaleString', 'valueOf'];
120
- if (typeof propName === 'string' && prohibitedMethods.includes(propName)) {
121
- throw new Error(`Access to prohibited method: ${propName}`);
122
- }
123
- }
124
- break;
125
- }
126
- case 'CallExpression': {
127
- const callNode = node;
128
- validateAST(callNode.callee);
129
- callNode.arguments.forEach(validateAST);
130
- break;
131
- }
132
- case 'ArrayExpression': {
133
- const arrayNode = node;
134
- arrayNode.elements.forEach((elem) => {
135
- if (elem)
136
- validateAST(elem);
137
- });
138
- break;
139
- }
140
- case 'ObjectExpression': {
141
- const objectNode = node;
142
- objectNode.properties.forEach((prop) => {
143
- const propNode = prop;
144
- validateAST(propNode.key);
145
- validateAST(propNode.value);
146
- });
147
- break;
148
- }
149
- case 'ArrowFunctionExpression': {
150
- const arrowFuncNode = node;
151
- arrowFuncNode.params.forEach((param) => {
152
- if (param.type !== 'Identifier') {
153
- throw new Error('Only identifier parameters are supported in arrow functions');
154
- }
155
- validateAST(param);
156
- });
157
- validateAST(arrowFuncNode.body);
158
- break;
159
- }
160
- default:
161
- throw new Error(`Unsupported syntax in placeholder: ${node.type}`);
162
- }
163
- };
164
- const evaluateAST = (node, context) => {
165
- switch (node.type) {
166
- case 'Literal': {
167
- const literalNode = node;
168
- return literalNode.value;
169
- }
170
- case 'Identifier': {
171
- const idNode = node;
172
- if (Object.prototype.hasOwnProperty.call(context, idNode.name)) {
173
- return context[idNode.name];
174
- }
175
- else if (Object.prototype.hasOwnProperty.call(allowedGlobals, idNode.name)) {
176
- return allowedGlobals[idNode.name];
177
- }
178
- else {
179
- throw new Error(`Undefined variable: ${idNode.name}`);
180
- }
181
- }
182
- case 'BinaryExpression': {
183
- const binaryNode = node;
184
- const left = evaluateAST(binaryNode.left, context);
185
- const right = evaluateAST(binaryNode.right, context);
186
- switch (binaryNode.operator) {
187
- case '+':
188
- return left + right;
189
- case '-':
190
- return left - right;
191
- case '*':
192
- return left * right;
193
- case '/':
194
- return left / right;
195
- case '%':
196
- return left % right;
197
- case '**':
198
- return left ** right;
199
- case '==':
200
- return left == right;
201
- case '!=':
202
- return left != right;
203
- case '===':
204
- return left === right;
205
- case '!==':
206
- return left !== right;
207
- case '<':
208
- return left < right;
209
- case '>':
210
- return left > right;
211
- case '<=':
212
- return left <= right;
213
- case '>=':
214
- return left >= right;
215
- default:
216
- throw new Error(`Unsupported operator: ${binaryNode.operator}`);
217
- }
218
- }
219
- case 'LogicalExpression': {
220
- const logicalNode = node;
221
- const leftLogical = evaluateAST(logicalNode.left, context);
222
- const rightLogical = evaluateAST(logicalNode.right, context);
223
- switch (logicalNode.operator) {
224
- case '&&':
225
- return leftLogical && rightLogical;
226
- case '||':
227
- return leftLogical || rightLogical;
228
- default:
229
- throw new Error(`Unsupported operator: ${logicalNode.operator}`);
230
- }
231
- }
232
- case 'UnaryExpression': {
233
- const unaryNode = node;
234
- const arg = evaluateAST(unaryNode.argument, context);
235
- switch (unaryNode.operator) {
236
- case '+':
237
- return +arg;
238
- case '-':
239
- return -arg;
240
- case '!':
241
- return !arg;
242
- default:
243
- throw new Error(`Unsupported operator: ${unaryNode.operator}`);
244
- }
245
- }
246
- case 'ConditionalExpression': {
247
- const condNode = node;
248
- const test = evaluateAST(condNode.test, context);
249
- return test
250
- ? evaluateAST(condNode.consequent, context)
251
- : evaluateAST(condNode.alternate, context);
252
- }
253
- case 'MemberExpression': {
254
- const memberNode = node;
255
- const obj = evaluateAST(memberNode.object, context);
256
- let prop;
257
- if (memberNode.computed) {
258
- prop = evaluateAST(memberNode.property, context);
259
- }
260
- else {
261
- prop = memberNode.property.name;
262
- }
263
- if (typeof prop === 'string' || typeof prop === 'number') {
264
- if (typeof prop === 'string' && ['constructor', '__proto__', 'prototype'].includes(prop)) {
265
- throw new Error('Access to prohibited property');
266
- }
267
- // Block prototype pollution methods
268
- if (typeof prop === 'string' && ['__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__'].includes(prop)) {
269
- throw new Error(`Access to prohibited method: ${prop}`);
270
- }
271
- return obj[prop];
272
- }
273
- else {
274
- throw new Error('Invalid property access');
275
- }
276
- }
277
- case 'CallExpression': {
278
- const callNode = node;
279
- const callee = evaluateAST(callNode.callee, context);
280
- const args = callNode.arguments.map((argNode) => evaluateAST(argNode, context));
281
- if (typeof callee === 'function') {
282
- if (callNode.callee.type === 'MemberExpression') {
283
- const memberExpr = callNode.callee;
284
- const obj = evaluateAST(memberExpr.object, context);
285
- if (obj !== null &&
286
- (typeof obj === 'object' ||
287
- typeof obj === 'number' ||
288
- typeof obj === 'string' ||
289
- typeof obj === 'boolean')) {
290
- return callee.call(obj, ...args);
291
- }
292
- else {
293
- throw new Error('Invalid object in member function call');
294
- }
295
- }
296
- else {
297
- // Use a type assertion to tell TypeScript this is a safe function call
298
- return callee(...args);
299
- }
300
- }
301
- else {
302
- throw new Error('Attempted to call a non-function');
303
- }
304
- }
305
- case 'ArrowFunctionExpression': {
306
- const arrowFuncNode = node;
307
- const params = arrowFuncNode.params.map((param) => param.name);
308
- const body = arrowFuncNode.body;
309
- return (...args) => {
310
- const newContext = { ...context };
311
- params.forEach((param, index) => {
312
- newContext[param] = args[index];
313
- });
314
- return evaluateAST(body, newContext);
315
- };
316
- }
317
- case 'ArrayExpression': {
318
- const arrayNode = node;
319
- return arrayNode.elements.map((elem) => (elem ? evaluateAST(elem, context) : null));
320
- }
321
- case 'ObjectExpression': {
322
- const objectNode = node;
323
- const objResult = {};
324
- objectNode.properties.forEach((prop) => {
325
- const propNode = prop;
326
- let key;
327
- if (propNode.key.type === 'Identifier') {
328
- key = propNode.key.name;
329
- }
330
- else {
331
- const evaluatedKey = evaluateAST(propNode.key, context);
332
- if (typeof evaluatedKey !== 'string' && typeof evaluatedKey !== 'number') {
333
- throw new Error('Object property keys must be strings or numbers');
334
- }
335
- key = String(evaluatedKey);
336
- }
337
- const value = evaluateAST(propNode.value, context);
338
- objResult[key] = value;
339
- });
340
- return objResult;
341
- }
342
- default:
343
- throw new Error(`Unsupported syntax in placeholder: ${node.type}`);
344
- }
345
- };
346
- const evaluatePlaceholders = (arg) => {
347
- const { content, context } = arg;
348
- let resultContent = '';
349
- let index = 0;
350
- while (index < content.length) {
351
- const startIndex = content.indexOf('{', index);
352
- if (startIndex === -1) {
353
- resultContent += content.slice(index);
354
- break;
355
- }
356
- resultContent += content.slice(index, startIndex);
357
- let braceCount = 1;
358
- let endIndex = startIndex + 1;
359
- while (endIndex < content.length && braceCount > 0) {
360
- if (content[endIndex] === '{') {
361
- braceCount++;
362
- }
363
- else if (content[endIndex] === '}') {
364
- braceCount--;
365
- }
366
- endIndex++;
367
- }
368
- if (braceCount === 0) {
369
- const code = content.slice(startIndex + 1, endIndex - 1).trim();
370
- if (expressionCache.has(code)) {
371
- const evalFunc = expressionCache.get(code);
372
- try {
373
- const value = evalFunc(context);
374
- resultContent += String(value);
375
- }
376
- catch {
377
- resultContent += content.slice(startIndex, endIndex);
378
- }
379
- }
380
- else {
381
- try {
382
- const ast = acorn.parseExpressionAt(code, 0, { ecmaVersion: 'latest' });
383
- validateAST(ast);
384
- const evalFunc = (ctx) => evaluateAST(ast, ctx);
385
- expressionCache.set(code, evalFunc);
386
- const value = evalFunc(context);
387
- resultContent += String(value);
388
- }
389
- catch {
390
- resultContent += content.slice(startIndex, endIndex);
391
- }
392
- }
393
- index = endIndex;
394
- }
395
- else {
396
- throw new Error('Invalid placeholder');
397
- }
398
- }
399
- return resultContent;
400
- };
401
- export const replacePlaceholders = (arg) => {
402
- const { content, variables, schemas } = arg;
403
- if (!content || typeof content !== 'string' || !content.includes('{') || !content.includes('}')) {
404
- return content;
405
- }
406
- const date = new Date();
407
- const formattedDate = formatDate(date);
408
- const formattedDateTime = formatDateTime(date);
409
- const data = {
410
- ...Object.fromEntries(schemas.flat().map((schema) => [schema.name, schema.readOnly ? schema.content || '' : ''])),
411
- ...variables,
412
- };
413
- const parsedInput = parseData(data);
414
- const context = {
415
- date: formattedDate,
416
- dateTime: formattedDateTime,
417
- ...parsedInput,
418
- };
419
- Object.entries(context).forEach(([key, value]) => {
420
- if (typeof value === 'string' && value.includes('{') && value.includes('}')) {
421
- context[key] = evaluatePlaceholders({ content: value, context });
422
- }
423
- });
424
- return evaluatePlaceholders({ content, context });
425
- };
426
- //# sourceMappingURL=expression.js.map