@gugananuvem/aws-local-simulator 1.0.33 → 1.0.34

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 (79) hide show
  1. package/README.md +834 -834
  2. package/aws-config +153 -153
  3. package/bin/aws-local-simulator.js +63 -63
  4. package/package.json +3 -2
  5. package/src/config/config-loader.js +114 -114
  6. package/src/config/default-config.js +79 -79
  7. package/src/config/env-loader.js +68 -68
  8. package/src/index.js +146 -146
  9. package/src/index.mjs +123 -123
  10. package/src/server.js +463 -463
  11. package/src/services/apigateway/index.js +75 -75
  12. package/src/services/apigateway/server.js +607 -607
  13. package/src/services/apigateway/simulator.js +1405 -1405
  14. package/src/services/athena/index.js +75 -75
  15. package/src/services/athena/server.js +101 -101
  16. package/src/services/athena/simulador.js +998 -998
  17. package/src/services/athena/simulator.js +346 -346
  18. package/src/services/cloudformation/index.js +106 -106
  19. package/src/services/cloudformation/server.js +417 -417
  20. package/src/services/cloudformation/simulador.js +1020 -1020
  21. package/src/services/cloudtrail/index.js +84 -84
  22. package/src/services/cloudtrail/server.js +235 -235
  23. package/src/services/cloudtrail/simulador.js +719 -719
  24. package/src/services/cloudwatch/index.js +84 -84
  25. package/src/services/cloudwatch/server.js +366 -366
  26. package/src/services/cloudwatch/simulador.js +1173 -1173
  27. package/src/services/cognito/index.js +79 -79
  28. package/src/services/cognito/server.js +297 -297
  29. package/src/services/cognito/simulator.js +1992 -1761
  30. package/src/services/config/index.js +96 -96
  31. package/src/services/config/server.js +215 -215
  32. package/src/services/config/simulador.js +1260 -1260
  33. package/src/services/dynamodb/index.js +74 -74
  34. package/src/services/dynamodb/server.js +139 -139
  35. package/src/services/dynamodb/simulator.js +1005 -994
  36. package/src/services/dynamodb/sqlite-store.js +722 -0
  37. package/src/services/ecs/index.js +65 -65
  38. package/src/services/ecs/server.js +235 -235
  39. package/src/services/ecs/simulator.js +844 -844
  40. package/src/services/eventbridge/index.js +89 -89
  41. package/src/services/eventbridge/server.js +209 -209
  42. package/src/services/eventbridge/simulator.js +684 -684
  43. package/src/services/index.js +45 -45
  44. package/src/services/kms/index.js +75 -75
  45. package/src/services/kms/server.js +81 -81
  46. package/src/services/kms/simulator.js +344 -344
  47. package/src/services/lambda/handler-loader.js +183 -183
  48. package/src/services/lambda/index.js +81 -81
  49. package/src/services/lambda/route-registry.js +274 -274
  50. package/src/services/lambda/server.js +191 -191
  51. package/src/services/lambda/simulator.js +364 -364
  52. package/src/services/parameter-store/index.js +80 -80
  53. package/src/services/parameter-store/server.js +50 -50
  54. package/src/services/parameter-store/simulator.js +201 -201
  55. package/src/services/s3/index.js +73 -73
  56. package/src/services/s3/server.js +350 -350
  57. package/src/services/s3/simulator.js +568 -568
  58. package/src/services/secret-manager/index.js +80 -80
  59. package/src/services/secret-manager/server.js +51 -51
  60. package/src/services/secret-manager/simulator.js +182 -182
  61. package/src/services/sns/index.js +89 -89
  62. package/src/services/sns/server.js +607 -607
  63. package/src/services/sns/simulator.js +1482 -1482
  64. package/src/services/sqs/index.js +98 -98
  65. package/src/services/sqs/server.js +360 -360
  66. package/src/services/sqs/simulator.js +509 -509
  67. package/src/services/sts/index.js +37 -37
  68. package/src/services/sts/server.js +144 -144
  69. package/src/services/sts/simulator.js +69 -69
  70. package/src/services/xray/index.js +83 -83
  71. package/src/services/xray/server.js +308 -308
  72. package/src/services/xray/simulador.js +994 -994
  73. package/src/template/aws-config-template.js +87 -87
  74. package/src/template/aws-config-template.mjs +90 -90
  75. package/src/template/config-template.json +203 -203
  76. package/src/utils/aws-config.js +91 -91
  77. package/src/utils/cloudtrail-audit.js +129 -129
  78. package/src/utils/local-store.js +83 -83
  79. package/src/utils/logger.js +59 -59
@@ -1,346 +1,346 @@
1
- 'use strict';
2
-
3
- const { randomUUID } = require('crypto');
4
- const { CloudTrailAudit } = require('../../utils/cloudtrail-audit');
5
-
6
- const ACCOUNT = '000000000000';
7
- const REGION = 'us-east-1';
8
-
9
- /**
10
- * Athena Simulator
11
- * Suporta: StartQueryExecution, GetQueryExecution, GetQueryResults,
12
- * StopQueryExecution, ListQueryExecutions, CreateNamedQuery,
13
- * GetNamedQuery, ListNamedQueries, DeleteNamedQuery,
14
- * CreateWorkGroup, GetWorkGroup, ListWorkGroups, DeleteWorkGroup
15
- */
16
- class AthenaSimulator {
17
- constructor(config, store, logger) {
18
- this.config = config;
19
- this.store = store;
20
- this.logger = logger;
21
-
22
- /** @type {Map<string, Object>} queryExecutionId → execution */
23
- this.queryExecutions = new Map();
24
-
25
- /** @type {Map<string, Object>} namedQueryId → namedQuery */
26
- this.namedQueries = new Map();
27
-
28
- /** @type {Map<string, Object>} workGroupName → workGroup */
29
- this.workGroups = new Map();
30
-
31
- this.audit = new CloudTrailAudit('athena.amazonaws.com');
32
- }
33
-
34
- async initialize() {
35
- // Cria workgroup padrão
36
- if (!this.workGroups.has('primary')) {
37
- this.workGroups.set('primary', {
38
- Name: 'primary',
39
- State: 'ENABLED',
40
- Description: 'Primary workgroup',
41
- CreationTime: new Date().toISOString(),
42
- Configuration: {
43
- ResultConfiguration: { OutputLocation: 's3://aws-athena-query-results-local/' },
44
- EnforceWorkGroupConfiguration: false,
45
- PublishCloudWatchMetricsEnabled: false,
46
- BytesScannedCutoffPerQuery: 0,
47
- RequesterPaysEnabled: false,
48
- },
49
- });
50
- }
51
-
52
- try {
53
- const data = await this.store.load('athena');
54
- if (data) {
55
- if (data.queryExecutions) this.queryExecutions = new Map(Object.entries(data.queryExecutions));
56
- if (data.namedQueries) this.namedQueries = new Map(Object.entries(data.namedQueries));
57
- if (data.workGroups) this.workGroups = new Map(Object.entries(data.workGroups));
58
- this.logger.info(`[Athena] Loaded ${this.queryExecutions.size} executions, ${this.namedQueries.size} named queries`);
59
- }
60
- } catch {
61
- this.logger.debug('[Athena] No persisted data, starting fresh');
62
- }
63
- }
64
-
65
- async _persist() {
66
- try {
67
- await this.store.save('athena', {
68
- queryExecutions: Object.fromEntries(this.queryExecutions),
69
- namedQueries: Object.fromEntries(this.namedQueries),
70
- workGroups: Object.fromEntries(this.workGroups),
71
- });
72
- } catch (err) {
73
- this.logger.warn(`[Athena] Failed to persist: ${err.message}`);
74
- }
75
- }
76
-
77
- // ── Query Execution ──────────────────────────────────────────────────────
78
-
79
- async startQueryExecution(params) {
80
- const {
81
- QueryString,
82
- QueryExecutionContext = {},
83
- ResultConfiguration = {},
84
- WorkGroup = 'primary',
85
- ClientRequestToken,
86
- } = params;
87
-
88
- if (!QueryString) throw this._error('InvalidRequestException', 'QueryString is required');
89
-
90
- const wg = this.workGroups.get(WorkGroup);
91
- if (!wg) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} does not exist`);
92
-
93
- const queryExecutionId = randomUUID();
94
- const outputLocation = ResultConfiguration.OutputLocation ||
95
- wg.Configuration?.ResultConfiguration?.OutputLocation ||
96
- `s3://aws-athena-query-results-local/${queryExecutionId}/`;
97
-
98
- const execution = {
99
- QueryExecutionId: queryExecutionId,
100
- Query: QueryString,
101
- StatementType: this._detectStatementType(QueryString),
102
- ResultConfiguration: { OutputLocation: outputLocation },
103
- QueryExecutionContext,
104
- Status: {
105
- State: 'SUCCEEDED',
106
- SubmissionDateTime: new Date().toISOString(),
107
- CompletionDateTime: new Date().toISOString(),
108
- },
109
- Statistics: {
110
- EngineExecutionTimeInMillis: Math.floor(Math.random() * 500) + 50,
111
- DataScannedInBytes: Math.floor(Math.random() * 1024 * 1024),
112
- TotalExecutionTimeInMillis: Math.floor(Math.random() * 600) + 100,
113
- QueryQueueTimeInMillis: 10,
114
- ServiceProcessingTimeInMillis: 20,
115
- },
116
- WorkGroup,
117
- _results: this._generateResults(QueryString),
118
- };
119
-
120
- this.queryExecutions.set(queryExecutionId, execution);
121
- await this._persist();
122
-
123
- this.audit.record({
124
- eventName: 'StartQueryExecution',
125
- readOnly: false,
126
- resources: [{ ARN: `arn:aws:athena:${REGION}:${ACCOUNT}:workgroup/${WorkGroup}`, type: 'AWS::Athena::WorkGroup' }],
127
- requestParameters: { queryString: QueryString, workGroup: WorkGroup },
128
- });
129
-
130
- this.logger.info(`[Athena] Query started: ${queryExecutionId}`);
131
- return { QueryExecutionId: queryExecutionId };
132
- }
133
-
134
- getQueryExecution({ QueryExecutionId }) {
135
- const exec = this.queryExecutions.get(QueryExecutionId);
136
- if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
137
-
138
- const { _results, ...clean } = exec;
139
- this.audit.record({
140
- eventName: 'GetQueryExecution',
141
- readOnly: true,
142
- requestParameters: { queryExecutionId: QueryExecutionId },
143
- });
144
- return { QueryExecution: clean };
145
- }
146
-
147
- getQueryResults({ QueryExecutionId, MaxResults = 1000, NextToken }) {
148
- const exec = this.queryExecutions.get(QueryExecutionId);
149
- if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
150
- if (exec.Status.State !== 'SUCCEEDED') {
151
- throw this._error('InvalidRequestException', `Query is in state ${exec.Status.State}`);
152
- }
153
-
154
- const rows = exec._results || [];
155
- const startIdx = NextToken ? parseInt(NextToken) : 0;
156
- const slice = rows.slice(startIdx, startIdx + MaxResults);
157
-
158
- this.audit.record({
159
- eventName: 'GetQueryResults',
160
- readOnly: true,
161
- requestParameters: { queryExecutionId: QueryExecutionId },
162
- });
163
-
164
- return {
165
- ResultSet: {
166
- Rows: slice,
167
- ResultSetMetadata: { ColumnInfo: exec._columnInfo || [] },
168
- },
169
- NextToken: rows.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
170
- };
171
- }
172
-
173
- async stopQueryExecution({ QueryExecutionId }) {
174
- const exec = this.queryExecutions.get(QueryExecutionId);
175
- if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
176
-
177
- exec.Status.State = 'CANCELLED';
178
- exec.Status.CompletionDateTime = new Date().toISOString();
179
- await this._persist();
180
-
181
- this.audit.record({ eventName: 'StopQueryExecution', readOnly: false, requestParameters: { queryExecutionId: QueryExecutionId } });
182
- return {};
183
- }
184
-
185
- listQueryExecutions({ MaxResults = 50, NextToken, WorkGroup } = {}) {
186
- let ids = Array.from(this.queryExecutions.keys());
187
- if (WorkGroup) ids = ids.filter(id => this.queryExecutions.get(id).WorkGroup === WorkGroup);
188
-
189
- const startIdx = NextToken ? parseInt(NextToken) : 0;
190
- const slice = ids.slice(startIdx, startIdx + MaxResults);
191
-
192
- return {
193
- QueryExecutionIds: slice,
194
- NextToken: ids.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
195
- };
196
- }
197
-
198
- // ── Named Queries ────────────────────────────────────────────────────────
199
-
200
- async createNamedQuery(params) {
201
- const { Name, Description = '', Database, QueryString, WorkGroup = 'primary' } = params;
202
- if (!Name || !QueryString) throw this._error('InvalidRequestException', 'Name and QueryString are required');
203
-
204
- const namedQueryId = randomUUID();
205
- const query = { NamedQueryId: namedQueryId, Name, Description, Database, QueryString, WorkGroup };
206
- this.namedQueries.set(namedQueryId, query);
207
- await this._persist();
208
-
209
- this.audit.record({ eventName: 'CreateNamedQuery', readOnly: false, requestParameters: { name: Name } });
210
- return { NamedQueryId: namedQueryId };
211
- }
212
-
213
- getNamedQuery({ NamedQueryId }) {
214
- const q = this.namedQueries.get(NamedQueryId);
215
- if (!q) throw this._error('InvalidRequestException', `Named query ${NamedQueryId} not found`);
216
- return { NamedQuery: q };
217
- }
218
-
219
- listNamedQueries({ MaxResults = 50, NextToken, WorkGroup } = {}) {
220
- let ids = Array.from(this.namedQueries.keys());
221
- if (WorkGroup) ids = ids.filter(id => this.namedQueries.get(id).WorkGroup === WorkGroup);
222
-
223
- const startIdx = NextToken ? parseInt(NextToken) : 0;
224
- const slice = ids.slice(startIdx, startIdx + MaxResults);
225
-
226
- return {
227
- NamedQueryIds: slice,
228
- NextToken: ids.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
229
- };
230
- }
231
-
232
- async deleteNamedQuery({ NamedQueryId }) {
233
- if (!this.namedQueries.has(NamedQueryId)) {
234
- throw this._error('InvalidRequestException', `Named query ${NamedQueryId} not found`);
235
- }
236
- this.namedQueries.delete(NamedQueryId);
237
- await this._persist();
238
- this.audit.record({ eventName: 'DeleteNamedQuery', readOnly: false, requestParameters: { namedQueryId: NamedQueryId } });
239
- return {};
240
- }
241
-
242
- // ── WorkGroups ───────────────────────────────────────────────────────────
243
-
244
- async createWorkGroup(params) {
245
- const { Name, Description = '', Configuration = {}, Tags = [] } = params;
246
- if (!Name) throw this._error('InvalidRequestException', 'Name is required');
247
- if (this.workGroups.has(Name)) throw this._error('InvalidRequestException', `WorkGroup ${Name} already exists`);
248
-
249
- const wg = {
250
- Name, Description, State: 'ENABLED', Tags,
251
- CreationTime: new Date().toISOString(),
252
- Configuration: {
253
- ResultConfiguration: Configuration.ResultConfiguration || { OutputLocation: `s3://aws-athena-query-results-local/${Name}/` },
254
- EnforceWorkGroupConfiguration: Configuration.EnforceWorkGroupConfiguration || false,
255
- PublishCloudWatchMetricsEnabled: Configuration.PublishCloudWatchMetricsEnabled || false,
256
- BytesScannedCutoffPerQuery: Configuration.BytesScannedCutoffPerQuery || 0,
257
- RequesterPaysEnabled: Configuration.RequesterPaysEnabled || false,
258
- },
259
- };
260
-
261
- this.workGroups.set(Name, wg);
262
- await this._persist();
263
- this.audit.record({ eventName: 'CreateWorkGroup', readOnly: false, requestParameters: { name: Name } });
264
- return {};
265
- }
266
-
267
- getWorkGroup({ WorkGroup }) {
268
- const wg = this.workGroups.get(WorkGroup);
269
- if (!wg) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} not found`);
270
- return { WorkGroup: wg };
271
- }
272
-
273
- listWorkGroups({ MaxResults = 50, NextToken } = {}) {
274
- const all = Array.from(this.workGroups.values()).map(({ Name, State, Description, CreationTime }) => ({
275
- Name, State, Description, CreationTime,
276
- }));
277
- const startIdx = NextToken ? parseInt(NextToken) : 0;
278
- const slice = all.slice(startIdx, startIdx + MaxResults);
279
- return {
280
- WorkGroups: slice,
281
- NextToken: all.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
282
- };
283
- }
284
-
285
- async deleteWorkGroup({ WorkGroup, RecursiveDeleteOption = false }) {
286
- if (WorkGroup === 'primary') throw this._error('InvalidRequestException', 'Cannot delete primary workgroup');
287
- if (!this.workGroups.has(WorkGroup)) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} not found`);
288
-
289
- if (RecursiveDeleteOption) {
290
- for (const [id, exec] of this.queryExecutions.entries()) {
291
- if (exec.WorkGroup === WorkGroup) this.queryExecutions.delete(id);
292
- }
293
- }
294
-
295
- this.workGroups.delete(WorkGroup);
296
- await this._persist();
297
- this.audit.record({ eventName: 'DeleteWorkGroup', readOnly: false, requestParameters: { workGroup: WorkGroup } });
298
- return {};
299
- }
300
-
301
- // ── Helpers ──────────────────────────────────────────────────────────────
302
-
303
- _detectStatementType(query) {
304
- const q = query.trim().toUpperCase();
305
- if (q.startsWith('SELECT')) return 'DML';
306
- if (q.startsWith('CREATE') || q.startsWith('DROP') || q.startsWith('ALTER')) return 'DDL';
307
- if (q.startsWith('INSERT') || q.startsWith('UPDATE') || q.startsWith('DELETE')) return 'DML';
308
- return 'UTILITY';
309
- }
310
-
311
- _generateResults(query) {
312
- const q = query.trim().toUpperCase();
313
- if (!q.startsWith('SELECT')) return [{ Data: [{ VarCharValue: 'OK' }] }];
314
-
315
- // Gera resultado simulado com header + 2 linhas de exemplo
316
- return [
317
- { Data: [{ VarCharValue: 'id' }, { VarCharValue: 'value' }, { VarCharValue: 'timestamp' }] },
318
- { Data: [{ VarCharValue: '1' }, { VarCharValue: 'sample-data-1' }, { VarCharValue: new Date().toISOString() }] },
319
- { Data: [{ VarCharValue: '2' }, { VarCharValue: 'sample-data-2' }, { VarCharValue: new Date().toISOString() }] },
320
- ];
321
- }
322
-
323
- _error(code, message) {
324
- const err = new Error(message);
325
- err.code = code;
326
- err.statusCode = 400;
327
- return err;
328
- }
329
-
330
- getStats() {
331
- return {
332
- queryExecutions: this.queryExecutions.size,
333
- namedQueries: this.namedQueries.size,
334
- workGroups: this.workGroups.size,
335
- };
336
- }
337
-
338
- async reset() {
339
- this.queryExecutions.clear();
340
- this.namedQueries.clear();
341
- this.workGroups.clear();
342
- await this._persist();
343
- }
344
- }
345
-
346
- module.exports = { AthenaSimulator };
1
+ 'use strict';
2
+
3
+ const { randomUUID } = require('crypto');
4
+ const { CloudTrailAudit } = require('../../utils/cloudtrail-audit');
5
+
6
+ const ACCOUNT = '000000000000';
7
+ const REGION = 'us-east-1';
8
+
9
+ /**
10
+ * Athena Simulator
11
+ * Suporta: StartQueryExecution, GetQueryExecution, GetQueryResults,
12
+ * StopQueryExecution, ListQueryExecutions, CreateNamedQuery,
13
+ * GetNamedQuery, ListNamedQueries, DeleteNamedQuery,
14
+ * CreateWorkGroup, GetWorkGroup, ListWorkGroups, DeleteWorkGroup
15
+ */
16
+ class AthenaSimulator {
17
+ constructor(config, store, logger) {
18
+ this.config = config;
19
+ this.store = store;
20
+ this.logger = logger;
21
+
22
+ /** @type {Map<string, Object>} queryExecutionId → execution */
23
+ this.queryExecutions = new Map();
24
+
25
+ /** @type {Map<string, Object>} namedQueryId → namedQuery */
26
+ this.namedQueries = new Map();
27
+
28
+ /** @type {Map<string, Object>} workGroupName → workGroup */
29
+ this.workGroups = new Map();
30
+
31
+ this.audit = new CloudTrailAudit('athena.amazonaws.com');
32
+ }
33
+
34
+ async initialize() {
35
+ // Cria workgroup padrão
36
+ if (!this.workGroups.has('primary')) {
37
+ this.workGroups.set('primary', {
38
+ Name: 'primary',
39
+ State: 'ENABLED',
40
+ Description: 'Primary workgroup',
41
+ CreationTime: new Date().toISOString(),
42
+ Configuration: {
43
+ ResultConfiguration: { OutputLocation: 's3://aws-athena-query-results-local/' },
44
+ EnforceWorkGroupConfiguration: false,
45
+ PublishCloudWatchMetricsEnabled: false,
46
+ BytesScannedCutoffPerQuery: 0,
47
+ RequesterPaysEnabled: false,
48
+ },
49
+ });
50
+ }
51
+
52
+ try {
53
+ const data = await this.store.load('athena');
54
+ if (data) {
55
+ if (data.queryExecutions) this.queryExecutions = new Map(Object.entries(data.queryExecutions));
56
+ if (data.namedQueries) this.namedQueries = new Map(Object.entries(data.namedQueries));
57
+ if (data.workGroups) this.workGroups = new Map(Object.entries(data.workGroups));
58
+ this.logger.info(`[Athena] Loaded ${this.queryExecutions.size} executions, ${this.namedQueries.size} named queries`);
59
+ }
60
+ } catch {
61
+ this.logger.debug('[Athena] No persisted data, starting fresh');
62
+ }
63
+ }
64
+
65
+ async _persist() {
66
+ try {
67
+ await this.store.save('athena', {
68
+ queryExecutions: Object.fromEntries(this.queryExecutions),
69
+ namedQueries: Object.fromEntries(this.namedQueries),
70
+ workGroups: Object.fromEntries(this.workGroups),
71
+ });
72
+ } catch (err) {
73
+ this.logger.warn(`[Athena] Failed to persist: ${err.message}`);
74
+ }
75
+ }
76
+
77
+ // ── Query Execution ──────────────────────────────────────────────────────
78
+
79
+ async startQueryExecution(params) {
80
+ const {
81
+ QueryString,
82
+ QueryExecutionContext = {},
83
+ ResultConfiguration = {},
84
+ WorkGroup = 'primary',
85
+ ClientRequestToken,
86
+ } = params;
87
+
88
+ if (!QueryString) throw this._error('InvalidRequestException', 'QueryString is required');
89
+
90
+ const wg = this.workGroups.get(WorkGroup);
91
+ if (!wg) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} does not exist`);
92
+
93
+ const queryExecutionId = randomUUID();
94
+ const outputLocation = ResultConfiguration.OutputLocation ||
95
+ wg.Configuration?.ResultConfiguration?.OutputLocation ||
96
+ `s3://aws-athena-query-results-local/${queryExecutionId}/`;
97
+
98
+ const execution = {
99
+ QueryExecutionId: queryExecutionId,
100
+ Query: QueryString,
101
+ StatementType: this._detectStatementType(QueryString),
102
+ ResultConfiguration: { OutputLocation: outputLocation },
103
+ QueryExecutionContext,
104
+ Status: {
105
+ State: 'SUCCEEDED',
106
+ SubmissionDateTime: new Date().toISOString(),
107
+ CompletionDateTime: new Date().toISOString(),
108
+ },
109
+ Statistics: {
110
+ EngineExecutionTimeInMillis: Math.floor(Math.random() * 500) + 50,
111
+ DataScannedInBytes: Math.floor(Math.random() * 1024 * 1024),
112
+ TotalExecutionTimeInMillis: Math.floor(Math.random() * 600) + 100,
113
+ QueryQueueTimeInMillis: 10,
114
+ ServiceProcessingTimeInMillis: 20,
115
+ },
116
+ WorkGroup,
117
+ _results: this._generateResults(QueryString),
118
+ };
119
+
120
+ this.queryExecutions.set(queryExecutionId, execution);
121
+ await this._persist();
122
+
123
+ this.audit.record({
124
+ eventName: 'StartQueryExecution',
125
+ readOnly: false,
126
+ resources: [{ ARN: `arn:aws:athena:${REGION}:${ACCOUNT}:workgroup/${WorkGroup}`, type: 'AWS::Athena::WorkGroup' }],
127
+ requestParameters: { queryString: QueryString, workGroup: WorkGroup },
128
+ });
129
+
130
+ this.logger.info(`[Athena] Query started: ${queryExecutionId}`);
131
+ return { QueryExecutionId: queryExecutionId };
132
+ }
133
+
134
+ getQueryExecution({ QueryExecutionId }) {
135
+ const exec = this.queryExecutions.get(QueryExecutionId);
136
+ if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
137
+
138
+ const { _results, ...clean } = exec;
139
+ this.audit.record({
140
+ eventName: 'GetQueryExecution',
141
+ readOnly: true,
142
+ requestParameters: { queryExecutionId: QueryExecutionId },
143
+ });
144
+ return { QueryExecution: clean };
145
+ }
146
+
147
+ getQueryResults({ QueryExecutionId, MaxResults = 1000, NextToken }) {
148
+ const exec = this.queryExecutions.get(QueryExecutionId);
149
+ if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
150
+ if (exec.Status.State !== 'SUCCEEDED') {
151
+ throw this._error('InvalidRequestException', `Query is in state ${exec.Status.State}`);
152
+ }
153
+
154
+ const rows = exec._results || [];
155
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
156
+ const slice = rows.slice(startIdx, startIdx + MaxResults);
157
+
158
+ this.audit.record({
159
+ eventName: 'GetQueryResults',
160
+ readOnly: true,
161
+ requestParameters: { queryExecutionId: QueryExecutionId },
162
+ });
163
+
164
+ return {
165
+ ResultSet: {
166
+ Rows: slice,
167
+ ResultSetMetadata: { ColumnInfo: exec._columnInfo || [] },
168
+ },
169
+ NextToken: rows.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
170
+ };
171
+ }
172
+
173
+ async stopQueryExecution({ QueryExecutionId }) {
174
+ const exec = this.queryExecutions.get(QueryExecutionId);
175
+ if (!exec) throw this._error('InvalidRequestException', `Query execution ${QueryExecutionId} not found`);
176
+
177
+ exec.Status.State = 'CANCELLED';
178
+ exec.Status.CompletionDateTime = new Date().toISOString();
179
+ await this._persist();
180
+
181
+ this.audit.record({ eventName: 'StopQueryExecution', readOnly: false, requestParameters: { queryExecutionId: QueryExecutionId } });
182
+ return {};
183
+ }
184
+
185
+ listQueryExecutions({ MaxResults = 50, NextToken, WorkGroup } = {}) {
186
+ let ids = Array.from(this.queryExecutions.keys());
187
+ if (WorkGroup) ids = ids.filter(id => this.queryExecutions.get(id).WorkGroup === WorkGroup);
188
+
189
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
190
+ const slice = ids.slice(startIdx, startIdx + MaxResults);
191
+
192
+ return {
193
+ QueryExecutionIds: slice,
194
+ NextToken: ids.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
195
+ };
196
+ }
197
+
198
+ // ── Named Queries ────────────────────────────────────────────────────────
199
+
200
+ async createNamedQuery(params) {
201
+ const { Name, Description = '', Database, QueryString, WorkGroup = 'primary' } = params;
202
+ if (!Name || !QueryString) throw this._error('InvalidRequestException', 'Name and QueryString are required');
203
+
204
+ const namedQueryId = randomUUID();
205
+ const query = { NamedQueryId: namedQueryId, Name, Description, Database, QueryString, WorkGroup };
206
+ this.namedQueries.set(namedQueryId, query);
207
+ await this._persist();
208
+
209
+ this.audit.record({ eventName: 'CreateNamedQuery', readOnly: false, requestParameters: { name: Name } });
210
+ return { NamedQueryId: namedQueryId };
211
+ }
212
+
213
+ getNamedQuery({ NamedQueryId }) {
214
+ const q = this.namedQueries.get(NamedQueryId);
215
+ if (!q) throw this._error('InvalidRequestException', `Named query ${NamedQueryId} not found`);
216
+ return { NamedQuery: q };
217
+ }
218
+
219
+ listNamedQueries({ MaxResults = 50, NextToken, WorkGroup } = {}) {
220
+ let ids = Array.from(this.namedQueries.keys());
221
+ if (WorkGroup) ids = ids.filter(id => this.namedQueries.get(id).WorkGroup === WorkGroup);
222
+
223
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
224
+ const slice = ids.slice(startIdx, startIdx + MaxResults);
225
+
226
+ return {
227
+ NamedQueryIds: slice,
228
+ NextToken: ids.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
229
+ };
230
+ }
231
+
232
+ async deleteNamedQuery({ NamedQueryId }) {
233
+ if (!this.namedQueries.has(NamedQueryId)) {
234
+ throw this._error('InvalidRequestException', `Named query ${NamedQueryId} not found`);
235
+ }
236
+ this.namedQueries.delete(NamedQueryId);
237
+ await this._persist();
238
+ this.audit.record({ eventName: 'DeleteNamedQuery', readOnly: false, requestParameters: { namedQueryId: NamedQueryId } });
239
+ return {};
240
+ }
241
+
242
+ // ── WorkGroups ───────────────────────────────────────────────────────────
243
+
244
+ async createWorkGroup(params) {
245
+ const { Name, Description = '', Configuration = {}, Tags = [] } = params;
246
+ if (!Name) throw this._error('InvalidRequestException', 'Name is required');
247
+ if (this.workGroups.has(Name)) throw this._error('InvalidRequestException', `WorkGroup ${Name} already exists`);
248
+
249
+ const wg = {
250
+ Name, Description, State: 'ENABLED', Tags,
251
+ CreationTime: new Date().toISOString(),
252
+ Configuration: {
253
+ ResultConfiguration: Configuration.ResultConfiguration || { OutputLocation: `s3://aws-athena-query-results-local/${Name}/` },
254
+ EnforceWorkGroupConfiguration: Configuration.EnforceWorkGroupConfiguration || false,
255
+ PublishCloudWatchMetricsEnabled: Configuration.PublishCloudWatchMetricsEnabled || false,
256
+ BytesScannedCutoffPerQuery: Configuration.BytesScannedCutoffPerQuery || 0,
257
+ RequesterPaysEnabled: Configuration.RequesterPaysEnabled || false,
258
+ },
259
+ };
260
+
261
+ this.workGroups.set(Name, wg);
262
+ await this._persist();
263
+ this.audit.record({ eventName: 'CreateWorkGroup', readOnly: false, requestParameters: { name: Name } });
264
+ return {};
265
+ }
266
+
267
+ getWorkGroup({ WorkGroup }) {
268
+ const wg = this.workGroups.get(WorkGroup);
269
+ if (!wg) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} not found`);
270
+ return { WorkGroup: wg };
271
+ }
272
+
273
+ listWorkGroups({ MaxResults = 50, NextToken } = {}) {
274
+ const all = Array.from(this.workGroups.values()).map(({ Name, State, Description, CreationTime }) => ({
275
+ Name, State, Description, CreationTime,
276
+ }));
277
+ const startIdx = NextToken ? parseInt(NextToken) : 0;
278
+ const slice = all.slice(startIdx, startIdx + MaxResults);
279
+ return {
280
+ WorkGroups: slice,
281
+ NextToken: all.length > startIdx + MaxResults ? String(startIdx + MaxResults) : undefined,
282
+ };
283
+ }
284
+
285
+ async deleteWorkGroup({ WorkGroup, RecursiveDeleteOption = false }) {
286
+ if (WorkGroup === 'primary') throw this._error('InvalidRequestException', 'Cannot delete primary workgroup');
287
+ if (!this.workGroups.has(WorkGroup)) throw this._error('InvalidRequestException', `WorkGroup ${WorkGroup} not found`);
288
+
289
+ if (RecursiveDeleteOption) {
290
+ for (const [id, exec] of this.queryExecutions.entries()) {
291
+ if (exec.WorkGroup === WorkGroup) this.queryExecutions.delete(id);
292
+ }
293
+ }
294
+
295
+ this.workGroups.delete(WorkGroup);
296
+ await this._persist();
297
+ this.audit.record({ eventName: 'DeleteWorkGroup', readOnly: false, requestParameters: { workGroup: WorkGroup } });
298
+ return {};
299
+ }
300
+
301
+ // ── Helpers ──────────────────────────────────────────────────────────────
302
+
303
+ _detectStatementType(query) {
304
+ const q = query.trim().toUpperCase();
305
+ if (q.startsWith('SELECT')) return 'DML';
306
+ if (q.startsWith('CREATE') || q.startsWith('DROP') || q.startsWith('ALTER')) return 'DDL';
307
+ if (q.startsWith('INSERT') || q.startsWith('UPDATE') || q.startsWith('DELETE')) return 'DML';
308
+ return 'UTILITY';
309
+ }
310
+
311
+ _generateResults(query) {
312
+ const q = query.trim().toUpperCase();
313
+ if (!q.startsWith('SELECT')) return [{ Data: [{ VarCharValue: 'OK' }] }];
314
+
315
+ // Gera resultado simulado com header + 2 linhas de exemplo
316
+ return [
317
+ { Data: [{ VarCharValue: 'id' }, { VarCharValue: 'value' }, { VarCharValue: 'timestamp' }] },
318
+ { Data: [{ VarCharValue: '1' }, { VarCharValue: 'sample-data-1' }, { VarCharValue: new Date().toISOString() }] },
319
+ { Data: [{ VarCharValue: '2' }, { VarCharValue: 'sample-data-2' }, { VarCharValue: new Date().toISOString() }] },
320
+ ];
321
+ }
322
+
323
+ _error(code, message) {
324
+ const err = new Error(message);
325
+ err.code = code;
326
+ err.statusCode = 400;
327
+ return err;
328
+ }
329
+
330
+ getStats() {
331
+ return {
332
+ queryExecutions: this.queryExecutions.size,
333
+ namedQueries: this.namedQueries.size,
334
+ workGroups: this.workGroups.size,
335
+ };
336
+ }
337
+
338
+ async reset() {
339
+ this.queryExecutions.clear();
340
+ this.namedQueries.clear();
341
+ this.workGroups.clear();
342
+ await this._persist();
343
+ }
344
+ }
345
+
346
+ module.exports = { AthenaSimulator };