UNPKG

@cn-shell/aws-utils

Version:
102 lines (101 loc) 2.66 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; }, }); } : function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; }); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); } : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function(mod) { return mod && mod.__esModule ? mod : { default: mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Topic = void 0; // imports here const Aws = __importStar(require("./aws-base")); const sns_1 = __importDefault(require("aws-sdk/clients/sns")); // import AWS from "aws-sdk/global"; // AWS.config.logger = console; // SQS consts here const SNS_API_VER = "2010-03-31"; // Class AwsSns here class Topic extends Aws.Base { // Constructor here constructor(name, opts) { super(name, opts); this._publishTopic = opts.publishTopic; this.info("Publish Topic: %s", this._publishTopic); // Create AWS service objects this._sns = new sns_1.default({ region: this._region, apiVersion: SNS_API_VER, }); } // Public and Private methods here async start() { return true; } async stop() {} async healthCheck() { return true; } async injectMessage(msg) { let parsed = JSON.parse(msg); return await this.publish(parsed.msg, parsed.attribs); } async publish(msg, attribs) { let pubParams = { TopicArn: this._publishTopic, Message: msg, MessageAttributes: attribs, }; let success = true; await this._sns .publish(pubParams) .promise() .catch(e => { this.error("SNS publish Error: %s", e); success = false; }); if (this._playbackFile !== "") { this.writePlayback( JSON.stringify({ msg, attribs }), Aws.Base.RecordTypes.SNS, ); } return success; } } exports.Topic = Topic;