milkio 1.0.0-alpha.5 → 1.0.0-alpha.50

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.
Files changed (42) hide show
  1. package/.co.toml +2 -2
  2. package/LICENSE +1 -1
  3. package/README.md +1 -1
  4. package/action/index.ts +16 -16
  5. package/command/index.ts +41 -37
  6. package/config/index.ts +33 -0
  7. package/context/index.ts +21 -18
  8. package/events/index.ts +26 -26
  9. package/exception/index.ts +31 -27
  10. package/execute/execute-id-generator.ts +5 -5
  11. package/execute/index.ts +96 -135
  12. package/index.ts +16 -13
  13. package/listener/index.ts +152 -123
  14. package/logger/index.ts +52 -52
  15. package/meta/index.ts +2 -2
  16. package/package.json +5 -5
  17. package/step/index.ts +21 -21
  18. package/stream/index.ts +16 -16
  19. package/tsconfig.json +18 -18
  20. package/type-safety/index.ts +16 -0
  21. package/types/index.ts +44 -57
  22. package/utils/create-id.ts +10 -2
  23. package/utils/headers-to-json.ts +3 -3
  24. package/utils/send-cookbook-event.ts +17 -16
  25. package/world/index.ts +61 -55
  26. package/.publish/publish.json +0 -7
  27. package/.publish/releases/0.0.0.md +0 -0
  28. package/.publish/releases/0.1.0.md +0 -13
  29. package/.publish/releases/0.2.0.md +0 -33
  30. package/.publish/releases/0.3.0.md +0 -85
  31. package/.publish/releases/0.4.0.md +0 -35
  32. package/.publish/releases/0.5.0.md +0 -19
  33. package/.publish/releases/0.6.0.md +0 -41
  34. package/.publish/releases/0.8.0.md +0 -128
  35. package/.publish/releases-github/0.0.0.md +0 -0
  36. package/.publish/releases-github/0.1.0.md +0 -13
  37. package/.publish/releases-github/0.2.0.md +0 -33
  38. package/.publish/releases-github/0.3.0.md +0 -85
  39. package/.publish/releases-github/0.4.0.md +0 -35
  40. package/.publish/releases-github/0.5.0.md +0 -19
  41. package/.publish/releases-github/0.6.0.md +0 -41
  42. package/.publish/releases-github/0.8.0.md +0 -126
package/.co.toml CHANGED
@@ -1,2 +1,2 @@
1
- ["general"]
2
- includes = ["co:bun"]
1
+ [general]
2
+ includes = [ "co:bun" ]
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 southern-aurora
3
+ Copyright (c) 2023 akirarika
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,3 +1,3 @@
1
1
  # Milkio
2
2
 
3
- Document: [milkio.fun](https://milkio.fun)
3
+ Document: [milkio.fun](https://milkio.fun)
package/action/index.ts CHANGED
@@ -1,19 +1,19 @@
1
- import { type $context, type $meta } from "..";
1
+ import type { $context, $meta } from '..'
2
2
 
3
- export const 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
- };
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
- export type ActionInit = {
11
- meta?: $meta;
12
- handler: (context: $context, params: any) => Promise<unknown>;
13
- };
10
+ export interface ActionInit {
11
+ meta?: $meta
12
+ handler: (context: $context, params: any) => Promise<unknown>
13
+ }
14
14
 
15
- export type Action<ActionInitT extends ActionInit> = {
16
- $milkioType: "action";
17
- meta: ActionInitT["meta"] extends undefined ? {} : ActionInitT["meta"];
18
- handler: ActionInitT["handler"];
19
- };
15
+ export interface Action<ActionInitT extends ActionInit> {
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 { type MilkioRuntimeInit, type MilkioInit, type GeneratedInit } from "..";
1
+ import type { GeneratedInit } from '..'
2
2
 
3
- export const 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
- };
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 type CommandInit = {
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 type Command<CommandInitT extends CommandInit> = {
14
- $milkioType: "command";
15
- handler: CommandInitT["handler"];
16
- };
13
+ export interface Command<CommandInitT extends CommandInit> {
14
+ $milkioType: 'command'
15
+ handler: CommandInitT['handler']
16
+ }
17
17
 
18
- export const __initCommander = <MilkioRuntime extends MilkioRuntimeInit<MilkioInit> = MilkioRuntimeInit<MilkioInit>>(generated: GeneratedInit, runtime: MilkioRuntime) => {
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: "index",
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("--") && v.includes("=")) {
27
- const vSplited = v.split("=");
28
- params.options[vSplited[0].slice(2)] = vSplited.slice(1, vSplited.length).join("=");
29
- } else if (v.startsWith("--")) {
30
- params.options[v.slice(2)] = "1";
31
- } else if (v.startsWith("-") && v.includes("=")) {
32
- const vSplited = v.split("=");
33
- params.options[vSplited[0].slice(1)] = vSplited.slice(1, vSplited.length).join("=");
34
- } else if (v.startsWith("-")) {
35
- params.options[v.slice(1)] = "1";
36
- } else {
37
- params.commands.push(v);
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] ?? "index"}`;
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
+ }
@@ -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,27 @@
1
- import { type MilkioHttpRequest, type MilkioHttpResponse, type $types, type Logger, type Steps, type Execute } from "..";
1
+ import type { MilkioHttpRequest, MilkioHttpResponse, $types, Logger, Action, MilkioRuntimeInit, MilkioInit } from '..'
2
2
 
3
3
  export interface $context {
4
- executeId: string;
5
- path: string;
6
- logger: Logger;
7
- http?: ContextHttp<Record<any, any>>;
8
- step: Steps<{}>["step"];
9
- execute: Execute;
4
+ _: MilkioRuntimeInit<MilkioInit>
5
+ develop: boolean
6
+ executeId: string
7
+ environment: string
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']>>
10
13
  }
11
14
 
12
- export type ContextHttp<ParamsParsed = any> = {
13
- url: URL;
14
- ip: string;
15
- path: { string: keyof $types["generated"]["routeSchema"]["$types"]; array: Array<string> };
15
+ export interface ContextHttp<ParamsParsed = any> {
16
+ url: URL
17
+ ip: string
18
+ path: { string: keyof $types['generated']['routeSchema'], array: Array<string> }
16
19
  params: {
17
- string: string;
18
- parsed: ParamsParsed;
19
- };
20
- request: MilkioHttpRequest;
21
- response: MilkioHttpResponse;
22
- };
20
+ string: string
21
+ parsed: ParamsParsed
22
+ }
23
+ request: MilkioHttpRequest
24
+ response: MilkioHttpResponse
25
+ }
23
26
 
24
- export type ContextCreatedHandler = (context: $context) => Promise<void> | void;
27
+ export type ContextCreatedHandler = (context: $context) => Promise<void> | void
package/events/index.ts CHANGED
@@ -1,47 +1,47 @@
1
- import { type $context, type ContextHttp, type Results, type Logger } from "..";
1
+ import type { $context, ContextHttp, Results, Logger, $meta } from '..'
2
2
 
3
3
  export interface $events {
4
- "milkio:httpRequest": { executeId: string; path: string; logger: Logger; http: ContextHttp<Record<string, any>> };
5
- "milkio:httpResponse": { executeId: string; path: string; logger: Logger; http: ContextHttp<Record<string, any>>; context: $context };
6
- "milkio:httpNotFound": { executeId: string; path: string; logger: Logger; http: ContextHttp<Record<string, any>> };
7
- "milkio:executeBefore": { executeId: string; path: string; logger: Logger; context: $context };
8
- "milkio:executeAfter": { executeId: string; path: string; logger: Logger; context: $context; results: Results<any> };
4
+ 'milkio:httpRequest': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>> }
5
+ 'milkio:httpResponse': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>>, context: $context }
6
+ 'milkio:httpNotFound': { executeId: string, path: string, logger: Logger, http: ContextHttp<Record<string, any>> }
7
+ 'milkio:executeBefore': { executeId: string, path: string, logger: Logger, meta: $meta, context: $context }
8
+ 'milkio:executeAfter': { executeId: string, path: string, logger: Logger, meta: $meta, context: $context, results: Results<any> }
9
9
  }
10
10
 
11
- export const __initEventManager = () => {
12
- const handlers = new Map<(event: any) => void, string>();
13
- const indexed = new Map<string, Set<(event: any) => void>>();
11
+ export function __initEventManager() {
12
+ const handlers = new Map<(event: any) => void, string>()
13
+ const indexed = new Map<string, Set<(event: any) => void>>()
14
14
 
15
15
  const eventManager = {
16
16
  on: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
17
- handlers.set(handler, key as string);
17
+ handlers.set(handler, key as string)
18
18
  if (indexed.has(key as string) === false) {
19
- indexed.set(key as string, new Set());
19
+ indexed.set(key as string, new Set())
20
20
  }
21
- const set = indexed.get(key as string)!;
22
- set.add(handler);
23
- handlers.set(handler, key as string);
21
+ const set = indexed.get(key as string)!
22
+ set.add(handler)
23
+ handlers.set(handler, key as string)
24
24
 
25
25
  return () => {
26
- handlers.delete(handler);
27
- set.delete(handler);
28
- };
26
+ handlers.delete(handler)
27
+ set.delete(handler)
28
+ }
29
29
  },
30
30
  off: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
31
- const set = indexed.get(key as string);
32
- if (!set) return;
33
- handlers.delete(handler);
34
- set.delete(handler);
31
+ const set = indexed.get(key as string)
32
+ if (!set) return
33
+ handlers.delete(handler)
34
+ set.delete(handler)
35
35
  },
36
36
  emit: async <Key extends keyof $events, Value extends $events[Key]>(key: Key, value: Value): Promise<void> => {
37
- const h = indexed.get(key as string);
37
+ const h = indexed.get(key as string)
38
38
  if (h) {
39
39
  for (const handler of h) {
40
- await handler(value);
40
+ await handler(value)
41
41
  }
42
42
  }
43
43
  },
44
- };
44
+ }
45
45
 
46
- return eventManager;
47
- };
46
+ return eventManager
47
+ }
@@ -1,44 +1,48 @@
1
- import { TSON } from "@southern-aurora/tson";
2
- import { type MilkioResponseReject, type Logger } from "..";
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; message: string };
9
- NOT_FOUND: { path: string };
10
- PARAMS_TYPE_INCORRECT: { path: string; expected: string; value: any; message: string } | null;
11
- UNACCEPTABLE: { expected: string; message: string };
12
- PARAMS_TYPE_NOT_SUPPORTED: { expected: string };
13
- INTERNAL_SERVER_ERROR: undefined;
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: code, data: data } as MilkioRejectError<Code, RejectData>;
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; data: RejectData; stack: string; $milkioReject: true };
24
+ export interface 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 ?? "Unnamed Exception";
27
+ const name = error?.code ?? error?.name ?? error?.constructor?.name ?? 'Unnamed Exception'
26
28
 
27
- if (error?.$milkioReject === true && error?.code === "NOT_FOUND") {
28
- logger.info(name, error?.data?.path ?? "Unknown path");
29
- } else {
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 ?? "").split("\n").slice(2).join("\n") : error?.stack ?? "";
32
- logger.error(name, `\n${TSON.stringify(error?.data)}`, `\n${stack}\n`);
33
- } catch (_) {
34
- logger.error(name, `\n${error?.toString()}`, `\n${error?.stack}\n`);
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: "INTERNAL_SERVER_ERROR", reject: undefined, executeId };
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 "../utils/create-id";
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 const defineDefaultExecuteIdGenerator = () => {
6
- return createId;
7
- };
5
+ export function defineDefaultExecuteIdGenerator() {
6
+ return createId
7
+ }