@miqro/core 1.3.6 → 1.3.8
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/README.md +38 -96
- package/dist/handler/api-router-utils.d.ts +12 -1
- package/dist/handler/api-router-utils.js +86 -1
- package/dist/handler/api-router-utils.js.map +1 -1
- package/dist/handler/api-router.d.ts +29 -2
- package/dist/handler/api-router.js +2 -2
- package/dist/handler/api-router.js.map +1 -1
- package/dist/handler/app.d.ts +2 -1
- package/dist/handler/app.js +4 -2
- package/dist/handler/app.js.map +1 -1
- package/dist/handler/common/context.d.ts +1 -1
- package/dist/handler/common/context.js +14 -9
- package/dist/handler/common/context.js.map +1 -1
- package/dist/handler/common/router-utils.js +60 -5
- package/dist/handler/common/router-utils.js.map +1 -1
- package/dist/handler/index.d.ts +0 -3
- package/dist/handler/index.js +0 -3
- package/dist/handler/index.js.map +1 -1
- package/dist/handler/middleware/form.js +7 -1
- package/dist/handler/middleware/form.js.map +1 -1
- package/dist/handler/middleware/group.js +44 -2
- package/dist/handler/middleware/group.js.map +1 -1
- package/dist/handler/middleware/index.js +2 -6
- package/dist/handler/middleware/index.js.map +1 -1
- package/dist/handler/middleware/json.js +8 -2
- package/dist/handler/middleware/json.js.map +1 -1
- package/dist/handler/middleware/text.js +8 -1
- package/dist/handler/middleware/text.js.map +1 -1
- package/dist/handler/proxy.d.ts +2 -5
- package/dist/handler/proxy.js +2 -3
- package/dist/handler/proxy.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/{util/test → test/common}/app.d.ts +2 -2
- package/dist/{util/test → test/common}/app.js +3 -2
- package/dist/test/common/app.js.map +1 -0
- package/dist/{util/test → test/common}/common.d.ts +0 -0
- package/dist/{util/test → test/common}/common.js +0 -0
- package/dist/test/common/common.js.map +1 -0
- package/dist/{util/test → test/common}/index.d.ts +1 -0
- package/dist/{util/test → test/common}/index.js +1 -0
- package/dist/test/common/index.js.map +1 -0
- package/dist/test/common/runner.d.ts +13 -0
- package/dist/test/common/runner.js +92 -0
- package/dist/test/common/runner.js.map +1 -0
- package/dist/test/index.d.ts +1 -0
- package/dist/{handler/response → test}/index.js +1 -3
- package/dist/test/index.js.map +1 -0
- package/dist/test/main.d.ts +1 -0
- package/dist/test/main.js +19 -0
- package/dist/test/main.js.map +1 -0
- package/dist/util/cli/index.d.ts +1 -1
- package/dist/util/error/parsererror.d.ts +2 -1
- package/dist/util/error/parsererror.js +5 -1
- package/dist/util/error/parsererror.js.map +1 -1
- package/dist/util/index.d.ts +0 -1
- package/dist/util/index.js +0 -1
- package/dist/util/index.js.map +1 -1
- package/dist/util/loader/index.d.ts +0 -3
- package/dist/util/loader/index.js +2 -9
- package/dist/util/loader/index.js.map +1 -1
- package/dist/util/request/index.d.ts +1 -1
- package/dist/util/request/index.js +1 -3
- package/dist/util/request/index.js.map +1 -1
- package/dist/util/session/common.d.ts +0 -1
- package/dist/util/session/common.js +0 -46
- package/dist/util/session/common.js.map +1 -1
- package/package.json +6 -11
- package/src/handler/api-router-utils.ts +91 -2
- package/src/handler/api-router.ts +30 -4
- package/src/handler/app.ts +2 -1
- package/src/handler/common/context.ts +14 -10
- package/src/handler/common/router-utils.ts +199 -144
- package/src/handler/index.ts +0 -3
- package/src/handler/middleware/form.ts +8 -2
- package/src/handler/middleware/group.ts +44 -2
- package/src/handler/middleware/index.ts +0 -4
- package/src/handler/middleware/json.ts +9 -3
- package/src/handler/middleware/text.ts +8 -1
- package/src/handler/proxy.ts +3 -8
- package/src/index.ts +1 -0
- package/src/{util/test → test/common}/app.ts +6 -5
- package/src/{util/test → test/common}/common.ts +0 -0
- package/src/{util/test → test/common}/index.ts +1 -0
- package/src/test/common/runner.ts +97 -0
- package/src/test/index.ts +1 -0
- package/src/test/main.ts +17 -0
- package/src/util/cli/index.ts +1 -1
- package/src/util/error/parsererror.ts +6 -1
- package/src/util/index.ts +0 -1
- package/src/util/loader/index.ts +1 -7
- package/src/util/request/index.ts +1 -1
- package/src/util/session/common.ts +0 -43
- package/tsconfig-test.json +13 -0
- package/dist/handler/api-handler.d.ts +0 -29
- package/dist/handler/api-handler.js +0 -42
- package/dist/handler/api-handler.js.map +0 -1
- package/dist/handler/feature-router.d.ts +0 -12
- package/dist/handler/feature-router.js +0 -90
- package/dist/handler/feature-router.js.map +0 -1
- package/dist/handler/response/html.d.ts +0 -10
- package/dist/handler/response/html.js +0 -34
- package/dist/handler/response/html.js.map +0 -1
- package/dist/handler/response/index.d.ts +0 -3
- package/dist/handler/response/index.js.map +0 -1
- package/dist/handler/response/json.d.ts +0 -2
- package/dist/handler/response/json.js +0 -21
- package/dist/handler/response/json.js.map +0 -1
- package/dist/handler/response/result.d.ts +0 -4
- package/dist/handler/response/result.js +0 -46
- package/dist/handler/response/result.js.map +0 -1
- package/dist/util/client.d.ts +0 -17
- package/dist/util/client.js +0 -55
- package/dist/util/client.js.map +0 -1
- package/dist/util/test/app.js.map +0 -1
- package/dist/util/test/common.js.map +0 -1
- package/dist/util/test/index.js.map +0 -1
- package/src/handler/api-handler.ts +0 -55
- package/src/handler/feature-router.ts +0 -92
- package/src/handler/response/html.ts +0 -36
- package/src/handler/response/index.ts +0 -3
- package/src/handler/response/json.ts +0 -19
- package/src/handler/response/result.ts +0 -40
- package/src/util/client.ts +0 -64
|
@@ -3,182 +3,237 @@ import { Context, Handler } from "./context";
|
|
|
3
3
|
import { Method } from "./context";
|
|
4
4
|
import { matchTokenizePath, PathPartToken, tokenizePath } from "./tokenize-match"
|
|
5
5
|
import { ParseRequest } from "../middleware/parse";
|
|
6
|
-
import { Parser, ParseOptions } from "../../util/parser";
|
|
6
|
+
import { Parser, ParseOptions, parse } from "../../util/parser";
|
|
7
7
|
import { SessionHandler, SessionHandlerOptions } from "../middleware/session";
|
|
8
8
|
import { GroupPolicyHandler } from "../middleware/group";
|
|
9
|
-
import { ResultParser } from "../response/result";
|
|
10
|
-
import { ResponseHandler } from "../response/json";
|
|
11
9
|
import { GroupPolicy } from "../../util/session";
|
|
12
10
|
|
|
11
|
+
const ResultParser = (options: ParseOptions | ParseOptions[], parser?: Parser): Handler<any> =>
|
|
12
|
+
async (ctx: Context): Promise<any> => {
|
|
13
|
+
if (!ctx.results || ctx.results.length === 0) {
|
|
14
|
+
ctx.logger.debug(`not parsing results`);
|
|
15
|
+
return undefined;
|
|
16
|
+
} else {
|
|
17
|
+
const index = ctx.results.length - 1;
|
|
18
|
+
const lastResult = ctx.results[index];
|
|
19
|
+
ctx.logger.trace(`parsing lastResult[%o]`, lastResult);
|
|
20
|
+
try {
|
|
21
|
+
if (options instanceof Array) {
|
|
22
|
+
for (let i = 0; i < options.length; i++) {
|
|
23
|
+
const o = options[i];
|
|
24
|
+
try {
|
|
25
|
+
const mappedResults = (parser ? parser.parse : parse)(`ctx.results[${index}]`, lastResult, o.options, o.mode, o.ignoreUndefined);
|
|
26
|
+
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
27
|
+
return mappedResults;
|
|
28
|
+
} catch (e) {
|
|
29
|
+
if (i === options.length - 1) {
|
|
30
|
+
throw e;
|
|
31
|
+
} else {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
const mappedResults = (parser ? parser.parse : parse)(`ctx.results[${index}]`, lastResult, options.options, options.mode, options.ignoreUndefined);
|
|
38
|
+
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
39
|
+
return mappedResults;
|
|
40
|
+
}
|
|
41
|
+
} catch (e) {
|
|
42
|
+
ctx.logger.error(`error parsing lastResult[%o]`, lastResult);
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
const ResponseHandler = (): Handler<void> =>
|
|
50
|
+
async (ctx: Context): Promise<void> => {
|
|
51
|
+
ctx.logger.trace("results [%o]", ctx.results);
|
|
52
|
+
if (!ctx.results || ctx.results.length === 0) {
|
|
53
|
+
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
54
|
+
} else {
|
|
55
|
+
const lastResult = ctx.results[ctx.results.length - 1];
|
|
56
|
+
ctx.logger.debug("response [%o]", lastResult);
|
|
57
|
+
if (lastResult !== undefined) {
|
|
58
|
+
await ctx.json(lastResult);
|
|
59
|
+
} else {
|
|
60
|
+
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
13
68
|
export interface HandlerWithOptions extends RouterHandlerOptions {
|
|
14
|
-
|
|
69
|
+
handler: Handler | Array<Handler>;
|
|
15
70
|
}
|
|
16
71
|
|
|
17
72
|
export interface RouteOptions {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
73
|
+
name?: string;
|
|
74
|
+
description?: string;
|
|
75
|
+
policy?: GroupPolicy;
|
|
76
|
+
request?: {
|
|
77
|
+
headers?: ParseOptions | ParseOptions[];
|
|
78
|
+
query?: ParseOptions | false | ParseOptions[];
|
|
79
|
+
params?: ParseOptions | false | ParseOptions[];
|
|
80
|
+
body?: ParseOptions | false | ParseOptions[];
|
|
81
|
+
};
|
|
82
|
+
response?: true | ParseOptions | ParseOptions[];
|
|
28
83
|
}
|
|
29
84
|
|
|
30
85
|
export interface RouteJSONDoc extends RouteOptions {
|
|
31
|
-
|
|
86
|
+
identifier: string;
|
|
32
87
|
}
|
|
33
88
|
|
|
34
89
|
export interface RouterJSONDoc {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
90
|
+
[path: string]: {
|
|
91
|
+
[method: string]: RouteJSONDoc
|
|
92
|
+
}
|
|
38
93
|
}
|
|
39
94
|
|
|
40
95
|
export interface RouterHandlerOptions extends RouteOptions {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
96
|
+
parser?: Parser;
|
|
97
|
+
middleware?: Handler | Handler[];
|
|
98
|
+
session?: SessionHandlerOptions | Handler;
|
|
44
99
|
}
|
|
45
100
|
|
|
46
101
|
export class RouterHandler {
|
|
47
102
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
77
|
-
throw new Error("bad arguments");
|
|
78
|
-
}
|
|
79
|
-
|
|
103
|
+
private readonly tokens: PathPartToken[];
|
|
104
|
+
private readonly handler: Array<Handler | Handler | Router>;
|
|
105
|
+
private readonly isRouter: boolean;
|
|
106
|
+
private readonly options?: RouterHandlerOptions;
|
|
107
|
+
|
|
108
|
+
public constructor(handler: Array<Handler> | HandlerWithOptions | Handler | Router, private readonly path: string | undefined, private readonly method: Method | undefined, options?: RouterHandlerOptions) {
|
|
109
|
+
this.tokens = tokenizePath(path);
|
|
110
|
+
|
|
111
|
+
let handlers: Array<Handler | Router> = []
|
|
112
|
+
this.isRouter = handler instanceof Router;
|
|
113
|
+
|
|
114
|
+
if (!this.isRouter && !(handler instanceof Array) && typeof handler === "object") {
|
|
115
|
+
// HandlerWithOptions
|
|
116
|
+
if (options) {
|
|
117
|
+
throw new Error("bad arguments. cannot send options with HandlerWithOptions");
|
|
118
|
+
}
|
|
119
|
+
const realHandler = (handler as HandlerWithOptions).handler;
|
|
120
|
+
const newOptions = {
|
|
121
|
+
...(handler as HandlerWithOptions)
|
|
122
|
+
};
|
|
123
|
+
delete (newOptions as any).handler;
|
|
124
|
+
handler = realHandler;
|
|
125
|
+
this.options = newOptions;
|
|
126
|
+
} else {
|
|
127
|
+
this.options = options;
|
|
128
|
+
}
|
|
129
|
+
const routeHandlers = handler instanceof Array ? handler : [handler] as Array<Handler | Router>;
|
|
80
130
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (this.options && this.options.session) {
|
|
85
|
-
handlers.push(typeof this.options.session !== "function" ? SessionHandler(this.options.session) : this.options.session);
|
|
86
|
-
}
|
|
87
|
-
if (this.options && this.options.policy) {
|
|
88
|
-
handlers.push(GroupPolicyHandler(this.options.policy));
|
|
89
|
-
}
|
|
90
|
-
if (this.options && this.options.request) {
|
|
91
|
-
handlers.push(ParseRequest(this.options.request));
|
|
92
|
-
}
|
|
131
|
+
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
132
|
+
throw new Error("bad arguments");
|
|
133
|
+
}
|
|
93
134
|
|
|
94
|
-
handlers = handlers.concat(routeHandlers);
|
|
95
135
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
136
|
+
if (this.options && this.options.middleware) {
|
|
137
|
+
handlers = handlers.concat(this.options.middleware);
|
|
138
|
+
}
|
|
139
|
+
if (this.options && this.options.session) {
|
|
140
|
+
handlers.push(typeof this.options.session !== "function" ? SessionHandler(this.options.session) : this.options.session);
|
|
141
|
+
}
|
|
142
|
+
if (this.options && this.options.policy) {
|
|
143
|
+
handlers.push(GroupPolicyHandler(this.options.policy));
|
|
144
|
+
}
|
|
145
|
+
if (this.options && this.options.request) {
|
|
146
|
+
handlers.push(ParseRequest(this.options.request));
|
|
105
147
|
}
|
|
106
148
|
|
|
107
|
-
|
|
108
|
-
const nPath = this.path && this.path.length > 1 ? this.path.substring(1) : "";
|
|
109
|
-
const nMethod = this.method ? this.method : "";
|
|
149
|
+
handlers = handlers.concat(routeHandlers);
|
|
110
150
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
119
|
-
throw new Error("cannot create doc routes override each other");
|
|
120
|
-
}
|
|
121
|
-
doc[`${prePath}${nPath}`][nMethod] = {
|
|
122
|
-
description: this.options.description,
|
|
123
|
-
name: this.options.name,
|
|
124
|
-
identifier: `${prePath}${nPath}${this.options.name ? this.options.name : ""}${nMethod ? `_${nMethod}` : ""}`.replace(/[^a-z0-9+]+/gi, '_').split("_").map(s => s.trim()).filter(s => s).join("_").toUpperCase(),
|
|
125
|
-
policy: this.options.policy,
|
|
126
|
-
request: this.options.request ? {
|
|
127
|
-
body: this.options.request.body,
|
|
128
|
-
headers: this.options.request.headers,
|
|
129
|
-
params: this.options.request.params,
|
|
130
|
-
query: this.options.request.query,
|
|
131
|
-
} : undefined,
|
|
132
|
-
response: this.options.response
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
return doc;
|
|
151
|
+
if (this.options && this.options.response) {
|
|
152
|
+
if (typeof this.options.response === "boolean") {
|
|
153
|
+
(handlers as Handler[]).push(ResponseHandler());
|
|
154
|
+
} else {
|
|
155
|
+
(handlers as Handler[]).push(ResultParser(this.options.response, this.options.parser));
|
|
156
|
+
(handlers as Handler[]).push(ResponseHandler());
|
|
157
|
+
}
|
|
136
158
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
159
|
+
this.handler = handlers;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
|
|
163
|
+
const nPath = this.path && this.path.length > 1 ? this.path.substring(1) : "";
|
|
164
|
+
const nMethod = this.method ? this.method : "";
|
|
165
|
+
|
|
166
|
+
if (this.isRouter) {
|
|
167
|
+
const router: Router = this.handler[0] as Router;
|
|
168
|
+
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
169
|
+
} else if (this.options) {
|
|
170
|
+
if (!doc[`${prePath}${nPath}`]) {
|
|
171
|
+
doc[`${prePath}${nPath}`] = {};
|
|
172
|
+
}
|
|
173
|
+
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
174
|
+
throw new Error("cannot create doc routes override each other");
|
|
175
|
+
}
|
|
176
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
177
|
+
description: this.options.description,
|
|
178
|
+
name: this.options.name,
|
|
179
|
+
identifier: `${prePath}${nPath}${this.options.name ? this.options.name : ""}${nMethod ? `_${nMethod}` : ""}`.replace(/[^a-z0-9+]+/gi, '_').split("_").map(s => s.trim()).filter(s => s).join("_").toUpperCase(),
|
|
180
|
+
policy: this.options.policy,
|
|
181
|
+
request: this.options.request ? {
|
|
182
|
+
body: this.options.request.body,
|
|
183
|
+
headers: this.options.request.headers,
|
|
184
|
+
params: this.options.request.params,
|
|
185
|
+
query: this.options.request.query,
|
|
186
|
+
} : undefined,
|
|
187
|
+
response: this.options.response
|
|
188
|
+
};
|
|
152
189
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (this.path === undefined) {
|
|
163
|
-
return {
|
|
164
|
-
match: true
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
return matchTokenizePath(this.isRouter, prePathTokens.concat(this.tokens), pathTokens);
|
|
190
|
+
return doc;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
public async invoke(ctx: Context, pathTokens: { value: string; lower: string; }[], prePathTokens: PathPartToken[]): Promise<boolean> {
|
|
194
|
+
for (let i = 0; i < this.handler.length; i++) {
|
|
195
|
+
const h = this.handler[i];
|
|
196
|
+
if (h instanceof Router) {
|
|
197
|
+
if (!this.pushResults(await h.run(ctx, pathTokens, prePathTokens.concat(this.tokens)), ctx)) {
|
|
198
|
+
return false;
|
|
168
199
|
}
|
|
200
|
+
} else {
|
|
201
|
+
if (!this.pushResults(await h(ctx), ctx)) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public isMatch(
|
|
210
|
+
method: string,
|
|
211
|
+
pathTokens: { value: string; lower: string; }[],
|
|
212
|
+
prePathTokens: PathPartToken[]): {
|
|
213
|
+
match: boolean;
|
|
214
|
+
params?: { [name: string]: string | undefined }
|
|
215
|
+
} {
|
|
216
|
+
if ((this.method === undefined || this.method === method)) {
|
|
217
|
+
if (this.path === undefined) {
|
|
169
218
|
return {
|
|
170
|
-
|
|
219
|
+
match: true
|
|
171
220
|
};
|
|
221
|
+
}
|
|
222
|
+
return matchTokenizePath(this.isRouter, prePathTokens.concat(this.tokens), pathTokens);
|
|
172
223
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
224
|
+
return {
|
|
225
|
+
match: false
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private pushResults(result: boolean, ctx: Context): boolean {
|
|
230
|
+
if (result === false) {
|
|
231
|
+
// ctx.logger.debug(`avoiding next handlers because handler returned false.`);
|
|
232
|
+
return false;
|
|
233
|
+
} else if (result !== true && result !== undefined) {
|
|
234
|
+
ctx.logger.trace("pushing to results [%o]", result);
|
|
235
|
+
ctx.results.push(result);
|
|
183
236
|
}
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
184
239
|
}
|
package/src/handler/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { parse as queryParse } from "querystring";
|
|
|
2
2
|
import { Context, Handler } from "../common";
|
|
3
3
|
import { BadRequestError } from "../../util/error";
|
|
4
4
|
import { checkEnvVariables } from "../../util/config";
|
|
5
|
-
import { DEFAULT_READ_BUFFER_LIMIT } from "./buffer";
|
|
5
|
+
import { ReadBuffer, DEFAULT_READ_BUFFER_LIMIT } from "./buffer";
|
|
6
6
|
|
|
7
7
|
const DEFAULT_FORM_MAX_KEYS = undefined;
|
|
8
8
|
|
|
@@ -25,9 +25,15 @@ export const URLEncodedParser = (options?: {
|
|
|
25
25
|
type = typeS;
|
|
26
26
|
maxKeys = maxKeysS !== "undefined" ? parseInt(maxKeysS, 10) : DEFAULT_FORM_MAX_KEYS;
|
|
27
27
|
}
|
|
28
|
+
const readBuffer = ReadBuffer({
|
|
29
|
+
limit
|
|
30
|
+
});
|
|
28
31
|
return async (ctx: Context): Promise<void> => {
|
|
29
32
|
try {
|
|
30
33
|
const isType = ctx.body === undefined && ctx.headers["content-type"] ? ctx.headers["content-type"].toLocaleLowerCase().indexOf(type.toLocaleLowerCase()) !== -1 : false;
|
|
34
|
+
if(isType && !ctx.buffer) {
|
|
35
|
+
await readBuffer(ctx);
|
|
36
|
+
}
|
|
31
37
|
if (isType && ctx.buffer && ctx.buffer.length <= limit) {
|
|
32
38
|
const string = ctx.buffer.toString();
|
|
33
39
|
if (string) {
|
|
@@ -42,7 +48,7 @@ export const URLEncodedParser = (options?: {
|
|
|
42
48
|
throw new BadRequestError(`buffer.length ${ctx.buffer.length} > ${limit}`);
|
|
43
49
|
}
|
|
44
50
|
} catch (e: any) {
|
|
45
|
-
if (e && e.name === "
|
|
51
|
+
if (e && e.name === "BadRequestError") {
|
|
46
52
|
throw e;
|
|
47
53
|
} else {
|
|
48
54
|
ctx.logger.error(e);
|
|
@@ -1,7 +1,49 @@
|
|
|
1
|
-
import { GroupPolicy,
|
|
2
|
-
import { ForbiddenError, UnAuthorizedError } from "../../util/error";
|
|
1
|
+
import { GroupPolicy, Session } from "../../util/session";
|
|
2
|
+
import { ForbiddenError, UnAuthorizedError, ParseOptionsError } from "../../util/error";
|
|
3
3
|
import { Context, Handler } from "../common";
|
|
4
4
|
|
|
5
|
+
const policyCheck = (session: Session, options: GroupPolicy): boolean => {
|
|
6
|
+
switch (options.groupPolicy) {
|
|
7
|
+
case "at_least_one":
|
|
8
|
+
for (const group of options.groups) {
|
|
9
|
+
if (group instanceof Array) {
|
|
10
|
+
let ret = true;
|
|
11
|
+
for (const g of group) {
|
|
12
|
+
if (session.groups.indexOf(g) === -1) {
|
|
13
|
+
ret = false;
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (ret) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
if (session.groups.indexOf(group) !== -1) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
case "all":
|
|
28
|
+
for (const group of options.groups) {
|
|
29
|
+
if (group instanceof Array) {
|
|
30
|
+
for (const g of group) {
|
|
31
|
+
if (session.groups.indexOf(g) === -1) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
if (session.groups.indexOf(group) === -1) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
default:
|
|
43
|
+
throw new ParseOptionsError(`policy [${options.groupPolicy}] not implemented!!`);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
5
47
|
export const GroupPolicyHandler = (options: GroupPolicy): Handler<void> => {
|
|
6
48
|
return async (ctx: Context): Promise<void> => {
|
|
7
49
|
try {
|
|
@@ -5,7 +5,6 @@ import { LoggerHandler } from "./logger";
|
|
|
5
5
|
import { TagResponse } from "./tag";
|
|
6
6
|
import { CORS } from "./cors";
|
|
7
7
|
import { CookieParser } from "./cookie";
|
|
8
|
-
import { ReadBuffer } from "./buffer";
|
|
9
8
|
import { JSONParser } from "./json";
|
|
10
9
|
import { TextParser } from "./text";
|
|
11
10
|
import { URLEncodedParser } from "./form";
|
|
@@ -33,9 +32,6 @@ export const middleware = (): Handler[] => {
|
|
|
33
32
|
if (isFeatureEnabled("CORS", false)) {
|
|
34
33
|
ret.push(CORS());
|
|
35
34
|
}
|
|
36
|
-
if (isFeatureEnabled("READ_BUFFER", true)) {
|
|
37
|
-
ret.push(ReadBuffer());
|
|
38
|
-
}
|
|
39
35
|
if (isFeatureEnabled("COOKIE_PARSER", true)) {
|
|
40
36
|
ret.push(CookieParser());
|
|
41
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context, Handler } from "../common";
|
|
2
2
|
import { BadRequestError } from "../../util/error";
|
|
3
3
|
import { checkEnvVariables } from "../../util/config";
|
|
4
|
-
import { DEFAULT_READ_BUFFER_LIMIT } from "./buffer";
|
|
4
|
+
import { DEFAULT_READ_BUFFER_LIMIT, ReadBuffer } from "./buffer";
|
|
5
5
|
|
|
6
6
|
export const JSONParser = (options?: {
|
|
7
7
|
limit?: number;
|
|
@@ -22,15 +22,21 @@ export const JSONParser = (options?: {
|
|
|
22
22
|
limit = parseInt(limitS, 10);
|
|
23
23
|
type = typeS;
|
|
24
24
|
}
|
|
25
|
+
const readBuffer = ReadBuffer({
|
|
26
|
+
limit
|
|
27
|
+
});
|
|
25
28
|
return async (ctx: Context): Promise<void> => {
|
|
26
29
|
try {
|
|
27
30
|
const isType = ctx.body === undefined && ctx.headers["content-type"] ? ctx.headers["content-type"].toLocaleLowerCase().indexOf(type.toLocaleLowerCase()) !== -1 : false;
|
|
31
|
+
if(isType && !ctx.buffer) {
|
|
32
|
+
await readBuffer(ctx);
|
|
33
|
+
}
|
|
28
34
|
if (isType && ctx.buffer && ctx.buffer.length <= limit) {
|
|
29
35
|
const string = ctx.buffer.toString();
|
|
30
36
|
if (string) {
|
|
31
37
|
const parsed = JSON.parse(string);
|
|
32
38
|
if (parsed instanceof Array && strict) {
|
|
33
|
-
throw new BadRequestError(`body cannot be an array
|
|
39
|
+
throw new BadRequestError(`body cannot be an array`);
|
|
34
40
|
}
|
|
35
41
|
ctx.body = parsed;
|
|
36
42
|
}
|
|
@@ -39,7 +45,7 @@ export const JSONParser = (options?: {
|
|
|
39
45
|
throw new BadRequestError(`buffer.length ${ctx.buffer.length} > ${limit}`);
|
|
40
46
|
}
|
|
41
47
|
} catch (e: any) {
|
|
42
|
-
if (e && e.name === "
|
|
48
|
+
if (e && e.name === "BadRequestError") {
|
|
43
49
|
throw e;
|
|
44
50
|
} else {
|
|
45
51
|
ctx.logger.error(e);
|
|
@@ -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 === "
|
|
39
|
+
if (e && e.name === "BadRequestError") {
|
|
33
40
|
throw e;
|
|
34
41
|
} else {
|
|
35
42
|
ctx.logger.error(e);
|
package/src/handler/proxy.ts
CHANGED
|
@@ -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
|
-
|
|
5
|
+
resolve(ctx: Context): Promise<RequestOptions>;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export
|
|
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
|
|
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);
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import { request, RequestOptions, RequestResponse
|
|
1
|
+
import { request, RequestOptions, RequestResponse } from "../../util/request";
|
|
2
2
|
import { App } from "../../handler/app";
|
|
3
3
|
import { existsSync, unlinkSync } from "fs";
|
|
4
4
|
import { createServer } from "http";
|
|
5
5
|
|
|
6
|
-
export const TestHelper = async (app: App, options: RequestOptions, cb?: (response: RequestResponse) => void): Promise<RequestResponse
|
|
6
|
+
export const TestHelper = async (app: App, options: RequestOptions, cb?: (response: RequestResponse) => void): Promise<RequestResponse> => {
|
|
7
7
|
const unixSocket = `/tmp/socket.test.helper${Date.now()}`;
|
|
8
8
|
if (existsSync(unixSocket)) {
|
|
9
9
|
unlinkSync(unixSocket);
|
|
10
10
|
}
|
|
11
11
|
const server = createServer(app.listener);
|
|
12
|
-
return new Promise<RequestResponse
|
|
12
|
+
return new Promise<RequestResponse>((resolve, reject) => {
|
|
13
13
|
server.listen(unixSocket, () => {
|
|
14
14
|
request({
|
|
15
|
+
disableThrow: true,
|
|
15
16
|
...options,
|
|
16
17
|
socketPath: unixSocket
|
|
17
18
|
}).then((response) => {
|
|
@@ -25,7 +26,7 @@ export const TestHelper = async (app: App, options: RequestOptions, cb?: (respon
|
|
|
25
26
|
}
|
|
26
27
|
resolve(response);
|
|
27
28
|
});
|
|
28
|
-
}).catch((e:
|
|
29
|
+
}).catch((e: any) => {
|
|
29
30
|
server.close(() => {
|
|
30
31
|
if (cb) {
|
|
31
32
|
try {
|
|
@@ -33,7 +34,7 @@ export const TestHelper = async (app: App, options: RequestOptions, cb?: (respon
|
|
|
33
34
|
} catch (ee) {
|
|
34
35
|
reject(ee);
|
|
35
36
|
}
|
|
36
|
-
resolve();
|
|
37
|
+
resolve(undefined as any);
|
|
37
38
|
} else {
|
|
38
39
|
reject(e);
|
|
39
40
|
}
|
|
File without changes
|