@nestia/fetcher 2.4.3 → 3.0.0-dev.20231209
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/EncryptedFetcher.js +9 -6
- package/lib/EncryptedFetcher.js.map +1 -1
- package/lib/IEncryptionPassword.d.ts +8 -2
- package/lib/{AesPkcs5.d.ts → internal/AesPkcs5.d.ts} +2 -2
- package/lib/{AesPkcs5.js → internal/AesPkcs5.js} +16 -8
- package/lib/internal/AesPkcs5.js.map +1 -0
- package/lib/internal/FetcherBase.d.ts +2 -2
- package/lib/internal/FetcherBase.js +38 -28
- package/lib/internal/FetcherBase.js.map +1 -1
- package/lib/internal/IFetchRoute.d.ts +1 -1
- package/package.json +1 -1
- package/src/EncryptedFetcher.ts +18 -14
- package/src/HttpError.ts +85 -85
- package/src/IConnection.ts +237 -237
- package/src/IEncryptionPassword.ts +8 -2
- package/src/IPropagation.ts +102 -102
- package/src/IRandomGenerator.ts +38 -38
- package/src/PlainFetcher.ts +106 -106
- package/src/Primitive.ts +135 -135
- package/src/Resolved.ts +116 -116
- package/src/index.ts +7 -7
- package/src/{AesPkcs5.ts → internal/AesPkcs5.ts} +22 -6
- package/src/internal/FetcherBase.ts +10 -5
- package/src/internal/IFetchRoute.ts +1 -0
- package/src/internal/Singleton.ts +20 -20
- package/lib/AesPkcs5.js.map +0 -1
package/lib/EncryptedFetcher.js
CHANGED
|
@@ -37,7 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.EncryptedFetcher = void 0;
|
|
40
|
-
var AesPkcs5_1 = require("./AesPkcs5");
|
|
40
|
+
var AesPkcs5_1 = require("./internal/AesPkcs5");
|
|
41
41
|
var FetcherBase_1 = require("./internal/FetcherBase");
|
|
42
42
|
/**
|
|
43
43
|
* Utility class for `fetch` functions used in `@nestia/sdk` with encryption.
|
|
@@ -85,8 +85,8 @@ var EncryptedFetcher;
|
|
|
85
85
|
decode: ((_d = route.response) === null || _d === void 0 ? void 0 : _d.encrypted) === true
|
|
86
86
|
? function (input, headers) {
|
|
87
87
|
var p = closure("decode")(headers, input);
|
|
88
|
-
var
|
|
89
|
-
return
|
|
88
|
+
var str = new TextDecoder().decode(AesPkcs5_1.AesPkcs5.decrypt(input, p.key, p.iv));
|
|
89
|
+
return str.length ? JSON.parse(str) : str;
|
|
90
90
|
}
|
|
91
91
|
: function (input) { return input; },
|
|
92
92
|
})(connection, route, input, stringify)];
|
|
@@ -123,9 +123,12 @@ var EncryptedFetcher;
|
|
|
123
123
|
: function (input) { return input; },
|
|
124
124
|
decode: ((_d = route.response) === null || _d === void 0 ? void 0 : _d.encrypted) === true
|
|
125
125
|
? function (input, headers) {
|
|
126
|
-
var
|
|
127
|
-
var
|
|
128
|
-
|
|
126
|
+
var binary = input;
|
|
127
|
+
var p = closure("decode")(headers, binary);
|
|
128
|
+
if (!binary.length)
|
|
129
|
+
return;
|
|
130
|
+
var str = new TextDecoder().decode(AesPkcs5_1.AesPkcs5.decrypt(binary, p.key, p.iv));
|
|
131
|
+
return str.length ? JSON.parse(str) : str;
|
|
129
132
|
}
|
|
130
133
|
: function (input) { return input; },
|
|
131
134
|
})(connection, route, input, stringify)];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EncryptedFetcher.js","sourceRoot":"","sources":["../src/EncryptedFetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"EncryptedFetcher.js","sourceRoot":"","sources":["../src/EncryptedFetcher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,gDAA+C;AAC/C,sDAAqD;AAGrD;;;;;;;;;;;;;GAaG;AACH,IAAiB,gBAAgB,CA4JhC;AA5JD,WAAiB,gBAAgB;IAuC/B,SAAsB,KAAK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C,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,CAAoB,EAAC;;;KAC5D;IApDqB,0BAAS,YAoD9B,CAAA;AACH,CAAC,EA5JgB,gBAAgB,gCAAhB,gBAAgB,QA4JhC"}
|
|
@@ -39,9 +39,15 @@ export declare namespace IEncryptionPassword {
|
|
|
39
39
|
/**
|
|
40
40
|
* Properties for the closure.
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
type IProps = IDecodeProps | IEncodeProps;
|
|
43
|
+
interface IDecodeProps {
|
|
44
|
+
headers: Record<string, IConnection.HeaderValue | undefined>;
|
|
45
|
+
body: Uint8Array;
|
|
46
|
+
direction: "decode";
|
|
47
|
+
}
|
|
48
|
+
interface IEncodeProps {
|
|
43
49
|
headers: Record<string, IConnection.HeaderValue | undefined>;
|
|
44
50
|
body: string;
|
|
45
|
-
direction: "encode"
|
|
51
|
+
direction: "encode";
|
|
46
52
|
}
|
|
47
53
|
}
|
|
@@ -17,7 +17,7 @@ export declare namespace AesPkcs5 {
|
|
|
17
17
|
* @param iv Initializer Vector for the encryption
|
|
18
18
|
* @return Encrypted data
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
const encrypt: (data: string | Uint8Array, key: string, iv: string) => Uint8Array;
|
|
21
21
|
/**
|
|
22
22
|
* Decrypt data.
|
|
23
23
|
*
|
|
@@ -26,5 +26,5 @@ export declare namespace AesPkcs5 {
|
|
|
26
26
|
* @param iv Initializer Vector for the decryption
|
|
27
27
|
* @return Decrypted data.
|
|
28
28
|
*/
|
|
29
|
-
|
|
29
|
+
const decrypt: (data: Uint8Array, key: string, iv: string) => Uint8Array;
|
|
30
30
|
}
|
|
@@ -25,12 +25,11 @@ var AesPkcs5;
|
|
|
25
25
|
* @param iv Initializer Vector for the encryption
|
|
26
26
|
* @return Encrypted data
|
|
27
27
|
*/
|
|
28
|
-
function
|
|
28
|
+
AesPkcs5.encrypt = function (data, key, iv) {
|
|
29
29
|
var bytes = key.length * 8;
|
|
30
30
|
var cipher = crypto_1.default.createCipheriv("AES-".concat(bytes, "-CBC"), key, iv);
|
|
31
|
-
return cipher.update(data,
|
|
32
|
-
}
|
|
33
|
-
AesPkcs5.encrypt = encrypt;
|
|
31
|
+
return concat(cipher.update(data), cipher.final());
|
|
32
|
+
};
|
|
34
33
|
/**
|
|
35
34
|
* Decrypt data.
|
|
36
35
|
*
|
|
@@ -39,11 +38,20 @@ var AesPkcs5;
|
|
|
39
38
|
* @param iv Initializer Vector for the decryption
|
|
40
39
|
* @return Decrypted data.
|
|
41
40
|
*/
|
|
42
|
-
function
|
|
41
|
+
AesPkcs5.decrypt = function (data, key, iv) {
|
|
43
42
|
var bytes = key.length * 8;
|
|
44
43
|
var decipher = crypto_1.default.createDecipheriv("AES-".concat(bytes, "-CBC"), key, iv);
|
|
45
|
-
return decipher.update(data,
|
|
46
|
-
}
|
|
47
|
-
|
|
44
|
+
return concat(decipher.update(data), decipher.final());
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
var concat = function (x, y) {
|
|
50
|
+
if (x.length === 0)
|
|
51
|
+
return y;
|
|
52
|
+
if (y.length === 0)
|
|
53
|
+
return x;
|
|
54
|
+
return Buffer.concat([x, y]);
|
|
55
|
+
};
|
|
48
56
|
})(AesPkcs5 || (exports.AesPkcs5 = AesPkcs5 = {}));
|
|
49
57
|
//# sourceMappingURL=AesPkcs5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AesPkcs5.js","sourceRoot":"","sources":["../../src/internal/AesPkcs5.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA4B;AAE5B;;;;;;;;;GASG;AACH,IAAiB,QAAQ,CAqDxB;AArDD,WAAiB,QAAQ;IACvB;;;;;;;OAOG;IACU,gBAAO,GAAG,UACrB,IAAyB,EACzB,GAAW,EACX,EAAU;QAEV,IAAM,KAAK,GAAW,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;QACrC,IAAM,MAAM,GAAkB,gBAAM,CAAC,cAAc,CACjD,cAAO,KAAK,SAAM,EAClB,GAAG,EACH,EAAE,CACH,CAAC;QACF,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;IACrD,CAAC,CAAC;IAEF;;;;;;;OAOG;IACU,gBAAO,GAAG,UACrB,IAAgB,EAChB,GAAW,EACX,EAAU;QAEV,IAAM,KAAK,GAAW,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC;QACrC,IAAM,QAAQ,GAAoB,gBAAM,CAAC,gBAAgB,CACvD,cAAO,KAAK,SAAM,EAClB,GAAG,EACH,EAAE,CACH,CAAC;QACF,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IACzD,CAAC,CAAC;IAEF;;OAEG;IACH,IAAM,MAAM,GAAG,UAAC,CAAS,EAAE,CAAS;QAClC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,CAAC,CAAC;QAC7B,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,CAAC,CAAC;QAC7B,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC/B,CAAC,CAAC;AACJ,CAAC,EArDgB,QAAQ,wBAAR,QAAQ,QAqDxB"}
|
|
@@ -5,8 +5,8 @@ import { IFetchRoute } from "./IFetchRoute";
|
|
|
5
5
|
export declare namespace FetcherBase {
|
|
6
6
|
interface IProps {
|
|
7
7
|
className: string;
|
|
8
|
-
encode: (input: any, headers: Record<string, IConnection.HeaderValue | undefined>) => string;
|
|
9
|
-
decode: (input: string, headers: Record<string, IConnection.HeaderValue | undefined>) => any;
|
|
8
|
+
encode: (input: any, headers: Record<string, IConnection.HeaderValue | undefined>) => string | Uint8Array;
|
|
9
|
+
decode: (input: string | Uint8Array, headers: Record<string, IConnection.HeaderValue | undefined>) => any;
|
|
10
10
|
}
|
|
11
11
|
const fetch: (props: IProps) => <Input, Output>(connection: IConnection, route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">, input?: Input | undefined, stringify?: ((input: Input) => string) | undefined) => Promise<Primitive<Output>>;
|
|
12
12
|
const propagate: (props: IProps) => <Input>(connection: IConnection, route: IFetchRoute<"DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT">, input?: Input | undefined, stringify?: ((input: Input) => string) | undefined) => Promise<IPropagation<any, any>>;
|
|
@@ -119,18 +119,18 @@ var FetcherBase;
|
|
|
119
119
|
var _Propagate = function (method) {
|
|
120
120
|
return function (props) {
|
|
121
121
|
return function (connection, route, input, stringify) { return __awaiter(_this, void 0, void 0, function () {
|
|
122
|
-
var headers, init, path, url, response, _a, result, _b, type, text, query, _c, _d, _e, _f;
|
|
123
|
-
var
|
|
124
|
-
return __generator(this, function (
|
|
125
|
-
switch (
|
|
122
|
+
var headers, init, path, url, response, _a, result, _b, type, text, query, _c, _d, _e, _f, _g, _h;
|
|
123
|
+
var _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
|
|
124
|
+
return __generator(this, function (_u) {
|
|
125
|
+
switch (_u.label) {
|
|
126
126
|
case 0:
|
|
127
|
-
headers = __assign({}, ((
|
|
127
|
+
headers = __assign({}, ((_j = connection.headers) !== null && _j !== void 0 ? _j : {}));
|
|
128
128
|
if (input !== undefined) {
|
|
129
|
-
if (((
|
|
129
|
+
if (((_k = route.request) === null || _k === void 0 ? void 0 : _k.type) === undefined)
|
|
130
130
|
throw new Error("Error on ".concat(props.className, ".fetch(): no content-type being configured."));
|
|
131
131
|
headers["Content-Type"] = route.request.type;
|
|
132
132
|
}
|
|
133
|
-
init = __assign(__assign({}, ((
|
|
133
|
+
init = __assign(__assign({}, ((_l = connection.options) !== null && _l !== void 0 ? _l : {})), { method: route.method, headers: (function () {
|
|
134
134
|
var e_1, _a, e_2, _b;
|
|
135
135
|
var output = [];
|
|
136
136
|
try {
|
|
@@ -169,9 +169,10 @@ var FetcherBase;
|
|
|
169
169
|
if (input !== undefined)
|
|
170
170
|
init.body = props.encode(
|
|
171
171
|
// BODY TRANSFORM
|
|
172
|
-
((
|
|
172
|
+
((_m = route.request) === null || _m === void 0 ? void 0 : _m.type) === "application/x-www-form-urlencoded"
|
|
173
173
|
? request_query_body(input)
|
|
174
|
-
: ((
|
|
174
|
+
: ((_o = route.request) === null || _o === void 0 ? void 0 : _o.type) !== "application/octet-stream" &&
|
|
175
|
+
((_p = route.request) === null || _p === void 0 ? void 0 : _p.type) !== "text/plain"
|
|
175
176
|
? (stringify !== null && stringify !== void 0 ? stringify : JSON.stringify)(input)
|
|
176
177
|
: input, headers);
|
|
177
178
|
path = connection.host[connection.host.length - 1] !== "/" &&
|
|
@@ -179,16 +180,16 @@ var FetcherBase;
|
|
|
179
180
|
? "/".concat(route.path)
|
|
180
181
|
: route.path;
|
|
181
182
|
url = new URL("".concat(connection.host).concat(path));
|
|
182
|
-
if (!((
|
|
183
|
-
_a =
|
|
183
|
+
if (!((_q = connection.fetch) !== null && _q !== void 0)) return [3 /*break*/, 1];
|
|
184
|
+
_a = _q;
|
|
184
185
|
return [3 /*break*/, 3];
|
|
185
186
|
case 1: return [4 /*yield*/, polyfill.get()];
|
|
186
187
|
case 2:
|
|
187
|
-
_a = (
|
|
188
|
-
|
|
188
|
+
_a = (_u.sent());
|
|
189
|
+
_u.label = 3;
|
|
189
190
|
case 3: return [4 /*yield*/, (_a)(url.href, init)];
|
|
190
191
|
case 4:
|
|
191
|
-
response =
|
|
192
|
+
response = _u.sent();
|
|
192
193
|
result = {
|
|
193
194
|
success: response.status === 200 ||
|
|
194
195
|
response.status === 201 ||
|
|
@@ -203,7 +204,7 @@ var FetcherBase;
|
|
|
203
204
|
return [4 /*yield*/, response.text()];
|
|
204
205
|
case 5:
|
|
205
206
|
// WHEN FAILED
|
|
206
|
-
_b.data =
|
|
207
|
+
_b.data = _u.sent();
|
|
207
208
|
type = response.headers.get("content-type");
|
|
208
209
|
if (method !== "fetch" &&
|
|
209
210
|
type &&
|
|
@@ -211,35 +212,44 @@ var FetcherBase;
|
|
|
211
212
|
try {
|
|
212
213
|
result.data = JSON.parse(result.data);
|
|
213
214
|
}
|
|
214
|
-
catch (
|
|
215
|
-
return [3 /*break*/,
|
|
215
|
+
catch (_v) { }
|
|
216
|
+
return [3 /*break*/, 15];
|
|
216
217
|
case 6:
|
|
217
218
|
if (!(route.method === "HEAD")) return [3 /*break*/, 7];
|
|
218
219
|
result.data = undefined;
|
|
219
|
-
return [3 /*break*/,
|
|
220
|
+
return [3 /*break*/, 15];
|
|
220
221
|
case 7:
|
|
221
|
-
if (!(((
|
|
222
|
+
if (!(((_r = route.response) === null || _r === void 0 ? void 0 : _r.type) === "application/json")) return [3 /*break*/, 9];
|
|
222
223
|
return [4 /*yield*/, response.text()];
|
|
223
224
|
case 8:
|
|
224
|
-
text =
|
|
225
|
+
text = _u.sent();
|
|
225
226
|
result.data = text.length ? JSON.parse(text) : undefined;
|
|
226
|
-
return [3 /*break*/,
|
|
227
|
+
return [3 /*break*/, 15];
|
|
227
228
|
case 9:
|
|
228
|
-
if (!(((
|
|
229
|
+
if (!(((_s = route.response) === null || _s === void 0 ? void 0 : _s.type) === "application/x-www-form-urlencoded")) return [3 /*break*/, 11];
|
|
229
230
|
_c = URLSearchParams.bind;
|
|
230
231
|
return [4 /*yield*/, response.text()];
|
|
231
232
|
case 10:
|
|
232
|
-
query = new (_c.apply(URLSearchParams, [void 0,
|
|
233
|
+
query = new (_c.apply(URLSearchParams, [void 0, _u.sent()]))();
|
|
233
234
|
result.data = route.parseQuery ? route.parseQuery(query) : query;
|
|
234
|
-
return [3 /*break*/,
|
|
235
|
+
return [3 /*break*/, 15];
|
|
235
236
|
case 11:
|
|
237
|
+
if (!(((_t = route.response) === null || _t === void 0 ? void 0 : _t.type) === "application/octet-stream")) return [3 /*break*/, 13];
|
|
236
238
|
_d = result;
|
|
237
239
|
_f = (_e = props).decode;
|
|
238
|
-
|
|
240
|
+
_g = Uint8Array.bind;
|
|
241
|
+
return [4 /*yield*/, response.arrayBuffer()];
|
|
239
242
|
case 12:
|
|
240
|
-
_d.data = _f.apply(_e, [
|
|
241
|
-
|
|
242
|
-
|
|
243
|
+
_d.data = _f.apply(_e, [new (_g.apply(Uint8Array, [void 0, _u.sent()]))(),
|
|
244
|
+
result.headers]);
|
|
245
|
+
return [3 /*break*/, 15];
|
|
246
|
+
case 13:
|
|
247
|
+
_h = result;
|
|
248
|
+
return [4 /*yield*/, response.text()];
|
|
249
|
+
case 14:
|
|
250
|
+
_h.data = _u.sent();
|
|
251
|
+
_u.label = 15;
|
|
252
|
+
case 15: return [2 /*return*/, result];
|
|
243
253
|
}
|
|
244
254
|
});
|
|
245
255
|
}); };
|
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"FetcherBase.js","sourceRoot":"","sources":["../../src/internal/FetcherBase.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAA8B;AAE9B,0CAAyC;AAKzC,yCAAwC;AAExC,IAAiB,WAAW,CAmK3B;AAnKD,WAAiB,WAAW;;IAab,iBAAK,GAChB,UAAC,KAAa;QACd,OAAA,UACE,UAAuB,EACvB,KAAwE,EACxE,KAAa,EACb,SAAoC;;;;4BAErB,qBAAM,UAAU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C,OAAO,CAAC,KAAK,CAAC,CAAA,gBAAA;YAArC,IAAA,KAAA,mBAAY,EAAX,GAAG,QAAA,EAAE,KAAK,QAAA;oBAAV,GAAG,EAAE,KAAK;SAIW;;;;;;;;;IACjC,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AAEF;;GAEG;AACH,IAAM,0BAA0B,GAAG,UACjC,OAAgB;IAEhB,IAAM,MAAM,GAAsC,EAAE,CAAC;IACrD,OAAO,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG;;;QACzB,IAAI,GAAG,KAAK,YAAY,EAAE,CAAC;YACzB,MAAA,MAAM,CAAC,GAAG,qCAAV,MAAM,CAAC,GAAG,IAAM,EAAE,EAAC;YACnB,CAAA,KAAC,MAAM,CAAC,GAAG,CAAc,CAAA,CAAC,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"}
|
|
@@ -43,7 +43,7 @@ export declare namespace IFetchRoute {
|
|
|
43
43
|
* Describes how content-type being used in body, and whether encrypted or not.
|
|
44
44
|
*/
|
|
45
45
|
interface IBody {
|
|
46
|
-
type: "application/json" | "application/x-www-form-urlencoded" | "text/plain";
|
|
46
|
+
type: "application/json" | "application/octet-stream" | "application/x-www-form-urlencoded" | "text/plain";
|
|
47
47
|
encrypted?: boolean;
|
|
48
48
|
}
|
|
49
49
|
}
|
package/package.json
CHANGED
package/src/EncryptedFetcher.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { AesPkcs5 } from "./AesPkcs5";
|
|
2
1
|
import { IConnection } from "./IConnection";
|
|
3
2
|
import { IEncryptionPassword } from "./IEncryptionPassword";
|
|
4
3
|
import { IPropagation } from "./IPropagation";
|
|
5
4
|
import { Primitive } from "./Primitive";
|
|
5
|
+
import { AesPkcs5 } from "./internal/AesPkcs5";
|
|
6
6
|
import { FetcherBase } from "./internal/FetcherBase";
|
|
7
7
|
import { IFetchRoute } from "./internal/IFetchRoute";
|
|
8
8
|
|
|
@@ -77,21 +77,20 @@ export namespace EncryptedFetcher {
|
|
|
77
77
|
? (direction: "encode" | "decode") =>
|
|
78
78
|
(
|
|
79
79
|
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
80
|
-
body: string,
|
|
80
|
+
body: string | Uint8Array,
|
|
81
81
|
) =>
|
|
82
82
|
(connection.encryption as IEncryptionPassword.Closure)({
|
|
83
83
|
headers,
|
|
84
|
-
body,
|
|
84
|
+
body: body as any,
|
|
85
85
|
direction,
|
|
86
86
|
})
|
|
87
87
|
: () => () => connection.encryption as IEncryptionPassword;
|
|
88
|
-
|
|
89
88
|
return FetcherBase.fetch({
|
|
90
89
|
className: "EncryptedFetcher",
|
|
91
90
|
encode:
|
|
92
91
|
route.request?.encrypted === true
|
|
93
92
|
? (input, headers) => {
|
|
94
|
-
const p
|
|
93
|
+
const p = closure("encode")(headers, input);
|
|
95
94
|
return AesPkcs5.encrypt(
|
|
96
95
|
(stringify ?? JSON.stringify)(input),
|
|
97
96
|
p.key,
|
|
@@ -103,8 +102,10 @@ export namespace EncryptedFetcher {
|
|
|
103
102
|
route.response?.encrypted === true
|
|
104
103
|
? (input, headers) => {
|
|
105
104
|
const p: IEncryptionPassword = closure("decode")(headers, input);
|
|
106
|
-
const
|
|
107
|
-
|
|
105
|
+
const str: string = new TextDecoder().decode(
|
|
106
|
+
AesPkcs5.decrypt(input as Uint8Array, p.key, p.iv),
|
|
107
|
+
);
|
|
108
|
+
return str.length ? JSON.parse(str) : str;
|
|
108
109
|
}
|
|
109
110
|
: (input) => input,
|
|
110
111
|
})(connection, route, input, stringify);
|
|
@@ -140,21 +141,20 @@ export namespace EncryptedFetcher {
|
|
|
140
141
|
? (direction: "encode" | "decode") =>
|
|
141
142
|
(
|
|
142
143
|
headers: Record<string, IConnection.HeaderValue | undefined>,
|
|
143
|
-
body: string,
|
|
144
|
+
body: string | Uint8Array,
|
|
144
145
|
) =>
|
|
145
146
|
(connection.encryption as IEncryptionPassword.Closure)({
|
|
146
147
|
headers,
|
|
147
|
-
body,
|
|
148
|
+
body: body as any,
|
|
148
149
|
direction,
|
|
149
150
|
})
|
|
150
151
|
: () => () => connection.encryption as IEncryptionPassword;
|
|
151
|
-
|
|
152
152
|
return FetcherBase.propagate({
|
|
153
153
|
className: "EncryptedFetcher",
|
|
154
154
|
encode:
|
|
155
155
|
route.request?.encrypted === true
|
|
156
156
|
? (input, headers) => {
|
|
157
|
-
const p
|
|
157
|
+
const p = closure("encode")(headers, input);
|
|
158
158
|
return AesPkcs5.encrypt(
|
|
159
159
|
(stringify ?? JSON.stringify)(input),
|
|
160
160
|
p.key,
|
|
@@ -165,9 +165,13 @@ export namespace EncryptedFetcher {
|
|
|
165
165
|
decode:
|
|
166
166
|
route.response?.encrypted === true
|
|
167
167
|
? (input, headers) => {
|
|
168
|
-
const
|
|
169
|
-
const
|
|
170
|
-
|
|
168
|
+
const binary: Uint8Array = input as Uint8Array;
|
|
169
|
+
const p: IEncryptionPassword = closure("decode")(headers, binary);
|
|
170
|
+
if (!binary.length) return;
|
|
171
|
+
const str: string = new TextDecoder().decode(
|
|
172
|
+
AesPkcs5.decrypt(binary, p.key, p.iv),
|
|
173
|
+
);
|
|
174
|
+
return str.length ? JSON.parse(str) : str;
|
|
171
175
|
}
|
|
172
176
|
: (input) => input,
|
|
173
177
|
})(connection, route, input, stringify) as Promise<Output>;
|
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;
|