u8-mqtt 0.0.23

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.
@@ -0,0 +1,1372 @@
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(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
+ function _mqtt_raw_pkt_dispatch(decode_raw_pkt) {
40
+ let u8 = new Uint8Array(0);
41
+ return u8_buf => {
42
+ u8 = 0 === u8.byteLength
43
+ ? u8_buf : _u8_join(u8, u8_buf);
44
+
45
+ const res = [];
46
+ while (1) {
47
+ const [len_body, len_vh] = decode_varint(u8, 1);
48
+ const len_pkt = len_body + len_vh;
49
+
50
+ if ( u8.byteLength < len_pkt )
51
+ return res
52
+
53
+ let b0 = u8[0];
54
+ let u8_body = 0 === len_body ? null
55
+ : u8.subarray(len_vh, len_pkt);
56
+
57
+ u8 = u8.subarray(len_pkt);
58
+
59
+ const pkt = decode_raw_pkt(b0, u8_body);
60
+ if (undefined !== pkt && null !== pkt)
61
+ res.push( pkt );
62
+ }
63
+ }
64
+ }
65
+
66
+ function _u8_join(a, b) {
67
+ const alen = a.byteLength;
68
+ const r = new Uint8Array(alen + b.byteLength);
69
+ r.set(a, 0);
70
+ r.set(b, alen);
71
+ return r
72
+ }
73
+
74
+ const mqtt_props = new Map();
75
+
76
+ {
77
+ const entries = [
78
+ [ 0x01, 'u8', 'payload_format_indicator'],
79
+ [ 0x02, 'u32', 'message_expiry_interval'],
80
+ [ 0x03, 'utf8', 'content_type'],
81
+ [ 0x08, 'utf8', 'response_topic'],
82
+ [ 0x09, 'bin', 'correlation_data'],
83
+ [ 0x0B, 'vint', 'subscription_identifier'],
84
+ [ 0x11, 'u32', 'session_expiry_interval'],
85
+ [ 0x12, 'utf8', 'assigned_client_identifier'],
86
+ [ 0x13, 'u16', 'server_keep_alive'],
87
+ [ 0x15, 'utf8', 'authentication_method'],
88
+ [ 0x16, 'bin', 'authentication_data'],
89
+ [ 0x17, 'u8', 'request_problem_information'],
90
+ [ 0x18, 'u32', 'will_delay_interval'],
91
+ [ 0x19, 'u8', 'request_response_information'],
92
+ [ 0x1A, 'utf8', 'response_information'],
93
+ [ 0x1C, 'utf8', 'server_reference'],
94
+ [ 0x1F, 'utf8', 'reason_string'],
95
+ [ 0x21, 'u16', 'receive_maximum'],
96
+ [ 0x22, 'u16', 'topic_alias_maximum'],
97
+ [ 0x23, 'u16', 'topic_alias'],
98
+ [ 0x24, 'u8', 'maximum_qo_s'],
99
+ [ 0x25, 'u8', 'retain_available'],
100
+ [ 0x26, 'pair', 'user_properties', true],
101
+ [ 0x27, 'u32', 'maximum_packet_size'],
102
+ [ 0x28, 'u8', 'wildcard_subscription_available'],
103
+ [ 0x29, 'u8', 'subscription_identifiers_available', true],
104
+ [ 0x2A, 'u8', 'shared_subscription_available'],
105
+ ];
106
+
107
+ for (const [id, type, name, plural] of entries) {
108
+ const prop_obj = {id, type, name};
109
+ if (plural) prop_obj.plural = plural;
110
+ mqtt_props.set(prop_obj.id, prop_obj);
111
+ mqtt_props.set(prop_obj.name, prop_obj);
112
+ }
113
+ }
114
+
115
+ const as_utf8 = u8 =>
116
+ new TextDecoder('utf-8').decode(u8);
117
+
118
+ const step_from = idx =>
119
+ (width, r) => ( r = idx, idx += width, r );
120
+
121
+ class mqtt_type_reader {
122
+ constructor(buf, idx=0) {
123
+ this.buf = buf;
124
+ this.step = step_from(idx);
125
+ }
126
+
127
+ _fork(buf, idx) {
128
+ return { __proto__: this, buf, step: step_from(idx) }
129
+ }
130
+
131
+ has_more() {
132
+ const {buf, step} = this;
133
+ return buf.byteLength > step(0)
134
+ }
135
+
136
+ u8() {
137
+ const {buf, step} = this;
138
+ return buf[step(1)]
139
+ }
140
+
141
+ u16() {
142
+ const {buf, step} = this;
143
+ const i = step(2);
144
+ return (buf[i]<<8) | buf[i+1]
145
+ }
146
+
147
+ u32() {
148
+ const {buf, step} = this;
149
+ const i = step(4);
150
+ return (buf[i]<<24) | (buf[i+1]<<16) | (buf[i+2]<<8) | buf[i+3]
151
+ }
152
+
153
+ vint() {
154
+ const {buf, step} = this;
155
+ const [n, vi, vi0] = decode_varint(buf, step(0));
156
+ step(vi - vi0);
157
+ return n
158
+ }
159
+
160
+ vbuf() {
161
+ const {buf, step} = this;
162
+ const [n, vi, vi0] = decode_varint(buf, step(0));
163
+ step(n + vi - vi0);
164
+ return 0 === n ? null
165
+ : buf.subarray(vi, step(0))
166
+ }
167
+
168
+ bin() {
169
+ const {buf, step} = this;
170
+ const i = step(2);
171
+ const len = (buf[i]<<8) | buf[i+1];
172
+ const i0 = step(len);
173
+ return buf.subarray(i0, i0+len)
174
+ }
175
+
176
+ utf8() { return as_utf8(this.bin()) }
177
+ pair() { return [ as_utf8(this.bin()), as_utf8(this.bin()) ] }
178
+
179
+ u8_flags(FlagsType) {
180
+ const {buf, step} = this;
181
+ return new FlagsType(buf[step(1)])
182
+ }
183
+
184
+ u8_reason(fn_reason) {
185
+ const {buf, step} = this;
186
+ return fn_reason( buf[step(1)] )
187
+ }
188
+
189
+ flush() {
190
+ const {buf, step} = this;
191
+ this.step = this.buf = null;
192
+ return buf.subarray(step(0))
193
+ }
194
+
195
+ props() {
196
+ let sub = this.vbuf();
197
+ return null === sub ? null
198
+ : this._fork(sub, 0)._read_props([])
199
+ }
200
+
201
+ _read_props(lst) {
202
+ while (this.has_more()) {
203
+ let k = this.u8();
204
+ let p = mqtt_props.get( k );
205
+ let v = this[p.type]();
206
+ lst.push([p.name, v]);
207
+ }
208
+ return lst
209
+ }
210
+ }
211
+
212
+
213
+
214
+ class U8_Reason extends Number {
215
+ constructor(u8, reason) { super(u8); this.reason = reason; }
216
+ }
217
+
218
+ function bind_reason_lookup(reason_entries) {
219
+ const reason_map = new Map();
220
+ for (const [u8, reason] of reason_entries)
221
+ reason_map.set( u8, new U8_Reason(u8, reason) );
222
+
223
+ return reason_map.get.bind(reason_map)
224
+ }
225
+
226
+ function mqtt_decode_connack(ns) {
227
+ class _connack_flags_ extends Number {
228
+ get session_present() { return this & 0x01 !== 0 }
229
+ }
230
+
231
+ const _connack_reason_ = bind_reason_lookup([
232
+ // MQTT 3.1.1
233
+ [ 0x00, 'Success'],
234
+ [ 0x01, 'Connection refused, unacceptable protocol version'],
235
+ [ 0x02, 'Connection refused, identifier rejected'],
236
+ [ 0x03, 'Connection refused, server unavailable'],
237
+ [ 0x04, 'Connection refused, bad user name or password'],
238
+ [ 0x05, 'Connection refused, not authorized'],
239
+
240
+ // MQTT 5.0
241
+ [ 0x81, 'Malformed Packet'],
242
+ [ 0x82, 'Protocol Error'],
243
+ [ 0x83, 'Implementation specific error'],
244
+ [ 0x84, 'Unsupported Protocol Version'],
245
+ [ 0x85, 'Client Identifier not valid'],
246
+ [ 0x86, 'Bad User Name or Password'],
247
+ [ 0x87, 'Not authorized'],
248
+ [ 0x88, 'Server unavailable'],
249
+ [ 0x89, 'Server busy'],
250
+ [ 0x8A, 'Banned'],
251
+ [ 0x8C, 'Bad authentication method'],
252
+ [ 0x90, 'Topic Name invalid'],
253
+ [ 0x95, 'Packet too large'],
254
+ [ 0x97, 'Quota exceeded'],
255
+ [ 0x99, 'Payload format invalid'],
256
+ [ 0x9A, 'Retain not supported'],
257
+ [ 0x9B, 'QoS not supported'],
258
+ [ 0x9C, 'Use another server'],
259
+ [ 0x9D, 'Server moved'],
260
+ [ 0x9F, 'Connection rate exceeded'],
261
+ ]);
262
+
263
+
264
+ return ns[0x2] = (pkt, u8_body) => {
265
+ const rdr = new mqtt_type_reader(u8_body, 0);
266
+
267
+ pkt.flags =
268
+ rdr.u8_flags(_connack_flags_);
269
+
270
+ pkt.reason = rdr.u8_reason(_connack_reason_);
271
+ if (5 <= pkt.mqtt_level)
272
+ pkt.props = rdr.props();
273
+ return pkt }
274
+ }
275
+
276
+ function mqtt_decode_publish(ns) {
277
+ return ns[0x3] = (pkt, u8_body) => {
278
+ const {hdr} = pkt;
279
+ pkt.dup = Boolean(hdr & 0x8);
280
+ pkt.retain = Boolean(hdr & 0x1);
281
+ const qos = pkt.qos = (hdr>>1) & 0x3;
282
+
283
+ const rdr = new mqtt_type_reader(u8_body, 0);
284
+ pkt.topic = rdr.utf8();
285
+ if (0 !== qos)
286
+ pkt.pkt_id = rdr.u16();
287
+
288
+ if (5 <= pkt.mqtt_level)
289
+ pkt.props = rdr.props();
290
+
291
+ pkt.payload = rdr.flush();
292
+ return pkt }
293
+ }
294
+
295
+ function mqtt_decode_puback(ns) {
296
+ const _puback_reason_ = bind_reason_lookup([
297
+ [ 0x00, 'Success'],
298
+
299
+ // MQTT 5.0
300
+ [ 0x10, 'No matching subscribers'],
301
+ [ 0x80, 'Unspecified error'],
302
+ [ 0x83, 'Implementation specific error'],
303
+ [ 0x87, 'Not authorized'],
304
+ [ 0x90, 'Topic Name invalid'],
305
+ [ 0x91, 'Packet identifier in use'],
306
+ [ 0x97, 'Quota exceeded'],
307
+ [ 0x99, 'Payload format invalid'],
308
+ ]);
309
+
310
+
311
+ return ns[0x4] = (pkt, u8_body) => {
312
+ const rdr = new mqtt_type_reader(u8_body, 0);
313
+
314
+ pkt.pkt_id = rdr.u16();
315
+ if (5 <= pkt.mqtt_level) {
316
+ pkt.reason = rdr.u8_reason(_puback_reason_);
317
+ pkt.props = rdr.props();
318
+ }
319
+
320
+ return pkt }
321
+ }
322
+
323
+ function mqtt_decode_pubxxx(ns) {
324
+ const _pubxxx_reason_ = bind_reason_lookup([
325
+ [ 0x00, 'Success' ],
326
+ [ 0x92, 'Packet Identifier not found' ],
327
+ ]);
328
+
329
+ return ns[0x5] = ns[0x6] = ns[0x7] = (pkt, u8_body) => {
330
+ const rdr = new mqtt_type_reader(u8_body, 0);
331
+
332
+ pkt.pkt_id = rdr.u16();
333
+ pkt.reason = rdr.u8_reason(_pubxxx_reason_);
334
+ if (5 <= pkt.mqtt_level)
335
+ pkt.props = rdr.props();
336
+ return pkt }
337
+ }
338
+
339
+ function _mqtt_decode_suback(_ack_reason_) {
340
+ return (pkt, u8_body) => {
341
+ const rdr = new mqtt_type_reader(u8_body, 0);
342
+
343
+ pkt.pkt_id = rdr.u16();
344
+ if (5 <= pkt.mqtt_level)
345
+ pkt.props = rdr.props();
346
+
347
+ const answers = pkt.answers = [];
348
+ while (rdr.has_more())
349
+ answers.push(
350
+ rdr.u8_reason(_ack_reason_) );
351
+
352
+ return pkt }
353
+ }
354
+
355
+ function mqtt_decode_suback(ns) {
356
+ const _suback_reason_ = bind_reason_lookup([
357
+ // MQTT 3.1.1
358
+ [ 0x00, 'Granted QoS 0'],
359
+ [ 0x01, 'Granted QoS 1'],
360
+ [ 0x02, 'Granted QoS 2'],
361
+
362
+ // MQTT 5.0
363
+ [ 0x80, 'Unspecified error'],
364
+ [ 0x83, 'Implementation specific error'],
365
+ [ 0x87, 'Not authorized'],
366
+ [ 0x8F, 'Topic Filter invalid'],
367
+ [ 0x91, 'Packet Identifier in use'],
368
+ [ 0x97, 'Quota exceeded'],
369
+ [ 0x9E, 'Shared Subscriptions not supported'],
370
+ [ 0xA1, 'Subscription Identifiers not supported'],
371
+ [ 0xA2, 'Wildcard Subscriptions not supported'],
372
+ ]);
373
+
374
+ return ns[0x9] = _mqtt_decode_suback(_suback_reason_)
375
+ }
376
+
377
+ function mqtt_decode_unsuback(ns) {
378
+ const _unsuback_reason_ = bind_reason_lookup([
379
+ [ 0x00, 'Success'],
380
+ [ 0x11, 'No subscription existed'],
381
+ [ 0x80, 'Unspecified error'],
382
+ [ 0x83, 'Implementation specific error'],
383
+ [ 0x87, 'Not authorized'],
384
+ [ 0x8F, 'Topic Filter invalid'],
385
+ [ 0x91, 'Packet Identifier in use'],
386
+ ]);
387
+
388
+ return ns[0xb] = _mqtt_decode_suback(_unsuback_reason_)
389
+ }
390
+
391
+ function mqtt_decode_pingxxx(ns) {
392
+ return ns[0xc] = ns[0xd] = pkt => pkt
393
+ }
394
+
395
+ function mqtt_decode_auth(ns) {
396
+ const _auth_reason_ = bind_reason_lookup([
397
+ // MQTT 5.0
398
+ [ 0x00, 'Success' ],
399
+ [ 0x18, 'Continue authentication' ],
400
+ [ 0x19, 'Re-authenticate' ],
401
+ ]);
402
+
403
+ return ns[0xf] = (pkt, u8_body) => {
404
+ if ( 5 <= pkt.mqtt_level ) {
405
+ const rdr = new mqtt_type_reader(u8_body, 0);
406
+ pkt.reason = rdr.u8_reason(_auth_reason_);
407
+ pkt.props = rdr.props();
408
+ }
409
+ return pkt }
410
+ }
411
+
412
+ function mqtt_pkt_writer_pool() {
413
+ const _pool_ = [];
414
+ return host =>
415
+ 0 === _pool_.length
416
+ ? mqtt_pkt_writer(host, _pool_)
417
+ : _pool_.pop()(host)
418
+ }
419
+
420
+ function mqtt_pkt_writer(host, _pool_) {
421
+ // avoid GCing push/pull when they can be reused
422
+ let n=0, rope=[];
423
+ return install(host)
424
+
425
+ function install(_host) {
426
+ host = _host;
427
+ host.push = push;
428
+ host.pack = pack;
429
+ }
430
+
431
+ function push(u8) {
432
+ rope.push(u8);
433
+ n += u8.length;
434
+ }
435
+
436
+ function pack(hdr) {
437
+ host = host.push = host.pack = null;
438
+
439
+ const res = _mqtt_pkt_rope(hdr, n, rope);
440
+ n=0; rope=[];
441
+ if (undefined !== _pool_)
442
+ _pool_.push(install);
443
+
444
+ return res
445
+ }
446
+ }
447
+
448
+
449
+ function _mqtt_pkt_rope(hdr, n, rope) {
450
+ const header = encode_varint(n, hdr);
451
+ let i = header.length;
452
+
453
+ const pkt = new Uint8Array(n + i);
454
+ pkt.set(header, 0);
455
+ for (const vec of rope) {
456
+ pkt.set(vec, i);
457
+ i += vec.length;
458
+ }
459
+ return pkt
460
+ }
461
+
462
+ const _is_array = Array.isArray;
463
+ const pack_utf8 = v => new TextEncoder('utf-8').encode(v);
464
+ const pack_u16 = v => [ (v>>>8) & 0xff, v & 0xff ];
465
+ const pack_u32 = v => [ (v>>>24) & 0xff, (v>>>16) & 0xff, (v>>>8) & 0xff, v & 0xff ];
466
+
467
+ class mqtt_type_writer {
468
+ constructor() {
469
+ this._pkt_writer(this);
470
+ }
471
+
472
+ as_pkt(hdr) { return this.pack([hdr]) }
473
+
474
+ u8(v) { this.push([ v & 0xff ]);}
475
+ u16(v) { this.push( pack_u16(v) );}
476
+ u32(v) { this.push( pack_u32(v) );}
477
+ vint(v) { this.push( encode_varint(v) );}
478
+
479
+ _u16_bin(u8_buf) {
480
+ const {push} = this;
481
+ push( pack_u16( u8_buf.byteLength ));
482
+ push( u8_buf );
483
+ }
484
+
485
+ flush(buf) {
486
+ if (null != buf)
487
+ this.push(
488
+ 'string' === typeof buf
489
+ ? pack_utf8(buf) : buf );
490
+
491
+ this.push = false;
492
+ }
493
+
494
+ bin(u8_buf) {
495
+ if (! u8_buf) return this.u16(0)
496
+ if ('string' === typeof u8_buf)
497
+ return this.utf8(u8_buf)
498
+
499
+ if (u8_buf.length !== u8_buf.byteLength)
500
+ u8_buf = new Uint8Array(u8_buf);
501
+ this._u16_bin(u8_buf);
502
+ }
503
+
504
+ utf8(v) { this._u16_bin( new TextEncoder('utf-8').encode(v) ); }
505
+
506
+ pair(k,v) {
507
+ this.utf8(k);
508
+ this.utf8(v);
509
+ }
510
+
511
+ u8_flags(v, enc_flags, b0=0) {
512
+ if (undefined !== v && isNaN(+v))
513
+ v = enc_flags(v, 0);
514
+
515
+ v |= b0;
516
+ this.push([v]);
517
+ return v
518
+ }
519
+
520
+ u8_reason(v) { this.push([v | 0]); }
521
+
522
+ props(props) {
523
+ if (! props)
524
+ return this.u8(0)
525
+
526
+ if (! _is_array(props))
527
+ props = props.entries
528
+ ? Array.from(props.entries())
529
+ : Object.entries(props);
530
+
531
+ if (0 === props.length)
532
+ return this.u8(0)
533
+
534
+ const wrt = this._fork();
535
+ for (const [name, value] of props) {
536
+ const {id, type} = mqtt_props.get(name);
537
+ wrt.u8(id);
538
+ wrt[type](value);
539
+ }
540
+
541
+ this.push(wrt.pack([]));
542
+ }
543
+
544
+ _fork() {
545
+ let self = { __proto__: this };
546
+ this._pkt_writer(self);
547
+ return self
548
+ }
549
+ }
550
+
551
+ mqtt_type_writer.prototype._pkt_writer =
552
+ mqtt_pkt_writer_pool();
553
+
554
+ const _c_mqtt_proto = new Uint8Array([
555
+ 0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
556
+
557
+ function mqtt_encode_connect(ns) {
558
+ const _enc_flags_connect = flags => 0
559
+ | ( flags.reserved ? 0x01 : 0 )
560
+ | ( (flags.will_qos & 0x3) << 3 )
561
+ | ( flags.clean_start ? 0x02 : 0 )
562
+ | ( flags.will_flag ? 0x04 : 0 )
563
+ | ( flags.will_retain ? 0x20 : 0 )
564
+ | ( flags.password ? 0x40 : 0 )
565
+ | ( flags.username ? 0x80 : 0 );
566
+
567
+ const _enc_flags_will = will => 0x4
568
+ | ( (will.qos & 0x3) << 3 )
569
+ | ( will.retain ? 0x20 : 0 );
570
+
571
+ return ns.connect = ( mqtt_level, pkt ) => {
572
+ const wrt = new mqtt_type_writer();
573
+
574
+ wrt.push(_c_mqtt_proto);
575
+ wrt.u8( mqtt_level );
576
+
577
+ const {will, username, password} = pkt;
578
+ const flags = wrt.u8_flags(
579
+ pkt.flags,
580
+ _enc_flags_connect,
581
+ 0 | (username ? 0x80 : 0)
582
+ | (password ? 0x40 : 0)
583
+ | (will ? _enc_flags_will(will) : 0) );
584
+
585
+ wrt.u16(pkt.keep_alive);
586
+
587
+ if (5 <= mqtt_level)
588
+ wrt.props(pkt.props);
589
+
590
+
591
+ wrt.utf8(pkt.client_id);
592
+ if (flags & 0x04) { // .will_flag
593
+ if (5 <= mqtt_level)
594
+ wrt.props(will.props);
595
+
596
+ wrt.utf8(will.topic);
597
+ wrt.bin(will.payload);
598
+ }
599
+
600
+ if (flags & 0x80) // .username
601
+ wrt.utf8(username);
602
+
603
+ if (flags & 0x40) // .password
604
+ wrt.bin(password);
605
+
606
+ return wrt.as_pkt(0x10)
607
+ }
608
+ }
609
+
610
+ function mqtt_encode_publish(ns) {
611
+ return ns.publish = ( mqtt_level, pkt ) => {
612
+ const qos = (pkt.qos & 0x3) << 1;
613
+ const wrt = new mqtt_type_writer();
614
+
615
+ wrt.utf8(pkt.topic);
616
+ if (0 !== qos)
617
+ wrt.u16(pkt.pkt_id);
618
+
619
+ if ( 5 <= mqtt_level) {
620
+ wrt.props(pkt.props);
621
+ wrt.flush(pkt.payload);
622
+ } else {
623
+ wrt.flush(pkt.payload);
624
+ }
625
+
626
+ return wrt.as_pkt(
627
+ 0x30 | qos | (pkt.dup ? 0x8 : 0) | (pkt.retain ? 0x1 : 0) )
628
+ }
629
+ }
630
+
631
+ function mqtt_encode_puback(ns) {
632
+ return ns.puback = ( mqtt_level, pkt ) => {
633
+ const wrt = new mqtt_type_writer();
634
+
635
+ wrt.u16(pkt.pkt_id);
636
+ if (5 <= mqtt_level) {
637
+ wrt.u8_reason(pkt.reason);
638
+ wrt.props(pkt.props);
639
+ }
640
+
641
+ return wrt.as_pkt(0x40)
642
+ }
643
+ }
644
+
645
+ function mqtt_encode_subscribe(ns) {
646
+ const _enc_subscribe_flags = opts => 0
647
+ | ( opts.qos & 0x3 )
648
+ | ( opts.retain ? 0x4 : 0 )
649
+ | ( (opts.retain_handling & 0x3) << 2 );
650
+
651
+ return ns.subscribe = ( mqtt_level, pkt ) => {
652
+ const wrt = new mqtt_type_writer();
653
+
654
+ wrt.u16(pkt.pkt_id);
655
+ if (5 <= mqtt_level)
656
+ wrt.props(pkt.props);
657
+
658
+ const f0 = _enc_subscribe_flags(pkt);
659
+ for (const each of pkt.topics) {
660
+ if ('string' === typeof each) {
661
+ wrt.utf8(each);
662
+ wrt.u8(f0);
663
+ } else {
664
+ let [topic, opts] =
665
+ _is_array(each) ? each
666
+ : [each.topic, each.opts];
667
+
668
+ wrt.utf8(topic);
669
+ if (undefined === opts) wrt.u8(f0);
670
+ else wrt.u8_flags(opts, _enc_subscribe_flags);
671
+ }
672
+ }
673
+
674
+ return wrt.as_pkt(0x82)
675
+ }
676
+ }
677
+
678
+ function mqtt_encode_unsubscribe(ns) {
679
+ return ns.unsubscribe = ( mqtt_level, pkt ) => {
680
+ const wrt = new mqtt_type_writer();
681
+
682
+ wrt.u16(pkt.pkt_id);
683
+ if (5 <= mqtt_level)
684
+ wrt.props(pkt.props);
685
+
686
+ for (const topic of pkt.topics)
687
+ wrt.utf8(topic);
688
+
689
+ return wrt.as_pkt(0xa2)
690
+ }
691
+ }
692
+
693
+ function mqtt_encode_pingxxx(ns) {
694
+ ns.pingreq = () => new Uint8Array([ 0xc0, 0 ]);
695
+ ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
696
+ }
697
+
698
+ function mqtt_encode_disconnect(ns) {
699
+ return ns.disconnect = ( mqtt_level, pkt ) => {
700
+ const wrt = new mqtt_type_writer();
701
+
702
+ if (pkt && 5 <= mqtt_level) {
703
+ if (pkt.reason || pkt.props) {
704
+ wrt.u8_reason(pkt.reason);
705
+ wrt.props(pkt.props);
706
+ }
707
+ }
708
+
709
+ return wrt.as_pkt(0xe0)
710
+ }
711
+ }
712
+
713
+ function mqtt_encode_auth(ns) {
714
+ return ns.auth = ( mqtt_level, pkt ) => {
715
+ if (5 > mqtt_level)
716
+ throw new Error('Auth packets are only available after MQTT 5.x')
717
+
718
+ const wrt = new mqtt_type_writer();
719
+
720
+ wrt.u8_reason(pkt.reason);
721
+ wrt.props(pkt.props);
722
+
723
+ return wrt.as_pkt(0xf0)
724
+ }
725
+ }
726
+
727
+
728
+ function _bind_mqtt_decode(lst_decode_ops) {
729
+ const by_id = [];
730
+ for (const op of lst_decode_ops) op(by_id);
731
+
732
+ return _pkt_ctx_ => _mqtt_raw_pkt_dispatch(
733
+ (b0, u8_body) => {
734
+ const decode_pkt = by_id[b0>>>4] || by_id[0];
735
+ if (undefined !== decode_pkt)
736
+ return decode_pkt({__proto__: _pkt_ctx_, b0}, u8_body)
737
+ })
738
+ }
739
+
740
+
741
+ function _bind_mqtt_encode(lst_encode_ops) {
742
+ const by_type = {};
743
+ for (const op of lst_encode_ops) op(by_type);
744
+
745
+ return ({mqtt_level}) => (type, pkt) =>
746
+ by_type[type]( mqtt_level, pkt )
747
+ }
748
+
749
+
750
+ const _pkt_types = ['reserved', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
751
+ const _bind_pkt_ctx = _pkt_ctx_ =>
752
+ Object.defineProperties(_pkt_ctx_ || {}, {
753
+ hdr: {get() { return this.b0 & 0xf }},
754
+ id: {get() { return this.b0 >>> 4 }},
755
+ type: {get() { return _pkt_types[this.b0 >>> 4] }},
756
+ });
757
+
758
+ function _bind_mqtt_session_ctx(sess_decode, sess_encode, _pkt_ctx_) {
759
+ sess_decode = _bind_mqtt_decode(sess_decode);
760
+ sess_encode = _bind_mqtt_encode(sess_encode);
761
+ _pkt_ctx_ = _bind_pkt_ctx(_pkt_ctx_);
762
+
763
+ return mqtt_level => _base_ => {
764
+ _base_ = _base_ || {__proto__: _pkt_ctx_, mqtt_level, get _base_() { return _base_ }};
765
+ return [ sess_decode(_base_), sess_encode(_base_), _base_ ] }
766
+ }
767
+
768
+ function mqtt_session_ctx(mqtt_level) {
769
+ let {ctx} = mqtt_session_ctx;
770
+ if (undefined === ctx ) {
771
+ let as_utf8 = u8 =>
772
+ new TextDecoder('utf-8').decode(u8);
773
+
774
+ let std_pkt_api = {
775
+ utf8(u8) { return as_utf8( u8 || this.payload ) },
776
+ json(u8) { return JSON.parse( as_utf8( u8 || this.payload ) || null ) },
777
+ };
778
+
779
+ mqtt_session_ctx.ctx = ctx =
780
+ _bind_mqtt_session_ctx(
781
+ [ // lst_decode_ops
782
+ mqtt_decode_connack,
783
+ mqtt_decode_publish,
784
+ mqtt_decode_puback,
785
+ mqtt_decode_pubxxx,
786
+ mqtt_decode_pingxxx,
787
+ mqtt_decode_suback,
788
+ mqtt_decode_unsuback,
789
+ mqtt_decode_auth, ],
790
+
791
+ [ // lst_encode_ops
792
+ mqtt_encode_connect,
793
+ mqtt_encode_disconnect,
794
+ mqtt_encode_publish,
795
+ mqtt_encode_puback,
796
+ mqtt_encode_pingxxx,
797
+ mqtt_encode_subscribe,
798
+ mqtt_encode_unsubscribe,
799
+ mqtt_encode_auth, ],
800
+
801
+ std_pkt_api );
802
+ }
803
+
804
+ return ctx(mqtt_level)
805
+ }
806
+
807
+ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
808
+ let q0 = _tiny_deferred_queue();
809
+ let q = _tiny_deferred_queue();
810
+
811
+ let _asy_send = async (...args) =>
812
+ (await q)(...args);
813
+ let _send = client._send = _asy_send;
814
+
815
+ let _ping = () => client._send('pingreq');
816
+ let tid_ping, _is_set = false;
817
+
818
+ return {
819
+ is_live: (() =>_asy_send !== _send)
820
+ , is_set: (() =>_is_set)
821
+
822
+ , reset() {
823
+ tid_ping = clearInterval(tid_ping);
824
+ client._send = _send = _asy_send;
825
+ _is_set = false;
826
+
827
+ // call client.on_reconnect in next promise microtask
828
+ _async_evt(client, client.on_reconnect);}
829
+
830
+ , ping(td) {
831
+ tid_ping = clearInterval(tid_ping);
832
+ if (td) {
833
+ tid_ping = setInterval(_ping, 1000 * td);
834
+ if (tid_ping.unref) {
835
+ tid_ping.unref();} } }
836
+
837
+ , async send_connect(... args) {
838
+ if (_asy_send === _send) {
839
+ _send = await q0;}
840
+
841
+ // await connack response
842
+ let res = await _send(...args);
843
+
844
+ client._send = _send;
845
+ q.notify(_send);
846
+ return res}
847
+
848
+ , set(mqtt_session, send_u8_pkt) {
849
+ _is_set = true;
850
+
851
+ let [mqtt_decode, mqtt_encode] = mqtt_session;
852
+
853
+ let on_mqtt_chunk = u8_buf =>
854
+ on_mqtt(
855
+ mqtt_decode(u8_buf),
856
+ {mqtt: client});
857
+
858
+ _send = async (type, pkt, key) => {
859
+ let res = undefined !== key
860
+ ? pkt_future(key) : true;
861
+
862
+ await send_u8_pkt(
863
+ mqtt_encode(type, pkt));
864
+
865
+ return res};
866
+
867
+
868
+ q0.notify(_send);
869
+
870
+ // call client.on_live in next promise microtask
871
+ _async_evt(client, client.on_live);
872
+
873
+ return on_mqtt_chunk} } }
874
+
875
+
876
+ async function _async_evt(obj, evt) {
877
+ // microtask break lookup
878
+ if (undefined !== evt) {
879
+ await evt.call(obj, await obj);
880
+ }
881
+ }
882
+ function _tiny_deferred_queue() {
883
+ let q = []; // tiny resetting deferred queue
884
+ q.then = y => { q.push(y); };
885
+ q.notify = v => {
886
+ for (let fn of q.splice(0,q.length))
887
+ fn(v); };
888
+ return q
889
+ }
890
+
891
+ function parse(str, loose) {
892
+ if (str instanceof RegExp) return { keys:false, pattern:str };
893
+ var c, o, tmp, ext, keys=[], pattern='', arr = str.split('/');
894
+ arr[0] || arr.shift();
895
+
896
+ while (tmp = arr.shift()) {
897
+ c = tmp[0];
898
+ if (c === '*') {
899
+ keys.push('wild');
900
+ pattern += '/(.*)';
901
+ } else if (c === ':') {
902
+ o = tmp.indexOf('?', 1);
903
+ ext = tmp.indexOf('.', 1);
904
+ keys.push( tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length) );
905
+ pattern += !!~o && !~ext ? '(?:/([^/]+?))?' : '/([^/]+?)';
906
+ if (!!~ext) pattern += (!!~o ? '?' : '') + '\\' + tmp.substring(ext);
907
+ } else {
908
+ pattern += '/' + tmp;
909
+ }
910
+ }
911
+
912
+ return {
913
+ keys: keys,
914
+ pattern: new RegExp('^' + pattern + (loose ? '(?=$|\/)' : '\/?$'), 'i')
915
+ };
916
+ }
917
+
918
+ // Use [regexparam][] for url-like topic parsing
919
+
920
+ function _ignore(pkt, params, ctx) {ctx.done = true;}
921
+
922
+ function _mqtt_topic_router() {
923
+ let pri_lsts = [[],[]];
924
+ let find = topic => _mqtt_routes_iter(pri_lsts, topic);
925
+
926
+ return {find,
927
+
928
+ add(topic_route, ...args) {
929
+ let fn = args.pop();
930
+ let priority = args.pop();
931
+
932
+ if ('function' !== typeof fn) {
933
+ if (false === fn) {
934
+ fn = _ignore;}
935
+ else throw new TypeError()}
936
+
937
+ let rte = parse(
938
+ topic_route.replace(/[+#]$/, '*'));
939
+
940
+ rte.tgt = fn;
941
+ pri_lsts[priority ? 0 : 1].push(rte);
942
+ return this}
943
+
944
+ , async invoke(pkt, ctx) {
945
+ ctx.idx = 0;
946
+
947
+ for (let [fn, params] of find(pkt.topic)) {
948
+ await fn(pkt, params, ctx);
949
+
950
+ if (ctx.done) {
951
+ break}
952
+ else ctx.idx++;}
953
+
954
+ let {pkt_id, qos} = pkt;
955
+ if (1 === qos) {
956
+ await ctx.mqtt._send('puback', {pkt_id});} } } }
957
+
958
+
959
+ function * _mqtt_routes_iter(all_route_lists, topic) {
960
+ for (let route_list of all_route_lists) {
961
+ for (let route of route_list) {
962
+ let res = _mqtt_route_match_one(topic, route);
963
+ if (undefined !== res) {
964
+ yield res;} } } }
965
+
966
+
967
+ function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
968
+ let match = pattern.exec('/'+topic);
969
+ if (null === match) {
970
+ return}
971
+
972
+ if (false === keys) {
973
+ let {groups} = match;
974
+ if (! groups) {
975
+ return [tgt]}
976
+
977
+ let params = {};
978
+ for (let k in groups) {
979
+ params[k] = groups[k];}
980
+
981
+ return [tgt, params]}
982
+
983
+ if (0 === keys.length) {
984
+ return [tgt]}
985
+
986
+ let params = {};
987
+ for (let i=0; i<keys.length; i++) {
988
+ params[ keys[i] ] = match[1+i];}
989
+ return [tgt, params]}
990
+
991
+ const _mqtt_cmdid_dispatch ={
992
+ create(target) {
993
+ return {__proto__: this, target, hashbelt: [new Map()]} }
994
+
995
+ , bind_pkt_future(_pkt_id=100) {
996
+ let {hashbelt} = this;
997
+
998
+ let _tmp_; // use _tmp_ to reuse _by_key closure
999
+ let _by_key = answer_monad =>
1000
+ hashbelt[0].set(_tmp_, answer_monad);
1001
+
1002
+ return (( pkt_or_key ) => {
1003
+ if ('string' === typeof pkt_or_key) {
1004
+ _tmp_ = pkt_or_key;}
1005
+ else {
1006
+ _pkt_id = (_pkt_id + 1) & 0xffff;
1007
+ _tmp_ = pkt_or_key.pkt_id = _pkt_id;}
1008
+
1009
+ return new Promise(_by_key)}) }
1010
+
1011
+ , answer(key, pkt) {
1012
+ for (let map of this.hashbelt) {
1013
+ let answer_monad = map.get(key);
1014
+ if (undefined !== answer_monad) {
1015
+ map.delete(key);
1016
+
1017
+ answer_monad([pkt, /*err*/]); // option/maybe monad
1018
+ return true} }
1019
+ return false}
1020
+
1021
+ , rotate_belt(n) {
1022
+ let {hashbelt} = this;
1023
+ hashbelt.unshift(new Map());
1024
+ for (let old of hashbelt.splice(n || 5)) {
1025
+ for (let answer_monad of old.values()) {
1026
+ answer_monad([/*pkt*/, 'expired']); } } }// option/maybe monad
1027
+
1028
+ , cmdids: ((() => {
1029
+ return [
1030
+ (() =>{} )// 0x0 reserved
1031
+ , by_evt // 0x1 connect
1032
+ , by_type // 0x2 connack
1033
+ , by_evt // 0x3 publish
1034
+ , by_id // 0x4 puback
1035
+ , by_id // 0x5 pubrec
1036
+ , by_id // 0x6 pubrel
1037
+ , by_id // 0x7 pubcomp
1038
+ , by_evt // 0x8 subscribe
1039
+ , by_id // 0x9 suback
1040
+ , by_evt // 0xa unsubscribe
1041
+ , by_id // 0xb unsuback
1042
+ , by_type // 0xc pingreq
1043
+ , by_type // 0xd pingresp
1044
+ , by_evt // 0xe disconnect
1045
+ , by_type ]// 0xf auth
1046
+
1047
+
1048
+ function by_id(disp, pkt) {
1049
+ disp.answer(pkt.pkt_id, pkt); }
1050
+
1051
+ function by_type(disp, pkt, ctx) {
1052
+ disp.answer(pkt.type, pkt);
1053
+ by_evt(disp, pkt, ctx);}
1054
+
1055
+ async function by_evt({target}, pkt, ctx) {
1056
+ let fn = target[`mqtt_${pkt.type}`]
1057
+ || target.mqtt_pkt;
1058
+
1059
+ if (undefined !== fn) {
1060
+ await fn.call(target, pkt, ctx);} } })()) };
1061
+
1062
+ function _mqtt_dispatch(opt, target) {
1063
+ let _disp_ = _mqtt_cmdid_dispatch.create(target);
1064
+ let { cmdids } = _disp_;
1065
+
1066
+ // default rotate at 1s across 5 buckets
1067
+ let { td: rotate_td=1000, n: rotate_n=5 } =
1068
+ opt && opt.rotate || {};
1069
+
1070
+ let rotate_ts = rotate_td + Date.now();
1071
+
1072
+ return [on_mqtt,
1073
+ _disp_.bind_pkt_future()]
1074
+
1075
+ function on_mqtt(pkt_list, ctx) {
1076
+ for (let pkt of pkt_list) {
1077
+ cmdids[pkt.id](_disp_, pkt, ctx); }
1078
+
1079
+ if (Date.now() > rotate_ts) {
1080
+ _disp_.rotate_belt(rotate_n);
1081
+ rotate_ts = rotate_td + Date.now();} } }
1082
+
1083
+ class MQTTBaseClient {
1084
+ constructor(opt={}) {
1085
+ this._conn_ = _mqtt_conn(this,
1086
+ this._init_dispatch(opt, this)); }
1087
+
1088
+ // Handshaking Packets
1089
+
1090
+ async connect(pkt={}) {
1091
+ let {client_id: cid} = pkt;
1092
+ if (! cid) {
1093
+ pkt.client_id = cid = this.init_client_id(['u8-mqtt--', '']);}
1094
+ else if (Array.isArray(cid)) {
1095
+ pkt.client_id = cid = this.init_client_id(cid);}
1096
+ else {this.client_id = cid;}
1097
+
1098
+ if (null == pkt.keep_alive) {
1099
+ pkt.keep_alive = 60;}
1100
+
1101
+ let res = await this._conn_
1102
+ .send_connect('connect', pkt, 'connack');
1103
+
1104
+ // TODO: merge with server's keep_alive frequency
1105
+ this._conn_.ping(pkt.keep_alive);
1106
+ return res}
1107
+
1108
+ async disconnect(pkt={}) {
1109
+ let res = await this._send('disconnect', pkt);
1110
+ this._conn_.reset();
1111
+ return res}
1112
+
1113
+ auth(pkt={}) {
1114
+ return this._send('auth', pkt, 'auth')}
1115
+
1116
+ ping() {return this._send('pingreq', null, 'pingresp')}
1117
+
1118
+
1119
+ // alias: sub
1120
+ subscribe(pkt, ex) {
1121
+ pkt = _as_topics(pkt, ex);
1122
+ return this._send('subscribe', pkt, pkt)}
1123
+
1124
+ // alias: unsub
1125
+ unsubscribe(pkt, ex) {
1126
+ pkt = _as_topics(pkt, ex);
1127
+ return this._send('unsubscribe', pkt, pkt)}
1128
+
1129
+ // alias: sub_topic
1130
+ subscribe_topic(topic_route, ...args) {
1131
+ let topic = topic_route.replace(/[:*].*$/, '#');
1132
+ this.on_topic(topic_route, true, args.pop() );// handler
1133
+ this.subscribe([[ topic ]], args.pop() );// ex
1134
+ return this}
1135
+
1136
+
1137
+ // alias: pub
1138
+ publish(pkt) {return _pub(this, pkt)}
1139
+ post(topic, payload) {return _pub(this, {qos:0, topic, payload})}
1140
+ send(topic, payload) {return _pub(this, {qos:1, topic, payload})}
1141
+ store(topic, payload) {return _pub(this, {qos:1, retain: 1, topic, payload})}
1142
+
1143
+ // alias: json_post
1144
+ obj_post(topic, msg, encode) {return _pub(this, {qos:0, topic, msg, arg:'msg'}, encode)}
1145
+ // alias: json_send
1146
+ obj_send(topic, msg, encode) {return _pub(this, {qos:1, topic, msg, arg:'msg'}, encode)}
1147
+ // alias: json_storek
1148
+ obj_store(topic, msg, encode) {return _pub(this, {qos:1, retain: 1, msg, arg:'msg'}, encode)}
1149
+
1150
+
1151
+
1152
+ // Utility Methods
1153
+
1154
+ init_client_id(parts) {
1155
+ let cid = this.client_id;
1156
+
1157
+ if (undefined === cid) {
1158
+ this.client_id = cid = (
1159
+
1160
+
1161
+
1162
+ this.new_client_id(parts)
1163
+ );}
1164
+
1165
+ return cid}
1166
+
1167
+ new_client_id(parts) {
1168
+ return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
1169
+
1170
+
1171
+
1172
+
1173
+
1174
+
1175
+
1176
+
1177
+
1178
+
1179
+
1180
+ // Internal API
1181
+
1182
+ /* async _send(type, pkt) -- provided by _conn_ and transport */
1183
+
1184
+ _init_router(opt) {
1185
+ let router = _mqtt_topic_router();
1186
+ this.on_topic = router.add;
1187
+ return this.router = router}
1188
+
1189
+ _init_dispatch(opt) {
1190
+ let router = this._init_router(opt, this);
1191
+
1192
+ let tgt ={
1193
+ __proto__: opt.on_mqtt_type || {}
1194
+ , mqtt_publish: router.invoke};
1195
+
1196
+ return _mqtt_dispatch(this, tgt) } }
1197
+
1198
+
1199
+ {
1200
+ let p = MQTTBaseClient.prototype;
1201
+ Object.assign(p,{
1202
+ pub: p.publish
1203
+ , sub: p.subscribe
1204
+ , unsub: p.unsubscribe
1205
+ , sub_topic: p.subscribe_topic
1206
+
1207
+ , json_post: p.obj_post
1208
+ , json_send: p.obj_send
1209
+ , json_store: p.obj_store} );
1210
+
1211
+ /*
1212
+ p.on_mqtt_type = {
1213
+ mqtt_auth(pkt, ctx) ::
1214
+ mqtt_connect(pkt, ctx) ::
1215
+ mqtt_connack(pkt, ctx) ::
1216
+ mqtt_disconnect(pkt, ctx) ::
1217
+
1218
+ mqtt_subscribe(pkt, ctx) ::
1219
+ mqtt_unsubscribe(pkt, ctx) ::
1220
+
1221
+ mqtt_pingreq(pkt, ctx) ::
1222
+ mqtt_pingresp(pkt, ctx) ::
1223
+ }
1224
+ */}
1225
+
1226
+
1227
+ function _pub(self, pkt, encode) {
1228
+ let key, {qos, msg, payload} = pkt;
1229
+ if (undefined === payload) {
1230
+ if (undefined === msg) {
1231
+ let arg = pkt.arg || 'payload';
1232
+ return v => _pub(self, {...pkt, [arg]: v}, encode)}
1233
+
1234
+ pkt.payload = encode
1235
+ ? encode(msg)
1236
+ : JSON.stringify(msg);}
1237
+
1238
+ if (1 === qos) key = pkt;
1239
+ return self._send('publish', pkt, key)}
1240
+
1241
+ function _as_topics(pkt, ex) {
1242
+ if ('string' === typeof pkt) {
1243
+ return {topics:[pkt], ... ex}}
1244
+ if (pkt[Symbol.iterator]) {
1245
+ return {topics:[... pkt], ... ex}}
1246
+ return ex ? {...pkt, ...ex} : pkt}
1247
+
1248
+ class MQTTCoreClient extends MQTTBaseClient {
1249
+ static _with_session(mqtt_session) {
1250
+ this.prototype._mqtt_session = mqtt_session;}
1251
+
1252
+ constructor(opt={}) {
1253
+ super(opt);
1254
+ this.with_live(opt.on_live);
1255
+ this.with_reconnect(opt.on_reconnect);}
1256
+
1257
+
1258
+ // on_live(client) ::
1259
+ with_live(on_live) {
1260
+ if (on_live) {
1261
+ this.on_live = on_live;}
1262
+
1263
+ return this}
1264
+
1265
+ // on_reconnect(client) ::
1266
+ with_reconnect(on_reconnect) {
1267
+ if (on_reconnect) {
1268
+ this.on_reconnect = on_reconnect;
1269
+
1270
+ if (! this._conn_.is_set) {
1271
+ on_reconnect(this);} }
1272
+
1273
+ return this}
1274
+
1275
+
1276
+ with_async_iter(async_iter, write_u8_pkt) {
1277
+ let on_mqtt_chunk = this._conn_.set(
1278
+ this._mqtt_session(),
1279
+ write_u8_pkt);
1280
+
1281
+ this._msg_loop = ((async () => {
1282
+ async_iter = await async_iter;
1283
+ for await (let chunk of async_iter)
1284
+ on_mqtt_chunk(chunk);
1285
+
1286
+ this._conn_.reset();})());
1287
+
1288
+ return this}
1289
+
1290
+
1291
+
1292
+ with_tcp(port, hostname) {
1293
+ if (!Number.isFinite(port)) {
1294
+ ({port, host: hostname} = port);}
1295
+
1296
+ Deno.connect({
1297
+ port: port || 1883, hostname, transport: 'tcp'})
1298
+ .then (( conn ) =>
1299
+ this.with_async_iter(
1300
+ Deno.iter(conn),
1301
+ u8_pkt => conn.write(u8_pkt)) );
1302
+
1303
+ return this}
1304
+
1305
+
1306
+
1307
+
1308
+
1309
+
1310
+
1311
+
1312
+
1313
+
1314
+
1315
+
1316
+
1317
+
1318
+
1319
+
1320
+
1321
+
1322
+
1323
+
1324
+
1325
+
1326
+
1327
+ with_websock(websock) {
1328
+ if (null == websock) {
1329
+ websock = 'ws://127.0.0.1:9001';}
1330
+
1331
+ if (websock.origin || 'string' === typeof websock) {
1332
+ websock = new WebSocket(new URL(websock), ['mqtt']);}
1333
+
1334
+ websock.binaryType = 'arraybuffer';
1335
+
1336
+ let ready, {readyState} = websock;
1337
+ if (1 !== readyState) {
1338
+ if (0 !== readyState) {
1339
+ throw new Error('Invalid WebSocket readyState') }
1340
+
1341
+ ready = new Promise( y =>
1342
+ websock.addEventListener('open', y, {once: true}));}
1343
+
1344
+
1345
+ let {_conn_} = this;
1346
+ let on_mqtt_chunk = _conn_.set(
1347
+ this._mqtt_session(),
1348
+ async u8_pkt =>(
1349
+ await ready
1350
+ , websock.send(u8_pkt)) );
1351
+
1352
+ websock.addEventListener('close',
1353
+ (() => {
1354
+ delete websock.onmessage;
1355
+ _conn_.reset();})
1356
+
1357
+ , {once: true});
1358
+
1359
+ websock.onmessage = evt =>
1360
+ on_mqtt_chunk(
1361
+ new Uint8Array(evt.data));
1362
+
1363
+ return this} }
1364
+
1365
+ class MQTTClient_v5 extends MQTTCoreClient {
1366
+ _mqtt_session() { return mqtt_session_ctx(5)() }
1367
+ }
1368
+
1369
+ const mqtt_v5 = opt => new MQTTClient_v5(opt);
1370
+
1371
+ export { MQTTClient_v5 as MQTTClient, MQTTClient_v5, mqtt_v5 as default, mqtt_v5 as mqtt, mqtt_v5 };
1372
+ //# sourceMappingURL=v5.mjs.map