@nestia/fetcher 2.4.5 → 2.4.6
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/internal/FetcherBase.js.map +1 -1
- package/package.json +1 -1
- package/src/AesPkcs5.ts +50 -50
- package/src/EncryptedFetcher.ts +175 -175
- package/src/HttpError.ts +85 -85
- package/src/IConnection.ts +21 -21
- package/src/IEncryptionPassword.ts +50 -50
- package/src/IPropagation.ts +6 -6
- package/src/IRandomGenerator.ts +38 -38
- package/src/PlainFetcher.ts +106 -106
- package/src/Primitive.ts +48 -47
- package/src/Resolved.ts +59 -58
- package/src/index.ts +7 -7
- package/src/internal/FetcherBase.ts +2 -2
- package/src/internal/IFetchRoute.ts +61 -61
- package/src/internal/Singleton.ts +20 -20
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FetcherBase.js","sourceRoot":"","sources":["../../src/internal/FetcherBase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA8B;AAE9B,0CAAyC;AAKzC,yCAAwC;AAExC,IAAiB,WAAW,CA8J3B;AA9JD,WAAiB,WAAW;;IAab,iBAAK,GAChB,UAAC,KAAa;QACd,OAAA,UACE,UAAuB,EACvB,KAAwE,EACxE,KAAa,EACb,SAAoC;;;;4BAErB,qBAAM,UAAU,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAC7C,UAAU,EACV,KAAK,EACL,KAAK,EACL,SAAS,CACV,EAAA;;wBALK,MAAM,GAAG,SAKd;wBACD,IAAK,MAAc,CAAC,OAAO,KAAK,KAAK;4BACnC,MAAM,IAAI,qBAAS,CACjB,KAAK,CAAC,MAAM,EACZ,KAAK,CAAC,IAAI,EACV,MAAM,CAAC,MAAuB,EAC9B,MAAM,CAAC,OAAO,EACd,MAAM,CAAC,IAAc,CACtB,C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IAAI,oCACzB,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,UAAC,GAAG,IAAK,OAAA,GAAG,CAAC,IAAI,EAAE,EAAV,CAAU,CAAC,WAC5C;QACJ,CAAC;;YAAM,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC7B,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/AesPkcs5.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import crypto from "crypto";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Utility class for the AES-128/256 encryption.
|
|
5
|
-
*
|
|
6
|
-
* - AES-128/256
|
|
7
|
-
* - CBC mode
|
|
8
|
-
* - PKCS#5 Padding
|
|
9
|
-
* - Base64 Encoding
|
|
10
|
-
*
|
|
11
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
12
|
-
*/
|
|
13
|
-
export namespace AesPkcs5 {
|
|
14
|
-
/**
|
|
15
|
-
* Encrypt data
|
|
16
|
-
*
|
|
17
|
-
* @param data Target data
|
|
18
|
-
* @param key Key value of the encryption.
|
|
19
|
-
* @param iv Initializer Vector for the encryption
|
|
20
|
-
* @return Encrypted data
|
|
21
|
-
*/
|
|
22
|
-
export function encrypt(data: string, key: string, iv: string): string {
|
|
23
|
-
const bytes: number = key.length * 8;
|
|
24
|
-
const cipher: crypto.Cipher = crypto.createCipheriv(
|
|
25
|
-
`AES-${bytes}-CBC`,
|
|
26
|
-
key,
|
|
27
|
-
iv,
|
|
28
|
-
);
|
|
29
|
-
return cipher.update(data, "utf8", "base64") + cipher.final("base64");
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Decrypt data.
|
|
34
|
-
*
|
|
35
|
-
* @param data Target data
|
|
36
|
-
* @param key Key value of the decryption.
|
|
37
|
-
* @param iv Initializer Vector for the decryption
|
|
38
|
-
* @return Decrypted data.
|
|
39
|
-
*/
|
|
40
|
-
export function decrypt(data: string, key: string, iv: string): string {
|
|
41
|
-
const bytes: number = key.length * 8;
|
|
42
|
-
const decipher: crypto.Decipher = crypto.createDecipheriv(
|
|
43
|
-
`AES-${bytes}-CBC`,
|
|
44
|
-
key,
|
|
45
|
-
iv,
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
return decipher.update(data, "base64", "utf8") + decipher.final("utf8");
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utility class for the AES-128/256 encryption.
|
|
5
|
+
*
|
|
6
|
+
* - AES-128/256
|
|
7
|
+
* - CBC mode
|
|
8
|
+
* - PKCS#5 Padding
|
|
9
|
+
* - Base64 Encoding
|
|
10
|
+
*
|
|
11
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
12
|
+
*/
|
|
13
|
+
export namespace AesPkcs5 {
|
|
14
|
+
/**
|
|
15
|
+
* Encrypt data
|
|
16
|
+
*
|
|
17
|
+
* @param data Target data
|
|
18
|
+
* @param key Key value of the encryption.
|
|
19
|
+
* @param iv Initializer Vector for the encryption
|
|
20
|
+
* @return Encrypted data
|
|
21
|
+
*/
|
|
22
|
+
export function encrypt(data: string, key: string, iv: string): string {
|
|
23
|
+
const bytes: number = key.length * 8;
|
|
24
|
+
const cipher: crypto.Cipher = crypto.createCipheriv(
|
|
25
|
+
`AES-${bytes}-CBC`,
|
|
26
|
+
key,
|
|
27
|
+
iv,
|
|
28
|
+
);
|
|
29
|
+
return cipher.update(data, "utf8", "base64") + cipher.final("base64");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Decrypt data.
|
|
34
|
+
*
|
|
35
|
+
* @param data Target data
|
|
36
|
+
* @param key Key value of the decryption.
|
|
37
|
+
* @param iv Initializer Vector for the decryption
|
|
38
|
+
* @return Decrypted data.
|
|
39
|
+
*/
|
|
40
|
+
export function decrypt(data: string, key: string, iv: string): string {
|
|
41
|
+
const bytes: number = key.length * 8;
|
|
42
|
+
const decipher: crypto.Decipher = crypto.createDecipheriv(
|
|
43
|
+
`AES-${bytes}-CBC`,
|
|
44
|
+
key,
|
|
45
|
+
iv,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return decipher.update(data, "base64", "utf8") + decipher.final("utf8");
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/EncryptedFetcher.ts
CHANGED
|
@@ -1,175 +1,175 @@
|
|
|
1
|
-
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
|
-
import { IConnection } from "./IConnection";
|
|
3
|
-
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
|
-
import { IPropagation } from "./IPropagation";
|
|
5
|
-
import { Primitive } from "./Primitive";
|
|
6
|
-
import { FetcherBase } from "./internal/FetcherBase";
|
|
7
|
-
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
11
|
-
*
|
|
12
|
-
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
|
|
13
|
-
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
14
|
-
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a collection of
|
|
15
|
-
* dedicated `fetch()` functions for `@nestia/sdk` with encryption.
|
|
16
|
-
*
|
|
17
|
-
* For reference, `EncryptedFetcher` class being used only when target controller
|
|
18
|
-
* method is encrypting body data by `@EncryptedRoute` or `@EncryptedBody` decorators.
|
|
19
|
-
* If those decorators are not used, {@link PlainFetcher} class would be used instead.
|
|
20
|
-
*
|
|
21
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
22
|
-
*/
|
|
23
|
-
export namespace EncryptedFetcher {
|
|
24
|
-
/**
|
|
25
|
-
* Fetch function only for `HEAD` method.
|
|
26
|
-
*
|
|
27
|
-
* @param connection Connection information for the remote HTTP server
|
|
28
|
-
* @param route Route information about the target API
|
|
29
|
-
* @return Nothing because of `HEAD` method
|
|
30
|
-
*/
|
|
31
|
-
export function fetch(
|
|
32
|
-
connection: IConnection,
|
|
33
|
-
route: IFetchRoute<"HEAD">,
|
|
34
|
-
): Promise<void>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Fetch function only for `GET` method.
|
|
38
|
-
*
|
|
39
|
-
* @param connection Connection information for the remote HTTP server
|
|
40
|
-
* @param route Route information about the target API
|
|
41
|
-
* @return Response body data from the remote API
|
|
42
|
-
*/
|
|
43
|
-
export function fetch<Output>(
|
|
44
|
-
connection: IConnection,
|
|
45
|
-
route: IFetchRoute<"GET">,
|
|
46
|
-
): Promise<Primitive<Output>>;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
|
|
50
|
-
*
|
|
51
|
-
* @param connection Connection information for the remote HTTP server
|
|
52
|
-
* @param route Route information about the target API
|
|
53
|
-
* @return Response body data from the remote API
|
|
54
|
-
*/
|
|
55
|
-
export function fetch<Input, Output>(
|
|
56
|
-
connection: IConnection,
|
|
57
|
-
route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
|
|
58
|
-
input?: Input,
|
|
59
|
-
stringify?: (input: Input) => string,
|
|
60
|
-
): Promise<Primitive<Output>>;
|
|
61
|
-
|
|
62
|
-
export async function fetch<Input, Output>(
|
|
63
|
-
connection: IConnection,
|
|
64
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
65
|
-
input?: Input,
|
|
66
|
-
stringify?: (input: Input) => string,
|
|
67
|
-
): Promise<Primitive<Output>> {
|
|
68
|
-
if (
|
|
69
|
-
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
70
|
-
connection.encryption === undefined
|
|
71
|
-
)
|
|
72
|
-
throw new Error(
|
|
73
|
-
"Error on EncryptedFetcher.fetch(): the encryption password has not been configured.",
|
|
74
|
-
);
|
|
75
|
-
const closure =
|
|
76
|
-
typeof connection.encryption === "function"
|
|
77
|
-
? (direction: "encode" | "decode") =>
|
|
78
|
-
(
|
|
79
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
80
|
-
body: string,
|
|
81
|
-
) =>
|
|
82
|
-
(connection.encryption as IEncryptionPassword.Closure)({
|
|
83
|
-
headers,
|
|
84
|
-
body,
|
|
85
|
-
direction,
|
|
86
|
-
})
|
|
87
|
-
: () => () => connection.encryption as IEncryptionPassword;
|
|
88
|
-
|
|
89
|
-
return FetcherBase.fetch({
|
|
90
|
-
className: "EncryptedFetcher",
|
|
91
|
-
encode:
|
|
92
|
-
route.request?.encrypted === true
|
|
93
|
-
? (input, headers) => {
|
|
94
|
-
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
95
|
-
return AesPkcs5.encrypt(
|
|
96
|
-
(stringify ?? JSON.stringify)(input),
|
|
97
|
-
p.key,
|
|
98
|
-
p.iv,
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
: (input) => input,
|
|
102
|
-
decode:
|
|
103
|
-
route.response?.encrypted === true
|
|
104
|
-
? (input, headers) => {
|
|
105
|
-
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
106
|
-
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
107
|
-
return s.length ? JSON.parse(s) : s;
|
|
108
|
-
}
|
|
109
|
-
: (input) => input,
|
|
110
|
-
})(connection, route, input, stringify);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function propagate<Output extends IPropagation<any, any>>(
|
|
114
|
-
connection: IConnection,
|
|
115
|
-
route: IFetchRoute<"GET" | "HEAD">,
|
|
116
|
-
): Promise<Output>;
|
|
117
|
-
|
|
118
|
-
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
119
|
-
connection: IConnection,
|
|
120
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
121
|
-
input?: Input,
|
|
122
|
-
stringify?: (input: Input) => string,
|
|
123
|
-
): Promise<Output>;
|
|
124
|
-
|
|
125
|
-
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
126
|
-
connection: IConnection,
|
|
127
|
-
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
128
|
-
input?: Input,
|
|
129
|
-
stringify?: (input: Input) => string,
|
|
130
|
-
): Promise<Output> {
|
|
131
|
-
if (
|
|
132
|
-
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
133
|
-
connection.encryption === undefined
|
|
134
|
-
)
|
|
135
|
-
throw new Error(
|
|
136
|
-
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
137
|
-
);
|
|
138
|
-
const closure =
|
|
139
|
-
typeof connection.encryption === "function"
|
|
140
|
-
? (direction: "encode" | "decode") =>
|
|
141
|
-
(
|
|
142
|
-
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
143
|
-
body: string,
|
|
144
|
-
) =>
|
|
145
|
-
(connection.encryption as IEncryptionPassword.Closure)({
|
|
146
|
-
headers,
|
|
147
|
-
body,
|
|
148
|
-
direction,
|
|
149
|
-
})
|
|
150
|
-
: () => () => connection.encryption as IEncryptionPassword;
|
|
151
|
-
|
|
152
|
-
return FetcherBase.propagate({
|
|
153
|
-
className: "EncryptedFetcher",
|
|
154
|
-
encode:
|
|
155
|
-
route.request?.encrypted === true
|
|
156
|
-
? (input, headers) => {
|
|
157
|
-
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
158
|
-
return AesPkcs5.encrypt(
|
|
159
|
-
(stringify ?? JSON.stringify)(input),
|
|
160
|
-
p.key,
|
|
161
|
-
p.iv,
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
: (input) => input,
|
|
165
|
-
decode:
|
|
166
|
-
route.response?.encrypted === true
|
|
167
|
-
? (input, headers) => {
|
|
168
|
-
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
169
|
-
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
170
|
-
return s.length ? JSON.parse(s) : s;
|
|
171
|
-
}
|
|
172
|
-
: (input) => input,
|
|
173
|
-
})(connection, route, input, stringify) as Promise<Output>;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
1
|
+
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
|
+
import { IConnection } from "./IConnection";
|
|
3
|
+
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
|
+
import { IPropagation } from "./IPropagation";
|
|
5
|
+
import { Primitive } from "./Primitive";
|
|
6
|
+
import { FetcherBase } from "./internal/FetcherBase";
|
|
7
|
+
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
11
|
+
*
|
|
12
|
+
* `EncryptedFetcher` is a utility class designed for SDK functions generated by
|
|
13
|
+
* [`@nestia/sdk`](https://nestia.io/docs/sdk/sdk), interacting with the remote
|
|
14
|
+
* HTTP API encrypted by AES-PKCS algorithm. In other words, this is a collection of
|
|
15
|
+
* dedicated `fetch()` functions for `@nestia/sdk` with encryption.
|
|
16
|
+
*
|
|
17
|
+
* For reference, `EncryptedFetcher` class being used only when target controller
|
|
18
|
+
* method is encrypting body data by `@EncryptedRoute` or `@EncryptedBody` decorators.
|
|
19
|
+
* If those decorators are not used, {@link PlainFetcher} class would be used instead.
|
|
20
|
+
*
|
|
21
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
22
|
+
*/
|
|
23
|
+
export namespace EncryptedFetcher {
|
|
24
|
+
/**
|
|
25
|
+
* Fetch function only for `HEAD` method.
|
|
26
|
+
*
|
|
27
|
+
* @param connection Connection information for the remote HTTP server
|
|
28
|
+
* @param route Route information about the target API
|
|
29
|
+
* @return Nothing because of `HEAD` method
|
|
30
|
+
*/
|
|
31
|
+
export function fetch(
|
|
32
|
+
connection: IConnection,
|
|
33
|
+
route: IFetchRoute<"HEAD">,
|
|
34
|
+
): Promise<void>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fetch function only for `GET` method.
|
|
38
|
+
*
|
|
39
|
+
* @param connection Connection information for the remote HTTP server
|
|
40
|
+
* @param route Route information about the target API
|
|
41
|
+
* @return Response body data from the remote API
|
|
42
|
+
*/
|
|
43
|
+
export function fetch<Output>(
|
|
44
|
+
connection: IConnection,
|
|
45
|
+
route: IFetchRoute<"GET">,
|
|
46
|
+
): Promise<Primitive<Output>>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Fetch function for the `POST`, `PUT`, `PATCH` and `DELETE` methods.
|
|
50
|
+
*
|
|
51
|
+
* @param connection Connection information for the remote HTTP server
|
|
52
|
+
* @param route Route information about the target API
|
|
53
|
+
* @return Response body data from the remote API
|
|
54
|
+
*/
|
|
55
|
+
export function fetch<Input, Output>(
|
|
56
|
+
connection: IConnection,
|
|
57
|
+
route: IFetchRoute<"POST" | "PUT" | "PATCH" | "DELETE">,
|
|
58
|
+
input?: Input,
|
|
59
|
+
stringify?: (input: Input) => string,
|
|
60
|
+
): Promise<Primitive<Output>>;
|
|
61
|
+
|
|
62
|
+
export async function fetch<Input, Output>(
|
|
63
|
+
connection: IConnection,
|
|
64
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
65
|
+
input?: Input,
|
|
66
|
+
stringify?: (input: Input) => string,
|
|
67
|
+
): Promise<Primitive<Output>> {
|
|
68
|
+
if (
|
|
69
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
70
|
+
connection.encryption === undefined
|
|
71
|
+
)
|
|
72
|
+
throw new Error(
|
|
73
|
+
"Error on EncryptedFetcher.fetch(): the encryption password has not been configured.",
|
|
74
|
+
);
|
|
75
|
+
const closure =
|
|
76
|
+
typeof connection.encryption === "function"
|
|
77
|
+
? (direction: "encode" | "decode") =>
|
|
78
|
+
(
|
|
79
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
80
|
+
body: string,
|
|
81
|
+
) =>
|
|
82
|
+
(connection.encryption as IEncryptionPassword.Closure)({
|
|
83
|
+
headers,
|
|
84
|
+
body,
|
|
85
|
+
direction,
|
|
86
|
+
})
|
|
87
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
88
|
+
|
|
89
|
+
return FetcherBase.fetch({
|
|
90
|
+
className: "EncryptedFetcher",
|
|
91
|
+
encode:
|
|
92
|
+
route.request?.encrypted === true
|
|
93
|
+
? (input, headers) => {
|
|
94
|
+
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
95
|
+
return AesPkcs5.encrypt(
|
|
96
|
+
(stringify ?? JSON.stringify)(input),
|
|
97
|
+
p.key,
|
|
98
|
+
p.iv,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
: (input) => input,
|
|
102
|
+
decode:
|
|
103
|
+
route.response?.encrypted === true
|
|
104
|
+
? (input, headers) => {
|
|
105
|
+
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
106
|
+
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
107
|
+
return s.length ? JSON.parse(s) : s;
|
|
108
|
+
}
|
|
109
|
+
: (input) => input,
|
|
110
|
+
})(connection, route, input, stringify);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function propagate<Output extends IPropagation<any, any>>(
|
|
114
|
+
connection: IConnection,
|
|
115
|
+
route: IFetchRoute<"GET" | "HEAD">,
|
|
116
|
+
): Promise<Output>;
|
|
117
|
+
|
|
118
|
+
export function propagate<Input, Output extends IPropagation<any, any>>(
|
|
119
|
+
connection: IConnection,
|
|
120
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
121
|
+
input?: Input,
|
|
122
|
+
stringify?: (input: Input) => string,
|
|
123
|
+
): Promise<Output>;
|
|
124
|
+
|
|
125
|
+
export async function propagate<Input, Output extends IPropagation<any, any>>(
|
|
126
|
+
connection: IConnection,
|
|
127
|
+
route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">,
|
|
128
|
+
input?: Input,
|
|
129
|
+
stringify?: (input: Input) => string,
|
|
130
|
+
): Promise<Output> {
|
|
131
|
+
if (
|
|
132
|
+
(route.request?.encrypted === true || route.response?.encrypted) &&
|
|
133
|
+
connection.encryption === undefined
|
|
134
|
+
)
|
|
135
|
+
throw new Error(
|
|
136
|
+
"Error on EncryptedFetcher.propagate(): the encryption password has not been configured.",
|
|
137
|
+
);
|
|
138
|
+
const closure =
|
|
139
|
+
typeof connection.encryption === "function"
|
|
140
|
+
? (direction: "encode" | "decode") =>
|
|
141
|
+
(
|
|
142
|
+
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
143
|
+
body: string,
|
|
144
|
+
) =>
|
|
145
|
+
(connection.encryption as IEncryptionPassword.Closure)({
|
|
146
|
+
headers,
|
|
147
|
+
body,
|
|
148
|
+
direction,
|
|
149
|
+
})
|
|
150
|
+
: () => () => connection.encryption as IEncryptionPassword;
|
|
151
|
+
|
|
152
|
+
return FetcherBase.propagate({
|
|
153
|
+
className: "EncryptedFetcher",
|
|
154
|
+
encode:
|
|
155
|
+
route.request?.encrypted === true
|
|
156
|
+
? (input, headers) => {
|
|
157
|
+
const p: IEncryptionPassword = closure("encode")(headers, input);
|
|
158
|
+
return AesPkcs5.encrypt(
|
|
159
|
+
(stringify ?? JSON.stringify)(input),
|
|
160
|
+
p.key,
|
|
161
|
+
p.iv,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
: (input) => input,
|
|
165
|
+
decode:
|
|
166
|
+
route.response?.encrypted === true
|
|
167
|
+
? (input, headers) => {
|
|
168
|
+
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
169
|
+
const s: string = AesPkcs5.decrypt(input, p.key, p.iv);
|
|
170
|
+
return s.length ? JSON.parse(s) : s;
|
|
171
|
+
}
|
|
172
|
+
: (input) => input,
|
|
173
|
+
})(connection, route, input, stringify) as Promise<Output>;
|
|
174
|
+
}
|
|
175
|
+
}
|
package/src/HttpError.ts
CHANGED
|
@@ -1,85 +1,85 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP Error.
|
|
3
|
-
*
|
|
4
|
-
* `HttpError` is a type of error class who've been thrown by the remote HTTP server.
|
|
5
|
-
*
|
|
6
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
7
|
-
*/
|
|
8
|
-
export class HttpError extends Error {
|
|
9
|
-
/**
|
|
10
|
-
* @internal
|
|
11
|
-
*/
|
|
12
|
-
private body_: any = NOT_YET;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Initializer Constructor.
|
|
16
|
-
*
|
|
17
|
-
* @param method Method of the HTTP request.
|
|
18
|
-
* @param path Path of the HTTP request.
|
|
19
|
-
* @param status Status code from the remote HTTP server.
|
|
20
|
-
* @param message Error message from the remote HTTP server.
|
|
21
|
-
*/
|
|
22
|
-
public constructor(
|
|
23
|
-
public readonly method:
|
|
24
|
-
| "GET"
|
|
25
|
-
| "DELETE"
|
|
26
|
-
| "POST"
|
|
27
|
-
| "PUT"
|
|
28
|
-
| "PATCH"
|
|
29
|
-
| "HEAD",
|
|
30
|
-
public readonly path: string,
|
|
31
|
-
public readonly status: number,
|
|
32
|
-
public readonly headers: Record<string, string | string[]>,
|
|
33
|
-
message: string,
|
|
34
|
-
) {
|
|
35
|
-
super(message);
|
|
36
|
-
|
|
37
|
-
// INHERITANCE POLYFILL
|
|
38
|
-
const proto: HttpError = new.target.prototype;
|
|
39
|
-
if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
|
|
40
|
-
else (this as any).__proto__ = proto;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* `HttpError` to JSON.
|
|
45
|
-
*
|
|
46
|
-
* When you call `JSON.stringify()` function on current `HttpError` instance,
|
|
47
|
-
* this `HttpError.toJSON()` method would be automatically called.
|
|
48
|
-
*
|
|
49
|
-
* Also, if response body from the remote HTTP server forms a JSON object,
|
|
50
|
-
* this `HttpError.toJSON()` method would be useful because it returns the
|
|
51
|
-
* parsed JSON object about the {@link message} property.
|
|
52
|
-
*
|
|
53
|
-
* @template T Expected type of the response body.
|
|
54
|
-
* @returns JSON object of the `HttpError`.
|
|
55
|
-
*/
|
|
56
|
-
public toJSON<T>(): HttpError.IProps<T> {
|
|
57
|
-
if (this.body_ === NOT_YET)
|
|
58
|
-
try {
|
|
59
|
-
this.body_ = JSON.parse(this.message);
|
|
60
|
-
} catch {
|
|
61
|
-
this.body_ = this.message;
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
method: this.method,
|
|
65
|
-
path: this.path,
|
|
66
|
-
status: this.status,
|
|
67
|
-
headers: this.headers,
|
|
68
|
-
message: this.body_,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
export namespace HttpError {
|
|
73
|
-
/**
|
|
74
|
-
* Returned type of {@link HttpError.toJSON} method.
|
|
75
|
-
*/
|
|
76
|
-
export interface IProps<T> {
|
|
77
|
-
method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
|
|
78
|
-
path: string;
|
|
79
|
-
status: number;
|
|
80
|
-
headers: Record<string, string | string[]>;
|
|
81
|
-
message: T;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const NOT_YET = {} as any;
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Error.
|
|
3
|
+
*
|
|
4
|
+
* `HttpError` is a type of error class who've been thrown by the remote HTTP server.
|
|
5
|
+
*
|
|
6
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
7
|
+
*/
|
|
8
|
+
export class HttpError extends Error {
|
|
9
|
+
/**
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
private body_: any = NOT_YET;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Initializer Constructor.
|
|
16
|
+
*
|
|
17
|
+
* @param method Method of the HTTP request.
|
|
18
|
+
* @param path Path of the HTTP request.
|
|
19
|
+
* @param status Status code from the remote HTTP server.
|
|
20
|
+
* @param message Error message from the remote HTTP server.
|
|
21
|
+
*/
|
|
22
|
+
public constructor(
|
|
23
|
+
public readonly method:
|
|
24
|
+
| "GET"
|
|
25
|
+
| "DELETE"
|
|
26
|
+
| "POST"
|
|
27
|
+
| "PUT"
|
|
28
|
+
| "PATCH"
|
|
29
|
+
| "HEAD",
|
|
30
|
+
public readonly path: string,
|
|
31
|
+
public readonly status: number,
|
|
32
|
+
public readonly headers: Record<string, string | string[]>,
|
|
33
|
+
message: string,
|
|
34
|
+
) {
|
|
35
|
+
super(message);
|
|
36
|
+
|
|
37
|
+
// INHERITANCE POLYFILL
|
|
38
|
+
const proto: HttpError = new.target.prototype;
|
|
39
|
+
if (Object.setPrototypeOf) Object.setPrototypeOf(this, proto);
|
|
40
|
+
else (this as any).__proto__ = proto;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `HttpError` to JSON.
|
|
45
|
+
*
|
|
46
|
+
* When you call `JSON.stringify()` function on current `HttpError` instance,
|
|
47
|
+
* this `HttpError.toJSON()` method would be automatically called.
|
|
48
|
+
*
|
|
49
|
+
* Also, if response body from the remote HTTP server forms a JSON object,
|
|
50
|
+
* this `HttpError.toJSON()` method would be useful because it returns the
|
|
51
|
+
* parsed JSON object about the {@link message} property.
|
|
52
|
+
*
|
|
53
|
+
* @template T Expected type of the response body.
|
|
54
|
+
* @returns JSON object of the `HttpError`.
|
|
55
|
+
*/
|
|
56
|
+
public toJSON<T>(): HttpError.IProps<T> {
|
|
57
|
+
if (this.body_ === NOT_YET)
|
|
58
|
+
try {
|
|
59
|
+
this.body_ = JSON.parse(this.message);
|
|
60
|
+
} catch {
|
|
61
|
+
this.body_ = this.message;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
method: this.method,
|
|
65
|
+
path: this.path,
|
|
66
|
+
status: this.status,
|
|
67
|
+
headers: this.headers,
|
|
68
|
+
message: this.body_,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export namespace HttpError {
|
|
73
|
+
/**
|
|
74
|
+
* Returned type of {@link HttpError.toJSON} method.
|
|
75
|
+
*/
|
|
76
|
+
export interface IProps<T> {
|
|
77
|
+
method: "GET" | "DELETE" | "POST" | "PUT" | "PATCH" | "HEAD";
|
|
78
|
+
path: string;
|
|
79
|
+
status: number;
|
|
80
|
+
headers: Record<string, string | string[]>;
|
|
81
|
+
message: T;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const NOT_YET = {} as any;
|