@smartcloud/pubsub-listener
Version:
Pubsub Listener Module of SmartCloud Utility Library for Node.js
100 lines (90 loc) • 4.08 kB
JavaScript
/*!
* Copyright 2017 Smart Parking Ltd. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module @smartcloud/pubsub-listener
*/
;
const Buffer = require('safe-buffer').Buffer;
const SmartCloudCommon = require('@smartcloud/common');
const SmartCloudErrors = require('@smartcloud/errors');
const SmartCloudLog = require('@smartcloud/logging');
/**
*
* @constructor
*
* @param {string} options.trigger - [Required] Trigger pubsub for this function
*
* @example
* console.error(new Log(obj, message))
*/
function PubsubListener(options) {
if(!options.trigger) throw new Error("trigger not defined when creating PubsubListener");
if(!options.listenerName) throw new Error("listenerName not defined when creating PubsubListener");
if(!options.targets) throw new Error("target not defined when creating SmartFunction");
let trigger = options.trigger;
let listenerName = options.listenerName;
let targets = options.targets;
let filter = options.filter;
let allowedSubtypes = undefined;
if(options.filter) {
allowedSubtypes = [];
options.filter.forEach(( subtype ) => {
allowedSubtypes[subtype] = subtype;
});
}
/**
* Decode data from a pubsub trigger event
*
* @param {object} event - The supplied pubsub message.
*
*/
this.getMessage = function (event) {
return new Promise(function(resolve, reject) {
const pubsubMessage = event.data;
const rawStringMessage = Buffer.from(pubsubMessage.data, 'base64').toString();
let decodedMessage = undefined;
try {
decodedMessage = JSON.parse(rawStringMessage);
} catch (error) {
let logMessage = `The listener - "${listenerName}" - cannot parse raw message - "${rawStringMessage}" - into JSON object. Skipping process. Error => ${error}`;
throw new SmartCloudErrors.FlowControlError(logMessage);
}
const factSubtype = decodedMessage.fact ? decodedMessage.fact.factSubtype : (decodedMessage.factSubtype ? decodedMessage.factSubtype : 'unknown-fact-subtype');
if(!filter || allowedSubtypes[factSubtype]) {
var newSource = {
sourceId: listenerName,
sourceType: 'cloud-function',
input: trigger,
inputType: 'pubsub',
output: targets
}
SmartCloudCommon.SmartCloudObject.addSourceTo(decodedMessage, newSource);
console.log(SmartCloudLog.Log(listenerName, decodedMessage, `${listenerName} received a message.`));
resolve({
eventId: event.eventId,
timestamp: event.timestamp,
attributes: event.data && event.data.attributes ? event.data.attributes : {},
message: decodedMessage
});
} else {
let logMessage = `${factSubtype} is not intended for this listener - "${listenerName}". Skipping process.`;
console.log(SmartCloudLog.Log(listenerName, decodedMessage, logMessage));
throw new SmartCloudErrors.FlowControlError(logMessage);
}
});
}
}
exports.PubsubListener = PubsubListener;