monastery 2.2.7 → 2.2.9

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.9](https://github.com/boycce/monastery/compare/2.2.8...2.2.9) (2024-05-02)
6
+
7
+ ### [2.2.8](https://github.com/boycce/monastery/compare/2.2.7...2.2.8) (2024-05-01)
8
+
5
9
  ### [2.2.7](https://github.com/boycce/monastery/compare/2.2.6...2.2.7) (2024-05-01)
6
10
 
7
11
  ### [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.9",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
@@ -33,7 +33,7 @@
33
33
  "@aws-sdk/lib-storage": "3.549.0",
34
34
  "@aws-sdk/s3-request-presigner": "3.549.0",
35
35
  "debug": "4.3.4",
36
- "file-type": "^16.5.4",
36
+ "file-type": "16.5.4",
37
37
  "mongodb": "^3.2.3",
38
38
  "monk": "7.3.4",
39
39
  "nanoid": "3.2.0",
@@ -50,8 +50,7 @@
50
50
  "supertest": "4.0.2"
51
51
  },
52
52
  "engines": {
53
- "node": ">=14",
54
- "npm": ">=6"
53
+ "node": ">=14"
55
54
  },
56
55
  "standard-version": {
57
56
  "infile": "changelog.md",
@@ -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,19 @@ 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 = '_s3Client'// useRegion ? '_s3ClientRegional' : '_s3Client'
54
+ return this[key] || (this[key] = new S3({
55
+ // ...(region: useRegion ? this.awsRegion : undefined,
56
+ region: this.awsRegion, // if region is missing it throws an error, but only in production...
55
57
  credentials: {
56
58
  accessKeyId: this.awsAccessKeyId,
57
59
  secretAccessKey: this.awsSecretAccessKey
@@ -230,11 +232,11 @@ let plugin = module.exports = {
230
232
  for (let doc of util.toArray(data)) {
231
233
  for (let imageField of this.imageFields) {
232
234
  if (options.getSignedUrls
233
- || (util.isDefined(imageField.getSignedUrl) ? imageField.getSignedUrl : plugin.getSignedUrl)) {
235
+ || (util.isDefined(imageField.getSignedUrl) ? imageField.getSignedUrl : plugin.getSignedUrlOption)) {
234
236
  let images = plugin._findImagesInData(doc, imageField, 0, '').filter(o => o.image)
235
237
  // todo: we could do this in parallel
236
238
  for (let image of images) {
237
- image.image.signedUrl = await plugin._getSignedUrl(image.image.path, 3600, imageField.awsBucket)
239
+ image.image.signedUrl = await plugin.getSignedUrl(image.image.path, 3600, imageField.awsBucket)
238
240
  }
239
241
  }
240
242
  }
@@ -589,24 +591,24 @@ let plugin = module.exports = {
589
591
  return list
590
592
  },
591
593
 
592
- _getSignedUrl: async (path, expires=3600, bucket) => {
594
+ getSignedUrl: async (path, expires=3600, bucket) => {
593
595
  /**
594
596
  * @param {string} path - aws file path
595
597
  * @param {number} <expires> - seconds
596
- * @param {number} <bucket>
598
+ * @param {string} <bucket>
597
599
  * @return {promise} signedUrl
598
600
  * @see v2: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property
599
601
  * @see v3: https://github.com/aws/aws-sdk-js-v3/blob/main/UPGRADING.md#s3-presigned-url
600
602
  */
601
603
  if (!plugin.getS3Client) {
602
604
  throw new Error(
603
- 'To use db._getSignedUrl(), the imagePlugin manager option must be defined, e.g. `monastery(..., { imagePlugin })`'
605
+ 'To use db.getSignedUrl(), the imagePlugin manager option must be defined, e.g. `monastery(..., { imagePlugin })`'
604
606
  )
605
607
  }
606
608
  const { GetObjectCommand } = require('@aws-sdk/client-s3')
607
609
  const params = { Bucket: bucket || plugin.awsBucket, Key: path }
608
610
  const command = new GetObjectCommand(params)
609
- let signedUrl = await getSignedUrl(plugin.getS3Client(), command, { expiresIn: expires })
611
+ let signedUrl = await getSignedUrl(plugin.getS3Client(true), command, { expiresIn: expires })
610
612
  // console.log(signedUrl)
611
613
  return signedUrl
612
614
  },
@@ -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