@miqro/core 1.4.2 → 1.5.4
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 +100 -53
- package/dist/api-router.d.ts +36 -0
- package/dist/{handler/api-router.js → api-router.js} +1 -1
- package/dist/api-router.js.map +1 -0
- package/dist/{handler/app.d.ts → app.d.ts} +1 -1
- package/dist/{handler/app.js → app.js} +19 -15
- package/dist/app.js.map +1 -0
- package/dist/common/api-router-helpers.d.ts +31 -0
- package/dist/{util/session/common.js → common/api-router-helpers.js} +1 -1
- package/dist/common/api-router-helpers.js.map +1 -0
- package/dist/{handler → common}/api-router-utils.d.ts +9 -6
- package/dist/{handler → common}/api-router-utils.js +26 -6
- package/dist/common/api-router-utils.js.map +1 -0
- package/dist/{util → common}/config/config.d.ts +4 -4
- package/dist/{util → common}/config/config.js +31 -28
- package/dist/common/config/config.js.map +1 -0
- package/dist/{util → common}/config/featuretoggle.d.ts +0 -0
- package/dist/{util → common}/config/featuretoggle.js +0 -0
- package/dist/common/config/featuretoggle.js.map +1 -0
- package/dist/{util → common}/config/index.d.ts +1 -1
- package/dist/{util → common}/config/index.js +1 -1
- package/dist/common/config/index.js.map +1 -0
- package/dist/common/config/loader.d.ts +13 -0
- package/dist/{util/loader/index.js → common/config/loader.js} +14 -9
- package/dist/common/config/loader.js.map +1 -0
- package/dist/{handler/common → common}/context.d.ts +5 -5
- package/dist/{handler/common → common}/context.js +4 -17
- package/dist/common/context.js.map +1 -0
- package/dist/common/error/badrequesterror.d.ts +3 -0
- package/dist/common/error/badrequesterror.js +11 -0
- package/dist/common/error/badrequesterror.js.map +1 -0
- package/dist/common/error/configfilenotfound.d.ts +3 -0
- package/dist/{util → common}/error/configfilenotfound.js +1 -2
- package/dist/common/error/configfilenotfound.js.map +1 -0
- package/dist/common/error/forbiddenerror.d.ts +3 -0
- package/dist/{util → common}/error/forbiddenerror.js +1 -2
- package/dist/common/error/forbiddenerror.js.map +1 -0
- package/dist/{util → common}/error/index.d.ts +1 -1
- package/dist/{util → common}/error/index.js +1 -1
- package/dist/common/error/index.js.map +1 -0
- package/dist/common/error/notfound.d.ts +3 -0
- package/dist/{util → common}/error/notfound.js +1 -2
- package/dist/common/error/notfound.js.map +1 -0
- package/dist/common/error/notimplemented.d.ts +3 -0
- package/dist/{util → common}/error/notimplemented.js +1 -2
- package/dist/common/error/notimplemented.js.map +1 -0
- package/dist/common/error/unauthorizederror.d.ts +3 -0
- package/dist/{util → common}/error/unauthorizederror.js +1 -2
- package/dist/common/error/unauthorizederror.js.map +1 -0
- package/dist/{handler/common → common}/helpers.d.ts +0 -0
- package/dist/{handler/common → common}/helpers.js +0 -0
- package/dist/common/helpers.js.map +1 -0
- package/dist/{util → common}/index.d.ts +4 -5
- package/dist/{util → common}/index.js +4 -5
- package/dist/common/index.js.map +1 -0
- package/dist/{util → common}/logger/common.d.ts +0 -0
- package/dist/{util → common}/logger/common.js +0 -0
- package/dist/common/logger/common.js.map +1 -0
- package/dist/{util → common}/logger/factory.d.ts +0 -0
- package/dist/{util → common}/logger/factory.js +8 -2
- package/dist/common/logger/factory.js.map +1 -0
- package/dist/{util → common}/logger/formatter.d.ts +0 -0
- package/dist/{util → common}/logger/formatter.js +0 -0
- package/dist/common/logger/formatter.js.map +1 -0
- package/dist/{util → common}/logger/index.d.ts +1 -1
- package/dist/{util → common}/logger/index.js +1 -0
- package/dist/common/logger/index.js.map +1 -0
- package/dist/{util → common}/logger/logger.d.ts +0 -0
- package/dist/{util → common}/logger/logger.js +0 -0
- package/dist/common/logger/logger.js.map +1 -0
- package/dist/{util → common}/logger/transport.d.ts +0 -0
- package/dist/{util → common}/logger/transport.js +2 -2
- package/dist/common/logger/transport.js.map +1 -0
- package/dist/common/router-helpers.d.ts +3 -0
- package/dist/common/router-helpers.js +22 -0
- package/dist/common/router-helpers.js.map +1 -0
- package/dist/common/router-utils.d.ts +35 -0
- package/dist/common/router-utils.js +67 -0
- package/dist/common/router-utils.js.map +1 -0
- package/dist/{handler/middleware/session.d.ts → common/session/common.d.ts} +16 -3
- package/dist/{util/parser → common/session}/common.js +0 -0
- package/dist/{util/parser → common/session}/common.js.map +1 -1
- package/dist/{util → common}/session/index.d.ts +0 -0
- package/dist/{util → common}/session/index.js +0 -0
- package/dist/common/session/index.js.map +1 -0
- package/dist/{util → common}/session/service.d.ts +3 -3
- package/dist/{util → common}/session/service.js +7 -7
- package/dist/common/session/service.js.map +1 -0
- package/dist/{handler/common → common}/tokenize-match.d.ts +0 -0
- package/dist/{handler/common → common}/tokenize-match.js +0 -0
- package/dist/common/tokenize-match.js.map +1 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/dist/{handler/middleware → middleware}/buffer.d.ts +0 -0
- package/dist/middleware/buffer.js +81 -0
- package/dist/middleware/buffer.js.map +1 -0
- package/dist/{handler/middleware → middleware}/cookie.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/cookie.js +2 -1
- package/dist/middleware/cookie.js.map +1 -0
- package/dist/{handler/middleware → middleware}/cors.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/cors.js +6 -7
- package/dist/middleware/cors.js.map +1 -0
- package/dist/{handler/middleware → middleware}/form.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/form.js +8 -7
- package/dist/middleware/form.js.map +1 -0
- package/dist/middleware/group.d.ts +2 -0
- package/dist/{handler/middleware → middleware}/group.js +7 -7
- package/dist/middleware/group.js.map +1 -0
- package/dist/{handler/middleware → middleware}/index.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/index.js +8 -8
- package/dist/middleware/index.js.map +1 -0
- package/dist/{handler/middleware → middleware}/json.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/json.js +7 -7
- package/dist/middleware/json.js.map +1 -0
- package/dist/{handler/middleware → middleware}/logger.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/logger.js +1 -1
- package/dist/middleware/logger.js.map +1 -0
- package/dist/middleware/parse.d.ts +9 -0
- package/dist/{handler/middleware → middleware}/parse.js +14 -13
- package/dist/middleware/parse.js.map +1 -0
- package/dist/middleware/session.d.ts +2 -0
- package/dist/{handler/middleware → middleware}/session.js +13 -14
- package/dist/middleware/session.js.map +1 -0
- package/dist/{handler/middleware → middleware}/tag.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/tag.js +1 -1
- package/dist/middleware/tag.js.map +1 -0
- package/dist/{handler/middleware → middleware}/text.d.ts +0 -0
- package/dist/{handler/middleware → middleware}/text.js +7 -8
- package/dist/middleware/text.js.map +1 -0
- package/dist/{handler/proxy.d.ts → proxy.d.ts} +1 -1
- package/dist/{handler/proxy.js → proxy.js} +3 -3
- package/dist/proxy.js.map +1 -0
- package/dist/{handler/common/router.d.ts → router.d.ts} +31 -7
- package/dist/router.js +260 -0
- package/dist/router.js.map +1 -0
- package/dist/static.d.ts +11 -0
- package/dist/static.js +192 -0
- package/dist/static.js.map +1 -0
- package/package.json +11 -14
- package/dist/handler/api-router-utils.js.map +0 -1
- package/dist/handler/api-router.d.ts +0 -70
- package/dist/handler/api-router.js.map +0 -1
- package/dist/handler/app.js.map +0 -1
- package/dist/handler/common/context.js.map +0 -1
- package/dist/handler/common/helpers.js.map +0 -1
- package/dist/handler/common/index.d.ts +0 -2
- package/dist/handler/common/index.js +0 -15
- package/dist/handler/common/index.js.map +0 -1
- package/dist/handler/common/router-utils.d.ts +0 -59
- package/dist/handler/common/router-utils.js +0 -189
- package/dist/handler/common/router-utils.js.map +0 -1
- package/dist/handler/common/router.js +0 -93
- package/dist/handler/common/router.js.map +0 -1
- package/dist/handler/common/tokenize-match.js.map +0 -1
- package/dist/handler/index.d.ts +0 -5
- package/dist/handler/index.js +0 -18
- package/dist/handler/index.js.map +0 -1
- package/dist/handler/middleware/buffer.js +0 -79
- package/dist/handler/middleware/buffer.js.map +0 -1
- package/dist/handler/middleware/cookie.js.map +0 -1
- package/dist/handler/middleware/cors.js.map +0 -1
- package/dist/handler/middleware/form.js.map +0 -1
- package/dist/handler/middleware/group.d.ts +0 -3
- package/dist/handler/middleware/group.js.map +0 -1
- package/dist/handler/middleware/index.js.map +0 -1
- package/dist/handler/middleware/json.js.map +0 -1
- package/dist/handler/middleware/logger.js.map +0 -1
- package/dist/handler/middleware/parse.d.ts +0 -11
- package/dist/handler/middleware/parse.js.map +0 -1
- package/dist/handler/middleware/session.js.map +0 -1
- package/dist/handler/middleware/tag.js.map +0 -1
- package/dist/handler/middleware/text.js.map +0 -1
- package/dist/handler/proxy.js.map +0 -1
- package/dist/util/cli/index.d.ts +0 -17
- package/dist/util/cli/index.js +0 -84
- package/dist/util/cli/index.js.map +0 -1
- package/dist/util/config/browser.d.ts +0 -1
- package/dist/util/config/browser.js +0 -10
- package/dist/util/config/browser.js.map +0 -1
- package/dist/util/config/config.js.map +0 -1
- package/dist/util/config/featuretoggle.js.map +0 -1
- package/dist/util/config/index.js.map +0 -1
- package/dist/util/error/configfilenotfound.d.ts +0 -4
- package/dist/util/error/configfilenotfound.js.map +0 -1
- package/dist/util/error/forbiddenerror.d.ts +0 -4
- package/dist/util/error/forbiddenerror.js.map +0 -1
- package/dist/util/error/index.js.map +0 -1
- package/dist/util/error/named.d.ts +0 -3
- package/dist/util/error/named.js +0 -11
- package/dist/util/error/named.js.map +0 -1
- package/dist/util/error/notfound.d.ts +0 -4
- package/dist/util/error/notfound.js.map +0 -1
- package/dist/util/error/notimplemented.d.ts +0 -4
- package/dist/util/error/notimplemented.js.map +0 -1
- package/dist/util/error/parsererror.d.ts +0 -8
- package/dist/util/error/parsererror.js +0 -20
- package/dist/util/error/parsererror.js.map +0 -1
- package/dist/util/error/unauthorizederror.d.ts +0 -4
- package/dist/util/error/unauthorizederror.js.map +0 -1
- package/dist/util/index.js.map +0 -1
- package/dist/util/loader/index.d.ts +0 -4
- package/dist/util/loader/index.js.map +0 -1
- package/dist/util/logger/common.js.map +0 -1
- package/dist/util/logger/factory.js.map +0 -1
- package/dist/util/logger/formatter.js.map +0 -1
- package/dist/util/logger/index.js.map +0 -1
- package/dist/util/logger/logger.js.map +0 -1
- package/dist/util/logger/transport.js.map +0 -1
- package/dist/util/parser/common.d.ts +0 -69
- package/dist/util/parser/get.d.ts +0 -3
- package/dist/util/parser/get.js +0 -44
- package/dist/util/parser/get.js.map +0 -1
- package/dist/util/parser/index.d.ts +0 -3
- package/dist/util/parser/index.js +0 -16
- package/dist/util/parser/index.js.map +0 -1
- package/dist/util/parser/parser.d.ts +0 -35
- package/dist/util/parser/parser.js +0 -198
- package/dist/util/parser/parser.js.map +0 -1
- package/dist/util/parser/types/any.d.ts +0 -2
- package/dist/util/parser/types/any.js +0 -11
- package/dist/util/parser/types/any.js.map +0 -1
- package/dist/util/parser/types/array.d.ts +0 -2
- package/dist/util/parser/types/array.js +0 -59
- package/dist/util/parser/types/array.js.map +0 -1
- package/dist/util/parser/types/boolean.d.ts +0 -2
- package/dist/util/parser/types/boolean.js +0 -12
- package/dist/util/parser/types/boolean.js.map +0 -1
- package/dist/util/parser/types/email.d.ts +0 -2
- package/dist/util/parser/types/email.js +0 -23
- package/dist/util/parser/types/email.js.map +0 -1
- package/dist/util/parser/types/enum.d.ts +0 -2
- package/dist/util/parser/types/enum.js +0 -26
- package/dist/util/parser/types/enum.js.map +0 -1
- package/dist/util/parser/types/index.d.ts +0 -12
- package/dist/util/parser/types/index.js +0 -25
- package/dist/util/parser/types/index.js.map +0 -1
- package/dist/util/parser/types/multiple.d.ts +0 -2
- package/dist/util/parser/types/multiple.js +0 -33
- package/dist/util/parser/types/multiple.js.map +0 -1
- package/dist/util/parser/types/nested.d.ts +0 -2
- package/dist/util/parser/types/nested.js +0 -16
- package/dist/util/parser/types/nested.js.map +0 -1
- package/dist/util/parser/types/number.d.ts +0 -2
- package/dist/util/parser/types/number.js +0 -29
- package/dist/util/parser/types/number.js.map +0 -1
- package/dist/util/parser/types/object.d.ts +0 -2
- package/dist/util/parser/types/object.js +0 -11
- package/dist/util/parser/types/object.js.map +0 -1
- package/dist/util/parser/types/regex.d.ts +0 -2
- package/dist/util/parser/types/regex.js +0 -16
- package/dist/util/parser/types/regex.js.map +0 -1
- package/dist/util/parser/types/string.d.ts +0 -2
- package/dist/util/parser/types/string.js +0 -19
- package/dist/util/parser/types/string.js.map +0 -1
- package/dist/util/parser/types/url.d.ts +0 -2
- package/dist/util/parser/types/url.js +0 -35
- package/dist/util/parser/types/url.js.map +0 -1
- package/dist/util/request/common.d.ts +0 -57
- package/dist/util/request/common.js +0 -146
- package/dist/util/request/common.js.map +0 -1
- package/dist/util/request/index.d.ts +0 -4
- package/dist/util/request/index.js +0 -231
- package/dist/util/request/index.js.map +0 -1
- package/dist/util/session/common.d.ts +0 -15
- package/dist/util/session/common.js.map +0 -1
- package/dist/util/session/index.js.map +0 -1
- package/dist/util/session/service.js.map +0 -1
- package/src/handler/api-router-utils.ts +0 -187
- package/src/handler/api-router.ts +0 -86
- package/src/handler/app.ts +0 -133
- package/src/handler/common/context.ts +0 -156
- package/src/handler/common/helpers.ts +0 -11
- package/src/handler/common/index.ts +0 -2
- package/src/handler/common/router-utils.ts +0 -232
- package/src/handler/common/router.ts +0 -98
- package/src/handler/common/tokenize-match.ts +0 -152
- package/src/handler/index.ts +0 -5
- package/src/handler/middleware/buffer.ts +0 -79
- package/src/handler/middleware/cookie.ts +0 -19
- package/src/handler/middleware/cors.ts +0 -98
- package/src/handler/middleware/form.ts +0 -59
- package/src/handler/middleware/group.ts +0 -70
- package/src/handler/middleware/index.ts +0 -48
- package/src/handler/middleware/json.ts +0 -56
- package/src/handler/middleware/logger.ts +0 -18
- package/src/handler/middleware/parse.ts +0 -93
- package/src/handler/middleware/session.ts +0 -155
- package/src/handler/middleware/tag.ts +0 -15
- package/src/handler/middleware/text.ts +0 -47
- package/src/handler/proxy.ts +0 -39
- package/src/index.ts +0 -2
- package/src/util/cli/index.ts +0 -81
- package/src/util/config/browser.ts +0 -5
- package/src/util/config/config.ts +0 -93
- package/src/util/config/featuretoggle.ts +0 -6
- package/src/util/config/index.ts +0 -3
- package/src/util/error/configfilenotfound.ts +0 -8
- package/src/util/error/forbiddenerror.ts +0 -8
- package/src/util/error/index.ts +0 -6
- package/src/util/error/named.ts +0 -3
- package/src/util/error/notfound.ts +0 -8
- package/src/util/error/notimplemented.ts +0 -8
- package/src/util/error/parsererror.ts +0 -15
- package/src/util/error/unauthorizederror.ts +0 -8
- package/src/util/index.ts +0 -8
- package/src/util/loader/index.ts +0 -61
- package/src/util/logger/common.ts +0 -6
- package/src/util/logger/factory.ts +0 -45
- package/src/util/logger/formatter.ts +0 -9
- package/src/util/logger/index.ts +0 -6
- package/src/util/logger/logger.ts +0 -114
- package/src/util/logger/transport.ts +0 -69
- package/src/util/parser/common.ts +0 -81
- package/src/util/parser/get.ts +0 -40
- package/src/util/parser/index.ts +0 -3
- package/src/util/parser/parser.ts +0 -235
- package/src/util/parser/types/any.ts +0 -8
- package/src/util/parser/types/array.ts +0 -73
- package/src/util/parser/types/boolean.ts +0 -13
- package/src/util/parser/types/email.ts +0 -26
- package/src/util/parser/types/enum.ts +0 -29
- package/src/util/parser/types/index.ts +0 -12
- package/src/util/parser/types/multiple.ts +0 -36
- package/src/util/parser/types/nested.ts +0 -19
- package/src/util/parser/types/number.ts +0 -34
- package/src/util/parser/types/object.ts +0 -10
- package/src/util/parser/types/regex.ts +0 -17
- package/src/util/parser/types/string.ts +0 -23
- package/src/util/parser/types/url.ts +0 -35
- package/src/util/request/common.ts +0 -193
- package/src/util/request/index.ts +0 -230
- package/src/util/session/common.ts +0 -20
- package/src/util/session/index.ts +0 -2
- package/src/util/session/service.ts +0 -154
- package/tsconfig.json +0 -32
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import { Router } from "./router";
|
|
2
|
-
import { Context, Handler } from "./context";
|
|
3
|
-
import { Method } from "./context";
|
|
4
|
-
import { matchTokenizePath, PathPartToken, tokenizePath } from "./tokenize-match"
|
|
5
|
-
import { ParseRequest } from "../middleware/parse";
|
|
6
|
-
import { Parser, ParseOptions, parse } from "../../util/parser";
|
|
7
|
-
import { SessionHandler, SessionHandlerOptions } from "../middleware/session";
|
|
8
|
-
import { GroupPolicyHandler } from "../middleware/group";
|
|
9
|
-
import { GroupPolicy } from "../../util/session";
|
|
10
|
-
|
|
11
|
-
const ResultParser = (options: ParseOptions | ParseOptions[], parser?: Parser): Handler<any> => async (ctx: Context): Promise<any> => {
|
|
12
|
-
if (!ctx.results || ctx.results.length === 0) {
|
|
13
|
-
ctx.logger.debug(`not parsing results`);
|
|
14
|
-
return undefined;
|
|
15
|
-
} else {
|
|
16
|
-
const index = ctx.results.length - 1;
|
|
17
|
-
const lastResult = ctx.results[index];
|
|
18
|
-
ctx.logger.trace(`parsing lastResult[%o]`, lastResult);
|
|
19
|
-
try {
|
|
20
|
-
if (options instanceof Array) {
|
|
21
|
-
for (let i = 0; i < options.length; i++) {
|
|
22
|
-
const o = options[i];
|
|
23
|
-
try {
|
|
24
|
-
const mappedResults = (parser ? parser.parse : parse)(`ctx.results[${index}]`, lastResult, o.options, o.mode, o.ignoreUndefined);
|
|
25
|
-
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
26
|
-
return mappedResults;
|
|
27
|
-
} catch (e) {
|
|
28
|
-
if (i === options.length - 1) {
|
|
29
|
-
throw e;
|
|
30
|
-
} else {
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
const mappedResults = (parser ? parser.parse : parse)(`ctx.results[${index}]`, lastResult, options.options, options.mode, options.ignoreUndefined);
|
|
37
|
-
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
38
|
-
return mappedResults;
|
|
39
|
-
}
|
|
40
|
-
} catch (e) {
|
|
41
|
-
ctx.logger.error(`error parsing lastResult[%o]`, lastResult);
|
|
42
|
-
throw e;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const ResponseHandler = (): Handler<void> => async (ctx: Context): Promise<void> => {
|
|
48
|
-
ctx.logger.trace("results [%o]", ctx.results);
|
|
49
|
-
if (!ctx.results || ctx.results.length === 0) {
|
|
50
|
-
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
51
|
-
} else {
|
|
52
|
-
const lastResult = ctx.results[ctx.results.length - 1];
|
|
53
|
-
ctx.logger.debug("response [%o]", lastResult);
|
|
54
|
-
if (lastResult !== undefined) {
|
|
55
|
-
await ctx.json(lastResult);
|
|
56
|
-
} else {
|
|
57
|
-
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export interface HandlerWithOptions extends RouterHandlerOptions {
|
|
63
|
-
handler: Handler | Array<Handler>;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export interface RouteOptions {
|
|
67
|
-
name?: string;
|
|
68
|
-
description?: string;
|
|
69
|
-
policy?: GroupPolicy;
|
|
70
|
-
request?: {
|
|
71
|
-
headers?: ParseOptions | ParseOptions[];
|
|
72
|
-
query?: ParseOptions | false | ParseOptions[];
|
|
73
|
-
params?: ParseOptions | false | ParseOptions[];
|
|
74
|
-
body?: ParseOptions | false | ParseOptions[];
|
|
75
|
-
};
|
|
76
|
-
response?: true | ParseOptions | ParseOptions[];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export interface RouteJSONDoc extends RouteOptions {
|
|
80
|
-
identifier: string;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface RouterJSONDoc {
|
|
84
|
-
[path: string]: {
|
|
85
|
-
[method: string]: RouteJSONDoc
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface RouterHandlerOptions extends RouteOptions {
|
|
90
|
-
parser?: Parser;
|
|
91
|
-
middleware?: Handler | Handler[];
|
|
92
|
-
session?: SessionHandlerOptions | Handler;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export class RouterHandler {
|
|
96
|
-
|
|
97
|
-
private readonly tokens: PathPartToken[];
|
|
98
|
-
private readonly handler: Array<Handler | Handler | Router>;
|
|
99
|
-
private readonly isRouter: boolean;
|
|
100
|
-
private readonly options?: RouterHandlerOptions;
|
|
101
|
-
|
|
102
|
-
public constructor(handler: Array<Handler> | HandlerWithOptions | Handler | Router, private readonly path: string | undefined, private readonly method: Method | undefined, options?: RouterHandlerOptions) {
|
|
103
|
-
this.tokens = tokenizePath(path);
|
|
104
|
-
|
|
105
|
-
let handlers: Array<Handler | Router> = []
|
|
106
|
-
this.isRouter = handler instanceof Router;
|
|
107
|
-
|
|
108
|
-
if (!this.isRouter && !(handler instanceof Array) && typeof handler === "object") {
|
|
109
|
-
// HandlerWithOptions
|
|
110
|
-
if (options) {
|
|
111
|
-
throw new Error("bad arguments. cannot send options with HandlerWithOptions");
|
|
112
|
-
}
|
|
113
|
-
const realHandler = (handler as HandlerWithOptions).handler;
|
|
114
|
-
const newOptions = {
|
|
115
|
-
...(handler as HandlerWithOptions)
|
|
116
|
-
};
|
|
117
|
-
delete (newOptions as any).handler;
|
|
118
|
-
handler = realHandler;
|
|
119
|
-
this.options = newOptions;
|
|
120
|
-
} else {
|
|
121
|
-
this.options = options;
|
|
122
|
-
}
|
|
123
|
-
const routeHandlers = handler instanceof Array ? handler : [handler] as Array<Handler | Router>;
|
|
124
|
-
|
|
125
|
-
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
126
|
-
throw new Error("bad arguments");
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (this.options && this.options.middleware) {
|
|
130
|
-
handlers = handlers.concat(this.options.middleware);
|
|
131
|
-
}
|
|
132
|
-
if (this.options && this.options.session) {
|
|
133
|
-
handlers.push(typeof this.options.session !== "function" ? SessionHandler(this.options.session) : this.options.session);
|
|
134
|
-
}
|
|
135
|
-
if (this.options && this.options.policy) {
|
|
136
|
-
handlers.push(GroupPolicyHandler(this.options.policy));
|
|
137
|
-
}
|
|
138
|
-
if (this.options && this.options.request) {
|
|
139
|
-
handlers.push(ParseRequest(this.options.request));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
handlers = handlers.concat(routeHandlers);
|
|
143
|
-
|
|
144
|
-
if (this.options && this.options.response) {
|
|
145
|
-
if (typeof this.options.response === "boolean") {
|
|
146
|
-
(handlers as Handler[]).push(ResponseHandler());
|
|
147
|
-
} else {
|
|
148
|
-
(handlers as Handler[]).push(ResultParser(this.options.response, this.options.parser));
|
|
149
|
-
(handlers as Handler[]).push(ResponseHandler());
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
this.handler = handlers;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
|
|
156
|
-
const nPath = this.path && this.path.length > 1 ? this.path.substring(1) : "";
|
|
157
|
-
const nMethod = this.method ? this.method : "";
|
|
158
|
-
|
|
159
|
-
if (this.isRouter) {
|
|
160
|
-
const router: Router = this.handler[0] as Router;
|
|
161
|
-
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
162
|
-
} else if (this.options) {
|
|
163
|
-
if (!doc[`${prePath}${nPath}`]) {
|
|
164
|
-
doc[`${prePath}${nPath}`] = {};
|
|
165
|
-
}
|
|
166
|
-
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
167
|
-
throw new Error("cannot create doc routes override each other");
|
|
168
|
-
}
|
|
169
|
-
doc[`${prePath}${nPath}`][nMethod] = {
|
|
170
|
-
description: this.options.description,
|
|
171
|
-
name: this.options.name,
|
|
172
|
-
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(),
|
|
173
|
-
policy: this.options.policy,
|
|
174
|
-
request: this.options.request ? {
|
|
175
|
-
body: this.options.request.body,
|
|
176
|
-
headers: this.options.request.headers,
|
|
177
|
-
params: this.options.request.params,
|
|
178
|
-
query: this.options.request.query,
|
|
179
|
-
} : undefined,
|
|
180
|
-
response: this.options.response
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
return doc;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
public async invoke(ctx: Context, pathTokens: { value: string; lower: string; }[], prePathTokens: PathPartToken[]): Promise<boolean> {
|
|
187
|
-
for (let i = 0; i < this.handler.length; i++) {
|
|
188
|
-
const h = this.handler[i];
|
|
189
|
-
if (h instanceof Router) {
|
|
190
|
-
if (!this.pushResults(await h.run(ctx, pathTokens, prePathTokens.concat(this.tokens)), ctx)) {
|
|
191
|
-
return false;
|
|
192
|
-
}
|
|
193
|
-
} else {
|
|
194
|
-
if (!this.pushResults(await h(ctx), ctx)) {
|
|
195
|
-
return false;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return true;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
public isMatch(
|
|
203
|
-
method: string,
|
|
204
|
-
pathTokens: { value: string; lower: string; }[],
|
|
205
|
-
prePathTokens: PathPartToken[]): {
|
|
206
|
-
match: boolean;
|
|
207
|
-
params?: { [name: string]: string | undefined }
|
|
208
|
-
} {
|
|
209
|
-
if ((this.method === undefined || this.method === method)) {
|
|
210
|
-
if (this.path === undefined) {
|
|
211
|
-
return {
|
|
212
|
-
match: true
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
return matchTokenizePath(this.isRouter, prePathTokens.concat(this.tokens), pathTokens);
|
|
216
|
-
}
|
|
217
|
-
return {
|
|
218
|
-
match: false
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
private pushResults(result: boolean, ctx: Context): boolean {
|
|
223
|
-
if (result === false) {
|
|
224
|
-
// ctx.logger.debug(`avoiding next handlers because handler returned false.`);
|
|
225
|
-
return false;
|
|
226
|
-
} else if (result !== true && result !== undefined) {
|
|
227
|
-
ctx.logger.trace("pushing to results [%o]", result);
|
|
228
|
-
ctx.results.push(result);
|
|
229
|
-
}
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage, RequestListener, ServerResponse } from "http";
|
|
2
|
-
import { Logger } from "../../util/logger";
|
|
3
|
-
import { Method, Handler, Context, ErrorHandler } from "./context";
|
|
4
|
-
import { RouterHandler, RouterJSONDoc, RouterHandlerOptions, HandlerWithOptions } from "./router-utils";
|
|
5
|
-
import { normalizePath, PathPartToken, splitPath } from "./tokenize-match";
|
|
6
|
-
|
|
7
|
-
export interface RouterOptions {
|
|
8
|
-
newRequestLogger?: (req: IncomingMessage) => Logger;
|
|
9
|
-
newContext?: (req: IncomingMessage, res: ServerResponse) => Context;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class Router {
|
|
13
|
-
protected readonly handlers: RouterHandler[] = [];
|
|
14
|
-
protected readonly errorHandlers: ErrorHandler[] = [];
|
|
15
|
-
public readonly listener: RequestListener;
|
|
16
|
-
constructor(protected config?: RouterOptions) {
|
|
17
|
-
this.listener = async (req, res) => {
|
|
18
|
-
const ctx = this.config && this.config.newContext ?
|
|
19
|
-
this.config.newContext(req, res) :
|
|
20
|
-
new Context(
|
|
21
|
-
req,
|
|
22
|
-
res,
|
|
23
|
-
this.config && this.config.newRequestLogger ?
|
|
24
|
-
this.config.newRequestLogger(req) :
|
|
25
|
-
undefined);
|
|
26
|
-
ctx.logger.trace(`request received`);
|
|
27
|
-
await this.run(ctx);
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
public get(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
31
|
-
return this.use(handler, path, "get", options);
|
|
32
|
-
}
|
|
33
|
-
public post(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
34
|
-
return this.use(handler, path, "post", options);
|
|
35
|
-
}
|
|
36
|
-
public patch(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
37
|
-
return this.use(handler, path, "patch", options);
|
|
38
|
-
}
|
|
39
|
-
public delete(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
40
|
-
return this.use(handler, path, "delete", options);
|
|
41
|
-
}
|
|
42
|
-
public put(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
43
|
-
return this.use(handler, path, "put", options);
|
|
44
|
-
}
|
|
45
|
-
public options(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
46
|
-
return this.use(handler, path, "options", options);
|
|
47
|
-
}
|
|
48
|
-
public use(handler: Array<Handler> | HandlerWithOptions | Handler | Router, path?: string, method?: Method, options?: RouterHandlerOptions): Router {
|
|
49
|
-
const nPath = path !== undefined ? normalizePath(path) : undefined;
|
|
50
|
-
const nMethod = method ? method.toUpperCase() as Method : method;
|
|
51
|
-
this.handlers.push(new RouterHandler(handler, nPath, nMethod, options));
|
|
52
|
-
return this;
|
|
53
|
-
}
|
|
54
|
-
public catch(errorHandler: Array<ErrorHandler> | ErrorHandler): Router {
|
|
55
|
-
const errorHandlers = errorHandler instanceof Array ? errorHandler : [errorHandler];
|
|
56
|
-
for (const e of errorHandlers) {
|
|
57
|
-
this.errorHandlers.push(e);
|
|
58
|
-
}
|
|
59
|
-
return this;
|
|
60
|
-
}
|
|
61
|
-
public async run(ctx: Context, pathTokens?: { value: string; lower: string; }[], prePathTokens: PathPartToken[] = []): Promise<boolean> {
|
|
62
|
-
pathTokens = pathTokens ? pathTokens : splitPath(ctx.path);
|
|
63
|
-
try {
|
|
64
|
-
let shouldContinue = true;
|
|
65
|
-
for (const h of this.handlers) {
|
|
66
|
-
if (!shouldContinue || ctx.res.headersSent) {
|
|
67
|
-
shouldContinue = false;
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
const isMatch = h.isMatch(ctx.method, pathTokens, prePathTokens);
|
|
71
|
-
if (isMatch.match) {
|
|
72
|
-
if (isMatch.params) {
|
|
73
|
-
ctx.params = ctx.params ? {
|
|
74
|
-
...ctx.params,
|
|
75
|
-
...isMatch.params
|
|
76
|
-
} : isMatch.params;
|
|
77
|
-
}
|
|
78
|
-
shouldContinue = await h.invoke(ctx, pathTokens, prePathTokens);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return !ctx.res.headersSent;
|
|
82
|
-
} catch (e: any) {
|
|
83
|
-
for (const eH of this.errorHandlers) {
|
|
84
|
-
const ret = await eH(e, ctx);
|
|
85
|
-
if (ret === false || ctx.res.headersSent) {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
throw e; // pass the error along
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
|
|
93
|
-
for (const h of this.handlers) {
|
|
94
|
-
doc = h.getJSONDoc(doc, prePath);
|
|
95
|
-
}
|
|
96
|
-
return doc;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { URLSearchParams } from "url";
|
|
2
|
-
import { newURLSearchParams } from "./helpers";
|
|
3
|
-
|
|
4
|
-
export const parseSearchParams = (search: URLSearchParams): { [name: string]: string | string[] } => {
|
|
5
|
-
const query: { [name: string]: string | string[] } = {};
|
|
6
|
-
search.forEach((value: string, key: string) => {
|
|
7
|
-
query[key] = query[key] instanceof Array ? (query[key] as string[]).concat([value]) : query[key] ? [query[key] as string, value] : value;
|
|
8
|
-
});
|
|
9
|
-
return query;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// appends query params to URLSearchParams and returns a copy
|
|
13
|
-
export const mergeSearchParams = (search: URLSearchParams, append?: { [name: string]: string | string[] | number | boolean | number[] | boolean[] }): URLSearchParams => {
|
|
14
|
-
const copy = newURLSearchParams(search.toString());
|
|
15
|
-
if (!append) {
|
|
16
|
-
return copy;
|
|
17
|
-
}
|
|
18
|
-
const keys = Object.keys(append);
|
|
19
|
-
for (const k of keys) {
|
|
20
|
-
if (append[k] instanceof Array) {
|
|
21
|
-
for (const a of append[k] as Array<any>) {
|
|
22
|
-
if (copy.has(k)) {
|
|
23
|
-
copy.append(k, String(a));
|
|
24
|
-
} else {
|
|
25
|
-
copy.set(k, String(a));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
} else {
|
|
30
|
-
if (copy.has(k)) {
|
|
31
|
-
copy.append(k, String(append[k]));
|
|
32
|
-
} else {
|
|
33
|
-
copy.set(k, String(append[k]));
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return copy;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// adds "/" to the final of path
|
|
41
|
-
// cannot account for query or hash params so send only new URL().pathname
|
|
42
|
-
export const normalizePath = (path: string): string => {
|
|
43
|
-
if (typeof path !== "string") {
|
|
44
|
-
throw new Error("path not string");
|
|
45
|
-
}
|
|
46
|
-
if (path.length > 1) {
|
|
47
|
-
if (path.charAt(0) !== "/") {
|
|
48
|
-
throw new Error("path doesnt start with /");
|
|
49
|
-
}
|
|
50
|
-
if (path[path.length - 1] !== "/") {
|
|
51
|
-
return `${path}/`;
|
|
52
|
-
} else {
|
|
53
|
-
return path; // already normalized
|
|
54
|
-
}
|
|
55
|
-
} else if (path !== "/") {
|
|
56
|
-
throw new Error("path of length 1 not /");
|
|
57
|
-
} else {
|
|
58
|
-
return path; // "/"
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface PathPartToken {
|
|
63
|
-
optional: boolean;
|
|
64
|
-
wild?: string;
|
|
65
|
-
token: string;
|
|
66
|
-
lower: string;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export const pathPartTokenize = (token: { value: string; lower: string; }): PathPartToken => {
|
|
70
|
-
const isWild = token.value[0] === ":";
|
|
71
|
-
const optional = token.value[token.value.length - 1] === "?";
|
|
72
|
-
const wild = isWild ? token.value.substring(1, token.value.length - (optional ? 1 : 0)) : undefined;
|
|
73
|
-
return {
|
|
74
|
-
optional,
|
|
75
|
-
wild,
|
|
76
|
-
token: token.value,
|
|
77
|
-
lower: token.lower
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export const splitPath = (path?: string): { value: string; lower: string; }[] => path === undefined ? [] : path.split("/").filter(p => p).map(p => {
|
|
82
|
-
return {
|
|
83
|
-
value: p,
|
|
84
|
-
lower: p.toLocaleLowerCase()
|
|
85
|
-
};
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
export const tokenizePath = (path?: string): PathPartToken[] => {
|
|
89
|
-
if (path === undefined) {
|
|
90
|
-
return [];
|
|
91
|
-
}
|
|
92
|
-
const tokens = splitPath(path).map(token => pathPartTokenize(token));
|
|
93
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
94
|
-
if (tokens[i].optional && i != tokens.length - 1) {
|
|
95
|
-
throw new Error("cannot set a path token as optional that's not the last one");
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return tokens;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export const matchTokenizePath = (checkAsRouter: boolean, tokens: PathPartToken[], ctxTokens: { value: string; lower: string; }[]): {
|
|
102
|
-
match: boolean;
|
|
103
|
-
params?: { [name: string]: string | undefined }
|
|
104
|
-
} => {
|
|
105
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
106
|
-
if (tokens[i].optional && i != tokens.length - 1) {
|
|
107
|
-
throw new Error("cannot set a path token as optional that's not the last one");
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
const lastTokenIsOptional = tokens.length > 0 ? tokens[tokens.length - 1].optional : false;
|
|
111
|
-
const couldBeUsingOptional = (lastTokenIsOptional &&
|
|
112
|
-
(
|
|
113
|
-
(!checkAsRouter && tokens.length - 1 === ctxTokens.length) ||
|
|
114
|
-
(checkAsRouter && tokens.length - 1 <= ctxTokens.length)
|
|
115
|
-
)
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
const params: { [name: string]: string | undefined } = {};
|
|
119
|
-
|
|
120
|
-
if ((!checkAsRouter && tokens.length === ctxTokens.length) || couldBeUsingOptional || (checkAsRouter && tokens.length <= ctxTokens.length)) {
|
|
121
|
-
// similiar count of tokens
|
|
122
|
-
for (let i = 0; i < ctxTokens.length; i++) {
|
|
123
|
-
const ctxToken = ctxTokens[i];
|
|
124
|
-
if (checkAsRouter && i >= tokens.length) {
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (tokens[i].wild || ctxToken.lower === tokens[i].lower) {
|
|
129
|
-
// check pass
|
|
130
|
-
const wild = tokens[i].wild;
|
|
131
|
-
if (wild) {
|
|
132
|
-
params[wild] = ctxToken.value;
|
|
133
|
-
}
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
match: false,
|
|
139
|
-
params: {}
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
return {
|
|
143
|
-
match: true,
|
|
144
|
-
params
|
|
145
|
-
};
|
|
146
|
-
} else {
|
|
147
|
-
return {
|
|
148
|
-
match: false,
|
|
149
|
-
params: {}
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
}
|
package/src/handler/index.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { gunzipSync } from "zlib";
|
|
2
|
-
import { Context, Handler } from "../common";
|
|
3
|
-
import { BadRequestError } from "../../util/error";
|
|
4
|
-
import { checkEnvVariables } from "../../util/config";
|
|
5
|
-
|
|
6
|
-
export const DEFAULT_READ_BUFFER_LIMIT = 1024 * 8 * 100;
|
|
7
|
-
|
|
8
|
-
const DEFAULT_READ_BUFFER_TIMEOUT = 1000 * 40;
|
|
9
|
-
|
|
10
|
-
export const ReadBuffer = (options?: {
|
|
11
|
-
limit?: number;
|
|
12
|
-
timeout?: number;
|
|
13
|
-
}): Handler<void> => {
|
|
14
|
-
let limit = DEFAULT_READ_BUFFER_LIMIT;
|
|
15
|
-
let timeout = DEFAULT_READ_BUFFER_TIMEOUT;
|
|
16
|
-
if (options) {
|
|
17
|
-
limit = options.limit !== undefined ? options.limit : limit;
|
|
18
|
-
timeout = options.timeout !== undefined ? options.timeout : timeout;
|
|
19
|
-
} else {
|
|
20
|
-
const [limitS, timeoutS] =
|
|
21
|
-
checkEnvVariables(["READ_BUFFER_LIMIT", "READ_BUFFER_TIMEOUT"], [String(DEFAULT_READ_BUFFER_LIMIT), String(DEFAULT_READ_BUFFER_TIMEOUT)]);
|
|
22
|
-
limit = parseInt(limitS, 10);
|
|
23
|
-
timeout = parseInt(timeoutS, 10);
|
|
24
|
-
}
|
|
25
|
-
return async (ctx: Context): Promise<void> => new Promise<void>((resolve, reject) => {
|
|
26
|
-
try {
|
|
27
|
-
const readTimeout = setTimeout(() => {
|
|
28
|
-
clearTimeout(readTimeout);
|
|
29
|
-
ctx.req.removeListener('error', errorListener);
|
|
30
|
-
ctx.req.removeListener('data', chunkListener);
|
|
31
|
-
ctx.req.removeListener('end', endListener);
|
|
32
|
-
ctx.close();
|
|
33
|
-
return;
|
|
34
|
-
}, timeout);
|
|
35
|
-
let cLength = 0;
|
|
36
|
-
const buffers: Buffer[] = [];
|
|
37
|
-
const endListener = () => {
|
|
38
|
-
clearTimeout(readTimeout);
|
|
39
|
-
ctx.req.removeListener('error', errorListener);
|
|
40
|
-
ctx.req.removeListener('data', chunkListener);
|
|
41
|
-
ctx.req.removeListener('end', endListener);
|
|
42
|
-
try {
|
|
43
|
-
const concatBuffers = Buffer.concat(buffers);
|
|
44
|
-
const responseBuffer: Buffer = ctx.headers["content-encoding"] === "gzip" ?
|
|
45
|
-
gunzipSync(concatBuffers) : concatBuffers;
|
|
46
|
-
ctx.buffer = responseBuffer;
|
|
47
|
-
resolve();
|
|
48
|
-
} catch (e) {
|
|
49
|
-
reject(e);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
const errorListener = (err: Error) => {
|
|
53
|
-
clearTimeout(readTimeout);
|
|
54
|
-
ctx.req.removeListener('error', errorListener);
|
|
55
|
-
ctx.req.removeListener('data', chunkListener);
|
|
56
|
-
ctx.req.removeListener('end', endListener);
|
|
57
|
-
reject(err);
|
|
58
|
-
};
|
|
59
|
-
const chunkListener = (chunk: Buffer) => {
|
|
60
|
-
cLength += chunk.length;
|
|
61
|
-
if (cLength > limit) {
|
|
62
|
-
clearTimeout(readTimeout);
|
|
63
|
-
ctx.req.removeListener('error', errorListener);
|
|
64
|
-
ctx.req.removeListener('data', chunkListener);
|
|
65
|
-
ctx.req.removeListener('end', endListener);
|
|
66
|
-
ctx.logger.error(`ctx.buffer.length ${cLength} > ${limit}. To accept this body set READ_BUFFER_LIMIT to a higher value.`);
|
|
67
|
-
reject(new BadRequestError(`buffer.length ${cLength} > ${limit}`));
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
buffers.push(chunk);
|
|
71
|
-
};
|
|
72
|
-
ctx.req.on('error', errorListener);
|
|
73
|
-
ctx.req.on('data', chunkListener);
|
|
74
|
-
ctx.req.on('end', endListener);
|
|
75
|
-
} catch (e) {
|
|
76
|
-
reject(e);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Handler, Context } from "../common";
|
|
2
|
-
|
|
3
|
-
import { parse } from "cookie";
|
|
4
|
-
|
|
5
|
-
export const CookieParser = (options?: {
|
|
6
|
-
decode?(value: string): string;
|
|
7
|
-
}): Handler<void> => {
|
|
8
|
-
return async (ctx: Context): Promise<void> => {
|
|
9
|
-
const cookies = parse(ctx.headers.cookie || '', options);
|
|
10
|
-
const cookieList = Object.keys(cookies);
|
|
11
|
-
for (const name of cookieList) {
|
|
12
|
-
const value = cookies[name];
|
|
13
|
-
if (value) {
|
|
14
|
-
ctx.cookies[name] = value;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common";
|
|
2
|
-
import { checkEnvVariables } from "../../util/config";
|
|
3
|
-
import { BadRequestError } from "../../util/error";
|
|
4
|
-
|
|
5
|
-
const DEFAULT_CORS_ORIGINS = "*";
|
|
6
|
-
const DEFAULT_CORS_METHODS = "GET,HEAD,PUT,PATCH,POST,DELETE";
|
|
7
|
-
const DEFAULT_CORS_PREFLIGHT_CONTINUE = false;
|
|
8
|
-
|
|
9
|
-
export interface CORSOptions {
|
|
10
|
-
origins: string[] | string | undefined;
|
|
11
|
-
validate?: (origin: string | undefined, origins: string[] | string) => boolean;
|
|
12
|
-
methods?: string;
|
|
13
|
-
preflightContinue?: boolean;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const isOriginAllowed = (origin: string | undefined, origins: string[] | string): boolean => {
|
|
17
|
-
if (origins instanceof Array) {
|
|
18
|
-
for (const o of origins) {
|
|
19
|
-
if (origin === o || o === "*") {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
} else {
|
|
25
|
-
return origin === origins;
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const CORS = (options?: CORSOptions): Handler<void> => {
|
|
30
|
-
let origins: string[] | undefined | string;
|
|
31
|
-
let methods: string;
|
|
32
|
-
let preFlightContinue = false;
|
|
33
|
-
if (options) {
|
|
34
|
-
origins = options.origins instanceof Array ? options.origins : (options.origins ? [options.origins] : undefined);
|
|
35
|
-
methods = options.methods ? options.methods : DEFAULT_CORS_METHODS;
|
|
36
|
-
preFlightContinue = options.preflightContinue ? true : false;
|
|
37
|
-
} else {
|
|
38
|
-
const [originsS, preFlightContinueS, methodsS] =
|
|
39
|
-
checkEnvVariables(["CORS_ORIGINS", "CORS_PREFLIGHT_CONTINUE", "CORS_METHODS"], [DEFAULT_CORS_ORIGINS, String(DEFAULT_CORS_PREFLIGHT_CONTINUE), DEFAULT_CORS_METHODS]);
|
|
40
|
-
origins = originsS.split(",").map(s => s.trim());
|
|
41
|
-
methods = methodsS;
|
|
42
|
-
preFlightContinue = preFlightContinueS.toLocaleLowerCase() === "true";
|
|
43
|
-
}
|
|
44
|
-
if (origins && origins.length === 1) {
|
|
45
|
-
origins = origins[0];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const validate: (origin: string | undefined, origins: string[] | string) => boolean =
|
|
49
|
-
options && options.validate ? options && options.validate : isOriginAllowed;
|
|
50
|
-
|
|
51
|
-
return async (ctx: Context): Promise<void> => {
|
|
52
|
-
// access-control-allow-origin
|
|
53
|
-
if (!origins || origins === '*') {
|
|
54
|
-
// allow any origin
|
|
55
|
-
ctx.setHeader("Access-Control-Allow-Origin", "*");
|
|
56
|
-
ctx.addVaryHeader("Origin");
|
|
57
|
-
} else {
|
|
58
|
-
if (ctx.headers.origin) {
|
|
59
|
-
if (validate(ctx.headers.origin, origins)) {
|
|
60
|
-
// reflect origin
|
|
61
|
-
ctx.setHeader("Access-Control-Allow-Origin", String(ctx.headers.origin));
|
|
62
|
-
ctx.addVaryHeader("Origin");
|
|
63
|
-
} else {
|
|
64
|
-
// not allowed
|
|
65
|
-
throw new BadRequestError(`bad origin`);
|
|
66
|
-
}
|
|
67
|
-
} else {
|
|
68
|
-
ctx.setHeader("Access-Control-Allow-Origin", origins instanceof Array ? origins[0] : origins);
|
|
69
|
-
ctx.addVaryHeader("Origin");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (ctx.method.toUpperCase() === "OPTIONS") {
|
|
74
|
-
// access-control-request-headers
|
|
75
|
-
if (methods) {
|
|
76
|
-
ctx.setHeader("Access-Control-Allow-Methods", methods);
|
|
77
|
-
}
|
|
78
|
-
// reflect access-control-request-headers
|
|
79
|
-
const allowedHeaders = ctx.headers['access-control-request-headers'];
|
|
80
|
-
if (allowedHeaders) {
|
|
81
|
-
ctx.addVaryHeader("Access-Control-Request-Headers");
|
|
82
|
-
ctx.setHeader("Access-Control-Allow-Headers", allowedHeaders);
|
|
83
|
-
}
|
|
84
|
-
// after preflight
|
|
85
|
-
if (preFlightContinue) {
|
|
86
|
-
return;
|
|
87
|
-
} else {
|
|
88
|
-
ctx.end({
|
|
89
|
-
headers: {
|
|
90
|
-
"Content-Length": 0
|
|
91
|
-
},
|
|
92
|
-
status: 204,
|
|
93
|
-
body: undefined
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
};
|