Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Oct 8, 2023
1 parent ffd3a46 commit 48eb13d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/constants/limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
* Max amount of retries for S3 requests
*/
export const S3_RETRY_LIMIT = 3;

/**
* Max amount of keys to be returned in a S3 request
*/
export const S3_MAX_KEYS = 1000;
26 changes: 10 additions & 16 deletions src/handlers/strategies/directoryListing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getFile } from './serveFile';

// Imports the Precompiled Handlebars Template
import htmlTemplate from '../../templates/directoryListing.out.js';
import { S3_RETRY_LIMIT } from '../../constants/limits';
import { S3_MAX_KEYS, S3_RETRY_LIMIT } from '../../constants/limits';

// Applies the Template into a Handlebars Template Function
const handleBarsTemplate = Handlebars.template(htmlTemplate);
Expand Down Expand Up @@ -119,24 +119,22 @@ async function fetchR2Result(
cursor: string | undefined,
env: Env
): Promise<ListObjectsV2CommandOutput> {
let result: ListObjectsV2CommandOutput | undefined = undefined;

let retriesRemaining = S3_RETRY_LIMIT;
while (retriesRemaining > 0) {
try {
// Send request to R2
result = await client.send(
const result = await client.send(
new ListObjectsV2Command({
Bucket: env.BUCKET_NAME,
Prefix: bucketPath,
Delimiter: '/',
MaxKeys: 1000,
MaxKeys: S3_MAX_KEYS,
ContinuationToken: cursor,
})
);

// Request succeeded, no need for any retries
break;
return result;
} catch (err) {
// Got an error, let's log it and retry
console.error(`R2 ListObjectsV2 error: ${err}`);
Expand All @@ -145,12 +143,8 @@ async function fetchR2Result(
}
}

if (result === undefined) {
// R2 isn't having a good day, return a 500
throw new Error(`R2 failed listing path ${bucketPath}`);
}

return result;
// R2 isn't having a good day, return a 500
throw new Error(`R2 failed listing path ${bucketPath}`);
}

/**
Expand Down Expand Up @@ -202,11 +196,11 @@ export async function listDirectory(
delimitedPrefixes.add(path.Prefix.substring(bucketPath.length));
});

const hasIndexFile = result.Contents?.find(
object => object.Key?.endsWith('index.html')
);
const hasIndexFile = result.Contents
? result.Contents.some(object => object.Key?.endsWith('index.html'))
: false;

if (hasIndexFile !== undefined && hasIndexFile !== null) {
if (hasIndexFile) {
return getFile(url, request, `${bucketPath}index.html`, env);
}

Expand Down

0 comments on commit 48eb13d

Please sign in to comment.