@carno.js/core 1.1.1 → 1.1.2

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 (119) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +188 -188
  3. package/dist/Carno.js +46 -26
  4. package/dist/Carno.mjs +46 -26
  5. package/dist/bun/index.js +4 -4
  6. package/dist/bun/index.js.map +29 -29
  7. package/package.json +2 -2
  8. package/src/Carno.ts +718 -673
  9. package/src/DefaultRoutes.ts +34 -34
  10. package/src/cache/CacheDriver.ts +50 -50
  11. package/src/cache/CacheService.ts +139 -139
  12. package/src/cache/MemoryDriver.ts +104 -104
  13. package/src/cache/RedisDriver.ts +116 -116
  14. package/src/compiler/JITCompiler.ts +167 -167
  15. package/src/container/Container.ts +168 -168
  16. package/src/context/Context.ts +130 -130
  17. package/src/cors/CorsHandler.ts +145 -145
  18. package/src/decorators/Controller.ts +63 -63
  19. package/src/decorators/Inject.ts +16 -16
  20. package/src/decorators/Middleware.ts +22 -22
  21. package/src/decorators/Service.ts +18 -18
  22. package/src/decorators/methods.ts +58 -58
  23. package/src/decorators/params.ts +47 -47
  24. package/src/events/Lifecycle.ts +97 -97
  25. package/src/exceptions/HttpException.ts +99 -99
  26. package/src/index.ts +95 -95
  27. package/src/metadata.ts +46 -46
  28. package/src/middleware/CarnoMiddleware.ts +14 -14
  29. package/src/router/RadixRouter.ts +225 -225
  30. package/src/testing/TestHarness.ts +185 -185
  31. package/src/utils/Metadata.ts +43 -43
  32. package/src/utils/parseQuery.ts +161 -161
  33. package/src/validation/ValibotAdapter.ts +95 -95
  34. package/src/validation/ValidatorAdapter.ts +69 -69
  35. package/src/validation/ZodAdapter.ts +102 -102
  36. package/dist/Carno.d.js +0 -14
  37. package/dist/Carno.d.mjs +0 -1
  38. package/dist/DefaultRoutes.d.js +0 -13
  39. package/dist/DefaultRoutes.d.mjs +0 -0
  40. package/dist/cache/CacheDriver.d.js +0 -13
  41. package/dist/cache/CacheDriver.d.mjs +0 -0
  42. package/dist/cache/CacheService.d.js +0 -13
  43. package/dist/cache/CacheService.d.mjs +0 -0
  44. package/dist/cache/MemoryDriver.d.js +0 -13
  45. package/dist/cache/MemoryDriver.d.mjs +0 -0
  46. package/dist/cache/RedisDriver.d.js +0 -13
  47. package/dist/cache/RedisDriver.d.mjs +0 -0
  48. package/dist/compiler/JITCompiler.d.js +0 -13
  49. package/dist/compiler/JITCompiler.d.mjs +0 -0
  50. package/dist/container/Container.d.js +0 -13
  51. package/dist/container/Container.d.mjs +0 -0
  52. package/dist/context/Context.d.js +0 -13
  53. package/dist/context/Context.d.mjs +0 -0
  54. package/dist/cors/CorsHandler.d.js +0 -13
  55. package/dist/cors/CorsHandler.d.mjs +0 -0
  56. package/dist/decorators/Controller.d.js +0 -13
  57. package/dist/decorators/Controller.d.mjs +0 -0
  58. package/dist/decorators/Inject.d.js +0 -13
  59. package/dist/decorators/Inject.d.mjs +0 -0
  60. package/dist/decorators/Middleware.d.js +0 -13
  61. package/dist/decorators/Middleware.d.mjs +0 -0
  62. package/dist/decorators/Service.d.js +0 -13
  63. package/dist/decorators/Service.d.mjs +0 -0
  64. package/dist/decorators/methods.d.js +0 -13
  65. package/dist/decorators/methods.d.mjs +0 -0
  66. package/dist/decorators/params.d.js +0 -13
  67. package/dist/decorators/params.d.mjs +0 -0
  68. package/dist/events/Lifecycle.d.js +0 -13
  69. package/dist/events/Lifecycle.d.mjs +0 -0
  70. package/dist/exceptions/HttpException.d.js +0 -13
  71. package/dist/exceptions/HttpException.d.mjs +0 -0
  72. package/dist/index.d.js +0 -130
  73. package/dist/index.d.mjs +0 -78
  74. package/dist/metadata.d.js +0 -13
  75. package/dist/metadata.d.mjs +0 -0
  76. package/dist/middleware/CarnoMiddleware.d.js +0 -13
  77. package/dist/middleware/CarnoMiddleware.d.mjs +0 -0
  78. package/dist/router/RadixRouter.d.js +0 -13
  79. package/dist/router/RadixRouter.d.mjs +0 -0
  80. package/dist/testing/TestHarness.d.js +0 -13
  81. package/dist/testing/TestHarness.d.mjs +0 -0
  82. package/dist/utils/Metadata.d.js +0 -13
  83. package/dist/utils/Metadata.d.mjs +0 -0
  84. package/dist/utils/parseQuery.d.js +0 -13
  85. package/dist/utils/parseQuery.d.mjs +0 -0
  86. package/dist/validation/ValibotAdapter.d.js +0 -13
  87. package/dist/validation/ValibotAdapter.d.mjs +0 -0
  88. package/dist/validation/ValidatorAdapter.d.js +0 -13
  89. package/dist/validation/ValidatorAdapter.d.mjs +0 -0
  90. package/dist/validation/ZodAdapter.d.js +0 -13
  91. package/dist/validation/ZodAdapter.d.mjs +0 -0
  92. package/src/Carno.d.ts +0 -135
  93. package/src/DefaultRoutes.d.ts +0 -19
  94. package/src/cache/CacheDriver.d.ts +0 -43
  95. package/src/cache/CacheService.d.ts +0 -89
  96. package/src/cache/MemoryDriver.d.ts +0 -32
  97. package/src/cache/RedisDriver.d.ts +0 -34
  98. package/src/compiler/JITCompiler.d.ts +0 -36
  99. package/src/container/Container.d.ts +0 -38
  100. package/src/context/Context.d.ts +0 -36
  101. package/src/cors/CorsHandler.d.ts +0 -47
  102. package/src/decorators/Controller.d.ts +0 -13
  103. package/src/decorators/Inject.d.ts +0 -6
  104. package/src/decorators/Middleware.d.ts +0 -5
  105. package/src/decorators/Service.d.ts +0 -9
  106. package/src/decorators/methods.d.ts +0 -7
  107. package/src/decorators/params.d.ts +0 -13
  108. package/src/events/Lifecycle.d.ts +0 -54
  109. package/src/exceptions/HttpException.d.ts +0 -43
  110. package/src/index.d.ts +0 -42
  111. package/src/metadata.d.ts +0 -41
  112. package/src/middleware/CarnoMiddleware.d.ts +0 -12
  113. package/src/router/RadixRouter.d.ts +0 -19
  114. package/src/testing/TestHarness.d.ts +0 -71
  115. package/src/utils/Metadata.d.ts +0 -20
  116. package/src/utils/parseQuery.d.ts +0 -23
  117. package/src/validation/ValibotAdapter.d.ts +0 -30
  118. package/src/validation/ValidatorAdapter.d.ts +0 -54
  119. package/src/validation/ZodAdapter.d.ts +0 -35
@@ -1,225 +1,225 @@
1
- /**
2
- * Ultra-fast Radix Router for Turbo.
3
- *
4
- * Optimizations:
5
- * - Char code comparisons (no string allocations)
6
- * - Frozen empty params object
7
- * - O(1) method lookup via char code index
8
- * - Minimal allocations in hot path
9
- */
10
-
11
- const EMPTY_PARAMS: Readonly<Record<string, string>> = Object.freeze({});
12
-
13
- interface Node<T> {
14
- part: string;
15
- store: T | null;
16
- children: Map<number, Node<T>> | null;
17
- paramChild: ParamNode<T> | null;
18
- wildcardStore: T | null;
19
- }
20
-
21
- interface ParamNode<T> {
22
- name: string;
23
- store: T | null;
24
- child: Node<T> | null;
25
- }
26
-
27
- export interface RouteMatch<T> {
28
- store: T;
29
- params: Record<string, string>;
30
- }
31
-
32
- function createNode<T>(part: string): Node<T> {
33
- return {
34
- part,
35
- store: null,
36
- children: null,
37
- paramChild: null,
38
- wildcardStore: null
39
- };
40
- }
41
-
42
- export class RadixRouter<T> {
43
- private roots: Record<string, Node<T>> = {};
44
-
45
- add(method: string, path: string, store: T): void {
46
- if (path === '') path = '/';
47
- if (path[0] !== '/') path = '/' + path;
48
-
49
- const isWildcard = path.endsWith('*');
50
-
51
- if (isWildcard) path = path.slice(0, -1);
52
-
53
- let node = this.roots[method];
54
-
55
- if (!node) {
56
- node = this.roots[method] = createNode('/');
57
- }
58
-
59
- let i = 0;
60
- const len = path.length;
61
-
62
- while (i < len) {
63
- const char = path.charCodeAt(i);
64
-
65
- if (char === 58) {
66
- const paramStart = i + 1;
67
- let paramEnd = paramStart;
68
-
69
- while (paramEnd < len && path.charCodeAt(paramEnd) !== 47) {
70
- paramEnd++;
71
- }
72
-
73
- const paramName = path.slice(paramStart, paramEnd);
74
-
75
- if (!node.paramChild) {
76
- node.paramChild = { name: paramName, store: null, child: null };
77
- }
78
-
79
- if (paramEnd >= len) {
80
- node.paramChild.store = store;
81
- return;
82
- }
83
-
84
- if (!node.paramChild.child) {
85
- node.paramChild.child = createNode(path.slice(paramEnd));
86
- }
87
-
88
- node = node.paramChild.child;
89
- i = paramEnd;
90
- continue;
91
- }
92
-
93
- let segmentEnd = i;
94
-
95
- while (segmentEnd < len && path.charCodeAt(segmentEnd) !== 47 && path.charCodeAt(segmentEnd) !== 58) {
96
- segmentEnd++;
97
- }
98
-
99
- if (segmentEnd < len && path.charCodeAt(segmentEnd) === 47) {
100
- segmentEnd++;
101
- }
102
-
103
- const segment = path.slice(i, segmentEnd);
104
-
105
- if (segment === node.part) {
106
- i = segmentEnd;
107
- continue;
108
- }
109
-
110
- if (!node.children) {
111
- node.children = new Map();
112
- }
113
-
114
- const firstChar = segment.charCodeAt(0);
115
- let child = node.children.get(firstChar);
116
-
117
- if (!child) {
118
- child = createNode(segment);
119
- node.children.set(firstChar, child);
120
- }
121
-
122
- node = child;
123
- i = segmentEnd;
124
- }
125
-
126
- if (isWildcard) {
127
- node.wildcardStore = store;
128
- } else {
129
- node.store = store;
130
- }
131
- }
132
-
133
- find(method: string, path: string): RouteMatch<T> | null {
134
- const root = this.roots[method];
135
-
136
- if (!root) return null;
137
-
138
- return this.matchPath(root, path, 0, path.length);
139
- }
140
-
141
- private matchPath(
142
- node: Node<T>,
143
- path: string,
144
- start: number,
145
- len: number
146
- ): RouteMatch<T> | null {
147
- const partLen = node.part.length;
148
- const end = start + partLen;
149
-
150
- if (partLen > 1) {
151
- if (end > len) return null;
152
-
153
- for (let i = 1, j = start + 1; i < partLen; i++, j++) {
154
- if (node.part.charCodeAt(i) !== path.charCodeAt(j)) {
155
- return null;
156
- }
157
- }
158
- }
159
-
160
- if (end === len) {
161
- if (node.store !== null) {
162
- return { store: node.store, params: EMPTY_PARAMS };
163
- }
164
-
165
- if (node.wildcardStore !== null) {
166
- return { store: node.wildcardStore, params: { '*': '' } };
167
- }
168
-
169
- return null;
170
- }
171
-
172
- if (node.children) {
173
- const child = node.children.get(path.charCodeAt(end));
174
-
175
- if (child) {
176
- const result = this.matchPath(child, path, end, len);
177
-
178
- if (result) return result;
179
- }
180
- }
181
-
182
- if (node.paramChild) {
183
- const param = node.paramChild;
184
- let paramEnd = end;
185
-
186
- while (paramEnd < len && path.charCodeAt(paramEnd) !== 47) {
187
- paramEnd++;
188
- }
189
-
190
- if (paramEnd === end) return null;
191
-
192
- const paramValue = path.slice(end, paramEnd);
193
-
194
- if (paramEnd >= len) {
195
- if (param.store !== null) {
196
- return {
197
- store: param.store,
198
- params: { [param.name]: paramValue }
199
- };
200
- }
201
- } else if (param.child) {
202
- const result = this.matchPath(param.child, path, paramEnd, len);
203
-
204
- if (result) {
205
- if (result.params === EMPTY_PARAMS) {
206
- result.params = { [param.name]: paramValue };
207
- } else {
208
- result.params[param.name] = paramValue;
209
- }
210
-
211
- return result;
212
- }
213
- }
214
- }
215
-
216
- if (node.wildcardStore !== null) {
217
- return {
218
- store: node.wildcardStore,
219
- params: { '*': path.slice(end) }
220
- };
221
- }
222
-
223
- return null;
224
- }
225
- }
1
+ /**
2
+ * Ultra-fast Radix Router for Turbo.
3
+ *
4
+ * Optimizations:
5
+ * - Char code comparisons (no string allocations)
6
+ * - Frozen empty params object
7
+ * - O(1) method lookup via char code index
8
+ * - Minimal allocations in hot path
9
+ */
10
+
11
+ const EMPTY_PARAMS: Readonly<Record<string, string>> = Object.freeze({});
12
+
13
+ interface Node<T> {
14
+ part: string;
15
+ store: T | null;
16
+ children: Map<number, Node<T>> | null;
17
+ paramChild: ParamNode<T> | null;
18
+ wildcardStore: T | null;
19
+ }
20
+
21
+ interface ParamNode<T> {
22
+ name: string;
23
+ store: T | null;
24
+ child: Node<T> | null;
25
+ }
26
+
27
+ export interface RouteMatch<T> {
28
+ store: T;
29
+ params: Record<string, string>;
30
+ }
31
+
32
+ function createNode<T>(part: string): Node<T> {
33
+ return {
34
+ part,
35
+ store: null,
36
+ children: null,
37
+ paramChild: null,
38
+ wildcardStore: null
39
+ };
40
+ }
41
+
42
+ export class RadixRouter<T> {
43
+ private roots: Record<string, Node<T>> = {};
44
+
45
+ add(method: string, path: string, store: T): void {
46
+ if (path === '') path = '/';
47
+ if (path[0] !== '/') path = '/' + path;
48
+
49
+ const isWildcard = path.endsWith('*');
50
+
51
+ if (isWildcard) path = path.slice(0, -1);
52
+
53
+ let node = this.roots[method];
54
+
55
+ if (!node) {
56
+ node = this.roots[method] = createNode('/');
57
+ }
58
+
59
+ let i = 0;
60
+ const len = path.length;
61
+
62
+ while (i < len) {
63
+ const char = path.charCodeAt(i);
64
+
65
+ if (char === 58) {
66
+ const paramStart = i + 1;
67
+ let paramEnd = paramStart;
68
+
69
+ while (paramEnd < len && path.charCodeAt(paramEnd) !== 47) {
70
+ paramEnd++;
71
+ }
72
+
73
+ const paramName = path.slice(paramStart, paramEnd);
74
+
75
+ if (!node.paramChild) {
76
+ node.paramChild = { name: paramName, store: null, child: null };
77
+ }
78
+
79
+ if (paramEnd >= len) {
80
+ node.paramChild.store = store;
81
+ return;
82
+ }
83
+
84
+ if (!node.paramChild.child) {
85
+ node.paramChild.child = createNode(path.slice(paramEnd));
86
+ }
87
+
88
+ node = node.paramChild.child;
89
+ i = paramEnd;
90
+ continue;
91
+ }
92
+
93
+ let segmentEnd = i;
94
+
95
+ while (segmentEnd < len && path.charCodeAt(segmentEnd) !== 47 && path.charCodeAt(segmentEnd) !== 58) {
96
+ segmentEnd++;
97
+ }
98
+
99
+ if (segmentEnd < len && path.charCodeAt(segmentEnd) === 47) {
100
+ segmentEnd++;
101
+ }
102
+
103
+ const segment = path.slice(i, segmentEnd);
104
+
105
+ if (segment === node.part) {
106
+ i = segmentEnd;
107
+ continue;
108
+ }
109
+
110
+ if (!node.children) {
111
+ node.children = new Map();
112
+ }
113
+
114
+ const firstChar = segment.charCodeAt(0);
115
+ let child = node.children.get(firstChar);
116
+
117
+ if (!child) {
118
+ child = createNode(segment);
119
+ node.children.set(firstChar, child);
120
+ }
121
+
122
+ node = child;
123
+ i = segmentEnd;
124
+ }
125
+
126
+ if (isWildcard) {
127
+ node.wildcardStore = store;
128
+ } else {
129
+ node.store = store;
130
+ }
131
+ }
132
+
133
+ find(method: string, path: string): RouteMatch<T> | null {
134
+ const root = this.roots[method];
135
+
136
+ if (!root) return null;
137
+
138
+ return this.matchPath(root, path, 0, path.length);
139
+ }
140
+
141
+ private matchPath(
142
+ node: Node<T>,
143
+ path: string,
144
+ start: number,
145
+ len: number
146
+ ): RouteMatch<T> | null {
147
+ const partLen = node.part.length;
148
+ const end = start + partLen;
149
+
150
+ if (partLen > 1) {
151
+ if (end > len) return null;
152
+
153
+ for (let i = 1, j = start + 1; i < partLen; i++, j++) {
154
+ if (node.part.charCodeAt(i) !== path.charCodeAt(j)) {
155
+ return null;
156
+ }
157
+ }
158
+ }
159
+
160
+ if (end === len) {
161
+ if (node.store !== null) {
162
+ return { store: node.store, params: EMPTY_PARAMS };
163
+ }
164
+
165
+ if (node.wildcardStore !== null) {
166
+ return { store: node.wildcardStore, params: { '*': '' } };
167
+ }
168
+
169
+ return null;
170
+ }
171
+
172
+ if (node.children) {
173
+ const child = node.children.get(path.charCodeAt(end));
174
+
175
+ if (child) {
176
+ const result = this.matchPath(child, path, end, len);
177
+
178
+ if (result) return result;
179
+ }
180
+ }
181
+
182
+ if (node.paramChild) {
183
+ const param = node.paramChild;
184
+ let paramEnd = end;
185
+
186
+ while (paramEnd < len && path.charCodeAt(paramEnd) !== 47) {
187
+ paramEnd++;
188
+ }
189
+
190
+ if (paramEnd === end) return null;
191
+
192
+ const paramValue = path.slice(end, paramEnd);
193
+
194
+ if (paramEnd >= len) {
195
+ if (param.store !== null) {
196
+ return {
197
+ store: param.store,
198
+ params: { [param.name]: paramValue }
199
+ };
200
+ }
201
+ } else if (param.child) {
202
+ const result = this.matchPath(param.child, path, paramEnd, len);
203
+
204
+ if (result) {
205
+ if (result.params === EMPTY_PARAMS) {
206
+ result.params = { [param.name]: paramValue };
207
+ } else {
208
+ result.params[param.name] = paramValue;
209
+ }
210
+
211
+ return result;
212
+ }
213
+ }
214
+ }
215
+
216
+ if (node.wildcardStore !== null) {
217
+ return {
218
+ store: node.wildcardStore,
219
+ params: { '*': path.slice(end) }
220
+ };
221
+ }
222
+
223
+ return null;
224
+ }
225
+ }