@miqro/core 1.3.6 → 1.4.0

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 (165) hide show
  1. package/README.md +36 -112
  2. package/dist/handler/api-router-utils.d.ts +12 -1
  3. package/dist/handler/api-router-utils.js +86 -1
  4. package/dist/handler/api-router-utils.js.map +1 -1
  5. package/dist/handler/api-router.d.ts +29 -2
  6. package/dist/handler/api-router.js +2 -2
  7. package/dist/handler/api-router.js.map +1 -1
  8. package/dist/handler/app.js +1 -0
  9. package/dist/handler/app.js.map +1 -1
  10. package/dist/handler/common/context.d.ts +3 -11
  11. package/dist/handler/common/context.js +18 -13
  12. package/dist/handler/common/context.js.map +1 -1
  13. package/dist/handler/common/index.js.map +1 -1
  14. package/dist/handler/common/router-utils.js +60 -5
  15. package/dist/handler/common/router-utils.js.map +1 -1
  16. package/dist/handler/index.d.ts +0 -3
  17. package/dist/handler/index.js +0 -3
  18. package/dist/handler/index.js.map +1 -1
  19. package/dist/handler/middleware/cookie.js +2 -2
  20. package/dist/handler/middleware/cookie.js.map +1 -1
  21. package/dist/handler/middleware/form.js +7 -1
  22. package/dist/handler/middleware/form.js.map +1 -1
  23. package/dist/handler/middleware/group.js +44 -2
  24. package/dist/handler/middleware/group.js.map +1 -1
  25. package/dist/handler/middleware/index.js +2 -6
  26. package/dist/handler/middleware/index.js.map +1 -1
  27. package/dist/handler/middleware/json.js +8 -2
  28. package/dist/handler/middleware/json.js.map +1 -1
  29. package/dist/handler/middleware/session.js.map +1 -1
  30. package/dist/handler/middleware/text.js +8 -1
  31. package/dist/handler/middleware/text.js.map +1 -1
  32. package/dist/handler/proxy.d.ts +2 -5
  33. package/dist/handler/proxy.js +2 -3
  34. package/dist/handler/proxy.js.map +1 -1
  35. package/dist/util/cli/index.d.ts +6 -1
  36. package/dist/util/cli/index.js +26 -0
  37. package/dist/util/cli/index.js.map +1 -1
  38. package/dist/util/error/configfilenotfound.js.map +1 -1
  39. package/dist/util/error/forbiddenerror.js.map +1 -1
  40. package/dist/util/error/notfound.js.map +1 -1
  41. package/dist/util/error/notimplemented.js.map +1 -1
  42. package/dist/util/error/parsererror.d.ts +2 -1
  43. package/dist/util/error/parsererror.js +5 -1
  44. package/dist/util/error/parsererror.js.map +1 -1
  45. package/dist/util/error/unauthorizederror.js.map +1 -1
  46. package/dist/util/index.d.ts +0 -1
  47. package/dist/util/index.js +0 -3
  48. package/dist/util/index.js.map +1 -1
  49. package/dist/util/loader/index.d.ts +0 -3
  50. package/dist/util/loader/index.js +2 -9
  51. package/dist/util/loader/index.js.map +1 -1
  52. package/dist/util/logger/formatter.js +1 -1
  53. package/dist/util/logger/formatter.js.map +1 -1
  54. package/dist/util/request/index.d.ts +1 -1
  55. package/dist/util/request/index.js +1 -3
  56. package/dist/util/request/index.js.map +1 -1
  57. package/dist/util/session/common.d.ts +0 -1
  58. package/dist/util/session/common.js +0 -46
  59. package/dist/util/session/common.js.map +1 -1
  60. package/dist/util/session/service.js +2 -2
  61. package/dist/util/session/service.js.map +1 -1
  62. package/package.json +14 -11
  63. package/src/handler/api-router-utils.ts +91 -2
  64. package/src/handler/api-router.ts +30 -4
  65. package/src/handler/app.ts +2 -1
  66. package/src/handler/common/context.ts +19 -24
  67. package/src/handler/common/index.ts +1 -2
  68. package/src/handler/common/router-utils.ts +194 -146
  69. package/src/handler/index.ts +0 -3
  70. package/src/handler/middleware/cookie.ts +12 -12
  71. package/src/handler/middleware/form.ts +8 -2
  72. package/src/handler/middleware/group.ts +44 -2
  73. package/src/handler/middleware/index.ts +24 -28
  74. package/src/handler/middleware/json.ts +9 -3
  75. package/src/handler/middleware/session.ts +128 -128
  76. package/src/handler/middleware/text.ts +8 -1
  77. package/src/handler/proxy.ts +3 -8
  78. package/src/util/cli/index.ts +25 -1
  79. package/src/util/error/configfilenotfound.ts +1 -1
  80. package/src/util/error/forbiddenerror.ts +1 -1
  81. package/src/util/error/notfound.ts +1 -1
  82. package/src/util/error/notimplemented.ts +1 -1
  83. package/src/util/error/parsererror.ts +7 -2
  84. package/src/util/error/unauthorizederror.ts +1 -1
  85. package/src/util/index.ts +0 -3
  86. package/src/util/loader/index.ts +1 -7
  87. package/src/util/logger/formatter.ts +1 -1
  88. package/src/util/parser/common.ts +1 -1
  89. package/src/util/parser/index.ts +0 -3
  90. package/src/util/request/index.ts +2 -2
  91. package/src/util/session/common.ts +0 -43
  92. package/src/util/session/index.ts +1 -1
  93. package/src/util/session/service.ts +126 -126
  94. package/dist/handler/api-handler.d.ts +0 -29
  95. package/dist/handler/api-handler.js +0 -42
  96. package/dist/handler/api-handler.js.map +0 -1
  97. package/dist/handler/feature-router.d.ts +0 -12
  98. package/dist/handler/feature-router.js +0 -90
  99. package/dist/handler/feature-router.js.map +0 -1
  100. package/dist/handler/response/html.d.ts +0 -10
  101. package/dist/handler/response/html.js +0 -34
  102. package/dist/handler/response/html.js.map +0 -1
  103. package/dist/handler/response/index.d.ts +0 -3
  104. package/dist/handler/response/index.js +0 -16
  105. package/dist/handler/response/index.js.map +0 -1
  106. package/dist/handler/response/json.d.ts +0 -2
  107. package/dist/handler/response/json.js +0 -21
  108. package/dist/handler/response/json.js.map +0 -1
  109. package/dist/handler/response/result.d.ts +0 -4
  110. package/dist/handler/response/result.js +0 -46
  111. package/dist/handler/response/result.js.map +0 -1
  112. package/dist/util/client.d.ts +0 -17
  113. package/dist/util/client.js +0 -55
  114. package/dist/util/client.js.map +0 -1
  115. package/dist/util/cookie/cookie.d.ts +0 -50
  116. package/dist/util/cookie/cookie.js +0 -168
  117. package/dist/util/cookie/cookie.js.map +0 -1
  118. package/dist/util/cookie/index.d.ts +0 -16
  119. package/dist/util/cookie/index.js +0 -9
  120. package/dist/util/cookie/index.js.map +0 -1
  121. package/dist/util/test/app.d.ts +0 -3
  122. package/dist/util/test/app.js +0 -50
  123. package/dist/util/test/app.js.map +0 -1
  124. package/dist/util/test/common.d.ts +0 -6
  125. package/dist/util/test/common.js +0 -15
  126. package/dist/util/test/common.js.map +0 -1
  127. package/dist/util/test/index.d.ts +0 -2
  128. package/dist/util/test/index.js +0 -15
  129. package/dist/util/test/index.js.map +0 -1
  130. package/dist/util/uuid/index.d.ts +0 -7
  131. package/dist/util/uuid/index.js +0 -8
  132. package/dist/util/uuid/index.js.map +0 -1
  133. package/dist/util/uuid/regex.d.ts +0 -2
  134. package/dist/util/uuid/regex.js +0 -5
  135. package/dist/util/uuid/regex.js.map +0 -1
  136. package/dist/util/uuid/rng.d.ts +0 -1
  137. package/dist/util/uuid/rng.js +0 -18
  138. package/dist/util/uuid/rng.js.map +0 -1
  139. package/dist/util/uuid/stringify.d.ts +0 -2
  140. package/dist/util/uuid/stringify.js +0 -50
  141. package/dist/util/uuid/stringify.js.map +0 -1
  142. package/dist/util/uuid/v4.d.ts +0 -5
  143. package/dist/util/uuid/v4.js +0 -27
  144. package/dist/util/uuid/v4.js.map +0 -1
  145. package/dist/util/uuid/validate.d.ts +0 -2
  146. package/dist/util/uuid/validate.js +0 -12
  147. package/dist/util/uuid/validate.js.map +0 -1
  148. package/src/handler/api-handler.ts +0 -55
  149. package/src/handler/feature-router.ts +0 -92
  150. package/src/handler/response/html.ts +0 -36
  151. package/src/handler/response/index.ts +0 -3
  152. package/src/handler/response/json.ts +0 -19
  153. package/src/handler/response/result.ts +0 -40
  154. package/src/util/client.ts +0 -64
  155. package/src/util/cookie/cookie.ts +0 -207
  156. package/src/util/cookie/index.ts +0 -6
  157. package/src/util/test/app.ts +0 -44
  158. package/src/util/test/common.ts +0 -17
  159. package/src/util/test/index.ts +0 -2
  160. package/src/util/uuid/index.ts +0 -5
  161. package/src/util/uuid/regex.ts +0 -2
  162. package/src/util/uuid/rng.ts +0 -14
  163. package/src/util/uuid/stringify.ts +0 -53
  164. package/src/util/uuid/v4.ts +0 -30
  165. package/src/util/uuid/validate.ts +0 -8
@@ -14,142 +14,142 @@ const DEFAULT_TOKEN_SET_COOKIE_PATH = "/";
14
14
  const DEFAULT_TOKEN_SET_COOKIE_SAME_SITE = "strict";
15
15
 
16
16
  export interface SessionHandlerOptions {
17
- authService: {
18
- verify(args: { token: string; ctx: Context }): Promise<Session | null>;
19
- };
20
- options?: {
21
- tokenLocation: "header" | "query" | "cookie";
22
- tokenLocationName: string | ((ctx: Context) => Promise<string>);
23
- setCookieOptions?: {
24
- httpOnly: boolean;
25
- secure: boolean;
26
- path: string | ((ctx: Context) => Promise<string>);
27
- sameSite: "lax" | "strict" | "none";
28
- }
17
+ authService: {
18
+ verify(args: { token: string; ctx: Context }): Promise<Session | null>;
19
+ };
20
+ options?: {
21
+ tokenLocation: "header" | "query" | "cookie";
22
+ tokenLocationName: string | ((ctx: Context) => Promise<string>);
23
+ setCookieOptions?: {
24
+ httpOnly: boolean;
25
+ secure: boolean;
26
+ path: string | ((ctx: Context) => Promise<string>);
27
+ sameSite: "lax" | "strict" | "none";
29
28
  }
29
+ }
30
30
  }
31
31
 
32
32
  export const SessionHandler = (config: SessionHandlerOptions): Handler<void> => {
33
- if (!config.options) {
34
- const tokenLocation = checkEnvVariables(["TOKEN_LOCATION"], [DEFAULT_TOKEN_LOCATION])[0];
35
- config.options = {
36
- tokenLocation: tokenLocation as any,
37
- tokenLocationName: "",
38
- setCookieOptions: {
39
- httpOnly: true,
40
- path: "/",
41
- sameSite: "strict",
42
- secure: true
43
- }
33
+ if (!config.options) {
34
+ const tokenLocation = checkEnvVariables(["TOKEN_LOCATION"], [DEFAULT_TOKEN_LOCATION])[0];
35
+ config.options = {
36
+ tokenLocation: tokenLocation as any,
37
+ tokenLocationName: "",
38
+ setCookieOptions: {
39
+ httpOnly: true,
40
+ path: "/",
41
+ sameSite: "strict",
42
+ secure: true
43
+ }
44
+ };
45
+ switch (tokenLocation) {
46
+ case "header":
47
+ config.options.tokenLocationName = checkEnvVariables(["TOKEN_HEADER"], [DEFAULT_TOKEN_HEADER])[0];
48
+ break;
49
+ case "query":
50
+ config.options.tokenLocationName = checkEnvVariables(["TOKEN_QUERY"], [DEFAULT_TOKEN_QUERY])[0];
51
+ break;
52
+ case "cookie":
53
+ config.options.tokenLocationName = checkEnvVariables(["TOKEN_COOKIE"], [DEFAULT_TOKEN_COOKIE])[0];
54
+ const [httpOnlyS, secureS, path, sameSite] = checkEnvVariables(
55
+ ["TOKEN_SET_COOKIE_HTTP_ONLY", "TOKEN_SET_COOKIE_SECURE", "TOKEN_SET_COOKIE_PATH", "TOKEN_SET_COOKIE_SAME_SITE"],
56
+ [DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY, DEFAULT_TOKEN_SET_COOKIE_SECURE, DEFAULT_TOKEN_SET_COOKIE_PATH, DEFAULT_TOKEN_SET_COOKIE_SAME_SITE]);
57
+ config.options.setCookieOptions = {
58
+ httpOnly: httpOnlyS === "true",
59
+ secure: secureS === "true",
60
+ path,
61
+ sameSite: sameSite as any
44
62
  };
45
- switch (tokenLocation) {
46
- case "header":
47
- config.options.tokenLocationName = checkEnvVariables(["TOKEN_HEADER"], [DEFAULT_TOKEN_HEADER])[0];
48
- break;
49
- case "query":
50
- config.options.tokenLocationName = checkEnvVariables(["TOKEN_QUERY"], [DEFAULT_TOKEN_QUERY])[0];
51
- break;
52
- case "cookie":
53
- config.options.tokenLocationName = checkEnvVariables(["TOKEN_COOKIE"], [DEFAULT_TOKEN_COOKIE])[0];
54
- const [httpOnlyS, secureS, path, sameSite] = checkEnvVariables(
55
- ["TOKEN_SET_COOKIE_HTTP_ONLY", "TOKEN_SET_COOKIE_SECURE", "TOKEN_SET_COOKIE_PATH", "TOKEN_SET_COOKIE_SAME_SITE"],
56
- [DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY, DEFAULT_TOKEN_SET_COOKIE_SECURE, DEFAULT_TOKEN_SET_COOKIE_PATH, DEFAULT_TOKEN_SET_COOKIE_SAME_SITE]);
57
- config.options.setCookieOptions = {
58
- httpOnly: httpOnlyS === "true",
59
- secure: secureS === "true",
60
- path,
61
- sameSite: sameSite as any
62
- };
63
- break;
64
- default:
65
- throw new Error(`TOKEN_LOCATION=${tokenLocation} not supported use (header or query)`);
66
- }
67
- } else {
68
- config.options = parse("options", config.options as any, [
69
- { name: "tokenLocation", required: true, type: "enum", enumValues: ["header", "query", "cookie"] },
70
- { name: "tokenLocationName", required: true, type: "any" },
71
- {
72
- name: "setCookieOptions", required: false, type: "nested", defaultValue: {
73
- httpOnly: DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY === "true",
74
- secure: DEFAULT_TOKEN_SET_COOKIE_SECURE === "true",
75
- path: DEFAULT_TOKEN_SET_COOKIE_PATH,
76
- sameSite: DEFAULT_TOKEN_SET_COOKIE_SAME_SITE,
77
- }, nestedOptions: {
78
- mode: "no_extra",
79
- options: [
80
- { name: "httpOnly", required: true, type: "boolean" },
81
- { name: "secure", required: true, type: "boolean" },
82
- { name: "path", required: true, type: "any" },
83
- { name: "sameSite", required: true, type: "enum", enumValues: ["lax", "strict", "none"] }
84
- ]
85
- }
86
- }
87
- ], "no_extra") as any;
63
+ break;
64
+ default:
65
+ throw new Error(`TOKEN_LOCATION=${tokenLocation} not supported use (header or query)`);
88
66
  }
67
+ } else {
68
+ config.options = parse("options", config.options as any, [
69
+ { name: "tokenLocation", required: true, type: "enum", enumValues: ["header", "query", "cookie"] },
70
+ { name: "tokenLocationName", required: true, type: "any" },
71
+ {
72
+ name: "setCookieOptions", required: false, type: "nested", defaultValue: {
73
+ httpOnly: DEFAULT_TOKEN_SET_COOKIE_HTTP_ONLY === "true",
74
+ secure: DEFAULT_TOKEN_SET_COOKIE_SECURE === "true",
75
+ path: DEFAULT_TOKEN_SET_COOKIE_PATH,
76
+ sameSite: DEFAULT_TOKEN_SET_COOKIE_SAME_SITE,
77
+ }, nestedOptions: {
78
+ mode: "no_extra",
79
+ options: [
80
+ { name: "httpOnly", required: true, type: "boolean" },
81
+ { name: "secure", required: true, type: "boolean" },
82
+ { name: "path", required: true, type: "any" },
83
+ { name: "sameSite", required: true, type: "enum", enumValues: ["lax", "strict", "none"] }
84
+ ]
85
+ }
86
+ }
87
+ ], "no_extra") as any;
88
+ }
89
89
 
90
- if (!config.authService) {
91
- throw new Error("authService must be provided!");
92
- }
93
- if (!config.options || !config.options.setCookieOptions) {
94
- throw new Error("config.options not populated!");
95
- }
96
- const tokenLocation = config.options.tokenLocation;
97
- const tokenLocationName = config.options.tokenLocationName;
98
- const setCookieOptions = config.options.setCookieOptions;
99
- return async (ctx: Context): Promise<void> => {
100
- try {
101
- const tlN = typeof tokenLocationName === "string" ? tokenLocationName : await tokenLocationName(ctx);
102
- let token = null;
103
- switch (tokenLocation) {
104
- case "header":
105
- token = ctx.headers[(tlN).toLowerCase()] as string;
106
- break;
107
- case "query":
108
- token = ctx.query[tlN] as string;
109
- break;
110
- case "cookie":
111
- token = ctx.cookies[tlN] ? ctx.cookies[tlN] : undefined;
112
- break;
113
- default:
114
- throw new Error(`TOKEN_LOCATION=${tokenLocation} not supported use (header, query or cookie)`);
115
- }
116
- if (!token) {
117
- ctx.logger.warn("No token provided!");
118
- throw new ForbiddenError("NO TOKEN");
119
- } else {
120
- const session = await config.authService.verify({ token, ctx });
121
- if (!session) {
122
- ctx.logger.warn("fail to authenticate token [%s]!", token);
123
- throw new UnAuthorizedError();
124
- } else {
125
- if (tokenLocation === "cookie" && session.token !== token) {
126
- const newTokenCookie = String(session.token ? session.token : token);
127
- const cookiePath = typeof setCookieOptions.path === "string" ? setCookieOptions.path : await setCookieOptions.path(ctx);
128
- ctx.setCookie(tlN, newTokenCookie, session.expires instanceof Date ? {
129
- httpOnly: setCookieOptions.httpOnly,
130
- secure: setCookieOptions.secure,
131
- path: cookiePath,
132
- sameSite: setCookieOptions.sameSite,
133
- expires: session.expires
134
- } : {
135
- httpOnly: setCookieOptions.httpOnly,
136
- path: cookiePath,
137
- secure: setCookieOptions.secure,
138
- sameSite: setCookieOptions.sameSite
139
- })
140
- }
141
- ctx.session = session;
142
- ctx.logger.debug("authenticated!");
143
- }
144
- }
145
- } catch (e: any) {
146
- if (e.message && e.message === "NO TOKEN") {
147
- throw e;
148
- } else {
149
- throw new UnAuthorizedError();
150
- }
90
+ if (!config.authService) {
91
+ throw new Error("authService must be provided!");
92
+ }
93
+ if (!config.options || !config.options.setCookieOptions) {
94
+ throw new Error("config.options not populated!");
95
+ }
96
+ const tokenLocation = config.options.tokenLocation;
97
+ const tokenLocationName = config.options.tokenLocationName;
98
+ const setCookieOptions = config.options.setCookieOptions;
99
+ return async (ctx: Context): Promise<void> => {
100
+ try {
101
+ const tlN = typeof tokenLocationName === "string" ? tokenLocationName : await tokenLocationName(ctx);
102
+ let token = null;
103
+ switch (tokenLocation) {
104
+ case "header":
105
+ token = ctx.headers[(tlN).toLowerCase()] as string;
106
+ break;
107
+ case "query":
108
+ token = ctx.query[tlN] as string;
109
+ break;
110
+ case "cookie":
111
+ token = ctx.cookies[tlN] ? ctx.cookies[tlN] : undefined;
112
+ break;
113
+ default:
114
+ throw new Error(`TOKEN_LOCATION=${tokenLocation} not supported use (header, query or cookie)`);
115
+ }
116
+ if (!token) {
117
+ ctx.logger.warn("No token provided!");
118
+ throw new ForbiddenError("NO TOKEN");
119
+ } else {
120
+ const session = await config.authService.verify({ token, ctx });
121
+ if (!session) {
122
+ ctx.logger.warn("fail to authenticate token [%s]!", token);
123
+ throw new UnAuthorizedError();
124
+ } else {
125
+ if (tokenLocation === "cookie" && session.token !== token) {
126
+ const newTokenCookie = String(session.token ? session.token : token);
127
+ const cookiePath = typeof setCookieOptions.path === "string" ? setCookieOptions.path : await setCookieOptions.path(ctx);
128
+ ctx.setCookie(tlN, newTokenCookie, session.expires instanceof Date ? {
129
+ httpOnly: setCookieOptions.httpOnly,
130
+ secure: setCookieOptions.secure,
131
+ path: cookiePath,
132
+ sameSite: setCookieOptions.sameSite,
133
+ expires: session.expires
134
+ } : {
135
+ httpOnly: setCookieOptions.httpOnly,
136
+ path: cookiePath,
137
+ secure: setCookieOptions.secure,
138
+ sameSite: setCookieOptions.sameSite
139
+ })
140
+ }
141
+ ctx.session = session;
142
+ ctx.logger.debug("authenticated!");
151
143
  }
152
- };
144
+ }
145
+ } catch (e: any) {
146
+ if (e.message && e.message === "NO TOKEN") {
147
+ throw e;
148
+ } else {
149
+ throw new UnAuthorizedError();
150
+ }
151
+ }
152
+ };
153
153
  };
154
154
 
155
155
 
@@ -2,6 +2,7 @@ import { Context, Handler } from "../common";
2
2
  import { BadRequestError } from "../../util/error";
3
3
  import { checkEnvVariables } from "../../util/config";
4
4
  import { DEFAULT_READ_BUFFER_LIMIT } from "./buffer";
5
+ import { ReadBuffer } from "./buffer";
5
6
 
6
7
  export const TextParser = (options?: {
7
8
  limit?: number;
@@ -18,9 +19,15 @@ export const TextParser = (options?: {
18
19
  limit = parseInt(limitS, 10);
19
20
  type = typeS;
20
21
  }
22
+ const readBuffer = ReadBuffer({
23
+ limit
24
+ });
21
25
  return async (ctx: Context): Promise<void> => {
22
26
  try {
23
27
  const isType = ctx.body === undefined && ctx.headers["content-type"] ? ctx.headers["content-type"].toLocaleLowerCase().indexOf(type.toLocaleLowerCase()) !== -1 : false;
28
+ if (isType && !ctx.buffer) {
29
+ await readBuffer(ctx);
30
+ }
24
31
  if (isType && ctx.buffer && ctx.buffer.length <= limit) {
25
32
  const string = ctx.buffer.toString();
26
33
  ctx.body = string;
@@ -29,7 +36,7 @@ export const TextParser = (options?: {
29
36
  throw new BadRequestError(`buffer.length ${ctx.buffer.length} > ${limit}`);
30
37
  }
31
38
  } catch (e: any) {
32
- if (e && e.name === "ParseOptionsError") {
39
+ if (e && e.name === "BadRequestError") {
33
40
  throw e;
34
41
  } else {
35
42
  ctx.logger.error(e);
@@ -2,17 +2,12 @@ import { request, RequestOptions, RequestResponse } from "../util/request";
2
2
  import { Context, Handler } from "./common";
3
3
 
4
4
  export interface ProxyServiceInterface {
5
- resolveRequest(ctx: Context): Promise<RequestOptions>;
5
+ resolve(ctx: Context): Promise<RequestOptions>;
6
6
  }
7
7
 
8
- export interface ProxyOptionsInterface {
9
- proxyService: ProxyServiceInterface;
10
- }
11
-
12
- export const ProxyHandler = (options: ProxyOptionsInterface): Handler<RequestResponse> =>
8
+ export const ProxyHandler = (resolver: ProxyServiceInterface): Handler<RequestResponse> =>
13
9
  async (ctx): Promise<RequestResponse> => {
14
- const resolver = options.proxyService;
15
- const requestConfig = await resolver.resolveRequest(ctx);
10
+ const requestConfig = await resolver.resolve(ctx);
16
11
  try {
17
12
  ctx.logger.debug("proxy resolveRequest to [%o]", requestConfig);
18
13
  const response = await request(requestConfig);
@@ -1,6 +1,7 @@
1
1
  import { SimpleMap } from "../parser/common";
2
2
  import { Logger } from "../logger";
3
- import { Callback } from "../test";
3
+
4
+ type Callback<T = any> = (...args: any[]) => T;
4
5
 
5
6
  // noinspection SpellCheckingInspection
6
7
  const routeCMDModule = async (cmdArg: string | undefined, cmds: CMDMapType, logger: Logger | Console, exit = true): Promise<void> => {
@@ -28,6 +29,29 @@ const routeCMDModule = async (cmdArg: string | undefined, cmds: CMDMapType, logg
28
29
  export type CMDMapType = SimpleMap<{ cb: Callback<void> | Callback<Promise<void>>; description: string; section?: string; }>
29
30
 
30
31
  export abstract class CLIUtil {
32
+ public static extractFlags(args: string[]): { flags: SimpleMap<string | string[]>; cmds: string[]; } {
33
+ const flags: SimpleMap<string | string[]> = {};
34
+ const cmds: string[] = [];
35
+ for (let i = 0; i < args.length; i++) {
36
+ const arg = args[i];
37
+ if (arg.indexOf("-") === 0) {
38
+ const argName = arg.substring(arg.indexOf("--") === 0 ? 2 : 1);
39
+ const argValue = args.length > i + 1 && args[i + 1] && args[i + 1].indexOf("-") != 0 ? args[i + 1] : "";
40
+ const flag = flags[argName];
41
+ if (flag instanceof Array) {
42
+ flag.push(argValue);
43
+ } else if (flag) {
44
+ flags[argName] = [flag, argValue];
45
+ } else {
46
+ flags[argName] = argValue;
47
+ }
48
+ i++;
49
+ } else {
50
+ cmds.push(arg);
51
+ }
52
+ }
53
+ return { flags, cmds };
54
+ }
31
55
  // noinspection SpellCheckingInspection
32
56
  public static cliFlow(cmds: CMDMapType, usage: string, logger: Logger | Console, cmdArg = process.argv[2], exit = true): void {
33
57
  const flow = async () => {
@@ -1,4 +1,4 @@
1
- import {NamedError} from "./named";
1
+ import { NamedError } from "./named";
2
2
 
3
3
  export class ConfigFileNotFoundError extends NamedError {
4
4
  constructor(message = "NOT FOUND") {
@@ -1,4 +1,4 @@
1
- import {NamedError} from "./named";
1
+ import { NamedError } from "./named";
2
2
 
3
3
  export class ForbiddenError extends NamedError {
4
4
  constructor(message = "FORBIDDEN") {
@@ -1,4 +1,4 @@
1
- import {NamedError} from "./named";
1
+ import { NamedError } from "./named";
2
2
 
3
3
  export class NotFoundError extends NamedError {
4
4
  constructor(message = "NOT FOUND") {
@@ -1,4 +1,4 @@
1
- import {NamedError} from "./named";
1
+ import { NamedError } from "./named";
2
2
 
3
3
  export class MethodNotImplementedError extends NamedError {
4
4
  constructor(message = "NOT IMPLEMENTED") {
@@ -1,4 +1,4 @@
1
- import {NamedError} from "./named";
1
+ import { NamedError } from "./named";
2
2
 
3
3
  export class ParseOptionsError extends NamedError {
4
4
  constructor(message = "BAD REQUEST", public argAttr?: string) {
@@ -7,4 +7,9 @@ export class ParseOptionsError extends NamedError {
7
7
  }
8
8
  }
9
9
 
10
- export class BadRequestError extends ParseOptionsError { }
10
+ export class BadRequestError extends NamedError {
11
+ constructor(message = "BAD REQUEST") {
12
+ super(message);
13
+ this.name = "BadRequestError";
14
+ }
15
+ }
@@ -1,4 +1,4 @@
1
- import {NamedError} from "./named";
1
+ import { NamedError } from "./named";
2
2
 
3
3
  export class UnAuthorizedError extends NamedError {
4
4
  constructor(message = "UNAUTHORIZED") {
package/src/util/index.ts CHANGED
@@ -6,6 +6,3 @@ export * from "./config";
6
6
  export * from "./logger";
7
7
  export * from "./session";
8
8
  export * from "./loader";
9
- export * from "./test";
10
- //export * from "./uuid";
11
- //export * from "./cookie";
@@ -3,9 +3,8 @@ import { Logger } from "../logger";
3
3
  import { existsSync, readdirSync, readFileSync } from "fs";
4
4
  import { ConfigOutput, ConfigPathResolver, ConfigStore, LoadConfigOut } from "../config";
5
5
  import { ConfigFileNotFoundError } from "../error";
6
- import { RequestOptions, request } from "../request";
7
6
 
8
- export const loadConfigString = (envFileContent: string, combined?: ConfigOutput): ConfigOutput => {
7
+ const loadConfigString = (envFileContent: string, combined?: ConfigOutput): ConfigOutput => {
9
8
  const ret: ConfigOutput = {};
10
9
  envFileContent.split("\n")
11
10
  .filter(value => value && value.length > 0 && value.substr(0, 1) !== "#")
@@ -26,11 +25,6 @@ export const loadConfigString = (envFileContent: string, combined?: ConfigOutput
26
25
  return ret;
27
26
  }
28
27
 
29
- export const loadRemoteConfigFile = async (options: RequestOptions, logger?: Logger): Promise<ConfigOutput> => {
30
- const { data } = await request(options, logger);
31
- return loadConfigString(data);
32
- }
33
-
34
28
  export const loadConfigFile = (envFilePath: string, combined?: ConfigOutput, logger?: Logger): ConfigOutput => {
35
29
  if (!existsSync(envFilePath)) {
36
30
  throw new ConfigFileNotFoundError(`config file [${envFilePath}] doesnt exists!`);
@@ -4,6 +4,6 @@ export type LoggerFormatter = (args: { identifier: string, level: LogLevel, mess
4
4
 
5
5
  export const defaultLoggerFormatter: LoggerFormatter =
6
6
  ({ identifier, level, message }) => `${new Date().toISOString()} ${process.pid} ` +
7
- `[${identifier}] ` +
7
+ `${identifier ? `[${identifier}] ` : ""}` +
8
8
  `${level !== "info" ? (level === "error" || level === "warn" ? `[${level.toUpperCase()}] ` : `[${level}] `) : ""}` +
9
9
  `${message}`;
@@ -78,4 +78,4 @@ export interface ParserInterface {
78
78
  options: ParseOption[] | ParseOptionMap,
79
79
  mode?: ParseOptionsMode,
80
80
  ignoreUndefined?: boolean): any
81
- }
81
+ }
@@ -1,6 +1,3 @@
1
1
  export * from "./parser";
2
2
  export * from "./common";
3
3
  export * from "./get";
4
-
5
-
6
-
@@ -5,7 +5,7 @@ import { parseRedirectLocation, RequestOptions, RequestResponse, ResponseError,
5
5
  import { gunzipSync } from "zlib";
6
6
  import { isBrowser } from "../config/browser";
7
7
 
8
- export { RequestOptions, RequestResponse, ResponseError } from "./common";
8
+ export { RequestOptions, RequestResponse } from "./common";
9
9
 
10
10
  const DEFAULT_USER_AGENT = "curl/7.69.1";
11
11
  const CONTENT_TYPE_HEADER = "Content-Type";
@@ -227,4 +227,4 @@ export const request = (options: RequestOptions, logger?: Logger): Promise<Reque
227
227
  }
228
228
  });
229
229
  }
230
- }
230
+ }
@@ -1,4 +1,3 @@
1
- import { ParseOptionsError } from "../error";
2
1
  import { SimpleMap, SimpleTypes } from "../parser/common";
3
2
 
4
3
  export interface NoTokenSession extends SimpleMap<SimpleTypes> {
@@ -19,45 +18,3 @@ export interface GroupPolicy {
19
18
  groups: GroupPolicyGroups[];
20
19
  groupPolicy: GroupPolicyType;
21
20
  }
22
-
23
- export const policyCheck = (session: Session, options: GroupPolicy): boolean => {
24
- switch (options.groupPolicy) {
25
- case "at_least_one":
26
- for (const group of options.groups) {
27
- if (group instanceof Array) {
28
- let ret = true;
29
- for (const g of group) {
30
- if (session.groups.indexOf(g) === -1) {
31
- ret = false;
32
- break;
33
- }
34
- }
35
- if (ret) {
36
- return true;
37
- }
38
- } else {
39
- if (session.groups.indexOf(group) !== -1) {
40
- return true;
41
- }
42
- }
43
- }
44
- return false;
45
- case "all":
46
- for (const group of options.groups) {
47
- if (group instanceof Array) {
48
- for (const g of group) {
49
- if (session.groups.indexOf(g) === -1) {
50
- return false;
51
- }
52
- }
53
- } else {
54
- if (session.groups.indexOf(group) === -1) {
55
- return false;
56
- }
57
- }
58
- }
59
- return true;
60
- default:
61
- throw new ParseOptionsError(`policy [${options.groupPolicy}] not implemented!!`);
62
- }
63
- };
@@ -1,2 +1,2 @@
1
1
  export * from "./common";
2
- export * from "./service";
2
+ export * from "./service";