Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize topic and subscription separator #80

Merged
merged 5 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ The only static method from the `PubSubFactory` class. It initiates a new PubSub
- `debug`: Display logs if it is set to true. It uses a [pino logger](https://getpino.io/#/) and [pino-pretty](https://github.com/pinojs/pino-pretty) if `NODE_ENV` is not equal to `production`.
- `pinoOptions`: If `debug` is set to true, set the [pino logger options](https://getpino.io/#/docs/api?id=options). Default to `level: debug` and `prettyPrint: true` if `NODE_ENV` is not equal to `production`.
- `topicsPrefix`: Add a prefix to all created topics. Example: `topicsPrefix: 'my-topic'`, all topics will begin with `my-topic+{your topic name}`.
- `subscriptionsPrefix`: Add a prefix to all created subscriptions. Example: `subscriptionsPrefix: 'my-sub'`, all topics will begin with `my-sub%{your topic name}`.
- `topicsSeparator`: Customize separator between topicsPrefix and topic name. Example: `topicsSeparator: '-'`, all topics will be `{topic prefix}-{your topic name} (default to '+')`.
- `subscriptionsPrefix`: Add a prefix to all created subscriptions. Example: `subscriptionsPrefix: 'my-sub'`, all subscriptions will begin with `my-sub%{your topic name}`.
- `subscriptionsSeparator`: Customize separator between subscriptionsPrefix and topic name. Example: `subscriptionsSeparator: '-'`, all subscriptions will be `{subscription prefix}-{your topic name} (default to '%')`.
- `namespace`: Add a namespace property to [Message attributes](https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage) when publishing on a topic.
- `environment`: Add a environment property to [Message attributes](https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage) when publishing on a topic.

Expand Down
24 changes: 22 additions & 2 deletions src/GoogleCloudPubSub/GoogleCloudPubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,31 @@ export class GoogleCloudPubSub implements GCPubSub {
*/
private readonly subscriptionsPrefix?: string;

/**
* Subscription separator
* Example: if "subscriptionSeparator = -",
* then subscription name will begin with "<subscriptionPrefix>-"
*
* @defaultValue `%`
*/
private readonly subscriptionsSeparator?: string;

/**
* Topic prefix
* Example: if "topicsPrefix = algoan",
* then topic names will begin with "algoan+"
*/
private readonly topicsPrefix?: string;

/**
* Topic separator
* Example: if "topicsSeparator = -",
* then topic names will begin with "<topicsPrefix>-"
*
* @defaultValue '+'
*/
private readonly topicsSeparator?: string;

/**
* An optional namespace
* Can be useful when emitting an event
Expand All @@ -74,7 +92,9 @@ export class GoogleCloudPubSub implements GCPubSub {
constructor(options: GooglePubSubOptions = {}) {
this.client = new GPubSub(options);
this.subscriptionsPrefix = options.subscriptionsPrefix;
this.subscriptionsSeparator = options.subscriptionsSeparator !== undefined ? options.subscriptionsSeparator : '%';
this.topicsPrefix = options.topicsPrefix;
this.topicsSeparator = options.topicsSeparator !== undefined ? options.topicsSeparator : '+';
this.namespace = options.namespace;
this.environment = options.environment;
this.topics = new Map();
Expand Down Expand Up @@ -227,7 +247,7 @@ export class GoogleCloudPubSub implements GCPubSub {
*/
private getTopicName(event: string): string {
if (this.topicsPrefix !== undefined) {
return `${this.topicsPrefix}+${event}`;
return `${this.topicsPrefix}${this.topicsSeparator}${event}`;
}

return event;
Expand All @@ -239,7 +259,7 @@ export class GoogleCloudPubSub implements GCPubSub {
*/
private getSubscriptionName(event: string): string {
if (this.subscriptionsPrefix !== undefined) {
return `${this.subscriptionsPrefix}%${event}`;
return `${this.subscriptionsPrefix}${this.subscriptionsSeparator}${event}`;
}

return event;
Expand Down
2 changes: 2 additions & 0 deletions src/GoogleCloudPubSub/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { PubSub } from '..';
*/
export interface GooglePubSubOptions extends ClientConfig {
topicsPrefix?: string;
topicsSeparator?: string;
subscriptionsPrefix?: string;
subscriptionsSeparator?: string;
namespace?: string;
environment?: string;
debug?: boolean;
Expand Down
62 changes: 61 additions & 1 deletion test/GoogleCloudPubSub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/* eslint-disable no-void */
import test, { CbExecutionContext, ExecutionContext } from 'ava';
import * as sinon from 'sinon';
import { isPayloadError, EmittedMessage, GCPubSub, PubSubFactory, Transport } from '../src';

import { EmittedMessage, GCPubSub, isPayloadError, PubSubFactory, Transport } from '../src';

const Emulator = require('google-pubsub-emulator');

Expand Down Expand Up @@ -210,3 +211,62 @@ test('GPS004 - should not add prefix to subscription and topic', async (t: Execu
t.true(isTopicExistingWithoutPrefix);
t.true(isSubcriptionExistingWithoutPrefix);
});

test('GPS005 - should add separator to subscription and topic', async (t: ExecutionContext): Promise<void> => {
const topicName: string = 'topic_3';
const pubsub: GCPubSub = PubSubFactory.create({
transport: Transport.GOOGLE_PUBSUB,
options: {
projectId,
topicsPrefix: 'algoan',
subscriptionsPrefix: 'test-app',
subscriptionsSeparator: '-',
},
});

await pubsub.listen(topicName);

const [isTopicExisting] = await pubsub.client.topic(`algoan+${topicName}`).exists();
const [isSubcriptionExisting] = await pubsub.client.subscription(`test-app-${topicName}`).exists();

t.true(isTopicExisting);
t.true(isSubcriptionExisting);
});

test('GPS006 - should not add separator to subscription and topic', async (t: ExecutionContext): Promise<void> => {
const topicName: string = 'topic_4';
const pubsub: GCPubSub = PubSubFactory.create({
transport: Transport.GOOGLE_PUBSUB,
options: {
projectId,
topicsPrefix: 'algoan',
subscriptionsPrefix: 'test-app',
},
});

await pubsub.listen(topicName);

const [isTopicExisting] = await pubsub.client.topic(`algoan+${topicName}`).exists();
const [isSubcriptionExisting] = await pubsub.client.subscription(`test-app%${topicName}`).exists();

t.true(isTopicExisting);
t.true(isSubcriptionExisting);
});

test('GPS007 - should add separator to topic name', async (t: ExecutionContext): Promise<void> => {
const topicName: string = 'topic_5';
const pubsub: GCPubSub = PubSubFactory.create({
transport: Transport.GOOGLE_PUBSUB,
options: {
projectId,
topicsPrefix: 'algoan',
topicsSeparator: '-',
},
});

await pubsub.listen(topicName);

const [isTopicExisting] = await pubsub.client.topic(`algoan-${topicName}`).exists();

t.true(isTopicExisting);
});