@miqro/core 1.4.2 → 1.5.1
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/README.md +16 -43
- package/dist/api-router.d.ts +36 -0
- package/dist/{handler/api-router.js → api-router.js} +1 -2
- package/dist/{handler/app.d.ts → app.d.ts} +1 -1
- package/dist/{handler/app.js → app.js} +8 -8
- package/dist/common/api-router-helpers.d.ts +31 -0
- package/dist/common/api-router-helpers.js +2 -0
- package/dist/{handler → common}/api-router-utils.d.ts +7 -6
- package/dist/{handler → common}/api-router-utils.js +4 -5
- package/dist/{util → common}/config/config.d.ts +4 -4
- package/dist/{util → common}/config/config.js +31 -29
- package/dist/{util → common}/config/featuretoggle.d.ts +0 -0
- package/dist/{util → common}/config/featuretoggle.js +0 -1
- package/dist/{util → common}/config/index.d.ts +1 -1
- package/dist/{util → common}/config/index.js +1 -2
- package/dist/common/config/loader.d.ts +13 -0
- package/dist/{util/loader/index.js → common/config/loader.js} +11 -6
- package/dist/{handler/common → common}/context.d.ts +5 -5
- package/dist/{handler/common → common}/context.js +3 -17
- package/dist/common/error/badrequesterror.d.ts +3 -0
- package/dist/common/error/badrequesterror.js +10 -0
- package/dist/common/error/configfilenotfound.d.ts +3 -0
- package/dist/{util → common}/error/configfilenotfound.js +1 -3
- package/dist/common/error/forbiddenerror.d.ts +3 -0
- package/dist/{util → common}/error/forbiddenerror.js +1 -3
- package/dist/{util → common}/error/index.d.ts +1 -1
- package/dist/{util → common}/error/index.js +1 -2
- package/dist/common/error/notfound.d.ts +3 -0
- package/dist/{util → common}/error/notfound.js +1 -3
- package/dist/common/error/notimplemented.d.ts +3 -0
- package/dist/{util → common}/error/notimplemented.js +1 -3
- package/dist/common/error/unauthorizederror.d.ts +3 -0
- package/dist/{util → common}/error/unauthorizederror.js +1 -3
- package/dist/{handler/common → common}/helpers.d.ts +0 -0
- package/dist/{handler/common → common}/helpers.js +0 -1
- package/dist/{util → common}/index.d.ts +5 -5
- package/dist/{util → common}/index.js +5 -6
- package/dist/{util → common}/logger/common.d.ts +0 -0
- package/dist/{util → common}/logger/common.js +0 -1
- package/dist/{util → common}/logger/factory.d.ts +0 -0
- package/dist/{util → common}/logger/factory.js +8 -3
- package/dist/{util → common}/logger/formatter.d.ts +0 -0
- package/dist/{util → common}/logger/formatter.js +0 -1
- package/dist/{util → common}/logger/index.d.ts +1 -1
- package/dist/{util → common}/logger/index.js +1 -1
- package/dist/{util → common}/logger/logger.d.ts +0 -0
- package/dist/{util → common}/logger/logger.js +0 -1
- package/dist/{util → common}/logger/transport.d.ts +0 -0
- package/dist/{util → common}/logger/transport.js +2 -3
- package/dist/{util → common}/parser/common.d.ts +0 -0
- package/dist/{util → common}/parser/common.js +0 -1
- package/dist/common/parser/error.d.ts +4 -0
- package/dist/common/parser/error.js +11 -0
- package/dist/{util → common}/parser/get.d.ts +0 -0
- package/dist/{util → common}/parser/get.js +1 -2
- package/dist/common/parser/helpers.d.ts +1 -0
- package/dist/common/parser/helpers.js +9 -0
- package/dist/{util → common}/parser/index.d.ts +1 -0
- package/dist/{util → common}/parser/index.js +1 -1
- package/dist/{util → common}/parser/parser.d.ts +0 -0
- package/dist/{util → common}/parser/parser.js +1 -2
- package/dist/{util → common}/parser/types/any.d.ts +0 -0
- package/dist/{util → common}/parser/types/any.js +0 -1
- package/dist/{util → common}/parser/types/array.d.ts +0 -0
- package/dist/{util → common}/parser/types/array.js +0 -1
- package/dist/{util → common}/parser/types/boolean.d.ts +0 -0
- package/dist/{util → common}/parser/types/boolean.js +0 -1
- package/dist/{util → common}/parser/types/email.d.ts +0 -0
- package/dist/{util → common}/parser/types/email.js +0 -1
- package/dist/{util → common}/parser/types/enum.d.ts +0 -0
- package/dist/{util → common}/parser/types/enum.js +1 -2
- package/dist/{util → common}/parser/types/index.d.ts +0 -0
- package/dist/{util → common}/parser/types/index.js +0 -1
- package/dist/{util → common}/parser/types/multiple.d.ts +0 -0
- package/dist/{util → common}/parser/types/multiple.js +1 -2
- package/dist/{util → common}/parser/types/nested.d.ts +0 -0
- package/dist/{util → common}/parser/types/nested.js +1 -2
- package/dist/{util → common}/parser/types/number.d.ts +0 -0
- package/dist/{util → common}/parser/types/number.js +0 -1
- package/dist/{util → common}/parser/types/object.d.ts +0 -0
- package/dist/{util → common}/parser/types/object.js +0 -1
- package/dist/{util → common}/parser/types/regex.d.ts +0 -0
- package/dist/{util → common}/parser/types/regex.js +1 -2
- package/dist/{util → common}/parser/types/string.d.ts +0 -0
- package/dist/{util → common}/parser/types/string.js +0 -1
- package/dist/{util → common}/parser/types/url.d.ts +0 -0
- package/dist/{util → common}/parser/types/url.js +1 -2
- package/dist/common/router-helpers.d.ts +3 -0
- package/dist/common/router-helpers.js +21 -0
- package/dist/common/router-utils.d.ts +33 -0
- package/dist/common/router-utils.js +62 -0
- package/dist/{handler/middleware/session.d.ts → common/session/common.d.ts} +16 -3
- package/dist/{util → common}/session/common.js +0 -1
- package/dist/{util → common}/session/index.d.ts +0 -0
- package/dist/{util → common}/session/index.js +0 -1
- package/dist/{util → common}/session/service.d.ts +2 -2
- package/dist/{util → common}/session/service.js +2 -3
- package/dist/{handler/common → common}/tokenize-match.d.ts +0 -0
- package/dist/{handler/common → common}/tokenize-match.js +0 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -3
- package/dist/{handler/middleware → middleware}/buffer.d.ts +0 -0
- package/dist/middleware/buffer.js +79 -0
- package/dist/{handler/middleware → middleware}/cookie.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/cookie.js +1 -2
- package/dist/{handler/middleware → middleware}/cors.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/cors.js +4 -6
- package/dist/{handler/middleware → middleware}/form.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/form.js +5 -7
- package/dist/middleware/group.d.ts +2 -0
- package/dist/{handler/middleware → middleware}/group.js +7 -8
- package/dist/{handler/middleware → middleware}/index.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/index.js +8 -9
- package/dist/{handler/middleware → middleware}/json.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/json.js +6 -8
- package/dist/{handler/middleware → middleware}/logger.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/logger.js +1 -2
- package/dist/{handler/middleware → middleware}/parse.d.ts +1 -3
- package/dist/{handler/middleware → middleware}/parse.js +3 -4
- package/dist/middleware/session.d.ts +2 -0
- package/dist/{handler/middleware → middleware}/session.js +11 -14
- package/dist/{handler/middleware → middleware}/tag.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/tag.js +1 -2
- package/dist/{handler/middleware → middleware}/text.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/text.js +5 -7
- package/dist/{handler/proxy.d.ts → proxy.d.ts} +1 -1
- package/dist/{handler/proxy.js → proxy.js} +3 -4
- package/dist/{handler/common/router.d.ts → router.d.ts} +31 -7
- package/dist/{handler/common/router-utils.js → router.js} +101 -73
- package/package.json +11 -16
- package/dist/handler/api-router-utils.js.map +0 -1
- package/dist/handler/api-router.d.ts +0 -70
- package/dist/handler/api-router.js.map +0 -1
- package/dist/handler/app.js.map +0 -1
- package/dist/handler/common/context.js.map +0 -1
- package/dist/handler/common/helpers.js.map +0 -1
- package/dist/handler/common/index.d.ts +0 -2
- package/dist/handler/common/index.js +0 -15
- package/dist/handler/common/index.js.map +0 -1
- package/dist/handler/common/router-utils.d.ts +0 -59
- package/dist/handler/common/router-utils.js.map +0 -1
- package/dist/handler/common/router.js +0 -93
- package/dist/handler/common/router.js.map +0 -1
- package/dist/handler/common/tokenize-match.js.map +0 -1
- package/dist/handler/index.d.ts +0 -5
- package/dist/handler/index.js +0 -18
- package/dist/handler/index.js.map +0 -1
- package/dist/handler/middleware/buffer.js +0 -79
- package/dist/handler/middleware/buffer.js.map +0 -1
- package/dist/handler/middleware/cookie.js.map +0 -1
- package/dist/handler/middleware/cors.js.map +0 -1
- package/dist/handler/middleware/form.js.map +0 -1
- package/dist/handler/middleware/group.d.ts +0 -3
- package/dist/handler/middleware/group.js.map +0 -1
- package/dist/handler/middleware/index.js.map +0 -1
- package/dist/handler/middleware/json.js.map +0 -1
- package/dist/handler/middleware/logger.js.map +0 -1
- package/dist/handler/middleware/parse.js.map +0 -1
- package/dist/handler/middleware/session.js.map +0 -1
- package/dist/handler/middleware/tag.js.map +0 -1
- package/dist/handler/middleware/text.js.map +0 -1
- package/dist/handler/proxy.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/util/cli/index.d.ts +0 -17
- package/dist/util/cli/index.js +0 -84
- package/dist/util/cli/index.js.map +0 -1
- package/dist/util/config/browser.d.ts +0 -1
- package/dist/util/config/browser.js +0 -10
- package/dist/util/config/browser.js.map +0 -1
- package/dist/util/config/config.js.map +0 -1
- package/dist/util/config/featuretoggle.js.map +0 -1
- package/dist/util/config/index.js.map +0 -1
- package/dist/util/error/configfilenotfound.d.ts +0 -4
- package/dist/util/error/configfilenotfound.js.map +0 -1
- package/dist/util/error/forbiddenerror.d.ts +0 -4
- package/dist/util/error/forbiddenerror.js.map +0 -1
- package/dist/util/error/index.js.map +0 -1
- package/dist/util/error/named.d.ts +0 -3
- package/dist/util/error/named.js +0 -11
- package/dist/util/error/named.js.map +0 -1
- package/dist/util/error/notfound.d.ts +0 -4
- package/dist/util/error/notfound.js.map +0 -1
- package/dist/util/error/notimplemented.d.ts +0 -4
- package/dist/util/error/notimplemented.js.map +0 -1
- package/dist/util/error/parsererror.d.ts +0 -8
- package/dist/util/error/parsererror.js +0 -20
- package/dist/util/error/parsererror.js.map +0 -1
- package/dist/util/error/unauthorizederror.d.ts +0 -4
- package/dist/util/error/unauthorizederror.js.map +0 -1
- package/dist/util/index.js.map +0 -1
- package/dist/util/loader/index.d.ts +0 -4
- package/dist/util/loader/index.js.map +0 -1
- package/dist/util/logger/common.js.map +0 -1
- package/dist/util/logger/factory.js.map +0 -1
- package/dist/util/logger/formatter.js.map +0 -1
- package/dist/util/logger/index.js.map +0 -1
- package/dist/util/logger/logger.js.map +0 -1
- package/dist/util/logger/transport.js.map +0 -1
- package/dist/util/parser/common.js.map +0 -1
- package/dist/util/parser/get.js.map +0 -1
- package/dist/util/parser/index.js.map +0 -1
- package/dist/util/parser/parser.js.map +0 -1
- package/dist/util/parser/types/any.js.map +0 -1
- package/dist/util/parser/types/array.js.map +0 -1
- package/dist/util/parser/types/boolean.js.map +0 -1
- package/dist/util/parser/types/email.js.map +0 -1
- package/dist/util/parser/types/enum.js.map +0 -1
- package/dist/util/parser/types/index.js.map +0 -1
- package/dist/util/parser/types/multiple.js.map +0 -1
- package/dist/util/parser/types/nested.js.map +0 -1
- package/dist/util/parser/types/number.js.map +0 -1
- package/dist/util/parser/types/object.js.map +0 -1
- package/dist/util/parser/types/regex.js.map +0 -1
- package/dist/util/parser/types/string.js.map +0 -1
- package/dist/util/parser/types/url.js.map +0 -1
- package/dist/util/request/common.d.ts +0 -57
- package/dist/util/request/common.js +0 -146
- package/dist/util/request/common.js.map +0 -1
- package/dist/util/request/index.d.ts +0 -4
- package/dist/util/request/index.js +0 -231
- package/dist/util/request/index.js.map +0 -1
- package/dist/util/session/common.d.ts +0 -15
- package/dist/util/session/common.js.map +0 -1
- package/dist/util/session/index.js.map +0 -1
- package/dist/util/session/service.js.map +0 -1
- package/src/handler/api-router-utils.ts +0 -187
- package/src/handler/api-router.ts +0 -86
- package/src/handler/app.ts +0 -133
- package/src/handler/common/context.ts +0 -156
- package/src/handler/common/helpers.ts +0 -11
- package/src/handler/common/index.ts +0 -2
- package/src/handler/common/router-utils.ts +0 -232
- package/src/handler/common/router.ts +0 -98
- package/src/handler/common/tokenize-match.ts +0 -152
- package/src/handler/index.ts +0 -5
- package/src/handler/middleware/buffer.ts +0 -79
- package/src/handler/middleware/cookie.ts +0 -19
- package/src/handler/middleware/cors.ts +0 -98
- package/src/handler/middleware/form.ts +0 -59
- package/src/handler/middleware/group.ts +0 -70
- package/src/handler/middleware/index.ts +0 -48
- package/src/handler/middleware/json.ts +0 -56
- package/src/handler/middleware/logger.ts +0 -18
- package/src/handler/middleware/parse.ts +0 -93
- package/src/handler/middleware/session.ts +0 -155
- package/src/handler/middleware/tag.ts +0 -15
- package/src/handler/middleware/text.ts +0 -47
- package/src/handler/proxy.ts +0 -39
- package/src/index.ts +0 -2
- package/src/util/cli/index.ts +0 -81
- package/src/util/config/browser.ts +0 -5
- package/src/util/config/config.ts +0 -93
- package/src/util/config/featuretoggle.ts +0 -6
- package/src/util/config/index.ts +0 -3
- package/src/util/error/configfilenotfound.ts +0 -8
- package/src/util/error/forbiddenerror.ts +0 -8
- package/src/util/error/index.ts +0 -6
- package/src/util/error/named.ts +0 -3
- package/src/util/error/notfound.ts +0 -8
- package/src/util/error/notimplemented.ts +0 -8
- package/src/util/error/parsererror.ts +0 -15
- package/src/util/error/unauthorizederror.ts +0 -8
- package/src/util/index.ts +0 -8
- package/src/util/loader/index.ts +0 -61
- package/src/util/logger/common.ts +0 -6
- package/src/util/logger/factory.ts +0 -45
- package/src/util/logger/formatter.ts +0 -9
- package/src/util/logger/index.ts +0 -6
- package/src/util/logger/logger.ts +0 -114
- package/src/util/logger/transport.ts +0 -69
- package/src/util/parser/common.ts +0 -81
- package/src/util/parser/get.ts +0 -40
- package/src/util/parser/index.ts +0 -3
- package/src/util/parser/parser.ts +0 -235
- package/src/util/parser/types/any.ts +0 -8
- package/src/util/parser/types/array.ts +0 -73
- package/src/util/parser/types/boolean.ts +0 -13
- package/src/util/parser/types/email.ts +0 -26
- package/src/util/parser/types/enum.ts +0 -29
- package/src/util/parser/types/index.ts +0 -12
- package/src/util/parser/types/multiple.ts +0 -36
- package/src/util/parser/types/nested.ts +0 -19
- package/src/util/parser/types/number.ts +0 -34
- package/src/util/parser/types/object.ts +0 -10
- package/src/util/parser/types/regex.ts +0 -17
- package/src/util/parser/types/string.ts +0 -23
- package/src/util/parser/types/url.ts +0 -35
- package/src/util/request/common.ts +0 -193
- package/src/util/request/index.ts +0 -230
- package/src/util/session/common.ts +0 -20
- package/src/util/session/index.ts +0 -2
- package/src/util/session/service.ts +0 -154
- package/tsconfig.json +0 -32
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { parse as queryParse } from "querystring";
|
|
2
|
-
import { Context, Handler } from "../common";
|
|
3
|
-
import { BadRequestError } from "../../util/error";
|
|
4
|
-
import { checkEnvVariables } from "../../util/config";
|
|
5
|
-
import { ReadBuffer, DEFAULT_READ_BUFFER_LIMIT } from "./buffer";
|
|
6
|
-
|
|
7
|
-
const DEFAULT_FORM_MAX_KEYS = undefined;
|
|
8
|
-
|
|
9
|
-
export const URLEncodedParser = (options?: {
|
|
10
|
-
limit?: number;
|
|
11
|
-
maxKeys?: number;
|
|
12
|
-
type?: string;
|
|
13
|
-
}): Handler<void> => {
|
|
14
|
-
let maxKeys: undefined | number = DEFAULT_FORM_MAX_KEYS;
|
|
15
|
-
let limit = DEFAULT_READ_BUFFER_LIMIT;
|
|
16
|
-
let type = "application/x-www-form-urlencoded";
|
|
17
|
-
if (options) {
|
|
18
|
-
limit = options.limit !== undefined ? options.limit : limit;
|
|
19
|
-
type = options.type !== undefined ? options.type : type;
|
|
20
|
-
maxKeys = options.maxKeys !== undefined ? options.maxKeys : maxKeys;
|
|
21
|
-
} else {
|
|
22
|
-
const [limitS, typeS, maxKeysS] =
|
|
23
|
-
checkEnvVariables(["BODY_PARSER_URL_ENCODED_LIMIT", "BODY_PARSER_URL_ENCODED_TYPE", "BODY_PARSER_URL_ENCODED_MAX_KEYS"], [String(DEFAULT_READ_BUFFER_LIMIT), "application/x-www-form-urlencoded", String(DEFAULT_FORM_MAX_KEYS)]);
|
|
24
|
-
limit = parseInt(limitS, 10);
|
|
25
|
-
type = typeS;
|
|
26
|
-
maxKeys = maxKeysS !== "undefined" ? parseInt(maxKeysS, 10) : DEFAULT_FORM_MAX_KEYS;
|
|
27
|
-
}
|
|
28
|
-
const readBuffer = ReadBuffer({
|
|
29
|
-
limit
|
|
30
|
-
});
|
|
31
|
-
return async (ctx: Context): Promise<void> => {
|
|
32
|
-
try {
|
|
33
|
-
const isType = ctx.body === undefined && ctx.headers["content-type"] ? ctx.headers["content-type"].toLocaleLowerCase().indexOf(type.toLocaleLowerCase()) !== -1 : false;
|
|
34
|
-
if (isType && !ctx.buffer) {
|
|
35
|
-
await readBuffer(ctx);
|
|
36
|
-
}
|
|
37
|
-
if (isType && ctx.buffer && ctx.buffer.length <= limit) {
|
|
38
|
-
const string = ctx.buffer.toString();
|
|
39
|
-
if (string) {
|
|
40
|
-
ctx.body = {
|
|
41
|
-
...queryParse(string, undefined, undefined, {
|
|
42
|
-
maxKeys
|
|
43
|
-
})
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
} else if (isType && ctx.buffer && ctx.buffer.length > limit) {
|
|
47
|
-
ctx.logger.error(`ctx.buffer.length ${ctx.buffer.length} > ${limit}. To accept this body set BODY_PARSER_URL_ENCODED_LIMIT to a higher value.`);
|
|
48
|
-
throw new BadRequestError(`buffer.length ${ctx.buffer.length} > ${limit}`);
|
|
49
|
-
}
|
|
50
|
-
} catch (e: any) {
|
|
51
|
-
if (e && e.name === "BadRequestError") {
|
|
52
|
-
throw e;
|
|
53
|
-
} else {
|
|
54
|
-
ctx.logger.error(e);
|
|
55
|
-
throw new BadRequestError();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { GroupPolicy, Session } from "../../util/session";
|
|
2
|
-
import { ForbiddenError, UnAuthorizedError, ParseOptionsError } from "../../util/error";
|
|
3
|
-
import { Context, Handler } from "../common";
|
|
4
|
-
|
|
5
|
-
const policyCheck = (session: Session, options: GroupPolicy): boolean => {
|
|
6
|
-
switch (options.groupPolicy) {
|
|
7
|
-
case "at_least_one":
|
|
8
|
-
for (const group of options.groups) {
|
|
9
|
-
if (group instanceof Array) {
|
|
10
|
-
let ret = true;
|
|
11
|
-
for (const g of group) {
|
|
12
|
-
if (session.groups.indexOf(g) === -1) {
|
|
13
|
-
ret = false;
|
|
14
|
-
break;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
if (ret) {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
} else {
|
|
21
|
-
if (session.groups.indexOf(group) !== -1) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return false;
|
|
27
|
-
case "all":
|
|
28
|
-
for (const group of options.groups) {
|
|
29
|
-
if (group instanceof Array) {
|
|
30
|
-
for (const g of group) {
|
|
31
|
-
if (session.groups.indexOf(g) === -1) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
if (session.groups.indexOf(group) === -1) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return true;
|
|
42
|
-
default:
|
|
43
|
-
throw new ParseOptionsError(`policy [${options.groupPolicy}] not implemented!!`);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const GroupPolicyHandler = (options: GroupPolicy): Handler<void> => {
|
|
48
|
-
return async (ctx: Context): Promise<void> => {
|
|
49
|
-
try {
|
|
50
|
-
if (!ctx.session) {
|
|
51
|
-
throw new UnAuthorizedError(`No Session!`);
|
|
52
|
-
} else {
|
|
53
|
-
const result = await policyCheck(ctx.session, options);
|
|
54
|
-
if (result) {
|
|
55
|
-
ctx.logger.debug("groups validated!");
|
|
56
|
-
} else {
|
|
57
|
-
ctx.logger.error("%sgroups fail to validate!", ctx && ctx.session && ctx.session.groups ? `[${ctx.session.groups.join(",")}] ` : "");
|
|
58
|
-
throw new ForbiddenError(`Invalid session. You are not permitted to do this!`);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
} catch (e: any) {
|
|
62
|
-
//(logger as Logger).warn(`request[${req.uuid}] message[${e.message}] stack[${e.stack}]`);
|
|
63
|
-
if (e.name && e.name !== "Error") {
|
|
64
|
-
throw e;
|
|
65
|
-
} else {
|
|
66
|
-
throw new UnAuthorizedError(`Invalid session. You are not permitted to do this!`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { isFeatureEnabled } from "../../util/config";
|
|
2
|
-
import { Handler } from "../common";
|
|
3
|
-
|
|
4
|
-
import { LoggerHandler } from "./logger";
|
|
5
|
-
import { TagResponse } from "./tag";
|
|
6
|
-
import { CORS } from "./cors";
|
|
7
|
-
import { CookieParser } from "./cookie";
|
|
8
|
-
import { JSONParser } from "./json";
|
|
9
|
-
import { TextParser } from "./text";
|
|
10
|
-
import { URLEncodedParser } from "./form";
|
|
11
|
-
|
|
12
|
-
export * from "./logger";
|
|
13
|
-
export * from "./json";
|
|
14
|
-
export * from "./text";
|
|
15
|
-
export * from "./form";
|
|
16
|
-
export * from "./cors";
|
|
17
|
-
export * from "./session";
|
|
18
|
-
export * from "./group";
|
|
19
|
-
export * from "./parse";
|
|
20
|
-
export * from "./tag";
|
|
21
|
-
export * from "./cookie";
|
|
22
|
-
export { ReadBuffer } from "./buffer";
|
|
23
|
-
|
|
24
|
-
export const middleware = (): Handler[] => {
|
|
25
|
-
const ret: Handler[] = [];
|
|
26
|
-
if (isFeatureEnabled("REQUEST_LOGGER", true)) {
|
|
27
|
-
ret.push(LoggerHandler());
|
|
28
|
-
}
|
|
29
|
-
if (isFeatureEnabled("TAG_RESPONSE", false)) {
|
|
30
|
-
ret.push(TagResponse());
|
|
31
|
-
}
|
|
32
|
-
if (isFeatureEnabled("CORS", false)) {
|
|
33
|
-
ret.push(CORS());
|
|
34
|
-
}
|
|
35
|
-
if (isFeatureEnabled("COOKIE_PARSER", true)) {
|
|
36
|
-
ret.push(CookieParser());
|
|
37
|
-
}
|
|
38
|
-
if (isFeatureEnabled("JSON_PARSER", true)) {
|
|
39
|
-
ret.push(JSONParser());
|
|
40
|
-
}
|
|
41
|
-
if (isFeatureEnabled("URL_ENCODED_PARSER", true)) {
|
|
42
|
-
ret.push(URLEncodedParser());
|
|
43
|
-
}
|
|
44
|
-
if (isFeatureEnabled("TEXT_PARSER", true)) {
|
|
45
|
-
ret.push(TextParser());
|
|
46
|
-
}
|
|
47
|
-
return ret;
|
|
48
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common";
|
|
2
|
-
import { BadRequestError } from "../../util/error";
|
|
3
|
-
import { checkEnvVariables } from "../../util/config";
|
|
4
|
-
import { DEFAULT_READ_BUFFER_LIMIT, ReadBuffer } from "./buffer";
|
|
5
|
-
|
|
6
|
-
export const JSONParser = (options?: {
|
|
7
|
-
limit?: number;
|
|
8
|
-
strict?: boolean;
|
|
9
|
-
type?: string;
|
|
10
|
-
}): Handler<void> => {
|
|
11
|
-
let strict = true;
|
|
12
|
-
let limit = DEFAULT_READ_BUFFER_LIMIT;
|
|
13
|
-
let type = "application/json";
|
|
14
|
-
if (options) {
|
|
15
|
-
strict = options.strict !== undefined ? options.strict : strict;
|
|
16
|
-
limit = options.limit !== undefined ? options.limit : limit;
|
|
17
|
-
type = options.type !== undefined ? options.type : type;
|
|
18
|
-
} else {
|
|
19
|
-
const [limitS, strictS, typeS] =
|
|
20
|
-
checkEnvVariables(["BODY_PARSER_LIMIT", "BODY_PARSER_STRICT", "BODY_PARSER_TYPE"], [String(DEFAULT_READ_BUFFER_LIMIT), "true", "application/json"])
|
|
21
|
-
strict = strictS === "true";
|
|
22
|
-
limit = parseInt(limitS, 10);
|
|
23
|
-
type = typeS;
|
|
24
|
-
}
|
|
25
|
-
const readBuffer = ReadBuffer({
|
|
26
|
-
limit
|
|
27
|
-
});
|
|
28
|
-
return async (ctx: Context): Promise<void> => {
|
|
29
|
-
try {
|
|
30
|
-
const isType = ctx.body === undefined && ctx.headers["content-type"] ? ctx.headers["content-type"].toLocaleLowerCase().indexOf(type.toLocaleLowerCase()) !== -1 : false;
|
|
31
|
-
if (isType && !ctx.buffer) {
|
|
32
|
-
await readBuffer(ctx);
|
|
33
|
-
}
|
|
34
|
-
if (isType && ctx.buffer && ctx.buffer.length <= limit) {
|
|
35
|
-
const string = ctx.buffer.toString();
|
|
36
|
-
if (string) {
|
|
37
|
-
const parsed = JSON.parse(string);
|
|
38
|
-
if (parsed instanceof Array && strict) {
|
|
39
|
-
throw new BadRequestError(`body cannot be an array`);
|
|
40
|
-
}
|
|
41
|
-
ctx.body = parsed;
|
|
42
|
-
}
|
|
43
|
-
} else if (isType && ctx.buffer && ctx.buffer.length > limit) {
|
|
44
|
-
ctx.logger.error(`ctx.buffer.length ${ctx.buffer.length} > ${limit}. To accept this body set BODY_PARSER_LIMIT to a higher value.`);
|
|
45
|
-
throw new BadRequestError(`buffer.length ${ctx.buffer.length} > ${limit}`);
|
|
46
|
-
}
|
|
47
|
-
} catch (e: any) {
|
|
48
|
-
if (e && e.name === "BadRequestError") {
|
|
49
|
-
throw e;
|
|
50
|
-
} else {
|
|
51
|
-
ctx.logger.error(e);
|
|
52
|
-
throw new BadRequestError();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common";
|
|
2
|
-
|
|
3
|
-
export const LoggerHandler = (options?: {
|
|
4
|
-
formatter: (ctx: Context) => string;
|
|
5
|
-
}): Handler<void> =>
|
|
6
|
-
async (ctx: Context): Promise<void> => {
|
|
7
|
-
ctx.res.on("close", () => {
|
|
8
|
-
const took = Date.now() - ctx.startMS;
|
|
9
|
-
const entry = options ? options.formatter(ctx) : `status[${ctx.res.statusCode}] content-length[${(ctx.res as any)._contentLength}] [${took}]ms`;
|
|
10
|
-
if (ctx.res.statusCode < 400) {
|
|
11
|
-
ctx.logger.info(entry);
|
|
12
|
-
} else if (ctx.res.statusCode >= 400 && ctx.res.statusCode < 500) {
|
|
13
|
-
ctx.logger.warn(entry);
|
|
14
|
-
} else {
|
|
15
|
-
ctx.logger.error(entry);
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { parse, Parser } from "../../util/parser";
|
|
2
|
-
import { ParseOptions } from "../../util/parser/common";
|
|
3
|
-
import { Context, Handler } from "../common";
|
|
4
|
-
|
|
5
|
-
export interface ParseRequestOptions {
|
|
6
|
-
headers?: ParseOptions | ParseOptions[];
|
|
7
|
-
query?: ParseOptions | false | ParseOptions[];
|
|
8
|
-
params?: ParseOptions | false | ParseOptions[];
|
|
9
|
-
body?: ParseOptions | false | ParseOptions[];
|
|
10
|
-
parser?: Parser;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const NO_OPTIONS: ParseOptions[] = [{
|
|
14
|
-
options: [],
|
|
15
|
-
mode: "no_extra"
|
|
16
|
-
}];
|
|
17
|
-
|
|
18
|
-
const ADD_EXTRA: ParseOptions[] = [{
|
|
19
|
-
options: [],
|
|
20
|
-
mode: "add_extra"
|
|
21
|
-
}];
|
|
22
|
-
|
|
23
|
-
const normalizeParseOptions = (option?: ParseOptions | false | ParseOptions[]): ParseOptions[] =>
|
|
24
|
-
option ? (option instanceof Array ? option : [option]) : (option === false ? NO_OPTIONS : ADD_EXTRA);
|
|
25
|
-
|
|
26
|
-
const parseRequestPart = (part: "query" | "body" | "params" | "headers", ctx: Context, option: ParseOptions[], parser?: Parser): void => {
|
|
27
|
-
const value = ctx[part] === undefined ? {} : ctx[part];
|
|
28
|
-
for (let i = 0; i < option.length; i++) {
|
|
29
|
-
const o = option[i];
|
|
30
|
-
try {
|
|
31
|
-
const parsed = (parser ? parser : { parse }).parse(`${part}`, value as any, o.options, o.mode, o.ignoreUndefined) as any;
|
|
32
|
-
ctx[part] = parsed;
|
|
33
|
-
ctx.logger.debug("req.%s parsed to [%o]", part, ctx[part]);
|
|
34
|
-
return;
|
|
35
|
-
} catch (e: any) {
|
|
36
|
-
if (i === option.length - 1) {
|
|
37
|
-
throw e;
|
|
38
|
-
} else {
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
export const ParseRequest = (options: ParseRequestOptions): Handler<void> => {
|
|
47
|
-
const query = normalizeParseOptions(options.query);
|
|
48
|
-
const params = normalizeParseOptions(options.params);
|
|
49
|
-
const body = normalizeParseOptions(options.body);
|
|
50
|
-
const headers = normalizeParseOptions(options.headers);
|
|
51
|
-
|
|
52
|
-
return async (ctx: Context): Promise<void> => {
|
|
53
|
-
try {
|
|
54
|
-
try {
|
|
55
|
-
if (options.headers !== undefined) {
|
|
56
|
-
parseRequestPart("headers", ctx, headers, options.parser);
|
|
57
|
-
}
|
|
58
|
-
} catch (e) {
|
|
59
|
-
ctx.logger.error("error parsing headers %o", ctx.headers);
|
|
60
|
-
throw e;
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
if (options.query !== undefined) {
|
|
64
|
-
parseRequestPart("query", ctx, query, options.parser);
|
|
65
|
-
}
|
|
66
|
-
} catch (e) {
|
|
67
|
-
ctx.logger.error("error parsing query %o", ctx.query);
|
|
68
|
-
throw e;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
if (options.params !== undefined) {
|
|
73
|
-
parseRequestPart("params", ctx, params, options.parser);
|
|
74
|
-
}
|
|
75
|
-
} catch (e) {
|
|
76
|
-
ctx.logger.error("error parsing params %o", ctx.params);
|
|
77
|
-
throw e;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
try {
|
|
81
|
-
if (options.body !== undefined) {
|
|
82
|
-
parseRequestPart("body", ctx, body, options.parser);
|
|
83
|
-
}
|
|
84
|
-
} catch (e: any) {
|
|
85
|
-
ctx.logger.error("error parsing body %o", ctx.body);
|
|
86
|
-
throw e;
|
|
87
|
-
}
|
|
88
|
-
} catch (e: any) {
|
|
89
|
-
ctx.logger.warn("error parsing request: %s", e.message);
|
|
90
|
-
throw e;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
};
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { parse } from "../../util/parser";
|
|
2
|
-
import { checkEnvVariables } from "../../util/config";
|
|
3
|
-
import { Session } from "../../util/session";
|
|
4
|
-
import { Context, Handler } from "../common";
|
|
5
|
-
import { ForbiddenError, UnAuthorizedError } from "../../util/error";
|
|
6
|
-
|
|
7
|
-
const DEFAULT_TOKEN_LOCATION = "header";
|
|
8
|
-
const DEFAULT_TOKEN_HEADER = "Authorization";
|
|
9
|
-
const DEFAULT_TOKEN_QUERY = "token";
|
|
10
|
-
const DEFAULT_TOKEN_COOKIE = "Authorization";
|
|
11
|
-
const DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY = "true";
|
|
12
|
-
const DEFAULT_TOKEN_SET_COOKIE_SECURE = "true";
|
|
13
|
-
const DEFAULT_TOKEN_SET_COOKIE_PATH = "/";
|
|
14
|
-
const DEFAULT_TOKEN_SET_COOKIE_SAME_SITE = "strict";
|
|
15
|
-
|
|
16
|
-
export interface SessionHandlerOptions {
|
|
17
|
-
authService: {
|
|
18
|
-
verify(args: { token: string; ctx: Context }): Promise<Session | null>;
|
|
19
|
-
};
|
|
20
|
-
options?: {
|
|
21
|
-
tokenLocation: "header" | "query" | "cookie";
|
|
22
|
-
tokenLocationName: string | ((ctx: Context) => Promise<string>);
|
|
23
|
-
setCookieOptions?: {
|
|
24
|
-
httpOnly: boolean;
|
|
25
|
-
secure: boolean;
|
|
26
|
-
path: string | ((ctx: Context) => Promise<string>);
|
|
27
|
-
sameSite: "lax" | "strict" | "none";
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const SessionHandler = (config: SessionHandlerOptions): Handler<void> => {
|
|
33
|
-
if (!config.options) {
|
|
34
|
-
const tokenLocation = checkEnvVariables(["TOKEN_LOCATION"], [DEFAULT_TOKEN_LOCATION])[0];
|
|
35
|
-
config.options = {
|
|
36
|
-
tokenLocation: tokenLocation as any,
|
|
37
|
-
tokenLocationName: "",
|
|
38
|
-
setCookieOptions: {
|
|
39
|
-
httpOnly: true,
|
|
40
|
-
path: "/",
|
|
41
|
-
sameSite: "strict",
|
|
42
|
-
secure: true
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
switch (tokenLocation) {
|
|
46
|
-
case "header":
|
|
47
|
-
config.options.tokenLocationName = checkEnvVariables(["TOKEN_HEADER"], [DEFAULT_TOKEN_HEADER])[0];
|
|
48
|
-
break;
|
|
49
|
-
case "query":
|
|
50
|
-
config.options.tokenLocationName = checkEnvVariables(["TOKEN_QUERY"], [DEFAULT_TOKEN_QUERY])[0];
|
|
51
|
-
break;
|
|
52
|
-
case "cookie":
|
|
53
|
-
config.options.tokenLocationName = checkEnvVariables(["TOKEN_COOKIE"], [DEFAULT_TOKEN_COOKIE])[0];
|
|
54
|
-
const [httpOnlyS, secureS, path, sameSite] = checkEnvVariables(
|
|
55
|
-
["TOKEN_SET_COOKIE_HTTP_ONLY", "TOKEN_SET_COOKIE_SECURE", "TOKEN_SET_COOKIE_PATH", "TOKEN_SET_COOKIE_SAME_SITE"],
|
|
56
|
-
[DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY, DEFAULT_TOKEN_SET_COOKIE_SECURE, DEFAULT_TOKEN_SET_COOKIE_PATH, DEFAULT_TOKEN_SET_COOKIE_SAME_SITE]);
|
|
57
|
-
config.options.setCookieOptions = {
|
|
58
|
-
httpOnly: httpOnlyS === "true",
|
|
59
|
-
secure: secureS === "true",
|
|
60
|
-
path,
|
|
61
|
-
sameSite: sameSite as any
|
|
62
|
-
};
|
|
63
|
-
break;
|
|
64
|
-
default:
|
|
65
|
-
throw new Error(`TOKEN_LOCATION=${tokenLocation} not supported use (header or query)`);
|
|
66
|
-
}
|
|
67
|
-
} else {
|
|
68
|
-
config.options = parse("options", config.options as any, [
|
|
69
|
-
{ name: "tokenLocation", required: true, type: "enum", enumValues: ["header", "query", "cookie"] },
|
|
70
|
-
{ name: "tokenLocationName", required: true, type: "any" },
|
|
71
|
-
{
|
|
72
|
-
name: "setCookieOptions", required: false, type: "nested", defaultValue: {
|
|
73
|
-
httpOnly: DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY === "true",
|
|
74
|
-
secure: DEFAULT_TOKEN_SET_COOKIE_SECURE === "true",
|
|
75
|
-
path: DEFAULT_TOKEN_SET_COOKIE_PATH,
|
|
76
|
-
sameSite: DEFAULT_TOKEN_SET_COOKIE_SAME_SITE,
|
|
77
|
-
}, nestedOptions: {
|
|
78
|
-
mode: "no_extra",
|
|
79
|
-
options: [
|
|
80
|
-
{ name: "httpOnly", required: true, type: "boolean" },
|
|
81
|
-
{ name: "secure", required: true, type: "boolean" },
|
|
82
|
-
{ name: "path", required: true, type: "any" },
|
|
83
|
-
{ name: "sameSite", required: true, type: "enum", enumValues: ["lax", "strict", "none"] }
|
|
84
|
-
]
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
], "no_extra") as any;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (!config.authService) {
|
|
91
|
-
throw new Error("authService must be provided!");
|
|
92
|
-
}
|
|
93
|
-
if (!config.options || !config.options.setCookieOptions) {
|
|
94
|
-
throw new Error("config.options not populated!");
|
|
95
|
-
}
|
|
96
|
-
const tokenLocation = config.options.tokenLocation;
|
|
97
|
-
const tokenLocationName = config.options.tokenLocationName;
|
|
98
|
-
const setCookieOptions = config.options.setCookieOptions;
|
|
99
|
-
return async (ctx: Context): Promise<void> => {
|
|
100
|
-
try {
|
|
101
|
-
const tlN = typeof tokenLocationName === "string" ? tokenLocationName : await tokenLocationName(ctx);
|
|
102
|
-
let token = null;
|
|
103
|
-
switch (tokenLocation) {
|
|
104
|
-
case "header":
|
|
105
|
-
token = ctx.headers[(tlN).toLowerCase()] as string;
|
|
106
|
-
break;
|
|
107
|
-
case "query":
|
|
108
|
-
token = ctx.query[tlN] as string;
|
|
109
|
-
break;
|
|
110
|
-
case "cookie":
|
|
111
|
-
token = ctx.cookies[tlN] ? ctx.cookies[tlN] : undefined;
|
|
112
|
-
break;
|
|
113
|
-
default:
|
|
114
|
-
throw new Error(`TOKEN_LOCATION=${tokenLocation} not supported use (header, query or cookie)`);
|
|
115
|
-
}
|
|
116
|
-
if (!token) {
|
|
117
|
-
ctx.logger.warn("No token provided!");
|
|
118
|
-
throw new ForbiddenError("NO TOKEN");
|
|
119
|
-
} else {
|
|
120
|
-
const session = await config.authService.verify({ token, ctx });
|
|
121
|
-
if (!session) {
|
|
122
|
-
ctx.logger.warn("fail to authenticate token [%s]!", token);
|
|
123
|
-
throw new UnAuthorizedError();
|
|
124
|
-
} else {
|
|
125
|
-
if (tokenLocation === "cookie" && session.token !== token) {
|
|
126
|
-
const newTokenCookie = String(session.token ? session.token : token);
|
|
127
|
-
const cookiePath = typeof setCookieOptions.path === "string" ? setCookieOptions.path : await setCookieOptions.path(ctx);
|
|
128
|
-
ctx.setCookie(tlN, newTokenCookie, session.expires instanceof Date ? {
|
|
129
|
-
httpOnly: setCookieOptions.httpOnly,
|
|
130
|
-
secure: setCookieOptions.secure,
|
|
131
|
-
path: cookiePath,
|
|
132
|
-
sameSite: setCookieOptions.sameSite,
|
|
133
|
-
expires: session.expires
|
|
134
|
-
} : {
|
|
135
|
-
httpOnly: setCookieOptions.httpOnly,
|
|
136
|
-
path: cookiePath,
|
|
137
|
-
secure: setCookieOptions.secure,
|
|
138
|
-
sameSite: setCookieOptions.sameSite
|
|
139
|
-
})
|
|
140
|
-
}
|
|
141
|
-
ctx.session = session;
|
|
142
|
-
ctx.logger.debug("authenticated!");
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
} catch (e: any) {
|
|
146
|
-
if (e.message && e.message === "NO TOKEN") {
|
|
147
|
-
throw e;
|
|
148
|
-
} else {
|
|
149
|
-
throw new UnAuthorizedError();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common";
|
|
2
|
-
|
|
3
|
-
export const TagResponse = (options?: {
|
|
4
|
-
uuid?: boolean;
|
|
5
|
-
}): Handler<void> =>
|
|
6
|
-
async (ctx: Context): Promise<void> => {
|
|
7
|
-
if (options && options.uuid) {
|
|
8
|
-
ctx.setHeader("uuid", ctx.uuid);
|
|
9
|
-
}
|
|
10
|
-
const requestUUID = ctx.headers['request-uuid'];
|
|
11
|
-
if (requestUUID) {
|
|
12
|
-
ctx.setHeader("request-uuid", requestUUID);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common";
|
|
2
|
-
import { BadRequestError } from "../../util/error";
|
|
3
|
-
import { checkEnvVariables } from "../../util/config";
|
|
4
|
-
import { DEFAULT_READ_BUFFER_LIMIT } from "./buffer";
|
|
5
|
-
import { ReadBuffer } from "./buffer";
|
|
6
|
-
|
|
7
|
-
export const TextParser = (options?: {
|
|
8
|
-
limit?: number;
|
|
9
|
-
type?: string;
|
|
10
|
-
}): Handler<void> => {
|
|
11
|
-
let limit = DEFAULT_READ_BUFFER_LIMIT;
|
|
12
|
-
let type = "plain/text";
|
|
13
|
-
if (options) {
|
|
14
|
-
limit = options.limit !== undefined ? options.limit : limit;
|
|
15
|
-
type = options.type !== undefined ? options.type : type;
|
|
16
|
-
} else {
|
|
17
|
-
const [limitS, typeS] =
|
|
18
|
-
checkEnvVariables(["BODY_TEXT_PARSER_LIMIT", "BODY_TEXT_PARSER_TYPE"], [String(DEFAULT_READ_BUFFER_LIMIT), "plain/text"]);
|
|
19
|
-
limit = parseInt(limitS, 10);
|
|
20
|
-
type = typeS;
|
|
21
|
-
}
|
|
22
|
-
const readBuffer = ReadBuffer({
|
|
23
|
-
limit
|
|
24
|
-
});
|
|
25
|
-
return async (ctx: Context): Promise<void> => {
|
|
26
|
-
try {
|
|
27
|
-
const isType = ctx.body === undefined && ctx.headers["content-type"] ? ctx.headers["content-type"].toLocaleLowerCase().indexOf(type.toLocaleLowerCase()) !== -1 : false;
|
|
28
|
-
if (isType && !ctx.buffer) {
|
|
29
|
-
await readBuffer(ctx);
|
|
30
|
-
}
|
|
31
|
-
if (isType && ctx.buffer && ctx.buffer.length <= limit) {
|
|
32
|
-
const string = ctx.buffer.toString();
|
|
33
|
-
ctx.body = string;
|
|
34
|
-
} else if (isType && ctx.buffer && ctx.buffer.length > limit) {
|
|
35
|
-
ctx.logger.error(`ctx.buffer.length ${ctx.buffer.length} > ${limit}. To accept this body set BODY_TEXT_PARSER_LIMIT to a higher value.`);
|
|
36
|
-
throw new BadRequestError(`buffer.length ${ctx.buffer.length} > ${limit}`);
|
|
37
|
-
}
|
|
38
|
-
} catch (e: any) {
|
|
39
|
-
if (e && e.name === "BadRequestError") {
|
|
40
|
-
throw e;
|
|
41
|
-
} else {
|
|
42
|
-
ctx.logger.error(e);
|
|
43
|
-
throw new BadRequestError();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
};
|
package/src/handler/proxy.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { request, RequestOptions, RequestResponse } from "../util/request";
|
|
2
|
-
import { Context, Handler } from "./common";
|
|
3
|
-
|
|
4
|
-
export interface ProxyServiceInterface {
|
|
5
|
-
resolve(ctx: Context): Promise<RequestOptions>;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const ProxyHandler = (resolver: ProxyServiceInterface): Handler<RequestResponse> =>
|
|
9
|
-
async (ctx): Promise<RequestResponse> => {
|
|
10
|
-
const requestConfig = await resolver.resolve(ctx);
|
|
11
|
-
try {
|
|
12
|
-
ctx.logger.debug("proxy resolveRequest to [%o]", requestConfig);
|
|
13
|
-
const response = await request(requestConfig);
|
|
14
|
-
ctx.logger.trace("response %o", response);
|
|
15
|
-
return response;
|
|
16
|
-
} catch (e: any) {
|
|
17
|
-
ctx.logger.error("Error connecting to endpoint in [%s] [%s]", requestConfig.url, e.message);
|
|
18
|
-
if (e.status && e.headers && e.data) {
|
|
19
|
-
return e;
|
|
20
|
-
} else {
|
|
21
|
-
throw e;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const ProxyResponseHandler = (): Handler<void> =>
|
|
27
|
-
async (ctx: Context): Promise<void> => {
|
|
28
|
-
if (!ctx.results || ctx.results.length === 0) {
|
|
29
|
-
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
30
|
-
} else {
|
|
31
|
-
const lastResult = ctx.results[ctx.results.length - 1];
|
|
32
|
-
ctx.logger.debug("response %o", lastResult);
|
|
33
|
-
await ctx.end({
|
|
34
|
-
headers: lastResult.headers,
|
|
35
|
-
body: lastResult.data,
|
|
36
|
-
status: lastResult.status
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
}
|
package/src/index.ts
DELETED