@kapeta/local-cluster-service 0.5.10 → 0.5.11

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.5.11](https://github.com/kapetacom/local-cluster-service/compare/v0.5.10...v0.5.11) (2023-06-20)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Improve path template parser. ([#36](https://github.com/kapetacom/local-cluster-service/issues/36)) ([b655da1](https://github.com/kapetacom/local-cluster-service/commit/b655da1e97740ff2b4fe0f2bcd7f75b04003753e))
7
+
1
8
  ## [0.5.10](https://github.com/kapetacom/local-cluster-service/compare/v0.5.9...v0.5.10) (2023-06-19)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapeta/local-cluster-service",
3
- "version": "0.5.10",
3
+ "version": "0.5.11",
4
4
  "description": "Manages configuration, ports and service discovery for locally running Kapeta systems",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -2,6 +2,17 @@
2
2
  const TYPE_VARIABLE = 'variable';
3
3
  const TYPE_PATH = 'path';
4
4
 
5
+ /**
6
+ * A path template is a string that can be used to match a path and extract variables from it.
7
+ *
8
+ * E.g. /foo/{bar}/baz
9
+ *
10
+ * Would match /foo/123/baz and extract bar=123
11
+ *
12
+ * You can also specify a regex for the variable:
13
+ * /foo/{bar:[0-9]+}/baz
14
+ *
15
+ */
5
16
  class PathTemplate {
6
17
  constructor(pathTemplate) {
7
18
  if (!pathTemplate.startsWith('/')) {
@@ -9,33 +20,41 @@ class PathTemplate {
9
20
  }
10
21
  this._path = pathTemplate;
11
22
 
12
- this._parts = pathTemplate.split(/{/g).map((part) => {
13
- if (part.endsWith('}')) {
14
- let regex,
15
- value = part.substr(0, part.length - 1);
16
-
17
- [value, regex] = value.split(/:/, 2);
23
+ const variableRegex = /{([^}]+)}/g;
24
+ let match, offset = 0;
25
+ this._parts = [];
26
+ while((match = variableRegex.exec(pathTemplate)) !== null) {
27
+ if (match.index > offset) {
28
+ this._parts.push({
29
+ type: TYPE_PATH,
30
+ value: pathTemplate.substring(offset, match.index)
31
+ });
32
+ }
18
33
 
19
- if (regex) {
20
- regex = new RegExp('^' + regex);
21
- } else {
22
- regex = /^[^\/]+/
23
- }
34
+ let regex;
35
+ let value = match[1];
36
+ [value, regex] = value.split(/:/, 2);
24
37
 
25
- return {
26
- type: TYPE_VARIABLE,
27
- value,
28
- regex
29
- };
38
+ if (regex) {
39
+ regex = new RegExp('^' + regex);
40
+ } else {
41
+ regex = /^[^\/]+/
30
42
  }
31
43
 
32
- return {
33
- type: TYPE_PATH,
34
- value: part
35
- };
36
- });
37
-
44
+ this._parts.push({
45
+ type: TYPE_VARIABLE,
46
+ value,
47
+ regex
48
+ });
49
+ offset = match.index + match[0].length;
50
+ }
38
51
 
52
+ if (offset < pathTemplate.length) {
53
+ this._parts.push({
54
+ type: TYPE_PATH,
55
+ value: pathTemplate.substring(offset)
56
+ });
57
+ }
39
58
  }
40
59
 
41
60
  get path() {
@@ -61,7 +80,7 @@ class PathTemplate {
61
80
  return null;
62
81
  }
63
82
 
64
- path = path.substr(part.value.length);
83
+ path = path.substring(part.value.length);
65
84
  break;
66
85
  case TYPE_VARIABLE:
67
86
  if (!part.regex.test(path)) {