Skip to content

Commit d4062cb

Browse files
committed
remove deasync and use async getFileLocation
1 parent 0d3e766 commit d4062cb

File tree

4 files changed

+45
-130
lines changed

4 files changed

+45
-130
lines changed

index.js

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const { S3Client, CreateBucketCommand, PutObjectCommand, DeleteObjectCommand, GetObjectCommand } = require('@aws-sdk/client-s3');
66
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
7-
const deasync = require('deasync');
87
const optionsFromArguments = require('./lib/optionsFromArguments');
98

109
const awsCredentialsDeprecationNotice = function awsCredentialsDeprecationNotice() {
@@ -151,7 +150,9 @@ class S3Adapter {
151150
await this.createBucket();
152151
const command = new PutObjectCommand(params);
153152
const response = await this._s3Client.send(command);
154-
return response;
153+
const location = `https://${this._bucket}.s3.${this._region}.amazonaws.com/${params.Key}`;
154+
155+
return Object.assign(response || {}, { Location: location });
155156
}
156157

157158
async deleteFile(filename) {
@@ -182,37 +183,14 @@ class S3Adapter {
182183
}
183184

184185
// Exposed only for testing purposes
185-
getSignedUrlSync(client, command, options) {
186-
let isDone = false;
187-
let signedUrl = '';
188-
let error = null;
189-
190-
getSignedUrl(client, command, options)
191-
.then((url) => {
192-
signedUrl = url;
193-
isDone = true;
194-
})
195-
.catch((err) => {
196-
error = err;
197-
isDone = true;
198-
});
199-
200-
// Block the event loop until the promise resolves
201-
while (!isDone) {
202-
deasync.sleep(10); // Sleep for 100 milliseconds
203-
}
204-
205-
if (error) {
206-
throw error;
207-
}
208-
209-
return signedUrl;
186+
getFileSignedUrl(client, command, options) {
187+
return getSignedUrl(client, command, options);
210188
}
211189

212190
// Generates and returns the location of a file stored in S3 for the given request and filename
213191
// The location is the direct S3 link if the option is set,
214192
// otherwise we serve the file through parse-server
215-
getFileLocation(config, filename) {
193+
async getFileLocation(config, filename) {
216194
const fileName = filename.split('/').map(encodeURIComponent).join('/');
217195
if (!this._directAccess) {
218196
return `${config.mount}/files/${config.applicationId}/${fileName}`;
@@ -226,7 +204,7 @@ class S3Adapter {
226204
const options = this._presignedUrlExpires ? { expiresIn: this._presignedUrlExpires } : {};
227205

228206
const command = new GetObjectCommand(params);
229-
presignedUrl = this.getSignedUrlSync(this._s3Client, command, options);
207+
presignedUrl = await this.getFileSignedUrl(this._s3Client, command, options);
230208

231209
if (!this._baseUrl) {
232210
return presignedUrl;

package-lock.json

Lines changed: 1 addition & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
"homepage": "https://github.com/parse-community/parse-server-s3-adapter#readme",
2727
"dependencies": {
2828
"@aws-sdk/client-s3": "3.626.0",
29-
"@aws-sdk/s3-request-presigner": "3.626.0",
30-
"deasync": "0.1.30"
29+
"@aws-sdk/s3-request-presigner": "3.626.0"
3130
},
3231
"devDependencies": {
3332
"@semantic-release/changelog": "5.0.1",

spec/test.spec.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('S3Adapter tests', () => {
2323
}, options));
2424
} else {
2525
const bucket = 'FAKE_BUCKET';
26+
const region = 'us-east-1';
2627

2728
s3 = new S3Adapter('FAKE_ACCESS_KEY', 'FAKE_SECRET_KEY', bucket, options);
2829

@@ -36,7 +37,7 @@ describe('S3Adapter tests', () => {
3637
objects[Key] = Body;
3738

3839
return Promise.resolve({
39-
Location: `https://${bucket}.s3.amazonaws.com/${Key}`,
40+
Location: `https://${bucket}.s3.${region}.amazonaws.com/${Key}`,
4041
});
4142
}
4243
if (command instanceof DeleteObjectCommand) {
@@ -317,7 +318,7 @@ describe('S3Adapter tests', () => {
317318
});
318319
});
319320

320-
describe('getFileLocation', () => {
321+
describe('getFileLocation with directAccess', () => {
321322
const testConfig = {
322323
mount: 'http://my.server.com/parse',
323324
applicationId: 'xxxx',
@@ -332,30 +333,30 @@ describe('S3Adapter tests', () => {
332333
};
333334
});
334335

335-
it('should get using the baseUrl', () => {
336+
it('should get using the baseUrl', async () => {
336337
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
337-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
338+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
338339
});
339340

340-
it('should get direct to baseUrl', () => {
341+
it('should get direct to baseUrl', async () => {
341342
options.baseUrlDirect = true;
342343
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
343-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
344+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
344345
});
345346

346-
it('should get without directAccess', () => {
347+
it('should get without directAccess', async () => {
347348
options.directAccess = false;
348349
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
349-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
350+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
350351
});
351352

352-
it('should go directly to amazon', () => {
353+
it('should go directly to amazon', async () => {
353354
delete options.baseUrl;
354355
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
355-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('https://my-bucket.s3.amazonaws.com/foo/bar/test.png');
356+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('https://my-bucket.s3.amazonaws.com/foo/bar/test.png');
356357
});
357358
});
358-
describe('getFileLocation', () => {
359+
describe('getFileLocation with baseUrl', () => {
359360
const testConfig = {
360361
mount: 'http://my.server.com/parse',
361362
applicationId: 'xxxx',
@@ -375,30 +376,30 @@ describe('S3Adapter tests', () => {
375376
};
376377
});
377378

378-
it('should get using the baseUrl', () => {
379+
it('should get using the baseUrl', async () => {
379380
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
380-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
381+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
381382
});
382383

383-
it('should get direct to baseUrl', () => {
384+
it('should get direct to baseUrl', async () => {
384385
options.baseUrlDirect = true;
385386
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
386-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
387+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
387388
});
388389

389-
it('should get without directAccess', () => {
390+
it('should get without directAccess', async () => {
390391
options.directAccess = false;
391392
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
392-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
393+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
393394
});
394395

395-
it('should go directly to amazon', () => {
396+
it('should go directly to amazon', async () => {
396397
delete options.baseUrl;
397398
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
398-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('https://my-bucket.s3.amazonaws.com/foo/bar/test.png');
399+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('https://my-bucket.s3.amazonaws.com/foo/bar/test.png');
399400
});
400401
});
401-
describe('getFileLocation', () => {
402+
describe('getFileLocation with presignedUrl', () => {
402403
const testConfig = {
403404
mount: 'http://my.server.com/parse',
404405
applicationId: 'xxxx',
@@ -414,29 +415,29 @@ describe('S3Adapter tests', () => {
414415
};
415416
});
416417

417-
it('should get using the baseUrl', () => {
418+
it('should get using the baseUrl', async () => {
418419
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
419-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
420+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/foo/bar/test.png');
420421
});
421422

422-
it('when use presigned URL should use S3 \'getObject\' operation', () => {
423+
it('when use presigned URL should use S3 \'getObject\' operation', async () => {
423424
options.presignedUrl = true;
424425
const s3 = makeS3Adapter(options);
425426

426427
let getSignedUrlCommand = '';
427-
s3.getSignedUrlSync = (_, command) => {
428+
s3.getFileSignedUrl = (_, command) => {
428429
getSignedUrlCommand = command;
429430
}
430431

431-
s3.getFileLocation(testConfig, 'test.png');
432+
await s3.getFileLocation(testConfig, 'test.png');
432433
expect(getSignedUrlCommand).toBeInstanceOf(GetObjectCommand);
433434
});
434435

435-
it('should get using the baseUrl and amazon using presigned URL', () => {
436+
it('should get using the baseUrl and amazon using presigned URL', async () => {
436437
options.presignedUrl = true;
437438
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
438439

439-
const fileLocation = s3.getFileLocation(testConfig, 'test.png');
440+
const fileLocation = await s3.getFileLocation(testConfig, 'test.png');
440441
expect(fileLocation).toMatch(/^http:\/\/example.com\/files\/foo\/bar\/test.png\?/);
441442
expect(fileLocation).toMatch(/X-Amz-Credential=accessKey%2F\d{8}%2F\w{2}-\w{1,9}-\d%2Fs3%2Faws4_request/);
442443
expect(fileLocation).toMatch(/X-Amz-Date=\d{8}T\d{6}Z/);
@@ -446,30 +447,30 @@ describe('S3Adapter tests', () => {
446447
expect(fileLocation).toContain('X-Amz-SignedHeaders=host');
447448
});
448449

449-
it('should get direct to baseUrl', () => {
450+
it('should get direct to baseUrl', async () => {
450451
options.baseUrlDirect = true;
451452
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
452-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
453+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://example.com/files/test.png');
453454
});
454455

455-
it('should get without directAccess', () => {
456+
it('should get without directAccess', async () => {
456457
options.directAccess = false;
457458
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
458-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
459+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('http://my.server.com/parse/files/xxxx/test.png');
459460
});
460461

461-
it('should go directly to amazon', () => {
462+
it('should go directly to amazon', async () => {
462463
delete options.baseUrl;
463464
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
464-
expect(s3.getFileLocation(testConfig, 'test.png')).toEqual('https://my-bucket.s3.amazonaws.com/foo/bar/test.png');
465+
expect(await s3.getFileLocation(testConfig, 'test.png')).toEqual('https://my-bucket.s3.amazonaws.com/foo/bar/test.png');
465466
});
466467

467-
it('should go directly to amazon using presigned URL', () => {
468+
it('should go directly to amazon using presigned URL', async () => {
468469
delete options.baseUrl;
469470
options.presignedUrl = true;
470471
const s3 = new S3Adapter('accessKey', 'secretKey', 'my-bucket', options);
471472

472-
const fileLocation = s3.getFileLocation(testConfig, 'test.png');
473+
const fileLocation = await s3.getFileLocation(testConfig, 'test.png');
473474
expect(fileLocation).toMatch(/^https:\/\/my-bucket.s3.us-east-1.amazonaws.com\/foo\/bar\/test.png\?/);
474475
expect(fileLocation).toMatch(/X-Amz-Credential=accessKey%2F\d{8}%2Fus-east-1%2Fs3%2Faws4_request/);
475476
expect(fileLocation).toMatch(/X-Amz-Date=\d{8}T\d{6}Z/);

0 commit comments

Comments
 (0)