UNPKG

@azure/event-hubs

Version:
143 lines (142 loc) 6.1 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 dataTransformer_exports = {}; __export(dataTransformer_exports, { dataSectionTypeCode: () => dataSectionTypeCode, defaultDataTransformer: () => defaultDataTransformer, isRheaAmqpSection: () => isRheaAmqpSection, sequenceSectionTypeCode: () => sequenceSectionTypeCode, valueSectionTypeCode: () => valueSectionTypeCode }); module.exports = __toCommonJS(dataTransformer_exports); var import_logger = require("./logger.js"); var import_buffer = require("buffer"); var import_is_buffer = __toESM(require("is-buffer")); var import_rhea_promise = require("rhea-promise"); const dataSectionTypeCode = 117; const sequenceSectionTypeCode = 118; const valueSectionTypeCode = 119; const defaultDataTransformer = { /** * A function that takes the body property from an EventData object * and returns an encoded body (some form of AMQP type). * * @param body - The AMQP message body * @param bodyType - The AMQP section to story the body in. * @returns The encoded AMQP message body as an AMQP Data/Sequence/Value section. */ encode(body, bodyType) { let result; const normalizedBody = body === void 0 ? null : body; if (bodyType === "value") { result = import_rhea_promise.message.data_section(normalizedBody); result.typecode = valueSectionTypeCode; } else if (bodyType === "sequence") { result = import_rhea_promise.message.sequence_section(normalizedBody); } else if ((0, import_is_buffer.default)(normalizedBody) || normalizedBody instanceof Uint8Array) { result = import_rhea_promise.message.data_section(normalizedBody); } else if (normalizedBody === null && bodyType === "data") { result = import_rhea_promise.message.data_section(null); } else { try { const bodyStr = JSON.stringify(normalizedBody); result = import_rhea_promise.message.data_section(import_buffer.Buffer.from(bodyStr, "utf8")); } catch (err) { const msg = `An error occurred while executing JSON.stringify() on the given body ` + body + `${err ? err.stack : JSON.stringify(err)}`; import_logger.logger.warning("[encode] " + msg); (0, import_logger.logErrorStackTrace)(err); throw new Error(msg); } } return result; }, /** * A function that takes the body property from an AMQP message, which can come from either * the 'data', 'value' or 'sequence' sections of an AMQP message. * * If the body is not a JSON string the the raw contents will be returned, along with the bodyType * indicating which part of the AMQP message the body was decoded from. * * @param body - The AMQP message body as received from rhea. * @param skipParsingBodyAsJson - Boolean to skip running JSON.parse() on message body when body type is `content`. * @returns The decoded/raw body and the body type. */ decode(body, skipParsingBodyAsJson) { try { if (isRheaAmqpSection(body)) { switch (body.typecode) { case dataSectionTypeCode: return { body: skipParsingBodyAsJson ? body.content : tryToJsonDecode(body.content), bodyType: "data" }; case sequenceSectionTypeCode: return { body: body.content, bodyType: "sequence" }; case valueSectionTypeCode: return { body: body.content, bodyType: "value" }; } } else { if ((0, import_is_buffer.default)(body)) { return { body: skipParsingBodyAsJson ? body : tryToJsonDecode(body), bodyType: "data" }; } return { body, bodyType: "value" }; } } catch (err) { import_logger.logger.verbose( "[decode] An error occurred while decoding the received message body. The error is: %O", err ); throw err; } } }; function tryToJsonDecode(body) { let processedBody = body; try { const bodyStr = processedBody.toString("utf8"); processedBody = JSON.parse(bodyStr); } catch (err) { import_logger.logger.verbose( "[decode] An error occurred while trying JSON.parse() on the received body. The error is %O", err ); } return processedBody; } function isRheaAmqpSection(possibleSection) { return possibleSection != null && typeof possibleSection.typecode === "number" && (possibleSection.typecode === dataSectionTypeCode || possibleSection.typecode === valueSectionTypeCode || possibleSection.typecode === sequenceSectionTypeCode); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { dataSectionTypeCode, defaultDataTransformer, isRheaAmqpSection, sequenceSectionTypeCode, valueSectionTypeCode }); //# sourceMappingURL=dataTransformer.js.map