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