@sapphire/plugin-api 7.0.0-next.03b2a90 → 7.0.0-next.301a15d
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/dist/cjs/index.cjs +22 -8
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +207 -114
- package/dist/cjs/lib/structures/Middleware.cjs.map +1 -1
- package/dist/cjs/lib/structures/MiddlewareStore.cjs +2 -2
- package/dist/cjs/lib/structures/MiddlewareStore.cjs.map +1 -1
- package/dist/cjs/lib/structures/Route.cjs +14 -11
- package/dist/cjs/lib/structures/Route.cjs.map +1 -1
- package/dist/cjs/lib/structures/RouteLoaderStrategy.cjs +2 -10
- package/dist/cjs/lib/structures/RouteLoaderStrategy.cjs.map +1 -1
- package/dist/cjs/lib/structures/RouteStore.cjs +2 -40
- package/dist/cjs/lib/structures/RouteStore.cjs.map +1 -1
- package/dist/cjs/lib/structures/api/ApiRequest.cjs +26 -0
- package/dist/cjs/lib/structures/api/ApiRequest.cjs.map +1 -1
- package/dist/cjs/lib/structures/api/ApiResponse.cjs +6 -0
- package/dist/cjs/lib/structures/api/ApiResponse.cjs.map +1 -1
- package/dist/cjs/lib/structures/http/Server.cjs +15 -14
- package/dist/cjs/lib/structures/http/Server.cjs.map +1 -1
- package/dist/cjs/lib/structures/router/RouterBranch.cjs +184 -0
- package/dist/cjs/lib/structures/router/RouterBranch.cjs.map +1 -0
- package/dist/cjs/lib/structures/router/RouterNode.cjs +67 -0
- package/dist/cjs/lib/structures/router/RouterNode.cjs.map +1 -0
- package/dist/cjs/lib/structures/router/RouterRoot.cjs +71 -0
- package/dist/cjs/lib/structures/router/RouterRoot.cjs.map +1 -0
- package/dist/cjs/listeners/PluginRouteError.cjs +2 -2
- package/dist/cjs/listeners/PluginRouteError.cjs.map +1 -1
- package/dist/cjs/listeners/PluginServerMiddlewareError.cjs +2 -2
- package/dist/cjs/listeners/PluginServerMiddlewareError.cjs.map +1 -1
- package/dist/cjs/listeners/PluginServerMiddlewareSuccess.cjs +4 -4
- package/dist/cjs/listeners/PluginServerMiddlewareSuccess.cjs.map +1 -1
- package/dist/cjs/listeners/PluginServerRequest.cjs +41 -7
- package/dist/cjs/listeners/PluginServerRequest.cjs.map +1 -1
- package/dist/cjs/listeners/{PluginServerMatch.cjs → PluginServerRouterBranchMethodNotAllowed.cjs} +4 -9
- package/dist/cjs/listeners/PluginServerRouterBranchMethodNotAllowed.cjs.map +1 -0
- package/dist/cjs/listeners/{PluginServerNoMatch.cjs → PluginServerRouterBranchNotFound.cjs} +2 -2
- package/dist/cjs/listeners/PluginServerRouterBranchNotFound.cjs.map +1 -0
- package/dist/cjs/listeners/PluginServerRouterFound.cjs +21 -0
- package/dist/cjs/listeners/PluginServerRouterFound.cjs.map +1 -0
- package/dist/cjs/listeners/_load.cjs +6 -4
- package/dist/cjs/listeners/_load.cjs.map +1 -1
- package/dist/cjs/middlewares/body.cjs +4 -3
- package/dist/cjs/middlewares/body.cjs.map +1 -1
- package/dist/cjs/middlewares/headers.cjs +9 -10
- package/dist/cjs/middlewares/headers.cjs.map +1 -1
- package/dist/esm/{chunk-JTFKMR4I.mjs → chunk-S573YWRP.mjs} +3 -2
- package/dist/esm/index.d.mts +207 -114
- package/dist/esm/index.mjs +5 -3
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lib/structures/MediaParser.mjs +1 -1
- package/dist/esm/lib/structures/MediaParserStore.mjs +1 -1
- package/dist/esm/lib/structures/Middleware.mjs +1 -1
- package/dist/esm/lib/structures/Middleware.mjs.map +1 -1
- package/dist/esm/lib/structures/MiddlewareStore.mjs +3 -3
- package/dist/esm/lib/structures/MiddlewareStore.mjs.map +1 -1
- package/dist/esm/lib/structures/Route.mjs +15 -12
- package/dist/esm/lib/structures/Route.mjs.map +1 -1
- package/dist/esm/lib/structures/RouteLoaderStrategy.mjs +3 -11
- package/dist/esm/lib/structures/RouteLoaderStrategy.mjs.map +1 -1
- package/dist/esm/lib/structures/RouteStore.mjs +3 -41
- package/dist/esm/lib/structures/RouteStore.mjs.map +1 -1
- package/dist/esm/lib/structures/api/ApiRequest.mjs +27 -1
- package/dist/esm/lib/structures/api/ApiRequest.mjs.map +1 -1
- package/dist/esm/lib/structures/api/ApiResponse.mjs +7 -1
- package/dist/esm/lib/structures/api/ApiResponse.mjs.map +1 -1
- package/dist/esm/lib/structures/api/CookieStore.mjs +1 -1
- package/dist/esm/lib/structures/http/Auth.mjs +1 -1
- package/dist/esm/lib/structures/http/HttpCodes.mjs +1 -1
- package/dist/esm/lib/structures/http/HttpMethods.mjs +1 -1
- package/dist/esm/lib/structures/http/Server.mjs +16 -15
- package/dist/esm/lib/structures/http/Server.mjs.map +1 -1
- package/dist/esm/lib/structures/router/RouterBranch.mjs +179 -0
- package/dist/esm/lib/structures/router/RouterBranch.mjs.map +1 -0
- package/dist/esm/lib/structures/router/RouterNode.mjs +56 -0
- package/dist/esm/lib/structures/router/RouterNode.mjs.map +1 -0
- package/dist/esm/lib/structures/router/RouterRoot.mjs +68 -0
- package/dist/esm/lib/structures/router/RouterRoot.mjs.map +1 -0
- package/dist/esm/lib/utils/MimeTypes.mjs +1 -1
- package/dist/esm/listeners/PluginRouteError.mjs +4 -4
- package/dist/esm/listeners/PluginRouteError.mjs.map +1 -1
- package/dist/esm/listeners/PluginServerMiddlewareError.mjs +4 -4
- package/dist/esm/listeners/PluginServerMiddlewareError.mjs.map +1 -1
- package/dist/esm/listeners/PluginServerMiddlewareSuccess.mjs +6 -6
- package/dist/esm/listeners/PluginServerMiddlewareSuccess.mjs.map +1 -1
- package/dist/esm/listeners/PluginServerRequest.mjs +37 -9
- package/dist/esm/listeners/PluginServerRequest.mjs.map +1 -1
- package/dist/esm/listeners/PluginServerRouterBranchMethodNotAllowed.mjs +18 -0
- package/dist/esm/listeners/PluginServerRouterBranchMethodNotAllowed.mjs.map +1 -0
- package/dist/esm/listeners/{PluginServerNoMatch.mjs → PluginServerRouterBranchNotFound.mjs} +4 -4
- package/dist/esm/listeners/PluginServerRouterBranchNotFound.mjs.map +1 -0
- package/dist/esm/listeners/PluginServerRouterFound.mjs +18 -0
- package/dist/esm/listeners/PluginServerRouterFound.mjs.map +1 -0
- package/dist/esm/listeners/_load.mjs +13 -11
- package/dist/esm/listeners/_load.mjs.map +1 -1
- package/dist/esm/mediaParsers/_load.mjs +1 -1
- package/dist/esm/mediaParsers/applicationFormUrlEncoded.mjs +1 -1
- package/dist/esm/mediaParsers/applicationJson.mjs +1 -1
- package/dist/esm/mediaParsers/textPlain.mjs +1 -1
- package/dist/esm/middlewares/_load.mjs +1 -1
- package/dist/esm/middlewares/auth.mjs +1 -1
- package/dist/esm/middlewares/body.mjs +5 -4
- package/dist/esm/middlewares/body.mjs.map +1 -1
- package/dist/esm/middlewares/cookies.mjs +1 -1
- package/dist/esm/middlewares/headers.mjs +10 -11
- package/dist/esm/middlewares/headers.mjs.map +1 -1
- package/dist/esm/register.mjs +1 -1
- package/dist/esm/routes/_load.mjs +1 -1
- package/dist/esm/routes/oauth/callback.post.mjs +1 -1
- package/dist/esm/routes/oauth/logout.post.mjs +1 -1
- package/package.json +4 -3
- package/dist/cjs/lib/utils/RouteData.cjs +0 -62
- package/dist/cjs/lib/utils/RouteData.cjs.map +0 -1
- package/dist/cjs/listeners/PluginServerMatch.cjs.map +0 -1
- package/dist/cjs/listeners/PluginServerNoMatch.cjs.map +0 -1
- package/dist/esm/lib/utils/RouteData.mjs +0 -56
- package/dist/esm/lib/utils/RouteData.mjs.map +0 -1
- package/dist/esm/listeners/PluginServerMatch.mjs +0 -23
- package/dist/esm/listeners/PluginServerMatch.mjs.map +0 -1
- package/dist/esm/listeners/PluginServerNoMatch.mjs.map +0 -1
- /package/dist/esm/{chunk-JTFKMR4I.mjs.map → chunk-S573YWRP.mjs.map} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/listeners/PluginRouteError.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/PluginRouteError.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;AAGzB,SAAS,iBAAiB;AAC1B,SAAS,mBAAmB;AAErB,IAAM,kBAAN,MAAM,wBAAuB,SAAS;AAAA,EACrC,YAAY,SAAiC;AACnD,UAAM,SAAS,EAAE,SAAS,UAAU,OAAO,YAAY,WAAW,CAAC;AAAA,EACpE;AAAA,EAEgB,IAAI,OAAc,UAAsB,UAAuB;AAE9E,SAAK,UAAU,OAAO,MAAM,KAAK;AAGjC,QAAI,CAAC,SAAS,cAAe,UAAS,OAAO,UAAU,mBAAmB,EAAE,KAAK,EAAE,OAAO,MAAM,WAAW,MAAM,CAAC;AAAA,EACnH;AACD;AAZ6C;AAAtC,IAAM,iBAAN","sourcesContent":["import { Listener } from '@sapphire/framework';\nimport type { ApiRequest } from '../lib/structures/api/ApiRequest';\nimport type { ApiResponse } from '../lib/structures/api/ApiResponse';\nimport { HttpCodes } from '../lib/structures/http/HttpCodes';\nimport { ServerEvent } from '../lib/structures/http/Server';\n\nexport class PluginListener extends Listener {\n\tpublic constructor(context: Listener.LoaderContext) {\n\t\tsuper(context, { emitter: 'server', event: ServerEvent.RouteError });\n\t}\n\n\tpublic override run(error: Error, _request: ApiRequest, response: ApiResponse) {\n\t\t// Log the error to console:\n\t\tthis.container.logger.fatal(error);\n\n\t\t// Send a response to the client if none was sent:\n\t\tif (!response.writableEnded) response.status(HttpCodes.InternalServerError).json({ error: error.message ?? error });\n\t}\n}\n"]}
|
|
@@ -8,9 +8,9 @@ var __defProp = Object.defineProperty;
|
|
|
8
8
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
9
|
var _PluginListener = class _PluginListener extends framework.Listener {
|
|
10
10
|
constructor(context) {
|
|
11
|
-
super(context, { emitter: "server", event: Server_cjs.
|
|
11
|
+
super(context, { emitter: "server", event: Server_cjs.ServerEvent.MiddlewareError });
|
|
12
12
|
}
|
|
13
|
-
run(error,
|
|
13
|
+
run(error, _request, response) {
|
|
14
14
|
this.container.logger.fatal(error);
|
|
15
15
|
if (!response.writableEnded) response.status(HttpCodes_cjs.HttpCodes.InternalServerError).json({ error: error.message ?? error });
|
|
16
16
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/listeners/PluginServerMiddlewareError.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/PluginServerMiddlewareError.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;AAGzB,SAAS,iBAAiB;AAC1B,SAAS,mBAAmB;AAErB,IAAM,kBAAN,MAAM,wBAAuB,SAAS;AAAA,EACrC,YAAY,SAAiC;AACnD,UAAM,SAAS,EAAE,SAAS,UAAU,OAAO,YAAY,gBAAgB,CAAC;AAAA,EACzE;AAAA,EAEgB,IAAI,OAAc,UAAsB,UAAuB;AAE9E,SAAK,UAAU,OAAO,MAAM,KAAK;AAGjC,QAAI,CAAC,SAAS,cAAe,UAAS,OAAO,UAAU,mBAAmB,EAAE,KAAK,EAAE,OAAO,MAAM,WAAW,MAAM,CAAC;AAAA,EACnH;AACD;AAZ6C;AAAtC,IAAM,iBAAN","sourcesContent":["import { Listener } from '@sapphire/framework';\nimport type { ApiRequest } from '../lib/structures/api/ApiRequest';\nimport type { ApiResponse } from '../lib/structures/api/ApiResponse';\nimport { HttpCodes } from '../lib/structures/http/HttpCodes';\nimport { ServerEvent } from '../lib/structures/http/Server';\n\nexport class PluginListener extends Listener {\n\tpublic constructor(context: Listener.LoaderContext) {\n\t\tsuper(context, { emitter: 'server', event: ServerEvent.MiddlewareError });\n\t}\n\n\tpublic override run(error: Error, _request: ApiRequest, response: ApiResponse) {\n\t\t// Log the error to console:\n\t\tthis.container.logger.fatal(error);\n\n\t\t// Send a response to the client if none was sent:\n\t\tif (!response.writableEnded) response.status(HttpCodes.InternalServerError).json({ error: error.message ?? error });\n\t}\n}\n"]}
|
|
@@ -7,13 +7,13 @@ var __defProp = Object.defineProperty;
|
|
|
7
7
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
8
|
var _PluginListener = class _PluginListener extends framework.Listener {
|
|
9
9
|
constructor(context) {
|
|
10
|
-
super(context, { emitter: "server", event: Server_cjs.
|
|
10
|
+
super(context, { emitter: "server", event: Server_cjs.ServerEvent.MiddlewareSuccess });
|
|
11
11
|
}
|
|
12
|
-
async run(request, response
|
|
12
|
+
async run(request, response) {
|
|
13
13
|
try {
|
|
14
|
-
await route.run(request, response);
|
|
14
|
+
await request.route.run(request, response);
|
|
15
15
|
} catch (error) {
|
|
16
|
-
this.container.server.emit(Server_cjs.
|
|
16
|
+
this.container.server.emit(Server_cjs.ServerEvent.RouteError, error, request, response);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/listeners/PluginServerMiddlewareSuccess.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/PluginServerMiddlewareSuccess.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;AAGzB,SAAS,mBAAmB;AAErB,IAAM,kBAAN,MAAM,wBAAuB,SAAS;AAAA,EACrC,YAAY,SAAiC;AACnD,UAAM,SAAS,EAAE,SAAS,UAAU,OAAO,YAAY,kBAAkB,CAAC;AAAA,EAC3E;AAAA,EAEA,MAAsB,IAAI,SAAqB,UAAuB;AACrE,QAAI;AACH,YAAM,QAAQ,MAAO,IAAI,SAAS,QAAQ;AAAA,IAC3C,SAAS,OAAO;AACf,WAAK,UAAU,OAAO,KAAK,YAAY,YAAY,OAAO,SAAS,QAAQ;AAAA,IAC5E;AAAA,EACD;AACD;AAZ6C;AAAtC,IAAM,iBAAN","sourcesContent":["import { Listener } from '@sapphire/framework';\nimport type { ApiRequest } from '../lib/structures/api/ApiRequest';\nimport type { ApiResponse } from '../lib/structures/api/ApiResponse';\nimport { ServerEvent } from '../lib/structures/http/Server';\n\nexport class PluginListener extends Listener {\n\tpublic constructor(context: Listener.LoaderContext) {\n\t\tsuper(context, { emitter: 'server', event: ServerEvent.MiddlewareSuccess });\n\t}\n\n\tpublic override async run(request: ApiRequest, response: ApiResponse) {\n\t\ttry {\n\t\t\tawait request.route!.run(request, response);\n\t\t} catch (error) {\n\t\t\tthis.container.server.emit(ServerEvent.RouteError, error, request, response);\n\t\t}\n\t}\n}\n"]}
|
|
@@ -2,28 +2,62 @@
|
|
|
2
2
|
|
|
3
3
|
var framework = require('@sapphire/framework');
|
|
4
4
|
var Server_cjs = require('../lib/structures/http/Server.cjs');
|
|
5
|
+
var RouterRoot_cjs = require('../lib/structures/router/RouterRoot.cjs');
|
|
5
6
|
|
|
6
7
|
var __defProp = Object.defineProperty;
|
|
8
|
+
var __typeError = (msg) => {
|
|
9
|
+
throw TypeError(msg);
|
|
10
|
+
};
|
|
7
11
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
12
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
13
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
14
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
15
|
+
var _PluginListener_instances, parseURL_fn;
|
|
8
16
|
var _PluginListener = class _PluginListener extends framework.Listener {
|
|
9
17
|
constructor(context) {
|
|
10
|
-
super(context, { emitter: "server", event: Server_cjs.
|
|
18
|
+
super(context, { emitter: "server", event: Server_cjs.ServerEvent.Request });
|
|
19
|
+
__privateAdd(this, _PluginListener_instances);
|
|
11
20
|
}
|
|
12
21
|
async run(request, response) {
|
|
13
|
-
const
|
|
22
|
+
const { parts, querystring } = __privateMethod(this, _PluginListener_instances, parseURL_fn).call(this, request.url);
|
|
23
|
+
request.query = Object.fromEntries(new URLSearchParams(querystring).entries());
|
|
24
|
+
const branch = this.container.server.routes.router.find(parts);
|
|
25
|
+
const node = branch ? branch.node : null;
|
|
26
|
+
const route = node ? node.get(request.method ?? "GET") : null;
|
|
27
|
+
if (node !== null) {
|
|
28
|
+
request.params = node.extractParameters(parts);
|
|
29
|
+
}
|
|
30
|
+
request.routerNode = node;
|
|
31
|
+
request.route = route;
|
|
14
32
|
try {
|
|
15
|
-
await this.container.server.middlewares.run(request, response
|
|
33
|
+
await this.container.server.middlewares.run(request, response);
|
|
16
34
|
} catch (error) {
|
|
17
|
-
this.container.server.emit(Server_cjs.
|
|
35
|
+
this.container.server.emit(Server_cjs.ServerEvent.MiddlewareError, error, request, response);
|
|
18
36
|
return;
|
|
19
37
|
}
|
|
20
|
-
if (
|
|
21
|
-
this.container.server.emit(Server_cjs.
|
|
38
|
+
if (branch === null) {
|
|
39
|
+
this.container.server.emit(Server_cjs.ServerEvent.RouterBranchNotFound, request, response);
|
|
40
|
+
} else if (route === null) {
|
|
41
|
+
this.container.server.emit(Server_cjs.ServerEvent.RouterBranchMethodNotAllowed, request, response);
|
|
22
42
|
} else {
|
|
23
|
-
this.container.server.emit(Server_cjs.
|
|
43
|
+
this.container.server.emit(Server_cjs.ServerEvent.RouterFound, request, response);
|
|
24
44
|
}
|
|
25
45
|
}
|
|
26
46
|
};
|
|
47
|
+
_PluginListener_instances = new WeakSet();
|
|
48
|
+
parseURL_fn = /* @__PURE__ */ __name(function(url = "") {
|
|
49
|
+
const index = url.indexOf("?");
|
|
50
|
+
let pathname;
|
|
51
|
+
let querystring;
|
|
52
|
+
if (index === -1) {
|
|
53
|
+
pathname = url;
|
|
54
|
+
querystring = "";
|
|
55
|
+
} else {
|
|
56
|
+
pathname = url.substring(0, index);
|
|
57
|
+
querystring = url.substring(index + 1);
|
|
58
|
+
}
|
|
59
|
+
return { parts: RouterRoot_cjs.RouterRoot.normalize(pathname), querystring };
|
|
60
|
+
}, "#parseURL");
|
|
27
61
|
__name(_PluginListener, "PluginListener");
|
|
28
62
|
var PluginListener = _PluginListener;
|
|
29
63
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/listeners/PluginServerRequest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/PluginServerRequest.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,SAAS,gBAAgB;AAIzB,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAL3B;AAOO,IAAM,kBAAN,MAAM,wBAAuB,SAAS;AAAA,EACrC,YAAY,SAAiC;AACnD,UAAM,SAAS,EAAE,SAAS,UAAU,OAAO,YAAY,QAAQ,CAAC;AAF3D;AAAA,EAGN;AAAA,EAEA,MAAsB,IAAI,SAAqB,UAAuB;AACrE,UAAM,EAAE,OAAO,YAAY,IAAI,sBAAK,wCAAL,WAAe,QAAQ;AACtD,YAAQ,QAAQ,OAAO,YAAY,IAAI,gBAAgB,WAAW,EAAE,QAAQ,CAAC;AAE7E,UAAM,SAAS,KAAK,UAAU,OAAO,OAAO,OAAO,KAAK,KAAK;AAC7D,UAAM,OAAO,SAAS,OAAO,OAAO;AACpC,UAAM,QAAQ,OAAO,KAAK,IAAK,QAAQ,UAAU,KAAoB,IAAI;AAEzE,QAAI,SAAS,MAAM;AAClB,cAAQ,SAAS,KAAM,kBAAkB,KAAK;AAAA,IAC/C;AAEA,YAAQ,aAAa;AACrB,YAAQ,QAAQ;AAEhB,QAAI;AAEH,YAAM,KAAK,UAAU,OAAO,YAAY,IAAI,SAAS,QAAQ;AAAA,IAC9D,SAAS,OAAO;AACf,WAAK,UAAU,OAAO,KAAK,YAAY,iBAAiB,OAAO,SAAS,QAAQ;AAGhF;AAAA,IACD;AAEA,QAAI,WAAW,MAAM;AACpB,WAAK,UAAU,OAAO,KAAK,YAAY,sBAAsB,SAAS,QAAQ;AAAA,IAC/E,WAAW,UAAU,MAAM;AAC1B,WAAK,UAAU,OAAO,KAAK,YAAY,8BAA8B,SAAS,QAAQ;AAAA,IACvF,OAAO;AACN,WAAK,UAAU,OAAO,KAAK,YAAY,aAAa,SAAS,QAAQ;AAAA,IACtE;AAAA,EACD;AAiBD;AAtDO;AAuCN,cAAS,gCAAC,MAAM,IAAI;AACnB,QAAM,QAAQ,IAAI,QAAQ,GAAG;AAE7B,MAAI;AACJ,MAAI;AACJ,MAAI,UAAU,IAAI;AACjB,eAAW;AACX,kBAAc;AAAA,EACf,OAAO;AACN,eAAW,IAAI,UAAU,GAAG,KAAK;AACjC,kBAAc,IAAI,UAAU,QAAQ,CAAC;AAAA,EACtC;AAEA,SAAO,EAAE,OAAO,WAAW,UAAU,QAAQ,GAAG,YAAY;AAC7D,GAdS;AAvCmC;AAAtC,IAAM,iBAAN","sourcesContent":["import { Listener } from '@sapphire/framework';\nimport type { ApiRequest } from '../lib/structures/api/ApiRequest';\nimport type { ApiResponse } from '../lib/structures/api/ApiResponse';\nimport type { MethodName } from '../lib/structures/http/HttpMethods';\nimport { ServerEvent } from '../lib/structures/http/Server';\nimport { RouterRoot } from '../lib/structures/router/RouterRoot';\n\nexport class PluginListener extends Listener {\n\tpublic constructor(context: Listener.LoaderContext) {\n\t\tsuper(context, { emitter: 'server', event: ServerEvent.Request });\n\t}\n\n\tpublic override async run(request: ApiRequest, response: ApiResponse) {\n\t\tconst { parts, querystring } = this.#parseURL(request.url);\n\t\trequest.query = Object.fromEntries(new URLSearchParams(querystring).entries());\n\n\t\tconst branch = this.container.server.routes.router.find(parts);\n\t\tconst node = branch ? branch.node : null;\n\t\tconst route = node ? node.get((request.method ?? 'GET') as MethodName) : null;\n\n\t\tif (node !== null) {\n\t\t\trequest.params = node!.extractParameters(parts);\n\t\t}\n\n\t\trequest.routerNode = node;\n\t\trequest.route = route;\n\n\t\ttry {\n\t\t\t// Middlewares need to be run regardless of the match, specially since browsers do an OPTIONS request first.\n\t\t\tawait this.container.server.middlewares.run(request, response);\n\t\t} catch (error) {\n\t\t\tthis.container.server.emit(ServerEvent.MiddlewareError, error, request, response);\n\n\t\t\t// If a middleware errored, it might cause undefined behavior in the routes, so we will return early.\n\t\t\treturn;\n\t\t}\n\n\t\tif (branch === null) {\n\t\t\tthis.container.server.emit(ServerEvent.RouterBranchNotFound, request, response);\n\t\t} else if (route === null) {\n\t\t\tthis.container.server.emit(ServerEvent.RouterBranchMethodNotAllowed, request, response);\n\t\t} else {\n\t\t\tthis.container.server.emit(ServerEvent.RouterFound, request, response);\n\t\t}\n\t}\n\n\t#parseURL(url = '') {\n\t\tconst index = url.indexOf('?');\n\n\t\tlet pathname: string;\n\t\tlet querystring: string;\n\t\tif (index === -1) {\n\t\t\tpathname = url;\n\t\t\tquerystring = '';\n\t\t} else {\n\t\t\tpathname = url.substring(0, index);\n\t\t\tquerystring = url.substring(index + 1);\n\t\t}\n\n\t\treturn { parts: RouterRoot.normalize(pathname), querystring };\n\t}\n}\n"]}
|
package/dist/cjs/listeners/{PluginServerMatch.cjs → PluginServerRouterBranchMethodNotAllowed.cjs}
RENAMED
|
@@ -7,15 +7,10 @@ var __defProp = Object.defineProperty;
|
|
|
7
7
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
8
|
var _PluginListener = class _PluginListener extends framework.Listener {
|
|
9
9
|
constructor(context) {
|
|
10
|
-
super(context, { emitter: "server", event: Server_cjs.
|
|
10
|
+
super(context, { emitter: "server", event: Server_cjs.ServerEvent.RouterBranchMethodNotAllowed });
|
|
11
11
|
}
|
|
12
|
-
run(
|
|
13
|
-
|
|
14
|
-
response.writableEnded ? Server_cjs.ServerEvents.MiddlewareFailure : Server_cjs.ServerEvents.MiddlewareSuccess,
|
|
15
|
-
request,
|
|
16
|
-
response,
|
|
17
|
-
route
|
|
18
|
-
);
|
|
12
|
+
run(_, response) {
|
|
13
|
+
if (!response.writableEnded) response.methodNotAllowed();
|
|
19
14
|
}
|
|
20
15
|
};
|
|
21
16
|
__name(_PluginListener, "PluginListener");
|
|
@@ -23,4 +18,4 @@ var PluginListener = _PluginListener;
|
|
|
23
18
|
|
|
24
19
|
exports.PluginListener = PluginListener;
|
|
25
20
|
//# sourceMappingURL=out.js.map
|
|
26
|
-
//# sourceMappingURL=
|
|
21
|
+
//# sourceMappingURL=PluginServerRouterBranchMethodNotAllowed.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/PluginServerRouterBranchMethodNotAllowed.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;AAGzB,SAAS,mBAAmB;AAErB,IAAM,kBAAN,MAAM,wBAAuB,SAAS;AAAA,EACrC,YAAY,SAAiC;AACnD,UAAM,SAAS,EAAE,SAAS,UAAU,OAAO,YAAY,6BAA6B,CAAC;AAAA,EACtF;AAAA,EAEgB,IAAI,GAAe,UAAuB;AACzD,QAAI,CAAC,SAAS,cAAe,UAAS,iBAAiB;AAAA,EACxD;AACD;AAR6C;AAAtC,IAAM,iBAAN","sourcesContent":["import { Listener } from '@sapphire/framework';\nimport type { ApiRequest } from '../lib/structures/api/ApiRequest';\nimport type { ApiResponse } from '../lib/structures/api/ApiResponse';\nimport { ServerEvent } from '../lib/structures/http/Server';\n\nexport class PluginListener extends Listener {\n\tpublic constructor(context: Listener.LoaderContext) {\n\t\tsuper(context, { emitter: 'server', event: ServerEvent.RouterBranchMethodNotAllowed });\n\t}\n\n\tpublic override run(_: ApiRequest, response: ApiResponse) {\n\t\tif (!response.writableEnded) response.methodNotAllowed();\n\t}\n}\n"]}
|
|
@@ -7,7 +7,7 @@ var __defProp = Object.defineProperty;
|
|
|
7
7
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
8
|
var _PluginListener = class _PluginListener extends framework.Listener {
|
|
9
9
|
constructor(context) {
|
|
10
|
-
super(context, { emitter: "server", event: Server_cjs.
|
|
10
|
+
super(context, { emitter: "server", event: Server_cjs.ServerEvent.RouterBranchNotFound });
|
|
11
11
|
}
|
|
12
12
|
run(_, response) {
|
|
13
13
|
if (!response.writableEnded) response.notFound();
|
|
@@ -18,4 +18,4 @@ var PluginListener = _PluginListener;
|
|
|
18
18
|
|
|
19
19
|
exports.PluginListener = PluginListener;
|
|
20
20
|
//# sourceMappingURL=out.js.map
|
|
21
|
-
//# sourceMappingURL=
|
|
21
|
+
//# sourceMappingURL=PluginServerRouterBranchNotFound.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/PluginServerRouterBranchNotFound.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;AAGzB,SAAS,mBAAmB;AAErB,IAAM,kBAAN,MAAM,wBAAuB,SAAS;AAAA,EACrC,YAAY,SAAiC;AACnD,UAAM,SAAS,EAAE,SAAS,UAAU,OAAO,YAAY,qBAAqB,CAAC;AAAA,EAC9E;AAAA,EAEgB,IAAI,GAAe,UAAuB;AACzD,QAAI,CAAC,SAAS,cAAe,UAAS,SAAS;AAAA,EAChD;AACD;AAR6C;AAAtC,IAAM,iBAAN","sourcesContent":["import { Listener } from '@sapphire/framework';\nimport type { ApiRequest } from '../lib/structures/api/ApiRequest';\nimport type { ApiResponse } from '../lib/structures/api/ApiResponse';\nimport { ServerEvent } from '../lib/structures/http/Server';\n\nexport class PluginListener extends Listener {\n\tpublic constructor(context: Listener.LoaderContext) {\n\t\tsuper(context, { emitter: 'server', event: ServerEvent.RouterBranchNotFound });\n\t}\n\n\tpublic override run(_: ApiRequest, response: ApiResponse) {\n\t\tif (!response.writableEnded) response.notFound();\n\t}\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var framework = require('@sapphire/framework');
|
|
4
|
+
var Server_cjs = require('../lib/structures/http/Server.cjs');
|
|
5
|
+
|
|
6
|
+
var __defProp = Object.defineProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var _PluginListener = class _PluginListener extends framework.Listener {
|
|
9
|
+
constructor(context) {
|
|
10
|
+
super(context, { emitter: "server", event: Server_cjs.ServerEvent.RouterFound });
|
|
11
|
+
}
|
|
12
|
+
run(request, response) {
|
|
13
|
+
this.container.server.emit(response.writableEnded ? Server_cjs.ServerEvent.MiddlewareFailure : Server_cjs.ServerEvent.MiddlewareSuccess, request, response);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
__name(_PluginListener, "PluginListener");
|
|
17
|
+
var PluginListener = _PluginListener;
|
|
18
|
+
|
|
19
|
+
exports.PluginListener = PluginListener;
|
|
20
|
+
//# sourceMappingURL=out.js.map
|
|
21
|
+
//# sourceMappingURL=PluginServerRouterFound.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/PluginServerRouterFound.ts"],"names":[],"mappings":";;;;AAAA,SAAS,gBAAgB;AAGzB,SAAS,mBAAmB;AAErB,IAAM,kBAAN,MAAM,wBAAuB,SAAS;AAAA,EACrC,YAAY,SAAiC;AACnD,UAAM,SAAS,EAAE,SAAS,UAAU,OAAO,YAAY,YAAY,CAAC;AAAA,EACrE;AAAA,EAEgB,IAAI,SAAqB,UAAuB;AAC/D,SAAK,UAAU,OAAO,KAAK,SAAS,gBAAgB,YAAY,oBAAoB,YAAY,mBAAmB,SAAS,QAAQ;AAAA,EACrI;AACD;AAR6C;AAAtC,IAAM,iBAAN","sourcesContent":["import { Listener } from '@sapphire/framework';\nimport type { ApiRequest } from '../lib/structures/api/ApiRequest';\nimport type { ApiResponse } from '../lib/structures/api/ApiResponse';\nimport { ServerEvent } from '../lib/structures/http/Server';\n\nexport class PluginListener extends Listener {\n\tpublic constructor(context: Listener.LoaderContext) {\n\t\tsuper(context, { emitter: 'server', event: ServerEvent.RouterFound });\n\t}\n\n\tpublic override run(request: ApiRequest, response: ApiResponse) {\n\t\tthis.container.server.emit(response.writableEnded ? ServerEvent.MiddlewareFailure : ServerEvent.MiddlewareSuccess, request, response);\n\t}\n}\n"]}
|
|
@@ -2,22 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
var pieces = require('@sapphire/pieces');
|
|
4
4
|
var PluginRouteError_cjs = require('./PluginRouteError.cjs');
|
|
5
|
-
var PluginServerMatch_cjs = require('./PluginServerMatch.cjs');
|
|
6
5
|
var PluginServerMiddlewareError_cjs = require('./PluginServerMiddlewareError.cjs');
|
|
7
6
|
var PluginServerMiddlewareSuccess_cjs = require('./PluginServerMiddlewareSuccess.cjs');
|
|
8
|
-
var PluginServerNoMatch_cjs = require('./PluginServerNoMatch.cjs');
|
|
9
7
|
var PluginServerRequest_cjs = require('./PluginServerRequest.cjs');
|
|
8
|
+
var PluginServerRouterBranchMethodNotAllowed_cjs = require('./PluginServerRouterBranchMethodNotAllowed.cjs');
|
|
9
|
+
var PluginServerRouterBranchNotFound_cjs = require('./PluginServerRouterBranchNotFound.cjs');
|
|
10
|
+
var PluginServerRouterFound_cjs = require('./PluginServerRouterFound.cjs');
|
|
10
11
|
|
|
11
12
|
var __defProp = Object.defineProperty;
|
|
12
13
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
13
14
|
function loadListeners() {
|
|
14
15
|
const store = "listeners";
|
|
15
16
|
void pieces.container.stores.loadPiece({ name: "PluginRouteError", piece: PluginRouteError_cjs.PluginListener, store });
|
|
16
|
-
void pieces.container.stores.loadPiece({ name: "PluginServerMatch", piece: PluginServerMatch_cjs.PluginListener, store });
|
|
17
17
|
void pieces.container.stores.loadPiece({ name: "PluginServerMiddlewareError", piece: PluginServerMiddlewareError_cjs.PluginListener, store });
|
|
18
18
|
void pieces.container.stores.loadPiece({ name: "PluginServerMiddlewareSuccess", piece: PluginServerMiddlewareSuccess_cjs.PluginListener, store });
|
|
19
|
-
void pieces.container.stores.loadPiece({ name: "PluginServerNoMatch", piece: PluginServerNoMatch_cjs.PluginListener, store });
|
|
20
19
|
void pieces.container.stores.loadPiece({ name: "PluginServerRequest", piece: PluginServerRequest_cjs.PluginListener, store });
|
|
20
|
+
void pieces.container.stores.loadPiece({ name: "PluginServerRouterBranchMethodNotAllowed", piece: PluginServerRouterBranchMethodNotAllowed_cjs.PluginListener, store });
|
|
21
|
+
void pieces.container.stores.loadPiece({ name: "PluginServerRouterBranchNotFound", piece: PluginServerRouterBranchNotFound_cjs.PluginListener, store });
|
|
22
|
+
void pieces.container.stores.loadPiece({ name: "PluginServerRouterFound", piece: PluginServerRouterFound_cjs.PluginListener, store });
|
|
21
23
|
}
|
|
22
24
|
__name(loadListeners, "loadListeners");
|
|
23
25
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/listeners/_load.ts"],"names":[],"mappings":";;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB,wBAAwB;AACnD,SAAS,kBAAkB,
|
|
1
|
+
{"version":3,"sources":["../../../src/listeners/_load.ts"],"names":[],"mappings":";;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,kBAAkB,wBAAwB;AACnD,SAAS,kBAAkB,mCAAmC;AAC9D,SAAS,kBAAkB,qCAAqC;AAChE,SAAS,kBAAkB,2BAA2B;AACtD,SAAS,kBAAkB,gDAAgD;AAC3E,SAAS,kBAAkB,wCAAwC;AACnE,SAAS,kBAAkB,+BAA+B;AAEnD,SAAS,gBAAgB;AAC/B,QAAM,QAAQ;AACd,OAAK,UAAU,OAAO,UAAU,EAAE,MAAM,oBAAoB,OAAO,kBAAkB,MAAM,CAAC;AAC5F,OAAK,UAAU,OAAO,UAAU,EAAE,MAAM,+BAA+B,OAAO,6BAA6B,MAAM,CAAC;AAClH,OAAK,UAAU,OAAO,UAAU,EAAE,MAAM,iCAAiC,OAAO,+BAA+B,MAAM,CAAC;AACtH,OAAK,UAAU,OAAO,UAAU,EAAE,MAAM,uBAAuB,OAAO,qBAAqB,MAAM,CAAC;AAClG,OAAK,UAAU,OAAO,UAAU,EAAE,MAAM,4CAA4C,OAAO,0CAA0C,MAAM,CAAC;AAC5I,OAAK,UAAU,OAAO,UAAU,EAAE,MAAM,oCAAoC,OAAO,kCAAkC,MAAM,CAAC;AAC5H,OAAK,UAAU,OAAO,UAAU,EAAE,MAAM,2BAA2B,OAAO,yBAAyB,MAAM,CAAC;AAC3G;AATgB","sourcesContent":["import { container } from '@sapphire/pieces';\nimport { PluginListener as PluginRouteError } from './PluginRouteError';\nimport { PluginListener as PluginServerMiddlewareError } from './PluginServerMiddlewareError';\nimport { PluginListener as PluginServerMiddlewareSuccess } from './PluginServerMiddlewareSuccess';\nimport { PluginListener as PluginServerRequest } from './PluginServerRequest';\nimport { PluginListener as PluginServerRouterBranchMethodNotAllowed } from './PluginServerRouterBranchMethodNotAllowed';\nimport { PluginListener as PluginServerRouterBranchNotFound } from './PluginServerRouterBranchNotFound';\nimport { PluginListener as PluginServerRouterFound } from './PluginServerRouterFound';\n\nexport function loadListeners() {\n\tconst store = 'listeners' as const;\n\tvoid container.stores.loadPiece({ name: 'PluginRouteError', piece: PluginRouteError, store });\n\tvoid container.stores.loadPiece({ name: 'PluginServerMiddlewareError', piece: PluginServerMiddlewareError, store });\n\tvoid container.stores.loadPiece({ name: 'PluginServerMiddlewareSuccess', piece: PluginServerMiddlewareSuccess, store });\n\tvoid container.stores.loadPiece({ name: 'PluginServerRequest', piece: PluginServerRequest, store });\n\tvoid container.stores.loadPiece({ name: 'PluginServerRouterBranchMethodNotAllowed', piece: PluginServerRouterBranchMethodNotAllowed, store });\n\tvoid container.stores.loadPiece({ name: 'PluginServerRouterBranchNotFound', piece: PluginServerRouterBranchNotFound, store });\n\tvoid container.stores.loadPiece({ name: 'PluginServerRouterFound', piece: PluginServerRouterFound, store });\n}\n"]}
|
|
@@ -13,20 +13,21 @@ var _PluginMiddleware = class _PluginMiddleware extends Middleware_cjs.Middlewar
|
|
|
13
13
|
__publicField(this, "mediaParsers");
|
|
14
14
|
this.mediaParsers = this.container.server.mediaParsers;
|
|
15
15
|
}
|
|
16
|
-
async run(request, response
|
|
16
|
+
async run(request, response) {
|
|
17
|
+
if (!request.route) return;
|
|
17
18
|
const contentType = request.headers["content-type"];
|
|
18
19
|
if (typeof contentType !== "string") return;
|
|
19
20
|
const lengthString = request.headers["content-length"];
|
|
20
21
|
if (typeof lengthString !== "string") return;
|
|
21
22
|
const length = Number(lengthString);
|
|
22
|
-
const maximumLength = route.maximumBodyLength;
|
|
23
|
+
const maximumLength = request.route.maximumBodyLength;
|
|
23
24
|
if (length > maximumLength) {
|
|
24
25
|
response.status(HttpCodes_cjs.HttpCodes.PayloadTooLarge).json({ error: "Exceeded maximum content length." });
|
|
25
26
|
return;
|
|
26
27
|
}
|
|
27
28
|
const type = this.mediaParsers.parseContentType(contentType);
|
|
28
29
|
const parser = this.mediaParsers.get(type);
|
|
29
|
-
if (!parser || !parser.accepts(route)) {
|
|
30
|
+
if (!parser || !parser.accepts(request.route)) {
|
|
30
31
|
response.status(HttpCodes_cjs.HttpCodes.UnsupportedMediaType).json({ error: `Unsupported type ${type}.` });
|
|
31
32
|
return;
|
|
32
33
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middlewares/body.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,iBAAiB;AAE1B,SAAS,kBAAkB;
|
|
1
|
+
{"version":3,"sources":["../../../src/middlewares/body.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,iBAAiB;AAE1B,SAAS,kBAAkB;AAEpB,IAAM,oBAAN,MAAM,0BAAyB,WAAW;AAAA,EAGzC,YAAY,SAAmC;AACrD,UAAM,SAAS,EAAE,UAAU,GAAG,CAAC;AAHhC,wBAAiB;AAIhB,SAAK,eAAe,KAAK,UAAU,OAAO;AAAA,EAC3C;AAAA,EAEA,MAAsB,IAAI,SAA6B,UAA+B;AACrF,QAAI,CAAC,QAAQ,MAAO;AAGpB,UAAM,cAAc,QAAQ,QAAQ,cAAc;AAClD,QAAI,OAAO,gBAAgB,SAAU;AAGrC,UAAM,eAAe,QAAQ,QAAQ,gBAAgB;AACrD,QAAI,OAAO,iBAAiB,SAAU;AAGtC,UAAM,SAAS,OAAO,YAAY;AAClC,UAAM,gBAAgB,QAAQ,MAAM;AACpC,QAAI,SAAS,eAAe;AAC3B,eAAS,OAAO,UAAU,eAAe,EAAE,KAAK,EAAE,OAAO,mCAAmC,CAAC;AAC7F;AAAA,IACD;AAGA,UAAM,OAAO,KAAK,aAAa,iBAAiB,WAAW;AAC3D,UAAM,SAAS,KAAK,aAAa,IAAI,IAAI;AACzC,QAAI,CAAC,UAAU,CAAC,OAAO,QAAQ,QAAQ,KAAK,GAAG;AAC9C,eAAS,OAAO,UAAU,oBAAoB,EAAE,KAAK,EAAE,OAAO,oBAAoB,IAAI,IAAI,CAAC;AAC3F;AAAA,IACD;AAEA,QAAI;AAEH,cAAQ,OAAO,MAAM,OAAO,IAAI,OAAO;AAAA,IACxC,QAAQ;AACP,eAAS,OAAO,UAAU,UAAU,EAAE,KAAK,EAAE,OAAO,gBAAgB,IAAI,SAAS,CAAC;AAAA,IACnF;AAAA,EACD;AACD;AA1CiD;AAA1C,IAAM,mBAAN","sourcesContent":["import { HttpCodes } from '../lib/structures/http/HttpCodes';\nimport type { MediaParserStore } from '../lib/structures/MediaParserStore';\nimport { Middleware } from '../lib/structures/Middleware';\n\nexport class PluginMiddleware extends Middleware {\n\tprivate readonly mediaParsers: MediaParserStore;\n\n\tpublic constructor(context: Middleware.LoaderContext) {\n\t\tsuper(context, { position: 20 });\n\t\tthis.mediaParsers = this.container.server.mediaParsers;\n\t}\n\n\tpublic override async run(request: Middleware.Request, response: Middleware.Response) {\n\t\tif (!request.route) return;\n\n\t\t// RFC 1341 4.\n\t\tconst contentType = request.headers['content-type'];\n\t\tif (typeof contentType !== 'string') return;\n\n\t\t// RFC 7230 3.3.2.\n\t\tconst lengthString = request.headers['content-length'];\n\t\tif (typeof lengthString !== 'string') return;\n\n\t\t// Verify if the content length is lower than accepted:\n\t\tconst length = Number(lengthString);\n\t\tconst maximumLength = request.route.maximumBodyLength;\n\t\tif (length > maximumLength) {\n\t\t\tresponse.status(HttpCodes.PayloadTooLarge).json({ error: 'Exceeded maximum content length.' });\n\t\t\treturn;\n\t\t}\n\n\t\t// Verify if the content type is supported by the parser:\n\t\tconst type = this.mediaParsers.parseContentType(contentType);\n\t\tconst parser = this.mediaParsers.get(type);\n\t\tif (!parser || !parser.accepts(request.route)) {\n\t\t\tresponse.status(HttpCodes.UnsupportedMediaType).json({ error: `Unsupported type ${type}.` });\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\t// Parse the content body:\n\t\t\trequest.body = await parser.run(request);\n\t\t} catch {\n\t\t\tresponse.status(HttpCodes.BadRequest).json({ error: `Cannot parse ${type} data.` });\n\t\t}\n\t}\n}\n"]}
|
|
@@ -15,20 +15,17 @@ var _PluginMiddleware = class _PluginMiddleware extends Middleware_cjs.Middlewar
|
|
|
15
15
|
this.origin = this.container.server.options.origin ?? "*";
|
|
16
16
|
this.routes = this.container.stores.get("routes");
|
|
17
17
|
}
|
|
18
|
-
run(request, response
|
|
18
|
+
run(request, response) {
|
|
19
19
|
response.setHeader("Date", (/* @__PURE__ */ new Date()).toUTCString());
|
|
20
20
|
response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
21
21
|
response.setHeader("Access-Control-Allow-Origin", this.origin);
|
|
22
22
|
response.setHeader("Access-Control-Allow-Headers", "Authorization, User-Agent, Content-Type");
|
|
23
|
-
response.setHeader("Access-Control-Allow-Methods", this.getMethods(route));
|
|
24
|
-
this.ensurePotentialEarlyExit(request, response
|
|
23
|
+
response.setHeader("Access-Control-Allow-Methods", this.getMethods(request.route ?? null));
|
|
24
|
+
this.ensurePotentialEarlyExit(request, response);
|
|
25
25
|
}
|
|
26
26
|
getMethods(route) {
|
|
27
27
|
if (route === null) {
|
|
28
|
-
|
|
29
|
-
if (methods.size === 0) return "";
|
|
30
|
-
if (methods.size === 1) return methods.firstKey();
|
|
31
|
-
return [...methods.keys()].join(", ");
|
|
28
|
+
return this.routes.router.supportedMethods.join(", ");
|
|
32
29
|
}
|
|
33
30
|
if (route.methods.size === 0) return "";
|
|
34
31
|
if (route.methods.size === 1) return route.methods.keys().next().value;
|
|
@@ -49,13 +46,15 @@ var _PluginMiddleware = class _PluginMiddleware extends Middleware_cjs.Middlewar
|
|
|
49
46
|
* @param response The API response that will go out
|
|
50
47
|
* @param route The route being requested by the request
|
|
51
48
|
*/
|
|
52
|
-
ensurePotentialEarlyExit(
|
|
53
|
-
if (
|
|
49
|
+
ensurePotentialEarlyExit({ method, route, routerNode }, response) {
|
|
50
|
+
if (method === "OPTIONS") {
|
|
54
51
|
if (!route || !route.methods.has("OPTIONS")) {
|
|
55
52
|
response.end();
|
|
56
53
|
}
|
|
57
|
-
} else if (
|
|
54
|
+
} else if (routerNode === null) {
|
|
58
55
|
response.status(HttpCodes_cjs.HttpCodes.NotFound).end();
|
|
56
|
+
} else if (route === null) {
|
|
57
|
+
response.status(HttpCodes_cjs.HttpCodes.MethodNotAllowed).end();
|
|
59
58
|
}
|
|
60
59
|
}
|
|
61
60
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/middlewares/headers.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,kBAAkB;AAG3B,SAAS,iBAAiB;AAEnB,IAAM,oBAAN,MAAM,0BAAyB,WAAW;AAAA,EAIzC,YAAY,SAAmC;AACrD,UAAM,SAAS,EAAE,UAAU,GAAG,CAAC;AAJhC,wBAAiB;AACjB,wBAAiB;AAIhB,SAAK,SAAS,KAAK,UAAU,OAAO,QAAQ,UAAU;AACtD,SAAK,SAAS,KAAK,UAAU,OAAO,IAAI,QAAQ;AAAA,EACjD;AAAA,EAEgB,IAAI,SAA6B,UAA+B
|
|
1
|
+
{"version":3,"sources":["../../../src/middlewares/headers.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,kBAAkB;AAG3B,SAAS,iBAAiB;AAEnB,IAAM,oBAAN,MAAM,0BAAyB,WAAW;AAAA,EAIzC,YAAY,SAAmC;AACrD,UAAM,SAAS,EAAE,UAAU,GAAG,CAAC;AAJhC,wBAAiB;AACjB,wBAAiB;AAIhB,SAAK,SAAS,KAAK,UAAU,OAAO,QAAQ,UAAU;AACtD,SAAK,SAAS,KAAK,UAAU,OAAO,IAAI,QAAQ;AAAA,EACjD;AAAA,EAEgB,IAAI,SAA6B,UAA+B;AAC/E,aAAS,UAAU,SAAQ,oBAAI,KAAK,GAAE,YAAY,CAAC;AACnD,aAAS,UAAU,oCAAoC,MAAM;AAC7D,aAAS,UAAU,+BAA+B,KAAK,MAAM;AAC7D,aAAS,UAAU,gCAAgC,yCAAyC;AAC5F,aAAS,UAAU,gCAAgC,KAAK,WAAW,QAAQ,SAAS,IAAI,CAAC;AAEzF,SAAK,yBAAyB,SAAS,QAAQ;AAAA,EAChD;AAAA,EAEQ,WAAW,OAAqB;AACvC,QAAI,UAAU,MAAM;AACnB,aAAO,KAAK,OAAO,OAAO,iBAAiB,KAAK,IAAI;AAAA,IACrD;AAEA,QAAI,MAAM,QAAQ,SAAS,EAAG,QAAO;AACrC,QAAI,MAAM,QAAQ,SAAS,EAAG,QAAO,MAAM,QAAQ,KAAK,EAAE,KAAK,EAAE;AACjE,WAAO,CAAC,GAAG,MAAM,OAAO,EAAE,KAAK,IAAI;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBQ,yBAAyB,EAAE,QAAQ,OAAO,WAAW,GAAuB,UAA+B;AAClH,QAAI,WAAW,WAAW;AACzB,UAAI,CAAC,SAAS,CAAC,MAAM,QAAQ,IAAI,SAAS,GAAG;AAC5C,iBAAS,IAAI;AAAA,MACd;AAAA,IACD,WAAW,eAAe,MAAM;AAC/B,eAAS,OAAO,UAAU,QAAQ,EAAE,IAAI;AAAA,IACzC,WAAW,UAAU,MAAM;AAC1B,eAAS,OAAO,UAAU,gBAAgB,EAAE,IAAI;AAAA,IACjD;AAAA,EACD;AACD;AAxDiD;AAA1C,IAAM,mBAAN","sourcesContent":["import { Middleware } from '../lib/structures/Middleware';\nimport type { Route } from '../lib/structures/Route';\nimport type { RouteStore } from '../lib/structures/RouteStore';\nimport { HttpCodes } from '../lib/structures/http/HttpCodes';\n\nexport class PluginMiddleware extends Middleware {\n\tprivate readonly origin: string;\n\tprivate readonly routes: RouteStore;\n\n\tpublic constructor(context: Middleware.LoaderContext) {\n\t\tsuper(context, { position: 10 });\n\t\tthis.origin = this.container.server.options.origin ?? '*';\n\t\tthis.routes = this.container.stores.get('routes');\n\t}\n\n\tpublic override run(request: Middleware.Request, response: Middleware.Response) {\n\t\tresponse.setHeader('Date', new Date().toUTCString());\n\t\tresponse.setHeader('Access-Control-Allow-Credentials', 'true');\n\t\tresponse.setHeader('Access-Control-Allow-Origin', this.origin);\n\t\tresponse.setHeader('Access-Control-Allow-Headers', 'Authorization, User-Agent, Content-Type');\n\t\tresponse.setHeader('Access-Control-Allow-Methods', this.getMethods(request.route ?? null));\n\n\t\tthis.ensurePotentialEarlyExit(request, response);\n\t}\n\n\tprivate getMethods(route: Route | null) {\n\t\tif (route === null) {\n\t\t\treturn this.routes.router.supportedMethods.join(', ');\n\t\t}\n\n\t\tif (route.methods.size === 0) return '';\n\t\tif (route.methods.size === 1) return route.methods.keys().next().value;\n\t\treturn [...route.methods].join(', ');\n\t}\n\n\t/**\n\t * **RFC 7231 4.3.7.**\n\t * > This method allows a client to determine the options and/or requirements associated with a\n\t * > resource, or the capabilities of a server, without implying a resource action.\n\t *\n\t * This method ensures that the request is exited early in case required\n\t * The conditions in which an early exit is required are:\n\t * 1. If the request method is 'OPTIONS'. In this case the request is returned with status code 200\n\t * 2. If the requested route isn't matched with any existing route in the RouteStore.\n\t * In this case the request is returned with a status code 404.\n\t *\n\t * @param request The API Request coming in\n\t * @param response The API response that will go out\n\t * @param route The route being requested by the request\n\t */\n\tprivate ensurePotentialEarlyExit({ method, route, routerNode }: Middleware.Request, response: Middleware.Response) {\n\t\tif (method === 'OPTIONS') {\n\t\t\tif (!route || !route.methods.has('OPTIONS')) {\n\t\t\t\tresponse.end();\n\t\t\t}\n\t\t} else if (routerNode === null) {\n\t\t\tresponse.status(HttpCodes.NotFound).end();\n\t\t} else if (route === null) {\n\t\t\tresponse.status(HttpCodes.MethodNotAllowed).end();\n\t\t}\n\t}\n}\n"]}
|
|
@@ -9,7 +9,8 @@ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot
|
|
|
9
9
|
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
10
10
|
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
11
11
|
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
12
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
12
13
|
|
|
13
|
-
export { __name, __privateAdd, __privateGet, __privateSet, __publicField };
|
|
14
|
+
export { __name, __privateAdd, __privateGet, __privateMethod, __privateSet, __publicField };
|
|
14
15
|
//# sourceMappingURL=out.js.map
|
|
15
|
-
//# sourceMappingURL=chunk-
|
|
16
|
+
//# sourceMappingURL=chunk-S573YWRP.mjs.map
|