UNPKG

@azure/event-hubs

Version:
242 lines (241 loc) • 9.87 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var eventData_exports = {}; __export(eventData_exports, { assertIsEventData: () => assertIsEventData, fromRheaMessage: () => fromRheaMessage, isAmqpAnnotatedMessage: () => isAmqpAnnotatedMessage, populateIdempotentMessageAnnotations: () => populateIdempotentMessageAnnotations, toRheaMessage: () => toRheaMessage }); module.exports = __toCommonJS(eventData_exports); var import_core_amqp = require("@azure/core-amqp"); var import_dataTransformer = require("./dataTransformer.js"); var import_rhea_promise = require("rhea-promise"); var import_core_util = require("@azure/core-util"); var import_constants = require("./util/constants.js"); var import_is_buffer = __toESM(require("is-buffer")); const messagePropertiesMap = { message_id: "messageId", user_id: "userId", to: "to", subject: "subject", reply_to: "replyTo", correlation_id: "correlationId", content_type: "contentType", content_encoding: "contentEncoding", absolute_expiry_time: "absoluteExpiryTime", creation_time: "creationTime", group_id: "groupId", group_sequence: "groupSequence", reply_to_group_id: "replyToGroupId" }; function fromRheaMessage(msg, skipParsingBodyAsJson) { const rawMessage = import_core_amqp.AmqpAnnotatedMessage.fromRheaMessage(msg); const { body, bodyType } = import_dataTransformer.defaultDataTransformer.decode(msg.body, skipParsingBodyAsJson); rawMessage.bodyType = bodyType; const data = { body, getRawAmqpMessage() { return rawMessage; } }; if (msg.message_annotations) { for (const annotationKey of Object.keys(msg.message_annotations)) { switch (annotationKey) { case import_core_amqp.Constants.partitionKey: data.partitionKey = msg.message_annotations[annotationKey]; break; case import_core_amqp.Constants.sequenceNumber: data.sequenceNumber = msg.message_annotations[annotationKey]; break; case import_core_amqp.Constants.enqueuedTime: data.enqueuedTimeUtc = new Date(msg.message_annotations[annotationKey]); break; case import_core_amqp.Constants.offset: data.offset = msg.message_annotations[annotationKey]; break; default: if (!data.systemProperties) { data.systemProperties = {}; } data.systemProperties[annotationKey] = convertDatesToNumbers( msg.message_annotations[annotationKey] ); break; } } } if (msg.application_properties) { data.properties = convertDatesToNumbers(msg.application_properties); } if (msg.delivery_annotations) { data.lastEnqueuedOffset = msg.delivery_annotations.last_enqueued_offset; data.lastSequenceNumber = msg.delivery_annotations.last_enqueued_sequence_number; data.lastEnqueuedTime = new Date(msg.delivery_annotations.last_enqueued_time_utc); data.retrievalTime = new Date( msg.delivery_annotations.runtime_info_retrieval_time_utc ); } const messageProperties = Object.keys(messagePropertiesMap); for (const messageProperty of messageProperties) { if (!data.systemProperties) { data.systemProperties = {}; } if (msg[messageProperty] != null) { data.systemProperties[messagePropertiesMap[messageProperty]] = convertDatesToNumbers( msg[messageProperty] ); } } if (msg.content_type != null) { data.contentType = msg.content_type; } if (msg.correlation_id != null) { data.correlationId = msg.correlation_id; } if (msg.message_id != null) { data.messageId = msg.message_id; } return data; } function toRheaMessage(data, partitionKey) { let rheaMessage; if (isAmqpAnnotatedMessage(data)) { rheaMessage = { ...import_core_amqp.AmqpAnnotatedMessage.toRheaMessage(data), body: import_dataTransformer.defaultDataTransformer.encode(data.body, data.bodyType ?? "data") }; } else { let bodyType = "data"; if (typeof data.getRawAmqpMessage === "function") { bodyType = data.getRawAmqpMessage().bodyType ?? "data"; } rheaMessage = { body: import_dataTransformer.defaultDataTransformer.encode(data.body, bodyType) }; rheaMessage.message_annotations = {}; if (data.properties) { rheaMessage.application_properties = data.properties; } if ((0, import_core_util.isDefined)(partitionKey)) { rheaMessage.message_annotations[import_core_amqp.Constants.partitionKey] = partitionKey; rheaMessage.durable = true; } if (data.contentType != null) { rheaMessage.content_type = data.contentType; } if (data.correlationId != null) { rheaMessage.correlation_id = data.correlationId; } if (data.messageId != null) { if (typeof data.messageId === "string" && data.messageId.length > import_core_amqp.Constants.maxMessageIdLength) { throw new Error( `Length of 'messageId' property on the event cannot be greater than ${import_core_amqp.Constants.maxMessageIdLength} characters.` ); } rheaMessage.message_id = data.messageId; } } return rheaMessage; } function assertIsEventData(data) { if (data.contentType != null && typeof data.contentType !== "string") { throw new Error( `Invalid 'contentType': expected 'string' or 'undefined', but received '${typeof data.contentType}'.` ); } if (data.correlationId != null && typeof data.correlationId !== "string" && typeof data.correlationId !== "number" && !(0, import_is_buffer.default)(data.correlationId)) { throw new Error( `Invalid 'correlationId': expected 'string', 'number', 'Buffer', or 'undefined', but received '${typeof data.correlationId}'.` ); } if (data.messageId != null && typeof data.messageId !== "string" && typeof data.messageId !== "number" && !(0, import_is_buffer.default)(data.messageId)) { throw new Error( `Invalid 'messageId': expected 'string', 'number', 'Buffer', or 'undefined', but received '${typeof data.messageId}'.` ); } if (data.properties !== void 0 && (typeof data.properties !== "object" || Array.isArray(data.properties))) { const actualType = Array.isArray(data.properties) ? "array" : typeof data.properties; throw new Error( `Invalid 'properties': expected an object or 'undefined', but received '${actualType}'.` ); } } function isAmqpAnnotatedMessage(possible) { return (0, import_core_util.isObjectWithProperties)(possible, ["body", "bodyType"]) && !(0, import_core_util.objectHasProperty)(possible, "getRawAmqpMessage"); } function convertDatesToNumbers(thing) { if (!(0, import_core_util.isDefined)(thing)) return thing; if (typeof thing === "object" && (0, import_core_util.objectHasProperty)(thing, "getTime") && typeof thing.getTime === "function") { return thing.getTime(); } if (Array.isArray(thing)) { return thing.map(convertDatesToNumbers); } if (typeof thing === "object" && (0, import_core_util.isDefined)(thing)) { const thingShallowCopy = { ...thing }; for (const key of Object.keys(thingShallowCopy)) { thingShallowCopy[key] = convertDatesToNumbers(thingShallowCopy[key]); } return thingShallowCopy; } return thing; } function populateIdempotentMessageAnnotations(rheaMessage, { isIdempotentPublishingEnabled, ownerLevel, producerGroupId, publishSequenceNumber }) { if (!isIdempotentPublishingEnabled) { return; } const messageAnnotations = rheaMessage.message_annotations || {}; if (!rheaMessage.message_annotations) { rheaMessage.message_annotations = messageAnnotations; } if ((0, import_core_util.isDefined)(ownerLevel)) { messageAnnotations[import_constants.idempotentProducerAmqpPropertyNames.epoch] = import_rhea_promise.types.wrap_short(ownerLevel); } if ((0, import_core_util.isDefined)(producerGroupId)) { messageAnnotations[import_constants.idempotentProducerAmqpPropertyNames.producerId] = import_rhea_promise.types.wrap_long(producerGroupId); } if ((0, import_core_util.isDefined)(publishSequenceNumber)) { messageAnnotations[import_constants.idempotentProducerAmqpPropertyNames.producerSequenceNumber] = import_rhea_promise.types.wrap_int(publishSequenceNumber); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { assertIsEventData, fromRheaMessage, isAmqpAnnotatedMessage, populateIdempotentMessageAnnotations, toRheaMessage }); //# sourceMappingURL=eventData.js.map