Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/helix-shared-storage/src/storage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export interface ListOptions {
* whether to list shallow i.e. not recursive
*/
shallow: boolean;
/**
* whether to include prefixes (for subfolders)
*/
prefixes: boolean;
/**
* max number of items to return
*/
Expand Down
12 changes: 11 additions & 1 deletion packages/helix-shared-storage/src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class Bucket {
*/
async list(prefix, opts = false) {
const {
shallow = false, maxItems = Number.POSITIVE_INFINITY,
shallow = false, maxItems = Number.POSITIVE_INFINITY, prefixes = false,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use includePrefixes ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming is hard 😉
let me change it to be includePrefixes before anyone is actually using it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in PR: #1169

} = typeof opts === 'boolean' ? { shallow: opts } : opts;

let ContinuationToken;
Expand All @@ -526,6 +526,16 @@ class Bucket {
// eslint-disable-next-line no-await-in-loop
const result = await this.client.send(new ListObjectsV2Command(input));
ContinuationToken = result.IsTruncated ? result.NextContinuationToken : '';

if (prefixes) {
(result.CommonPrefixes || []).forEach(({ Prefix }) => {
objects.push({
key: Prefix,
path: `${Prefix.substring(prefix.length)}`,
});
});
}

(result.Contents || []).forEach((content) => {
const key = content.Key;
objects.push({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"ListBucketResult": {
"Name": "helix-code-bus",
"Prefix": "",
"NextContinuationToken": "next",
"KeyCount": 1,
"MaxKeys": 1,
"Delimiter": "/",
"IsTruncated": false,
"Contents": [{
"Key": "/owner/repo/ref/myfolder/somefile.html",
"LastModified": "2021-06-06T04:05:06.000Z",
"Size": "999999"
}],
"CommonPrefixes": [{
"Prefix": "/owner/repo/ref/myfolder/sub1/"
}, {
"Prefix": "/owner/repo/ref/myfolder/sub2/"
}]
}
}
50 changes: 49 additions & 1 deletion packages/helix-shared-storage/test/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ describe('Storage test', () => {
.reply(200, new xml2js.Builder().buildObject(listReply));

const bus = storage.codeBus();
const folders = await bus.list('/owner/repo/ref/');
const folders = await bus.list('/owner/repo/ref/', { prefixes: true });

assert.deepStrictEqual(folders, [
{
Expand All @@ -1150,6 +1150,54 @@ describe('Storage test', () => {
]);
});

it('can list to include subfolder prefixes', async () => {
const listReply = JSON.parse(await fs.readFile(path.resolve(__testdir, 'fixtures', 'list-subfolder-prefixes.json'), 'utf-8'));
nock('https://helix-code-bus.s3.fake.amazonaws.com')
.get('/?list-type=2&prefix=%2Fowner%2Frepo%2Fref%2Fmyfolder%2F')
.reply(200, new xml2js.Builder().buildObject(listReply));

const bus = storage.codeBus();
const folders = await bus.list('/owner/repo/ref/myfolder/', { prefixes: true });

assert.deepStrictEqual(folders, [
{
key: '/owner/repo/ref/myfolder/sub1/',
path: 'sub1/',
},
{
key: '/owner/repo/ref/myfolder/sub2/',
path: 'sub2/',
},
{
contentLength: 999999,
contentType: 'text/html',
key: '/owner/repo/ref/myfolder/somefile.html',
lastModified: new Date('2021-06-06T04:05:06.000Z'),
path: 'somefile.html',
},
]);
});

it('can list to not include subfolder prefixes', async () => {
const listReply = JSON.parse(await fs.readFile(path.resolve(__testdir, 'fixtures', 'list-subfolder-prefixes.json'), 'utf-8'));
nock('https://helix-code-bus.s3.fake.amazonaws.com')
.get('/?list-type=2&prefix=%2Fowner%2Frepo%2Fref%2Fmyfolder%2F')
.reply(200, new xml2js.Builder().buildObject(listReply));

const bus = storage.codeBus();
const folders = await bus.list('/owner/repo/ref/myfolder/');

assert.deepStrictEqual(folders, [
{
contentLength: 999999,
contentType: 'text/html',
key: '/owner/repo/ref/myfolder/somefile.html',
lastModified: new Date('2021-06-06T04:05:06.000Z'),
path: 'somefile.html',
},
]);
});

it('can list fewer items than are available', async () => {
const listReply = JSON.parse(await fs.readFile(path.resolve(__testdir, 'fixtures', 'list-truncated-reply.json'), 'utf-8'));
nock('https://helix-code-bus.s3.fake.amazonaws.com')
Expand Down