monastery 2.2.7 → 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,8 @@
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
+
5
7
  ### [2.2.7](https://github.com/boycce/monastery/compare/2.2.6...2.2.7) (2024-05-01)
6
8
 
7
9
  ### [2.2.6](https://github.com/boycce/monastery/compare/2.2.5...2.2.6) (2024-04-30)
@@ -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,7 +64,8 @@ module.exports = function(uri, opts, fn) {
64
64
  }
65
65
 
66
66
  // Initiate any plugins
67
- manager._getSignedUrl = manager.imagePluginFile._getSignedUrl
67
+
68
+ manager.getSignedUrl = manager._getSignedUrl = manager.imagePluginFile.getSignedUrl
68
69
  if (manager.imagePlugin) {
69
70
  manager.imagePluginFile.setup(manager, util.isObject(manager.imagePlugin)? manager.imagePlugin : {})
70
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.7",
5
+ "version": "2.2.8",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
@@ -31,7 +31,7 @@ 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) : {},
@@ -41,17 +41,18 @@ let plugin = module.exports = {
41
41
  throw new Error('Monastery imagePlugin: awsRegion, awsBucket, awsAccessKeyId, or awsSecretAccessKey is not defined')
42
42
  }
43
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
+ throw new Error('Monastery imagePlugin: v3 requires awsRegion to be defined for signing urls, e.g. \'ap-southeast-2\'')
45
45
  }
46
46
 
47
47
  // Create s3 'service' instance (defer require since it takes 120ms to load)
48
48
  // v2: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
49
49
  // v3: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/
50
50
  // v3 examples: https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/javascript_s3_code_examples.html
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,24 +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
602
  if (!plugin.getS3Client) {
602
603
  throw new Error(
603
- 'To use db._getSignedUrl(), the imagePlugin manager option must be defined, e.g. `monastery(..., { imagePlugin })`'
604
+ 'To use db.getSignedUrl(), the imagePlugin manager option must be defined, e.g. `monastery(..., { imagePlugin })`'
604
605
  )
605
606
  }
606
607
  const { GetObjectCommand } = require('@aws-sdk/client-s3')
607
608
  const params = { Bucket: bucket || plugin.awsBucket, Key: path }
608
609
  const command = new GetObjectCommand(params)
609
- let signedUrl = await getSignedUrl(plugin.getS3Client(), command, { expiresIn: expires })
610
+ let signedUrl = await getSignedUrl(plugin.getS3Client(true), command, { expiresIn: expires })
610
611
  // console.log(signedUrl)
611
612
  return signedUrl
612
613
  },
@@ -642,7 +642,7 @@ module.exports = function(monastery, opendb) {
642
642
  expect(plugin.awsAcl).toEqual('public-read')
643
643
  expect(plugin.filesize).toEqual(undefined)
644
644
  expect(plugin.formats).toEqual(['bmp', 'gif', 'jpg', 'jpeg', 'png', 'tiff'])
645
- expect(plugin.getSignedUrl).toEqual(undefined)
645
+ expect(plugin.getSignedUrlOption).toEqual(undefined)
646
646
  expect(plugin.metadata).toEqual(undefined)
647
647
  expect(plugin.path).toEqual(expect.any(Function))
648
648
  expect(plugin.params).toEqual({})
@@ -796,7 +796,7 @@ module.exports = function(monastery, opendb) {
796
796
  serverSelectionTimeoutMS: 2000,
797
797
  imagePlugin: {
798
798
  ...imagePluginFakeOpts,
799
- awsRegion: 's3-ap-southeast-2',
799
+ awsRegion: 'ap-southeast-2',
800
800
  getSignedUrl: true,
801
801
  },
802
802
  })).db