@gugananuvem/aws-local-simulator 1.0.9 → 1.0.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.
Files changed (41) hide show
  1. package/README.md +204 -192
  2. package/bin/aws-local-simulator.js +62 -62
  3. package/package.json +2 -2
  4. package/src/config/config-loader.js +112 -112
  5. package/src/config/default-config.js +65 -65
  6. package/src/config/env-loader.js +68 -66
  7. package/src/index.js +130 -130
  8. package/src/index.mjs +123 -123
  9. package/src/server.js +221 -219
  10. package/src/services/apigateway/index.js +66 -66
  11. package/src/services/apigateway/server.js +434 -434
  12. package/src/services/apigateway/simulator.js +1251 -1251
  13. package/src/services/cognito/index.js +65 -65
  14. package/src/services/cognito/server.js +228 -228
  15. package/src/services/cognito/simulator.js +847 -847
  16. package/src/services/dynamodb/index.js +70 -70
  17. package/src/services/dynamodb/server.js +121 -121
  18. package/src/services/dynamodb/simulator.js +620 -614
  19. package/src/services/ecs/index.js +66 -0
  20. package/src/services/ecs/server.js +234 -0
  21. package/src/services/ecs/simulator.js +845 -0
  22. package/src/services/eventbridge/index.js +84 -84
  23. package/src/services/index.js +18 -18
  24. package/src/services/lambda/handler-loader.js +172 -172
  25. package/src/services/lambda/index.js +72 -72
  26. package/src/services/lambda/route-registry.js +274 -274
  27. package/src/services/lambda/server.js +152 -152
  28. package/src/services/lambda/simulator.js +284 -277
  29. package/src/services/s3/index.js +69 -69
  30. package/src/services/s3/server.js +238 -238
  31. package/src/services/s3/simulator.js +740 -740
  32. package/src/services/sns/index.js +75 -75
  33. package/src/services/sqs/index.js +95 -95
  34. package/src/services/sqs/server.js +273 -273
  35. package/src/services/sqs/simulator.js +659 -659
  36. package/src/template/aws-config-template.js +87 -87
  37. package/src/template/aws-config-template.mjs +90 -90
  38. package/src/template/config-template.json +203 -165
  39. package/src/utils/aws-config.js +91 -91
  40. package/src/utils/local-store.js +67 -67
  41. 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;