eip-cloud-services 1.0.15 → 1.0.17

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 +1 -1
  2. package/src/redis.js +19 -17
  3. package/src/s3.js +27 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eip-cloud-services",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "Houses a collection of helpers for connecting with Cloud services.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/redis.js CHANGED
@@ -10,7 +10,7 @@ let redisClient;
10
10
  const getClient = async () => {
11
11
  try {
12
12
  if ( !redisClient ) {
13
- redisClient = redis.createClient ( { url: `${config.redis.host}:${config.redis.port}` } );
13
+ redisClient = redis.createClient ( { url: `${config?.redis?.host}:${config?.redis?.port}` } );
14
14
  await redisClient.connect ();
15
15
  redisClient.on ( 'error', error => {
16
16
  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' );
@@ -26,6 +26,8 @@ const getClient = async () => {
26
26
  }
27
27
  };
28
28
 
29
+ exports.getClient = getClient ();
30
+
29
31
  /**
30
32
  * Retrieves all keys matching a pattern.
31
33
  * @param {string} pattern - The pattern to match.
@@ -40,7 +42,7 @@ exports.keys = async ( pattern ) => {
40
42
  const client = await getClient ();
41
43
 
42
44
  const commandArgs = [
43
- pattern.startsWith ( config.redis.prefix ) ? pattern : config.redis.prefix + pattern,
45
+ pattern.startsWith ( config?.redis?.prefix ) ? pattern : config?.redis?.prefix + pattern,
44
46
  ];
45
47
 
46
48
  return await client.sendCommand ( [ 'KEYS', ...commandArgs ] );
@@ -73,7 +75,7 @@ exports.scan = async ( cursor, matchPattern = '*', count = 100 ) => {
73
75
 
74
76
  const commandArgs = [
75
77
  String ( cursor ),
76
- 'MATCH', matchPattern.startsWith ( config.redis.prefix ) ? matchPattern : config.redis.prefix + matchPattern,
78
+ 'MATCH', matchPattern.startsWith ( config?.redis?.prefix ) ? matchPattern : config?.redis?.prefix + matchPattern,
77
79
  'COUNT', String ( count ),
78
80
  ];
79
81
 
@@ -100,7 +102,7 @@ exports.get = async ( key ) => {
100
102
  const client = await getClient ();
101
103
 
102
104
  const commandArgs = [
103
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
105
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
104
106
  ];
105
107
 
106
108
  const data = await client.sendCommand ( [ 'GET', ...commandArgs ] );
@@ -135,7 +137,7 @@ exports.multiGet = async ( keys ) => {
135
137
  if ( Array.isArray ( keys ) ) {
136
138
  const client = await getClient ();
137
139
 
138
- const commandArgs = keys.map ( key => key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key );
140
+ const commandArgs = keys.map ( key => key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key );
139
141
 
140
142
  const data = await client.sendCommand ( [ 'MGET', ...commandArgs ] );
141
143
 
@@ -175,7 +177,7 @@ exports.set = async ( key, value, ex, px ) => {
175
177
  const client = await getClient ();
176
178
 
177
179
  const commandArgs = [
178
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
180
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
179
181
  value,
180
182
  ];
181
183
 
@@ -208,7 +210,7 @@ exports.del = async ( key ) => {
208
210
  const client = await getClient ();
209
211
 
210
212
  const commandArgs = [
211
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
213
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
212
214
  ];
213
215
 
214
216
  return await client.sendCommand ( [ 'DEL', ...commandArgs ] );
@@ -239,7 +241,7 @@ exports.setAdd = async ( key, members ) => {
239
241
  const client = await getClient ();
240
242
 
241
243
  const commandArgs = [
242
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
244
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
243
245
  ...( Array.isArray ( members ) ? members : [ members ] ),
244
246
  ];
245
247
 
@@ -270,7 +272,7 @@ exports.setRemove = async ( key, members ) => {
270
272
  const client = await getClient ();
271
273
 
272
274
  const commandArgs = [
273
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
275
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
274
276
  ...( Array.isArray ( members ) ? members : [ members ] ),
275
277
  ];
276
278
 
@@ -299,7 +301,7 @@ exports.setMembers = async ( key ) => {
299
301
  const client = await getClient ();
300
302
 
301
303
  const commandArgs = [
302
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
304
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
303
305
  ];
304
306
 
305
307
  return await client.sendCommand ( [ 'SMEMBERS', ...commandArgs ] );
@@ -330,7 +332,7 @@ exports.setPop = async ( key ) => {
330
332
  const client = await getClient ();
331
333
 
332
334
  const commandArgs = [
333
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
335
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
334
336
  ];
335
337
 
336
338
  const member = await client.sendCommand ( [ 'SPOP', ...commandArgs ] );
@@ -368,7 +370,7 @@ exports.listUnshift = async ( key, values ) => {
368
370
  const client = await getClient ();
369
371
 
370
372
  const commandArgs = [
371
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
373
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
372
374
  ];
373
375
 
374
376
  if ( typeof values === 'string' ) {
@@ -409,7 +411,7 @@ exports.listPush = async ( key, values ) => {
409
411
  const client = await getClient ();
410
412
 
411
413
  const commandArgs = [
412
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
414
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
413
415
  ];
414
416
 
415
417
  if ( typeof values === 'string' ) {
@@ -450,7 +452,7 @@ exports.listPop = async ( key, count = 1 ) => {
450
452
  const client = await getClient ();
451
453
 
452
454
  const commandArgs = [
453
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
455
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
454
456
  String ( count ),
455
457
  ];
456
458
 
@@ -483,7 +485,7 @@ exports.listRange = async ( key, start = 0, end = -1 ) => {
483
485
  const client = await getClient ();
484
486
 
485
487
  const commandArgs = [
486
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
488
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
487
489
  String ( start ),
488
490
  String ( end ),
489
491
  ];
@@ -515,7 +517,7 @@ exports.getExpiryInSeconds = async ( key ) => {
515
517
  const client = await getClient ();
516
518
 
517
519
  const commandArgs = [
518
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
520
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
519
521
  ];
520
522
 
521
523
  return await client.sendCommand ( [ 'TTL', ...commandArgs ] );
@@ -545,7 +547,7 @@ exports.setExpiry = async ( key, seconds = 0 ) => {
545
547
  const client = await getClient ();
546
548
 
547
549
  const commandArgs = [
548
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
550
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
549
551
  String ( seconds ),
550
552
  ];
551
553
 
package/src/s3.js CHANGED
@@ -48,18 +48,18 @@ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirector
48
48
  const zlib = require ( 'zlib' );
49
49
  const crypto = require ( 'crypto' );
50
50
  const { cwd } = require ( 'process' );
51
- const { log } = config?.s3?.logsFunction ? require ( `${ cwd ()}/${config.s3.logsFunction}` ) : console;
51
+ const { log } = config?.s3?.logsFunction ? require ( `${ cwd ()}/${config?.s3?.logsFunction}` ) : console;
52
52
  const S3 = new S3Client ( { region: 'eu-west-1' } );
53
53
 
54
54
  /**
55
55
  * Check if an object exists in S3.
56
56
  *
57
57
  * @param {string} key - The object key.
58
- * @param {string} [bucket=config.s3.Bucket] - The bucket name. Defaults to the configured bucket.
58
+ * @param {string} [bucket=config?.s3?.Bucket] - The bucket name. Defaults to the configured bucket.
59
59
  * @returns {Promise<boolean>} A promise that resolves to true if the object exists, false otherwise.
60
60
  * @description Checks if an object with the specified key exists in S3.
61
61
  */
62
- exports.exists = async ( key, bucket = config.s3.Bucket ) => {
62
+ exports.exists = async ( key, bucket = config?.s3?.Bucket ) => {
63
63
  try {
64
64
  const command = new HeadObjectCommand ( {
65
65
  Bucket: bucket,
@@ -68,13 +68,13 @@ exports.exists = async ( key, bucket = config.s3.Bucket ) => {
68
68
 
69
69
  await S3.send ( command );
70
70
 
71
- if ( config.s3.logs === 'verbose' )
71
+ if ( config?.s3?.logs === 'verbose' )
72
72
  log ( `S3 [EXISTS]: ${key} on ${bucket} - Exists` );
73
73
 
74
74
  return true;
75
75
  }
76
76
  catch ( error ) {
77
- if ( config.s3.logs === 'verbose' )
77
+ if ( config?.s3?.logs === 'verbose' )
78
78
  log ( `S3 [EXISTS]: ${key} on ${bucket} - Does not exist` );
79
79
 
80
80
  return false;
@@ -85,18 +85,18 @@ exports.exists = async ( key, bucket = config.s3.Bucket ) => {
85
85
  * Get an object from S3.
86
86
  *
87
87
  * @param {string} key - The object key.
88
- * @param {string} [bucket=config.s3.Bucket] - The bucket name. Defaults to the configured bucket.
88
+ * @param {string} [bucket=config?.s3?.Bucket] - The bucket name. Defaults to the configured bucket.
89
89
  * @returns {Promise} A promise that resolves to the retrieved object.
90
90
  * @description Retrieves an object from S3 based on the provided key.
91
91
  */
92
- exports.get = async ( key, bucket = config.s3.Bucket ) => {
92
+ exports.get = async ( key, bucket = config?.s3?.Bucket ) => {
93
93
  try {
94
94
  const command = new GetObjectCommand ( {
95
95
  Bucket: bucket,
96
96
  Key: key
97
97
  } );
98
98
 
99
- if ( config.s3.logs === 'verbose' )
99
+ if ( config?.s3?.logs === 'verbose' )
100
100
  log ( `S3 [GET]: Getting ${bucket}/${key}.` );
101
101
 
102
102
  const response = await S3.send ( command );
@@ -104,14 +104,14 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
104
104
 
105
105
  if ( response.ContentEncoding && response.ContentEncoding === 'gzip' ) {
106
106
 
107
- if ( config.s3.logs === 'verbose' )
107
+ if ( config?.s3?.logs === 'verbose' )
108
108
  log ( `S3 [GET]: ${key} on ${bucket} was unzipped (was gzipped).` );
109
109
 
110
110
  data = zlib.unzipSync ( data );
111
111
  }
112
112
 
113
113
  if ( response.ContentType !== 'application/json' && !response.Metadata[ 'tmg-json' ] ) {
114
- if ( config.s3.logs === 'output' )
114
+ if ( config?.s3?.logs === 'output' )
115
115
  log ( `S3 [GET]: Returned ${response.ContentType} from ${bucket}/${key}.` );
116
116
 
117
117
  return data.toString ( 'utf8' );
@@ -134,11 +134,11 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
134
134
 
135
135
  data = Buffer.from ( decryptedArrayBuffer ).toString ( 'utf8' );
136
136
 
137
- if ( config.s3.logs === 'verbose' )
137
+ if ( config?.s3?.logs === 'verbose' )
138
138
  log ( `S3 [GET]: ${key} on ${bucket} - JSON content was decrypted.` );
139
139
  }
140
140
 
141
- if ( config.s3.logs === 'output' )
141
+ if ( config?.s3?.logs === 'output' )
142
142
  log ( `S3 [GET]: ${bucket}/${key} - JSON content was returned.` );
143
143
 
144
144
  return JSON.parse ( data.toString ( 'utf8' ) );
@@ -158,7 +158,7 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
158
158
  * @param {string} key - The object key.
159
159
  * @param {Buffer|Uint8Array|Blob|string} body - The object body.
160
160
  * @param {object} [options] - The optional parameters for setting the object.
161
- * @param {string} [options.bucket=config.s3.Bucket] - The bucket name. Defaults to the configured bucket.
161
+ * @param {string} [options.bucket=config?.s3?.Bucket] - The bucket name. Defaults to the configured bucket.
162
162
  * @param {string} [options.contentType='application/json'] - The content type of the object. Defaults to 'application/json'.
163
163
  * @param {string} [options.acl='public-read'] - The ACL (Access Control List) of the object. Defaults to 'public-read'.
164
164
  * @param {string} [options.cacheControl='max-age=25,s-maxage=30,must-revalidate'] - Sets cache control for the object.
@@ -169,7 +169,7 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
169
169
  */
170
170
  exports.set = async ( key, body, options = {} ) => {
171
171
  const {
172
- bucket = config.s3.Bucket,
172
+ bucket = config?.s3?.Bucket,
173
173
  contentType = 'application/json',
174
174
  acl = 'public-read',
175
175
  cacheControl = 'max-age=25,s-maxage=30,must-revalidate',
@@ -179,7 +179,7 @@ exports.set = async ( key, body, options = {} ) => {
179
179
 
180
180
  if ( encrypt && ( contentType === 'application/json' || contentType === 'text/plain' ) ) {
181
181
 
182
- if ( config.s3.logs === 'verbose' )
182
+ if ( config?.s3?.logs === 'verbose' )
183
183
  log ( `S3 [SET]: ${bucket}/${key} - Encrypting.` );
184
184
 
185
185
  const encoder = new TextEncoder ();
@@ -218,7 +218,7 @@ exports.set = async ( key, body, options = {} ) => {
218
218
 
219
219
  const data = await S3.send ( command );
220
220
 
221
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' )
221
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' )
222
222
  log ( `S3 [SET]: ${bucket}/${key} - Stored.` );
223
223
 
224
224
  return data;
@@ -233,11 +233,11 @@ exports.set = async ( key, body, options = {} ) => {
233
233
  * Delete an object from S3.
234
234
  *
235
235
  * @param {string} key - The object key.
236
- * @param {string} [bucket=config.s3.Bucket] - The bucket name. Defaults to the configured bucket.
236
+ * @param {string} [bucket=config?.s3?.Bucket] - The bucket name. Defaults to the configured bucket.
237
237
  * @returns {Promise} A promise that resolves when the object is successfully deleted from S3.
238
238
  * @description Deletes an object from S3 based on the provided key.
239
239
  */
240
- exports.del = async ( key, bucket = config.s3.Bucket ) => {
240
+ exports.del = async ( key, bucket = config?.s3?.Bucket ) => {
241
241
  try {
242
242
  const command = new DeleteObjectCommand ( {
243
243
  Bucket: bucket,
@@ -246,7 +246,7 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
246
246
 
247
247
  const data = await S3.send ( command );
248
248
 
249
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' )
249
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' )
250
250
  log ( `S3 [DELETE]: ${key} on ${bucket} - Deleted.` );
251
251
 
252
252
  return data;
@@ -261,12 +261,12 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
261
261
  *
262
262
  * @param {string} sourceKey - The source object key.
263
263
  * @param {string} destinationKey - The destination object key.
264
- * @param {string} [sourceBucket=config.s3.Bucket] - The source bucket name. Defaults to the configured bucket.
265
- * @param {string} [destinationBucket=config.s3.Bucket] - The destination bucket name. Defaults to the configured bucket.
264
+ * @param {string} [sourceBucket=config?.s3?.Bucket] - The source bucket name. Defaults to the configured bucket.
265
+ * @param {string} [destinationBucket=config?.s3?.Bucket] - The destination bucket name. Defaults to the configured bucket.
266
266
  * @returns {Promise} A promise that resolves when the object is successfully moved in S3.
267
267
  * @description Moves an object from the source location to the destination location within S3.
268
268
  */
269
- exports.copy = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucket, destinationBucket = config.s3.Bucket ) => {
269
+ exports.copy = async ( sourceKey, destinationKey, sourceBucket = config?.s3?.Bucket, destinationBucket = config?.s3?.Bucket ) => {
270
270
  try {
271
271
  // Copy the object to the destination location
272
272
  const copyCommand = new CopyObjectCommand ( {
@@ -279,7 +279,7 @@ exports.copy = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
279
279
 
280
280
  await S3.send ( copyCommand );
281
281
 
282
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' ){
282
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' ){
283
283
  if ( sourceBucket === destinationBucket ){
284
284
  log ( `S3 [COPY]: ${sourceKey} moved to ${destinationKey} on ${sourceBucket}.` );
285
285
  }
@@ -298,12 +298,12 @@ exports.copy = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
298
298
  *
299
299
  * @param {string} sourceKey - The source object key.
300
300
  * @param {string} destinationKey - The destination object key.
301
- * @param {string} [sourceBucket=config.s3.Bucket] - The source bucket name. Defaults to the configured bucket.
302
- * @param {string} [destinationBucket=config.s3.Bucket] - The destination bucket name. Defaults to the configured bucket.
301
+ * @param {string} [sourceBucket=config?.s3?.Bucket] - The source bucket name. Defaults to the configured bucket.
302
+ * @param {string} [destinationBucket=config?.s3?.Bucket] - The destination bucket name. Defaults to the configured bucket.
303
303
  * @returns {Promise} A promise that resolves when the object is successfully moved in S3.
304
304
  * @description Moves an object from the source location to the destination location within S3.
305
305
  */
306
- exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucket, destinationBucket = config.s3.Bucket ) => {
306
+ exports.move = async ( sourceKey, destinationKey, sourceBucket = config?.s3?.Bucket, destinationBucket = config?.s3?.Bucket ) => {
307
307
  try {
308
308
 
309
309
  // Copy the object to the destination location
@@ -312,7 +312,7 @@ exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
312
312
  // Delete the object from the source location
313
313
  await this.del ( sourceKey, sourceBucket );
314
314
 
315
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' ){
315
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' ){
316
316
  if ( sourceBucket === destinationBucket ){
317
317
  log ( `S3 [MOVE]: ${sourceKey} moved to ${destinationKey} on ${sourceBucket}.` );
318
318
  }