0.7.2 • Published 5 months ago

@yourrentals/cloudevent-receiver-core v0.7.2

Weekly downloads
-
License
-
Repository
-
Last release
5 months ago

@yourrentals/cloudevent-receiver-core

This library defines the core logic for receiving messages from the message bus.

Installation

yarn add @yourrentals/cloudevent-receiver-core

Usage

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:

ErrorDescriptionExpected HTTP status codeRetryable
InvalidSignatureErrorThe signature of the message is invalid401No
UnparsableMessageErrorThe message is not a valid CloudEvent400No
NotFoundErrorThe queue name is not registered404Yes
ConflictErrorThe queue name is already registered409Yes
TooManyRequestsErrorThe queue is currently busy429Yes
ClientErrorAny other client error400No
ServerErrorAny other server error500Yes
0.7.2

5 months ago

0.7.1

5 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.5.2

7 months ago

0.5.1

7 months ago

0.5.0

7 months ago

0.4.3

7 months ago

0.4.2

7 months ago

0.4.1

7 months ago