forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Schema Registry Avro] New Encoder Design (Azure#19842)
Fixes Azure#20061 ## Overview Revamps the schema registry encoder to work on messages instead of buffers based on the recommendation of the Azure messaging architect. This changes the APIs as follows: ```ts const buffer: NodeJS.Buffer = await serializer.serialize(value, schema); ``` becomes ```ts const message: MessageWithMetadata = await encoder.encodeMessageData(value, schema); ``` where `MessageWithMetadata` has a `body` field as well as a `contentType` field. The latter's format is `avro/binary+<Schema ID>` For derserializing, the change is as follows: ```ts const deserializedValue = await serializer.deserialize(buffer); ``` becomes: ```ts const decodedObject = await encoder.decodeMessageData(message); ``` ## Improvement upon Azure#15959 This design introduces a new `messageAdapter` option in the encoder constructor to support processing of any message type (e.g. [cloud event](https://github.com/cloudevents/spec/blob/v1.0.1/spec.md)): ```ts const encoder = new SchemaRegistryAvroEncoder(schemaRegistryClient, { groupName, messageAdapter: adapter }); ``` where `adapter` is a message adapter that follows the following contract: ```ts interface MessageAdapter<MessageT> { produceMessage: (messageWithMetadata: MessageWithMetadata) => MessageT; consumeMessage: (message: MessageT) => MessageWithMetadata; } interface MessageWithMetadata { body: Uint8Array; contentType: string; } ``` For convenience, the PR adds a couple of convenience adapter factories for Event Hubs's `EventData` and Event Grid's `SendCloudEventInput<Uint8Array>`. For example, the `createCloudEventAdapter` factory can be called to construct an adapter for the latter as follows: ```ts const adapter = createCloudEventAdapter({ type: "azure.sdk.eventgrid.samples.cloudevent", source: "/azure/sdk/schemaregistry/samples/withEventGrid", }), ``` Note that these adapter factories are exported by their respective messaging package without explicitly implementing the contract and the PR adds new encoder tests that check whether the produced adapters follow the contract. This organization could change in the future if we create a new core place for the contract to be imported from. See the newly added samples for how to send such messages with Event Hubs and Event Grid. Schema Registry commitment tracking: Azure#15959 Tracking issue: Azure#18608 First iteration design: Azure#18365
- Loading branch information
1 parent
a3f10ce
commit be4578a
Showing
26 changed files
with
1,219 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { EventData } from "./eventData"; | ||
|
||
/** | ||
* A message with payload and content type fields | ||
* | ||
* This interface is hidden because it is already exported by `@azure/schema-registry-avro` | ||
* | ||
* @hidden | ||
*/ | ||
export interface MessageWithMetadata { | ||
/** | ||
* The message's binary data | ||
*/ | ||
body: Uint8Array; | ||
/** | ||
* The message's content type | ||
*/ | ||
contentType: string; | ||
} | ||
|
||
/** | ||
* A message adapter interface that specifies methods for producing and consuming | ||
* messages with payloads and content type fields. | ||
* | ||
* This interface is hidden because it is already exported by `@azure/schema-registry-avro` | ||
* | ||
* @hidden | ||
*/ | ||
export interface MessageAdapter<MessageT> { | ||
/** | ||
* defines how to create a message from a payload and a content type | ||
*/ | ||
produceMessage: (messageWithMetadata: MessageWithMetadata) => MessageT; | ||
/** | ||
* defines how to access the payload and the content type of a message | ||
*/ | ||
consumeMessage: (message: MessageT) => MessageWithMetadata; | ||
} | ||
|
||
// This type should always be equivalent to Omit<Omit<EventData, "body">, "contentType"> | ||
/** | ||
* Parameters to the `createEventDataAdapter` function that creates an event data adapter. | ||
*/ | ||
export interface EventDataAdapterParameters { | ||
/** | ||
* The correlation identifier that allows an | ||
* application to specify a context for the message for the purposes of correlation, for example | ||
* reflecting the MessageId of a message that is being replied to. | ||
*/ | ||
correlationId?: string | number | Buffer; | ||
|
||
/** | ||
* The message identifier is an | ||
* application-defined value that uniquely identifies the message and its payload. | ||
* | ||
* Note: Numbers that are not whole integers are not allowed. | ||
*/ | ||
messageId?: string | number | Buffer; | ||
|
||
/** | ||
* Set of key value pairs that can be used to set properties specific to user application. | ||
*/ | ||
properties?: { | ||
[key: string]: any; | ||
}; | ||
} | ||
|
||
/** | ||
* A function that constructs an event data adapter. That adapter can be used | ||
* with `@azure/schema-registry-avro` to encode and decode body in event data. | ||
* | ||
* @param params - parameters to create the event data | ||
* @returns An event data adapter that can produce and consume event data | ||
*/ | ||
export function createEventDataAdapter( | ||
params: EventDataAdapterParameters = {} | ||
): MessageAdapter<EventData> { | ||
return { | ||
produceMessage: ({ body, contentType }: MessageWithMetadata) => { | ||
return { | ||
...params, | ||
body, | ||
contentType, | ||
}; | ||
}, | ||
consumeMessage: (message: EventData): MessageWithMetadata => { | ||
const { body, contentType } = message; | ||
if (body === undefined || !(body instanceof Uint8Array)) { | ||
throw new Error("Expected the body field to be defined and have a Uint8Array"); | ||
} | ||
if (contentType === undefined) { | ||
throw new Error("Expected the contentType field to be defined"); | ||
} | ||
return { | ||
body, | ||
contentType, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.