@nestia/core 0.1.4 → 0.1.5
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.
- package/lib/decorators/DynamicModule.d.ts +3 -0
- package/lib/decorators/DynamicModule.js +73 -0
- package/lib/decorators/DynamicModule.js.map +1 -0
- package/lib/decorators/EncryptedModule.js +16 -137
- package/lib/decorators/EncryptedModule.js.map +1 -1
- package/lib/decorators/internal/headers_to_object.d.ts +3 -1
- package/lib/decorators/internal/headers_to_object.js +0 -3
- package/lib/decorators/internal/headers_to_object.js.map +1 -1
- package/lib/decorators/internal/load_controller.d.ts +2 -0
- package/lib/decorators/internal/load_controller.js +153 -0
- package/lib/decorators/internal/load_controller.js.map +1 -0
- package/lib/decorators/internal/route_error.d.ts +3 -1
- package/lib/decorators/internal/route_error.js +0 -3
- package/lib/decorators/internal/route_error.js.map +1 -1
- package/lib/module.d.ts +2 -0
- package/lib/module.js +2 -0
- package/lib/module.js.map +1 -1
- package/package.json +1 -1
- package/src/decorators/DynamicModule.ts +16 -0
- package/src/decorators/EncryptedBody.ts +102 -102
- package/src/decorators/EncryptedController.ts +43 -43
- package/src/decorators/EncryptedModule.ts +77 -127
- package/src/decorators/EncryptedRoute.ts +203 -203
- package/src/decorators/PlainBody.ts +38 -38
- package/src/decorators/TypedBody.ts +49 -49
- package/src/decorators/TypedParam.ts +70 -70
- package/src/decorators/TypedRoute.ts +149 -149
- package/src/decorators/internal/EncryptedConstant.ts +4 -4
- package/src/decorators/internal/get_path_and_stringify.ts +77 -77
- package/src/decorators/internal/headers_to_object.ts +10 -13
- package/src/decorators/internal/load_controller.ts +35 -0
- package/src/decorators/internal/route_error.ts +38 -41
- package/src/decorators/internal/validate_request_body.ts +59 -59
- package/src/index.ts +5 -5
- package/src/module.ts +11 -9
- package/src/options/INestiaTransformOptions.ts +6 -6
- package/src/options/INestiaTransformProject.ts +7 -6
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IResponseBodyStringifier.ts +25 -25
- package/src/transform.ts +20 -20
- package/src/transformers/BodyTransformer.ts +106 -106
- package/src/transformers/FileTransformer.ts +49 -49
- package/src/transformers/MethodTransformer.ts +91 -91
- package/src/transformers/NodeTransformer.ts +18 -18
- package/src/transformers/ParameterTransformer.ts +45 -45
- package/src/transformers/RouteTransformer.ts +131 -131
- package/src/typings/Creator.ts +3 -3
- package/src/utils/ExceptionManager.ts +126 -126
- package/src/utils/Singleton.ts +20 -20
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
|
|
2
|
-
import {
|
|
3
|
-
BadRequestException,
|
|
4
|
-
ExecutionContext,
|
|
5
|
-
createParamDecorator,
|
|
6
|
-
} from "@nestjs/common";
|
|
7
|
-
import type express from "express";
|
|
8
|
-
import raw from "raw-body";
|
|
9
|
-
import { assert, is, validate } from "typia";
|
|
10
|
-
|
|
11
|
-
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
12
|
-
import { Singleton } from "../utils/Singleton";
|
|
13
|
-
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
14
|
-
import { headers_to_object } from "./internal/headers_to_object";
|
|
15
|
-
import { validate_request_body } from "./internal/validate_request_body";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Encrypted body decorator.
|
|
19
|
-
*
|
|
20
|
-
* `EncryptedBody` is a decorator function getting JSON data from HTTP request who've
|
|
21
|
-
* been encrypted by AES-128/256 algorithm. Also, `EncyrptedBody` validates the JSON
|
|
22
|
-
* data type through
|
|
23
|
-
* [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
|
|
24
|
-
* function and throws `BadRequestException` error (status code: 400), if the JSON
|
|
25
|
-
* data is not following the promised type.
|
|
26
|
-
*
|
|
27
|
-
* For reference, `EncryptedRoute` decrypts request body usnig those options.
|
|
28
|
-
*
|
|
29
|
-
* - AES-128/256
|
|
30
|
-
* - CBC mode
|
|
31
|
-
* - PKCS #5 Padding
|
|
32
|
-
* - Base64 Encoding
|
|
33
|
-
*
|
|
34
|
-
* @return Parameter decorator
|
|
35
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
36
|
-
*/
|
|
37
|
-
export function EncryptedBody<T>(validator?: IRequestBodyValidator<T>) {
|
|
38
|
-
const checker = validate_request_body("EncryptedBody")(validator);
|
|
39
|
-
return createParamDecorator(async function EncryptedBody(
|
|
40
|
-
_unknown: any,
|
|
41
|
-
ctx: ExecutionContext,
|
|
42
|
-
) {
|
|
43
|
-
const request: express.Request = ctx.switchToHttp().getRequest();
|
|
44
|
-
if (request.readable === false)
|
|
45
|
-
throw new BadRequestException(
|
|
46
|
-
"Request body is not the text/plain.",
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
const param:
|
|
50
|
-
| IEncryptionPassword
|
|
51
|
-
| IEncryptionPassword.Closure
|
|
52
|
-
| undefined = Reflect.getMetadata(
|
|
53
|
-
ENCRYPTION_METADATA_KEY,
|
|
54
|
-
ctx.getClass(),
|
|
55
|
-
);
|
|
56
|
-
if (!param)
|
|
57
|
-
throw new Error(
|
|
58
|
-
"Error on EncryptedBody(): no encryption password is given.",
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
// GET BODY DATA
|
|
62
|
-
const headers: Singleton<Record<string, string>> = new Singleton(() =>
|
|
63
|
-
headers_to_object(request.headers),
|
|
64
|
-
);
|
|
65
|
-
const body: string = (await raw(request, "utf8")).trim();
|
|
66
|
-
const password: IEncryptionPassword =
|
|
67
|
-
typeof param === "function"
|
|
68
|
-
? param({ headers: headers.get(), body }, false)
|
|
69
|
-
: param;
|
|
70
|
-
const disabled: boolean =
|
|
71
|
-
password.disabled === undefined
|
|
72
|
-
? false
|
|
73
|
-
: typeof password.disabled === "function"
|
|
74
|
-
? password.disabled({ headers: headers.get(), body }, true)
|
|
75
|
-
: password.disabled;
|
|
76
|
-
|
|
77
|
-
// PARSE AND VALIDATE DATA
|
|
78
|
-
const data: any = JSON.parse(
|
|
79
|
-
disabled ? body : decrypt(body, password.key, password.iv),
|
|
80
|
-
);
|
|
81
|
-
checker(data);
|
|
82
|
-
return data;
|
|
83
|
-
})();
|
|
84
|
-
}
|
|
85
|
-
Object.assign(EncryptedBody, assert);
|
|
86
|
-
Object.assign(EncryptedBody, is);
|
|
87
|
-
Object.assign(EncryptedBody, validate);
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @internal
|
|
91
|
-
*/
|
|
92
|
-
function decrypt(body: string, key: string, iv: string): string {
|
|
93
|
-
try {
|
|
94
|
-
return AesPkcs5.decrypt(body, key, iv);
|
|
95
|
-
} catch (exp) {
|
|
96
|
-
if (exp instanceof Error)
|
|
97
|
-
throw new BadRequestException(
|
|
98
|
-
"Failed to decrypt the request body. Check your body content or encryption password.",
|
|
99
|
-
);
|
|
100
|
-
else throw exp;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
1
|
+
import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
|
|
2
|
+
import {
|
|
3
|
+
BadRequestException,
|
|
4
|
+
ExecutionContext,
|
|
5
|
+
createParamDecorator,
|
|
6
|
+
} from "@nestjs/common";
|
|
7
|
+
import type express from "express";
|
|
8
|
+
import raw from "raw-body";
|
|
9
|
+
import { assert, is, validate } from "typia";
|
|
10
|
+
|
|
11
|
+
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
12
|
+
import { Singleton } from "../utils/Singleton";
|
|
13
|
+
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
14
|
+
import { headers_to_object } from "./internal/headers_to_object";
|
|
15
|
+
import { validate_request_body } from "./internal/validate_request_body";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Encrypted body decorator.
|
|
19
|
+
*
|
|
20
|
+
* `EncryptedBody` is a decorator function getting JSON data from HTTP request who've
|
|
21
|
+
* been encrypted by AES-128/256 algorithm. Also, `EncyrptedBody` validates the JSON
|
|
22
|
+
* data type through
|
|
23
|
+
* [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
|
|
24
|
+
* function and throws `BadRequestException` error (status code: 400), if the JSON
|
|
25
|
+
* data is not following the promised type.
|
|
26
|
+
*
|
|
27
|
+
* For reference, `EncryptedRoute` decrypts request body usnig those options.
|
|
28
|
+
*
|
|
29
|
+
* - AES-128/256
|
|
30
|
+
* - CBC mode
|
|
31
|
+
* - PKCS #5 Padding
|
|
32
|
+
* - Base64 Encoding
|
|
33
|
+
*
|
|
34
|
+
* @return Parameter decorator
|
|
35
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
36
|
+
*/
|
|
37
|
+
export function EncryptedBody<T>(validator?: IRequestBodyValidator<T>) {
|
|
38
|
+
const checker = validate_request_body("EncryptedBody")(validator);
|
|
39
|
+
return createParamDecorator(async function EncryptedBody(
|
|
40
|
+
_unknown: any,
|
|
41
|
+
ctx: ExecutionContext,
|
|
42
|
+
) {
|
|
43
|
+
const request: express.Request = ctx.switchToHttp().getRequest();
|
|
44
|
+
if (request.readable === false)
|
|
45
|
+
throw new BadRequestException(
|
|
46
|
+
"Request body is not the text/plain.",
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const param:
|
|
50
|
+
| IEncryptionPassword
|
|
51
|
+
| IEncryptionPassword.Closure
|
|
52
|
+
| undefined = Reflect.getMetadata(
|
|
53
|
+
ENCRYPTION_METADATA_KEY,
|
|
54
|
+
ctx.getClass(),
|
|
55
|
+
);
|
|
56
|
+
if (!param)
|
|
57
|
+
throw new Error(
|
|
58
|
+
"Error on EncryptedBody(): no encryption password is given.",
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// GET BODY DATA
|
|
62
|
+
const headers: Singleton<Record<string, string>> = new Singleton(() =>
|
|
63
|
+
headers_to_object(request.headers),
|
|
64
|
+
);
|
|
65
|
+
const body: string = (await raw(request, "utf8")).trim();
|
|
66
|
+
const password: IEncryptionPassword =
|
|
67
|
+
typeof param === "function"
|
|
68
|
+
? param({ headers: headers.get(), body }, false)
|
|
69
|
+
: param;
|
|
70
|
+
const disabled: boolean =
|
|
71
|
+
password.disabled === undefined
|
|
72
|
+
? false
|
|
73
|
+
: typeof password.disabled === "function"
|
|
74
|
+
? password.disabled({ headers: headers.get(), body }, true)
|
|
75
|
+
: password.disabled;
|
|
76
|
+
|
|
77
|
+
// PARSE AND VALIDATE DATA
|
|
78
|
+
const data: any = JSON.parse(
|
|
79
|
+
disabled ? body : decrypt(body, password.key, password.iv),
|
|
80
|
+
);
|
|
81
|
+
checker(data);
|
|
82
|
+
return data;
|
|
83
|
+
})();
|
|
84
|
+
}
|
|
85
|
+
Object.assign(EncryptedBody, assert);
|
|
86
|
+
Object.assign(EncryptedBody, is);
|
|
87
|
+
Object.assign(EncryptedBody, validate);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
function decrypt(body: string, key: string, iv: string): string {
|
|
93
|
+
try {
|
|
94
|
+
return AesPkcs5.decrypt(body, key, iv);
|
|
95
|
+
} catch (exp) {
|
|
96
|
+
if (exp instanceof Error)
|
|
97
|
+
throw new BadRequestException(
|
|
98
|
+
"Failed to decrypt the request body. Check your body content or encryption password.",
|
|
99
|
+
);
|
|
100
|
+
else throw exp;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { IEncryptionPassword } from "@nestia/fetcher";
|
|
2
|
-
import { Controller } from "@nestjs/common";
|
|
3
|
-
|
|
4
|
-
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Encrypted controller.
|
|
8
|
-
*
|
|
9
|
-
* `EncryptedController` is an extension of the {@link nest.Controller} class decorator
|
|
10
|
-
* function who configures encryption password of the AES-128/256 algorithm. The
|
|
11
|
-
* encryption algorithm and password would be used by {@link EncryptedRoute} and
|
|
12
|
-
* {@link EncryptedBody} to encrypt the request and response body of the HTTP protocol.
|
|
13
|
-
*
|
|
14
|
-
* > However, if you've configure the {@link IEncryptionPassword.disabled} to be `true`,
|
|
15
|
-
* > you can disable the encryption and decryption algorithm. Therefore, when the
|
|
16
|
-
* > {@link IEncryptionPassword.disable} becomes the `true`, content like request and
|
|
17
|
-
* > response body would be considered as a plain text instead.
|
|
18
|
-
*
|
|
19
|
-
* By the way, you can configure the encryption password in the global level by using
|
|
20
|
-
* {@link EncryptedModule} instead of the {@link nest.Module} in the module level. In
|
|
21
|
-
* that case, you don't need to use this `EncryptedController` more. Just use the
|
|
22
|
-
* {@link nest.Controller} without duplicated encryption password definitions.
|
|
23
|
-
*
|
|
24
|
-
* Of course, if you want to use different encryption password from the
|
|
25
|
-
* {@link EncryptedModule}, this `EncryptedController` would be useful again. Therefore,
|
|
26
|
-
* I recommend to use this `EncryptedController` decorator function only when you must
|
|
27
|
-
* configure different encryption password from the {@link EncryptedModule}.
|
|
28
|
-
*
|
|
29
|
-
* @param path Path of the HTTP request
|
|
30
|
-
* @param password Encryption password or its getter function
|
|
31
|
-
* @returns Class decorator
|
|
32
|
-
*
|
|
33
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
34
|
-
*/
|
|
35
|
-
export function EncryptedController(
|
|
36
|
-
path: string,
|
|
37
|
-
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
38
|
-
): ClassDecorator {
|
|
39
|
-
return function (target: any) {
|
|
40
|
-
Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
|
|
41
|
-
Controller(path)(target);
|
|
42
|
-
};
|
|
43
|
-
}
|
|
1
|
+
import { IEncryptionPassword } from "@nestia/fetcher";
|
|
2
|
+
import { Controller } from "@nestjs/common";
|
|
3
|
+
|
|
4
|
+
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Encrypted controller.
|
|
8
|
+
*
|
|
9
|
+
* `EncryptedController` is an extension of the {@link nest.Controller} class decorator
|
|
10
|
+
* function who configures encryption password of the AES-128/256 algorithm. The
|
|
11
|
+
* encryption algorithm and password would be used by {@link EncryptedRoute} and
|
|
12
|
+
* {@link EncryptedBody} to encrypt the request and response body of the HTTP protocol.
|
|
13
|
+
*
|
|
14
|
+
* > However, if you've configure the {@link IEncryptionPassword.disabled} to be `true`,
|
|
15
|
+
* > you can disable the encryption and decryption algorithm. Therefore, when the
|
|
16
|
+
* > {@link IEncryptionPassword.disable} becomes the `true`, content like request and
|
|
17
|
+
* > response body would be considered as a plain text instead.
|
|
18
|
+
*
|
|
19
|
+
* By the way, you can configure the encryption password in the global level by using
|
|
20
|
+
* {@link EncryptedModule} instead of the {@link nest.Module} in the module level. In
|
|
21
|
+
* that case, you don't need to use this `EncryptedController` more. Just use the
|
|
22
|
+
* {@link nest.Controller} without duplicated encryption password definitions.
|
|
23
|
+
*
|
|
24
|
+
* Of course, if you want to use different encryption password from the
|
|
25
|
+
* {@link EncryptedModule}, this `EncryptedController` would be useful again. Therefore,
|
|
26
|
+
* I recommend to use this `EncryptedController` decorator function only when you must
|
|
27
|
+
* configure different encryption password from the {@link EncryptedModule}.
|
|
28
|
+
*
|
|
29
|
+
* @param path Path of the HTTP request
|
|
30
|
+
* @param password Encryption password or its getter function
|
|
31
|
+
* @returns Class decorator
|
|
32
|
+
*
|
|
33
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
34
|
+
*/
|
|
35
|
+
export function EncryptedController(
|
|
36
|
+
path: string,
|
|
37
|
+
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
38
|
+
): ClassDecorator {
|
|
39
|
+
return function (target: any) {
|
|
40
|
+
Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, target);
|
|
41
|
+
Controller(path)(target);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -1,127 +1,77 @@
|
|
|
1
|
-
import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
|
|
2
|
-
import { Module, ModuleMetadata } from "@nestjs/common";
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Encrypted module.
|
|
10
|
-
*
|
|
11
|
-
* `EncryptedModule` is an extension of the {@link Module} class decorator function
|
|
12
|
-
* who configures encryption password of the AES-128/256 algorithm. The encryption
|
|
13
|
-
* algorithm and password would be used by {@link EncryptedRoute} and {@link EncryptedBody}
|
|
14
|
-
* to encrypt the request and response bod of the HTTP protocol.
|
|
15
|
-
*
|
|
16
|
-
* By using this `EncryptedModule` decorator function, all of the
|
|
17
|
-
* {@link Controller controllers} configured in the *metadata* would be automatically
|
|
18
|
-
* changed to the {@link EncryptedController} with the *password*. If there're some
|
|
19
|
-
* original {@link EncryptedController} decorated classes in the *metadata*, their
|
|
20
|
-
* encryption password would be kept.
|
|
21
|
-
*
|
|
22
|
-
* Therefore, if you're planning to place original {@link EncryptedController} decorated
|
|
23
|
-
* classes in the *metadata*, I hope them to have different encryption password from the
|
|
24
|
-
* module level. If not, I recommend you use the {@link Controller} decorator
|
|
25
|
-
* function instead.
|
|
26
|
-
*
|
|
27
|
-
* In addition, the `EncryptedModule` supports a convenient dynamic controller importing
|
|
28
|
-
* function, {@link EncryptedModule.dynamic}. If you utilize the function with directory
|
|
29
|
-
* path of the controller classes, it imports and configures the controller classes into
|
|
30
|
-
* the `Module`, automatically.
|
|
31
|
-
*
|
|
32
|
-
* @param metadata Module configuration metadata
|
|
33
|
-
* @param password Encryption password or its getter function
|
|
34
|
-
* @returns Class decorator
|
|
35
|
-
*
|
|
36
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
37
|
-
*/
|
|
38
|
-
export function EncryptedModule(
|
|
39
|
-
metadata: ModuleMetadata,
|
|
40
|
-
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
41
|
-
): ClassDecorator {
|
|
42
|
-
return function (target: any) {
|
|
43
|
-
Module(metadata)(target);
|
|
44
|
-
if (metadata.controllers === undefined) return;
|
|
45
|
-
|
|
46
|
-
for (const
|
|
47
|
-
if (
|
|
48
|
-
Reflect.
|
|
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
|
-
controllers: await controllers(path, password),
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// RETURNS WITH DECORATING
|
|
82
|
-
@EncryptedModule(metadata, password)
|
|
83
|
-
class Module {}
|
|
84
|
-
return Module;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function controllers(
|
|
88
|
-
path: string,
|
|
89
|
-
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
90
|
-
): Promise<any[]> {
|
|
91
|
-
const output: any[] = [];
|
|
92
|
-
await iterate(output, path);
|
|
93
|
-
|
|
94
|
-
for (const controller of output)
|
|
95
|
-
if (
|
|
96
|
-
Reflect.hasMetadata(ENCRYPTION_METADATA_KEY, controller) ===
|
|
97
|
-
false
|
|
98
|
-
)
|
|
99
|
-
Reflect.defineMetadata(
|
|
100
|
-
ENCRYPTION_METADATA_KEY,
|
|
101
|
-
password,
|
|
102
|
-
controller,
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
return output;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function iterate(controllers: object[], path: string): Promise<void> {
|
|
109
|
-
const directory: string[] = await fs.promises.readdir(path);
|
|
110
|
-
for (const file of directory) {
|
|
111
|
-
const current: string = `${path}/${file}`;
|
|
112
|
-
const stats: fs.Stats = await fs.promises.lstat(current);
|
|
113
|
-
|
|
114
|
-
if (stats.isDirectory() === true)
|
|
115
|
-
await iterate(controllers, current);
|
|
116
|
-
else if (file.substring(file.length - 3) === `.${EXTENSION}`) {
|
|
117
|
-
const external: any = await import(current);
|
|
118
|
-
for (const key in external) {
|
|
119
|
-
const instance: object = external[key];
|
|
120
|
-
if (Reflect.getMetadata("path", instance) !== undefined)
|
|
121
|
-
controllers.push(instance);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
const EXTENSION = is_ts_node ? "ts" : "js";
|
|
1
|
+
import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
|
|
2
|
+
import { Module, ModuleMetadata } from "@nestjs/common";
|
|
3
|
+
|
|
4
|
+
import { Creator } from "../typings/Creator";
|
|
5
|
+
import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
|
|
6
|
+
import { load_controllers } from "./internal/load_controller";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Encrypted module.
|
|
10
|
+
*
|
|
11
|
+
* `EncryptedModule` is an extension of the {@link Module} class decorator function
|
|
12
|
+
* who configures encryption password of the AES-128/256 algorithm. The encryption
|
|
13
|
+
* algorithm and password would be used by {@link EncryptedRoute} and {@link EncryptedBody}
|
|
14
|
+
* to encrypt the request and response bod of the HTTP protocol.
|
|
15
|
+
*
|
|
16
|
+
* By using this `EncryptedModule` decorator function, all of the
|
|
17
|
+
* {@link Controller controllers} configured in the *metadata* would be automatically
|
|
18
|
+
* changed to the {@link EncryptedController} with the *password*. If there're some
|
|
19
|
+
* original {@link EncryptedController} decorated classes in the *metadata*, their
|
|
20
|
+
* encryption password would be kept.
|
|
21
|
+
*
|
|
22
|
+
* Therefore, if you're planning to place original {@link EncryptedController} decorated
|
|
23
|
+
* classes in the *metadata*, I hope them to have different encryption password from the
|
|
24
|
+
* module level. If not, I recommend you use the {@link Controller} decorator
|
|
25
|
+
* function instead.
|
|
26
|
+
*
|
|
27
|
+
* In addition, the `EncryptedModule` supports a convenient dynamic controller importing
|
|
28
|
+
* function, {@link EncryptedModule.dynamic}. If you utilize the function with directory
|
|
29
|
+
* path of the controller classes, it imports and configures the controller classes into
|
|
30
|
+
* the `Module`, automatically.
|
|
31
|
+
*
|
|
32
|
+
* @param metadata Module configuration metadata
|
|
33
|
+
* @param password Encryption password or its getter function
|
|
34
|
+
* @returns Class decorator
|
|
35
|
+
*
|
|
36
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
37
|
+
*/
|
|
38
|
+
export function EncryptedModule(
|
|
39
|
+
metadata: ModuleMetadata,
|
|
40
|
+
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
41
|
+
): ClassDecorator {
|
|
42
|
+
return function (target: any) {
|
|
43
|
+
Module(metadata)(target);
|
|
44
|
+
if (metadata.controllers === undefined) return;
|
|
45
|
+
|
|
46
|
+
for (const c of metadata.controllers)
|
|
47
|
+
if (Reflect.hasMetadata(ENCRYPTION_METADATA_KEY, c) === false)
|
|
48
|
+
Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export namespace EncryptedModule {
|
|
53
|
+
/**
|
|
54
|
+
* Dynamic encrypted module.
|
|
55
|
+
*
|
|
56
|
+
* `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule} function
|
|
57
|
+
* who configures controller classes by the dynamic importing. By specifying directory
|
|
58
|
+
* path of the controller classes, those controllers would be automatically imported
|
|
59
|
+
* and configured.
|
|
60
|
+
*
|
|
61
|
+
* @param path Directory path of the controller classes
|
|
62
|
+
* @param password Encryption password or its getter function
|
|
63
|
+
* @returns Class decorated module instance
|
|
64
|
+
*/
|
|
65
|
+
export async function dynamic(
|
|
66
|
+
path: string,
|
|
67
|
+
password: IEncryptionPassword | IEncryptionPassword.Closure,
|
|
68
|
+
): Promise<object> {
|
|
69
|
+
// LOAD CONTROLLERS
|
|
70
|
+
const controllers: Creator<object>[] = await load_controllers(path);
|
|
71
|
+
|
|
72
|
+
// RETURNS WITH DECORATING
|
|
73
|
+
@EncryptedModule({ controllers }, password)
|
|
74
|
+
class NestiaModule {}
|
|
75
|
+
return NestiaModule;
|
|
76
|
+
}
|
|
77
|
+
}
|