Skip to content

Commit bf88785

Browse files
committed
Allow async key generation and revise test
1 parent b7d124f commit bf88785

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@ class S3Adapter {
141141
// For a given config object, filename, and data, store a file in S3
142142
// Returns a promise containing the S3 object creation response
143143
async createFile(filename, data, contentType, options = {}, config = {}) {
144-
145144
let key_without_prefix = filename;
146-
if (this._generateKey instanceof Function) {
147-
key_without_prefix = this._generateKey(filename, contentType, options);
145+
if (typeof this._generateKey === 'function') {
146+
const candidate = this._generateKey(filename, contentType, options);
147+
key_without_prefix =
148+
candidate && typeof candidate.then === 'function' ? await candidate : candidate;
149+
if (typeof key_without_prefix !== 'string' || key_without_prefix.length === 0) {
150+
throw new Error('generateKey must return a non-empty string');
151+
}
148152
}
149153

150154
const params = {

spec/test.spec.js

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,15 +905,96 @@ describe('S3Adapter tests', () => {
905905
const options = {
906906
bucket: 'bucket-1',
907907
generateKey: () => {
908-
throw 'Generate key failed';
908+
throw new Error('Generate key failed');
909909
}
910910
};
911911
const s3 = new S3Adapter(options);
912912
s3._s3Client = s3ClientMock;
913913

914914
await expectAsync(
915915
s3.createFile('file.txt', 'hello world', 'text/utf8', {})
916-
).toBeRejectedWith('Generate key failed');
916+
).toBeRejectedWithError('Generate key failed');
917+
});
918+
919+
it('should handle async generateKey function', async () => {
920+
const options = {
921+
bucket: 'bucket-1',
922+
generateKey: async (filename) => {
923+
// Simulate async operation
924+
await new Promise(resolve => setTimeout(resolve, 10));
925+
return `async-${filename}`;
926+
}
927+
};
928+
const s3 = new S3Adapter(options);
929+
s3ClientMock.send.and.returnValue(Promise.resolve({}));
930+
s3._s3Client = s3ClientMock;
931+
932+
await s3.createFile('file.txt', 'hello world', 'text/utf8', {});
933+
934+
expect(s3ClientMock.send).toHaveBeenCalledTimes(2);
935+
const commands = s3ClientMock.send.calls.all();
936+
const commandArg = commands[1].args[0];
937+
expect(commandArg).toBeInstanceOf(PutObjectCommand);
938+
expect(commandArg.input.Key).toBe('async-file.txt');
939+
});
940+
941+
it('should handle generateKey that returns a Promise', async () => {
942+
const options = {
943+
bucket: 'bucket-1',
944+
generateKey: (filename) => {
945+
return Promise.resolve(`promise-${filename}`);
946+
}
947+
};
948+
const s3 = new S3Adapter(options);
949+
s3ClientMock.send.and.returnValue(Promise.resolve({}));
950+
s3._s3Client = s3ClientMock;
951+
952+
await s3.createFile('file.txt', 'hello world', 'text/utf8', {});
953+
954+
expect(s3ClientMock.send).toHaveBeenCalledTimes(2);
955+
const commands = s3ClientMock.send.calls.all();
956+
const commandArg = commands[1].args[0];
957+
expect(commandArg).toBeInstanceOf(PutObjectCommand);
958+
expect(commandArg.input.Key).toBe('promise-file.txt');
959+
});
960+
961+
it('should validate generateKey returns a non-empty string', async () => {
962+
const options = {
963+
bucket: 'bucket-1',
964+
generateKey: () => ''
965+
};
966+
const s3 = new S3Adapter(options);
967+
s3._s3Client = s3ClientMock;
968+
969+
await expectAsync(
970+
s3.createFile('file.txt', 'hello world', 'text/utf8', {})
971+
).toBeRejectedWithError('generateKey must return a non-empty string');
972+
});
973+
974+
it('should validate generateKey returns a string (not number)', async () => {
975+
const options = {
976+
bucket: 'bucket-1',
977+
generateKey: () => 12345
978+
};
979+
const s3 = new S3Adapter(options);
980+
s3._s3Client = s3ClientMock;
981+
982+
await expectAsync(
983+
s3.createFile('file.txt', 'hello world', 'text/utf8', {})
984+
).toBeRejectedWithError('generateKey must return a non-empty string');
985+
});
986+
987+
it('should validate async generateKey returns a string', async () => {
988+
const options = {
989+
bucket: 'bucket-1',
990+
generateKey: async () => null
991+
};
992+
const s3 = new S3Adapter(options);
993+
s3._s3Client = s3ClientMock;
994+
995+
await expectAsync(
996+
s3.createFile('file.txt', 'hello world', 'text/utf8', {})
997+
).toBeRejectedWithError('generateKey must return a non-empty string');
917998
});
918999
});
9191000

0 commit comments

Comments
 (0)