-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmqtt_shared.ts
More file actions
237 lines (196 loc) · 7.46 KB
/
mqtt_shared.ts
File metadata and controls
237 lines (196 loc) · 7.46 KB
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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
/**
* @packageDocumentation
*/
import * as event from "./event";
/**
* Converts payload to Buffer or string regardless of the supplied type
* @param payload The payload to convert
* @internal
*/
export function normalize_payload(payload: any): Buffer | string {
if (payload instanceof Buffer) {
// pass Buffer through
return payload;
}
if (typeof payload === 'string') {
// pass string through
return payload;
}
if (ArrayBuffer.isView(payload)) {
// return Buffer with view upon the same bytes (no copy)
const view = payload as ArrayBufferView;
return Buffer.from(view.buffer, view.byteOffset, view.byteLength);
}
if (payload instanceof ArrayBuffer) {
// return Buffer with view upon the same bytes (no copy)
return Buffer.from(payload);
}
if (typeof payload === 'object') {
// Convert Object to JSON string
return JSON.stringify(payload);
}
if (!payload) {
return "";
}
throw new TypeError("payload parameter must be a string, object, or DataView.");
}
/**
* Converts payload to Buffer only, regardless of the supplied type
* @param payload The payload to convert
* @internal
*/
export function normalize_payload_to_buffer(payload: any): Buffer {
let normalized = normalize_payload(payload);
if (typeof normalized === 'string') {
// pass string through
return Buffer.from(normalized);
}
return normalized;
}
/** @internal */
export const DEFAULT_KEEP_ALIVE : number = 1200;
function isValidTopicInternal(topic: string, isFilter: boolean) : boolean {
if (topic.length === 0 || topic.length > 65535) {
return false;
}
let sawHash : boolean = false;
for (let segment of topic.split('/')) {
if (sawHash) {
return false;
}
if (segment.length === 0) {
continue;
}
if (segment.includes("+")) {
if (!isFilter) {
return false;
}
if (segment.length > 1) {
return false;
}
}
if (segment.includes("#")) {
if (!isFilter) {
return false;
}
if (segment.length > 1) {
return false;
}
sawHash = true;
}
}
return true;
}
export function isValidTopicFilter(topicFilter: any) : boolean {
if (typeof(topicFilter) !== 'string') {
return false;
}
let topicFilterAsString = topicFilter as string;
return isValidTopicInternal(topicFilterAsString, true);
}
export function isValidTopic(topic: any) : boolean {
if (typeof(topic) !== 'string') {
return false;
}
let topicAsString = topic as string;
return isValidTopicInternal(topicAsString, false);
}
export type PublishAcknowledgementFunctor = () => void;
/**
* Wrapper class containing a one-use singleton handle that can be used to trigger sending the acknowledgement (Puback in
* QoS 1, Pubrec in QoS 2) packet for an incoming publish.
*/
export class PublishAcknowledgementHandleWrapper {
private ackHandle : PublishAcknowledgementHandle | null;
constructor(handle : PublishAcknowledgementHandle | null) {
this.ackHandle = handle;
}
/**
* Attempt to take the acknowledgement handle held by the wrapper. This will only succeed for the first caller;
* after the initial call, null will be returned. By taking the handle, the caller assumes responsibility
* for sending the acknowledgement packet associated with the incoming publish packet. Failing to trigger the
* acknowledgement will cause the broker to potentially re-send the publish.
*/
acquireHandle() : PublishAcknowledgementHandle | null {
let handle = this.ackHandle;
this.ackHandle = null;
return handle;
}
}
function movePublishAcknowledgementHandleWrapper(wrapper: PublishAcknowledgementHandleWrapper | undefined, compositionFunctor?: PublishAcknowledgementFunctor) : PublishAcknowledgementHandleWrapper | undefined {
if (wrapper) {
let handle = wrapper.acquireHandle();
if (compositionFunctor && handle) {
let interiorHandle = handle;
handle = new PublishAcknowledgementHandle(() => {
interiorHandle.invokeAcknowledgement();
compositionFunctor();
});
}
return new PublishAcknowledgementHandleWrapper(handle);
}
return undefined;
}
/** @internal */
export function emitAcknowledgeableEvent<T>(emitter: event.BufferedEventEmitter, ackEvent: string, ackEventPayload: T, wrapperFieldName: string, ackHandleWrapper?: PublishAcknowledgementHandleWrapper, compositionFunctor?: PublishAcknowledgementFunctor) : void {
ackHandleWrapper = movePublishAcknowledgementHandleWrapper(ackHandleWrapper, compositionFunctor);
if (ackHandleWrapper) {
(ackEventPayload as any)[wrapperFieldName] = ackHandleWrapper;
emitter.emitWithCallback(ackEvent, () => {
if (ackHandleWrapper) {
let handle = ackHandleWrapper.acquireHandle();
if (handle) {
// Even if corked, all listeners have had a chance to react to the event
// and acquire the acknowledgement handle if they wanted to. If no one did so, then we do it ourselves.
handle.invokeAcknowledgement();
}
}
}, ackEventPayload);
} else {
emitter.emit(ackEvent, ackEventPayload);
}
}
/** @internal */
export function queueAcknowledgeableEvent<T>(emitter: event.BufferedEventEmitter, ackEvent: string, ackEventPayload: T, wrapperFieldName: string, ackHandleWrapper?: PublishAcknowledgementHandleWrapper, compositionFunctor?: PublishAcknowledgementFunctor) : void {
let wrapper : PublishAcknowledgementHandleWrapper | undefined = movePublishAcknowledgementHandleWrapper(ackHandleWrapper, compositionFunctor);
queueMicrotask(() => {
if (wrapper) {
(ackEventPayload as any)[wrapperFieldName] = wrapper;
emitter.emitWithCallback(ackEvent, () => {
if (wrapper) {
let handle = wrapper.acquireHandle();
if (handle) {
// Even if corked, all listeners have had a chance to react to the event
// and acquire the acknowledgement handle if they wanted to. If no one did so, then we do it ourselves.
handle.invokeAcknowledgement();
}
}
}, ackEventPayload);
} else {
emitter.emit(ackEvent, ackEventPayload);
}
});
}
/**
* Object that allows the holder to trigger the acknowledgement for an associated publish packet.
*/
export class PublishAcknowledgementHandle {
private acknowledgementFunction? : PublishAcknowledgementFunctor;
constructor(acknowledgementFunction : PublishAcknowledgementFunctor) {
this.acknowledgementFunction = acknowledgementFunction;
}
/**
* trigger the acknowledgement for an associated Publish packet
*/
invokeAcknowledgement() : void {
let acknowledgementFunction = this.acknowledgementFunction;
this.acknowledgementFunction = undefined;
if (acknowledgementFunction) {
acknowledgementFunction();
}
}
}