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

cloudevents-router

v3.1.0

Published

Simple typed message router middleware for consuming cloudevents

Downloads

332

Readme

npm codecov

Cloud Events Router

The simplest typed cloudevents routing library for nodejs. Library has a single dependency to cloudevents npm library.

Library does not provide any input validation, just routing and typecasting!

Install

npm install cloudevents-router

Define types

// Hand-code or use external events map
// [type: string]: T

import { MessagePublishedData } from "@google/events/cloud/pubsub/v1/MessagePublishedData";
import { BuildEventData } from "@google/events/cloud/cloudbuild/v1/BuildEventData";

type EventMap = {
	"google.cloud.pubsub.topic.v1.messagePublished": MessagePublishedData;
	"google.cloud.cloudbuild.build.v1.statusChanged": BuildEventData;
	"my.custom.event.v1": {
		username: string;
	};
};

/*
Google Cloud events are described in cloudevents-router-gcp package

  npm install cloudevents-router-gcp
  import type { GoogleEvents } from 'cloudevents-router-gcp'

*/

Other known compatible packages include:

Consume events

import { CloudEventsRouter } from "cloudevents-router";

const router = new CloudEventsRouter<EventMap>();

router.on("google.cloud.pubsub.topic.v1.messagePublished", async (event) => {
	console.log("PubSub ordering key", event.data.message?.orderingKey);
});

router.on("google.cloud.cloudbuild.build.v1.statusChanged", async (event) => {
	console.log("Build images array", event.artifacts?.images);

	if (!event.artifacts) {
		// Error in handler will return 500 error code to the producer
		throw new Error("Artifacts not present");
	}
});

router.onUnhandled((event) => {
	// This counts as normal consumer for any undefined event
	// ... so server will always return 200 status code
	// ... if you don't want to acknowledge events you need to throw from this handler
	console.log("Unknown event", event);
});

See example for more useful PubSub handling

Connect to http server

Using the @whatwg-node/server server adapter

import { getServerAdapter } from "cloudevents-router";
import http from "http";

const middleware = getServerAdapter(router);
const server = http.createServer(middleware);

server.listen(5000);

If you are using express and want have a specific path for cloudevents processing.

import { getServerAdapter } from "cloudevents-router";
import express from "express";

const app = express();

app.post("/webhooks", getServerAdapter(router));

app.listen(5000);

See @whatwg-node/server for many more integration options.

Return codes from server adapter:

  • 200 - OK
  • 405 - Method Not Allowed
  • 501 - Not Implemented (event not listened, and no onUnhandled listener)
  • 500 - Internal Server Error (error during processing)

Manual server configuration

If you are not using a webserver at all or your framework is not supported by ``then you can always manually connect the CloudEventsRouter using theprocess() method.

See more integration examples at https://github.com/cloudevents/sdk-javascript

import { HTTP } from "cloudevents"

const receivedEvent = HTTP.toEvent({ ... });
await router.process(receivedEvent)

Thats it ...

... happy coding :)