-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogrammatic-api.ts
More file actions
153 lines (125 loc) · 4.76 KB
/
Copy pathprogrammatic-api.ts
File metadata and controls
153 lines (125 loc) · 4.76 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
/**
* Programmatic API — start a fauxqs server, create resources, use the SDK,
* inspect queues, purge state, and apply setup configs.
*/
import { startFauxqs } from "fauxqs";
import type { FauxqsServer, FauxqsInitConfig } from "fauxqs";
import { SQSClient, SendMessageCommand, ReceiveMessageCommand, DeleteMessageCommand } from "@aws-sdk/client-sqs";
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
import { S3Client, PutObjectCommand, GetObjectCommand } from "@aws-sdk/client-s3";
async function main() {
// Start the server on a random port with logging disabled
const server: FauxqsServer = await startFauxqs({
port: 0,
logger: false,
});
const endpoint = `http://127.0.0.1:${server.port}`;
const credentials = { accessKeyId: "test", secretAccessKey: "test" };
const region = "us-east-1";
// --- Create resources programmatically (no SDK calls needed) ---
server.createQueue("my-queue");
server.createQueue("my-dlq");
// Queue with custom attributes and a DLQ
server.createQueue("my-worker-queue", {
attributes: {
VisibilityTimeout: "60",
RedrivePolicy: JSON.stringify({
deadLetterTargetArn: "arn:aws:sqs:us-east-1:000000000000:my-dlq",
maxReceiveCount: "3",
}),
},
tags: { environment: "dev" },
});
server.createTopic("my-topic");
server.createTopic("my-tagged-topic", {
tags: { team: "platform" },
});
// Subscribe a queue to a topic
server.subscribe({ topic: "my-topic", queue: "my-queue" });
// Subscribe with filter policy
server.subscribe({
topic: "my-topic",
queue: "my-worker-queue",
attributes: {
FilterPolicy: JSON.stringify({ eventType: ["order.created"] }),
},
});
server.createBucket("my-bucket");
// --- Use the AWS SDK clients ---
const sqsClient = new SQSClient({ endpoint, region, credentials });
const snsClient = new SNSClient({ endpoint, region, credentials });
const s3Client = new S3Client({ endpoint, region, credentials, forcePathStyle: true });
// SQS: Send and receive a message
const queueUrl = `http://sqs.us-east-1.127.0.0.1:${server.port}/000000000000/my-queue`;
await sqsClient.send(new SendMessageCommand({
QueueUrl: queueUrl,
MessageBody: JSON.stringify({ orderId: "123", action: "process" }),
}));
const receiveResult = await sqsClient.send(new ReceiveMessageCommand({
QueueUrl: queueUrl,
MaxNumberOfMessages: 1,
WaitTimeSeconds: 1,
}));
if (receiveResult.Messages?.[0]) {
const msg = receiveResult.Messages[0];
console.log("Received:", msg.Body);
await sqsClient.send(new DeleteMessageCommand({
QueueUrl: queueUrl,
ReceiptHandle: msg.ReceiptHandle!,
}));
}
// SNS: Publish to a topic (fans out to subscribed queues)
await snsClient.send(new PublishCommand({
TopicArn: "arn:aws:sns:us-east-1:000000000000:my-topic",
Message: JSON.stringify({ orderId: "456" }),
MessageAttributes: {
eventType: { DataType: "String", StringValue: "order.created" },
},
}));
// S3: Upload and download an object
await s3Client.send(new PutObjectCommand({
Bucket: "my-bucket",
Key: "data/report.json",
Body: Buffer.from(JSON.stringify({ total: 42 })),
ContentType: "application/json",
}));
const getResult = await s3Client.send(new GetObjectCommand({
Bucket: "my-bucket",
Key: "data/report.json",
}));
const body = await getResult.Body?.transformToString();
console.log("S3 object:", body);
// --- Inspect queue state (non-destructive) ---
const inspection = server.inspectQueue("my-queue");
if (inspection) {
console.log("Queue:", inspection.name);
console.log("URL:", inspection.url);
console.log("ARN:", inspection.arn);
console.log("Ready messages:", inspection.messages.ready.length);
console.log("Delayed messages:", inspection.messages.delayed.length);
console.log("Inflight messages:", inspection.messages.inflight.length);
// Access message details
for (const msg of inspection.messages.ready) {
console.log(` - ${msg.messageId}: ${msg.body}`);
}
for (const entry of inspection.messages.inflight) {
console.log(` - inflight: ${entry.message.messageId}, receipt: ${entry.receiptHandle}`);
}
}
// --- Apply a setup config (idempotent) ---
const config: FauxqsInitConfig = {
queues: [
{ name: "another-queue" },
{ name: "my-queue" }, // Already exists — skipped, not an error
],
topics: [{ name: "another-topic" }],
subscriptions: [{ topic: "another-topic", queue: "another-queue" }],
buckets: ["another-bucket"],
};
server.setup(config);
// --- Purge all state ---
server.purgeAll(); // Clears all queues, topics, subscriptions, and buckets
// --- Stop the server ---
await server.stop();
}
main().catch(console.error);