@azure/eventgrid
Version:
An isomorphic client library for the Azure Event Grid service.
88 lines • 4.33 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventGridDeserializer = void 0;
const core_client_1 = require("@azure/core-client");
const models_js_1 = require("./models.js");
const mappers_js_1 = require("./generated/models/mappers.js");
const util_js_1 = require("./util.js");
const serializer = (0, core_client_1.createSerializer)();
/**
* EventGridDeserializer is used to aid in processing events delivered by EventGrid. It can deserialize a JSON encoded payload
* of either a single event or batch of events as well as be used to convert the result of `JSON.parse` into an
* `EventGridEvent` or `CloudEvent` like object.
*
* Unlike normal JSON deseralization, EventGridDeserializer does some additional conversions:
*
* - The consumer parses the event time property into a `Date` object, for ease of use.
* - When deserializing an event in the CloudEvent schema, if the event contains binary data, it is base64 decoded
* and returned as an instance of the `Uint8Array` type.
*/
class EventGridDeserializer {
async deserializeEventGridEvents(encodedEvents) {
const decodedArray = (0, util_js_1.parseAndWrap)(encodedEvents);
const events = [];
for (const o of decodedArray) {
(0, util_js_1.validateEventGridEvent)(o);
const deserialized = serializer.deserialize(mappers_js_1.EventGridEvent, o, "");
events.push(deserialized);
}
return events;
}
async deserializeCloudEvents(encodedEvents) {
const decodedArray = (0, util_js_1.parseAndWrap)(encodedEvents);
const events = [];
for (const o of decodedArray) {
(0, util_js_1.validateCloudEventEvent)(o);
// Check that the required fields are present and of the correct type and the optional fields are missing
// or of the correct type.
const deserialized = serializer.deserialize(mappers_js_1.CloudEvent, o, "");
const modelEvent = {
specversion: deserialized.specversion,
id: deserialized.id,
source: deserialized.source,
type: deserialized.type,
};
if (deserialized.datacontenttype !== undefined) {
modelEvent.datacontenttype = deserialized.datacontenttype;
}
if (deserialized.dataschema !== undefined) {
modelEvent.dataschema = deserialized.dataschema;
}
if (deserialized.subject !== undefined) {
modelEvent.subject = deserialized.subject;
}
if (deserialized.time !== undefined) {
modelEvent.time = deserialized.time;
}
if (deserialized.data !== undefined) {
modelEvent.data = deserialized.data;
}
// If the data the event represents binary, it is encoded as base64 text in a different property on the event and we need to transform it.
if (deserialized.dataBase64 !== undefined) {
if (deserialized.data !== undefined) {
throw new TypeError("event contains both a data and data_base64 field");
}
if (!(deserialized.dataBase64 instanceof Uint8Array)) {
throw new TypeError("event data_base64 property is not an instance of Uint8Array");
}
modelEvent.data = deserialized.dataBase64;
}
// Build the "extensionsAttributes" property bag by removing all known top level properties.
const extensionAttributes = Object.assign({}, deserialized);
for (const propName of models_js_1.cloudEventReservedPropertyNames) {
delete extensionAttributes[propName];
}
delete extensionAttributes.dataBase64;
// If any properties remain, copy them to the model.
if (Object.keys(extensionAttributes).length > 0) {
modelEvent.extensionAttributes = extensionAttributes;
}
events.push(modelEvent);
}
return events;
}
}
exports.EventGridDeserializer = EventGridDeserializer;
//# sourceMappingURL=consumer.js.map