Skip to content

Commit

Permalink
feat: handle s3 get object
Browse files Browse the repository at this point in the history
  • Loading branch information
dipratap committed Sep 2, 2024
1 parent a7a1e00 commit 20d5a2a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/metatags/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import SeoChecks from './seo-checks.js';

async function fetchAndProcessPageObject(s3Client, bucketName, key, prefix, log) {
const object = await getObjectFromKey(s3Client, bucketName, key, log);
if (!object?.Body?.tags || typeof object.Body.tags !== 'object') {
if (!object?.tags || typeof object.tags !== 'object') {
log.error(`No Scraped tags found in S3 ${key} object, body ${JSON.stringify(object.Body)}`);
return null;
}
const pageUrl = key.slice(prefix.length - 1).replace('scrape.json', ''); // Remove the prefix and .json suffix
return {
[pageUrl]: {
title: object.Body.tags.title,
description: object.Body.tags.description,
h1: object.Body.tags.h1 || [],
title: object.tags.title,
description: object.tags.description,
h1: object.tags.h1 || [],
},
};
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/s3-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export async function getObjectFromKey(s3Client, bucketName, key, log) {
Key: key,
});
try {
return await s3Client.send(command);
const response = await s3Client.send(command);
const body = await response.Body.transformToString();
return JSON.parse(body);
} catch (err) {
log.error(`Error while fetching S3 object from bucket ${bucketName} using key ${key}`, err);
return null;
Expand Down
75 changes: 41 additions & 34 deletions test/audits/metatags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,35 +245,39 @@ describe('Meta Tags', () => {
})))
.resolves({
Contents: [
{ Key: 'scrapes/site-id/blog/page1.json' },
{ Key: 'scrapes/site-id/blog/page2.json' },
{ Key: 'scrapes/site-id/blog/page1/scrape.json' },
{ Key: 'scrapes/site-id/blog/page2/scrape.json' },
],
});

s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
Key: 'scrapes/site-id/blog/page1/scrape.json',
}))).returns({
Body: {
tags: {
title: 'Test Page',
description: '',
},
transformToString: () => JSON.stringify({
tags: {
title: 'Test Page',
description: '',
},
}),
},
});
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page2.json',
Key: 'scrapes/site-id/blog/page2/scrape.json',
}))).returns({
Body: {
tags: {
title: 'Test Page',
h1: [
'This is a dummy H1 that is overly length from SEO perspective',
],
},
transformToString: () => JSON.stringify({
tags: {
title: 'Test Page',
h1: [
'This is a dummy H1 that is overly length from SEO perspective',
],
},
}),
},
});
const addAuditStub = sinon.stub().resolves();
Expand Down Expand Up @@ -362,41 +366,44 @@ describe('Meta Tags', () => {
})))
.resolves({
Contents: [
{ Key: 'scrapes/site-id/blog/page1.json' },
{ Key: 'scrapes/site-id/blog/page2.json' },
{ Key: 'scrapes/site-id/blog/page1/scrape.json' },
{ Key: 'scrapes/site-id/blog/page2/scrape.json' },
],
});

s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page1.json',
Key: 'scrapes/site-id/blog/page1/scrape.json',
}))).returns({

Body: {
tags: {
title: 'This is an SEO optimal page1 valid title.',
description: 'This is a dummy description that is optimal from SEO perspective for page1. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective due to its length exceeding 60 chars',
'This is second h1 tag on same page',
],
},
transformToString: () => JSON.stringify({
tags: {
title: 'This is an SEO optimal page1 valid title.',
description: 'This is a dummy description that is optimal from SEO perspective for page1. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective due to its length exceeding 60 chars',
'This is second h1 tag on same page',
],
},
}),
},
});
s3ClientStub.send
.withArgs(sinon.match.instanceOf(GetObjectCommand).and(sinon.match.has('input', {
Bucket: 'test-bucket',
Key: 'scrapes/site-id/blog/page2.json',
Key: 'scrapes/site-id/blog/page2/scrape.json',
}))).returns({
Body: {
tags: {
title: 'This is a SEO wise optimised page2 title.',
description: 'This is a dummy description that is optimal from SEO perspective for page2. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective',
],
},
transformToString: () => JSON.stringify({
tags: {
title: 'This is a SEO wise optimised page2 title.',
description: 'This is a dummy description that is optimal from SEO perspective for page2. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective',
],
},
}),
},
});
const addAuditStub = sinon.stub().resolves();
Expand Down
8 changes: 6 additions & 2 deletions test/utils/s3-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@ describe('S3 Utility Functions', () => {
it('should return the S3 object when getObject succeeds', async () => {
const bucketName = 'test-bucket';
const key = 'test-key';
const expectedObject = { Body: 'file contents' };
const expectedObject = { Body: { transformToString: () => '{"tags": {"title": "sample-title"}}' } };

const s3ClientMock = {
send: () => expectedObject,
};

const result = await getObjectFromKey(s3ClientMock, bucketName, key, logMock);
expect(result).to.deep.equal(expectedObject);
expect(result).to.deep.equal({
tags: {
title: 'sample-title',
},
});
});

it('should return null and log an error when getObject fails', async () => {
Expand Down

0 comments on commit 20d5a2a

Please sign in to comment.