eip-cloud-services 1.0.4 → 1.0.6
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 +2 -2
- package/src/cdn.js +4 -0
- package/src/s3.js +47 -3
- package/src/util/log.js +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eip-cloud-services",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
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/cdn.js
CHANGED
|
@@ -3,6 +3,7 @@ const { GoogleAuth } = require ( 'google-auth-library' );
|
|
|
3
3
|
const { initialiseGoogleAuth } = require ( './gcp' );
|
|
4
4
|
const config = require ( 'config' );
|
|
5
5
|
const packageJson = require ( '../package.json' );
|
|
6
|
+
const { log } = config.s3.logsFunction ? require ( `${ cwd ()}/${config.s3.logsFunction}` ) : console;
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Create a CDN invalidation for the specified key(s) and environment.
|
|
@@ -37,6 +38,9 @@ exports.createInvalidation = async ( cdn, key, environment = 'production' ) => {
|
|
|
37
38
|
throw new Error ( 'Invalid key argument. Expected a string or an array of strings.' );
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
if ( config.cdn.log )
|
|
42
|
+
log ( `CDN [INVALIDATE]: ${paths.map ( path => `https://${cdn}${environment !== 'production' ? '-test' : ''}.eip.telegraph.co.uk/${path}` ).join ( ', ' )}\n` );
|
|
43
|
+
|
|
40
44
|
switch ( cdnSettings.type ) {
|
|
41
45
|
case 'google':
|
|
42
46
|
await invalidateGoogleCDN ( cdnSettings, paths );
|
package/src/s3.js
CHANGED
|
@@ -42,7 +42,8 @@ const { S3Client, HeadObjectCommand, GetObjectCommand, PutObjectCommand, DeleteO
|
|
|
42
42
|
const config = require ( 'config' );
|
|
43
43
|
const zlib = require ( 'zlib' );
|
|
44
44
|
const crypto = require ( 'crypto' );
|
|
45
|
-
|
|
45
|
+
const { cwd } = require ( 'process' );
|
|
46
|
+
const { log } = config.s3.logsFunction ? require ( `${ cwd ()}/${config.s3.logsFunction}` ) : console;
|
|
46
47
|
const S3 = new S3Client ( { region: 'eu-west-1' } );
|
|
47
48
|
|
|
48
49
|
/**
|
|
@@ -61,10 +62,16 @@ exports.exists = async ( key, bucket = config.s3.Bucket ) => {
|
|
|
61
62
|
} );
|
|
62
63
|
|
|
63
64
|
await S3.send ( command );
|
|
65
|
+
|
|
66
|
+
if ( config.s3.logs === 'verbose' )
|
|
67
|
+
log ( `S3 [EXISTS]: ${key} on ${bucket} - Exists` );
|
|
64
68
|
|
|
65
69
|
return true;
|
|
66
70
|
}
|
|
67
71
|
catch ( error ) {
|
|
72
|
+
if ( config.s3.logs === 'verbose' )
|
|
73
|
+
log ( `S3 [EXISTS]: ${key} on ${bucket} - Does not exist` );
|
|
74
|
+
|
|
68
75
|
return false;
|
|
69
76
|
}
|
|
70
77
|
};
|
|
@@ -73,24 +80,35 @@ exports.exists = async ( key, bucket = config.s3.Bucket ) => {
|
|
|
73
80
|
* Get an object from S3.
|
|
74
81
|
*
|
|
75
82
|
* @param {string} key - The object key.
|
|
83
|
+
* @param {string} [bucket=config.s3.Bucket] - The bucket name. Defaults to the configured bucket.
|
|
76
84
|
* @returns {Promise} A promise that resolves to the retrieved object.
|
|
77
85
|
* @description Retrieves an object from S3 based on the provided key.
|
|
78
86
|
*/
|
|
79
|
-
exports.get = async ( key ) => {
|
|
87
|
+
exports.get = async ( key, bucket = config.s3.Bucket ) => {
|
|
80
88
|
try {
|
|
81
89
|
const command = new GetObjectCommand ( {
|
|
82
|
-
Bucket:
|
|
90
|
+
Bucket: bucket,
|
|
83
91
|
Key: key
|
|
84
92
|
} );
|
|
85
93
|
|
|
94
|
+
if ( config.s3.logs === 'verbose' )
|
|
95
|
+
log ( `S3 [GET]: Getting ${bucket}/${key}.` );
|
|
96
|
+
|
|
86
97
|
const response = await S3.send ( command );
|
|
87
98
|
let data = await streamToBuffer ( response.Body );
|
|
88
99
|
|
|
89
100
|
if ( response.ContentEncoding && response.ContentEncoding === 'gzip' ) {
|
|
101
|
+
|
|
102
|
+
if ( config.s3.logs === 'verbose' )
|
|
103
|
+
log ( `S3 [GET]: ${key} on ${bucket} was unzipped (was gzipped).` );
|
|
104
|
+
|
|
90
105
|
data = zlib.unzipSync ( data );
|
|
91
106
|
}
|
|
92
107
|
|
|
93
108
|
if ( response.ContentType !== 'application/json' ) {
|
|
109
|
+
if ( config.s3.logs === 'output' )
|
|
110
|
+
log ( `S3 [GET]: Returned ${response.ContentType} from ${bucket}/${key}.` );
|
|
111
|
+
|
|
94
112
|
return data.toString ( 'utf8' );
|
|
95
113
|
}
|
|
96
114
|
|
|
@@ -108,9 +126,16 @@ exports.get = async ( key ) => {
|
|
|
108
126
|
key,
|
|
109
127
|
Buffer.from ( data.toString (), 'base64' )
|
|
110
128
|
);
|
|
129
|
+
|
|
111
130
|
data = Buffer.from ( decryptedArrayBuffer ).toString ( 'utf8' );
|
|
131
|
+
|
|
132
|
+
if ( config.s3.logs === 'verbose' )
|
|
133
|
+
log ( `S3 [GET]: ${key} on ${bucket} - JSON content was decrypted.` );
|
|
112
134
|
}
|
|
113
135
|
|
|
136
|
+
if ( config.s3.logs === 'output' )
|
|
137
|
+
log ( `S3 [GET]: ${bucket}/${key} - JSON content was returned.` );
|
|
138
|
+
|
|
114
139
|
return JSON.parse ( data.toString ( 'utf8' ) );
|
|
115
140
|
}
|
|
116
141
|
catch ( error ) {
|
|
@@ -148,6 +173,10 @@ exports.set = async ( key, body, options = {} ) => {
|
|
|
148
173
|
} = options;
|
|
149
174
|
|
|
150
175
|
if ( encrypt && contentType === 'application/json' ) {
|
|
176
|
+
|
|
177
|
+
if ( config.s3.logs === 'verbose' )
|
|
178
|
+
log ( `S3 [SET]: ${bucket}/${key} - Encrypting.` );
|
|
179
|
+
|
|
151
180
|
const encoder = new TextEncoder ();
|
|
152
181
|
const data = encoder.encode ( body );
|
|
153
182
|
|
|
@@ -182,6 +211,9 @@ exports.set = async ( key, body, options = {} ) => {
|
|
|
182
211
|
} );
|
|
183
212
|
|
|
184
213
|
const data = await S3.send ( command );
|
|
214
|
+
|
|
215
|
+
if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' )
|
|
216
|
+
log ( `S3 [SET]: ${bucket}/${key} - Stored.` );
|
|
185
217
|
|
|
186
218
|
return data;
|
|
187
219
|
}
|
|
@@ -208,6 +240,9 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
|
|
|
208
240
|
|
|
209
241
|
const data = await S3.send ( command );
|
|
210
242
|
|
|
243
|
+
if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' )
|
|
244
|
+
log ( `S3 [DELETE]: ${key} on ${bucket} - Deleted.` );
|
|
245
|
+
|
|
211
246
|
return data;
|
|
212
247
|
}
|
|
213
248
|
catch ( error ) {
|
|
@@ -250,6 +285,15 @@ exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
|
|
|
250
285
|
} );
|
|
251
286
|
|
|
252
287
|
await S3.send ( deleteCommand );
|
|
288
|
+
|
|
289
|
+
if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' ){
|
|
290
|
+
if ( sourceBucket === destinationBucket ){
|
|
291
|
+
log ( `S3 [MOVE]: ${sourceKey} moved to ${destinationKey} on ${sourceBucket}.` );
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
log ( `S3 [MOVE]: ${sourceKey} on ${sourceBucket} moved to ${destinationKey} on ${destinationBucket}.` );
|
|
295
|
+
}
|
|
296
|
+
}
|
|
253
297
|
}
|
|
254
298
|
catch ( error ) {
|
|
255
299
|
throw error;
|
package/src/util/log.js
ADDED