u8-mqtt 0.4.0 → 0.5.0
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/README.md +3 -3
- package/cjs/basic-v4.cjs +107 -146
- package/cjs/basic-v4.cjs.map +1 -1
- package/cjs/basic-v5.cjs +106 -145
- package/cjs/basic-v5.cjs.map +1 -1
- package/cjs/index.cjs +163 -206
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +164 -207
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +163 -206
- package/cjs/v5.cjs.map +1 -1
- package/code/_dispatch.jsy +16 -0
- package/code/base.jsy +72 -133
- package/code/core.jsy +15 -9
- package/code/router_path.jsy +38 -51
- package/code/with_topic_router.jsy +20 -11
- package/esm/basic-v4.js +1154 -0
- package/esm/basic-v4.js.map +1 -0
- package/esm/basic-v5.js +1416 -0
- package/esm/basic-v5.js.map +1 -0
- package/esm/deno/basic-v4.js +107 -146
- package/esm/deno/basic-v4.js.map +1 -1
- package/esm/deno/basic-v5.js +106 -145
- package/esm/deno/basic-v5.js.map +1 -1
- package/esm/deno/index.js +163 -206
- package/esm/deno/index.js.map +1 -1
- package/esm/deno/v4.js +164 -207
- package/esm/deno/v4.js.map +1 -1
- package/esm/deno/v5.js +163 -206
- package/esm/deno/v5.js.map +1 -1
- package/esm/index.js +1599 -0
- package/esm/index.js.map +1 -0
- package/esm/node/basic-v4.js +107 -146
- package/esm/node/basic-v4.js.map +1 -1
- package/esm/node/basic-v4.mjs +107 -146
- package/esm/node/basic-v4.mjs.map +1 -1
- package/esm/node/basic-v5.js +106 -145
- package/esm/node/basic-v5.js.map +1 -1
- package/esm/node/basic-v5.mjs +106 -145
- package/esm/node/basic-v5.mjs.map +1 -1
- package/esm/node/index.js +163 -206
- package/esm/node/index.js.map +1 -1
- package/esm/node/index.mjs +163 -206
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.js +164 -207
- package/esm/node/v4.js.map +1 -1
- package/esm/node/v4.mjs +164 -207
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.js +163 -206
- package/esm/node/v5.js.map +1 -1
- package/esm/node/v5.mjs +163 -206
- package/esm/node/v5.mjs.map +1 -1
- package/esm/v4.js +1336 -0
- package/esm/v4.js.map +1 -0
- package/esm/v5.js +1599 -0
- package/esm/v5.js.map +1 -0
- package/esm/web/basic-v4.js +107 -146
- package/esm/web/basic-v4.js.map +1 -1
- package/esm/web/basic-v4.min.js +1 -1
- package/esm/web/basic-v4.min.js.br +0 -0
- package/esm/web/basic-v4.min.js.gz +0 -0
- package/esm/web/basic-v5.js +106 -145
- package/esm/web/basic-v5.js.map +1 -1
- package/esm/web/basic-v5.min.js +1 -1
- package/esm/web/basic-v5.min.js.br +0 -0
- package/esm/web/basic-v5.min.js.gz +0 -0
- package/esm/web/index.js +163 -206
- package/esm/web/index.js.map +1 -1
- package/esm/web/index.min.js +1 -1
- package/esm/web/index.min.js.br +0 -0
- package/esm/web/index.min.js.gz +0 -0
- package/esm/web/v4.js +164 -207
- package/esm/web/v4.js.map +1 -1
- package/esm/web/v4.min.js +1 -1
- package/esm/web/v4.min.js.br +0 -0
- package/esm/web/v4.min.js.gz +0 -0
- package/esm/web/v5.js +163 -206
- package/esm/web/v5.js.map +1 -1
- package/esm/web/v5.min.js +1 -1
- package/esm/web/v5.min.js.br +0 -0
- package/esm/web/v5.min.js.gz +0 -0
- package/package.json +7 -8
package/esm/v4.js
ADDED
|
@@ -0,0 +1,1336 @@
|
|
|
1
|
+
function encode_varint(n, a=[]) {
|
|
2
|
+
do {
|
|
3
|
+
const ni = n & 0x7f;
|
|
4
|
+
n >>>= 7;
|
|
5
|
+
a.push( ni | (0===n ? 0 : 0x80) );
|
|
6
|
+
} while (n > 0)
|
|
7
|
+
return a
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
export function decode_varint_loop(u8, i=0) {
|
|
13
|
+
let i0 = i
|
|
14
|
+
let shift = 0, n = (u8[i] & 0x7f)
|
|
15
|
+
while ( 0x80 & u8[i++] )
|
|
16
|
+
n |= (u8[i] & 0x7f) << (shift += 7)
|
|
17
|
+
|
|
18
|
+
return [n, i, i0]
|
|
19
|
+
}
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
function decode_varint$1(u8, i=0) {
|
|
24
|
+
let i0 = i;
|
|
25
|
+
// unrolled for a max of 4 chains
|
|
26
|
+
let n = (u8[i] & 0x7f) << 0;
|
|
27
|
+
if ( 0x80 & u8[i++] ) {
|
|
28
|
+
n |= (u8[i] & 0x7f) << 7;
|
|
29
|
+
if ( 0x80 & u8[i++] ) {
|
|
30
|
+
n |= (u8[i] & 0x7f) << 14;
|
|
31
|
+
if ( 0x80 & u8[i++] ) {
|
|
32
|
+
n |= (u8[i] & 0x7f) << 21;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return [n, i, i0]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class U8_Reason extends Number {
|
|
40
|
+
static of(v, pkt_kind, by_kind) {
|
|
41
|
+
let self = new this(v);
|
|
42
|
+
self.reason = by_kind?.[pkt_kind]?.get(v) || pkt_kind;
|
|
43
|
+
return self
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let mqtt_reader_v4$1 = class mqtt_reader_v4 {
|
|
48
|
+
static of(buf) { return this.prototype.of(buf) }
|
|
49
|
+
of(buf) {
|
|
50
|
+
let step = (width, k) => (k=0|step.k, step.k=k+width, k);
|
|
51
|
+
return {__proto__: this, buf, step}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
has_more() {
|
|
55
|
+
return this.buf.byteLength > (this.step.k|0)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
u8() {
|
|
59
|
+
return this.buf[this.step(1)]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
u16() {
|
|
63
|
+
let {buf, step} = this, i = step(2);
|
|
64
|
+
return (buf[i]<<8) | buf[i+1]
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
u32() {
|
|
68
|
+
let {buf, step} = this, i = step(4);
|
|
69
|
+
return (buf[i]<<24) | (buf[i+1]<<16) | (buf[i+2]<<8) | buf[i+3]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
vint() {
|
|
73
|
+
let {buf, step} = this;
|
|
74
|
+
let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
|
|
75
|
+
step(vi - vi0);
|
|
76
|
+
return n
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
bin() {
|
|
80
|
+
let {buf, step} = this, i = step(2);
|
|
81
|
+
let len = (buf[i]<<8) | buf[i+1];
|
|
82
|
+
i = step(len);
|
|
83
|
+
return buf.subarray(i, i+len)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
utf8() { return new TextDecoder('utf-8').decode(this.bin()) }
|
|
87
|
+
pair() { return [ this.utf8(), this.utf8() ] }
|
|
88
|
+
|
|
89
|
+
flags(FlagsType) { return new FlagsType(this.buf[this.step(1)]) }
|
|
90
|
+
|
|
91
|
+
reason(pkt_kind) {
|
|
92
|
+
let v = this.buf[this.step(1)];
|
|
93
|
+
if (null != v)
|
|
94
|
+
return U8_Reason.of(v, pkt_kind, this._reasons_by)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
flush() {
|
|
98
|
+
let {buf, step} = this;
|
|
99
|
+
this.step = this.buf = null;
|
|
100
|
+
return buf.subarray(step.k|0)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
|
|
106
|
+
mqtt_reader = class extends mqtt_reader {
|
|
107
|
+
static reasons(pkt_type, ...reason_entries) {
|
|
108
|
+
let proto = this.prototype;
|
|
109
|
+
proto._reasons_by = {... proto._reasons_by};
|
|
110
|
+
|
|
111
|
+
let lut = (proto._reasons_by[pkt_type] ||= new Map());
|
|
112
|
+
for (let [u8, reason] of reason_entries)
|
|
113
|
+
lut.set( u8, reason );
|
|
114
|
+
|
|
115
|
+
return this
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
for (let fn_info of info_fn_list)
|
|
120
|
+
fn_info(mqtt_reader);
|
|
121
|
+
|
|
122
|
+
return mqtt_reader
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
class mqtt_writer_v4 {
|
|
126
|
+
static of() { return this.prototype.of() }
|
|
127
|
+
of() { return {__proto__: this, $:[]} }
|
|
128
|
+
|
|
129
|
+
static init() { return this }
|
|
130
|
+
|
|
131
|
+
as_pkt(pkt_id) { return this.pack([pkt_id]) }
|
|
132
|
+
|
|
133
|
+
push(...z) { this.$.push(...z); }
|
|
134
|
+
pack(hdr) {
|
|
135
|
+
let z, i, n=0, parts = this.$;
|
|
136
|
+
this.$ = false;
|
|
137
|
+
for (z of parts) n += z.length;
|
|
138
|
+
|
|
139
|
+
hdr = encode_varint(n, hdr);
|
|
140
|
+
i = hdr.length;
|
|
141
|
+
|
|
142
|
+
let pkt = new Uint8Array(i + n);
|
|
143
|
+
pkt.set(hdr, 0);
|
|
144
|
+
for (z of parts) {
|
|
145
|
+
pkt.set(z, i);
|
|
146
|
+
i += z.length;
|
|
147
|
+
}
|
|
148
|
+
return pkt
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
u8(v) { this.push([ v & 0xff ]); }
|
|
152
|
+
u16(v) { this.push([ (v>>>8) & 0xff, v & 0xff ]); }
|
|
153
|
+
u32(v) { this.push([ (v>>>24) & 0xff, (v>>>16) & 0xff, (v>>>8) & 0xff, v & 0xff ]); }
|
|
154
|
+
vint(v) { this.push( encode_varint(v) );}
|
|
155
|
+
|
|
156
|
+
bin(u8_buf) {
|
|
157
|
+
if (! u8_buf) return this.u16(0)
|
|
158
|
+
if ('string' === typeof u8_buf)
|
|
159
|
+
return this.utf8(u8_buf)
|
|
160
|
+
|
|
161
|
+
if (u8_buf.length !== u8_buf.byteLength)
|
|
162
|
+
u8_buf = new Uint8Array(u8_buf);
|
|
163
|
+
|
|
164
|
+
this.u16(u8_buf.byteLength);
|
|
165
|
+
this.push(u8_buf);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
utf8(v) {
|
|
169
|
+
let u8_buf = new TextEncoder('utf-8').encode(v);
|
|
170
|
+
this.u16(u8_buf.byteLength);
|
|
171
|
+
this.push(u8_buf);
|
|
172
|
+
}
|
|
173
|
+
pair(k,v) { this.utf8(k); this.utf8(v); }
|
|
174
|
+
|
|
175
|
+
flags(v, enc_flags, b0=0) {
|
|
176
|
+
if (undefined !== v && isNaN(+v))
|
|
177
|
+
v = enc_flags(v, 0);
|
|
178
|
+
|
|
179
|
+
v |= b0;
|
|
180
|
+
this.push([v]);
|
|
181
|
+
return v
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
reason(v) { this.push([v | 0]); }
|
|
185
|
+
|
|
186
|
+
flush(buf) {
|
|
187
|
+
if (null != buf)
|
|
188
|
+
this.push(
|
|
189
|
+
'string' === typeof buf
|
|
190
|
+
? new TextEncoder('utf-8').encode(buf)
|
|
191
|
+
: buf );
|
|
192
|
+
|
|
193
|
+
this.push = false;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function mqtt_decode_connack(ns, mqtt_reader) {
|
|
198
|
+
class _connack_flags_ extends Number {
|
|
199
|
+
get session_present() { return this & 0x01 !== 0 }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return ns[0x2] = (pkt, u8_body) => {
|
|
203
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
204
|
+
|
|
205
|
+
pkt.flags =
|
|
206
|
+
rdr.flags(_connack_flags_);
|
|
207
|
+
|
|
208
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
209
|
+
if (5 <= pkt.mqtt_level)
|
|
210
|
+
pkt.props = rdr.props();
|
|
211
|
+
return pkt }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
function _connack_v4(mqtt_reader) {
|
|
216
|
+
mqtt_reader.reasons('connack',
|
|
217
|
+
// MQTT 3.1.1
|
|
218
|
+
[ 0x00, 'Success'],
|
|
219
|
+
[ 0x01, 'Connection refused, unacceptable protocol version'],
|
|
220
|
+
[ 0x02, 'Connection refused, identifier rejected'],
|
|
221
|
+
[ 0x03, 'Connection refused, server unavailable'],
|
|
222
|
+
[ 0x04, 'Connection refused, bad user name or password'],
|
|
223
|
+
[ 0x05, 'Connection refused, not authorized'],
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function mqtt_decode_publish(ns, mqtt_reader) {
|
|
228
|
+
return ns[0x3] = (pkt, u8_body) => {
|
|
229
|
+
let {hdr} = pkt;
|
|
230
|
+
pkt.dup = Boolean(hdr & 0x8);
|
|
231
|
+
pkt.retain = Boolean(hdr & 0x1);
|
|
232
|
+
let qos = pkt.qos = (hdr>>1) & 0x3;
|
|
233
|
+
|
|
234
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
235
|
+
pkt.topic = rdr.utf8();
|
|
236
|
+
if (0 !== qos)
|
|
237
|
+
pkt.pkt_id = rdr.u16();
|
|
238
|
+
|
|
239
|
+
if (5 <= pkt.mqtt_level)
|
|
240
|
+
pkt.props = rdr.props();
|
|
241
|
+
|
|
242
|
+
pkt.payload = rdr.flush();
|
|
243
|
+
return pkt }
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function mqtt_decode_puback(ns, mqtt_reader) {
|
|
247
|
+
return ns[0x4] = (pkt, u8_body) => {
|
|
248
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
249
|
+
|
|
250
|
+
pkt.pkt_id = rdr.u16();
|
|
251
|
+
if (5 <= pkt.mqtt_level) {
|
|
252
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
253
|
+
pkt.props = rdr.props();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return pkt }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function _mqtt_decode_suback(mqtt_reader) {
|
|
260
|
+
return (pkt, u8_body) => {
|
|
261
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
262
|
+
|
|
263
|
+
pkt.pkt_id = rdr.u16();
|
|
264
|
+
if (5 <= pkt.mqtt_level)
|
|
265
|
+
pkt.props = rdr.props();
|
|
266
|
+
|
|
267
|
+
let answers = pkt.answers = [];
|
|
268
|
+
while (rdr.has_more())
|
|
269
|
+
answers.push(
|
|
270
|
+
rdr.reason(pkt.type) );
|
|
271
|
+
|
|
272
|
+
return pkt }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function mqtt_decode_suback(ns, mqtt_reader) {
|
|
276
|
+
return ns[0x9] = _mqtt_decode_suback(mqtt_reader)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function _suback_v4(mqtt_reader) {
|
|
280
|
+
mqtt_reader.reasons('suback',
|
|
281
|
+
// MQTT 3.1.1
|
|
282
|
+
[ 0x00, 'Granted QoS 0'],
|
|
283
|
+
[ 0x01, 'Granted QoS 1'],
|
|
284
|
+
[ 0x02, 'Granted QoS 2'],
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function mqtt_decode_unsuback(ns, mqtt_reader) {
|
|
289
|
+
return ns[0xb] = _mqtt_decode_suback(mqtt_reader)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function _unsuback_v4(mqtt_reader) {
|
|
293
|
+
mqtt_reader.reasons('unsuback',
|
|
294
|
+
// MQTT 3.1.1
|
|
295
|
+
[ 0x00, 'Success'],
|
|
296
|
+
[ 0x11, 'No subscription existed'],
|
|
297
|
+
[ 0x80, 'Unspecified error'],
|
|
298
|
+
[ 0x83, 'Implementation specific error'],
|
|
299
|
+
[ 0x87, 'Not authorized'],
|
|
300
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
301
|
+
[ 0x91, 'Packet Identifier in use'],
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function mqtt_decode_pingxxx(ns) {
|
|
306
|
+
return ns[0xc] = ns[0xd] = pkt => pkt
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function mqtt_decode_disconnect(ns, mqtt_reader) {
|
|
310
|
+
return ns[0xe] = (pkt, u8_body) => {
|
|
311
|
+
if (u8_body && 5 <= pkt.mqtt_level) {
|
|
312
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
313
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
314
|
+
pkt.props = rdr.props();
|
|
315
|
+
}
|
|
316
|
+
return pkt }
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function mqtt_encode_connect(ns, mqtt_writer) {
|
|
320
|
+
const _c_mqtt_proto = new Uint8Array([
|
|
321
|
+
0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
|
|
322
|
+
|
|
323
|
+
const _enc_flags_connect = flags => 0
|
|
324
|
+
| ( flags.reserved ? 0x01 : 0 )
|
|
325
|
+
| ( (flags.will_qos & 0x3) << 3 )
|
|
326
|
+
| ( flags.clean_start ? 0x02 : 0 )
|
|
327
|
+
| ( flags.will_flag ? 0x04 : 0 )
|
|
328
|
+
| ( flags.will_retain ? 0x20 : 0 )
|
|
329
|
+
| ( flags.password ? 0x40 : 0 )
|
|
330
|
+
| ( flags.username ? 0x80 : 0 );
|
|
331
|
+
|
|
332
|
+
const _enc_flags_will = will => 0x4
|
|
333
|
+
| ( (will.qos & 0x3) << 3 )
|
|
334
|
+
| ( will.retain ? 0x20 : 0 );
|
|
335
|
+
|
|
336
|
+
return ns.connect = ( mqtt_level, pkt ) => {
|
|
337
|
+
let wrt = mqtt_writer.of(pkt);
|
|
338
|
+
|
|
339
|
+
wrt.push(_c_mqtt_proto);
|
|
340
|
+
wrt.u8( mqtt_level );
|
|
341
|
+
|
|
342
|
+
let {will, username, password} = pkt;
|
|
343
|
+
let flags = wrt.flags(
|
|
344
|
+
pkt.flags,
|
|
345
|
+
_enc_flags_connect,
|
|
346
|
+
0 | (username ? 0x80 : 0)
|
|
347
|
+
| (password ? 0x40 : 0)
|
|
348
|
+
| (will ? _enc_flags_will(will) : 0) );
|
|
349
|
+
|
|
350
|
+
wrt.u16(pkt.keep_alive);
|
|
351
|
+
|
|
352
|
+
if (5 <= mqtt_level)
|
|
353
|
+
wrt.props(pkt.props);
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
wrt.utf8(pkt.client_id);
|
|
357
|
+
if (flags & 0x04) { // .will_flag
|
|
358
|
+
if (5 <= mqtt_level)
|
|
359
|
+
wrt.props(will.props);
|
|
360
|
+
|
|
361
|
+
wrt.utf8(will.topic);
|
|
362
|
+
wrt.bin(will.payload);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (flags & 0x80) // .username
|
|
366
|
+
wrt.utf8(username);
|
|
367
|
+
|
|
368
|
+
if (flags & 0x40) // .password
|
|
369
|
+
wrt.bin(password);
|
|
370
|
+
|
|
371
|
+
return wrt.as_pkt(0x10)
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function mqtt_encode_publish(ns, mqtt_writer) {
|
|
376
|
+
return ns.publish = ( mqtt_level, pkt ) => {
|
|
377
|
+
let qos = (pkt.qos & 0x3) << 1;
|
|
378
|
+
let wrt = mqtt_writer.of(pkt);
|
|
379
|
+
|
|
380
|
+
wrt.utf8(pkt.topic);
|
|
381
|
+
if (0 !== qos)
|
|
382
|
+
wrt.u16(pkt.pkt_id);
|
|
383
|
+
|
|
384
|
+
if ( 5 <= mqtt_level) {
|
|
385
|
+
wrt.props(pkt.props);
|
|
386
|
+
wrt.flush(pkt.payload);
|
|
387
|
+
} else {
|
|
388
|
+
wrt.flush(pkt.payload);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return wrt.as_pkt(
|
|
392
|
+
0x30 | qos | (pkt.dup ? 0x8 : 0) | (pkt.retain ? 0x1 : 0) )
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function mqtt_encode_puback(ns, mqtt_writer) {
|
|
397
|
+
return ns.puback = ( mqtt_level, pkt ) => {
|
|
398
|
+
let wrt = mqtt_writer.of(pkt);
|
|
399
|
+
|
|
400
|
+
wrt.u16(pkt.pkt_id);
|
|
401
|
+
if (5 <= mqtt_level) {
|
|
402
|
+
wrt.reason(pkt.reason);
|
|
403
|
+
wrt.props(pkt.props);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return wrt.as_pkt(0x40)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function mqtt_encode_subscribe(ns, mqtt_writer) {
|
|
411
|
+
const _enc_subscribe_flags = opts => 0
|
|
412
|
+
| ( opts.qos & 0x3 )
|
|
413
|
+
| ( opts.retain ? 0x4 : 0 )
|
|
414
|
+
| ( (opts.retain_handling & 0x3) << 2 );
|
|
415
|
+
|
|
416
|
+
return ns.subscribe = ( mqtt_level, pkt ) => {
|
|
417
|
+
let wrt = mqtt_writer.of(pkt);
|
|
418
|
+
|
|
419
|
+
wrt.u16(pkt.pkt_id);
|
|
420
|
+
if (5 <= mqtt_level)
|
|
421
|
+
wrt.props(pkt.props);
|
|
422
|
+
|
|
423
|
+
let f0 = _enc_subscribe_flags(pkt);
|
|
424
|
+
for (let each of pkt.topics) {
|
|
425
|
+
if ('string' === typeof each) {
|
|
426
|
+
wrt.utf8(each);
|
|
427
|
+
wrt.u8(f0);
|
|
428
|
+
} else {
|
|
429
|
+
let [topic, opts] =
|
|
430
|
+
Array.isArray(each) ? each
|
|
431
|
+
: [each.topic, each.opts];
|
|
432
|
+
|
|
433
|
+
wrt.utf8(topic);
|
|
434
|
+
if (undefined === opts) wrt.u8(f0);
|
|
435
|
+
else wrt.flags(opts, _enc_subscribe_flags);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return wrt.as_pkt(0x82)
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function mqtt_encode_unsubscribe(ns, mqtt_writer) {
|
|
444
|
+
return ns.unsubscribe = ( mqtt_level, pkt ) => {
|
|
445
|
+
let wrt = mqtt_writer.of(pkt);
|
|
446
|
+
|
|
447
|
+
wrt.u16(pkt.pkt_id);
|
|
448
|
+
if (5 <= mqtt_level)
|
|
449
|
+
wrt.props(pkt.props);
|
|
450
|
+
|
|
451
|
+
for (let topic of pkt.topics)
|
|
452
|
+
wrt.utf8(topic);
|
|
453
|
+
|
|
454
|
+
return wrt.as_pkt(0xa2)
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function mqtt_encode_pingxxx(ns) {
|
|
459
|
+
ns.pingreq = () => new Uint8Array([ 0xc0, 0 ]);
|
|
460
|
+
ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function mqtt_encode_disconnect(ns, mqtt_writer) {
|
|
464
|
+
return ns.disconnect = ( mqtt_level, pkt ) => {
|
|
465
|
+
let wrt = mqtt_writer.of(pkt);
|
|
466
|
+
|
|
467
|
+
if (pkt && 5 <= mqtt_level) {
|
|
468
|
+
if (pkt.reason || pkt.props) {
|
|
469
|
+
wrt.reason(pkt.reason);
|
|
470
|
+
wrt.props(pkt.props);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return wrt.as_pkt(0xe0)
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// not a v4 packet: import { mqtt_encode_auth } from './encode/auth.js'
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
const mqtt_decode_v4 = [
|
|
482
|
+
mqtt_decode_connack,
|
|
483
|
+
mqtt_decode_publish,
|
|
484
|
+
mqtt_decode_puback,
|
|
485
|
+
mqtt_decode_suback,
|
|
486
|
+
mqtt_decode_unsuback,
|
|
487
|
+
mqtt_decode_pingxxx,
|
|
488
|
+
mqtt_decode_disconnect,
|
|
489
|
+
];
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
const mqtt_encode_v4 = [
|
|
493
|
+
mqtt_encode_connect,
|
|
494
|
+
mqtt_encode_puback,
|
|
495
|
+
mqtt_encode_publish,
|
|
496
|
+
mqtt_encode_subscribe,
|
|
497
|
+
mqtt_encode_unsubscribe,
|
|
498
|
+
mqtt_encode_pingxxx,
|
|
499
|
+
mqtt_encode_disconnect,
|
|
500
|
+
];
|
|
501
|
+
|
|
502
|
+
const mqtt_reader_v4 = /* #__PURE__ */
|
|
503
|
+
mqtt_reader_info(
|
|
504
|
+
mqtt_reader_v4$1,
|
|
505
|
+
_connack_v4,
|
|
506
|
+
_suback_v4,
|
|
507
|
+
_unsuback_v4,
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
const mqtt_opts_v4 =
|
|
511
|
+
{ decode_fns: mqtt_decode_v4,
|
|
512
|
+
mqtt_reader: mqtt_reader_v4,
|
|
513
|
+
encode_fns: mqtt_encode_v4,
|
|
514
|
+
mqtt_writer: mqtt_writer_v4, };
|
|
515
|
+
|
|
516
|
+
function parse(str, loose) {
|
|
517
|
+
if (str instanceof RegExp) return { keys:false, pattern:str };
|
|
518
|
+
var c, o, tmp, ext, keys=[], pattern='', arr = str.split('/');
|
|
519
|
+
arr[0] || arr.shift();
|
|
520
|
+
|
|
521
|
+
while (tmp = arr.shift()) {
|
|
522
|
+
c = tmp[0];
|
|
523
|
+
if (c === '*') {
|
|
524
|
+
keys.push('wild');
|
|
525
|
+
pattern += '/(.*)';
|
|
526
|
+
} else if (c === ':') {
|
|
527
|
+
o = tmp.indexOf('?', 1);
|
|
528
|
+
ext = tmp.indexOf('.', 1);
|
|
529
|
+
keys.push( tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length) );
|
|
530
|
+
pattern += !!~o && !~ext ? '(?:/([^/]+?))?' : '/([^/]+?)';
|
|
531
|
+
if (!!~ext) pattern += (!!~o ? '?' : '') + '\\' + tmp.substring(ext);
|
|
532
|
+
} else {
|
|
533
|
+
pattern += '/' + tmp;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return {
|
|
538
|
+
keys: keys,
|
|
539
|
+
pattern: new RegExp('^' + pattern + (loose ? '(?=$|\/)' : '\/?$'), 'i')
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/*
|
|
544
|
+
class AbstractTopicRouter ::
|
|
545
|
+
async invoke(pkt, ctx) ::
|
|
546
|
+
add(topic_route, ...args) ::
|
|
547
|
+
remove(topic_route, priority) ::
|
|
548
|
+
clear(priority) ::
|
|
549
|
+
find(topic) :: // optional
|
|
550
|
+
mqtt_topic(topic_route)
|
|
551
|
+
*/
|
|
552
|
+
|
|
553
|
+
const with_topic_router = mqtt_topic_router =>
|
|
554
|
+
MQTTKlass => class extends MQTTKlass {
|
|
555
|
+
static _aliases() {
|
|
556
|
+
return super._aliases() +
|
|
557
|
+
' sub_topic:subscribe_topic unsub_topic:unsubscribe_topic'}
|
|
558
|
+
|
|
559
|
+
_init_router(opt, self, target) {
|
|
560
|
+
this._subs = [];
|
|
561
|
+
let router = this.router = target.router =
|
|
562
|
+
mqtt_topic_router(opt, this, target);
|
|
563
|
+
return router?.invoke}
|
|
564
|
+
|
|
565
|
+
on_sub(suback, pkt) {
|
|
566
|
+
suback.pkt = pkt;
|
|
567
|
+
this._subs.push(suback);
|
|
568
|
+
return suback}
|
|
569
|
+
subs_settled() {
|
|
570
|
+
return Promise.allSettled(
|
|
571
|
+
this._subs.splice(0,Infinity)) }
|
|
572
|
+
|
|
573
|
+
// alias: sub_topic
|
|
574
|
+
subscribe_topic(topic_route, ...args) {
|
|
575
|
+
let router = this.router;
|
|
576
|
+
router.add(topic_route, true, args.pop() );// handler
|
|
577
|
+
let topic = router.mqtt_topic(topic_route);
|
|
578
|
+
this.subscribe(topic, ...args );// ex, topic_prefix
|
|
579
|
+
return this }// fluent api -- return this and track side effects
|
|
580
|
+
|
|
581
|
+
// alias: unsub_topic
|
|
582
|
+
unsubscribe_topic(topic_route, ...args) {
|
|
583
|
+
let router = this.router;
|
|
584
|
+
router.remove(topic_route, true);
|
|
585
|
+
let topic = router.mqtt_topic(topic_route);
|
|
586
|
+
return this.unsubscribe(topic, ...args ) }// topic_prefix
|
|
587
|
+
|
|
588
|
+
// add topic handlers without corresponding subscribe packet
|
|
589
|
+
on_topic(...args) {
|
|
590
|
+
this.router.add(...args);
|
|
591
|
+
return this} };
|
|
592
|
+
|
|
593
|
+
// Use [regexparam][] for url-like topic parsing
|
|
594
|
+
// [regexparam]: https://github.com/lukeed/regexparam
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
const with_topic_path_router = /* #__PURE__ */
|
|
598
|
+
with_topic_router(mqtt_topic_path_router);
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
const mqtt_topic = topic_route =>
|
|
602
|
+
topic_route
|
|
603
|
+
.replace(/[*].*$/, '#')
|
|
604
|
+
.replace(/:\w[^\/]*/g, '+');
|
|
605
|
+
|
|
606
|
+
/* From the [MQTT v5 Spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Topic_Names_and)
|
|
607
|
+
4.7.1.2 Multi-level wildcard -- (‘#’ U+0023)
|
|
608
|
+
... MUST be specified either on its own or following a topic level separator.
|
|
609
|
+
In either case it MUST be the last character specified in the Topic Filter
|
|
610
|
+
|
|
611
|
+
4.7.1.3 Single-level wildcard -- (‘+’ U+002B)
|
|
612
|
+
...it MUST occupy an entire level of the filter.
|
|
613
|
+
*/
|
|
614
|
+
|
|
615
|
+
const as_topic_path = (topic_route, id) =>(
|
|
616
|
+
id=1,
|
|
617
|
+
topic_route
|
|
618
|
+
.replace(/#$/, '*' )// replace MQTT '#' multi-level wildcard at end
|
|
619
|
+
.replace(/\+/g, () => `:$${id++}` ) );// replace MQTT '+' single-level wildcards
|
|
620
|
+
|
|
621
|
+
function _ignore(pkt, params, ctx) {ctx.done = true;}
|
|
622
|
+
|
|
623
|
+
function mqtt_topic_path_router() {
|
|
624
|
+
let pri_lsts = [[],[]], rm = Symbol();
|
|
625
|
+
let find = topic => _routes_iter(pri_lsts, topic);
|
|
626
|
+
|
|
627
|
+
// return duck-type compatible with AbstractTopicRouter in ./with_topic_router
|
|
628
|
+
return {find, mqtt_topic,
|
|
629
|
+
add(topic_route, ...args) {
|
|
630
|
+
let fn = args.pop();
|
|
631
|
+
let priority = args.pop();
|
|
632
|
+
|
|
633
|
+
if ('function' !== typeof fn) {
|
|
634
|
+
if (fn) {throw new TypeError()}
|
|
635
|
+
fn = _ignore;}
|
|
636
|
+
|
|
637
|
+
let rte = parse(as_topic_path(topic_route));
|
|
638
|
+
|
|
639
|
+
rte.key = topic_route;
|
|
640
|
+
rte.tgt = fn;
|
|
641
|
+
pri_lsts[priority ? 0 : 1].push(rte);
|
|
642
|
+
return this}
|
|
643
|
+
|
|
644
|
+
, remove(topic_route, priority) {
|
|
645
|
+
let lst = pri_lsts[priority ? 0 : 1];
|
|
646
|
+
return _route_remove([lst], topic_route)}
|
|
647
|
+
|
|
648
|
+
, clear(priority) {
|
|
649
|
+
pri_lsts[priority ? 0 : 1] = [];
|
|
650
|
+
if (null == priority) {
|
|
651
|
+
pri_lsts[1] = []; } }// null clears both lists
|
|
652
|
+
|
|
653
|
+
, async invoke(pkt, ctx) {
|
|
654
|
+
ctx.idx = 0;
|
|
655
|
+
ctx.rm = rm;
|
|
656
|
+
|
|
657
|
+
for (let [fn, params] of find(pkt.topic)) {
|
|
658
|
+
let res = await fn(pkt, params, ctx);
|
|
659
|
+
|
|
660
|
+
if (rm === res) {
|
|
661
|
+
_route_remove(pri_lsts, fn);}
|
|
662
|
+
|
|
663
|
+
if (ctx.done) {
|
|
664
|
+
break}
|
|
665
|
+
else ctx.idx++;}
|
|
666
|
+
|
|
667
|
+
if (1 === pkt.qos) {
|
|
668
|
+
await ctx.mqtt.puback(pkt);} } } }
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
function * _routes_iter(all_route_lists, topic) {
|
|
672
|
+
topic = topic.replace(/^[\/]*/, '/'); // ensure '/' prefix for regexparam library
|
|
673
|
+
for (let route_list of all_route_lists) {
|
|
674
|
+
for (let {keys, pattern, tgt} of route_list) {
|
|
675
|
+
let match = pattern.exec(topic);
|
|
676
|
+
if (match) {
|
|
677
|
+
let params = keys
|
|
678
|
+
? keys.reduce(
|
|
679
|
+
(o, k, i) => (o[k] = match[1+i], o)
|
|
680
|
+
, {})
|
|
681
|
+
: match.groups ?? match;
|
|
682
|
+
yield [tgt, params];} } } }
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
function _route_remove(all_route_lists, query) {
|
|
686
|
+
let fn_match = route =>(
|
|
687
|
+
route===query
|
|
688
|
+
|| route.tgt===query
|
|
689
|
+
|| route.key===query);
|
|
690
|
+
for (let lst of all_route_lists) {
|
|
691
|
+
let i = lst.findIndex(fn_match);
|
|
692
|
+
if (0 <= i) {
|
|
693
|
+
lst.splice(i,1);
|
|
694
|
+
return true} }
|
|
695
|
+
return false}
|
|
696
|
+
|
|
697
|
+
/*
|
|
698
|
+
export function decode_varint_loop(u8, i=0) {
|
|
699
|
+
let i0 = i
|
|
700
|
+
let shift = 0, n = (u8[i] & 0x7f)
|
|
701
|
+
while ( 0x80 & u8[i++] )
|
|
702
|
+
n |= (u8[i] & 0x7f) << (shift += 7)
|
|
703
|
+
|
|
704
|
+
return [n, i, i0]
|
|
705
|
+
}
|
|
706
|
+
*/
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
function decode_varint(u8, i=0) {
|
|
710
|
+
let i0 = i;
|
|
711
|
+
// unrolled for a max of 4 chains
|
|
712
|
+
let n = (u8[i] & 0x7f) << 0;
|
|
713
|
+
if ( 0x80 & u8[i++] ) {
|
|
714
|
+
n |= (u8[i] & 0x7f) << 7;
|
|
715
|
+
if ( 0x80 & u8[i++] ) {
|
|
716
|
+
n |= (u8[i] & 0x7f) << 14;
|
|
717
|
+
if ( 0x80 & u8[i++] ) {
|
|
718
|
+
n |= (u8[i] & 0x7f) << 21;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
return [n, i, i0]
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
function mqtt_raw_dispatch(opt) {
|
|
726
|
+
let u8 = new Uint8Array(0);
|
|
727
|
+
return u8_buf => {
|
|
728
|
+
u8 = 0 === u8.byteLength
|
|
729
|
+
? u8_buf : _u8_join(u8, u8_buf);
|
|
730
|
+
|
|
731
|
+
let res = [];
|
|
732
|
+
while (1) {
|
|
733
|
+
let [len_body, len_vh] = decode_varint(u8, 1);
|
|
734
|
+
let len_pkt = len_body + len_vh;
|
|
735
|
+
|
|
736
|
+
if ( u8.byteLength < len_pkt )
|
|
737
|
+
return res
|
|
738
|
+
|
|
739
|
+
let b0 = u8[0];
|
|
740
|
+
let u8_body = 0 === len_body ? null
|
|
741
|
+
: u8.subarray(len_vh, len_pkt);
|
|
742
|
+
|
|
743
|
+
u8 = u8.subarray(len_pkt);
|
|
744
|
+
|
|
745
|
+
let pkt = opt.decode_pkt(b0, u8_body, opt);
|
|
746
|
+
if (null != pkt)
|
|
747
|
+
res.push( pkt );
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
function _u8_join(a, b) {
|
|
753
|
+
let alen = a.byteLength, r = new Uint8Array(alen + b.byteLength);
|
|
754
|
+
r.set(a, 0);
|
|
755
|
+
r.set(b, alen);
|
|
756
|
+
return r
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
|
|
760
|
+
|
|
761
|
+
function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
|
|
762
|
+
pkt_ctx = {
|
|
763
|
+
__proto__: pkt_ctx || opts.pkt_ctx,
|
|
764
|
+
mqtt_level,
|
|
765
|
+
get hdr() { return this.b0 & 0xf },
|
|
766
|
+
get id() { return this.b0 >>> 4 },
|
|
767
|
+
get type() { return _pkt_types[this.b0 >>> 4] },
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
let op, _decode_by_id=[], _encode_by_type={};
|
|
771
|
+
for (op of opts.encode_fns)
|
|
772
|
+
op(_encode_by_type, opts.mqtt_writer);
|
|
773
|
+
for (op of opts.decode_fns)
|
|
774
|
+
op(_decode_by_id, opts.mqtt_reader);
|
|
775
|
+
|
|
776
|
+
return {
|
|
777
|
+
pkt_ctx,
|
|
778
|
+
|
|
779
|
+
encode_pkt(type, pkt) {
|
|
780
|
+
return _encode_by_type[type]( mqtt_level, pkt ) },
|
|
781
|
+
|
|
782
|
+
decode_pkt(b0, u8_body) {
|
|
783
|
+
let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
|
|
784
|
+
return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
|
|
785
|
+
|
|
786
|
+
mqtt_stream() {
|
|
787
|
+
let self = { __proto__: this, pkt_ctx: { __proto__: pkt_ctx } };
|
|
788
|
+
self.pkt_ctx._base_ = self.pkt_ctx;
|
|
789
|
+
self.decode = mqtt_raw_dispatch(self);
|
|
790
|
+
return self
|
|
791
|
+
},
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function ao_defer_ctx(as_res = (...args) => args) {
|
|
796
|
+
let y,n,_pset = (a,b) => { y=a, n=b; };
|
|
797
|
+
return p =>(
|
|
798
|
+
p = new Promise(_pset)
|
|
799
|
+
, as_res(p, y, n)) }
|
|
800
|
+
|
|
801
|
+
const ao_defer_v = /* #__PURE__ */
|
|
802
|
+
ao_defer_ctx();
|
|
803
|
+
|
|
804
|
+
Promise.resolve({type:'init'});
|
|
805
|
+
|
|
806
|
+
function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
807
|
+
let _q_init = ao_defer_v(), _q_ready = ao_defer_v();
|
|
808
|
+
let _send_ready = async (...args) => (await _q_ready[0])(...args);
|
|
809
|
+
let _send_mqtt_pkt, _has_connected;
|
|
810
|
+
client._send = _send_ready;
|
|
811
|
+
|
|
812
|
+
return {
|
|
813
|
+
async when_ready() {await _q_ready[0];}
|
|
814
|
+
|
|
815
|
+
, ping: _ping_interval (() =>_send_mqtt_pkt?.('pingreq'))
|
|
816
|
+
|
|
817
|
+
, reset(err) {
|
|
818
|
+
if (! _send_mqtt_pkt) {return}
|
|
819
|
+
|
|
820
|
+
if (err) {
|
|
821
|
+
_q_init[2](err);}
|
|
822
|
+
|
|
823
|
+
_send_mqtt_pkt = null;
|
|
824
|
+
_q_init = ao_defer_v();
|
|
825
|
+
client._send = _send_ready;
|
|
826
|
+
|
|
827
|
+
// call client.on_conn_reset in next promise microtask
|
|
828
|
+
client.conn_emit('on_disconnect', false===err, err);}
|
|
829
|
+
|
|
830
|
+
, async send_connect(... args) {
|
|
831
|
+
if (! _send_mqtt_pkt) {
|
|
832
|
+
await _q_init[0]; }// _send_mqtt_pkt is set before fulfilled
|
|
833
|
+
|
|
834
|
+
// await connack response
|
|
835
|
+
let res = await _send_mqtt_pkt(...args);
|
|
836
|
+
if (0 == res[0].reason) {
|
|
837
|
+
_has_connected = true;
|
|
838
|
+
// resolve _q_ready[0] with _send_mqtt_pkt closure
|
|
839
|
+
_q_ready[1](client._send = _send_mqtt_pkt);
|
|
840
|
+
_q_ready = ao_defer_v();
|
|
841
|
+
client.conn_emit('on_ready');}
|
|
842
|
+
|
|
843
|
+
return res}
|
|
844
|
+
|
|
845
|
+
, is_set: (() =>!! _send_mqtt_pkt)
|
|
846
|
+
, set(mqtt_ctx, send_u8_pkt) {
|
|
847
|
+
if (_send_mqtt_pkt) {
|
|
848
|
+
throw new Error('Already connected')}
|
|
849
|
+
|
|
850
|
+
mqtt_ctx = mqtt_ctx.mqtt_stream();
|
|
851
|
+
let sess_ctx = {mqtt: client};
|
|
852
|
+
let on_mqtt_chunk = u8_buf =>
|
|
853
|
+
on_mqtt(mqtt_ctx.decode(u8_buf), sess_ctx);
|
|
854
|
+
|
|
855
|
+
_send_mqtt_pkt = async (type, pkt, key) => {
|
|
856
|
+
let res = undefined !== key
|
|
857
|
+
? pkt_future(key) : true;
|
|
858
|
+
|
|
859
|
+
await send_u8_pkt(
|
|
860
|
+
mqtt_ctx.encode_pkt(type, pkt));
|
|
861
|
+
|
|
862
|
+
return res};
|
|
863
|
+
|
|
864
|
+
_q_init[1](_send_mqtt_pkt); // resolve _q_init with _send_mqtt_pkt closure
|
|
865
|
+
|
|
866
|
+
// call client.on_live in next promise microtask
|
|
867
|
+
client.conn_emit('on_live', _has_connected);
|
|
868
|
+
return on_mqtt_chunk} } }
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
function _ping_interval(send_ping) {
|
|
872
|
+
let tid;
|
|
873
|
+
return (( td ) => {
|
|
874
|
+
tid = clearInterval(tid);
|
|
875
|
+
if (td) {
|
|
876
|
+
tid = setInterval(send_ping, 1000 * td);
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
// ensure the interval allows the NodeJS event loop to exit
|
|
882
|
+
tid.unref?.();
|
|
883
|
+
return true} }) }
|
|
884
|
+
|
|
885
|
+
const _mqtt_cmdid_dispatch ={
|
|
886
|
+
create(target) {
|
|
887
|
+
return {__proto__: this, target, hashbelt: [new Map()]} }
|
|
888
|
+
|
|
889
|
+
, bind_pkt_future(_pkt_id=100) {
|
|
890
|
+
let {hashbelt} = this;
|
|
891
|
+
|
|
892
|
+
let _tmp_; // use _tmp_ to reuse _by_key closure
|
|
893
|
+
let _by_key = answer_monad =>
|
|
894
|
+
hashbelt[0].set(_tmp_, answer_monad);
|
|
895
|
+
|
|
896
|
+
return (( pkt_or_key ) => {
|
|
897
|
+
if ('string' === typeof pkt_or_key) {
|
|
898
|
+
_tmp_ = pkt_or_key;}
|
|
899
|
+
else {
|
|
900
|
+
_pkt_id = (_pkt_id + 1) & 0xffff;
|
|
901
|
+
_tmp_ = pkt_or_key.pkt_id = _pkt_id;}
|
|
902
|
+
|
|
903
|
+
return new Promise(_by_key)}) }
|
|
904
|
+
|
|
905
|
+
, answer(key, pkt) {
|
|
906
|
+
for (let map of this.hashbelt) {
|
|
907
|
+
let answer_monad = map.get(key);
|
|
908
|
+
if (undefined !== answer_monad) {
|
|
909
|
+
map.delete(key);
|
|
910
|
+
|
|
911
|
+
answer_monad([pkt, /*err*/]); // option/maybe monad
|
|
912
|
+
return true} }
|
|
913
|
+
return false}
|
|
914
|
+
|
|
915
|
+
, rotate_belt(n) {
|
|
916
|
+
let {hashbelt} = this;
|
|
917
|
+
hashbelt.unshift(new Map());
|
|
918
|
+
for (let old of hashbelt.splice(n || 5)) {
|
|
919
|
+
for (let answer_monad of old.values()) {
|
|
920
|
+
answer_monad([/*pkt*/, 'expired']); } } }// option/maybe monad
|
|
921
|
+
|
|
922
|
+
, cmdids: ((() => {
|
|
923
|
+
return [
|
|
924
|
+
(() =>{} )// 0x0 reserved
|
|
925
|
+
, by_evt // 0x1 connect
|
|
926
|
+
, by_type // 0x2 connack
|
|
927
|
+
, by_evt // 0x3 publish
|
|
928
|
+
, by_id // 0x4 puback
|
|
929
|
+
, by_id // 0x5 pubrec
|
|
930
|
+
, by_id // 0x6 pubrel
|
|
931
|
+
, by_id // 0x7 pubcomp
|
|
932
|
+
, by_evt // 0x8 subscribe
|
|
933
|
+
, by_id // 0x9 suback
|
|
934
|
+
, by_evt // 0xa unsubscribe
|
|
935
|
+
, by_id // 0xb unsuback
|
|
936
|
+
, by_type // 0xc pingreq
|
|
937
|
+
, by_type // 0xd pingresp
|
|
938
|
+
, by_evt // 0xe disconnect
|
|
939
|
+
, by_type ]// 0xf auth
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
function by_id(disp, pkt) {
|
|
943
|
+
disp.answer(pkt.pkt_id, pkt); }
|
|
944
|
+
|
|
945
|
+
function by_type(disp, pkt, ctx) {
|
|
946
|
+
disp.answer(pkt.type, pkt);
|
|
947
|
+
by_evt(disp, pkt, ctx);}
|
|
948
|
+
|
|
949
|
+
async function by_evt({target}, pkt, ctx) {
|
|
950
|
+
let fn = target[`mqtt_${pkt.type}`]
|
|
951
|
+
|| target.mqtt_pkt;
|
|
952
|
+
|
|
953
|
+
await fn?.call(target, pkt, ctx);} })()) };
|
|
954
|
+
|
|
955
|
+
/*
|
|
956
|
+
on_mqtt_type = {
|
|
957
|
+
mqtt_auth(pkt, ctx) ::
|
|
958
|
+
mqtt_connect(pkt, ctx) ::
|
|
959
|
+
mqtt_connack(pkt, ctx) ::
|
|
960
|
+
mqtt_disconnect(pkt, ctx) ::
|
|
961
|
+
|
|
962
|
+
mqtt_publish(pkt, ctx)
|
|
963
|
+
mqtt_subscribe(pkt, ctx) ::
|
|
964
|
+
mqtt_unsubscribe(pkt, ctx) ::
|
|
965
|
+
|
|
966
|
+
mqtt_pingreq(pkt, ctx) ::
|
|
967
|
+
mqtt_pingresp(pkt, ctx) ::
|
|
968
|
+
}
|
|
969
|
+
*/
|
|
970
|
+
|
|
971
|
+
function _mqtt_dispatch(opt, target) {
|
|
972
|
+
let _disp_ = _mqtt_cmdid_dispatch.create(target);
|
|
973
|
+
let { cmdids } = _disp_;
|
|
974
|
+
|
|
975
|
+
// default rotate at 1s across 5 buckets
|
|
976
|
+
let { td: rotate_td=1000, n: rotate_n=5 } =
|
|
977
|
+
opt && opt.rotate || {};
|
|
978
|
+
|
|
979
|
+
let rotate_ts = rotate_td + Date.now();
|
|
980
|
+
|
|
981
|
+
return [on_mqtt,
|
|
982
|
+
_disp_.bind_pkt_future()]
|
|
983
|
+
|
|
984
|
+
function on_mqtt(pkt_list, ctx) {
|
|
985
|
+
for (let pkt of pkt_list) {
|
|
986
|
+
cmdids[pkt.id](_disp_, pkt, ctx); }
|
|
987
|
+
|
|
988
|
+
if (Date.now() > rotate_ts) {
|
|
989
|
+
_disp_.rotate_belt(rotate_n);
|
|
990
|
+
rotate_ts = rotate_td + Date.now();} } }
|
|
991
|
+
|
|
992
|
+
class MQTTError extends Error {
|
|
993
|
+
constructor(mqtt_pkt, reason=mqtt_pkt.reason) {
|
|
994
|
+
super(`[0x${reason.toString(16)}] ${reason.reason}`);
|
|
995
|
+
this.mqtt_pkt = mqtt_pkt;
|
|
996
|
+
this.reason = reason;} }
|
|
997
|
+
|
|
998
|
+
class MQTTBase {
|
|
999
|
+
constructor(opt={}) {
|
|
1000
|
+
this.with(opt);
|
|
1001
|
+
this._conn_ = _mqtt_conn(this,
|
|
1002
|
+
this._init_dispatch(opt, this)); }
|
|
1003
|
+
|
|
1004
|
+
with(fns_ns) {
|
|
1005
|
+
for (let [k,v] of Object.entries(fns_ns)) {
|
|
1006
|
+
if ('function' === typeof v) {this[k] = v;} }
|
|
1007
|
+
return this}
|
|
1008
|
+
|
|
1009
|
+
async conn_emit(evt, arg, err_arg) {
|
|
1010
|
+
this.log_conn?.(evt, arg, err_arg);
|
|
1011
|
+
try {
|
|
1012
|
+
let fn_evt = this[await evt]; // microtask break using `await evt`
|
|
1013
|
+
if (fn_evt) {
|
|
1014
|
+
await fn_evt.call(this, this, arg, err_arg);}
|
|
1015
|
+
else if (err_arg) {throw err_arg} }
|
|
1016
|
+
catch (err) {
|
|
1017
|
+
this.on_error(err, evt);} }
|
|
1018
|
+
|
|
1019
|
+
on_error(err, evt) {
|
|
1020
|
+
console.warn('[[u8-mqtt error: %s]]', evt, err); }
|
|
1021
|
+
|
|
1022
|
+
// Handshaking Packets
|
|
1023
|
+
|
|
1024
|
+
async connect(pkt={}) {
|
|
1025
|
+
let cid = pkt.client_id || this.client_id;
|
|
1026
|
+
if ('string' !== typeof cid) {
|
|
1027
|
+
// see init_client_id implementation in core.jsy
|
|
1028
|
+
pkt.client_id = cid = this.init_client_id(cid);}
|
|
1029
|
+
this.client_id = cid;
|
|
1030
|
+
|
|
1031
|
+
if (null == pkt.keep_alive) {
|
|
1032
|
+
pkt.keep_alive = 60;}
|
|
1033
|
+
|
|
1034
|
+
let res = await this._conn_
|
|
1035
|
+
.send_connect('connect', pkt, 'connack');
|
|
1036
|
+
|
|
1037
|
+
if (0 != res[0].reason) {
|
|
1038
|
+
throw new this.MQTTError(res[0])}
|
|
1039
|
+
|
|
1040
|
+
// TODO: merge with server's keep_alive frequency
|
|
1041
|
+
this._conn_.ping(pkt.keep_alive);
|
|
1042
|
+
return res}
|
|
1043
|
+
|
|
1044
|
+
async disconnect(pkt={}) {
|
|
1045
|
+
let res = await this._send('disconnect', pkt);
|
|
1046
|
+
this._conn_.reset(false);
|
|
1047
|
+
return res}
|
|
1048
|
+
|
|
1049
|
+
auth(pkt={}) {
|
|
1050
|
+
return this._send('auth', pkt, 'auth')}
|
|
1051
|
+
|
|
1052
|
+
ping() {return this._send('pingreq', null, 'pingresp')}
|
|
1053
|
+
puback({pkt_id}) {return this._send('puback', {pkt_id})}
|
|
1054
|
+
|
|
1055
|
+
// alias: sub
|
|
1056
|
+
subscribe(pkt, ex, topic_prefix) {
|
|
1057
|
+
pkt = _as_topics(pkt, ex, topic_prefix);
|
|
1058
|
+
let suback = this._send('subscribe', pkt, pkt);
|
|
1059
|
+
return this.on_sub?.(suback, pkt) ?? suback}
|
|
1060
|
+
|
|
1061
|
+
// alias: unsub
|
|
1062
|
+
unsubscribe(pkt, ex, topic_prefix) {
|
|
1063
|
+
pkt = _as_topics(pkt, ex, topic_prefix);
|
|
1064
|
+
return this._send('unsubscribe', pkt, pkt)}
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
post(topic, payload, pub_opt) {// qos:0
|
|
1068
|
+
return this.pub({topic, payload, qos:0}, pub_opt)}
|
|
1069
|
+
send(topic, payload, pub_opt) {// qos:1
|
|
1070
|
+
return this.pub({topic, payload, qos:1}, pub_opt)}
|
|
1071
|
+
store(topic, payload, pub_opt) {// qos:1, retain: 1
|
|
1072
|
+
return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
|
|
1073
|
+
|
|
1074
|
+
// alias: json_post
|
|
1075
|
+
obj_post(topic, msg, pub_opt) {// qos:0
|
|
1076
|
+
return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
|
|
1077
|
+
// alias: json_send
|
|
1078
|
+
obj_send(topic, msg, pub_opt) {// qos:1
|
|
1079
|
+
return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
|
|
1080
|
+
// alias: json_store
|
|
1081
|
+
obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
|
|
1082
|
+
return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
|
|
1083
|
+
|
|
1084
|
+
// alias: publish -- because 'pub' is shorter for semantic aliases above
|
|
1085
|
+
async pub(pkt, pub_opt) {
|
|
1086
|
+
if (undefined === pkt.payload) {
|
|
1087
|
+
if ('function' === typeof pub_opt) {
|
|
1088
|
+
pub_opt = {fn_encode: pub_opt};}
|
|
1089
|
+
|
|
1090
|
+
let {msg} = pkt;
|
|
1091
|
+
switch (typeof msg) {
|
|
1092
|
+
case 'function':
|
|
1093
|
+
pub_opt = {...pub_opt, fn_encode: msg};
|
|
1094
|
+
// flow into 'undefined' case
|
|
1095
|
+
case 'undefined':
|
|
1096
|
+
// return a single-value closure to publish packets
|
|
1097
|
+
return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
|
|
1098
|
+
|
|
1099
|
+
// Encode payload from msg; fn_encode allows alternative to JSON.stringify
|
|
1100
|
+
let {fn_encode} = pub_opt || {};
|
|
1101
|
+
pkt.payload = fn_encode
|
|
1102
|
+
? await fn_encode(msg)
|
|
1103
|
+
: JSON.stringify(msg);}
|
|
1104
|
+
|
|
1105
|
+
if (pub_opt) {
|
|
1106
|
+
if (pub_opt.props) {
|
|
1107
|
+
pkt.props = pub_opt.props;}
|
|
1108
|
+
if (pub_opt.xform) {
|
|
1109
|
+
pkt = pub_opt.xform(pkt) || pkt;} }
|
|
1110
|
+
|
|
1111
|
+
return this._send('publish', pkt,
|
|
1112
|
+
pkt.qos ? pkt : void 0 ) }// key
|
|
1113
|
+
|
|
1114
|
+
|
|
1115
|
+
// Internal API
|
|
1116
|
+
|
|
1117
|
+
/* async _send(type, pkt) -- provided by _conn_ and transport */
|
|
1118
|
+
|
|
1119
|
+
_init_dispatch(opt) {
|
|
1120
|
+
this.constructor?._once_();
|
|
1121
|
+
let target ={__proto__: opt.on_mqtt_type};
|
|
1122
|
+
target.mqtt_publish ||=
|
|
1123
|
+
this._init_router?.(opt, this, target);
|
|
1124
|
+
return _mqtt_dispatch(this, target)}
|
|
1125
|
+
|
|
1126
|
+
static _aliases() {
|
|
1127
|
+
return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
|
|
1128
|
+
|
|
1129
|
+
static _once_(self=this) {
|
|
1130
|
+
self._once_ = _=>0;
|
|
1131
|
+
let p = self.prototype;
|
|
1132
|
+
p.MQTTError = MQTTError;
|
|
1133
|
+
for (let alias of self._aliases().split(/\s+/)) {
|
|
1134
|
+
alias = alias.split(':');
|
|
1135
|
+
let fn = alias[1] && p[alias[1]];
|
|
1136
|
+
if (fn) {p[alias[0]] = fn;} } } }
|
|
1137
|
+
|
|
1138
|
+
|
|
1139
|
+
function _as_topics(pkt, ex, topic_prefix) {
|
|
1140
|
+
if (ex?.trim) {// string
|
|
1141
|
+
topic_prefix = ex;
|
|
1142
|
+
ex = null;}
|
|
1143
|
+
|
|
1144
|
+
pkt =(
|
|
1145
|
+
pkt.trim // string
|
|
1146
|
+
? {topics:[pkt], ... ex}
|
|
1147
|
+
: pkt[Symbol.iterator]
|
|
1148
|
+
? {topics:[... pkt], ... ex}
|
|
1149
|
+
: ex ? {...pkt, ...ex}
|
|
1150
|
+
: pkt);
|
|
1151
|
+
|
|
1152
|
+
if (topic_prefix) {
|
|
1153
|
+
// particularly useful with shared queues, e.g.
|
|
1154
|
+
// topic_prefix = '$share/some-queue-name/'
|
|
1155
|
+
let _prefix_topics = v =>
|
|
1156
|
+
v.trim ? topic_prefix+v : v.map(_prefix_topics);
|
|
1157
|
+
|
|
1158
|
+
pkt.topics = pkt.topics.map(_prefix_topics);}
|
|
1159
|
+
return pkt}
|
|
1160
|
+
|
|
1161
|
+
const pkt_api = {
|
|
1162
|
+
utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
|
|
1163
|
+
json(u8) { return JSON.parse( this.utf8(u8) || null ) },
|
|
1164
|
+
text(u8) { return this.utf8(u8) },
|
|
1165
|
+
};
|
|
1166
|
+
|
|
1167
|
+
class MQTTCore extends MQTTBase {
|
|
1168
|
+
static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
|
|
1169
|
+
let self = class extends this {};
|
|
1170
|
+
self.prototype.mqtt_ctx =
|
|
1171
|
+
mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
|
|
1172
|
+
return self}
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
// automatic Client Id for connect()
|
|
1176
|
+
init_client_id(parts=['u8-mqtt--','']) {
|
|
1177
|
+
let sess_stg=this.sess_stg;
|
|
1178
|
+
let key, cid = sess_stg?.getItem(key=parts.join(' '));
|
|
1179
|
+
if (! cid) {
|
|
1180
|
+
cid = parts.join(Math.random().toString(36).slice(2));
|
|
1181
|
+
sess_stg?.setItem(key, cid);}
|
|
1182
|
+
return cid}
|
|
1183
|
+
|
|
1184
|
+
get sess_stg() {return globalThis.sessionStorage}
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
//on_error(err, evt) ::
|
|
1188
|
+
// console.warn @ '[[u8-mqtt error: %s]]', evt, err
|
|
1189
|
+
|
|
1190
|
+
//log_conn(evt, arg, err_arg) ::
|
|
1191
|
+
// console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
|
|
1192
|
+
|
|
1193
|
+
on_live(client, is_reconnect) {
|
|
1194
|
+
if (is_reconnect) {
|
|
1195
|
+
return client.connect()} }
|
|
1196
|
+
|
|
1197
|
+
//on_reconnect(client) ::
|
|
1198
|
+
|
|
1199
|
+
_use_conn(fn_reconnect) {
|
|
1200
|
+
return (this.reconnect = fn_reconnect)?.()}
|
|
1201
|
+
with_autoreconnect(opt=2000) {
|
|
1202
|
+
if (opt.toFixed) {opt ={delay: opt};}
|
|
1203
|
+
return this.with({
|
|
1204
|
+
on_reconnect() {
|
|
1205
|
+
this.delay(opt.delay || 2000)
|
|
1206
|
+
.then(this.reconnect)
|
|
1207
|
+
.then(opt.reconnect, opt.error);} }) }
|
|
1208
|
+
|
|
1209
|
+
on_disconnect(client, intentional) {
|
|
1210
|
+
if (! intentional) {
|
|
1211
|
+
return client.on_reconnect?.()} }
|
|
1212
|
+
|
|
1213
|
+
delay(ms) {
|
|
1214
|
+
return new Promise(done => setTimeout(done, ms)) }
|
|
1215
|
+
|
|
1216
|
+
with_async_iter(async_iter, write_u8_pkt) {
|
|
1217
|
+
let on_mqtt_chunk = this._conn_.set(
|
|
1218
|
+
this.mqtt_ctx,
|
|
1219
|
+
write_u8_pkt);
|
|
1220
|
+
|
|
1221
|
+
this._msg_loop = ((async () => {
|
|
1222
|
+
try {
|
|
1223
|
+
async_iter = await async_iter;
|
|
1224
|
+
for await (let chunk of async_iter)
|
|
1225
|
+
on_mqtt_chunk(chunk);
|
|
1226
|
+
this._conn_.reset();}
|
|
1227
|
+
catch (err) {
|
|
1228
|
+
this._conn_.reset(err);} })());
|
|
1229
|
+
|
|
1230
|
+
return this}
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
|
|
1277
|
+
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
with_stream(read_stream, write_stream) {
|
|
1284
|
+
if (undefined === write_stream) {
|
|
1285
|
+
write_stream = read_stream;}
|
|
1286
|
+
|
|
1287
|
+
return this.with_async_iter(read_stream,
|
|
1288
|
+
u8_pkt => write_stream.write(u8_pkt)) }
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
with_websock(websock) {
|
|
1292
|
+
if (! websock?.send) {
|
|
1293
|
+
websock = new URL(websock || 'ws://127.0.0.1:9001');
|
|
1294
|
+
return this._use_conn (() =>
|
|
1295
|
+
this.with_websock(
|
|
1296
|
+
new WebSocket(websock, ['mqtt'])) ) }
|
|
1297
|
+
|
|
1298
|
+
websock.binaryType = 'arraybuffer';
|
|
1299
|
+
|
|
1300
|
+
let ready, {readyState} = websock;
|
|
1301
|
+
if (1 !== readyState) {
|
|
1302
|
+
if (0 !== readyState) {
|
|
1303
|
+
throw new Error('Invalid WebSocket readyState') }
|
|
1304
|
+
|
|
1305
|
+
ready = new Promise(fn => websock.onopen = fn); }
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
let {_conn_} = this;
|
|
1309
|
+
let on_mqtt_chunk = _conn_.set(
|
|
1310
|
+
this.mqtt_ctx,
|
|
1311
|
+
async u8_pkt =>(
|
|
1312
|
+
await ready
|
|
1313
|
+
, websock.send(u8_pkt)) );
|
|
1314
|
+
|
|
1315
|
+
websock.onmessage = evt =>(on_mqtt_chunk(new Uint8Array(evt.data)));
|
|
1316
|
+
websock.onclose = evt => {
|
|
1317
|
+
if (! evt.wasClean) {
|
|
1318
|
+
var err = new Error('websocket connection close');
|
|
1319
|
+
err.code = evt.code;
|
|
1320
|
+
err.reason = evt.reason;}
|
|
1321
|
+
|
|
1322
|
+
_conn_.reset(err);};
|
|
1323
|
+
|
|
1324
|
+
return this} }
|
|
1325
|
+
|
|
1326
|
+
const version = '0.5.0';
|
|
1327
|
+
|
|
1328
|
+
const MQTTClient_v4 = /* #__PURE__ */
|
|
1329
|
+
with_topic_path_router(
|
|
1330
|
+
MQTTCore.mqtt_ctx(4, mqtt_opts_v4) );
|
|
1331
|
+
|
|
1332
|
+
const mqtt_v4 = opt =>
|
|
1333
|
+
new MQTTClient_v4(opt);
|
|
1334
|
+
|
|
1335
|
+
export { MQTTClient_v4 as MQTTClient, MQTTClient_v4, mqtt_v4 as default, mqtt_v4 as mqtt, mqtt_v4, version };
|
|
1336
|
+
//# sourceMappingURL=v4.js.map
|