-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub-event.ts
executable file
·30 lines (29 loc) · 1.03 KB
/
pubsub-event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @license
* Copyright FabricElements. All Rights Reserved.
*/
import {PubSub} from '@google-cloud/pubsub';
import {Attributes, PublishOptions} from '@google-cloud/pubsub/build/src/publisher/index.js';
import {logger} from 'firebase-functions/v2';
import {emulator} from './variables.js';
/**
* PubSub basic event
*
* @param {PubSub} ps
* @param {string} topic
* @param {object} data
* @param {any} attributes
* @param {any} options
*/
export default async (ps: PubSub, topic: string, data: object = {}, attributes: Attributes = {}, options: PublishOptions = {}) => {
const message = JSON.stringify(data);
const dataBuffer = Buffer.from(message);
const topicClass = ps.topic(topic, options);
try {
const messageId = await topicClass.publishMessage({data: dataBuffer, attributes});
if (emulator) logger.log(`Message ${messageId} published with data: ${JSON.stringify(data)}`);
} catch (error: any) {
logger.error(`Received error while publishing: ${error.message ?? error.toString()}`);
process.exitCode = 1;
}
};