@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.
- package/README.md +0 -6
- package/esm/common/application/application-protocol.js +1 -1
- package/esm/common/application/application-protocol.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/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-protocol.js +1 -1
- package/lib/common/application/application-protocol.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/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 +7 -17
- package/src/common/aop/aop.spec.ts +145 -0
- package/src/common/application/application-protocol.ts +1 -1
- package/src/common/application/lifecycle.spec.ts +148 -0
- package/src/common/config/config-provider.ts +1 -1
- 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/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
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { describe, it } from 'bun:test';
|
|
2
|
+
import { expect } from 'chai';
|
|
3
|
+
import { ApplicationFactory } from '../application/application-factory';
|
|
4
|
+
import {
|
|
5
|
+
Component, Autowired, Optional, Constant, AutowiredProvider, PostConstruct, Named, Tagged
|
|
6
|
+
} from '../annotation';
|
|
7
|
+
import { ContainerUtil } from './container-util';
|
|
8
|
+
import { ContainerProvider } from './container-provider';
|
|
9
|
+
import { Scope } from './scope';
|
|
10
|
+
import { Provider } from '../provider';
|
|
11
|
+
import { ConfigProvider } from '../config/config-protocol';
|
|
12
|
+
import { autoBind } from './auto-bind';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 组件装配的约定(阶段 1.E 行为测试):只用公开的注解、ApplicationFactory 和 ContainerUtil 断言,
|
|
16
|
+
* 不碰 inversify 的 API,这样阶段 2 换容器时这些用例可以原样复用。
|
|
17
|
+
*
|
|
18
|
+
* 注意:组件元数据是全局登记的,ApplicationFactory.create() 里的 autoBind() 会把登记过的全部取走。
|
|
19
|
+
* 所以每个用例都在自己的函数里定义类,然后马上 create,用例之间互不影响。
|
|
20
|
+
*/
|
|
21
|
+
async function boot(props: Record<string, any> = {}) {
|
|
22
|
+
return ApplicationFactory.create(props);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('core 组件装配', () => {
|
|
26
|
+
|
|
27
|
+
it('@Component 不带参数时以类本身为 id,默认单例', async () => {
|
|
28
|
+
@Component()
|
|
29
|
+
class Plain { }
|
|
30
|
+
await boot();
|
|
31
|
+
const a = ContainerUtil.get(Plain);
|
|
32
|
+
expect(a).to.be.instanceOf(Plain);
|
|
33
|
+
expect(ContainerUtil.get(Plain)).to.equal(a);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('@Component(id) 同时可以按类和按 id 取到,且是同一个实例', async () => {
|
|
37
|
+
const Greeter = Symbol('Greeter');
|
|
38
|
+
@Component(Greeter)
|
|
39
|
+
class GreeterImpl { hello() { return 'hi'; } }
|
|
40
|
+
await boot();
|
|
41
|
+
const byId = ContainerUtil.get<GreeterImpl>(Greeter);
|
|
42
|
+
expect(byId.hello()).to.equal('hi');
|
|
43
|
+
expect(ContainerUtil.get(GreeterImpl)).to.equal(byId);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('一个组件可以登记多个 id,都指向同一个实例', async () => {
|
|
47
|
+
const A = Symbol('A');
|
|
48
|
+
const B = Symbol('B');
|
|
49
|
+
@Component({ id: [A, B] })
|
|
50
|
+
class Both { }
|
|
51
|
+
await boot();
|
|
52
|
+
expect(ContainerUtil.get(A)).to.be.instanceOf(Both);
|
|
53
|
+
expect(ContainerUtil.get(A)).to.equal(ContainerUtil.get(B));
|
|
54
|
+
expect(ContainerUtil.get(Both)).to.equal(ContainerUtil.get(A));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('Scope.Transient 每次取都是新实例', async () => {
|
|
58
|
+
@Component({ scope: Scope.Transient })
|
|
59
|
+
class Fresh { }
|
|
60
|
+
await boot();
|
|
61
|
+
expect(ContainerUtil.get(Fresh)).not.to.equal(ContainerUtil.get(Fresh));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('@Autowired 属性注入:按声明的类型,也可以显式给 id', async () => {
|
|
65
|
+
const Repo = Symbol('Repo');
|
|
66
|
+
@Component(Repo)
|
|
67
|
+
class RepoImpl { find() { return 'row'; } }
|
|
68
|
+
@Component()
|
|
69
|
+
class Dep { }
|
|
70
|
+
@Component()
|
|
71
|
+
class Service {
|
|
72
|
+
@Autowired(Repo)
|
|
73
|
+
readonly repo: RepoImpl;
|
|
74
|
+
@Autowired()
|
|
75
|
+
readonly dep: Dep;
|
|
76
|
+
}
|
|
77
|
+
await boot();
|
|
78
|
+
const s = ContainerUtil.get(Service);
|
|
79
|
+
expect(s.repo.find()).to.equal('row');
|
|
80
|
+
expect(s.dep).to.equal(ContainerUtil.get(Dep));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('@Autowired 构造器参数注入', async () => {
|
|
84
|
+
const Clock = Symbol('Clock');
|
|
85
|
+
@Component(Clock)
|
|
86
|
+
class FixedClock { now() { return 42; } }
|
|
87
|
+
@Component()
|
|
88
|
+
class UsesClock {
|
|
89
|
+
constructor(@Autowired(Clock) readonly clock: FixedClock) { }
|
|
90
|
+
}
|
|
91
|
+
await boot();
|
|
92
|
+
expect(ContainerUtil.get(UsesClock).clock.now()).to.equal(42);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('同一 id 有多个实现时,数组类型的 @Autowired 拿到全部', async () => {
|
|
96
|
+
const Plugin = Symbol('Plugin');
|
|
97
|
+
@Component(Plugin)
|
|
98
|
+
class P1 { readonly name = 'p1'; }
|
|
99
|
+
@Component(Plugin)
|
|
100
|
+
class P2 { readonly name = 'p2'; }
|
|
101
|
+
@Component()
|
|
102
|
+
class Host {
|
|
103
|
+
@Autowired(Plugin)
|
|
104
|
+
readonly plugins: { name: string }[];
|
|
105
|
+
}
|
|
106
|
+
await boot();
|
|
107
|
+
expect(ContainerUtil.get(Host).plugins.map(p => p.name).sort()).to.deep.equal(['p1', 'p2']);
|
|
108
|
+
expect(ContainerUtil.getAll(Plugin)).to.have.length(2);
|
|
109
|
+
expect([P1, P2]).to.have.length(2);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('@Optional:没有实现时单值为 undefined,数组为 []', async () => {
|
|
113
|
+
const Missing = Symbol('Missing');
|
|
114
|
+
const MissingMany = Symbol('MissingMany');
|
|
115
|
+
@Component()
|
|
116
|
+
class Tolerant {
|
|
117
|
+
@Autowired(Missing) @Optional()
|
|
118
|
+
readonly one?: unknown;
|
|
119
|
+
@Autowired(MissingMany) @Optional()
|
|
120
|
+
readonly many: unknown[];
|
|
121
|
+
}
|
|
122
|
+
await boot();
|
|
123
|
+
const t = ContainerUtil.get(Tolerant);
|
|
124
|
+
expect(t.one).to.equal(undefined);
|
|
125
|
+
expect(t.many).to.deep.equal([]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('没有实现又不是 @Optional 时,取的时候报错,错误信息里有缺的 id', async () => {
|
|
129
|
+
const Absent = Symbol('AbsentService');
|
|
130
|
+
@Component()
|
|
131
|
+
class Needy {
|
|
132
|
+
@Autowired(Absent)
|
|
133
|
+
readonly absent: unknown;
|
|
134
|
+
}
|
|
135
|
+
await boot();
|
|
136
|
+
expect(() => ContainerUtil.get(Needy)).to.throw(/AbsentService/);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('@Component({ id, name / tag / default }) 按名字、标签或默认选实现,类本身仍可直接取(F1)', async () => {
|
|
140
|
+
const Store = Symbol('Store');
|
|
141
|
+
@Component({ id: Store, name: 'redis' })
|
|
142
|
+
class RedisStore { readonly kind = 'redis'; }
|
|
143
|
+
@Component({ id: Store, tag: { tag: 'region', value: 'eu' } })
|
|
144
|
+
class EuStore { readonly kind = 'eu'; }
|
|
145
|
+
@Component({ id: Store, default: true })
|
|
146
|
+
class MemoryStore { readonly kind = 'memory'; }
|
|
147
|
+
@Component()
|
|
148
|
+
class Consumer {
|
|
149
|
+
@Autowired(Store) @Named('redis')
|
|
150
|
+
readonly redis: { kind: string };
|
|
151
|
+
@Autowired(Store) @Tagged('region', 'eu')
|
|
152
|
+
readonly eu: { kind: string };
|
|
153
|
+
@Autowired(Store)
|
|
154
|
+
readonly fallback: { kind: string };
|
|
155
|
+
}
|
|
156
|
+
await boot();
|
|
157
|
+
const c = ContainerUtil.get(Consumer);
|
|
158
|
+
expect([c.redis.kind, c.eu.kind, c.fallback.kind]).to.deep.equal(['redis', 'eu', 'memory']);
|
|
159
|
+
expect(ContainerUtil.getNamed<{ kind: string }>(Store, 'redis')).to.equal(ContainerUtil.get(RedisStore));
|
|
160
|
+
expect(ContainerUtil.getTagged<{ kind: string }>(Store, 'region', 'eu')).to.equal(ContainerUtil.get(EuStore));
|
|
161
|
+
expect(ContainerUtil.get(Store)).to.equal(ContainerUtil.get(MemoryStore));
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('rebind: true 替换先加载的模块里的实现(应用覆盖框架或其他包的组件)', async () => {
|
|
165
|
+
const Mailer = Symbol('Mailer');
|
|
166
|
+
@Component(Mailer)
|
|
167
|
+
class DefaultMailer { readonly who = 'default'; }
|
|
168
|
+
const base = autoBind();
|
|
169
|
+
@Component({ id: Mailer, rebind: true })
|
|
170
|
+
class CustomMailer { readonly who = 'custom'; }
|
|
171
|
+
const overrides = autoBind();
|
|
172
|
+
await ApplicationFactory.create({}, base, overrides);
|
|
173
|
+
expect(ContainerUtil.getAll<{ who: string }>(Mailer).map(m => m.who)).to.deep.equal(['custom']);
|
|
174
|
+
expect([DefaultMailer, CustomMailer]).to.have.length(2);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('现状:同一批 autoBind 里 rebind 本批先定义的组件会失败(按定义的逆序登记,rebind 先于 bind 执行)', async () => {
|
|
178
|
+
const Mailer = Symbol('SameBatchMailer');
|
|
179
|
+
@Component(Mailer)
|
|
180
|
+
class DefaultMailer { }
|
|
181
|
+
@Component({ id: Mailer, rebind: true })
|
|
182
|
+
class CustomMailer { }
|
|
183
|
+
let error: Error | undefined;
|
|
184
|
+
try {
|
|
185
|
+
await boot();
|
|
186
|
+
} catch (e) {
|
|
187
|
+
error = e as Error;
|
|
188
|
+
}
|
|
189
|
+
expect(error?.message).to.match(/unbind.*SameBatchMailer/i);
|
|
190
|
+
expect([DefaultMailer, CustomMailer]).to.have.length(2);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('rebind 替换框架自带组件:替换后注入点拿到的是新实现', async () => {
|
|
194
|
+
@Component({ id: ConfigProvider, rebind: true })
|
|
195
|
+
class FixedConfig {
|
|
196
|
+
get(key: string) { return key === 'answer' ? 42 : undefined; }
|
|
197
|
+
}
|
|
198
|
+
@Component()
|
|
199
|
+
class ReadsConfig {
|
|
200
|
+
@Autowired(ConfigProvider)
|
|
201
|
+
readonly config: FixedConfig;
|
|
202
|
+
}
|
|
203
|
+
await boot();
|
|
204
|
+
expect(ContainerUtil.get(ReadsConfig).config).to.be.instanceOf(FixedConfig);
|
|
205
|
+
expect(ContainerUtil.get(ReadsConfig).config.get('answer')).to.equal(42);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('@Constant 登记常量,多个 id 都能取到', async () => {
|
|
209
|
+
const Token = Symbol('Token');
|
|
210
|
+
const Alias = Symbol('TokenAlias');
|
|
211
|
+
@Constant([Token, Alias], { secret: 'abc' })
|
|
212
|
+
class Holder { }
|
|
213
|
+
await boot();
|
|
214
|
+
expect(ContainerUtil.get(Token)).to.deep.equal({ secret: 'abc' });
|
|
215
|
+
expect(ContainerUtil.get(Alias)).to.equal(ContainerUtil.get(Token));
|
|
216
|
+
expect(Holder).to.be.a('function');
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('@AutowiredProvider 注入 Provider:按 priority 从小到大排序(sortSync)', async () => {
|
|
220
|
+
const Step = Symbol('Step');
|
|
221
|
+
@Component(Step)
|
|
222
|
+
class Late { readonly priority = 300; readonly name = 'late'; }
|
|
223
|
+
@Component(Step)
|
|
224
|
+
class Early { readonly priority = 100; readonly name = 'early'; }
|
|
225
|
+
@Component(Step)
|
|
226
|
+
class Middle { readonly priority = 200; readonly name = 'middle'; }
|
|
227
|
+
@Component()
|
|
228
|
+
class Pipeline {
|
|
229
|
+
@AutowiredProvider(Step)
|
|
230
|
+
readonly steps: Provider<{ name: string, priority: number }>;
|
|
231
|
+
}
|
|
232
|
+
await boot();
|
|
233
|
+
const names = ContainerUtil.get(Pipeline).steps.sortSync().map(s => s.name);
|
|
234
|
+
expect(names).to.have.members(['early', 'middle', 'late']);
|
|
235
|
+
// Prioritizeable:priority 大的排前面
|
|
236
|
+
expect(names).to.deep.equal(['late', 'middle', 'early']);
|
|
237
|
+
expect([Late, Early, Middle]).to.have.length(3);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('@PostConstruct 在注入完成后调用一次,单例只调用一次', async () => {
|
|
241
|
+
let calls = 0;
|
|
242
|
+
@Component()
|
|
243
|
+
class Dep { readonly ok = true; }
|
|
244
|
+
@Component()
|
|
245
|
+
class Init {
|
|
246
|
+
@Autowired()
|
|
247
|
+
readonly dep: Dep;
|
|
248
|
+
seenDep = false;
|
|
249
|
+
@PostConstruct()
|
|
250
|
+
protected init() {
|
|
251
|
+
calls++;
|
|
252
|
+
this.seenDep = this.dep.ok;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
await boot();
|
|
256
|
+
expect(ContainerUtil.get(Init).seenDep).to.equal(true);
|
|
257
|
+
ContainerUtil.get(Init);
|
|
258
|
+
expect(calls).to.equal(1);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('detached 的 @Autowired:不在容器里的对象也能按需从当前容器取值', async () => {
|
|
262
|
+
const Clock = Symbol('DetachedClock');
|
|
263
|
+
@Component(Clock)
|
|
264
|
+
class Clk { now() { return 7; } }
|
|
265
|
+
class NotAComponent {
|
|
266
|
+
@Autowired({ id: Clock, detached: true })
|
|
267
|
+
readonly clock: Clk;
|
|
268
|
+
}
|
|
269
|
+
await boot();
|
|
270
|
+
expect(new NotAComponent().clock.now()).to.equal(7);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('detached 不支持构造器参数,给出明确的错误', () => {
|
|
274
|
+
expect(() => {
|
|
275
|
+
class Bad {
|
|
276
|
+
constructor(@Autowired({ id: Symbol('x'), detached: true }) readonly x: unknown) { }
|
|
277
|
+
}
|
|
278
|
+
return Bad;
|
|
279
|
+
}).to.throw(/not supported/);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('每次 ApplicationFactory.create 都换成新的容器,ContainerUtil 指向最新的那个', async () => {
|
|
283
|
+
@Component()
|
|
284
|
+
class Counter { value = 0; }
|
|
285
|
+
await boot();
|
|
286
|
+
const first = ContainerProvider.provide();
|
|
287
|
+
ContainerUtil.get(Counter).value = 5;
|
|
288
|
+
await boot();
|
|
289
|
+
expect(ContainerProvider.provide()).not.to.equal(first);
|
|
290
|
+
// 新容器里没有再登记 Counter(元数据已被上一次 create 取走)
|
|
291
|
+
expect(() => ContainerUtil.get(Counter)).to.throw();
|
|
292
|
+
});
|
|
293
|
+
});
|
|
@@ -65,14 +65,23 @@ function resolve(metadata: ComponentMetadata, bind: interfaces.Bind, rebind: int
|
|
|
65
65
|
mid = mid.inTransientScope();
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
const constrain = (syntax: any) => {
|
|
69
|
+
if (name) {
|
|
70
|
+
return syntax.whenTargetNamed(name);
|
|
71
|
+
} else if (tag) {
|
|
72
|
+
return syntax.whenTargetTagged(tag.tag, tag.value);
|
|
73
|
+
} else if (metadata.default) {
|
|
74
|
+
return syntax.whenTargetIsDefault();
|
|
75
|
+
} else if (when) {
|
|
76
|
+
return syntax.when(when);
|
|
77
|
+
}
|
|
78
|
+
return syntax;
|
|
79
|
+
};
|
|
80
|
+
// name / tag / default / when choose among the implementations of an id, so they go on the bindings of
|
|
81
|
+
// the ids (`@Component({ id, name })`); the class itself stays reachable without them.
|
|
82
|
+
const aliases = !metadata.rebind && (name || tag || metadata.default || when) ? _ids : [];
|
|
83
|
+
if (aliases.length === 0) {
|
|
84
|
+
mid = constrain(mid);
|
|
76
85
|
}
|
|
77
86
|
|
|
78
87
|
if (onActivation) {
|
|
@@ -82,7 +91,11 @@ function resolve(metadata: ComponentMetadata, bind: interfaces.Bind, rebind: int
|
|
|
82
91
|
}
|
|
83
92
|
|
|
84
93
|
for (const item of _ids) {
|
|
85
|
-
|
|
94
|
+
if (aliases.includes(item)) {
|
|
95
|
+
constrain(bind(item).toDynamicValue(context => context.container.get(id)));
|
|
96
|
+
} else {
|
|
97
|
+
bind(item).toService(id);
|
|
98
|
+
}
|
|
86
99
|
}
|
|
87
100
|
}
|
|
88
101
|
|
|
@@ -98,4 +111,4 @@ function resolveConstant(metadata: ConstantOption, bind: interfaces.Bind, rebind
|
|
|
98
111
|
for (const item of ids) {
|
|
99
112
|
bind(item!).toService(id!);
|
|
100
113
|
}
|
|
101
|
-
}
|
|
114
|
+
}
|
|
@@ -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
|
+
});
|