@ndiinginc/router 4.11.2 → 4.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/app.js +1 -2
  3. package/src/util.js +24 -35
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndiinginc/router",
3
- "version": "4.11.2",
3
+ "version": "4.12.0",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/app.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const http = require("http");
2
- const { EMPTY_PARAMS } = require("./util");
3
2
  const { Router } = require("./router");
4
3
  const { Request } = require("./request");
5
4
  const { Response } = require("./response");
@@ -67,7 +66,7 @@ class App extends Router {
67
66
  */
68
67
  async handle(req, res) {
69
68
  const { params, paramsLength, handlers, errorHandlers } = this.find(req.method, req.path);
70
- req.params = paramsLength ? Object.assign(Object.create(null), params) : EMPTY_PARAMS;
69
+ req.params = paramsLength ? Object.assign(Object.create(null), params) : Object.create(null);
71
70
  let err;
72
71
  try {
73
72
  const handlerPromise = async () => {
package/src/util.js CHANGED
@@ -1,6 +1,3 @@
1
- const EMPTY_PARAMS = Object.create(null);
2
- exports.EMPTY_PARAMS = EMPTY_PARAMS;
3
-
4
1
  /**
5
2
  * @param {String} raw
6
3
  * @returns {String}
@@ -14,39 +11,21 @@ function decodeParam(raw) {
14
11
  }
15
12
  exports.decodeParam = decodeParam;
16
13
 
17
- /**
18
- * @param {String} raw
19
- * @returns {String}
20
- */
21
- function decodeQuery(raw) {
22
- const normalized = raw.replace(/\+/g, " ");
23
- try {
24
- return decodeURIComponent(normalized);
25
- } catch (error) {
26
- return normalized;
27
- }
28
- }
29
-
30
14
  /**
31
15
  * @param {String} queryString
32
16
  * @returns {Object}
33
17
  */
34
18
  function parseQueryString(queryString) {
35
- if (queryString === "") return Object.create(null);
36
- const regex = /([^=&]*)=([^&]*)/g;
37
- const result = Object.create(null);
38
- let match;
39
- while ((match = regex.exec(queryString))) {
40
- const [, rawName, rawValue] = match;
41
- const name = decodeQuery(rawName);
42
- const value = decodeQuery(rawValue);
43
- if (Object.hasOwn(result, name)) {
44
- if (Array.isArray(result[name])) result[name].push(value);
45
- else result[name] = [result[name], value];
46
- } else result[name] = value;
19
+ const query = Object.create(null);
20
+ if (queryString) {
21
+ for (const [name, value] of new URLSearchParams(queryString)) {
22
+ if (Object.hasOwn(query, name)) {
23
+ if (Array.isArray(query[name])) query[name].push(value);
24
+ else query[name] = [query[name], value];
25
+ } else query[name] = value;
26
+ }
47
27
  }
48
-
49
- return result;
28
+ return query;
50
29
  }
51
30
  exports.parseQueryString = parseQueryString;
52
31
 
@@ -55,11 +34,21 @@ exports.parseQueryString = parseQueryString;
55
34
  * @returns {{path: any;queryString: any;}}
56
35
  */
57
36
  function parseUrl(url) {
58
- if (!(url.includes("?") || url.includes("#"))) {
59
- return { path: url, queryString: "" };
37
+ let path = url;
38
+ let queryString = "";
39
+ let hash = "";
40
+ if (url.includes("?") || url.includes("#")) {
41
+ const hashIdx = url.indexOf("#");
42
+ if (hashIdx !== -1) {
43
+ hash = url.slice(hashIdx + 1);
44
+ path = url.slice(0, hashIdx);
45
+ }
46
+ const queryIdx = path.indexOf("?");
47
+ if (queryIdx !== -1) {
48
+ queryString = path.slice(queryIdx + 1);
49
+ path = path.slice(0, queryIdx);
50
+ }
60
51
  }
61
- const regex = /^([^?#]*)(\?([^#]*))?(#.*)?$/;
62
- const [, path = "/", , queryString = ""] = regex.exec(url);
63
- return { path, queryString };
52
+ return { path, queryString, hash };
64
53
  }
65
54
  exports.parseUrl = parseUrl;