milkio 1.0.0-alpha.16 → 1.0.0-alpha.18
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/config/index.ts +48 -0
- package/context/index.ts +2 -0
- package/execute/index.ts +8 -3
- package/index.ts +2 -0
- package/listener/index.ts +19 -15
- package/package.json +1 -1
- package/types/index.ts +1 -0
package/config/index.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type $types, type GeneratedInit } from "../types";
|
|
2
|
+
|
|
3
|
+
export const config = <ConfigDefaultT extends ConfigDefault, ConfigEnvironmentsT extends ConfigEnvironments<ConfigDefaultT>>(def: ConfigDefaultT, envs: ConfigEnvironmentsT = {} as Record<any, any>): [ConfigDefaultT, ConfigEnvironmentsT] => {
|
|
4
|
+
return [def, envs];
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const getConfig = async <Namespace extends keyof $types["generated"]["configSchema"]>(generated: GeneratedInit, env: Record<any, any>, envMode: string, namespace: Namespace) => {
|
|
8
|
+
if (generated.configSchema[namespace][1] && envMode in generated.configSchema[namespace][1]) {
|
|
9
|
+
return {
|
|
10
|
+
...(await generated.configSchema[namespace][0](env)),
|
|
11
|
+
...(await generated.configSchema[namespace][1][envMode](env)),
|
|
12
|
+
};
|
|
13
|
+
} else {
|
|
14
|
+
return {
|
|
15
|
+
...(await generated.configSchema[namespace][0](env)),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ConfigDefault = (env: Record<string, string>) => Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
21
|
+
|
|
22
|
+
export type ConfigEnvironments<T extends ConfigDefault> = {
|
|
23
|
+
[key: string]: (env: Record<string, string>) => Partial<Awaited<ReturnType<T>>> | Promise<Partial<Awaited<ReturnType<T>>>>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function envToString(value: string | number | undefined, defaultValue: string) {
|
|
27
|
+
if (value === undefined) return defaultValue;
|
|
28
|
+
|
|
29
|
+
return `${value}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function envToNumber(value: string | undefined, defaultValue: number) {
|
|
33
|
+
if (value === undefined) return defaultValue;
|
|
34
|
+
|
|
35
|
+
return Number.parseInt(value, 10);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function envToBoolean(value: string | number | undefined, defaultValue: boolean) {
|
|
39
|
+
if (value === "true") return true;
|
|
40
|
+
|
|
41
|
+
if (value === "false") return false;
|
|
42
|
+
|
|
43
|
+
if (value === "") return false;
|
|
44
|
+
|
|
45
|
+
if (undefined === value) return defaultValue;
|
|
46
|
+
|
|
47
|
+
return Boolean(value);
|
|
48
|
+
}
|
package/context/index.ts
CHANGED
|
@@ -2,10 +2,12 @@ import { type MilkioHttpRequest, type MilkioHttpResponse, type $types, type Logg
|
|
|
2
2
|
|
|
3
3
|
export interface $context {
|
|
4
4
|
executeId: string;
|
|
5
|
+
environment: string;
|
|
5
6
|
path: string;
|
|
6
7
|
logger: Logger;
|
|
7
8
|
http?: ContextHttp<Record<any, any>>;
|
|
8
9
|
step: Steps<{}>["step"];
|
|
10
|
+
getConfig: <Namespace extends keyof $types["generated"]["configSchema"]>(namespace: Namespace) => Promise<Readonly<Awaited<ReturnType<$types["generated"]["configSchema"][Namespace][0]>>>>;
|
|
9
11
|
call: <Module extends Promise<{ default: Action<any> }>>(module: Module, params: Parameters<Awaited<Module>["default"]["handler"]>[1]) => Promise<ReturnType<Awaited<Module>["default"]["handler"]>>;
|
|
10
12
|
}
|
|
11
13
|
|
package/execute/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { type IValidation } from "typia";
|
|
2
2
|
import { TSON } from "@southern-aurora/tson";
|
|
3
|
-
import {
|
|
4
|
-
import { reject, type $context, type $meta, type ExecuteOptions, type Logger, type MilkioRuntimeInit, type Results, type GeneratedInit, type MilkioInit, createLogger, exceptionHandler, Ping, createStep } from "..";
|
|
3
|
+
import { reject, type $context, type $meta, type Logger, type MilkioRuntimeInit, type Results, type GeneratedInit, type MilkioInit, createStep, getConfig } from "..";
|
|
5
4
|
import { headersToJSON } from "../utils/headers-to-json";
|
|
6
5
|
|
|
7
6
|
export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRuntimeInit<MilkioInit>> = MilkioRuntimeInit<MilkioInit>>(generated: GeneratedInit, runtime: MilkioRuntime) => {
|
|
8
7
|
const __execute = async (
|
|
8
|
+
env: Record<any, any> | undefined,
|
|
9
|
+
envMode: string | undefined,
|
|
9
10
|
routeSchema: any,
|
|
10
11
|
options: {
|
|
11
12
|
createdExecuteId: string;
|
|
@@ -24,7 +25,9 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
24
25
|
}
|
|
25
26
|
),
|
|
26
27
|
): Promise<{ executeId: string; headers: Headers; params: Record<any, unknown>; results: Results<any>; context: $context; meta: Readonly<$meta>; type: "action" | "stream" }> => {
|
|
27
|
-
const executeId = options.createdExecuteId;
|
|
28
|
+
const executeId: string = options.createdExecuteId;
|
|
29
|
+
env = env ?? {};
|
|
30
|
+
envMode = envMode ?? "production";
|
|
28
31
|
let headers: Headers;
|
|
29
32
|
if (!(options.headers instanceof Headers)) {
|
|
30
33
|
// @ts-ignore
|
|
@@ -62,9 +65,11 @@ export const __initExecuter = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
62
65
|
if (options.mixinContext?.http?.params?.string) options.mixinContext.http.params.parsed = params; // listen でパースしたパラメータを渡す
|
|
63
66
|
const context = {
|
|
64
67
|
...(options.mixinContext ? options.mixinContext : {}),
|
|
68
|
+
envMode,
|
|
65
69
|
path: options.path,
|
|
66
70
|
logger: options.createdLogger,
|
|
67
71
|
executeId: options.createdExecuteId,
|
|
72
|
+
getConfig: (namespace: string) => getConfig(generated, env, envMode, namespace),
|
|
68
73
|
call: (module: any, options: any) => __call(context, module, options),
|
|
69
74
|
step: createStep(),
|
|
70
75
|
} as unknown as $context;
|
package/index.ts
CHANGED
package/listener/index.ts
CHANGED
|
@@ -16,8 +16,12 @@ export type MilkioHttpResponse = Mixin<
|
|
|
16
16
|
|
|
17
17
|
export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRuntimeInit<MilkioInit>> = MilkioRuntimeInit<MilkioInit>>(generated: GeneratedInit, runtime: MilkioRuntime, executer: ReturnType<typeof __initExecuter>) => {
|
|
18
18
|
const port = runtime.port;
|
|
19
|
-
const fetch = async (
|
|
20
|
-
|
|
19
|
+
const fetch = async (options: {
|
|
20
|
+
request: MilkioHttpRequest;
|
|
21
|
+
envMode?: string;
|
|
22
|
+
env?: Record<any, any>;
|
|
23
|
+
}): Promise<Response> => {
|
|
24
|
+
if (options.request.method === "OPTIONS") {
|
|
21
25
|
return new Response(undefined, {
|
|
22
26
|
headers: {
|
|
23
27
|
"Access-Control-Allow-Methods": runtime.cors?.corsAllowMethods ?? "*",
|
|
@@ -27,7 +31,7 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
27
31
|
});
|
|
28
32
|
}
|
|
29
33
|
|
|
30
|
-
if (request.url.endsWith("/generate_204")) {
|
|
34
|
+
if (options.request.url.endsWith("/generate_204")) {
|
|
31
35
|
return new Response("", {
|
|
32
36
|
status: 204,
|
|
33
37
|
headers: {
|
|
@@ -40,12 +44,12 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
40
44
|
});
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
const url = new URL(request.url);
|
|
47
|
+
const url = new URL(options.request.url);
|
|
44
48
|
let pathArray = url.pathname.substring(1).split("/");
|
|
45
49
|
if (runtime.ignorePathLevel !== undefined && runtime.ignorePathLevel !== 0) pathArray = pathArray.slice(runtime.ignorePathLevel);
|
|
46
50
|
const pathString = `/${pathArray.join("/")}`;
|
|
47
51
|
|
|
48
|
-
const executeId = runtime?.executeId ? await runtime.executeId(request) : createId();
|
|
52
|
+
const executeId = runtime?.executeId ? await runtime.executeId(options.request) : createId();
|
|
49
53
|
const logger = createLogger(runtime, pathString, executeId);
|
|
50
54
|
runtime.runtime.request.set(executeId, { logger: logger });
|
|
51
55
|
const response: MilkioHttpResponse = {
|
|
@@ -62,9 +66,9 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
62
66
|
|
|
63
67
|
try {
|
|
64
68
|
const http = (await (async () => {
|
|
65
|
-
const ip = runtime.realIp ? runtime.realIp(request) : "::1";
|
|
69
|
+
const ip = runtime.realIp ? runtime.realIp(options.request) : "::1";
|
|
66
70
|
const params = {
|
|
67
|
-
string: await request.text(),
|
|
71
|
+
string: await options.request.text(),
|
|
68
72
|
parsed: undefined,
|
|
69
73
|
};
|
|
70
74
|
|
|
@@ -73,7 +77,7 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
73
77
|
ip,
|
|
74
78
|
path: { string: pathString as keyof $types["generated"]["routeSchema"]["$types"], array: pathArray },
|
|
75
79
|
params,
|
|
76
|
-
request,
|
|
80
|
+
request: options.request,
|
|
77
81
|
response,
|
|
78
82
|
} as ContextHttp;
|
|
79
83
|
})())!;
|
|
@@ -85,7 +89,7 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
85
89
|
throw reject("NOT_FOUND", { path: http.path.string as string });
|
|
86
90
|
}
|
|
87
91
|
|
|
88
|
-
if (!request.headers.get("Accept")?.startsWith("text/event-stream")) {
|
|
92
|
+
if (!options.request.headers.get("Accept")?.startsWith("text/event-stream")) {
|
|
89
93
|
// action
|
|
90
94
|
const routeSchema = generated.routeSchema.routes.get(http.path.string);
|
|
91
95
|
if (routeSchema === undefined) {
|
|
@@ -94,11 +98,11 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
94
98
|
}
|
|
95
99
|
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.` });
|
|
96
100
|
|
|
97
|
-
const executed = await executer.__execute(routeSchema, {
|
|
101
|
+
const executed = await executer.__execute(options.env, options.envMode, routeSchema, {
|
|
98
102
|
createdExecuteId: executeId,
|
|
99
103
|
createdLogger: logger,
|
|
100
104
|
path: http.path.string as string,
|
|
101
|
-
headers: request.headers as Headers,
|
|
105
|
+
headers: options.request.headers as Headers,
|
|
102
106
|
mixinContext: { http },
|
|
103
107
|
params: http.params.string,
|
|
104
108
|
paramsType: "string",
|
|
@@ -116,11 +120,11 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
116
120
|
if (routeSchema === undefined) throw reject("NOT_FOUND", { path: http.path.string as string });
|
|
117
121
|
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.` });
|
|
118
122
|
|
|
119
|
-
const executed = await executer.__execute(routeSchema, {
|
|
123
|
+
const executed = await executer.__execute(options.env, options.envMode, routeSchema, {
|
|
120
124
|
createdExecuteId: executeId,
|
|
121
125
|
createdLogger: logger,
|
|
122
126
|
path: http.path.string as string,
|
|
123
|
-
headers: request.headers as Headers,
|
|
127
|
+
headers: options.request.headers as Headers,
|
|
124
128
|
mixinContext: { http },
|
|
125
129
|
params: http.params.string,
|
|
126
130
|
paramsType: "string",
|
|
@@ -137,7 +141,7 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
137
141
|
try {
|
|
138
142
|
controller.write(`data:@${JSON.stringify({ success: true, data: undefined, executeId } satisfies MilkioResponseSuccess<any>)}\n\n`);
|
|
139
143
|
for await (const value of executed.results.value) {
|
|
140
|
-
if (!request.signal.aborted) {
|
|
144
|
+
if (!options.request.signal.aborted) {
|
|
141
145
|
const result: string = JSON.stringify([null, TSON.encode(value)]);
|
|
142
146
|
controller.write(`data:${result}\n\n`);
|
|
143
147
|
} else {
|
|
@@ -166,7 +170,7 @@ export const __initListener = <MilkioRuntime extends MilkioRuntimeInit<MilkioRun
|
|
|
166
170
|
try {
|
|
167
171
|
controller.enqueue(`data:@${JSON.stringify({ success: true, data: undefined, executeId } satisfies MilkioResponseSuccess<any>)}\n\n`);
|
|
168
172
|
for await (const value of executed.results.value) {
|
|
169
|
-
if (!request.signal.aborted) {
|
|
173
|
+
if (!options.request.signal.aborted) {
|
|
170
174
|
const result: string = JSON.stringify([null, TSON.encode(value)]);
|
|
171
175
|
controller.enqueue(`data:${result}\n\n`);
|
|
172
176
|
} else {
|
package/package.json
CHANGED