eip-cloud-services 1.0.14 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eip-cloud-services",
3
- "version": "1.0.14",
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": {
package/src/cdn.js CHANGED
@@ -1,7 +1,12 @@
1
1
  const { CloudFrontClient, CreateInvalidationCommand } = require ( '@aws-sdk/client-cloudfront' );
2
2
  const { GoogleAuth } = require ( 'google-auth-library' );
3
3
  const { initialiseGoogleAuth } = require ( './gcp' );
4
- const config = require ( 'config' );
4
+ const fs = require ( 'fs' );
5
+ let config = {};
6
+ const configDirPath = `${ process.cwd ()}/config`;
7
+ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirectory () ) {
8
+ config = require ( 'config' ); // require the config directory if it exists
9
+ }
5
10
  const packageJson = require ( '../package.json' );
6
11
  const { cwd } = require ( 'process' );
7
12
  const { log } = config?.s3?.logsFunction ? require ( `${ cwd ()}/${config.s3.logsFunction}` ) : console;
package/src/lambda.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const { LambdaClient, InvokeCommand } = require ( '@aws-sdk/client-lambda' );
2
- const config = require ( 'config' );
3
2
  const packageJson = require ( '../package.json' );
4
3
 
5
4
  /**
package/src/mysql.js CHANGED
@@ -1,5 +1,10 @@
1
1
  const mysql = require ( 'mysql' );
2
- const config = require ( 'config' );
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
+ }
3
8
 
4
9
  let pool = null;
5
10
 
package/src/redis.js CHANGED
@@ -1,11 +1,16 @@
1
1
  const redis = require ( 'redis' );
2
- const config = require ( 'config' );
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
+ }
3
8
  let redisClient;
4
9
 
5
10
  const getClient = async () => {
6
11
  try {
7
12
  if ( !redisClient ) {
8
- redisClient = redis.createClient ( { url: `${config.redis.host}:${config.redis.port}` } );
13
+ redisClient = redis.createClient ( { url: `${config?.redis?.host}:${config?.redis?.port}` } );
9
14
  await redisClient.connect ();
10
15
  redisClient.on ( 'error', error => {
11
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' );
@@ -35,7 +40,7 @@ exports.keys = async ( pattern ) => {
35
40
  const client = await getClient ();
36
41
 
37
42
  const commandArgs = [
38
- pattern.startsWith ( config.redis.prefix ) ? pattern : config.redis.prefix + pattern,
43
+ pattern.startsWith ( config?.redis?.prefix ) ? pattern : config?.redis?.prefix + pattern,
39
44
  ];
40
45
 
41
46
  return await client.sendCommand ( [ 'KEYS', ...commandArgs ] );
@@ -68,7 +73,7 @@ exports.scan = async ( cursor, matchPattern = '*', count = 100 ) => {
68
73
 
69
74
  const commandArgs = [
70
75
  String ( cursor ),
71
- 'MATCH', matchPattern.startsWith ( config.redis.prefix ) ? matchPattern : config.redis.prefix + matchPattern,
76
+ 'MATCH', matchPattern.startsWith ( config?.redis?.prefix ) ? matchPattern : config?.redis?.prefix + matchPattern,
72
77
  'COUNT', String ( count ),
73
78
  ];
74
79
 
@@ -95,7 +100,7 @@ exports.get = async ( key ) => {
95
100
  const client = await getClient ();
96
101
 
97
102
  const commandArgs = [
98
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
103
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
99
104
  ];
100
105
 
101
106
  const data = await client.sendCommand ( [ 'GET', ...commandArgs ] );
@@ -130,7 +135,7 @@ exports.multiGet = async ( keys ) => {
130
135
  if ( Array.isArray ( keys ) ) {
131
136
  const client = await getClient ();
132
137
 
133
- 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 );
134
139
 
135
140
  const data = await client.sendCommand ( [ 'MGET', ...commandArgs ] );
136
141
 
@@ -170,7 +175,7 @@ exports.set = async ( key, value, ex, px ) => {
170
175
  const client = await getClient ();
171
176
 
172
177
  const commandArgs = [
173
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
178
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
174
179
  value,
175
180
  ];
176
181
 
@@ -203,7 +208,7 @@ exports.del = async ( key ) => {
203
208
  const client = await getClient ();
204
209
 
205
210
  const commandArgs = [
206
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
211
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
207
212
  ];
208
213
 
209
214
  return await client.sendCommand ( [ 'DEL', ...commandArgs ] );
@@ -234,7 +239,7 @@ exports.setAdd = async ( key, members ) => {
234
239
  const client = await getClient ();
235
240
 
236
241
  const commandArgs = [
237
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
242
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
238
243
  ...( Array.isArray ( members ) ? members : [ members ] ),
239
244
  ];
240
245
 
@@ -265,7 +270,7 @@ exports.setRemove = async ( key, members ) => {
265
270
  const client = await getClient ();
266
271
 
267
272
  const commandArgs = [
268
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
273
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
269
274
  ...( Array.isArray ( members ) ? members : [ members ] ),
270
275
  ];
271
276
 
@@ -294,7 +299,7 @@ exports.setMembers = async ( key ) => {
294
299
  const client = await getClient ();
295
300
 
296
301
  const commandArgs = [
297
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
302
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
298
303
  ];
299
304
 
300
305
  return await client.sendCommand ( [ 'SMEMBERS', ...commandArgs ] );
@@ -325,7 +330,7 @@ exports.setPop = async ( key ) => {
325
330
  const client = await getClient ();
326
331
 
327
332
  const commandArgs = [
328
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
333
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
329
334
  ];
330
335
 
331
336
  const member = await client.sendCommand ( [ 'SPOP', ...commandArgs ] );
@@ -363,7 +368,7 @@ exports.listUnshift = async ( key, values ) => {
363
368
  const client = await getClient ();
364
369
 
365
370
  const commandArgs = [
366
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
371
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
367
372
  ];
368
373
 
369
374
  if ( typeof values === 'string' ) {
@@ -404,7 +409,7 @@ exports.listPush = async ( key, values ) => {
404
409
  const client = await getClient ();
405
410
 
406
411
  const commandArgs = [
407
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
412
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
408
413
  ];
409
414
 
410
415
  if ( typeof values === 'string' ) {
@@ -445,7 +450,7 @@ exports.listPop = async ( key, count = 1 ) => {
445
450
  const client = await getClient ();
446
451
 
447
452
  const commandArgs = [
448
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
453
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
449
454
  String ( count ),
450
455
  ];
451
456
 
@@ -478,7 +483,7 @@ exports.listRange = async ( key, start = 0, end = -1 ) => {
478
483
  const client = await getClient ();
479
484
 
480
485
  const commandArgs = [
481
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
486
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
482
487
  String ( start ),
483
488
  String ( end ),
484
489
  ];
@@ -510,7 +515,7 @@ exports.getExpiryInSeconds = async ( key ) => {
510
515
  const client = await getClient ();
511
516
 
512
517
  const commandArgs = [
513
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
518
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
514
519
  ];
515
520
 
516
521
  return await client.sendCommand ( [ 'TTL', ...commandArgs ] );
@@ -540,7 +545,7 @@ exports.setExpiry = async ( key, seconds = 0 ) => {
540
545
  const client = await getClient ();
541
546
 
542
547
  const commandArgs = [
543
- key.startsWith ( config.redis.prefix ) ? key : config.redis.prefix + key,
548
+ key.startsWith ( config?.redis?.prefix ) ? key : config?.redis?.prefix + key,
544
549
  String ( seconds ),
545
550
  ];
546
551
 
package/src/s3.js CHANGED
@@ -39,22 +39,27 @@
39
39
  */
40
40
 
41
41
  const { S3Client, HeadObjectCommand, GetObjectCommand, PutObjectCommand, DeleteObjectCommand, CopyObjectCommand, DeleteObjectsCommand } = require ( '@aws-sdk/client-s3' );
42
- const config = require ( 'config' );
42
+ const fs = require ( 'fs' );
43
+ let config = {};
44
+ const configDirPath = `${ process.cwd ()}/config`;
45
+ if ( fs.existsSync ( configDirPath ) && fs.statSync ( configDirPath ).isDirectory () ) {
46
+ config = require ( 'config' ); // require the config directory if it exists
47
+ }
43
48
  const zlib = require ( 'zlib' );
44
49
  const crypto = require ( 'crypto' );
45
50
  const { cwd } = require ( 'process' );
46
- const { log } = config?.s3?.logsFunction ? require ( `${ cwd ()}/${config.s3.logsFunction}` ) : console;
51
+ const { log } = config?.s3?.logsFunction ? require ( `${ cwd ()}/${config?.s3?.logsFunction}` ) : console;
47
52
  const S3 = new S3Client ( { region: 'eu-west-1' } );
48
53
 
49
54
  /**
50
55
  * Check if an object exists in S3.
51
56
  *
52
57
  * @param {string} key - The object key.
53
- * @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.
54
59
  * @returns {Promise<boolean>} A promise that resolves to true if the object exists, false otherwise.
55
60
  * @description Checks if an object with the specified key exists in S3.
56
61
  */
57
- exports.exists = async ( key, bucket = config.s3.Bucket ) => {
62
+ exports.exists = async ( key, bucket = config?.s3?.Bucket ) => {
58
63
  try {
59
64
  const command = new HeadObjectCommand ( {
60
65
  Bucket: bucket,
@@ -63,13 +68,13 @@ exports.exists = async ( key, bucket = config.s3.Bucket ) => {
63
68
 
64
69
  await S3.send ( command );
65
70
 
66
- if ( config.s3.logs === 'verbose' )
71
+ if ( config?.s3?.logs === 'verbose' )
67
72
  log ( `S3 [EXISTS]: ${key} on ${bucket} - Exists` );
68
73
 
69
74
  return true;
70
75
  }
71
76
  catch ( error ) {
72
- if ( config.s3.logs === 'verbose' )
77
+ if ( config?.s3?.logs === 'verbose' )
73
78
  log ( `S3 [EXISTS]: ${key} on ${bucket} - Does not exist` );
74
79
 
75
80
  return false;
@@ -80,18 +85,18 @@ exports.exists = async ( key, bucket = config.s3.Bucket ) => {
80
85
  * Get an object from S3.
81
86
  *
82
87
  * @param {string} key - The object key.
83
- * @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.
84
89
  * @returns {Promise} A promise that resolves to the retrieved object.
85
90
  * @description Retrieves an object from S3 based on the provided key.
86
91
  */
87
- exports.get = async ( key, bucket = config.s3.Bucket ) => {
92
+ exports.get = async ( key, bucket = config?.s3?.Bucket ) => {
88
93
  try {
89
94
  const command = new GetObjectCommand ( {
90
95
  Bucket: bucket,
91
96
  Key: key
92
97
  } );
93
98
 
94
- if ( config.s3.logs === 'verbose' )
99
+ if ( config?.s3?.logs === 'verbose' )
95
100
  log ( `S3 [GET]: Getting ${bucket}/${key}.` );
96
101
 
97
102
  const response = await S3.send ( command );
@@ -99,14 +104,14 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
99
104
 
100
105
  if ( response.ContentEncoding && response.ContentEncoding === 'gzip' ) {
101
106
 
102
- if ( config.s3.logs === 'verbose' )
107
+ if ( config?.s3?.logs === 'verbose' )
103
108
  log ( `S3 [GET]: ${key} on ${bucket} was unzipped (was gzipped).` );
104
109
 
105
110
  data = zlib.unzipSync ( data );
106
111
  }
107
112
 
108
113
  if ( response.ContentType !== 'application/json' && !response.Metadata[ 'tmg-json' ] ) {
109
- if ( config.s3.logs === 'output' )
114
+ if ( config?.s3?.logs === 'output' )
110
115
  log ( `S3 [GET]: Returned ${response.ContentType} from ${bucket}/${key}.` );
111
116
 
112
117
  return data.toString ( 'utf8' );
@@ -129,11 +134,11 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
129
134
 
130
135
  data = Buffer.from ( decryptedArrayBuffer ).toString ( 'utf8' );
131
136
 
132
- if ( config.s3.logs === 'verbose' )
137
+ if ( config?.s3?.logs === 'verbose' )
133
138
  log ( `S3 [GET]: ${key} on ${bucket} - JSON content was decrypted.` );
134
139
  }
135
140
 
136
- if ( config.s3.logs === 'output' )
141
+ if ( config?.s3?.logs === 'output' )
137
142
  log ( `S3 [GET]: ${bucket}/${key} - JSON content was returned.` );
138
143
 
139
144
  return JSON.parse ( data.toString ( 'utf8' ) );
@@ -153,7 +158,7 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
153
158
  * @param {string} key - The object key.
154
159
  * @param {Buffer|Uint8Array|Blob|string} body - The object body.
155
160
  * @param {object} [options] - The optional parameters for setting the object.
156
- * @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.
157
162
  * @param {string} [options.contentType='application/json'] - The content type of the object. Defaults to 'application/json'.
158
163
  * @param {string} [options.acl='public-read'] - The ACL (Access Control List) of the object. Defaults to 'public-read'.
159
164
  * @param {string} [options.cacheControl='max-age=25,s-maxage=30,must-revalidate'] - Sets cache control for the object.
@@ -164,7 +169,7 @@ exports.get = async ( key, bucket = config.s3.Bucket ) => {
164
169
  */
165
170
  exports.set = async ( key, body, options = {} ) => {
166
171
  const {
167
- bucket = config.s3.Bucket,
172
+ bucket = config?.s3?.Bucket,
168
173
  contentType = 'application/json',
169
174
  acl = 'public-read',
170
175
  cacheControl = 'max-age=25,s-maxage=30,must-revalidate',
@@ -174,7 +179,7 @@ exports.set = async ( key, body, options = {} ) => {
174
179
 
175
180
  if ( encrypt && ( contentType === 'application/json' || contentType === 'text/plain' ) ) {
176
181
 
177
- if ( config.s3.logs === 'verbose' )
182
+ if ( config?.s3?.logs === 'verbose' )
178
183
  log ( `S3 [SET]: ${bucket}/${key} - Encrypting.` );
179
184
 
180
185
  const encoder = new TextEncoder ();
@@ -207,12 +212,13 @@ exports.set = async ( key, body, options = {} ) => {
207
212
  ContentType: contentType,
208
213
  ACL: acl,
209
214
  CacheControl: cacheControl,
210
- Metadata: metadata
215
+ Metadata: metadata,
216
+ ContentLength: Buffer.byteLength ( body )
211
217
  } );
212
218
 
213
219
  const data = await S3.send ( command );
214
220
 
215
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' )
221
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' )
216
222
  log ( `S3 [SET]: ${bucket}/${key} - Stored.` );
217
223
 
218
224
  return data;
@@ -227,11 +233,11 @@ exports.set = async ( key, body, options = {} ) => {
227
233
  * Delete an object from S3.
228
234
  *
229
235
  * @param {string} key - The object key.
230
- * @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.
231
237
  * @returns {Promise} A promise that resolves when the object is successfully deleted from S3.
232
238
  * @description Deletes an object from S3 based on the provided key.
233
239
  */
234
- exports.del = async ( key, bucket = config.s3.Bucket ) => {
240
+ exports.del = async ( key, bucket = config?.s3?.Bucket ) => {
235
241
  try {
236
242
  const command = new DeleteObjectCommand ( {
237
243
  Bucket: bucket,
@@ -240,7 +246,7 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
240
246
 
241
247
  const data = await S3.send ( command );
242
248
 
243
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' )
249
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' )
244
250
  log ( `S3 [DELETE]: ${key} on ${bucket} - Deleted.` );
245
251
 
246
252
  return data;
@@ -255,12 +261,12 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
255
261
  *
256
262
  * @param {string} sourceKey - The source object key.
257
263
  * @param {string} destinationKey - The destination object key.
258
- * @param {string} [sourceBucket=config.s3.Bucket] - The source bucket name. Defaults to the configured bucket.
259
- * @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.
260
266
  * @returns {Promise} A promise that resolves when the object is successfully moved in S3.
261
267
  * @description Moves an object from the source location to the destination location within S3.
262
268
  */
263
- 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 ) => {
264
270
  try {
265
271
  // Copy the object to the destination location
266
272
  const copyCommand = new CopyObjectCommand ( {
@@ -273,7 +279,7 @@ exports.copy = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
273
279
 
274
280
  await S3.send ( copyCommand );
275
281
 
276
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' ){
282
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' ){
277
283
  if ( sourceBucket === destinationBucket ){
278
284
  log ( `S3 [COPY]: ${sourceKey} moved to ${destinationKey} on ${sourceBucket}.` );
279
285
  }
@@ -292,12 +298,12 @@ exports.copy = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
292
298
  *
293
299
  * @param {string} sourceKey - The source object key.
294
300
  * @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.
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.
297
303
  * @returns {Promise} A promise that resolves when the object is successfully moved in S3.
298
304
  * @description Moves an object from the source location to the destination location within S3.
299
305
  */
300
- 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 ) => {
301
307
  try {
302
308
 
303
309
  // Copy the object to the destination location
@@ -306,7 +312,7 @@ exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
306
312
  // Delete the object from the source location
307
313
  await this.del ( sourceKey, sourceBucket );
308
314
 
309
- if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' ){
315
+ if ( config?.s3?.logs === 'outputs' || config?.s3?.logs === 'verbose' ){
310
316
  if ( sourceBucket === destinationBucket ){
311
317
  log ( `S3 [MOVE]: ${sourceKey} moved to ${destinationKey} on ${sourceBucket}.` );
312
318
  }