New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@sagi.io/workers-pubsub

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sagi.io/workers-pubsub

Google Pub/Sub API for Cloudflare Workers

  • 0.0.15
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

workers-pubsub

@sagi.io/workers-pubsub is a Google Pub/Sub REST API for Cloudflare Workers (can also be used with Node).

⭐ We use it at OpenSay to efficiently access Google's PubSub REST API with 1 round trip.

CircleCI Coverage Status MIT License version

Installation

$ npm i @sagi.io/workers-pubsub

API

We follow Google's Pub/Sub REST API specification. We'll add more methods if there's demand.

See below for concrete examples for Cloudflare Workers and Node.js.

PubSubREST({ ... })

Instantiates PubSub instance.

Function definition:

 const PubSubREST = async ({
  serviceAccountJSON,
  cryptoImpl = null,
  fetchImpl = null,
}) => { ... }

Where:

  • serviceAccountJSON required Is a Google Cloud service account with a Pub/Sub Admin role. An object.
  • cryptoImpl optional Not needed when running on Cloudflare Workers. See concrete example below for how to use it with Node.js.
  • fetchImpl optional Not needed when running on Cloudflare Workers. See concrete example below for how to use it with Node.js.

PubSub.topics.publish({ ... })

Publishes a message to a topic.

Function definition:

const publish = ({ topic, messages } = {}) => { ... }

Where:

  • topic required The topic to send messages to.
  • messages required an array of Pub/Sub messages. You can use the PubSub.helpers.createPubSubMessage method to easily create a Pub/Sub message.

PubSub.topics.list({ ... })

Lists all topics.

PubSub.helpers.createPubSubMessage({ ... })

Helps create a PubSub message easily.

Function definition:

const createPubSubMessage = ({ message = '', attributes = undefined } = {}) => { ... }

Where:

  • message optional A message string. e.g. Hello World.
  • attributes optional An object with string values. e.g. { type: 'slack-poll' }.
  • ordering_key optional An ordering key to allow subscribers to receive messages in order in the same region. Read more here.

Returns a Pub/Sub message.

Cloudflare Workers Example

import base64url from 'base64url'
import PubSubREST from '@sagi.io/workers-pubsub'

const serviceAccountJSON = ...

const PubSub = await PubSubREST({ serviceAccountJSON })

const topic = 'gcf-task'
const psMessage = PubSub.helpers.createPubSubMessage({ message: 'Hello World!' })
const messages = [ psMessage ]

await PubSub.topics.publish({ topic, messages })

Node.js Example

import fetchImpl from 'cross-fetch'
import { Crypto }from 'node-webcrypto-ossl'
import PubSubREST from '@sagi.io/workers-pubsub'

const cryptoImpl = new Crypto()

const serviceAccountJSON = ...

const PubSub = await PubSubREST({ serviceAccountJSON, cryptoImpl. fetchImpl })

const topic = 'gcf-task'
const psMessage = PubSub.helpers.createPubSubMessage({ message: 'Hello World!' })
const messages = [ psMessage ]

await PubSub.topics.publish({ topic, messages })

Keywords

FAQs

Package last updated on 04 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc