Skip to content

Commit 6eae9e1

Browse files
authored
feat: Add support for option baseUrl set to async function (#285)
1 parent 15ab6a0 commit 6eae9e1

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,39 @@ describe('S3Adapter tests', () => {
640640
});
641641
});
642642

643+
describe('getFileLocation with async baseUrl', () => {
644+
const testConfig = {
645+
mount: 'http://example.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+
return await Promise.resolve('http://example.com/files');
656+
},
657+
};
658+
});
659+
660+
it('should await async baseUrl', async () => {
661+
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
662+
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
663+
'http://example.com/files/foo/bar/test.png'
664+
);
665+
});
666+
667+
it('should direct to async baseUrl when baseUrlDirect', async () => {
668+
options.baseUrlDirect = true;
669+
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
670+
await expectAsync(s3.getFileLocation(testConfig, 'test.png')).toBeResolvedTo(
671+
'http://example.com/files/test.png'
672+
);
673+
});
674+
});
675+
643676
describe('validateFilename', () => {
644677
let options;
645678

0 commit comments

Comments
 (0)