Package Exports
- @sagi.io/workers-pubsub
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sagi.io/workers-pubsub) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
workers-pubsub
@sagi.io/workers-pubsub is a Google Pub/Sub REST API for Cloudflare Workers (can also be used with Node).
Installation
$ npm i @sagi.io/workers-pubsubAPI
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:
serviceAccountJSONrequired Is a Google Cloud service account with a Pub/Sub Admin role. An object.cryptoImploptional Not needed when running on Cloudflare Workers. See concrete example below for how to use it with Node.js.fetchImploptional 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:
topicrequired The topic to send messages to.messagesrequired an array of Pub/Sub messages. You can use thePubSub.helpers.createPubSubMessagemethod 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:
messageoptional A message string. e.g.Hello World.attributesoptional An object with string values. e.g.{ type: 'slack-poll' }.
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 })