@celljs/core 4.0.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.
Files changed (173) hide show
  1. package/README.md +0 -6
  2. package/esm/common/application/application-protocol.js +1 -1
  3. package/esm/common/application/application-protocol.js.map +1 -1
  4. package/esm/common/config/config-provider.js +2 -1
  5. package/esm/common/config/config-provider.js.map +1 -1
  6. package/esm/common/container/auto-bind.js +26 -12
  7. package/esm/common/container/auto-bind.js.map +1 -1
  8. package/esm/common/el/expression-compiler.d.ts +4 -15
  9. package/esm/common/el/expression-compiler.js +6 -163
  10. package/esm/common/el/expression-compiler.js.map +1 -1
  11. package/esm/common/el/expression-handler.d.ts +7 -2
  12. package/esm/common/el/expression-handler.js +14 -53
  13. package/esm/common/el/expression-handler.js.map +1 -1
  14. package/esm/common/el/expression-util.d.ts +68 -0
  15. package/esm/common/el/expression-util.js +347 -0
  16. package/esm/common/el/expression-util.js.map +1 -0
  17. package/esm/common/el/index.d.ts +1 -0
  18. package/esm/common/el/index.js +1 -0
  19. package/esm/common/el/index.js.map +1 -1
  20. package/esm/common/logger/abstract-logger.d.ts +4 -2
  21. package/esm/common/logger/abstract-logger.js +11 -6
  22. package/esm/common/logger/abstract-logger.js.map +1 -1
  23. package/esm/common/logger/logger-protocol.d.ts +4 -0
  24. package/esm/common/logger/logger-protocol.js +2 -0
  25. package/esm/common/logger/logger-protocol.js.map +1 -1
  26. package/esm/common/logger/logger.js +7 -7
  27. package/esm/common/logger/logger.js.map +1 -1
  28. package/esm/common/test/mock-logger.d.ts +1 -0
  29. package/esm/common/test/mock-logger.js +3 -0
  30. package/esm/common/test/mock-logger.js.map +1 -1
  31. package/esm/common/utils/emitter.d.ts +1 -10
  32. package/esm/common/utils/emitter.js +0 -56
  33. package/esm/common/utils/emitter.js.map +1 -1
  34. package/esm/common/utils/mime-type-util.js +1 -1
  35. package/esm/common/utils/mime-type-util.js.map +1 -1
  36. package/esm/common/utils/mime-type.js +1 -1
  37. package/esm/common/utils/mime-type.js.map +1 -1
  38. package/esm/node/context/context.d.ts +11 -2
  39. package/esm/node/context/context.js +36 -12
  40. package/esm/node/context/context.js.map +1 -1
  41. package/esm/node/index.d.ts +0 -1
  42. package/esm/node/index.js +0 -1
  43. package/esm/node/index.js.map +1 -1
  44. package/lib/common/aop/aop.spec.d.ts +2 -0
  45. package/lib/common/aop/aop.spec.d.ts.map +1 -0
  46. package/lib/common/aop/aop.spec.js +170 -0
  47. package/lib/common/aop/aop.spec.js.map +1 -0
  48. package/lib/common/application/application-protocol.js +1 -1
  49. package/lib/common/application/application-protocol.js.map +1 -1
  50. package/lib/common/application/lifecycle.spec.d.ts +2 -0
  51. package/lib/common/application/lifecycle.spec.d.ts.map +1 -0
  52. package/lib/common/application/lifecycle.spec.js +155 -0
  53. package/lib/common/application/lifecycle.spec.js.map +1 -0
  54. package/lib/common/config/config-provider.js +2 -1
  55. package/lib/common/config/config-provider.js.map +1 -1
  56. package/lib/common/container/assembly.spec.d.ts +2 -0
  57. package/lib/common/container/assembly.spec.d.ts.map +1 -0
  58. package/lib/common/container/assembly.spec.js +464 -0
  59. package/lib/common/container/assembly.spec.js.map +1 -0
  60. package/lib/common/container/auto-bind.js +26 -12
  61. package/lib/common/container/auto-bind.js.map +1 -1
  62. package/lib/common/el/expression-compiler.d.ts +4 -15
  63. package/lib/common/el/expression-compiler.d.ts.map +1 -1
  64. package/lib/common/el/expression-compiler.js +6 -163
  65. package/lib/common/el/expression-compiler.js.map +1 -1
  66. package/lib/common/el/expression-handler.d.ts +7 -2
  67. package/lib/common/el/expression-handler.d.ts.map +1 -1
  68. package/lib/common/el/expression-handler.js +14 -56
  69. package/lib/common/el/expression-handler.js.map +1 -1
  70. package/lib/common/el/expression-handler.spec.d.ts +2 -0
  71. package/lib/common/el/expression-handler.spec.d.ts.map +1 -0
  72. package/lib/common/el/expression-handler.spec.js +99 -0
  73. package/lib/common/el/expression-handler.spec.js.map +1 -0
  74. package/lib/common/el/expression-util.d.ts +69 -0
  75. package/lib/common/el/expression-util.d.ts.map +1 -0
  76. package/lib/common/el/expression-util.js +352 -0
  77. package/lib/common/el/expression-util.js.map +1 -0
  78. package/lib/common/el/expression-util.spec.d.ts +2 -0
  79. package/lib/common/el/expression-util.spec.d.ts.map +1 -0
  80. package/lib/common/el/expression-util.spec.js +45 -0
  81. package/lib/common/el/expression-util.spec.js.map +1 -0
  82. package/lib/common/el/index.d.ts +1 -0
  83. package/lib/common/el/index.d.ts.map +1 -1
  84. package/lib/common/el/index.js +1 -0
  85. package/lib/common/el/index.js.map +1 -1
  86. package/lib/common/el/value-and-el.spec.d.ts +2 -0
  87. package/lib/common/el/value-and-el.spec.d.ts.map +1 -0
  88. package/lib/common/el/value-and-el.spec.js +216 -0
  89. package/lib/common/el/value-and-el.spec.js.map +1 -0
  90. package/lib/common/logger/abstract-logger.d.ts +4 -2
  91. package/lib/common/logger/abstract-logger.d.ts.map +1 -1
  92. package/lib/common/logger/abstract-logger.js +10 -5
  93. package/lib/common/logger/abstract-logger.js.map +1 -1
  94. package/lib/common/logger/logger-protocol.d.ts +4 -0
  95. package/lib/common/logger/logger-protocol.d.ts.map +1 -1
  96. package/lib/common/logger/logger-protocol.js +3 -1
  97. package/lib/common/logger/logger-protocol.js.map +1 -1
  98. package/lib/common/logger/logger.js +7 -7
  99. package/lib/common/logger/logger.js.map +1 -1
  100. package/lib/common/logger/logger.spec.d.ts +2 -0
  101. package/lib/common/logger/logger.spec.d.ts.map +1 -0
  102. package/lib/common/logger/logger.spec.js +57 -0
  103. package/lib/common/logger/logger.spec.js.map +1 -0
  104. package/lib/common/test/mock-logger.d.ts +1 -0
  105. package/lib/common/test/mock-logger.d.ts.map +1 -1
  106. package/lib/common/test/mock-logger.js +3 -0
  107. package/lib/common/test/mock-logger.js.map +1 -1
  108. package/lib/common/utils/emitter.d.ts +1 -10
  109. package/lib/common/utils/emitter.d.ts.map +1 -1
  110. package/lib/common/utils/emitter.js +1 -58
  111. package/lib/common/utils/emitter.js.map +1 -1
  112. package/lib/common/utils/mime-type-util.js +1 -1
  113. package/lib/common/utils/mime-type-util.js.map +1 -1
  114. package/lib/common/utils/mime-type.js +1 -1
  115. package/lib/common/utils/mime-type.js.map +1 -1
  116. package/lib/common/utils/utils-behavior.spec.d.ts +2 -0
  117. package/lib/common/utils/utils-behavior.spec.d.ts.map +1 -0
  118. package/lib/common/utils/utils-behavior.spec.js +232 -0
  119. package/lib/common/utils/utils-behavior.spec.js.map +1 -0
  120. package/lib/node/context/context.d.ts +11 -2
  121. package/lib/node/context/context.d.ts.map +1 -1
  122. package/lib/node/context/context.js +36 -10
  123. package/lib/node/context/context.js.map +1 -1
  124. package/lib/node/context/context.spec.d.ts +2 -0
  125. package/lib/node/context/context.spec.d.ts.map +1 -0
  126. package/lib/node/context/context.spec.js +202 -0
  127. package/lib/node/context/context.spec.js.map +1 -0
  128. package/lib/node/index.d.ts +0 -1
  129. package/lib/node/index.d.ts.map +1 -1
  130. package/lib/node/index.js +0 -1
  131. package/lib/node/index.js.map +1 -1
  132. package/package.json +7 -17
  133. package/src/common/aop/aop.spec.ts +145 -0
  134. package/src/common/application/application-protocol.ts +1 -1
  135. package/src/common/application/lifecycle.spec.ts +148 -0
  136. package/src/common/config/config-provider.ts +1 -1
  137. package/src/common/container/assembly.spec.ts +293 -0
  138. package/src/common/container/auto-bind.ts +23 -10
  139. package/src/common/el/expression-compiler.ts +6 -166
  140. package/src/common/el/expression-handler.spec.ts +110 -0
  141. package/src/common/el/expression-handler.ts +14 -51
  142. package/src/common/el/expression-util.spec.ts +45 -0
  143. package/src/common/el/expression-util.ts +363 -0
  144. package/src/common/el/index.ts +2 -1
  145. package/src/common/el/value-and-el.spec.ts +186 -0
  146. package/src/common/logger/abstract-logger.ts +12 -6
  147. package/src/common/logger/logger-protocol.ts +5 -0
  148. package/src/common/logger/logger.spec.ts +61 -0
  149. package/src/common/logger/logger.ts +7 -7
  150. package/src/common/test/mock-logger.ts +3 -0
  151. package/src/common/utils/emitter.ts +1 -63
  152. package/src/common/utils/mime-type-util.ts +1 -1
  153. package/src/common/utils/mime-type.ts +1 -1
  154. package/src/common/utils/utils-behavior.spec.ts +259 -0
  155. package/src/node/context/context.spec.ts +219 -0
  156. package/src/node/context/context.ts +35 -10
  157. package/src/node/index.ts +0 -1
  158. package/esm/node/utils/crc.d.ts +0 -1
  159. package/esm/node/utils/crc.js +0 -7
  160. package/esm/node/utils/crc.js.map +0 -1
  161. package/esm/node/utils/index.d.ts +0 -1
  162. package/esm/node/utils/index.js +0 -2
  163. package/esm/node/utils/index.js.map +0 -1
  164. package/lib/node/utils/crc.d.ts +0 -2
  165. package/lib/node/utils/crc.d.ts.map +0 -1
  166. package/lib/node/utils/crc.js +0 -10
  167. package/lib/node/utils/crc.js.map +0 -1
  168. package/lib/node/utils/index.d.ts +0 -2
  169. package/lib/node/utils/index.d.ts.map +0 -1
  170. package/lib/node/utils/index.js +0 -18
  171. package/lib/node/utils/index.js.map +0 -1
  172. package/src/node/utils/crc.ts +0 -7
  173. package/src/node/utils/index.ts +0 -1
@@ -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 traverse from 'traverse';
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
- this.handle(c, c);
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 this.doHandle(textOrObj, ctx, expressionCompilerOptions);
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 doHandle(text: string, ctx?: ExpressionContext, expressionCompilerOptions?: ExpressionCompilerOptions): any {
47
- const sections = this.expressionCompiler.compileSections(text, expressionCompilerOptions);
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
+ });
@@ -0,0 +1,363 @@
1
+ import { Jexl } from 'jexl';
2
+ import { ExpressionCompiler, ExpressionCompilerOptions, ExpressionContext } from './expression-protocol';
3
+
4
+ interface MiddleExpression {
5
+ expression: any;
6
+ nextText?: string;
7
+ }
8
+
9
+ /**
10
+ * Splits a text into literal sections and compiled `${...}` expressions (jexl). It has no dependency
11
+ * injection, so the build (@celljs/vite) and the runtime share it through `@celljs/core/el`.
12
+ */
13
+ export class ExpressionSectionCompiler implements ExpressionCompiler {
14
+
15
+ protected static jexlEngine?: any;
16
+
17
+ /** @param jexl the engine; the runtime passes the one of its JexlEngineProvider, which has the transforms */
18
+ constructor(protected readonly jexl: () => any = () => (ExpressionSectionCompiler.jexlEngine ??= new Jexl())) { }
19
+
20
+ private ESCAPE_CHAR = '\\';
21
+ private SPECIAL_CHAR = '$';
22
+ private BRACKET_BEGIN = '{';
23
+ private BRACKET_END = '}';
24
+
25
+ protected getSpecialChar(opitons: Required<ExpressionCompilerOptions>): string {
26
+ if (opitons.ignoreSpecialChar) {
27
+ return '';
28
+ }
29
+ return opitons.specialChar;
30
+ }
31
+
32
+ protected equalsSpecialChar(opitons: Required<ExpressionCompilerOptions>, char: string): boolean {
33
+ if (opitons.ignoreSpecialChar) {
34
+ return false;
35
+ }
36
+ return char === opitons.specialChar;
37
+ }
38
+
39
+ compileSections(text: string, options: ExpressionCompilerOptions): any[] {
40
+ const merged = {
41
+ ...{ escapeChar: this.ESCAPE_CHAR, specialChar: this.SPECIAL_CHAR, bracketBegin: this.BRACKET_BEGIN, bracketEnd: this.BRACKET_END, ...options }
42
+ } as Required<ExpressionCompilerOptions>;
43
+ if (!text || text.indexOf(`${this.getSpecialChar(merged)}${merged.bracketBegin}`) < 0) {
44
+ return [];
45
+ }
46
+
47
+ const sections: any[] = [];
48
+ let middleText: string | undefined = text;
49
+ while (middleText) {
50
+ const me: MiddleExpression | undefined = this.middleCompile(middleText, merged);
51
+ if (!me) {
52
+ sections.push(middleText);
53
+ middleText = undefined;
54
+ } else {
55
+ sections.push(me.expression);
56
+ middleText = me.nextText;
57
+ }
58
+ }
59
+
60
+ return sections;
61
+ }
62
+
63
+ protected middleCompile(text: string, options: Required<ExpressionCompilerOptions>): MiddleExpression | undefined {
64
+ let me: MiddleExpression | undefined;
65
+ const prefix = `${this.getSpecialChar(options)}${options.bracketBegin}`;
66
+ const prefix2 = `${prefix}${options.bracketBegin}`;
67
+ if (text.startsWith(prefix2)) {
68
+ me = this.nextMiddleExpression(text.substring(prefix2.length), 2, options);
69
+ } else if (text.startsWith(prefix)) {
70
+ me = this.nextMiddleExpression(text.substring(prefix.length), undefined, options);
71
+ } else {
72
+ me = this.nextString(text, options);
73
+ }
74
+ return me;
75
+ }
76
+
77
+ protected nextMiddleExpression(text: string, bracketBeginCharNum = 1, options: Required<ExpressionCompilerOptions>): MiddleExpression | undefined {
78
+ let stringed = false;
79
+ let escaped = false;
80
+ let bracketBeginCharFound = 0;
81
+
82
+ const section: string[] = [];
83
+ for (let i = 0; i < text.length; i++) {
84
+ const c = text[i];
85
+ if (!escaped) {
86
+ if ('\'' === c || '"' === c) {
87
+ stringed = !stringed;
88
+ section.push(c);
89
+ continue;
90
+ } else
91
+ if (c === options.escapeChar) {
92
+ escaped = true;
93
+ continue;
94
+ }
95
+ }
96
+
97
+ if (stringed) {
98
+ section.push(c);
99
+ escaped = false;
100
+ } else if (escaped) {
101
+ if (this.equalsSpecialChar(options, c) || options.bracketBegin === c || options.bracketEnd === c) {
102
+ section.push(c);
103
+ } else {
104
+ section.push(options.escapeChar);
105
+ section.push(c);
106
+ }
107
+ escaped = false;
108
+ } else if (options.bracketBegin === c) {
109
+ bracketBeginCharFound++;
110
+ section.push(c);
111
+ } else if (options.bracketEnd === c) {
112
+ if (bracketBeginCharFound === 0 && bracketBeginCharNum === 1) {
113
+ const jexlEngine = this.jexl();
114
+ const expression = jexlEngine.createExpression(section.join(''));
115
+ let nextText;
116
+ if (i !== text.length - 1) {
117
+ nextText = text.substring(i + 1);
118
+ }
119
+ return { expression, nextText };
120
+ } else if (bracketBeginCharFound > 0) {
121
+ bracketBeginCharFound--;
122
+ section.push(c);
123
+ } else {
124
+ bracketBeginCharNum--;
125
+ }
126
+ } else {
127
+ section.push(c);
128
+ }
129
+ }
130
+ }
131
+
132
+ protected nextString(text: string, options: Required<ExpressionCompilerOptions>): MiddleExpression {
133
+ let escaped = false;
134
+ let specialCharFound = false;
135
+
136
+ const section: string[] = [];
137
+ for (let i = 0; i < text.length; i++) {
138
+ const c = text[i];
139
+ if (!escaped) {
140
+ if ('\'' === c || '"' === c) {
141
+ section.push(c);
142
+ continue;
143
+ } else
144
+ if (c === options.escapeChar) {
145
+ escaped = true;
146
+ continue;
147
+ }
148
+ }
149
+
150
+ if (escaped) {
151
+ if (this.equalsSpecialChar(options, c) || options.bracketBegin === c || options.bracketEnd === c) {
152
+ section.push(c);
153
+ } else {
154
+ section.push(options.escapeChar);
155
+ section.push(c);
156
+ }
157
+ escaped = false;
158
+ } else
159
+ if (specialCharFound || options.ignoreSpecialChar) {
160
+ if (options.bracketBegin === c) {
161
+ const expression = section.join('');
162
+ const nextText = options.ignoreSpecialChar ? text.substring(i) : text.substring(i - 1);
163
+ return { expression, nextText };
164
+ } else {
165
+ if (!options.ignoreSpecialChar) {
166
+ specialCharFound = false;
167
+ section.push(options.specialChar);
168
+ }
169
+ section.push(c);
170
+ }
171
+ } else {
172
+ if (this.equalsSpecialChar(options, c)) {
173
+ specialCharFound = true;
174
+ } else {
175
+ section.push(c);
176
+ }
177
+ }
178
+ }
179
+
180
+ return { expression: section.join('') };
181
+ }
182
+
183
+ }
184
+
185
+ /** An expression of the configuration that cannot be evaluated; `path` is its key, e.g. `cell.db.url`. */
186
+ export class ExpressionError extends Error {
187
+
188
+ override readonly name = 'ExpressionError';
189
+
190
+ constructor(readonly path: string, readonly template: string, message: string, override readonly cause?: unknown) {
191
+ super(message);
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Evaluation of `${...}` expressions, shared by the runtime (ExpressionHandler) and the build
197
+ * (@celljs/vite) through `@celljs/core/el`. The result of an expression is never evaluated again: a value
198
+ * that comes from the environment (`${env.X}`), from a template variable or from another expression is
199
+ * text, whatever it contains. References between configuration keys still resolve: a key that refers to
200
+ * another one evaluates that key's own template (see {@link ExpressionUtil.evaluateConfiguration}).
201
+ */
202
+ export namespace ExpressionUtil {
203
+
204
+ export interface Options {
205
+ /** Defaults to an {@link ExpressionSectionCompiler} on a plain jexl engine (no transforms). */
206
+ compiler?: ExpressionCompiler;
207
+ compilerOptions?: ExpressionCompilerOptions;
208
+ }
209
+
210
+ let defaultCompiler: ExpressionCompiler | undefined;
211
+
212
+ function compilerOf(options: Options): ExpressionCompiler {
213
+ return options.compiler ?? (defaultCompiler ??= new ExpressionSectionCompiler());
214
+ }
215
+
216
+ /**
217
+ * Evaluates the expressions of `text`: a text with a single expression keeps the type of its value,
218
+ * mixed text is joined into a string. `context` may be a function, then it is only called when the text
219
+ * has an expression.
220
+ */
221
+ export function evaluateText(text: string, context: ExpressionContext | (() => ExpressionContext), options: Options = {}): any {
222
+ const sections = compilerOf(options).compileSections(text, options.compilerOptions);
223
+ if (!sections.some(section => typeof section !== 'string')) {
224
+ return text;
225
+ }
226
+ const c = typeof context === 'function' ? context() : context;
227
+ if (sections.length === 1) {
228
+ return sections[0].evalSync(c);
229
+ }
230
+ return sections.map(section => (typeof section === 'string' ? section : section.evalSync(c))).join('');
231
+ }
232
+
233
+ /**
234
+ * Replaces the templates (strings) of `node` by `evaluate`'s values, in place, depth first. Objects
235
+ * marked `_ignoreEl` are left alone, circular references are not followed, and values are not walked
236
+ * into: what an expression returns is not a template.
237
+ */
238
+ export function walk(node: any, evaluate: (text: string, owner: any, key: string) => any,
239
+ enter?: (child: object, owner: object, key: string) => void): void {
240
+ doWalk(node, [], evaluate, enter);
241
+ }
242
+
243
+ function doWalk(node: any, ancestors: object[], evaluate: (text: string, owner: any, key: string) => any,
244
+ enter?: (child: object, owner: object, key: string) => void): void {
245
+ if (typeof node !== 'object' || node === null || node._ignoreEl === true || ancestors.includes(node)) {
246
+ return;
247
+ }
248
+ ancestors.push(node);
249
+ for (const key of Object.keys(node)) {
250
+ const child = node[key];
251
+ if (typeof child === 'string') {
252
+ const value = evaluate(child, node, key);
253
+ if (value !== child) {
254
+ node[key] = value;
255
+ }
256
+ } else {
257
+ if (enter && typeof child === 'object' && child !== null) {
258
+ enter(child, node, key);
259
+ }
260
+ doWalk(child, ancestors, evaluate, enter);
261
+ }
262
+ }
263
+ ancestors.pop();
264
+ }
265
+
266
+ /**
267
+ * Evaluates the configuration `root` in place. An expression sees the configuration through a view in
268
+ * which every template that it reaches is evaluated first (once, cached), so keys refer to each other in
269
+ * any order; a value an expression returns is never evaluated again. Objects marked `_ignoreEl` (such
270
+ * as `env`) are read as they are. Failures, including a circular reference, are {@link ExpressionError}s
271
+ * naming the key.
272
+ */
273
+ export function evaluateConfiguration(root: ExpressionContext, options: Options = {}): void {
274
+ const values = new Map<object, Map<string, unknown>>();
275
+ const resolving: string[] = [];
276
+ const paths = new WeakMap<object, string>([[root, '']]);
277
+ const views = new WeakMap<object, object>();
278
+ const targets = new WeakMap<object, object>();
279
+
280
+ const pathOf = (owner: object, key: string) => {
281
+ const base = paths.get(owner);
282
+ return base ? `${base}.${key}` : key;
283
+ };
284
+ const unwrap = (value: any): any => {
285
+ if (typeof value !== 'object' || value === null) {
286
+ return value;
287
+ }
288
+ const target = targets.get(value);
289
+ if (target) {
290
+ return target;
291
+ }
292
+ // Arrays and objects an expression builds may hold views of the configuration.
293
+ if (Array.isArray(value)) {
294
+ return value.map(unwrap);
295
+ }
296
+ if (Object.getPrototypeOf(value) === Object.prototype) {
297
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, unwrap(v)]));
298
+ }
299
+ return value;
300
+ };
301
+ const valueOf = (owner: any, key: string): unknown => {
302
+ const cached = values.get(owner);
303
+ if (cached?.has(key)) {
304
+ return cached.get(key);
305
+ }
306
+ const template = owner[key];
307
+ const path = pathOf(owner, key);
308
+ if (resolving.includes(path)) {
309
+ throw new ExpressionError(path, template, `Circular reference in the configuration: ${[...resolving, path].join(' -> ')}`);
310
+ }
311
+ resolving.push(path);
312
+ let value: unknown;
313
+ try {
314
+ value = unwrap(evaluateText(template, () => view(root), options));
315
+ } catch (error) {
316
+ if (error instanceof ExpressionError) {
317
+ throw error;
318
+ }
319
+ throw new ExpressionError(path, template, `Cannot evaluate ${path}: ${template} (${(error as Error)?.message ?? error})`, error);
320
+ } finally {
321
+ resolving.pop();
322
+ }
323
+ if (!values.has(owner)) {
324
+ values.set(owner, new Map());
325
+ }
326
+ values.get(owner)!.set(key, value);
327
+ return value;
328
+ };
329
+ const view = (target: any): any => {
330
+ if (typeof target !== 'object' || target === null || target._ignoreEl === true) {
331
+ return target;
332
+ }
333
+ let proxy = views.get(target);
334
+ if (!proxy) {
335
+ const created: object = new Proxy(target, {
336
+ get: (t, key, receiver) => {
337
+ const value = Reflect.get(t, key, receiver);
338
+ if (typeof key !== 'string' || !Object.prototype.hasOwnProperty.call(t, key)) {
339
+ return value;
340
+ }
341
+ if (typeof value === 'string') {
342
+ return valueOf(t, key);
343
+ }
344
+ if (typeof value === 'object' && value !== null && !paths.has(value)) {
345
+ paths.set(value, pathOf(t, key));
346
+ }
347
+ return view(value);
348
+ }
349
+ });
350
+ views.set(target, created);
351
+ targets.set(created, target);
352
+ proxy = created;
353
+ }
354
+ return proxy;
355
+ };
356
+
357
+ walk(root, (text, owner, key) => valueOf(owner, key), (child, owner, key) => {
358
+ if (!paths.has(child)) {
359
+ paths.set(child, pathOf(owner, key));
360
+ }
361
+ });
362
+ }
363
+ }
@@ -2,5 +2,6 @@ export * from './expression-protocol';
2
2
  export * from './expression-compiler';
3
3
  export * from './expression-context-provider';
4
4
  export * from './expression-handler';
5
+ export * from './expression-util';
5
6
  export * from './jexl-engine-provider';
6
- export * from './core-context-initializer';
7
+ export * from './core-context-initializer';