Socket
Socket
Sign inDemoInstall

@6river/pubcap

Package Overview
Dependencies
144
Maintainers
4
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @6river/pubcap

Google Pub/Sub message Capture


Version published
Weekly downloads
24
increased by4.35%
Maintainers
4
Install size
256 kB
Created
Weekly downloads
 

Readme

Source

PubCap

PubCap is designed to capture messages published to Google Pub/Sub topics.

Install and Setup

Install the component as development dependency:

npm i @sixriver/pubcap -D

Usage

Bellow is the typical setup for Mocha framework.

Setup before/after hooks to register topics being listened and the code to cleanup the mess after the tests are over:


const pubcap = new PubCap({

});

before(async function() {
	await pubcap.listen(pubsub, ['topic1', 'topic2']);
});

after(async function() {
	await pubcap.close();
});

Optionally setup beforeEach or afterEach hook to drain messages from the registered topics:

beforeEach(async function() {
	await pubcap.drain();
})

Access captured messages using PubCap#messages method:

it('should capture messages', async function() {
	const myMessages = await pubcap.messages('my-topic');
	// ...
});

Getting Raw Pub/Sub messages

Method PubCap#messages by default returns the result of JSON.parse(msg.data.toString()). Use Raw decoder to get raw Pub/Sub messages:

import {PubCap, RAW} from '@6river/pubcap';

const pubcap = new PubCap();
// ...
const messages = await pubcap.messages('my-topic', {decoder: RAW});

Writing your own message decoder

Implement the Decoder interface to get raw messages converted some custom (or typesafe) way.


import {Message} from '@google-cloud/pubsub';
import {Decoder, PubCap} from '@6river/pubcap';

// Suppose we have some message type
interface FooMessage {
	id: string;
	foo: boolean;
}

// Here is our decoder
class FooDecoder implements Decoder<FooMessage> {
	decode(msg: Message): FooMessage {
		const foo: FooMessage = ...
		return foo;
	}
}


// now make the decoder and use it
const decoder = new FooDecoder();
const messages = await pubcap.messages('my-topic', {decoder});

Se also

test/pubcap.spec.ts as an example.

Keywords

FAQs

Last updated on 20 Sep 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc