@miqro/core 4.0.1 → 5.0.2
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/build/app.d.ts +15 -11
- package/build/app.js +22 -20
- package/build/app.js.map +1 -1
- package/build/common.d.ts +106 -0
- package/build/common.js +477 -0
- package/build/common.js.map +1 -0
- package/build/lib.d.ts +7 -38
- package/build/lib.js +6 -33
- package/build/lib.js.map +1 -1
- package/build/middleware/body-parser.d.ts +78 -0
- package/build/middleware/body-parser.js +552 -0
- package/build/middleware/body-parser.js.map +1 -0
- package/build/middleware/router.d.ts +114 -0
- package/build/middleware/router.js +704 -0
- package/build/middleware/router.js.map +1 -0
- package/build/middleware/session.d.ts +27 -3
- package/build/middleware/session.js +238 -18
- package/build/middleware/session.js.map +1 -1
- package/build/types.d.ts +309 -0
- package/build/types.js +455 -0
- package/build/types.js.map +1 -0
- package/build/{websocket/cluster.d.ts → websocket.d.ts} +13 -2
- package/build/websocket.js +316 -0
- package/build/websocket.js.map +1 -0
- package/package.json +7 -7
- package/src/app.ts +36 -32
- package/src/common.ts +528 -0
- package/src/lib.ts +63 -101
- package/src/middleware/body-parser.ts +597 -0
- package/src/middleware/router.ts +840 -0
- package/src/middleware/session.ts +249 -19
- package/src/types.ts +785 -0
- package/src/websocket.ts +356 -0
- package/build/api-router.d.ts +0 -34
- package/build/api-router.js +0 -48
- package/build/api-router.js.map +0 -1
- package/build/app-utils.d.ts +0 -13
- package/build/app-utils.js +0 -78
- package/build/app-utils.js.map +0 -1
- package/build/common/api-router-types.d.ts +0 -28
- package/build/common/api-router-types.js +0 -24
- package/build/common/api-router-types.js.map +0 -1
- package/build/common/api-router-utils.d.ts +0 -3
- package/build/common/api-router-utils.js +0 -118
- package/build/common/api-router-utils.js.map +0 -1
- package/build/common/config/config.d.ts +0 -15
- package/build/common/config/config.js +0 -38
- package/build/common/config/config.js.map +0 -1
- package/build/common/config/featuretoggle.d.ts +0 -1
- package/build/common/config/featuretoggle.js +0 -6
- package/build/common/config/featuretoggle.js.map +0 -1
- package/build/common/config/loader.d.ts +0 -13
- package/build/common/config/loader.js +0 -64
- package/build/common/config/loader.js.map +0 -1
- package/build/common/context.d.ts +0 -57
- package/build/common/context.js +0 -137
- package/build/common/context.js.map +0 -1
- package/build/common/error/badrequesterror.d.ts +0 -3
- package/build/common/error/badrequesterror.js +0 -7
- package/build/common/error/badrequesterror.js.map +0 -1
- package/build/common/error/configfilenotfound.d.ts +0 -3
- package/build/common/error/configfilenotfound.js +0 -7
- package/build/common/error/configfilenotfound.js.map +0 -1
- package/build/common/error/forbiddenerror.d.ts +0 -3
- package/build/common/error/forbiddenerror.js +0 -7
- package/build/common/error/forbiddenerror.js.map +0 -1
- package/build/common/error/notfound.d.ts +0 -3
- package/build/common/error/notfound.js +0 -7
- package/build/common/error/notfound.js.map +0 -1
- package/build/common/error/notimplemented.d.ts +0 -3
- package/build/common/error/notimplemented.js +0 -7
- package/build/common/error/notimplemented.js.map +0 -1
- package/build/common/error/unauthorizederror.d.ts +0 -3
- package/build/common/error/unauthorizederror.js +0 -7
- package/build/common/error/unauthorizederror.js.map +0 -1
- package/build/common/logger/constants.d.ts +0 -14
- package/build/common/logger/constants.js +0 -22
- package/build/common/logger/constants.js.map +0 -1
- package/build/common/logger/factory.d.ts +0 -16
- package/build/common/logger/factory.js +0 -47
- package/build/common/logger/factory.js.map +0 -1
- package/build/common/logger/interfaces.d.ts +0 -18
- package/build/common/logger/interfaces.js +0 -2
- package/build/common/logger/interfaces.js.map +0 -1
- package/build/common/logger/logger.d.ts +0 -35
- package/build/common/logger/logger.js +0 -95
- package/build/common/logger/logger.js.map +0 -1
- package/build/common/logger/transport/console.d.ts +0 -5
- package/build/common/logger/transport/console.js +0 -37
- package/build/common/logger/transport/console.js.map +0 -1
- package/build/common/logger/transport/file.d.ts +0 -2
- package/build/common/logger/transport/file.js +0 -25
- package/build/common/logger/transport/file.js.map +0 -1
- package/build/common/parser.d.ts +0 -7
- package/build/common/parser.js +0 -44
- package/build/common/parser.js.map +0 -1
- package/build/common/router-default-logger-factory.d.ts +0 -4
- package/build/common/router-default-logger-factory.js +0 -38
- package/build/common/router-default-logger-factory.js.map +0 -1
- package/build/common/router-handler.d.ts +0 -20
- package/build/common/router-handler.js +0 -154
- package/build/common/router-handler.js.map +0 -1
- package/build/common/router-types.d.ts +0 -63
- package/build/common/router-types.js +0 -86
- package/build/common/router-types.js.map +0 -1
- package/build/common/router-utils.d.ts +0 -3
- package/build/common/router-utils.js +0 -46
- package/build/common/router-utils.js.map +0 -1
- package/build/common/session/common.d.ts +0 -37
- package/build/common/session/common.js +0 -2
- package/build/common/session/common.js.map +0 -1
- package/build/common/session/service.d.ts +0 -26
- package/build/common/session/service.js +0 -154
- package/build/common/session/service.js.map +0 -1
- package/build/common/tokenize-match.d.ts +0 -19
- package/build/common/tokenize-match.js +0 -73
- package/build/common/tokenize-match.js.map +0 -1
- package/build/common/url-utils.d.ts +0 -6
- package/build/common/url-utils.js +0 -48
- package/build/common/url-utils.js.map +0 -1
- package/build/middleware/buffer.d.ts +0 -6
- package/build/middleware/buffer.js +0 -78
- package/build/middleware/buffer.js.map +0 -1
- package/build/middleware/cookie.d.ts +0 -4
- package/build/middleware/cookie.js +0 -15
- package/build/middleware/cookie.js.map +0 -1
- package/build/middleware/cors.d.ts +0 -8
- package/build/middleware/cors.js +0 -89
- package/build/middleware/cors.js.map +0 -1
- package/build/middleware/form.d.ts +0 -6
- package/build/middleware/form.js +0 -58
- package/build/middleware/form.js.map +0 -1
- package/build/middleware/group.d.ts +0 -3
- package/build/middleware/group.js +0 -77
- package/build/middleware/group.js.map +0 -1
- package/build/middleware/json.d.ts +0 -6
- package/build/middleware/json.js +0 -55
- package/build/middleware/json.js.map +0 -1
- package/build/middleware/logger.d.ts +0 -4
- package/build/middleware/logger.js +0 -16
- package/build/middleware/logger.js.map +0 -1
- package/build/middleware/parse.d.ts +0 -14
- package/build/middleware/parse.js +0 -88
- package/build/middleware/parse.js.map +0 -1
- package/build/middleware/result.d.ts +0 -9
- package/build/middleware/result.js +0 -62
- package/build/middleware/result.js.map +0 -1
- package/build/middleware/tag.d.ts +0 -4
- package/build/middleware/tag.js +0 -10
- package/build/middleware/tag.js.map +0 -1
- package/build/middleware/text.d.ts +0 -5
- package/build/middleware/text.js +0 -46
- package/build/middleware/text.js.map +0 -1
- package/build/proxy.d.ts +0 -19
- package/build/proxy.js +0 -74
- package/build/proxy.js.map +0 -1
- package/build/router.d.ts +0 -26
- package/build/router.js +0 -116
- package/build/router.js.map +0 -1
- package/build/static.d.ts +0 -14
- package/build/static.js +0 -211
- package/build/static.js.map +0 -1
- package/build/websocket/cluster.js +0 -114
- package/build/websocket/cluster.js.map +0 -1
- package/build/websocket/server.d.ts +0 -15
- package/build/websocket/server.js +0 -127
- package/build/websocket/server.js.map +0 -1
- package/build/websocket/types.d.ts +0 -39
- package/build/websocket/types.js +0 -2
- package/build/websocket/types.js.map +0 -1
- package/build/websocket/utils.d.ts +0 -8
- package/build/websocket/utils.js +0 -77
- package/build/websocket/utils.js.map +0 -1
- package/src/api-router.ts +0 -50
- package/src/app-utils.ts +0 -82
- package/src/common/api-router-types.ts +0 -52
- package/src/common/api-router-utils.ts +0 -126
- package/src/common/config/config.ts +0 -49
- package/src/common/config/featuretoggle.ts +0 -6
- package/src/common/config/loader.ts +0 -81
- package/src/common/context.ts +0 -192
- package/src/common/error/badrequesterror.ts +0 -6
- package/src/common/error/configfilenotfound.ts +0 -6
- package/src/common/error/forbiddenerror.ts +0 -6
- package/src/common/error/notfound.ts +0 -6
- package/src/common/error/notimplemented.ts +0 -6
- package/src/common/error/unauthorizederror.ts +0 -6
- package/src/common/logger/constants.ts +0 -28
- package/src/common/logger/factory.ts +0 -53
- package/src/common/logger/interfaces.ts +0 -19
- package/src/common/logger/logger.ts +0 -136
- package/src/common/logger/transport/console.ts +0 -36
- package/src/common/logger/transport/file.ts +0 -26
- package/src/common/parser.ts +0 -50
- package/src/common/router-default-logger-factory.ts +0 -44
- package/src/common/router-handler.ts +0 -186
- package/src/common/router-types.ts +0 -153
- package/src/common/router-utils.ts +0 -47
- package/src/common/session/common.ts +0 -40
- package/src/common/session/service.ts +0 -175
- package/src/common/tokenize-match.ts +0 -98
- package/src/common/url-utils.ts +0 -46
- package/src/middleware/buffer.ts +0 -82
- package/src/middleware/cookie.ts +0 -20
- package/src/middleware/cors.ts +0 -98
- package/src/middleware/form.ts +0 -61
- package/src/middleware/group.ts +0 -74
- package/src/middleware/json.ts +0 -57
- package/src/middleware/logger.ts +0 -18
- package/src/middleware/parse.ts +0 -105
- package/src/middleware/result.ts +0 -73
- package/src/middleware/tag.ts +0 -15
- package/src/middleware/text.ts +0 -47
- package/src/proxy.ts +0 -99
- package/src/router.ts +0 -140
- package/src/static.ts +0 -242
- package/src/websocket/cluster.ts +0 -128
- package/src/websocket/server.ts +0 -135
- package/src/websocket/types.ts +0 -41
- package/src/websocket/utils.ts +0 -95
package/src/middleware/group.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common/context.js";
|
|
2
|
-
import { BadRequestError } from "../common/error/badrequesterror.js";
|
|
3
|
-
import { ForbiddenError } from "../common/error/forbiddenerror.js";
|
|
4
|
-
import { UnAuthorizedError } from "../common/error/unauthorizederror.js";
|
|
5
|
-
import { DEFAULT_PARSER, GroupPolicySchema, assertNotUndefined } from "../common/parser.js";
|
|
6
|
-
import { GroupPolicy, Session } from "../common/session/common.js";
|
|
7
|
-
|
|
8
|
-
const policyCheck = (session: Session, options: GroupPolicy): boolean => {
|
|
9
|
-
switch (options.groupPolicy) {
|
|
10
|
-
case "at_least_one":
|
|
11
|
-
for (const group of options.groups) {
|
|
12
|
-
if (group instanceof Array) {
|
|
13
|
-
let ret = true;
|
|
14
|
-
for (const g of group) {
|
|
15
|
-
if (session.groups.indexOf(g) === -1) {
|
|
16
|
-
ret = false;
|
|
17
|
-
break;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
if (ret) {
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
} else {
|
|
24
|
-
if (session.groups.indexOf(group) !== -1) {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
case "all":
|
|
31
|
-
for (const group of options.groups) {
|
|
32
|
-
if (group instanceof Array) {
|
|
33
|
-
for (const g of group) {
|
|
34
|
-
if (session.groups.indexOf(g) === -1) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
if (session.groups.indexOf(group) === -1) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return true;
|
|
45
|
-
default:
|
|
46
|
-
throw new BadRequestError(`policy [${options.groupPolicy}] not implemented!!`);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const GroupPolicyHandler = (options: GroupPolicy): Handler<void> => {
|
|
51
|
-
options = assertNotUndefined(DEFAULT_PARSER.parse(options, GroupPolicySchema, "options"));
|
|
52
|
-
return function GroupPolicyHandler(ctx: Context): void {
|
|
53
|
-
try {
|
|
54
|
-
if (!ctx.session) {
|
|
55
|
-
throw new UnAuthorizedError(`No Session!`);
|
|
56
|
-
} else {
|
|
57
|
-
const result = policyCheck(ctx.session, options);
|
|
58
|
-
if (result) {
|
|
59
|
-
ctx.logger.debug("groups validated!");
|
|
60
|
-
} else {
|
|
61
|
-
ctx.logger.error("%s groups fail to validate!", ctx && ctx.session && ctx.session.groups ? `[${ctx.session.groups.join(",")}] ` : "");
|
|
62
|
-
throw new ForbiddenError(`Invalid session. You are not permitted to do this!`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
} catch (e: any) {
|
|
66
|
-
//(logger as Logger).warn(`request[${req.uuid}] message[${e.message}] stack[${e.stack}]`);
|
|
67
|
-
if (e.name && e.name !== "Error") {
|
|
68
|
-
throw e;
|
|
69
|
-
} else {
|
|
70
|
-
throw new UnAuthorizedError(`Invalid session. You are not permitted to do this!`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
};
|
package/src/middleware/json.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { checkEnvVariables } from "../common/config/config.js";
|
|
2
|
-
import { Context, Handler } from "../common/context.js";
|
|
3
|
-
import { BadRequestError } from "../common/error/badrequesterror.js";
|
|
4
|
-
import { DEFAULT_READ_BUFFER_LIMIT, ReadBuffer } from "./buffer.js";
|
|
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 function JSONParser(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.logger.debug("ctx.body = %o", parsed);
|
|
42
|
-
ctx.body = parsed;
|
|
43
|
-
}
|
|
44
|
-
} else if (isType && ctx.buffer && ctx.buffer.length > limit) {
|
|
45
|
-
ctx.logger.error(`ctx.buffer.length ${ctx.buffer.length} > ${limit}. To accept this body set BODY_PARSER_LIMIT to a higher value.`);
|
|
46
|
-
throw new BadRequestError(`buffer.length ${ctx.buffer.length} > ${limit}`);
|
|
47
|
-
}
|
|
48
|
-
} catch (e: any) {
|
|
49
|
-
if (e && e.name === "BadRequestError") {
|
|
50
|
-
throw e;
|
|
51
|
-
} else {
|
|
52
|
-
ctx.logger.error(e);
|
|
53
|
-
throw new BadRequestError();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
};
|
package/src/middleware/logger.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common/context.js";
|
|
2
|
-
|
|
3
|
-
export const LoggerHandler = (options?: {
|
|
4
|
-
formatter: (ctx: Context) => string;
|
|
5
|
-
}): Handler<void> =>
|
|
6
|
-
function LoggerHandler(ctx: Context): 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
|
-
}
|
package/src/middleware/parse.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { ParserInterface } from "@miqro/parser";
|
|
2
|
-
import { DEFAULT_PARSER } from "../common/parser.js";
|
|
3
|
-
import { ParserMode, SchemaProperties } from "../common/router-types.js";
|
|
4
|
-
import { Context, Handler } from "../common/context.js";
|
|
5
|
-
|
|
6
|
-
export interface ParseRequestOptions {
|
|
7
|
-
headers?: string | SchemaProperties | SchemaProperties[];
|
|
8
|
-
headersMode?: ParserMode;
|
|
9
|
-
query?: string | SchemaProperties | boolean | SchemaProperties[];
|
|
10
|
-
queryMode?: ParserMode;
|
|
11
|
-
params?: string | SchemaProperties | boolean | SchemaProperties[];
|
|
12
|
-
paramsMode?: ParserMode;
|
|
13
|
-
body?: string | SchemaProperties | boolean | SchemaProperties[];
|
|
14
|
-
bodyMode?: ParserMode;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const NO_OPTIONS: SchemaProperties[] = [];
|
|
18
|
-
|
|
19
|
-
const ADD_EXTRA: SchemaProperties[] = [];
|
|
20
|
-
|
|
21
|
-
const normalizeParseOptions = (option?: string | SchemaProperties | SchemaProperties[] | boolean): { properties: Array<string | SchemaProperties>, mode: ParserMode | undefined; } => {
|
|
22
|
-
const mode: ParserMode | undefined = typeof option === "boolean" && option ? "add_extra" : undefined;
|
|
23
|
-
return {
|
|
24
|
-
mode,
|
|
25
|
-
properties: option !== undefined && typeof option !== "string" && typeof option !== "boolean" ?
|
|
26
|
-
(option instanceof Array ? option : [option]) :
|
|
27
|
-
(typeof option === "boolean" ?
|
|
28
|
-
(option ? ADD_EXTRA : NO_OPTIONS) :
|
|
29
|
-
(typeof option === "string" ? [option] : ADD_EXTRA))
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const parseRequestPart = (part: "query" | "body" | "params" | "headers", ctx: Context, option: Array<SchemaProperties | string>, mode?: ParserMode, parser?: ParserInterface): void => {
|
|
35
|
-
if (part === "headers" && mode === undefined) {
|
|
36
|
-
mode = "add_extra";
|
|
37
|
-
}
|
|
38
|
-
const value = ctx[part] === undefined ? {} : ctx[part];
|
|
39
|
-
for (let i = 0; i < option.length; i++) {
|
|
40
|
-
const o = option[i];
|
|
41
|
-
try {
|
|
42
|
-
ctx[part] = (parser ? parser : DEFAULT_PARSER).parse(value as any, typeof o === "string" ? o : {
|
|
43
|
-
type: "object",
|
|
44
|
-
properties: o,
|
|
45
|
-
mode
|
|
46
|
-
}, `${part}`) as any;
|
|
47
|
-
ctx.logger.debug("req.%s parsed to [%o]", part, ctx[part]);
|
|
48
|
-
return;
|
|
49
|
-
} catch (e: any) {
|
|
50
|
-
if (i === option.length - 1) {
|
|
51
|
-
throw e;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
export const ParseRequest = (options: ParseRequestOptions, parser?: ParserInterface): Handler<void> => {
|
|
59
|
-
const query = normalizeParseOptions(options.query);
|
|
60
|
-
const params = normalizeParseOptions(options.params);
|
|
61
|
-
const body = normalizeParseOptions(options.body);
|
|
62
|
-
const headers = normalizeParseOptions(options.headers);
|
|
63
|
-
|
|
64
|
-
return function ParseRequest(ctx: Context): void {
|
|
65
|
-
try {
|
|
66
|
-
try {
|
|
67
|
-
if (options.headers !== undefined) {
|
|
68
|
-
parseRequestPart("headers", ctx, headers.properties, options.headersMode ? options.headersMode : headers.mode, parser);
|
|
69
|
-
}
|
|
70
|
-
} catch (e) {
|
|
71
|
-
ctx.logger.error("error parsing headers %o", ctx.headers);
|
|
72
|
-
throw e;
|
|
73
|
-
}
|
|
74
|
-
try {
|
|
75
|
-
if (options.query !== undefined) {
|
|
76
|
-
parseRequestPart("query", ctx, query.properties, options.queryMode ? options.queryMode : query.mode, parser);
|
|
77
|
-
}
|
|
78
|
-
} catch (e) {
|
|
79
|
-
ctx.logger.error("error parsing query %o", ctx.query);
|
|
80
|
-
throw e;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
if (options.params !== undefined) {
|
|
85
|
-
parseRequestPart("params", ctx, params.properties, options.paramsMode ? options.paramsMode : params.mode, parser);
|
|
86
|
-
}
|
|
87
|
-
} catch (e) {
|
|
88
|
-
ctx.logger.error("error parsing params %o", ctx.params);
|
|
89
|
-
throw e;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
if (options.body !== undefined) {
|
|
94
|
-
parseRequestPart("body", ctx, body.properties, options.bodyMode ? options.bodyMode : body.mode, parser);
|
|
95
|
-
}
|
|
96
|
-
} catch (e: any) {
|
|
97
|
-
ctx.logger.error("error parsing body %o", ctx.body);
|
|
98
|
-
throw e;
|
|
99
|
-
}
|
|
100
|
-
} catch (e: any) {
|
|
101
|
-
ctx.logger.warn("error parsing request: %s", e.message);
|
|
102
|
-
throw e;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
};
|
package/src/middleware/result.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { ParserInterface } from "@miqro/parser";
|
|
2
|
-
import { Context, Handler } from "../common/context.js";
|
|
3
|
-
import { ParserMode, SchemaProperties } from "../common/router-types.js";
|
|
4
|
-
import { DEFAULT_PARSER } from "../common/parser.js";
|
|
5
|
-
|
|
6
|
-
export const ResultParser = (options: {
|
|
7
|
-
headers?: string | SchemaProperties | SchemaProperties[];
|
|
8
|
-
headersMode?: ParserMode;
|
|
9
|
-
body?: string | boolean | SchemaProperties | SchemaProperties[];
|
|
10
|
-
bodyMode?: ParserMode;
|
|
11
|
-
}, parser?: ParserInterface): Handler => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
//options = parse(options, "ParserArgs[]!", undefined, "options");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const bodyParser = options.body instanceof Array ?
|
|
18
|
-
options.body :
|
|
19
|
-
typeof options.body === "boolean" ?
|
|
20
|
-
options.body === true ?
|
|
21
|
-
[{
|
|
22
|
-
type: "any"
|
|
23
|
-
}] : [] : (typeof options.body === "string" ? [options.body] : options.body ? [options.body] : []);
|
|
24
|
-
const headersParser = options.headers instanceof Array ?
|
|
25
|
-
options.headers :
|
|
26
|
-
typeof options.headers === "boolean" ?
|
|
27
|
-
options.headers === true ?
|
|
28
|
-
[{
|
|
29
|
-
type: "any"
|
|
30
|
-
}] : [] : (typeof options.headers === "string" ? [options.headers] : options.headers ? [options.headers] : []);
|
|
31
|
-
|
|
32
|
-
return async function ResultParser(ctx: Context): Promise<any> {
|
|
33
|
-
if (!ctx.results || ctx.results.length === 0) {
|
|
34
|
-
ctx.logger.debug(`not parsing results`);
|
|
35
|
-
return undefined;
|
|
36
|
-
} else {
|
|
37
|
-
const index = ctx.results.length - 1;
|
|
38
|
-
const lastResult = ctx.results[index] ? ctx.results[index] : undefined;
|
|
39
|
-
ctx.logger.trace(`parsing lastResult.body[%o]`, lastResult);
|
|
40
|
-
try {
|
|
41
|
-
const mappedLastResult = {
|
|
42
|
-
...lastResult
|
|
43
|
-
};
|
|
44
|
-
mappedLastResult.body = parsePart(ctx, "body", lastResult.body, bodyParser, options.bodyMode, parser);
|
|
45
|
-
mappedLastResult.headers = parsePart(ctx, "headers", lastResult.headers, headersParser, options.headersMode, parser);
|
|
46
|
-
return mappedLastResult;
|
|
47
|
-
} catch (e) {
|
|
48
|
-
ctx.logger.error(`error parsing lastResult[%o]`, lastResult);
|
|
49
|
-
ctx.logger.error(e);
|
|
50
|
-
throw e;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function parsePart(ctx: Context, part: "body" | "headers", value: any, args: Array<string | SchemaProperties>, mode?: ParserMode, parser?: ParserInterface) {
|
|
57
|
-
for (let i = 0; i < args.length; i++) {
|
|
58
|
-
const o = args[i];
|
|
59
|
-
try {
|
|
60
|
-
const mappedResultBody = (parser ? parser : DEFAULT_PARSER).parse(value, typeof o === "string" ? o : {
|
|
61
|
-
type: "object",
|
|
62
|
-
properties: o,
|
|
63
|
-
mode
|
|
64
|
-
}, `ctx.results.${part}`);
|
|
65
|
-
ctx.logger.trace(`ctx.results.%s mapped to %o`, part, mappedResultBody);
|
|
66
|
-
return mappedResultBody;
|
|
67
|
-
} catch (e) {
|
|
68
|
-
if (i === args.length - 1) {
|
|
69
|
-
throw e;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
package/src/middleware/tag.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common/context.js";
|
|
2
|
-
|
|
3
|
-
export const TagResponse = (options?: {
|
|
4
|
-
uuid?: boolean;
|
|
5
|
-
}): Handler<void> =>
|
|
6
|
-
function TagResponse(ctx: Context): 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
|
-
|
package/src/middleware/text.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { checkEnvVariables } from "../common/config/config.js";
|
|
2
|
-
import { Context, Handler } from "../common/context.js";
|
|
3
|
-
import { BadRequestError } from "../common/error/badrequesterror.js";
|
|
4
|
-
import { DEFAULT_READ_BUFFER_LIMIT, ReadBuffer } from "./buffer.js";
|
|
5
|
-
|
|
6
|
-
export const TextParser = (options?: {
|
|
7
|
-
limit?: number;
|
|
8
|
-
type?: string;
|
|
9
|
-
}): Handler<void> => {
|
|
10
|
-
let limit = DEFAULT_READ_BUFFER_LIMIT;
|
|
11
|
-
let type = "plain/text";
|
|
12
|
-
if (options) {
|
|
13
|
-
limit = options.limit !== undefined ? options.limit : limit;
|
|
14
|
-
type = options.type !== undefined ? options.type : type;
|
|
15
|
-
} else {
|
|
16
|
-
const [limitS, typeS] =
|
|
17
|
-
checkEnvVariables(["BODY_TEXT_PARSER_LIMIT", "BODY_TEXT_PARSER_TYPE"], [String(DEFAULT_READ_BUFFER_LIMIT), "plain/text"]);
|
|
18
|
-
limit = parseInt(limitS, 10);
|
|
19
|
-
type = typeS;
|
|
20
|
-
}
|
|
21
|
-
const readBuffer = ReadBuffer({
|
|
22
|
-
limit
|
|
23
|
-
});
|
|
24
|
-
return async function TextParser(ctx: Context): Promise<void> {
|
|
25
|
-
try {
|
|
26
|
-
const isType = ctx.body === undefined && ctx.headers["content-type"] ? ctx.headers["content-type"].toLocaleLowerCase().indexOf(type.toLocaleLowerCase()) !== -1 : false;
|
|
27
|
-
if (isType && !ctx.buffer) {
|
|
28
|
-
await readBuffer(ctx);
|
|
29
|
-
}
|
|
30
|
-
if (isType && ctx.buffer && ctx.buffer.length <= limit) {
|
|
31
|
-
const string = ctx.buffer.toString();
|
|
32
|
-
ctx.logger.debug("ctx.body = %s", string);
|
|
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/proxy.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { Context } from "./common/context.js";
|
|
2
|
-
import { request, RequestOptions } from "@miqro/request";
|
|
3
|
-
import { Router } from "./router.js";
|
|
4
|
-
import { normalizePath } from "./common/url-utils.js";
|
|
5
|
-
import { PathPartToken } from "./common/tokenize-match.js";
|
|
6
|
-
import { URL } from "url";
|
|
7
|
-
import { IncomingMessage } from "http";
|
|
8
|
-
import { RouterOptions } from "./common/router-types.js";
|
|
9
|
-
|
|
10
|
-
export interface ProxyRouterOptions extends RouterOptions {
|
|
11
|
-
url: string;
|
|
12
|
-
rejectUnauthorized?: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class ProxyRouter extends Router {
|
|
16
|
-
private readonly proxyURL: URL;
|
|
17
|
-
private readonly rejectUnauthorized?: boolean;
|
|
18
|
-
|
|
19
|
-
constructor(options: ProxyRouterOptions) {
|
|
20
|
-
super(options);
|
|
21
|
-
const proxyURL = new URL(options.url);
|
|
22
|
-
proxyURL.pathname = normalizePath(proxyURL.pathname);
|
|
23
|
-
this.proxyURL = proxyURL;
|
|
24
|
-
this.rejectUnauthorized = options.rejectUnauthorized;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async run(ctx: Context, pathTokens?: { value: string; lower: string }[], prePathTokens: PathPartToken[] = []): Promise<boolean> {
|
|
28
|
-
|
|
29
|
-
const level = prePathTokens.length;
|
|
30
|
-
const shouldContinue = await super.run(ctx, pathTokens, prePathTokens);
|
|
31
|
-
|
|
32
|
-
if (shouldContinue) {
|
|
33
|
-
const relative = ctx.path.split("/").map(s => s.trim()).filter(s => s).slice(level).join("/");
|
|
34
|
-
|
|
35
|
-
const nUrl = new URL(this.proxyURL.toString());
|
|
36
|
-
nUrl.pathname = nUrl.pathname + relative;
|
|
37
|
-
const url = nUrl.toString();
|
|
38
|
-
|
|
39
|
-
const headers = {
|
|
40
|
-
...ctx.headers
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
delete headers["connection"];
|
|
44
|
-
delete headers["keep-alive"];
|
|
45
|
-
|
|
46
|
-
let responseHeaders;
|
|
47
|
-
|
|
48
|
-
const requestArgs: RequestOptions = {
|
|
49
|
-
url: url.toString(),
|
|
50
|
-
query: ctx.query,
|
|
51
|
-
disableThrow: true,
|
|
52
|
-
rejectUnauthorized: this.rejectUnauthorized,
|
|
53
|
-
disableUserAgent: true,
|
|
54
|
-
method: ctx.method,
|
|
55
|
-
headers,
|
|
56
|
-
onChunk: async function (chunk: Buffer, res: IncomingMessage): Promise<void> {
|
|
57
|
-
const responseHeaders = {
|
|
58
|
-
...res.headers
|
|
59
|
-
};
|
|
60
|
-
delete responseHeaders["connection"];
|
|
61
|
-
delete responseHeaders["keep-alive"];
|
|
62
|
-
if (!ctx.res.headersSent) {
|
|
63
|
-
const headerNames = Object.keys(responseHeaders);
|
|
64
|
-
for (const name of headerNames) {
|
|
65
|
-
ctx.setHeader(name, responseHeaders[name] as any);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
if (res.statusCode !== undefined) {
|
|
69
|
-
ctx.res.statusCode = res.statusCode
|
|
70
|
-
}
|
|
71
|
-
await ctx.write(chunk);
|
|
72
|
-
},
|
|
73
|
-
data: ctx.method !== "GET" ? ctx.buffer : undefined
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
ctx.logger.info("proxy to %s", url.toString());
|
|
77
|
-
ctx.logger.debug("proxy with %o", requestArgs);
|
|
78
|
-
|
|
79
|
-
const response = await request(requestArgs);
|
|
80
|
-
|
|
81
|
-
if (!ctx.res.headersSent) {
|
|
82
|
-
const responseHeaders = {
|
|
83
|
-
...response.headers
|
|
84
|
-
};
|
|
85
|
-
const headerNames = Object.keys(responseHeaders);
|
|
86
|
-
for (const name of headerNames) {
|
|
87
|
-
ctx.setHeader(name, responseHeaders[name] as any);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
await ctx.end();
|
|
92
|
-
}
|
|
93
|
-
return !ctx.res.headersSent;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export const Proxy = (options: ProxyRouterOptions) => {
|
|
98
|
-
return new ProxyRouter(options);
|
|
99
|
-
}
|
package/src/router.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { RequestListener } from "http";
|
|
2
|
-
import { Logger } from "./common/logger/logger.js";
|
|
3
|
-
import { Context, ErrorHandler, Handler, Method } from "./common/context.js";
|
|
4
|
-
import {
|
|
5
|
-
HandlerWithOptions,
|
|
6
|
-
MinimalRouter,
|
|
7
|
-
RouterHandlerOptions,
|
|
8
|
-
RouterJSONDoc,
|
|
9
|
-
RouterOptions
|
|
10
|
-
} from "./common/router-types.js";
|
|
11
|
-
import { PathPart, PathPartToken, splitPath } from "./common/tokenize-match.js";
|
|
12
|
-
import { defaultLoggerFactory } from "./common/router-default-logger-factory.js";
|
|
13
|
-
import { RouterHandler } from "./common/router-handler.js";
|
|
14
|
-
import { randomUUID } from "crypto";
|
|
15
|
-
import { normalizePath } from "./common/url-utils.js";
|
|
16
|
-
|
|
17
|
-
export class Router implements MinimalRouter {
|
|
18
|
-
protected readonly handlers: RouterHandler[] = [];
|
|
19
|
-
protected readonly errorHandlers: ErrorHandler[] = [];
|
|
20
|
-
public readonly listener: RequestListener;
|
|
21
|
-
|
|
22
|
-
constructor(protected config?: RouterOptions) {
|
|
23
|
-
const loggerFactory = this.config && this.config.loggerFactory ? this.config.loggerFactory : defaultLoggerFactory;
|
|
24
|
-
this.listener = async (req, res) => {
|
|
25
|
-
const uuid = randomUUID();
|
|
26
|
-
const logger = loggerFactory(uuid, req);
|
|
27
|
-
const ctx = new Context(uuid, req, res, logger);
|
|
28
|
-
ctx.logger.trace(`request received`);
|
|
29
|
-
await this.run(ctx);
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public get(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
34
|
-
return this.use(handler, path, "get", options);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public post(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
38
|
-
return this.use(handler, path, "post", options);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public patch(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
42
|
-
return this.use(handler, path, "patch", options);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public delete(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
46
|
-
return this.use(handler, path, "delete", options);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public put(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
50
|
-
return this.use(handler, path, "put", options);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public options(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
54
|
-
return this.use(handler, path, "options", options);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public use(handler: Array<Handler> | HandlerWithOptions | Handler | MinimalRouter, path?: string, method?: Method, options?: RouterHandlerOptions): Router {
|
|
58
|
-
const nPath = path !== undefined ? normalizePath(path) : undefined;
|
|
59
|
-
const nMethod = method ? method.toUpperCase() as Method : method;
|
|
60
|
-
this.handlers.push(new RouterHandler(handler, nPath, nMethod, options));
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public catch(errorHandler: Array<ErrorHandler> | ErrorHandler): Router {
|
|
65
|
-
const errorHandlers = errorHandler instanceof Array ? errorHandler : [errorHandler];
|
|
66
|
-
for (const e of errorHandlers) {
|
|
67
|
-
if (typeof e !== "function") {
|
|
68
|
-
throw new Error(`errorHandler ${e} not a function!`);
|
|
69
|
-
}
|
|
70
|
-
this.errorHandlers.push(e);
|
|
71
|
-
}
|
|
72
|
-
return this;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public async run(ctx: Context, pathTokens?: PathPart[], prePathTokens: PathPartToken[] = []): Promise<boolean> {
|
|
76
|
-
pathTokens = pathTokens ? pathTokens : splitPath(ctx.path);
|
|
77
|
-
try {
|
|
78
|
-
for (const h of this.handlers) {
|
|
79
|
-
if (ctx.res.headersSent) {
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
const isMatch = h.isMatch(ctx.method, pathTokens, prePathTokens);
|
|
83
|
-
if (isMatch.match) {
|
|
84
|
-
if (isMatch.params) {
|
|
85
|
-
ctx.params = ctx.params ? {
|
|
86
|
-
...ctx.params,
|
|
87
|
-
...isMatch.params
|
|
88
|
-
} : isMatch.params;
|
|
89
|
-
}
|
|
90
|
-
const shouldContinue = await h.invoke(ctx, pathTokens, prePathTokens);
|
|
91
|
-
if (!shouldContinue) {
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return !ctx.res.headersSent;
|
|
97
|
-
} catch (e: any) {
|
|
98
|
-
for (const eH of this.errorHandlers) {
|
|
99
|
-
const ret = await eH(e, ctx);
|
|
100
|
-
if (ret === false || ctx.res.headersSent) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
throw e; // pass the error along
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
|
|
109
|
-
for (const h of this.handlers) {
|
|
110
|
-
doc = h.getJSONDoc(doc, prePath);
|
|
111
|
-
}
|
|
112
|
-
return doc;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public logPaths(logger: Logger): void {
|
|
116
|
-
this.toString().split("\n").forEach(l=>logger.info(l));
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public toString(): string {
|
|
120
|
-
let ret = "";
|
|
121
|
-
const doc = this.getJSONDoc();
|
|
122
|
-
const paths = Object.keys(doc);
|
|
123
|
-
|
|
124
|
-
for(const path of paths) {
|
|
125
|
-
if (path) {
|
|
126
|
-
const pathData = doc[path];
|
|
127
|
-
|
|
128
|
-
const methods = Object.keys(pathData);
|
|
129
|
-
|
|
130
|
-
for (const method of methods) {
|
|
131
|
-
if (method) {
|
|
132
|
-
const methodData = pathData[method];
|
|
133
|
-
ret += `${ret === "" ? "" : "\n"}${method}:${path}\n\t${methodData.identifier}${methodData.name ? `\n\t\t${methodData.name}` : ""}${methodData.description ? `\n\t\t${methodData.description}` : ""}`;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return ret;
|
|
139
|
-
}
|
|
140
|
-
}
|