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