eip-cloud-services 1.2.2 → 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.2",
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/redis.js CHANGED
@@ -8,6 +8,11 @@ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirector
8
8
  }
9
9
 
10
10
  const clients = {};
11
+ const applyPrefix = (value) => {
12
+ const prefix = config?.redis?.prefix;
13
+ if (!prefix || typeof value !== 'string') return value;
14
+ return value.startsWith(prefix) ? value : `${prefix}${value}`;
15
+ };
11
16
 
12
17
  /**
13
18
  * Creates or retrieves a Redis client instance based on a given client identifier.
@@ -725,9 +730,10 @@ exports.setExpiry = async ( key, seconds = 0 ) => {
725
730
  exports.publish = async ( channel, message ) => {
726
731
  try {
727
732
  if ( typeof channel === 'string' && typeof message === 'string' ) {
728
- const client = await getClient ( `${channel}_pub` );
733
+ const fullChannel = applyPrefix ( channel );
734
+ const client = await getClient ( `${fullChannel}_pub` );
729
735
 
730
- return client.publish ( channel, message );
736
+ return client.publish ( fullChannel, message );
731
737
  }
732
738
 
733
739
  throw new Error ( 'redis.publish expects string types for both channel and message' );
@@ -749,15 +755,16 @@ exports.publish = async ( channel, message ) => {
749
755
  exports.subscribe = async ( channel, messageHandler ) => {
750
756
  try {
751
757
  if ( typeof channel === 'string' && typeof messageHandler === 'function' ) {
752
- const client = await getClient ( `${channel}_sub` );
758
+ const fullChannel = applyPrefix ( channel );
759
+ const client = await getClient ( `${fullChannel}_sub` );
753
760
 
754
761
  client.on ( 'message', ( receivedChannel, message ) => {
755
- if ( receivedChannel === channel ) {
762
+ if ( receivedChannel === fullChannel ) {
756
763
  messageHandler ( message );
757
764
  }
758
765
  } );
759
766
 
760
- await client.subscribe ( channel );
767
+ await client.subscribe ( fullChannel );
761
768
 
762
769
  return;
763
770
  }
@@ -781,12 +788,13 @@ exports.subscribe = async ( channel, messageHandler ) => {
781
788
  exports.unsubscribe = async ( channel ) => {
782
789
  try {
783
790
  if ( typeof channel === 'string' ) {
784
- const client = await getClient ( `${channel}_sub` );
791
+ const fullChannel = applyPrefix ( channel );
792
+ const client = await getClient ( `${fullChannel}_sub` );
785
793
 
786
794
  client.removeAllListeners ( 'message' );
787
795
 
788
- await client.unsubscribe ( channel );
789
- await deleteClient ( `${channel}_sub` );
796
+ await client.unsubscribe ( fullChannel );
797
+ await deleteClient ( `${fullChannel}_sub` );
790
798
 
791
799
  return;
792
800
  }
package/src/s3.js CHANGED
@@ -197,13 +197,14 @@ exports.get = async ( key, bucket = config?.s3?.Bucket, options = {} ) => {
197
197
  * @description Sets an object in S3 with the provided key, body, and optional parameters.
198
198
  */
199
199
  exports.set = async ( key, body, options = {} ) => {
200
- const bucket = options.bucket || config?.s3?.Bucket;
201
- const contentType = options.contentType || 'application/json';
202
- const cacheControl = options.cacheControl || 'max-age=25,s-maxage=30,must-revalidate';
203
- const encrypt = Boolean ( options.encrypt );
204
- const metadata = options.metadata || {};
205
- const hasAcl = Object.prototype.hasOwnProperty.call ( options, 'acl' );
206
- const acl = hasAcl ? options.acl : 'public-read';
200
+ const {
201
+ bucket = config?.s3?.Bucket,
202
+ contentType = 'application/json',
203
+ acl = 'public-read',
204
+ cacheControl = 'max-age=25,s-maxage=30,must-revalidate',
205
+ encrypt = false,
206
+ metadata = {}
207
+ } = options;
207
208
 
208
209
  if ( encrypt && ( contentType === 'application/json' || contentType === 'text/plain' ) ) {
209
210
 
@@ -233,21 +234,16 @@ exports.set = async ( key, body, options = {} ) => {
233
234
  }
234
235
 
235
236
  try {
236
- const params = {
237
+ const command = new PutObjectCommand ( {
237
238
  Bucket: bucket,
238
239
  Key: key,
239
240
  Body: body,
240
241
  ContentType: contentType,
242
+ ACL: acl,
241
243
  CacheControl: cacheControl,
242
244
  Metadata: metadata,
243
245
  ContentLength: Buffer.byteLength ( body )
244
- };
245
-
246
- if ( acl !== null && acl !== undefined && acl !== false ) {
247
- params.ACL = acl;
248
- }
249
-
250
- const command = new PutObjectCommand ( params );
246
+ } );
251
247
 
252
248
  const data = await S3.send ( command );
253
249
 
@@ -412,4 +408,4 @@ const streamToBuffer = async ( stream ) => {
412
408
  await pipelineAsync ( stream, collectorStream );
413
409
 
414
410
  return Buffer.concat ( chunks );
415
- };
411
+ };
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
+ };