@celljs/core 3.9.0 → 4.1.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/README.md +0 -6
- package/cell.remote.yml +8 -0
- package/cell.yml +0 -3
- package/esm/common/application/application-entry.js +21 -3
- package/esm/common/application/application-entry.js.map +1 -1
- package/esm/common/application/application-host.d.ts +38 -0
- package/esm/common/application/application-host.js +17 -0
- package/esm/common/application/application-host.js.map +1 -0
- package/esm/common/application/application-protocol.js +1 -1
- package/esm/common/application/application-protocol.js.map +1 -1
- package/esm/common/application/index.d.ts +1 -0
- package/esm/common/application/index.js +1 -0
- package/esm/common/application/index.js.map +1 -1
- package/esm/common/config/config-provider.js +2 -1
- package/esm/common/config/config-provider.js.map +1 -1
- package/esm/common/config/env-config-util.d.ts +48 -0
- package/esm/common/config/env-config-util.js +185 -0
- package/esm/common/config/env-config-util.js.map +1 -0
- package/esm/common/config/index.d.ts +1 -0
- package/esm/common/config/index.js +1 -0
- package/esm/common/config/index.js.map +1 -1
- package/esm/common/container/auto-bind.js +26 -12
- package/esm/common/container/auto-bind.js.map +1 -1
- package/esm/common/el/expression-compiler.d.ts +4 -15
- package/esm/common/el/expression-compiler.js +6 -163
- package/esm/common/el/expression-compiler.js.map +1 -1
- package/esm/common/el/expression-handler.d.ts +7 -2
- package/esm/common/el/expression-handler.js +14 -53
- package/esm/common/el/expression-handler.js.map +1 -1
- package/esm/common/el/expression-util.d.ts +68 -0
- package/esm/common/el/expression-util.js +347 -0
- package/esm/common/el/expression-util.js.map +1 -0
- package/esm/common/el/index.d.ts +1 -0
- package/esm/common/el/index.js +1 -0
- package/esm/common/el/index.js.map +1 -1
- package/esm/common/logger/abstract-logger.d.ts +4 -2
- package/esm/common/logger/abstract-logger.js +11 -6
- package/esm/common/logger/abstract-logger.js.map +1 -1
- package/esm/common/logger/logger-protocol.d.ts +4 -0
- package/esm/common/logger/logger-protocol.js +2 -0
- package/esm/common/logger/logger-protocol.js.map +1 -1
- package/esm/common/logger/logger.js +7 -7
- package/esm/common/logger/logger.js.map +1 -1
- package/esm/common/test/mock-logger.d.ts +1 -0
- package/esm/common/test/mock-logger.js +3 -0
- package/esm/common/test/mock-logger.js.map +1 -1
- package/esm/common/utils/emitter.d.ts +1 -10
- package/esm/common/utils/emitter.js +0 -56
- package/esm/common/utils/emitter.js.map +1 -1
- package/esm/common/utils/mime-type-util.js +1 -1
- package/esm/common/utils/mime-type-util.js.map +1 -1
- package/esm/common/utils/mime-type.js +1 -1
- package/esm/common/utils/mime-type.js.map +1 -1
- package/esm/node/context/context.d.ts +11 -2
- package/esm/node/context/context.js +36 -12
- package/esm/node/context/context.js.map +1 -1
- package/esm/node/index.d.ts +0 -1
- package/esm/node/index.js +0 -1
- package/esm/node/index.js.map +1 -1
- package/lib/common/aop/aop.spec.d.ts +2 -0
- package/lib/common/aop/aop.spec.d.ts.map +1 -0
- package/lib/common/aop/aop.spec.js +170 -0
- package/lib/common/aop/aop.spec.js.map +1 -0
- package/lib/common/application/application-entry.d.ts.map +1 -1
- package/lib/common/application/application-entry.js +21 -3
- package/lib/common/application/application-entry.js.map +1 -1
- package/lib/common/application/application-host.d.ts +39 -0
- package/lib/common/application/application-host.d.ts.map +1 -0
- package/lib/common/application/application-host.js +20 -0
- package/lib/common/application/application-host.js.map +1 -0
- package/lib/common/application/application-protocol.js +1 -1
- package/lib/common/application/application-protocol.js.map +1 -1
- package/lib/common/application/index.d.ts +1 -0
- package/lib/common/application/index.d.ts.map +1 -1
- package/lib/common/application/index.js +1 -0
- package/lib/common/application/index.js.map +1 -1
- package/lib/common/application/lifecycle.spec.d.ts +2 -0
- package/lib/common/application/lifecycle.spec.d.ts.map +1 -0
- package/lib/common/application/lifecycle.spec.js +155 -0
- package/lib/common/application/lifecycle.spec.js.map +1 -0
- package/lib/common/config/config-provider.js +2 -1
- package/lib/common/config/config-provider.js.map +1 -1
- package/lib/common/config/env-config-util.d.ts +49 -0
- package/lib/common/config/env-config-util.d.ts.map +1 -0
- package/lib/common/config/env-config-util.js +188 -0
- package/lib/common/config/env-config-util.js.map +1 -0
- package/lib/common/config/env-config-util.spec.d.ts +2 -0
- package/lib/common/config/env-config-util.spec.d.ts.map +1 -0
- package/lib/common/config/env-config-util.spec.js +314 -0
- package/lib/common/config/env-config-util.spec.js.map +1 -0
- package/lib/common/config/index.d.ts +1 -0
- package/lib/common/config/index.d.ts.map +1 -1
- package/lib/common/config/index.js +1 -0
- package/lib/common/config/index.js.map +1 -1
- package/lib/common/container/assembly.spec.d.ts +2 -0
- package/lib/common/container/assembly.spec.d.ts.map +1 -0
- package/lib/common/container/assembly.spec.js +464 -0
- package/lib/common/container/assembly.spec.js.map +1 -0
- package/lib/common/container/auto-bind.js +26 -12
- package/lib/common/container/auto-bind.js.map +1 -1
- package/lib/common/el/expression-compiler.d.ts +4 -15
- package/lib/common/el/expression-compiler.d.ts.map +1 -1
- package/lib/common/el/expression-compiler.js +6 -163
- package/lib/common/el/expression-compiler.js.map +1 -1
- package/lib/common/el/expression-handler.d.ts +7 -2
- package/lib/common/el/expression-handler.d.ts.map +1 -1
- package/lib/common/el/expression-handler.js +14 -56
- package/lib/common/el/expression-handler.js.map +1 -1
- package/lib/common/el/expression-handler.spec.d.ts +2 -0
- package/lib/common/el/expression-handler.spec.d.ts.map +1 -0
- package/lib/common/el/expression-handler.spec.js +99 -0
- package/lib/common/el/expression-handler.spec.js.map +1 -0
- package/lib/common/el/expression-util.d.ts +69 -0
- package/lib/common/el/expression-util.d.ts.map +1 -0
- package/lib/common/el/expression-util.js +352 -0
- package/lib/common/el/expression-util.js.map +1 -0
- package/lib/common/el/expression-util.spec.d.ts +2 -0
- package/lib/common/el/expression-util.spec.d.ts.map +1 -0
- package/lib/common/el/expression-util.spec.js +45 -0
- package/lib/common/el/expression-util.spec.js.map +1 -0
- package/lib/common/el/index.d.ts +1 -0
- package/lib/common/el/index.d.ts.map +1 -1
- package/lib/common/el/index.js +1 -0
- package/lib/common/el/index.js.map +1 -1
- package/lib/common/el/value-and-el.spec.d.ts +2 -0
- package/lib/common/el/value-and-el.spec.d.ts.map +1 -0
- package/lib/common/el/value-and-el.spec.js +216 -0
- package/lib/common/el/value-and-el.spec.js.map +1 -0
- package/lib/common/logger/abstract-logger.d.ts +4 -2
- package/lib/common/logger/abstract-logger.d.ts.map +1 -1
- package/lib/common/logger/abstract-logger.js +10 -5
- package/lib/common/logger/abstract-logger.js.map +1 -1
- package/lib/common/logger/logger-protocol.d.ts +4 -0
- package/lib/common/logger/logger-protocol.d.ts.map +1 -1
- package/lib/common/logger/logger-protocol.js +3 -1
- package/lib/common/logger/logger-protocol.js.map +1 -1
- package/lib/common/logger/logger.js +7 -7
- package/lib/common/logger/logger.js.map +1 -1
- package/lib/common/logger/logger.spec.d.ts +2 -0
- package/lib/common/logger/logger.spec.d.ts.map +1 -0
- package/lib/common/logger/logger.spec.js +57 -0
- package/lib/common/logger/logger.spec.js.map +1 -0
- package/lib/common/test/mock-logger.d.ts +1 -0
- package/lib/common/test/mock-logger.d.ts.map +1 -1
- package/lib/common/test/mock-logger.js +3 -0
- package/lib/common/test/mock-logger.js.map +1 -1
- package/lib/common/utils/emitter.d.ts +1 -10
- package/lib/common/utils/emitter.d.ts.map +1 -1
- package/lib/common/utils/emitter.js +1 -58
- package/lib/common/utils/emitter.js.map +1 -1
- package/lib/common/utils/mime-type-util.js +1 -1
- package/lib/common/utils/mime-type-util.js.map +1 -1
- package/lib/common/utils/mime-type.js +1 -1
- package/lib/common/utils/mime-type.js.map +1 -1
- package/lib/common/utils/utils-behavior.spec.d.ts +2 -0
- package/lib/common/utils/utils-behavior.spec.d.ts.map +1 -0
- package/lib/common/utils/utils-behavior.spec.js +232 -0
- package/lib/common/utils/utils-behavior.spec.js.map +1 -0
- package/lib/node/context/context.d.ts +11 -2
- package/lib/node/context/context.d.ts.map +1 -1
- package/lib/node/context/context.js +36 -10
- package/lib/node/context/context.js.map +1 -1
- package/lib/node/context/context.spec.d.ts +2 -0
- package/lib/node/context/context.spec.d.ts.map +1 -0
- package/lib/node/context/context.spec.js +202 -0
- package/lib/node/context/context.spec.js.map +1 -0
- package/lib/node/index.d.ts +0 -1
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +0 -1
- package/lib/node/index.js.map +1 -1
- package/package.json +20 -41
- package/src/common/aop/aop.spec.ts +145 -0
- package/src/common/application/application-entry.ts +20 -4
- package/src/common/application/application-host.ts +46 -0
- package/src/common/application/application-protocol.ts +1 -1
- package/src/common/application/index.ts +1 -0
- package/src/common/application/lifecycle.spec.ts +148 -0
- package/src/common/config/config-provider.ts +1 -1
- package/src/common/config/env-config-util.spec.ts +341 -0
- package/src/common/config/env-config-util.ts +225 -0
- package/src/common/config/index.ts +1 -0
- package/src/common/container/assembly.spec.ts +293 -0
- package/src/common/container/auto-bind.ts +23 -10
- package/src/common/el/expression-compiler.ts +6 -166
- package/src/common/el/expression-handler.spec.ts +110 -0
- package/src/common/el/expression-handler.ts +14 -51
- package/src/common/el/expression-util.spec.ts +45 -0
- package/src/common/el/expression-util.ts +363 -0
- package/src/common/el/index.ts +2 -1
- package/src/common/el/value-and-el.spec.ts +186 -0
- package/src/common/logger/abstract-logger.ts +12 -6
- package/src/common/logger/logger-protocol.ts +5 -0
- package/src/common/logger/logger.spec.ts +61 -0
- package/src/common/logger/logger.ts +7 -7
- package/src/common/test/mock-logger.ts +3 -0
- package/src/common/utils/emitter.ts +1 -63
- package/src/common/utils/mime-type-util.ts +1 -1
- package/src/common/utils/mime-type.ts +1 -1
- package/src/common/utils/utils-behavior.spec.ts +259 -0
- package/src/node/context/context.spec.ts +219 -0
- package/src/node/context/context.ts +35 -10
- package/src/node/index.ts +0 -1
- package/cell-remote.yml +0 -3
- package/esm/node/utils/crc.d.ts +0 -1
- package/esm/node/utils/crc.js +0 -7
- package/esm/node/utils/crc.js.map +0 -1
- package/esm/node/utils/index.d.ts +0 -1
- package/esm/node/utils/index.js +0 -2
- package/esm/node/utils/index.js.map +0 -1
- package/lib/node/utils/crc.d.ts +0 -2
- package/lib/node/utils/crc.d.ts.map +0 -1
- package/lib/node/utils/crc.js +0 -10
- package/lib/node/utils/crc.js.map +0 -1
- package/lib/node/utils/index.d.ts +0 -2
- package/lib/node/utils/index.d.ts.map +0 -1
- package/lib/node/utils/index.js +0 -18
- package/lib/node/utils/index.js.map +0 -1
- package/src/node/utils/crc.ts +0 -7
- package/src/node/utils/index.ts +0 -1
|
@@ -1,178 +1,18 @@
|
|
|
1
|
-
import { ExpressionCompiler, ExpressionCompilerOptions, JexlEngineProvider } from './expression-protocol';
|
|
2
1
|
import { Component, Autowired } from '../annotation';
|
|
2
|
+
import { ExpressionCompiler, ExpressionCompilerOptions, JexlEngineProvider } from './expression-protocol';
|
|
3
|
+
import { ExpressionSectionCompiler } from './expression-util';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
expression: any;
|
|
6
|
-
nextText?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
5
|
+
/** The {@link ExpressionSectionCompiler} on the jexl engine of the container (with its transforms). */
|
|
9
6
|
@Component(ExpressionCompiler)
|
|
10
7
|
export class ExpressionCompilerImpl implements ExpressionCompiler {
|
|
11
8
|
|
|
12
|
-
private ESCAPE_CHAR = '\\';
|
|
13
|
-
private SPECIAL_CHAR = '$';
|
|
14
|
-
private BRACKET_BEGIN = '{';
|
|
15
|
-
private BRACKET_END = '}';
|
|
16
|
-
|
|
17
9
|
@Autowired(JexlEngineProvider)
|
|
18
10
|
protected readonly jexlEngineProvider: JexlEngineProvider<any>;
|
|
19
11
|
|
|
20
|
-
protected
|
|
21
|
-
if (opitons.ignoreSpecialChar) {
|
|
22
|
-
return '';
|
|
23
|
-
}
|
|
24
|
-
return opitons.specialChar;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
protected equalsSpecialChar(opitons: Required<ExpressionCompilerOptions>, char: string): boolean {
|
|
28
|
-
if (opitons.ignoreSpecialChar) {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
return char === opitons.specialChar;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
compileSections(text: string, options: ExpressionCompilerOptions): any[] {
|
|
35
|
-
const merged = {
|
|
36
|
-
...{ escapeChar: this.ESCAPE_CHAR, specialChar: this.SPECIAL_CHAR, bracketBegin: this.BRACKET_BEGIN, bracketEnd: this.BRACKET_END, ...options }
|
|
37
|
-
} as Required<ExpressionCompilerOptions>;
|
|
38
|
-
if (!text || text.indexOf(`${this.getSpecialChar(merged)}${merged.bracketBegin}`) < 0) {
|
|
39
|
-
return [];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const sections: any[] = [];
|
|
43
|
-
let middleText: string | undefined = text;
|
|
44
|
-
while (middleText) {
|
|
45
|
-
const me: MiddleExpression | undefined = this.middleCompile(middleText, merged);
|
|
46
|
-
if (!me) {
|
|
47
|
-
sections.push(middleText);
|
|
48
|
-
middleText = undefined;
|
|
49
|
-
} else {
|
|
50
|
-
sections.push(me.expression);
|
|
51
|
-
middleText = me.nextText;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return sections;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
protected middleCompile(text: string, options: Required<ExpressionCompilerOptions>): MiddleExpression | undefined {
|
|
59
|
-
let me: MiddleExpression | undefined;
|
|
60
|
-
const prefix = `${this.getSpecialChar(options)}${options.bracketBegin}`;
|
|
61
|
-
const prefix2 = `${prefix}${options.bracketBegin}`;
|
|
62
|
-
if (text.startsWith(prefix2)) {
|
|
63
|
-
me = this.nextMiddleExpression(text.substring(prefix2.length), 2, options);
|
|
64
|
-
} else if (text.startsWith(prefix)) {
|
|
65
|
-
me = this.nextMiddleExpression(text.substring(prefix.length), undefined, options);
|
|
66
|
-
} else {
|
|
67
|
-
me = this.nextString(text, options);
|
|
68
|
-
}
|
|
69
|
-
return me;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
protected nextMiddleExpression(text: string, bracketBeginCharNum = 1, options: Required<ExpressionCompilerOptions>): MiddleExpression | undefined {
|
|
73
|
-
let stringed = false;
|
|
74
|
-
let escaped = false;
|
|
75
|
-
let bracketBeginCharFound = 0;
|
|
76
|
-
|
|
77
|
-
const section: string[] = [];
|
|
78
|
-
for (let i = 0; i < text.length; i++) {
|
|
79
|
-
const c = text[i];
|
|
80
|
-
if (!escaped) {
|
|
81
|
-
if ('\'' === c || '"' === c) {
|
|
82
|
-
stringed = !stringed;
|
|
83
|
-
section.push(c);
|
|
84
|
-
continue;
|
|
85
|
-
} else
|
|
86
|
-
if (c === options.escapeChar) {
|
|
87
|
-
escaped = true;
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (stringed) {
|
|
93
|
-
section.push(c);
|
|
94
|
-
escaped = false;
|
|
95
|
-
} else if (escaped) {
|
|
96
|
-
if (this.equalsSpecialChar(options, c) || options.bracketBegin === c || options.bracketEnd === c) {
|
|
97
|
-
section.push(c);
|
|
98
|
-
} else {
|
|
99
|
-
section.push(options.escapeChar);
|
|
100
|
-
section.push(c);
|
|
101
|
-
}
|
|
102
|
-
escaped = false;
|
|
103
|
-
} else if (options.bracketBegin === c) {
|
|
104
|
-
bracketBeginCharFound++;
|
|
105
|
-
section.push(c);
|
|
106
|
-
} else if (options.bracketEnd === c) {
|
|
107
|
-
if (bracketBeginCharFound === 0 && bracketBeginCharNum === 1) {
|
|
108
|
-
const jexlEngine = this.jexlEngineProvider.provide();
|
|
109
|
-
const expression = jexlEngine.createExpression(section.join(''));
|
|
110
|
-
let nextText;
|
|
111
|
-
if (i !== text.length - 1) {
|
|
112
|
-
nextText = text.substring(i + 1);
|
|
113
|
-
}
|
|
114
|
-
return { expression, nextText };
|
|
115
|
-
} else if (bracketBeginCharFound > 0) {
|
|
116
|
-
bracketBeginCharFound--;
|
|
117
|
-
section.push(c);
|
|
118
|
-
} else {
|
|
119
|
-
bracketBeginCharNum--;
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
section.push(c);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
protected nextString(text: string, options: Required<ExpressionCompilerOptions>): MiddleExpression {
|
|
128
|
-
let escaped = false;
|
|
129
|
-
let specialCharFound = false;
|
|
130
|
-
|
|
131
|
-
const section: string[] = [];
|
|
132
|
-
for (let i = 0; i < text.length; i++) {
|
|
133
|
-
const c = text[i];
|
|
134
|
-
if (!escaped) {
|
|
135
|
-
if ('\'' === c || '"' === c) {
|
|
136
|
-
section.push(c);
|
|
137
|
-
continue;
|
|
138
|
-
} else
|
|
139
|
-
if (c === options.escapeChar) {
|
|
140
|
-
escaped = true;
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (escaped) {
|
|
146
|
-
if (this.equalsSpecialChar(options, c) || options.bracketBegin === c || options.bracketEnd === c) {
|
|
147
|
-
section.push(c);
|
|
148
|
-
} else {
|
|
149
|
-
section.push(options.escapeChar);
|
|
150
|
-
section.push(c);
|
|
151
|
-
}
|
|
152
|
-
escaped = false;
|
|
153
|
-
} else
|
|
154
|
-
if (specialCharFound || options.ignoreSpecialChar) {
|
|
155
|
-
if (options.bracketBegin === c) {
|
|
156
|
-
const expression = section.join('');
|
|
157
|
-
const nextText = options.ignoreSpecialChar ? text.substring(i) : text.substring(i - 1);
|
|
158
|
-
return { expression, nextText };
|
|
159
|
-
} else {
|
|
160
|
-
if (!options.ignoreSpecialChar) {
|
|
161
|
-
specialCharFound = false;
|
|
162
|
-
section.push(options.specialChar);
|
|
163
|
-
}
|
|
164
|
-
section.push(c);
|
|
165
|
-
}
|
|
166
|
-
} else {
|
|
167
|
-
if (this.equalsSpecialChar(options, c)) {
|
|
168
|
-
specialCharFound = true;
|
|
169
|
-
} else {
|
|
170
|
-
section.push(c);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
12
|
+
protected readonly sectionCompiler = new ExpressionSectionCompiler(() => this.jexlEngineProvider.provide());
|
|
174
13
|
|
|
175
|
-
|
|
14
|
+
compileSections(text: string, options: ExpressionCompilerOptions = {}): any[] {
|
|
15
|
+
return this.sectionCompiler.compileSections(text, options);
|
|
176
16
|
}
|
|
177
17
|
|
|
178
18
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { ExpressionCompilerImpl } from './expression-compiler';
|
|
4
|
+
import { ExpressionHandlerImpl } from './expression-handler';
|
|
5
|
+
import { JexlEngineProviderImpl } from './jexl-engine-provider';
|
|
6
|
+
|
|
7
|
+
function handler(configuration?: Record<string, unknown>): ExpressionHandlerImpl {
|
|
8
|
+
const jexlEngineProvider = new JexlEngineProviderImpl();
|
|
9
|
+
const compiler = new ExpressionCompilerImpl();
|
|
10
|
+
Object.defineProperty(compiler, 'jexlEngineProvider', { value: jexlEngineProvider });
|
|
11
|
+
const instance = new ExpressionHandlerImpl();
|
|
12
|
+
Object.defineProperty(instance, 'jexlEngineProvider', { value: jexlEngineProvider });
|
|
13
|
+
Object.defineProperty(instance, 'expressionCompiler', { value: compiler });
|
|
14
|
+
Object.defineProperty(instance, 'expressionContextProvider', { value: { provide: () => configuration } });
|
|
15
|
+
return instance;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('ExpressionHandler', () => {
|
|
19
|
+
it('evaluates every string of nested objects and arrays in place', () => {
|
|
20
|
+
const ctx = { a: { b: 2 }, name: 'cell' };
|
|
21
|
+
const target = { x: '${a.b + 1}', list: ['${name}', 'plain', 7, { deep: 'hi ${name}' }], n: 1, t: true, nil: null };
|
|
22
|
+
const result = handler().handle(target, ctx);
|
|
23
|
+
expect(result).to.equal(target);
|
|
24
|
+
expect(target).to.deep.equal({ x: 3, list: ['cell', 'plain', 7, { deep: 'hi cell' }], n: 1, t: true, nil: null });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('leaves objects marked _ignoreEl alone', () => {
|
|
28
|
+
const ignored = { _ignoreEl: true, value: '${name}' };
|
|
29
|
+
const target = { ignored, other: '${name}' };
|
|
30
|
+
handler().handle(target, { name: 'cell' });
|
|
31
|
+
expect(target.ignored).to.equal(ignored);
|
|
32
|
+
expect(ignored.value).to.equal('${name}');
|
|
33
|
+
expect(target.other).to.equal('cell');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('does not walk into an object an expression evaluates to: values are not templates', () => {
|
|
37
|
+
const ctx = { obj: { greeting: 'hi ${name}' }, name: 'cell' };
|
|
38
|
+
const target = { value: '${obj}' };
|
|
39
|
+
handler().handle(target, ctx);
|
|
40
|
+
expect(target.value).to.deep.equal({ greeting: 'hi ${name}' });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('stops at circular references', () => {
|
|
44
|
+
const target: Record<string, unknown> = { name: '${n}' };
|
|
45
|
+
target.self = target;
|
|
46
|
+
target.list = [target];
|
|
47
|
+
handler().handle(target, { n: 'cell' });
|
|
48
|
+
expect(target.name).to.equal('cell');
|
|
49
|
+
expect(target.self).to.equal(target);
|
|
50
|
+
expect((target.list as unknown[])[0]).to.equal(target);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('does not assign unchanged values (frozen objects without expressions are fine)', () => {
|
|
54
|
+
const frozen = Object.freeze({ a: 1, b: 'text', c: Object.freeze([1, 2]) });
|
|
55
|
+
const target = { frozen, x: '${n}' };
|
|
56
|
+
handler().handle(target, { n: 2 });
|
|
57
|
+
expect(target.x).to.equal(2);
|
|
58
|
+
expect(target.frozen).to.equal(frozen);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('F6: values are text, not expressions', () => {
|
|
62
|
+
const secret = 'S3CRET';
|
|
63
|
+
const env = (x: string) => ({ X: x, _ignoreEl: true });
|
|
64
|
+
|
|
65
|
+
it('a value from the environment is not evaluated', () => {
|
|
66
|
+
const configuration = { cell: { secret, a: '${env.X}' }, env: env('${cell.secret}') };
|
|
67
|
+
expect(handler(configuration).handle('${cell.a}')).to.equal('${cell.secret}');
|
|
68
|
+
expect(configuration.cell.a).to.equal('${cell.secret}');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('nor through another key, in either order', () => {
|
|
72
|
+
const configuration = { cell: { b: '${cell.a}', secret, a: '${env.X}', c: 'x-${cell.a}' }, env: env('${cell.secret}') };
|
|
73
|
+
const h = handler(configuration);
|
|
74
|
+
expect(h.handle('${cell.b}')).to.equal('${cell.secret}');
|
|
75
|
+
expect(h.handle('${cell.c}')).to.equal('x-${cell.secret}');
|
|
76
|
+
expect(configuration.cell.c).to.equal('x-${cell.secret}');
|
|
77
|
+
expect(configuration.cell.b).to.equal('${cell.secret}');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('nor when it is part of a longer text', () => {
|
|
81
|
+
const configuration = { cell: { secret, url: 'http://${env.X}/path' }, env: env('${cell.secret}') };
|
|
82
|
+
expect(handler(configuration).handle('${cell.url}')).to.equal('http://${cell.secret}/path');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('a template variable is not evaluated either (prompt templates)', () => {
|
|
86
|
+
const text = handler().handle('hi {name}', { name: '{secret}', secret: 'S' }, { ignoreSpecialChar: true, bracketBegin: '{', bracketEnd: '}', ignoreContextExpression: true });
|
|
87
|
+
expect(text).to.equal('hi {secret}');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('keys still refer to each other in any order, through any depth', () => {
|
|
91
|
+
const configuration = { cell: { x: '${cell.y}-x', y: '${cell.deep.z}', deep: { z: 'z', n: '${cell.port * 2}' }, port: 21 } };
|
|
92
|
+
const h = handler(configuration);
|
|
93
|
+
expect(h.handle('${cell.x}')).to.equal('z-x');
|
|
94
|
+
expect(h.handle('${cell.deep.n}')).to.equal(42);
|
|
95
|
+
expect(configuration.cell.y).to.equal('z');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('a circular reference fails with the keys involved', () => {
|
|
99
|
+
const configuration = { cell: { a: '${cell.b}', b: '${cell.a}' } };
|
|
100
|
+
expect(() => handler(configuration).handle('${cell.a}')).to.throw('Circular reference in the configuration: cell.a -> cell.b -> cell.a');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('evaluating a value is still possible on purpose, with the eval transform', () => {
|
|
104
|
+
const instance = handler({ cell: { secret, a: '${env.X | eval}' }, env: env('${cell.secret}') });
|
|
105
|
+
const jexl = (instance as unknown as { jexlEngineProvider: { provide(): any } }).jexlEngineProvider.provide();
|
|
106
|
+
jexl.addTransform('eval', (text: string) => instance.handle(text));
|
|
107
|
+
expect(instance.handle('${cell.a}')).to.equal(secret);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { Component, Autowired } from '../annotation';
|
|
2
2
|
import { JexlEngineProvider, ExpressionHandler, ExpressionContextProvider, ExpressionCompiler, ExpressionContext, ExpressionCompilerOptions } from './expression-protocol';
|
|
3
|
-
import
|
|
3
|
+
import { ExpressionUtil } from './expression-util';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Evaluates `${...}` expressions with {@link ExpressionUtil}: the result of an expression is never
|
|
7
|
+
* evaluated again, references between configuration keys still resolve. Evaluate a text on purpose
|
|
8
|
+
* with the `eval` transform.
|
|
9
|
+
*/
|
|
5
10
|
@Component(ExpressionHandler)
|
|
6
11
|
export class ExpressionHandlerImpl implements ExpressionHandler {
|
|
7
12
|
|
|
@@ -21,65 +26,23 @@ export class ExpressionHandlerImpl implements ExpressionHandler {
|
|
|
21
26
|
|
|
22
27
|
if (!ctx && c !== this._ctx && !expressionCompilerOptions?.ignoreContextExpression) {
|
|
23
28
|
this._ctx = c;
|
|
24
|
-
|
|
29
|
+
ExpressionUtil.evaluateConfiguration(c, this.options(expressionCompilerOptions));
|
|
25
30
|
}
|
|
26
31
|
return c;
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
handle(textOrObj: string | Object, ctx?: ExpressionContext, expressionCompilerOptions?: ExpressionCompilerOptions): any {
|
|
35
|
+
const options = this.options(expressionCompilerOptions);
|
|
36
|
+
const context = () => this.getContext(ctx, expressionCompilerOptions);
|
|
30
37
|
if (typeof textOrObj === 'string') {
|
|
31
|
-
return
|
|
32
|
-
} else {
|
|
33
|
-
/* eslint-disable-next-line @typescript-eslint/no-this-alias */
|
|
34
|
-
const self = this;
|
|
35
|
-
traverse(textOrObj).forEach(function (value: any) {
|
|
36
|
-
if (typeof value === 'string') {
|
|
37
|
-
this.update(self.handle(value, ctx, expressionCompilerOptions));
|
|
38
|
-
} else if (value && (value as any)._ignoreEl === true) {
|
|
39
|
-
this.update(value, true);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
return textOrObj;
|
|
38
|
+
return ExpressionUtil.evaluateText(textOrObj, context, options);
|
|
43
39
|
}
|
|
40
|
+
ExpressionUtil.walk(textOrObj, text => ExpressionUtil.evaluateText(text, context, options));
|
|
41
|
+
return textOrObj;
|
|
44
42
|
}
|
|
45
43
|
|
|
46
|
-
protected
|
|
47
|
-
|
|
48
|
-
if (sections.length > 0) {
|
|
49
|
-
if (this.hasExpression(sections)) {
|
|
50
|
-
const c = this.getContext(ctx, expressionCompilerOptions);
|
|
51
|
-
if (sections.length === 1) {
|
|
52
|
-
let value = sections[0].evalSync(c);
|
|
53
|
-
if (typeof value === 'string') {
|
|
54
|
-
value = this.handle(value, c);
|
|
55
|
-
}
|
|
56
|
-
return value;
|
|
57
|
-
}
|
|
58
|
-
const result: string[] = [];
|
|
59
|
-
for (const section of sections) {
|
|
60
|
-
if (typeof section === 'string') {
|
|
61
|
-
result.push(section);
|
|
62
|
-
} else {
|
|
63
|
-
let value = section.evalSync(c);
|
|
64
|
-
if (typeof value === 'string') {
|
|
65
|
-
value = this.handle(value, c);
|
|
66
|
-
}
|
|
67
|
-
result.push(value);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return result.join('');
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return text;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
protected hasExpression(sections: any[]) {
|
|
77
|
-
for (const section of sections) {
|
|
78
|
-
if (typeof section !== 'string') {
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return false;
|
|
44
|
+
protected options(compilerOptions?: ExpressionCompilerOptions): ExpressionUtil.Options {
|
|
45
|
+
return { compiler: this.expressionCompiler, compilerOptions };
|
|
83
46
|
}
|
|
84
47
|
|
|
85
48
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { ExpressionError, ExpressionUtil } from './expression-util';
|
|
4
|
+
|
|
5
|
+
/** ExpressionUtil is the evaluation shared with @celljs/vite (`@celljs/core/el`): no container involved. */
|
|
6
|
+
describe('ExpressionUtil', () => {
|
|
7
|
+
it('evaluates a text against a context, keeping the type of a single expression', () => {
|
|
8
|
+
expect(ExpressionUtil.evaluateText('${a + 1}', { a: 1 })).to.equal(2);
|
|
9
|
+
expect(ExpressionUtil.evaluateText('x-${a}-y', { a: 1 })).to.equal('x-1-y');
|
|
10
|
+
expect(ExpressionUtil.evaluateText('plain', () => { throw new Error('not called'); })).to.equal('plain');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('does not evaluate what an expression returns', () => {
|
|
14
|
+
expect(ExpressionUtil.evaluateText('${v}', { v: '${secret}', secret: 's' })).to.equal('${secret}');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('evaluates a configuration in place: references in any order, env values are text', () => {
|
|
18
|
+
const config: Record<string, any> = {
|
|
19
|
+
env: { _ignoreEl: true, API_URL: '${cell.secret}' },
|
|
20
|
+
cell: { url: '${cell.base}/api', base: 'https://${cell.host}', host: 'example.com', api: '${env.API_URL}', copy: '${cell.api}', secret: 's' },
|
|
21
|
+
};
|
|
22
|
+
ExpressionUtil.evaluateConfiguration(config);
|
|
23
|
+
expect(config.cell).to.deep.equal({
|
|
24
|
+
url: 'https://example.com/api', base: 'https://example.com', host: 'example.com', api: '${cell.secret}', copy: '${cell.secret}', secret: 's',
|
|
25
|
+
});
|
|
26
|
+
expect(config.env.API_URL).to.equal('${cell.secret}');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('reports failures as an ExpressionError naming the key', () => {
|
|
30
|
+
const fail = (config: Record<string, any>) => {
|
|
31
|
+
try {
|
|
32
|
+
ExpressionUtil.evaluateConfiguration(config);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return error as ExpressionError;
|
|
35
|
+
}
|
|
36
|
+
throw new Error('no error');
|
|
37
|
+
};
|
|
38
|
+
const unknown = fail({ cell: { keys: "${ ['x'] | nothing }" } });
|
|
39
|
+
expect(unknown).to.be.instanceOf(ExpressionError);
|
|
40
|
+
expect(unknown.path).to.equal('cell.keys');
|
|
41
|
+
expect(unknown.message).to.contain('Cannot evaluate cell.keys');
|
|
42
|
+
const circular = fail({ a: '${b}', b: '${a}' });
|
|
43
|
+
expect(circular.message).to.equal('Circular reference in the configuration: a -> b -> a');
|
|
44
|
+
});
|
|
45
|
+
});
|