monastery 3.0.12 → 3.0.14
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 +4 -0
- package/docs/manager/index.md +1 -0
- package/lib/index.js +1 -1
- package/package.json +1 -1
- package/plugins/images/index.js +12 -11
- package/test/plugin-images.js +9 -16
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
|
+
### [3.0.14](https://github.com/boycce/monastery/compare/3.0.13...3.0.14) (2024-05-01)
|
|
6
|
+
|
|
7
|
+
### [3.0.13](https://github.com/boycce/monastery/compare/3.0.12...3.0.13) (2024-05-01)
|
|
8
|
+
|
|
5
9
|
### [3.0.12](https://github.com/boycce/monastery/compare/3.0.11...3.0.12) (2024-05-01)
|
|
6
10
|
|
|
7
11
|
### [3.0.11](https://github.com/boycce/monastery/compare/3.0.10...3.0.11) (2024-04-30)
|
package/docs/manager/index.md
CHANGED
|
@@ -60,6 +60,7 @@ db.onError((err) => {
|
|
|
60
60
|
- `manager.models()`: [see models](./models.html)
|
|
61
61
|
- `manager.onError(Function)`: Catches connection errors
|
|
62
62
|
- `manager.onOpen(Function)`: Triggers on successful connection
|
|
63
|
+
- `manager.getSignedUrl(path, expires, bucket)`: You can sign AWS S3 paths using this image plugin helper
|
|
63
64
|
|
|
64
65
|
### Dates
|
|
65
66
|
|
package/lib/index.js
CHANGED
|
@@ -282,7 +282,7 @@ Manager.prototype.parseData = function(obj) {
|
|
|
282
282
|
}
|
|
283
283
|
|
|
284
284
|
Manager.prototype.model = Model
|
|
285
|
-
Manager.prototype._getSignedUrl = imagePluginFile.
|
|
285
|
+
Manager.prototype.getSignedUrl = Manager.prototype._getSignedUrl = imagePluginFile.getSignedUrl
|
|
286
286
|
|
|
287
287
|
inherits(Manager, EventEmitter)
|
|
288
288
|
module.exports = Manager
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "monastery",
|
|
3
3
|
"description": "⛪ A simple, straightforward MongoDB ODM",
|
|
4
4
|
"author": "Ricky Boyce",
|
|
5
|
-
"version": "3.0.
|
|
5
|
+
"version": "3.0.14",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
package/plugins/images/index.js
CHANGED
|
@@ -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.
|
|
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.
|
|
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
|
-
|
|
54
|
-
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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 {
|
|
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.
|
|
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
|
},
|
package/test/plugin-images.js
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
const util = require('../lib/util.js')
|
|
3
3
|
const imagePluginFile = require('../plugins/images/index.js')
|
|
4
4
|
const monastery = require('../lib/index.js')
|
|
5
|
+
const imagePluginFakeOpts = { awsBucket: 'fake', awsRegion: 'fake', awsAccessKeyId: 'fake', awsSecretAccessKey: 'fake' }
|
|
5
6
|
|
|
6
7
|
let db
|
|
7
8
|
afterAll(async () => { db.close() })
|
|
8
9
|
beforeAll(async () => {
|
|
9
10
|
db = monastery('127.0.0.1/monastery', {
|
|
10
11
|
timestamps: false,
|
|
11
|
-
imagePlugin:
|
|
12
|
+
imagePlugin: imagePluginFakeOpts,
|
|
12
13
|
})
|
|
13
14
|
})
|
|
14
15
|
|
|
@@ -69,7 +70,7 @@ test('images initialisation', async () => {
|
|
|
69
70
|
}})
|
|
70
71
|
|
|
71
72
|
// Initialisation success
|
|
72
|
-
expect(db.opts.imagePlugin).toEqual(
|
|
73
|
+
expect(db.opts.imagePlugin).toEqual(imagePluginFakeOpts)
|
|
73
74
|
|
|
74
75
|
let expected = {
|
|
75
76
|
bucket: { type: 'string', isString: true },
|
|
@@ -572,7 +573,7 @@ test('images option defaults', async () => {
|
|
|
572
573
|
expect(imagePluginFile.awsAcl).toEqual('public-read')
|
|
573
574
|
expect(imagePluginFile.filesize).toEqual(undefined)
|
|
574
575
|
expect(imagePluginFile.formats).toEqual(['bmp', 'gif', 'jpg', 'jpeg', 'png', 'tiff'])
|
|
575
|
-
expect(imagePluginFile.
|
|
576
|
+
expect(imagePluginFile.getSignedUrlOption).toEqual(undefined)
|
|
576
577
|
expect(imagePluginFile.metadata).toEqual(undefined)
|
|
577
578
|
expect(imagePluginFile.path).toEqual(expect.any(Function))
|
|
578
579
|
expect(imagePluginFile.params).toEqual({})
|
|
@@ -641,9 +642,7 @@ test('images options formats & filesizes', async () => {
|
|
|
641
642
|
const db3 = monastery('127.0.0.1/monastery', {
|
|
642
643
|
timestamps: false,
|
|
643
644
|
imagePlugin: {
|
|
644
|
-
|
|
645
|
-
awsAccessKeyId: 'fake',
|
|
646
|
-
awsSecretAccessKey: 'fake',
|
|
645
|
+
...imagePluginFakeOpts,
|
|
647
646
|
formats: ['jpg', 'jpeg', 'png', 'ico'],
|
|
648
647
|
filesize: 1000 * 270,
|
|
649
648
|
},
|
|
@@ -722,10 +721,8 @@ test('images option getSignedUrls', async () => {
|
|
|
722
721
|
const db3 = monastery('127.0.0.1/monastery', {
|
|
723
722
|
timestamps: false,
|
|
724
723
|
imagePlugin: {
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
awsSecretAccessKey: 'fake',
|
|
728
|
-
awsRegion: 's3-ap-southeast-2',
|
|
724
|
+
...imagePluginFakeOpts,
|
|
725
|
+
awsRegion: 'ap-southeast-2',
|
|
729
726
|
getSignedUrl: true,
|
|
730
727
|
},
|
|
731
728
|
})
|
|
@@ -781,10 +778,8 @@ test('images options awsAcl, awsBucket, metadata, params, path', async () => {
|
|
|
781
778
|
const db3 = monastery('127.0.0.1/monastery', {
|
|
782
779
|
timestamps: false,
|
|
783
780
|
imagePlugin: {
|
|
781
|
+
...imagePluginFakeOpts,
|
|
784
782
|
awsAcl: 'private',
|
|
785
|
-
awsBucket: 'fake',
|
|
786
|
-
awsAccessKeyId: 'fake',
|
|
787
|
-
awsSecretAccessKey: 'fake',
|
|
788
783
|
metadata: { small: '*x300' , medium: '*x800', large: '*x1200' },
|
|
789
784
|
params: { ContentLanguage: 'DE'},
|
|
790
785
|
path: (uid, basename, ext, file) => `images/${basename}`,
|
|
@@ -884,9 +879,7 @@ test('images option depreciations', async () => {
|
|
|
884
879
|
logLevel: 1,
|
|
885
880
|
timestamps: false,
|
|
886
881
|
imagePlugin: {
|
|
887
|
-
|
|
888
|
-
awsAccessKeyId: 'fake',
|
|
889
|
-
awsSecretAccessKey: 'fake',
|
|
882
|
+
...imagePluginFakeOpts,
|
|
890
883
|
bucketDir: 'old',
|
|
891
884
|
},
|
|
892
885
|
})
|