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

@pluralsight/gcp-pubsub-lite

v1.0.15

Published

Convenience wrapper for GCP Pub/Sub

Downloads

24

Readme

gcp-pubsub-lite

This is a convenience library/wrapper for the official GCP Pub/Sub library. You supply our wrapper with the official GCP Pub/Sub library so you control which version you want to use. This way, our library will not block you from applying e.g. the latest security updates, or pinning to a previous version. We will keep this library up-to-date to be compatible with recent versions of the official library. Currently we support @google-cloud/pubsub versions 1.4.1+ and node.js v12+.

The official Google library, while full-featured, requires focused reading to understand and boilerplate to accomplish simple tasks. It uses an OO approach where the same things can be accomplished with different classes, in slightly different ways. By contrast, gcp-pubsub-lite uses simple, easy to use, functions. For example, gcp-pubsub-lite enables simple subscription polling and sending/receiving JSON data as shown below.

Installation

# npm
npm i @google-cloud/pubsub @pluralsight/gcp-pubsub-lite

# yarn
yarn add @google-cloud/pubsub @pluralsight/gcp-pubsub-lite

Usage Example

const gcpPubSub = require("@google-cloud/pubsub");
const psLite = require("@pluralsight/gcp-pubsub-lite");

const {GCP_PROJECT_ID: gcpProjectId} = process.env;
psLite.setup(gcpPubSub, gcpProjectId);

const topicName = "topicName";
await psLite.createTopic(topicName); // idempotent

const subName = "subName";
await psLite.createSubscription(topicName, subName); // idempotent

const messageData = {test: true, count: 5, data: "foobar", pi: 3.14};
await psLite.publishJson(topicName, messageData);
let isPolling = true;

while (isPolling) {
    const envelopes = await psLite.pull(subName, 1);
    if (!envelopes.length) {
      // wait 500ms and try again
      await new Promise(resolve => setTimeout(resolve, 500));
      continue;
    }
    const [envelope] = envelopes;
    const {message, ackId} = envelope;
    const copyOfMessageData = psLite.jsonifyMessageData(message);

    console.log("message contains:", copyOfMessageData);
    await psLite.acknowledge(subName, [ackId]);
    isPolling = false;
}

await Promise.all([psLite.deleteSubscription(subName),
                   psLite.deleteTopic(topicName)]);

Documentation

Functions

setup(gcpPubSub, projectId)

Sets up the library. This must be called before other functions in this library.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | gcpPubSub | object | Google Pub/Sub Library from require("@google-cloud/pubsub") | | projectId | string | GCP project id |

createTopic(topicName) ⇒ Promise.<Array.<object>>

Creates a Pub/Sub Topic. Idempotent.

Kind: global function
Returns: Promise.<Array.<object>> - returns response from Google library's topic.get(): [Topic, apiResponse], see https://googleapis.dev/nodejs/pubsub/latest/global.html#GetTopicCallback

| Param | Type | Description | | --- | --- | --- | | topicName | string | Name of topic to create |

deleteTopic(topicName) ⇒ Promise.<string>

Deletes a Pub/Sub Topic.

Kind: global function
Returns: Promise.<string> - returns deleted topicName

| Param | Type | Description | | --- | --- | --- | | topicName | string | Name of topic to delete |

createSubscription(topicName, subscriptionName, [options]) ⇒ Promise.<object>

Create a Pub/Sub subscription. Idempotent.

Kind: global function
Returns: Promise.<object> - returns {success: true}

| Param | Type | Default | Description | | --- | --- | --- | --- | | topicName | string | | Name of topic to which the subscription attaches. Topic must exist. | | subscriptionName | string | | Name of subscription to create | | [options] | object | {} | Options passed to GCP Lib's subscriber.createSubscription(). see https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html |

deleteSubscription(subscriptionName) ⇒ Promise

Delete a Pub/Sub subscription.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | subscriptionName | string | Name of subscription to delete |

publish(topicName, message) ⇒ Promise.<object>

publish a message to a topic

Kind: global function
Returns: Promise.<object> - Pub/Sub PublishResponse object, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse

| Param | Type | Description | | --- | --- | --- | | topicName | string | Name of topic to publish the message to | | message | PubsubMessage | Message object, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage | | message.data | buffer | object | string | Message data, must be compatible with Buffer.from() https://nodejs.org/docs/latest-v12.x/api/buffer.html |

publishJson(topicName, message) ⇒ Promise.<object>

publish a json message to a topic

Kind: global function
Returns: Promise.<object> - Pub/Sub PublishResponse object, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse

| Param | Type | Description | | --- | --- | --- | | topicName | string | Name of topic to publish the message to | | message | * | javascript data to publish. Must be compatible with JSON.stringify() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |

pull(subscriptionName, [maxMessages], [returnImmediately]) ⇒ Promise.<Array.<object>>

pull messages from a topic

Kind: global function
Returns: Promise.<Array.<object>> - receivedMessages, see: https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PullResponse

| Param | Type | Default | Description | | --- | --- | --- | --- | | subscriptionName | string | | Name of subscription | | [maxMessages] | number | 1 | Maximum number of messages to pull | | [returnImmediately] | boolean | true | Whether or not to return immediately. If false, waits about 5 seconds then the promise rejects |

acknowledge(subscriptionName, ackIds) ⇒ Promise

acknowledge completion of a pulled message

Kind: global function

| Param | Type | Description | | --- | --- | --- | | subscriptionName | string | Name of subscription | | ackIds | Array.<string> | The acknowledgment ID for the messages being acknowledged that was returned by the Pub/Sub system in the Pull response. Must not be empty. |

subscriptionExists(subscriptionName) ⇒ Promise.<boolean>

Whether or not a subscription exists

Kind: global function

| Param | Type | Description | | --- | --- | --- | | subscriptionName | string | Name of subscription |

jsonifyMessageData(message) ⇒ *

Takes a JSON pub/sub message and returns the data part as a native javascript value

Kind: global function
Returns: * - native javascript value

| Param | Type | Description | | --- | --- | --- | | message | receivedMessage | https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.ReceivedMessage |

topicExists(topicName) ⇒ Promise.<boolean>

Checks whether a topic exists

Kind: global function

| Param | Type | Description | | --- | --- | --- | | topicName | string | name of the topic to check |

getProject() ⇒ string

inspects this module, get what gcp project is being used

Kind: global function

getPublisher() ⇒ PublisherClient

inspects this module, get internal google pub/sub publisher client

Kind: global function
Returns: PublisherClient - https://googleapis.dev/nodejs/pubsub/latest/v1.PublisherClient.html

getSubscriber() ⇒ SubscriberClient

inspects this module, get internal google pub/sub subscriber client

Kind: global function
Returns: SubscriberClient - https://googleapis.dev/nodejs/pubsub/latest/v1.SubscriberClient.html

publishMany(topicName, messages) ⇒ Promise.<Array.<PublishResponse>>

Publish many messages to a topic

Kind: global function
Returns: Promise.<Array.<PublishResponse>> - Pub/Sub PublishResponse objects, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse

| Param | Type | Description | | --- | --- | --- | | topicName | string | Name of the topic to publish to | | messages | Array.<PubsubMessage> | Message objects to publish, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage |

publishManyJson(topicName, messages) ⇒ Promise.<Array.<PublishResponse>>

Publish many json messages to a topic

Kind: global function
Returns: Promise.<Array.<PublishResponse>> - Pub/Sub PublishResponse objects, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse

| Param | Type | Description | | --- | --- | --- | | topicName | string | Name of the topic to publish to | | messages | Array.<object> | message objects to publish. data value be compatible with JSON.stringify() see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage |

Contributions

Pull Requests are welcome.