@esportsplus/routing 0.1.31 → 0.1.32

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/build/browser.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { effect, reactive, root } from '@esportsplus/reactivity';
2
2
  import { next } from '@esportsplus/pipeline';
3
3
  import factory from './router/index.js';
4
- let cache = [];
4
+ let cache = [], location = window.location;
5
5
  function back() {
6
6
  window.history.back();
7
7
  }
@@ -9,30 +9,32 @@ function forward() {
9
9
  window.history.forward();
10
10
  }
11
11
  function href() {
12
- let data = new URL(window.location?.href || ''), path = data.hash ? data.hash.slice(1).split('?') : ['/', ''], request = {
13
- href: data.href,
14
- hostname: data.hostname,
12
+ let hash = location.hash || '#/', path = hash ? hash.slice(1).split('?') : ['/', ''], request = {
13
+ href: location.href,
14
+ hostname: location.hostname,
15
15
  method: 'GET',
16
- origin: data.origin,
16
+ origin: location.origin,
17
17
  path: path[0],
18
- port: data.port,
19
- protocol: data.protocol,
18
+ port: location.port,
19
+ protocol: location.protocol,
20
20
  query: {}
21
21
  };
22
22
  if (path[1]) {
23
- for (let [key, value] of (new URLSearchParams(path[1])).entries()) {
24
- request.query[key] = value;
23
+ let query = request.query, params = new URLSearchParams(path[1]);
24
+ for (let [key, value] of params.entries()) {
25
+ query[key] = value;
25
26
  }
26
27
  }
27
28
  return request;
28
29
  }
29
30
  function match(request, router, subdomain) {
30
31
  if (router.subdomains !== null) {
31
- for (let i = 0, n = router.subdomains.length; i < n; i++) {
32
- if (!request.hostname.startsWith(router.subdomains[i])) {
32
+ let subdomains = router.subdomains;
33
+ for (let i = 0, n = subdomains.length; i < n; i++) {
34
+ if (!request.hostname.startsWith(subdomains[i])) {
33
35
  continue;
34
36
  }
35
- subdomain = router.subdomains[i];
37
+ subdomain = subdomains[i];
36
38
  break;
37
39
  }
38
40
  }
@@ -2,11 +2,13 @@ import { ON_DELETE, ON_GET, ON_POST, ON_PUT, STATIC } from '../constants.js';
2
2
  import { Node } from './node.js';
3
3
  import pipeline from '@esportsplus/pipeline';
4
4
  function normalize(path) {
5
- if (path[0] !== '/') {
6
- path = '/' + path;
7
- }
8
- if (path.at(-1) === '/') {
9
- path = path.slice(0, -1);
5
+ if (path) {
6
+ if (path[0] !== '/') {
7
+ path = '/' + path;
8
+ }
9
+ if (path.at(-1) === '/') {
10
+ path = path.slice(0, -1);
11
+ }
10
12
  }
11
13
  return path || '/';
12
14
  }
@@ -40,7 +42,7 @@ class Router {
40
42
  }
41
43
  add(radixkey, route) {
42
44
  if (radixkey.indexOf(':') === -1 || this.root.add(radixkey, route).type === STATIC) {
43
- if (this.static[radixkey]) {
45
+ if (radixkey in this.static) {
44
46
  throw new Error(`Routing: static path '${radixkey}' is already in use`);
45
47
  }
46
48
  this.static[radixkey] = route;
@@ -113,11 +115,12 @@ class Router {
113
115
  }
114
116
  }
115
117
  if (route.subdomain) {
118
+ let subdomain = route.subdomain.toLowerCase();
116
119
  if (!this.subdomains) {
117
- this.subdomains = [route.subdomain];
120
+ this.subdomains = [subdomain];
118
121
  }
119
122
  else {
120
- this.subdomains.push(route.subdomain);
123
+ this.subdomains.push(subdomain);
121
124
  }
122
125
  }
123
126
  return this;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
4
  "@esportsplus/pipeline": "^1.1.5",
5
- "@esportsplus/reactivity": "^0.12.3",
5
+ "@esportsplus/reactivity": "^0.12.4",
6
6
  "@esportsplus/utilities": "^0.21.1"
7
7
  },
8
8
  "devDependencies": {
@@ -13,7 +13,7 @@
13
13
  "private": false,
14
14
  "type": "module",
15
15
  "types": "./build/index.d.ts",
16
- "version": "0.1.31",
16
+ "version": "0.1.32",
17
17
  "scripts": {
18
18
  "build": "tsc && tsc-alias",
19
19
  "-": "-"
package/src/browser.ts CHANGED
@@ -4,7 +4,8 @@ import { next } from '@esportsplus/pipeline';
4
4
  import factory from './router';
5
5
 
6
6
 
7
- let cache: Request<any>[] = [];
7
+ let cache: Request<any>[] = [],
8
+ location = window.location;
8
9
 
9
10
 
10
11
  function back() {
@@ -16,22 +17,25 @@ function forward() {
16
17
  }
17
18
 
18
19
  function href<T>() {
19
- let data = new URL( window.location?.href || '' ),
20
- path = data.hash ? data.hash.slice(1).split('?') : ['/', ''],
20
+ let hash = location.hash || '#/',
21
+ path = hash ? hash.slice(1).split('?') : ['/', ''],
21
22
  request = {
22
- href: data.href,
23
- hostname: data.hostname,
23
+ href: location.href,
24
+ hostname: location.hostname,
24
25
  method: 'GET',
25
- origin: data.origin,
26
+ origin: location.origin,
26
27
  path: path[0],
27
- port: data.port,
28
- protocol: data.protocol,
28
+ port: location.port,
29
+ protocol: location.protocol,
29
30
  query: {} as Record<PropertyKey, unknown>
30
31
  };
31
32
 
32
33
  if (path[1]) {
33
- for (let [key, value] of (new URLSearchParams(path[1])).entries()) {
34
- request.query[key] = value;
34
+ let query = request.query,
35
+ params = new URLSearchParams(path[1]);
36
+
37
+ for (let [key, value] of params.entries()) {
38
+ query[key] = value;
35
39
  }
36
40
  }
37
41
 
@@ -40,12 +44,14 @@ function href<T>() {
40
44
 
41
45
  function match<T>(request: Request<T>, router: Router<T>, subdomain?: string) {
42
46
  if (router.subdomains !== null) {
43
- for (let i = 0, n = router.subdomains.length; i < n; i++) {
44
- if (!request.hostname.startsWith(router.subdomains[i])) {
47
+ let subdomains = router.subdomains;
48
+
49
+ for (let i = 0, n = subdomains.length; i < n; i++) {
50
+ if (!request.hostname.startsWith(subdomains[i])) {
45
51
  continue;
46
52
  }
47
53
 
48
- subdomain = router.subdomains[i];
54
+ subdomain = subdomains[i];
49
55
  break;
50
56
  }
51
57
  }
@@ -5,12 +5,14 @@ import pipeline from '@esportsplus/pipeline';
5
5
 
6
6
 
7
7
  function normalize(path: string) {
8
- if (path[0] !== '/') {
9
- path = '/' + path;
10
- }
8
+ if (path) {
9
+ if (path[0] !== '/') {
10
+ path = '/' + path;
11
+ }
11
12
 
12
- if (path.at(-1) === '/') {
13
- path = path.slice(0, -1);
13
+ if (path.at(-1) === '/') {
14
+ path = path.slice(0, -1);
15
+ }
14
16
  }
15
17
 
16
18
  return path || '/';
@@ -55,7 +57,7 @@ class Router<T> {
55
57
 
56
58
  private add(radixkey: string, route: Route<T>) {
57
59
  if (radixkey.indexOf(':') === -1 || this.root.add(radixkey, route).type === STATIC) {
58
- if (this.static[radixkey]) {
60
+ if (radixkey in this.static) {
59
61
  throw new Error(`Routing: static path '${radixkey}' is already in use`);
60
62
  }
61
63
 
@@ -152,11 +154,13 @@ class Router<T> {
152
154
  }
153
155
 
154
156
  if (route.subdomain) {
157
+ let subdomain = route.subdomain.toLowerCase();
158
+
155
159
  if (!this.subdomains) {
156
- this.subdomains = [route.subdomain];
160
+ this.subdomains = [subdomain];
157
161
  }
158
162
  else {
159
- this.subdomains.push(route.subdomain);
163
+ this.subdomains.push(subdomain);
160
164
  }
161
165
  }
162
166