aws-appsync-subscription-link 3.1.2 → 3.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/__tests__/link/realtime-subscription-handshake-link-test.ts +89 -28
- package/lib/index.d.mts +44 -0
- package/lib/index.js +5 -4
- package/lib/index.mjs +3631 -0
- package/lib/non-terminating-http-link.js +6 -5
- package/lib/non-terminating-link.js +6 -3
- package/lib/realtime-subscription-handshake-link.d.ts +1 -1
- package/lib/realtime-subscription-handshake-link.js +87 -57
- package/lib/subscription-handshake-link.d.ts +1 -1
- package/lib/subscription-handshake-link.js +9 -6
- package/lib/types/index.d.ts +4 -4
- package/lib/types/index.js +1 -0
- package/lib/utils/index.js +2 -1
- package/lib/utils/logger.d.ts +1 -1
- package/lib/utils/logger.js +11 -9
- package/lib/utils/retry.js +9 -5
- package/package.json +1 -1
- package/lib/vendor/paho-mqtt.js +0 -2392
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,3631 @@
|
|
|
1
|
+
function _array_like_to_array(arr, len) {
|
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
+
return arr2;
|
|
5
|
+
}
|
|
6
|
+
function _array_with_holes(arr) {
|
|
7
|
+
if (Array.isArray(arr)) return arr;
|
|
8
|
+
}
|
|
9
|
+
function _array_without_holes(arr) {
|
|
10
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
11
|
+
}
|
|
12
|
+
function _assert_this_initialized(self1) {
|
|
13
|
+
if (self1 === void 0) {
|
|
14
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
15
|
+
}
|
|
16
|
+
return self1;
|
|
17
|
+
}
|
|
18
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
19
|
+
try {
|
|
20
|
+
var info = gen[key](arg);
|
|
21
|
+
var value = info.value;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
reject(error);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (info.done) {
|
|
27
|
+
resolve(value);
|
|
28
|
+
} else {
|
|
29
|
+
Promise.resolve(value).then(_next, _throw);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function _async_to_generator(fn) {
|
|
33
|
+
return function() {
|
|
34
|
+
var self1 = this, args = arguments;
|
|
35
|
+
return new Promise(function(resolve, reject) {
|
|
36
|
+
var gen = fn.apply(self1, args);
|
|
37
|
+
function _next(value) {
|
|
38
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
39
|
+
}
|
|
40
|
+
function _throw(err) {
|
|
41
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
42
|
+
}
|
|
43
|
+
_next(undefined);
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function _call_super(_this, derived, args) {
|
|
48
|
+
derived = _get_prototype_of(derived);
|
|
49
|
+
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
|
|
50
|
+
}
|
|
51
|
+
function _class_call_check(instance, Constructor) {
|
|
52
|
+
if (!(instance instanceof Constructor)) {
|
|
53
|
+
throw new TypeError("Cannot call a class as a function");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function _construct(Parent, args, Class) {
|
|
57
|
+
if (_is_native_reflect_construct()) {
|
|
58
|
+
_construct = Reflect.construct;
|
|
59
|
+
} else {
|
|
60
|
+
_construct = function construct(Parent, args, Class) {
|
|
61
|
+
var a = [
|
|
62
|
+
null
|
|
63
|
+
];
|
|
64
|
+
a.push.apply(a, args);
|
|
65
|
+
var Constructor = Function.bind.apply(Parent, a);
|
|
66
|
+
var instance = new Constructor();
|
|
67
|
+
if (Class) _set_prototype_of(instance, Class.prototype);
|
|
68
|
+
return instance;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return _construct.apply(null, arguments);
|
|
72
|
+
}
|
|
73
|
+
function _defineProperties(target, props) {
|
|
74
|
+
for(var i = 0; i < props.length; i++){
|
|
75
|
+
var descriptor = props[i];
|
|
76
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
77
|
+
descriptor.configurable = true;
|
|
78
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
79
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
|
83
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
84
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
85
|
+
return Constructor;
|
|
86
|
+
}
|
|
87
|
+
function _define_property(obj, key, value) {
|
|
88
|
+
if (key in obj) {
|
|
89
|
+
Object.defineProperty(obj, key, {
|
|
90
|
+
value: value,
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: true,
|
|
93
|
+
writable: true
|
|
94
|
+
});
|
|
95
|
+
} else {
|
|
96
|
+
obj[key] = value;
|
|
97
|
+
}
|
|
98
|
+
return obj;
|
|
99
|
+
}
|
|
100
|
+
function _get_prototype_of(o) {
|
|
101
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
|
102
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
103
|
+
};
|
|
104
|
+
return _get_prototype_of(o);
|
|
105
|
+
}
|
|
106
|
+
function _inherits(subClass, superClass) {
|
|
107
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
108
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
109
|
+
}
|
|
110
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
111
|
+
constructor: {
|
|
112
|
+
value: subClass,
|
|
113
|
+
writable: true,
|
|
114
|
+
configurable: true
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
|
118
|
+
}
|
|
119
|
+
function _instanceof(left, right) {
|
|
120
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
121
|
+
return !!right[Symbol.hasInstance](left);
|
|
122
|
+
} else {
|
|
123
|
+
return left instanceof right;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function _is_native_function(fn) {
|
|
127
|
+
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
128
|
+
}
|
|
129
|
+
function _iterable_to_array(iter) {
|
|
130
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
131
|
+
}
|
|
132
|
+
function _iterable_to_array_limit(arr, i) {
|
|
133
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
134
|
+
if (_i == null) return;
|
|
135
|
+
var _arr = [];
|
|
136
|
+
var _n = true;
|
|
137
|
+
var _d = false;
|
|
138
|
+
var _s, _e;
|
|
139
|
+
try {
|
|
140
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
141
|
+
_arr.push(_s.value);
|
|
142
|
+
if (i && _arr.length === i) break;
|
|
143
|
+
}
|
|
144
|
+
} catch (err) {
|
|
145
|
+
_d = true;
|
|
146
|
+
_e = err;
|
|
147
|
+
} finally{
|
|
148
|
+
try {
|
|
149
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
150
|
+
} finally{
|
|
151
|
+
if (_d) throw _e;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return _arr;
|
|
155
|
+
}
|
|
156
|
+
function _non_iterable_rest() {
|
|
157
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
158
|
+
}
|
|
159
|
+
function _non_iterable_spread() {
|
|
160
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
161
|
+
}
|
|
162
|
+
function _object_spread(target) {
|
|
163
|
+
for(var i = 1; i < arguments.length; i++){
|
|
164
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
165
|
+
var ownKeys = Object.keys(source);
|
|
166
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
167
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
168
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
ownKeys.forEach(function(key) {
|
|
172
|
+
_define_property(target, key, source[key]);
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return target;
|
|
176
|
+
}
|
|
177
|
+
function ownKeys(object, enumerableOnly) {
|
|
178
|
+
var keys = Object.keys(object);
|
|
179
|
+
if (Object.getOwnPropertySymbols) {
|
|
180
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
181
|
+
if (enumerableOnly) {
|
|
182
|
+
symbols = symbols.filter(function(sym) {
|
|
183
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
keys.push.apply(keys, symbols);
|
|
187
|
+
}
|
|
188
|
+
return keys;
|
|
189
|
+
}
|
|
190
|
+
function _object_spread_props(target, source) {
|
|
191
|
+
source = source != null ? source : {};
|
|
192
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
193
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
194
|
+
} else {
|
|
195
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
196
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return target;
|
|
200
|
+
}
|
|
201
|
+
function _object_without_properties(source, excluded) {
|
|
202
|
+
if (source == null) return {};
|
|
203
|
+
var target = _object_without_properties_loose(source, excluded);
|
|
204
|
+
var key, i;
|
|
205
|
+
if (Object.getOwnPropertySymbols) {
|
|
206
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
207
|
+
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
208
|
+
key = sourceSymbolKeys[i];
|
|
209
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
210
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
211
|
+
target[key] = source[key];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return target;
|
|
215
|
+
}
|
|
216
|
+
function _object_without_properties_loose(source, excluded) {
|
|
217
|
+
if (source == null) return {};
|
|
218
|
+
var target = {};
|
|
219
|
+
var sourceKeys = Object.keys(source);
|
|
220
|
+
var key, i;
|
|
221
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
222
|
+
key = sourceKeys[i];
|
|
223
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
224
|
+
target[key] = source[key];
|
|
225
|
+
}
|
|
226
|
+
return target;
|
|
227
|
+
}
|
|
228
|
+
function _possible_constructor_return(self1, call) {
|
|
229
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) {
|
|
230
|
+
return call;
|
|
231
|
+
}
|
|
232
|
+
return _assert_this_initialized(self1);
|
|
233
|
+
}
|
|
234
|
+
function _set_prototype_of(o, p) {
|
|
235
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
|
236
|
+
o.__proto__ = p;
|
|
237
|
+
return o;
|
|
238
|
+
};
|
|
239
|
+
return _set_prototype_of(o, p);
|
|
240
|
+
}
|
|
241
|
+
function _sliced_to_array(arr, i) {
|
|
242
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
243
|
+
}
|
|
244
|
+
function _to_consumable_array(arr) {
|
|
245
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
246
|
+
}
|
|
247
|
+
function _to_primitive(input, hint) {
|
|
248
|
+
if (_type_of(input) !== "object" || input === null) return input;
|
|
249
|
+
var prim = input[Symbol.toPrimitive];
|
|
250
|
+
if (prim !== undefined) {
|
|
251
|
+
var res = prim.call(input, hint || "default");
|
|
252
|
+
if (_type_of(res) !== "object") return res;
|
|
253
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
254
|
+
}
|
|
255
|
+
return (hint === "string" ? String : Number)(input);
|
|
256
|
+
}
|
|
257
|
+
function _to_property_key(arg) {
|
|
258
|
+
var key = _to_primitive(arg, "string");
|
|
259
|
+
return _type_of(key) === "symbol" ? key : String(key);
|
|
260
|
+
}
|
|
261
|
+
function _type_of(obj) {
|
|
262
|
+
"@swc/helpers - typeof";
|
|
263
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
264
|
+
}
|
|
265
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
266
|
+
if (!o) return;
|
|
267
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
268
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
269
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
270
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
271
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
272
|
+
}
|
|
273
|
+
function _wrap_native_super(Class) {
|
|
274
|
+
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
275
|
+
_wrap_native_super = function wrapNativeSuper(Class) {
|
|
276
|
+
if (Class === null || !_is_native_function(Class)) return Class;
|
|
277
|
+
if (typeof Class !== "function") {
|
|
278
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
279
|
+
}
|
|
280
|
+
if (typeof _cache !== "undefined") {
|
|
281
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
282
|
+
_cache.set(Class, Wrapper);
|
|
283
|
+
}
|
|
284
|
+
function Wrapper() {
|
|
285
|
+
return _construct(Class, arguments, _get_prototype_of(this).constructor);
|
|
286
|
+
}
|
|
287
|
+
Wrapper.prototype = Object.create(Class.prototype, {
|
|
288
|
+
constructor: {
|
|
289
|
+
value: Wrapper,
|
|
290
|
+
enumerable: false,
|
|
291
|
+
writable: true,
|
|
292
|
+
configurable: true
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
return _set_prototype_of(Wrapper, Class);
|
|
296
|
+
};
|
|
297
|
+
return _wrap_native_super(Class);
|
|
298
|
+
}
|
|
299
|
+
function _is_native_reflect_construct() {
|
|
300
|
+
try {
|
|
301
|
+
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
|
302
|
+
} catch (_) {}
|
|
303
|
+
return (_is_native_reflect_construct = function() {
|
|
304
|
+
return !!result;
|
|
305
|
+
})();
|
|
306
|
+
}
|
|
307
|
+
function _ts_generator(thisArg, body) {
|
|
308
|
+
var f, y, t, _ = {
|
|
309
|
+
label: 0,
|
|
310
|
+
sent: function() {
|
|
311
|
+
if (t[0] & 1) throw t[1];
|
|
312
|
+
return t[1];
|
|
313
|
+
},
|
|
314
|
+
trys: [],
|
|
315
|
+
ops: []
|
|
316
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
317
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
318
|
+
return this;
|
|
319
|
+
}), g;
|
|
320
|
+
function verb(n) {
|
|
321
|
+
return function(v) {
|
|
322
|
+
return step([
|
|
323
|
+
n,
|
|
324
|
+
v
|
|
325
|
+
]);
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
function step(op) {
|
|
329
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
330
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
331
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
332
|
+
if (y = 0, t) op = [
|
|
333
|
+
op[0] & 2,
|
|
334
|
+
t.value
|
|
335
|
+
];
|
|
336
|
+
switch(op[0]){
|
|
337
|
+
case 0:
|
|
338
|
+
case 1:
|
|
339
|
+
t = op;
|
|
340
|
+
break;
|
|
341
|
+
case 4:
|
|
342
|
+
_.label++;
|
|
343
|
+
return {
|
|
344
|
+
value: op[1],
|
|
345
|
+
done: false
|
|
346
|
+
};
|
|
347
|
+
case 5:
|
|
348
|
+
_.label++;
|
|
349
|
+
y = op[1];
|
|
350
|
+
op = [
|
|
351
|
+
0
|
|
352
|
+
];
|
|
353
|
+
continue;
|
|
354
|
+
case 7:
|
|
355
|
+
op = _.ops.pop();
|
|
356
|
+
_.trys.pop();
|
|
357
|
+
continue;
|
|
358
|
+
default:
|
|
359
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
360
|
+
_ = 0;
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
364
|
+
_.label = op[1];
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
368
|
+
_.label = t[1];
|
|
369
|
+
t = op;
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
if (t && _.label < t[2]) {
|
|
373
|
+
_.label = t[2];
|
|
374
|
+
_.ops.push(op);
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
if (t[2]) _.ops.pop();
|
|
378
|
+
_.trys.pop();
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
op = body.call(thisArg, _);
|
|
382
|
+
} catch (e) {
|
|
383
|
+
op = [
|
|
384
|
+
6,
|
|
385
|
+
e
|
|
386
|
+
];
|
|
387
|
+
y = 0;
|
|
388
|
+
} finally{
|
|
389
|
+
f = t = 0;
|
|
390
|
+
}
|
|
391
|
+
if (op[0] & 5) throw op[1];
|
|
392
|
+
return {
|
|
393
|
+
value: op[0] ? op[1] : void 0,
|
|
394
|
+
done: true
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
var __create = Object.create;
|
|
399
|
+
var __defProp = Object.defineProperty;
|
|
400
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
401
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
402
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
403
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
404
|
+
var __commonJS = function(cb, mod) {
|
|
405
|
+
return function __require() {
|
|
406
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = {
|
|
407
|
+
exports: {}
|
|
408
|
+
}).exports, mod), mod.exports;
|
|
409
|
+
};
|
|
410
|
+
};
|
|
411
|
+
var __copyProps = function(to, from, except, desc) {
|
|
412
|
+
if (from && (typeof from === "undefined" ? "undefined" : _type_of(from)) === "object" || typeof from === "function") {
|
|
413
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
414
|
+
try {
|
|
415
|
+
var _loop = function() {
|
|
416
|
+
var key = _step.value;
|
|
417
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
418
|
+
get: function() {
|
|
419
|
+
return from[key];
|
|
420
|
+
},
|
|
421
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
422
|
+
});
|
|
423
|
+
};
|
|
424
|
+
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
425
|
+
} catch (err) {
|
|
426
|
+
_didIteratorError = true;
|
|
427
|
+
_iteratorError = err;
|
|
428
|
+
} finally{
|
|
429
|
+
try {
|
|
430
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
431
|
+
_iterator.return();
|
|
432
|
+
}
|
|
433
|
+
} finally{
|
|
434
|
+
if (_didIteratorError) {
|
|
435
|
+
throw _iteratorError;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return to;
|
|
441
|
+
};
|
|
442
|
+
var __toESM = function(mod, isNodeMode, target) {
|
|
443
|
+
return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(// If the importer is in node compatibility mode or this is not an ESM
|
|
444
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
445
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
446
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
447
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
448
|
+
value: mod,
|
|
449
|
+
enumerable: true
|
|
450
|
+
}) : target, mod);
|
|
451
|
+
};
|
|
452
|
+
// src/vendor/paho-mqtt.js
|
|
453
|
+
var require_paho_mqtt = __commonJS({
|
|
454
|
+
"src/vendor/paho-mqtt.js": function(exports, module) {
|
|
455
|
+
(function ExportLibrary(root, factory) {
|
|
456
|
+
if ((typeof exports === "undefined" ? "undefined" : _type_of(exports)) === "object" && (typeof module === "undefined" ? "undefined" : _type_of(module)) === "object") {
|
|
457
|
+
module.exports = factory();
|
|
458
|
+
} else if (typeof define === "function" && define.amd) {
|
|
459
|
+
define(factory);
|
|
460
|
+
} else if ((typeof exports === "undefined" ? "undefined" : _type_of(exports)) === "object") {
|
|
461
|
+
exports = factory();
|
|
462
|
+
} else {
|
|
463
|
+
root.Paho = factory();
|
|
464
|
+
}
|
|
465
|
+
})(exports, function LibraryFactory() {
|
|
466
|
+
var PahoMQTT = function(global2) {
|
|
467
|
+
var version = "@VERSION@-@BUILDLEVEL@";
|
|
468
|
+
var localStorage = global2.localStorage || /* @__PURE__ */ function() {
|
|
469
|
+
var data = {};
|
|
470
|
+
return {
|
|
471
|
+
setItem: function setItem(key, item) {
|
|
472
|
+
data[key] = item;
|
|
473
|
+
},
|
|
474
|
+
getItem: function getItem(key) {
|
|
475
|
+
return data[key];
|
|
476
|
+
},
|
|
477
|
+
removeItem: function removeItem(key) {
|
|
478
|
+
delete data[key];
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
}();
|
|
482
|
+
var MESSAGE_TYPE = {
|
|
483
|
+
CONNECT: 1,
|
|
484
|
+
CONNACK: 2,
|
|
485
|
+
PUBLISH: 3,
|
|
486
|
+
PUBACK: 4,
|
|
487
|
+
PUBREC: 5,
|
|
488
|
+
PUBREL: 6,
|
|
489
|
+
PUBCOMP: 7,
|
|
490
|
+
SUBSCRIBE: 8,
|
|
491
|
+
SUBACK: 9,
|
|
492
|
+
UNSUBSCRIBE: 10,
|
|
493
|
+
UNSUBACK: 11,
|
|
494
|
+
PINGREQ: 12,
|
|
495
|
+
PINGRESP: 13,
|
|
496
|
+
DISCONNECT: 14
|
|
497
|
+
};
|
|
498
|
+
var validate = function validate(obj, keys) {
|
|
499
|
+
for(var key in obj){
|
|
500
|
+
if (obj.hasOwnProperty(key)) {
|
|
501
|
+
if (keys.hasOwnProperty(key)) {
|
|
502
|
+
if (_type_of(obj[key]) !== keys[key]) throw new Error(format(ERROR.INVALID_TYPE, [
|
|
503
|
+
_type_of(obj[key]),
|
|
504
|
+
key
|
|
505
|
+
]));
|
|
506
|
+
} else {
|
|
507
|
+
var errorStr = "Unknown property, " + key + ". Valid properties are:";
|
|
508
|
+
for(var validKey in keys)if (keys.hasOwnProperty(validKey)) errorStr = errorStr + " " + validKey;
|
|
509
|
+
throw new Error(errorStr);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
var scope = function scope(f, scope2) {
|
|
515
|
+
return function() {
|
|
516
|
+
return f.apply(scope2, arguments);
|
|
517
|
+
};
|
|
518
|
+
};
|
|
519
|
+
var ERROR = {
|
|
520
|
+
OK: {
|
|
521
|
+
code: 0,
|
|
522
|
+
text: "AMQJSC0000I OK."
|
|
523
|
+
},
|
|
524
|
+
CONNECT_TIMEOUT: {
|
|
525
|
+
code: 1,
|
|
526
|
+
text: "AMQJSC0001E Connect timed out."
|
|
527
|
+
},
|
|
528
|
+
SUBSCRIBE_TIMEOUT: {
|
|
529
|
+
code: 2,
|
|
530
|
+
text: "AMQJS0002E Subscribe timed out."
|
|
531
|
+
},
|
|
532
|
+
UNSUBSCRIBE_TIMEOUT: {
|
|
533
|
+
code: 3,
|
|
534
|
+
text: "AMQJS0003E Unsubscribe timed out."
|
|
535
|
+
},
|
|
536
|
+
PING_TIMEOUT: {
|
|
537
|
+
code: 4,
|
|
538
|
+
text: "AMQJS0004E Ping timed out."
|
|
539
|
+
},
|
|
540
|
+
INTERNAL_ERROR: {
|
|
541
|
+
code: 5,
|
|
542
|
+
text: "AMQJS0005E Internal error. Error Message: {0}, Stack trace: {1}"
|
|
543
|
+
},
|
|
544
|
+
CONNACK_RETURNCODE: {
|
|
545
|
+
code: 6,
|
|
546
|
+
text: "AMQJS0006E Bad Connack return code:{0} {1}."
|
|
547
|
+
},
|
|
548
|
+
SOCKET_ERROR: {
|
|
549
|
+
code: 7,
|
|
550
|
+
text: "AMQJS0007E Socket error:{0}."
|
|
551
|
+
},
|
|
552
|
+
SOCKET_CLOSE: {
|
|
553
|
+
code: 8,
|
|
554
|
+
text: "AMQJS0008I Socket closed."
|
|
555
|
+
},
|
|
556
|
+
MALFORMED_UTF: {
|
|
557
|
+
code: 9,
|
|
558
|
+
text: "AMQJS0009E Malformed UTF data:{0} {1} {2}."
|
|
559
|
+
},
|
|
560
|
+
UNSUPPORTED: {
|
|
561
|
+
code: 10,
|
|
562
|
+
text: "AMQJS0010E {0} is not supported by this browser."
|
|
563
|
+
},
|
|
564
|
+
INVALID_STATE: {
|
|
565
|
+
code: 11,
|
|
566
|
+
text: "AMQJS0011E Invalid state {0}."
|
|
567
|
+
},
|
|
568
|
+
INVALID_TYPE: {
|
|
569
|
+
code: 12,
|
|
570
|
+
text: "AMQJS0012E Invalid type {0} for {1}."
|
|
571
|
+
},
|
|
572
|
+
INVALID_ARGUMENT: {
|
|
573
|
+
code: 13,
|
|
574
|
+
text: "AMQJS0013E Invalid argument {0} for {1}."
|
|
575
|
+
},
|
|
576
|
+
UNSUPPORTED_OPERATION: {
|
|
577
|
+
code: 14,
|
|
578
|
+
text: "AMQJS0014E Unsupported operation."
|
|
579
|
+
},
|
|
580
|
+
INVALID_STORED_DATA: {
|
|
581
|
+
code: 15,
|
|
582
|
+
text: "AMQJS0015E Invalid data in local storage key={0} value={1}."
|
|
583
|
+
},
|
|
584
|
+
INVALID_MQTT_MESSAGE_TYPE: {
|
|
585
|
+
code: 16,
|
|
586
|
+
text: "AMQJS0016E Invalid MQTT message type {0}."
|
|
587
|
+
},
|
|
588
|
+
MALFORMED_UNICODE: {
|
|
589
|
+
code: 17,
|
|
590
|
+
text: "AMQJS0017E Malformed Unicode string:{0} {1}."
|
|
591
|
+
},
|
|
592
|
+
BUFFER_FULL: {
|
|
593
|
+
code: 18,
|
|
594
|
+
text: "AMQJS0018E Message buffer is full, maximum buffer size: {0}."
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
var CONNACK_RC = {
|
|
598
|
+
0: "Connection Accepted",
|
|
599
|
+
1: "Connection Refused: unacceptable protocol version",
|
|
600
|
+
2: "Connection Refused: identifier rejected",
|
|
601
|
+
3: "Connection Refused: server unavailable",
|
|
602
|
+
4: "Connection Refused: bad user name or password",
|
|
603
|
+
5: "Connection Refused: not authorized"
|
|
604
|
+
};
|
|
605
|
+
var format = function format(error, substitutions) {
|
|
606
|
+
var text = error.text;
|
|
607
|
+
if (substitutions) {
|
|
608
|
+
var field, start;
|
|
609
|
+
for(var i = 0; i < substitutions.length; i++){
|
|
610
|
+
field = "{" + i + "}";
|
|
611
|
+
start = text.indexOf(field);
|
|
612
|
+
if (start > 0) {
|
|
613
|
+
var part1 = text.substring(0, start);
|
|
614
|
+
var part2 = text.substring(start + field.length);
|
|
615
|
+
text = part1 + substitutions[i] + part2;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return text;
|
|
620
|
+
};
|
|
621
|
+
var MqttProtoIdentifierv3 = [
|
|
622
|
+
0,
|
|
623
|
+
6,
|
|
624
|
+
77,
|
|
625
|
+
81,
|
|
626
|
+
73,
|
|
627
|
+
115,
|
|
628
|
+
100,
|
|
629
|
+
112,
|
|
630
|
+
3
|
|
631
|
+
];
|
|
632
|
+
var MqttProtoIdentifierv4 = [
|
|
633
|
+
0,
|
|
634
|
+
4,
|
|
635
|
+
77,
|
|
636
|
+
81,
|
|
637
|
+
84,
|
|
638
|
+
84,
|
|
639
|
+
4
|
|
640
|
+
];
|
|
641
|
+
var WireMessage = function WireMessage(type, options) {
|
|
642
|
+
this.type = type;
|
|
643
|
+
for(var name in options){
|
|
644
|
+
if (options.hasOwnProperty(name)) {
|
|
645
|
+
this[name] = options[name];
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
WireMessage.prototype.encode = function() {
|
|
650
|
+
var first = (this.type & 15) << 4;
|
|
651
|
+
var remLength = 0;
|
|
652
|
+
var topicStrLength = [];
|
|
653
|
+
var destinationNameLength = 0;
|
|
654
|
+
var willMessagePayloadBytes;
|
|
655
|
+
if (this.messageIdentifier !== void 0) remLength += 2;
|
|
656
|
+
switch(this.type){
|
|
657
|
+
// If this a Connect then we need to include 12 bytes for its header
|
|
658
|
+
case MESSAGE_TYPE.CONNECT:
|
|
659
|
+
switch(this.mqttVersion){
|
|
660
|
+
case 3:
|
|
661
|
+
remLength += MqttProtoIdentifierv3.length + 3;
|
|
662
|
+
break;
|
|
663
|
+
case 4:
|
|
664
|
+
remLength += MqttProtoIdentifierv4.length + 3;
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
remLength += UTF8Length(this.clientId) + 2;
|
|
668
|
+
if (this.willMessage !== void 0) {
|
|
669
|
+
remLength += UTF8Length(this.willMessage.destinationName) + 2;
|
|
670
|
+
willMessagePayloadBytes = this.willMessage.payloadBytes;
|
|
671
|
+
if (!_instanceof(willMessagePayloadBytes, Uint8Array)) willMessagePayloadBytes = new Uint8Array(payloadBytes);
|
|
672
|
+
remLength += willMessagePayloadBytes.byteLength + 2;
|
|
673
|
+
}
|
|
674
|
+
if (this.userName !== void 0) remLength += UTF8Length(this.userName) + 2;
|
|
675
|
+
if (this.password !== void 0) remLength += UTF8Length(this.password) + 2;
|
|
676
|
+
break;
|
|
677
|
+
// Subscribe, Unsubscribe can both contain topic strings
|
|
678
|
+
case MESSAGE_TYPE.SUBSCRIBE:
|
|
679
|
+
first |= 2;
|
|
680
|
+
for(var i = 0; i < this.topics.length; i++){
|
|
681
|
+
topicStrLength[i] = UTF8Length(this.topics[i]);
|
|
682
|
+
remLength += topicStrLength[i] + 2;
|
|
683
|
+
}
|
|
684
|
+
remLength += this.requestedQos.length;
|
|
685
|
+
break;
|
|
686
|
+
case MESSAGE_TYPE.UNSUBSCRIBE:
|
|
687
|
+
first |= 2;
|
|
688
|
+
for(var i = 0; i < this.topics.length; i++){
|
|
689
|
+
topicStrLength[i] = UTF8Length(this.topics[i]);
|
|
690
|
+
remLength += topicStrLength[i] + 2;
|
|
691
|
+
}
|
|
692
|
+
break;
|
|
693
|
+
case MESSAGE_TYPE.PUBREL:
|
|
694
|
+
first |= 2;
|
|
695
|
+
break;
|
|
696
|
+
case MESSAGE_TYPE.PUBLISH:
|
|
697
|
+
if (this.payloadMessage.duplicate) first |= 8;
|
|
698
|
+
first = first |= this.payloadMessage.qos << 1;
|
|
699
|
+
if (this.payloadMessage.retained) first |= 1;
|
|
700
|
+
destinationNameLength = UTF8Length(this.payloadMessage.destinationName);
|
|
701
|
+
remLength += destinationNameLength + 2;
|
|
702
|
+
var payloadBytes = this.payloadMessage.payloadBytes;
|
|
703
|
+
remLength += payloadBytes.byteLength;
|
|
704
|
+
if (_instanceof(payloadBytes, ArrayBuffer)) payloadBytes = new Uint8Array(payloadBytes);
|
|
705
|
+
else if (!_instanceof(payloadBytes, Uint8Array)) payloadBytes = new Uint8Array(payloadBytes.buffer);
|
|
706
|
+
break;
|
|
707
|
+
case MESSAGE_TYPE.DISCONNECT:
|
|
708
|
+
break;
|
|
709
|
+
default:
|
|
710
|
+
break;
|
|
711
|
+
}
|
|
712
|
+
var mbi = encodeMBI(remLength);
|
|
713
|
+
var pos = mbi.length + 1;
|
|
714
|
+
var buffer = new ArrayBuffer(remLength + pos);
|
|
715
|
+
var byteStream = new Uint8Array(buffer);
|
|
716
|
+
byteStream[0] = first;
|
|
717
|
+
byteStream.set(mbi, 1);
|
|
718
|
+
if (this.type == MESSAGE_TYPE.PUBLISH) pos = writeString(this.payloadMessage.destinationName, destinationNameLength, byteStream, pos);
|
|
719
|
+
else if (this.type == MESSAGE_TYPE.CONNECT) {
|
|
720
|
+
switch(this.mqttVersion){
|
|
721
|
+
case 3:
|
|
722
|
+
byteStream.set(MqttProtoIdentifierv3, pos);
|
|
723
|
+
pos += MqttProtoIdentifierv3.length;
|
|
724
|
+
break;
|
|
725
|
+
case 4:
|
|
726
|
+
byteStream.set(MqttProtoIdentifierv4, pos);
|
|
727
|
+
pos += MqttProtoIdentifierv4.length;
|
|
728
|
+
break;
|
|
729
|
+
}
|
|
730
|
+
var connectFlags = 0;
|
|
731
|
+
if (this.cleanSession) connectFlags = 2;
|
|
732
|
+
if (this.willMessage !== void 0) {
|
|
733
|
+
connectFlags |= 4;
|
|
734
|
+
connectFlags |= this.willMessage.qos << 3;
|
|
735
|
+
if (this.willMessage.retained) {
|
|
736
|
+
connectFlags |= 32;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (this.userName !== void 0) connectFlags |= 128;
|
|
740
|
+
if (this.password !== void 0) connectFlags |= 64;
|
|
741
|
+
byteStream[pos++] = connectFlags;
|
|
742
|
+
pos = writeUint16(this.keepAliveInterval, byteStream, pos);
|
|
743
|
+
}
|
|
744
|
+
if (this.messageIdentifier !== void 0) pos = writeUint16(this.messageIdentifier, byteStream, pos);
|
|
745
|
+
switch(this.type){
|
|
746
|
+
case MESSAGE_TYPE.CONNECT:
|
|
747
|
+
pos = writeString(this.clientId, UTF8Length(this.clientId), byteStream, pos);
|
|
748
|
+
if (this.willMessage !== void 0) {
|
|
749
|
+
pos = writeString(this.willMessage.destinationName, UTF8Length(this.willMessage.destinationName), byteStream, pos);
|
|
750
|
+
pos = writeUint16(willMessagePayloadBytes.byteLength, byteStream, pos);
|
|
751
|
+
byteStream.set(willMessagePayloadBytes, pos);
|
|
752
|
+
pos += willMessagePayloadBytes.byteLength;
|
|
753
|
+
}
|
|
754
|
+
if (this.userName !== void 0) pos = writeString(this.userName, UTF8Length(this.userName), byteStream, pos);
|
|
755
|
+
if (this.password !== void 0) pos = writeString(this.password, UTF8Length(this.password), byteStream, pos);
|
|
756
|
+
break;
|
|
757
|
+
case MESSAGE_TYPE.PUBLISH:
|
|
758
|
+
byteStream.set(payloadBytes, pos);
|
|
759
|
+
break;
|
|
760
|
+
// case MESSAGE_TYPE.PUBREC:
|
|
761
|
+
// case MESSAGE_TYPE.PUBREL:
|
|
762
|
+
// case MESSAGE_TYPE.PUBCOMP:
|
|
763
|
+
// break;
|
|
764
|
+
case MESSAGE_TYPE.SUBSCRIBE:
|
|
765
|
+
for(var i = 0; i < this.topics.length; i++){
|
|
766
|
+
pos = writeString(this.topics[i], topicStrLength[i], byteStream, pos);
|
|
767
|
+
byteStream[pos++] = this.requestedQos[i];
|
|
768
|
+
}
|
|
769
|
+
break;
|
|
770
|
+
case MESSAGE_TYPE.UNSUBSCRIBE:
|
|
771
|
+
for(var i = 0; i < this.topics.length; i++)pos = writeString(this.topics[i], topicStrLength[i], byteStream, pos);
|
|
772
|
+
break;
|
|
773
|
+
default:
|
|
774
|
+
}
|
|
775
|
+
return buffer;
|
|
776
|
+
};
|
|
777
|
+
function decodeMessage(input, pos) {
|
|
778
|
+
var startingPos = pos;
|
|
779
|
+
var first = input[pos];
|
|
780
|
+
var type = first >> 4;
|
|
781
|
+
var messageInfo = first &= 15;
|
|
782
|
+
pos += 1;
|
|
783
|
+
var digit;
|
|
784
|
+
var remLength = 0;
|
|
785
|
+
var multiplier = 1;
|
|
786
|
+
do {
|
|
787
|
+
if (pos == input.length) {
|
|
788
|
+
return [
|
|
789
|
+
null,
|
|
790
|
+
startingPos
|
|
791
|
+
];
|
|
792
|
+
}
|
|
793
|
+
digit = input[pos++];
|
|
794
|
+
remLength += (digit & 127) * multiplier;
|
|
795
|
+
multiplier *= 128;
|
|
796
|
+
}while ((digit & 128) !== 0);
|
|
797
|
+
var endPos = pos + remLength;
|
|
798
|
+
if (endPos > input.length) {
|
|
799
|
+
return [
|
|
800
|
+
null,
|
|
801
|
+
startingPos
|
|
802
|
+
];
|
|
803
|
+
}
|
|
804
|
+
var wireMessage = new WireMessage(type);
|
|
805
|
+
switch(type){
|
|
806
|
+
case MESSAGE_TYPE.CONNACK:
|
|
807
|
+
var connectAcknowledgeFlags = input[pos++];
|
|
808
|
+
if (connectAcknowledgeFlags & 1) wireMessage.sessionPresent = true;
|
|
809
|
+
wireMessage.returnCode = input[pos++];
|
|
810
|
+
break;
|
|
811
|
+
case MESSAGE_TYPE.PUBLISH:
|
|
812
|
+
var qos = messageInfo >> 1 & 3;
|
|
813
|
+
var len = readUint16(input, pos);
|
|
814
|
+
pos += 2;
|
|
815
|
+
var topicName = parseUTF8(input, pos, len);
|
|
816
|
+
pos += len;
|
|
817
|
+
if (qos > 0) {
|
|
818
|
+
wireMessage.messageIdentifier = readUint16(input, pos);
|
|
819
|
+
pos += 2;
|
|
820
|
+
}
|
|
821
|
+
var message = new PahoMQTT.Message(input.subarray(pos, endPos));
|
|
822
|
+
if ((messageInfo & 1) == 1) message.retained = true;
|
|
823
|
+
if ((messageInfo & 8) == 8) message.duplicate = true;
|
|
824
|
+
message.qos = qos;
|
|
825
|
+
message.destinationName = topicName;
|
|
826
|
+
wireMessage.payloadMessage = message;
|
|
827
|
+
break;
|
|
828
|
+
case MESSAGE_TYPE.PUBACK:
|
|
829
|
+
case MESSAGE_TYPE.PUBREC:
|
|
830
|
+
case MESSAGE_TYPE.PUBREL:
|
|
831
|
+
case MESSAGE_TYPE.PUBCOMP:
|
|
832
|
+
case MESSAGE_TYPE.UNSUBACK:
|
|
833
|
+
wireMessage.messageIdentifier = readUint16(input, pos);
|
|
834
|
+
break;
|
|
835
|
+
case MESSAGE_TYPE.SUBACK:
|
|
836
|
+
wireMessage.messageIdentifier = readUint16(input, pos);
|
|
837
|
+
pos += 2;
|
|
838
|
+
wireMessage.returnCode = input.subarray(pos, endPos);
|
|
839
|
+
break;
|
|
840
|
+
default:
|
|
841
|
+
break;
|
|
842
|
+
}
|
|
843
|
+
return [
|
|
844
|
+
wireMessage,
|
|
845
|
+
endPos
|
|
846
|
+
];
|
|
847
|
+
}
|
|
848
|
+
function writeUint16(input, buffer, offset) {
|
|
849
|
+
buffer[offset++] = input >> 8;
|
|
850
|
+
buffer[offset++] = input % 256;
|
|
851
|
+
return offset;
|
|
852
|
+
}
|
|
853
|
+
function writeString(input, utf8Length, buffer, offset) {
|
|
854
|
+
offset = writeUint16(utf8Length, buffer, offset);
|
|
855
|
+
stringToUTF8(input, buffer, offset);
|
|
856
|
+
return offset + utf8Length;
|
|
857
|
+
}
|
|
858
|
+
function readUint16(buffer, offset) {
|
|
859
|
+
return 256 * buffer[offset] + buffer[offset + 1];
|
|
860
|
+
}
|
|
861
|
+
function encodeMBI(number) {
|
|
862
|
+
var output = new Array(1);
|
|
863
|
+
var numBytes = 0;
|
|
864
|
+
do {
|
|
865
|
+
var digit = number % 128;
|
|
866
|
+
number = number >> 7;
|
|
867
|
+
if (number > 0) {
|
|
868
|
+
digit |= 128;
|
|
869
|
+
}
|
|
870
|
+
output[numBytes++] = digit;
|
|
871
|
+
}while (number > 0 && numBytes < 4);
|
|
872
|
+
return output;
|
|
873
|
+
}
|
|
874
|
+
function UTF8Length(input) {
|
|
875
|
+
var output = 0;
|
|
876
|
+
for(var i = 0; i < input.length; i++){
|
|
877
|
+
var charCode = input.charCodeAt(i);
|
|
878
|
+
if (charCode > 2047) {
|
|
879
|
+
if (55296 <= charCode && charCode <= 56319) {
|
|
880
|
+
i++;
|
|
881
|
+
output++;
|
|
882
|
+
}
|
|
883
|
+
output += 3;
|
|
884
|
+
} else if (charCode > 127) output += 2;
|
|
885
|
+
else output++;
|
|
886
|
+
}
|
|
887
|
+
return output;
|
|
888
|
+
}
|
|
889
|
+
function stringToUTF8(input, output, start) {
|
|
890
|
+
var pos = start;
|
|
891
|
+
for(var i = 0; i < input.length; i++){
|
|
892
|
+
var charCode = input.charCodeAt(i);
|
|
893
|
+
if (55296 <= charCode && charCode <= 56319) {
|
|
894
|
+
var lowCharCode = input.charCodeAt(++i);
|
|
895
|
+
if (isNaN(lowCharCode)) {
|
|
896
|
+
throw new Error(format(ERROR.MALFORMED_UNICODE, [
|
|
897
|
+
charCode,
|
|
898
|
+
lowCharCode
|
|
899
|
+
]));
|
|
900
|
+
}
|
|
901
|
+
charCode = (charCode - 55296 << 10) + (lowCharCode - 56320) + 65536;
|
|
902
|
+
}
|
|
903
|
+
if (charCode <= 127) {
|
|
904
|
+
output[pos++] = charCode;
|
|
905
|
+
} else if (charCode <= 2047) {
|
|
906
|
+
output[pos++] = charCode >> 6 & 31 | 192;
|
|
907
|
+
output[pos++] = charCode & 63 | 128;
|
|
908
|
+
} else if (charCode <= 65535) {
|
|
909
|
+
output[pos++] = charCode >> 12 & 15 | 224;
|
|
910
|
+
output[pos++] = charCode >> 6 & 63 | 128;
|
|
911
|
+
output[pos++] = charCode & 63 | 128;
|
|
912
|
+
} else {
|
|
913
|
+
output[pos++] = charCode >> 18 & 7 | 240;
|
|
914
|
+
output[pos++] = charCode >> 12 & 63 | 128;
|
|
915
|
+
output[pos++] = charCode >> 6 & 63 | 128;
|
|
916
|
+
output[pos++] = charCode & 63 | 128;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
return output;
|
|
920
|
+
}
|
|
921
|
+
function parseUTF8(input, offset, length) {
|
|
922
|
+
var output = "";
|
|
923
|
+
var utf16;
|
|
924
|
+
var pos = offset;
|
|
925
|
+
while(pos < offset + length){
|
|
926
|
+
var byte1 = input[pos++];
|
|
927
|
+
if (byte1 < 128) utf16 = byte1;
|
|
928
|
+
else {
|
|
929
|
+
var byte2 = input[pos++] - 128;
|
|
930
|
+
if (byte2 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [
|
|
931
|
+
byte1.toString(16),
|
|
932
|
+
byte2.toString(16),
|
|
933
|
+
""
|
|
934
|
+
]));
|
|
935
|
+
if (byte1 < 224) utf16 = 64 * (byte1 - 192) + byte2;
|
|
936
|
+
else {
|
|
937
|
+
var byte3 = input[pos++] - 128;
|
|
938
|
+
if (byte3 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [
|
|
939
|
+
byte1.toString(16),
|
|
940
|
+
byte2.toString(16),
|
|
941
|
+
byte3.toString(16)
|
|
942
|
+
]));
|
|
943
|
+
if (byte1 < 240) utf16 = 4096 * (byte1 - 224) + 64 * byte2 + byte3;
|
|
944
|
+
else {
|
|
945
|
+
var byte4 = input[pos++] - 128;
|
|
946
|
+
if (byte4 < 0) throw new Error(format(ERROR.MALFORMED_UTF, [
|
|
947
|
+
byte1.toString(16),
|
|
948
|
+
byte2.toString(16),
|
|
949
|
+
byte3.toString(16),
|
|
950
|
+
byte4.toString(16)
|
|
951
|
+
]));
|
|
952
|
+
if (byte1 < 248) utf16 = 262144 * (byte1 - 240) + 4096 * byte2 + 64 * byte3 + byte4;
|
|
953
|
+
else throw new Error(format(ERROR.MALFORMED_UTF, [
|
|
954
|
+
byte1.toString(16),
|
|
955
|
+
byte2.toString(16),
|
|
956
|
+
byte3.toString(16),
|
|
957
|
+
byte4.toString(16)
|
|
958
|
+
]));
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
if (utf16 > 65535) {
|
|
963
|
+
utf16 -= 65536;
|
|
964
|
+
output += String.fromCharCode(55296 + (utf16 >> 10));
|
|
965
|
+
utf16 = 56320 + (utf16 & 1023);
|
|
966
|
+
}
|
|
967
|
+
output += String.fromCharCode(utf16);
|
|
968
|
+
}
|
|
969
|
+
return output;
|
|
970
|
+
}
|
|
971
|
+
var Pinger = function Pinger(client, keepAliveInterval) {
|
|
972
|
+
this._client = client;
|
|
973
|
+
this._keepAliveInterval = keepAliveInterval * 1e3;
|
|
974
|
+
this.isReset = false;
|
|
975
|
+
var pingReq = new WireMessage(MESSAGE_TYPE.PINGREQ).encode();
|
|
976
|
+
var doTimeout = function doTimeout(pinger) {
|
|
977
|
+
return function() {
|
|
978
|
+
return doPing.apply(pinger);
|
|
979
|
+
};
|
|
980
|
+
};
|
|
981
|
+
var doPing = function doPing() {
|
|
982
|
+
if (!this.isReset) {
|
|
983
|
+
this._client._trace("Pinger.doPing", "Timed out");
|
|
984
|
+
this._client._disconnected(ERROR.PING_TIMEOUT.code, format(ERROR.PING_TIMEOUT));
|
|
985
|
+
} else {
|
|
986
|
+
this.isReset = false;
|
|
987
|
+
this._client._trace("Pinger.doPing", "send PINGREQ");
|
|
988
|
+
this._client.socket.send(pingReq);
|
|
989
|
+
this.timeout = setTimeout(doTimeout(this), this._keepAliveInterval);
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
this.reset = function() {
|
|
993
|
+
this.isReset = true;
|
|
994
|
+
clearTimeout(this.timeout);
|
|
995
|
+
if (this._keepAliveInterval > 0) this.timeout = setTimeout(doTimeout(this), this._keepAliveInterval);
|
|
996
|
+
};
|
|
997
|
+
this.cancel = function() {
|
|
998
|
+
clearTimeout(this.timeout);
|
|
999
|
+
};
|
|
1000
|
+
};
|
|
1001
|
+
var Timeout = function Timeout(client, timeoutSeconds, action, args) {
|
|
1002
|
+
if (!timeoutSeconds) timeoutSeconds = 30;
|
|
1003
|
+
var doTimeout = function doTimeout(action2, client2, args2) {
|
|
1004
|
+
return function() {
|
|
1005
|
+
return action2.apply(client2, args2);
|
|
1006
|
+
};
|
|
1007
|
+
};
|
|
1008
|
+
this.timeout = setTimeout(doTimeout(action, client, args), timeoutSeconds * 1e3);
|
|
1009
|
+
this.cancel = function() {
|
|
1010
|
+
clearTimeout(this.timeout);
|
|
1011
|
+
};
|
|
1012
|
+
};
|
|
1013
|
+
var ClientImpl = function ClientImpl(uri, host, port, path, clientId) {
|
|
1014
|
+
if (!("WebSocket" in global2 && global2.WebSocket !== null)) {
|
|
1015
|
+
throw new Error(format(ERROR.UNSUPPORTED, [
|
|
1016
|
+
"WebSocket"
|
|
1017
|
+
]));
|
|
1018
|
+
}
|
|
1019
|
+
if (!("ArrayBuffer" in global2 && global2.ArrayBuffer !== null)) {
|
|
1020
|
+
throw new Error(format(ERROR.UNSUPPORTED, [
|
|
1021
|
+
"ArrayBuffer"
|
|
1022
|
+
]));
|
|
1023
|
+
}
|
|
1024
|
+
this._trace("Paho.Client", uri, host, port, path, clientId);
|
|
1025
|
+
this.host = host;
|
|
1026
|
+
this.port = port;
|
|
1027
|
+
this.path = path;
|
|
1028
|
+
this.uri = uri;
|
|
1029
|
+
this.clientId = clientId;
|
|
1030
|
+
this._wsuri = null;
|
|
1031
|
+
this._localKey = host + ":" + port + (path != "/mqtt" ? ":" + path : "") + ":" + clientId + ":";
|
|
1032
|
+
this._msg_queue = [];
|
|
1033
|
+
this._buffered_msg_queue = [];
|
|
1034
|
+
this._sentMessages = {};
|
|
1035
|
+
this._receivedMessages = {};
|
|
1036
|
+
this._notify_msg_sent = {};
|
|
1037
|
+
this._message_identifier = 1;
|
|
1038
|
+
this._sequence = 0;
|
|
1039
|
+
for(var key in localStorage)if (key.indexOf("Sent:" + this._localKey) === 0 || key.indexOf("Received:" + this._localKey) === 0) this.restore(key);
|
|
1040
|
+
};
|
|
1041
|
+
ClientImpl.prototype.host = null;
|
|
1042
|
+
ClientImpl.prototype.port = null;
|
|
1043
|
+
ClientImpl.prototype.path = null;
|
|
1044
|
+
ClientImpl.prototype.uri = null;
|
|
1045
|
+
ClientImpl.prototype.clientId = null;
|
|
1046
|
+
ClientImpl.prototype.socket = null;
|
|
1047
|
+
ClientImpl.prototype.connected = false;
|
|
1048
|
+
ClientImpl.prototype.maxMessageIdentifier = 65536;
|
|
1049
|
+
ClientImpl.prototype.connectOptions = null;
|
|
1050
|
+
ClientImpl.prototype.hostIndex = null;
|
|
1051
|
+
ClientImpl.prototype.onConnected = null;
|
|
1052
|
+
ClientImpl.prototype.onConnectionLost = null;
|
|
1053
|
+
ClientImpl.prototype.onMessageDelivered = null;
|
|
1054
|
+
ClientImpl.prototype.onMessageArrived = null;
|
|
1055
|
+
ClientImpl.prototype.traceFunction = null;
|
|
1056
|
+
ClientImpl.prototype._msg_queue = null;
|
|
1057
|
+
ClientImpl.prototype._buffered_msg_queue = null;
|
|
1058
|
+
ClientImpl.prototype._connectTimeout = null;
|
|
1059
|
+
ClientImpl.prototype.sendPinger = null;
|
|
1060
|
+
ClientImpl.prototype.receivePinger = null;
|
|
1061
|
+
ClientImpl.prototype._reconnectInterval = 1;
|
|
1062
|
+
ClientImpl.prototype._reconnecting = false;
|
|
1063
|
+
ClientImpl.prototype._reconnectTimeout = null;
|
|
1064
|
+
ClientImpl.prototype.disconnectedPublishing = false;
|
|
1065
|
+
ClientImpl.prototype.disconnectedBufferSize = 5e3;
|
|
1066
|
+
ClientImpl.prototype.receiveBuffer = null;
|
|
1067
|
+
ClientImpl.prototype._traceBuffer = null;
|
|
1068
|
+
ClientImpl.prototype._MAX_TRACE_ENTRIES = 100;
|
|
1069
|
+
ClientImpl.prototype.connect = function(connectOptions) {
|
|
1070
|
+
var connectOptionsMasked = this._traceMask(connectOptions, "password");
|
|
1071
|
+
this._trace("Client.connect", connectOptionsMasked, this.socket, this.connected);
|
|
1072
|
+
if (this.connected) throw new Error(format(ERROR.INVALID_STATE, [
|
|
1073
|
+
"already connected"
|
|
1074
|
+
]));
|
|
1075
|
+
if (this.socket) throw new Error(format(ERROR.INVALID_STATE, [
|
|
1076
|
+
"already connected"
|
|
1077
|
+
]));
|
|
1078
|
+
if (this._reconnecting) {
|
|
1079
|
+
this._reconnectTimeout.cancel();
|
|
1080
|
+
this._reconnectTimeout = null;
|
|
1081
|
+
this._reconnecting = false;
|
|
1082
|
+
}
|
|
1083
|
+
this.connectOptions = connectOptions;
|
|
1084
|
+
this._reconnectInterval = 1;
|
|
1085
|
+
this._reconnecting = false;
|
|
1086
|
+
if (connectOptions.uris) {
|
|
1087
|
+
this.hostIndex = 0;
|
|
1088
|
+
this._doConnect(connectOptions.uris[0]);
|
|
1089
|
+
} else {
|
|
1090
|
+
this._doConnect(this.uri);
|
|
1091
|
+
}
|
|
1092
|
+
};
|
|
1093
|
+
ClientImpl.prototype.subscribe = function(filter3, subscribeOptions) {
|
|
1094
|
+
this._trace("Client.subscribe", filter3, subscribeOptions);
|
|
1095
|
+
if (!this.connected) throw new Error(format(ERROR.INVALID_STATE, [
|
|
1096
|
+
"not connected"
|
|
1097
|
+
]));
|
|
1098
|
+
var wireMessage = new WireMessage(MESSAGE_TYPE.SUBSCRIBE);
|
|
1099
|
+
wireMessage.topics = [
|
|
1100
|
+
filter3
|
|
1101
|
+
];
|
|
1102
|
+
if (subscribeOptions.qos !== void 0) wireMessage.requestedQos = [
|
|
1103
|
+
subscribeOptions.qos
|
|
1104
|
+
];
|
|
1105
|
+
else wireMessage.requestedQos = [
|
|
1106
|
+
0
|
|
1107
|
+
];
|
|
1108
|
+
if (subscribeOptions.onSuccess) {
|
|
1109
|
+
wireMessage.onSuccess = function(grantedQos) {
|
|
1110
|
+
subscribeOptions.onSuccess({
|
|
1111
|
+
invocationContext: subscribeOptions.invocationContext,
|
|
1112
|
+
grantedQos: grantedQos
|
|
1113
|
+
});
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
if (subscribeOptions.onFailure) {
|
|
1117
|
+
wireMessage.onFailure = function(errorCode) {
|
|
1118
|
+
subscribeOptions.onFailure({
|
|
1119
|
+
invocationContext: subscribeOptions.invocationContext,
|
|
1120
|
+
errorCode: errorCode,
|
|
1121
|
+
errorMessage: format(errorCode)
|
|
1122
|
+
});
|
|
1123
|
+
};
|
|
1124
|
+
}
|
|
1125
|
+
if (subscribeOptions.timeout) {
|
|
1126
|
+
wireMessage.timeOut = new Timeout(this, subscribeOptions.timeout, subscribeOptions.onFailure, [
|
|
1127
|
+
{
|
|
1128
|
+
invocationContext: subscribeOptions.invocationContext,
|
|
1129
|
+
errorCode: ERROR.SUBSCRIBE_TIMEOUT.code,
|
|
1130
|
+
errorMessage: format(ERROR.SUBSCRIBE_TIMEOUT)
|
|
1131
|
+
}
|
|
1132
|
+
]);
|
|
1133
|
+
}
|
|
1134
|
+
this._requires_ack(wireMessage);
|
|
1135
|
+
this._schedule_message(wireMessage);
|
|
1136
|
+
};
|
|
1137
|
+
ClientImpl.prototype.unsubscribe = function(filter3, unsubscribeOptions) {
|
|
1138
|
+
this._trace("Client.unsubscribe", filter3, unsubscribeOptions);
|
|
1139
|
+
if (!this.connected) throw new Error(format(ERROR.INVALID_STATE, [
|
|
1140
|
+
"not connected"
|
|
1141
|
+
]));
|
|
1142
|
+
var wireMessage = new WireMessage(MESSAGE_TYPE.UNSUBSCRIBE);
|
|
1143
|
+
wireMessage.topics = [
|
|
1144
|
+
filter3
|
|
1145
|
+
];
|
|
1146
|
+
if (unsubscribeOptions.onSuccess) {
|
|
1147
|
+
wireMessage.callback = function() {
|
|
1148
|
+
unsubscribeOptions.onSuccess({
|
|
1149
|
+
invocationContext: unsubscribeOptions.invocationContext
|
|
1150
|
+
});
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
if (unsubscribeOptions.timeout) {
|
|
1154
|
+
wireMessage.timeOut = new Timeout(this, unsubscribeOptions.timeout, unsubscribeOptions.onFailure, [
|
|
1155
|
+
{
|
|
1156
|
+
invocationContext: unsubscribeOptions.invocationContext,
|
|
1157
|
+
errorCode: ERROR.UNSUBSCRIBE_TIMEOUT.code,
|
|
1158
|
+
errorMessage: format(ERROR.UNSUBSCRIBE_TIMEOUT)
|
|
1159
|
+
}
|
|
1160
|
+
]);
|
|
1161
|
+
}
|
|
1162
|
+
this._requires_ack(wireMessage);
|
|
1163
|
+
this._schedule_message(wireMessage);
|
|
1164
|
+
};
|
|
1165
|
+
ClientImpl.prototype.send = function(message) {
|
|
1166
|
+
this._trace("Client.send", message);
|
|
1167
|
+
var wireMessage = new WireMessage(MESSAGE_TYPE.PUBLISH);
|
|
1168
|
+
wireMessage.payloadMessage = message;
|
|
1169
|
+
if (this.connected) {
|
|
1170
|
+
if (message.qos > 0) {
|
|
1171
|
+
this._requires_ack(wireMessage);
|
|
1172
|
+
} else if (this.onMessageDelivered) {
|
|
1173
|
+
this._notify_msg_sent[wireMessage] = this.onMessageDelivered(wireMessage.payloadMessage);
|
|
1174
|
+
}
|
|
1175
|
+
this._schedule_message(wireMessage);
|
|
1176
|
+
} else {
|
|
1177
|
+
if (this._reconnecting && this.disconnectedPublishing) {
|
|
1178
|
+
var messageCount = Object.keys(this._sentMessages).length + this._buffered_msg_queue.length;
|
|
1179
|
+
if (messageCount > this.disconnectedBufferSize) {
|
|
1180
|
+
throw new Error(format(ERROR.BUFFER_FULL, [
|
|
1181
|
+
this.disconnectedBufferSize
|
|
1182
|
+
]));
|
|
1183
|
+
} else {
|
|
1184
|
+
if (message.qos > 0) {
|
|
1185
|
+
this._requires_ack(wireMessage);
|
|
1186
|
+
} else {
|
|
1187
|
+
wireMessage.sequence = ++this._sequence;
|
|
1188
|
+
this._buffered_msg_queue.unshift(wireMessage);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
} else {
|
|
1192
|
+
throw new Error(format(ERROR.INVALID_STATE, [
|
|
1193
|
+
"not connected"
|
|
1194
|
+
]));
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
};
|
|
1198
|
+
ClientImpl.prototype.disconnect = function() {
|
|
1199
|
+
this._trace("Client.disconnect");
|
|
1200
|
+
if (this._reconnecting) {
|
|
1201
|
+
this._reconnectTimeout.cancel();
|
|
1202
|
+
this._reconnectTimeout = null;
|
|
1203
|
+
this._reconnecting = false;
|
|
1204
|
+
}
|
|
1205
|
+
if (!this.socket) throw new Error(format(ERROR.INVALID_STATE, [
|
|
1206
|
+
"not connecting or connected"
|
|
1207
|
+
]));
|
|
1208
|
+
var wireMessage = new WireMessage(MESSAGE_TYPE.DISCONNECT);
|
|
1209
|
+
this._notify_msg_sent[wireMessage] = scope(this._disconnected, this);
|
|
1210
|
+
this._schedule_message(wireMessage);
|
|
1211
|
+
};
|
|
1212
|
+
ClientImpl.prototype.getTraceLog = function() {
|
|
1213
|
+
if (this._traceBuffer !== null) {
|
|
1214
|
+
this._trace("Client.getTraceLog", /* @__PURE__ */ new Date());
|
|
1215
|
+
this._trace("Client.getTraceLog in flight messages", this._sentMessages.length);
|
|
1216
|
+
for(var key in this._sentMessages)this._trace("_sentMessages ", key, this._sentMessages[key]);
|
|
1217
|
+
for(var key in this._receivedMessages)this._trace("_receivedMessages ", key, this._receivedMessages[key]);
|
|
1218
|
+
return this._traceBuffer;
|
|
1219
|
+
}
|
|
1220
|
+
};
|
|
1221
|
+
ClientImpl.prototype.startTrace = function() {
|
|
1222
|
+
if (this._traceBuffer === null) {
|
|
1223
|
+
this._traceBuffer = [];
|
|
1224
|
+
}
|
|
1225
|
+
this._trace("Client.startTrace", /* @__PURE__ */ new Date(), version);
|
|
1226
|
+
};
|
|
1227
|
+
ClientImpl.prototype.stopTrace = function() {
|
|
1228
|
+
delete this._traceBuffer;
|
|
1229
|
+
};
|
|
1230
|
+
ClientImpl.prototype._doConnect = function(wsurl) {
|
|
1231
|
+
if (this.connectOptions.useSSL) {
|
|
1232
|
+
var uriParts = wsurl.split(":");
|
|
1233
|
+
uriParts[0] = "wss";
|
|
1234
|
+
wsurl = uriParts.join(":");
|
|
1235
|
+
}
|
|
1236
|
+
this._wsuri = wsurl;
|
|
1237
|
+
this.connected = false;
|
|
1238
|
+
if (this.connectOptions.mqttVersion < 4) {
|
|
1239
|
+
this.socket = new WebSocket(wsurl, [
|
|
1240
|
+
"mqttv3.1"
|
|
1241
|
+
]);
|
|
1242
|
+
} else {
|
|
1243
|
+
this.socket = new WebSocket(wsurl, [
|
|
1244
|
+
"mqtt"
|
|
1245
|
+
]);
|
|
1246
|
+
}
|
|
1247
|
+
this.socket.binaryType = "arraybuffer";
|
|
1248
|
+
this.socket.onopen = scope(this._on_socket_open, this);
|
|
1249
|
+
this.socket.onmessage = scope(this._on_socket_message, this);
|
|
1250
|
+
this.socket.onerror = scope(this._on_socket_error, this);
|
|
1251
|
+
this.socket.onclose = scope(this._on_socket_close, this);
|
|
1252
|
+
this.sendPinger = new Pinger(this, this.connectOptions.keepAliveInterval);
|
|
1253
|
+
this.receivePinger = new Pinger(this, this.connectOptions.keepAliveInterval);
|
|
1254
|
+
if (this._connectTimeout) {
|
|
1255
|
+
this._connectTimeout.cancel();
|
|
1256
|
+
this._connectTimeout = null;
|
|
1257
|
+
}
|
|
1258
|
+
this._connectTimeout = new Timeout(this, this.connectOptions.timeout, this._disconnected, [
|
|
1259
|
+
ERROR.CONNECT_TIMEOUT.code,
|
|
1260
|
+
format(ERROR.CONNECT_TIMEOUT)
|
|
1261
|
+
]);
|
|
1262
|
+
};
|
|
1263
|
+
ClientImpl.prototype._schedule_message = function(message) {
|
|
1264
|
+
this._msg_queue.unshift(message);
|
|
1265
|
+
if (this.connected) {
|
|
1266
|
+
this._process_queue();
|
|
1267
|
+
}
|
|
1268
|
+
};
|
|
1269
|
+
ClientImpl.prototype.store = function(prefix, wireMessage) {
|
|
1270
|
+
var storedMessage = {
|
|
1271
|
+
type: wireMessage.type,
|
|
1272
|
+
messageIdentifier: wireMessage.messageIdentifier,
|
|
1273
|
+
version: 1
|
|
1274
|
+
};
|
|
1275
|
+
switch(wireMessage.type){
|
|
1276
|
+
case MESSAGE_TYPE.PUBLISH:
|
|
1277
|
+
if (wireMessage.pubRecReceived) storedMessage.pubRecReceived = true;
|
|
1278
|
+
storedMessage.payloadMessage = {};
|
|
1279
|
+
var hex = "";
|
|
1280
|
+
var messageBytes = wireMessage.payloadMessage.payloadBytes;
|
|
1281
|
+
for(var i = 0; i < messageBytes.length; i++){
|
|
1282
|
+
if (messageBytes[i] <= 15) hex = hex + "0" + messageBytes[i].toString(16);
|
|
1283
|
+
else hex = hex + messageBytes[i].toString(16);
|
|
1284
|
+
}
|
|
1285
|
+
storedMessage.payloadMessage.payloadHex = hex;
|
|
1286
|
+
storedMessage.payloadMessage.qos = wireMessage.payloadMessage.qos;
|
|
1287
|
+
storedMessage.payloadMessage.destinationName = wireMessage.payloadMessage.destinationName;
|
|
1288
|
+
if (wireMessage.payloadMessage.duplicate) storedMessage.payloadMessage.duplicate = true;
|
|
1289
|
+
if (wireMessage.payloadMessage.retained) storedMessage.payloadMessage.retained = true;
|
|
1290
|
+
if (prefix.indexOf("Sent:") === 0) {
|
|
1291
|
+
if (wireMessage.sequence === void 0) wireMessage.sequence = ++this._sequence;
|
|
1292
|
+
storedMessage.sequence = wireMessage.sequence;
|
|
1293
|
+
}
|
|
1294
|
+
break;
|
|
1295
|
+
default:
|
|
1296
|
+
throw Error(format(ERROR.INVALID_STORED_DATA, [
|
|
1297
|
+
prefix + this._localKey + wireMessage.messageIdentifier,
|
|
1298
|
+
storedMessage
|
|
1299
|
+
]));
|
|
1300
|
+
}
|
|
1301
|
+
localStorage.setItem(prefix + this._localKey + wireMessage.messageIdentifier, JSON.stringify(storedMessage));
|
|
1302
|
+
};
|
|
1303
|
+
ClientImpl.prototype.restore = function(key) {
|
|
1304
|
+
var value = localStorage.getItem(key);
|
|
1305
|
+
var storedMessage = JSON.parse(value);
|
|
1306
|
+
var wireMessage = new WireMessage(storedMessage.type, storedMessage);
|
|
1307
|
+
switch(storedMessage.type){
|
|
1308
|
+
case MESSAGE_TYPE.PUBLISH:
|
|
1309
|
+
var hex = storedMessage.payloadMessage.payloadHex;
|
|
1310
|
+
var buffer = new ArrayBuffer(hex.length / 2);
|
|
1311
|
+
var byteStream = new Uint8Array(buffer);
|
|
1312
|
+
var i = 0;
|
|
1313
|
+
while(hex.length >= 2){
|
|
1314
|
+
var x = parseInt(hex.substring(0, 2), 16);
|
|
1315
|
+
hex = hex.substring(2, hex.length);
|
|
1316
|
+
byteStream[i++] = x;
|
|
1317
|
+
}
|
|
1318
|
+
var payloadMessage = new PahoMQTT.Message(byteStream);
|
|
1319
|
+
payloadMessage.qos = storedMessage.payloadMessage.qos;
|
|
1320
|
+
payloadMessage.destinationName = storedMessage.payloadMessage.destinationName;
|
|
1321
|
+
if (storedMessage.payloadMessage.duplicate) payloadMessage.duplicate = true;
|
|
1322
|
+
if (storedMessage.payloadMessage.retained) payloadMessage.retained = true;
|
|
1323
|
+
wireMessage.payloadMessage = payloadMessage;
|
|
1324
|
+
break;
|
|
1325
|
+
default:
|
|
1326
|
+
throw Error(format(ERROR.INVALID_STORED_DATA, [
|
|
1327
|
+
key,
|
|
1328
|
+
value
|
|
1329
|
+
]));
|
|
1330
|
+
}
|
|
1331
|
+
if (key.indexOf("Sent:" + this._localKey) === 0) {
|
|
1332
|
+
wireMessage.payloadMessage.duplicate = true;
|
|
1333
|
+
this._sentMessages[wireMessage.messageIdentifier] = wireMessage;
|
|
1334
|
+
} else if (key.indexOf("Received:" + this._localKey) === 0) {
|
|
1335
|
+
this._receivedMessages[wireMessage.messageIdentifier] = wireMessage;
|
|
1336
|
+
}
|
|
1337
|
+
};
|
|
1338
|
+
ClientImpl.prototype._process_queue = function() {
|
|
1339
|
+
var message = null;
|
|
1340
|
+
while(message = this._msg_queue.pop()){
|
|
1341
|
+
this._socket_send(message);
|
|
1342
|
+
if (this._notify_msg_sent[message]) {
|
|
1343
|
+
this._notify_msg_sent[message]();
|
|
1344
|
+
delete this._notify_msg_sent[message];
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
};
|
|
1348
|
+
ClientImpl.prototype._requires_ack = function(wireMessage) {
|
|
1349
|
+
var messageCount = Object.keys(this._sentMessages).length;
|
|
1350
|
+
if (messageCount > this.maxMessageIdentifier) throw Error("Too many messages:" + messageCount);
|
|
1351
|
+
while(this._sentMessages[this._message_identifier] !== void 0){
|
|
1352
|
+
this._message_identifier++;
|
|
1353
|
+
}
|
|
1354
|
+
wireMessage.messageIdentifier = this._message_identifier;
|
|
1355
|
+
this._sentMessages[wireMessage.messageIdentifier] = wireMessage;
|
|
1356
|
+
if (wireMessage.type === MESSAGE_TYPE.PUBLISH) {
|
|
1357
|
+
this.store("Sent:", wireMessage);
|
|
1358
|
+
}
|
|
1359
|
+
if (this._message_identifier === this.maxMessageIdentifier) {
|
|
1360
|
+
this._message_identifier = 1;
|
|
1361
|
+
}
|
|
1362
|
+
};
|
|
1363
|
+
ClientImpl.prototype._on_socket_open = function() {
|
|
1364
|
+
var wireMessage = new WireMessage(MESSAGE_TYPE.CONNECT, this.connectOptions);
|
|
1365
|
+
wireMessage.clientId = this.clientId;
|
|
1366
|
+
this._socket_send(wireMessage);
|
|
1367
|
+
};
|
|
1368
|
+
ClientImpl.prototype._on_socket_message = function(event) {
|
|
1369
|
+
this._trace("Client._on_socket_message", event.data);
|
|
1370
|
+
var messages = this._deframeMessages(event.data);
|
|
1371
|
+
for(var i = 0; i < messages.length; i += 1){
|
|
1372
|
+
this._handleMessage(messages[i]);
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
ClientImpl.prototype._deframeMessages = function(data) {
|
|
1376
|
+
var byteArray = new Uint8Array(data);
|
|
1377
|
+
var messages = [];
|
|
1378
|
+
if (this.receiveBuffer) {
|
|
1379
|
+
var newData = new Uint8Array(this.receiveBuffer.length + byteArray.length);
|
|
1380
|
+
newData.set(this.receiveBuffer);
|
|
1381
|
+
newData.set(byteArray, this.receiveBuffer.length);
|
|
1382
|
+
byteArray = newData;
|
|
1383
|
+
delete this.receiveBuffer;
|
|
1384
|
+
}
|
|
1385
|
+
try {
|
|
1386
|
+
var offset = 0;
|
|
1387
|
+
while(offset < byteArray.length){
|
|
1388
|
+
var result = decodeMessage(byteArray, offset);
|
|
1389
|
+
var wireMessage = result[0];
|
|
1390
|
+
offset = result[1];
|
|
1391
|
+
if (wireMessage !== null) {
|
|
1392
|
+
messages.push(wireMessage);
|
|
1393
|
+
} else {
|
|
1394
|
+
break;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
if (offset < byteArray.length) {
|
|
1398
|
+
this.receiveBuffer = byteArray.subarray(offset);
|
|
1399
|
+
}
|
|
1400
|
+
} catch (error) {
|
|
1401
|
+
var errorStack = error.hasOwnProperty("stack") == "undefined" ? error.stack.toString() : "No Error Stack Available";
|
|
1402
|
+
this._disconnected(ERROR.INTERNAL_ERROR.code, format(ERROR.INTERNAL_ERROR, [
|
|
1403
|
+
error.message,
|
|
1404
|
+
errorStack
|
|
1405
|
+
]));
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
return messages;
|
|
1409
|
+
};
|
|
1410
|
+
ClientImpl.prototype._handleMessage = function(wireMessage) {
|
|
1411
|
+
this._trace("Client._handleMessage", wireMessage);
|
|
1412
|
+
try {
|
|
1413
|
+
switch(wireMessage.type){
|
|
1414
|
+
case MESSAGE_TYPE.CONNACK:
|
|
1415
|
+
this._connectTimeout.cancel();
|
|
1416
|
+
if (this._reconnectTimeout) this._reconnectTimeout.cancel();
|
|
1417
|
+
if (this.connectOptions.cleanSession) {
|
|
1418
|
+
for(var key in this._sentMessages){
|
|
1419
|
+
var sentMessage = this._sentMessages[key];
|
|
1420
|
+
localStorage.removeItem("Sent:" + this._localKey + sentMessage.messageIdentifier);
|
|
1421
|
+
}
|
|
1422
|
+
this._sentMessages = {};
|
|
1423
|
+
for(var key in this._receivedMessages){
|
|
1424
|
+
var receivedMessage = this._receivedMessages[key];
|
|
1425
|
+
localStorage.removeItem("Received:" + this._localKey + receivedMessage.messageIdentifier);
|
|
1426
|
+
}
|
|
1427
|
+
this._receivedMessages = {};
|
|
1428
|
+
}
|
|
1429
|
+
if (wireMessage.returnCode === 0) {
|
|
1430
|
+
this.connected = true;
|
|
1431
|
+
if (this.connectOptions.uris) this.hostIndex = this.connectOptions.uris.length;
|
|
1432
|
+
} else {
|
|
1433
|
+
this._disconnected(ERROR.CONNACK_RETURNCODE.code, format(ERROR.CONNACK_RETURNCODE, [
|
|
1434
|
+
wireMessage.returnCode,
|
|
1435
|
+
CONNACK_RC[wireMessage.returnCode]
|
|
1436
|
+
]));
|
|
1437
|
+
break;
|
|
1438
|
+
}
|
|
1439
|
+
var sequencedMessages = [];
|
|
1440
|
+
for(var msgId in this._sentMessages){
|
|
1441
|
+
if (this._sentMessages.hasOwnProperty(msgId)) sequencedMessages.push(this._sentMessages[msgId]);
|
|
1442
|
+
}
|
|
1443
|
+
if (this._buffered_msg_queue.length > 0) {
|
|
1444
|
+
var msg = null;
|
|
1445
|
+
while(msg = this._buffered_msg_queue.pop()){
|
|
1446
|
+
sequencedMessages.push(msg);
|
|
1447
|
+
if (this.onMessageDelivered) this._notify_msg_sent[msg] = this.onMessageDelivered(msg.payloadMessage);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
var sequencedMessages = sequencedMessages.sort(function(a, b) {
|
|
1451
|
+
return a.sequence - b.sequence;
|
|
1452
|
+
});
|
|
1453
|
+
for(var i = 0, len = sequencedMessages.length; i < len; i++){
|
|
1454
|
+
var sentMessage = sequencedMessages[i];
|
|
1455
|
+
if (sentMessage.type == MESSAGE_TYPE.PUBLISH && sentMessage.pubRecReceived) {
|
|
1456
|
+
var pubRelMessage = new WireMessage(MESSAGE_TYPE.PUBREL, {
|
|
1457
|
+
messageIdentifier: sentMessage.messageIdentifier
|
|
1458
|
+
});
|
|
1459
|
+
this._schedule_message(pubRelMessage);
|
|
1460
|
+
} else {
|
|
1461
|
+
this._schedule_message(sentMessage);
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
if (this.connectOptions.onSuccess) {
|
|
1465
|
+
this.connectOptions.onSuccess({
|
|
1466
|
+
invocationContext: this.connectOptions.invocationContext
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
var reconnected = false;
|
|
1470
|
+
if (this._reconnecting) {
|
|
1471
|
+
reconnected = true;
|
|
1472
|
+
this._reconnectInterval = 1;
|
|
1473
|
+
this._reconnecting = false;
|
|
1474
|
+
}
|
|
1475
|
+
this._connected(reconnected, this._wsuri);
|
|
1476
|
+
this._process_queue();
|
|
1477
|
+
break;
|
|
1478
|
+
case MESSAGE_TYPE.PUBLISH:
|
|
1479
|
+
this._receivePublish(wireMessage);
|
|
1480
|
+
break;
|
|
1481
|
+
case MESSAGE_TYPE.PUBACK:
|
|
1482
|
+
var sentMessage = this._sentMessages[wireMessage.messageIdentifier];
|
|
1483
|
+
if (sentMessage) {
|
|
1484
|
+
delete this._sentMessages[wireMessage.messageIdentifier];
|
|
1485
|
+
localStorage.removeItem("Sent:" + this._localKey + wireMessage.messageIdentifier);
|
|
1486
|
+
if (this.onMessageDelivered) this.onMessageDelivered(sentMessage.payloadMessage);
|
|
1487
|
+
}
|
|
1488
|
+
break;
|
|
1489
|
+
case MESSAGE_TYPE.PUBREC:
|
|
1490
|
+
var sentMessage = this._sentMessages[wireMessage.messageIdentifier];
|
|
1491
|
+
if (sentMessage) {
|
|
1492
|
+
sentMessage.pubRecReceived = true;
|
|
1493
|
+
var pubRelMessage = new WireMessage(MESSAGE_TYPE.PUBREL, {
|
|
1494
|
+
messageIdentifier: wireMessage.messageIdentifier
|
|
1495
|
+
});
|
|
1496
|
+
this.store("Sent:", sentMessage);
|
|
1497
|
+
this._schedule_message(pubRelMessage);
|
|
1498
|
+
}
|
|
1499
|
+
break;
|
|
1500
|
+
case MESSAGE_TYPE.PUBREL:
|
|
1501
|
+
var receivedMessage = this._receivedMessages[wireMessage.messageIdentifier];
|
|
1502
|
+
localStorage.removeItem("Received:" + this._localKey + wireMessage.messageIdentifier);
|
|
1503
|
+
if (receivedMessage) {
|
|
1504
|
+
this._receiveMessage(receivedMessage);
|
|
1505
|
+
delete this._receivedMessages[wireMessage.messageIdentifier];
|
|
1506
|
+
}
|
|
1507
|
+
var pubCompMessage = new WireMessage(MESSAGE_TYPE.PUBCOMP, {
|
|
1508
|
+
messageIdentifier: wireMessage.messageIdentifier
|
|
1509
|
+
});
|
|
1510
|
+
this._schedule_message(pubCompMessage);
|
|
1511
|
+
break;
|
|
1512
|
+
case MESSAGE_TYPE.PUBCOMP:
|
|
1513
|
+
var sentMessage = this._sentMessages[wireMessage.messageIdentifier];
|
|
1514
|
+
delete this._sentMessages[wireMessage.messageIdentifier];
|
|
1515
|
+
localStorage.removeItem("Sent:" + this._localKey + wireMessage.messageIdentifier);
|
|
1516
|
+
if (this.onMessageDelivered) this.onMessageDelivered(sentMessage.payloadMessage);
|
|
1517
|
+
break;
|
|
1518
|
+
case MESSAGE_TYPE.SUBACK:
|
|
1519
|
+
var sentMessage = this._sentMessages[wireMessage.messageIdentifier];
|
|
1520
|
+
if (sentMessage) {
|
|
1521
|
+
if (sentMessage.timeOut) sentMessage.timeOut.cancel();
|
|
1522
|
+
if (wireMessage.returnCode[0] === 128) {
|
|
1523
|
+
if (sentMessage.onFailure) {
|
|
1524
|
+
sentMessage.onFailure(wireMessage.returnCode);
|
|
1525
|
+
}
|
|
1526
|
+
} else if (sentMessage.onSuccess) {
|
|
1527
|
+
sentMessage.onSuccess(wireMessage.returnCode);
|
|
1528
|
+
}
|
|
1529
|
+
delete this._sentMessages[wireMessage.messageIdentifier];
|
|
1530
|
+
}
|
|
1531
|
+
break;
|
|
1532
|
+
case MESSAGE_TYPE.UNSUBACK:
|
|
1533
|
+
var sentMessage = this._sentMessages[wireMessage.messageIdentifier];
|
|
1534
|
+
if (sentMessage) {
|
|
1535
|
+
if (sentMessage.timeOut) sentMessage.timeOut.cancel();
|
|
1536
|
+
if (sentMessage.callback) {
|
|
1537
|
+
sentMessage.callback();
|
|
1538
|
+
}
|
|
1539
|
+
delete this._sentMessages[wireMessage.messageIdentifier];
|
|
1540
|
+
}
|
|
1541
|
+
break;
|
|
1542
|
+
case MESSAGE_TYPE.PINGRESP:
|
|
1543
|
+
this.sendPinger.reset();
|
|
1544
|
+
break;
|
|
1545
|
+
case MESSAGE_TYPE.DISCONNECT:
|
|
1546
|
+
this._disconnected(ERROR.INVALID_MQTT_MESSAGE_TYPE.code, format(ERROR.INVALID_MQTT_MESSAGE_TYPE, [
|
|
1547
|
+
wireMessage.type
|
|
1548
|
+
]));
|
|
1549
|
+
break;
|
|
1550
|
+
default:
|
|
1551
|
+
this._disconnected(ERROR.INVALID_MQTT_MESSAGE_TYPE.code, format(ERROR.INVALID_MQTT_MESSAGE_TYPE, [
|
|
1552
|
+
wireMessage.type
|
|
1553
|
+
]));
|
|
1554
|
+
}
|
|
1555
|
+
} catch (error) {
|
|
1556
|
+
var errorStack = error.hasOwnProperty("stack") == "undefined" ? error.stack.toString() : "No Error Stack Available";
|
|
1557
|
+
this._disconnected(ERROR.INTERNAL_ERROR.code, format(ERROR.INTERNAL_ERROR, [
|
|
1558
|
+
error.message,
|
|
1559
|
+
errorStack
|
|
1560
|
+
]));
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
ClientImpl.prototype._on_socket_error = function(error) {
|
|
1565
|
+
if (!this._reconnecting) {
|
|
1566
|
+
this._disconnected(ERROR.SOCKET_ERROR.code, format(ERROR.SOCKET_ERROR, [
|
|
1567
|
+
error.data
|
|
1568
|
+
]));
|
|
1569
|
+
}
|
|
1570
|
+
};
|
|
1571
|
+
ClientImpl.prototype._on_socket_close = function() {
|
|
1572
|
+
if (!this._reconnecting) {
|
|
1573
|
+
this._disconnected(ERROR.SOCKET_CLOSE.code, format(ERROR.SOCKET_CLOSE));
|
|
1574
|
+
}
|
|
1575
|
+
};
|
|
1576
|
+
ClientImpl.prototype._socket_send = function(wireMessage) {
|
|
1577
|
+
if (wireMessage.type == 1) {
|
|
1578
|
+
var wireMessageMasked = this._traceMask(wireMessage, "password");
|
|
1579
|
+
this._trace("Client._socket_send", wireMessageMasked);
|
|
1580
|
+
} else this._trace("Client._socket_send", wireMessage);
|
|
1581
|
+
this.socket.send(wireMessage.encode());
|
|
1582
|
+
this.sendPinger.reset();
|
|
1583
|
+
};
|
|
1584
|
+
ClientImpl.prototype._receivePublish = function(wireMessage) {
|
|
1585
|
+
switch(wireMessage.payloadMessage.qos){
|
|
1586
|
+
case "undefined":
|
|
1587
|
+
case 0:
|
|
1588
|
+
this._receiveMessage(wireMessage);
|
|
1589
|
+
break;
|
|
1590
|
+
case 1:
|
|
1591
|
+
var pubAckMessage = new WireMessage(MESSAGE_TYPE.PUBACK, {
|
|
1592
|
+
messageIdentifier: wireMessage.messageIdentifier
|
|
1593
|
+
});
|
|
1594
|
+
this._schedule_message(pubAckMessage);
|
|
1595
|
+
this._receiveMessage(wireMessage);
|
|
1596
|
+
break;
|
|
1597
|
+
case 2:
|
|
1598
|
+
this._receivedMessages[wireMessage.messageIdentifier] = wireMessage;
|
|
1599
|
+
this.store("Received:", wireMessage);
|
|
1600
|
+
var pubRecMessage = new WireMessage(MESSAGE_TYPE.PUBREC, {
|
|
1601
|
+
messageIdentifier: wireMessage.messageIdentifier
|
|
1602
|
+
});
|
|
1603
|
+
this._schedule_message(pubRecMessage);
|
|
1604
|
+
break;
|
|
1605
|
+
default:
|
|
1606
|
+
throw Error("Invaild qos=" + wireMessage.payloadMessage.qos);
|
|
1607
|
+
}
|
|
1608
|
+
};
|
|
1609
|
+
ClientImpl.prototype._receiveMessage = function(wireMessage) {
|
|
1610
|
+
if (this.onMessageArrived) {
|
|
1611
|
+
this.onMessageArrived(wireMessage.payloadMessage);
|
|
1612
|
+
}
|
|
1613
|
+
};
|
|
1614
|
+
ClientImpl.prototype._connected = function(reconnect, uri) {
|
|
1615
|
+
if (this.onConnected) this.onConnected(reconnect, uri);
|
|
1616
|
+
};
|
|
1617
|
+
ClientImpl.prototype._reconnect = function() {
|
|
1618
|
+
this._trace("Client._reconnect");
|
|
1619
|
+
if (!this.connected) {
|
|
1620
|
+
this._reconnecting = true;
|
|
1621
|
+
this.sendPinger.cancel();
|
|
1622
|
+
this.receivePinger.cancel();
|
|
1623
|
+
if (this._reconnectInterval < 128) this._reconnectInterval = this._reconnectInterval * 2;
|
|
1624
|
+
if (this.connectOptions.uris) {
|
|
1625
|
+
this.hostIndex = 0;
|
|
1626
|
+
this._doConnect(this.connectOptions.uris[0]);
|
|
1627
|
+
} else {
|
|
1628
|
+
this._doConnect(this.uri);
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
};
|
|
1632
|
+
ClientImpl.prototype._disconnected = function(errorCode, errorText) {
|
|
1633
|
+
this._trace("Client._disconnected", errorCode, errorText);
|
|
1634
|
+
if (errorCode !== void 0 && this._reconnecting) {
|
|
1635
|
+
this._reconnectTimeout = new Timeout(this, this._reconnectInterval, this._reconnect);
|
|
1636
|
+
return;
|
|
1637
|
+
}
|
|
1638
|
+
this.sendPinger.cancel();
|
|
1639
|
+
this.receivePinger.cancel();
|
|
1640
|
+
if (this._connectTimeout) {
|
|
1641
|
+
this._connectTimeout.cancel();
|
|
1642
|
+
this._connectTimeout = null;
|
|
1643
|
+
}
|
|
1644
|
+
this._msg_queue = [];
|
|
1645
|
+
this._buffered_msg_queue = [];
|
|
1646
|
+
this._notify_msg_sent = {};
|
|
1647
|
+
if (this.socket) {
|
|
1648
|
+
this.socket.onopen = null;
|
|
1649
|
+
this.socket.onmessage = null;
|
|
1650
|
+
this.socket.onerror = null;
|
|
1651
|
+
this.socket.onclose = null;
|
|
1652
|
+
if (this.socket.readyState === 1) this.socket.close();
|
|
1653
|
+
delete this.socket;
|
|
1654
|
+
}
|
|
1655
|
+
if (this.connectOptions.uris && this.hostIndex < this.connectOptions.uris.length - 1) {
|
|
1656
|
+
this.hostIndex++;
|
|
1657
|
+
this._doConnect(this.connectOptions.uris[this.hostIndex]);
|
|
1658
|
+
} else {
|
|
1659
|
+
if (errorCode === void 0) {
|
|
1660
|
+
errorCode = ERROR.OK.code;
|
|
1661
|
+
errorText = format(ERROR.OK);
|
|
1662
|
+
}
|
|
1663
|
+
if (this.connected) {
|
|
1664
|
+
this.connected = false;
|
|
1665
|
+
if (this.onConnectionLost) {
|
|
1666
|
+
this.onConnectionLost({
|
|
1667
|
+
errorCode: errorCode,
|
|
1668
|
+
errorMessage: errorText,
|
|
1669
|
+
reconnect: this.connectOptions.reconnect,
|
|
1670
|
+
uri: this._wsuri
|
|
1671
|
+
});
|
|
1672
|
+
}
|
|
1673
|
+
if (errorCode !== ERROR.OK.code && this.connectOptions.reconnect) {
|
|
1674
|
+
this._reconnectInterval = 1;
|
|
1675
|
+
this._reconnect();
|
|
1676
|
+
return;
|
|
1677
|
+
}
|
|
1678
|
+
} else {
|
|
1679
|
+
if (this.connectOptions.mqttVersion === 4 && this.connectOptions.mqttVersionExplicit === false) {
|
|
1680
|
+
this._trace("Failed to connect V4, dropping back to V3");
|
|
1681
|
+
this.connectOptions.mqttVersion = 3;
|
|
1682
|
+
if (this.connectOptions.uris) {
|
|
1683
|
+
this.hostIndex = 0;
|
|
1684
|
+
this._doConnect(this.connectOptions.uris[0]);
|
|
1685
|
+
} else {
|
|
1686
|
+
this._doConnect(this.uri);
|
|
1687
|
+
}
|
|
1688
|
+
} else if (this.connectOptions.onFailure) {
|
|
1689
|
+
this.connectOptions.onFailure({
|
|
1690
|
+
invocationContext: this.connectOptions.invocationContext,
|
|
1691
|
+
errorCode: errorCode,
|
|
1692
|
+
errorMessage: errorText
|
|
1693
|
+
});
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
};
|
|
1698
|
+
ClientImpl.prototype._trace = function() {
|
|
1699
|
+
if (this.traceFunction) {
|
|
1700
|
+
var args = Array.prototype.slice.call(arguments);
|
|
1701
|
+
for(var i in args){
|
|
1702
|
+
if (typeof args[i] !== "undefined") args.splice(i, 1, JSON.stringify(args[i]));
|
|
1703
|
+
}
|
|
1704
|
+
var record = args.join("");
|
|
1705
|
+
this.traceFunction({
|
|
1706
|
+
severity: "Debug",
|
|
1707
|
+
message: record
|
|
1708
|
+
});
|
|
1709
|
+
}
|
|
1710
|
+
if (this._traceBuffer !== null) {
|
|
1711
|
+
for(var i = 0, max = arguments.length; i < max; i++){
|
|
1712
|
+
if (this._traceBuffer.length == this._MAX_TRACE_ENTRIES) {
|
|
1713
|
+
this._traceBuffer.shift();
|
|
1714
|
+
}
|
|
1715
|
+
if (i === 0) this._traceBuffer.push(arguments[i]);
|
|
1716
|
+
else if (typeof arguments[i] === "undefined") this._traceBuffer.push(arguments[i]);
|
|
1717
|
+
else this._traceBuffer.push(" " + JSON.stringify(arguments[i]));
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
};
|
|
1721
|
+
ClientImpl.prototype._traceMask = function(traceObject, masked) {
|
|
1722
|
+
var traceObjectMasked = {};
|
|
1723
|
+
for(var attr in traceObject){
|
|
1724
|
+
if (traceObject.hasOwnProperty(attr)) {
|
|
1725
|
+
if (attr == masked) traceObjectMasked[attr] = "******";
|
|
1726
|
+
else traceObjectMasked[attr] = traceObject[attr];
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
return traceObjectMasked;
|
|
1730
|
+
};
|
|
1731
|
+
var Client2 = function Client2(host, port, path, clientId) {
|
|
1732
|
+
var uri;
|
|
1733
|
+
if (typeof host !== "string") throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1734
|
+
typeof host === "undefined" ? "undefined" : _type_of(host),
|
|
1735
|
+
"host"
|
|
1736
|
+
]));
|
|
1737
|
+
if (arguments.length == 2) {
|
|
1738
|
+
clientId = port;
|
|
1739
|
+
uri = host;
|
|
1740
|
+
var match = uri.match(/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))?(\/.*)$/);
|
|
1741
|
+
if (match) {
|
|
1742
|
+
host = match[4] || match[2];
|
|
1743
|
+
port = parseInt(match[7]);
|
|
1744
|
+
path = match[8];
|
|
1745
|
+
} else {
|
|
1746
|
+
throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1747
|
+
host,
|
|
1748
|
+
"host"
|
|
1749
|
+
]));
|
|
1750
|
+
}
|
|
1751
|
+
} else {
|
|
1752
|
+
if (arguments.length == 3) {
|
|
1753
|
+
clientId = path;
|
|
1754
|
+
path = "/mqtt";
|
|
1755
|
+
}
|
|
1756
|
+
if (typeof port !== "number" || port < 0) throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1757
|
+
typeof port === "undefined" ? "undefined" : _type_of(port),
|
|
1758
|
+
"port"
|
|
1759
|
+
]));
|
|
1760
|
+
if (typeof path !== "string") throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1761
|
+
typeof path === "undefined" ? "undefined" : _type_of(path),
|
|
1762
|
+
"path"
|
|
1763
|
+
]));
|
|
1764
|
+
var ipv6AddSBracket = host.indexOf(":") !== -1 && host.slice(0, 1) !== "[" && host.slice(-1) !== "]";
|
|
1765
|
+
uri = "ws://" + (ipv6AddSBracket ? "[" + host + "]" : host) + ":" + port + path;
|
|
1766
|
+
}
|
|
1767
|
+
var clientIdLength = 0;
|
|
1768
|
+
for(var i = 0; i < clientId.length; i++){
|
|
1769
|
+
var charCode = clientId.charCodeAt(i);
|
|
1770
|
+
if (55296 <= charCode && charCode <= 56319) {
|
|
1771
|
+
i++;
|
|
1772
|
+
}
|
|
1773
|
+
clientIdLength++;
|
|
1774
|
+
}
|
|
1775
|
+
if (typeof clientId !== "string" || clientIdLength > 65535) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1776
|
+
clientId,
|
|
1777
|
+
"clientId"
|
|
1778
|
+
]));
|
|
1779
|
+
var client = new ClientImpl(uri, host, port, path, clientId);
|
|
1780
|
+
Object.defineProperties(this, {
|
|
1781
|
+
"host": {
|
|
1782
|
+
get: function get() {
|
|
1783
|
+
return host;
|
|
1784
|
+
},
|
|
1785
|
+
set: function set() {
|
|
1786
|
+
throw new Error(format(ERROR.UNSUPPORTED_OPERATION));
|
|
1787
|
+
}
|
|
1788
|
+
},
|
|
1789
|
+
"port": {
|
|
1790
|
+
get: function get() {
|
|
1791
|
+
return port;
|
|
1792
|
+
},
|
|
1793
|
+
set: function set() {
|
|
1794
|
+
throw new Error(format(ERROR.UNSUPPORTED_OPERATION));
|
|
1795
|
+
}
|
|
1796
|
+
},
|
|
1797
|
+
"path": {
|
|
1798
|
+
get: function get() {
|
|
1799
|
+
return path;
|
|
1800
|
+
},
|
|
1801
|
+
set: function set() {
|
|
1802
|
+
throw new Error(format(ERROR.UNSUPPORTED_OPERATION));
|
|
1803
|
+
}
|
|
1804
|
+
},
|
|
1805
|
+
"uri": {
|
|
1806
|
+
get: function get() {
|
|
1807
|
+
return uri;
|
|
1808
|
+
},
|
|
1809
|
+
set: function set() {
|
|
1810
|
+
throw new Error(format(ERROR.UNSUPPORTED_OPERATION));
|
|
1811
|
+
}
|
|
1812
|
+
},
|
|
1813
|
+
"clientId": {
|
|
1814
|
+
get: function get() {
|
|
1815
|
+
return client.clientId;
|
|
1816
|
+
},
|
|
1817
|
+
set: function set() {
|
|
1818
|
+
throw new Error(format(ERROR.UNSUPPORTED_OPERATION));
|
|
1819
|
+
}
|
|
1820
|
+
},
|
|
1821
|
+
"onConnected": {
|
|
1822
|
+
get: function get() {
|
|
1823
|
+
return client.onConnected;
|
|
1824
|
+
},
|
|
1825
|
+
set: function set(newOnConnected) {
|
|
1826
|
+
if (typeof newOnConnected === "function") client.onConnected = newOnConnected;
|
|
1827
|
+
else throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1828
|
+
typeof newOnConnected === "undefined" ? "undefined" : _type_of(newOnConnected),
|
|
1829
|
+
"onConnected"
|
|
1830
|
+
]));
|
|
1831
|
+
}
|
|
1832
|
+
},
|
|
1833
|
+
"disconnectedPublishing": {
|
|
1834
|
+
get: function get() {
|
|
1835
|
+
return client.disconnectedPublishing;
|
|
1836
|
+
},
|
|
1837
|
+
set: function set(newDisconnectedPublishing) {
|
|
1838
|
+
client.disconnectedPublishing = newDisconnectedPublishing;
|
|
1839
|
+
}
|
|
1840
|
+
},
|
|
1841
|
+
"disconnectedBufferSize": {
|
|
1842
|
+
get: function get() {
|
|
1843
|
+
return client.disconnectedBufferSize;
|
|
1844
|
+
},
|
|
1845
|
+
set: function set(newDisconnectedBufferSize) {
|
|
1846
|
+
client.disconnectedBufferSize = newDisconnectedBufferSize;
|
|
1847
|
+
}
|
|
1848
|
+
},
|
|
1849
|
+
"onConnectionLost": {
|
|
1850
|
+
get: function get() {
|
|
1851
|
+
return client.onConnectionLost;
|
|
1852
|
+
},
|
|
1853
|
+
set: function set(newOnConnectionLost) {
|
|
1854
|
+
if (typeof newOnConnectionLost === "function") client.onConnectionLost = newOnConnectionLost;
|
|
1855
|
+
else throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1856
|
+
typeof newOnConnectionLost === "undefined" ? "undefined" : _type_of(newOnConnectionLost),
|
|
1857
|
+
"onConnectionLost"
|
|
1858
|
+
]));
|
|
1859
|
+
}
|
|
1860
|
+
},
|
|
1861
|
+
"onMessageDelivered": {
|
|
1862
|
+
get: function get() {
|
|
1863
|
+
return client.onMessageDelivered;
|
|
1864
|
+
},
|
|
1865
|
+
set: function set(newOnMessageDelivered) {
|
|
1866
|
+
if (typeof newOnMessageDelivered === "function") client.onMessageDelivered = newOnMessageDelivered;
|
|
1867
|
+
else throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1868
|
+
typeof newOnMessageDelivered === "undefined" ? "undefined" : _type_of(newOnMessageDelivered),
|
|
1869
|
+
"onMessageDelivered"
|
|
1870
|
+
]));
|
|
1871
|
+
}
|
|
1872
|
+
},
|
|
1873
|
+
"onMessageArrived": {
|
|
1874
|
+
get: function get() {
|
|
1875
|
+
return client.onMessageArrived;
|
|
1876
|
+
},
|
|
1877
|
+
set: function set(newOnMessageArrived) {
|
|
1878
|
+
if (typeof newOnMessageArrived === "function") client.onMessageArrived = newOnMessageArrived;
|
|
1879
|
+
else throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1880
|
+
typeof newOnMessageArrived === "undefined" ? "undefined" : _type_of(newOnMessageArrived),
|
|
1881
|
+
"onMessageArrived"
|
|
1882
|
+
]));
|
|
1883
|
+
}
|
|
1884
|
+
},
|
|
1885
|
+
"trace": {
|
|
1886
|
+
get: function get() {
|
|
1887
|
+
return client.traceFunction;
|
|
1888
|
+
},
|
|
1889
|
+
set: function set(trace) {
|
|
1890
|
+
if (typeof trace === "function") {
|
|
1891
|
+
client.traceFunction = trace;
|
|
1892
|
+
} else {
|
|
1893
|
+
throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1894
|
+
typeof trace === "undefined" ? "undefined" : _type_of(trace),
|
|
1895
|
+
"onTrace"
|
|
1896
|
+
]));
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
});
|
|
1901
|
+
this.connect = function(connectOptions) {
|
|
1902
|
+
connectOptions = connectOptions || {};
|
|
1903
|
+
validate(connectOptions, {
|
|
1904
|
+
timeout: "number",
|
|
1905
|
+
userName: "string",
|
|
1906
|
+
password: "string",
|
|
1907
|
+
willMessage: "object",
|
|
1908
|
+
keepAliveInterval: "number",
|
|
1909
|
+
cleanSession: "boolean",
|
|
1910
|
+
useSSL: "boolean",
|
|
1911
|
+
invocationContext: "object",
|
|
1912
|
+
onSuccess: "function",
|
|
1913
|
+
onFailure: "function",
|
|
1914
|
+
hosts: "object",
|
|
1915
|
+
ports: "object",
|
|
1916
|
+
reconnect: "boolean",
|
|
1917
|
+
mqttVersion: "number",
|
|
1918
|
+
mqttVersionExplicit: "boolean",
|
|
1919
|
+
uris: "object"
|
|
1920
|
+
});
|
|
1921
|
+
if (connectOptions.keepAliveInterval === void 0) connectOptions.keepAliveInterval = 60;
|
|
1922
|
+
if (connectOptions.mqttVersion > 4 || connectOptions.mqttVersion < 3) {
|
|
1923
|
+
throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1924
|
+
connectOptions.mqttVersion,
|
|
1925
|
+
"connectOptions.mqttVersion"
|
|
1926
|
+
]));
|
|
1927
|
+
}
|
|
1928
|
+
if (connectOptions.mqttVersion === void 0) {
|
|
1929
|
+
connectOptions.mqttVersionExplicit = false;
|
|
1930
|
+
connectOptions.mqttVersion = 4;
|
|
1931
|
+
} else {
|
|
1932
|
+
connectOptions.mqttVersionExplicit = true;
|
|
1933
|
+
}
|
|
1934
|
+
if (connectOptions.password !== void 0 && connectOptions.userName === void 0) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1935
|
+
connectOptions.password,
|
|
1936
|
+
"connectOptions.password"
|
|
1937
|
+
]));
|
|
1938
|
+
if (connectOptions.willMessage) {
|
|
1939
|
+
if (!_instanceof(connectOptions.willMessage, Message)) throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1940
|
+
connectOptions.willMessage,
|
|
1941
|
+
"connectOptions.willMessage"
|
|
1942
|
+
]));
|
|
1943
|
+
connectOptions.willMessage.stringPayload = null;
|
|
1944
|
+
if (typeof connectOptions.willMessage.destinationName === "undefined") throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1945
|
+
_type_of(connectOptions.willMessage.destinationName),
|
|
1946
|
+
"connectOptions.willMessage.destinationName"
|
|
1947
|
+
]));
|
|
1948
|
+
}
|
|
1949
|
+
if (typeof connectOptions.cleanSession === "undefined") connectOptions.cleanSession = true;
|
|
1950
|
+
if (connectOptions.hosts) {
|
|
1951
|
+
if (!_instanceof(connectOptions.hosts, Array)) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1952
|
+
connectOptions.hosts,
|
|
1953
|
+
"connectOptions.hosts"
|
|
1954
|
+
]));
|
|
1955
|
+
if (connectOptions.hosts.length < 1) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1956
|
+
connectOptions.hosts,
|
|
1957
|
+
"connectOptions.hosts"
|
|
1958
|
+
]));
|
|
1959
|
+
var usingURIs = false;
|
|
1960
|
+
for(var i2 = 0; i2 < connectOptions.hosts.length; i2++){
|
|
1961
|
+
if (typeof connectOptions.hosts[i2] !== "string") throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1962
|
+
_type_of(connectOptions.hosts[i2]),
|
|
1963
|
+
"connectOptions.hosts[" + i2 + "]"
|
|
1964
|
+
]));
|
|
1965
|
+
if (/^(wss?):\/\/((\[(.+)\])|([^\/]+?))(:(\d+))?(\/.*)$/.test(connectOptions.hosts[i2])) {
|
|
1966
|
+
if (i2 === 0) {
|
|
1967
|
+
usingURIs = true;
|
|
1968
|
+
} else if (!usingURIs) {
|
|
1969
|
+
throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1970
|
+
connectOptions.hosts[i2],
|
|
1971
|
+
"connectOptions.hosts[" + i2 + "]"
|
|
1972
|
+
]));
|
|
1973
|
+
}
|
|
1974
|
+
} else if (usingURIs) {
|
|
1975
|
+
throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1976
|
+
connectOptions.hosts[i2],
|
|
1977
|
+
"connectOptions.hosts[" + i2 + "]"
|
|
1978
|
+
]));
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
if (!usingURIs) {
|
|
1982
|
+
if (!connectOptions.ports) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1983
|
+
connectOptions.ports,
|
|
1984
|
+
"connectOptions.ports"
|
|
1985
|
+
]));
|
|
1986
|
+
if (!_instanceof(connectOptions.ports, Array)) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1987
|
+
connectOptions.ports,
|
|
1988
|
+
"connectOptions.ports"
|
|
1989
|
+
]));
|
|
1990
|
+
if (connectOptions.hosts.length !== connectOptions.ports.length) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
1991
|
+
connectOptions.ports,
|
|
1992
|
+
"connectOptions.ports"
|
|
1993
|
+
]));
|
|
1994
|
+
connectOptions.uris = [];
|
|
1995
|
+
for(var i2 = 0; i2 < connectOptions.hosts.length; i2++){
|
|
1996
|
+
if (typeof connectOptions.ports[i2] !== "number" || connectOptions.ports[i2] < 0) throw new Error(format(ERROR.INVALID_TYPE, [
|
|
1997
|
+
_type_of(connectOptions.ports[i2]),
|
|
1998
|
+
"connectOptions.ports[" + i2 + "]"
|
|
1999
|
+
]));
|
|
2000
|
+
var host2 = connectOptions.hosts[i2];
|
|
2001
|
+
var port2 = connectOptions.ports[i2];
|
|
2002
|
+
var ipv6 = host2.indexOf(":") !== -1;
|
|
2003
|
+
uri = "ws://" + (ipv6 ? "[" + host2 + "]" : host2) + ":" + port2 + path;
|
|
2004
|
+
connectOptions.uris.push(uri);
|
|
2005
|
+
}
|
|
2006
|
+
} else {
|
|
2007
|
+
connectOptions.uris = connectOptions.hosts;
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
client.connect(connectOptions);
|
|
2011
|
+
};
|
|
2012
|
+
this.subscribe = function(filter3, subscribeOptions) {
|
|
2013
|
+
if (typeof filter3 !== "string") throw new Error("Invalid argument:" + filter3);
|
|
2014
|
+
subscribeOptions = subscribeOptions || {};
|
|
2015
|
+
validate(subscribeOptions, {
|
|
2016
|
+
qos: "number",
|
|
2017
|
+
invocationContext: "object",
|
|
2018
|
+
onSuccess: "function",
|
|
2019
|
+
onFailure: "function",
|
|
2020
|
+
timeout: "number"
|
|
2021
|
+
});
|
|
2022
|
+
if (subscribeOptions.timeout && !subscribeOptions.onFailure) throw new Error("subscribeOptions.timeout specified with no onFailure callback.");
|
|
2023
|
+
if (typeof subscribeOptions.qos !== "undefined" && !(subscribeOptions.qos === 0 || subscribeOptions.qos === 1 || subscribeOptions.qos === 2)) throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
2024
|
+
subscribeOptions.qos,
|
|
2025
|
+
"subscribeOptions.qos"
|
|
2026
|
+
]));
|
|
2027
|
+
client.subscribe(filter3, subscribeOptions);
|
|
2028
|
+
};
|
|
2029
|
+
this.unsubscribe = function(filter3, unsubscribeOptions) {
|
|
2030
|
+
if (typeof filter3 !== "string") throw new Error("Invalid argument:" + filter3);
|
|
2031
|
+
unsubscribeOptions = unsubscribeOptions || {};
|
|
2032
|
+
validate(unsubscribeOptions, {
|
|
2033
|
+
invocationContext: "object",
|
|
2034
|
+
onSuccess: "function",
|
|
2035
|
+
onFailure: "function",
|
|
2036
|
+
timeout: "number"
|
|
2037
|
+
});
|
|
2038
|
+
if (unsubscribeOptions.timeout && !unsubscribeOptions.onFailure) throw new Error("unsubscribeOptions.timeout specified with no onFailure callback.");
|
|
2039
|
+
client.unsubscribe(filter3, unsubscribeOptions);
|
|
2040
|
+
};
|
|
2041
|
+
this.send = function(topic, payload, qos, retained) {
|
|
2042
|
+
var message;
|
|
2043
|
+
if (arguments.length === 0) {
|
|
2044
|
+
throw new Error("Invalid argument.length");
|
|
2045
|
+
} else if (arguments.length == 1) {
|
|
2046
|
+
if (!_instanceof(topic, Message) && typeof topic !== "string") throw new Error("Invalid argument:" + (typeof topic === "undefined" ? "undefined" : _type_of(topic)));
|
|
2047
|
+
message = topic;
|
|
2048
|
+
if (typeof message.destinationName === "undefined") throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
2049
|
+
message.destinationName,
|
|
2050
|
+
"Message.destinationName"
|
|
2051
|
+
]));
|
|
2052
|
+
client.send(message);
|
|
2053
|
+
} else {
|
|
2054
|
+
message = new Message(payload);
|
|
2055
|
+
message.destinationName = topic;
|
|
2056
|
+
if (arguments.length >= 3) message.qos = qos;
|
|
2057
|
+
if (arguments.length >= 4) message.retained = retained;
|
|
2058
|
+
client.send(message);
|
|
2059
|
+
}
|
|
2060
|
+
};
|
|
2061
|
+
this.publish = function(topic, payload, qos, retained) {
|
|
2062
|
+
var message;
|
|
2063
|
+
if (arguments.length === 0) {
|
|
2064
|
+
throw new Error("Invalid argument.length");
|
|
2065
|
+
} else if (arguments.length == 1) {
|
|
2066
|
+
if (!_instanceof(topic, Message) && typeof topic !== "string") throw new Error("Invalid argument:" + (typeof topic === "undefined" ? "undefined" : _type_of(topic)));
|
|
2067
|
+
message = topic;
|
|
2068
|
+
if (typeof message.destinationName === "undefined") throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
2069
|
+
message.destinationName,
|
|
2070
|
+
"Message.destinationName"
|
|
2071
|
+
]));
|
|
2072
|
+
client.send(message);
|
|
2073
|
+
} else {
|
|
2074
|
+
message = new Message(payload);
|
|
2075
|
+
message.destinationName = topic;
|
|
2076
|
+
if (arguments.length >= 3) message.qos = qos;
|
|
2077
|
+
if (arguments.length >= 4) message.retained = retained;
|
|
2078
|
+
client.send(message);
|
|
2079
|
+
}
|
|
2080
|
+
};
|
|
2081
|
+
this.disconnect = function() {
|
|
2082
|
+
client.disconnect();
|
|
2083
|
+
};
|
|
2084
|
+
this.getTraceLog = function() {
|
|
2085
|
+
return client.getTraceLog();
|
|
2086
|
+
};
|
|
2087
|
+
this.startTrace = function() {
|
|
2088
|
+
client.startTrace();
|
|
2089
|
+
};
|
|
2090
|
+
this.stopTrace = function() {
|
|
2091
|
+
client.stopTrace();
|
|
2092
|
+
};
|
|
2093
|
+
this.isConnected = function() {
|
|
2094
|
+
return client.connected;
|
|
2095
|
+
};
|
|
2096
|
+
};
|
|
2097
|
+
var Message = function Message(newPayload) {
|
|
2098
|
+
var payload;
|
|
2099
|
+
if (typeof newPayload === "string" || _instanceof(newPayload, ArrayBuffer) || ArrayBuffer.isView(newPayload) && !_instanceof(newPayload, DataView)) {
|
|
2100
|
+
payload = newPayload;
|
|
2101
|
+
} else {
|
|
2102
|
+
throw format(ERROR.INVALID_ARGUMENT, [
|
|
2103
|
+
newPayload,
|
|
2104
|
+
"newPayload"
|
|
2105
|
+
]);
|
|
2106
|
+
}
|
|
2107
|
+
var destinationName;
|
|
2108
|
+
var qos = 0;
|
|
2109
|
+
var retained = false;
|
|
2110
|
+
var duplicate = false;
|
|
2111
|
+
Object.defineProperties(this, {
|
|
2112
|
+
"payloadString": {
|
|
2113
|
+
enumerable: true,
|
|
2114
|
+
get: function get() {
|
|
2115
|
+
if (typeof payload === "string") return payload;
|
|
2116
|
+
else return parseUTF8(payload, 0, payload.length);
|
|
2117
|
+
}
|
|
2118
|
+
},
|
|
2119
|
+
"payloadBytes": {
|
|
2120
|
+
enumerable: true,
|
|
2121
|
+
get: function get() {
|
|
2122
|
+
if (typeof payload === "string") {
|
|
2123
|
+
var buffer = new ArrayBuffer(UTF8Length(payload));
|
|
2124
|
+
var byteStream = new Uint8Array(buffer);
|
|
2125
|
+
stringToUTF8(payload, byteStream, 0);
|
|
2126
|
+
return byteStream;
|
|
2127
|
+
} else {
|
|
2128
|
+
return payload;
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
},
|
|
2132
|
+
"destinationName": {
|
|
2133
|
+
enumerable: true,
|
|
2134
|
+
get: function get() {
|
|
2135
|
+
return destinationName;
|
|
2136
|
+
},
|
|
2137
|
+
set: function set(newDestinationName) {
|
|
2138
|
+
if (typeof newDestinationName === "string") destinationName = newDestinationName;
|
|
2139
|
+
else throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
2140
|
+
newDestinationName,
|
|
2141
|
+
"newDestinationName"
|
|
2142
|
+
]));
|
|
2143
|
+
}
|
|
2144
|
+
},
|
|
2145
|
+
"qos": {
|
|
2146
|
+
enumerable: true,
|
|
2147
|
+
get: function get() {
|
|
2148
|
+
return qos;
|
|
2149
|
+
},
|
|
2150
|
+
set: function set(newQos) {
|
|
2151
|
+
if (newQos === 0 || newQos === 1 || newQos === 2) qos = newQos;
|
|
2152
|
+
else throw new Error("Invalid argument:" + newQos);
|
|
2153
|
+
}
|
|
2154
|
+
},
|
|
2155
|
+
"retained": {
|
|
2156
|
+
enumerable: true,
|
|
2157
|
+
get: function get() {
|
|
2158
|
+
return retained;
|
|
2159
|
+
},
|
|
2160
|
+
set: function set(newRetained) {
|
|
2161
|
+
if (typeof newRetained === "boolean") retained = newRetained;
|
|
2162
|
+
else throw new Error(format(ERROR.INVALID_ARGUMENT, [
|
|
2163
|
+
newRetained,
|
|
2164
|
+
"newRetained"
|
|
2165
|
+
]));
|
|
2166
|
+
}
|
|
2167
|
+
},
|
|
2168
|
+
"topic": {
|
|
2169
|
+
enumerable: true,
|
|
2170
|
+
get: function get() {
|
|
2171
|
+
return destinationName;
|
|
2172
|
+
},
|
|
2173
|
+
set: function set(newTopic) {
|
|
2174
|
+
destinationName = newTopic;
|
|
2175
|
+
}
|
|
2176
|
+
},
|
|
2177
|
+
"duplicate": {
|
|
2178
|
+
enumerable: true,
|
|
2179
|
+
get: function get() {
|
|
2180
|
+
return duplicate;
|
|
2181
|
+
},
|
|
2182
|
+
set: function set(newDuplicate) {
|
|
2183
|
+
duplicate = newDuplicate;
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
});
|
|
2187
|
+
};
|
|
2188
|
+
return {
|
|
2189
|
+
Client: Client2,
|
|
2190
|
+
Message: Message
|
|
2191
|
+
};
|
|
2192
|
+
}(typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {});
|
|
2193
|
+
return PahoMQTT;
|
|
2194
|
+
});
|
|
2195
|
+
}
|
|
2196
|
+
});
|
|
2197
|
+
// src/subscription-handshake-link.ts
|
|
2198
|
+
import { ApolloLink, Observable } from "@apollo/client/core";
|
|
2199
|
+
import { CombinedGraphQLErrors } from "@apollo/client/errors";
|
|
2200
|
+
import { filter } from "rxjs/operators";
|
|
2201
|
+
// src/utils/logger.ts
|
|
2202
|
+
import debug from "debug";
|
|
2203
|
+
var debugLogger = debug("aws-appsync");
|
|
2204
|
+
var extend = function extend1() {
|
|
2205
|
+
var category = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "";
|
|
2206
|
+
var newCategory = category ? _to_consumable_array(this.namespace.split(":")).concat([
|
|
2207
|
+
category
|
|
2208
|
+
]).join(":") : this.namespace;
|
|
2209
|
+
var result = debug(newCategory);
|
|
2210
|
+
result.extend = extend.bind(result);
|
|
2211
|
+
return result;
|
|
2212
|
+
};
|
|
2213
|
+
debugLogger.extend = extend.bind(debugLogger);
|
|
2214
|
+
var logger_default = debugLogger;
|
|
2215
|
+
// src/subscription-handshake-link.ts
|
|
2216
|
+
var Paho = __toESM(require_paho_mqtt());
|
|
2217
|
+
import { getMainDefinition } from "@apollo/client/utilities";
|
|
2218
|
+
var logger = logger_default.extend("subscriptions");
|
|
2219
|
+
var mqttLogger = logger.extend("mqtt");
|
|
2220
|
+
var CONTROL_EVENTS_KEY = "@@controlEvents";
|
|
2221
|
+
var SubscriptionHandshakeLink = /*#__PURE__*/ function(ApolloLink) {
|
|
2222
|
+
"use strict";
|
|
2223
|
+
_inherits(SubscriptionHandshakeLink, ApolloLink);
|
|
2224
|
+
function SubscriptionHandshakeLink(subsInfoContextKey) {
|
|
2225
|
+
_class_call_check(this, SubscriptionHandshakeLink);
|
|
2226
|
+
var _this;
|
|
2227
|
+
_this = _call_super(this, SubscriptionHandshakeLink);
|
|
2228
|
+
_this.topicObservers = /* @__PURE__ */ new Map();
|
|
2229
|
+
_this.clientObservers = /* @__PURE__ */ new Map();
|
|
2230
|
+
_this.onMessage = function(topic, message, selectionNames) {
|
|
2231
|
+
var parsedMessage = JSON.parse(message);
|
|
2232
|
+
var observers = _this.topicObservers.get(topic);
|
|
2233
|
+
var data = selectionNames.reduce(function(acc, name) {
|
|
2234
|
+
return acc[name] = acc[name] || null, acc;
|
|
2235
|
+
}, parsedMessage.data || {});
|
|
2236
|
+
logger("Message received", {
|
|
2237
|
+
data: data,
|
|
2238
|
+
topic: topic,
|
|
2239
|
+
observers: observers
|
|
2240
|
+
});
|
|
2241
|
+
observers.forEach(function(observer) {
|
|
2242
|
+
try {
|
|
2243
|
+
observer.next(_object_spread({}, parsedMessage, {
|
|
2244
|
+
data: data
|
|
2245
|
+
}));
|
|
2246
|
+
} catch (err) {
|
|
2247
|
+
logger(err);
|
|
2248
|
+
}
|
|
2249
|
+
});
|
|
2250
|
+
};
|
|
2251
|
+
_this.subsInfoContextKey = subsInfoContextKey;
|
|
2252
|
+
return _this;
|
|
2253
|
+
}
|
|
2254
|
+
_create_class(SubscriptionHandshakeLink, [
|
|
2255
|
+
_define_property({
|
|
2256
|
+
key: "request"
|
|
2257
|
+
}, "value", function request(operation) {
|
|
2258
|
+
var _this = this;
|
|
2259
|
+
var _operation_getContext = operation.getContext(), subsInfo = _operation_getContext[this.subsInfoContextKey], tmp = _operation_getContext.controlMessages, _ref = tmp === void 0 ? _define_property({}, CONTROL_EVENTS_KEY, void 0) : tmp, controlEvents = _ref[CONTROL_EVENTS_KEY];
|
|
2260
|
+
var tmp1 = subsInfo.extensions, _ref1 = tmp1 === void 0 ? {
|
|
2261
|
+
subscription: {
|
|
2262
|
+
newSubscriptions: {},
|
|
2263
|
+
mqttConnections: []
|
|
2264
|
+
}
|
|
2265
|
+
} : tmp1, _ref_subscription = _ref1.subscription, newSubscriptions = _ref_subscription.newSubscriptions, mqttConnections = _ref_subscription.mqttConnections, _subsInfo_errors = subsInfo.errors, errors = _subsInfo_errors === void 0 ? [] : _subsInfo_errors;
|
|
2266
|
+
if (errors && errors.length) {
|
|
2267
|
+
return new Observable(function(observer) {
|
|
2268
|
+
observer.error(new CombinedGraphQLErrors({
|
|
2269
|
+
errors: errors,
|
|
2270
|
+
data: null
|
|
2271
|
+
}));
|
|
2272
|
+
return function() {};
|
|
2273
|
+
});
|
|
2274
|
+
}
|
|
2275
|
+
var newSubscriptionTopics = Object.keys(newSubscriptions).map(function(subKey) {
|
|
2276
|
+
return newSubscriptions[subKey].topic;
|
|
2277
|
+
});
|
|
2278
|
+
var existingTopicsWithObserver = new Set(newSubscriptionTopics.filter(function(t) {
|
|
2279
|
+
return _this.topicObservers.has(t);
|
|
2280
|
+
}));
|
|
2281
|
+
var newTopics = new Set(newSubscriptionTopics.filter(function(t) {
|
|
2282
|
+
return !existingTopicsWithObserver.has(t);
|
|
2283
|
+
}));
|
|
2284
|
+
return new Observable(function(observer) {
|
|
2285
|
+
existingTopicsWithObserver.forEach(function(t) {
|
|
2286
|
+
_this.topicObservers.get(t).add(observer);
|
|
2287
|
+
var anObserver = Array.from(_this.topicObservers.get(t)).find(function() {
|
|
2288
|
+
return true;
|
|
2289
|
+
});
|
|
2290
|
+
var _Array_from_find = _sliced_to_array(Array.from(_this.clientObservers).find(function(param) {
|
|
2291
|
+
var _param = _sliced_to_array(param, 2), observers = _param[1].observers;
|
|
2292
|
+
return observers.has(anObserver);
|
|
2293
|
+
}), 1), clientId = _Array_from_find[0];
|
|
2294
|
+
_this.clientObservers.get(clientId).observers.add(observer);
|
|
2295
|
+
});
|
|
2296
|
+
var newTopicsConnectionInfo = mqttConnections.filter(function(c) {
|
|
2297
|
+
return c.topics.some(function(t) {
|
|
2298
|
+
return newTopics.has(t);
|
|
2299
|
+
});
|
|
2300
|
+
}).map(function(_param) {
|
|
2301
|
+
var topics = _param.topics, rest = _object_without_properties(_param, [
|
|
2302
|
+
"topics"
|
|
2303
|
+
]);
|
|
2304
|
+
return _object_spread_props(_object_spread({}, rest), {
|
|
2305
|
+
topics: topics.filter(function(t) {
|
|
2306
|
+
return newTopics.has(t);
|
|
2307
|
+
})
|
|
2308
|
+
});
|
|
2309
|
+
});
|
|
2310
|
+
_this.connectNewClients(newTopicsConnectionInfo, observer, operation);
|
|
2311
|
+
return function() {
|
|
2312
|
+
var clientsForCurrentObserver = Array.from(_this.clientObservers).filter(function(param) {
|
|
2313
|
+
var _param = _sliced_to_array(param, 2), observers = _param[1].observers;
|
|
2314
|
+
return observers.has(observer);
|
|
2315
|
+
});
|
|
2316
|
+
clientsForCurrentObserver.forEach(function(param) {
|
|
2317
|
+
var _param = _sliced_to_array(param, 1), clientId = _param[0];
|
|
2318
|
+
return _this.clientObservers.get(clientId).observers.delete(observer);
|
|
2319
|
+
});
|
|
2320
|
+
_this.clientObservers.forEach(function(param) {
|
|
2321
|
+
var observers = param.observers, client = param.client;
|
|
2322
|
+
if (observers.size === 0) {
|
|
2323
|
+
if (client.isConnected()) {
|
|
2324
|
+
client.disconnect();
|
|
2325
|
+
}
|
|
2326
|
+
_this.clientObservers.delete(client.clientId);
|
|
2327
|
+
}
|
|
2328
|
+
});
|
|
2329
|
+
_this.clientObservers = new Map(Array.from(_this.clientObservers).filter(function(param) {
|
|
2330
|
+
var _param = _sliced_to_array(param, 2), observers = _param[1].observers;
|
|
2331
|
+
return observers.size > 0;
|
|
2332
|
+
}));
|
|
2333
|
+
_this.topicObservers.forEach(function(observers) {
|
|
2334
|
+
return observers.delete(observer);
|
|
2335
|
+
});
|
|
2336
|
+
_this.topicObservers = new Map(Array.from(_this.topicObservers).filter(function(param) {
|
|
2337
|
+
var _param = _sliced_to_array(param, 2), observers = _param[1];
|
|
2338
|
+
return observers.size > 0;
|
|
2339
|
+
}));
|
|
2340
|
+
};
|
|
2341
|
+
}).pipe(filter(function(data) {
|
|
2342
|
+
var tmp = data.extensions, _ref = tmp === void 0 ? {} : tmp, _ref_controlMsgType = _ref.controlMsgType, controlMsgType = _ref_controlMsgType === void 0 ? void 0 : _ref_controlMsgType;
|
|
2343
|
+
var isControlMsg = typeof controlMsgType !== "undefined";
|
|
2344
|
+
return controlEvents === true || !isControlMsg;
|
|
2345
|
+
}));
|
|
2346
|
+
}),
|
|
2347
|
+
{
|
|
2348
|
+
key: "connectNewClients",
|
|
2349
|
+
value: function connectNewClients(connectionInfo, observer, operation) {
|
|
2350
|
+
return _async_to_generator(function() {
|
|
2351
|
+
var _this, query, selectionNames, result, data;
|
|
2352
|
+
return _ts_generator(this, function(_state) {
|
|
2353
|
+
_this = this;
|
|
2354
|
+
query = operation.query;
|
|
2355
|
+
selectionNames = getMainDefinition(query).selectionSet.selections.map(function(param) {
|
|
2356
|
+
var value = param.name.value;
|
|
2357
|
+
return value;
|
|
2358
|
+
});
|
|
2359
|
+
result = Promise.all(connectionInfo.map(function(c) {
|
|
2360
|
+
return _this.connectNewClient(c, observer, selectionNames);
|
|
2361
|
+
}));
|
|
2362
|
+
data = selectionNames.reduce(function(acc, name) {
|
|
2363
|
+
return acc[name] = acc[name] || null, acc;
|
|
2364
|
+
}, {});
|
|
2365
|
+
observer.next({
|
|
2366
|
+
data: data,
|
|
2367
|
+
extensions: {
|
|
2368
|
+
controlMsgType: "CONNECTED",
|
|
2369
|
+
controlMsgInfo: {
|
|
2370
|
+
connectionInfo: connectionInfo
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
});
|
|
2374
|
+
return [
|
|
2375
|
+
2,
|
|
2376
|
+
result
|
|
2377
|
+
];
|
|
2378
|
+
});
|
|
2379
|
+
}).call(this);
|
|
2380
|
+
}
|
|
2381
|
+
},
|
|
2382
|
+
{
|
|
2383
|
+
key: "connectNewClient",
|
|
2384
|
+
value: function connectNewClient(connectionInfo, observer, selectionNames) {
|
|
2385
|
+
return _async_to_generator(function() {
|
|
2386
|
+
var _this, clientId, url2, topics, client;
|
|
2387
|
+
return _ts_generator(this, function(_state) {
|
|
2388
|
+
switch(_state.label){
|
|
2389
|
+
case 0:
|
|
2390
|
+
_this = this;
|
|
2391
|
+
clientId = connectionInfo.client, url2 = connectionInfo.url, topics = connectionInfo.topics;
|
|
2392
|
+
client = new Paho.Client(url2, clientId);
|
|
2393
|
+
client.trace = mqttLogger.bind(null, clientId);
|
|
2394
|
+
client.onConnectionLost = function(_param) {
|
|
2395
|
+
var errorCode = _param.errorCode, args = _object_without_properties(_param, [
|
|
2396
|
+
"errorCode"
|
|
2397
|
+
]);
|
|
2398
|
+
if (errorCode !== 0) {
|
|
2399
|
+
topics.forEach(function(t) {
|
|
2400
|
+
if (_this.topicObservers.has(t)) {
|
|
2401
|
+
_this.topicObservers.get(t).forEach(function(observer2) {
|
|
2402
|
+
return observer2.error(_object_spread_props(_object_spread({}, args), {
|
|
2403
|
+
permanent: true
|
|
2404
|
+
}));
|
|
2405
|
+
});
|
|
2406
|
+
}
|
|
2407
|
+
});
|
|
2408
|
+
}
|
|
2409
|
+
topics.forEach(function(t) {
|
|
2410
|
+
return _this.topicObservers.delete(t);
|
|
2411
|
+
});
|
|
2412
|
+
};
|
|
2413
|
+
client.onMessageArrived = function(param) {
|
|
2414
|
+
var destinationName = param.destinationName, payloadString = param.payloadString;
|
|
2415
|
+
return _this.onMessage(destinationName, payloadString, selectionNames);
|
|
2416
|
+
};
|
|
2417
|
+
return [
|
|
2418
|
+
4,
|
|
2419
|
+
new Promise(function(resolve, reject) {
|
|
2420
|
+
client.connect({
|
|
2421
|
+
useSSL: url2.indexOf("wss://") === 0,
|
|
2422
|
+
mqttVersion: 3,
|
|
2423
|
+
onSuccess: function() {
|
|
2424
|
+
return resolve(client);
|
|
2425
|
+
},
|
|
2426
|
+
onFailure: reject
|
|
2427
|
+
});
|
|
2428
|
+
})
|
|
2429
|
+
];
|
|
2430
|
+
case 1:
|
|
2431
|
+
_state.sent();
|
|
2432
|
+
return [
|
|
2433
|
+
4,
|
|
2434
|
+
this.subscribeToTopics(client, topics, observer)
|
|
2435
|
+
];
|
|
2436
|
+
case 2:
|
|
2437
|
+
_state.sent();
|
|
2438
|
+
return [
|
|
2439
|
+
2,
|
|
2440
|
+
client
|
|
2441
|
+
];
|
|
2442
|
+
}
|
|
2443
|
+
});
|
|
2444
|
+
}).call(this);
|
|
2445
|
+
}
|
|
2446
|
+
},
|
|
2447
|
+
{
|
|
2448
|
+
key: "subscribeToTopics",
|
|
2449
|
+
value: function subscribeToTopics(client, topics, observer) {
|
|
2450
|
+
var _this = this;
|
|
2451
|
+
return Promise.all(topics.map(function(topic) {
|
|
2452
|
+
return _this.subscribeToTopic(client, topic, observer);
|
|
2453
|
+
}));
|
|
2454
|
+
}
|
|
2455
|
+
},
|
|
2456
|
+
{
|
|
2457
|
+
key: "subscribeToTopic",
|
|
2458
|
+
value: function subscribeToTopic(client, topic, observer) {
|
|
2459
|
+
var _this = this;
|
|
2460
|
+
return new Promise(function(resolve, reject) {
|
|
2461
|
+
client.subscribe(topic, {
|
|
2462
|
+
onSuccess: function() {
|
|
2463
|
+
if (!_this.topicObservers.has(topic)) {
|
|
2464
|
+
_this.topicObservers.set(topic, /* @__PURE__ */ new Set());
|
|
2465
|
+
}
|
|
2466
|
+
if (!_this.clientObservers.has(client.clientId)) {
|
|
2467
|
+
_this.clientObservers.set(client.clientId, {
|
|
2468
|
+
client: client,
|
|
2469
|
+
observers: /* @__PURE__ */ new Set()
|
|
2470
|
+
});
|
|
2471
|
+
}
|
|
2472
|
+
_this.topicObservers.get(topic).add(observer);
|
|
2473
|
+
_this.clientObservers.get(client.clientId).observers.add(observer);
|
|
2474
|
+
resolve(topic);
|
|
2475
|
+
},
|
|
2476
|
+
onFailure: reject
|
|
2477
|
+
});
|
|
2478
|
+
});
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
]);
|
|
2482
|
+
return SubscriptionHandshakeLink;
|
|
2483
|
+
}(ApolloLink);
|
|
2484
|
+
// src/index.ts
|
|
2485
|
+
import { ApolloLink as ApolloLink4, Observable as Observable3 } from "@apollo/client/core";
|
|
2486
|
+
import { createHttpLink } from "@apollo/client/link/http";
|
|
2487
|
+
import { getMainDefinition as getMainDefinition2 } from "@apollo/client/utilities";
|
|
2488
|
+
// src/non-terminating-link.ts
|
|
2489
|
+
import { ApolloLink as ApolloLink2 } from "@apollo/client/core";
|
|
2490
|
+
import { setContext } from "@apollo/client/link/context";
|
|
2491
|
+
var NonTerminatingLink = /*#__PURE__*/ function(ApolloLink2) {
|
|
2492
|
+
"use strict";
|
|
2493
|
+
_inherits(NonTerminatingLink, ApolloLink2);
|
|
2494
|
+
function NonTerminatingLink(contextKey, param) {
|
|
2495
|
+
var link = param.link;
|
|
2496
|
+
_class_call_check(this, NonTerminatingLink);
|
|
2497
|
+
var _this;
|
|
2498
|
+
_this = _call_super(this, NonTerminatingLink);
|
|
2499
|
+
_this.contextKey = contextKey;
|
|
2500
|
+
_this.link = link;
|
|
2501
|
+
return _this;
|
|
2502
|
+
}
|
|
2503
|
+
_create_class(NonTerminatingLink, [
|
|
2504
|
+
{
|
|
2505
|
+
key: "request",
|
|
2506
|
+
value: function request(operation, forward) {
|
|
2507
|
+
var _this = this;
|
|
2508
|
+
return setContext(function(_request, prevContext) {
|
|
2509
|
+
return _async_to_generator(function() {
|
|
2510
|
+
var _this, result;
|
|
2511
|
+
return _ts_generator(this, function(_state) {
|
|
2512
|
+
switch(_state.label){
|
|
2513
|
+
case 0:
|
|
2514
|
+
_this = this;
|
|
2515
|
+
return [
|
|
2516
|
+
4,
|
|
2517
|
+
new Promise(function(resolve, reject) {
|
|
2518
|
+
_this.link.request(operation, forward).subscribe({
|
|
2519
|
+
next: resolve,
|
|
2520
|
+
error: reject
|
|
2521
|
+
});
|
|
2522
|
+
})
|
|
2523
|
+
];
|
|
2524
|
+
case 1:
|
|
2525
|
+
result = _state.sent();
|
|
2526
|
+
return [
|
|
2527
|
+
2,
|
|
2528
|
+
_object_spread_props(_object_spread({}, prevContext), _define_property({}, this.contextKey, result))
|
|
2529
|
+
];
|
|
2530
|
+
}
|
|
2531
|
+
});
|
|
2532
|
+
}).call(_this);
|
|
2533
|
+
}).request(operation, forward);
|
|
2534
|
+
}
|
|
2535
|
+
}
|
|
2536
|
+
]);
|
|
2537
|
+
return NonTerminatingLink;
|
|
2538
|
+
}(ApolloLink2);
|
|
2539
|
+
// src/realtime-subscription-handshake-link.ts
|
|
2540
|
+
import { ApolloLink as ApolloLink3, Observable as Observable2 } from "@apollo/client/core";
|
|
2541
|
+
import { filter as filter2 } from "rxjs/operators";
|
|
2542
|
+
import { Signer, AUTH_TYPE, USER_AGENT_HEADER, USER_AGENT } from "aws-appsync-auth-link";
|
|
2543
|
+
import { GraphQLError, print } from "graphql/index.js";
|
|
2544
|
+
import * as url from "url";
|
|
2545
|
+
import { v4 as uuid } from "uuid";
|
|
2546
|
+
// src/utils/retry.ts
|
|
2547
|
+
var logger2 = logger_default.extend("retry");
|
|
2548
|
+
var MAX_DELAY_MS = 5e3;
|
|
2549
|
+
var NonRetryableError = /*#__PURE__*/ function(Error1) {
|
|
2550
|
+
"use strict";
|
|
2551
|
+
_inherits(NonRetryableError, Error1);
|
|
2552
|
+
function NonRetryableError(message) {
|
|
2553
|
+
_class_call_check(this, NonRetryableError);
|
|
2554
|
+
var _this;
|
|
2555
|
+
_this = _call_super(this, NonRetryableError, [
|
|
2556
|
+
message
|
|
2557
|
+
]);
|
|
2558
|
+
_this.nonRetryable = true;
|
|
2559
|
+
return _this;
|
|
2560
|
+
}
|
|
2561
|
+
return NonRetryableError;
|
|
2562
|
+
}(_wrap_native_super(Error));
|
|
2563
|
+
var isNonRetryableError = function(obj) {
|
|
2564
|
+
var key = "nonRetryable";
|
|
2565
|
+
return obj && obj[key];
|
|
2566
|
+
};
|
|
2567
|
+
function retry(functionToRetry, args, delayFn) {
|
|
2568
|
+
var attempt = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 1;
|
|
2569
|
+
return _async_to_generator(function() {
|
|
2570
|
+
var err, retryIn;
|
|
2571
|
+
return _ts_generator(this, function(_state) {
|
|
2572
|
+
switch(_state.label){
|
|
2573
|
+
case 0:
|
|
2574
|
+
logger2("Attempt #".concat(attempt));
|
|
2575
|
+
_state.label = 1;
|
|
2576
|
+
case 1:
|
|
2577
|
+
_state.trys.push([
|
|
2578
|
+
1,
|
|
2579
|
+
3,
|
|
2580
|
+
,
|
|
2581
|
+
8
|
|
2582
|
+
]);
|
|
2583
|
+
return [
|
|
2584
|
+
4,
|
|
2585
|
+
functionToRetry.apply(void 0, args)
|
|
2586
|
+
];
|
|
2587
|
+
case 2:
|
|
2588
|
+
_state.sent();
|
|
2589
|
+
return [
|
|
2590
|
+
3,
|
|
2591
|
+
8
|
|
2592
|
+
];
|
|
2593
|
+
case 3:
|
|
2594
|
+
err = _state.sent();
|
|
2595
|
+
logger2("error ".concat(err));
|
|
2596
|
+
if (isNonRetryableError(err)) {
|
|
2597
|
+
logger2("non retryable error");
|
|
2598
|
+
throw err;
|
|
2599
|
+
}
|
|
2600
|
+
retryIn = delayFn(attempt, args, err);
|
|
2601
|
+
logger2("retryIn ", retryIn);
|
|
2602
|
+
if (!(retryIn !== false)) return [
|
|
2603
|
+
3,
|
|
2604
|
+
6
|
|
2605
|
+
];
|
|
2606
|
+
return [
|
|
2607
|
+
4,
|
|
2608
|
+
new Promise(function(res) {
|
|
2609
|
+
return setTimeout(res, retryIn);
|
|
2610
|
+
})
|
|
2611
|
+
];
|
|
2612
|
+
case 4:
|
|
2613
|
+
_state.sent();
|
|
2614
|
+
return [
|
|
2615
|
+
4,
|
|
2616
|
+
retry(functionToRetry, args, delayFn, attempt + 1)
|
|
2617
|
+
];
|
|
2618
|
+
case 5:
|
|
2619
|
+
return [
|
|
2620
|
+
2,
|
|
2621
|
+
_state.sent()
|
|
2622
|
+
];
|
|
2623
|
+
case 6:
|
|
2624
|
+
throw err;
|
|
2625
|
+
case 7:
|
|
2626
|
+
return [
|
|
2627
|
+
3,
|
|
2628
|
+
8
|
|
2629
|
+
];
|
|
2630
|
+
case 8:
|
|
2631
|
+
return [
|
|
2632
|
+
2
|
|
2633
|
+
];
|
|
2634
|
+
}
|
|
2635
|
+
});
|
|
2636
|
+
})();
|
|
2637
|
+
}
|
|
2638
|
+
function jitteredBackoff(maxDelayMs) {
|
|
2639
|
+
var BASE_TIME_MS = 100;
|
|
2640
|
+
var JITTER_FACTOR = 100;
|
|
2641
|
+
return function(attempt) {
|
|
2642
|
+
var delay = Math.pow(2, attempt) * BASE_TIME_MS + JITTER_FACTOR * Math.random();
|
|
2643
|
+
return delay > maxDelayMs ? false : delay;
|
|
2644
|
+
};
|
|
2645
|
+
}
|
|
2646
|
+
var jitteredExponentialRetry = function(functionToRetry, args) {
|
|
2647
|
+
var maxDelayMs = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : MAX_DELAY_MS;
|
|
2648
|
+
return retry(functionToRetry, args, jitteredBackoff(maxDelayMs));
|
|
2649
|
+
};
|
|
2650
|
+
// src/realtime-subscription-handshake-link.ts
|
|
2651
|
+
var logger3 = logger_default.extend("subscriptions");
|
|
2652
|
+
var CONTROL_EVENTS_KEY2 = "@@controlEvents";
|
|
2653
|
+
var NON_RETRYABLE_CODES = [
|
|
2654
|
+
400,
|
|
2655
|
+
401,
|
|
2656
|
+
403
|
|
2657
|
+
];
|
|
2658
|
+
var SERVICE = "appsync";
|
|
2659
|
+
var APPSYNC_REALTIME_HEADERS = {
|
|
2660
|
+
accept: "application/json, text/javascript",
|
|
2661
|
+
"content-encoding": "amz-1.0",
|
|
2662
|
+
"content-type": "application/json; charset=UTF-8"
|
|
2663
|
+
};
|
|
2664
|
+
var CONNECTION_INIT_TIMEOUT = 15e3;
|
|
2665
|
+
var START_ACK_TIMEOUT = 15e3;
|
|
2666
|
+
var SERVER_KEEP_ALIVE_TIMEOUT = 1 * 60 * 1e3;
|
|
2667
|
+
var DEFAULT_KEEP_ALIVE_TIMEOUT = 5 * 60 * 1e3;
|
|
2668
|
+
var standardDomainPattern = /^https:\/\/\w{26}\.appsync\-api\.\w{2}(?:(?:\-\w{2,})+)\-\d\.amazonaws.com\/graphql$/i;
|
|
2669
|
+
var customDomainPath = "/realtime";
|
|
2670
|
+
var AppSyncRealTimeSubscriptionHandshakeLink = /*#__PURE__*/ function(ApolloLink3) {
|
|
2671
|
+
"use strict";
|
|
2672
|
+
_inherits(_AppSyncRealTimeSubscriptionHandshakeLink, ApolloLink3);
|
|
2673
|
+
function _AppSyncRealTimeSubscriptionHandshakeLink(param) {
|
|
2674
|
+
var theUrl = param.url, theRegion = param.region, theAuth = param.auth, keepAliveTimeoutMs = param.keepAliveTimeoutMs, proxy = param.proxy;
|
|
2675
|
+
_class_call_check(this, _AppSyncRealTimeSubscriptionHandshakeLink);
|
|
2676
|
+
var _this;
|
|
2677
|
+
_this = _call_super(this, _AppSyncRealTimeSubscriptionHandshakeLink);
|
|
2678
|
+
_this.socketStatus = 0 /* CLOSED */ ;
|
|
2679
|
+
_this.keepAliveTimeout = void 0;
|
|
2680
|
+
_this.subscriptionObserverMap = /* @__PURE__ */ new Map();
|
|
2681
|
+
_this.promiseArray = [];
|
|
2682
|
+
_this.url = theUrl;
|
|
2683
|
+
_this.region = theRegion;
|
|
2684
|
+
_this.auth = theAuth;
|
|
2685
|
+
_this.proxyUrl = proxy === null || proxy === void 0 ? void 0 : proxy.url;
|
|
2686
|
+
_this.keepAliveTimeout = keepAliveTimeoutMs;
|
|
2687
|
+
if (_this.url && !_this.url.startsWith("https://")) {
|
|
2688
|
+
logger3("WARNING: AppSync endpoint URL is not using HTTPS. Credentials may be sent unencrypted: ".concat(_this.url));
|
|
2689
|
+
}
|
|
2690
|
+
if (_this.proxyUrl && !_this.proxyUrl.startsWith("https://")) {
|
|
2691
|
+
logger3("WARNING: Proxy URL is not using HTTPS. Credentials may be sent unencrypted: ".concat(_this.proxyUrl));
|
|
2692
|
+
}
|
|
2693
|
+
if (_this.keepAliveTimeout < SERVER_KEEP_ALIVE_TIMEOUT) {
|
|
2694
|
+
var configName = "keepAliveTimeoutMs";
|
|
2695
|
+
throw new Error("".concat(configName, " must be greater than or equal to ").concat(SERVER_KEEP_ALIVE_TIMEOUT, " (").concat(_this.keepAliveTimeout, " used)."));
|
|
2696
|
+
}
|
|
2697
|
+
return _this;
|
|
2698
|
+
}
|
|
2699
|
+
_create_class(_AppSyncRealTimeSubscriptionHandshakeLink, [
|
|
2700
|
+
{
|
|
2701
|
+
// Check if url matches standard domain pattern
|
|
2702
|
+
key: "isCustomDomain",
|
|
2703
|
+
value: function isCustomDomain(url2) {
|
|
2704
|
+
return url2.match(standardDomainPattern) === null;
|
|
2705
|
+
}
|
|
2706
|
+
},
|
|
2707
|
+
_define_property({
|
|
2708
|
+
key: "request"
|
|
2709
|
+
}, "value", function request(operation) {
|
|
2710
|
+
var _this = this;
|
|
2711
|
+
var query = operation.query, variables = operation.variables;
|
|
2712
|
+
var _operation_getContext = operation.getContext(), tmp = _operation_getContext.controlMessages, _ref = tmp === void 0 ? _define_property({}, CONTROL_EVENTS_KEY2, void 0) : tmp, controlEvents = _ref[CONTROL_EVENTS_KEY2], headers = _operation_getContext.headers;
|
|
2713
|
+
return new Observable2(function(observer) {
|
|
2714
|
+
if (!_this.url) {
|
|
2715
|
+
observer.error({
|
|
2716
|
+
errors: [
|
|
2717
|
+
_object_spread({}, new GraphQLError("Subscribe only available for AWS AppSync endpoint"))
|
|
2718
|
+
]
|
|
2719
|
+
});
|
|
2720
|
+
observer.complete();
|
|
2721
|
+
} else {
|
|
2722
|
+
var subscriptionId = uuid();
|
|
2723
|
+
var token = _this.auth.type === AUTH_TYPE.AMAZON_COGNITO_USER_POOLS || _this.auth.type === AUTH_TYPE.OPENID_CONNECT ? _this.auth.jwtToken : null;
|
|
2724
|
+
token = _this.auth.type === AUTH_TYPE.AWS_LAMBDA ? _this.auth.token : token;
|
|
2725
|
+
var options = {
|
|
2726
|
+
appSyncGraphqlEndpoint: _this.url,
|
|
2727
|
+
authenticationType: _this.auth.type,
|
|
2728
|
+
query: print(query),
|
|
2729
|
+
region: _this.region,
|
|
2730
|
+
graphql_headers: function() {
|
|
2731
|
+
return headers;
|
|
2732
|
+
},
|
|
2733
|
+
variables: variables,
|
|
2734
|
+
apiKey: _this.auth.type === AUTH_TYPE.API_KEY ? _this.auth.apiKey : "",
|
|
2735
|
+
credentials: _this.auth.type === AUTH_TYPE.AWS_IAM ? _this.auth.credentials : null,
|
|
2736
|
+
token: token
|
|
2737
|
+
};
|
|
2738
|
+
_this._startSubscriptionWithAWSAppSyncRealTime({
|
|
2739
|
+
options: options,
|
|
2740
|
+
observer: observer,
|
|
2741
|
+
subscriptionId: subscriptionId
|
|
2742
|
+
});
|
|
2743
|
+
return function() {
|
|
2744
|
+
return _async_to_generator(function() {
|
|
2745
|
+
var subscriptionState;
|
|
2746
|
+
return _ts_generator(this, function(_state) {
|
|
2747
|
+
try {
|
|
2748
|
+
this._verifySubscriptionAlreadyStarted(subscriptionId);
|
|
2749
|
+
subscriptionState = this.subscriptionObserverMap.get(subscriptionId).subscriptionState;
|
|
2750
|
+
if (subscriptionState === 1 /* CONNECTED */ ) {
|
|
2751
|
+
this._sendUnsubscriptionMessage(subscriptionId);
|
|
2752
|
+
} else {
|
|
2753
|
+
throw new Error("Subscription has failed, starting to remove subscription.");
|
|
2754
|
+
}
|
|
2755
|
+
} catch (err) {
|
|
2756
|
+
this._removeSubscriptionObserver(subscriptionId);
|
|
2757
|
+
return [
|
|
2758
|
+
2
|
|
2759
|
+
];
|
|
2760
|
+
}
|
|
2761
|
+
return [
|
|
2762
|
+
2
|
|
2763
|
+
];
|
|
2764
|
+
});
|
|
2765
|
+
}).call(_this);
|
|
2766
|
+
};
|
|
2767
|
+
}
|
|
2768
|
+
}).pipe(filter2(function(data) {
|
|
2769
|
+
var tmp = data.extensions, _ref = tmp === void 0 ? {} : tmp, _ref_controlMsgType = _ref.controlMsgType, controlMsgType = _ref_controlMsgType === void 0 ? void 0 : _ref_controlMsgType;
|
|
2770
|
+
var isControlMsg = typeof controlMsgType !== "undefined";
|
|
2771
|
+
return controlEvents === true || !isControlMsg;
|
|
2772
|
+
}));
|
|
2773
|
+
}),
|
|
2774
|
+
{
|
|
2775
|
+
key: "_verifySubscriptionAlreadyStarted",
|
|
2776
|
+
value: function _verifySubscriptionAlreadyStarted(subscriptionId) {
|
|
2777
|
+
return _async_to_generator(function() {
|
|
2778
|
+
var _this, subscriptionState;
|
|
2779
|
+
return _ts_generator(this, function(_state) {
|
|
2780
|
+
_this = this;
|
|
2781
|
+
subscriptionState = this.subscriptionObserverMap.get(subscriptionId).subscriptionState;
|
|
2782
|
+
if (subscriptionState === 0 /* PENDING */ ) {
|
|
2783
|
+
return [
|
|
2784
|
+
2,
|
|
2785
|
+
new Promise(function(res, rej) {
|
|
2786
|
+
var _this_subscriptionObserverMap_get = _this.subscriptionObserverMap.get(subscriptionId), observer = _this_subscriptionObserverMap_get.observer, subscriptionState2 = _this_subscriptionObserverMap_get.subscriptionState, variables = _this_subscriptionObserverMap_get.variables, query = _this_subscriptionObserverMap_get.query;
|
|
2787
|
+
_this.subscriptionObserverMap.set(subscriptionId, {
|
|
2788
|
+
observer: observer,
|
|
2789
|
+
subscriptionState: subscriptionState2,
|
|
2790
|
+
variables: variables,
|
|
2791
|
+
query: query,
|
|
2792
|
+
subscriptionReadyCallback: res,
|
|
2793
|
+
subscriptionFailedCallback: rej
|
|
2794
|
+
});
|
|
2795
|
+
})
|
|
2796
|
+
];
|
|
2797
|
+
}
|
|
2798
|
+
return [
|
|
2799
|
+
2
|
|
2800
|
+
];
|
|
2801
|
+
});
|
|
2802
|
+
}).call(this);
|
|
2803
|
+
}
|
|
2804
|
+
},
|
|
2805
|
+
{
|
|
2806
|
+
key: "_sendUnsubscriptionMessage",
|
|
2807
|
+
value: function _sendUnsubscriptionMessage(subscriptionId) {
|
|
2808
|
+
try {
|
|
2809
|
+
if (this.awsRealTimeSocket && this.awsRealTimeSocket.readyState === WebSocket.OPEN && this.socketStatus === 1 /* READY */ ) {
|
|
2810
|
+
var unsubscribeMessage = {
|
|
2811
|
+
id: subscriptionId,
|
|
2812
|
+
type: "stop" /* GQL_STOP */
|
|
2813
|
+
};
|
|
2814
|
+
var stringToAWSRealTime = JSON.stringify(unsubscribeMessage);
|
|
2815
|
+
this.awsRealTimeSocket.send(stringToAWSRealTime);
|
|
2816
|
+
this._removeSubscriptionObserver(subscriptionId);
|
|
2817
|
+
}
|
|
2818
|
+
} catch (err) {
|
|
2819
|
+
logger3({
|
|
2820
|
+
err: err
|
|
2821
|
+
});
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
},
|
|
2825
|
+
{
|
|
2826
|
+
key: "_removeSubscriptionObserver",
|
|
2827
|
+
value: function _removeSubscriptionObserver(subscriptionId) {
|
|
2828
|
+
this.subscriptionObserverMap.delete(subscriptionId);
|
|
2829
|
+
setTimeout(this._closeSocketIfRequired.bind(this), 1e3);
|
|
2830
|
+
}
|
|
2831
|
+
},
|
|
2832
|
+
{
|
|
2833
|
+
key: "_closeSocketIfRequired",
|
|
2834
|
+
value: function _closeSocketIfRequired() {
|
|
2835
|
+
if (this.subscriptionObserverMap.size > 0) {
|
|
2836
|
+
return;
|
|
2837
|
+
}
|
|
2838
|
+
if (!this.awsRealTimeSocket) {
|
|
2839
|
+
this.socketStatus = 0 /* CLOSED */ ;
|
|
2840
|
+
return;
|
|
2841
|
+
}
|
|
2842
|
+
if (this.awsRealTimeSocket.bufferedAmount > 0) {
|
|
2843
|
+
setTimeout(this._closeSocketIfRequired.bind(this), 1e3);
|
|
2844
|
+
} else {
|
|
2845
|
+
logger3("closing WebSocket...");
|
|
2846
|
+
clearTimeout(this.keepAliveTimeoutId);
|
|
2847
|
+
var tempSocket = this.awsRealTimeSocket;
|
|
2848
|
+
tempSocket.close(1e3);
|
|
2849
|
+
this.awsRealTimeSocket = null;
|
|
2850
|
+
this.socketStatus = 0 /* CLOSED */ ;
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2853
|
+
},
|
|
2854
|
+
{
|
|
2855
|
+
key: "_startSubscriptionWithAWSAppSyncRealTime",
|
|
2856
|
+
value: function _startSubscriptionWithAWSAppSyncRealTime(_0) {
|
|
2857
|
+
return _async_to_generator(function(param) {
|
|
2858
|
+
var _this, options, observer, subscriptionId, appSyncGraphqlEndpoint, authenticationType, query, variables, apiKey, region, _options_graphql_headers, graphql_headers, credentials, token, subscriptionState, data, dataString, headerObj, _tmp, subscriptionMessage, stringToAWSRealTime, err, _err_message, message, _ref, subscriptionFailedCallback2, _ref1, subscriptionFailedCallback, subscriptionReadyCallback;
|
|
2859
|
+
return _ts_generator(this, function(_state) {
|
|
2860
|
+
switch(_state.label){
|
|
2861
|
+
case 0:
|
|
2862
|
+
_this = this;
|
|
2863
|
+
options = param.options, observer = param.observer, subscriptionId = param.subscriptionId;
|
|
2864
|
+
appSyncGraphqlEndpoint = options.appSyncGraphqlEndpoint, authenticationType = options.authenticationType, query = options.query, variables = options.variables, apiKey = options.apiKey, region = options.region, _options_graphql_headers = options.graphql_headers, graphql_headers = _options_graphql_headers === void 0 ? function() {
|
|
2865
|
+
return {};
|
|
2866
|
+
} : _options_graphql_headers, credentials = options.credentials, token = options.token;
|
|
2867
|
+
subscriptionState = 0 /* PENDING */ ;
|
|
2868
|
+
data = {
|
|
2869
|
+
query: query,
|
|
2870
|
+
variables: variables
|
|
2871
|
+
};
|
|
2872
|
+
this.subscriptionObserverMap.set(subscriptionId, {
|
|
2873
|
+
observer: observer,
|
|
2874
|
+
query: query,
|
|
2875
|
+
variables: variables,
|
|
2876
|
+
subscriptionState: subscriptionState,
|
|
2877
|
+
startAckTimeoutId: null
|
|
2878
|
+
});
|
|
2879
|
+
dataString = JSON.stringify(data);
|
|
2880
|
+
_tmp = [
|
|
2881
|
+
{}
|
|
2882
|
+
];
|
|
2883
|
+
return [
|
|
2884
|
+
4,
|
|
2885
|
+
this._awsRealTimeHeaderBasedAuth({
|
|
2886
|
+
apiKey: apiKey,
|
|
2887
|
+
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
2888
|
+
authenticationType: authenticationType,
|
|
2889
|
+
payload: dataString,
|
|
2890
|
+
canonicalUri: "",
|
|
2891
|
+
region: region,
|
|
2892
|
+
credentials: credentials,
|
|
2893
|
+
token: token,
|
|
2894
|
+
graphql_headers: graphql_headers
|
|
2895
|
+
})
|
|
2896
|
+
];
|
|
2897
|
+
case 1:
|
|
2898
|
+
headerObj = _object_spread_props.apply(void 0, [
|
|
2899
|
+
_object_spread.apply(void 0, _tmp.concat([
|
|
2900
|
+
_state.sent()
|
|
2901
|
+
])),
|
|
2902
|
+
_define_property({}, USER_AGENT_HEADER, USER_AGENT)
|
|
2903
|
+
]);
|
|
2904
|
+
subscriptionMessage = {
|
|
2905
|
+
id: subscriptionId,
|
|
2906
|
+
payload: {
|
|
2907
|
+
data: dataString,
|
|
2908
|
+
extensions: {
|
|
2909
|
+
authorization: _object_spread({}, headerObj)
|
|
2910
|
+
}
|
|
2911
|
+
},
|
|
2912
|
+
type: "start" /* GQL_START */
|
|
2913
|
+
};
|
|
2914
|
+
stringToAWSRealTime = JSON.stringify(subscriptionMessage);
|
|
2915
|
+
_state.label = 2;
|
|
2916
|
+
case 2:
|
|
2917
|
+
_state.trys.push([
|
|
2918
|
+
2,
|
|
2919
|
+
4,
|
|
2920
|
+
,
|
|
2921
|
+
5
|
|
2922
|
+
]);
|
|
2923
|
+
return [
|
|
2924
|
+
4,
|
|
2925
|
+
this._initializeWebSocketConnection({
|
|
2926
|
+
apiKey: apiKey,
|
|
2927
|
+
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
2928
|
+
authenticationType: authenticationType,
|
|
2929
|
+
region: region,
|
|
2930
|
+
credentials: credentials,
|
|
2931
|
+
token: token
|
|
2932
|
+
})
|
|
2933
|
+
];
|
|
2934
|
+
case 3:
|
|
2935
|
+
_state.sent();
|
|
2936
|
+
return [
|
|
2937
|
+
3,
|
|
2938
|
+
5
|
|
2939
|
+
];
|
|
2940
|
+
case 4:
|
|
2941
|
+
err = _state.sent();
|
|
2942
|
+
_err_message = err.message, message = _err_message === void 0 ? "" : _err_message;
|
|
2943
|
+
observer.error({
|
|
2944
|
+
errors: [
|
|
2945
|
+
_object_spread({}, new GraphQLError("Connection failed: ".concat(message)))
|
|
2946
|
+
]
|
|
2947
|
+
});
|
|
2948
|
+
observer.complete();
|
|
2949
|
+
_ref = this.subscriptionObserverMap.get(subscriptionId) || {}, subscriptionFailedCallback2 = _ref.subscriptionFailedCallback;
|
|
2950
|
+
if (typeof subscriptionFailedCallback2 === "function") {
|
|
2951
|
+
subscriptionFailedCallback2();
|
|
2952
|
+
}
|
|
2953
|
+
return [
|
|
2954
|
+
2
|
|
2955
|
+
];
|
|
2956
|
+
case 5:
|
|
2957
|
+
_ref1 = this.subscriptionObserverMap.get(subscriptionId) || {}, subscriptionFailedCallback = _ref1.subscriptionFailedCallback, subscriptionReadyCallback = _ref1.subscriptionReadyCallback;
|
|
2958
|
+
this.subscriptionObserverMap.set(subscriptionId, {
|
|
2959
|
+
observer: observer,
|
|
2960
|
+
subscriptionState: subscriptionState,
|
|
2961
|
+
variables: variables,
|
|
2962
|
+
query: query,
|
|
2963
|
+
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
2964
|
+
subscriptionFailedCallback: subscriptionFailedCallback,
|
|
2965
|
+
startAckTimeoutId: setTimeout(function() {
|
|
2966
|
+
_this._timeoutStartSubscriptionAck.call(_this, subscriptionId);
|
|
2967
|
+
}, START_ACK_TIMEOUT)
|
|
2968
|
+
});
|
|
2969
|
+
if (this.awsRealTimeSocket) {
|
|
2970
|
+
this.awsRealTimeSocket.send(stringToAWSRealTime);
|
|
2971
|
+
}
|
|
2972
|
+
return [
|
|
2973
|
+
2
|
|
2974
|
+
];
|
|
2975
|
+
}
|
|
2976
|
+
});
|
|
2977
|
+
}).apply(this, arguments);
|
|
2978
|
+
}
|
|
2979
|
+
},
|
|
2980
|
+
{
|
|
2981
|
+
key: "_initializeWebSocketConnection",
|
|
2982
|
+
value: function _initializeWebSocketConnection(param) {
|
|
2983
|
+
var _this = this;
|
|
2984
|
+
var appSyncGraphqlEndpoint = param.appSyncGraphqlEndpoint, authenticationType = param.authenticationType, apiKey = param.apiKey, region = param.region, credentials = param.credentials, token = param.token;
|
|
2985
|
+
if (this.socketStatus === 1 /* READY */ ) {
|
|
2986
|
+
return;
|
|
2987
|
+
}
|
|
2988
|
+
return new Promise(function(res, rej) {
|
|
2989
|
+
return _async_to_generator(function() {
|
|
2990
|
+
var payloadString, headerObj, headerString, headerBase64url, discoverableEndpoint, protocols, err;
|
|
2991
|
+
return _ts_generator(this, function(_state) {
|
|
2992
|
+
switch(_state.label){
|
|
2993
|
+
case 0:
|
|
2994
|
+
this.promiseArray.push({
|
|
2995
|
+
res: res,
|
|
2996
|
+
rej: rej
|
|
2997
|
+
});
|
|
2998
|
+
if (!(this.socketStatus === 0 /* CLOSED */ )) return [
|
|
2999
|
+
3,
|
|
3000
|
+
5
|
|
3001
|
+
];
|
|
3002
|
+
_state.label = 1;
|
|
3003
|
+
case 1:
|
|
3004
|
+
_state.trys.push([
|
|
3005
|
+
1,
|
|
3006
|
+
4,
|
|
3007
|
+
,
|
|
3008
|
+
5
|
|
3009
|
+
]);
|
|
3010
|
+
this.socketStatus = 2 /* CONNECTING */ ;
|
|
3011
|
+
payloadString = "{}";
|
|
3012
|
+
return [
|
|
3013
|
+
4,
|
|
3014
|
+
this._awsRealTimeHeaderBasedAuth({
|
|
3015
|
+
authenticationType: authenticationType,
|
|
3016
|
+
payload: payloadString,
|
|
3017
|
+
canonicalUri: "/connect",
|
|
3018
|
+
apiKey: apiKey,
|
|
3019
|
+
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
3020
|
+
region: region,
|
|
3021
|
+
credentials: credentials,
|
|
3022
|
+
token: token,
|
|
3023
|
+
graphql_headers: function() {}
|
|
3024
|
+
})
|
|
3025
|
+
];
|
|
3026
|
+
case 2:
|
|
3027
|
+
headerObj = _state.sent();
|
|
3028
|
+
headerString = JSON.stringify(headerObj);
|
|
3029
|
+
headerBase64url = Buffer.from(headerString).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
3030
|
+
if (this.proxyUrl) {
|
|
3031
|
+
discoverableEndpoint = this.proxyUrl.replace(/\/graphql$/, "").concat(customDomainPath).replace("https://", "wss://").replace("http://", "ws://");
|
|
3032
|
+
} else if (this.isCustomDomain(appSyncGraphqlEndpoint)) {
|
|
3033
|
+
discoverableEndpoint = appSyncGraphqlEndpoint.concat(customDomainPath);
|
|
3034
|
+
discoverableEndpoint = discoverableEndpoint.replace("https://", "wss://").replace("http://", "ws://");
|
|
3035
|
+
} else {
|
|
3036
|
+
discoverableEndpoint = appSyncGraphqlEndpoint.replace("appsync-api", "appsync-realtime-api").replace("gogi-beta", "grt-beta");
|
|
3037
|
+
discoverableEndpoint = discoverableEndpoint.replace("https://", "wss://").replace("http://", "ws://");
|
|
3038
|
+
}
|
|
3039
|
+
protocols = [
|
|
3040
|
+
"graphql-ws",
|
|
3041
|
+
"header-".concat(headerBase64url)
|
|
3042
|
+
];
|
|
3043
|
+
return [
|
|
3044
|
+
4,
|
|
3045
|
+
this._initializeRetryableHandshake({
|
|
3046
|
+
awsRealTimeUrl: discoverableEndpoint,
|
|
3047
|
+
protocols: protocols
|
|
3048
|
+
})
|
|
3049
|
+
];
|
|
3050
|
+
case 3:
|
|
3051
|
+
_state.sent();
|
|
3052
|
+
this.promiseArray.forEach(function(param) {
|
|
3053
|
+
var res2 = param.res;
|
|
3054
|
+
logger3("Notifying connection successful");
|
|
3055
|
+
res2();
|
|
3056
|
+
});
|
|
3057
|
+
this.socketStatus = 1 /* READY */ ;
|
|
3058
|
+
this.promiseArray = [];
|
|
3059
|
+
return [
|
|
3060
|
+
3,
|
|
3061
|
+
5
|
|
3062
|
+
];
|
|
3063
|
+
case 4:
|
|
3064
|
+
err = _state.sent();
|
|
3065
|
+
this.promiseArray.forEach(function(param) {
|
|
3066
|
+
var rej2 = param.rej;
|
|
3067
|
+
return rej2(err);
|
|
3068
|
+
});
|
|
3069
|
+
this.promiseArray = [];
|
|
3070
|
+
if (this.awsRealTimeSocket && this.awsRealTimeSocket.readyState === WebSocket.OPEN) {
|
|
3071
|
+
this.awsRealTimeSocket.close(3001);
|
|
3072
|
+
}
|
|
3073
|
+
this.awsRealTimeSocket = null;
|
|
3074
|
+
this.socketStatus = 0 /* CLOSED */ ;
|
|
3075
|
+
return [
|
|
3076
|
+
3,
|
|
3077
|
+
5
|
|
3078
|
+
];
|
|
3079
|
+
case 5:
|
|
3080
|
+
return [
|
|
3081
|
+
2
|
|
3082
|
+
];
|
|
3083
|
+
}
|
|
3084
|
+
});
|
|
3085
|
+
}).call(_this);
|
|
3086
|
+
});
|
|
3087
|
+
}
|
|
3088
|
+
},
|
|
3089
|
+
{
|
|
3090
|
+
key: "_awsRealTimeHeaderBasedAuth",
|
|
3091
|
+
value: function _awsRealTimeHeaderBasedAuth(_0) {
|
|
3092
|
+
return _async_to_generator(function(param) {
|
|
3093
|
+
var authenticationType, payload, canonicalUri, appSyncGraphqlEndpoint, apiKey, region, credentials, token, graphql_headers, headerHandler, handler, host, result;
|
|
3094
|
+
return _ts_generator(this, function(_state) {
|
|
3095
|
+
switch(_state.label){
|
|
3096
|
+
case 0:
|
|
3097
|
+
authenticationType = param.authenticationType, payload = param.payload, canonicalUri = param.canonicalUri, appSyncGraphqlEndpoint = param.appSyncGraphqlEndpoint, apiKey = param.apiKey, region = param.region, credentials = param.credentials, token = param.token, graphql_headers = param.graphql_headers;
|
|
3098
|
+
headerHandler = {
|
|
3099
|
+
API_KEY: this._awsRealTimeApiKeyHeader.bind(this),
|
|
3100
|
+
AWS_IAM: this._awsRealTimeIAMHeader.bind(this),
|
|
3101
|
+
OPENID_CONNECT: this._awsRealTimeAuthorizationHeader.bind(this),
|
|
3102
|
+
AMAZON_COGNITO_USER_POOLS: this._awsRealTimeAuthorizationHeader.bind(this),
|
|
3103
|
+
AWS_LAMBDA: this._awsRealTimeAuthorizationHeader.bind(this)
|
|
3104
|
+
};
|
|
3105
|
+
handler = headerHandler[authenticationType];
|
|
3106
|
+
if (typeof handler !== "function") {
|
|
3107
|
+
throw new NonRetryableError("Authentication type ".concat(authenticationType, " not supported"));
|
|
3108
|
+
}
|
|
3109
|
+
host = url.parse(this.url).host;
|
|
3110
|
+
return [
|
|
3111
|
+
4,
|
|
3112
|
+
handler({
|
|
3113
|
+
payload: payload,
|
|
3114
|
+
canonicalUri: canonicalUri,
|
|
3115
|
+
appSyncGraphqlEndpoint: appSyncGraphqlEndpoint,
|
|
3116
|
+
apiKey: apiKey,
|
|
3117
|
+
region: region,
|
|
3118
|
+
host: host,
|
|
3119
|
+
credentials: credentials,
|
|
3120
|
+
token: token,
|
|
3121
|
+
graphql_headers: graphql_headers
|
|
3122
|
+
})
|
|
3123
|
+
];
|
|
3124
|
+
case 1:
|
|
3125
|
+
result = _state.sent();
|
|
3126
|
+
return [
|
|
3127
|
+
2,
|
|
3128
|
+
result
|
|
3129
|
+
];
|
|
3130
|
+
}
|
|
3131
|
+
});
|
|
3132
|
+
}).apply(this, arguments);
|
|
3133
|
+
}
|
|
3134
|
+
},
|
|
3135
|
+
{
|
|
3136
|
+
key: "_awsRealTimeAuthorizationHeader",
|
|
3137
|
+
value: function _awsRealTimeAuthorizationHeader(_0) {
|
|
3138
|
+
return _async_to_generator(function(param) {
|
|
3139
|
+
var host, token, graphql_headers, _tmp, _tmp1, _tmp2;
|
|
3140
|
+
return _ts_generator(this, function(_state) {
|
|
3141
|
+
switch(_state.label){
|
|
3142
|
+
case 0:
|
|
3143
|
+
host = param.host, token = param.token, graphql_headers = param.graphql_headers;
|
|
3144
|
+
_tmp = {};
|
|
3145
|
+
if (!(typeof token === "function")) return [
|
|
3146
|
+
3,
|
|
3147
|
+
2
|
|
3148
|
+
];
|
|
3149
|
+
return [
|
|
3150
|
+
4,
|
|
3151
|
+
token.call(void 0)
|
|
3152
|
+
];
|
|
3153
|
+
case 1:
|
|
3154
|
+
_tmp1 = _state.sent();
|
|
3155
|
+
return [
|
|
3156
|
+
3,
|
|
3157
|
+
4
|
|
3158
|
+
];
|
|
3159
|
+
case 2:
|
|
3160
|
+
return [
|
|
3161
|
+
4,
|
|
3162
|
+
token
|
|
3163
|
+
];
|
|
3164
|
+
case 3:
|
|
3165
|
+
_tmp1 = _state.sent();
|
|
3166
|
+
_state.label = 4;
|
|
3167
|
+
case 4:
|
|
3168
|
+
_tmp2 = [
|
|
3169
|
+
(_tmp.Authorization = _tmp1, _tmp.host = host, _tmp)
|
|
3170
|
+
];
|
|
3171
|
+
return [
|
|
3172
|
+
4,
|
|
3173
|
+
graphql_headers()
|
|
3174
|
+
];
|
|
3175
|
+
case 5:
|
|
3176
|
+
return [
|
|
3177
|
+
2,
|
|
3178
|
+
_object_spread.apply(void 0, _tmp2.concat([
|
|
3179
|
+
_state.sent()
|
|
3180
|
+
]))
|
|
3181
|
+
];
|
|
3182
|
+
}
|
|
3183
|
+
});
|
|
3184
|
+
}).apply(this, arguments);
|
|
3185
|
+
}
|
|
3186
|
+
},
|
|
3187
|
+
{
|
|
3188
|
+
key: "_awsRealTimeApiKeyHeader",
|
|
3189
|
+
value: function _awsRealTimeApiKeyHeader(_0) {
|
|
3190
|
+
return _async_to_generator(function(param) {
|
|
3191
|
+
var apiKey, host, graphql_headers, dt, dtStr, _tmp;
|
|
3192
|
+
return _ts_generator(this, function(_state) {
|
|
3193
|
+
switch(_state.label){
|
|
3194
|
+
case 0:
|
|
3195
|
+
apiKey = param.apiKey, host = param.host, graphql_headers = param.graphql_headers;
|
|
3196
|
+
dt = /* @__PURE__ */ new Date();
|
|
3197
|
+
dtStr = dt.toISOString().replace(/[:\-]|\.\d{3}/g, "");
|
|
3198
|
+
_tmp = [
|
|
3199
|
+
{
|
|
3200
|
+
host: host,
|
|
3201
|
+
"x-amz-date": dtStr,
|
|
3202
|
+
"x-api-key": apiKey
|
|
3203
|
+
}
|
|
3204
|
+
];
|
|
3205
|
+
return [
|
|
3206
|
+
4,
|
|
3207
|
+
graphql_headers()
|
|
3208
|
+
];
|
|
3209
|
+
case 1:
|
|
3210
|
+
return [
|
|
3211
|
+
2,
|
|
3212
|
+
_object_spread.apply(void 0, _tmp.concat([
|
|
3213
|
+
_state.sent()
|
|
3214
|
+
]))
|
|
3215
|
+
];
|
|
3216
|
+
}
|
|
3217
|
+
});
|
|
3218
|
+
}).apply(this, arguments);
|
|
3219
|
+
}
|
|
3220
|
+
},
|
|
3221
|
+
{
|
|
3222
|
+
key: "_awsRealTimeIAMHeader",
|
|
3223
|
+
value: function _awsRealTimeIAMHeader(_0) {
|
|
3224
|
+
return _async_to_generator(function(param) {
|
|
3225
|
+
var payload, canonicalUri, appSyncGraphqlEndpoint, region, credentials, endpointInfo, creds, _ref, accessKeyId, secretAccessKey, sessionToken, formattedCredentials, request, signed_params;
|
|
3226
|
+
return _ts_generator(this, function(_state) {
|
|
3227
|
+
switch(_state.label){
|
|
3228
|
+
case 0:
|
|
3229
|
+
payload = param.payload, canonicalUri = param.canonicalUri, appSyncGraphqlEndpoint = param.appSyncGraphqlEndpoint, region = param.region, credentials = param.credentials;
|
|
3230
|
+
endpointInfo = {
|
|
3231
|
+
region: region,
|
|
3232
|
+
service: SERVICE
|
|
3233
|
+
};
|
|
3234
|
+
creds = typeof credentials === "function" ? credentials.call() : credentials || {};
|
|
3235
|
+
if (!(creds && typeof creds.getPromise === "function")) return [
|
|
3236
|
+
3,
|
|
3237
|
+
2
|
|
3238
|
+
];
|
|
3239
|
+
return [
|
|
3240
|
+
4,
|
|
3241
|
+
creds.getPromise()
|
|
3242
|
+
];
|
|
3243
|
+
case 1:
|
|
3244
|
+
_state.sent();
|
|
3245
|
+
_state.label = 2;
|
|
3246
|
+
case 2:
|
|
3247
|
+
if (!creds) {
|
|
3248
|
+
throw new Error("No credentials");
|
|
3249
|
+
}
|
|
3250
|
+
return [
|
|
3251
|
+
4,
|
|
3252
|
+
creds
|
|
3253
|
+
];
|
|
3254
|
+
case 3:
|
|
3255
|
+
_ref = _state.sent(), accessKeyId = _ref.accessKeyId, secretAccessKey = _ref.secretAccessKey, sessionToken = _ref.sessionToken;
|
|
3256
|
+
formattedCredentials = {
|
|
3257
|
+
access_key: accessKeyId,
|
|
3258
|
+
secret_key: secretAccessKey,
|
|
3259
|
+
session_token: sessionToken
|
|
3260
|
+
};
|
|
3261
|
+
request = {
|
|
3262
|
+
url: "".concat(appSyncGraphqlEndpoint).concat(canonicalUri),
|
|
3263
|
+
body: payload,
|
|
3264
|
+
method: "POST",
|
|
3265
|
+
headers: _object_spread({}, APPSYNC_REALTIME_HEADERS)
|
|
3266
|
+
};
|
|
3267
|
+
signed_params = Signer.sign(request, formattedCredentials, endpointInfo);
|
|
3268
|
+
return [
|
|
3269
|
+
2,
|
|
3270
|
+
signed_params.headers
|
|
3271
|
+
];
|
|
3272
|
+
}
|
|
3273
|
+
});
|
|
3274
|
+
}).apply(this, arguments);
|
|
3275
|
+
}
|
|
3276
|
+
},
|
|
3277
|
+
{
|
|
3278
|
+
key: "_initializeRetryableHandshake",
|
|
3279
|
+
value: function _initializeRetryableHandshake(_0) {
|
|
3280
|
+
return _async_to_generator(function(param) {
|
|
3281
|
+
var awsRealTimeUrl, protocols;
|
|
3282
|
+
return _ts_generator(this, function(_state) {
|
|
3283
|
+
switch(_state.label){
|
|
3284
|
+
case 0:
|
|
3285
|
+
awsRealTimeUrl = param.awsRealTimeUrl, protocols = param.protocols;
|
|
3286
|
+
logger3("Initializaling retryable Handshake");
|
|
3287
|
+
return [
|
|
3288
|
+
4,
|
|
3289
|
+
jitteredExponentialRetry(this._initializeHandshake.bind(this), [
|
|
3290
|
+
{
|
|
3291
|
+
awsRealTimeUrl: awsRealTimeUrl,
|
|
3292
|
+
protocols: protocols
|
|
3293
|
+
}
|
|
3294
|
+
])
|
|
3295
|
+
];
|
|
3296
|
+
case 1:
|
|
3297
|
+
_state.sent();
|
|
3298
|
+
return [
|
|
3299
|
+
2
|
|
3300
|
+
];
|
|
3301
|
+
}
|
|
3302
|
+
});
|
|
3303
|
+
}).apply(this, arguments);
|
|
3304
|
+
}
|
|
3305
|
+
},
|
|
3306
|
+
{
|
|
3307
|
+
key: "_initializeHandshake",
|
|
3308
|
+
value: function _initializeHandshake(_0) {
|
|
3309
|
+
return _async_to_generator(function(param) {
|
|
3310
|
+
var _this, awsRealTimeUrl, protocols, err, errorType, errorCode;
|
|
3311
|
+
return _ts_generator(this, function(_state) {
|
|
3312
|
+
switch(_state.label){
|
|
3313
|
+
case 0:
|
|
3314
|
+
_this = this;
|
|
3315
|
+
awsRealTimeUrl = param.awsRealTimeUrl, protocols = param.protocols;
|
|
3316
|
+
logger3("Initializing handshake ".concat(awsRealTimeUrl));
|
|
3317
|
+
_state.label = 1;
|
|
3318
|
+
case 1:
|
|
3319
|
+
_state.trys.push([
|
|
3320
|
+
1,
|
|
3321
|
+
4,
|
|
3322
|
+
,
|
|
3323
|
+
5
|
|
3324
|
+
]);
|
|
3325
|
+
return [
|
|
3326
|
+
4,
|
|
3327
|
+
function() {
|
|
3328
|
+
return new Promise(function(res, rej) {
|
|
3329
|
+
var newSocket = _AppSyncRealTimeSubscriptionHandshakeLink.createWebSocket(awsRealTimeUrl, protocols);
|
|
3330
|
+
newSocket.onerror = function() {
|
|
3331
|
+
logger3("WebSocket connection error");
|
|
3332
|
+
};
|
|
3333
|
+
newSocket.onclose = function() {
|
|
3334
|
+
rej(new Error("Connection handshake error"));
|
|
3335
|
+
};
|
|
3336
|
+
newSocket.onopen = function() {
|
|
3337
|
+
_this.awsRealTimeSocket = newSocket;
|
|
3338
|
+
return res();
|
|
3339
|
+
};
|
|
3340
|
+
});
|
|
3341
|
+
}()
|
|
3342
|
+
];
|
|
3343
|
+
case 2:
|
|
3344
|
+
_state.sent();
|
|
3345
|
+
return [
|
|
3346
|
+
4,
|
|
3347
|
+
function() {
|
|
3348
|
+
return new Promise(function(res, rej) {
|
|
3349
|
+
var checkAckOk = function checkAckOk() {
|
|
3350
|
+
if (!ackOk) {
|
|
3351
|
+
rej(new Error("Connection timeout: ack from AWSRealTime was not received on ".concat(CONNECTION_INIT_TIMEOUT, " ms")));
|
|
3352
|
+
}
|
|
3353
|
+
};
|
|
3354
|
+
var ackOk = false;
|
|
3355
|
+
_this.awsRealTimeSocket.onerror = function(error) {
|
|
3356
|
+
logger3("WebSocket closed ".concat(JSON.stringify(error)));
|
|
3357
|
+
};
|
|
3358
|
+
_this.awsRealTimeSocket.onclose = function(event) {
|
|
3359
|
+
logger3("WebSocket closed ".concat(event.reason));
|
|
3360
|
+
rej(new Error(JSON.stringify(event)));
|
|
3361
|
+
};
|
|
3362
|
+
_this.awsRealTimeSocket.onmessage = function(message) {
|
|
3363
|
+
var data;
|
|
3364
|
+
try {
|
|
3365
|
+
data = JSON.parse(message.data);
|
|
3366
|
+
} catch (e) {
|
|
3367
|
+
logger3("Failed to parse WebSocket message");
|
|
3368
|
+
return;
|
|
3369
|
+
}
|
|
3370
|
+
var type = data.type, tmp = data.payload, _ref = tmp === void 0 ? {} : tmp, _ref_connectionTimeoutMs = _ref.connectionTimeoutMs, connectionTimeoutMs = _ref_connectionTimeoutMs === void 0 ? DEFAULT_KEEP_ALIVE_TIMEOUT : _ref_connectionTimeoutMs;
|
|
3371
|
+
logger3("subscription message from AWS AppSyncRealTime: ".concat(type));
|
|
3372
|
+
if (type === "connection_ack" /* GQL_CONNECTION_ACK */ ) {
|
|
3373
|
+
ackOk = true;
|
|
3374
|
+
var validTimeout = typeof connectionTimeoutMs === "number" && connectionTimeoutMs >= SERVER_KEEP_ALIVE_TIMEOUT ? connectionTimeoutMs : DEFAULT_KEEP_ALIVE_TIMEOUT;
|
|
3375
|
+
var _this_keepAliveTimeout;
|
|
3376
|
+
_this.keepAliveTimeout = (_this_keepAliveTimeout = _this.keepAliveTimeout) !== null && _this_keepAliveTimeout !== void 0 ? _this_keepAliveTimeout : validTimeout;
|
|
3377
|
+
_this.awsRealTimeSocket.onmessage = _this._handleIncomingSubscriptionMessage.bind(_this);
|
|
3378
|
+
_this.awsRealTimeSocket.onerror = function(err) {
|
|
3379
|
+
logger3(err);
|
|
3380
|
+
_this._errorDisconnect("Connection closed" /* CONNECTION_CLOSED */ );
|
|
3381
|
+
};
|
|
3382
|
+
_this.awsRealTimeSocket.onclose = function(event) {
|
|
3383
|
+
logger3("WebSocket closed ".concat(event.reason));
|
|
3384
|
+
_this._errorDisconnect("Connection closed" /* CONNECTION_CLOSED */ );
|
|
3385
|
+
};
|
|
3386
|
+
res("Cool, connected to AWS AppSyncRealTime");
|
|
3387
|
+
return;
|
|
3388
|
+
}
|
|
3389
|
+
if (type === "connection_error" /* GQL_CONNECTION_ERROR */ ) {
|
|
3390
|
+
var tmp1 = data.payload, _ref1 = tmp1 === void 0 ? {} : tmp1, tmp2 = _ref1.errors, _ref2 = _sliced_to_array(tmp2 === void 0 ? [] : tmp2, 1), tmp3 = _ref2[0], _ref3 = tmp3 === void 0 ? {} : tmp3, _ref_errorType = _ref3.errorType, errorType = _ref_errorType === void 0 ? "" : _ref_errorType, _ref_errorCode = _ref3.errorCode, errorCode = _ref_errorCode === void 0 ? 0 : _ref_errorCode;
|
|
3391
|
+
rej({
|
|
3392
|
+
errorType: errorType,
|
|
3393
|
+
errorCode: errorCode
|
|
3394
|
+
});
|
|
3395
|
+
}
|
|
3396
|
+
};
|
|
3397
|
+
var gqlInit = {
|
|
3398
|
+
type: "connection_init" /* GQL_CONNECTION_INIT */
|
|
3399
|
+
};
|
|
3400
|
+
_this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
|
|
3401
|
+
setTimeout(checkAckOk.bind(_this), CONNECTION_INIT_TIMEOUT);
|
|
3402
|
+
});
|
|
3403
|
+
}()
|
|
3404
|
+
];
|
|
3405
|
+
case 3:
|
|
3406
|
+
_state.sent();
|
|
3407
|
+
return [
|
|
3408
|
+
3,
|
|
3409
|
+
5
|
|
3410
|
+
];
|
|
3411
|
+
case 4:
|
|
3412
|
+
err = _state.sent();
|
|
3413
|
+
errorType = err.errorType, errorCode = err.errorCode;
|
|
3414
|
+
if (NON_RETRYABLE_CODES.indexOf(errorCode) >= 0) {
|
|
3415
|
+
throw new NonRetryableError(errorType);
|
|
3416
|
+
} else if (errorType) {
|
|
3417
|
+
throw new Error(errorType);
|
|
3418
|
+
} else {
|
|
3419
|
+
throw err;
|
|
3420
|
+
}
|
|
3421
|
+
return [
|
|
3422
|
+
3,
|
|
3423
|
+
5
|
|
3424
|
+
];
|
|
3425
|
+
case 5:
|
|
3426
|
+
return [
|
|
3427
|
+
2
|
|
3428
|
+
];
|
|
3429
|
+
}
|
|
3430
|
+
});
|
|
3431
|
+
}).apply(this, arguments);
|
|
3432
|
+
}
|
|
3433
|
+
},
|
|
3434
|
+
{
|
|
3435
|
+
key: "_handleIncomingSubscriptionMessage",
|
|
3436
|
+
value: function _handleIncomingSubscriptionMessage(message) {
|
|
3437
|
+
var parsed;
|
|
3438
|
+
try {
|
|
3439
|
+
parsed = JSON.parse(message.data);
|
|
3440
|
+
} catch (e) {
|
|
3441
|
+
logger3("Failed to parse incoming subscription message");
|
|
3442
|
+
return;
|
|
3443
|
+
}
|
|
3444
|
+
var _parsed_id = parsed.id, id = _parsed_id === void 0 ? "" : _parsed_id, payload = parsed.payload, type = parsed.type;
|
|
3445
|
+
logger3("subscription message from AWS AppSync RealTime: ".concat(type, " id: ").concat(id));
|
|
3446
|
+
var _ref = this.subscriptionObserverMap.get(id) || {}, _ref_observer = _ref.observer, observer = _ref_observer === void 0 ? null : _ref_observer, _ref_query = _ref.query, query = _ref_query === void 0 ? "" : _ref_query, _ref_variables = _ref.variables, variables = _ref_variables === void 0 ? {} : _ref_variables, _ref_startAckTimeoutId = _ref.startAckTimeoutId, startAckTimeoutId = _ref_startAckTimeoutId === void 0 ? 0 : _ref_startAckTimeoutId, _ref_subscriptionReadyCallback = _ref.subscriptionReadyCallback, subscriptionReadyCallback = _ref_subscriptionReadyCallback === void 0 ? null : _ref_subscriptionReadyCallback, _ref_subscriptionFailedCallback = _ref.subscriptionFailedCallback, subscriptionFailedCallback = _ref_subscriptionFailedCallback === void 0 ? null : _ref_subscriptionFailedCallback;
|
|
3447
|
+
logger3({
|
|
3448
|
+
id: id,
|
|
3449
|
+
observer: observer,
|
|
3450
|
+
query: query,
|
|
3451
|
+
variables: variables
|
|
3452
|
+
});
|
|
3453
|
+
if (type === "data" /* GQL_DATA */ && payload && payload.data) {
|
|
3454
|
+
if (observer) {
|
|
3455
|
+
observer.next(payload);
|
|
3456
|
+
} else {
|
|
3457
|
+
logger3("observer not found for id: ".concat(id));
|
|
3458
|
+
}
|
|
3459
|
+
return;
|
|
3460
|
+
}
|
|
3461
|
+
if (type === "start_ack" /* GQL_START_ACK */ ) {
|
|
3462
|
+
logger3("subscription ready for ".concat(JSON.stringify({
|
|
3463
|
+
query: query,
|
|
3464
|
+
variables: variables
|
|
3465
|
+
})));
|
|
3466
|
+
if (typeof subscriptionReadyCallback === "function") {
|
|
3467
|
+
subscriptionReadyCallback();
|
|
3468
|
+
}
|
|
3469
|
+
clearTimeout(startAckTimeoutId);
|
|
3470
|
+
if (observer) {
|
|
3471
|
+
observer.next({
|
|
3472
|
+
data: payload,
|
|
3473
|
+
extensions: {
|
|
3474
|
+
controlMsgType: "CONNECTED"
|
|
3475
|
+
}
|
|
3476
|
+
});
|
|
3477
|
+
} else {
|
|
3478
|
+
logger3("observer not found for id: ".concat(id));
|
|
3479
|
+
}
|
|
3480
|
+
var subscriptionState = 1 /* CONNECTED */ ;
|
|
3481
|
+
this.subscriptionObserverMap.set(id, {
|
|
3482
|
+
observer: observer,
|
|
3483
|
+
query: query,
|
|
3484
|
+
variables: variables,
|
|
3485
|
+
startAckTimeoutId: null,
|
|
3486
|
+
subscriptionState: subscriptionState,
|
|
3487
|
+
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
3488
|
+
subscriptionFailedCallback: subscriptionFailedCallback
|
|
3489
|
+
});
|
|
3490
|
+
return;
|
|
3491
|
+
}
|
|
3492
|
+
if (type === "ka" /* GQL_CONNECTION_KEEP_ALIVE */ ) {
|
|
3493
|
+
clearTimeout(this.keepAliveTimeoutId);
|
|
3494
|
+
this.keepAliveTimeoutId = setTimeout(this._errorDisconnect.bind(this, "Timeout disconnect" /* TIMEOUT_DISCONNECT */ ), this.keepAliveTimeout);
|
|
3495
|
+
return;
|
|
3496
|
+
}
|
|
3497
|
+
if (type === "error" /* GQL_ERROR */ ) {
|
|
3498
|
+
var subscriptionState1 = 2 /* FAILED */ ;
|
|
3499
|
+
this.subscriptionObserverMap.set(id, {
|
|
3500
|
+
observer: observer,
|
|
3501
|
+
query: query,
|
|
3502
|
+
variables: variables,
|
|
3503
|
+
startAckTimeoutId: startAckTimeoutId,
|
|
3504
|
+
subscriptionReadyCallback: subscriptionReadyCallback,
|
|
3505
|
+
subscriptionFailedCallback: subscriptionFailedCallback,
|
|
3506
|
+
subscriptionState: subscriptionState1
|
|
3507
|
+
});
|
|
3508
|
+
clearTimeout(startAckTimeoutId);
|
|
3509
|
+
if (observer) {
|
|
3510
|
+
observer.error({
|
|
3511
|
+
errors: [
|
|
3512
|
+
_object_spread({}, new GraphQLError("Connection failed: ".concat(JSON.stringify(payload))))
|
|
3513
|
+
]
|
|
3514
|
+
});
|
|
3515
|
+
observer.complete();
|
|
3516
|
+
} else {
|
|
3517
|
+
logger3("observer not found for id: ".concat(id));
|
|
3518
|
+
}
|
|
3519
|
+
if (typeof subscriptionFailedCallback === "function") {
|
|
3520
|
+
subscriptionFailedCallback();
|
|
3521
|
+
}
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
},
|
|
3525
|
+
{
|
|
3526
|
+
key: "_errorDisconnect",
|
|
3527
|
+
value: function _errorDisconnect(msg) {
|
|
3528
|
+
logger3("Disconnect error: ".concat(msg));
|
|
3529
|
+
this.subscriptionObserverMap.forEach(function(param) {
|
|
3530
|
+
var observer = param.observer;
|
|
3531
|
+
if (observer && !observer.closed) {
|
|
3532
|
+
observer.error({
|
|
3533
|
+
errors: [
|
|
3534
|
+
_object_spread({}, new GraphQLError(msg))
|
|
3535
|
+
]
|
|
3536
|
+
});
|
|
3537
|
+
}
|
|
3538
|
+
});
|
|
3539
|
+
this.subscriptionObserverMap.clear();
|
|
3540
|
+
if (this.awsRealTimeSocket) {
|
|
3541
|
+
this.awsRealTimeSocket.close();
|
|
3542
|
+
}
|
|
3543
|
+
this.socketStatus = 0 /* CLOSED */ ;
|
|
3544
|
+
}
|
|
3545
|
+
},
|
|
3546
|
+
{
|
|
3547
|
+
key: "_timeoutStartSubscriptionAck",
|
|
3548
|
+
value: function _timeoutStartSubscriptionAck(subscriptionId) {
|
|
3549
|
+
var _ref = this.subscriptionObserverMap.get(subscriptionId) || {}, observer = _ref.observer, query = _ref.query, variables = _ref.variables;
|
|
3550
|
+
if (!observer) {
|
|
3551
|
+
return;
|
|
3552
|
+
}
|
|
3553
|
+
this.subscriptionObserverMap.set(subscriptionId, {
|
|
3554
|
+
observer: observer,
|
|
3555
|
+
query: query,
|
|
3556
|
+
variables: variables,
|
|
3557
|
+
subscriptionState: 2 /* FAILED */
|
|
3558
|
+
});
|
|
3559
|
+
if (observer && !observer.closed) {
|
|
3560
|
+
observer.error({
|
|
3561
|
+
errors: [
|
|
3562
|
+
_object_spread({}, new GraphQLError("Subscription timeout ".concat(JSON.stringify({
|
|
3563
|
+
query: query,
|
|
3564
|
+
variables: variables
|
|
3565
|
+
}))))
|
|
3566
|
+
]
|
|
3567
|
+
});
|
|
3568
|
+
observer.complete();
|
|
3569
|
+
}
|
|
3570
|
+
logger3("timeoutStartSubscription", JSON.stringify({
|
|
3571
|
+
query: query,
|
|
3572
|
+
variables: variables
|
|
3573
|
+
}));
|
|
3574
|
+
}
|
|
3575
|
+
}
|
|
3576
|
+
], [
|
|
3577
|
+
{
|
|
3578
|
+
key: "createWebSocket",
|
|
3579
|
+
value: function createWebSocket(awsRealTimeUrl, protocol) {
|
|
3580
|
+
return new WebSocket(awsRealTimeUrl, protocol);
|
|
3581
|
+
}
|
|
3582
|
+
}
|
|
3583
|
+
]);
|
|
3584
|
+
return _AppSyncRealTimeSubscriptionHandshakeLink;
|
|
3585
|
+
}(ApolloLink3);
|
|
3586
|
+
// src/index.ts
|
|
3587
|
+
function createSubscriptionHandshakeLink(infoOrUrl, theResultsFetcherLink) {
|
|
3588
|
+
var resultsFetcherLink, subscriptionLinks;
|
|
3589
|
+
if (typeof infoOrUrl === "string") {
|
|
3590
|
+
resultsFetcherLink = theResultsFetcherLink || createHttpLink({
|
|
3591
|
+
uri: infoOrUrl
|
|
3592
|
+
});
|
|
3593
|
+
subscriptionLinks = ApolloLink4.from([
|
|
3594
|
+
new NonTerminatingLink("controlMessages", _define_property({}, "link", new ApolloLink4(function(operation, _forward) {
|
|
3595
|
+
return new Observable3(function(observer) {
|
|
3596
|
+
var _operation_variables = operation.variables, controlEvents = _operation_variables[CONTROL_EVENTS_KEY], variables = _object_without_properties(operation.variables, [
|
|
3597
|
+
CONTROL_EVENTS_KEY
|
|
3598
|
+
].map(_to_property_key));
|
|
3599
|
+
if (typeof controlEvents !== "undefined") {
|
|
3600
|
+
operation.variables = variables;
|
|
3601
|
+
}
|
|
3602
|
+
observer.next(_define_property({}, CONTROL_EVENTS_KEY, controlEvents));
|
|
3603
|
+
return function() {};
|
|
3604
|
+
});
|
|
3605
|
+
}))),
|
|
3606
|
+
new NonTerminatingLink("subsInfo", {
|
|
3607
|
+
link: resultsFetcherLink
|
|
3608
|
+
}),
|
|
3609
|
+
new SubscriptionHandshakeLink("subsInfo")
|
|
3610
|
+
]);
|
|
3611
|
+
} else {
|
|
3612
|
+
var url2 = infoOrUrl.url;
|
|
3613
|
+
resultsFetcherLink = theResultsFetcherLink || createHttpLink({
|
|
3614
|
+
uri: url2
|
|
3615
|
+
});
|
|
3616
|
+
subscriptionLinks = new AppSyncRealTimeSubscriptionHandshakeLink(infoOrUrl);
|
|
3617
|
+
}
|
|
3618
|
+
return ApolloLink4.split(function(operation) {
|
|
3619
|
+
var query = operation.query;
|
|
3620
|
+
var _getMainDefinition2 = getMainDefinition2(query), kind = _getMainDefinition2.kind, graphqlOperation = _getMainDefinition2.operation;
|
|
3621
|
+
var isSubscription = kind === "OperationDefinition" && graphqlOperation === "subscription";
|
|
3622
|
+
return isSubscription;
|
|
3623
|
+
}, subscriptionLinks, resultsFetcherLink);
|
|
3624
|
+
}
|
|
3625
|
+
export { CONTROL_EVENTS_KEY, createSubscriptionHandshakeLink }; /*!
|
|
3626
|
+
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3627
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
3628
|
+
*/ /*!
|
|
3629
|
+
* Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3630
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
3631
|
+
*/
|