milkio 1.0.0-alpha.13 → 1.0.0-alpha.15
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/context/index.ts +2 -2
- package/execute/index.ts +15 -81
- package/index.ts +0 -1
- package/listener/index.ts +7 -2
- package/package.json +2 -2
- package/types/index.ts +0 -15
- package/world/index.ts +1 -8
- package/use/index.ts +0 -18
package/context/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type MilkioHttpRequest, type MilkioHttpResponse, type $types, type Logger, type Steps, type
|
|
1
|
+
import { type MilkioHttpRequest, type MilkioHttpResponse, type $types, type Logger, type Steps, type Mixin, type ExecuteOptions, type Action } from "..";
|
|
2
2
|
|
|
3
3
|
export interface $context {
|
|
4
4
|
executeId: string;
|
|
@@ -6,7 +6,7 @@ export interface $context {
|
|
|
6
6
|
logger: Logger;
|
|
7
7
|
http?: ContextHttp<Record<any, any>>;
|
|
8
8
|
step: Steps<{}>["step"];
|
|
9
|
-
|
|
9
|
+
call: <Module extends Promise<{ default: Action<any> }>>(module: Module, params: Parameters<Awaited<Module>["default"]["handler"]>[1]) => Promise<ReturnType<Awaited<Module>["default"]["handler"]>>;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export type ContextHttp<ParamsParsed = any> = {
|
package/execute/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { reject, type $context, type $meta, type ExecuteOptions, type Logger, ty
|
|
|
5
5
|
import { headersToJSON } from "../utils/headers-to-json";
|
|
6
6
|
|
|
7
7
|
export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRuntimeInit<MilkioInit>> = MilkioRuntimeInit<MilkioInit>>(generated: GeneratedInit, runtime: MilkioRuntime) => {
|
|
8
|
-
const
|
|
8
|
+
const __execute = async (
|
|
9
9
|
routeSchema: any,
|
|
10
10
|
options: {
|
|
11
11
|
createdExecuteId: string;
|
|
@@ -13,7 +13,6 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
13
13
|
path: string;
|
|
14
14
|
headers: Record<string, string> | Headers;
|
|
15
15
|
mixinContext: Record<any, any> | undefined;
|
|
16
|
-
extendContext?: $context;
|
|
17
16
|
} & (
|
|
18
17
|
| {
|
|
19
18
|
params: Record<any, any>;
|
|
@@ -61,16 +60,14 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
61
60
|
params = { ...paramsRand, ...params };
|
|
62
61
|
}
|
|
63
62
|
if (options.mixinContext?.http?.params?.string) options.mixinContext.http.params.parsed = params; // listen でパースしたパラメータを渡す
|
|
64
|
-
const context =
|
|
65
|
-
options.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
step: createStep(),
|
|
73
|
-
} as unknown as $context);
|
|
63
|
+
const context = {
|
|
64
|
+
...(options.mixinContext ? options.mixinContext : {}),
|
|
65
|
+
path: options.path,
|
|
66
|
+
logger: options.createdLogger,
|
|
67
|
+
executeId: options.createdExecuteId,
|
|
68
|
+
call: (module: any, options: any) => __call(context, module, options),
|
|
69
|
+
step: createStep(),
|
|
70
|
+
} as unknown as $context;
|
|
74
71
|
const results: Results<unknown> = { value: undefined };
|
|
75
72
|
|
|
76
73
|
if (runtime.develop) {
|
|
@@ -85,85 +82,22 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
85
82
|
if (!validation.success) throw reject("PARAMS_TYPE_INCORRECT", { ...validation.errors[0], message: `The value '${validation.errors[0].path}' is '${validation.errors[0].value}', which does not meet '${validation.errors[0].expected}' requirements.` });
|
|
86
83
|
}
|
|
87
84
|
|
|
88
|
-
|
|
85
|
+
await runtime.emit("milkio:executeBefore", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context });
|
|
89
86
|
|
|
90
87
|
results.value = await module.default.handler(context, params);
|
|
91
88
|
|
|
92
|
-
|
|
89
|
+
await runtime.emit("milkio:executeAfter", { executeId: options.createdExecuteId, logger: options.createdLogger, path: options.path, context, results });
|
|
93
90
|
|
|
94
91
|
return { executeId, headers, params, results, context, meta, type: module.$milkioType };
|
|
95
92
|
};
|
|
96
93
|
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const executeId = context?.executeId ?? createId();
|
|
101
|
-
const logger = context?.logger ?? createLogger(runtime, path, executeId);
|
|
102
|
-
if (!context?.logger) runtime.runtime.request.set(executeId, { logger: logger });
|
|
103
|
-
|
|
104
|
-
try {
|
|
105
|
-
const routeSchema = generated.routeSchema.routes.get(path);
|
|
106
|
-
if (routeSchema === undefined) {
|
|
107
|
-
throw reject("NOT_FOUND", { path: path });
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const executed = await __call(routeSchema, {
|
|
111
|
-
createdExecuteId: executeId,
|
|
112
|
-
createdLogger: logger,
|
|
113
|
-
path: path,
|
|
114
|
-
headers: options.headers ?? {},
|
|
115
|
-
mixinContext: {},
|
|
116
|
-
params: options.params ?? {},
|
|
117
|
-
paramsType: "raw",
|
|
118
|
-
extendContext: context,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
if (routeSchema.type === "stream") {
|
|
122
|
-
// stream
|
|
123
|
-
return [
|
|
124
|
-
undefined,
|
|
125
|
-
(async function* () {
|
|
126
|
-
try {
|
|
127
|
-
for await (const result of executed.results.value) {
|
|
128
|
-
yield [null, result];
|
|
129
|
-
}
|
|
130
|
-
return undefined;
|
|
131
|
-
} catch (error) {
|
|
132
|
-
const reject = exceptionHandler(executeId, logger, error);
|
|
133
|
-
const result: any = {};
|
|
134
|
-
result[reject.code] = reject.reject;
|
|
135
|
-
|
|
136
|
-
yield [result, null];
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
})(),
|
|
140
|
-
{ executeId: executeId },
|
|
141
|
-
];
|
|
142
|
-
} else {
|
|
143
|
-
// action
|
|
144
|
-
return [null, executed.results.value, { executeId: executeId }];
|
|
145
|
-
}
|
|
146
|
-
} catch (error) {
|
|
147
|
-
const reject = exceptionHandler(executeId, logger, error);
|
|
148
|
-
const result: any = {};
|
|
149
|
-
result[reject.code] = reject.reject;
|
|
150
|
-
|
|
151
|
-
return [result, null, { executeId: executeId }];
|
|
152
|
-
}
|
|
94
|
+
const __call = async (context: $context, module: { default: any }, params?: any): Promise<any> => {
|
|
95
|
+
const moduleAwaited = await module;
|
|
96
|
+
return await moduleAwaited.default.handler(context, params);
|
|
153
97
|
};
|
|
154
98
|
|
|
155
|
-
const ping = async (): Promise<Ping> => [
|
|
156
|
-
null,
|
|
157
|
-
{
|
|
158
|
-
connect: true,
|
|
159
|
-
delay: 0,
|
|
160
|
-
serverTimestamp: Date.now(),
|
|
161
|
-
},
|
|
162
|
-
];
|
|
163
|
-
|
|
164
99
|
return {
|
|
165
100
|
__call,
|
|
166
|
-
|
|
167
|
-
ping,
|
|
101
|
+
__execute,
|
|
168
102
|
};
|
|
169
103
|
};
|
package/index.ts
CHANGED
package/listener/index.ts
CHANGED
|
@@ -80,6 +80,11 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
80
80
|
|
|
81
81
|
await runtime.emit("milkio:httpRequest", { executeId, logger, path: http.path.string as string, http });
|
|
82
82
|
|
|
83
|
+
if (!runtime.develop && (http.path.string as string).includes("#")) {
|
|
84
|
+
await runtime.emit("milkio:httpNotFound", { executeId, logger, path: http.path.string as string, http });
|
|
85
|
+
throw reject("NOT_FOUND", { path: http.path.string as string });
|
|
86
|
+
}
|
|
87
|
+
|
|
83
88
|
if (!request.headers.get("Accept")?.startsWith("text/event-stream")) {
|
|
84
89
|
// action
|
|
85
90
|
const routeSchema = generated.routeSchema.routes.get(http.path.string);
|
|
@@ -89,7 +94,7 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
89
94
|
}
|
|
90
95
|
if (routeSchema.type !== "action") throw reject("UNACCEPTABLE", { expected: "stream", message: `Not acceptable, the Accept in the request header should be "text/event-stream". If you are using the "@milkio/stargate" package, please add \`type: "stream"\` to the execute options.` });
|
|
91
96
|
|
|
92
|
-
const executed = await executer.
|
|
97
|
+
const executed = await executer.__execute(routeSchema, {
|
|
93
98
|
createdExecuteId: executeId,
|
|
94
99
|
createdLogger: logger,
|
|
95
100
|
path: http.path.string as string,
|
|
@@ -111,7 +116,7 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
111
116
|
if (routeSchema === undefined) throw reject("NOT_FOUND", { path: http.path.string as string });
|
|
112
117
|
if (routeSchema.type !== "stream") throw reject("UNACCEPTABLE", { expected: "stream", message: `Not acceptable, the Accept in the request header should be "application/json". If you are using the "@milkio/stargate" package, please remove \`type: "stream"\` to the execute options.` });
|
|
113
118
|
|
|
114
|
-
const executed = await executer.
|
|
119
|
+
const executed = await executer.__execute(routeSchema, {
|
|
115
120
|
createdExecuteId: executeId,
|
|
116
121
|
createdLogger: logger,
|
|
117
122
|
path: http.path.string as string,
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "milkio",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"module": "index.ts",
|
|
5
|
-
"version": "1.0.0-alpha.
|
|
5
|
+
"version": "1.0.0-alpha.15",
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"typescript": "^5.4.2"
|
|
8
8
|
},
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@paralleldrive/cuid2": "^2.2.2",
|
|
12
12
|
"@poech/camel-hump-under": "^1.1.0",
|
|
13
|
-
"@southern-aurora/tson": "
|
|
13
|
+
"@southern-aurora/tson": "*",
|
|
14
14
|
"chalk": "^5.3.0",
|
|
15
15
|
"date-fns": "^4.1.0",
|
|
16
16
|
"typia": "6.9.0"
|
package/types/index.ts
CHANGED
|
@@ -56,21 +56,6 @@ export type Ping =
|
|
|
56
56
|
},
|
|
57
57
|
];
|
|
58
58
|
|
|
59
|
-
export type Execute = <Path extends keyof $types["generated"]["routeSchema"]["$types"]>(
|
|
60
|
-
path: Path,
|
|
61
|
-
options?: Mixin<
|
|
62
|
-
ExecuteOptions,
|
|
63
|
-
{
|
|
64
|
-
headers?: Record<string, string>;
|
|
65
|
-
params?: $types["generated"]["routeSchema"]["$types"][Path]["params"];
|
|
66
|
-
}
|
|
67
|
-
>,
|
|
68
|
-
) => $types["generated"]["routeSchema"]["$types"][Path]["🐣"] extends boolean
|
|
69
|
-
? // action
|
|
70
|
-
Promise<[Partial<$rejectCode>, null, ExecuteResultsOption] | [null, ExecuteActionResults<Path>, ExecuteResultsOption]>
|
|
71
|
-
: // stream
|
|
72
|
-
Promise<[Partial<$rejectCode>, null, ExecuteResultsOption] | [null, AsyncGenerator<[Partial<$rejectCode>, null] | [null, ExecuteStreamResults<Path>], null>, ExecuteResultsOption]>;
|
|
73
|
-
|
|
74
59
|
export type ExecuteResultsOption = { executeId: string };
|
|
75
60
|
|
|
76
61
|
export type ExecuteActionResults<Path extends keyof Generated["routeSchema"]["$types"], Generated extends $types["generated"] = $types["generated"]> = Generated["routeSchema"]["$types"][Path]["result"];
|
package/world/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { __initCommander, __initListener, __initExecuter, __initEventManager, type ExecuteId, type Logger, type Mixin, type GeneratedInit, type
|
|
1
|
+
import { __initCommander, __initListener, __initExecuter, __initEventManager, type ExecuteId, type Logger, type Mixin, type GeneratedInit, type Ping, type LoggerSubmittingHandler, type LoggerInsertingHandler, type $context } from "..";
|
|
2
2
|
import { defineDefaultExecuteIdGenerator } from "../execute/execute-id-generator";
|
|
3
3
|
|
|
4
4
|
export type MilkioInit = {
|
|
@@ -62,10 +62,6 @@ export const createWorld = async <MilkioOptions extends MilkioInit>(generated: G
|
|
|
62
62
|
on: eventManager.on,
|
|
63
63
|
off: eventManager.off,
|
|
64
64
|
emit: eventManager.emit,
|
|
65
|
-
// executer
|
|
66
|
-
_executer: executer,
|
|
67
|
-
execute: executer.execute,
|
|
68
|
-
ping: executer.ping,
|
|
69
65
|
// commander
|
|
70
66
|
commander,
|
|
71
67
|
// listener
|
|
@@ -83,9 +79,6 @@ export type MilkioWorld<MilkioOptions extends MilkioInit = MilkioInit> = {
|
|
|
83
79
|
on: Awaited<ReturnType<typeof __initEventManager>>["on"];
|
|
84
80
|
off: Awaited<ReturnType<typeof __initEventManager>>["off"];
|
|
85
81
|
emit: Awaited<ReturnType<typeof __initEventManager>>["emit"];
|
|
86
|
-
// executer
|
|
87
|
-
_executer: Awaited<ReturnType<typeof __initExecuter<MilkioRuntimeInit<MilkioOptions>>>>;
|
|
88
|
-
execute: Execute;
|
|
89
82
|
ping: (options?: { timeout?: number }) => Promise<Ping>;
|
|
90
83
|
// commander
|
|
91
84
|
commander: Awaited<ReturnType<typeof __initCommander<MilkioRuntimeInit<MilkioOptions>>>>;
|
package/use/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { $context } from "../context";
|
|
2
|
-
|
|
3
|
-
export const defineUse = <Mode extends "dynamic" | "static", Use extends Mode extends "static" ? () => Promise<unknown> | unknown : (...args: [$context, ...Array<unknown>]) => Promise<unknown> | unknown>(mode: Mode, use: Use): ((...args: Parameters<Use>) => Promise<Awaited<ReturnType<Use>>>) => {
|
|
4
|
-
if (mode === "static") {
|
|
5
|
-
let useCreated: any | undefined;
|
|
6
|
-
|
|
7
|
-
const getUse = async () => {
|
|
8
|
-
if (useCreated === undefined) {
|
|
9
|
-
useCreated = await (use as any)();
|
|
10
|
-
}
|
|
11
|
-
return useCreated;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
return getUse;
|
|
15
|
-
} else {
|
|
16
|
-
return use as any;
|
|
17
|
-
}
|
|
18
|
-
};
|