@gugananuvem/aws-local-simulator 1.0.10 → 1.0.12

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 (44) hide show
  1. package/README.md +257 -193
  2. package/bin/aws-local-simulator.js +62 -62
  3. package/package.json +2 -2
  4. package/src/config/config-loader.js +114 -112
  5. package/src/config/default-config.js +67 -65
  6. package/src/config/env-loader.js +68 -68
  7. package/src/index.js +130 -130
  8. package/src/index.mjs +123 -123
  9. package/src/server.js +223 -222
  10. package/src/services/apigateway/index.js +68 -66
  11. package/src/services/apigateway/server.js +487 -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 +279 -228
  15. package/src/services/cognito/simulator.js +1114 -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 +65 -65
  20. package/src/services/ecs/server.js +233 -233
  21. package/src/services/ecs/simulator.js +844 -844
  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 +183 -172
  25. package/src/services/lambda/index.js +73 -72
  26. package/src/services/lambda/route-registry.js +274 -274
  27. package/src/services/lambda/server.js +145 -152
  28. package/src/services/lambda/simulator.js +172 -285
  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 +345 -273
  35. package/src/services/sqs/simulator.js +441 -660
  36. package/src/services/sts/index.js +37 -0
  37. package/src/services/sts/server.js +142 -0
  38. package/src/services/sts/simulator.js +69 -0
  39. package/src/template/aws-config-template.js +87 -87
  40. package/src/template/aws-config-template.mjs +90 -90
  41. package/src/template/config-template.json +203 -203
  42. package/src/utils/aws-config.js +91 -91
  43. package/src/utils/local-store.js +67 -67
  44. package/src/utils/logger.js +59 -59
@@ -1,153 +1,146 @@
1
- /**
2
- * Lambda Server - Servidor HTTP para Lambda (API Gateway style)
3
- */
4
-
5
- const express = require('express');
6
- const cors = require('cors');
7
- const LambdaSimulator = require('./simulator');
8
- const logger = require('../../utils/logger');
9
-
10
- class LambdaServer {
11
- constructor(port, config) {
12
- this.port = port;
13
- this.config = config;
14
- this.app = express();
15
- this.simulator = null;
16
- this.server = null;
17
- this.setupMiddlewares();
18
- }
19
-
20
- setupMiddlewares() {
21
- this.app.use(express.json({ limit: '10mb' }));
22
- this.app.use(express.urlencoded({ extended: true }));
23
- this.app.use(cors());
24
-
25
- // Logging de requisições
26
- if (logger.currentLogLevel === 'verboso') {
27
- this.app.use((req, res, next) => {
28
- const start = Date.now();
29
- res.on('finish', () => {
30
- const duration = Date.now() - start;
31
- logger.verboso(`Lambda: ${req.method} ${req.path} - ${duration}ms`);
32
- });
33
- next();
34
- });
35
- }
36
- }
37
-
38
- async initialize() {
39
- this.simulator = new LambdaSimulator(this.config);
40
- await this.simulator.initialize();
41
- this.setupRoutes();
42
- }
43
-
44
- setupRoutes() {
45
- // Health check
46
- this.app.get('/health', (req, res) => {
47
- res.json({
48
- status: 'healthy',
49
- version: require('../../../package.json').version,
50
- lambdas: this.simulator.getLambdasCount(),
51
- routes: this.simulator.listRoutes()
52
- });
53
- });
54
-
55
- // Rota catch-all para todas as Lambdas
56
- this.app.all('*', async (req, res) => {
57
- const result = await this.simulator.handleRequest(req, res);
58
-
59
- if (result && result.error) {
60
- res.status(result.status).json(result.error);
61
- }
62
- });
63
-
64
- // Admin endpoints
65
- this.setupAdminRoutes();
66
- }
67
-
68
- setupAdminRoutes() {
69
- // Listar todas as Lambdas
70
- this.app.get('/__admin/lambdas', (req, res) => {
71
- res.json(this.simulator.listLambdas());
72
- });
73
-
74
- // Detalhes de uma Lambda
75
- this.app.get('/__admin/lambdas/:path', (req, res) => {
76
- const lambda = this.simulator.getLambda(req.params.path);
77
- if (lambda) {
78
- res.json(lambda);
79
- } else {
80
- res.status(404).json({ error: 'Lambda not found' });
81
- }
82
- });
83
-
84
- // Recarregar Lambdas
85
- this.app.post('/__admin/reload', async (req, res) => {
86
- await this.simulator.reloadLambdas();
87
- res.json({
88
- message: 'Lambdas recarregadas',
89
- count: this.simulator.getLambdasCount()
90
- });
91
- });
92
-
93
- // Injetar variável de ambiente
94
- this.app.post('/__admin/env', (req, res) => {
95
- const { key, value } = req.body;
96
- if (key && value !== undefined) {
97
- this.simulator.setEnvironmentVariable(key, value);
98
- res.json({ message: `Environment variable ${key} set` });
99
- } else {
100
- res.status(400).json({ error: 'Missing key or value' });
101
- }
102
- });
103
-
104
- // Listar variáveis de ambiente
105
- this.app.get('/__admin/env', (req, res) => {
106
- res.json(this.simulator.getEnvironmentVariables());
107
- });
108
-
109
- // Estatísticas
110
- this.app.get('/__admin/stats', (req, res) => {
111
- res.json(this.simulator.getStats());
112
- });
113
- }
114
-
115
- start() {
116
- return new Promise((resolve) => {
117
- this.server = this.app.listen(this.port, () => {
118
- logger.info(`🚀 Lambda API rodando em http://localhost:${this.port}`);
119
- this.printRoutes();
120
- resolve();
121
- });
122
- });
123
- }
124
-
125
- printRoutes() {
126
- logger.info('\n📚 Lambdas registradas:');
127
- const lambdas = this.simulator.listLambdas();
128
- for (const lambda of lambdas) {
129
- logger.info(` ${lambda.path.padEnd(30)} -> ${lambda.handlerName || 'anonymous'}`);
130
- }
131
- }
132
-
133
- stop() {
134
- return new Promise((resolve) => {
135
- if (this.server) {
136
- this.server.close(() => resolve());
137
- } else {
138
- resolve();
139
- }
140
- });
141
- }
142
-
143
- getStatus() {
144
- return {
145
- running: !!this.server,
146
- port: this.port,
147
- endpoint: `http://localhost:${this.port}`,
148
- lambdasCount: this.simulator?.getLambdasCount() || 0
149
- };
150
- }
151
- }
152
-
1
+ /**
2
+ * Lambda Server - Servidor HTTP para Lambda (API Gateway style)
3
+ */
4
+
5
+ const express = require('express');
6
+ const cors = require('cors');
7
+ const LambdaSimulator = require('./simulator');
8
+ const logger = require('../../utils/logger');
9
+
10
+ class LambdaServer {
11
+ constructor(port, config) {
12
+ this.port = port;
13
+ this.config = config;
14
+ this.app = express();
15
+ this.simulator = null;
16
+ this.server = null;
17
+ this.setupMiddlewares();
18
+ }
19
+
20
+ setupMiddlewares() {
21
+ this.app.use(express.json({ limit: '10mb' }));
22
+ this.app.use(express.urlencoded({ extended: true }));
23
+ this.app.use(cors());
24
+
25
+ // Logging de requisições
26
+ if (logger.currentLogLevel === 'verboso') {
27
+ this.app.use((req, res, next) => {
28
+ const start = Date.now();
29
+ res.on('finish', () => {
30
+ const duration = Date.now() - start;
31
+ logger.verboso(`Lambda: ${req.method} ${req.path} - ${duration}ms`);
32
+ });
33
+ next();
34
+ });
35
+ }
36
+ }
37
+
38
+ async initialize() {
39
+ this.simulator = new LambdaSimulator(this.config);
40
+ await this.simulator.initialize();
41
+ this.setupRoutes();
42
+ }
43
+
44
+ setupRoutes() {
45
+ // Health check
46
+ this.app.get('/health', (req, res) => {
47
+ res.json({ status: 'healthy', lambdas: this.simulator.getLambdasCount() });
48
+ });
49
+
50
+ // AWS Lambda Invoke API: POST /2015-03-31/functions/{functionName}/invocations
51
+ this.app.post('/2015-03-31/functions/:functionName/invocations', async (req, res) => {
52
+ const { functionName } = req.params;
53
+ const invocationType = req.headers['x-amz-invocation-type'] || 'RequestResponse';
54
+ const event = req.body || {};
55
+
56
+ logger.debug(`Lambda invoke: ${functionName} (${invocationType})`);
57
+
58
+ try {
59
+ const result = await this.simulator.invoke(functionName, event, invocationType);
60
+
61
+ if (invocationType === 'Event') {
62
+ return res.status(202).send();
63
+ }
64
+
65
+ res.status(result.StatusCode || 200).json(result.Payload);
66
+ } catch (err) {
67
+ if (err.message && err.message.includes('Function not found')) {
68
+ return res.status(404).json({ __type: 'ResourceNotFoundException', message: err.message });
69
+ }
70
+ logger.error('Lambda invoke error:', err);
71
+ res.status(500).json({ __type: 'ServiceException', message: err.message });
72
+ }
73
+ });
74
+
75
+ // Admin endpoints
76
+ this.setupAdminRoutes();
77
+ }
78
+
79
+ setupAdminRoutes() {
80
+ this.app.get('/__admin/functions', (req, res) => {
81
+ res.json(this.simulator.listLambdas());
82
+ });
83
+
84
+ this.app.post('/__admin/reload', async (req, res) => {
85
+ await this.simulator.reloadLambdas();
86
+ res.json({ message: 'Lambdas recarregadas', count: this.simulator.getLambdasCount() });
87
+ });
88
+
89
+ this.app.post('/__admin/env', (req, res) => {
90
+ const { key, value } = req.body;
91
+ if (key && value !== undefined) {
92
+ this.simulator.setEnvironmentVariable(key, value);
93
+ res.json({ message: `Environment variable ${key} set` });
94
+ } else {
95
+ res.status(400).json({ error: 'Missing key or value' });
96
+ }
97
+ });
98
+
99
+ this.app.get('/__admin/env', (req, res) => {
100
+ res.json(this.simulator.getEnvironmentVariables());
101
+ });
102
+
103
+ this.app.get('/__admin/stats', (req, res) => {
104
+ res.json(this.simulator.getStats());
105
+ });
106
+ }
107
+
108
+ start() {
109
+ return new Promise((resolve) => {
110
+ this.server = this.app.listen(this.port, () => {
111
+ logger.info(`🚀 Lambda API rodando em http://localhost:${this.port}`);
112
+ this.printRoutes();
113
+ resolve();
114
+ });
115
+ });
116
+ }
117
+
118
+ printRoutes() {
119
+ logger.info('\n📚 Lambdas registradas:');
120
+ const lambdas = this.simulator.listLambdas();
121
+ for (const lambda of lambdas) {
122
+ logger.info(` ${lambda.name.padEnd(30)} -> ${lambda.handlerName || 'anonymous'}`);
123
+ }
124
+ }
125
+
126
+ stop() {
127
+ return new Promise((resolve) => {
128
+ if (this.server) {
129
+ this.server.close(() => resolve());
130
+ } else {
131
+ resolve();
132
+ }
133
+ });
134
+ }
135
+
136
+ getStatus() {
137
+ return {
138
+ running: !!this.server,
139
+ port: this.port,
140
+ endpoint: `http://localhost:${this.port}`,
141
+ lambdasCount: this.simulator?.getLambdasCount() || 0
142
+ };
143
+ }
144
+ }
145
+
153
146
  module.exports = LambdaServer;