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-gcp

v2.0.0

Published

Google CloudEvents type mapping for cloudevents-router package

Downloads

297

Readme

npm codecov

GCP CloudEvents mapping

This library is designed to work with cloudevents-router package.

We generate a mapping for all Google CloudEvents based on JSON Schema catalog published at googleapis/google-cloudevents

Install

npm install cloudevents-router-gcp

Use just types

import type { GoogleEvents } from "cloudevents-router-gcp";

/*
Will be equal to the following code snippet:

import { LogEntryData } from '@google/events/cloud/audit/v1/LogEntryData'
import { MessagePublishedData } from '@google/events/cloud/pubsub/v1/MessagePublishedData'
...

export type GoogleEvents = {
  'google.cloud.audit.log.v1.written': LogEntryData
  'google.cloud.pubsub.topic.v1.messagePublished': MessagePublishedData
  ...
}
*/

Use with cloudevents-router

import type { GoogleEvents } from "cloudevents-router-gcp";
import { CloudEventsRouter, getMiddleware } from "cloudevents-router";
import http from "http";

const router = new CloudEventsRouter<GoogleEvents>();

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

// See cloudevents-router documentation for more integration examples
const middleware = getMiddleware(router, { path: "/" });
const server = http.createServer(middleware);

server.listen(5000);

PubSub instructions

While consuming PubSub events with cloudevents is simple the type definition for PubSub is not very useful.

Payload is passed as string or undefined, and all topics end up in the same handler (as message type is always google.cloud.pubsub.topic.v1.messagePublished).

interface MessagePublishedData {
  subscription?: string;
  message?: {
    attributes?: { [key: string]: string };
    data?: string;
    messageId?: string;
    orderingKey?: string;
    publishTime?: Date | string;
  };
}

To make it a bit less painful we ship a PubSubParsedMessage<T> generic type and some helper functions to parse and re-route the PubSub messages.

1. Define your JSON message types

import type { GoogleEvents, PubSubParsedMessage } from "cloudevents-router-gcp";

// Define message payload type
type EventMap = GoogleEvents & {
  "pubsub.userAdded": PubSubParsedMessage<{
    username: string;
    password: string;
    age?: number;
  }>;
  "pubsub.profileChanged": PubSubParsedMessage<{
    username: string;
    profilePicture: string;
  }>;
};

This will replace data attribute (data?: string) in the original MessagePublishedData type with a more useful extracted type definition.

2. Write handlers for your messages

import { CloudEventsRouter } from "cloudevents-router";

const router = new CloudEventsRouter<EventMap>();

router.on("pubsub.profileChanged", async (event) => {
  console.log("Profile picture added", event.data.message?.data.profilePicture);
});

3. Parse and consume PubSub messages

To use the above handler we need to parse and republish PubSub messages

import { republishPubSubByTopic } from "cloudevents-router-gcp";

// use the router from above
// const router = ...

republishPubSubByTopic(router, {
  topics: {
    "user-added-topic": "pubsub.userAdded",
    "profile-changed-topic": "pubsub.profileChanged",
  },
});

// setup server as above
// const server = ...
server.listen(5000);

GCP PubSub payloads

GCP is publishing data in some known but not too well documented formats.

| Service | Description | Messages | | ------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | | container-registry | Every time container registry is updated a message is published to grc topic | pubsub.ArtifactMessage | | cloud-build | Cloud Build publishes messages on a Google Pub/Sub topic called cloud-builds when your build's state changes | TODO | | alert-center | Alert Center can push notifications to user defined pubsub topic | TODO |

There are many more, if you are using one of them and its not implemented, please feel free to create a pull request so others can also benefit.

usage is the same as for manually typed messages.

import { messages } from "cloudevents-router-gcp";

const EventMap = {
  "artifact.published": messages.ArtifactMessage,
};

// ...

Thats it ...

... happy coding :)