milkio 1.0.0-alpha.8 → 1.0.0-alpha.81
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 +2 -2
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/action/index.ts +14 -14
- package/bootstrap/index.ts +7 -0
- package/command/index.ts +41 -37
- package/config/index.ts +33 -0
- package/context/index.ts +22 -18
- package/event/index.ts +48 -0
- package/exception/index.ts +31 -27
- package/execute/execute-id-generator.ts +5 -5
- package/execute/index.ts +101 -135
- package/flow/index.ts +35 -0
- package/handler/index.ts +7 -0
- package/index.ts +19 -13
- package/listener/index.ts +181 -123
- package/logger/index.ts +52 -52
- package/meta/index.ts +2 -2
- package/package.json +7 -7
- package/step/index.ts +21 -21
- package/stream/index.ts +14 -14
- package/tsconfig.json +19 -18
- package/type-safety/index.ts +16 -0
- package/types/index.ts +40 -53
- package/utils/create-id.ts +10 -2
- package/utils/headers-to-json.ts +3 -3
- package/utils/send-cookbook-event.ts +17 -16
- package/world/index.ts +63 -55
- package/.publish/publish.json +0 -7
- package/.publish/releases/0.0.0.md +0 -0
- package/.publish/releases/0.1.0.md +0 -13
- package/.publish/releases/0.2.0.md +0 -33
- package/.publish/releases/0.3.0.md +0 -85
- package/.publish/releases/0.4.0.md +0 -35
- package/.publish/releases/0.5.0.md +0 -19
- package/.publish/releases/0.6.0.md +0 -41
- package/.publish/releases/0.8.0.md +0 -128
- package/.publish/releases-github/0.0.0.md +0 -0
- package/.publish/releases-github/0.1.0.md +0 -13
- package/.publish/releases-github/0.2.0.md +0 -33
- package/.publish/releases-github/0.3.0.md +0 -85
- package/.publish/releases-github/0.4.0.md +0 -35
- package/.publish/releases-github/0.5.0.md +0 -19
- package/.publish/releases-github/0.6.0.md +0 -41
- package/.publish/releases-github/0.8.0.md +0 -126
- package/events/index.ts +0 -47
package/.co.toml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
[
|
|
2
|
-
includes = ["co:bun"]
|
|
1
|
+
[general]
|
|
2
|
+
includes = [ "co:bun" ]
|
package/LICENSE
CHANGED
package/README.md
CHANGED
package/action/index.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { $context, $meta } from '..'
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
const action = init as unknown as Action<ActionInitT
|
|
5
|
-
action.$milkioType =
|
|
6
|
-
if (action.meta === undefined) action.meta = {}
|
|
7
|
-
return action
|
|
8
|
-
}
|
|
3
|
+
export function action<ActionInitT extends ActionInit>(init: ActionInitT): Action<ActionInitT> {
|
|
4
|
+
const action = init as unknown as Action<ActionInitT>
|
|
5
|
+
action.$milkioType = 'action'
|
|
6
|
+
if (action.meta === undefined) action.meta = {}
|
|
7
|
+
return action
|
|
8
|
+
}
|
|
9
9
|
|
|
10
10
|
export type ActionInit = {
|
|
11
|
-
meta?: $meta
|
|
12
|
-
handler: (context: $context, params: any) => Promise<unknown
|
|
13
|
-
}
|
|
11
|
+
meta?: $meta
|
|
12
|
+
handler: (context: $context, params: any) => Promise<unknown>
|
|
13
|
+
}
|
|
14
14
|
|
|
15
15
|
export type Action<ActionInitT extends ActionInit> = {
|
|
16
|
-
$milkioType:
|
|
17
|
-
meta: ActionInitT[
|
|
18
|
-
handler: ActionInitT[
|
|
19
|
-
}
|
|
16
|
+
$milkioType: 'action'
|
|
17
|
+
meta: ActionInitT['meta'] extends undefined ? {} : ActionInitT['meta']
|
|
18
|
+
handler: ActionInitT['handler']
|
|
19
|
+
}
|
package/command/index.ts
CHANGED
|
@@ -1,53 +1,57 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { GeneratedInit } from '..'
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
const command = init as unknown as Command<CommandInitT
|
|
5
|
-
command.$milkioType =
|
|
6
|
-
return command
|
|
7
|
-
}
|
|
3
|
+
export function command<CommandInitT extends CommandInit>(init: CommandInitT): Command<CommandInitT> {
|
|
4
|
+
const command = init as unknown as Command<CommandInitT>
|
|
5
|
+
command.$milkioType = 'command'
|
|
6
|
+
return command
|
|
7
|
+
}
|
|
8
8
|
|
|
9
|
-
export
|
|
10
|
-
handler: (commands: Array<string>, options: Record<string, string>) => Promise<unknown
|
|
11
|
-
}
|
|
9
|
+
export interface CommandInit {
|
|
10
|
+
handler: (commands: Array<string>, options: Record<string, string>) => Promise<unknown>
|
|
11
|
+
}
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
$milkioType:
|
|
15
|
-
handler: CommandInitT[
|
|
16
|
-
}
|
|
13
|
+
export interface Command<CommandInitT extends CommandInit> {
|
|
14
|
+
$milkioType: 'command'
|
|
15
|
+
handler: CommandInitT['handler']
|
|
16
|
+
}
|
|
17
17
|
|
|
18
|
-
export
|
|
18
|
+
export function __initCommander(generated: GeneratedInit, runtime: any) {
|
|
19
19
|
const commander = async (argv: Array<string>, options?: { onNotFound?: () => any }) => {
|
|
20
20
|
const params = {
|
|
21
|
-
path:
|
|
21
|
+
path: 'index',
|
|
22
22
|
commands: [] as Array<string>,
|
|
23
23
|
options: {} as Record<string, string | true>,
|
|
24
|
-
}
|
|
24
|
+
}
|
|
25
25
|
for (const v of argv.slice(3)) {
|
|
26
|
-
if (v.startsWith(
|
|
27
|
-
const vSplited = v.split(
|
|
28
|
-
params.options[vSplited[0].slice(2)] = vSplited.slice(1, vSplited.length).join(
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
params.options[
|
|
36
|
-
}
|
|
37
|
-
|
|
26
|
+
if (v.startsWith('--') && v.includes('=')) {
|
|
27
|
+
const vSplited = v.split('=')
|
|
28
|
+
params.options[vSplited[0].slice(2)] = vSplited.slice(1, vSplited.length).join('=')
|
|
29
|
+
}
|
|
30
|
+
else if (v.startsWith('--')) {
|
|
31
|
+
params.options[v.slice(2)] = '1'
|
|
32
|
+
}
|
|
33
|
+
else if (v.startsWith('-') && v.includes('=')) {
|
|
34
|
+
const vSplited = v.split('=')
|
|
35
|
+
params.options[vSplited[0].slice(1)] = vSplited.slice(1, vSplited.length).join('=')
|
|
36
|
+
}
|
|
37
|
+
else if (v.startsWith('-')) {
|
|
38
|
+
params.options[v.slice(1)] = '1'
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
params.commands.push(v)
|
|
38
42
|
}
|
|
39
43
|
}
|
|
40
|
-
if (argv.length === 2) params.path = `index
|
|
41
|
-
else params.path = `${argv[2] ??
|
|
44
|
+
if (argv.length === 2) params.path = `index`
|
|
45
|
+
else params.path = `${argv[2] ?? 'index'}`
|
|
42
46
|
|
|
43
47
|
if (!(params.path in generated.commandSchema.commands)) {
|
|
44
|
-
if (options?.onNotFound) return await options.onNotFound()
|
|
45
|
-
console.log(`\x1B[44m UwU \x1B[0m \x1B[2mCommand not found:\x1B[0m \x1B[32m${params.path}\x1B[0m`)
|
|
46
|
-
return undefined
|
|
48
|
+
if (options?.onNotFound) return await options.onNotFound()
|
|
49
|
+
console.log(`\x1B[44m UwU \x1B[0m \x1B[2mCommand not found:\x1B[0m \x1B[32m${params.path}\x1B[0m`)
|
|
50
|
+
return undefined
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
return await generated.commandSchema.commands[params.path].module.handler(params.commands, params.options)
|
|
50
|
-
}
|
|
53
|
+
return await generated.commandSchema.commands[params.path].module.handler(params.commands, params.options)
|
|
54
|
+
}
|
|
51
55
|
|
|
52
|
-
return commander
|
|
53
|
-
}
|
|
56
|
+
return commander
|
|
57
|
+
}
|
package/config/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function config<ConfigT extends Config>(config: ConfigT): ConfigT {
|
|
2
|
+
return config
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export type Config = (mode: string) => Promise<Record<string, unknown>> | Record<string, unknown>
|
|
6
|
+
|
|
7
|
+
export interface ConfigEnvironments<T extends Config> {
|
|
8
|
+
[key: string]: (env: Record<string, string>) => Partial<Awaited<ReturnType<T>>> | Promise<Partial<Awaited<ReturnType<T>>>>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function envToString(value: string | number | undefined, defaultValue: string) {
|
|
12
|
+
if (value === undefined) return defaultValue
|
|
13
|
+
|
|
14
|
+
return `${value}`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function envToNumber(value: string | undefined, defaultValue: number) {
|
|
18
|
+
if (value === undefined) return defaultValue
|
|
19
|
+
|
|
20
|
+
return Number.parseInt(value, 10)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function envToBoolean(value: string | number | undefined, defaultValue: boolean) {
|
|
24
|
+
if (value === 'true') return true
|
|
25
|
+
|
|
26
|
+
if (value === 'false') return false
|
|
27
|
+
|
|
28
|
+
if (value === '') return false
|
|
29
|
+
|
|
30
|
+
if (undefined === value) return defaultValue
|
|
31
|
+
|
|
32
|
+
return Boolean(value)
|
|
33
|
+
}
|
package/context/index.ts
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { MilkioHttpRequest, MilkioHttpResponse, $types, Logger, Action, MilkioRuntimeInit, MilkioInit } from '..'
|
|
2
2
|
|
|
3
3
|
export interface $context {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
_: MilkioRuntimeInit<MilkioInit>
|
|
5
|
+
develop: boolean
|
|
6
|
+
executeId: string
|
|
7
|
+
emit: MilkioRuntimeInit<MilkioInit>['emit']
|
|
8
|
+
path: string
|
|
9
|
+
logger: Logger
|
|
10
|
+
http: ContextHttp<Record<any, any>>
|
|
11
|
+
config: Readonly<Awaited<ReturnType<$types['configSchema']['get']>>>
|
|
12
|
+
call: <Module extends Promise<{ default: Action<any> }>>(module: Module, params: Parameters<Awaited<Module>['default']['handler']>[1]) => Promise<ReturnType<Awaited<Module>['default']['handler']>>
|
|
13
|
+
onFinally: (handler: () => void | Promise<void>) => void
|
|
10
14
|
}
|
|
11
15
|
|
|
12
|
-
export
|
|
13
|
-
url: URL
|
|
14
|
-
ip: string
|
|
15
|
-
path: { string: keyof $types[
|
|
16
|
+
export interface ContextHttp<ParamsParsed = any> {
|
|
17
|
+
url: URL
|
|
18
|
+
ip: string
|
|
19
|
+
path: { string: keyof $types['generated']['routeSchema'], array: Array<string> }
|
|
16
20
|
params: {
|
|
17
|
-
string: string
|
|
18
|
-
parsed: ParamsParsed
|
|
19
|
-
}
|
|
20
|
-
request: MilkioHttpRequest
|
|
21
|
-
response: MilkioHttpResponse
|
|
22
|
-
}
|
|
21
|
+
string: string
|
|
22
|
+
parsed: ParamsParsed
|
|
23
|
+
}
|
|
24
|
+
request: MilkioHttpRequest
|
|
25
|
+
response: MilkioHttpResponse
|
|
26
|
+
}
|
|
23
27
|
|
|
24
|
-
export type ContextCreatedHandler = (context: $context) => Promise<void> | void
|
|
28
|
+
export type ContextCreatedHandler = (context: $context) => Promise<void> | void
|
package/event/index.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { $context, ContextHttp, Results, Logger, $meta } from '..'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export interface $events {
|
|
5
|
+
'milkio:httpRequest': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>> }
|
|
6
|
+
'milkio:httpResponse': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>>, context: $context }
|
|
7
|
+
'milkio:httpNotFound': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>> }
|
|
8
|
+
'milkio:executeBefore': { executeId: string, path: string, logger: Logger, meta: $meta, context: $context }
|
|
9
|
+
'milkio:executeAfter': { executeId: string, path: string, logger: Logger, meta: $meta, context: $context, results: Results<any> }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function __initEventManager() {
|
|
13
|
+
const handlers = new Map<(event: any) => void, string>()
|
|
14
|
+
const indexed = new Map<string, Set<(event: any) => void>>()
|
|
15
|
+
|
|
16
|
+
const eventManager = {
|
|
17
|
+
on: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
|
|
18
|
+
handlers.set(handler, key as string)
|
|
19
|
+
if (indexed.has(key as string) === false) {
|
|
20
|
+
indexed.set(key as string, new Set())
|
|
21
|
+
}
|
|
22
|
+
const set = indexed.get(key as string)!
|
|
23
|
+
set.add(handler)
|
|
24
|
+
handlers.set(handler, key as string)
|
|
25
|
+
|
|
26
|
+
return () => {
|
|
27
|
+
handlers.delete(handler)
|
|
28
|
+
set.delete(handler)
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
off: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
|
|
32
|
+
const set = indexed.get(key as string)
|
|
33
|
+
if (!set) return
|
|
34
|
+
handlers.delete(handler)
|
|
35
|
+
set.delete(handler)
|
|
36
|
+
},
|
|
37
|
+
emit: async <Key extends keyof $events, Value extends $events[Key]>(key: Key, value: Value): Promise<void> => {
|
|
38
|
+
const h = indexed.get(key as string)
|
|
39
|
+
if (h) {
|
|
40
|
+
for (const handler of h) {
|
|
41
|
+
await handler(value)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return eventManager
|
|
48
|
+
}
|
package/exception/index.ts
CHANGED
|
@@ -1,44 +1,48 @@
|
|
|
1
|
-
import { TSON } from
|
|
2
|
-
import {
|
|
1
|
+
import { TSON } from '@southern-aurora/tson'
|
|
2
|
+
import type { MilkioResponseReject, Logger } from '..'
|
|
3
3
|
|
|
4
4
|
export interface $rejectCode {
|
|
5
|
-
FAIL: string
|
|
6
|
-
REQUEST_FAIL: any
|
|
7
|
-
NOT_DEVELOP_MODE: string
|
|
8
|
-
REQUEST_TIMEOUT: { timeout: number
|
|
9
|
-
NOT_FOUND: { path: string }
|
|
10
|
-
PARAMS_TYPE_INCORRECT: { path: string
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
FAIL: string
|
|
6
|
+
REQUEST_FAIL: any
|
|
7
|
+
NOT_DEVELOP_MODE: string
|
|
8
|
+
REQUEST_TIMEOUT: { timeout: number, message: string }
|
|
9
|
+
NOT_FOUND: { path: string }
|
|
10
|
+
PARAMS_TYPE_INCORRECT: { path: string, expected: string, value: any, message: string } | null
|
|
11
|
+
RESULTS_TYPE_INCORRECT: { path: string, expected: string, value: any, message: string } | null
|
|
12
|
+
UNACCEPTABLE: { expected: string, message: string }
|
|
13
|
+
PARAMS_TYPE_NOT_SUPPORTED: { expected: string }
|
|
14
|
+
RESULTS_TYPE_NOT_SUPPORTED: { expected: string }
|
|
15
|
+
INTERNAL_SERVER_ERROR: undefined
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
export function reject<Code extends keyof $rejectCode, RejectData extends $rejectCode[Code]>(code: Code, data: RejectData): MilkioRejectError<Code, RejectData> {
|
|
17
|
-
const error = { $milkioReject: true, code
|
|
18
|
-
Error.captureStackTrace(error)
|
|
19
|
-
return error
|
|
19
|
+
const error = { $milkioReject: true, code, data } as MilkioRejectError<Code, RejectData>
|
|
20
|
+
Error.captureStackTrace(error)
|
|
21
|
+
return error
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
export type MilkioRejectError<Code extends keyof $rejectCode = keyof $rejectCode, RejectData extends $rejectCode[Code] = $rejectCode[Code]> = { code: Code
|
|
24
|
+
export type MilkioRejectError<Code extends keyof $rejectCode = keyof $rejectCode, RejectData extends $rejectCode[Code] = $rejectCode[Code]> = { code: Code, data: RejectData, stack: string, $milkioReject: true }
|
|
23
25
|
|
|
24
26
|
export function exceptionHandler(executeId: string, logger: Logger, error: MilkioRejectError<any, any> | any): MilkioResponseReject {
|
|
25
|
-
const name = error?.code ?? error?.name ?? error?.constructor?.name ??
|
|
27
|
+
const name = error?.code ?? error?.name ?? error?.constructor?.name ?? 'Unnamed Exception'
|
|
26
28
|
|
|
27
|
-
if (error?.$milkioReject === true && error?.code ===
|
|
28
|
-
logger.info(name, error?.data?.path ??
|
|
29
|
-
}
|
|
29
|
+
if (error?.$milkioReject === true && error?.code === 'NOT_FOUND') {
|
|
30
|
+
logger.info(name, error?.data?.path ?? 'Unknown path')
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
30
33
|
try {
|
|
31
|
-
const stack = error?.$milkioReject ? (error?.stack ??
|
|
32
|
-
logger.error(name, `\n${TSON.stringify(error?.data)}`, `\n${stack}\n`)
|
|
33
|
-
}
|
|
34
|
-
|
|
34
|
+
const stack = error?.$milkioReject ? (error?.stack ?? '').split('\n').slice(2).join('\n') : error?.stack ?? ''
|
|
35
|
+
logger.error(name, `\n${TSON.stringify(error?.data)}`, `\n${stack}\n`)
|
|
36
|
+
}
|
|
37
|
+
catch (_) {
|
|
38
|
+
logger.error(name, `\n${error?.toString()}`, `\n${error?.stack}\n`)
|
|
35
39
|
}
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
let result: MilkioResponseReject
|
|
42
|
+
let result: MilkioResponseReject
|
|
39
43
|
|
|
40
|
-
if (error?.$milkioReject === true) result = { success: false, code: error.code, reject: error.data, executeId }
|
|
41
|
-
else result = { success: false, code:
|
|
44
|
+
if (error?.$milkioReject === true) result = { success: false, code: error.code, reject: error.data, executeId }
|
|
45
|
+
else result = { success: false, code: 'INTERNAL_SERVER_ERROR', reject: undefined, executeId }
|
|
42
46
|
|
|
43
|
-
return result
|
|
47
|
+
return result
|
|
44
48
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { createId } from
|
|
1
|
+
import { createId } from '../utils/create-id'
|
|
2
2
|
|
|
3
|
-
export type ExecuteIdGenerator = (request?: Request) => string | Promise<string
|
|
3
|
+
export type ExecuteIdGenerator = (request?: Request) => string | Promise<string>
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
return createId
|
|
7
|
-
}
|
|
5
|
+
export function defineDefaultExecuteIdGenerator() {
|
|
6
|
+
return createId
|
|
7
|
+
}
|