milkio 0.0.3 → 0.0.4
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/defines/define-http-handler.ts +13 -13
- package/kernel/config.ts +1 -2
- package/kernel/context.ts +2 -2
- package/kernel/fail.ts +2 -2
- package/kernel/milkio.ts +3 -3
- package/kernel/validate.ts +1 -1
- package/package.json +1 -1
- package/utils/handle-catch-error.ts +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
|
-
import {
|
|
2
|
+
import { configMilkio, loggerPushTags, loggerSubmit, useLogger, loggerSubmitAll, runtime, MiddlewareEvent } from "..";
|
|
3
3
|
import type { ExecuteId, MilkioApp, Mixin } from "..";
|
|
4
4
|
import { hanldeCatchError } from "../utils/handle-catch-error";
|
|
5
5
|
import { routerHandler } from "../../../src/router";
|
|
@@ -33,7 +33,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
33
33
|
exit(1);
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
-
const fetch = async (request:
|
|
36
|
+
const fetch = async (request: MilkioHTTPRequest) => {
|
|
37
37
|
const fullurl = new URL(request.request.url, `http://${request.request.headers.get("host") ?? "localhost"}`);
|
|
38
38
|
const executeId = (options?.executeIdGenerator ? await options.executeIdGenerator(request.request) : createUlid()) as ExecuteId;
|
|
39
39
|
runtime.execute.executeIds.add(executeId);
|
|
@@ -50,14 +50,14 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
50
50
|
timein: new Date().getTime()
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
-
const response:
|
|
53
|
+
const response: MilkioHTTPResponse = {
|
|
54
54
|
body: "",
|
|
55
55
|
status: 200,
|
|
56
56
|
headers: {
|
|
57
57
|
"Content-Type": "application/json",
|
|
58
|
-
"Access-Control-Allow-Methods":
|
|
59
|
-
"Access-Control-Allow-Headers":
|
|
60
|
-
"Access-Control-Allow-Origin":
|
|
58
|
+
"Access-Control-Allow-Methods": configMilkio.corsAllowMethods ?? "*",
|
|
59
|
+
"Access-Control-Allow-Headers": configMilkio.corsAllowHeaders ?? "*",
|
|
60
|
+
"Access-Control-Allow-Origin": configMilkio.corsAllowOrigin ?? "*"
|
|
61
61
|
}
|
|
62
62
|
};
|
|
63
63
|
|
|
@@ -69,9 +69,9 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
69
69
|
|
|
70
70
|
return new Response("", {
|
|
71
71
|
headers: {
|
|
72
|
-
"Access-Control-Allow-Methods":
|
|
73
|
-
"Access-Control-Allow-Headers":
|
|
74
|
-
"Access-Control-Allow-Origin":
|
|
72
|
+
"Access-Control-Allow-Methods": configMilkio.corsAllowMethods ?? "*",
|
|
73
|
+
"Access-Control-Allow-Headers": configMilkio.corsAllowHeaders ?? "*",
|
|
74
|
+
"Access-Control-Allow-Origin": configMilkio.corsAllowOrigin ?? "*"
|
|
75
75
|
}
|
|
76
76
|
});
|
|
77
77
|
}
|
|
@@ -80,7 +80,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
80
80
|
|
|
81
81
|
// Compatible with API gateway's ability to differentiate versions by path
|
|
82
82
|
// see: /src/config/ConfigProgram.ts in "ignorePathLevel"
|
|
83
|
-
if (
|
|
83
|
+
if (configMilkio.ignorePathLevel !== 0) path = path.slice(configMilkio.ignorePathLevel);
|
|
84
84
|
|
|
85
85
|
let pathstr = path.join("/") as keyof (typeof schema)["apiMethodsSchema"];
|
|
86
86
|
|
|
@@ -92,7 +92,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
92
92
|
loggerPushTags(executeId, {
|
|
93
93
|
body: rawbody || "no body"
|
|
94
94
|
});
|
|
95
|
-
response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":${JSON.stringify(failCode
|
|
95
|
+
response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":${JSON.stringify(failCode.NOT_FOUND())}}}`;
|
|
96
96
|
|
|
97
97
|
loggerPushTags(executeId, {
|
|
98
98
|
status: response.status,
|
|
@@ -188,11 +188,11 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
|
|
|
188
188
|
return fetch;
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
-
export type
|
|
191
|
+
export type MilkioHTTPRequest = {
|
|
192
192
|
request: Request;
|
|
193
193
|
};
|
|
194
194
|
|
|
195
|
-
export type
|
|
195
|
+
export type MilkioHTTPResponse = Mixin<
|
|
196
196
|
ResponseInit,
|
|
197
197
|
{
|
|
198
198
|
//
|
package/kernel/config.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { cwd, env } from "node:process";
|
|
2
2
|
import { envToBoolean, envToNumber, envToString } from "..";
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const configMilkio = {
|
|
5
5
|
cwd: cwd(),
|
|
6
6
|
port: envToNumber(env.PORT, 9000),
|
|
7
7
|
milkioRunMode: envToString(env.milkio_RUN_MODE, "DEFAULT"),
|
|
8
8
|
milkioTest: envToString(env.milkio_TEST, ""),
|
|
9
9
|
debug: envToBoolean(env.DEBUG, false),
|
|
10
|
-
redisUrl: envToString(env.REDIS_URL, "redis://:123456@your-not-redis-url:6379"),
|
|
11
10
|
ignorePathLevel: envToNumber(env.IGNORE_PATH_LEVEL, 0),
|
|
12
11
|
corsAllowMethods: envToString(env.CORS_ALLOW_METHODS, "*"),
|
|
13
12
|
corsAllowHeaders: envToString(env.CORS_ALLOW_HEADERS, "*"),
|
package/kernel/context.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { URL } from "node:url";
|
|
2
|
-
import type { ExecuteId, Logger,
|
|
2
|
+
import type { ExecuteId, Logger, MilkioHTTPResponse } from "..";
|
|
3
3
|
|
|
4
4
|
export type MilkioContext = {
|
|
5
5
|
path: string;
|
|
@@ -20,5 +20,5 @@ export type FrameworkHTTPDetail = {
|
|
|
20
20
|
fullurl: URL;
|
|
21
21
|
ip: string;
|
|
22
22
|
request: Request;
|
|
23
|
-
response:
|
|
23
|
+
response: MilkioHTTPResponse;
|
|
24
24
|
};
|
package/kernel/fail.ts
CHANGED
|
@@ -4,7 +4,7 @@ export function reject<Code extends keyof typeof failCode, FailData extends (typ
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
|
|
5
5
|
const message = failCode[code]?.(data as any) ?? "";
|
|
6
6
|
const error = {
|
|
7
|
-
name: "
|
|
7
|
+
name: "MilkioReject",
|
|
8
8
|
code,
|
|
9
9
|
message,
|
|
10
10
|
data,
|
|
@@ -16,7 +16,7 @@ export function reject<Code extends keyof typeof failCode, FailData extends (typ
|
|
|
16
16
|
return error;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export type
|
|
19
|
+
export type MilkioReject = ReturnType<typeof reject>;
|
|
20
20
|
|
|
21
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
22
|
export type MilkioFailCode = Record<string, (arg: any) => string>;
|
package/kernel/milkio.ts
CHANGED
|
@@ -47,7 +47,7 @@ export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
|
|
|
47
47
|
|
|
48
48
|
if (MilkioAppOptions.enableMaxRunningTimeoutLimit && MilkioAppOptions.enableMaxRunningTimeoutLimit >= 1) {
|
|
49
49
|
setTimeout(() => {
|
|
50
|
-
console.log('❌
|
|
50
|
+
console.log('❌ Milkio reached the limit of "maxRunningTimeout" in the options and automatically exited.');
|
|
51
51
|
exit(0);
|
|
52
52
|
}, MilkioAppOptions.enableMaxRunningTimeoutLimit * 60 * 1000);
|
|
53
53
|
runtime.maxRunningTimeout.enable = true;
|
|
@@ -137,7 +137,7 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
137
137
|
|
|
138
138
|
if (runtime.maxRequest.enable) {
|
|
139
139
|
if (runtime.maxRequest.counter >= runtime.maxRequest.expected) {
|
|
140
|
-
console.log("❌
|
|
140
|
+
console.log("❌ Milkio reached the limit of 'maxRequest' in the options and automatically exited.");
|
|
141
141
|
exit(0);
|
|
142
142
|
}
|
|
143
143
|
runtime.maxRequest.counter++;
|
|
@@ -149,7 +149,7 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
149
149
|
success: false,
|
|
150
150
|
fail: {
|
|
151
151
|
code: "NOT_FOUND",
|
|
152
|
-
message: failCode
|
|
152
|
+
message: failCode.NOT_FOUND(),
|
|
153
153
|
data: undefined
|
|
154
154
|
}
|
|
155
155
|
} satisfies ExecuteResult<Result>;
|
package/kernel/validate.ts
CHANGED
|
@@ -5,7 +5,7 @@ export function _validate(validator: IValidation.IFailure | IValidation.ISuccess
|
|
|
5
5
|
if (validator.success) return;
|
|
6
6
|
const error = validator.errors[0];
|
|
7
7
|
|
|
8
|
-
throw reject("
|
|
8
|
+
throw reject("TYPE_SAFE_ERROR", {
|
|
9
9
|
path: error.path,
|
|
10
10
|
expected: error.expected,
|
|
11
11
|
value: error.value
|
package/package.json
CHANGED
|
@@ -9,8 +9,8 @@ export function hanldeCatchError(error: any, executeId: ExecuteId): ExecuteResul
|
|
|
9
9
|
if (error.stack) logger.error("\nError Stack: ", error.stack);
|
|
10
10
|
else logger.error("\nError Stack: ", error);
|
|
11
11
|
|
|
12
|
-
if (error.name !== "
|
|
13
|
-
// If it is not
|
|
12
|
+
if (error.name !== "MilkioReject") {
|
|
13
|
+
// If it is not MilkioReject, it is considered an internal server error that should not be exposed
|
|
14
14
|
logger.error(`FailCode: INTERNAL_SERVER_ERROR`);
|
|
15
15
|
|
|
16
16
|
return {
|
|
@@ -18,7 +18,7 @@ export function hanldeCatchError(error: any, executeId: ExecuteId): ExecuteResul
|
|
|
18
18
|
success: false,
|
|
19
19
|
fail: {
|
|
20
20
|
code: "INTERNAL_SERVER_ERROR",
|
|
21
|
-
message: failCode
|
|
21
|
+
message: failCode.INTERNAL_SERVER_ERROR(),
|
|
22
22
|
data: undefined
|
|
23
23
|
}
|
|
24
24
|
};
|