eip-cloud-services 1.0.3 → 1.0.5
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 +63 -2
- 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.5",
|
|
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.cdn.logFunction ? require ( config.cdn.logFunction ) : console.log;
|
|
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,6 +42,7 @@ 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
|
+
const log = config.s3.logFunction ? require ( config.s3.logFunction ) : console.log;
|
|
45
46
|
|
|
46
47
|
const S3 = new S3Client ( { region: 'eu-west-1' } );
|
|
47
48
|
|
|
@@ -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,26 +80,61 @@ 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
|
}
|
|
114
|
+
|
|
115
|
+
if ( response.ContentType === 'application/json' && response.Metadata[ 'tmg-crypt' ] && response.Metadata[ 'tmg-crypt-vec' ] ) {
|
|
116
|
+
const key = await crypto.subtle.importKey (
|
|
117
|
+
'raw',
|
|
118
|
+
Buffer.from ( response.Metadata[ 'tmg-crypt' ], 'base64' ),
|
|
119
|
+
{ name: 'AES-CBC', length: 256 },
|
|
120
|
+
false,
|
|
121
|
+
[ 'decrypt' ]
|
|
122
|
+
);
|
|
123
|
+
const iv = Buffer.from ( response.Metadata[ 'tmg-crypt-vec' ], 'base64' );
|
|
124
|
+
const decryptedArrayBuffer = await crypto.subtle.decrypt (
|
|
125
|
+
{ name: 'AES-CBC', iv },
|
|
126
|
+
key,
|
|
127
|
+
Buffer.from ( data.toString (), 'base64' )
|
|
128
|
+
);
|
|
129
|
+
|
|
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.` );
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if ( config.s3.logs === 'output' )
|
|
137
|
+
log ( `S3 [GET]: ${bucket}/${key} - JSON content was returned.` );
|
|
96
138
|
|
|
97
139
|
return JSON.parse ( data.toString ( 'utf8' ) );
|
|
98
140
|
}
|
|
@@ -131,6 +173,10 @@ exports.set = async ( key, body, options = {} ) => {
|
|
|
131
173
|
} = options;
|
|
132
174
|
|
|
133
175
|
if ( encrypt && contentType === 'application/json' ) {
|
|
176
|
+
|
|
177
|
+
if ( config.s3.logs === 'verbose' )
|
|
178
|
+
log ( `S3 [SET]: ${bucket}/${key} - Encrypting.` );
|
|
179
|
+
|
|
134
180
|
const encoder = new TextEncoder ();
|
|
135
181
|
const data = encoder.encode ( body );
|
|
136
182
|
|
|
@@ -165,6 +211,9 @@ exports.set = async ( key, body, options = {} ) => {
|
|
|
165
211
|
} );
|
|
166
212
|
|
|
167
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.` );
|
|
168
217
|
|
|
169
218
|
return data;
|
|
170
219
|
}
|
|
@@ -191,6 +240,9 @@ exports.del = async ( key, bucket = config.s3.Bucket ) => {
|
|
|
191
240
|
|
|
192
241
|
const data = await S3.send ( command );
|
|
193
242
|
|
|
243
|
+
if ( config.s3.logs === 'outputs' || config.s3.logs === 'verbose' )
|
|
244
|
+
log ( `S3 [DELETE]: ${key} on ${bucket} - Deleted.` );
|
|
245
|
+
|
|
194
246
|
return data;
|
|
195
247
|
}
|
|
196
248
|
catch ( error ) {
|
|
@@ -233,6 +285,15 @@ exports.move = async ( sourceKey, destinationKey, sourceBucket = config.s3.Bucke
|
|
|
233
285
|
} );
|
|
234
286
|
|
|
235
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
|
+
}
|
|
236
297
|
}
|
|
237
298
|
catch ( error ) {
|
|
238
299
|
throw error;
|
package/src/util/log.js
ADDED