@ndiinginc/router 4.4.1 → 4.5.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.4.1",
3
+ "version": "4.5.0",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const { inspect } = require("node:util");
1
+ require('./polyfill.js')
2
2
  const { json } = require("./middleware/json.js");
3
3
  const { cors } = require("./middleware/cors.js");
4
4
  const { App } = require("./app.js");
package/src/node.js CHANGED
@@ -1,37 +1,19 @@
1
1
  const { Pattern } = require("./pattern.js");
2
2
 
3
+
3
4
  class Node {
4
5
  constructor(segment = "") {
5
- this.segment = segment;
6
- this.hasRegExp = segment.startsWith(":") || segment.indexOf("*") !== -1;
7
6
  this.hasRegExpChildren = false;
8
- this.pattern = this.hasRegExp && new Pattern(segment);
7
+ this.pattern = /[:*]/.test(segment) && new Pattern(segment);
9
8
  /**@type {Map<string,Node>}*/
10
9
  this.children = new Map();
11
- this.handlers = new Map([
12
- ["ALL", []],
13
- ["GET", []],
14
- ["QUERY", []],
15
- ["HEAD", []],
16
- ["OPTIONS", []],
17
- ["TRACE", []],
18
- ["PUT", []],
19
- ["DELETE", []],
20
- ["POST", []],
21
- ["PATCH", []],
22
- ["CONNECT", []],
23
- ]);
24
- // debug
25
- Object.defineProperty(this, "handlers", { configurable: true, enumerable: false, writable: true });
10
+ this.handlers = new Map();
26
11
  }
27
12
 
28
13
  add(segment) {
29
- if (!this.children.has(segment)) {
30
- const node = new Node(segment);
31
- if (node.hasRegExp) this.hasRegExpChildren = true; // node.hasRegExp;
32
- this.children.set(segment, node);
33
- }
34
- return this.children.get(segment);
14
+ const node = new Node(segment);
15
+ if (node.pattern) this.hasRegExpChildren = true; // node.hasRegExp;
16
+ return this.children.getOrInsert(segment,node);
35
17
  }
36
18
 
37
19
  /**
@@ -41,12 +23,12 @@ class Node {
41
23
  for (const [method, handlers] of node.handlers) {
42
24
  for (const handler of handlers) {
43
25
  handler.seq += offset;
44
- this.handlers.get(method).push(handler);
26
+ this.handlers.getOrInsert(method,[]).push(handler);
45
27
  }
46
28
  }
47
29
 
48
30
  for (const [segment, child] of node.children) {
49
- if (child.hasRegExp) this.hasRegExpChildren = true; // child.hasRegExp;
31
+ if (child.pattern) this.hasRegExpChildren = true; // child.hasRegExp;
50
32
  if (!this.children.has(segment)) {
51
33
  child.setSeqOffset(offset);
52
34
  this.children.set(segment, child);
@@ -0,0 +1,21 @@
1
+ // Polyfill for Map.prototype.getOrInsert
2
+ if (!Map.prototype.getOrInsert) {
3
+ Map.prototype.getOrInsert = function (key, defaultValue) {
4
+ if (!this.has(key)) {
5
+ this.set(key, defaultValue);
6
+ }
7
+ return this.get(key);
8
+ };
9
+ }
10
+ // Polyfill for Map.prototype.getOrInsertComputed
11
+ if (!Map.prototype.getOrInsertComputed) {
12
+ Map.prototype.getOrInsertComputed = function (key, callbackFunction) {
13
+ if (typeof callbackFunction !== 'function') {
14
+ throw new TypeError('The callback argument must be a function');
15
+ }
16
+ if (!this.has(key)) {
17
+ this.set(key, callbackFunction(key));
18
+ }
19
+ return this.get(key);
20
+ };
21
+ }
package/src/router.js CHANGED
@@ -34,7 +34,7 @@ class Router {
34
34
  this.seq = node.getMaxSeq() + 1;
35
35
  } else {
36
36
  handler.seq = this.seq++;
37
- node.handlers.get(method).push(handler);
37
+ node.handlers.getOrInsert(method,[]).push(handler);
38
38
  }
39
39
  }
40
40
  }
@@ -116,7 +116,7 @@ class Router {
116
116
 
117
117
  if (!matched && node.hasRegExpChildren) {
118
118
  for (const [, child] of node.children) {
119
- if (!child.hasRegExp) continue;
119
+ if (!child.pattern) continue;
120
120
  const match = child.pattern.exec(input);
121
121
  if (match) {
122
122
  matched = child;
@@ -141,18 +141,20 @@ class Router {
141
141
 
142
142
  node = matched;
143
143
 
144
+ if(node.handlers.has("ALL"))
144
145
  for (const handler of node.handlers.get("ALL")) {
145
146
  if (handler.length === 3) errorHandlers[handler.seq] = handler;
146
147
  else handlers[handler.seq] = handler;
147
148
  }
148
149
  }
149
150
 
151
+ if(this.root.handlers.has("ALL"))
150
152
  for (const handler of this.root.handlers.get("ALL")) {
151
153
  if (handler.length === 3) errorHandlers[handler.seq] = handler;
152
154
  else handlers[handler.seq] = handler;
153
155
  }
154
156
 
155
- if (node) {
157
+ if (node&&node.handlers.has(method)) {
156
158
  for (const handler of node.handlers.get(method)) {
157
159
  handlers[handler.seq] = handler;
158
160
  }