@carno.js/core 1.1.1 → 1.2.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 (124) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +188 -188
  3. package/dist/Carno.js +45 -26
  4. package/dist/Carno.mjs +45 -26
  5. package/dist/bun/index.js +4 -4
  6. package/dist/bun/index.js.map +30 -29
  7. package/dist/compression/CompressionMiddleware.js +110 -0
  8. package/dist/compression/CompressionMiddleware.mjs +90 -0
  9. package/dist/index.js +3 -1
  10. package/dist/index.mjs +2 -0
  11. package/package.json +2 -2
  12. package/src/Carno.ts +728 -673
  13. package/src/DefaultRoutes.ts +34 -34
  14. package/src/cache/CacheDriver.ts +50 -50
  15. package/src/cache/CacheService.ts +139 -139
  16. package/src/cache/MemoryDriver.ts +104 -104
  17. package/src/cache/RedisDriver.ts +116 -116
  18. package/src/compiler/JITCompiler.ts +167 -167
  19. package/src/compression/CompressionMiddleware.ts +221 -0
  20. package/src/container/Container.ts +168 -168
  21. package/src/context/Context.ts +130 -130
  22. package/src/cors/CorsHandler.ts +145 -145
  23. package/src/decorators/Controller.ts +63 -63
  24. package/src/decorators/Inject.ts +16 -16
  25. package/src/decorators/Middleware.ts +22 -22
  26. package/src/decorators/Service.ts +18 -18
  27. package/src/decorators/methods.ts +58 -58
  28. package/src/decorators/params.ts +47 -47
  29. package/src/events/Lifecycle.ts +97 -97
  30. package/src/exceptions/HttpException.ts +99 -99
  31. package/src/index.ts +99 -95
  32. package/src/metadata.ts +46 -46
  33. package/src/middleware/CarnoMiddleware.ts +20 -14
  34. package/src/router/RadixRouter.ts +225 -225
  35. package/src/testing/TestHarness.ts +185 -185
  36. package/src/utils/Metadata.ts +43 -43
  37. package/src/utils/parseQuery.ts +161 -161
  38. package/src/validation/ValibotAdapter.ts +95 -95
  39. package/src/validation/ValidatorAdapter.ts +69 -69
  40. package/src/validation/ZodAdapter.ts +102 -102
  41. package/dist/Carno.d.js +0 -14
  42. package/dist/Carno.d.mjs +0 -1
  43. package/dist/DefaultRoutes.d.js +0 -13
  44. package/dist/DefaultRoutes.d.mjs +0 -0
  45. package/dist/cache/CacheDriver.d.js +0 -13
  46. package/dist/cache/CacheDriver.d.mjs +0 -0
  47. package/dist/cache/CacheService.d.js +0 -13
  48. package/dist/cache/CacheService.d.mjs +0 -0
  49. package/dist/cache/MemoryDriver.d.js +0 -13
  50. package/dist/cache/MemoryDriver.d.mjs +0 -0
  51. package/dist/cache/RedisDriver.d.js +0 -13
  52. package/dist/cache/RedisDriver.d.mjs +0 -0
  53. package/dist/compiler/JITCompiler.d.js +0 -13
  54. package/dist/compiler/JITCompiler.d.mjs +0 -0
  55. package/dist/container/Container.d.js +0 -13
  56. package/dist/container/Container.d.mjs +0 -0
  57. package/dist/context/Context.d.js +0 -13
  58. package/dist/context/Context.d.mjs +0 -0
  59. package/dist/cors/CorsHandler.d.js +0 -13
  60. package/dist/cors/CorsHandler.d.mjs +0 -0
  61. package/dist/decorators/Controller.d.js +0 -13
  62. package/dist/decorators/Controller.d.mjs +0 -0
  63. package/dist/decorators/Inject.d.js +0 -13
  64. package/dist/decorators/Inject.d.mjs +0 -0
  65. package/dist/decorators/Middleware.d.js +0 -13
  66. package/dist/decorators/Middleware.d.mjs +0 -0
  67. package/dist/decorators/Service.d.js +0 -13
  68. package/dist/decorators/Service.d.mjs +0 -0
  69. package/dist/decorators/methods.d.js +0 -13
  70. package/dist/decorators/methods.d.mjs +0 -0
  71. package/dist/decorators/params.d.js +0 -13
  72. package/dist/decorators/params.d.mjs +0 -0
  73. package/dist/events/Lifecycle.d.js +0 -13
  74. package/dist/events/Lifecycle.d.mjs +0 -0
  75. package/dist/exceptions/HttpException.d.js +0 -13
  76. package/dist/exceptions/HttpException.d.mjs +0 -0
  77. package/dist/index.d.js +0 -130
  78. package/dist/index.d.mjs +0 -78
  79. package/dist/metadata.d.js +0 -13
  80. package/dist/metadata.d.mjs +0 -0
  81. package/dist/middleware/CarnoMiddleware.d.js +0 -13
  82. package/dist/middleware/CarnoMiddleware.d.mjs +0 -0
  83. package/dist/router/RadixRouter.d.js +0 -13
  84. package/dist/router/RadixRouter.d.mjs +0 -0
  85. package/dist/testing/TestHarness.d.js +0 -13
  86. package/dist/testing/TestHarness.d.mjs +0 -0
  87. package/dist/utils/Metadata.d.js +0 -13
  88. package/dist/utils/Metadata.d.mjs +0 -0
  89. package/dist/utils/parseQuery.d.js +0 -13
  90. package/dist/utils/parseQuery.d.mjs +0 -0
  91. package/dist/validation/ValibotAdapter.d.js +0 -13
  92. package/dist/validation/ValibotAdapter.d.mjs +0 -0
  93. package/dist/validation/ValidatorAdapter.d.js +0 -13
  94. package/dist/validation/ValidatorAdapter.d.mjs +0 -0
  95. package/dist/validation/ZodAdapter.d.js +0 -13
  96. package/dist/validation/ZodAdapter.d.mjs +0 -0
  97. package/src/Carno.d.ts +0 -135
  98. package/src/DefaultRoutes.d.ts +0 -19
  99. package/src/cache/CacheDriver.d.ts +0 -43
  100. package/src/cache/CacheService.d.ts +0 -89
  101. package/src/cache/MemoryDriver.d.ts +0 -32
  102. package/src/cache/RedisDriver.d.ts +0 -34
  103. package/src/compiler/JITCompiler.d.ts +0 -36
  104. package/src/container/Container.d.ts +0 -38
  105. package/src/context/Context.d.ts +0 -36
  106. package/src/cors/CorsHandler.d.ts +0 -47
  107. package/src/decorators/Controller.d.ts +0 -13
  108. package/src/decorators/Inject.d.ts +0 -6
  109. package/src/decorators/Middleware.d.ts +0 -5
  110. package/src/decorators/Service.d.ts +0 -9
  111. package/src/decorators/methods.d.ts +0 -7
  112. package/src/decorators/params.d.ts +0 -13
  113. package/src/events/Lifecycle.d.ts +0 -54
  114. package/src/exceptions/HttpException.d.ts +0 -43
  115. package/src/index.d.ts +0 -42
  116. package/src/metadata.d.ts +0 -41
  117. package/src/middleware/CarnoMiddleware.d.ts +0 -12
  118. package/src/router/RadixRouter.d.ts +0 -19
  119. package/src/testing/TestHarness.d.ts +0 -71
  120. package/src/utils/Metadata.d.ts +0 -20
  121. package/src/utils/parseQuery.d.ts +0 -23
  122. package/src/validation/ValibotAdapter.d.ts +0 -30
  123. package/src/validation/ValidatorAdapter.d.ts +0 -54
  124. 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
+ }