1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {DecoratorFactory} from '@loopback/metadata';
|
7 | import debugModule from 'debug';
|
8 | import {Binding} from './binding';
|
9 | import {BindingSelector} from './binding-filter';
|
10 | import {Context} from './context';
|
11 | import {Injection, InjectionMetadata} from './inject';
|
12 | import {BoundValue, tryWithFinally, ValueOrPromise} from './value-promise';
|
13 |
|
14 | const debugSession = debugModule('loopback:context:resolver:session');
|
15 | const getTargetName = DecoratorFactory.getTargetName;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export type ResolutionAction = (
|
21 | session: ResolutionSession,
|
22 | ) => ValueOrPromise<BoundValue>;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | export interface BindingElement {
|
28 | type: 'binding';
|
29 | value: Readonly<Binding>;
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export interface InjectionElement {
|
36 | type: 'injection';
|
37 | value: Readonly<Injection>;
|
38 | }
|
39 |
|
40 | export interface InjectionDescriptor {
|
41 | targetName: string;
|
42 | bindingSelector: BindingSelector;
|
43 | metadata: InjectionMetadata;
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | export type ResolutionElement = BindingElement | InjectionElement;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function isBinding(
|
56 | element: ResolutionElement | undefined,
|
57 | ): element is BindingElement {
|
58 | return element != null && element.type === 'binding';
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function isInjection(
|
66 | element: ResolutionElement | undefined,
|
67 | ): element is InjectionElement {
|
68 | return element != null && element.type === 'injection';
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export class ResolutionSession {
|
76 | |
77 |
|
78 |
|
79 |
|
80 | readonly stack: ResolutionElement[] = [];
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | static fork(session?: ResolutionSession): ResolutionSession | undefined {
|
89 | if (session === undefined) return undefined;
|
90 | const copy = new ResolutionSession();
|
91 | copy.stack.push(...session.stack);
|
92 | return copy;
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | static runWithBinding(
|
102 | action: ResolutionAction,
|
103 | binding: Readonly<Binding>,
|
104 | session = new ResolutionSession(),
|
105 | ) {
|
106 |
|
107 | session.pushBinding(binding);
|
108 | return tryWithFinally(
|
109 | () => action(session),
|
110 | () => session.popBinding(),
|
111 | );
|
112 | }
|
113 |
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | static runWithInjection(
|
121 | action: ResolutionAction,
|
122 | injection: Readonly<Injection>,
|
123 | session = new ResolutionSession(),
|
124 | ) {
|
125 | session.pushInjection(injection);
|
126 | return tryWithFinally(
|
127 | () => action(session),
|
128 | () => session.popInjection(),
|
129 | );
|
130 | }
|
131 |
|
132 | |
133 |
|
134 |
|
135 |
|
136 | static describeInjection(
|
137 | injection: Readonly<Injection>,
|
138 | ): InjectionDescriptor {
|
139 | const name = getTargetName(
|
140 | injection.target,
|
141 | injection.member,
|
142 | injection.methodDescriptorOrParameterIndex,
|
143 | );
|
144 | return {
|
145 | targetName: name,
|
146 | bindingSelector: injection.bindingSelector,
|
147 | metadata: injection.metadata,
|
148 | };
|
149 | }
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 | pushInjection(injection: Readonly<Injection>) {
|
156 |
|
157 | if (debugSession.enabled) {
|
158 | debugSession(
|
159 | 'Enter injection:',
|
160 | ResolutionSession.describeInjection(injection),
|
161 | );
|
162 | }
|
163 | this.stack.push({type: 'injection', value: injection});
|
164 |
|
165 | if (debugSession.enabled) {
|
166 | debugSession('Resolution path:', this.getResolutionPath());
|
167 | }
|
168 | }
|
169 |
|
170 | |
171 |
|
172 |
|
173 | popInjection() {
|
174 | const top = this.stack.pop();
|
175 | if (!isInjection(top)) {
|
176 | throw new Error('The top element must be an injection');
|
177 | }
|
178 |
|
179 | const injection = top.value;
|
180 |
|
181 | if (debugSession.enabled) {
|
182 | debugSession(
|
183 | 'Exit injection:',
|
184 | ResolutionSession.describeInjection(injection),
|
185 | );
|
186 | debugSession('Resolution path:', this.getResolutionPath() || '<empty>');
|
187 | }
|
188 | return injection;
|
189 | }
|
190 |
|
191 | |
192 |
|
193 |
|
194 | get currentInjection(): Readonly<Injection> | undefined {
|
195 | for (let i = this.stack.length - 1; i >= 0; i--) {
|
196 | const element = this.stack[i];
|
197 | if (isInjection(element)) return element.value;
|
198 | }
|
199 | return undefined;
|
200 | }
|
201 |
|
202 | |
203 |
|
204 |
|
205 | get currentBinding(): Readonly<Binding> | undefined {
|
206 | for (let i = this.stack.length - 1; i >= 0; i--) {
|
207 | const element = this.stack[i];
|
208 | if (isBinding(element)) return element.value;
|
209 | }
|
210 | return undefined;
|
211 | }
|
212 |
|
213 | |
214 |
|
215 |
|
216 |
|
217 | pushBinding(binding: Readonly<Binding>) {
|
218 |
|
219 | if (debugSession.enabled) {
|
220 | debugSession('Enter binding:', binding.toJSON());
|
221 | }
|
222 |
|
223 | if (this.stack.find(i => isBinding(i) && i.value === binding)) {
|
224 | const msg =
|
225 | `Circular dependency detected: ` +
|
226 | `${this.getResolutionPath()} --> ${binding.key}`;
|
227 | debugSession(msg);
|
228 | throw new Error(msg);
|
229 | }
|
230 | this.stack.push({type: 'binding', value: binding});
|
231 |
|
232 | if (debugSession.enabled) {
|
233 | debugSession('Resolution path:', this.getResolutionPath());
|
234 | }
|
235 | }
|
236 |
|
237 | |
238 |
|
239 |
|
240 | popBinding(): Readonly<Binding> {
|
241 | const top = this.stack.pop();
|
242 | if (!isBinding(top)) {
|
243 | throw new Error('The top element must be a binding');
|
244 | }
|
245 | const binding = top.value;
|
246 |
|
247 | if (debugSession.enabled) {
|
248 | debugSession('Exit binding:', binding?.toJSON());
|
249 | debugSession('Resolution path:', this.getResolutionPath() || '<empty>');
|
250 | }
|
251 | return binding;
|
252 | }
|
253 |
|
254 | |
255 |
|
256 |
|
257 | get bindingStack(): Readonly<Binding>[] {
|
258 | return this.stack.filter(isBinding).map(e => e.value);
|
259 | }
|
260 |
|
261 | |
262 |
|
263 |
|
264 | get injectionStack(): Readonly<Injection>[] {
|
265 | return this.stack.filter(isInjection).map(e => e.value);
|
266 | }
|
267 |
|
268 | |
269 |
|
270 |
|
271 | getBindingPath() {
|
272 | return this.stack.filter(isBinding).map(describe).join(' --> ');
|
273 | }
|
274 |
|
275 | |
276 |
|
277 |
|
278 | getInjectionPath() {
|
279 | return this.injectionStack
|
280 | .map(i => ResolutionSession.describeInjection(i).targetName)
|
281 | .join(' --> ');
|
282 | }
|
283 |
|
284 | |
285 |
|
286 |
|
287 |
|
288 |
|
289 | getResolutionPath() {
|
290 | return this.stack.map(describe).join(' --> ');
|
291 | }
|
292 |
|
293 | toString() {
|
294 | return this.getResolutionPath();
|
295 | }
|
296 | }
|
297 |
|
298 | function describe(e: ResolutionElement) {
|
299 | switch (e.type) {
|
300 | case 'injection':
|
301 | return '@' + ResolutionSession.describeInjection(e.value).targetName;
|
302 | case 'binding':
|
303 | return e.value.key;
|
304 | }
|
305 | }
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | export interface ResolutionOptions {
|
311 | |
312 |
|
313 |
|
314 | session?: ResolutionSession;
|
315 |
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 | optional?: boolean;
|
322 |
|
323 | |
324 |
|
325 |
|
326 |
|
327 |
|
328 | asProxyWithInterceptors?: boolean;
|
329 | }
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | export type ResolutionOptionsOrSession = ResolutionOptions | ResolutionSession;
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 | export function asResolutionOptions(
|
341 | optionsOrSession?: ResolutionOptionsOrSession,
|
342 | ): ResolutionOptions {
|
343 |
|
344 | if (optionsOrSession instanceof ResolutionSession) {
|
345 | return {session: optionsOrSession};
|
346 | }
|
347 | return optionsOrSession ?? {};
|
348 | }
|
349 |
|
350 |
|
351 |
|
352 |
|
353 | export interface ResolutionContext<T = unknown> {
|
354 | |
355 |
|
356 |
|
357 | readonly context: Context;
|
358 | |
359 |
|
360 |
|
361 | readonly binding: Readonly<Binding<T>>;
|
362 | |
363 |
|
364 |
|
365 | readonly options: ResolutionOptions;
|
366 | }
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | export class ResolutionError extends Error {
|
372 | constructor(
|
373 | message: string,
|
374 | readonly resolutionCtx: Partial<ResolutionContext>,
|
375 | ) {
|
376 | super(ResolutionError.buildMessage(message, resolutionCtx));
|
377 | this.name = ResolutionError.name;
|
378 | }
|
379 |
|
380 | private static buildDetails(resolutionCtx: Partial<ResolutionContext>) {
|
381 | return {
|
382 | context: resolutionCtx.context?.name ?? '',
|
383 | binding: resolutionCtx.binding?.key ?? '',
|
384 | resolutionPath: resolutionCtx.options?.session?.getResolutionPath() ?? '',
|
385 | };
|
386 | }
|
387 |
|
388 | |
389 |
|
390 |
|
391 |
|
392 |
|
393 | private static buildMessage(
|
394 | reason: string,
|
395 | resolutionCtx: Partial<ResolutionContext>,
|
396 | ) {
|
397 | const info = this.describeResolutionContext(resolutionCtx);
|
398 | const message = `${reason} (${info})`;
|
399 | return message;
|
400 | }
|
401 |
|
402 | private static describeResolutionContext(
|
403 | resolutionCtx: Partial<ResolutionContext>,
|
404 | ) {
|
405 | const details = ResolutionError.buildDetails(resolutionCtx);
|
406 | const items: string[] = [];
|
407 | for (const [name, val] of Object.entries(details)) {
|
408 | if (val !== '') {
|
409 | items.push(`${name}: ${val}`);
|
410 | }
|
411 | }
|
412 | return items.join(', ');
|
413 | }
|
414 | }
|