eip-cloud-services 1.0.15 → 1.0.16

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 +17 -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.16",
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
@@ -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' );
@@ -40,7 +40,7 @@ exports.keys = async ( pattern ) => {
40
40
  const client = await getClient ();
41
41
 
42
42
  const commandArgs = [
43
- pattern.startsWith ( config.redis.prefix ) ? pattern : config.redis.prefix + pattern,
43
+ pattern.startsWith ( config?.redis?.prefix ) ? pattern : config?.redis?.prefix + pattern,
44
44
  ];
45
45
 
46
46
  return await client.sendCommand ( [ 'KEYS', ...commandArgs ] );
@@ -73,7 +73,7 @@ exports.scan = async ( cursor, matchPattern = '*', count = 100 ) => {
73
73
 
74
74
  const commandArgs = [
75
75
  String ( cursor ),
76
- 'MATCH', matchPattern.startsWith ( config.redis.prefix ) ? matchPattern : config.redis.prefix + matchPattern,
76
+ 'MATCH', matchPattern.startsWith ( config?.redis?.prefix ) ? matchPattern : config?.redis?.prefix + matchPattern,
77
77
  'COUNT', String ( count ),
78
78
  ];
79
79
 
@@ -100,7 +100,7 @@ exports.get = async ( key ) => {
100
100
  const client = await getClient ();
101
101
 
102
102
  const commandArgs = [
103
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
103
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
104
104
  ];
105
105
 
106
106
  const data = await client.sendCommand ( [ 'GET', ...commandArgs ] );
@@ -135,7 +135,7 @@ exports.multiGet = async ( keys ) => {
135
135
  if ( Array.isArray ( keys ) ) {
136
136
  const client = await getClient ();
137
137
 
138
- const commandArgs = keys.map ( key => key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key );
138
+ const commandArgs = keys.map ( key => key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key );
139
139
 
140
140
  const data = await client.sendCommand ( [ 'MGET', ...commandArgs ] );
141
141
 
@@ -175,7 +175,7 @@ exports.set = async ( key, value, ex, px ) => {
175
175
  const client = await getClient ();
176
176
 
177
177
  const commandArgs = [
178
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
178
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
179
179
  value,
180
180
  ];
181
181
 
@@ -208,7 +208,7 @@ exports.del = async ( key ) => {
208
208
  const client = await getClient ();
209
209
 
210
210
  const commandArgs = [
211
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
211
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
212
212
  ];
213
213
 
214
214
  return await client.sendCommand ( [ 'DEL', ...commandArgs ] );
@@ -239,7 +239,7 @@ exports.setAdd = async ( key, members ) => {
239
239
  const client = await getClient ();
240
240
 
241
241
  const commandArgs = [
242
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
242
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
243
243
  ...( Array.isArray ( members ) ? members : [ members ] ),
244
244
  ];
245
245
 
@@ -270,7 +270,7 @@ exports.setRemove = async ( key, members ) => {
270
270
  const client = await getClient ();
271
271
 
272
272
  const commandArgs = [
273
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
273
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
274
274
  ...( Array.isArray ( members ) ? members : [ members ] ),
275
275
  ];
276
276
 
@@ -299,7 +299,7 @@ exports.setMembers = async ( key ) => {
299
299
  const client = await getClient ();
300
300
 
301
301
  const commandArgs = [
302
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
302
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
303
303
  ];
304
304
 
305
305
  return await client.sendCommand ( [ 'SMEMBERS', ...commandArgs ] );
@@ -330,7 +330,7 @@ exports.setPop = async ( key ) => {
330
330
  const client = await getClient ();
331
331
 
332
332
  const commandArgs = [
333
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
333
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
334
334
  ];
335
335
 
336
336
  const member = await client.sendCommand ( [ 'SPOP', ...commandArgs ] );
@@ -368,7 +368,7 @@ exports.listUnshift = async ( key, values ) => {
368
368
  const client = await getClient ();
369
369
 
370
370
  const commandArgs = [
371
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
371
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
372
372
  ];
373
373
 
374
374
  if ( typeof values === 'string' ) {
@@ -409,7 +409,7 @@ exports.listPush = async ( key, values ) => {
409
409
  const client = await getClient ();
410
410
 
411
411
  const commandArgs = [
412
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
412
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
413
413
  ];
414
414
 
415
415
  if ( typeof values === 'string' ) {
@@ -450,7 +450,7 @@ exports.listPop = async ( key, count = 1 ) => {
450
450
  const client = await getClient ();
451
451
 
452
452
  const commandArgs = [
453
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
453
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
454
454
  String ( count ),
455
455
  ];
456
456
 
@@ -483,7 +483,7 @@ exports.listRange = async ( key, start = 0, end = -1 ) => {
483
483
  const client = await getClient ();
484
484
 
485
485
  const commandArgs = [
486
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
486
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
487
487
  String ( start ),
488
488
  String ( end ),
489
489
  ];
@@ -515,7 +515,7 @@ exports.getExpiryInSeconds = async ( key ) => {
515
515
  const client = await getClient ();
516
516
 
517
517
  const commandArgs = [
518
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
518
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
519
519
  ];
520
520
 
521
521
  return await client.sendCommand ( [ 'TTL', ...commandArgs ] );
@@ -545,7 +545,7 @@ exports.setExpiry = async ( key, seconds = 0 ) => {
545
545
  const client = await getClient ();
546
546
 
547
547
  const commandArgs = [
548
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
548
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
549
549
  String ( seconds ),
550
550
  ];
551
551
 
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
  }