Skip to content

Commit

Permalink
chore(cb2-12978): Fix TFL Feed Empty File (#95)
Browse files Browse the repository at this point in the history
* feat(cb2-12978): corrected getItemFromS3 method and updated test accordingly

* feat(cb2-12978): run npm audit fix to resolve dev dep vulns
  • Loading branch information
cb-cs authored Jul 8, 2024
1 parent 05cbc2e commit 574dcf3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 26 additions & 5 deletions src/infrastructure/s3BucketService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
GetObjectCommand,
GetObjectCommandInput,
GetObjectCommandOutput,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3';
import logger from '../utils/logger';
import { Readable } from "stream";

export async function uploadToS3(processedData: string, fileName: string, callback: () => void): Promise<void> {
const s3: S3Client = configureS3();
Expand All @@ -21,11 +22,21 @@ export async function uploadToS3(processedData: string, fileName: string, callba
export async function getItemFromS3(key: string): Promise<string | undefined> {
logger.info(`Reading contents of file ${key}`);
const s3 = configureS3();
const params: GetObjectCommandInput = { Bucket: process.env.AWS_S3_BUCKET_NAME ?? '', Key: key };
const command: GetObjectCommand = new GetObjectCommand(
{
Bucket: process.env.AWS_S3_BUCKET_NAME ?? '',
Key: key,
}
);

try {
const body = (await s3.send(new GetObjectCommand(params))).Body?.toString();
logger.info(`File contents retrieved: ${body}`);
return body;
const response: GetObjectCommandOutput = await s3.send(command);

if (response.Body instanceof Readable) {
const bufferedString = await streamToString(response.Body);
logger.info(`File contents retrieved: ${bufferedString}`);
return bufferedString;
}
} catch (err) {
logger.error(`Error reading file from S3 ${JSON.stringify(err)}`);
throw err;
Expand Down Expand Up @@ -78,3 +89,13 @@ function configureS3() {
}
return new S3Client({});
}

async function streamToString(stream: Readable): Promise<string> {
const chunks: Uint8Array[] = [];

for await (const chunk of stream) {
chunks.push(chunk);
}

return Buffer.concat(chunks).toString('utf-8');
}
4 changes: 3 additions & 1 deletion tests/unit/infrastructure/s3BucketService.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable import/first */
import { Readable } from "stream";

process.env.LOG_LEVEL = 'error';
const mockPromise = jest.fn();
const mockGetObject = jest.fn(() => ({
Expand Down Expand Up @@ -32,7 +34,7 @@ describe('readAndUpsert', () => {
const originalFileContents = 'the original content of the file';
const newFileContents = 'new content for the file';

client.on(GetObjectCommand).resolves({ Body: Buffer.from(originalFileContents) } as unknown as GetObjectCommandOutput);
client.on(GetObjectCommand).resolves({ Body: Readable.from(Buffer.from(originalFileContents)) } as unknown as GetObjectCommandOutput);
client.on(PutObjectCommand).callsFake(mockUpload);

const contents = await readAndUpsert(fileName, newFileContents);
Expand Down

0 comments on commit 574dcf3

Please sign in to comment.