milkio 0.0.10 → 0.0.11
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/.co.toml +0 -0
- package/api-test/index.ts +64 -0
- package/c.ts +39 -57
- package/defines/define-api-test.ts +3 -3
- package/defines/define-api.ts +3 -3
- package/defines/define-http-handler.ts +60 -69
- package/defines/define-middleware.ts +2 -2
- package/defines/define-use.ts +6 -6
- package/index.ts +23 -22
- package/kernel/context.ts +1 -2
- package/kernel/fail.ts +6 -6
- package/kernel/logger.ts +38 -38
- package/kernel/meta.ts +5 -5
- package/kernel/middleware.ts +16 -16
- package/kernel/milkio.ts +70 -95
- package/kernel/runtime.ts +2 -7
- package/kernel/validate.ts +5 -5
- package/package.json +4 -1
- package/scripts/gen-insignificant.ts +261 -0
- package/scripts/gen-significant.ts +176 -0
- package/{scripts → scripts-del}/build-cookbook.ts +119 -119
- package/scripts-del/build-dto.ts +65 -0
- package/{scripts → scripts-del}/generate/generate-app-partial.ts +31 -31
- package/{scripts → scripts-del}/generate/generate-app.ts +41 -41
- package/scripts-del/generate/generate-database.ts +22 -0
- package/scripts-del/generate-partial.ts +15 -0
- package/scripts-del/generate.ts +23 -0
- package/templates/api.ts +4 -4
- package/types.ts +29 -19
- package/utils/create-template.ts +5 -5
- package/utils/create-ulid.ts +3 -3
- package/utils/env-to-boolean.ts +5 -5
- package/utils/env-to-number.ts +2 -2
- package/utils/env-to-string.ts +2 -2
- package/utils/exec.ts +12 -12
- package/utils/handle-catch-error.ts +10 -10
- package/utils/remove-dir.ts +11 -11
- package/utils/tson.ts +2 -2
- package/defines/define-api-test-handler.ts +0 -71
- package/kernel/config.ts +0 -14
- package/scripts/build-dto.ts +0 -65
- package/scripts/generate/generate-database.ts +0 -22
- package/scripts/generate-database.ts +0 -23
- package/scripts/generate-partial.ts +0 -15
- package/scripts/generate.ts +0 -23
package/kernel/logger.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
|
|
3
|
-
import { runtime, type ExecuteId } from ".."
|
|
4
|
-
import { loggerOptions } from "../../../src/logger"
|
|
3
|
+
import { runtime, type ExecuteId } from ".."
|
|
4
|
+
import { loggerOptions } from "../../../src/logger"
|
|
5
5
|
|
|
6
6
|
export type LoggerItem = {
|
|
7
7
|
executeId: ExecuteId;
|
|
@@ -25,89 +25,89 @@ export type Logger = {
|
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
export const loggerController = (() => {
|
|
28
|
-
const logs = new Map<ExecuteId, { __LOG_DEATIL__: Array<LoggerItem>; [key: string]: any }>()
|
|
28
|
+
const logs = new Map<ExecuteId, { __LOG_DEATIL__: Array<LoggerItem>; [key: string]: any }>()
|
|
29
29
|
|
|
30
30
|
const loggerPushTags = (executeId: ExecuteId, tags: Record<string, any>) => {
|
|
31
|
-
if (!logs.has(executeId)) logs.set(executeId, { __LOG_DEATIL__: [] })
|
|
32
|
-
const logItem = logs.get(executeId)
|
|
31
|
+
if (!logs.has(executeId)) logs.set(executeId, { __LOG_DEATIL__: [] })
|
|
32
|
+
const logItem = logs.get(executeId)
|
|
33
33
|
for (const key in tags) {
|
|
34
|
-
logItem![key] = tags[key]
|
|
34
|
+
logItem![key] = tags[key]
|
|
35
35
|
}
|
|
36
|
-
}
|
|
36
|
+
}
|
|
37
37
|
|
|
38
38
|
const loggerSubmit = async (executeId: ExecuteId) => {
|
|
39
|
-
if (!logs.has(executeId)) return
|
|
40
|
-
if (executeId === "global") return
|
|
39
|
+
if (!logs.has(executeId)) return
|
|
40
|
+
if (executeId === "global") return
|
|
41
41
|
const loggerSubmitOptions: Record<string, string> = {
|
|
42
42
|
executeId
|
|
43
|
-
}
|
|
44
|
-
const log = logs.get(executeId)
|
|
43
|
+
}
|
|
44
|
+
const log = logs.get(executeId)!
|
|
45
45
|
for (const key in log) {
|
|
46
|
-
if (key === "__LOG_DEATIL__") continue
|
|
47
|
-
loggerSubmitOptions[key] = log[key]
|
|
46
|
+
if (key === "__LOG_DEATIL__") continue
|
|
47
|
+
loggerSubmitOptions[key] = log[key]
|
|
48
48
|
}
|
|
49
|
-
logs.delete(executeId)
|
|
50
|
-
loggerOptions.onSubmit(loggerSubmitOptions, log.__LOG_DEATIL__)
|
|
51
|
-
}
|
|
49
|
+
logs.delete(executeId)
|
|
50
|
+
loggerOptions.onSubmit(loggerSubmitOptions, log.__LOG_DEATIL__)
|
|
51
|
+
}
|
|
52
52
|
|
|
53
53
|
const loggerSubmitAll = async () => {
|
|
54
54
|
for (const executeId of logs.keys()) {
|
|
55
|
-
await loggerSubmit(executeId)
|
|
55
|
+
await loggerSubmit(executeId)
|
|
56
56
|
}
|
|
57
|
-
}
|
|
57
|
+
}
|
|
58
58
|
|
|
59
59
|
const insertItem = (executeId: ExecuteId, level: LoggerLevel, description: string, params: Array<unknown>): void => {
|
|
60
|
-
let executeIds: Array<string> = []
|
|
60
|
+
let executeIds: Array<string> = []
|
|
61
61
|
if (executeId === "global") {
|
|
62
|
-
executeIds = Array.from(new Set([...Array.from(runtime.execute.executeIds), ...Array.from(runtime.execute.executeIds)]))
|
|
62
|
+
executeIds = Array.from(new Set([...Array.from(runtime.execute.executeIds), ...Array.from(runtime.execute.executeIds)]))
|
|
63
63
|
} else {
|
|
64
|
-
executeIds = [executeId]
|
|
64
|
+
executeIds = [executeId]
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
for (const executeId of executeIds) {
|
|
68
|
-
if (!logs.has(executeId as ExecuteId)) logs.set(executeId as ExecuteId, { __LOG_DEATIL__: [] })
|
|
68
|
+
if (!logs.has(executeId as ExecuteId)) logs.set(executeId as ExecuteId, { __LOG_DEATIL__: [] })
|
|
69
69
|
const loggerItem = {
|
|
70
70
|
executeId: executeId as ExecuteId,
|
|
71
71
|
loggerLevel: level,
|
|
72
72
|
description,
|
|
73
73
|
params
|
|
74
|
-
} satisfies LoggerItem
|
|
75
|
-
if (!loggerOptions.onInsert(loggerItem)) return
|
|
76
|
-
logs.get(executeId as ExecuteId)!.__LOG_DEATIL__.push(loggerItem)
|
|
74
|
+
} satisfies LoggerItem
|
|
75
|
+
if (!loggerOptions.onInsert(loggerItem)) return
|
|
76
|
+
logs.get(executeId as ExecuteId)!.__LOG_DEATIL__.push(loggerItem)
|
|
77
77
|
}
|
|
78
|
-
}
|
|
78
|
+
}
|
|
79
79
|
|
|
80
80
|
const useLogger = (executeId: ExecuteId) => {
|
|
81
81
|
return {
|
|
82
82
|
debug(description: string, ...params: Array<unknown>) {
|
|
83
|
-
insertItem(executeId, "debug", description, params)
|
|
83
|
+
insertItem(executeId, "debug", description, params)
|
|
84
84
|
},
|
|
85
85
|
log(description: string, ...params: Array<unknown>) {
|
|
86
|
-
insertItem(executeId, "log", description, params)
|
|
86
|
+
insertItem(executeId, "log", description, params)
|
|
87
87
|
},
|
|
88
88
|
warn(description: string, ...params: Array<unknown>) {
|
|
89
|
-
insertItem(executeId, "warn", description, params)
|
|
89
|
+
insertItem(executeId, "warn", description, params)
|
|
90
90
|
},
|
|
91
91
|
error(description: string, ...params: Array<unknown>) {
|
|
92
|
-
insertItem(executeId, "error", description, params)
|
|
92
|
+
insertItem(executeId, "error", description, params)
|
|
93
93
|
}
|
|
94
|
-
} satisfies Logger
|
|
95
|
-
}
|
|
94
|
+
} satisfies Logger
|
|
95
|
+
}
|
|
96
96
|
|
|
97
97
|
return {
|
|
98
98
|
loggerPushTags,
|
|
99
99
|
loggerSubmit,
|
|
100
100
|
loggerSubmitAll,
|
|
101
101
|
useLogger
|
|
102
|
-
}
|
|
103
|
-
})()
|
|
102
|
+
}
|
|
103
|
+
})()
|
|
104
104
|
|
|
105
|
-
export const useLogger = loggerController.useLogger
|
|
105
|
+
export const useLogger = loggerController.useLogger
|
|
106
106
|
|
|
107
|
-
export const loggerPushTags = loggerController.loggerPushTags
|
|
107
|
+
export const loggerPushTags = loggerController.loggerPushTags
|
|
108
108
|
|
|
109
|
-
export const loggerSubmit = loggerController.loggerSubmit
|
|
109
|
+
export const loggerSubmit = loggerController.loggerSubmit
|
|
110
110
|
|
|
111
|
-
export const loggerSubmitAll = loggerController.loggerSubmitAll
|
|
111
|
+
export const loggerSubmitAll = loggerController.loggerSubmitAll
|
|
112
112
|
|
|
113
113
|
export type LoggerLevel = "debug" | "log" | "warn" | "error";
|
package/kernel/meta.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { Meta } from "../../../src/meta"
|
|
2
|
-
import schema from "../../../
|
|
1
|
+
import type { Meta } from "../../../src/meta"
|
|
2
|
+
import schema from "../../../generated/api-schema"
|
|
3
3
|
|
|
4
4
|
export async function useMeta(path: string): Promise<Meta> {
|
|
5
5
|
// @ts-ignore
|
|
6
|
-
const api = schema.apiMethodsSchema[path as keyof (typeof schema)["apiMethodsTypeSchema"]]()
|
|
7
|
-
const module = await api.module
|
|
8
|
-
return module.api.meta
|
|
6
|
+
const api = schema.apiMethodsSchema[path as keyof (typeof schema)["apiMethodsTypeSchema"]]()
|
|
7
|
+
const module = await api.module
|
|
8
|
+
return module.api.meta
|
|
9
9
|
}
|
package/kernel/middleware.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument */
|
|
2
2
|
|
|
3
|
-
import type { Context } from "../../../src/context"
|
|
4
|
-
import type { FrameworkHTTPDetail } from "./context"
|
|
5
|
-
import { type MilkioApp } from "milkio"
|
|
3
|
+
import type { Context } from "../../../src/context"
|
|
4
|
+
import type { FrameworkHTTPDetail } from "./context"
|
|
5
|
+
import { type MilkioApp } from "milkio"
|
|
6
6
|
|
|
7
7
|
export type BootstrapMiddleware = (milkio: MilkioApp) => Promise<void> | void;
|
|
8
8
|
export type BeforeExecuteMiddleware = (context: Context) => Promise<void> | void;
|
|
@@ -19,32 +19,32 @@ export type MiddlewareOptions = {
|
|
|
19
19
|
export type MiddlewareFn = (...args: Array<any>) => Promise<void> | void;
|
|
20
20
|
export type MiddlewareT<T extends MiddlewareFn = MiddlewareFn> = { id: string; index: number; middleware: T };
|
|
21
21
|
|
|
22
|
-
export const _middlewareEvents = new Map<string, (a: MiddlewareT, b: MiddlewareT) => number>()
|
|
23
|
-
export const _middlewares = new Map<string, Array<MiddlewareT>>()
|
|
22
|
+
export const _middlewareEvents = new Map<string, (a: MiddlewareT, b: MiddlewareT) => number>()
|
|
23
|
+
export const _middlewares = new Map<string, Array<MiddlewareT>>()
|
|
24
24
|
|
|
25
25
|
export const MiddlewareEvent = (() => {
|
|
26
26
|
const defineMiddlewareEvent = (name: string, sortFn: (a: MiddlewareT, b: MiddlewareT) => number) => {
|
|
27
|
-
_middlewareEvents.set(name, sortFn)
|
|
28
|
-
}
|
|
27
|
+
_middlewareEvents.set(name, sortFn)
|
|
28
|
+
}
|
|
29
29
|
|
|
30
30
|
const sortMiddlewareEvent = () => {
|
|
31
31
|
for (const [key, middleware] of _middlewares) {
|
|
32
|
-
const sort = _middlewareEvents.get(key)
|
|
33
|
-
if (sort) middleware.sort(sort)
|
|
32
|
+
const sort = _middlewareEvents.get(key)
|
|
33
|
+
if (sort) middleware.sort(sort)
|
|
34
34
|
}
|
|
35
|
-
}
|
|
35
|
+
}
|
|
36
36
|
|
|
37
37
|
const handleMiddleware = async (name: string, args: Array<any> /* Parameters<MiddlewareOptions[Name]> */) => {
|
|
38
|
-
const mds = _middlewares.get(name)
|
|
39
|
-
if (!mds) return
|
|
38
|
+
const mds = _middlewares.get(name)
|
|
39
|
+
if (!mds) return
|
|
40
40
|
for (const md of mds) {
|
|
41
|
-
await md.middleware(...args)
|
|
41
|
+
await md.middleware(...args)
|
|
42
42
|
}
|
|
43
|
-
}
|
|
43
|
+
}
|
|
44
44
|
|
|
45
45
|
return {
|
|
46
46
|
define: defineMiddlewareEvent,
|
|
47
47
|
handle: handleMiddleware,
|
|
48
48
|
_sort: sortMiddlewareEvent
|
|
49
|
-
}
|
|
50
|
-
})()
|
|
49
|
+
}
|
|
50
|
+
})()
|
package/kernel/milkio.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
/* eslint-disable no-console, @typescript-eslint/no-invalid-void-type, @typescript-eslint/await-thenable, @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { type MiddlewareOptions, _middlewares, MiddlewareEvent } from "./middleware"
|
|
3
|
-
import schema from "../../../
|
|
4
|
-
import type { Context } from "../../../src/context"
|
|
5
|
-
import { failCode } from "../../../src/fail-code"
|
|
6
|
-
import type { MilkioContext } from "./context"
|
|
7
|
-
import { type Mixin, type ExecuteId, type Fail, type FailEnumerates, loggerPushTags, loggerSubmit, runtime, TSON, type Logger, useLogger
|
|
8
|
-
import { hanldeCatchError } from "../utils/handle-catch-error"
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { createUlid } from "../utils/create-ulid";
|
|
2
|
+
import { type MiddlewareOptions, _middlewares, MiddlewareEvent } from "./middleware"
|
|
3
|
+
import schema from "../../../generated/api-schema"
|
|
4
|
+
import type { Context } from "../../../src/context"
|
|
5
|
+
import { failCode } from "../../../src/fail-code"
|
|
6
|
+
import type { MilkioContext } from "./context"
|
|
7
|
+
import { type Mixin, type ExecuteId, type Fail, type FailEnumerates, loggerPushTags, loggerSubmit, runtime, TSON, type Logger, useLogger } from ".."
|
|
8
|
+
import { hanldeCatchError } from "../utils/handle-catch-error"
|
|
9
|
+
import { createUlid } from "../utils/create-ulid"
|
|
10
|
+
import { _validate } from "./validate"
|
|
12
11
|
|
|
13
12
|
export type MilkioAppOptions = {
|
|
14
13
|
/**
|
|
@@ -23,13 +22,6 @@ export type MilkioAppOptions = {
|
|
|
23
22
|
* When Milkio is launched, the closer it is to the front of the array, the more it is on the outer layer of the "onion".
|
|
24
23
|
*/
|
|
25
24
|
middlewares?: () => Array<MiddlewareOptions>;
|
|
26
|
-
/**
|
|
27
|
-
* maxRequest
|
|
28
|
-
* @description
|
|
29
|
-
* When the function runs for a long time, it is possible that the memory will continuously expand (not necessarily due to memory leaks, but also possibly due to having a large number of routes).
|
|
30
|
-
* Set a maximum number of requests, when the number of requests reaches this value, kill the process and automatically restart it from outside (K8S or whatever).
|
|
31
|
-
*/
|
|
32
|
-
enableMaxRequestLimit?: number | null | undefined;
|
|
33
25
|
/**
|
|
34
26
|
* maxRunningTime (minutes)
|
|
35
27
|
* @description
|
|
@@ -40,19 +32,11 @@ export type MilkioAppOptions = {
|
|
|
40
32
|
};
|
|
41
33
|
|
|
42
34
|
export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
|
|
43
|
-
if (argv.length > 3 && argv[2].startsWith("--run-mode:")) configMilkio.milkioRunMode = process.argv[2].split("--run-mode:")[1];
|
|
44
|
-
|
|
45
|
-
if (MilkioAppOptions?.enableMaxRequestLimit && MilkioAppOptions.enableMaxRequestLimit >= 1) {
|
|
46
|
-
runtime.maxRequest.expected = MilkioAppOptions.enableMaxRequestLimit;
|
|
47
|
-
runtime.maxRequest.enable = true;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
35
|
if (MilkioAppOptions.enableMaxRunningTimeoutLimit && MilkioAppOptions.enableMaxRunningTimeoutLimit >= 1) {
|
|
51
36
|
setTimeout(() => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
runtime.maxRunningTimeout.enable = true;
|
|
37
|
+
throw new Error('Milkio reached the limit of "maxRunningTimeout" in the options and automatically exited.')
|
|
38
|
+
}, MilkioAppOptions.enableMaxRunningTimeoutLimit * 60 * 1000)
|
|
39
|
+
runtime.maxRunningTimeout.enable = true
|
|
56
40
|
}
|
|
57
41
|
|
|
58
42
|
const MilkioApp = {
|
|
@@ -60,54 +44,52 @@ export async function createMilkioApp(MilkioAppOptions: MilkioAppOptions = {}) {
|
|
|
60
44
|
executeToJson: _executeToJson,
|
|
61
45
|
_executeCore,
|
|
62
46
|
_executeCoreToJson
|
|
63
|
-
}
|
|
47
|
+
}
|
|
64
48
|
|
|
65
49
|
if (MilkioAppOptions.bootstraps) {
|
|
66
|
-
await Promise.all(MilkioAppOptions.bootstraps())
|
|
50
|
+
await Promise.all(MilkioAppOptions.bootstraps())
|
|
67
51
|
}
|
|
68
52
|
|
|
69
53
|
if (MilkioAppOptions.middlewares) {
|
|
70
|
-
MiddlewareEvent.define("bootstrap", (a, b) => a.index - b.index)
|
|
71
|
-
MiddlewareEvent.define("beforeExecute", (a, b) => a.index - b.index)
|
|
72
|
-
MiddlewareEvent.define("afterExecute", (a, b) => b.index - a.index)
|
|
73
|
-
MiddlewareEvent.define("afterHTTPRequest", (a, b) => a.index - b.index)
|
|
74
|
-
MiddlewareEvent.define("beforeHTTPResponse", (a, b) => b.index - a.index)
|
|
54
|
+
MiddlewareEvent.define("bootstrap", (a, b) => a.index - b.index)
|
|
55
|
+
MiddlewareEvent.define("beforeExecute", (a, b) => a.index - b.index)
|
|
56
|
+
MiddlewareEvent.define("afterExecute", (a, b) => b.index - a.index)
|
|
57
|
+
MiddlewareEvent.define("afterHTTPRequest", (a, b) => a.index - b.index)
|
|
58
|
+
MiddlewareEvent.define("beforeHTTPResponse", (a, b) => b.index - a.index)
|
|
75
59
|
|
|
76
|
-
const middlewares = MilkioAppOptions.middlewares()
|
|
60
|
+
const middlewares = MilkioAppOptions.middlewares()
|
|
77
61
|
|
|
78
62
|
for (let index = 0; index < middlewares.length; index++) {
|
|
79
|
-
const middlewareOptions = middlewares[index]
|
|
63
|
+
const middlewareOptions = middlewares[index]
|
|
80
64
|
for (const name in middlewareOptions) {
|
|
81
|
-
let middleware = _middlewares.get(name)
|
|
65
|
+
let middleware = _middlewares.get(name)
|
|
82
66
|
if (middleware === undefined) {
|
|
83
|
-
middleware = []
|
|
84
|
-
_middlewares.set(name, middleware)
|
|
67
|
+
middleware = []
|
|
68
|
+
_middlewares.set(name, middleware)
|
|
85
69
|
}
|
|
86
|
-
const id = createUlid()
|
|
87
|
-
middleware.push({ id, index, middleware: middlewareOptions[name] })
|
|
70
|
+
const id = createUlid()
|
|
71
|
+
middleware.push({ id, index, middleware: middlewareOptions[name] })
|
|
88
72
|
}
|
|
89
73
|
}
|
|
90
|
-
MiddlewareEvent._sort()
|
|
74
|
+
MiddlewareEvent._sort()
|
|
91
75
|
|
|
92
|
-
await MiddlewareEvent.handle("bootstrap", [MilkioApp])
|
|
76
|
+
await MiddlewareEvent.handle("bootstrap", [MilkioApp])
|
|
93
77
|
}
|
|
94
78
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return MilkioApp;
|
|
79
|
+
return MilkioApp
|
|
98
80
|
}
|
|
99
81
|
|
|
100
82
|
async function _execute<Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headersInit: Record<string, string> | Headers = {}, options?: ExecuteOptions): Promise<ExecuteResult<Result>> {
|
|
101
|
-
const executeId = (options?.executeId ?? createUlid()) as ExecuteId
|
|
102
|
-
const logger = useLogger(executeId)
|
|
103
|
-
runtime.execute.executeIds.add(executeId)
|
|
83
|
+
const executeId = (options?.executeId ?? createUlid()) as ExecuteId
|
|
84
|
+
const logger = useLogger(executeId)
|
|
85
|
+
runtime.execute.executeIds.add(executeId)
|
|
104
86
|
|
|
105
87
|
loggerPushTags(executeId, {
|
|
106
88
|
from: "execute",
|
|
107
89
|
executeId,
|
|
108
90
|
params,
|
|
109
91
|
path
|
|
110
|
-
})
|
|
92
|
+
})
|
|
111
93
|
|
|
112
94
|
const result: any = await _executeCore(path, params, headersInit, {
|
|
113
95
|
...options,
|
|
@@ -116,15 +98,15 @@ async function _execute<Path extends keyof (typeof schema)["apiMethodsTypeSchema
|
|
|
116
98
|
onAfterHeaders: (headers) => {
|
|
117
99
|
loggerPushTags(executeId, {
|
|
118
100
|
headers: headers.toJSON()
|
|
119
|
-
})
|
|
101
|
+
})
|
|
120
102
|
}
|
|
121
|
-
})
|
|
103
|
+
})
|
|
122
104
|
|
|
123
|
-
loggerPushTags(executeId, { result })
|
|
124
|
-
await loggerSubmit(executeId)
|
|
125
|
-
runtime.execute.executeIds.delete(executeId)
|
|
105
|
+
loggerPushTags(executeId, { result })
|
|
106
|
+
await loggerSubmit(executeId)
|
|
107
|
+
runtime.execute.executeIds.delete(executeId)
|
|
126
108
|
|
|
127
|
-
return result
|
|
109
|
+
return result
|
|
128
110
|
}
|
|
129
111
|
|
|
130
112
|
/**
|
|
@@ -133,17 +115,9 @@ async function _execute<Path extends keyof (typeof schema)["apiMethodsTypeSchema
|
|
|
133
115
|
* Both execute and httpServer essentially call executeCore.
|
|
134
116
|
*/
|
|
135
117
|
async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSchema"], Result extends Awaited<ReturnType<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>>>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headersInit: Record<string, string> | Headers = {}, options: ExecuteCoreOptions): Promise<ExecuteResult<Result>> {
|
|
136
|
-
const executeId = options.executeId as ExecuteId
|
|
137
|
-
|
|
138
|
-
params = TSON.decode(params);
|
|
118
|
+
const executeId = options.executeId as ExecuteId
|
|
139
119
|
|
|
140
|
-
|
|
141
|
-
if (runtime.maxRequest.counter >= runtime.maxRequest.expected) {
|
|
142
|
-
console.log("❌ Milkio reached the limit of 'maxRequest' in the options and automatically exited.");
|
|
143
|
-
exit(0);
|
|
144
|
-
}
|
|
145
|
-
runtime.maxRequest.counter++;
|
|
146
|
-
}
|
|
120
|
+
params = TSON.decode(params)
|
|
147
121
|
|
|
148
122
|
if (!(path in schema.apiMethodsSchema)) {
|
|
149
123
|
const result = {
|
|
@@ -154,22 +128,23 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
154
128
|
message: failCode.NOT_FOUND(),
|
|
155
129
|
data: undefined
|
|
156
130
|
}
|
|
157
|
-
} satisfies ExecuteResult<Result
|
|
131
|
+
} satisfies ExecuteResult<Result>
|
|
158
132
|
|
|
159
|
-
return result
|
|
133
|
+
return result
|
|
160
134
|
}
|
|
161
135
|
|
|
162
|
-
let headers: Headers
|
|
136
|
+
let headers: Headers
|
|
163
137
|
if (!(headersInit instanceof Headers)) {
|
|
138
|
+
// @ts-ignore
|
|
164
139
|
headers = new Headers({
|
|
165
140
|
...headersInit
|
|
166
|
-
})
|
|
141
|
+
})
|
|
167
142
|
} else {
|
|
168
|
-
headers = headersInit
|
|
143
|
+
headers = headersInit
|
|
169
144
|
}
|
|
170
145
|
|
|
171
146
|
if (options?.onAfterHeaders) {
|
|
172
|
-
await options.onAfterHeaders(headers)
|
|
147
|
+
await options.onAfterHeaders(headers)
|
|
173
148
|
}
|
|
174
149
|
|
|
175
150
|
const context: Context = {
|
|
@@ -178,61 +153,61 @@ async function _executeCore<Path extends keyof (typeof schema)["apiMethodsTypeSc
|
|
|
178
153
|
headers,
|
|
179
154
|
logger: options.logger,
|
|
180
155
|
detail: options?.detail ?? {}
|
|
181
|
-
}
|
|
156
|
+
}
|
|
182
157
|
|
|
183
|
-
let result: { value: Result }
|
|
158
|
+
let result: { value: Result }
|
|
184
159
|
try {
|
|
185
160
|
// before execute middleware
|
|
186
|
-
await MiddlewareEvent.handle("beforeExecute", [context])
|
|
161
|
+
await MiddlewareEvent.handle("beforeExecute", [context])
|
|
187
162
|
|
|
188
163
|
// check type
|
|
189
164
|
// @ts-ignore
|
|
190
165
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
191
|
-
_validate(await (await schema.apiValidator.validate[path]()).params(params))
|
|
166
|
+
_validate(await (await schema.apiValidator.validate[path]()).params(params))
|
|
192
167
|
|
|
193
168
|
// execute api
|
|
194
|
-
let api: any
|
|
195
|
-
if (apis.has(path)) api = apis.get(path)
|
|
169
|
+
let api: any
|
|
170
|
+
if (apis.has(path)) api = apis.get(path)
|
|
196
171
|
else {
|
|
197
172
|
// @ts-ignore
|
|
198
|
-
api = schema.apiMethodsSchema[path]()
|
|
199
|
-
apis.set(path, api)
|
|
173
|
+
api = schema.apiMethodsSchema[path]()
|
|
174
|
+
apis.set(path, api)
|
|
200
175
|
}
|
|
201
|
-
const apiModuleAwaited = await api.module
|
|
176
|
+
const apiModuleAwaited = await api.module
|
|
202
177
|
|
|
203
|
-
const apiMethod = apiModuleAwaited.api.action
|
|
178
|
+
const apiMethod = apiModuleAwaited.api.action
|
|
204
179
|
|
|
205
180
|
// @ts-ignore
|
|
206
|
-
result = { value: await apiMethod(params, context) }
|
|
181
|
+
result = { value: await apiMethod(params, context) }
|
|
207
182
|
|
|
208
183
|
// after execute middleware
|
|
209
|
-
await MiddlewareEvent.handle("afterExecute", [context, result])
|
|
184
|
+
await MiddlewareEvent.handle("afterExecute", [context, result])
|
|
210
185
|
} catch (error: any) {
|
|
211
|
-
const errorResult = hanldeCatchError(error, executeId)
|
|
186
|
+
const errorResult = hanldeCatchError(error, executeId)
|
|
212
187
|
|
|
213
|
-
return errorResult
|
|
188
|
+
return errorResult
|
|
214
189
|
}
|
|
215
190
|
|
|
216
191
|
return {
|
|
217
192
|
executeId,
|
|
218
193
|
success: true,
|
|
219
194
|
data: result.value
|
|
220
|
-
}
|
|
195
|
+
}
|
|
221
196
|
}
|
|
222
197
|
|
|
223
198
|
async function _executeToJson<Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headersInit: Record<string, string> | Headers = {}, options?: ExecuteOptions): Promise<string> {
|
|
224
|
-
const resultsRaw = await _execute(path, params, headersInit, options)
|
|
225
|
-
const results = await (await schema.apiValidator.validate[path]()).results(TSON.encode(resultsRaw))
|
|
226
|
-
return results
|
|
199
|
+
const resultsRaw = await _execute(path, params, headersInit, options)
|
|
200
|
+
const results = await (await schema.apiValidator.validate[path]()).results(TSON.encode(resultsRaw))
|
|
201
|
+
return results
|
|
227
202
|
}
|
|
228
203
|
|
|
229
204
|
async function _executeCoreToJson<Path extends keyof (typeof schema)["apiMethodsTypeSchema"]>(path: Path, params: Parameters<(typeof schema)["apiMethodsTypeSchema"][Path]["api"]["action"]>[0] | string, headersInit: Record<string, string> | Headers = {}, options: ExecuteCoreOptions): Promise<string> {
|
|
230
|
-
const resultsRaw = await _executeCore(path, params, headersInit, options)
|
|
231
|
-
const results = await (await schema.apiValidator.validate[path]()).results(TSON.encode(resultsRaw))
|
|
232
|
-
return results
|
|
205
|
+
const resultsRaw = await _executeCore(path, params, headersInit, options)
|
|
206
|
+
const results = await (await schema.apiValidator.validate[path]()).results(TSON.encode(resultsRaw))
|
|
207
|
+
return results
|
|
233
208
|
}
|
|
234
209
|
|
|
235
|
-
const apis = new Map<string, any>()
|
|
210
|
+
const apis = new Map<string, any>()
|
|
236
211
|
|
|
237
212
|
export type ExecuteResult<Result> = ExecuteResultSuccess<Result> | ExecuteResultFail;
|
|
238
213
|
|
package/kernel/runtime.ts
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
import { type ExecuteId } from ".."
|
|
1
|
+
import { type ExecuteId } from ".."
|
|
2
2
|
|
|
3
3
|
export const runtime = {
|
|
4
4
|
execute: {
|
|
5
5
|
executeIds: new Set<ExecuteId>()
|
|
6
6
|
},
|
|
7
|
-
maxRequest: {
|
|
8
|
-
enable: false,
|
|
9
|
-
counter: 0,
|
|
10
|
-
expected: 0
|
|
11
|
-
},
|
|
12
7
|
maxRunningTimeout: {
|
|
13
8
|
enable: false,
|
|
14
9
|
expectedEndedAt: 0
|
|
15
10
|
}
|
|
16
|
-
}
|
|
11
|
+
}
|
package/kernel/validate.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type { IValidation } from "typia"
|
|
2
|
-
import { reject } from "../kernel/fail"
|
|
1
|
+
import type { IValidation } from "typia"
|
|
2
|
+
import { reject } from "../kernel/fail"
|
|
3
3
|
|
|
4
4
|
export function _validate(validator: IValidation.IFailure | IValidation.ISuccess): void {
|
|
5
|
-
if (validator.success) return
|
|
6
|
-
const error = validator.errors[0]
|
|
5
|
+
if (validator.success) return
|
|
6
|
+
const error = validator.errors[0]
|
|
7
7
|
|
|
8
8
|
throw reject("TYPE_SAFE_ERROR", {
|
|
9
9
|
path: error.path,
|
|
10
10
|
expected: error.expected,
|
|
11
11
|
value: error.value
|
|
12
|
-
})
|
|
12
|
+
})
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
"name": "milkio",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"module": "index.ts",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.11",
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"typescript": "^5.4.2"
|
|
8
8
|
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"milkio": "./c.ts"
|
|
11
|
+
},
|
|
9
12
|
"dependencies": {
|
|
10
13
|
"@poech/camel-hump-under": "^1.1.0",
|
|
11
14
|
"@southern-aurora/tson": "2.0.2",
|