eip-cloud-services 1.2.3 → 1.2.4

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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## Summary
4
4
 
5
- The EIP Cloud Services Module is a comprehensive Node.js package that provides seamless integration with various cloud services, including Redis, AWS S3, CDN, AWS Lambda, and MySQL. This module is designed to simplify the complexities of interacting with these cloud services, offering a range of functionalities from data caching and storage to content delivery and serverless computing.
5
+ The EIP Cloud Services Module is a comprehensive Node.js package that provides seamless integration with various cloud services, including Redis, AWS S3, CDN, AWS Lambda, AWS SQS, and MySQL. This module is designed to simplify the complexities of interacting with these cloud services, offering a range of functionalities from data caching and storage to content delivery and serverless computing.
6
6
 
7
7
  ## Installation and Import
8
8
 
@@ -58,6 +58,10 @@ module.exports = {
58
58
  logs: "verbose", // "verbose" or "outputs", any other value will not log. verbose will log all activity. outputs will only log when there is a file updated.
59
59
  logsFunction: ( message ) => { ... } // Optional, if nothing is provided console.log will be used.
60
60
  },
61
+ sqs: {
62
+ region: 'eu-west-1',
63
+ endpoint: 'http://localhost:4566' // Optional (e.g. localstack)
64
+ },
61
65
  mysql: {
62
66
  connectionLimit: 10, // Max connections
63
67
  host: 'my-database.domain.com',
@@ -76,6 +80,7 @@ module.exports = {
76
80
  3. [CDN Module](#cdn-module)
77
81
  4. [MySQL Module](#mysql-module)
78
82
  5. [AWS Lambda Module](#aws-lambda-module)
83
+ 6. [AWS SQS Module](#aws-sqs-module)
79
84
 
80
85
  # AWS Lambda Module
81
86
 
@@ -131,6 +136,30 @@ The module includes error handling to manage issues related to Lambda invocation
131
136
 
132
137
  Logging is provided to track the start and completion of Lambda invocations, aiding in debugging and monitoring.
133
138
 
139
+ # AWS SQS Module
140
+
141
+ ## Overview
142
+
143
+ This module provides helpers for sending, receiving, and deleting messages in AWS SQS queues.
144
+
145
+ ## Usage
146
+
147
+ ```javascript
148
+ const sqs = require('eip-cloud-services').sqs;
149
+
150
+ const queueUrl = await sqs.getQueueUrl('my-queue');
151
+ await sqs.sendMessage(queueUrl, { type: 'example', payload: 'hello' });
152
+
153
+ const messages = await sqs.receiveMessages(queueUrl, { maxMessages: 5, waitTimeSeconds: 10 });
154
+ if (messages.length > 0) {
155
+ await sqs.deleteMessages(queueUrl, messages);
156
+ }
157
+ ```
158
+
159
+ ## Configuration
160
+
161
+ Set `sqs.region` and optionally `sqs.endpoint` in your config. The module defaults to `eu-west-1` and uses the AWS SDK credential chain.
162
+
134
163
  # MySQL Module
135
164
 
136
165
  ## Overview
package/index.js CHANGED
@@ -3,5 +3,6 @@ exports.redis = require ( './src/redis' );
3
3
  exports.s3 = require ( './src/s3' );
4
4
  exports.cdn = require ( './src/cdn' );
5
5
  exports.lambda = require ( './src/lambda' );
6
+ exports.sqs = require ( './src/sqs' );
6
7
  exports.mysql = require ( './src/mysql' );
7
- exports.mysql8 = require ( './src/mysql8' );
8
+ exports.mysql8 = require ( './src/mysql8' );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eip-cloud-services",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "Houses a collection of helpers for connecting with Cloud services.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,6 +17,7 @@
17
17
  "@aws-sdk/client-cloudfront": "^3.354.0",
18
18
  "@aws-sdk/client-lambda": "^3.356.0",
19
19
  "@aws-sdk/client-s3": "^3.354.0",
20
+ "@aws-sdk/client-sqs": "^3.356.0",
20
21
  "@aws-sdk/client-secrets-manager": "^3.354.0",
21
22
  "config": "^3.3.9",
22
23
  "google-auth-library": "^8.8.0",
package/src/sqs.js ADDED
@@ -0,0 +1,116 @@
1
+ const { SQSClient, SendMessageCommand, ReceiveMessageCommand, DeleteMessageCommand, DeleteMessageBatchCommand, GetQueueUrlCommand, GetQueueAttributesCommand } = require ( '@aws-sdk/client-sqs' );
2
+ const fs = require ( 'fs' );
3
+ let config = {};
4
+ const configDirPath = `${ process.cwd ()}/config`;
5
+ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirectory () ) {
6
+ config = require ( 'config' ); // require the config directory if it exists
7
+ }
8
+
9
+ const resolveClientConfig = () => {
10
+ const region = config?.sqs?.region || process.env.AWS_REGION || 'eu-west-1';
11
+ const endpoint = config?.sqs?.endpoint;
12
+ return endpoint ? { region, endpoint } : { region };
13
+ };
14
+
15
+ const client = new SQSClient ( resolveClientConfig () );
16
+
17
+ /**
18
+ * Resolve a queue URL by name.
19
+ *
20
+ * @param {string} queueName - The name of the queue.
21
+ * @returns {Promise<string>} The queue URL.
22
+ */
23
+ exports.getQueueUrl = async ( queueName ) => {
24
+ const command = new GetQueueUrlCommand ( { QueueName: queueName } );
25
+ const response = await client.send ( command );
26
+ return response.QueueUrl;
27
+ };
28
+
29
+ /**
30
+ * Fetch queue attributes.
31
+ *
32
+ * @param {string} queueUrl - The queue URL.
33
+ * @param {string[]} [attributeNames=['All']] - Attribute names to fetch.
34
+ * @returns {Promise<Record<string, string>>} Attributes map.
35
+ */
36
+ exports.getQueueAttributes = async ( queueUrl, attributeNames = [ 'All' ] ) => {
37
+ const command = new GetQueueAttributesCommand ( {
38
+ QueueUrl: queueUrl,
39
+ AttributeNames: attributeNames
40
+ } );
41
+ const response = await client.send ( command );
42
+ return response.Attributes || {};
43
+ };
44
+
45
+ /**
46
+ * Send a message to an SQS queue.
47
+ *
48
+ * @param {string} queueUrl - The queue URL.
49
+ * @param {string|object} messageBody - The message payload.
50
+ * @param {object} [options={}] - Additional SQS message options.
51
+ * @returns {Promise<object>} Send response.
52
+ */
53
+ exports.sendMessage = async ( queueUrl, messageBody, options = {} ) => {
54
+ const body = typeof messageBody === 'string' ? messageBody : JSON.stringify ( messageBody );
55
+ const command = new SendMessageCommand ( {
56
+ QueueUrl: queueUrl,
57
+ MessageBody: body,
58
+ ...options
59
+ } );
60
+ return client.send ( command );
61
+ };
62
+
63
+ /**
64
+ * Receive messages from an SQS queue.
65
+ *
66
+ * @param {string} queueUrl - The queue URL.
67
+ * @param {object} [options={}] - Receive options.
68
+ * @returns {Promise<object[]>} Array of messages.
69
+ */
70
+ exports.receiveMessages = async ( queueUrl, options = {} ) => {
71
+ const command = new ReceiveMessageCommand ( {
72
+ QueueUrl: queueUrl,
73
+ MaxNumberOfMessages: options.maxMessages || options.MaxNumberOfMessages || 1,
74
+ WaitTimeSeconds: options.waitTimeSeconds || options.WaitTimeSeconds || 10,
75
+ VisibilityTimeout: options.visibilityTimeout || options.VisibilityTimeout,
76
+ MessageAttributeNames: options.MessageAttributeNames || [ 'All' ],
77
+ AttributeNames: options.AttributeNames || [ 'All' ]
78
+ } );
79
+ const response = await client.send ( command );
80
+ return response.Messages || [];
81
+ };
82
+
83
+ /**
84
+ * Delete a single message from an SQS queue.
85
+ *
86
+ * @param {string} queueUrl - The queue URL.
87
+ * @param {string} receiptHandle - The receipt handle of the message.
88
+ * @returns {Promise<object>} Delete response.
89
+ */
90
+ exports.deleteMessage = async ( queueUrl, receiptHandle ) => {
91
+ const command = new DeleteMessageCommand ( {
92
+ QueueUrl: queueUrl,
93
+ ReceiptHandle: receiptHandle
94
+ } );
95
+ return client.send ( command );
96
+ };
97
+
98
+ /**
99
+ * Delete multiple messages from an SQS queue.
100
+ *
101
+ * @param {string} queueUrl - The queue URL.
102
+ * @param {Array<{receiptHandle?: string, ReceiptHandle?: string}>} messages - Messages or receipt handles.
103
+ * @returns {Promise<object>} Batch delete response.
104
+ */
105
+ exports.deleteMessages = async ( queueUrl, messages ) => {
106
+ const entries = ( messages || [] ).map ( ( msg, index ) => ( {
107
+ Id: String ( index ),
108
+ ReceiptHandle: msg?.ReceiptHandle || msg?.receiptHandle || msg
109
+ } ) );
110
+ if ( entries.length === 0 ) return { Successful: [], Failed: [] };
111
+ const command = new DeleteMessageBatchCommand ( {
112
+ QueueUrl: queueUrl,
113
+ Entries: entries
114
+ } );
115
+ return client.send ( command );
116
+ };