Skip to content

Commit

Permalink
Merge 3d3c8a5 into 5a5ba99
Browse files Browse the repository at this point in the history
  • Loading branch information
calandrajose committed Jun 29, 2023
2 parents 5a5ba99 + 3d3c8a5 commit 0335240
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
16 changes: 14 additions & 2 deletions lib/handler.js
Expand Up @@ -77,8 +77,20 @@ module.exports = class Handler {
}
}

static getFullData(body) {
return body?.contentS3Path ? Lambda.getBodyFromS3(body.contentS3Path) : body;
static async getFullData(body) {

if(!body?.contentS3Path)
return body;

const s3Body = await Lambda.getBodyFromS3(body.contentS3Path);

if(!body?.error)
return s3Body;

return {
...s3Body,
error: body.error
};
}

static async emitEnded() {
Expand Down
12 changes: 10 additions & 2 deletions lib/parallel-handler.js
Expand Up @@ -29,12 +29,20 @@ module.exports = class ParallelHandler extends Handler {

static getFullData(body) {

return Promise.all(body.map(item => {
return Promise.all(body.map(async item => {

if(!item?.contentS3Path)
return item;

return Lambda.getBodyFromS3(item.contentS3Path);
const s3Body = await Lambda.getBodyFromS3(item.contentS3Path);

if(!item?.error)
return s3Body;

return {
...s3Body,
error: item.error
};
}));
}
};
49 changes: 49 additions & 0 deletions tests/handler.js
Expand Up @@ -387,6 +387,55 @@ describe('Handler', () => {
sinon.assert.calledOnceWithExactly(Lambda.bodyToS3Path, 'step-function-payloads', body, []);
});

it('When the payload is long and the lambda is running as a step function should preserve error property if it exists', async () => {

sinon.restore();

const body = {
name: 'Some-Name',
age: 30,
numbers: []
};

for(let index = 100000; index < 130000; index++)
body.numbers.push(String(index));

const contentS3Path = 'step-function-payloads/2022/12/23/addasdsadas.json';

sinon.stub(Lambda, 'getBodyFromS3').resolves(body);

sinon.stub(Lambda, 'bodyToS3Path').resolves({
contentS3Path
});

const stateMachine = {
id: 'id-state-machine',
name: 'state-machine-test'
};

const apiSession = new ApiSession({ ...session });
class LambdaFunctionExample {

process() {
return {
session: this.session,
body: this.data
};
}
}

assert.deepStrictEqual(await Handler.handle(
LambdaFunctionExample,
{ session, body: { contentS3Path, error: { Error: 'Lambda.Unknown' } }, stateMachine }
), { session: apiSession, body: { contentS3Path }, stateMachine });

sinon.assert.calledOnceWithExactly(Lambda.getBodyFromS3, contentS3Path);
sinon.assert.calledOnceWithExactly(Lambda.bodyToS3Path, 'step-function-payloads', {
...body,
error: { Error: 'Lambda.Unknown' }
}, []);
});

it('Should return the same value when the payload has no session and body and the lambda is executed as a step function', async () => {

const stateMachine = {
Expand Down
7 changes: 5 additions & 2 deletions tests/parallel-handler.js
Expand Up @@ -71,7 +71,8 @@ describe('ParallelHandler', () => {

const body3 = {
name: 'Long Body 1',
example: 'Imagine that this body weighs more than 250KB'
example: 'Imagine that this body weighs more than 250KB',
error: { Error: 'Lambda.Unknown' }
};

const body4 = {
Expand All @@ -80,7 +81,8 @@ describe('ParallelHandler', () => {
};

const bodyLong1 = {
contentS3Path: 'step-function-payloads/2023/01/01/bodyLong1.json'
contentS3Path: 'step-function-payloads/2023/01/01/bodyLong1.json',
error: { Error: 'Lambda.Unknown' }
};

const bodyLong2 = {
Expand Down Expand Up @@ -114,6 +116,7 @@ describe('ParallelHandler', () => {
class LambdaFunctionExample {

process() {

return {
session: this.session,
body: this.data
Expand Down

0 comments on commit 0335240

Please sign in to comment.