eip-cloud-services 1.2.3 → 1.2.5

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/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
 
4
4
  All notable changes to this project will be documented in this file.
5
5
 
6
+ ## [1.2.5] - 2026-03-02
7
+
8
+ ### Fixed
9
+ - Hardened Redis error handling to avoid throwing from client error events, reducing process instability during transient disconnects.
10
+ - Reworked pub/sub client usage to use shared publisher/subscriber clients instead of per-channel client fan-out.
11
+ - Improved subscription lifecycle management by tracking channel handlers and unsubscribing only when no handlers remain.
12
+ - Improved Redis client cleanup and stale-client recovery behavior.
13
+
6
14
  ## [1.1.0] - 2023-11-18
7
15
 
8
16
  ### Changed
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.5",
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,45 @@ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirector
8
8
  }
9
9
 
10
10
  const clients = {};
11
+ const subscriptionHandlers = new Map ();
12
+ let mainSubListenerAttached = false;
13
+ const lastConnectionErrorByClient = new Map ();
14
+ const CONNECTION_ERROR_LOG_WINDOW_MS = 30000;
15
+
16
+ const logConnectionError = ( clientId, error ) => {
17
+ const now = Date.now ();
18
+ const last = lastConnectionErrorByClient.get ( clientId ) || 0;
19
+ if ( now - last < CONNECTION_ERROR_LOG_WINDOW_MS ) {
20
+ return;
21
+ }
22
+ lastConnectionErrorByClient.set ( clientId, now );
23
+ console.error ( '\x1b[33mREDIS CONNECTION FAILED: Redis connection failed. If you\'re running locally, is a redis server actually active? Also, check your connection configuration.\x1b[0m', {
24
+ clientId,
25
+ error: error?.message || error
26
+ } );
27
+ };
28
+
29
+ const attachMainSubscriberListener = ( client ) => {
30
+ if ( mainSubListenerAttached ) {
31
+ return;
32
+ }
33
+ client.on ( 'message', ( receivedChannel, message ) => {
34
+ const handlers = subscriptionHandlers.get ( receivedChannel );
35
+ if ( !handlers || handlers.size === 0 ) {
36
+ return;
37
+ }
38
+ handlers.forEach ( handler => {
39
+ try {
40
+ handler ( message );
41
+ }
42
+ catch ( error ) {
43
+ console.error ( 'REDIS LIB ERROR [subscribe handler]', error );
44
+ }
45
+ } );
46
+ } );
47
+ mainSubListenerAttached = true;
48
+ };
49
+
11
50
  const applyPrefix = (value) => {
12
51
  const prefix = config?.redis?.prefix;
13
52
  if (!prefix || typeof value !== 'string') return value;
@@ -32,6 +71,20 @@ const applyPrefix = (value) => {
32
71
  */
33
72
  const getClient = async ( clientId = 'main' ) => {
34
73
  try {
74
+ if ( clients[ clientId ] ) {
75
+ const status = clients[ clientId ].status;
76
+ if ( status === 'end' || status === 'close' ) {
77
+ try {
78
+ clients[ clientId ].disconnect ();
79
+ }
80
+ catch ( _err ) {}
81
+ delete clients[ clientId ];
82
+ if ( clientId === 'main_sub' ) {
83
+ mainSubListenerAttached = false;
84
+ }
85
+ }
86
+ }
87
+
35
88
  if ( !clients[ clientId ] ) {
36
89
  let redisClient;
37
90
 
@@ -52,8 +105,8 @@ const getClient = async ( clientId = 'main' ) => {
52
105
  redisClient.once ( 'error', reject );
53
106
  } );
54
107
 
55
- redisClient.on ( 'node error', err => {
56
- console.error ( 'Redis cluster node error', err );
108
+ redisClient.on ( 'node error', error => {
109
+ logConnectionError ( clientId, error );
57
110
  } );
58
111
  }
59
112
  else if ( config?.redis?.clusterEnabled && ( !Array.isArray ( config.redis.cluster ) || config.redis.cluster.length === 0 ) ) {
@@ -66,8 +119,7 @@ const getClient = async ( clientId = 'main' ) => {
66
119
  } );
67
120
 
68
121
  redisClient.on ( 'error', error => {
69
- console.error ( '\x1b[33mREDIS CONNECTION FAILED: Redis connection failed. If you\'re running locally, is a redis server actually active? Also, check your connection configuration.\x1b[0m' );
70
- throw error;
122
+ logConnectionError ( clientId, error );
71
123
  } );
72
124
 
73
125
  await new Promise ( ( resolve, reject ) => {
@@ -77,6 +129,14 @@ const getClient = async ( clientId = 'main' ) => {
77
129
  }
78
130
 
79
131
  clients[ clientId ] = redisClient;
132
+
133
+ if ( clientId === 'main_sub' ) {
134
+ attachMainSubscriberListener ( redisClient );
135
+ const channels = [ ...subscriptionHandlers.keys () ];
136
+ if ( channels.length > 0 ) {
137
+ await redisClient.subscribe ( ...channels );
138
+ }
139
+ }
80
140
  }
81
141
 
82
142
  return clients[ clientId ];
@@ -88,8 +148,19 @@ const getClient = async ( clientId = 'main' ) => {
88
148
 
89
149
  const deleteClient = async ( clientId ) => {
90
150
  if ( clients[ clientId ] ){
91
- await clients[ clientId ].quit ();
151
+ try {
152
+ await clients[ clientId ].quit ();
153
+ }
154
+ catch ( _err ) {
155
+ try {
156
+ clients[ clientId ].disconnect ();
157
+ }
158
+ catch ( __err ) {}
159
+ }
92
160
  delete clients[ clientId ];
161
+ if ( clientId === 'main_sub' ) {
162
+ mainSubListenerAttached = false;
163
+ }
93
164
  }
94
165
  };
95
166
 
@@ -731,7 +802,7 @@ exports.publish = async ( channel, message ) => {
731
802
  try {
732
803
  if ( typeof channel === 'string' && typeof message === 'string' ) {
733
804
  const fullChannel = applyPrefix ( channel );
734
- const client = await getClient ( `${fullChannel}_pub` );
805
+ const client = await exports.getPubClient ();
735
806
 
736
807
  return client.publish ( fullChannel, message );
737
808
  }
@@ -756,16 +827,21 @@ exports.subscribe = async ( channel, messageHandler ) => {
756
827
  try {
757
828
  if ( typeof channel === 'string' && typeof messageHandler === 'function' ) {
758
829
  const fullChannel = applyPrefix ( channel );
759
- const client = await getClient ( `${fullChannel}_sub` );
830
+ const client = await exports.getSubClient ();
831
+ attachMainSubscriberListener ( client );
760
832
 
761
- client.on ( 'message', ( receivedChannel, message ) => {
762
- if ( receivedChannel === fullChannel ) {
763
- messageHandler ( message );
833
+ if ( !subscriptionHandlers.has ( fullChannel ) ) {
834
+ subscriptionHandlers.set ( fullChannel, new Set () );
835
+ try {
836
+ await client.subscribe ( fullChannel );
764
837
  }
765
- } );
838
+ catch ( error ) {
839
+ subscriptionHandlers.delete ( fullChannel );
840
+ throw error;
841
+ }
842
+ }
843
+ subscriptionHandlers.get ( fullChannel ).add ( messageHandler );
766
844
 
767
- await client.subscribe ( fullChannel );
768
-
769
845
  return;
770
846
  }
771
847
  else {
@@ -781,21 +857,32 @@ exports.subscribe = async ( channel, messageHandler ) => {
781
857
  /**
782
858
  * Unsubscribes from a channel.
783
859
  * @param {string} channel - The channel to unsubscribe from.
860
+ * @param {Function} [messageHandler] - Optional specific handler to unsubscribe.
784
861
  * @returns {Promise<void>} - A promise that resolves when the unsubscribe is complete.
785
862
  *
786
863
  * @description This method unsubscribes from the specified channel in Redis for incoming messages.
787
864
  */
788
- exports.unsubscribe = async ( channel ) => {
865
+ exports.unsubscribe = async ( channel, messageHandler ) => {
789
866
  try {
790
867
  if ( typeof channel === 'string' ) {
791
868
  const fullChannel = applyPrefix ( channel );
792
- const client = await getClient ( `${fullChannel}_sub` );
793
-
794
- client.removeAllListeners ( 'message' );
869
+ const handlers = subscriptionHandlers.get ( fullChannel );
870
+ if ( !handlers ) {
871
+ return;
872
+ }
795
873
 
796
- await client.unsubscribe ( fullChannel );
797
- await deleteClient ( `${fullChannel}_sub` );
874
+ if ( typeof messageHandler === 'function' ) {
875
+ handlers.delete ( messageHandler );
876
+ }
877
+ else {
878
+ handlers.clear ();
879
+ }
798
880
 
881
+ if ( handlers.size === 0 ) {
882
+ subscriptionHandlers.delete ( fullChannel );
883
+ const client = await exports.getSubClient ();
884
+ await client.unsubscribe ( fullChannel );
885
+ }
799
886
  return;
800
887
  }
801
888
  else {
@@ -833,5 +920,20 @@ exports.info = async () => {
833
920
  * @description This method closes the Redis connection using the 'quit' command, ensuring a graceful shutdown of the connection.
834
921
  */
835
922
  exports.kill = async () => {
836
- await Promise.all ( Object.values ( clients ).map ( client => client.quit () ) );
923
+ await Promise.all (
924
+ Object.entries ( clients ).map ( async ( [ clientId, client ] ) => {
925
+ try {
926
+ await client.quit ();
927
+ }
928
+ catch ( _err ) {
929
+ try {
930
+ client.disconnect ();
931
+ }
932
+ catch ( __err ) {}
933
+ }
934
+ delete clients[ clientId ];
935
+ } )
936
+ );
937
+ subscriptionHandlers.clear ();
938
+ mainSubListenerAttached = false;
837
939
  };
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
+ };