@pdfme/common 5.1.6-dev.2 → 5.1.7-dev.2
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/cjs/__tests__/dynamicTemplate.test.js +197 -0
- package/dist/cjs/__tests__/dynamicTemplate.test.js.map +1 -0
- package/dist/cjs/__tests__/expression.test.js +271 -0
- package/dist/cjs/__tests__/expression.test.js.map +1 -0
- package/dist/cjs/__tests__/helper.test.js +92 -254
- package/dist/cjs/__tests__/helper.test.js.map +1 -1
- package/dist/cjs/src/constants.js +6 -0
- package/dist/cjs/src/constants.js.map +1 -1
- package/dist/cjs/src/dynamicTemplate.js +239 -0
- package/dist/cjs/src/dynamicTemplate.js.map +1 -0
- package/dist/cjs/src/expression.js +392 -0
- package/dist/cjs/src/expression.js.map +1 -0
- package/dist/cjs/src/helper.js +6 -238
- package/dist/cjs/src/helper.js.map +1 -1
- package/dist/cjs/src/index.js +5 -2
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/schema.js +1 -0
- package/dist/cjs/src/schema.js.map +1 -1
- package/dist/esm/__tests__/dynamicTemplate.test.js +172 -0
- package/dist/esm/__tests__/dynamicTemplate.test.js.map +1 -0
- package/dist/esm/__tests__/expression.test.js +269 -0
- package/dist/esm/__tests__/expression.test.js.map +1 -0
- package/dist/esm/__tests__/helper.test.js +94 -256
- package/dist/esm/__tests__/helper.test.js.map +1 -1
- package/dist/esm/src/constants.js +6 -0
- package/dist/esm/src/constants.js.map +1 -1
- package/dist/esm/src/dynamicTemplate.js +235 -0
- package/dist/esm/src/dynamicTemplate.js.map +1 -0
- package/dist/esm/src/expression.js +365 -0
- package/dist/esm/src/expression.js.map +1 -0
- package/dist/esm/src/helper.js +5 -236
- package/dist/esm/src/helper.js.map +1 -1
- package/dist/esm/src/index.js +4 -2
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/schema.js +1 -0
- package/dist/esm/src/schema.js.map +1 -1
- package/dist/types/__tests__/dynamicTemplate.test.d.ts +1 -0
- package/dist/types/__tests__/expression.test.d.ts +1 -0
- package/dist/types/src/dynamicTemplate.d.ts +15 -0
- package/dist/types/src/expression.d.ts +6 -0
- package/dist/types/src/helper.d.ts +34 -15
- package/dist/types/src/index.d.ts +4 -2
- package/dist/types/src/schema.d.ts +3631 -517
- package/package.json +5 -1
- package/src/constants.ts +8 -0
- package/src/dynamicTemplate.ts +277 -0
- package/src/expression.ts +392 -0
- package/src/helper.ts +10 -282
- package/src/index.ts +3 -1
- package/src/schema.ts +1 -0
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@pdfme/common",
|
3
|
-
"version": "5.1.
|
3
|
+
"version": "5.1.7-dev.2",
|
4
4
|
"sideEffects": false,
|
5
5
|
"author": "hand-dot",
|
6
6
|
"license": "MIT",
|
@@ -44,6 +44,7 @@
|
|
44
44
|
},
|
45
45
|
"dependencies": {
|
46
46
|
"@pdfme/pdf-lib": "^1.18.3",
|
47
|
+
"acorn": "^8.14.0",
|
47
48
|
"buffer": "^6.0.3",
|
48
49
|
"zod": "^3.20.2"
|
49
50
|
},
|
@@ -71,5 +72,8 @@
|
|
71
72
|
},
|
72
73
|
"publishConfig": {
|
73
74
|
"access": "public"
|
75
|
+
},
|
76
|
+
"devDependencies": {
|
77
|
+
"@types/estree": "^1.0.6"
|
74
78
|
}
|
75
79
|
}
|
package/src/constants.ts
CHANGED
@@ -4,6 +4,14 @@ export const PT_TO_PX_RATIO = 1.333;
|
|
4
4
|
export const PT_TO_MM_RATIO = 0.3528;
|
5
5
|
export const MM_TO_PT_RATIO = 2.8346; // https://www.ddc.co.jp/words/archives/20090701114500.html
|
6
6
|
export const ZOOM = 3.7795275591;
|
7
|
+
|
8
|
+
// TODO replace in the future
|
9
|
+
// export const BLANK_PDF: BlankPdf = {
|
10
|
+
// width: 210,
|
11
|
+
// height: 297,
|
12
|
+
// padding: [10, 10, 10, 10],
|
13
|
+
// };
|
14
|
+
|
7
15
|
export const BLANK_PDF =
|
8
16
|
'data:application/pdf;base64,JVBERi0xLjcKJeLjz9MKNSAwIG9iago8PAovRmlsdGVyIC9GbGF0ZURlY29kZQovTGVuZ3RoIDM4Cj4+CnN0cmVhbQp4nCvkMlAwUDC1NNUzMVGwMDHUszRSKErlCtfiyuMK5AIAXQ8GCgplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL01lZGlhQm94IFswIDAgNTk1LjQ0IDg0MS45Ml0KL1Jlc291cmNlcyA8PAo+PgovQ29udGVudHMgNSAwIFIKL1BhcmVudCAyIDAgUgo+PgplbmRvYmoKMiAwIG9iago8PAovVHlwZSAvUGFnZXMKL0tpZHMgWzQgMCBSXQovQ291bnQgMQo+PgplbmRvYmoKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjMgMCBvYmoKPDwKL3RyYXBwZWQgKGZhbHNlKQovQ3JlYXRvciAoU2VyaWYgQWZmaW5pdHkgRGVzaWduZXIgMS4xMC40KQovVGl0bGUgKFVudGl0bGVkLnBkZikKL0NyZWF0aW9uRGF0ZSAoRDoyMDIyMDEwNjE0MDg1OCswOScwMCcpCi9Qcm9kdWNlciAoaUxvdmVQREYpCi9Nb2REYXRlIChEOjIwMjIwMTA2MDUwOTA5WikKPj4KZW5kb2JqCjYgMCBvYmoKPDwKL1NpemUgNwovUm9vdCAxIDAgUgovSW5mbyAzIDAgUgovSUQgWzwyODhCM0VENTAyOEU0MDcyNERBNzNCOUE0Nzk4OUEwQT4gPEY1RkJGNjg4NkVERDZBQUNBNDRCNEZDRjBBRDUxRDlDPl0KL1R5cGUgL1hSZWYKL1cgWzEgMiAyXQovRmlsdGVyIC9GbGF0ZURlY29kZQovSW5kZXggWzAgN10KL0xlbmd0aCAzNgo+PgpzdHJlYW0KeJxjYGD4/5+RUZmBgZHhFZBgDAGxakAEP5BgEmFgAABlRwQJCmVuZHN0cmVhbQplbmRvYmoKc3RhcnR4cmVmCjUzMgolJUVPRgo=';
|
9
17
|
export const DEFAULT_FONT_NAME = 'Roboto';
|
@@ -0,0 +1,277 @@
|
|
1
|
+
import { Schema, Template, BasePdf, BlankPdf, CommonOptions } from './types';
|
2
|
+
import { cloneDeep, isBlankPdf } from './helper';
|
3
|
+
|
4
|
+
interface ModifyTemplateForDynamicTableArg {
|
5
|
+
template: Template;
|
6
|
+
input: Record<string, string>;
|
7
|
+
_cache: Map<any, any>;
|
8
|
+
options: CommonOptions;
|
9
|
+
getDynamicHeights: (
|
10
|
+
value: string,
|
11
|
+
args: { schema: Schema; basePdf: BasePdf; options: CommonOptions; _cache: Map<any, any> }
|
12
|
+
) => Promise<number[]>;
|
13
|
+
}
|
14
|
+
|
15
|
+
class LayoutNode {
|
16
|
+
index = 0;
|
17
|
+
|
18
|
+
schema?: Schema;
|
19
|
+
|
20
|
+
children: LayoutNode[] = [];
|
21
|
+
|
22
|
+
width = 0;
|
23
|
+
height = 0;
|
24
|
+
padding: [number, number, number, number] = [0, 0, 0, 0];
|
25
|
+
position: { x: number; y: number } = { x: 0, y: 0 };
|
26
|
+
|
27
|
+
constructor({ width = 0, height = 0 } = {}) {
|
28
|
+
this.width = width;
|
29
|
+
this.height = height;
|
30
|
+
}
|
31
|
+
|
32
|
+
setIndex(index: number): void {
|
33
|
+
this.index = index;
|
34
|
+
}
|
35
|
+
|
36
|
+
setSchema(schema: Schema): void {
|
37
|
+
this.schema = schema;
|
38
|
+
}
|
39
|
+
|
40
|
+
setWidth(width: number): void {
|
41
|
+
this.width = width;
|
42
|
+
}
|
43
|
+
|
44
|
+
setHeight(height: number): void {
|
45
|
+
this.height = height;
|
46
|
+
}
|
47
|
+
|
48
|
+
setPadding(padding: [number, number, number, number]): void {
|
49
|
+
this.padding = padding;
|
50
|
+
}
|
51
|
+
|
52
|
+
setPosition(position: { x: number; y: number }): void {
|
53
|
+
this.position = position;
|
54
|
+
}
|
55
|
+
|
56
|
+
insertChild(child: LayoutNode): void {
|
57
|
+
const index = this.getChildCount();
|
58
|
+
child.setIndex(index);
|
59
|
+
this.children.splice(index, 0, child);
|
60
|
+
}
|
61
|
+
|
62
|
+
getChildCount(): number {
|
63
|
+
return this.children.length;
|
64
|
+
}
|
65
|
+
|
66
|
+
getChild(index: number): LayoutNode {
|
67
|
+
return this.children[index];
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
function createPage(basePdf: BlankPdf) {
|
72
|
+
const page = new LayoutNode({ ...basePdf });
|
73
|
+
page.setPadding(basePdf.padding);
|
74
|
+
return page;
|
75
|
+
}
|
76
|
+
|
77
|
+
function createNode(arg: {
|
78
|
+
schema: Schema;
|
79
|
+
position: { x: number; y: number };
|
80
|
+
width: number;
|
81
|
+
height: number;
|
82
|
+
}) {
|
83
|
+
const { position, width, height, schema } = arg;
|
84
|
+
const node = new LayoutNode({ width, height });
|
85
|
+
node.setPosition(position);
|
86
|
+
node.setSchema(schema);
|
87
|
+
return node;
|
88
|
+
}
|
89
|
+
|
90
|
+
function resortChildren(page: LayoutNode, orderMap: Map<string, number>): void {
|
91
|
+
page.children = page.children
|
92
|
+
.sort((a, b) => {
|
93
|
+
const orderA = orderMap.get(a.schema?.name!);
|
94
|
+
const orderB = orderMap.get(b.schema?.name!);
|
95
|
+
if (orderA === undefined || orderB === undefined) {
|
96
|
+
throw new Error('[@pdfme/common] order is not defined');
|
97
|
+
}
|
98
|
+
return orderA - orderB;
|
99
|
+
})
|
100
|
+
.map((child, index) => {
|
101
|
+
child.setIndex(index);
|
102
|
+
return child;
|
103
|
+
});
|
104
|
+
}
|
105
|
+
|
106
|
+
async function createOnePage(
|
107
|
+
arg: {
|
108
|
+
basePdf: BlankPdf;
|
109
|
+
schemaPage: Schema[];
|
110
|
+
orderMap: Map<string, number>;
|
111
|
+
} & Omit<ModifyTemplateForDynamicTableArg, 'template'>
|
112
|
+
): Promise<LayoutNode> {
|
113
|
+
const { basePdf, schemaPage, orderMap, input, options, _cache, getDynamicHeights } = arg;
|
114
|
+
const page = createPage(basePdf);
|
115
|
+
|
116
|
+
const schemaPositions: number[] = [];
|
117
|
+
const sortedSchemaEntries = cloneDeep(schemaPage).sort((a, b) => a.position.y - b.position.y);
|
118
|
+
const diffMap = new Map();
|
119
|
+
for (const schema of sortedSchemaEntries) {
|
120
|
+
const { position, width } = schema;
|
121
|
+
|
122
|
+
const opt = { schema, basePdf, options, _cache };
|
123
|
+
const value = (schema.readOnly ? schema.content : input?.[schema.name]) || '';
|
124
|
+
const heights = await getDynamicHeights(value, opt);
|
125
|
+
|
126
|
+
const heightsSum = heights.reduce((acc, cur) => acc + cur, 0);
|
127
|
+
const originalHeight = schema.height;
|
128
|
+
if (heightsSum !== originalHeight) {
|
129
|
+
diffMap.set(position.y + originalHeight, heightsSum - originalHeight);
|
130
|
+
}
|
131
|
+
heights.forEach((height, index) => {
|
132
|
+
let y = schema.position.y + heights.reduce((acc, cur, i) => (i < index ? acc + cur : acc), 0);
|
133
|
+
for (const [diffY, diff] of diffMap.entries()) {
|
134
|
+
if (diffY <= schema.position.y) {
|
135
|
+
y += diff;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
const node = createNode({ schema, position: { ...position, y }, width, height });
|
139
|
+
|
140
|
+
schemaPositions.push(y + height + basePdf.padding[2]);
|
141
|
+
page.insertChild(node);
|
142
|
+
});
|
143
|
+
}
|
144
|
+
|
145
|
+
const pageHeight = Math.max(...schemaPositions, basePdf.height - basePdf.padding[2]);
|
146
|
+
page.setHeight(pageHeight);
|
147
|
+
|
148
|
+
resortChildren(page, orderMap);
|
149
|
+
|
150
|
+
return page;
|
151
|
+
}
|
152
|
+
|
153
|
+
function breakIntoPages(arg: {
|
154
|
+
longPage: LayoutNode;
|
155
|
+
orderMap: Map<string, number>;
|
156
|
+
basePdf: BlankPdf;
|
157
|
+
}): LayoutNode[] {
|
158
|
+
const { longPage, orderMap, basePdf } = arg;
|
159
|
+
const pages: LayoutNode[] = [createPage(basePdf)];
|
160
|
+
const [paddingTop, , paddingBottom] = basePdf.padding;
|
161
|
+
const yAdjustments: { page: number; value: number }[] = [];
|
162
|
+
|
163
|
+
const getPageHeight = (pageIndex: number) =>
|
164
|
+
basePdf.height - paddingBottom - (pageIndex > 0 ? paddingTop : 0);
|
165
|
+
|
166
|
+
const calculateNewY = (y: number, pageIndex: number) => {
|
167
|
+
const newY = y - pageIndex * (basePdf.height - paddingTop - paddingBottom);
|
168
|
+
|
169
|
+
while (pages.length <= pageIndex) {
|
170
|
+
if (!pages[pageIndex]) {
|
171
|
+
pages.push(createPage(basePdf));
|
172
|
+
yAdjustments.push({ page: pageIndex, value: (newY - paddingTop) * -1 });
|
173
|
+
}
|
174
|
+
}
|
175
|
+
return newY + (yAdjustments.find((adj) => adj.page === pageIndex)?.value || 0);
|
176
|
+
};
|
177
|
+
|
178
|
+
const children = longPage.children.sort((a, b) => a.position.y - b.position.y);
|
179
|
+
for (let i = 0; i < children.length; i++) {
|
180
|
+
const { schema, position, height, width } = children[i];
|
181
|
+
const { y, x } = position;
|
182
|
+
|
183
|
+
let targetPageIndex = Math.floor(y / getPageHeight(pages.length - 1));
|
184
|
+
let newY = calculateNewY(y, targetPageIndex);
|
185
|
+
|
186
|
+
if (newY + height > basePdf.height - paddingBottom) {
|
187
|
+
targetPageIndex++;
|
188
|
+
newY = calculateNewY(y, targetPageIndex);
|
189
|
+
}
|
190
|
+
|
191
|
+
if (!schema) throw new Error('[@pdfme/common] schema is undefined');
|
192
|
+
|
193
|
+
const clonedElement = createNode({ schema, position: { x, y: newY }, width, height });
|
194
|
+
pages[targetPageIndex].insertChild(clonedElement);
|
195
|
+
}
|
196
|
+
|
197
|
+
pages.forEach((page) => resortChildren(page, orderMap));
|
198
|
+
|
199
|
+
return pages;
|
200
|
+
}
|
201
|
+
|
202
|
+
function createNewTemplate(pages: LayoutNode[], basePdf: BlankPdf): Template {
|
203
|
+
const newTemplate: Template = {
|
204
|
+
schemas: Array.from({ length: pages.length }, () => [] as Schema[]),
|
205
|
+
basePdf: basePdf,
|
206
|
+
};
|
207
|
+
|
208
|
+
const nameToSchemas = new Map<string, LayoutNode[]>();
|
209
|
+
|
210
|
+
cloneDeep(pages).forEach((page, pageIndex) => {
|
211
|
+
page.children.forEach((child) => {
|
212
|
+
const { schema } = child;
|
213
|
+
if (!schema) throw new Error('[@pdfme/common] schema is undefined');
|
214
|
+
|
215
|
+
const name = schema.name;
|
216
|
+
if (!nameToSchemas.has(name)) {
|
217
|
+
nameToSchemas.set(name, []);
|
218
|
+
}
|
219
|
+
nameToSchemas.get(name)!.push(child);
|
220
|
+
|
221
|
+
const sameNameSchemas = page.children.filter((c) => c.schema?.name === name);
|
222
|
+
const start = nameToSchemas.get(name)!.length - sameNameSchemas.length;
|
223
|
+
|
224
|
+
if (sameNameSchemas.length > 0) {
|
225
|
+
if (!sameNameSchemas[0].schema) {
|
226
|
+
throw new Error('[@pdfme/common] schema is undefined');
|
227
|
+
}
|
228
|
+
|
229
|
+
// Use the first schema to get the schema and position
|
230
|
+
const schema = sameNameSchemas[0].schema;
|
231
|
+
const height = sameNameSchemas.reduce((acc, cur) => acc + cur.height, 0);
|
232
|
+
const position = sameNameSchemas[0].position;
|
233
|
+
|
234
|
+
// Currently, __bodyRange exists for table schemas, but if we make it more abstract,
|
235
|
+
// it could be used for other schemas as well to render schemas that have been split by page breaks, starting from the middle.
|
236
|
+
schema.__bodyRange = {
|
237
|
+
start: Math.max(start - 1, 0),
|
238
|
+
end: start + sameNameSchemas.length - 1,
|
239
|
+
};
|
240
|
+
|
241
|
+
// Currently, this is used to determine whether to display the header when a table is split.
|
242
|
+
schema.__isSplit = start > 0;
|
243
|
+
|
244
|
+
const newSchema = Object.assign({}, schema, { position, height });
|
245
|
+
const index = newTemplate.schemas[pageIndex].findIndex((s) => s.name === name);
|
246
|
+
if (index !== -1) {
|
247
|
+
newTemplate.schemas[pageIndex][index] = newSchema;
|
248
|
+
} else {
|
249
|
+
newTemplate.schemas[pageIndex].push(newSchema);
|
250
|
+
}
|
251
|
+
}
|
252
|
+
});
|
253
|
+
});
|
254
|
+
|
255
|
+
return newTemplate;
|
256
|
+
}
|
257
|
+
|
258
|
+
export const getDynamicTemplate = async (
|
259
|
+
arg: ModifyTemplateForDynamicTableArg
|
260
|
+
): Promise<Template> => {
|
261
|
+
const { template } = arg;
|
262
|
+
if (!isBlankPdf(template.basePdf)) {
|
263
|
+
return template;
|
264
|
+
}
|
265
|
+
|
266
|
+
const basePdf = template.basePdf as BlankPdf;
|
267
|
+
const pages: LayoutNode[] = [];
|
268
|
+
|
269
|
+
for (const schemaPage of template.schemas) {
|
270
|
+
const orderMap = new Map(schemaPage.map((schema, index) => [schema.name, index]));
|
271
|
+
const longPage = await createOnePage({ basePdf, schemaPage, orderMap, ...arg });
|
272
|
+
const brokenPages = breakIntoPages({ longPage, basePdf, orderMap });
|
273
|
+
pages.push(...brokenPages);
|
274
|
+
}
|
275
|
+
|
276
|
+
return createNewTemplate(pages, template.basePdf);
|
277
|
+
};
|
@@ -0,0 +1,392 @@
|
|
1
|
+
import * as acorn from 'acorn';
|
2
|
+
import type { Node as AcornNode, Identifier, Property } from 'estree';
|
3
|
+
import type { SchemaPageArray } from './types';
|
4
|
+
|
5
|
+
const expressionCache = new Map<string, (context: Record<string, unknown>) => unknown>();
|
6
|
+
const parseDataCache = new Map<string, Record<string, unknown>>();
|
7
|
+
|
8
|
+
const parseData = (data: Record<string, unknown>): Record<string, unknown> => {
|
9
|
+
const key = JSON.stringify(data);
|
10
|
+
if (parseDataCache.has(key)) {
|
11
|
+
return parseDataCache.get(key)!;
|
12
|
+
}
|
13
|
+
|
14
|
+
const parsed = Object.fromEntries(
|
15
|
+
Object.entries(data).map(([key, value]) => {
|
16
|
+
if (typeof value === 'string') {
|
17
|
+
try {
|
18
|
+
const parsedValue = JSON.parse(value) as unknown;
|
19
|
+
return [key, parsedValue];
|
20
|
+
} catch {
|
21
|
+
return [key, value];
|
22
|
+
}
|
23
|
+
}
|
24
|
+
return [key, value];
|
25
|
+
})
|
26
|
+
);
|
27
|
+
|
28
|
+
parseDataCache.set(key, parsed);
|
29
|
+
return parsed;
|
30
|
+
};
|
31
|
+
|
32
|
+
const padZero = (num: number): string => String(num).padStart(2, '0');
|
33
|
+
|
34
|
+
const formatDate = (date: Date): string =>
|
35
|
+
`${date.getFullYear()}/${padZero(date.getMonth() + 1)}/${padZero(date.getDate())}`;
|
36
|
+
|
37
|
+
const formatDateTime = (date: Date): string =>
|
38
|
+
`${formatDate(date)} ${padZero(date.getHours())}:${padZero(date.getMinutes())}`;
|
39
|
+
|
40
|
+
const allowedGlobals: Record<string, unknown> = {
|
41
|
+
Math,
|
42
|
+
String,
|
43
|
+
Number,
|
44
|
+
Boolean,
|
45
|
+
Array,
|
46
|
+
Object,
|
47
|
+
Date,
|
48
|
+
JSON,
|
49
|
+
isNaN,
|
50
|
+
parseFloat,
|
51
|
+
parseInt,
|
52
|
+
decodeURI,
|
53
|
+
decodeURIComponent,
|
54
|
+
encodeURI,
|
55
|
+
encodeURIComponent,
|
56
|
+
};
|
57
|
+
|
58
|
+
const validateAST = (node: AcornNode): void => {
|
59
|
+
switch (node.type) {
|
60
|
+
case 'Literal':
|
61
|
+
case 'Identifier':
|
62
|
+
break;
|
63
|
+
case 'BinaryExpression':
|
64
|
+
case 'LogicalExpression': {
|
65
|
+
const binaryNode = node;
|
66
|
+
validateAST(binaryNode.left);
|
67
|
+
validateAST(binaryNode.right);
|
68
|
+
break;
|
69
|
+
}
|
70
|
+
case 'UnaryExpression': {
|
71
|
+
const unaryNode = node;
|
72
|
+
validateAST(unaryNode.argument);
|
73
|
+
break;
|
74
|
+
}
|
75
|
+
case 'ConditionalExpression': {
|
76
|
+
const condNode = node;
|
77
|
+
validateAST(condNode.test);
|
78
|
+
validateAST(condNode.consequent);
|
79
|
+
validateAST(condNode.alternate);
|
80
|
+
break;
|
81
|
+
}
|
82
|
+
case 'MemberExpression': {
|
83
|
+
const memberNode = node;
|
84
|
+
validateAST(memberNode.object);
|
85
|
+
if (memberNode.computed) {
|
86
|
+
validateAST(memberNode.property);
|
87
|
+
} else {
|
88
|
+
const propName = (memberNode.property as Identifier).name;
|
89
|
+
if (['constructor', '__proto__', 'prototype'].includes(propName)) {
|
90
|
+
throw new Error('Access to prohibited property');
|
91
|
+
}
|
92
|
+
const prohibitedMethods = ['toLocaleString', 'valueOf'];
|
93
|
+
if (typeof propName === 'string' && prohibitedMethods.includes(propName)) {
|
94
|
+
throw new Error(`Access to prohibited method: ${propName}`);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
break;
|
98
|
+
}
|
99
|
+
case 'CallExpression': {
|
100
|
+
const callNode = node;
|
101
|
+
validateAST(callNode.callee);
|
102
|
+
callNode.arguments.forEach(validateAST);
|
103
|
+
break;
|
104
|
+
}
|
105
|
+
case 'ArrayExpression': {
|
106
|
+
const arrayNode = node;
|
107
|
+
arrayNode.elements.forEach((elem) => {
|
108
|
+
if (elem) validateAST(elem);
|
109
|
+
});
|
110
|
+
break;
|
111
|
+
}
|
112
|
+
case 'ObjectExpression': {
|
113
|
+
const objectNode = node;
|
114
|
+
objectNode.properties.forEach((prop) => {
|
115
|
+
const propNode = prop as Property;
|
116
|
+
validateAST(propNode.key);
|
117
|
+
validateAST(propNode.value);
|
118
|
+
});
|
119
|
+
break;
|
120
|
+
}
|
121
|
+
case 'ArrowFunctionExpression': {
|
122
|
+
const arrowFuncNode = node;
|
123
|
+
arrowFuncNode.params.forEach((param) => {
|
124
|
+
if (param.type !== 'Identifier') {
|
125
|
+
throw new Error('Only identifier parameters are supported in arrow functions');
|
126
|
+
}
|
127
|
+
validateAST(param);
|
128
|
+
});
|
129
|
+
validateAST(arrowFuncNode.body);
|
130
|
+
break;
|
131
|
+
}
|
132
|
+
default:
|
133
|
+
throw new Error(`Unsupported syntax in placeholder: ${node.type}`);
|
134
|
+
}
|
135
|
+
};
|
136
|
+
|
137
|
+
const evaluateAST = (node: AcornNode, context: Record<string, unknown>): unknown => {
|
138
|
+
switch (node.type) {
|
139
|
+
case 'Literal': {
|
140
|
+
const literalNode = node;
|
141
|
+
return literalNode.value;
|
142
|
+
}
|
143
|
+
case 'Identifier': {
|
144
|
+
const idNode = node;
|
145
|
+
if (Object.prototype.hasOwnProperty.call(context, idNode.name)) {
|
146
|
+
return context[idNode.name];
|
147
|
+
} else if (Object.prototype.hasOwnProperty.call(allowedGlobals, idNode.name)) {
|
148
|
+
return allowedGlobals[idNode.name];
|
149
|
+
} else {
|
150
|
+
throw new Error(`Undefined variable: ${idNode.name}`);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
case 'BinaryExpression': {
|
154
|
+
const binaryNode = node;
|
155
|
+
const left = evaluateAST(binaryNode.left, context) as number;
|
156
|
+
const right = evaluateAST(binaryNode.right, context) as number;
|
157
|
+
switch (binaryNode.operator) {
|
158
|
+
case '+':
|
159
|
+
return left + right;
|
160
|
+
case '-':
|
161
|
+
return left - right;
|
162
|
+
case '*':
|
163
|
+
return left * right;
|
164
|
+
case '/':
|
165
|
+
return left / right;
|
166
|
+
case '%':
|
167
|
+
return left % right;
|
168
|
+
case '**':
|
169
|
+
return left ** right;
|
170
|
+
default:
|
171
|
+
throw new Error(`Unsupported operator: ${binaryNode.operator}`);
|
172
|
+
}
|
173
|
+
}
|
174
|
+
case 'LogicalExpression': {
|
175
|
+
const logicalNode = node;
|
176
|
+
const leftLogical = evaluateAST(logicalNode.left, context);
|
177
|
+
const rightLogical = evaluateAST(logicalNode.right, context);
|
178
|
+
switch (logicalNode.operator) {
|
179
|
+
case '&&':
|
180
|
+
return leftLogical && rightLogical;
|
181
|
+
case '||':
|
182
|
+
return leftLogical || rightLogical;
|
183
|
+
default:
|
184
|
+
throw new Error(`Unsupported operator: ${logicalNode.operator}`);
|
185
|
+
}
|
186
|
+
}
|
187
|
+
case 'UnaryExpression': {
|
188
|
+
const unaryNode = node;
|
189
|
+
const arg = evaluateAST(unaryNode.argument, context) as number;
|
190
|
+
switch (unaryNode.operator) {
|
191
|
+
case '+':
|
192
|
+
return +arg;
|
193
|
+
case '-':
|
194
|
+
return -arg;
|
195
|
+
case '!':
|
196
|
+
return !arg;
|
197
|
+
default:
|
198
|
+
throw new Error(`Unsupported operator: ${unaryNode.operator}`);
|
199
|
+
}
|
200
|
+
}
|
201
|
+
case 'ConditionalExpression': {
|
202
|
+
const condNode = node;
|
203
|
+
const test = evaluateAST(condNode.test, context);
|
204
|
+
return test
|
205
|
+
? evaluateAST(condNode.consequent, context)
|
206
|
+
: evaluateAST(condNode.alternate, context);
|
207
|
+
}
|
208
|
+
case 'MemberExpression': {
|
209
|
+
const memberNode = node;
|
210
|
+
const obj = evaluateAST(memberNode.object, context) as Record<string, unknown>;
|
211
|
+
let prop: string | number;
|
212
|
+
if (memberNode.computed) {
|
213
|
+
prop = evaluateAST(memberNode.property, context) as string | number;
|
214
|
+
} else {
|
215
|
+
prop = (memberNode.property as Identifier).name;
|
216
|
+
}
|
217
|
+
if (typeof prop === 'string' || typeof prop === 'number') {
|
218
|
+
if (typeof prop === 'string' && ['constructor', '__proto__', 'prototype'].includes(prop)) {
|
219
|
+
throw new Error('Access to prohibited property');
|
220
|
+
}
|
221
|
+
return obj[prop];
|
222
|
+
} else {
|
223
|
+
throw new Error('Invalid property access');
|
224
|
+
}
|
225
|
+
}
|
226
|
+
case 'CallExpression': {
|
227
|
+
const callNode = node;
|
228
|
+
const callee = evaluateAST(callNode.callee, context);
|
229
|
+
const args = callNode.arguments.map((argNode) => evaluateAST(argNode, context));
|
230
|
+
if (typeof callee === 'function') {
|
231
|
+
if (callNode.callee.type === 'MemberExpression') {
|
232
|
+
const memberExpr = callNode.callee;
|
233
|
+
const obj = evaluateAST(memberExpr.object, context);
|
234
|
+
if (
|
235
|
+
obj !== null &&
|
236
|
+
(typeof obj === 'object' ||
|
237
|
+
typeof obj === 'number' ||
|
238
|
+
typeof obj === 'string' ||
|
239
|
+
typeof obj === 'boolean')
|
240
|
+
) {
|
241
|
+
return callee.call(obj, ...args);
|
242
|
+
} else {
|
243
|
+
throw new Error('Invalid object in member function call');
|
244
|
+
}
|
245
|
+
} else {
|
246
|
+
return callee(...args);
|
247
|
+
}
|
248
|
+
} else {
|
249
|
+
throw new Error('Attempted to call a non-function');
|
250
|
+
}
|
251
|
+
}
|
252
|
+
case 'ArrowFunctionExpression': {
|
253
|
+
const arrowFuncNode = node;
|
254
|
+
const params = arrowFuncNode.params.map((param) => (param as Identifier).name);
|
255
|
+
const body = arrowFuncNode.body;
|
256
|
+
|
257
|
+
return (...args: unknown[]) => {
|
258
|
+
const newContext = { ...context };
|
259
|
+
params.forEach((param, index) => {
|
260
|
+
newContext[param] = args[index];
|
261
|
+
});
|
262
|
+
return evaluateAST(body, newContext);
|
263
|
+
};
|
264
|
+
}
|
265
|
+
case 'ArrayExpression': {
|
266
|
+
const arrayNode = node;
|
267
|
+
return arrayNode.elements.map((elem) => (elem ? evaluateAST(elem, context) : null));
|
268
|
+
}
|
269
|
+
case 'ObjectExpression': {
|
270
|
+
const objectNode = node;
|
271
|
+
const objResult: Record<string, unknown> = {};
|
272
|
+
objectNode.properties.forEach((prop) => {
|
273
|
+
const propNode = prop as Property;
|
274
|
+
let key: string;
|
275
|
+
if (propNode.key.type === 'Identifier') {
|
276
|
+
key = propNode.key.name;
|
277
|
+
} else {
|
278
|
+
const evaluatedKey = evaluateAST(propNode.key, context);
|
279
|
+
if (typeof evaluatedKey !== 'string' && typeof evaluatedKey !== 'number') {
|
280
|
+
throw new Error('Object property keys must be strings or numbers');
|
281
|
+
}
|
282
|
+
key = String(evaluatedKey);
|
283
|
+
}
|
284
|
+
const value = evaluateAST(propNode.value, context);
|
285
|
+
objResult[key] = value;
|
286
|
+
});
|
287
|
+
return objResult;
|
288
|
+
}
|
289
|
+
default:
|
290
|
+
throw new Error(`Unsupported syntax in placeholder: ${node.type}`);
|
291
|
+
}
|
292
|
+
};
|
293
|
+
|
294
|
+
const evaluatePlaceholders = (arg: {
|
295
|
+
content: string;
|
296
|
+
context: Record<string, unknown>;
|
297
|
+
}): string => {
|
298
|
+
const { content, context } = arg;
|
299
|
+
|
300
|
+
let resultContent = '';
|
301
|
+
let index = 0;
|
302
|
+
|
303
|
+
while (index < content.length) {
|
304
|
+
const startIndex = content.indexOf('{', index);
|
305
|
+
if (startIndex === -1) {
|
306
|
+
resultContent += content.slice(index);
|
307
|
+
break;
|
308
|
+
}
|
309
|
+
|
310
|
+
resultContent += content.slice(index, startIndex);
|
311
|
+
let braceCount = 1;
|
312
|
+
let endIndex = startIndex + 1;
|
313
|
+
|
314
|
+
while (endIndex < content.length && braceCount > 0) {
|
315
|
+
if (content[endIndex] === '{') {
|
316
|
+
braceCount++;
|
317
|
+
} else if (content[endIndex] === '}') {
|
318
|
+
braceCount--;
|
319
|
+
}
|
320
|
+
endIndex++;
|
321
|
+
}
|
322
|
+
|
323
|
+
if (braceCount === 0) {
|
324
|
+
const code = content.slice(startIndex + 1, endIndex - 1).trim();
|
325
|
+
|
326
|
+
if (expressionCache.has(code)) {
|
327
|
+
const evalFunc = expressionCache.get(code)!;
|
328
|
+
try {
|
329
|
+
const value = evalFunc(context);
|
330
|
+
resultContent += String(value);
|
331
|
+
} catch {
|
332
|
+
resultContent += content.slice(startIndex, endIndex);
|
333
|
+
}
|
334
|
+
} else {
|
335
|
+
try {
|
336
|
+
const ast = acorn.parseExpressionAt(code, 0, { ecmaVersion: 'latest' }) as AcornNode;
|
337
|
+
validateAST(ast);
|
338
|
+
const evalFunc = (ctx: Record<string, unknown>) => evaluateAST(ast, ctx);
|
339
|
+
expressionCache.set(code, evalFunc);
|
340
|
+
const value = evalFunc(context);
|
341
|
+
resultContent += String(value);
|
342
|
+
} catch {
|
343
|
+
resultContent += content.slice(startIndex, endIndex);
|
344
|
+
}
|
345
|
+
}
|
346
|
+
|
347
|
+
index = endIndex;
|
348
|
+
} else {
|
349
|
+
throw new Error('Invalid placeholder');
|
350
|
+
}
|
351
|
+
}
|
352
|
+
|
353
|
+
return resultContent;
|
354
|
+
};
|
355
|
+
|
356
|
+
|
357
|
+
export const replacePlaceholders = (arg: {
|
358
|
+
content: string;
|
359
|
+
variables: Record<string, any>;
|
360
|
+
schemas: SchemaPageArray;
|
361
|
+
}): string => {
|
362
|
+
const { content, variables, schemas } = arg;
|
363
|
+
if (!content || typeof content !== 'string' || !content.includes('{') || !content.includes('}')) {
|
364
|
+
return content;
|
365
|
+
}
|
366
|
+
|
367
|
+
const date = new Date();
|
368
|
+
const formattedDate = formatDate(date);
|
369
|
+
const formattedDateTime = formatDateTime(date);
|
370
|
+
|
371
|
+
const data = {
|
372
|
+
...Object.fromEntries(
|
373
|
+
schemas.flat().map((schema) => [schema.name, schema.readOnly ? schema.content || '' : ''])
|
374
|
+
),
|
375
|
+
...variables,
|
376
|
+
};
|
377
|
+
const parsedInput = parseData(data);
|
378
|
+
|
379
|
+
const context: Record<string, unknown> = {
|
380
|
+
date: formattedDate,
|
381
|
+
dateTime: formattedDateTime,
|
382
|
+
...parsedInput,
|
383
|
+
};
|
384
|
+
|
385
|
+
Object.entries(context).forEach(([key, value]) => {
|
386
|
+
if (typeof value === 'string' && value.includes('{') && value.includes('}')) {
|
387
|
+
context[key] = evaluatePlaceholders({ content: value, context });
|
388
|
+
}
|
389
|
+
});
|
390
|
+
|
391
|
+
return evaluatePlaceholders({ content, context });
|
392
|
+
};
|