@geekmidas/events 0.0.1

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 (211) hide show
  1. package/README.md +473 -0
  2. package/dist/BasicConnection-BNi6_olg.mjs +42 -0
  3. package/dist/BasicConnection-BNi6_olg.mjs.map +1 -0
  4. package/dist/BasicConnection-C4_Y0nIj.d.mts +26 -0
  5. package/dist/BasicConnection-CddnULuT.d.cts +26 -0
  6. package/dist/BasicConnection-CzfMubv_.cjs +48 -0
  7. package/dist/BasicConnection-CzfMubv_.cjs.map +1 -0
  8. package/dist/BasicPublisher-BzfzHIfb.cjs +20 -0
  9. package/dist/BasicPublisher-BzfzHIfb.cjs.map +1 -0
  10. package/dist/BasicPublisher-Cg3SxIVR.mjs +14 -0
  11. package/dist/BasicPublisher-Cg3SxIVR.mjs.map +1 -0
  12. package/dist/BasicPublisher-DCBxaTwC.d.mts +12 -0
  13. package/dist/BasicPublisher-V5hELHG9.d.cts +12 -0
  14. package/dist/BasicSubscriber-BB3aKeph.d.mts +12 -0
  15. package/dist/BasicSubscriber-BEv5N21f.mjs +18 -0
  16. package/dist/BasicSubscriber-BEv5N21f.mjs.map +1 -0
  17. package/dist/BasicSubscriber-DOwgFw5j.d.cts +12 -0
  18. package/dist/BasicSubscriber-DyTKOixe.cjs +24 -0
  19. package/dist/BasicSubscriber-DyTKOixe.cjs.map +1 -0
  20. package/dist/EventConnection-ClLMoIUU.mjs +36 -0
  21. package/dist/EventConnection-ClLMoIUU.mjs.map +1 -0
  22. package/dist/EventConnection-DZhn1Msl.d.cts +13 -0
  23. package/dist/EventConnection-DeXfLGom.d.mts +13 -0
  24. package/dist/EventConnection-F_N81SXu.cjs +41 -0
  25. package/dist/EventConnection-F_N81SXu.cjs.map +1 -0
  26. package/dist/EventConnection.cjs +5 -0
  27. package/dist/EventConnection.d.cts +3 -0
  28. package/dist/EventConnection.d.mts +3 -0
  29. package/dist/EventConnection.mjs +4 -0
  30. package/dist/Publisher-C3m8JFXm.d.mts +19 -0
  31. package/dist/Publisher-DS0aOh8r.mjs +65 -0
  32. package/dist/Publisher-DS0aOh8r.mjs.map +1 -0
  33. package/dist/Publisher-ceu6OxCj.cjs +70 -0
  34. package/dist/Publisher-ceu6OxCj.cjs.map +1 -0
  35. package/dist/Publisher-nXNVjL0e.d.cts +19 -0
  36. package/dist/Publisher.cjs +4 -0
  37. package/dist/Publisher.d.cts +3 -0
  38. package/dist/Publisher.d.mts +3 -0
  39. package/dist/Publisher.mjs +4 -0
  40. package/dist/RabbitMQConnection-7cWp_-_Q.d.cts +43 -0
  41. package/dist/RabbitMQConnection-BN_GybOv.cjs +113 -0
  42. package/dist/RabbitMQConnection-BN_GybOv.cjs.map +1 -0
  43. package/dist/RabbitMQConnection-C2HC7J7K.d.mts +43 -0
  44. package/dist/RabbitMQConnection-eVOVS4Dl.mjs +107 -0
  45. package/dist/RabbitMQConnection-eVOVS4Dl.mjs.map +1 -0
  46. package/dist/RabbitMQPublisher-BZX0edgL.mjs +39 -0
  47. package/dist/RabbitMQPublisher-BZX0edgL.mjs.map +1 -0
  48. package/dist/RabbitMQPublisher-CSCn6gMA.cjs +45 -0
  49. package/dist/RabbitMQPublisher-CSCn6gMA.cjs.map +1 -0
  50. package/dist/RabbitMQPublisher-CpcbgrXj.d.cts +23 -0
  51. package/dist/RabbitMQPublisher-DDZi15Ro.d.mts +23 -0
  52. package/dist/RabbitMQSubscriber-B5I-hBaT.mjs +57 -0
  53. package/dist/RabbitMQSubscriber-B5I-hBaT.mjs.map +1 -0
  54. package/dist/RabbitMQSubscriber-BJ9PTGny.d.cts +25 -0
  55. package/dist/RabbitMQSubscriber-CIvIw8MG.d.mts +25 -0
  56. package/dist/RabbitMQSubscriber-Cj7qLIi6.cjs +63 -0
  57. package/dist/RabbitMQSubscriber-Cj7qLIi6.cjs.map +1 -0
  58. package/dist/SNSConnection-CAImaMM2.d.mts +40 -0
  59. package/dist/SNSConnection-Cqbr7-Ho.d.cts +40 -0
  60. package/dist/SNSConnection-D7wCf9Si.mjs +70 -0
  61. package/dist/SNSConnection-D7wCf9Si.mjs.map +1 -0
  62. package/dist/SNSConnection-DVsC_pbB.cjs +76 -0
  63. package/dist/SNSConnection-DVsC_pbB.cjs.map +1 -0
  64. package/dist/SNSPublisher-BZ6N3JI9.cjs +48 -0
  65. package/dist/SNSPublisher-BZ6N3JI9.cjs.map +1 -0
  66. package/dist/SNSPublisher-BfWjzNiB.mjs +42 -0
  67. package/dist/SNSPublisher-BfWjzNiB.mjs.map +1 -0
  68. package/dist/SNSPublisher-Dkhb4LU-.d.mts +21 -0
  69. package/dist/SNSPublisher-RVZFa7kL.d.cts +21 -0
  70. package/dist/SNSSubscriber-B31JmkAE.d.mts +54 -0
  71. package/dist/SNSSubscriber-C9YJWa7m.mjs +143 -0
  72. package/dist/SNSSubscriber-C9YJWa7m.mjs.map +1 -0
  73. package/dist/SNSSubscriber-UE0ia4XM.cjs +149 -0
  74. package/dist/SNSSubscriber-UE0ia4XM.cjs.map +1 -0
  75. package/dist/SNSSubscriber-k2UwKUxu.d.cts +54 -0
  76. package/dist/SQSConnection-BwNjUPr4.cjs +76 -0
  77. package/dist/SQSConnection-BwNjUPr4.cjs.map +1 -0
  78. package/dist/SQSConnection-C5WFqZW7.d.mts +40 -0
  79. package/dist/SQSConnection-Eleqqre3.mjs +70 -0
  80. package/dist/SQSConnection-Eleqqre3.mjs.map +1 -0
  81. package/dist/SQSConnection-geav2Rsn.d.cts +40 -0
  82. package/dist/SQSPublisher-CdsayOLV.d.mts +23 -0
  83. package/dist/SQSPublisher-DG9o4ME3.mjs +57 -0
  84. package/dist/SQSPublisher-DG9o4ME3.mjs.map +1 -0
  85. package/dist/SQSPublisher-Dq5353oZ.cjs +63 -0
  86. package/dist/SQSPublisher-Dq5353oZ.cjs.map +1 -0
  87. package/dist/SQSPublisher-Utb_FTe5.d.cts +23 -0
  88. package/dist/SQSSubscriber-B6iL1EnX.d.cts +43 -0
  89. package/dist/SQSSubscriber-CD66P7hH.mjs +121 -0
  90. package/dist/SQSSubscriber-CD66P7hH.mjs.map +1 -0
  91. package/dist/SQSSubscriber-DBeE4XAR.d.mts +43 -0
  92. package/dist/SQSSubscriber-DZDNgLE5.cjs +127 -0
  93. package/dist/SQSSubscriber-DZDNgLE5.cjs.map +1 -0
  94. package/dist/Subscriber-Bd9hb_jp.mjs +74 -0
  95. package/dist/Subscriber-Bd9hb_jp.mjs.map +1 -0
  96. package/dist/Subscriber-Cff9EIpv.d.mts +19 -0
  97. package/dist/Subscriber-CpAwcUzb.d.cts +19 -0
  98. package/dist/Subscriber-saVOWuMq.cjs +79 -0
  99. package/dist/Subscriber-saVOWuMq.cjs.map +1 -0
  100. package/dist/Subscriber.cjs +4 -0
  101. package/dist/Subscriber.d.cts +3 -0
  102. package/dist/Subscriber.d.mts +3 -0
  103. package/dist/Subscriber.mjs +4 -0
  104. package/dist/basic/BasicConnection.cjs +4 -0
  105. package/dist/basic/BasicConnection.d.cts +3 -0
  106. package/dist/basic/BasicConnection.d.mts +3 -0
  107. package/dist/basic/BasicConnection.mjs +4 -0
  108. package/dist/basic/BasicPublisher.cjs +3 -0
  109. package/dist/basic/BasicPublisher.d.cts +4 -0
  110. package/dist/basic/BasicPublisher.d.mts +4 -0
  111. package/dist/basic/BasicPublisher.mjs +3 -0
  112. package/dist/basic/BasicSubscriber.cjs +3 -0
  113. package/dist/basic/BasicSubscriber.d.cts +4 -0
  114. package/dist/basic/BasicSubscriber.d.mts +4 -0
  115. package/dist/basic/BasicSubscriber.mjs +3 -0
  116. package/dist/basic/index.cjs +8 -0
  117. package/dist/basic/index.d.cts +5 -0
  118. package/dist/basic/index.d.mts +5 -0
  119. package/dist/basic/index.mjs +6 -0
  120. package/dist/chunk-CUT6urMc.cjs +30 -0
  121. package/dist/index.cjs +9 -0
  122. package/dist/index.d.cts +5 -0
  123. package/dist/index.d.mts +5 -0
  124. package/dist/index.mjs +6 -0
  125. package/dist/rabbitmq/RabbitMQConnection.cjs +4 -0
  126. package/dist/rabbitmq/RabbitMQConnection.d.cts +3 -0
  127. package/dist/rabbitmq/RabbitMQConnection.d.mts +3 -0
  128. package/dist/rabbitmq/RabbitMQConnection.mjs +4 -0
  129. package/dist/rabbitmq/RabbitMQPublisher.cjs +3 -0
  130. package/dist/rabbitmq/RabbitMQPublisher.d.cts +4 -0
  131. package/dist/rabbitmq/RabbitMQPublisher.d.mts +4 -0
  132. package/dist/rabbitmq/RabbitMQPublisher.mjs +3 -0
  133. package/dist/rabbitmq/RabbitMQSubscriber.cjs +3 -0
  134. package/dist/rabbitmq/RabbitMQSubscriber.d.cts +4 -0
  135. package/dist/rabbitmq/RabbitMQSubscriber.d.mts +4 -0
  136. package/dist/rabbitmq/RabbitMQSubscriber.mjs +3 -0
  137. package/dist/rabbitmq/index.cjs +8 -0
  138. package/dist/rabbitmq/index.d.cts +5 -0
  139. package/dist/rabbitmq/index.d.mts +5 -0
  140. package/dist/rabbitmq/index.mjs +6 -0
  141. package/dist/sns/SNSConnection.cjs +4 -0
  142. package/dist/sns/SNSConnection.d.cts +3 -0
  143. package/dist/sns/SNSConnection.d.mts +3 -0
  144. package/dist/sns/SNSConnection.mjs +4 -0
  145. package/dist/sns/SNSPublisher.cjs +3 -0
  146. package/dist/sns/SNSPublisher.d.cts +4 -0
  147. package/dist/sns/SNSPublisher.d.mts +4 -0
  148. package/dist/sns/SNSPublisher.mjs +3 -0
  149. package/dist/sns/SNSSubscriber.cjs +6 -0
  150. package/dist/sns/SNSSubscriber.d.cts +4 -0
  151. package/dist/sns/SNSSubscriber.d.mts +4 -0
  152. package/dist/sns/SNSSubscriber.mjs +6 -0
  153. package/dist/sns/index.cjs +10 -0
  154. package/dist/sns/index.d.cts +5 -0
  155. package/dist/sns/index.d.mts +5 -0
  156. package/dist/sns/index.mjs +8 -0
  157. package/dist/sqs/SQSConnection.cjs +4 -0
  158. package/dist/sqs/SQSConnection.d.cts +3 -0
  159. package/dist/sqs/SQSConnection.d.mts +3 -0
  160. package/dist/sqs/SQSConnection.mjs +4 -0
  161. package/dist/sqs/SQSPublisher.cjs +3 -0
  162. package/dist/sqs/SQSPublisher.d.cts +4 -0
  163. package/dist/sqs/SQSPublisher.d.mts +4 -0
  164. package/dist/sqs/SQSPublisher.mjs +3 -0
  165. package/dist/sqs/SQSSubscriber.cjs +3 -0
  166. package/dist/sqs/SQSSubscriber.d.cts +4 -0
  167. package/dist/sqs/SQSSubscriber.d.mts +4 -0
  168. package/dist/sqs/SQSSubscriber.mjs +3 -0
  169. package/dist/sqs/index.cjs +8 -0
  170. package/dist/sqs/index.d.cts +5 -0
  171. package/dist/sqs/index.d.mts +5 -0
  172. package/dist/sqs/index.mjs +6 -0
  173. package/dist/types-B63qHVOG.mjs +17 -0
  174. package/dist/types-B63qHVOG.mjs.map +1 -0
  175. package/dist/types-CvSMEO5z.cjs +23 -0
  176. package/dist/types-CvSMEO5z.cjs.map +1 -0
  177. package/dist/types-D0g-o6wX.d.mts +65 -0
  178. package/dist/types-DEGUg5BA.d.cts +65 -0
  179. package/dist/types.cjs +3 -0
  180. package/dist/types.d.cts +2 -0
  181. package/dist/types.d.mts +2 -0
  182. package/dist/types.mjs +3 -0
  183. package/package.json +49 -0
  184. package/src/EventConnection.ts +39 -0
  185. package/src/Publisher.ts +87 -0
  186. package/src/Subscriber.ts +102 -0
  187. package/src/__tests__/Subscriber.spec.ts +41 -0
  188. package/src/basic/BasicConnection.ts +47 -0
  189. package/src/basic/BasicPublisher.ts +15 -0
  190. package/src/basic/BasicSubscriber.ts +24 -0
  191. package/src/basic/__tests__/BasicSubscriber.spec.ts +166 -0
  192. package/src/basic/index.ts +3 -0
  193. package/src/index.ts +25 -0
  194. package/src/rabbitmq/RabbitMQConnection.ts +148 -0
  195. package/src/rabbitmq/RabbitMQPublisher.ts +67 -0
  196. package/src/rabbitmq/RabbitMQSubscriber.ts +113 -0
  197. package/src/rabbitmq/__tests__/RabbitMQPublisher.spec.ts +378 -0
  198. package/src/rabbitmq/__tests__/RabbitMQSubscriber.spec.ts +292 -0
  199. package/src/rabbitmq/index.ts +6 -0
  200. package/src/sns/SNSConnection.ts +98 -0
  201. package/src/sns/SNSPublisher.ts +64 -0
  202. package/src/sns/SNSSubscriber.ts +222 -0
  203. package/src/sns/__tests__/SNS-SQS-Integration.spec.ts +389 -0
  204. package/src/sns/index.ts +6 -0
  205. package/src/sqs/SQSConnection.ts +97 -0
  206. package/src/sqs/SQSPublisher.ts +105 -0
  207. package/src/sqs/SQSSubscriber.ts +189 -0
  208. package/src/sqs/__tests__/SQSPublisher.spec.ts +189 -0
  209. package/src/sqs/index.ts +6 -0
  210. package/src/types.ts +76 -0
  211. package/tsdown.config.ts +5 -0
@@ -0,0 +1,98 @@
1
+ import { SNSClient, type SNSClientConfig } from '@aws-sdk/client-sns';
2
+ import type { EventConnection } from '../types';
3
+ import { EventPublisherType } from '../types';
4
+
5
+ export interface SNSConnectionConfig {
6
+ topicArn: string;
7
+ region?: string;
8
+ endpoint?: string; // Custom endpoint (e.g., for LocalStack)
9
+ credentials?: {
10
+ accessKeyId: string;
11
+ secretAccessKey: string;
12
+ sessionToken?: string;
13
+ };
14
+ }
15
+
16
+ export class SNSConnection implements EventConnection {
17
+ readonly type = EventPublisherType.SNS;
18
+ private client: SNSClient;
19
+ private connected = false;
20
+ private config: SNSConnectionConfig;
21
+
22
+ constructor(config: SNSConnectionConfig) {
23
+ this.config = config;
24
+
25
+ const clientConfig: SNSClientConfig = {
26
+ region: config.region,
27
+ endpoint: config.endpoint,
28
+ credentials: config.credentials,
29
+ };
30
+
31
+ this.client = new SNSClient(clientConfig);
32
+ }
33
+
34
+ /**
35
+ * Create an SNSConnection from a connection string
36
+ * Format: sns://?topicArn=arn:aws:sns:region:account:topic&region=us-east-1&endpoint=http://localhost:4566
37
+ */
38
+ static async fromConnectionString(
39
+ connectionString: string,
40
+ ): Promise<SNSConnection> {
41
+ const url = new URL(connectionString);
42
+ const params = url.searchParams;
43
+
44
+ const topicArn = params.get('topicArn');
45
+ if (!topicArn) {
46
+ throw new Error('topicArn parameter is required in connection string');
47
+ }
48
+
49
+ const config: SNSConnectionConfig = {
50
+ topicArn,
51
+ region: params.get('region') || undefined,
52
+ endpoint: params.get('endpoint') || undefined,
53
+ };
54
+
55
+ // Parse credentials if provided
56
+ const accessKeyId = params.get('accessKeyId');
57
+ const secretAccessKey = params.get('secretAccessKey');
58
+ if (accessKeyId && secretAccessKey) {
59
+ config.credentials = {
60
+ accessKeyId,
61
+ secretAccessKey,
62
+ sessionToken: params.get('sessionToken') || undefined,
63
+ };
64
+ }
65
+
66
+ const connection = new SNSConnection(config);
67
+ await connection.connect();
68
+ return connection;
69
+ }
70
+
71
+ async connect(): Promise<void> {
72
+ // SNS doesn't have a traditional connection, but we mark as connected
73
+ this.connected = true;
74
+ }
75
+
76
+ async close(): Promise<void> {
77
+ this.client.destroy();
78
+ this.connected = false;
79
+ }
80
+
81
+ isConnected(): boolean {
82
+ return this.connected;
83
+ }
84
+
85
+ /**
86
+ * Get the underlying SNS client
87
+ */
88
+ get snsClient(): SNSClient {
89
+ return this.client;
90
+ }
91
+
92
+ /**
93
+ * Get the topic ARN for this connection
94
+ */
95
+ get topicArn(): string {
96
+ return this.config.topicArn;
97
+ }
98
+ }
@@ -0,0 +1,64 @@
1
+ import { PublishCommand } from '@aws-sdk/client-sns';
2
+ import type { EventPublisher, PublishableMessage } from '../types';
3
+ import type { SNSConnection } from './SNSConnection';
4
+
5
+ export interface SNSPublisherOptions {
6
+ // Optional publisher-specific options
7
+ }
8
+
9
+ export class SNSPublisher<TMessage extends PublishableMessage<string, any>>
10
+ implements EventPublisher<TMessage>
11
+ {
12
+ constructor(
13
+ private connection: SNSConnection,
14
+ private options: SNSPublisherOptions = {},
15
+ ) {}
16
+
17
+ /**
18
+ * Create an SNSPublisher from a connection string
19
+ * Format: sns://?topicArn=arn:aws:sns:region:account:topic&region=us-east-1&endpoint=http://localhost:4566
20
+ */
21
+ static async fromConnectionString<
22
+ TMessage extends PublishableMessage<string, any>,
23
+ >(connectionString: string): Promise<SNSPublisher<TMessage>> {
24
+ const { SNSConnection } = await import('./SNSConnection');
25
+ const connection =
26
+ await SNSConnection.fromConnectionString(connectionString);
27
+
28
+ return new SNSPublisher<TMessage>(connection);
29
+ }
30
+
31
+ async publish(messages: TMessage[]): Promise<void> {
32
+ if (messages.length === 0) return;
33
+
34
+ if (!this.connection.isConnected()) {
35
+ await this.connection.connect();
36
+ }
37
+
38
+ // Publish all messages
39
+ await Promise.all(messages.map((message) => this.publishMessage(message)));
40
+ }
41
+
42
+ private async publishMessage(message: TMessage): Promise<void> {
43
+ const command = new PublishCommand({
44
+ TopicArn: this.connection.topicArn,
45
+ Message: JSON.stringify({
46
+ type: message.type,
47
+ payload: message.payload,
48
+ }),
49
+ MessageAttributes: {
50
+ type: {
51
+ DataType: 'String',
52
+ StringValue: message.type,
53
+ },
54
+ },
55
+ });
56
+
57
+ await this.connection.snsClient.send(command);
58
+ }
59
+
60
+ async close(): Promise<void> {
61
+ // Publisher doesn't own the connection
62
+ // Connection should be closed by whoever created it
63
+ }
64
+ }
@@ -0,0 +1,222 @@
1
+ import { SubscribeCommand, UnsubscribeCommand } from '@aws-sdk/client-sns';
2
+ import {
3
+ CreateQueueCommand,
4
+ DeleteQueueCommand,
5
+ GetQueueAttributesCommand,
6
+ SQSClient,
7
+ SetQueueAttributesCommand,
8
+ } from '@aws-sdk/client-sqs';
9
+ import { SQSConnection } from '../sqs/SQSConnection';
10
+ import { SQSSubscriber } from '../sqs/SQSSubscriber';
11
+ import type { EventSubscriber, PublishableMessage } from '../types';
12
+ import type { SNSConnection } from './SNSConnection';
13
+
14
+ export interface SNSSubscriberOptions {
15
+ queueName?: string; // Optional queue name (defaults to auto-generated)
16
+ createQueue?: boolean; // Auto-create SQS queue (default: true)
17
+ deleteQueueOnClose?: boolean; // Delete queue when subscriber closes (default: false for named queues, true for auto-generated)
18
+ waitTimeSeconds?: number; // SQS long polling (default: 20)
19
+ maxMessages?: number; // Max messages per poll (default: 10)
20
+ }
21
+
22
+ /**
23
+ * SNS Subscriber - receives push-based messages from SNS via SQS queue
24
+ *
25
+ * Architecture:
26
+ * SNS Topic → (push) → SQS Queue → Subscriber
27
+ *
28
+ * This subscriber automatically:
29
+ * 1. Creates an SQS queue (or uses existing one)
30
+ * 2. Subscribes the queue to the SNS topic
31
+ * 3. Receives messages pushed from SNS to the queue
32
+ * 4. Filters messages by topic ARN and message type
33
+ * 5. Processes and acknowledges messages
34
+ *
35
+ * Note: While SQS long-polling is used internally to receive messages,
36
+ * this is an implementation detail of the SNS push delivery mechanism.
37
+ * Messages are pushed from SNS to SQS, and we receive those pushed messages.
38
+ */
39
+ export class SNSSubscriber<TMessage extends PublishableMessage<string, any>>
40
+ implements EventSubscriber<TMessage>
41
+ {
42
+ private sqsClient: SQSClient;
43
+ private queueUrl?: string;
44
+ private queueArn?: string;
45
+ private subscriptionArn?: string;
46
+ private sqsSubscriber?: SQSSubscriber<TMessage>;
47
+ private options: Required<SNSSubscriberOptions>;
48
+
49
+ constructor(
50
+ private connection: SNSConnection,
51
+ options: SNSSubscriberOptions = {},
52
+ ) {
53
+ this.options = {
54
+ queueName:
55
+ options.queueName ||
56
+ `sns-sub-${Date.now()}-${Math.random().toString(36).substring(7)}`,
57
+ createQueue: options.createQueue ?? true,
58
+ deleteQueueOnClose: options.deleteQueueOnClose ?? !options.queueName, // Delete auto-generated queues
59
+ waitTimeSeconds: options.waitTimeSeconds ?? 20,
60
+ maxMessages: options.maxMessages ?? 10,
61
+ };
62
+
63
+ // Create SQS client with same config as SNS
64
+ this.sqsClient = new SQSClient({
65
+ region: (this.connection as any).config.region,
66
+ endpoint: (this.connection as any).config.endpoint,
67
+ credentials: (this.connection as any).config.credentials,
68
+ });
69
+ }
70
+
71
+ /**
72
+ * Create an SNSSubscriber from a connection string
73
+ * Format: sns://?topicArn=...&queueName=myqueue&region=us-east-1
74
+ */
75
+ static async fromConnectionString<
76
+ TMessage extends PublishableMessage<string, any>,
77
+ >(connectionString: string): Promise<SNSSubscriber<TMessage>> {
78
+ const url = new URL(connectionString);
79
+ const params = url.searchParams;
80
+
81
+ const { SNSConnection } = await import('./SNSConnection');
82
+ const connection =
83
+ await SNSConnection.fromConnectionString(connectionString);
84
+
85
+ const options: SNSSubscriberOptions = {
86
+ queueName: params.get('queueName') || undefined,
87
+ createQueue: params.get('createQueue') !== 'false',
88
+ deleteQueueOnClose: params.get('deleteQueueOnClose') === 'true',
89
+ waitTimeSeconds: params.get('waitTimeSeconds')
90
+ ? Number.parseInt(params.get('waitTimeSeconds')!, 10)
91
+ : undefined,
92
+ };
93
+
94
+ return new SNSSubscriber<TMessage>(connection, options);
95
+ }
96
+
97
+ async subscribe(
98
+ messages: TMessage['type'][],
99
+ listener: (payload: TMessage) => Promise<void>,
100
+ ): Promise<void> {
101
+ // Setup SQS queue and SNS subscription
102
+ await this.setupQueue();
103
+ await this.subscribeToTopic();
104
+
105
+ // Create SQS connection for the subscriber
106
+ const sqsConnection = new SQSConnection({
107
+ queueUrl: this.queueUrl!,
108
+ region: (this.connection as any).config.region,
109
+ endpoint: (this.connection as any).config.endpoint,
110
+ credentials: (this.connection as any).config.credentials,
111
+ });
112
+ await sqsConnection.connect();
113
+
114
+ // Use SQSSubscriber to receive messages, with topic filtering
115
+ this.sqsSubscriber = new SQSSubscriber<TMessage>(sqsConnection, {
116
+ waitTimeSeconds: this.options.waitTimeSeconds,
117
+ maxMessages: this.options.maxMessages,
118
+ expectedTopicArn: this.connection.topicArn,
119
+ });
120
+
121
+ await this.sqsSubscriber.subscribe(messages, listener);
122
+ }
123
+
124
+ /**
125
+ * Stop receiving messages and clean up resources
126
+ */
127
+ async stop(): Promise<void> {
128
+ // Stop SQS subscriber
129
+ if (this.sqsSubscriber) {
130
+ this.sqsSubscriber.stop();
131
+ }
132
+
133
+ // Unsubscribe from topic
134
+ if (this.subscriptionArn) {
135
+ try {
136
+ const command = new UnsubscribeCommand({
137
+ SubscriptionArn: this.subscriptionArn,
138
+ });
139
+ await this.connection.snsClient.send(command);
140
+ } catch (error) {
141
+ console.error('Error unsubscribing:', error);
142
+ }
143
+ }
144
+
145
+ // Delete queue if configured
146
+ if (this.queueUrl && this.options.deleteQueueOnClose) {
147
+ try {
148
+ const command = new DeleteQueueCommand({
149
+ QueueUrl: this.queueUrl,
150
+ });
151
+ await this.sqsClient.send(command);
152
+ } catch (error) {
153
+ console.error('Error deleting queue:', error);
154
+ }
155
+ }
156
+
157
+ this.sqsClient.destroy();
158
+ }
159
+
160
+ private async setupQueue(): Promise<void> {
161
+ if (this.options.createQueue) {
162
+ // Create queue
163
+ const createCommand = new CreateQueueCommand({
164
+ QueueName: this.options.queueName,
165
+ });
166
+ const response = await this.sqsClient.send(createCommand);
167
+ this.queueUrl = response.QueueUrl!;
168
+ } else {
169
+ throw new Error('Queue must exist or createQueue option must be true');
170
+ }
171
+
172
+ // Get queue ARN
173
+ const attrsCommand = new GetQueueAttributesCommand({
174
+ QueueUrl: this.queueUrl,
175
+ AttributeNames: ['QueueArn'],
176
+ });
177
+ const attrsResponse = await this.sqsClient.send(attrsCommand);
178
+ this.queueArn = attrsResponse.Attributes?.QueueArn!;
179
+
180
+ // Set queue policy to allow SNS
181
+ await this.setQueuePolicy();
182
+ }
183
+
184
+ private async setQueuePolicy(): Promise<void> {
185
+ const policy = {
186
+ Version: '2012-10-17',
187
+ Statement: [
188
+ {
189
+ Effect: 'Allow',
190
+ Principal: '*',
191
+ Action: 'SQS:SendMessage',
192
+ Resource: this.queueArn,
193
+ Condition: {
194
+ ArnEquals: {
195
+ 'aws:SourceArn': this.connection.topicArn,
196
+ },
197
+ },
198
+ },
199
+ ],
200
+ };
201
+
202
+ const command = new SetQueueAttributesCommand({
203
+ QueueUrl: this.queueUrl,
204
+ Attributes: {
205
+ Policy: JSON.stringify(policy),
206
+ },
207
+ });
208
+
209
+ await this.sqsClient.send(command);
210
+ }
211
+
212
+ private async subscribeToTopic(): Promise<void> {
213
+ const command = new SubscribeCommand({
214
+ TopicArn: this.connection.topicArn,
215
+ Protocol: 'sqs',
216
+ Endpoint: this.queueArn,
217
+ });
218
+
219
+ const response = await this.connection.snsClient.send(command);
220
+ this.subscriptionArn = response.SubscriptionArn!;
221
+ }
222
+ }