@ndiinginc/router 4.12.0 → 4.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndiinginc/router",
3
- "version": "4.12.0",
3
+ "version": "4.13.0",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/rest/router.http CHANGED
@@ -8,6 +8,10 @@ Content-Type: application/json; charset=utf-8; asdfadsf
8
8
  "user":"name",
9
9
  "pass":"word"
10
10
  }
11
+ ###
12
+ POST http://localhost:3000
13
+ Content-Type: application/x-www-form-urlencoded
14
+
11
15
  ###
12
16
  GET http://localhost:4000
13
17
  ###
package/src/index.js CHANGED
@@ -3,9 +3,11 @@ const { App } = require("./app");
3
3
  const { Router } = require("./router");
4
4
  const { json } = require("./middleware/json");
5
5
  const { cors } = require("./middleware/cors");
6
+ const { urlencoded } = require("./middleware/urlencoded");
6
7
 
7
8
  App.Router = Router;
8
9
  App.json = json;
9
10
  App.cors = cors;
11
+ App.urlencoded = urlencoded;
10
12
 
11
13
  module.exports = App;
@@ -0,0 +1,44 @@
1
+ const http = require("http");
2
+ const { parseQueryString } = require("../util");
3
+
4
+ /**
5
+ * @typedef {Object} Options
6
+ * @property {Number} [limit=1.024e+5]
7
+ */
8
+
9
+ /**
10
+ * @param {Options} Options
11
+ * @returns {Callback}
12
+ */
13
+ function urlencoded({ limit = 1.024e5 } = {}) {
14
+ return async (req, res) => {
15
+ if (!["POST", "PUT", "PATCH", "QUERY"].includes(req.method)) {
16
+ return;
17
+ }
18
+ const contentType = req.headers["content-type"];
19
+ if (!contentType || !contentType.includes("application/x-www-form-urlencoded")) {
20
+ return;
21
+ }
22
+ const contentLength = parseInt(req.headers["content-length"], 10);
23
+ if (contentLength && contentLength > limit) {
24
+ res.statusCode = 413;
25
+ throw new Error(http.STATUS_CODES[res.statusCode]);
26
+ }
27
+ let chunkLength = 0;
28
+ const chunks = [];
29
+ for await (const chunk of req) {
30
+ chunkLength += chunk.length;
31
+ if (chunkLength > limit) {
32
+ res.statusCode = 413;
33
+ throw new Error(http.STATUS_CODES[res.statusCode]);
34
+ }
35
+ chunks.push(chunk);
36
+ }
37
+ if (chunks.length === 0) {
38
+ req.body = null;
39
+ return;
40
+ }
41
+ req.body = parseQueryString(Buffer.concat(chunks).toString("utf8"));
42
+ };
43
+ }
44
+ exports.urlencoded = urlencoded;
package/src/router.js CHANGED
@@ -189,32 +189,14 @@ class Router {
189
189
  }
190
190
  const segment = segments[index];
191
191
  if (node.children.has(segment)) {
192
- const res = this.search(
193
- //
194
- method,
195
- segments,
196
- node.children.get(segment),
197
- path,
198
- params,
199
- state,
200
- index + 1,
201
- );
192
+ const res = this.search(method, segments, node.children.get(segment), path, params, state, index + 1);
202
193
  if (res.matched) return res;
203
194
  }
204
195
  if (node.params.size) {
205
196
  for (const [, child] of node.params) {
206
197
  const prev = params[child.name];
207
198
  params[child.name] = decodeParam(segment);
208
- const res = this.search(
209
- //
210
- method,
211
- segments,
212
- child,
213
- path,
214
- params,
215
- state,
216
- index + 1,
217
- );
199
+ const res = this.search(method, segments, child, path, params, state, index + 1);
218
200
  if (res.matched) return res;
219
201
  if (prev === undefined) {
220
202
  const { [child.name]: _, ...rest } = params;
@@ -223,8 +205,7 @@ class Router {
223
205
  }
224
206
  }
225
207
  if (node.wildcard && (node.wildcard.handlers.get(method) || node.wildcard.handlers.get("ALL"))) {
226
- const rest = segments.slice(index).join("/");
227
- params["wildcard"] = decodeParam(rest);
208
+ params["wildcard"] = segments.slice(index).map(decodeParam).join("/");
228
209
  path.push(node.wildcard);
229
210
  return { path, params, matched: true };
230
211
  }
@@ -265,12 +246,7 @@ class Router {
265
246
  * @returns {{matched: boolean;params: Object;paramsLength: number;handlers: any[];errorHandlers: any[];}}
266
247
  */
267
248
  find(method, path) {
268
- const res = this.search(
269
- //
270
- method,
271
- this.split(path),
272
- this.root,
273
- );
249
+ const res = this.search(method, this.split(path), this.root);
274
250
  const handlers = {};
275
251
  const errorHandlers = {};
276
252
  for (const r of res.path) {