Skip to content

Commit

Permalink
Because I do not know how to read docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
bstopp committed Aug 29, 2024
1 parent e89ec8d commit 49e8390
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 88 deletions.
32 changes: 0 additions & 32 deletions src/storage/object/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
import {
S3Client,
ListObjectsV2Command,
HeadObjectCommand,
} from '@aws-sdk/client-s3';

import getS3Config from '../utils/config.js';
import formatList from '../utils/list.js';

const limit = 100;

function buildInput({ org, key }) {
return {
Bucket: `${org}-content`,
Expand All @@ -28,34 +25,6 @@ function buildInput({ org, key }) {
};
}

/**
* Adds metadata to the list of objects specified.
*
* @param {S3Client} s3client the s3 client
* @param {DaCtx} daCtx the DA context
* @param {Object<{path: string, name: string}>} list list of entries
*/
async function populateMetadata(s3client, daCtx, list) {
let idx = 0;
while (idx < list.length) {
const promises = list.slice(idx, idx + limit)
.filter((item) => item.ext)
.map(async (item) => {
const Key = item.path.substring(1).split('/').slice(1).join('/');
const input = { Bucket: `${daCtx.org}-content`, Key };
const cmd = new HeadObjectCommand(input);
return s3client.send(cmd).then((resp) => {
if (resp.$metadata.httpStatusCode === 200) {
// eslint-disable-next-line no-param-reassign
item.lastModified = resp.LastModified.getTime();
}
});
});
await Promise.all(promises);
idx += limit;
}
}

export default async function listObjects(env, daCtx) {
const config = getS3Config(env);
const client = new S3Client(config);
Expand All @@ -66,7 +35,6 @@ export default async function listObjects(env, daCtx) {
const resp = await client.send(command);
// console.log(resp);
const body = formatList(resp, daCtx);
await populateMetadata(client, daCtx, body);
return {
body: JSON.stringify(body),
status: resp.$metadata.httpStatusCode,
Expand Down
5 changes: 4 additions & 1 deletion src/storage/utils/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export default function formatList(resp, daCtx) {
// Do not show any hidden files.
if (!name) return;
const item = { path: `/${daCtx.org}/${content.Key}`, name };
if (ext !== 'props') item.ext = ext;
if (ext !== 'props') {
item.ext = ext;
item.lastModified = content.LastModified.getTime();
}

combined.push(item);
});
Expand Down
59 changes: 4 additions & 55 deletions test/storage/object/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@
*/

import assert from 'node:assert';
import { ListObjectsV2Command, HeadObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3';
import { mockClient } from 'aws-sdk-client-mock';

const s3Mock = mockClient(S3Client);

import listObjects from '../../../src/storage/object/list.js';

const CommonPrefixes = [
{ Prefix: 'wknd/mydir/' },
{ Prefix: 'wknd/mydir2/' },
];

const Contents = [
{ Key: 'wknd/index.html' },
{ Key: 'wknd/nav.html' },
{ Key: 'wknd/footer.html' },
{ Key: 'wknd/index.html', LastModified: new Date() },
{ Key: 'wknd/nav.html', LastModified: new Date() },
{ Key: 'wknd/footer.html', LastModified: new Date() },
];


Expand All @@ -35,63 +30,17 @@ describe('List Objects', () => {
s3Mock.reset();
});

it('ignores folders for metadata', async () => {
s3Mock.on(ListObjectsV2Command, {
Bucket: 'adobe-content',
Prefix: 'wknd/',
Delimiter: '/',
}).resolves({ $metadata: { httpStatusCode: 200 }, CommonPrefixes });
s3Mock.on(HeadObjectCommand).rejects(new Error('Should Not be called.'));
const daCtx = { org: 'adobe', key: 'wknd' };
const resp = await listObjects({}, daCtx);
const data = JSON.parse(resp.body);
assert.strictEqual(data.length, 2);
assert(data.every((item) => !item.ext));
});

it('populates file metadata', async () => {
s3Mock.on(ListObjectsV2Command, {
Bucket: 'adobe-content',
Prefix: 'wknd/',
Delimiter: '/',
}).resolves({ $metadata: { httpStatusCode: 200 }, Contents });

s3Mock.on(HeadObjectCommand).resolves({ $metadata: { httpStatusCode: 200 }, LastModified: new Date() });

const daCtx = { org: 'adobe', key: 'wknd' };
const resp = await listObjects({}, daCtx);
const data = JSON.parse(resp.body);
assert.strictEqual(data.length, 3);
assert(data.every((item) => item.ext && item.lastModified));
assert(s3Mock.commandCalls(HeadObjectCommand, { Bucket: 'adobe-content', Key: 'wknd/index.html' }));
assert(s3Mock.commandCalls(HeadObjectCommand, { Bucket: 'adobe-content', Key: 'wknd/nav.html' }));
assert(s3Mock.commandCalls(HeadObjectCommand, { Bucket: 'adobe-content', Key: 'wknd/footer.html' }));
});

it('handles a longer list', async () => {

const prefixes = [...CommonPrefixes];
for (let i = 0; i < 100; i++) {
prefixes.push({ Prefix: `wknd/mydir${i}/` });
}

const contents = [...Contents];
for (let i = 0; i < 100; i++) {
contents.push({ Key: `wknd/file${i}.html` });
}

s3Mock.on(ListObjectsV2Command, {
Bucket: 'adobe-content',
Prefix: 'wknd/',
Delimiter: '/',
}).resolves({ $metadata: { httpStatusCode: 200 }, CommonPrefixes: prefixes, Contents: contents });

s3Mock.on(HeadObjectCommand).resolves({ $metadata: { httpStatusCode: 200 }, LastModified: new Date() });

const daCtx = { org: 'adobe', key: 'wknd' };
const resp = await listObjects({}, daCtx);
const data = JSON.parse(resp.body);
assert.strictEqual(data.length, 205);
assert(s3Mock.commandCalls(HeadObjectCommand).length = 103);
});
})

0 comments on commit 49e8390

Please sign in to comment.