UNPKG

@othree.io/chisel-aws

Version:

AWS Support for Chisel

65 lines 3.57 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.publishEvent = exports.publishShardedEvent = exports.hashCode = void 0; const optional_1 = require("@othree.io/optional"); const client_sns_1 = require("@aws-sdk/client-sns"); const hashCode = (value) => { return [...value].reduce((hash, c) => { const newHash = ((hash << 5) - hash) + c.charCodeAt(0); return newHash & newHash; }, 0); }; exports.hashCode = hashCode; const publishEventToTopic = (snsClient, configuration, topicArn) => (event, state) => { return (0, optional_1.TryAsync)(() => __awaiter(void 0, void 0, void 0, function* () { const metadata = (0, optional_1.Optional)(event.metadata).map(metadata => { return Object.keys(metadata).reduce((acum, key) => { return Object.assign(Object.assign({}, acum), { [key]: { DataType: 'String', StringValue: metadata[key], } }); }, {}); }).orElse({}); const command = new client_sns_1.PublishCommand({ Message: JSON.stringify({ event: event, state: configuration.IncludeState ? state : undefined, }), MessageAttributes: Object.assign(Object.assign({}, metadata), { bc: { DataType: 'String', StringValue: configuration.BoundedContext, }, eventType: { DataType: 'String', StringValue: event.type.toString(), } }), MessageGroupId: configuration.IsFifo ? (0, optional_1.Optional)(configuration.GetMessageGroupId) .map(_ => _(event, state)) .orElse(event.contextId.toString()) : undefined, MessageDeduplicationId: configuration.IsFifo ? (0, optional_1.Optional)(configuration.GetMessageDeduplicationId) .map(_ => _(event, state)) .orElse(event.eventId.toString()) : undefined, TopicArn: topicArn, }); return snsClient.send(command); })).then(maybeResult => maybeResult.map(_ => true)); }; const publishShardedEvent = (hashCode) => (snsClient, configuration) => (event, state) => __awaiter(void 0, void 0, void 0, function* () { const topicIdx = Math.abs(hashCode(event.contextId)) % configuration.TopicsArn.length; const topic = configuration.TopicsArn[topicIdx]; return publishEventToTopic(snsClient, configuration, topic)(event, state); }); exports.publishShardedEvent = publishShardedEvent; const publishEvent = (snsClient, configuration) => (event, state) => __awaiter(void 0, void 0, void 0, function* () { return publishEventToTopic(snsClient, configuration, configuration.TopicArn)(event, state); }); exports.publishEvent = publishEvent; //# sourceMappingURL=actor.js.map