0.7.2 • Published 2 years ago
@yourrentals/cloudevent-receiver-core v0.7.2
@yourrentals/cloudevent-receiver-core
This library defines the core logic for receiving messages from the message bus.
Installation
yarn add @yourrentals/cloudevent-receiver-coreUsage
import {CloudEventReceiver, isCloudEventDataPlaneError, WebhookSignatureV1HmacPlugin, SqsMessage, CloudEvent} from '@yourrentals/cloudevent-receiver-core';
const receiver = new CloudEventReceiver({
queues: [
{
name: 'my-queue',
handler: async (event: CloudEvent) => {
// ...
},
},
{
name: 'my-queue',
handler: async (event: SqsMessage) => {
// ...
},
isCloudEvent: false
},
],
verifierPlugins: [new WebhookSignatureV1HmacPlugin('secret')],
});
// Starts a HTTP server that listens for messages from the message bus
const app = express();
app.post('/message-bus/:queueName', async (req, res) => {
const queueName = req.params.queueName;
try {
await receiver.handle(queueName, req.headers, req.body);
res.status(200).send();
} catch (e) {
if (isCloudEventDataPlaneError(e)) {
res.status(e.statusCode).send(e.message);
}
res.status(500).send();
}
});Raw Mode
The library can receive messages from any sources so long that it implements the Webhook Signature.
To disable parsing as cloudevent, set the isCloudEvent option to false.
By default, the event type is assumed to be a SQS Message
const receiver = new CloudEventReceiver({
queues: [
{
name: 'my-queue',
handler: async (event: SqsMessage) => {
// ...
},
// Disable parsing as cloudevent
isCloudEvent: false
},
],
verifierPlugins: [new WebhookSignatureV1HmacPlugin('secret')],
});Errors
The library defines the following errors:
| Error | Description | Expected HTTP status code | Retryable |
|---|---|---|---|
| InvalidSignatureError | The signature of the message is invalid | 401 | No |
| UnparsableMessageError | The message is not a valid CloudEvent | 400 | No |
| NotFoundError | The queue name is not registered | 404 | Yes |
| ConflictError | The queue name is already registered | 409 | Yes |
| TooManyRequestsError | The queue is currently busy | 429 | Yes |
| ClientError | Any other client error | 400 | No |
| ServerError | Any other server error | 500 | Yes |