eip-cloud-services 1.0.7 → 1.0.9

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 (3) hide show
  1. package/package.json +2 -2
  2. package/src/redis.js +47 -11
  3. package/src/s3.js +33 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eip-cloud-services",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Houses a collection of helpers for connecting with Cloud services.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,4 +22,4 @@
22
22
  "mysql": "^2.18.1",
23
23
  "redis": "^4.6.7"
24
24
  }
25
- }
25
+ }
package/src/redis.js CHANGED
@@ -80,7 +80,43 @@ exports.get = async ( key ) => {
80
80
  throw new Error ( `redis.get expects a string key, instead the type provided was: ${typeof key}` );
81
81
  }
82
82
  catch ( error ) {
83
- console.log ( 'REDIS LIB ERROR [get]', error.stack );
83
+ console.log ( 'REDIS LIB ERROR [get]', error );
84
+ throw error;
85
+ }
86
+ };
87
+
88
+ /**
89
+ * Retrieves the values of multiple keys.
90
+ * @param {string[]} keys - The keys to retrieve.
91
+ * @returns {Promise} - A promise that resolves with the values of the keys.
92
+ *
93
+ * @description This method retrieves the values associated with the specified keys from Redis.
94
+ * If the value is a JSON string, it will be parsed and returned as a JavaScript object.
95
+ * If the value is not a valid JSON string, it will be returned as a string.
96
+ */
97
+ exports.multiGet = async ( keys ) => {
98
+ try {
99
+ if ( Array.isArray ( keys ) ) {
100
+ const client = await getClient ();
101
+
102
+ const commandArgs = keys.map ( key => key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key );
103
+
104
+ const data = await client.sendCommand ( [ 'MGET', ...commandArgs ] );
105
+
106
+ return data.map ( val => {
107
+ try {
108
+ return JSON.parse ( val );
109
+ }
110
+ catch {
111
+ return val;
112
+ }
113
+ } );
114
+ }
115
+
116
+ throw new Error ( `redis.multiGet expects an array of keys, instead the type provided was: ${typeof keys}` );
117
+ }
118
+ catch ( error ) {
119
+ console.log ( 'REDIS LIB ERROR [multiGet]', error );
84
120
  throw error;
85
121
  }
86
122
  };
@@ -145,7 +181,7 @@ exports.del = async ( key ) => {
145
181
  throw new Error ( `redis.del expects a string key, instead the type provided was: ${typeof key}` );
146
182
  }
147
183
  catch ( error ) {
148
- console.log ( 'REDIS LIB ERROR [del]', error.stack );
184
+ console.log ( 'REDIS LIB ERROR [del]', error );
149
185
  throw error;
150
186
  }
151
187
  };
@@ -208,7 +244,7 @@ exports.setRemove = async ( key, members ) => {
208
244
  throw new Error ( `redis.setRemove expects a string key, instead the type provided was: ${typeof key}` );
209
245
  }
210
246
  catch ( error ) {
211
- console.log ( 'REDIS LIB ERROR [setRemove]', error.stack );
247
+ console.log ( 'REDIS LIB ERROR [setRemove]', error );
212
248
  throw error;
213
249
  }
214
250
  };
@@ -236,7 +272,7 @@ exports.setMembers = async ( key ) => {
236
272
  throw new Error ( `redis.setMembers expects a string key, instead the type provided was: ${typeof key}` );
237
273
  }
238
274
  catch ( error ) {
239
- console.log ( 'REDIS LIB ERROR [setMembers]', error.stack );
275
+ console.log ( 'REDIS LIB ERROR [setMembers]', error );
240
276
  throw error;
241
277
  }
242
278
  };
@@ -274,7 +310,7 @@ exports.setPop = async ( key ) => {
274
310
  throw new Error ( `redis.setPop expects a string key, instead the type provided was: ${typeof key}` );
275
311
  }
276
312
  catch ( error ) {
277
- console.log ( 'REDIS LIB ERROR [setPop]', error.stack );
313
+ console.log ( 'REDIS LIB ERROR [setPop]', error );
278
314
  throw error;
279
315
  }
280
316
  };
@@ -315,7 +351,7 @@ exports.listUnshift = async ( key, values ) => {
315
351
  throw new Error ( `redis.listUnshift expects a string key, instead the type provided was: ${typeof key}` );
316
352
  }
317
353
  catch ( error ) {
318
- console.log ( 'REDIS LIB ERROR [listUnshift]', error.stack );
354
+ console.log ( 'REDIS LIB ERROR [listUnshift]', error );
319
355
  throw error;
320
356
  }
321
357
  };
@@ -356,7 +392,7 @@ exports.listPush = async ( key, values ) => {
356
392
  throw new Error ( `redis.listPush expects a string key, instead the type provided was: ${typeof key}` );
357
393
  }
358
394
  catch ( error ) {
359
- console.log ( 'REDIS LIB ERROR [listPush]', error.stack );
395
+ console.log ( 'REDIS LIB ERROR [listPush]', error );
360
396
  throw error;
361
397
  }
362
398
  };
@@ -388,7 +424,7 @@ exports.listPop = async ( key, count = 1 ) => {
388
424
  throw new Error ( `redis.listPop expects a string key, instead the type provided was: ${typeof key}` );
389
425
  }
390
426
  catch ( error ) {
391
- console.log ( 'REDIS LIB ERROR [listPop]', error.stack );
427
+ console.log ( 'REDIS LIB ERROR [listPop]', error );
392
428
  throw error;
393
429
  }
394
430
  };
@@ -422,7 +458,7 @@ exports.listRange = async ( key, start = 0, end = -1 ) => {
422
458
  throw new Error ( `redis.listRange expects a string key, instead the type provided was: ${typeof key}` );
423
459
  }
424
460
  catch ( error ) {
425
- console.log ( 'REDIS LIB ERROR [listRange]', error.stack );
461
+ console.log ( 'REDIS LIB ERROR [listRange]', error );
426
462
  throw error;
427
463
  }
428
464
  };
@@ -452,7 +488,7 @@ exports.getExpiryInSeconds = async ( key ) => {
452
488
  throw new Error ( `redis.getExpiryInSeconds expects a string key, instead the type provided was: ${typeof key}` );
453
489
  }
454
490
  catch ( error ) {
455
- console.log ( 'REDIS LIB ERROR [getExpiryInSeconds]', error.stack );
491
+ console.log ( 'REDIS LIB ERROR [getExpiryInSeconds]', error );
456
492
  throw error;
457
493
  }
458
494
  };
@@ -483,7 +519,7 @@ exports.setExpiry = async ( key, seconds = 0 ) => {
483
519
  throw new Error ( `redis.setExpiry expects a string key, instead the type provided was: ${typeof key}` );
484
520
  }
485
521
  catch ( error ) {
486
- console.log ( 'REDIS LIB ERROR [setExpiry]', error.stack );
522
+ console.log ( 'REDIS LIB ERROR [setExpiry]', error );
487
523
  throw error;
488
524
  }
489
525
  };
package/src/s3.js CHANGED
@@ -251,7 +251,7 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
251
251
  };
252
252
 
253
253
  /**
254
- * Move an object within S3 to a different location.
254
+ * Copy an object within S3 to a different location. (This keep the original like a COPY / PASTE operation)
255
255
  *
256
256
  * @param {string} sourceKey - The source object key.
257
257
  * @param {string} destinationKey - The destination object key.
@@ -260,7 +260,7 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
260
260
  * @returns {Promise} A promise that resolves when the object is successfully moved in S3.
261
261
  * @description Moves an object from the source location to the destination location within S3.
262
262
  */
263
- exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucket, destinationBucket = config.s3.Bucket ) => {
263
+ exports.copy = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucket, destinationBucket = config.s3.Bucket ) => {
264
264
  try {
265
265
  // Copy the object to the destination location
266
266
  const copyCommand = new CopyObjectCommand ( {
@@ -272,19 +272,39 @@ exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
272
272
  } );
273
273
 
274
274
  await S3.send ( copyCommand );
275
-
276
- // Delete the object from the source location
277
- const deleteCommand = new DeleteObjectsCommand ( {
278
- Bucket: sourceBucket,
279
- Delete: {
280
- Objects: [
281
- { Key: sourceKey }
282
- ],
283
- Quiet: true
275
+
276
+ if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' ){
277
+ if ( sourceBucket === destinationBucket ){
278
+ log ( `S3 [COPY]: ${sourceKey} moved to ${destinationKey} on ${sourceBucket}.` );
284
279
  }
285
- } );
280
+ else {
281
+ log ( `S3 [COPY]: ${sourceKey} on ${sourceBucket} moved to ${destinationKey} on ${destinationBucket}.` );
282
+ }
283
+ }
284
+ }
285
+ catch ( error ) {
286
+ throw error;
287
+ }
288
+ };
289
+
290
+ /**
291
+ * Move an object within S3 to a different location. (This deletes the original like a CUT / PASTE operation)
292
+ *
293
+ * @param {string} sourceKey - The source object key.
294
+ * @param {string} destinationKey - The destination object key.
295
+ * @param {string} [sourceBucket=config.s3.Bucket] - The source bucket name. Defaults to the configured bucket.
296
+ * @param {string} [destinationBucket=config.s3.Bucket] - The destination bucket name. Defaults to the configured bucket.
297
+ * @returns {Promise} A promise that resolves when the object is successfully moved in S3.
298
+ * @description Moves an object from the source location to the destination location within S3.
299
+ */
300
+ exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucket, destinationBucket = config.s3.Bucket ) => {
301
+ try {
302
+
303
+ // Copy the object to the destination location
304
+ await this.copy ( sourceKey, destinationKey, sourceBucket, destinationBucket );
286
305
 
287
- await S3.send ( deleteCommand );
306
+ // Delete the object from the source location
307
+ await this.del ( sourceKey, sourceBucket );
288
308
 
289
309
  if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' ){
290
310
  if ( sourceBucket === destinationBucket ){