-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit-config.ts
More file actions
197 lines (166 loc) · 5.78 KB
/
init-config.ts
File metadata and controls
197 lines (166 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Init Config — pre-create queues, topics, subscriptions, and buckets
* on server startup using file-based or inline configuration.
*/
import { startFauxqs } from "fauxqs";
import type { FauxqsInitConfig } from "fauxqs";
import { SQSClient, SendMessageCommand, ReceiveMessageCommand } from "@aws-sdk/client-sqs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
const __dirname = dirname(fileURLToPath(import.meta.url));
async function fileBasedInit() {
// ---------------------------------------------------------------
// File-based init — point to a JSON file
// ---------------------------------------------------------------
// Pass a path to a JSON file (see ../config/init.json)
const server = await startFauxqs({
port: 0,
logger: false,
init: join(__dirname, "..", "config", "init.json"),
});
// All resources from init.json are now available
const endpoint = `http://127.0.0.1:${server.port}`;
const credentials = { accessKeyId: "test", secretAccessKey: "test" };
const region = "us-east-1";
const sqsClient = new SQSClient({ endpoint, region, credentials });
// The "orders" queue from init.json is ready to use
const queueUrl = `http://sqs.us-east-1.127.0.0.1:${server.port}/000000000000/orders`;
await sqsClient.send(new SendMessageCommand({
QueueUrl: queueUrl,
MessageBody: JSON.stringify({ orderId: "1001" }),
}));
const result = await sqsClient.send(new ReceiveMessageCommand({
QueueUrl: queueUrl,
MaxNumberOfMessages: 1,
WaitTimeSeconds: 1,
}));
console.log("Received from init'd queue:", result.Messages?.[0]?.Body);
await server.stop();
}
async function inlineInit() {
// ---------------------------------------------------------------
// Inline init config — pass the config object directly
// ---------------------------------------------------------------
const config: FauxqsInitConfig = {
region: "us-west-2",
queues: [
{ name: "payments" },
{ name: "payments-dlq" },
{
name: "payments-processor",
attributes: {
VisibilityTimeout: "120",
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: "arn:aws:sqs:us-west-2:000000000000:payments-dlq",
maxReceiveCount: "5",
}),
},
tags: { service: "payments" },
},
],
topics: [
{ name: "payment-events" },
],
subscriptions: [
{
topic: "payment-events",
queue: "payments",
attributes: {
FilterPolicy: JSON.stringify({
eventType: ["payment.completed", "payment.refunded"],
}),
},
},
{
topic: "payment-events",
queue: "payments-processor",
attributes: {
RawMessageDelivery: "true",
},
},
],
buckets: ["payment-receipts"],
};
const server = await startFauxqs({
port: 0,
logger: false,
init: config,
});
console.log("Server started with inline config on port", server.port);
await server.stop();
}
async function dlqChainSetup() {
// ---------------------------------------------------------------
// DLQ chain — main queue → DLQ → DLQ-DLQ
// ---------------------------------------------------------------
const config: FauxqsInitConfig = {
queues: [
// DLQ targets must be created before the queues that reference them.
// Init config creates queues in array order, so list targets first.
{ name: "final-dlq" },
{
name: "first-dlq",
attributes: {
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: "arn:aws:sqs:us-east-1:000000000000:final-dlq",
maxReceiveCount: "3",
}),
},
},
{
name: "main-queue",
attributes: {
VisibilityTimeout: "10",
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: "arn:aws:sqs:us-east-1:000000000000:first-dlq",
maxReceiveCount: "2",
}),
},
},
],
};
const server = await startFauxqs({
port: 0,
logger: false,
init: config,
});
console.log("DLQ chain configured");
await server.stop();
}
async function setupIdempotency() {
// ---------------------------------------------------------------
// server.setup() is idempotent — safe to call multiple times
// ---------------------------------------------------------------
const server = await startFauxqs({ port: 0, logger: false });
const config: FauxqsInitConfig = {
queues: [{ name: "idempotent-queue" }],
topics: [{ name: "idempotent-topic" }],
subscriptions: [{ topic: "idempotent-topic", queue: "idempotent-queue" }],
buckets: ["idempotent-bucket"],
};
// First call creates everything
server.setup(config);
// Second call is a no-op — existing resources are skipped
server.setup(config);
// Send a message to prove the queue works
const endpoint = `http://127.0.0.1:${server.port}`;
const sqsClient = new SQSClient({ endpoint, region: "us-east-1", credentials: { accessKeyId: "test", secretAccessKey: "test" } });
await sqsClient.send(new SendMessageCommand({
QueueUrl: `http://sqs.us-east-1.127.0.0.1:${server.port}/000000000000/idempotent-queue`,
MessageBody: "still works after double setup",
}));
// ---------------------------------------------------------------
// Purge + re-apply pattern (useful between test suites)
// ---------------------------------------------------------------
server.purgeAll(); // Clears all state
server.setup(config); // Re-creates resources from scratch
console.log("Purge + re-apply complete");
await server.stop();
}
async function main() {
await fileBasedInit();
await inlineInit();
await dlqChainSetup();
await setupIdempotency();
}
main().catch(console.error);