Skip to content

Commit d75a657

Browse files
committed
refactor: streamline JSON parsing in processS3Event by iterating over lines and enhancing event handling
1 parent 8e360f0 commit d75a657

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

tests/serverless-plugins-integration/test-s3.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,23 @@ function processS3Event(output) {
6868
if (!output.includes('Records') || !output.includes('eventSource":"minio:s3"')) return;
6969

7070
try {
71-
const jsonStart = output.indexOf('{');
72-
if (jsonStart < 0) return;
73-
74-
const jsonEnd = output.lastIndexOf('}');
75-
if (jsonEnd <= jsonStart) return;
76-
77-
const jsonStr = output.slice(jsonStart, jsonEnd + 1);
78-
79-
const eventData = JSON.parse(jsonStr);
80-
if (!eventData || !eventData.Records || eventData.Records.length === 0) return;
81-
const eventId = `${eventData.Records[0].s3.bucket.name}-${eventData.Records[0].s3.object.key}`;
82-
incrementlambdaCallCounter(eventId);
71+
output
72+
.split('\n')
73+
.filter(line => line.includes('Records') && line.includes('eventSource":"minio:s3"'))
74+
.forEach(line => {
75+
const jsonStart = line.indexOf('{');
76+
if (jsonStart < 0) return;
77+
78+
const jsonEnd = line.lastIndexOf('}');
79+
if (jsonEnd <= jsonStart) return;
80+
81+
const jsonStr = line.slice(jsonStart, jsonEnd + 1);
82+
83+
const eventData = JSON.parse(jsonStr);
84+
if (!eventData || !eventData.Records || eventData.Records.length === 0) return;
85+
const eventId = `${eventData.Records[0].s3.bucket.name}-${eventData.Records[0].s3.object.key}`;
86+
incrementlambdaCallCounter(eventId);
87+
});
8388
} catch (err) {
8489
console.error('Error in processS3Event:', {err, output});
8590
}

0 commit comments

Comments
 (0)