@nestia/core 7.0.0-dev.20250607 → 7.0.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 (75) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +92 -92
  3. package/package.json +3 -3
  4. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  5. package/src/decorators/DynamicModule.ts +43 -43
  6. package/src/decorators/EncryptedBody.ts +101 -101
  7. package/src/decorators/EncryptedController.ts +38 -38
  8. package/src/decorators/EncryptedModule.ts +100 -100
  9. package/src/decorators/EncryptedRoute.ts +219 -219
  10. package/src/decorators/HumanRoute.ts +22 -22
  11. package/src/decorators/NoTransformConfigurationError.ts +32 -32
  12. package/src/decorators/PlainBody.ts +79 -79
  13. package/src/decorators/SwaggerCustomizer.ts +115 -115
  14. package/src/decorators/SwaggerExample.ts +100 -100
  15. package/src/decorators/TypedBody.ts +59 -59
  16. package/src/decorators/TypedException.ts +166 -166
  17. package/src/decorators/TypedFormData.ts +195 -195
  18. package/src/decorators/TypedHeaders.ts +64 -64
  19. package/src/decorators/TypedParam.ts +77 -77
  20. package/src/decorators/TypedQuery.ts +245 -245
  21. package/src/decorators/TypedRoute.ts +214 -214
  22. package/src/decorators/WebSocketRoute.ts +242 -242
  23. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  24. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  25. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  26. package/src/decorators/internal/get_path_and_querify.ts +108 -108
  27. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  28. package/src/decorators/internal/get_text_body.ts +20 -20
  29. package/src/decorators/internal/headers_to_object.ts +13 -13
  30. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  31. package/src/decorators/internal/load_controller.ts +49 -49
  32. package/src/decorators/internal/route_error.ts +45 -45
  33. package/src/decorators/internal/validate_request_body.ts +74 -74
  34. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  35. package/src/decorators/internal/validate_request_headers.ts +86 -86
  36. package/src/decorators/internal/validate_request_query.ts +74 -74
  37. package/src/index.ts +5 -5
  38. package/src/module.ts +22 -22
  39. package/src/options/INestiaTransformOptions.ts +38 -38
  40. package/src/options/INestiaTransformProject.ts +8 -8
  41. package/src/options/IRequestBodyValidator.ts +20 -20
  42. package/src/options/IRequestFormDataProps.ts +27 -27
  43. package/src/options/IRequestHeadersValidator.ts +22 -22
  44. package/src/options/IRequestQueryValidator.ts +20 -20
  45. package/src/options/IResponseBodyQuerifier.ts +25 -25
  46. package/src/options/IResponseBodyStringifier.ts +30 -30
  47. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  48. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  49. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  50. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  51. package/src/programmers/TypedParamProgrammer.ts +33 -33
  52. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  53. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  54. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  55. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  56. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  57. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  58. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  59. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  60. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  61. package/src/transform.ts +35 -35
  62. package/src/transformers/FileTransformer.ts +110 -110
  63. package/src/transformers/MethodTransformer.ts +103 -103
  64. package/src/transformers/NodeTransformer.ts +23 -23
  65. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  66. package/src/transformers/ParameterTransformer.ts +57 -57
  67. package/src/transformers/TypedRouteTransformer.ts +85 -85
  68. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  69. package/src/typings/Creator.ts +3 -3
  70. package/src/typings/get-function-location.d.ts +7 -7
  71. package/src/utils/ArrayUtil.ts +7 -7
  72. package/src/utils/ExceptionManager.ts +112 -112
  73. package/src/utils/Singleton.ts +20 -20
  74. package/src/utils/SourceFinder.ts +57 -57
  75. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,429 +1,429 @@
1
- /// <reference path="../typings/get-function-location.d.ts" />
2
- import { INestApplication, VersioningType } from "@nestjs/common";
3
- import {
4
- HOST_METADATA,
5
- MODULE_PATH,
6
- PATH_METADATA,
7
- SCOPE_OPTIONS_METADATA,
8
- VERSION_METADATA,
9
- } from "@nestjs/common/constants";
10
- import { VERSION_NEUTRAL, VersionValue } from "@nestjs/common/interfaces";
11
- import { NestContainer } from "@nestjs/core";
12
- import { InstanceWrapper } from "@nestjs/core/injector/instance-wrapper";
13
- import { Module } from "@nestjs/core/injector/module";
14
- import getFunctionLocation from "get-function-location";
15
- import { IncomingMessage, Server } from "http";
16
- import path from "path";
17
- import { Path } from "path-parser";
18
- import { Duplex } from "stream";
19
- import { WebSocketAcceptor } from "tgrid";
20
- import typia from "typia";
21
- import WebSocket from "ws";
22
-
23
- import { IWebSocketRouteReflect } from "../decorators/internal/IWebSocketRouteReflect";
24
- import { ArrayUtil } from "../utils/ArrayUtil";
25
- import { VersioningStrategy } from "../utils/VersioningStrategy";
26
-
27
- export class WebSocketAdaptor {
28
- public static async upgrade(
29
- app: INestApplication,
30
- ): Promise<WebSocketAdaptor> {
31
- return new this(app, await visitApplication(app));
32
- }
33
-
34
- public readonly close = async (): Promise<void> =>
35
- new Promise((resolve) => {
36
- this.http.off("close", this.close);
37
- this.http.off("upgrade", this.handleUpgrade);
38
- this.ws.close(() => resolve());
39
- });
40
-
41
- private constructor(app: INestApplication, operations: IOperator[]) {
42
- this.operators = operations;
43
- this.ws = new WebSocket.Server({ noServer: true });
44
- this.http = app.getHttpServer();
45
- this.http.on("close", this.close);
46
- this.http.on("upgrade", this.handleUpgrade);
47
- }
48
-
49
- private readonly handleUpgrade = (
50
- request: IncomingMessage,
51
- duplex: Duplex,
52
- head: Buffer,
53
- ) => {
54
- this.ws.handleUpgrade(request, duplex, head, (client, request) =>
55
- WebSocketAcceptor.upgrade(
56
- request,
57
- client as any,
58
- async (acceptor): Promise<void> => {
59
- const path: string = (() => {
60
- const index: number = acceptor.path.indexOf("?");
61
- return index === -1 ? acceptor.path : acceptor.path.slice(0, index);
62
- })();
63
- for (const op of this.operators) {
64
- const params: Record<string, string> | null = op.parser.test(path);
65
- if (params !== null)
66
- try {
67
- await op.handler({ params, acceptor });
68
- } catch (error) {
69
- if (
70
- acceptor.state === WebSocketAcceptor.State.OPEN ||
71
- acceptor.state === WebSocketAcceptor.State.ACCEPTING
72
- )
73
- await acceptor.reject(
74
- 1008,
75
- error instanceof Error
76
- ? JSON.stringify({ ...error })
77
- : "unknown error",
78
- );
79
- } finally {
80
- return;
81
- }
82
- }
83
- await acceptor.reject(1002, `WebSocket API not found`);
84
- },
85
- ),
86
- );
87
- };
88
-
89
- private readonly http: Server;
90
- private readonly operators: IOperator[];
91
- private readonly ws: WebSocket.Server;
92
- }
93
-
94
- const visitApplication = async (
95
- app: INestApplication,
96
- ): Promise<IOperator[]> => {
97
- const operators: IOperator[] = [];
98
- const errors: IControllerError[] = [];
99
-
100
- const config: IConfig = {
101
- globalPrefix:
102
- typeof (app as any).config?.globalPrefix === "string"
103
- ? (app as any).config.globalPrefix
104
- : undefined,
105
- versioning: (() => {
106
- const versioning = (app as any).config?.versioningOptions;
107
- return versioning === undefined || versioning.type !== VersioningType.URI
108
- ? undefined
109
- : {
110
- prefix:
111
- versioning.prefix === undefined || versioning.prefix === false
112
- ? "v"
113
- : versioning.prefix,
114
- defaultVersion: versioning.defaultVersion,
115
- };
116
- })(),
117
- };
118
- const container: NestContainer = (app as any).container as NestContainer;
119
- const modules: Module[] = [...container.getModules().values()].filter(
120
- (m) => !!m.controllers?.size,
121
- );
122
- for (const m of modules) {
123
- const modulePrefix: string =
124
- Reflect.getMetadata(
125
- MODULE_PATH + container.getModules().applicationId,
126
- m.metatype,
127
- ) ??
128
- Reflect.getMetadata(MODULE_PATH, m.metatype) ??
129
- "";
130
- for (const controller of m.controllers.values())
131
- await visitController({
132
- config,
133
- errors,
134
- operators,
135
- controller,
136
- modulePrefix,
137
- });
138
- }
139
- if (errors.length)
140
- throw new Error(
141
- [
142
- `WebSocketAdaptor: ${errors.length} error(s) found:`,
143
- ``,
144
- ...errors.map((e) =>
145
- [
146
- ` - controller: ${e.name}`,
147
- ` methods:`,
148
- ...e.methods.map((m) =>
149
- [
150
- ` - name: ${m.name}`,
151
- ` file: ${m.source}:${m.line}:${m.column}`,
152
- ` reasons:`,
153
- ...m.messages.map(
154
- (msg) =>
155
- ` - ${msg
156
- .split("\n")
157
- .map((str, i) => (i == 0 ? str : ` ${str}`))
158
- .join("\n")}`,
159
- ),
160
- ].join("\n"),
161
- ),
162
- ].join("\n"),
163
- ),
164
- ].join("\n"),
165
- );
166
- return operators;
167
- };
168
-
169
- const visitController = async (props: {
170
- config: IConfig;
171
- errors: IControllerError[];
172
- operators: IOperator[];
173
- controller: InstanceWrapper<object>;
174
- modulePrefix: string;
175
- }): Promise<void> => {
176
- if (
177
- ArrayUtil.has(
178
- Reflect.getMetadataKeys(props.controller.metatype as Function),
179
- PATH_METADATA,
180
- HOST_METADATA,
181
- SCOPE_OPTIONS_METADATA,
182
- ) === false
183
- )
184
- return;
185
-
186
- const methodErrors: IMethodError[] = [];
187
- const controller: IController = {
188
- name: props.controller.name,
189
- instance: props.controller.instance,
190
- constructor: props.controller.metatype as Function,
191
- prototype: Object.getPrototypeOf(props.controller.instance),
192
- prefixes: (() => {
193
- const value: string | string[] = Reflect.getMetadata(
194
- PATH_METADATA,
195
- props.controller.metatype as object,
196
- );
197
- if (typeof value === "string") return [value];
198
- else if (value.length === 0) return [""];
199
- else return value;
200
- })(),
201
- versions: props.config.versioning
202
- ? VersioningStrategy.cast(
203
- Reflect.getMetadata(
204
- VERSION_METADATA,
205
- props.controller.metatype as Function,
206
- ),
207
- )
208
- : undefined,
209
- modulePrefix: props.modulePrefix,
210
- };
211
- for (const mk of getOwnPropertyNames(controller.prototype).filter(
212
- (key) =>
213
- key !== "constructor" && typeof controller.prototype[key] === "function",
214
- )) {
215
- const errorMessages: string[] = [];
216
- visitMethod({
217
- config: props.config,
218
- operators: props.operators,
219
- controller,
220
- method: {
221
- key: mk,
222
- value: controller.prototype[mk],
223
- },
224
- report: (msg) => errorMessages.push(msg),
225
- });
226
- if (errorMessages.length) {
227
- const file = await getFunctionLocation(controller.prototype[mk]);
228
- methodErrors.push({
229
- name: mk,
230
- messages: errorMessages,
231
- ...file,
232
- source: path.relative(
233
- process.cwd(),
234
- file.source.replace("file:///", ""),
235
- ),
236
- });
237
- }
238
- }
239
-
240
- if (methodErrors.length)
241
- props.errors.push({
242
- name: controller.name,
243
- methods: methodErrors,
244
- });
245
- };
246
-
247
- const visitMethod = (props: {
248
- config: IConfig;
249
- operators: IOperator[];
250
- controller: IController;
251
- method: Entry<Function>;
252
- report: (message: string) => void;
253
- }): void => {
254
- const route: IWebSocketRouteReflect | undefined = Reflect.getMetadata(
255
- "nestia/WebSocketRoute",
256
- props.method.value,
257
- );
258
- if (typia.is<IWebSocketRouteReflect>(route) === false) return;
259
-
260
- const parameters: IWebSocketRouteReflect.IArgument[] = (
261
- (Reflect.getMetadata(
262
- "nestia/WebSocketRoute/Parameters",
263
- props.controller.prototype,
264
- props.method.key,
265
- ) ?? []) as IWebSocketRouteReflect.IArgument[]
266
- ).sort((a, b) => a.index - b.index);
267
- // acceptor must be
268
- if (parameters.some((p) => p.category === "acceptor") === false)
269
- return props.report(
270
- "@WebSocketRoute.Acceptor() decorated parameter must be.",
271
- );
272
- // length of parameters must be fulfilled
273
- if (parameters.length !== props.method.value.length)
274
- return props.report(
275
- [
276
- "Every parameters must be one of below:",
277
- " - @WebSocketRoute.Acceptor()",
278
- " - @WebSocketRoute.Driver()",
279
- " - @WebSocketRoute.Header()",
280
- " - @WebSocketRoute.Param()",
281
- " - @WebSocketRoute.Query()",
282
- ].join("\n"),
283
- );
284
-
285
- const versions: string[] = VersioningStrategy.merge(props.config.versioning)([
286
- ...(props.controller.versions ?? []),
287
- ...VersioningStrategy.cast(
288
- Reflect.getMetadata(VERSION_METADATA, props.method.value),
289
- ),
290
- ]);
291
- for (const v of versions)
292
- for (const cp of wrapPaths(props.controller.prefixes))
293
- for (const mp of wrapPaths(route.paths)) {
294
- const parser: Path = new Path(
295
- "/" +
296
- [
297
- props.config.globalPrefix ?? "",
298
- v,
299
- props.controller.modulePrefix,
300
- cp,
301
- mp,
302
- ]
303
- .filter((str) => !!str.length)
304
- .join("/")
305
- .split("/")
306
- .filter((str) => str.length)
307
- .join("/"),
308
- );
309
- const pathParams: IWebSocketRouteReflect.IParam[] = parameters.filter(
310
- (p) => p.category === "param",
311
- ) as IWebSocketRouteReflect.IParam[];
312
- if (parser.params.length !== pathParams.length) {
313
- props.report(
314
- [
315
- `Path "${parser}" must have same number of parameters with @WebSocketRoute.Param()`,
316
- ` - path: ${JSON.stringify(parser.params)}`,
317
- ` - arguments: ${JSON.stringify(pathParams.map((p) => p.field))}`,
318
- ].join("\n"),
319
- );
320
- continue;
321
- }
322
- const meet: boolean = pathParams
323
- .map((p) => {
324
- const has: boolean = parser.params.includes(p.field);
325
- if (has === false)
326
- props.report(
327
- `Path "${parser}" must have parameter "${p.field}" with @WebSocketRoute.Param()`,
328
- );
329
- return has;
330
- })
331
- .every((b) => b);
332
- if (meet === false) continue;
333
-
334
- props.operators.push({
335
- parser,
336
- handler: async (input: {
337
- params: Record<string, string>;
338
- acceptor: WebSocketAcceptor<any, any, any>;
339
- }): Promise<void> => {
340
- const args: any[] = [];
341
- try {
342
- for (const p of parameters)
343
- if (p.category === "acceptor") args.push(input.acceptor);
344
- else if (p.category === "driver")
345
- args.push(input.acceptor.getDriver());
346
- else if (p.category === "header") {
347
- const error: Error | null = p.validate(input.acceptor.header);
348
- if (error !== null) throw error;
349
- args.push(input.acceptor.header);
350
- } else if (p.category === "param")
351
- args.push(p.assert(input.params[p.field]));
352
- else if (p.category === "query") {
353
- const query: any | Error = p.validate(
354
- new URLSearchParams(
355
- input.acceptor.path.indexOf("?") !== -1
356
- ? input.acceptor.path.split("?")[1]
357
- : "",
358
- ),
359
- );
360
- if (query instanceof Error) throw query;
361
- args.push(query);
362
- }
363
- } catch (exp) {
364
- await input.acceptor.reject(
365
- 1003,
366
- exp instanceof Error
367
- ? JSON.stringify({ ...exp })
368
- : "unknown error",
369
- );
370
- return;
371
- }
372
- await props.method.value.call(props.controller.instance, ...args);
373
- },
374
- });
375
- }
376
- };
377
-
378
- const wrapPaths = (value: string[]) => (value.length === 0 ? [""] : value);
379
- const getOwnPropertyNames = (prototype: any): string[] => {
380
- const result: Set<string> = new Set();
381
- const iterate = (m: any) => {
382
- if (m === null) return;
383
- for (const k of Object.getOwnPropertyNames(m)) result.add(k);
384
- iterate(Object.getPrototypeOf(m));
385
- };
386
- iterate(prototype);
387
- return Array.from(result);
388
- };
389
-
390
- interface Entry<T> {
391
- key: string;
392
- value: T;
393
- }
394
-
395
- interface IController {
396
- name: string;
397
- versions: Array<string | typeof VERSION_NEUTRAL> | undefined;
398
- instance: object;
399
- constructor: Function;
400
- prototype: any;
401
- prefixes: string[];
402
- modulePrefix: string;
403
- }
404
- interface IOperator {
405
- parser: Path;
406
- handler: (props: {
407
- params: Record<string, string>;
408
- acceptor: WebSocketAcceptor<any, any, any>;
409
- }) => Promise<any>;
410
- }
411
- interface IConfig {
412
- globalPrefix?: string;
413
- versioning?: {
414
- prefix: string;
415
- defaultVersion?: VersionValue;
416
- };
417
- }
418
-
419
- interface IControllerError {
420
- name: string;
421
- methods: IMethodError[];
422
- }
423
- interface IMethodError {
424
- name: string;
425
- messages: string[];
426
- source: string;
427
- line: number;
428
- column: number;
429
- }
1
+ /// <reference path="../typings/get-function-location.d.ts" />
2
+ import { INestApplication, VersioningType } from "@nestjs/common";
3
+ import {
4
+ HOST_METADATA,
5
+ MODULE_PATH,
6
+ PATH_METADATA,
7
+ SCOPE_OPTIONS_METADATA,
8
+ VERSION_METADATA,
9
+ } from "@nestjs/common/constants";
10
+ import { VERSION_NEUTRAL, VersionValue } from "@nestjs/common/interfaces";
11
+ import { NestContainer } from "@nestjs/core";
12
+ import { InstanceWrapper } from "@nestjs/core/injector/instance-wrapper";
13
+ import { Module } from "@nestjs/core/injector/module";
14
+ import getFunctionLocation from "get-function-location";
15
+ import { IncomingMessage, Server } from "http";
16
+ import path from "path";
17
+ import { Path } from "path-parser";
18
+ import { Duplex } from "stream";
19
+ import { WebSocketAcceptor } from "tgrid";
20
+ import typia from "typia";
21
+ import WebSocket from "ws";
22
+
23
+ import { IWebSocketRouteReflect } from "../decorators/internal/IWebSocketRouteReflect";
24
+ import { ArrayUtil } from "../utils/ArrayUtil";
25
+ import { VersioningStrategy } from "../utils/VersioningStrategy";
26
+
27
+ export class WebSocketAdaptor {
28
+ public static async upgrade(
29
+ app: INestApplication,
30
+ ): Promise<WebSocketAdaptor> {
31
+ return new this(app, await visitApplication(app));
32
+ }
33
+
34
+ public readonly close = async (): Promise<void> =>
35
+ new Promise((resolve) => {
36
+ this.http.off("close", this.close);
37
+ this.http.off("upgrade", this.handleUpgrade);
38
+ this.ws.close(() => resolve());
39
+ });
40
+
41
+ private constructor(app: INestApplication, operations: IOperator[]) {
42
+ this.operators = operations;
43
+ this.ws = new WebSocket.Server({ noServer: true });
44
+ this.http = app.getHttpServer();
45
+ this.http.on("close", this.close);
46
+ this.http.on("upgrade", this.handleUpgrade);
47
+ }
48
+
49
+ private readonly handleUpgrade = (
50
+ request: IncomingMessage,
51
+ duplex: Duplex,
52
+ head: Buffer,
53
+ ) => {
54
+ this.ws.handleUpgrade(request, duplex, head, (client, request) =>
55
+ WebSocketAcceptor.upgrade(
56
+ request,
57
+ client as any,
58
+ async (acceptor): Promise<void> => {
59
+ const path: string = (() => {
60
+ const index: number = acceptor.path.indexOf("?");
61
+ return index === -1 ? acceptor.path : acceptor.path.slice(0, index);
62
+ })();
63
+ for (const op of this.operators) {
64
+ const params: Record<string, string> | null = op.parser.test(path);
65
+ if (params !== null)
66
+ try {
67
+ await op.handler({ params, acceptor });
68
+ } catch (error) {
69
+ if (
70
+ acceptor.state === WebSocketAcceptor.State.OPEN ||
71
+ acceptor.state === WebSocketAcceptor.State.ACCEPTING
72
+ )
73
+ await acceptor.reject(
74
+ 1008,
75
+ error instanceof Error
76
+ ? JSON.stringify({ ...error })
77
+ : "unknown error",
78
+ );
79
+ } finally {
80
+ return;
81
+ }
82
+ }
83
+ await acceptor.reject(1002, `WebSocket API not found`);
84
+ },
85
+ ),
86
+ );
87
+ };
88
+
89
+ private readonly http: Server;
90
+ private readonly operators: IOperator[];
91
+ private readonly ws: WebSocket.Server;
92
+ }
93
+
94
+ const visitApplication = async (
95
+ app: INestApplication,
96
+ ): Promise<IOperator[]> => {
97
+ const operators: IOperator[] = [];
98
+ const errors: IControllerError[] = [];
99
+
100
+ const config: IConfig = {
101
+ globalPrefix:
102
+ typeof (app as any).config?.globalPrefix === "string"
103
+ ? (app as any).config.globalPrefix
104
+ : undefined,
105
+ versioning: (() => {
106
+ const versioning = (app as any).config?.versioningOptions;
107
+ return versioning === undefined || versioning.type !== VersioningType.URI
108
+ ? undefined
109
+ : {
110
+ prefix:
111
+ versioning.prefix === undefined || versioning.prefix === false
112
+ ? "v"
113
+ : versioning.prefix,
114
+ defaultVersion: versioning.defaultVersion,
115
+ };
116
+ })(),
117
+ };
118
+ const container: NestContainer = (app as any).container as NestContainer;
119
+ const modules: Module[] = [...container.getModules().values()].filter(
120
+ (m) => !!m.controllers?.size,
121
+ );
122
+ for (const m of modules) {
123
+ const modulePrefix: string =
124
+ Reflect.getMetadata(
125
+ MODULE_PATH + container.getModules().applicationId,
126
+ m.metatype,
127
+ ) ??
128
+ Reflect.getMetadata(MODULE_PATH, m.metatype) ??
129
+ "";
130
+ for (const controller of m.controllers.values())
131
+ await visitController({
132
+ config,
133
+ errors,
134
+ operators,
135
+ controller,
136
+ modulePrefix,
137
+ });
138
+ }
139
+ if (errors.length)
140
+ throw new Error(
141
+ [
142
+ `WebSocketAdaptor: ${errors.length} error(s) found:`,
143
+ ``,
144
+ ...errors.map((e) =>
145
+ [
146
+ ` - controller: ${e.name}`,
147
+ ` methods:`,
148
+ ...e.methods.map((m) =>
149
+ [
150
+ ` - name: ${m.name}`,
151
+ ` file: ${m.source}:${m.line}:${m.column}`,
152
+ ` reasons:`,
153
+ ...m.messages.map(
154
+ (msg) =>
155
+ ` - ${msg
156
+ .split("\n")
157
+ .map((str, i) => (i == 0 ? str : ` ${str}`))
158
+ .join("\n")}`,
159
+ ),
160
+ ].join("\n"),
161
+ ),
162
+ ].join("\n"),
163
+ ),
164
+ ].join("\n"),
165
+ );
166
+ return operators;
167
+ };
168
+
169
+ const visitController = async (props: {
170
+ config: IConfig;
171
+ errors: IControllerError[];
172
+ operators: IOperator[];
173
+ controller: InstanceWrapper<object>;
174
+ modulePrefix: string;
175
+ }): Promise<void> => {
176
+ if (
177
+ ArrayUtil.has(
178
+ Reflect.getMetadataKeys(props.controller.metatype as Function),
179
+ PATH_METADATA,
180
+ HOST_METADATA,
181
+ SCOPE_OPTIONS_METADATA,
182
+ ) === false
183
+ )
184
+ return;
185
+
186
+ const methodErrors: IMethodError[] = [];
187
+ const controller: IController = {
188
+ name: props.controller.name,
189
+ instance: props.controller.instance,
190
+ constructor: props.controller.metatype as Function,
191
+ prototype: Object.getPrototypeOf(props.controller.instance),
192
+ prefixes: (() => {
193
+ const value: string | string[] = Reflect.getMetadata(
194
+ PATH_METADATA,
195
+ props.controller.metatype as object,
196
+ );
197
+ if (typeof value === "string") return [value];
198
+ else if (value.length === 0) return [""];
199
+ else return value;
200
+ })(),
201
+ versions: props.config.versioning
202
+ ? VersioningStrategy.cast(
203
+ Reflect.getMetadata(
204
+ VERSION_METADATA,
205
+ props.controller.metatype as Function,
206
+ ),
207
+ )
208
+ : undefined,
209
+ modulePrefix: props.modulePrefix,
210
+ };
211
+ for (const mk of getOwnPropertyNames(controller.prototype).filter(
212
+ (key) =>
213
+ key !== "constructor" && typeof controller.prototype[key] === "function",
214
+ )) {
215
+ const errorMessages: string[] = [];
216
+ visitMethod({
217
+ config: props.config,
218
+ operators: props.operators,
219
+ controller,
220
+ method: {
221
+ key: mk,
222
+ value: controller.prototype[mk],
223
+ },
224
+ report: (msg) => errorMessages.push(msg),
225
+ });
226
+ if (errorMessages.length) {
227
+ const file = await getFunctionLocation(controller.prototype[mk]);
228
+ methodErrors.push({
229
+ name: mk,
230
+ messages: errorMessages,
231
+ ...file,
232
+ source: path.relative(
233
+ process.cwd(),
234
+ file.source.replace("file:///", ""),
235
+ ),
236
+ });
237
+ }
238
+ }
239
+
240
+ if (methodErrors.length)
241
+ props.errors.push({
242
+ name: controller.name,
243
+ methods: methodErrors,
244
+ });
245
+ };
246
+
247
+ const visitMethod = (props: {
248
+ config: IConfig;
249
+ operators: IOperator[];
250
+ controller: IController;
251
+ method: Entry<Function>;
252
+ report: (message: string) => void;
253
+ }): void => {
254
+ const route: IWebSocketRouteReflect | undefined = Reflect.getMetadata(
255
+ "nestia/WebSocketRoute",
256
+ props.method.value,
257
+ );
258
+ if (typia.is<IWebSocketRouteReflect>(route) === false) return;
259
+
260
+ const parameters: IWebSocketRouteReflect.IArgument[] = (
261
+ (Reflect.getMetadata(
262
+ "nestia/WebSocketRoute/Parameters",
263
+ props.controller.prototype,
264
+ props.method.key,
265
+ ) ?? []) as IWebSocketRouteReflect.IArgument[]
266
+ ).sort((a, b) => a.index - b.index);
267
+ // acceptor must be
268
+ if (parameters.some((p) => p.category === "acceptor") === false)
269
+ return props.report(
270
+ "@WebSocketRoute.Acceptor() decorated parameter must be.",
271
+ );
272
+ // length of parameters must be fulfilled
273
+ if (parameters.length !== props.method.value.length)
274
+ return props.report(
275
+ [
276
+ "Every parameters must be one of below:",
277
+ " - @WebSocketRoute.Acceptor()",
278
+ " - @WebSocketRoute.Driver()",
279
+ " - @WebSocketRoute.Header()",
280
+ " - @WebSocketRoute.Param()",
281
+ " - @WebSocketRoute.Query()",
282
+ ].join("\n"),
283
+ );
284
+
285
+ const versions: string[] = VersioningStrategy.merge(props.config.versioning)([
286
+ ...(props.controller.versions ?? []),
287
+ ...VersioningStrategy.cast(
288
+ Reflect.getMetadata(VERSION_METADATA, props.method.value),
289
+ ),
290
+ ]);
291
+ for (const v of versions)
292
+ for (const cp of wrapPaths(props.controller.prefixes))
293
+ for (const mp of wrapPaths(route.paths)) {
294
+ const parser: Path = new Path(
295
+ "/" +
296
+ [
297
+ props.config.globalPrefix ?? "",
298
+ v,
299
+ props.controller.modulePrefix,
300
+ cp,
301
+ mp,
302
+ ]
303
+ .filter((str) => !!str.length)
304
+ .join("/")
305
+ .split("/")
306
+ .filter((str) => str.length)
307
+ .join("/"),
308
+ );
309
+ const pathParams: IWebSocketRouteReflect.IParam[] = parameters.filter(
310
+ (p) => p.category === "param",
311
+ ) as IWebSocketRouteReflect.IParam[];
312
+ if (parser.params.length !== pathParams.length) {
313
+ props.report(
314
+ [
315
+ `Path "${parser}" must have same number of parameters with @WebSocketRoute.Param()`,
316
+ ` - path: ${JSON.stringify(parser.params)}`,
317
+ ` - arguments: ${JSON.stringify(pathParams.map((p) => p.field))}`,
318
+ ].join("\n"),
319
+ );
320
+ continue;
321
+ }
322
+ const meet: boolean = pathParams
323
+ .map((p) => {
324
+ const has: boolean = parser.params.includes(p.field);
325
+ if (has === false)
326
+ props.report(
327
+ `Path "${parser}" must have parameter "${p.field}" with @WebSocketRoute.Param()`,
328
+ );
329
+ return has;
330
+ })
331
+ .every((b) => b);
332
+ if (meet === false) continue;
333
+
334
+ props.operators.push({
335
+ parser,
336
+ handler: async (input: {
337
+ params: Record<string, string>;
338
+ acceptor: WebSocketAcceptor<any, any, any>;
339
+ }): Promise<void> => {
340
+ const args: any[] = [];
341
+ try {
342
+ for (const p of parameters)
343
+ if (p.category === "acceptor") args.push(input.acceptor);
344
+ else if (p.category === "driver")
345
+ args.push(input.acceptor.getDriver());
346
+ else if (p.category === "header") {
347
+ const error: Error | null = p.validate(input.acceptor.header);
348
+ if (error !== null) throw error;
349
+ args.push(input.acceptor.header);
350
+ } else if (p.category === "param")
351
+ args.push(p.assert(input.params[p.field]));
352
+ else if (p.category === "query") {
353
+ const query: any | Error = p.validate(
354
+ new URLSearchParams(
355
+ input.acceptor.path.indexOf("?") !== -1
356
+ ? input.acceptor.path.split("?")[1]
357
+ : "",
358
+ ),
359
+ );
360
+ if (query instanceof Error) throw query;
361
+ args.push(query);
362
+ }
363
+ } catch (exp) {
364
+ await input.acceptor.reject(
365
+ 1003,
366
+ exp instanceof Error
367
+ ? JSON.stringify({ ...exp })
368
+ : "unknown error",
369
+ );
370
+ return;
371
+ }
372
+ await props.method.value.call(props.controller.instance, ...args);
373
+ },
374
+ });
375
+ }
376
+ };
377
+
378
+ const wrapPaths = (value: string[]) => (value.length === 0 ? [""] : value);
379
+ const getOwnPropertyNames = (prototype: any): string[] => {
380
+ const result: Set<string> = new Set();
381
+ const iterate = (m: any) => {
382
+ if (m === null) return;
383
+ for (const k of Object.getOwnPropertyNames(m)) result.add(k);
384
+ iterate(Object.getPrototypeOf(m));
385
+ };
386
+ iterate(prototype);
387
+ return Array.from(result);
388
+ };
389
+
390
+ interface Entry<T> {
391
+ key: string;
392
+ value: T;
393
+ }
394
+
395
+ interface IController {
396
+ name: string;
397
+ versions: Array<string | typeof VERSION_NEUTRAL> | undefined;
398
+ instance: object;
399
+ constructor: Function;
400
+ prototype: any;
401
+ prefixes: string[];
402
+ modulePrefix: string;
403
+ }
404
+ interface IOperator {
405
+ parser: Path;
406
+ handler: (props: {
407
+ params: Record<string, string>;
408
+ acceptor: WebSocketAcceptor<any, any, any>;
409
+ }) => Promise<any>;
410
+ }
411
+ interface IConfig {
412
+ globalPrefix?: string;
413
+ versioning?: {
414
+ prefix: string;
415
+ defaultVersion?: VersionValue;
416
+ };
417
+ }
418
+
419
+ interface IControllerError {
420
+ name: string;
421
+ methods: IMethodError[];
422
+ }
423
+ interface IMethodError {
424
+ name: string;
425
+ messages: string[];
426
+ source: string;
427
+ line: number;
428
+ column: number;
429
+ }