npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@relaycorp/cloudevents-transport

v2.0.27

Published

Node.js library to send/receive CloudEvents over HTTP binary or any cloud-specific service like GCP PubSub

Downloads

132

Readme

@relaycorp/cloudevents-transport

This is a Node.js library to send/receive CloudEvents over the following transports:

Emitters

To create an emitter, simply pass the name of the transport and the channel to the makeEmitter function. For example:

import type { EmitterFunction } from 'cloudevents';
import { makeEmitter } from '@relaycorp/cloudevents-transport';

const transport = process.env.CE_TRANSPORT_NAME ?? 'ce-http-binary';
const channel = process.env.CE_CHANNEL ?? 'https://cloudevents-broker.com';
const emitter: EmitterFunction = await makeEmitter(transport, channel);

Refer to the documentation of each transport below to learn about the structure channel parameter.

Then the emitter can be used as a regular EmitterFunction from the cloudevents library. For example:

import { CloudEvent } from 'cloudevents';

const event = new CloudEvent({
  type: 'com.example.some-event',
  source: 'https://example.com',
  data: 'Hello, world!',
});
await emitter(event);

Receivers

To create a receiver, simply pass the name of the transport to the makeReceiver function. For example:

import { makeReceiver } from '@relaycorp/cloudevents-transport';

const transport = process.env.CE_TRANSPORT_NAME ?? 'ce-http-binary';
const receiver = await makeReceiver(transport);

Then the receiver can be used to convert cloudevents Messages to CloudEventV1s. For example, using Fastify:

import { makeReceiver } from '@relaycorp/cloudevents-transport';
import type { CloudEventV1 } from 'cloudevents';
import type { FastifyInstance, FastifyPluginOptions } from 'fastify';

export async function registerEventReceiver(server: FastifyInstance): Promise<void> {
  // Accept any content type
  server.removeAllContentTypeParsers();
  server.addContentTypeParser('*', { parseAs: 'buffer' }, (_request, payload, next) => {
    next(null, payload);
  });

  // Initialise the receiver once and reuse it across requests
  const transport = process.env.CE_TRANSPORT_NAME ?? 'ce-http-binary';
  const convertMessageToEvent = await makeReceiver(transport);

  server.post('/', async (request, reply) => {
    let event: CloudEventV1<Buffer>;
    try {
      event = convertMessageToEvent(request.headers, request.body);
    } catch (err) {
      return reply.status(400).send({ reason: err.message });
    }

    return reply.status(200).send({ eventId: event.id });
  });
}

Supported transports

ce-http-binary

This is the standard CloudEvents HTTP transport, in binary mode.

The channel passed to the emitter must be the URL of the CloudEvents endpoint that will receive the events.

google-pubsub

This transport doesn't actually use CloudEvents at all -- it simply converts the CloudEvent to a Google PubSub message and vice versa. Fields between the two formats are mapped as follows:

| CloudEvent field | PubSub field | | ---------------- | ------------- | | id | messageId | | time | publishTime | | data | data |

All other CloudEvents fields, including extensions, are mapped to PubSub attributes with the same name.

The channel passed to the emitter must be the name of the PubSub topic where messages are to be published.