|
| 1 | +/* |
| 2 | + Copyright 2021 The CloudEvents Authors |
| 3 | + SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | + |
| 6 | +import { Binding, Deserializer, CloudEvent, CloudEventV1, CONSTANTS, Message, ValidationError, Headers } from "../.."; |
| 7 | + |
| 8 | +export { |
| 9 | + MQTT, |
| 10 | + MQTTMessage, |
| 11 | + MQTTMessageFactory |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * Extends the base {@linkcode Message} interface to include MQTT attributes, some of which |
| 16 | + * are aliases of the {Message} attributes. |
| 17 | + */ |
| 18 | +interface MQTTMessage<T> extends Message<T> { |
| 19 | + /** |
| 20 | + * Identifies this message as a PUBLISH packet. MQTTMessages created with |
| 21 | + * the `binary` and `structured` Serializers will contain a "Content Type" |
| 22 | + * property in the PUBLISH record. |
| 23 | + * @see https://github.com/cloudevents/spec/blob/v1.0.1/mqtt-protocol-binding.md#3-mqtt-publish-message-mapping |
| 24 | + */ |
| 25 | + PUBLISH: Record<string, string | undefined> | undefined |
| 26 | + /** |
| 27 | + * Alias of {Message#body} |
| 28 | + */ |
| 29 | + payload: T | undefined, |
| 30 | + /** |
| 31 | + * Alias of {Message#headers} |
| 32 | + */ |
| 33 | + "User Properties": Headers | undefined |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Binding for MQTT transport support |
| 38 | + * @implements @linkcode Binding |
| 39 | + */ |
| 40 | +const MQTT: Binding = { |
| 41 | + binary, |
| 42 | + structured, |
| 43 | + toEvent: toEvent as Deserializer, |
| 44 | + isEvent |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Converts a CloudEvent into an MQTTMessage<T> with the event's data as the message payload |
| 49 | + * @param {CloudEventV1} event a CloudEvent |
| 50 | + * @returns {MQTTMessage<T>} the event serialized as an MQTTMessage<T> with binary encoding |
| 51 | + * @implements {Serializer} |
| 52 | + */ |
| 53 | +function binary<T>(event: CloudEventV1<T>): MQTTMessage<T> { |
| 54 | + let properties; |
| 55 | + if (event instanceof CloudEvent) { |
| 56 | + properties = event.toJSON(); |
| 57 | + } else { |
| 58 | + properties = event; |
| 59 | + } |
| 60 | + const body = properties.data as T; |
| 61 | + delete properties.data; |
| 62 | + |
| 63 | + return MQTTMessageFactory(event.datacontenttype as string, properties, body); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Converts a CloudEvent into an MQTTMessage<T> with the event as the message payload |
| 68 | + * @param {CloudEventV1} event a CloudEvent |
| 69 | + * @returns {MQTTMessage<T>} the event serialized as an MQTTMessage<T> with structured encoding |
| 70 | + * @implements {Serializer} |
| 71 | + */ |
| 72 | +function structured<T>(event: CloudEventV1<T>): MQTTMessage<T> { |
| 73 | + let body; |
| 74 | + if (event instanceof CloudEvent) { |
| 75 | + body = event.toJSON(); |
| 76 | + } else { |
| 77 | + body = event; |
| 78 | + } |
| 79 | + return MQTTMessageFactory(CONSTANTS.DEFAULT_CE_CONTENT_TYPE, {}, body) as MQTTMessage<T>; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * A helper function to create an MQTTMessage<T> object, with "User Properties" as an alias |
| 84 | + * for "headers" and "payload" an alias for body, and a "PUBLISH" record with a "Content Type" |
| 85 | + * property. |
| 86 | + * @param {string} contentType the "Content Type" attribute on PUBLISH |
| 87 | + * @param {Record<string, unknown>} headers the headers and "User Properties" |
| 88 | + * @param {T} body the message body/payload |
| 89 | + * @returns {MQTTMessage<T>} a message initialized with the provided attributes |
| 90 | + */ |
| 91 | +function MQTTMessageFactory<T>(contentType: string, headers: Record<string, unknown>, body: T): MQTTMessage<T> { |
| 92 | + return { |
| 93 | + PUBLISH: { |
| 94 | + "Content Type": contentType |
| 95 | + }, |
| 96 | + body, |
| 97 | + get payload() { |
| 98 | + return this.body as T; |
| 99 | + }, |
| 100 | + headers: headers as Headers, |
| 101 | + get "User Properties"() { |
| 102 | + return this.headers as any; |
| 103 | + } |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Converts an MQTTMessage<T> into a CloudEvent |
| 109 | + * @param {Message<T>} message the message to deserialize |
| 110 | + * @param {boolean} strict determines if a ValidationError will be thrown on bad input - defaults to false |
| 111 | + * @returns {CloudEventV1<T>} an event |
| 112 | + * @implements {Deserializer} |
| 113 | + */ |
| 114 | +function toEvent<T>(message: Message<T>, strict = false): CloudEventV1<T> | CloudEventV1<T>[] { |
| 115 | + if (strict && !isEvent(message)) { |
| 116 | + throw new ValidationError("No CloudEvent detected"); |
| 117 | + } |
| 118 | + if (isStructuredMessage(message as MQTTMessage<T>)) { |
| 119 | + const evt = (typeof message.body === "string") ? JSON.parse(message.body): message.body; |
| 120 | + return new CloudEvent({ |
| 121 | + ...evt as CloudEventV1<T> |
| 122 | + }, false); |
| 123 | + } else { |
| 124 | + return new CloudEvent<T>({ |
| 125 | + ...message.headers, |
| 126 | + data: message.body as T, |
| 127 | + }, false); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Determine if the message is a CloudEvent |
| 133 | + * @param {Message<T>} message an MQTTMessage |
| 134 | + * @returns {boolean} true if the message contains an event |
| 135 | + */ |
| 136 | +function isEvent<T>(message: Message<T>): boolean { |
| 137 | + return isBinaryMessage(message) || isStructuredMessage(message as MQTTMessage<T>); |
| 138 | +} |
| 139 | + |
| 140 | +function isBinaryMessage<T>(message: Message<T>): boolean { |
| 141 | + return (!!message.headers.id && !!message.headers.source |
| 142 | + && !! message.headers.type && !!message.headers.specversion); |
| 143 | +} |
| 144 | + |
| 145 | +function isStructuredMessage<T>(message: MQTTMessage<T>): boolean { |
| 146 | + if (!message) { return false; } |
| 147 | + return (message.PUBLISH && message?.PUBLISH["Content Type"]?.startsWith(CONSTANTS.MIME_CE_JSON)) || false; |
| 148 | +} |
0 commit comments