@gugananuvem/aws-local-simulator 1.0.15 → 1.0.16
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/README.md +789 -594
- package/bin/aws-local-simulator.js +63 -63
- package/package.json +2 -2
- package/src/config/config-loader.js +114 -114
- package/src/config/default-config.js +68 -68
- package/src/config/env-loader.js +68 -68
- package/src/index.js +146 -146
- package/src/index.mjs +123 -123
- package/src/server.js +227 -227
- package/src/services/apigateway/index.js +75 -73
- package/src/services/apigateway/server.js +570 -507
- package/src/services/apigateway/simulator.js +1261 -1261
- package/src/services/athena/index.js +75 -75
- package/src/services/athena/server.js +101 -101
- package/src/services/athena/simulador.js +998 -998
- package/src/services/athena/simulator.js +346 -346
- package/src/services/cloudformation/index.js +106 -106
- package/src/services/cloudformation/server.js +417 -417
- package/src/services/cloudformation/simulador.js +1045 -1045
- package/src/services/cloudtrail/index.js +84 -84
- package/src/services/cloudtrail/server.js +235 -235
- package/src/services/cloudtrail/simulador.js +719 -719
- package/src/services/cloudwatch/index.js +84 -84
- package/src/services/cloudwatch/server.js +366 -366
- package/src/services/cloudwatch/simulador.js +1173 -1173
- package/src/services/cognito/index.js +79 -79
- package/src/services/cognito/server.js +301 -301
- package/src/services/cognito/simulator.js +1655 -1655
- package/src/services/config/index.js +96 -96
- package/src/services/config/server.js +215 -215
- package/src/services/config/simulador.js +1260 -1260
- package/src/services/dynamodb/index.js +74 -74
- package/src/services/dynamodb/server.js +125 -125
- package/src/services/dynamodb/simulator.js +630 -630
- package/src/services/ecs/index.js +65 -65
- package/src/services/ecs/server.js +235 -235
- package/src/services/ecs/simulator.js +844 -844
- package/src/services/eventbridge/index.js +89 -89
- package/src/services/eventbridge/server.js +209 -209
- package/src/services/eventbridge/simulator.js +684 -684
- package/src/services/index.js +45 -45
- package/src/services/kms/index.js +75 -75
- package/src/services/kms/server.js +67 -67
- package/src/services/kms/simulator.js +324 -324
- package/src/services/lambda/handler-loader.js +183 -183
- package/src/services/lambda/index.js +78 -78
- package/src/services/lambda/route-registry.js +274 -274
- package/src/services/lambda/server.js +145 -145
- package/src/services/lambda/simulator.js +199 -199
- package/src/services/parameter-store/index.js +80 -80
- package/src/services/parameter-store/server.js +50 -50
- package/src/services/parameter-store/simulator.js +201 -201
- package/src/services/s3/index.js +73 -73
- package/src/services/s3/server.js +329 -329
- package/src/services/s3/simulator.js +565 -565
- package/src/services/secret-manager/index.js +80 -80
- package/src/services/secret-manager/server.js +50 -50
- package/src/services/secret-manager/simulator.js +171 -171
- package/src/services/sns/index.js +89 -89
- package/src/services/sns/server.js +580 -580
- package/src/services/sns/simulator.js +1482 -1482
- package/src/services/sqs/index.js +98 -93
- package/src/services/sqs/server.js +349 -349
- package/src/services/sqs/simulator.js +441 -441
- package/src/services/sts/index.js +37 -37
- package/src/services/sts/server.js +144 -144
- package/src/services/sts/simulator.js +69 -69
- package/src/services/xray/index.js +83 -83
- package/src/services/xray/server.js +308 -308
- package/src/services/xray/simulador.js +994 -994
- package/src/template/aws-config-template.js +87 -87
- package/src/template/aws-config-template.mjs +90 -90
- package/src/template/config-template.json +203 -203
- package/src/utils/aws-config.js +91 -91
- package/src/utils/cloudtrail-audit.js +129 -129
- package/src/utils/local-store.js +83 -83
- package/src/utils/logger.js +59 -59
|
@@ -1,275 +1,275 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Route Registry - Gerencia o registro e matching de rotas para Lambdas
|
|
3
|
-
* Suporta: Path parameters, Wildcards, Middlewares
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
class RouteRegistry {
|
|
7
|
-
constructor() {
|
|
8
|
-
this.routes = new Map();
|
|
9
|
-
this.middlewares = new Map();
|
|
10
|
-
this.globalMiddlewares = [];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Registra uma rota
|
|
15
|
-
* @param {string} path - Caminho da rota (ex: /users/:id, /api/*)
|
|
16
|
-
* @param {Function} handler - Função handler da Lambda
|
|
17
|
-
* @param {Object} env - Variáveis de ambiente específicas
|
|
18
|
-
* @param {Array} middlewares - Middlewares específicos da rota
|
|
19
|
-
*/
|
|
20
|
-
register(path, handler, env = {}, middlewares = []) {
|
|
21
|
-
// Normaliza o path
|
|
22
|
-
const normalizedPath = this.normalizePath(path);
|
|
23
|
-
|
|
24
|
-
// Parse do path para extrair parâmetros
|
|
25
|
-
const { pattern, params } = this.parsePath(normalizedPath);
|
|
26
|
-
|
|
27
|
-
this.routes.set(normalizedPath, {
|
|
28
|
-
path: normalizedPath,
|
|
29
|
-
pattern,
|
|
30
|
-
params,
|
|
31
|
-
handler,
|
|
32
|
-
env,
|
|
33
|
-
middlewares,
|
|
34
|
-
isWildcard: normalizedPath.includes('*'),
|
|
35
|
-
hasParams: params.length > 0,
|
|
36
|
-
timestamp: Date.now()
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// Adiciona versão com barra se não existir
|
|
40
|
-
if (!normalizedPath.endsWith('/') && normalizedPath !== '*') {
|
|
41
|
-
const slashPath = `${normalizedPath}/`;
|
|
42
|
-
if (!this.routes.has(slashPath)) {
|
|
43
|
-
this.routes.set(slashPath, {
|
|
44
|
-
path: slashPath,
|
|
45
|
-
pattern: this.parsePath(slashPath).pattern,
|
|
46
|
-
params: this.parsePath(slashPath).params,
|
|
47
|
-
handler,
|
|
48
|
-
env,
|
|
49
|
-
middlewares,
|
|
50
|
-
isWildcard: false,
|
|
51
|
-
hasParams: this.parsePath(slashPath).params.length > 0,
|
|
52
|
-
timestamp: Date.now()
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Registra um middleware global
|
|
62
|
-
*/
|
|
63
|
-
use(middleware) {
|
|
64
|
-
this.globalMiddlewares.push(middleware);
|
|
65
|
-
return this;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Registra um middleware para uma rota específica
|
|
70
|
-
*/
|
|
71
|
-
useFor(path, middleware) {
|
|
72
|
-
const normalizedPath = this.normalizePath(path);
|
|
73
|
-
if (!this.middlewares.has(normalizedPath)) {
|
|
74
|
-
this.middlewares.set(normalizedPath, []);
|
|
75
|
-
}
|
|
76
|
-
this.middlewares.get(normalizedPath).push(middleware);
|
|
77
|
-
return this;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Encontra uma rota que corresponde ao path
|
|
82
|
-
* @param {string} path - Path da requisição
|
|
83
|
-
* @returns {Object|null} - Rota encontrada com parâmetros extraídos
|
|
84
|
-
*/
|
|
85
|
-
find(path) {
|
|
86
|
-
const normalizedPath = this.normalizePath(path);
|
|
87
|
-
|
|
88
|
-
// Busca exata primeiro
|
|
89
|
-
if (this.routes.has(normalizedPath)) {
|
|
90
|
-
const route = this.routes.get(normalizedPath);
|
|
91
|
-
return {
|
|
92
|
-
...route,
|
|
93
|
-
params: {}
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Busca por prefixo (para rotas sem parâmetros)
|
|
98
|
-
for (const [routePath, route] of this.routes.entries()) {
|
|
99
|
-
if (!route.hasParams && !route.isWildcard && normalizedPath.startsWith(routePath)) {
|
|
100
|
-
return {
|
|
101
|
-
...route,
|
|
102
|
-
params: {}
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Busca por padrão com parâmetros
|
|
108
|
-
for (const [routePath, route] of this.routes.entries()) {
|
|
109
|
-
if (route.hasParams && route.pattern) {
|
|
110
|
-
const match = route.pattern.exec(normalizedPath);
|
|
111
|
-
if (match) {
|
|
112
|
-
const params = {};
|
|
113
|
-
route.params.forEach((paramName, index) => {
|
|
114
|
-
params[paramName] = match[index + 1];
|
|
115
|
-
});
|
|
116
|
-
return {
|
|
117
|
-
...route,
|
|
118
|
-
params
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Busca wildcard
|
|
125
|
-
for (const [routePath, route] of this.routes.entries()) {
|
|
126
|
-
if (route.isWildcard) {
|
|
127
|
-
const wildcardPath = routePath.replace('*', '');
|
|
128
|
-
if (normalizedPath.startsWith(wildcardPath)) {
|
|
129
|
-
return {
|
|
130
|
-
...route,
|
|
131
|
-
params: {
|
|
132
|
-
wildcard: normalizedPath.substring(wildcardPath.length)
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Obtém todos os middlewares para uma rota
|
|
144
|
-
*/
|
|
145
|
-
getMiddlewares(route) {
|
|
146
|
-
const routeMiddlewares = this.middlewares.get(route.path) || [];
|
|
147
|
-
return [...this.globalMiddlewares, ...routeMiddlewares, ...route.middlewares];
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Normaliza o path
|
|
152
|
-
*/
|
|
153
|
-
normalizePath(path) {
|
|
154
|
-
// Remove query string
|
|
155
|
-
const pathWithoutQuery = path.split('?')[0];
|
|
156
|
-
|
|
157
|
-
// Remove trailing slash
|
|
158
|
-
let normalized = pathWithoutQuery.replace(/\/+$/, '');
|
|
159
|
-
|
|
160
|
-
// Adiciona slash inicial se necessário
|
|
161
|
-
if (!normalized.startsWith('/')) {
|
|
162
|
-
normalized = `/${normalized}`;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Se for vazio, usa root
|
|
166
|
-
if (normalized === '') {
|
|
167
|
-
normalized = '/';
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return normalized;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Parse do path para extrair parâmetros e criar regex pattern
|
|
175
|
-
* @param {string} path - Path da rota (ex: /users/:id/posts/:postId)
|
|
176
|
-
* @returns {Object} - { pattern, params }
|
|
177
|
-
*/
|
|
178
|
-
parsePath(path) {
|
|
179
|
-
const params = [];
|
|
180
|
-
|
|
181
|
-
// Substitui :param por regex capture group
|
|
182
|
-
const patternString = path.replace(/:[^\s/]+/g, (match) => {
|
|
183
|
-
const paramName = match.substring(1);
|
|
184
|
-
params.push(paramName);
|
|
185
|
-
return '([^/]+)';
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
// Substitui * por regex wildcard
|
|
189
|
-
const finalPatternString = patternString.replace(/\*/g, '(.*)');
|
|
190
|
-
|
|
191
|
-
// Cria regex
|
|
192
|
-
const pattern = new RegExp(`^${finalPatternString}$`);
|
|
193
|
-
|
|
194
|
-
return { pattern, params };
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Remove uma rota
|
|
199
|
-
*/
|
|
200
|
-
unregister(path) {
|
|
201
|
-
const normalizedPath = this.normalizePath(path);
|
|
202
|
-
this.routes.delete(normalizedPath);
|
|
203
|
-
return this;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Lista todas as rotas
|
|
208
|
-
*/
|
|
209
|
-
list() {
|
|
210
|
-
const routes = [];
|
|
211
|
-
for (const [path, route] of this.routes.entries()) {
|
|
212
|
-
routes.push({
|
|
213
|
-
path,
|
|
214
|
-
handler: route.handler.name || 'anonymous',
|
|
215
|
-
hasParams: route.hasParams,
|
|
216
|
-
isWildcard: route.isWildcard,
|
|
217
|
-
params: route.params,
|
|
218
|
-
env: route.env
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
return routes;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Limpa todas as rotas
|
|
226
|
-
*/
|
|
227
|
-
clear() {
|
|
228
|
-
this.routes.clear();
|
|
229
|
-
this.middlewares.clear();
|
|
230
|
-
this.globalMiddlewares = [];
|
|
231
|
-
return this;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Verifica se uma rota existe
|
|
236
|
-
*/
|
|
237
|
-
has(path) {
|
|
238
|
-
const normalizedPath = this.normalizePath(path);
|
|
239
|
-
return this.routes.has(normalizedPath) || this.find(path) !== null;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Obtém todas as rotas em formato de objeto
|
|
244
|
-
*/
|
|
245
|
-
getAll() {
|
|
246
|
-
const result = {};
|
|
247
|
-
for (const [path, route] of this.routes.entries()) {
|
|
248
|
-
result[path] = {
|
|
249
|
-
handler: route.handler,
|
|
250
|
-
env: route.env,
|
|
251
|
-
hasParams: route.hasParams,
|
|
252
|
-
params: route.params
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
return result;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Obtém estatísticas das rotas
|
|
260
|
-
*/
|
|
261
|
-
getStats() {
|
|
262
|
-
const routes = Array.from(this.routes.values());
|
|
263
|
-
return {
|
|
264
|
-
total: routes.length,
|
|
265
|
-
withParams: routes.filter(r => r.hasParams).length,
|
|
266
|
-
wildcards: routes.filter(r => r.isWildcard).length,
|
|
267
|
-
routes: routes.map(r => ({
|
|
268
|
-
path: r.path,
|
|
269
|
-
handler: r.handler.name || 'anonymous'
|
|
270
|
-
}))
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Route Registry - Gerencia o registro e matching de rotas para Lambdas
|
|
3
|
+
* Suporta: Path parameters, Wildcards, Middlewares
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class RouteRegistry {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.routes = new Map();
|
|
9
|
+
this.middlewares = new Map();
|
|
10
|
+
this.globalMiddlewares = [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Registra uma rota
|
|
15
|
+
* @param {string} path - Caminho da rota (ex: /users/:id, /api/*)
|
|
16
|
+
* @param {Function} handler - Função handler da Lambda
|
|
17
|
+
* @param {Object} env - Variáveis de ambiente específicas
|
|
18
|
+
* @param {Array} middlewares - Middlewares específicos da rota
|
|
19
|
+
*/
|
|
20
|
+
register(path, handler, env = {}, middlewares = []) {
|
|
21
|
+
// Normaliza o path
|
|
22
|
+
const normalizedPath = this.normalizePath(path);
|
|
23
|
+
|
|
24
|
+
// Parse do path para extrair parâmetros
|
|
25
|
+
const { pattern, params } = this.parsePath(normalizedPath);
|
|
26
|
+
|
|
27
|
+
this.routes.set(normalizedPath, {
|
|
28
|
+
path: normalizedPath,
|
|
29
|
+
pattern,
|
|
30
|
+
params,
|
|
31
|
+
handler,
|
|
32
|
+
env,
|
|
33
|
+
middlewares,
|
|
34
|
+
isWildcard: normalizedPath.includes('*'),
|
|
35
|
+
hasParams: params.length > 0,
|
|
36
|
+
timestamp: Date.now()
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Adiciona versão com barra se não existir
|
|
40
|
+
if (!normalizedPath.endsWith('/') && normalizedPath !== '*') {
|
|
41
|
+
const slashPath = `${normalizedPath}/`;
|
|
42
|
+
if (!this.routes.has(slashPath)) {
|
|
43
|
+
this.routes.set(slashPath, {
|
|
44
|
+
path: slashPath,
|
|
45
|
+
pattern: this.parsePath(slashPath).pattern,
|
|
46
|
+
params: this.parsePath(slashPath).params,
|
|
47
|
+
handler,
|
|
48
|
+
env,
|
|
49
|
+
middlewares,
|
|
50
|
+
isWildcard: false,
|
|
51
|
+
hasParams: this.parsePath(slashPath).params.length > 0,
|
|
52
|
+
timestamp: Date.now()
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Registra um middleware global
|
|
62
|
+
*/
|
|
63
|
+
use(middleware) {
|
|
64
|
+
this.globalMiddlewares.push(middleware);
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Registra um middleware para uma rota específica
|
|
70
|
+
*/
|
|
71
|
+
useFor(path, middleware) {
|
|
72
|
+
const normalizedPath = this.normalizePath(path);
|
|
73
|
+
if (!this.middlewares.has(normalizedPath)) {
|
|
74
|
+
this.middlewares.set(normalizedPath, []);
|
|
75
|
+
}
|
|
76
|
+
this.middlewares.get(normalizedPath).push(middleware);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Encontra uma rota que corresponde ao path
|
|
82
|
+
* @param {string} path - Path da requisição
|
|
83
|
+
* @returns {Object|null} - Rota encontrada com parâmetros extraídos
|
|
84
|
+
*/
|
|
85
|
+
find(path) {
|
|
86
|
+
const normalizedPath = this.normalizePath(path);
|
|
87
|
+
|
|
88
|
+
// Busca exata primeiro
|
|
89
|
+
if (this.routes.has(normalizedPath)) {
|
|
90
|
+
const route = this.routes.get(normalizedPath);
|
|
91
|
+
return {
|
|
92
|
+
...route,
|
|
93
|
+
params: {}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Busca por prefixo (para rotas sem parâmetros)
|
|
98
|
+
for (const [routePath, route] of this.routes.entries()) {
|
|
99
|
+
if (!route.hasParams && !route.isWildcard && normalizedPath.startsWith(routePath)) {
|
|
100
|
+
return {
|
|
101
|
+
...route,
|
|
102
|
+
params: {}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Busca por padrão com parâmetros
|
|
108
|
+
for (const [routePath, route] of this.routes.entries()) {
|
|
109
|
+
if (route.hasParams && route.pattern) {
|
|
110
|
+
const match = route.pattern.exec(normalizedPath);
|
|
111
|
+
if (match) {
|
|
112
|
+
const params = {};
|
|
113
|
+
route.params.forEach((paramName, index) => {
|
|
114
|
+
params[paramName] = match[index + 1];
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
...route,
|
|
118
|
+
params
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Busca wildcard
|
|
125
|
+
for (const [routePath, route] of this.routes.entries()) {
|
|
126
|
+
if (route.isWildcard) {
|
|
127
|
+
const wildcardPath = routePath.replace('*', '');
|
|
128
|
+
if (normalizedPath.startsWith(wildcardPath)) {
|
|
129
|
+
return {
|
|
130
|
+
...route,
|
|
131
|
+
params: {
|
|
132
|
+
wildcard: normalizedPath.substring(wildcardPath.length)
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Obtém todos os middlewares para uma rota
|
|
144
|
+
*/
|
|
145
|
+
getMiddlewares(route) {
|
|
146
|
+
const routeMiddlewares = this.middlewares.get(route.path) || [];
|
|
147
|
+
return [...this.globalMiddlewares, ...routeMiddlewares, ...route.middlewares];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Normaliza o path
|
|
152
|
+
*/
|
|
153
|
+
normalizePath(path) {
|
|
154
|
+
// Remove query string
|
|
155
|
+
const pathWithoutQuery = path.split('?')[0];
|
|
156
|
+
|
|
157
|
+
// Remove trailing slash
|
|
158
|
+
let normalized = pathWithoutQuery.replace(/\/+$/, '');
|
|
159
|
+
|
|
160
|
+
// Adiciona slash inicial se necessário
|
|
161
|
+
if (!normalized.startsWith('/')) {
|
|
162
|
+
normalized = `/${normalized}`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Se for vazio, usa root
|
|
166
|
+
if (normalized === '') {
|
|
167
|
+
normalized = '/';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return normalized;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Parse do path para extrair parâmetros e criar regex pattern
|
|
175
|
+
* @param {string} path - Path da rota (ex: /users/:id/posts/:postId)
|
|
176
|
+
* @returns {Object} - { pattern, params }
|
|
177
|
+
*/
|
|
178
|
+
parsePath(path) {
|
|
179
|
+
const params = [];
|
|
180
|
+
|
|
181
|
+
// Substitui :param por regex capture group
|
|
182
|
+
const patternString = path.replace(/:[^\s/]+/g, (match) => {
|
|
183
|
+
const paramName = match.substring(1);
|
|
184
|
+
params.push(paramName);
|
|
185
|
+
return '([^/]+)';
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// Substitui * por regex wildcard
|
|
189
|
+
const finalPatternString = patternString.replace(/\*/g, '(.*)');
|
|
190
|
+
|
|
191
|
+
// Cria regex
|
|
192
|
+
const pattern = new RegExp(`^${finalPatternString}$`);
|
|
193
|
+
|
|
194
|
+
return { pattern, params };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Remove uma rota
|
|
199
|
+
*/
|
|
200
|
+
unregister(path) {
|
|
201
|
+
const normalizedPath = this.normalizePath(path);
|
|
202
|
+
this.routes.delete(normalizedPath);
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Lista todas as rotas
|
|
208
|
+
*/
|
|
209
|
+
list() {
|
|
210
|
+
const routes = [];
|
|
211
|
+
for (const [path, route] of this.routes.entries()) {
|
|
212
|
+
routes.push({
|
|
213
|
+
path,
|
|
214
|
+
handler: route.handler.name || 'anonymous',
|
|
215
|
+
hasParams: route.hasParams,
|
|
216
|
+
isWildcard: route.isWildcard,
|
|
217
|
+
params: route.params,
|
|
218
|
+
env: route.env
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
return routes;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Limpa todas as rotas
|
|
226
|
+
*/
|
|
227
|
+
clear() {
|
|
228
|
+
this.routes.clear();
|
|
229
|
+
this.middlewares.clear();
|
|
230
|
+
this.globalMiddlewares = [];
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Verifica se uma rota existe
|
|
236
|
+
*/
|
|
237
|
+
has(path) {
|
|
238
|
+
const normalizedPath = this.normalizePath(path);
|
|
239
|
+
return this.routes.has(normalizedPath) || this.find(path) !== null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Obtém todas as rotas em formato de objeto
|
|
244
|
+
*/
|
|
245
|
+
getAll() {
|
|
246
|
+
const result = {};
|
|
247
|
+
for (const [path, route] of this.routes.entries()) {
|
|
248
|
+
result[path] = {
|
|
249
|
+
handler: route.handler,
|
|
250
|
+
env: route.env,
|
|
251
|
+
hasParams: route.hasParams,
|
|
252
|
+
params: route.params
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Obtém estatísticas das rotas
|
|
260
|
+
*/
|
|
261
|
+
getStats() {
|
|
262
|
+
const routes = Array.from(this.routes.values());
|
|
263
|
+
return {
|
|
264
|
+
total: routes.length,
|
|
265
|
+
withParams: routes.filter(r => r.hasParams).length,
|
|
266
|
+
wildcards: routes.filter(r => r.isWildcard).length,
|
|
267
|
+
routes: routes.map(r => ({
|
|
268
|
+
path: r.path,
|
|
269
|
+
handler: r.handler.name || 'anonymous'
|
|
270
|
+
}))
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
275
|
module.exports = RouteRegistry;
|