awilixify 3.3.0 → 3.5.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 (43) hide show
  1. package/README.md +1 -1
  2. package/dist/index.d.ts +2 -1
  3. package/dist/index.js +1 -0
  4. package/dist/lib/decorators/decorator-state.js +6 -0
  5. package/dist/lib/decorators/decorator-state.types.d.ts +1 -1
  6. package/dist/lib/di/common.types.d.ts +0 -1
  7. package/dist/lib/di/contexts/container-context-base.d.ts +4 -0
  8. package/dist/lib/di/contexts/container-context-base.js +30 -18
  9. package/dist/lib/di/contexts/di-context-async.js +4 -31
  10. package/dist/lib/di/contexts/di-context-base.js +5 -5
  11. package/dist/lib/di/contexts/di-context.d.ts +5 -0
  12. package/dist/lib/di/contexts/di-context.js +54 -0
  13. package/dist/lib/di/errors.d.ts +18 -0
  14. package/dist/lib/di/errors.js +36 -0
  15. package/dist/lib/di/modules/configure-module.d.ts +21 -0
  16. package/dist/lib/di/modules/configure-module.js +11 -0
  17. package/dist/lib/di/modules/module-def.types.d.ts +1 -1
  18. package/dist/lib/di/processors/controller-processor.d.ts +4 -1
  19. package/dist/lib/di/processors/controller-processor.js +57 -52
  20. package/dist/lib/di/processors/handler-processor.d.ts +3 -1
  21. package/dist/lib/di/processors/handler-processor.js +5 -3
  22. package/dist/lib/di/processors/initializer-processor.d.ts +3 -1
  23. package/dist/lib/di/processors/initializer-processor.js +25 -21
  24. package/dist/lib/di/processors/interceptor-processor.d.ts +5 -1
  25. package/dist/lib/di/processors/interceptor-processor.js +59 -69
  26. package/dist/lib/di/processors/keyed-feature-registrar.d.ts +4 -1
  27. package/dist/lib/di/processors/keyed-feature-registrar.js +58 -61
  28. package/dist/lib/di/providers/inquirer-resolver-factory.d.ts +10 -0
  29. package/dist/lib/di/providers/inquirer-resolver-factory.js +61 -0
  30. package/dist/lib/di/providers/provider-resolver.d.ts +5 -2
  31. package/dist/lib/di/providers/provider-resolver.js +43 -30
  32. package/dist/lib/di/providers/types/controller.types.d.ts +2 -1
  33. package/dist/lib/di/providers/types/handler.types.d.ts +0 -5
  34. package/dist/lib/di/providers/types/initializer.types.d.ts +2 -1
  35. package/dist/lib/di/providers/types/interceptor.types.d.ts +3 -2
  36. package/dist/lib/di/providers/types/middleware.types.d.ts +1 -4
  37. package/dist/lib/di/providers/types/provider.types.d.ts +4 -2
  38. package/dist/lib/di/type-guards.d.ts +1 -0
  39. package/dist/lib/extension-api/index.d.ts +1 -0
  40. package/dist/lib/extension-api/index.js +1 -0
  41. package/dist/lib/http/exceptions.d.ts +2 -2
  42. package/dist/tsconfig.build.tsbuildinfo +1 -1
  43. package/package.json +18 -9
@@ -11,6 +11,7 @@ export const HandlerType = {
11
11
  };
12
12
  export class HandlerProcessor {
13
13
  providerOptions;
14
+ inquirerResolverFactory;
14
15
  devtoolsProcessorRef;
15
16
  keyedFeatureRegistrar;
16
17
  static handlerConfig = {
@@ -25,10 +26,11 @@ export class HandlerProcessor {
25
26
  mediatorKey: "commandMediator",
26
27
  },
27
28
  };
28
- constructor(providerOptions, devtoolsProcessorRef) {
29
+ constructor(providerOptions, inquirerResolverFactory, devtoolsProcessorRef) {
29
30
  this.providerOptions = providerOptions;
31
+ this.inquirerResolverFactory = inquirerResolverFactory;
30
32
  this.devtoolsProcessorRef = devtoolsProcessorRef;
31
- this.keyedFeatureRegistrar = new KeyedFeatureRegistrar(providerOptions, devtoolsProcessorRef);
33
+ this.keyedFeatureRegistrar = new KeyedFeatureRegistrar(providerOptions, inquirerResolverFactory, devtoolsProcessorRef);
32
34
  }
33
35
  processHandlers(m, scope, importedModulesWithScope, handlerType) {
34
36
  const { handlersKey, mediatorKey, preHandlersKey } = HandlerProcessor.handlerConfig[handlerType];
@@ -52,7 +54,7 @@ export class HandlerProcessor {
52
54
  }
53
55
  const options = ProviderResolver.mergeResolverOptions(m, this.providerOptions, handlerOptions);
54
56
  const handlerSymbol = Symbol(`${handlerKey}_${HandlerClass.name}`);
55
- const handlerResolver = Awilix.asClass(HandlerClass, options);
57
+ const handlerResolver = this.inquirerResolverFactory.create(HandlerClass, options);
56
58
  scope.register({
57
59
  [handlerSymbol]: this.createTracedHandlerResolver({
58
60
  handlerClassName: HandlerClass.name,
@@ -2,6 +2,7 @@ import type * as Awilix from "awilix";
2
2
  import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
3
3
  import type { RegisteredModuleScope } from "../contexts/container-context-base.js";
4
4
  import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
5
+ import type { InquirerResolverFactory } from "../providers/inquirer-resolver-factory.js";
5
6
  import type { ConstructorController } from "../providers/provider.types.js";
6
7
  import type { ModuleInitOptions } from "./lifecycle-processor.js";
7
8
  export type ControllerRuntimeEntry = {
@@ -13,8 +14,9 @@ export declare class InitializerProcessor {
13
14
  private readonly devtoolsProcessorRef;
14
15
  private readonly keyedFeatureRegistrar;
15
16
  private readonly resolversByModule;
16
- constructor(devtoolsProcessorRef: DevtoolsProcessorRef);
17
+ constructor(inquirerResolverFactory: InquirerResolverFactory, devtoolsProcessorRef: DevtoolsProcessorRef);
17
18
  collectInitializers(m: M, scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[], controllers: ControllerRuntimeEntry[]): InitializerTask | null;
19
+ private initializeControllerMethod;
18
20
  private ensureNoMultiInvokableInitializersPerMethod;
19
21
  private filterActiveInitializers;
20
22
  }
@@ -5,10 +5,11 @@ import { runInRequestScopeContext } from "../request-scope-context.js";
5
5
  import { KeyedFeatureRegistrar } from "./keyed-feature-registrar.js";
6
6
  export class InitializerProcessor {
7
7
  devtoolsProcessorRef;
8
- keyedFeatureRegistrar = new KeyedFeatureRegistrar({});
8
+ keyedFeatureRegistrar;
9
9
  resolversByModule = new WeakMap();
10
- constructor(devtoolsProcessorRef) {
10
+ constructor(inquirerResolverFactory, devtoolsProcessorRef) {
11
11
  this.devtoolsProcessorRef = devtoolsProcessorRef;
12
+ this.keyedFeatureRegistrar = new KeyedFeatureRegistrar({}, inquirerResolverFactory);
12
13
  }
13
14
  collectInitializers(m, scope, importedModulesWithScope, controllers) {
14
15
  this.resolversByModule.set(m, this.keyedFeatureRegistrar.register({
@@ -34,29 +35,32 @@ export class InitializerProcessor {
34
35
  return;
35
36
  for (const controller of controllers) {
36
37
  for (const methodName of getControllerMethodNames(controller.controllerClass)) {
37
- const invoke = (...args) => runInRequestScopeContext(() => controller.resolve()[methodName](...args));
38
- this.ensureNoMultiInvokableInitializersPerMethod(m.name, methodName, controller, activeInitializers);
39
- for (const [, resolveInitializer] of activeInitializers) {
40
- const initializer = resolveInitializer();
41
- const decoratorState = resolveDecoratorState(controller.controllerClass, initializer.token);
42
- if (decoratorState === null)
43
- continue;
44
- const metadata = decoratorState.methods.get(methodName);
45
- if (metadata === undefined)
46
- continue;
47
- await initializer.initialize({
48
- moduleName: m.name,
49
- target: controller.controllerClass,
50
- methodName,
51
- metadata,
52
- decoratorState,
53
- ...(initializer.usesInvoke ? { invoke } : {}),
54
- });
55
- }
38
+ await this.initializeControllerMethod(m.name, controller, methodName, activeInitializers);
56
39
  }
57
40
  }
58
41
  };
59
42
  }
43
+ async initializeControllerMethod(moduleName, controller, methodName, initializers) {
44
+ const invoke = (...args) => runInRequestScopeContext(() => controller.resolve()[methodName](...args));
45
+ this.ensureNoMultiInvokableInitializersPerMethod(moduleName, methodName, controller, initializers);
46
+ for (const [, resolveInitializer] of initializers) {
47
+ const initializer = resolveInitializer();
48
+ const decoratorState = resolveDecoratorState(controller.controllerClass, initializer.token);
49
+ if (decoratorState === null)
50
+ continue;
51
+ const metadata = decoratorState.methods.get(methodName);
52
+ if (metadata === undefined)
53
+ continue;
54
+ await initializer.initialize({
55
+ moduleName,
56
+ target: controller.controllerClass,
57
+ methodName,
58
+ metadata,
59
+ decoratorState,
60
+ ...(initializer.usesInvoke ? { invoke } : {}),
61
+ });
62
+ }
63
+ }
60
64
  ensureNoMultiInvokableInitializersPerMethod(moduleName, methodName, controller, initializers) {
61
65
  const invokableInitializers = initializers
62
66
  .map(([, resolver]) => resolver())
@@ -3,11 +3,13 @@ import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
3
3
  import type { RegisteredModuleScope } from "../contexts/container-context-base.js";
4
4
  import type { DiContextOptions } from "../contexts/di-context-base.js";
5
5
  import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
6
+ import type { InquirerResolverFactory } from "../providers/inquirer-resolver-factory.js";
6
7
  export declare class InterceptorProcessor {
8
+ private readonly inquirerResolverFactory;
7
9
  private readonly devtoolsProcessorRef;
8
10
  private readonly keyedFeatureRegistrar;
9
11
  private readonly resolversByModule;
10
- constructor(providerOptions: DiContextOptions["providerOptions"], devtoolsProcessorRef: DevtoolsProcessorRef);
12
+ constructor(providerOptions: DiContextOptions["providerOptions"], inquirerResolverFactory: InquirerResolverFactory, devtoolsProcessorRef: DevtoolsProcessorRef);
11
13
  processInterceptors(m: M, scope: Awilix.AwilixContainer, importedModulesWithScope: RegisteredModuleScope[]): void;
12
14
  createInterceptedProviderResolver({ module, useClass, options, resolver, }: {
13
15
  module: M;
@@ -16,6 +18,8 @@ export declare class InterceptorProcessor {
16
18
  resolver?: Awilix.Resolver<any>;
17
19
  }): Awilix.Resolver<any>;
18
20
  private createInterceptedProviderInstance;
21
+ private prepareSelfCalls;
22
+ private collectInterceptorMetadata;
19
23
  private callWithInterceptorChain;
20
24
  private get devtoolsTracer();
21
25
  }
@@ -3,12 +3,14 @@ import { hasDecoratorMethodMetadata, resolveDecoratorState, } from "../../decora
3
3
  import { isPromiseLike } from "../type-guards.js";
4
4
  import { KeyedFeatureRegistrar } from "./keyed-feature-registrar.js";
5
5
  export class InterceptorProcessor {
6
+ inquirerResolverFactory;
6
7
  devtoolsProcessorRef;
7
8
  keyedFeatureRegistrar;
8
9
  resolversByModule = new WeakMap();
9
- constructor(providerOptions, devtoolsProcessorRef) {
10
+ constructor(providerOptions, inquirerResolverFactory, devtoolsProcessorRef) {
11
+ this.inquirerResolverFactory = inquirerResolverFactory;
10
12
  this.devtoolsProcessorRef = devtoolsProcessorRef;
11
- this.keyedFeatureRegistrar = new KeyedFeatureRegistrar(providerOptions ?? {});
13
+ this.keyedFeatureRegistrar = new KeyedFeatureRegistrar(providerOptions ?? {}, inquirerResolverFactory);
12
14
  }
13
15
  processInterceptors(m, scope, importedModulesWithScope) {
14
16
  const resolverMap = this.keyedFeatureRegistrar.register({
@@ -20,7 +22,7 @@ export class InterceptorProcessor {
20
22
  this.resolversByModule.set(m, resolverMap);
21
23
  }
22
24
  createInterceptedProviderResolver({ module, useClass, options, resolver, }) {
23
- const baseResolver = resolver || Awilix.asClass(useClass, options);
25
+ const baseResolver = resolver || this.inquirerResolverFactory.create(useClass, options);
24
26
  const interceptors = [
25
27
  ...(this.resolversByModule.get(module)?.values() ?? []),
26
28
  ];
@@ -37,7 +39,7 @@ export class InterceptorProcessor {
37
39
  }
38
40
  createInterceptedProviderInstance(instance, metadataTarget, resolveInterceptors, moduleName) {
39
41
  const wrappers = new Map();
40
- const targetBoundMethods = new Set();
42
+ let selfCallsPrepared = false;
41
43
  const self = this;
42
44
  return new Proxy(instance, {
43
45
  get(target, propertyKey, proxyReceiver) {
@@ -53,50 +55,21 @@ export class InterceptorProcessor {
53
55
  return existing;
54
56
  const wrapped = (...args) => {
55
57
  const interceptors = resolveInterceptors.map((resolve) => resolve());
56
- const metadataByToken = new Map();
57
- for (const interceptor of interceptors) {
58
- const state = resolveDecoratorState(metadataTarget, interceptor.token);
59
- if (state === null)
60
- continue;
61
- const method = state.methods.get(propertyKey);
62
- if (method !== undefined) {
63
- metadataByToken.set(interceptor.token.stateSymbol, {
64
- state,
65
- method,
66
- });
67
- }
58
+ if (!selfCallsPrepared) {
59
+ self.prepareSelfCalls(target, metadataTarget, interceptors, proxyReceiver);
60
+ selfCallsPrepared = true;
68
61
  }
69
- const receiver = !self.devtoolsTracer || targetBoundMethods.has(propertyKey)
70
- ? target
71
- : proxyReceiver;
72
- const apply = () => {
73
- if (metadataByToken.size === 0)
74
- return value.apply(receiver, args);
75
- return self.callWithInterceptorChain({
76
- target: receiver,
77
- methodName: propertyKey,
78
- moduleName,
79
- args,
80
- metadataByToken,
81
- interceptors,
82
- proceed: () => value.apply(receiver, args),
83
- });
84
- };
85
- if (receiver === target)
86
- return apply();
87
- return applyWithPrivateFieldFallback(apply, () => {
88
- targetBoundMethods.add(propertyKey);
89
- if (metadataByToken.size === 0)
90
- return value.apply(target, args);
91
- return self.callWithInterceptorChain({
92
- target,
93
- methodName: propertyKey,
94
- moduleName,
95
- args,
96
- metadataByToken,
97
- interceptors,
98
- proceed: () => value.apply(target, args),
99
- });
62
+ const metadataByToken = self.collectInterceptorMetadata(metadataTarget, propertyKey, interceptors);
63
+ if (metadataByToken.size === 0)
64
+ return value.apply(target, args);
65
+ return self.callWithInterceptorChain({
66
+ target,
67
+ methodName: propertyKey,
68
+ moduleName,
69
+ args,
70
+ metadataByToken,
71
+ interceptors,
72
+ proceed: () => value.apply(target, args),
100
73
  });
101
74
  };
102
75
  wrappers.set(propertyKey, wrapped);
@@ -104,6 +77,45 @@ export class InterceptorProcessor {
104
77
  },
105
78
  });
106
79
  }
80
+ prepareSelfCalls(instance, metadataTarget, interceptors, proxy) {
81
+ const methodNames = new Set();
82
+ for (const interceptor of interceptors) {
83
+ const state = resolveDecoratorState(metadataTarget, interceptor.token);
84
+ for (const methodName of state?.methods.keys() ?? []) {
85
+ methodNames.add(methodName);
86
+ }
87
+ }
88
+ for (const methodName of methodNames) {
89
+ if (methodName === "constructor")
90
+ continue;
91
+ const method = Reflect.get(proxy, methodName);
92
+ if (typeof method !== "function" || isClassReference(method))
93
+ continue;
94
+ Object.defineProperty(instance, methodName, {
95
+ configurable: true,
96
+ enumerable: false,
97
+ writable: true,
98
+ ...Object.getOwnPropertyDescriptor(instance, methodName),
99
+ value: method,
100
+ });
101
+ }
102
+ }
103
+ collectInterceptorMetadata(metadataTarget, propertyKey, interceptors) {
104
+ const metadataByToken = new Map();
105
+ for (const interceptor of interceptors) {
106
+ const state = resolveDecoratorState(metadataTarget, interceptor.token);
107
+ if (state === null)
108
+ continue;
109
+ const method = state.methods.get(propertyKey);
110
+ if (method !== undefined) {
111
+ metadataByToken.set(interceptor.token.stateSymbol, {
112
+ state,
113
+ method,
114
+ });
115
+ }
116
+ }
117
+ return metadataByToken;
118
+ }
107
119
  callWithInterceptorChain({ target, methodName, moduleName, args, metadataByToken, interceptors, proceed, }) {
108
120
  const invoke = (index, next) => {
109
121
  if (index >= interceptors.length)
@@ -179,28 +191,6 @@ export class InterceptorProcessor {
179
191
  return this.devtoolsProcessorRef.current?.tracer;
180
192
  }
181
193
  }
182
- function applyWithPrivateFieldFallback(tryFn, fallbackFn) {
183
- try {
184
- const result = tryFn();
185
- if (isPromiseLike(result)) {
186
- return result.catch((error) => {
187
- if (!isPrivateMemberAccessError(error))
188
- throw error;
189
- return fallbackFn();
190
- });
191
- }
192
- return result;
193
- }
194
- catch (error) {
195
- if (!isPrivateMemberAccessError(error))
196
- throw error;
197
- return fallbackFn();
198
- }
199
- }
200
- function isPrivateMemberAccessError(error) {
201
- return (error instanceof TypeError &&
202
- /Cannot (read|access) private (member|method)/.test(error.message));
203
- }
204
194
  function serializeMetadata(value, depth = 0) {
205
195
  if (depth > 5)
206
196
  return "[...]";
@@ -2,6 +2,7 @@ import * as Awilix from "awilix";
2
2
  import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
3
3
  import type { RegisteredModuleScope } from "../contexts/container-context-base.js";
4
4
  import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
5
+ import type { InquirerResolverFactory } from "../providers/inquirer-resolver-factory.js";
5
6
  type KeyedFeatureKind = "queryPreHandlers" | "commandPreHandlers" | "interceptors" | "initializers";
6
7
  type RegisterKeyedFeatureParams = {
7
8
  featureKind: KeyedFeatureKind;
@@ -11,10 +12,12 @@ type RegisterKeyedFeatureParams = {
11
12
  };
12
13
  export declare class KeyedFeatureRegistrar {
13
14
  private readonly providerOptions;
15
+ private readonly inquirerResolverFactory;
14
16
  private readonly devtoolsProcessorRef;
15
- constructor(providerOptions: Partial<Awilix.BuildResolverOptions<any>>, devtoolsProcessorRef?: DevtoolsProcessorRef);
17
+ constructor(providerOptions: Partial<Awilix.BuildResolverOptions<any>>, inquirerResolverFactory: InquirerResolverFactory, devtoolsProcessorRef?: DevtoolsProcessorRef);
16
18
  private readonly featureConfig;
17
19
  register<T>({ featureKind, module, scope, importedModulesWithScope, }: RegisterKeyedFeatureParams): Map<string, () => T>;
20
+ private registerFeature;
18
21
  private createFeatureResolver;
19
22
  private get devtoolsTracer();
20
23
  private getProviderOptions;
@@ -5,9 +5,11 @@ import { getOrCreateRequestScope, resolveFromRequestScope, } from "../request-sc
5
5
  import { hasUseClass } from "../type-guards.js";
6
6
  export class KeyedFeatureRegistrar {
7
7
  providerOptions;
8
+ inquirerResolverFactory;
8
9
  devtoolsProcessorRef;
9
- constructor(providerOptions, devtoolsProcessorRef = {}) {
10
+ constructor(providerOptions, inquirerResolverFactory, devtoolsProcessorRef = {}) {
10
11
  this.providerOptions = providerOptions;
12
+ this.inquirerResolverFactory = inquirerResolverFactory;
11
13
  this.devtoolsProcessorRef = devtoolsProcessorRef;
12
14
  }
13
15
  featureConfig = {
@@ -29,77 +31,72 @@ export class KeyedFeatureRegistrar {
29
31
  },
30
32
  };
31
33
  register({ featureKind, module, scope, importedModulesWithScope, }) {
32
- const ownerByKey = new Map();
33
- const resolverMap = new Map();
34
- const ownerByInitializerToken = new Map();
35
- const config = this.featureConfig[featureKind];
36
- const providerResolver = new ProviderResolver(undefined, this.getProviderOptions(featureKind), {}, getOrCreateRequestScope);
34
+ const context = {
35
+ config: this.featureConfig[featureKind],
36
+ featureKind,
37
+ module,
38
+ scope,
39
+ providerResolver: new ProviderResolver(undefined, this.getProviderOptions(featureKind), getOrCreateRequestScope, this.inquirerResolverFactory),
40
+ ownerByKey: new Map(),
41
+ resolverMap: new Map(),
42
+ ownerByInitializerToken: new Map(),
43
+ };
37
44
  for (const { module: importedModule, moduleScope: importedModuleScope, } of importedModulesWithScope) {
38
- for (const key of importedModule[config.exportKey] ?? []) {
45
+ for (const key of importedModule[context.config.exportKey] ?? []) {
39
46
  const feature = importedModule[featureKind]?.[key];
40
47
  if (!feature) {
41
48
  throw new ERRORS.InvalidProviderDefinitionError(importedModule.name, key);
42
49
  }
43
- if (ownerByKey.has(key)) {
44
- throw new ERRORS.FeatureNameConflictError(module.name, config.featureName, key, ownerByKey.get(key) ?? importedModule.name);
45
- }
46
- const symbol = Symbol(`${featureKind}_export_${importedModule.name}_${key}`);
47
- scope.register({
48
- [symbol]: this.createFeatureResolver({
49
- feature,
50
- featureKind,
51
- key,
52
- module: importedModule,
53
- providerResolver,
54
- resolutionScope: importedModuleScope.scope,
55
- wrapForExport: true,
56
- }),
50
+ this.registerFeature(context, {
51
+ key,
52
+ feature,
53
+ ownerModule: importedModule,
54
+ resolutionScope: importedModuleScope.scope,
55
+ wrapForExport: true,
57
56
  });
58
- if (featureKind === "initializers") {
59
- this.ensureInitializerTokenUniqueness({
60
- scope,
61
- symbol,
62
- ownerByInitializerToken,
63
- moduleName: module.name,
64
- featureName: feature.name || key,
65
- });
66
- }
67
- resolverMap.set(key, featureKind === "initializers"
68
- ? () => scope.resolve(symbol)
69
- : () => resolveFromRequestScope(scope, symbol));
70
- ownerByKey.set(key, importedModule.name);
71
57
  }
72
58
  }
73
59
  for (const [key, feature] of Object.entries(module[featureKind] ?? {})) {
74
- if (ownerByKey.has(key)) {
75
- throw new ERRORS.FeatureNameConflictError(module.name, config.featureName, key, ownerByKey.get(key) ?? module.name);
76
- }
77
- const symbol = Symbol(`${featureKind}_${module.name}_${key}`);
78
- scope.register({
79
- [symbol]: this.createFeatureResolver({
80
- feature,
81
- featureKind,
82
- key,
83
- module,
84
- providerResolver,
85
- resolutionScope: scope,
86
- }),
60
+ this.registerFeature(context, {
61
+ key,
62
+ feature,
63
+ ownerModule: module,
64
+ resolutionScope: scope,
65
+ });
66
+ }
67
+ return context.resolverMap;
68
+ }
69
+ registerFeature(context, params) {
70
+ if (context.ownerByKey.has(params.key)) {
71
+ throw new ERRORS.FeatureNameConflictError(context.module.name, context.config.featureName, params.key, context.ownerByKey.get(params.key) ?? params.ownerModule.name);
72
+ }
73
+ const symbol = Symbol(params.wrapForExport
74
+ ? `${context.featureKind}_export_${params.ownerModule.name}_${params.key}`
75
+ : `${context.featureKind}_${params.ownerModule.name}_${params.key}`);
76
+ context.scope.register({
77
+ [symbol]: this.createFeatureResolver({
78
+ feature: params.feature,
79
+ featureKind: context.featureKind,
80
+ key: params.key,
81
+ module: params.ownerModule,
82
+ providerResolver: context.providerResolver,
83
+ resolutionScope: params.resolutionScope,
84
+ wrapForExport: params.wrapForExport,
85
+ }),
86
+ });
87
+ if (context.featureKind === "initializers") {
88
+ this.ensureInitializerTokenUniqueness({
89
+ scope: context.scope,
90
+ symbol,
91
+ ownerByInitializerToken: context.ownerByInitializerToken,
92
+ moduleName: context.module.name,
93
+ featureName: params.feature.name || params.key,
87
94
  });
88
- if (featureKind === "initializers") {
89
- this.ensureInitializerTokenUniqueness({
90
- scope,
91
- symbol,
92
- ownerByInitializerToken,
93
- moduleName: module.name,
94
- featureName: feature.name || key,
95
- });
96
- }
97
- resolverMap.set(key, featureKind === "initializers"
98
- ? () => scope.resolve(symbol)
99
- : () => resolveFromRequestScope(scope, symbol));
100
- ownerByKey.set(key, module.name);
101
95
  }
102
- return resolverMap;
96
+ context.resolverMap.set(params.key, context.featureKind === "initializers"
97
+ ? () => context.scope.resolve(symbol)
98
+ : () => resolveFromRequestScope(context.scope, symbol));
99
+ context.ownerByKey.set(params.key, params.ownerModule.name);
103
100
  }
104
101
  createFeatureResolver({ feature, featureKind, key, module, providerResolver, resolutionScope, wrapForExport = false, }) {
105
102
  const resolver = providerResolver.resolveClassProvider({
@@ -0,0 +1,10 @@
1
+ import * as Awilix from "awilix";
2
+ export declare class InquirerResolverFactory {
3
+ private currentFrame;
4
+ private enabled;
5
+ enableTracking(): void;
6
+ private withConsumer;
7
+ create<T extends object>(useClass: Awilix.Constructor<T>, options: Awilix.BuildResolverOptions<T> & {
8
+ injectInquirer?: boolean;
9
+ }): Awilix.Resolver<T>;
10
+ }
@@ -0,0 +1,61 @@
1
+ import * as Awilix from "awilix";
2
+ import { InquirerRequiresTransientLifetimeError } from "../errors.js";
3
+ export class InquirerResolverFactory {
4
+ currentFrame;
5
+ enabled = false;
6
+ enableTracking() {
7
+ this.enabled = true;
8
+ }
9
+ withConsumer(consumer, resolve) {
10
+ const previous = this.currentFrame;
11
+ this.currentFrame = { consumer, parent: previous?.consumer };
12
+ try {
13
+ return resolve();
14
+ }
15
+ finally {
16
+ this.currentFrame = previous;
17
+ }
18
+ }
19
+ create(useClass, options) {
20
+ const { injectInquirer, ...resolverOptions } = options;
21
+ if (injectInquirer && options.lifetime !== Awilix.Lifetime.TRANSIENT) {
22
+ throw new InquirerRequiresTransientLifetimeError(useClass.name);
23
+ }
24
+ const baseResolver = Awilix.asClass(useClass, {
25
+ ...resolverOptions,
26
+ ...(injectInquirer && {
27
+ isLeakSafe: resolverOptions.isLeakSafe ?? true,
28
+ injector: (container) => ({
29
+ ...resolverOptions.injector?.(container),
30
+ inquirer: this.currentFrame?.parent,
31
+ }),
32
+ }),
33
+ });
34
+ return {
35
+ ...baseResolver,
36
+ resolve: (container) => {
37
+ if (!this.enabled)
38
+ return baseResolver.resolve(container);
39
+ const consumer = useClass.prototype;
40
+ if ((resolverOptions.injectionMode ?? container.options.injectionMode) ===
41
+ Awilix.InjectionMode.CLASSIC) {
42
+ return this.withConsumer(consumer, () => baseResolver.resolve(container));
43
+ }
44
+ const cradle = new Proxy(container.cradle, {
45
+ get: (target, key) => this.withConsumer(consumer, () => Reflect.get(target, key)),
46
+ });
47
+ const contextualContainer = new Proxy(container, {
48
+ get: (target, key) => {
49
+ if (key === "cradle")
50
+ return cradle;
51
+ if (key === "resolve") {
52
+ return (...args) => this.withConsumer(consumer, () => container.resolve(...args));
53
+ }
54
+ return Reflect.get(target, key);
55
+ },
56
+ });
57
+ return this.withConsumer(consumer, () => baseResolver.resolve(contextualContainer));
58
+ },
59
+ };
60
+ }
61
+ }
@@ -2,6 +2,7 @@ import * as Awilix from "awilix";
2
2
  import type { DevtoolsProcessorRef } from "../../devtools/devtools.types.js";
3
3
  import type { InternalModuleLike as M } from "../modules/runtime-module.types.js";
4
4
  import type { InterceptorProcessor } from "../processors/interceptor-processor.js";
5
+ import type { InquirerResolverFactory } from "./inquirer-resolver-factory.js";
5
6
  import type { AnyProvider } from "./provider.types.js";
6
7
  export type RequestScopeResolver = (scope: Awilix.AwilixContainer) => Awilix.AwilixContainer;
7
8
  type ResolveProviderParams = {
@@ -21,13 +22,15 @@ type ResolveClassProviderParams = {
21
22
  export declare class ProviderResolver {
22
23
  private readonly interceptorProcessor;
23
24
  private readonly providerOptions;
25
+ private readonly requestScopeResolver;
26
+ private readonly inquirerResolverFactory;
24
27
  private readonly devtoolsProcessorRef;
25
- private readonly requestScopeResolver?;
26
- constructor(interceptorProcessor: InterceptorProcessor | undefined, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, devtoolsProcessorRef: DevtoolsProcessorRef, requestScopeResolver?: RequestScopeResolver | undefined);
28
+ constructor(interceptorProcessor: InterceptorProcessor | undefined, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, requestScopeResolver: RequestScopeResolver | undefined, inquirerResolverFactory: InquirerResolverFactory, devtoolsProcessorRef?: DevtoolsProcessorRef);
27
29
  static mergeResolverOptions(module: M, providerOptions: Partial<Awilix.BuildResolverOptions<any>>, options?: Partial<Awilix.BuildResolverOptions<any>>): Awilix.BuildResolverOptions<any>;
28
30
  resolveClassProvider({ provider, resolutionScope, module, wrapForExport, }: ResolveClassProviderParams): Awilix.Resolver<any>;
29
31
  resolveProvider({ key, moduleId, provider, resolutionScope, module, }: ResolveProviderParams): Awilix.Resolver<any>;
30
32
  resolveProviderAsync({ key, moduleId, provider, resolutionScope, module, }: ResolveProviderParams): Promise<Awilix.Resolver<any>>;
33
+ private resolveFactoryProvider;
31
34
  private extractResolverOptions;
32
35
  private createProxyResolver;
33
36
  private get devtoolsTracer();