-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
discovery.service.ts
268 lines (238 loc) · 8.38 KB
/
discovery.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
import { Injectable, Scope, Type } from '@nestjs/common';
import { PATH_METADATA } from '@nestjs/common/constants';
import { STATIC_CONTEXT } from '@nestjs/core/injector/constants';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { Module } from '@nestjs/core/injector/module';
import { ModulesContainer } from '@nestjs/core/injector/modules-container';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { flatMap, get, isNil, some, uniqBy } from 'lodash';
import {
DiscoveredClass,
DiscoveredClassWithMeta,
DiscoveredMethodWithMeta,
Filter,
MetaKey,
} from './discovery.interfaces';
/**
* Attempts to retrieve meta information from a Nest DiscoveredClass component
* @param key The meta key to retrieve data from
* @param component The discovered component to retrieve meta from
*/
export function getComponentMetaAtKey<T>(
key: MetaKey,
component: DiscoveredClass,
): T | undefined {
const dependencyMeta = Reflect.getMetadata(
key,
component.dependencyType,
) as T;
if (dependencyMeta) {
return dependencyMeta;
}
if (component.injectType != null) {
return Reflect.getMetadata(key, component.injectType) as T;
}
}
/**
* A filter that can be used to search for DiscoveredClasses in an App that contain meta attached to a
* certain key
* @param key The meta key to search for
*/
export const withMetaAtKey: (key: MetaKey) => Filter<DiscoveredClass> =
(key) => (component) => {
const metaTargets: Function[] = [
get(component, 'instance.constructor') as any,
component.injectType as Function,
].filter((x) => !isNil(x));
return some(metaTargets, (x) => Reflect.getMetadata(key, x));
};
@Injectable()
export class DiscoveryService {
private discoveredControllers?: Promise<DiscoveredClass[]>;
private discoveredProviders?: Promise<DiscoveredClass[]>;
constructor(
private readonly modulesContainer: ModulesContainer,
private readonly metadataScanner: MetadataScanner,
) {}
/**
* Discovers all providers in a Nest App that match a filter
* @param filter
*/
async providers(filter: Filter<DiscoveredClass>): Promise<DiscoveredClass[]> {
if (!this.discoveredProviders) {
this.discoveredProviders = this.discover('providers');
}
return (await this.discoveredProviders).filter((x) => filter(x));
}
/**
* Discovers all controller methods that either directly have a certain meta key attached to them
* or belong to a controller that has the same meta key attached to them
* @param metaKey The meta key to scan for
* @param metaFilter An optional filter for the contents of the meta object
*/
async methodsAndControllerMethodsWithMetaAtKey<T>(
metaKey: MetaKey,
metaFilter: Filter<T> = () => true,
): Promise<DiscoveredMethodWithMeta<T>[]> {
const controllersWithMeta = (
await this.controllersWithMetaAtKey<T>(metaKey)
).filter((x) => metaFilter(x.meta));
const methodsFromDecoratedControllers = flatMap(
controllersWithMeta,
(controller) => {
return this.classMethodsWithMetaAtKey<T>(
controller.discoveredClass,
PATH_METADATA,
);
},
);
const decoratedMethods = (
await this.controllerMethodsWithMetaAtKey<T>(metaKey)
).filter((x) => metaFilter(x.meta));
return uniqBy(
[...methodsFromDecoratedControllers, ...decoratedMethods],
(x) => x.discoveredMethod.handler,
);
}
/**
* Discovers all providers in an App that have meta at a specific key and returns the provider(s) and associated meta
* @param metaKey The metakey to scan for
*/
async providersWithMetaAtKey<T>(
metaKey: MetaKey,
): Promise<DiscoveredClassWithMeta<T>[]> {
const providers = await this.providers(withMetaAtKey(metaKey));
return providers.map((x) => ({
meta: getComponentMetaAtKey<T>(metaKey, x) as T,
discoveredClass: x,
}));
}
/**
* Discovers all controllers in a Nest App that match a filter
* @param filter
*/
async controllers(
filter: Filter<DiscoveredClass>,
): Promise<DiscoveredClass[]> {
if (!this.discoveredControllers) {
this.discoveredControllers = this.discover('controllers');
}
return (await this.discoveredControllers).filter((x) => filter(x));
}
/**
* Discovers all controllers in an App that have meta at a specific key and returns the controller(s) and associated meta
* @param metaKey The metakey to scan for
*/
async controllersWithMetaAtKey<T>(
metaKey: MetaKey,
): Promise<DiscoveredClassWithMeta<T>[]> {
const controllers = await this.controllers(withMetaAtKey(metaKey));
return controllers.map((x) => ({
meta: getComponentMetaAtKey<T>(metaKey, x) as T,
discoveredClass: x,
}));
}
/**
* Discovers all method handlers matching a particular metakey from a Provider or Controller
* @param component
* @param metaKey
*/
classMethodsWithMetaAtKey<T>(
component: DiscoveredClass,
metaKey: MetaKey,
): DiscoveredMethodWithMeta<T>[] {
const { instance } = component;
if (!instance) {
return [];
}
const prototype = Object.getPrototypeOf(instance);
return this.metadataScanner
.getAllMethodNames(prototype)
.map((name) =>
this.extractMethodMetaAtKey<T>(metaKey, component, prototype, name),
)
.filter((x) => !isNil(x.meta));
}
/**
* Discovers all the methods that exist on providers in a Nest App that contain metadata under a specific key
* @param metaKey The metakey to scan for
* @param providerFilter A predicate used to limit the providers being scanned. Defaults to all providers in the app module
*/
async providerMethodsWithMetaAtKey<T>(
metaKey: MetaKey,
providerFilter: Filter<DiscoveredClass> = () => true,
): Promise<DiscoveredMethodWithMeta<T>[]> {
const providers = await this.providers(providerFilter);
return flatMap(providers, (provider) =>
this.classMethodsWithMetaAtKey<T>(provider, metaKey),
);
}
/**
* Discovers all the methods that exist on controllers in a Nest App that contain metadata under a specific key
* @param metaKey The metakey to scan for
* @param controllerFilter A predicate used to limit the controllers being scanned. Defaults to all providers in the app module
*/
async controllerMethodsWithMetaAtKey<T>(
metaKey: MetaKey,
controllerFilter: Filter<DiscoveredClass> = () => true,
): Promise<DiscoveredMethodWithMeta<T>[]> {
const controllers = await this.controllers(controllerFilter);
return flatMap(controllers, (controller) =>
this.classMethodsWithMetaAtKey<T>(controller, metaKey),
);
}
private async toDiscoveredClass(
nestModule: Module,
wrapper: InstanceWrapper<any>,
): Promise<DiscoveredClass> {
const instanceHost = wrapper.getInstanceByContextId(
STATIC_CONTEXT,
wrapper && wrapper.id ? wrapper.id : undefined,
);
if (instanceHost.isPending && !instanceHost.isResolved) {
await instanceHost.donePromise;
}
return {
name: wrapper.name as string,
instance: instanceHost.instance,
injectType: wrapper.metatype,
dependencyType: get(instanceHost, 'instance.constructor'),
parentModule: {
name: nestModule.metatype.name,
instance: nestModule.instance,
injectType: nestModule.metatype,
dependencyType: nestModule.instance.constructor as Type<object>,
},
};
}
private extractMethodMetaAtKey<T>(
metaKey: MetaKey,
discoveredClass: DiscoveredClass,
prototype: any,
methodName: string,
): DiscoveredMethodWithMeta<T> {
const handler = prototype[methodName];
const meta: T = Reflect.getMetadata(metaKey, handler);
return {
meta,
discoveredMethod: {
handler,
methodName,
parentClass: discoveredClass,
},
};
}
private async discover(component: 'providers' | 'controllers') {
const modulesMap = [...this.modulesContainer.entries()];
return Promise.all(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
flatMap(modulesMap, ([key, nestModule]) => {
const components = [...nestModule[component].values()];
return components
.filter((component) => component.scope !== Scope.REQUEST)
.map((component) => this.toDiscoveredClass(nestModule, component));
}),
);
}
}