@loopback/context 3.15.1 → 3.17.2

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.
@@ -72,7 +72,7 @@ export function isBindingAddress(
72
72
  return (
73
73
  typeof bindingSelector !== 'function' &&
74
74
  (typeof bindingSelector === 'string' ||
75
- // See https://github.com/strongloop/loopback-next/issues/4570
75
+ // See https://github.com/loopbackio/loopback-next/issues/4570
76
76
  // `bindingSelector instanceof BindingKey` is not always reliable as the
77
77
  // `@loopback/context` module might be loaded from multiple locations if
78
78
  // `npm install` does not dedupe or there are mixed versions in the tree
package/src/binding.ts CHANGED
@@ -508,11 +508,21 @@ export class Binding<T = BoundValue> extends EventEmitter {
508
508
  const options = asResolutionOptions(optionsOrSession);
509
509
  const resolutionCtx = this.getResolutionContext(ctx, options);
510
510
  if (resolutionCtx == null) return undefined;
511
+
512
+ // Keep a snapshot for proxy
513
+ const savedSession =
514
+ ResolutionSession.fork(options.session) ?? new ResolutionSession();
515
+
511
516
  // First check cached value for non-transient
512
517
  if (this._cache) {
513
518
  if (this.scope !== BindingScope.TRANSIENT) {
514
519
  if (resolutionCtx && this._cache.has(resolutionCtx)) {
515
- return this._cache.get(resolutionCtx)!;
520
+ const value = this._cache.get(resolutionCtx)!;
521
+ return this.getValueOrProxy(
522
+ resolutionCtx,
523
+ {...options, session: savedSession},
524
+ value,
525
+ );
516
526
  }
517
527
  }
518
528
  }
@@ -524,7 +534,12 @@ export class Binding<T = BoundValue> extends EventEmitter {
524
534
  if (typeof this._getValue === 'function') {
525
535
  const result = ResolutionSession.runWithBinding(
526
536
  s => {
527
- const optionsWithSession = {...options, session: s};
537
+ const optionsWithSession = {
538
+ ...options,
539
+ session: s,
540
+ // Force to be the non-proxy version
541
+ asProxyWithInterceptors: false,
542
+ };
528
543
  // We already test `this._getValue` is a function. It's safe to assert
529
544
  // that `this._getValue` is not undefined.
530
545
  return this._getValue!({
@@ -535,7 +550,12 @@ export class Binding<T = BoundValue> extends EventEmitter {
535
550
  this,
536
551
  options.session,
537
552
  );
538
- return this._cacheValue(resolutionCtx!, result);
553
+ const value = this._cacheValue(resolutionCtx!, result);
554
+ return this.getValueOrProxy(
555
+ resolutionCtx,
556
+ {...options, session: savedSession},
557
+ value,
558
+ );
539
559
  }
540
560
  // `@inject.binding` adds a binding without _getValue
541
561
  if (options.optional) return undefined;
@@ -547,6 +567,23 @@ export class Binding<T = BoundValue> extends EventEmitter {
547
567
  );
548
568
  }
549
569
 
570
+ private getValueOrProxy(
571
+ resolutionCtx: Context,
572
+ options: ResolutionOptions,
573
+ value: ValueOrPromise<T>,
574
+ ): ValueOrPromise<T> {
575
+ const session = options.session!;
576
+ session.pushBinding(this);
577
+ return Binding.valueOrProxy(
578
+ {
579
+ context: resolutionCtx,
580
+ binding: this,
581
+ options,
582
+ },
583
+ value,
584
+ );
585
+ }
586
+
550
587
  /**
551
588
  * Locate and validate the resolution context
552
589
  * @param ctx - Current context
@@ -1060,11 +1097,11 @@ function createInterceptionProxyFromInstance<T>(
1060
1097
  ) {
1061
1098
  return transformValueOrPromise(instOrPromise, inst => {
1062
1099
  if (typeof inst !== 'object' || inst == null) return inst;
1063
- return (createProxyWithInterceptors(
1100
+ return createProxyWithInterceptors(
1064
1101
  // Cast inst from `T` to `object`
1065
- (inst as unknown) as object,
1102
+ inst as unknown as object,
1066
1103
  context,
1067
1104
  session,
1068
- ) as unknown) as T;
1105
+ ) as unknown as T;
1069
1106
  });
1070
1107
  }
@@ -16,10 +16,8 @@ export class ContextTagIndexer {
16
16
  /**
17
17
  * Index for bindings by tag names
18
18
  */
19
- readonly bindingsIndexedByTag: Map<
20
- string,
21
- Set<Readonly<Binding<unknown>>>
22
- > = new Map();
19
+ readonly bindingsIndexedByTag: Map<string, Set<Readonly<Binding<unknown>>>> =
20
+ new Map();
23
21
 
24
22
  /**
25
23
  * A listener for binding events
@@ -47,7 +47,8 @@ export interface ContextViewEvent<T> extends ContextEvent {
47
47
  */
48
48
  export class ContextView<T = unknown>
49
49
  extends EventEmitter
50
- implements ContextObserver {
50
+ implements ContextObserver
51
+ {
51
52
  /**
52
53
  * An array of cached bindings that matches the binding filter
53
54
  */
package/src/context.ts CHANGED
@@ -128,7 +128,7 @@ export class Context extends EventEmitter {
128
128
  // The number of listeners can grow with the number of child contexts
129
129
  // For example, each request can add a listener to the RestServer and the
130
130
  // listener is removed when the request processing is finished.
131
- // See https://github.com/strongloop/loopback-next/issues/4363
131
+ // See https://github.com/loopbackio/loopback-next/issues/4363
132
132
  this.setMaxListeners(Infinity);
133
133
  if (typeof _parent === 'string') {
134
134
  name = _parent;
package/src/inject.ts CHANGED
@@ -152,20 +152,21 @@ export function inject(
152
152
  if (typeof methodDescriptorOrParameterIndex === 'number') {
153
153
  // The decorator is applied to a method parameter
154
154
  // Please note propertyKey is `undefined` for constructor
155
- const paramDecorator: ParameterDecorator = ParameterDecoratorFactory.createDecorator<Injection>(
156
- INJECT_PARAMETERS_KEY,
157
- {
158
- target,
159
- member,
160
- methodDescriptorOrParameterIndex,
161
- bindingSelector,
162
- metadata: injectionMetadata,
163
- resolve,
164
- },
165
- // Do not deep clone the spec as only metadata is mutable and it's
166
- // shallowly cloned
167
- {cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
168
- );
155
+ const paramDecorator: ParameterDecorator =
156
+ ParameterDecoratorFactory.createDecorator<Injection>(
157
+ INJECT_PARAMETERS_KEY,
158
+ {
159
+ target,
160
+ member,
161
+ methodDescriptorOrParameterIndex,
162
+ bindingSelector,
163
+ metadata: injectionMetadata,
164
+ resolve,
165
+ },
166
+ // Do not deep clone the spec as only metadata is mutable and it's
167
+ // shallowly cloned
168
+ {cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
169
+ );
169
170
  paramDecorator(target, member!, methodDescriptorOrParameterIndex);
170
171
  } else if (member) {
171
172
  // Property or method
@@ -186,20 +187,21 @@ export function inject(
186
187
  ),
187
188
  );
188
189
  }
189
- const propDecorator: PropertyDecorator = PropertyDecoratorFactory.createDecorator<Injection>(
190
- INJECT_PROPERTIES_KEY,
191
- {
192
- target,
193
- member,
194
- methodDescriptorOrParameterIndex,
195
- bindingSelector,
196
- metadata: injectionMetadata,
197
- resolve,
198
- },
199
- // Do not deep clone the spec as only metadata is mutable and it's
200
- // shallowly cloned
201
- {cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
202
- );
190
+ const propDecorator: PropertyDecorator =
191
+ PropertyDecoratorFactory.createDecorator<Injection>(
192
+ INJECT_PROPERTIES_KEY,
193
+ {
194
+ target,
195
+ member,
196
+ methodDescriptorOrParameterIndex,
197
+ bindingSelector,
198
+ metadata: injectionMetadata,
199
+ resolve,
200
+ },
201
+ // Do not deep clone the spec as only metadata is mutable and it's
202
+ // shallowly cloned
203
+ {cloneInputSpec: false, decoratorName: injectionMetadata.decorator},
204
+ );
203
205
  propDecorator(target, member!);
204
206
  } else {
205
207
  // It won't happen here as `@inject` is not compatible with ClassDecorator
@@ -508,7 +510,7 @@ function shouldSkipBaseConstructorInjection(targetClass: Object) {
508
510
  const classDef = targetClass.toString();
509
511
  return (
510
512
  /*
511
- * See https://github.com/strongloop/loopback-next/issues/2946
513
+ * See https://github.com/loopbackio/loopback-next/issues/2946
512
514
  * A class decorator can return a new constructor that mixes in
513
515
  * additional properties/methods.
514
516
  *
@@ -537,7 +539,7 @@ function shouldSkipBaseConstructorInjection(targetClass: Object) {
537
539
  /\s+constructor\s*\(\s*\)\s*\{\s*super\(\.\.\.arguments\)/,
538
540
  ) &&
539
541
  /*
540
- * See https://github.com/strongloop/loopback-next/issues/1565
542
+ * See https://github.com/loopbackio/loopback-next/issues/1565
541
543
  *
542
544
  * @example
543
545
  * ```ts
@@ -24,7 +24,7 @@ export type AsValueOrPromise<T> = T extends Promise<unknown>
24
24
  export type AsInterceptedFunction<T> = T extends (
25
25
  ...args: InvocationArgs
26
26
  ) => infer R
27
- ? (...args: InvocationArgs) => AsValueOrPromise<R>
27
+ ? (...args: Parameters<T>) => AsValueOrPromise<R>
28
28
  : T;
29
29
 
30
30
  /**
@@ -242,7 +242,7 @@ export class GenericInterceptorChain<C extends Context = Context> {
242
242
  */
243
243
  export function invokeInterceptors<
244
244
  C extends Context = Context,
245
- T = InvocationResult
245
+ T = InvocationResult,
246
246
  >(
247
247
  context: C,
248
248
  interceptors: GenericInterceptorOrKey<C>[],
@@ -287,7 +287,7 @@ export function intercept(...interceptorOrKeys: InterceptorOrKey[]) {
287
287
  target: any,
288
288
  method?: string,
289
289
  // Use `any` to for `TypedPropertyDescriptor`
290
- // See https://github.com/strongloop/loopback-next/pull/2704
290
+ // See https://github.com/loopbackio/loopback-next/pull/2704
291
291
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
292
292
  methodDescriptor?: TypedPropertyDescriptor<any>,
293
293
  ) {
package/src/keys.ts CHANGED
@@ -71,9 +71,10 @@ export namespace ContextBindings {
71
71
  /**
72
72
  * Binding key for ConfigurationResolver
73
73
  */
74
- export const CONFIGURATION_RESOLVER = BindingKey.create<ConfigurationResolver>(
75
- `${BindingKey.CONFIG_NAMESPACE}.resolver`,
76
- );
74
+ export const CONFIGURATION_RESOLVER =
75
+ BindingKey.create<ConfigurationResolver>(
76
+ `${BindingKey.CONFIG_NAMESPACE}.resolver`,
77
+ );
77
78
 
78
79
  /**
79
80
  * Binding key for ordered groups of global interceptors
@@ -92,20 +92,6 @@ export class ResolutionSession {
92
92
  return copy;
93
93
  }
94
94
 
95
- /**
96
- * Start to resolve a binding within the session
97
- * @param binding - The current binding
98
- * @param session - The current resolution session
99
- */
100
- private static enterBinding(
101
- binding: Readonly<Binding>,
102
- session?: ResolutionSession,
103
- ): ResolutionSession {
104
- session = session ?? new ResolutionSession();
105
- session.pushBinding(binding);
106
- return session;
107
- }
108
-
109
95
  /**
110
96
  * Run the given action with the given binding and session
111
97
  * @param action - A function to do some work with the resolution session
@@ -115,29 +101,16 @@ export class ResolutionSession {
115
101
  static runWithBinding(
116
102
  action: ResolutionAction,
117
103
  binding: Readonly<Binding>,
118
- session?: ResolutionSession,
104
+ session = new ResolutionSession(),
119
105
  ) {
120
- const resolutionSession = ResolutionSession.enterBinding(binding, session);
106
+ // Start to resolve a binding within the session
107
+ session.pushBinding(binding);
121
108
  return tryWithFinally(
122
- () => action(resolutionSession),
123
- () => resolutionSession.popBinding(),
109
+ () => action(session),
110
+ () => session.popBinding(),
124
111
  );
125
112
  }
126
113
 
127
- /**
128
- * Push an injection into the session
129
- * @param injection - The current injection
130
- * @param session - The current resolution session
131
- */
132
- private static enterInjection(
133
- injection: Readonly<Injection>,
134
- session?: ResolutionSession,
135
- ): ResolutionSession {
136
- session = session ?? new ResolutionSession();
137
- session.pushInjection(injection);
138
- return session;
139
- }
140
-
141
114
  /**
142
115
  * Run the given action with the given injection and session
143
116
  * @param action - A function to do some work with the resolution session
@@ -147,15 +120,12 @@ export class ResolutionSession {
147
120
  static runWithInjection(
148
121
  action: ResolutionAction,
149
122
  injection: Readonly<Injection>,
150
- session?: ResolutionSession,
123
+ session = new ResolutionSession(),
151
124
  ) {
152
- const resolutionSession = ResolutionSession.enterInjection(
153
- injection,
154
- session,
155
- );
125
+ session.pushInjection(injection);
156
126
  return tryWithFinally(
157
- () => action(resolutionSession),
158
- () => resolutionSession.popInjection(),
127
+ () => action(session),
128
+ () => session.popInjection(),
159
129
  );
160
130
  }
161
131
 
@@ -299,7 +269,7 @@ export class ResolutionSession {
299
269
  * Get the binding path as `bindingA --> bindingB --> bindingC`.
300
270
  */
301
271
  getBindingPath() {
302
- return this.bindingStack.map(b => b.key).join(' --> ');
272
+ return this.stack.filter(isBinding).map(describe).join(' --> ');
303
273
  }
304
274
 
305
275
  /**
@@ -311,22 +281,13 @@ export class ResolutionSession {
311
281
  .join(' --> ');
312
282
  }
313
283
 
314
- private static describe(e: ResolutionElement) {
315
- switch (e.type) {
316
- case 'injection':
317
- return '@' + ResolutionSession.describeInjection(e.value).targetName;
318
- case 'binding':
319
- return e.value.key;
320
- }
321
- }
322
-
323
284
  /**
324
285
  * Get the resolution path including bindings and injections, for example:
325
286
  * `bindingA --> @ClassA[0] --> bindingB --> @ClassB.prototype.prop1
326
287
  * --> bindingC`.
327
288
  */
328
289
  getResolutionPath() {
329
- return this.stack.map(i => ResolutionSession.describe(i)).join(' --> ');
290
+ return this.stack.map(describe).join(' --> ');
330
291
  }
331
292
 
332
293
  toString() {
@@ -334,6 +295,15 @@ export class ResolutionSession {
334
295
  }
335
296
  }
336
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
+
337
307
  /**
338
308
  * Options for binding/dependency resolution
339
309
  */
@@ -314,4 +314,5 @@ export function uuid() {
314
314
  * @deprecated This pattern is an internal helper used by unit-tests, we are no
315
315
  * longer using it.
316
316
  */
317
- export const UUID_PATTERN = /[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/i;
317
+ export const UUID_PATTERN =
318
+ /[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/i;