milkio 0.2.11 → 0.2.13

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.
@@ -18,6 +18,7 @@ export type ExecuteHttpServerOptions = {
18
18
  * @returns
19
19
  */
20
20
  executeIdGenerator?: (request: Request) => string | Promise<string>;
21
+ getRealIp?: (request: Request) => string;
21
22
  };
22
23
 
23
24
  export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOptions = {}) {
@@ -26,7 +27,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
26
27
  const executeId = (options?.executeIdGenerator ? await options.executeIdGenerator(request.request) : createUlid()) as ExecuteId;
27
28
  runtime.execute.executeIds.add(executeId);
28
29
  const logger = useLogger(executeId);
29
- const ip = (request.request.headers.get("x-forwarded-for") as string | undefined)?.split(",")[0] ?? "0.0.0.0";
30
+ const ip = options.getRealIp ? options.getRealIp(request.request) : (request.request.headers.get("X-Forwarded-For") as string | undefined)?.split(",")[0] ?? "0.0.0.0";
30
31
  const headers = request.request.headers;
31
32
 
32
33
  loggerPushTags(executeId, {
@@ -92,7 +93,11 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
92
93
  body: rawbody || "no body",
93
94
  });
94
95
 
95
- if (!detail.response.body) detail.response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":${JSON.stringify(failCode.NOT_FOUND())}}}`;
96
+ if (!detail.response.body) {
97
+ if (!detail.response.headers["Content-Type"]) detail.response.headers["Content-Type"] = "application/json";
98
+ if (!detail.response.headers["Cache-Control"]) detail.response.headers["Cache-Control"] = "no-cache";
99
+ detail.response.body = `{"executeId":"${executeId}","success":false,"fail":{"code":"NOT_FOUND","message":"${failCode.NOT_FOUND()}"}}`;
100
+ }
96
101
  await MiddlewareEvent.handle("httpNotFound", [detail]);
97
102
 
98
103
  loggerPushTags(executeId, {
@@ -159,10 +164,12 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
159
164
  };
160
165
  await MiddlewareEvent.handle("beforeHttpResponse", [middlewareResponse, detail]);
161
166
 
167
+ if (!detail.response.headers["Content-Type"]) detail.response.headers["Content-Type"] = "application/json";
162
168
  if (!detail.response.headers["Cache-Control"]) detail.response.headers["Cache-Control"] = "no-cache";
163
169
  if (!detail.response.body) detail.response.body = middlewareResponse.value;
164
170
  } catch (error) {
165
171
  const result = hanldeCatchError(error, executeId);
172
+ if (!response.headers["Content-Type"]) response.headers["Content-Type"] = "application/json";
166
173
  if (!response.headers["Cache-Control"]) response.headers["Cache-Control"] = "no-cache";
167
174
  if (!response.body) response.body = TSON.stringify(result);
168
175
  }
package/kernel/logger.ts CHANGED
@@ -23,10 +23,10 @@ export type Logger = {
23
23
  };
24
24
 
25
25
  export const loggerController = (() => {
26
- const logs = new Map<ExecuteId, { __LOG_DEATIL__: Array<LoggerItem>; [key: string]: any }>();
26
+ const logs = new Map<ExecuteId, { __LOG_DETAIL__: Array<LoggerItem>; [key: string]: any }>();
27
27
 
28
28
  const loggerPushTags = (executeId: ExecuteId, tags: Record<string, any>) => {
29
- if (!logs.has(executeId)) logs.set(executeId, { __LOG_DEATIL__: [] });
29
+ if (!logs.has(executeId)) logs.set(executeId, { __LOG_DETAIL__: [] });
30
30
  const logItem = logs.get(executeId);
31
31
  for (const key in tags) {
32
32
  logItem![key] = tags[key];
@@ -41,11 +41,11 @@ export const loggerController = (() => {
41
41
  };
42
42
  const log = logs.get(executeId)!;
43
43
  for (const key in log) {
44
- if (key === "__LOG_DEATIL__") continue;
44
+ if (key === "__LOG_DETAIL__") continue;
45
45
  loggerSubmitOptions[key] = log[key];
46
46
  }
47
47
  logs.delete(executeId);
48
- loggerOptions.onSubmit(loggerSubmitOptions, log.__LOG_DEATIL__);
48
+ loggerOptions.onSubmit(loggerSubmitOptions, log.__LOG_DETAIL__);
49
49
  };
50
50
 
51
51
  const loggerSubmitAll = async () => {
@@ -63,7 +63,7 @@ export const loggerController = (() => {
63
63
  }
64
64
 
65
65
  for (const executeId of executeIds) {
66
- if (!logs.has(executeId as ExecuteId)) logs.set(executeId as ExecuteId, { __LOG_DEATIL__: [] });
66
+ if (!logs.has(executeId as ExecuteId)) logs.set(executeId as ExecuteId, { __LOG_DETAIL__: [] });
67
67
  const loggerItem = {
68
68
  executeId: executeId as ExecuteId,
69
69
  loggerLevel: level,
@@ -71,7 +71,7 @@ export const loggerController = (() => {
71
71
  params,
72
72
  } satisfies LoggerItem;
73
73
  if (!loggerOptions.onInsert(loggerItem)) return;
74
- logs.get(executeId as ExecuteId)!.__LOG_DEATIL__.push(loggerItem);
74
+ logs.get(executeId as ExecuteId)!.__LOG_DETAIL__.push(loggerItem);
75
75
  }
76
76
  };
77
77
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.2.11",
5
+ "version": "0.2.13",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },