-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
eventData.ts
475 lines (440 loc) · 13.5 KB
/
eventData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AmqpAnnotatedMessage, Constants } from "@azure/core-amqp";
import { BodyTypes, defaultDataTransformer } from "./dataTransformer";
import { DeliveryAnnotations, MessageAnnotations, Message as RheaMessage } from "rhea-promise";
import { isDefined, isObjectWithProperties, objectHasProperty } from "./util/typeGuards";
/**
* Describes the delivery annotations.
* @hidden
*/
export interface EventHubDeliveryAnnotations extends DeliveryAnnotations {
/**
* The offset of the last event.
*/
last_enqueued_offset?: string;
/**
* The sequence number of the last event.
*/
last_enqueued_sequence_number?: number;
/**
* The enqueued time of the last event.
*/
last_enqueued_time_utc?: number;
/**
* The retrieval time of the last event.
*/
runtime_info_retrieval_time_utc?: number;
/**
* Any unknown delivery annotations.
*/
[x: string]: any;
}
/**
* Map containing message attributes that will be held in the message header.
* @hidden
*/
export interface EventHubMessageAnnotations extends MessageAnnotations {
/**
* Annotation for the partition key set for the event.
*/
"x-opt-partition-key"?: string | null;
/**
* Annontation for the sequence number of the event.
*/
"x-opt-sequence-number"?: number;
/**
* Annotation for the enqueued time of the event.
*/
"x-opt-enqueued-time"?: number;
/**
* Annotation for the offset of the event.
*/
"x-opt-offset"?: string;
/**
* Any other annotation that can be added to the message.
*/
[x: string]: any;
}
/**
* Describes the structure of an event to be sent or received from the EventHub.
* @hidden
*/
export interface EventDataInternal {
/**
* The message body that needs to be sent or is received.
*/
body: any;
/**
* The enqueued time of the event.
*/
enqueuedTimeUtc?: Date;
/**
* If specified EventHub will hash this to a partitionId.
* It guarantees that messages end up in a specific partition on the event hub.
*/
partitionKey?: string | null;
/**
* The offset of the event.
*/
offset?: number;
/**
* The sequence number of the event.
*/
sequenceNumber?: number;
/**
* The application specific properties.
*/
properties?: { [property: string]: any };
/**
* The last sequence number of the event within the partition stream of the Event Hub.
*/
lastSequenceNumber?: number;
/**
* The offset of the last enqueued event.
*/
lastEnqueuedOffset?: string;
/**
* The enqueued UTC time of the last event.
*/
lastEnqueuedTime?: Date;
/**
* The time when the runtime info was retrieved
*/
retrievalTime?: Date;
/**
* The properties set by the service.
*/
systemProperties?: { [property: string]: any };
/**
* The content type of the message. Optionally describes
* the payload of the message, with a descriptor following the format of RFC2045, Section 5, for
* example "application/json".
*/
contentType?: string;
/**
* 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;
/**
* Returns the underlying raw amqp message.
*/
getRawAmqpMessage(): AmqpAnnotatedMessage;
}
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"
} as const;
/**
* Converts the AMQP message to an EventData.
* @param msg - The AMQP message that needs to be converted to EventData.
* @param skipParsingBodyAsJson - Boolean to skip running JSON.parse() on message body when body type is `content`.
* @hidden
*/
export function fromRheaMessage(
msg: RheaMessage,
skipParsingBodyAsJson: boolean
): EventDataInternal {
const rawMessage = AmqpAnnotatedMessage.fromRheaMessage(msg);
const { body, bodyType } = defaultDataTransformer.decode(msg.body, skipParsingBodyAsJson);
rawMessage.bodyType = bodyType;
const data: EventDataInternal = {
body,
getRawAmqpMessage() {
return rawMessage;
}
};
if (msg.message_annotations) {
for (const annotationKey of Object.keys(msg.message_annotations)) {
switch (annotationKey) {
case Constants.partitionKey:
data.partitionKey = msg.message_annotations[annotationKey];
break;
case Constants.sequenceNumber:
data.sequenceNumber = msg.message_annotations[annotationKey];
break;
case Constants.enqueuedTime:
data.enqueuedTimeUtc = new Date(msg.message_annotations[annotationKey]);
break;
case 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 as number);
data.retrievalTime = new Date(
msg.delivery_annotations.runtime_info_retrieval_time_utc as number
);
}
const messageProperties = Object.keys(messagePropertiesMap) as Array<
keyof typeof 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;
}
/**
* Converts an EventData object to an AMQP message.
* @param data - The EventData object that needs to be converted to an AMQP message.
* @param partitionKey - An optional key to determine the partition that this event should land in.
* @internal
*/
export function toRheaMessage(
data: EventData | AmqpAnnotatedMessage,
partitionKey?: string
): RheaMessage {
let rheaMessage: RheaMessage;
if (isAmqpAnnotatedMessage(data)) {
rheaMessage = {
...AmqpAnnotatedMessage.toRheaMessage(data),
body: defaultDataTransformer.encode(data.body, data.bodyType ?? "data")
};
} else {
let bodyType: BodyTypes = "data";
if (typeof (data as EventDataInternal).getRawAmqpMessage === "function") {
/*
If the event is being round-tripped, then we respect the `bodyType` of the
underlying AMQP message.
*/
bodyType = (data as EventDataInternal).getRawAmqpMessage().bodyType ?? "data";
}
rheaMessage = {
body: defaultDataTransformer.encode(data.body, bodyType)
};
// As per the AMQP 1.0 spec If the message-annotations or delivery-annotations section is omitted,
// it is equivalent to a message-annotations section containing anempty map of annotations.
rheaMessage.message_annotations = {};
if (data.properties) {
rheaMessage.application_properties = data.properties;
}
if (isDefined(partitionKey)) {
rheaMessage.message_annotations[Constants.partitionKey] = partitionKey;
// Event Hub service cannot route messages to a specific partition based on the partition key
// if AMQP message header is an empty object. Hence we make sure that header is always present
// with atleast one property. Setting durable to true, helps us achieve that.
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 > Constants.maxMessageIdLength
) {
throw new Error(
`Length of 'messageId' property on the event cannot be greater than ${Constants.maxMessageIdLength} characters.`
);
}
rheaMessage.message_id = data.messageId;
}
}
return rheaMessage;
}
/**
* The interface that describes the data to be sent to Event Hub.
* Use this as a reference when creating the object to be sent when using the `EventHubProducerClient`.
* For example, `{ body: "your-data" }` or
* ```
* {
* body: "your-data",
* properties: {
* propertyName: "property value"
* }
* }
* ```
*/
export interface EventData {
/**
* The message body that needs to be sent.
* If the application reading the events is not using this SDK,
* convert your body payload to a byte array or Buffer for better
* cross-language compatibility.
*/
body: any;
/**
* The content type of the message. Optionally describes
* the payload of the message, with a descriptor following the format of RFC2045, Section 5, for
* example "application/json".
*/
contentType?: string;
/**
* 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;
};
}
/**
* The interface that describes the structure of the event received from Event Hub.
* Use this as a reference when creating the `processEvents` function to process the events
* recieved from an Event Hub when using the `EventHubConsumerClient`.
*/
export interface ReceivedEventData {
/**
* The message body that needs to be sent or is received.
*/
body: any;
/**
* The application specific properties.
*/
properties?: {
[key: string]: any;
};
/**
* The enqueued time of the event.
*/
enqueuedTimeUtc: Date;
/**
* When specified Event Hub will hash this to a partitionId.
* It guarantees that messages end up in a specific partition on the event hub.
*/
partitionKey: string | null;
/**
* The offset of the event.
*/
offset: number;
/**
* The sequence number of the event.
*/
sequenceNumber: number;
/**
* The properties set by the service.
*/
systemProperties?: {
[key: string]: any;
};
/**
* The content type of the message. Optionally describes
* the payload of the message, with a descriptor following the format of RFC2045, Section 5, for
* example "application/json".
*/
contentType?: string;
/**
* 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.
*/
messageId?: string | number | Buffer;
/**
* Returns the underlying raw amqp message.
*/
getRawAmqpMessage(): AmqpAnnotatedMessage;
}
/**
* @internal
*/
export function isAmqpAnnotatedMessage(possible: unknown): possible is AmqpAnnotatedMessage {
return (
isObjectWithProperties(possible, ["body", "bodyType"]) &&
!objectHasProperty(possible, "getRawAmqpMessage")
);
}
/**
* Converts any Date objects into a number representing date.getTime().
* Recursively checks for any Date objects in arrays and objects.
* @internal
*/
function convertDatesToNumbers<T = unknown>(thing: T): T {
// fast exit
if (!isDefined(thing)) return thing;
// When 'thing' is a Date, return the number representation
if (
typeof thing === "object" &&
objectHasProperty(thing, "getTime") &&
typeof thing.getTime === "function"
) {
return thing.getTime();
}
/*
Examples:
[0, 'foo', new Date(), { nested: new Date()}]
*/
if (Array.isArray(thing)) {
return (thing.map(convertDatesToNumbers) as unknown) as T;
}
/*
Examples:
{ foo: new Date(), children: { nested: new Date() }}
*/
if (typeof thing === "object" && isDefined(thing)) {
thing = { ...thing };
for (const key of Object.keys(thing)) {
(thing as any)[key] = convertDatesToNumbers((thing as any)[key]);
}
}
return thing;
}