@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.
Files changed (77) hide show
  1. package/README.md +789 -594
  2. package/bin/aws-local-simulator.js +63 -63
  3. package/package.json +2 -2
  4. package/src/config/config-loader.js +114 -114
  5. package/src/config/default-config.js +68 -68
  6. package/src/config/env-loader.js +68 -68
  7. package/src/index.js +146 -146
  8. package/src/index.mjs +123 -123
  9. package/src/server.js +227 -227
  10. package/src/services/apigateway/index.js +75 -73
  11. package/src/services/apigateway/server.js +570 -507
  12. package/src/services/apigateway/simulator.js +1261 -1261
  13. package/src/services/athena/index.js +75 -75
  14. package/src/services/athena/server.js +101 -101
  15. package/src/services/athena/simulador.js +998 -998
  16. package/src/services/athena/simulator.js +346 -346
  17. package/src/services/cloudformation/index.js +106 -106
  18. package/src/services/cloudformation/server.js +417 -417
  19. package/src/services/cloudformation/simulador.js +1045 -1045
  20. package/src/services/cloudtrail/index.js +84 -84
  21. package/src/services/cloudtrail/server.js +235 -235
  22. package/src/services/cloudtrail/simulador.js +719 -719
  23. package/src/services/cloudwatch/index.js +84 -84
  24. package/src/services/cloudwatch/server.js +366 -366
  25. package/src/services/cloudwatch/simulador.js +1173 -1173
  26. package/src/services/cognito/index.js +79 -79
  27. package/src/services/cognito/server.js +301 -301
  28. package/src/services/cognito/simulator.js +1655 -1655
  29. package/src/services/config/index.js +96 -96
  30. package/src/services/config/server.js +215 -215
  31. package/src/services/config/simulador.js +1260 -1260
  32. package/src/services/dynamodb/index.js +74 -74
  33. package/src/services/dynamodb/server.js +125 -125
  34. package/src/services/dynamodb/simulator.js +630 -630
  35. package/src/services/ecs/index.js +65 -65
  36. package/src/services/ecs/server.js +235 -235
  37. package/src/services/ecs/simulator.js +844 -844
  38. package/src/services/eventbridge/index.js +89 -89
  39. package/src/services/eventbridge/server.js +209 -209
  40. package/src/services/eventbridge/simulator.js +684 -684
  41. package/src/services/index.js +45 -45
  42. package/src/services/kms/index.js +75 -75
  43. package/src/services/kms/server.js +67 -67
  44. package/src/services/kms/simulator.js +324 -324
  45. package/src/services/lambda/handler-loader.js +183 -183
  46. package/src/services/lambda/index.js +78 -78
  47. package/src/services/lambda/route-registry.js +274 -274
  48. package/src/services/lambda/server.js +145 -145
  49. package/src/services/lambda/simulator.js +199 -199
  50. package/src/services/parameter-store/index.js +80 -80
  51. package/src/services/parameter-store/server.js +50 -50
  52. package/src/services/parameter-store/simulator.js +201 -201
  53. package/src/services/s3/index.js +73 -73
  54. package/src/services/s3/server.js +329 -329
  55. package/src/services/s3/simulator.js +565 -565
  56. package/src/services/secret-manager/index.js +80 -80
  57. package/src/services/secret-manager/server.js +50 -50
  58. package/src/services/secret-manager/simulator.js +171 -171
  59. package/src/services/sns/index.js +89 -89
  60. package/src/services/sns/server.js +580 -580
  61. package/src/services/sns/simulator.js +1482 -1482
  62. package/src/services/sqs/index.js +98 -93
  63. package/src/services/sqs/server.js +349 -349
  64. package/src/services/sqs/simulator.js +441 -441
  65. package/src/services/sts/index.js +37 -37
  66. package/src/services/sts/server.js +144 -144
  67. package/src/services/sts/simulator.js +69 -69
  68. package/src/services/xray/index.js +83 -83
  69. package/src/services/xray/server.js +308 -308
  70. package/src/services/xray/simulador.js +994 -994
  71. package/src/template/aws-config-template.js +87 -87
  72. package/src/template/aws-config-template.mjs +90 -90
  73. package/src/template/config-template.json +203 -203
  74. package/src/utils/aws-config.js +91 -91
  75. package/src/utils/cloudtrail-audit.js +129 -129
  76. package/src/utils/local-store.js +83 -83
  77. package/src/utils/logger.js +59 -59
@@ -1,308 +1,308 @@
1
- 'use strict';
2
-
3
- /**
4
- * @fileoverview X-Ray HTTP Server
5
- *
6
- * Protocolo: JSON com header X-Amz-Target
7
- * Compatível com @aws-sdk/client-xray (XRayClient)
8
- *
9
- * Target prefix: AmazonXRay
10
- *
11
- * Operações suportadas:
12
- * - PutTraceSegments
13
- * - BatchGetTraces
14
- * - GetTraceSummaries
15
- * - GetTraceGraph
16
- * - GetServiceGraph
17
- * - CreateGroup / UpdateGroup / DeleteGroup / GetGroup / GetGroups
18
- * - CreateSamplingRule / UpdateSamplingRule / DeleteSamplingRule
19
- * - GetSamplingRules / GetSamplingStatisticSummaries / GetSamplingTargets
20
- * - PutEncryptionConfig / GetEncryptionConfig
21
- * - TagResource / UntagResource / ListTagsForResource
22
- * - GetInsight / GetInsightSummaries / GetInsightEvents / GetInsightImpactGraph
23
- *
24
- * Rotas admin:
25
- * GET /__admin/traces - lista todos os traces
26
- * GET /__admin/groups - lista todos os grupos
27
- * GET /__admin/sampling-rules - lista sampling rules
28
- * GET /__admin/status - status do simulador
29
- * POST /__admin/reset - reseta todos os dados
30
- * GET /__admin/health - health check
31
- *
32
- * Rota interna:
33
- * POST /__internal/record-service-call - usada pelos outros serviços para registrar traces
34
- */
35
-
36
- const { XRaySimulator } = require('./simulador');
37
-
38
- function parseBody(req) {
39
- return new Promise((resolve, reject) => {
40
- let body = '';
41
- req.on('data', chunk => (body += chunk.toString()));
42
- req.on('end', () => {
43
- try {
44
- resolve(body ? JSON.parse(body) : {});
45
- } catch {
46
- resolve({});
47
- }
48
- });
49
- req.on('error', reject);
50
- });
51
- }
52
-
53
- function sendJson(res, statusCode, data) {
54
- const body = JSON.stringify(data);
55
- res.writeHead(statusCode, {
56
- 'Content-Type': 'application/json',
57
- 'Content-Length': Buffer.byteLength(body),
58
- 'x-amzn-RequestId': require('crypto').randomUUID(),
59
- });
60
- res.end(body);
61
- }
62
-
63
- function sendError(res, err) {
64
- const statusCode = err.statusCode || 400;
65
- sendJson(res, statusCode, {
66
- __type: err.code || 'ServiceException',
67
- message: err.message,
68
- });
69
- }
70
-
71
- // ─── Mapa de operações ────────────────────────────────────────────────────────
72
-
73
- const OPERATION_MAP = {
74
- // Traces
75
- 'PutTraceSegments': (sim, body) => sim.putTraceSegments(body),
76
- 'BatchGetTraces': (sim, body) => sim.batchGetTraces(body),
77
- 'GetTraceSummaries': (sim, body) => sim.getTraceSummaries(body),
78
- 'GetTraceGraph': (sim, body) => sim.getTraceGraph(body),
79
-
80
- // Service Graph
81
- 'GetServiceGraph': (sim, body) => sim.getServiceGraph(body),
82
-
83
- // Groups
84
- 'CreateGroup': (sim, body) => sim.createGroup(body),
85
- 'UpdateGroup': (sim, body) => sim.updateGroup(body),
86
- 'DeleteGroup': (sim, body) => sim.deleteGroup(body),
87
- 'GetGroup': (sim, body) => sim.getGroup(body),
88
- 'GetGroups': (sim, body) => sim.getGroups(body),
89
-
90
- // Sampling Rules
91
- 'CreateSamplingRule': (sim, body) => sim.createSamplingRule(body),
92
- 'UpdateSamplingRule': (sim, body) => sim.updateSamplingRule(body),
93
- 'DeleteSamplingRule': (sim, body) => sim.deleteSamplingRule(body),
94
- 'GetSamplingRules': (sim, body) => sim.getSamplingRules(body),
95
- 'GetSamplingStatisticSummaries': (sim, body) => sim.getSamplingStatisticSummaries(body),
96
- 'GetSamplingTargets': (sim, body) => sim.getSamplingTargets(body),
97
-
98
- // Encryption
99
- 'PutEncryptionConfig': (sim, body) => sim.putEncryptionConfig(body),
100
- 'GetEncryptionConfig': (sim, body) => sim.getEncryptionConfig(),
101
-
102
- // Tags
103
- 'TagResource': (sim, body) => sim.tagResource(body),
104
- 'UntagResource': (sim, body) => sim.untagResource(body),
105
- 'ListTagsForResource': (sim, body) => sim.listTagsForResource(body),
106
-
107
- // Insights
108
- 'GetInsight': (sim, body) => sim.getInsight(body),
109
- 'GetInsightSummaries': (sim, body) => sim.getInsightSummaries(body),
110
- 'GetInsightEvents': (sim, body) => sim.getInsightEvents(body),
111
- 'GetInsightImpactGraph': (sim, body) => sim.getInsightImpactGraph(body),
112
- };
113
-
114
- // ─── Factory do servidor HTTP ─────────────────────────────────────────────────
115
-
116
- function createXRayServer(simulator) {
117
- const http = require('http');
118
-
119
- const server = http.createServer(async (req, res) => {
120
- // CORS
121
- res.setHeader('Access-Control-Allow-Origin', '*');
122
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
123
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Amz-Target, Authorization, X-Amz-Date, X-Api-Key');
124
-
125
- if (req.method === 'OPTIONS') {
126
- res.writeHead(204);
127
- res.end();
128
- return;
129
- }
130
-
131
- const url = new URL(req.url, `http://${req.headers.host}`);
132
- const pathname = url.pathname;
133
-
134
- // ── Rotas Admin ───────────────────────────────────────────────────────────
135
-
136
- if (pathname.startsWith('/__admin/')) {
137
- const body = req.method !== 'GET' ? await parseBody(req) : {};
138
-
139
- try {
140
- if (req.method === 'GET' && pathname === '/__admin/traces') {
141
- const traces = Array.from(simulator._traces.values());
142
- return sendJson(res, 200, { traces, total: traces.length });
143
- }
144
-
145
- if (req.method === 'GET' && pathname === '/__admin/groups') {
146
- const groups = Array.from(simulator._groups.values());
147
- return sendJson(res, 200, { groups, total: groups.length });
148
- }
149
-
150
- if (req.method === 'GET' && pathname === '/__admin/sampling-rules') {
151
- const rules = Array.from(simulator._samplingRules.values());
152
- return sendJson(res, 200, { samplingRules: rules, total: rules.length });
153
- }
154
-
155
- if (req.method === 'GET' && pathname === '/__admin/status') {
156
- return sendJson(res, 200, simulator.getStatus());
157
- }
158
-
159
- if (req.method === 'GET' && pathname === '/__admin/health') {
160
- return sendJson(res, 200, {
161
- status: 'ok',
162
- service: 'xray',
163
- traces: simulator._traces.size,
164
- uptime: process.uptime(),
165
- });
166
- }
167
-
168
- if (req.method === 'POST' && pathname === '/__admin/reset') {
169
- await simulator.reset();
170
- return sendJson(res, 200, { message: 'X-Ray simulator reset successfully' });
171
- }
172
-
173
- return sendJson(res, 404, { message: 'Admin route not found' });
174
- } catch (err) {
175
- return sendError(res, err);
176
- }
177
- }
178
-
179
- // ── Rota interna: registro de chamadas de outros serviços ─────────────────
180
-
181
- if (req.method === 'POST' && pathname === '/__internal/record-service-call') {
182
- try {
183
- const body = await parseBody(req);
184
- const traceId = simulator.recordServiceCall(body);
185
- return sendJson(res, 200, { TraceId: traceId });
186
- } catch (err) {
187
- return sendError(res, err);
188
- }
189
- }
190
-
191
- // ── API X-Ray via X-Amz-Target ───────────────────────────────────────────
192
-
193
- const target = req.headers['x-amz-target'] || '';
194
- // Aceita: AmazonXRay.PutTraceSegments ou xray.PutTraceSegments
195
- const operation = target.split('.').pop();
196
-
197
- if (operation && OPERATION_MAP[operation]) {
198
- try {
199
- const body = await parseBody(req);
200
- const result = await OPERATION_MAP[operation](simulator, body);
201
- return sendJson(res, 200, result);
202
- } catch (err) {
203
- return sendError(res, err);
204
- }
205
- }
206
-
207
- // ── Rotas REST alternativas (path-based) ──────────────────────────────────
208
-
209
- try {
210
- const body = req.method !== 'GET' ? await parseBody(req) : {};
211
-
212
- // POST /TraceSegments
213
- if (req.method === 'POST' && pathname === '/TraceSegments') {
214
- const result = await simulator.putTraceSegments(body);
215
- return sendJson(res, 200, result);
216
- }
217
-
218
- // POST /Traces (BatchGetTraces)
219
- if (req.method === 'POST' && pathname === '/Traces') {
220
- const result = await simulator.batchGetTraces(body);
221
- return sendJson(res, 200, result);
222
- }
223
-
224
- // GET /TraceSummaries
225
- if (req.method === 'GET' && pathname === '/TraceSummaries') {
226
- const params = Object.fromEntries(url.searchParams.entries());
227
- const result = await simulator.getTraceSummaries({
228
- StartTime: params.StartTime ? parseFloat(params.StartTime) : undefined,
229
- EndTime: params.EndTime ? parseFloat(params.EndTime) : undefined,
230
- FilterExpression: params.FilterExpression,
231
- NextToken: params.NextToken,
232
- });
233
- return sendJson(res, 200, result);
234
- }
235
-
236
- // POST /TraceGraph
237
- if (req.method === 'POST' && pathname === '/TraceGraph') {
238
- const result = await simulator.getTraceGraph(body);
239
- return sendJson(res, 200, result);
240
- }
241
-
242
- // GET /ServiceGraph
243
- if (req.method === 'GET' && pathname === '/ServiceGraph') {
244
- const params = Object.fromEntries(url.searchParams.entries());
245
- const result = await simulator.getServiceGraph({
246
- StartTime: params.StartTime ? parseFloat(params.StartTime) : undefined,
247
- EndTime: params.EndTime ? parseFloat(params.EndTime) : undefined,
248
- GroupName: params.GroupName,
249
- GroupARN: params.GroupARN,
250
- NextToken: params.NextToken,
251
- });
252
- return sendJson(res, 200, result);
253
- }
254
-
255
- // POST /Groups
256
- if (req.method === 'POST' && pathname === '/Groups') {
257
- const result = await simulator.createGroup(body);
258
- return sendJson(res, 200, result);
259
- }
260
-
261
- // GET /Groups
262
- if (req.method === 'GET' && pathname === '/Groups') {
263
- const params = Object.fromEntries(url.searchParams.entries());
264
- const result = await simulator.getGroups({ NextToken: params.NextToken });
265
- return sendJson(res, 200, result);
266
- }
267
-
268
- // POST /SamplingRules
269
- if (req.method === 'POST' && pathname === '/SamplingRules') {
270
- const result = await simulator.createSamplingRule(body);
271
- return sendJson(res, 201, result);
272
- }
273
-
274
- // GET /SamplingRules
275
- if (req.method === 'GET' && pathname === '/SamplingRules') {
276
- const params = Object.fromEntries(url.searchParams.entries());
277
- const result = await simulator.getSamplingRules({ NextToken: params.NextToken });
278
- return sendJson(res, 200, result);
279
- }
280
-
281
- // POST /SamplingTargets
282
- if (req.method === 'POST' && pathname === '/SamplingTargets') {
283
- const result = await simulator.getSamplingTargets(body);
284
- return sendJson(res, 200, result);
285
- }
286
-
287
- // GET /EncryptionConfig
288
- if (req.method === 'GET' && pathname === '/EncryptionConfig') {
289
- const result = await simulator.getEncryptionConfig();
290
- return sendJson(res, 200, result);
291
- }
292
-
293
- // PUT /EncryptionConfig
294
- if (req.method === 'PUT' && pathname === '/EncryptionConfig') {
295
- const result = await simulator.putEncryptionConfig(body);
296
- return sendJson(res, 200, result);
297
- }
298
-
299
- return sendJson(res, 404, { message: `Route not found: ${req.method} ${pathname}` });
300
- } catch (err) {
301
- return sendError(res, err);
302
- }
303
- });
304
-
305
- return server;
306
- }
307
-
308
- module.exports = { createXRayServer };
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview X-Ray HTTP Server
5
+ *
6
+ * Protocolo: JSON com header X-Amz-Target
7
+ * Compatível com @aws-sdk/client-xray (XRayClient)
8
+ *
9
+ * Target prefix: AmazonXRay
10
+ *
11
+ * Operações suportadas:
12
+ * - PutTraceSegments
13
+ * - BatchGetTraces
14
+ * - GetTraceSummaries
15
+ * - GetTraceGraph
16
+ * - GetServiceGraph
17
+ * - CreateGroup / UpdateGroup / DeleteGroup / GetGroup / GetGroups
18
+ * - CreateSamplingRule / UpdateSamplingRule / DeleteSamplingRule
19
+ * - GetSamplingRules / GetSamplingStatisticSummaries / GetSamplingTargets
20
+ * - PutEncryptionConfig / GetEncryptionConfig
21
+ * - TagResource / UntagResource / ListTagsForResource
22
+ * - GetInsight / GetInsightSummaries / GetInsightEvents / GetInsightImpactGraph
23
+ *
24
+ * Rotas admin:
25
+ * GET /__admin/traces - lista todos os traces
26
+ * GET /__admin/groups - lista todos os grupos
27
+ * GET /__admin/sampling-rules - lista sampling rules
28
+ * GET /__admin/status - status do simulador
29
+ * POST /__admin/reset - reseta todos os dados
30
+ * GET /__admin/health - health check
31
+ *
32
+ * Rota interna:
33
+ * POST /__internal/record-service-call - usada pelos outros serviços para registrar traces
34
+ */
35
+
36
+ const { XRaySimulator } = require('./simulador');
37
+
38
+ function parseBody(req) {
39
+ return new Promise((resolve, reject) => {
40
+ let body = '';
41
+ req.on('data', chunk => (body += chunk.toString()));
42
+ req.on('end', () => {
43
+ try {
44
+ resolve(body ? JSON.parse(body) : {});
45
+ } catch {
46
+ resolve({});
47
+ }
48
+ });
49
+ req.on('error', reject);
50
+ });
51
+ }
52
+
53
+ function sendJson(res, statusCode, data) {
54
+ const body = JSON.stringify(data);
55
+ res.writeHead(statusCode, {
56
+ 'Content-Type': 'application/json',
57
+ 'Content-Length': Buffer.byteLength(body),
58
+ 'x-amzn-RequestId': require('crypto').randomUUID(),
59
+ });
60
+ res.end(body);
61
+ }
62
+
63
+ function sendError(res, err) {
64
+ const statusCode = err.statusCode || 400;
65
+ sendJson(res, statusCode, {
66
+ __type: err.code || 'ServiceException',
67
+ message: err.message,
68
+ });
69
+ }
70
+
71
+ // ─── Mapa de operações ────────────────────────────────────────────────────────
72
+
73
+ const OPERATION_MAP = {
74
+ // Traces
75
+ 'PutTraceSegments': (sim, body) => sim.putTraceSegments(body),
76
+ 'BatchGetTraces': (sim, body) => sim.batchGetTraces(body),
77
+ 'GetTraceSummaries': (sim, body) => sim.getTraceSummaries(body),
78
+ 'GetTraceGraph': (sim, body) => sim.getTraceGraph(body),
79
+
80
+ // Service Graph
81
+ 'GetServiceGraph': (sim, body) => sim.getServiceGraph(body),
82
+
83
+ // Groups
84
+ 'CreateGroup': (sim, body) => sim.createGroup(body),
85
+ 'UpdateGroup': (sim, body) => sim.updateGroup(body),
86
+ 'DeleteGroup': (sim, body) => sim.deleteGroup(body),
87
+ 'GetGroup': (sim, body) => sim.getGroup(body),
88
+ 'GetGroups': (sim, body) => sim.getGroups(body),
89
+
90
+ // Sampling Rules
91
+ 'CreateSamplingRule': (sim, body) => sim.createSamplingRule(body),
92
+ 'UpdateSamplingRule': (sim, body) => sim.updateSamplingRule(body),
93
+ 'DeleteSamplingRule': (sim, body) => sim.deleteSamplingRule(body),
94
+ 'GetSamplingRules': (sim, body) => sim.getSamplingRules(body),
95
+ 'GetSamplingStatisticSummaries': (sim, body) => sim.getSamplingStatisticSummaries(body),
96
+ 'GetSamplingTargets': (sim, body) => sim.getSamplingTargets(body),
97
+
98
+ // Encryption
99
+ 'PutEncryptionConfig': (sim, body) => sim.putEncryptionConfig(body),
100
+ 'GetEncryptionConfig': (sim, body) => sim.getEncryptionConfig(),
101
+
102
+ // Tags
103
+ 'TagResource': (sim, body) => sim.tagResource(body),
104
+ 'UntagResource': (sim, body) => sim.untagResource(body),
105
+ 'ListTagsForResource': (sim, body) => sim.listTagsForResource(body),
106
+
107
+ // Insights
108
+ 'GetInsight': (sim, body) => sim.getInsight(body),
109
+ 'GetInsightSummaries': (sim, body) => sim.getInsightSummaries(body),
110
+ 'GetInsightEvents': (sim, body) => sim.getInsightEvents(body),
111
+ 'GetInsightImpactGraph': (sim, body) => sim.getInsightImpactGraph(body),
112
+ };
113
+
114
+ // ─── Factory do servidor HTTP ─────────────────────────────────────────────────
115
+
116
+ function createXRayServer(simulator) {
117
+ const http = require('http');
118
+
119
+ const server = http.createServer(async (req, res) => {
120
+ // CORS
121
+ res.setHeader('Access-Control-Allow-Origin', '*');
122
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
123
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-Amz-Target, Authorization, X-Amz-Date, X-Api-Key');
124
+
125
+ if (req.method === 'OPTIONS') {
126
+ res.writeHead(204);
127
+ res.end();
128
+ return;
129
+ }
130
+
131
+ const url = new URL(req.url, `http://${req.headers.host}`);
132
+ const pathname = url.pathname;
133
+
134
+ // ── Rotas Admin ───────────────────────────────────────────────────────────
135
+
136
+ if (pathname.startsWith('/__admin/')) {
137
+ const body = req.method !== 'GET' ? await parseBody(req) : {};
138
+
139
+ try {
140
+ if (req.method === 'GET' && pathname === '/__admin/traces') {
141
+ const traces = Array.from(simulator._traces.values());
142
+ return sendJson(res, 200, { traces, total: traces.length });
143
+ }
144
+
145
+ if (req.method === 'GET' && pathname === '/__admin/groups') {
146
+ const groups = Array.from(simulator._groups.values());
147
+ return sendJson(res, 200, { groups, total: groups.length });
148
+ }
149
+
150
+ if (req.method === 'GET' && pathname === '/__admin/sampling-rules') {
151
+ const rules = Array.from(simulator._samplingRules.values());
152
+ return sendJson(res, 200, { samplingRules: rules, total: rules.length });
153
+ }
154
+
155
+ if (req.method === 'GET' && pathname === '/__admin/status') {
156
+ return sendJson(res, 200, simulator.getStatus());
157
+ }
158
+
159
+ if (req.method === 'GET' && pathname === '/__admin/health') {
160
+ return sendJson(res, 200, {
161
+ status: 'ok',
162
+ service: 'xray',
163
+ traces: simulator._traces.size,
164
+ uptime: process.uptime(),
165
+ });
166
+ }
167
+
168
+ if (req.method === 'POST' && pathname === '/__admin/reset') {
169
+ await simulator.reset();
170
+ return sendJson(res, 200, { message: 'X-Ray simulator reset successfully' });
171
+ }
172
+
173
+ return sendJson(res, 404, { message: 'Admin route not found' });
174
+ } catch (err) {
175
+ return sendError(res, err);
176
+ }
177
+ }
178
+
179
+ // ── Rota interna: registro de chamadas de outros serviços ─────────────────
180
+
181
+ if (req.method === 'POST' && pathname === '/__internal/record-service-call') {
182
+ try {
183
+ const body = await parseBody(req);
184
+ const traceId = simulator.recordServiceCall(body);
185
+ return sendJson(res, 200, { TraceId: traceId });
186
+ } catch (err) {
187
+ return sendError(res, err);
188
+ }
189
+ }
190
+
191
+ // ── API X-Ray via X-Amz-Target ───────────────────────────────────────────
192
+
193
+ const target = req.headers['x-amz-target'] || '';
194
+ // Aceita: AmazonXRay.PutTraceSegments ou xray.PutTraceSegments
195
+ const operation = target.split('.').pop();
196
+
197
+ if (operation && OPERATION_MAP[operation]) {
198
+ try {
199
+ const body = await parseBody(req);
200
+ const result = await OPERATION_MAP[operation](simulator, body);
201
+ return sendJson(res, 200, result);
202
+ } catch (err) {
203
+ return sendError(res, err);
204
+ }
205
+ }
206
+
207
+ // ── Rotas REST alternativas (path-based) ──────────────────────────────────
208
+
209
+ try {
210
+ const body = req.method !== 'GET' ? await parseBody(req) : {};
211
+
212
+ // POST /TraceSegments
213
+ if (req.method === 'POST' && pathname === '/TraceSegments') {
214
+ const result = await simulator.putTraceSegments(body);
215
+ return sendJson(res, 200, result);
216
+ }
217
+
218
+ // POST /Traces (BatchGetTraces)
219
+ if (req.method === 'POST' && pathname === '/Traces') {
220
+ const result = await simulator.batchGetTraces(body);
221
+ return sendJson(res, 200, result);
222
+ }
223
+
224
+ // GET /TraceSummaries
225
+ if (req.method === 'GET' && pathname === '/TraceSummaries') {
226
+ const params = Object.fromEntries(url.searchParams.entries());
227
+ const result = await simulator.getTraceSummaries({
228
+ StartTime: params.StartTime ? parseFloat(params.StartTime) : undefined,
229
+ EndTime: params.EndTime ? parseFloat(params.EndTime) : undefined,
230
+ FilterExpression: params.FilterExpression,
231
+ NextToken: params.NextToken,
232
+ });
233
+ return sendJson(res, 200, result);
234
+ }
235
+
236
+ // POST /TraceGraph
237
+ if (req.method === 'POST' && pathname === '/TraceGraph') {
238
+ const result = await simulator.getTraceGraph(body);
239
+ return sendJson(res, 200, result);
240
+ }
241
+
242
+ // GET /ServiceGraph
243
+ if (req.method === 'GET' && pathname === '/ServiceGraph') {
244
+ const params = Object.fromEntries(url.searchParams.entries());
245
+ const result = await simulator.getServiceGraph({
246
+ StartTime: params.StartTime ? parseFloat(params.StartTime) : undefined,
247
+ EndTime: params.EndTime ? parseFloat(params.EndTime) : undefined,
248
+ GroupName: params.GroupName,
249
+ GroupARN: params.GroupARN,
250
+ NextToken: params.NextToken,
251
+ });
252
+ return sendJson(res, 200, result);
253
+ }
254
+
255
+ // POST /Groups
256
+ if (req.method === 'POST' && pathname === '/Groups') {
257
+ const result = await simulator.createGroup(body);
258
+ return sendJson(res, 200, result);
259
+ }
260
+
261
+ // GET /Groups
262
+ if (req.method === 'GET' && pathname === '/Groups') {
263
+ const params = Object.fromEntries(url.searchParams.entries());
264
+ const result = await simulator.getGroups({ NextToken: params.NextToken });
265
+ return sendJson(res, 200, result);
266
+ }
267
+
268
+ // POST /SamplingRules
269
+ if (req.method === 'POST' && pathname === '/SamplingRules') {
270
+ const result = await simulator.createSamplingRule(body);
271
+ return sendJson(res, 201, result);
272
+ }
273
+
274
+ // GET /SamplingRules
275
+ if (req.method === 'GET' && pathname === '/SamplingRules') {
276
+ const params = Object.fromEntries(url.searchParams.entries());
277
+ const result = await simulator.getSamplingRules({ NextToken: params.NextToken });
278
+ return sendJson(res, 200, result);
279
+ }
280
+
281
+ // POST /SamplingTargets
282
+ if (req.method === 'POST' && pathname === '/SamplingTargets') {
283
+ const result = await simulator.getSamplingTargets(body);
284
+ return sendJson(res, 200, result);
285
+ }
286
+
287
+ // GET /EncryptionConfig
288
+ if (req.method === 'GET' && pathname === '/EncryptionConfig') {
289
+ const result = await simulator.getEncryptionConfig();
290
+ return sendJson(res, 200, result);
291
+ }
292
+
293
+ // PUT /EncryptionConfig
294
+ if (req.method === 'PUT' && pathname === '/EncryptionConfig') {
295
+ const result = await simulator.putEncryptionConfig(body);
296
+ return sendJson(res, 200, result);
297
+ }
298
+
299
+ return sendJson(res, 404, { message: `Route not found: ${req.method} ${pathname}` });
300
+ } catch (err) {
301
+ return sendError(res, err);
302
+ }
303
+ });
304
+
305
+ return server;
306
+ }
307
+
308
+ module.exports = { createXRayServer };