monastery 2.2.6 → 2.2.8

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/changelog.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [2.2.8](https://github.com/boycce/monastery/compare/2.2.7...2.2.8) (2024-05-01)
6
+
7
+ ### [2.2.7](https://github.com/boycce/monastery/compare/2.2.6...2.2.7) (2024-05-01)
8
+
5
9
  ### [2.2.6](https://github.com/boycce/monastery/compare/2.2.5...2.2.6) (2024-04-30)
6
10
 
7
11
  ### [2.2.5](https://github.com/boycce/monastery/compare/2.2.4...2.2.5) (2024-04-30)
@@ -12,7 +12,7 @@ To use the default image plugin shipped with monastery, you need to use the opti
12
12
  imagePlugin: {
13
13
  awsAcl: 'public-read', // default
14
14
  awsBucket: 'your-bucket-name',
15
- awsRegion: undefined, // required when using getSignedUrl (e.g. 's3-ap-southeast-2')
15
+ awsRegion: undefined, // e.g. 'ap-southeast-2'
16
16
  awsAccessKeyId: 'your-key-here',
17
17
  awsSecretAccessKey: 'your-key-here',
18
18
  filesize: undefined, // default (max filesize in bytes)
@@ -54,6 +54,7 @@ monastery('localhost/mydb,192.168.1.1').then((db) => {
54
54
  - `manager.isId(String|ObjectId)`: Checks if the passed variable is a valid MongoDB ObjectId or ObjectId string
55
55
  - `manager.model()`: [see model](./model.html)
56
56
  - `manager.models()`: [see models](./models.html)
57
+ - `manager.getSignedUrl(path, expires, bucket)`: You can sign AWS S3 paths using this image plugin helper
57
58
 
58
59
  ### Dates
59
60
 
package/lib/index.js CHANGED
@@ -64,6 +64,8 @@ module.exports = function(uri, opts, fn) {
64
64
  }
65
65
 
66
66
  // Initiate any plugins
67
+
68
+ manager.getSignedUrl = manager._getSignedUrl = manager.imagePluginFile.getSignedUrl
67
69
  if (manager.imagePlugin) {
68
70
  manager.imagePluginFile.setup(manager, util.isObject(manager.imagePlugin)? manager.imagePlugin : {})
69
71
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "monastery",
3
3
  "description": "⛪ A straight forward MongoDB ODM built around Monk",
4
4
  "author": "Ricky Boyce",
5
- "version": "2.2.6",
5
+ "version": "2.2.8",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
@@ -31,27 +31,28 @@ let plugin = module.exports = {
31
31
  this.bucketDir = options.bucketDir || 'full' // depreciated > 1.36.2
32
32
  this.filesize = options.filesize
33
33
  this.formats = options.formats || ['bmp', 'gif', 'jpg', 'jpeg', 'png', 'tiff']
34
- this.getSignedUrl = options.getSignedUrl
34
+ this.getSignedUrlOption = options.getSignedUrl
35
35
  this.manager = manager
36
36
  this.metadata = options.metadata ? util.deepCopy(options.metadata) : undefined,
37
37
  this.params = options.params ? util.deepCopy(options.params) : {},
38
38
  this.path = options.path || function (uid, basename, ext, file) { return `full/${uid}.${ext}` }
39
39
 
40
40
  if (!options.awsBucket || !options.awsAccessKeyId || !options.awsSecretAccessKey) {
41
- manager.error('Monastery imagePlugin: awsBucket, awsAccessKeyId, or awsSecretAccessKey is not defined')
42
- delete manager.imagePlugin
43
- return
41
+ throw new Error('Monastery imagePlugin: awsRegion, awsBucket, awsAccessKeyId, or awsSecretAccessKey is not defined')
42
+ }
43
+ if (!options.awsRegion) {
44
+ throw new Error('Monastery imagePlugin: v3 requires awsRegion to be defined for signing urls, e.g. \'ap-southeast-2\'')
44
45
  }
45
46
 
46
47
  // Create s3 'service' instance (defer require since it takes 120ms to load)
47
48
  // v2: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
48
49
  // v3: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/
49
50
  // v3 examples: https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/javascript_s3_code_examples.html
50
- manager._getSignedUrl = this._getSignedUrl
51
- this.getS3Client = () => {
51
+ this.getS3Client = (useRegion) => {
52
52
  const { S3 } = require('@aws-sdk/client-s3')
53
- return this._s3Client || (this._s3Client = new S3({
54
- region: this.awsRegion,
53
+ const key = useRegion ? '_s3ClientRegional' : '_s3Client'
54
+ return this[key] || (this[key] = new S3({
55
+ region: useRegion ? this.awsRegion : undefined,
55
56
  credentials: {
56
57
  accessKeyId: this.awsAccessKeyId,
57
58
  secretAccessKey: this.awsSecretAccessKey
@@ -230,11 +231,11 @@ let plugin = module.exports = {
230
231
  for (let doc of util.toArray(data)) {
231
232
  for (let imageField of this.imageFields) {
232
233
  if (options.getSignedUrls
233
- || (util.isDefined(imageField.getSignedUrl) ? imageField.getSignedUrl : plugin.getSignedUrl)) {
234
+ || (util.isDefined(imageField.getSignedUrl) ? imageField.getSignedUrl : plugin.getSignedUrlOption)) {
234
235
  let images = plugin._findImagesInData(doc, imageField, 0, '').filter(o => o.image)
235
236
  // todo: we could do this in parallel
236
237
  for (let image of images) {
237
- image.image.signedUrl = await plugin._getSignedUrl(image.image.path, 3600, imageField.awsBucket)
238
+ image.image.signedUrl = await plugin.getSignedUrl(image.image.path, 3600, imageField.awsBucket)
238
239
  }
239
240
  }
240
241
  }
@@ -589,22 +590,24 @@ let plugin = module.exports = {
589
590
  return list
590
591
  },
591
592
 
592
- _getSignedUrl: async (path, expires=3600, bucket) => {
593
+ getSignedUrl: async (path, expires=3600, bucket) => {
593
594
  /**
594
595
  * @param {string} path - aws file path
595
596
  * @param {number} <expires> - seconds
596
- * @param {number} <bucket>
597
+ * @param {string} <bucket>
597
598
  * @return {promise} signedUrl
598
599
  * @see v2: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property
599
600
  * @see v3: https://github.com/aws/aws-sdk-js-v3/blob/main/UPGRADING.md#s3-presigned-url
600
601
  */
601
- if (!plugin.awsRegion) {
602
- throw 'Monastery requires config.awsRegion to be defined when using getSignedUrl\'s'
602
+ if (!plugin.getS3Client) {
603
+ throw new Error(
604
+ 'To use db.getSignedUrl(), the imagePlugin manager option must be defined, e.g. `monastery(..., { imagePlugin })`'
605
+ )
603
606
  }
604
607
  const { GetObjectCommand } = require('@aws-sdk/client-s3')
605
608
  const params = { Bucket: bucket || plugin.awsBucket, Key: path }
606
609
  const command = new GetObjectCommand(params)
607
- let signedUrl = await getSignedUrl(plugin.getS3Client(), command, { expiresIn: expires })
610
+ let signedUrl = await getSignedUrl(plugin.getS3Client(true), command, { expiresIn: expires })
608
611
  // console.log(signedUrl)
609
612
  return signedUrl
610
613
  },
@@ -1,4 +1,5 @@
1
1
  let util = require('../lib/util.js')
2
+ const imagePluginFakeOpts = { awsBucket: 'fake', awsRegion: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
2
3
 
3
4
  module.exports = function(monastery, opendb) {
4
5
 
@@ -58,7 +59,7 @@ module.exports = function(monastery, opendb) {
58
59
  let db = (await opendb(null, {
59
60
  timestamps: false,
60
61
  serverSelectionTimeoutMS: 2000,
61
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
62
+ imagePlugin: imagePluginFakeOpts
62
63
  })).db
63
64
 
64
65
  let user = db.model('user', { fields: {
@@ -68,7 +69,7 @@ module.exports = function(monastery, opendb) {
68
69
  }})
69
70
 
70
71
  // Initialisation success
71
- expect(db.imagePlugin).toEqual({ awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' })
72
+ expect(db.imagePlugin).toEqual(imagePluginFakeOpts)
72
73
 
73
74
  let expected = {
74
75
  bucket: { type: 'string', isString: true },
@@ -96,7 +97,7 @@ module.exports = function(monastery, opendb) {
96
97
  let db = (await opendb(null, {
97
98
  timestamps: false,
98
99
  serverSelectionTimeoutMS: 2000,
99
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
100
+ imagePlugin: imagePluginFakeOpts
100
101
  })).db
101
102
  let plugin = db.imagePluginFile
102
103
  db.model('user', { fields: {
@@ -147,7 +148,7 @@ module.exports = function(monastery, opendb) {
147
148
  let db = (await opendb(null, {
148
149
  timestamps: false,
149
150
  serverSelectionTimeoutMS: 2000,
150
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
151
+ imagePlugin: imagePluginFakeOpts
151
152
  })).db
152
153
 
153
154
  let user = db.model('user', { fields: {
@@ -255,7 +256,7 @@ module.exports = function(monastery, opendb) {
255
256
  let db = (await opendb(null, {
256
257
  timestamps: false,
257
258
  serverSelectionTimeoutMS: 2000,
258
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
259
+ imagePlugin: imagePluginFakeOpts
259
260
  })).db
260
261
 
261
262
  let user = db.model('user', { fields: {
@@ -341,7 +342,7 @@ module.exports = function(monastery, opendb) {
341
342
  let db = (await opendb(null, {
342
343
  timestamps: false,
343
344
  serverSelectionTimeoutMS: 2000,
344
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
345
+ imagePlugin: imagePluginFakeOpts
345
346
  })).db
346
347
 
347
348
  let user = db.model('user', { fields: {
@@ -392,7 +393,7 @@ module.exports = function(monastery, opendb) {
392
393
  let db = (await opendb(null, {
393
394
  timestamps: false,
394
395
  serverSelectionTimeoutMS: 2000,
395
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
396
+ imagePlugin: imagePluginFakeOpts
396
397
  })).db
397
398
 
398
399
  db.model('user', { fields: {
@@ -470,7 +471,7 @@ module.exports = function(monastery, opendb) {
470
471
  let db = (await opendb(null, {
471
472
  timestamps: false,
472
473
  serverSelectionTimeoutMS: 2000,
473
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
474
+ imagePlugin: imagePluginFakeOpts
474
475
  })).db
475
476
 
476
477
  let user = db.model('user', { fields: {
@@ -524,7 +525,7 @@ module.exports = function(monastery, opendb) {
524
525
  let db = (await opendb(null, {
525
526
  timestamps: false,
526
527
  serverSelectionTimeoutMS: 2000,
527
- imagePlugin: { awsBucket: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
528
+ imagePlugin: imagePluginFakeOpts
528
529
  })).db
529
530
 
530
531
  let user = db.model('user', { fields: {
@@ -621,11 +622,7 @@ module.exports = function(monastery, opendb) {
621
622
  let db = (await opendb(null, {
622
623
  timestamps: false,
623
624
  serverSelectionTimeoutMS: 2000,
624
- imagePlugin: {
625
- awsBucket: 'fake',
626
- awsAccessKeyId: 'fake',
627
- awsSecretAccessKey: 'fake',
628
- }
625
+ imagePlugin: imagePluginFakeOpts
629
626
  })).db
630
627
 
631
628
  let user = db.model('user', {
@@ -645,7 +642,7 @@ module.exports = function(monastery, opendb) {
645
642
  expect(plugin.awsAcl).toEqual('public-read')
646
643
  expect(plugin.filesize).toEqual(undefined)
647
644
  expect(plugin.formats).toEqual(['bmp', 'gif', 'jpg', 'jpeg', 'png', 'tiff'])
648
- expect(plugin.getSignedUrl).toEqual(undefined)
645
+ expect(plugin.getSignedUrlOption).toEqual(undefined)
649
646
  expect(plugin.metadata).toEqual(undefined)
650
647
  expect(plugin.path).toEqual(expect.any(Function))
651
648
  expect(plugin.params).toEqual({})
@@ -717,9 +714,7 @@ module.exports = function(monastery, opendb) {
717
714
  timestamps: false,
718
715
  serverSelectionTimeoutMS: 2000,
719
716
  imagePlugin: {
720
- awsBucket: 'fake',
721
- awsAccessKeyId: 'fake',
722
- awsSecretAccessKey: 'fake',
717
+ ...imagePluginFakeOpts,
723
718
  formats: ['jpg', 'jpeg', 'png', 'ico'],
724
719
  filesize: 1000 * 270,
725
720
  }
@@ -800,10 +795,8 @@ module.exports = function(monastery, opendb) {
800
795
  timestamps: false,
801
796
  serverSelectionTimeoutMS: 2000,
802
797
  imagePlugin: {
803
- awsBucket: 'fake',
804
- awsAccessKeyId: 'fake',
805
- awsSecretAccessKey: 'fake',
806
- awsRegion: 's3-ap-southeast-2',
798
+ ...imagePluginFakeOpts,
799
+ awsRegion: 'ap-southeast-2',
807
800
  getSignedUrl: true,
808
801
  },
809
802
  })).db
@@ -861,10 +854,8 @@ module.exports = function(monastery, opendb) {
861
854
  timestamps: false,
862
855
  serverSelectionTimeoutMS: 2000,
863
856
  imagePlugin: {
857
+ ...imagePluginFakeOpts,
864
858
  awsAcl: 'private',
865
- awsBucket: 'fake',
866
- awsAccessKeyId: 'fake',
867
- awsSecretAccessKey: 'fake',
868
859
  metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
869
860
  params: { ContentLanguage: 'DE'},
870
861
  path: (uid, basename, ext, file) => `images/${basename}`,
@@ -966,9 +957,7 @@ module.exports = function(monastery, opendb) {
966
957
  timestamps: false,
967
958
  serverSelectionTimeoutMS: 2000,
968
959
  imagePlugin: {
969
- awsBucket: 'fake',
970
- awsAccessKeyId: 'fake',
971
- awsSecretAccessKey: 'fake',
960
+ ...imagePluginFakeOpts,
972
961
  bucketDir: 'old',
973
962
  }
974
963
  })).db