Skip to content

Commit 15e4a0e

Browse files
committed
feat: support async baseUrl
1 parent 15ab6a0 commit 15e4a0e

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
* Security upgrade cookie and parse-server ([#274](https://github.com/parse-community/parse-server-s3-adapter/issues/274)) ([4b6aadc](https://github.com/parse-community/parse-server-s3-adapter/commit/4b6aadc7244dd91d79818c7c6cf752333ee79776))
77

8+
### Features
9+
10+
* Support async `baseUrl` function
11+
812
# [4.1.0](https://github.com/parse-community/parse-server-s3-adapter/compare/4.0.0...4.1.0) (2025-01-03)
913

1014

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ The preferred method is to use the default AWS credentials pattern. If no AWS c
126126
"bucketPrefix": '', // default value
127127
"directAccess": false, // default value
128128
"fileAcl": null, // default value
129-
"baseUrl": null, // default value
129+
"baseUrl": null, // string, function or async function
130130
"baseUrlDirect": false, // default value
131131
"signatureVersion": 'v4', // default value
132132
"globalCacheControl": null, // default value. Or 'public, max-age=86400' for 24 hrs Cache-Control
@@ -227,7 +227,7 @@ var s3Options = {
227227
"region": 'us-east-1', // default value
228228
"bucketPrefix": '', // default value
229229
"directAccess": false, // default value
230-
"baseUrl": null // default value
230+
"baseUrl": null // string, function or async function
231231
"signatureVersion": 'v4', // default value
232232
"globalCacheControl": null, // default value. Or 'public, max-age=86400' for 24 hrs Cache-Control
233233
"presignedUrl": false, // default value

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ const serialize = obj => {
3131
return str.join('&');
3232
};
3333

34-
function buildDirectAccessUrl(baseUrl, baseUrlFileKey, presignedUrl, config, filename) {
35-
let directAccessUrl;
34+
async function buildDirectAccessUrl(baseUrl, baseUrlFileKey, presignedUrl, config, filename) {
35+
let urlBase;
3636
if (typeof baseUrl === 'function') {
37-
directAccessUrl = `${baseUrl(config, filename)}/${baseUrlFileKey}`;
37+
urlBase = await baseUrl(config, filename);
3838
} else {
39-
directAccessUrl = `${baseUrl}/${baseUrlFileKey}`;
39+
urlBase = baseUrl;
4040
}
41+
let directAccessUrl = `${urlBase}/${baseUrlFileKey}`;
4142

4243
if (presignedUrl) {
4344
directAccessUrl += presignedUrl.substring(presignedUrl.indexOf('?'));
@@ -246,7 +247,7 @@ class S3Adapter {
246247
}
247248

248249
const baseUrlFileKey = this._baseUrlDirect ? fileName : fileKey;
249-
return buildDirectAccessUrl(this._baseUrl, baseUrlFileKey, presignedUrl, config, filename);
250+
return await buildDirectAccessUrl(this._baseUrl, baseUrlFileKey, presignedUrl, config, filename);
250251
}
251252

252253
async handleFileStream(filename, req, res) {

spec/test.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,40 @@ describe('S3Adapter tests', () => {
640640
});
641641
});
642642

643+
describe('getFileLocation with async baseUrl', () => {
644+
const testConfig = {
645+
mount: 'http://my.server.com/parse',
646+
applicationId: 'xxxx',
647+
};
648+
let options;
649+
650+
beforeEach(() => {
651+
options = {
652+
directAccess: true,
653+
bucketPrefix: 'foo/bar/',
654+
baseUrl: async () => {
655+
await Promise.resolve();
656+
return 'http://example.com/files';
657+
},
658+
};
659+
});
660+
661+
it('should await async baseUrl', async () => {
662+
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
663+
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
664+
'http://example.com/files/foo/bar/test.png'
665+
);
666+
});
667+
668+
it('should direct to async baseUrl when baseUrlDirect', async () => {
669+
options.baseUrlDirect = true;
670+
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
671+
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
672+
'http://example.com/files/test.png'
673+
);
674+
});
675+
});
676+
643677
describe('validateFilename', () => {
644678
let options;
645679

0 commit comments

Comments
 (0)