@celljs/core 4.3.0 → 4.4.1

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 (239) hide show
  1. package/README.md +48 -20
  2. package/cell.schema.json +84 -0
  3. package/cell.yml +13 -12
  4. package/esm/browser/application/frontend-application.d.ts +2 -2
  5. package/esm/browser/application/frontend-application.js +6 -25
  6. package/esm/browser/application/frontend-application.js.map +1 -1
  7. package/esm/common/annotation/component.js +1 -2
  8. package/esm/common/annotation/component.js.map +1 -1
  9. package/esm/common/annotation/service.js +2 -1
  10. package/esm/common/annotation/service.js.map +1 -1
  11. package/esm/common/application/application-entry.js +4 -0
  12. package/esm/common/application/application-entry.js.map +1 -1
  13. package/esm/common/application/application-protocol.d.ts +9 -3
  14. package/esm/common/application/application-protocol.js +19 -7
  15. package/esm/common/application/application-protocol.js.map +1 -1
  16. package/esm/common/application/application.js +1 -1
  17. package/esm/common/application/application.js.map +1 -1
  18. package/esm/common/config/config-util.d.ts +3 -0
  19. package/esm/common/config/config-util.js +6 -1
  20. package/esm/common/config/config-util.js.map +1 -1
  21. package/esm/common/config/dynamic-config.d.ts +3 -0
  22. package/esm/common/config/dynamic-config.js +3 -1
  23. package/esm/common/config/dynamic-config.js.map +1 -1
  24. package/esm/common/config/env-config-util.d.ts +32 -26
  25. package/esm/common/config/env-config-util.js +84 -40
  26. package/esm/common/config/env-config-util.js.map +1 -1
  27. package/esm/common/constants.d.ts +1 -1
  28. package/esm/common/constants.js +1 -1
  29. package/esm/common/constants.js.map +1 -1
  30. package/esm/common/container/auto-bind.js +2 -2
  31. package/esm/common/container/auto-bind.js.map +1 -1
  32. package/esm/common/container/container.js +3 -3
  33. package/esm/common/container/container.js.map +1 -1
  34. package/esm/common/el/core-context-initializer.js +3 -1
  35. package/esm/common/el/core-context-initializer.js.map +1 -1
  36. package/esm/common/el/expression-engine.d.ts +3 -3
  37. package/esm/common/el/expression-engine.js +18 -9
  38. package/esm/common/el/expression-engine.js.map +1 -1
  39. package/esm/common/el/expression-handler.js +2 -1
  40. package/esm/common/el/expression-handler.js.map +1 -1
  41. package/esm/common/el/expression-util.d.ts +17 -4
  42. package/esm/common/el/expression-util.js +28 -9
  43. package/esm/common/el/expression-util.js.map +1 -1
  44. package/esm/common/index.d.ts +1 -0
  45. package/esm/common/index.js +1 -0
  46. package/esm/common/index.js.map +1 -1
  47. package/esm/common/logger/abstract-logger.d.ts +2 -1
  48. package/esm/common/logger/abstract-logger.js +28 -17
  49. package/esm/common/logger/abstract-logger.js.map +1 -1
  50. package/esm/common/logger/logger-protocol.d.ts +2 -2
  51. package/esm/common/logger/logger-protocol.js +1 -1
  52. package/esm/common/logger/logger-protocol.js.map +1 -1
  53. package/esm/common/logger/logger.js +11 -4
  54. package/esm/common/logger/logger.js.map +1 -1
  55. package/esm/common/security-check/index.d.ts +2 -0
  56. package/esm/common/security-check/index.js +3 -0
  57. package/esm/common/security-check/index.js.map +1 -0
  58. package/esm/common/security-check/security-check-protocol.d.ts +40 -0
  59. package/esm/common/security-check/security-check-protocol.js +24 -0
  60. package/esm/common/security-check/security-check-protocol.js.map +1 -0
  61. package/esm/common/security-check/security-checker.d.ts +14 -0
  62. package/esm/common/security-check/security-checker.js +61 -0
  63. package/esm/common/security-check/security-checker.js.map +1 -0
  64. package/esm/common/utils/emitter.d.ts +2 -0
  65. package/esm/common/utils/emitter.js +4 -0
  66. package/esm/common/utils/emitter.js.map +1 -1
  67. package/esm/common/utils/error-util.d.ts +8 -0
  68. package/esm/common/utils/error-util.js +24 -0
  69. package/esm/common/utils/error-util.js.map +1 -1
  70. package/esm/common/utils/event.d.ts +6 -1
  71. package/esm/common/utils/event.js +20 -6
  72. package/esm/common/utils/event.js.map +1 -1
  73. package/esm/common/utils/uuid.d.ts +1 -1
  74. package/esm/common/utils/uuid.js +1 -1
  75. package/esm/common/utils/uuid.js.map +1 -1
  76. package/esm/node/application/backend-application.js +1 -1
  77. package/esm/node/application/backend-application.js.map +1 -1
  78. package/esm/node/context/context.js +3 -3
  79. package/esm/node/context/context.js.map +1 -1
  80. package/lib/browser/application/frontend-application.d.ts +2 -2
  81. package/lib/browser/application/frontend-application.d.ts.map +1 -1
  82. package/lib/browser/application/frontend-application.js +6 -25
  83. package/lib/browser/application/frontend-application.js.map +1 -1
  84. package/lib/common/annotation/component.js +1 -2
  85. package/lib/common/annotation/component.js.map +1 -1
  86. package/lib/common/annotation/service.d.ts.map +1 -1
  87. package/lib/common/annotation/service.js +2 -1
  88. package/lib/common/annotation/service.js.map +1 -1
  89. package/lib/common/annotation/service.spec.d.ts +2 -0
  90. package/lib/common/annotation/service.spec.d.ts.map +1 -0
  91. package/lib/common/annotation/service.spec.js +43 -0
  92. package/lib/common/annotation/service.spec.js.map +1 -0
  93. package/lib/common/aop/aop.spec.js +4 -4
  94. package/lib/common/aop/aop.spec.js.map +1 -1
  95. package/lib/common/application/application-entry.js +4 -0
  96. package/lib/common/application/application-entry.js.map +1 -1
  97. package/lib/common/application/application-protocol.d.ts +9 -3
  98. package/lib/common/application/application-protocol.d.ts.map +1 -1
  99. package/lib/common/application/application-protocol.js +19 -7
  100. package/lib/common/application/application-protocol.js.map +1 -1
  101. package/lib/common/application/application.js +1 -1
  102. package/lib/common/application/application.js.map +1 -1
  103. package/lib/common/application/lifecycle.spec.js +54 -7
  104. package/lib/common/application/lifecycle.spec.js.map +1 -1
  105. package/lib/common/config/config-util.d.ts +3 -0
  106. package/lib/common/config/config-util.d.ts.map +1 -1
  107. package/lib/common/config/config-util.js +5 -0
  108. package/lib/common/config/config-util.js.map +1 -1
  109. package/lib/common/config/dynamic-config.d.ts +3 -0
  110. package/lib/common/config/dynamic-config.d.ts.map +1 -1
  111. package/lib/common/config/dynamic-config.js +4 -2
  112. package/lib/common/config/dynamic-config.js.map +1 -1
  113. package/lib/common/config/env-config-util.d.ts +32 -26
  114. package/lib/common/config/env-config-util.d.ts.map +1 -1
  115. package/lib/common/config/env-config-util.js +84 -40
  116. package/lib/common/config/env-config-util.js.map +1 -1
  117. package/lib/common/config/env-config-util.spec.js +148 -132
  118. package/lib/common/config/env-config-util.spec.js.map +1 -1
  119. package/lib/common/constants.d.ts +1 -1
  120. package/lib/common/constants.d.ts.map +1 -1
  121. package/lib/common/constants.js +1 -1
  122. package/lib/common/constants.js.map +1 -1
  123. package/lib/common/container/auto-bind.js +2 -2
  124. package/lib/common/container/auto-bind.js.map +1 -1
  125. package/lib/common/container/container.js +3 -3
  126. package/lib/common/container/container.js.map +1 -1
  127. package/lib/common/el/core-context-initializer.d.ts.map +1 -1
  128. package/lib/common/el/core-context-initializer.js +3 -1
  129. package/lib/common/el/core-context-initializer.js.map +1 -1
  130. package/lib/common/el/expression-engine.d.ts +3 -3
  131. package/lib/common/el/expression-engine.js +18 -9
  132. package/lib/common/el/expression-engine.js.map +1 -1
  133. package/lib/common/el/expression-engine.spec.js +15 -7
  134. package/lib/common/el/expression-engine.spec.js.map +1 -1
  135. package/lib/common/el/expression-handler.d.ts.map +1 -1
  136. package/lib/common/el/expression-handler.js +2 -1
  137. package/lib/common/el/expression-handler.js.map +1 -1
  138. package/lib/common/el/expression-util.d.ts +17 -4
  139. package/lib/common/el/expression-util.d.ts.map +1 -1
  140. package/lib/common/el/expression-util.js +28 -10
  141. package/lib/common/el/expression-util.js.map +1 -1
  142. package/lib/common/index.d.ts +1 -0
  143. package/lib/common/index.d.ts.map +1 -1
  144. package/lib/common/index.js +1 -0
  145. package/lib/common/index.js.map +1 -1
  146. package/lib/common/logger/abstract-logger.d.ts +2 -1
  147. package/lib/common/logger/abstract-logger.d.ts.map +1 -1
  148. package/lib/common/logger/abstract-logger.js +28 -17
  149. package/lib/common/logger/abstract-logger.js.map +1 -1
  150. package/lib/common/logger/logger-protocol.d.ts +2 -2
  151. package/lib/common/logger/logger-protocol.d.ts.map +1 -1
  152. package/lib/common/logger/logger-protocol.js +1 -1
  153. package/lib/common/logger/logger-protocol.js.map +1 -1
  154. package/lib/common/logger/logger.d.ts.map +1 -1
  155. package/lib/common/logger/logger.js +11 -4
  156. package/lib/common/logger/logger.js.map +1 -1
  157. package/lib/common/logger/logger.spec.js +35 -0
  158. package/lib/common/logger/logger.spec.js.map +1 -1
  159. package/lib/common/security-check/index.d.ts +3 -0
  160. package/lib/common/security-check/index.d.ts.map +1 -0
  161. package/lib/common/security-check/index.js +19 -0
  162. package/lib/common/security-check/index.js.map +1 -0
  163. package/lib/common/security-check/security-check-protocol.d.ts +41 -0
  164. package/lib/common/security-check/security-check-protocol.d.ts.map +1 -0
  165. package/lib/common/security-check/security-check-protocol.js +28 -0
  166. package/lib/common/security-check/security-check-protocol.js.map +1 -0
  167. package/lib/common/security-check/security-checker.d.ts +15 -0
  168. package/lib/common/security-check/security-checker.d.ts.map +1 -0
  169. package/lib/common/security-check/security-checker.js +64 -0
  170. package/lib/common/security-check/security-checker.js.map +1 -0
  171. package/lib/common/security-check/security-checker.spec.d.ts +2 -0
  172. package/lib/common/security-check/security-checker.spec.d.ts.map +1 -0
  173. package/lib/common/security-check/security-checker.spec.js +68 -0
  174. package/lib/common/security-check/security-checker.spec.js.map +1 -0
  175. package/lib/common/utils/emitter.d.ts +2 -0
  176. package/lib/common/utils/emitter.d.ts.map +1 -1
  177. package/lib/common/utils/emitter.js +4 -0
  178. package/lib/common/utils/emitter.js.map +1 -1
  179. package/lib/common/utils/error-util.d.ts +8 -0
  180. package/lib/common/utils/error-util.d.ts.map +1 -1
  181. package/lib/common/utils/error-util.js +24 -0
  182. package/lib/common/utils/error-util.js.map +1 -1
  183. package/lib/common/utils/event.d.ts +6 -1
  184. package/lib/common/utils/event.d.ts.map +1 -1
  185. package/lib/common/utils/event.js +20 -6
  186. package/lib/common/utils/event.js.map +1 -1
  187. package/lib/common/utils/utils-behavior.spec.js +21 -2
  188. package/lib/common/utils/utils-behavior.spec.js.map +1 -1
  189. package/lib/common/utils/uuid.d.ts +1 -1
  190. package/lib/common/utils/uuid.d.ts.map +1 -1
  191. package/lib/common/utils/uuid.js +2 -2
  192. package/lib/common/utils/uuid.js.map +1 -1
  193. package/lib/node/application/backend-application.js +1 -1
  194. package/lib/node/application/backend-application.js.map +1 -1
  195. package/lib/node/context/context.d.ts.map +1 -1
  196. package/lib/node/context/context.js +3 -3
  197. package/lib/node/context/context.js.map +1 -1
  198. package/lib/node/context/context.spec.js +13 -0
  199. package/lib/node/context/context.spec.js.map +1 -1
  200. package/package.json +6 -2
  201. package/src/browser/application/frontend-application.ts +7 -28
  202. package/src/common/annotation/component.ts +1 -1
  203. package/src/common/annotation/service.spec.ts +29 -0
  204. package/src/common/annotation/service.ts +2 -1
  205. package/src/common/aop/aop.spec.ts +4 -4
  206. package/src/common/application/application-entry.ts +4 -0
  207. package/src/common/application/application-protocol.ts +21 -11
  208. package/src/common/application/application.ts +1 -1
  209. package/src/common/application/lifecycle.spec.ts +52 -8
  210. package/src/common/config/config-util.ts +7 -1
  211. package/src/common/config/dynamic-config.ts +6 -1
  212. package/src/common/config/env-config-util.spec.ts +152 -149
  213. package/src/common/config/env-config-util.ts +95 -51
  214. package/src/common/constants.ts +1 -1
  215. package/src/common/container/auto-bind.ts +1 -1
  216. package/src/common/container/container.ts +3 -3
  217. package/src/common/el/core-context-initializer.ts +4 -2
  218. package/src/common/el/expression-engine.spec.ts +17 -8
  219. package/src/common/el/expression-engine.ts +17 -9
  220. package/src/common/el/expression-handler.ts +2 -1
  221. package/src/common/el/expression-util.ts +37 -8
  222. package/src/common/index.ts +1 -0
  223. package/src/common/logger/abstract-logger.ts +30 -17
  224. package/src/common/logger/logger-protocol.ts +1 -1
  225. package/src/common/logger/logger.spec.ts +37 -0
  226. package/src/common/logger/logger.ts +12 -4
  227. package/src/common/security-check/index.ts +2 -0
  228. package/src/common/security-check/security-check-protocol.ts +59 -0
  229. package/src/common/security-check/security-checker.spec.ts +70 -0
  230. package/src/common/security-check/security-checker.ts +48 -0
  231. package/src/common/utils/emitter.ts +5 -0
  232. package/src/common/utils/error-util.ts +27 -1
  233. package/src/common/utils/event.ts +20 -9
  234. package/src/common/utils/utils-behavior.spec.ts +23 -3
  235. package/src/common/utils/uuid.ts +1 -1
  236. package/src/node/application/backend-application.ts +1 -1
  237. package/src/node/context/context.spec.ts +14 -0
  238. package/src/node/context/context.ts +2 -3
  239. package/cell.remote.yml +0 -8
@@ -7,9 +7,9 @@ import { MethodBeforeAdvice, AfterReturningAdvice, AfterThrowsAdvice } from './a
7
7
 
8
8
  /**
9
9
  * AOP 的约定(阶段 1.E 行为测试):@Aspect 登记通知,proxy: true 的组件被代理。
10
- * 需要 cell.aop.enabled(core 的 cell.yml 默认 true;这里直接放进配置)。
10
+ * 需要 aop.enabled(core 的 cell.yml 默认 true;这里直接放进配置)。
11
11
  */
12
- const AOP_ON = { cell: { aop: { enabled: true } } };
12
+ const AOP_ON = { aop: { enabled: true } };
13
13
 
14
14
  function recordingAspects(log: string[], pointcut?: string) {
15
15
  const opt = <T>(id: T) => (pointcut ? { id, pointcut } : id) as any;
@@ -84,7 +84,7 @@ describe('core AOP', () => {
84
84
  expect(await result).to.equal(8);
85
85
  });
86
86
 
87
- it('没有 proxy: true 的组件不被代理;cell.aop.enabled 为 false 时也不代理', async () => {
87
+ it('没有 proxy: true 的组件不被代理;aop.enabled 为 false 时也不代理', async () => {
88
88
  const log: string[] = [];
89
89
  recordingAspects(log);
90
90
  @Component()
@@ -99,7 +99,7 @@ describe('core AOP', () => {
99
99
  class Disabled {
100
100
  hi() { return 'hi'; }
101
101
  }
102
- await ApplicationFactory.create({ cell: { aop: { enabled: false } } });
102
+ await ApplicationFactory.create({ aop: { enabled: false } });
103
103
  expect(ContainerUtil.get(Disabled).hi()).to.equal('hi');
104
104
  expect(log).to.deep.equal([]);
105
105
  });
@@ -29,6 +29,10 @@ const host = ApplicationHost.get();
29
29
  if (reason) {
30
30
  console.error(reason);
31
31
  }
32
+ // Nothing may keep the process alive, and it must not end as a success.
33
+ if (typeof process !== 'undefined') {
34
+ process.exitCode = 1;
35
+ }
32
36
  }
33
37
  })().catch(error => {
34
38
  // The container failed to load (a DI module threw): stop, or tell the host.
@@ -4,6 +4,8 @@ import { Event } from '../utils/event';
4
4
  import { Emitter } from '../utils/emitter';
5
5
  import { Logger } from '../logger/logger-protocol';
6
6
  import { Autowired, Optional } from '../annotation';
7
+ import { SecurityChecker } from '../security-check/security-check-protocol';
8
+ import { ErrorUtil } from '../utils/error-util';
7
9
 
8
10
  export const ApplicationLifecycle = Symbol('ApplicationLifecycle');
9
11
  export const Application = Symbol('Application');
@@ -41,35 +43,43 @@ export abstract class AbstractApplication implements Application {
41
43
  @Autowired(Logger)
42
44
  protected readonly logger: Logger;
43
45
 
46
+ @Autowired(SecurityChecker)
47
+ protected readonly securityChecker: SecurityChecker;
48
+
44
49
  /**
45
- * Initialize and start the frontend application.
50
+ * Applies the security checks (in production, `securityChecks: fail` stops here), then calls
51
+ * `initialize` of every lifecycle and `onStart` one after the other. A lifecycle that throws is
52
+ * logged and the others still run.
46
53
  */
47
54
  protected async doStart(): Promise<void> {
55
+ await this.securityChecker.enforce();
48
56
  for (const lifecycle of this.lifecycles) {
49
57
  if (lifecycle.initialize) {
50
58
  try {
51
59
  lifecycle.initialize();
52
60
  } catch (error) {
53
- this.logger.error('Could not initialize lifecycle', error);
61
+ this.logger.error(`Could not initialize lifecycle ${lifecycle.constructor.name}: ${ErrorUtil.describe(error)}`);
54
62
  }
55
63
  }
56
64
  }
57
-
58
- // TODO
59
-
60
65
  for (const lifecycle of this.lifecycles) {
61
66
  if (lifecycle.onStart) {
62
67
  try {
63
- await lifecycle.onStart(this);
68
+ await this.startLifecycle(lifecycle);
64
69
  } catch (error) {
65
- this.logger.error('Could not start lifecycle', error);
70
+ this.logger.error(`Could not start lifecycle ${lifecycle.constructor.name}: ${ErrorUtil.describe(error)}`);
66
71
  }
67
72
  }
68
73
  }
69
74
  }
70
75
 
76
+ /** Runs `onStart` of one lifecycle; the frontend measures it. */
77
+ protected startLifecycle(lifecycle: ApplicationLifecycle<Application>): MaybePromise<void> {
78
+ return lifecycle.onStart!(this);
79
+ }
80
+
71
81
  /**
72
- * Stop the frontend application lifecycle.
82
+ * Calls `onStop` of every lifecycle; one that throws is logged and the others still run.
73
83
  */
74
84
  protected doStop(): void {
75
85
  for (const lifecycle of this.lifecycles) {
@@ -77,7 +87,7 @@ export abstract class AbstractApplication implements Application {
77
87
  try {
78
88
  lifecycle.onStop(this);
79
89
  } catch (error) {
80
- this.logger.error('Could not stop lifecycle', error);
90
+ this.logger.error(`Could not stop lifecycle ${lifecycle.constructor.name}: ${ErrorUtil.describe(error)}`);
81
91
  }
82
92
  }
83
93
  }
@@ -89,7 +99,7 @@ export type ApplicationState =
89
99
  'init'
90
100
  | 'started'
91
101
  | 'ready'
92
- | 'stoped';
102
+ | 'stopped';
93
103
 
94
104
  export interface ApplicationStateService<T extends string> {
95
105
  state: T;
@@ -138,4 +148,4 @@ export abstract class AbstractApplicationStateService<T extends string> implemen
138
148
  return Promise.race(states.map(s => this.reachedState(s)));
139
149
  }
140
150
 
141
- }
151
+ }
@@ -15,6 +15,6 @@ export class CommonApplication extends AbstractApplication {
15
15
 
16
16
  async stop(): Promise<void> {
17
17
  this.doStop();
18
- this.stateService.state = 'stoped';
18
+ this.stateService.state = 'stopped';
19
19
  }
20
20
  }
@@ -7,6 +7,7 @@ import { ContainerUtil } from '../container/container-util';
7
7
  import { Scope } from '../container/scope';
8
8
  import { Logger } from '../logger/logger-protocol';
9
9
  import { MockLogger } from '../test/mock-logger';
10
+ import { SecurityCheck, SecurityCheckError, SecurityWarning } from '../security-check/security-check-protocol';
10
11
 
11
12
  /**
12
13
  * 应用生命周期的约定(阶段 1.E 行为测试):ApplicationLifecycle 的调用顺序、出错时的行为、状态变化。
@@ -97,7 +98,12 @@ describe('core 应用生命周期', () => {
97
98
  await app.start();
98
99
  await app.stop();
99
100
  expect(rec.events).to.deep.equal(['init', 'start', 'stop']);
100
- expect(rec.errors).to.deep.equal(['Could not initialize lifecycle', 'Could not start lifecycle', 'Could not stop lifecycle']);
101
+ // Each line names the lifecycle and carries the error with its stack.
102
+ expect(rec.errors.map(line => line.split('\n')[0])).to.deep.equal([
103
+ 'Could not initialize lifecycle Broken: Error: init boom',
104
+ 'Could not start lifecycle Broken: Error: start boom',
105
+ 'Could not stop lifecycle Broken: Error: stop boom'
106
+ ]);
101
107
  expect([Broken, Healthy]).to.have.length(2);
102
108
  });
103
109
 
@@ -107,23 +113,23 @@ describe('core 应用生命周期', () => {
107
113
  await app.stop();
108
114
  });
109
115
 
110
- it('状态:init → started → ready,stop 之后是 stoped;reachedState 与 onStateChanged', async () => {
116
+ it('状态:init → started → ready,stop 之后是 stopped;reachedState 与 onStateChanged', async () => {
111
117
  const app = await ApplicationFactory.create({});
112
118
  const states = ContainerUtil.get<ApplicationStateService<ApplicationState>>(ApplicationStateService);
113
119
  expect(states.state).to.equal('init');
114
120
  const seen: string[] = [];
115
121
  states.onStateChanged(s => seen.push(s));
116
122
  const ready = states.reachedState('ready');
117
- const readyOrStopped = states.reachedAnyState('ready', 'stoped');
123
+ const readyOrStopped = states.reachedAnyState('ready', 'stopped');
118
124
  await app.start();
119
125
  await ready;
120
126
  await readyOrStopped;
121
127
  expect(states.state).to.equal('ready');
122
128
  await app.stop();
123
- expect(states.state).to.equal('stoped');
124
- expect(seen).to.deep.equal(['started', 'ready', 'stoped']);
129
+ expect(states.state).to.equal('stopped');
130
+ expect(seen).to.deep.equal(['started', 'ready', 'stopped']);
125
131
  // 当前状态再等会立刻 resolve
126
- await states.reachedState('stoped');
132
+ await states.reachedState('stopped');
127
133
  });
128
134
 
129
135
  it('离开一个状态之后,再 reachedState(那个状态) 立即 resolve(F7)', async () => {
@@ -133,7 +139,7 @@ describe('core 应用生命周期', () => {
133
139
  const race = (p: Promise<void>) => Promise.race([p.then(() => 'resolved'), new Promise(r => setTimeout(() => r('pending'), 20))]);
134
140
  expect(await race(states.reachedState('ready'))).to.equal('resolved');
135
141
  expect(await race(states.reachedState('started'))).to.equal('resolved');
136
- expect(await race(states.reachedState('stoped'))).to.equal('pending');
142
+ expect(await race(states.reachedState('stopped'))).to.equal('pending');
137
143
  });
138
144
 
139
145
  it('同一个状态重复设置不会重复通知', async () => {
@@ -145,4 +151,42 @@ describe('core 应用生命周期', () => {
145
151
  states.state = 'started';
146
152
  expect(seen).to.deep.equal(['started']);
147
153
  });
148
- });
154
+ });
155
+
156
+ describe('启动时的安全检查', () => {
157
+ /** Registers a check that warns and a lifecycle that records its start; autoBind of the next create() takes them. */
158
+ function components(rec: Recorder) {
159
+ @Component(SecurityCheck)
160
+ class PublicKeyCheck implements SecurityCheck {
161
+ check(): SecurityWarning[] {
162
+ return [{ code: 'CELL_SECURITY_TEST', message: 'The key is public.', hint: 'Set your own key.' }];
163
+ }
164
+ }
165
+ @Component(ApplicationLifecycle)
166
+ class Started implements ApplicationLifecycle<Application> {
167
+ onStart() { rec.events.push('start'); }
168
+ }
169
+ return [PublicKeyCheck, Started];
170
+ }
171
+
172
+ it('生产环境 securityChecks: fail 时 start() 拒绝启动,生命周期不会运行;warn 时照常启动', async () => {
173
+ const nodeEnv = process.env.NODE_ENV;
174
+ const rec: Recorder = { events: [], errors: [] };
175
+ process.env.NODE_ENV = 'production';
176
+ try {
177
+ components(rec);
178
+ const failing = await ApplicationFactory.create({ securityChecks: 'fail', logger: { level: 'error' } });
179
+ const error = await failing.start().then(() => undefined, e => e);
180
+ expect(error).to.be.instanceOf(SecurityCheckError);
181
+ expect(error.code).to.equal('CELL_SECURITY_CHECK_FAILED');
182
+ expect(rec.events).to.deep.equal([]);
183
+ components(rec);
184
+ const warning = await ApplicationFactory.create({ securityChecks: 'warn', logger: { level: 'error' } });
185
+ await warning.start();
186
+ expect(rec.events).to.deep.equal(['start']);
187
+ await warning.stop();
188
+ } finally {
189
+ process.env.NODE_ENV = nodeEnv;
190
+ }
191
+ });
192
+ });
@@ -1,6 +1,7 @@
1
1
  import { ConfigProvider } from './config-protocol';
2
2
  import { ContainerUtil } from '../container';
3
- import { config } from './dynamic-config';
3
+ import { config, types } from './dynamic-config';
4
+ import type { ConfigTypes } from './env-config-util';
4
5
  import { currentThis } from '../utils';
5
6
 
6
7
  export namespace ConfigUtil {
@@ -11,4 +12,9 @@ export namespace ConfigUtil {
11
12
  export function getRaw() {
12
13
  return currentThis.cellProps || config;
13
14
  }
15
+
16
+ /** Schema types of keys whose value is an expression; tests that set `cellProps` may set `cellConfigTypes`. */
17
+ export function getTypes(): ConfigTypes {
18
+ return currentThis.cellConfigTypes || types;
19
+ }
14
20
  }
@@ -1,2 +1,7 @@
1
- // dynamic loading config at compile time
1
+ import type { ConfigTypes } from './env-config-util';
2
+
3
+ // Replaced by the build (@celljs/vite) with the merged configuration of the application.
2
4
  export const config: { [key: string]: any } = {};
5
+
6
+ /** Schema types of the keys whose value is an expression, which evaluation converts to (`server.port: ${env.PORT ?? 3000}`). */
7
+ export const types: ConfigTypes = {};