-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: centralize sqsEventAdapter in spacecat-shared-utils (#128)
Centralizes the `sqsEventAdapter` function in the `spacecat-shared-utils` package.
- Loading branch information
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Wrapper to turn an SQS record into a function param | ||
* Inspired by https://github.com/adobe/helix-admin/blob/main/src/index.js#L108-L133 | ||
* | ||
* @param {UniversalAction} fn | ||
* @returns {function(object, UniversalContext): Promise<Response>} | ||
*/ | ||
export function sqsEventAdapter(fn) { | ||
return async (req, context) => { | ||
const { log } = context; | ||
let message; | ||
|
||
try { | ||
// currently not publishing batch messages | ||
const records = context.invocation?.event?.Records; | ||
log.info(`Received ${records.length} many records. ID of the first message in the batch: ${records[0]?.messageId}`); | ||
message = JSON.parse(records[0]?.body); | ||
log.info(`Received message with id: ${context.invocation?.event?.Records.length}`); | ||
} catch (e) { | ||
log.error('Function was not invoked properly, message body is not a valid JSON', e); | ||
return new Response('', { | ||
status: 400, | ||
headers: { | ||
'x-error': 'Event does not contain a valid message body', | ||
}, | ||
}); | ||
} | ||
return fn(message, context); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
import { sqsEventAdapter } from '../src/sqs.js'; | ||
|
||
const exampleHandler = sinon.spy(async (message, context) => { | ||
const { log } = context; | ||
const messageStr = JSON.stringify(message); | ||
log.info(`Handling message ${messageStr}`); | ||
return new Response(messageStr); | ||
}); | ||
|
||
describe('SQS helpers', () => { | ||
const emptyRequest = {}; | ||
|
||
it('should handle an invalid context with no records', async () => { | ||
const contextNoRecords = { | ||
log: console, | ||
}; | ||
|
||
const handler = sqsEventAdapter(exampleHandler); | ||
const response = await handler(emptyRequest, contextNoRecords); | ||
|
||
expect(response.status).to.equal(400); | ||
expect(response.headers.get('x-error')).to.equal('Event does not contain a valid message body'); | ||
}); | ||
|
||
it('should handle a valid context with an event record', async () => { | ||
const context = { | ||
log: console, | ||
invocation: { | ||
event: { | ||
Records: [ | ||
{ | ||
body: JSON.stringify({ id: '1234567890' }), | ||
messageId: 'abcd', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
const handler = sqsEventAdapter(exampleHandler); | ||
const response = await handler(emptyRequest, context); | ||
|
||
expect(response.status).to.equal(200); | ||
const result = await response.json(); | ||
expect(result.id).to.equal('1234567890'); | ||
expect(exampleHandler.calledWith({ id: '1234567890' })).to.be.true; | ||
}); | ||
}); |