u8-mqtt 0.0.26 → 0.1.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 (50) hide show
  1. package/cjs/index.cjs +36 -11
  2. package/cjs/index.cjs.map +1 -1
  3. package/cjs/v4.cjs +36 -11
  4. package/cjs/v4.cjs.map +1 -1
  5. package/cjs/v5.cjs +36 -11
  6. package/cjs/v5.cjs.map +1 -1
  7. package/code/_router.jsy +15 -1
  8. package/code/base.jsy +17 -7
  9. package/code/core.jsy +3 -3
  10. package/code/session.mjs +2 -1
  11. package/esm/deno/index.js +1471 -0
  12. package/esm/deno/index.js.map +1 -0
  13. package/esm/deno/index.mjs +37 -12
  14. package/esm/deno/index.mjs.map +1 -1
  15. package/esm/deno/v4.js +1465 -0
  16. package/esm/deno/v4.js.map +1 -0
  17. package/esm/deno/v4.mjs +37 -12
  18. package/esm/deno/v4.mjs.map +1 -1
  19. package/esm/deno/v5.js +1465 -0
  20. package/esm/deno/v5.js.map +1 -0
  21. package/esm/deno/v5.mjs +37 -12
  22. package/esm/deno/v5.mjs.map +1 -1
  23. package/esm/node/index.js +1473 -0
  24. package/esm/node/index.js.map +1 -0
  25. package/esm/node/index.mjs +36 -11
  26. package/esm/node/index.mjs.map +1 -1
  27. package/esm/node/v4.js +1467 -0
  28. package/esm/node/v4.js.map +1 -0
  29. package/esm/node/v4.mjs +36 -11
  30. package/esm/node/v4.mjs.map +1 -1
  31. package/esm/node/v5.js +1467 -0
  32. package/esm/node/v5.js.map +1 -0
  33. package/esm/node/v5.mjs +36 -11
  34. package/esm/node/v5.mjs.map +1 -1
  35. package/esm/web/index.js +1471 -0
  36. package/esm/web/index.js.map +1 -0
  37. package/esm/web/index.min.mjs +1 -1
  38. package/esm/web/index.mjs +35 -10
  39. package/esm/web/index.mjs.map +1 -1
  40. package/esm/web/v4.js +1465 -0
  41. package/esm/web/v4.js.map +1 -0
  42. package/esm/web/v4.min.mjs +1 -1
  43. package/esm/web/v4.mjs +35 -10
  44. package/esm/web/v4.mjs.map +1 -1
  45. package/esm/web/v5.js +1465 -0
  46. package/esm/web/v5.js.map +1 -0
  47. package/esm/web/v5.min.mjs +1 -1
  48. package/esm/web/v5.mjs +35 -10
  49. package/esm/web/v5.mjs.map +1 -1
  50. package/package.json +10 -10
package/esm/node/v5.js ADDED
@@ -0,0 +1,1467 @@
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_disconnect(ns) {
398
+ const _disconnect_reason_ = bind_reason_lookup([
399
+ // MQTT 5.0
400
+ [ 0x00, 'Normal disconnection'],
401
+ [ 0x04, 'Disconnect with Will Message'],
402
+ [ 0x80, 'Unspecified error'],
403
+ [ 0x81, 'Malformed Packet'],
404
+ [ 0x82, 'Protocol Error'],
405
+ [ 0x83, 'Implementation specific error'],
406
+ [ 0x87, 'Not authorized'],
407
+ [ 0x89, 'Server busy'],
408
+ [ 0x8B, 'Server shutting down'],
409
+ [ 0x8D, 'Keep Alive timeout'],
410
+ [ 0x8E, 'Session taken over'],
411
+ [ 0x8F, 'Topic Filter invalid'],
412
+ [ 0x90, 'Topic Name invalid'],
413
+ [ 0x93, 'Receive Maximum exceeded'],
414
+ [ 0x94, 'Topic Alias invalid'],
415
+ [ 0x95, 'Packet too large'],
416
+ [ 0x96, 'Message rate too high'],
417
+ [ 0x97, 'Quota exceeded'],
418
+ [ 0x98, 'Administrative action'],
419
+ [ 0x99, 'Payload format invalid'],
420
+ [ 0x9A, 'Retain not supported'],
421
+ [ 0x9B, 'QoS not supported'],
422
+ [ 0x9C, 'Use another server'],
423
+ [ 0x9D, 'Server moved'],
424
+ [ 0x9E, 'Shared Subscriptions not supported'],
425
+ [ 0x9F, 'Connection rate exceeded'],
426
+ [ 0xA0, 'Maximum connect time'],
427
+ [ 0xA1, 'Subscription Identifiers not supported'],
428
+ [ 0xA2, 'Wildcard Subscriptions not supported'],
429
+ ]);
430
+
431
+
432
+ return ns[0xe] = (pkt, u8_body) => {
433
+ if (u8_body && 5 <= pkt.mqtt_level) {
434
+ const rdr = new mqtt_type_reader(u8_body, 0);
435
+ pkt.reason = rdr.u8_reason(_disconnect_reason_);
436
+ pkt.props = rdr.props();
437
+ }
438
+ return pkt }
439
+ }
440
+
441
+ function mqtt_decode_auth(ns) {
442
+ const _auth_reason_ = bind_reason_lookup([
443
+ // MQTT 5.0
444
+ [ 0x00, 'Success' ],
445
+ [ 0x18, 'Continue authentication' ],
446
+ [ 0x19, 'Re-authenticate' ],
447
+ ]);
448
+
449
+ return ns[0xf] = (pkt, u8_body) => {
450
+ if ( 5 <= pkt.mqtt_level ) {
451
+ const rdr = new mqtt_type_reader(u8_body, 0);
452
+ pkt.reason = rdr.u8_reason(_auth_reason_);
453
+ pkt.props = rdr.props();
454
+ }
455
+ return pkt }
456
+ }
457
+
458
+ function mqtt_pkt_writer_pool() {
459
+ const _pool_ = [];
460
+ return host =>
461
+ 0 === _pool_.length
462
+ ? mqtt_pkt_writer(host, _pool_)
463
+ : _pool_.pop()(host)
464
+ }
465
+
466
+ function mqtt_pkt_writer(host, _pool_) {
467
+ // avoid GCing push/pull when they can be reused
468
+ let n=0, rope=[];
469
+ return install(host)
470
+
471
+ function install(_host) {
472
+ host = _host;
473
+ host.push = push;
474
+ host.pack = pack;
475
+ }
476
+
477
+ function push(u8) {
478
+ rope.push(u8);
479
+ n += u8.length;
480
+ }
481
+
482
+ function pack(hdr) {
483
+ host = host.push = host.pack = null;
484
+
485
+ const res = _mqtt_pkt_rope(hdr, n, rope);
486
+ n=0; rope=[];
487
+ if (undefined !== _pool_)
488
+ _pool_.push(install);
489
+
490
+ return res
491
+ }
492
+ }
493
+
494
+
495
+ function _mqtt_pkt_rope(hdr, n, rope) {
496
+ const header = encode_varint(n, hdr);
497
+ let i = header.length;
498
+
499
+ const pkt = new Uint8Array(n + i);
500
+ pkt.set(header, 0);
501
+ for (const vec of rope) {
502
+ pkt.set(vec, i);
503
+ i += vec.length;
504
+ }
505
+ return pkt
506
+ }
507
+
508
+ const _is_array = Array.isArray;
509
+ const pack_utf8 = v => new TextEncoder('utf-8').encode(v);
510
+ const pack_u16 = v => [ (v>>>8) & 0xff, v & 0xff ];
511
+ const pack_u32 = v => [ (v>>>24) & 0xff, (v>>>16) & 0xff, (v>>>8) & 0xff, v & 0xff ];
512
+
513
+ class mqtt_type_writer {
514
+ constructor() {
515
+ this._pkt_writer(this);
516
+ }
517
+
518
+ as_pkt(hdr) { return this.pack([hdr]) }
519
+
520
+ u8(v) { this.push([ v & 0xff ]);}
521
+ u16(v) { this.push( pack_u16(v) );}
522
+ u32(v) { this.push( pack_u32(v) );}
523
+ vint(v) { this.push( encode_varint(v) );}
524
+
525
+ _u16_bin(u8_buf) {
526
+ const {push} = this;
527
+ push( pack_u16( u8_buf.byteLength ));
528
+ push( u8_buf );
529
+ }
530
+
531
+ flush(buf) {
532
+ if (null != buf)
533
+ this.push(
534
+ 'string' === typeof buf
535
+ ? pack_utf8(buf) : buf );
536
+
537
+ this.push = false;
538
+ }
539
+
540
+ bin(u8_buf) {
541
+ if (! u8_buf) return this.u16(0)
542
+ if ('string' === typeof u8_buf)
543
+ return this.utf8(u8_buf)
544
+
545
+ if (u8_buf.length !== u8_buf.byteLength)
546
+ u8_buf = new Uint8Array(u8_buf);
547
+ this._u16_bin(u8_buf);
548
+ }
549
+
550
+ utf8(v) { this._u16_bin( new TextEncoder('utf-8').encode(v) ); }
551
+
552
+ pair(k,v) {
553
+ this.utf8(k);
554
+ this.utf8(v);
555
+ }
556
+
557
+ u8_flags(v, enc_flags, b0=0) {
558
+ if (undefined !== v && isNaN(+v))
559
+ v = enc_flags(v, 0);
560
+
561
+ v |= b0;
562
+ this.push([v]);
563
+ return v
564
+ }
565
+
566
+ u8_reason(v) { this.push([v | 0]); }
567
+
568
+ props(props) {
569
+ if (! props)
570
+ return this.u8(0)
571
+
572
+ if (! _is_array(props))
573
+ props = props.entries
574
+ ? Array.from(props.entries())
575
+ : Object.entries(props);
576
+
577
+ if (0 === props.length)
578
+ return this.u8(0)
579
+
580
+ const wrt = this._fork();
581
+ for (const [name, value] of props) {
582
+ const {id, type} = mqtt_props.get(name);
583
+ wrt.u8(id);
584
+ wrt[type](value);
585
+ }
586
+
587
+ this.push(wrt.pack([]));
588
+ }
589
+
590
+ _fork() {
591
+ let self = { __proto__: this };
592
+ this._pkt_writer(self);
593
+ return self
594
+ }
595
+ }
596
+
597
+ mqtt_type_writer.prototype._pkt_writer =
598
+ mqtt_pkt_writer_pool();
599
+
600
+ const _c_mqtt_proto = new Uint8Array([
601
+ 0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
602
+
603
+ function mqtt_encode_connect(ns) {
604
+ const _enc_flags_connect = flags => 0
605
+ | ( flags.reserved ? 0x01 : 0 )
606
+ | ( (flags.will_qos & 0x3) << 3 )
607
+ | ( flags.clean_start ? 0x02 : 0 )
608
+ | ( flags.will_flag ? 0x04 : 0 )
609
+ | ( flags.will_retain ? 0x20 : 0 )
610
+ | ( flags.password ? 0x40 : 0 )
611
+ | ( flags.username ? 0x80 : 0 );
612
+
613
+ const _enc_flags_will = will => 0x4
614
+ | ( (will.qos & 0x3) << 3 )
615
+ | ( will.retain ? 0x20 : 0 );
616
+
617
+ return ns.connect = ( mqtt_level, pkt ) => {
618
+ const wrt = new mqtt_type_writer();
619
+
620
+ wrt.push(_c_mqtt_proto);
621
+ wrt.u8( mqtt_level );
622
+
623
+ const {will, username, password} = pkt;
624
+ const flags = wrt.u8_flags(
625
+ pkt.flags,
626
+ _enc_flags_connect,
627
+ 0 | (username ? 0x80 : 0)
628
+ | (password ? 0x40 : 0)
629
+ | (will ? _enc_flags_will(will) : 0) );
630
+
631
+ wrt.u16(pkt.keep_alive);
632
+
633
+ if (5 <= mqtt_level)
634
+ wrt.props(pkt.props);
635
+
636
+
637
+ wrt.utf8(pkt.client_id);
638
+ if (flags & 0x04) { // .will_flag
639
+ if (5 <= mqtt_level)
640
+ wrt.props(will.props);
641
+
642
+ wrt.utf8(will.topic);
643
+ wrt.bin(will.payload);
644
+ }
645
+
646
+ if (flags & 0x80) // .username
647
+ wrt.utf8(username);
648
+
649
+ if (flags & 0x40) // .password
650
+ wrt.bin(password);
651
+
652
+ return wrt.as_pkt(0x10)
653
+ }
654
+ }
655
+
656
+ function mqtt_encode_publish(ns) {
657
+ return ns.publish = ( mqtt_level, pkt ) => {
658
+ const qos = (pkt.qos & 0x3) << 1;
659
+ const wrt = new mqtt_type_writer();
660
+
661
+ wrt.utf8(pkt.topic);
662
+ if (0 !== qos)
663
+ wrt.u16(pkt.pkt_id);
664
+
665
+ if ( 5 <= mqtt_level) {
666
+ wrt.props(pkt.props);
667
+ wrt.flush(pkt.payload);
668
+ } else {
669
+ wrt.flush(pkt.payload);
670
+ }
671
+
672
+ return wrt.as_pkt(
673
+ 0x30 | qos | (pkt.dup ? 0x8 : 0) | (pkt.retain ? 0x1 : 0) )
674
+ }
675
+ }
676
+
677
+ function mqtt_encode_puback(ns) {
678
+ return ns.puback = ( mqtt_level, pkt ) => {
679
+ const wrt = new mqtt_type_writer();
680
+
681
+ wrt.u16(pkt.pkt_id);
682
+ if (5 <= mqtt_level) {
683
+ wrt.u8_reason(pkt.reason);
684
+ wrt.props(pkt.props);
685
+ }
686
+
687
+ return wrt.as_pkt(0x40)
688
+ }
689
+ }
690
+
691
+ function mqtt_encode_subscribe(ns) {
692
+ const _enc_subscribe_flags = opts => 0
693
+ | ( opts.qos & 0x3 )
694
+ | ( opts.retain ? 0x4 : 0 )
695
+ | ( (opts.retain_handling & 0x3) << 2 );
696
+
697
+ return ns.subscribe = ( mqtt_level, pkt ) => {
698
+ const wrt = new mqtt_type_writer();
699
+
700
+ wrt.u16(pkt.pkt_id);
701
+ if (5 <= mqtt_level)
702
+ wrt.props(pkt.props);
703
+
704
+ const f0 = _enc_subscribe_flags(pkt);
705
+ for (const each of pkt.topics) {
706
+ if ('string' === typeof each) {
707
+ wrt.utf8(each);
708
+ wrt.u8(f0);
709
+ } else {
710
+ let [topic, opts] =
711
+ _is_array(each) ? each
712
+ : [each.topic, each.opts];
713
+
714
+ wrt.utf8(topic);
715
+ if (undefined === opts) wrt.u8(f0);
716
+ else wrt.u8_flags(opts, _enc_subscribe_flags);
717
+ }
718
+ }
719
+
720
+ return wrt.as_pkt(0x82)
721
+ }
722
+ }
723
+
724
+ function mqtt_encode_unsubscribe(ns) {
725
+ return ns.unsubscribe = ( mqtt_level, pkt ) => {
726
+ const wrt = new mqtt_type_writer();
727
+
728
+ wrt.u16(pkt.pkt_id);
729
+ if (5 <= mqtt_level)
730
+ wrt.props(pkt.props);
731
+
732
+ for (const topic of pkt.topics)
733
+ wrt.utf8(topic);
734
+
735
+ return wrt.as_pkt(0xa2)
736
+ }
737
+ }
738
+
739
+ function mqtt_encode_pingxxx(ns) {
740
+ ns.pingreq = () => new Uint8Array([ 0xc0, 0 ]);
741
+ ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
742
+ }
743
+
744
+ function mqtt_encode_disconnect(ns) {
745
+ return ns.disconnect = ( mqtt_level, pkt ) => {
746
+ const wrt = new mqtt_type_writer();
747
+
748
+ if (pkt && 5 <= mqtt_level) {
749
+ if (pkt.reason || pkt.props) {
750
+ wrt.u8_reason(pkt.reason);
751
+ wrt.props(pkt.props);
752
+ }
753
+ }
754
+
755
+ return wrt.as_pkt(0xe0)
756
+ }
757
+ }
758
+
759
+ function mqtt_encode_auth(ns) {
760
+ return ns.auth = ( mqtt_level, pkt ) => {
761
+ if (5 > mqtt_level)
762
+ throw new Error('Auth packets are only available after MQTT 5.x')
763
+
764
+ const wrt = new mqtt_type_writer();
765
+
766
+ wrt.u8_reason(pkt.reason);
767
+ wrt.props(pkt.props);
768
+
769
+ return wrt.as_pkt(0xf0)
770
+ }
771
+ }
772
+
773
+
774
+ function _bind_mqtt_decode(lst_decode_ops) {
775
+ const by_id = [];
776
+ for (const op of lst_decode_ops) op(by_id);
777
+
778
+ return _pkt_ctx_ => _mqtt_raw_pkt_dispatch(
779
+ (b0, u8_body) => {
780
+ const decode_pkt = by_id[b0>>>4] || by_id[0];
781
+ if (undefined !== decode_pkt)
782
+ return decode_pkt({__proto__: _pkt_ctx_, b0}, u8_body)
783
+ })
784
+ }
785
+
786
+
787
+ function _bind_mqtt_encode(lst_encode_ops) {
788
+ const by_type = {};
789
+ for (const op of lst_encode_ops) op(by_type);
790
+
791
+ return ({mqtt_level}) => (type, pkt) =>
792
+ by_type[type]( mqtt_level, pkt )
793
+ }
794
+
795
+
796
+ const _pkt_types = ['reserved', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
797
+ const _bind_pkt_ctx = _pkt_ctx_ =>
798
+ Object.defineProperties(_pkt_ctx_ || {}, {
799
+ hdr: {get() { return this.b0 & 0xf }},
800
+ id: {get() { return this.b0 >>> 4 }},
801
+ type: {get() { return _pkt_types[this.b0 >>> 4] }},
802
+ });
803
+
804
+ function _bind_mqtt_session_ctx(sess_decode, sess_encode, _pkt_ctx_) {
805
+ sess_decode = _bind_mqtt_decode(sess_decode);
806
+ sess_encode = _bind_mqtt_encode(sess_encode);
807
+ _pkt_ctx_ = _bind_pkt_ctx(_pkt_ctx_);
808
+
809
+ return mqtt_level => _base_ => {
810
+ _base_ = _base_ || {__proto__: _pkt_ctx_, mqtt_level, get _base_() { return _base_ }};
811
+ return [ sess_decode(_base_), sess_encode(_base_), _base_ ] }
812
+ }
813
+
814
+ function mqtt_session_ctx(mqtt_level) {
815
+ let {ctx} = mqtt_session_ctx;
816
+ if (undefined === ctx ) {
817
+ let as_utf8 = u8 =>
818
+ new TextDecoder('utf-8').decode(u8);
819
+
820
+ let std_pkt_api = {
821
+ utf8(u8) { return as_utf8( u8 || this.payload ) },
822
+ json(u8) { return JSON.parse( this.utf8(u8) || null ) },
823
+ text(u8) { return this.utf8(u8) },
824
+ };
825
+
826
+ mqtt_session_ctx.ctx = ctx =
827
+ _bind_mqtt_session_ctx(
828
+ [ // lst_decode_ops
829
+ mqtt_decode_connack,
830
+ mqtt_decode_disconnect,
831
+ mqtt_decode_publish,
832
+ mqtt_decode_puback,
833
+ mqtt_decode_pubxxx,
834
+ mqtt_decode_pingxxx,
835
+ mqtt_decode_suback,
836
+ mqtt_decode_unsuback,
837
+ mqtt_decode_auth, ],
838
+
839
+ [ // lst_encode_ops
840
+ mqtt_encode_connect,
841
+ mqtt_encode_disconnect,
842
+ mqtt_encode_publish,
843
+ mqtt_encode_puback,
844
+ mqtt_encode_pingxxx,
845
+ mqtt_encode_subscribe,
846
+ mqtt_encode_unsubscribe,
847
+ mqtt_encode_auth, ],
848
+
849
+ std_pkt_api );
850
+ }
851
+
852
+ return ctx(mqtt_level)
853
+ }
854
+
855
+ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
856
+ let q0 = _tiny_deferred_queue();
857
+ let q = _tiny_deferred_queue();
858
+
859
+ let _asy_send = async (...args) =>
860
+ (await q)(...args);
861
+ let _send = client._send = _asy_send;
862
+
863
+ let _ping = () => client._send('pingreq');
864
+ let tid_ping, _is_set = false;
865
+
866
+ return {
867
+ is_live: (() =>_asy_send !== _send)
868
+ , is_set: (() =>_is_set)
869
+
870
+ , reset() {
871
+ tid_ping = clearInterval(tid_ping);
872
+ client._send = _send = _asy_send;
873
+ _is_set = false;
874
+
875
+ // call client.on_reconnect in next promise microtask
876
+ _async_evt(client, client.on_reconnect);}
877
+
878
+ , ping(td) {
879
+ tid_ping = clearInterval(tid_ping);
880
+ if (td) {
881
+ tid_ping = setInterval(_ping, 1000 * td);
882
+ if (tid_ping.unref) {
883
+ tid_ping.unref();} } }
884
+
885
+ , async send_connect(... args) {
886
+ if (_asy_send === _send) {
887
+ _send = await q0;}
888
+
889
+ // await connack response
890
+ let res = await _send(...args);
891
+
892
+ client._send = _send;
893
+ q.notify(_send);
894
+ return res}
895
+
896
+ , set(mqtt_session, send_u8_pkt) {
897
+ _is_set = true;
898
+
899
+ let [mqtt_decode, mqtt_encode] = mqtt_session;
900
+
901
+ let on_mqtt_chunk = u8_buf =>
902
+ on_mqtt(
903
+ mqtt_decode(u8_buf),
904
+ {mqtt: client});
905
+
906
+ _send = async (type, pkt, key) => {
907
+ let res = undefined !== key
908
+ ? pkt_future(key) : true;
909
+
910
+ await send_u8_pkt(
911
+ mqtt_encode(type, pkt));
912
+
913
+ return res};
914
+
915
+
916
+ q0.notify(_send);
917
+
918
+ // call client.on_live in next promise microtask
919
+ _async_evt(client, client.on_live);
920
+
921
+ return on_mqtt_chunk} } }
922
+
923
+
924
+ async function _async_evt(obj, evt) {
925
+ // microtask break lookup
926
+ if (undefined !== evt) {
927
+ await evt.call(obj, await obj);
928
+ }
929
+ }
930
+ function _tiny_deferred_queue() {
931
+ let q = []; // tiny resetting deferred queue
932
+ q.then = y => { q.push(y); };
933
+ q.notify = v => {
934
+ for (let fn of q.splice(0,q.length))
935
+ fn(v); };
936
+ return q
937
+ }
938
+
939
+ function parse(str, loose) {
940
+ if (str instanceof RegExp) return { keys:false, pattern:str };
941
+ var c, o, tmp, ext, keys=[], pattern='', arr = str.split('/');
942
+ arr[0] || arr.shift();
943
+
944
+ while (tmp = arr.shift()) {
945
+ c = tmp[0];
946
+ if (c === '*') {
947
+ keys.push('wild');
948
+ pattern += '/(.*)';
949
+ } else if (c === ':') {
950
+ o = tmp.indexOf('?', 1);
951
+ ext = tmp.indexOf('.', 1);
952
+ keys.push( tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length) );
953
+ pattern += !!~o && !~ext ? '(?:/([^/]+?))?' : '/([^/]+?)';
954
+ if (!!~ext) pattern += (!!~o ? '?' : '') + '\\' + tmp.substring(ext);
955
+ } else {
956
+ pattern += '/' + tmp;
957
+ }
958
+ }
959
+
960
+ return {
961
+ keys: keys,
962
+ pattern: new RegExp('^' + pattern + (loose ? '(?=$|\/)' : '\/?$'), 'i')
963
+ };
964
+ }
965
+
966
+ // Use [regexparam][] for url-like topic parsing
967
+
968
+ function _ignore(pkt, params, ctx) {ctx.done = true;}
969
+
970
+ function _mqtt_topic_router() {
971
+ let pri_lsts = [[],[]];
972
+ let find = topic => _mqtt_routes_iter(pri_lsts, topic);
973
+
974
+ return {find,
975
+
976
+ add(topic_route, ...args) {
977
+ let fn = args.pop();
978
+ let priority = args.pop();
979
+
980
+ if ('function' !== typeof fn) {
981
+ if (false === fn) {
982
+ fn = _ignore;}
983
+ else throw new TypeError()}
984
+
985
+ let rte = parse(
986
+ topic_route.replace(/[+#]$/, '*'));
987
+
988
+ rte.key = topic_route;
989
+ rte.tgt = fn;
990
+ pri_lsts[priority ? 0 : 1].push(rte);
991
+ return this}
992
+
993
+ , remove(topic_route, priority) {
994
+ let lst = pri_lsts[priority ? 0 : 1];
995
+ lst = lst.filter(e => e.key !== topic_route);
996
+ pri_lsts[priority ? 0 : 1] = lst;}
997
+
998
+ , clear(priority) {
999
+ pri_lsts[priority ? 0 : 1] = [];
1000
+ if (null == priority) {
1001
+ pri_lsts[1] = [];} }
1002
+
1003
+ , async invoke(pkt, ctx) {
1004
+ ctx.idx = 0;
1005
+
1006
+ for (let [fn, params] of find(pkt.topic)) {
1007
+ await fn(pkt, params, ctx);
1008
+
1009
+ if (ctx.done) {
1010
+ break}
1011
+ else ctx.idx++;}
1012
+
1013
+ let {pkt_id, qos} = pkt;
1014
+ if (1 === qos) {
1015
+ await ctx.mqtt._send('puback', {pkt_id});} } } }
1016
+
1017
+
1018
+ function * _mqtt_routes_iter(all_route_lists, topic) {
1019
+ for (let route_list of all_route_lists) {
1020
+ for (let route of route_list) {
1021
+ let res = _mqtt_route_match_one(topic, route);
1022
+ if (undefined !== res) {
1023
+ yield res;} } } }
1024
+
1025
+
1026
+ function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
1027
+ let match = '/' !== topic[0]
1028
+ ? pattern.exec('/'+topic)
1029
+ : pattern.exec(topic);
1030
+
1031
+ if (null === match) {
1032
+ return}
1033
+
1034
+ if (false === keys) {
1035
+ let {groups} = match;
1036
+ if (! groups) {
1037
+ return [tgt]}
1038
+
1039
+ let params = {};
1040
+ for (let k in groups) {
1041
+ params[k] = groups[k];}
1042
+
1043
+ return [tgt, params]}
1044
+
1045
+ if (0 === keys.length) {
1046
+ return [tgt]}
1047
+
1048
+ let params = {};
1049
+ for (let i=0; i<keys.length; i++) {
1050
+ params[ keys[i] ] = match[1+i];}
1051
+ return [tgt, params]}
1052
+
1053
+ const _mqtt_cmdid_dispatch ={
1054
+ create(target) {
1055
+ return {__proto__: this, target, hashbelt: [new Map()]} }
1056
+
1057
+ , bind_pkt_future(_pkt_id=100) {
1058
+ let {hashbelt} = this;
1059
+
1060
+ let _tmp_; // use _tmp_ to reuse _by_key closure
1061
+ let _by_key = answer_monad =>
1062
+ hashbelt[0].set(_tmp_, answer_monad);
1063
+
1064
+ return (( pkt_or_key ) => {
1065
+ if ('string' === typeof pkt_or_key) {
1066
+ _tmp_ = pkt_or_key;}
1067
+ else {
1068
+ _pkt_id = (_pkt_id + 1) & 0xffff;
1069
+ _tmp_ = pkt_or_key.pkt_id = _pkt_id;}
1070
+
1071
+ return new Promise(_by_key)}) }
1072
+
1073
+ , answer(key, pkt) {
1074
+ for (let map of this.hashbelt) {
1075
+ let answer_monad = map.get(key);
1076
+ if (undefined !== answer_monad) {
1077
+ map.delete(key);
1078
+
1079
+ answer_monad([pkt, /*err*/]); // option/maybe monad
1080
+ return true} }
1081
+ return false}
1082
+
1083
+ , rotate_belt(n) {
1084
+ let {hashbelt} = this;
1085
+ hashbelt.unshift(new Map());
1086
+ for (let old of hashbelt.splice(n || 5)) {
1087
+ for (let answer_monad of old.values()) {
1088
+ answer_monad([/*pkt*/, 'expired']); } } }// option/maybe monad
1089
+
1090
+ , cmdids: ((() => {
1091
+ return [
1092
+ (() =>{} )// 0x0 reserved
1093
+ , by_evt // 0x1 connect
1094
+ , by_type // 0x2 connack
1095
+ , by_evt // 0x3 publish
1096
+ , by_id // 0x4 puback
1097
+ , by_id // 0x5 pubrec
1098
+ , by_id // 0x6 pubrel
1099
+ , by_id // 0x7 pubcomp
1100
+ , by_evt // 0x8 subscribe
1101
+ , by_id // 0x9 suback
1102
+ , by_evt // 0xa unsubscribe
1103
+ , by_id // 0xb unsuback
1104
+ , by_type // 0xc pingreq
1105
+ , by_type // 0xd pingresp
1106
+ , by_evt // 0xe disconnect
1107
+ , by_type ]// 0xf auth
1108
+
1109
+
1110
+ function by_id(disp, pkt) {
1111
+ disp.answer(pkt.pkt_id, pkt); }
1112
+
1113
+ function by_type(disp, pkt, ctx) {
1114
+ disp.answer(pkt.type, pkt);
1115
+ by_evt(disp, pkt, ctx);}
1116
+
1117
+ async function by_evt({target}, pkt, ctx) {
1118
+ let fn = target[`mqtt_${pkt.type}`]
1119
+ || target.mqtt_pkt;
1120
+
1121
+ if (undefined !== fn) {
1122
+ await fn.call(target, pkt, ctx);} } })()) };
1123
+
1124
+ function _mqtt_dispatch(opt, target) {
1125
+ let _disp_ = _mqtt_cmdid_dispatch.create(target);
1126
+ let { cmdids } = _disp_;
1127
+
1128
+ // default rotate at 1s across 5 buckets
1129
+ let { td: rotate_td=1000, n: rotate_n=5 } =
1130
+ opt && opt.rotate || {};
1131
+
1132
+ let rotate_ts = rotate_td + Date.now();
1133
+
1134
+ return [on_mqtt,
1135
+ _disp_.bind_pkt_future()]
1136
+
1137
+ function on_mqtt(pkt_list, ctx) {
1138
+ for (let pkt of pkt_list) {
1139
+ cmdids[pkt.id](_disp_, pkt, ctx); }
1140
+
1141
+ if (Date.now() > rotate_ts) {
1142
+ _disp_.rotate_belt(rotate_n);
1143
+ rotate_ts = rotate_td + Date.now();} } }
1144
+
1145
+ class MQTTBaseClient {
1146
+ constructor(opt={}) {
1147
+ this._conn_ = _mqtt_conn(this,
1148
+ this._init_dispatch(opt, this)); }
1149
+
1150
+ // Handshaking Packets
1151
+
1152
+ async connect(pkt={}) {
1153
+ let {client_id: cid} = pkt;
1154
+ if (! cid) {
1155
+ pkt.client_id = cid = this.init_client_id(['u8-mqtt--', '']);}
1156
+ else if (Array.isArray(cid)) {
1157
+ pkt.client_id = cid = this.init_client_id(cid);}
1158
+ else {this.client_id = cid;}
1159
+
1160
+ if (null == pkt.keep_alive) {
1161
+ pkt.keep_alive = 60;}
1162
+
1163
+ let res = await this._conn_
1164
+ .send_connect('connect', pkt, 'connack');
1165
+
1166
+ // TODO: merge with server's keep_alive frequency
1167
+ this._conn_.ping(pkt.keep_alive);
1168
+ return res}
1169
+
1170
+ async disconnect(pkt={}) {
1171
+ let res = await this._send('disconnect', pkt);
1172
+ this._conn_.reset();
1173
+ return res}
1174
+
1175
+ auth(pkt={}) {
1176
+ return this._send('auth', pkt, 'auth')}
1177
+
1178
+ ping() {return this._send('pingreq', null, 'pingresp')}
1179
+
1180
+
1181
+ // alias: sub
1182
+ subscribe(pkt, ex) {
1183
+ pkt = _as_topics(pkt, ex);
1184
+ return this._send('subscribe', pkt, pkt)}
1185
+
1186
+ // alias: unsub
1187
+ unsubscribe(pkt, ex) {
1188
+ pkt = _as_topics(pkt, ex);
1189
+ return this._send('unsubscribe', pkt, pkt)}
1190
+
1191
+ get on_topic() {return this.router.add}
1192
+
1193
+ // alias: sub_topic
1194
+ subscribe_topic(topic_route, ...args) {
1195
+ let topic = this.topic_for(topic_route);
1196
+ this.router.add(topic_route, true, args.pop() );// handler
1197
+ this.subscribe([[ topic ]], args.pop() );// ex
1198
+ return this}
1199
+
1200
+ // alias: unsub_topic
1201
+ unsubscribe_topic(topic_route) {
1202
+ let topic = this.topic_for(topic_route);
1203
+ this.router.remove(topic_route, true);
1204
+ return this.unsubscribe([[ topic ]]) }
1205
+
1206
+ topic_for(topic_route) {
1207
+ return topic_route.replace(/[:*].*$/, '#')}
1208
+
1209
+
1210
+ // alias: pub
1211
+ publish(pkt, fn_encode) {return _pub(this, pkt, fn_encode)}
1212
+ post(topic, payload) {return _pub.m(this, topic, payload)}
1213
+ send(topic, payload) {return _pub.mq(this, topic, payload)}
1214
+ store(topic, payload) {return _pub.mqr(this, topic, payload)}
1215
+
1216
+ json_post(topic, msg) {return _pub.o(this, topic, msg)}
1217
+ json_send(topic, msg) {return _pub.oq(this, topic, msg)}
1218
+ json_store(topic, msg) {return _pub.oqr(this, topic, msg)}
1219
+
1220
+ obj_post(topic, msg, fn_encode) {return _pub.o(this, topic, msg, fn_encode)}
1221
+ obj_send(topic, msg, fn_encode) {return _pub.oq(this, topic, msg, fn_encode)}
1222
+ obj_store(topic, msg, fn_encode) {return _pub.oqr(this, topic, msg, fn_encode)}
1223
+
1224
+
1225
+
1226
+ // Utility Methods
1227
+
1228
+ init_client_id(parts) {
1229
+ let cid = this.client_id;
1230
+
1231
+ if (undefined === cid) {
1232
+ this.client_id = cid = (
1233
+
1234
+
1235
+
1236
+ this.new_client_id(parts)
1237
+ );}
1238
+
1239
+ return cid}
1240
+
1241
+ new_client_id(parts) {
1242
+ return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
1243
+
1244
+
1245
+
1246
+
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+
1253
+
1254
+ // Internal API
1255
+
1256
+ /* async _send(type, pkt) -- provided by _conn_ and transport */
1257
+
1258
+ _init_router(opt) {
1259
+ return this.router = _mqtt_topic_router()}
1260
+
1261
+ _init_dispatch(opt) {
1262
+ let router = this._init_router(opt, this);
1263
+
1264
+ let tgt ={
1265
+ __proto__: opt.on_mqtt_type || {}
1266
+ , router};
1267
+
1268
+ if (! tgt.mqtt_publish) {
1269
+ tgt.mqtt_publish = router.invoke;}
1270
+
1271
+ return _mqtt_dispatch(this, tgt)} }
1272
+
1273
+
1274
+ {
1275
+ let p = MQTTBaseClient.prototype;
1276
+ Object.assign(p,{
1277
+ pub: p.publish
1278
+ , sub: p.subscribe
1279
+ , unsub: p.unsubscribe
1280
+ , sub_topic: p.subscribe_topic
1281
+ , unsub_topic: p.unsubscribe_topic} );
1282
+
1283
+ /*
1284
+ p.on_mqtt_type = {
1285
+ mqtt_auth(pkt, ctx) ::
1286
+ mqtt_connect(pkt, ctx) ::
1287
+ mqtt_connack(pkt, ctx) ::
1288
+ mqtt_disconnect(pkt, ctx) ::
1289
+
1290
+ mqtt_publish(pkt, ctx)
1291
+ mqtt_subscribe(pkt, ctx) ::
1292
+ mqtt_unsubscribe(pkt, ctx) ::
1293
+
1294
+ mqtt_pingreq(pkt, ctx) ::
1295
+ mqtt_pingresp(pkt, ctx) ::
1296
+ }
1297
+ */}
1298
+
1299
+
1300
+ function _as_topics(pkt, ex) {
1301
+ if ('string' === typeof pkt) {
1302
+ return {topics:[pkt], ... ex}}
1303
+ if (pkt[Symbol.iterator]) {
1304
+ return {topics:[... pkt], ... ex}}
1305
+ return ex ? {...pkt, ...ex} : pkt}
1306
+
1307
+
1308
+ async function _pub(self, pkt, fn_encode) {
1309
+ if (undefined === pkt.payload) {
1310
+ let {msg} = pkt;
1311
+ switch (typeof msg) {
1312
+ case 'function':
1313
+ fn_encode = msg;
1314
+ msg = undefined;
1315
+
1316
+ case 'undefined':
1317
+ let arg = pkt.arg || 'payload';
1318
+ return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
1319
+
1320
+ default:
1321
+ pkt.payload = fn_encode
1322
+ ? await fn_encode(msg)
1323
+ : JSON.stringify(msg);} }
1324
+
1325
+ return self._send('publish', pkt,
1326
+ pkt.qos ? pkt : void 0 ) }// key
1327
+
1328
+ {
1329
+ Object.assign(_pub,{
1330
+ m: (self, topic, payload) =>
1331
+ _pub(self, {topic, payload, qos:0})
1332
+ , mq: (self, topic, payload) =>
1333
+ _pub(self, {topic, payload, qos:1})
1334
+ , mqr: (self, topic, payload) =>
1335
+ _pub(self, {topic, payload, qos:1, retain: 1})
1336
+
1337
+ , o: (self, topic, msg, fn_encode) =>
1338
+ _pub(self, {topic, msg, arg: 'msg', qos:0}, fn_encode)
1339
+ , oq: (self, topic, msg, fn_encode) =>
1340
+ _pub(self, {topic, msg, arg: 'msg', qos:1}, fn_encode)
1341
+ , oqr: (self, topic, msg, fn_encode) =>
1342
+ _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, fn_encode)} ); }
1343
+
1344
+ class MQTTCoreClient extends MQTTBaseClient {
1345
+ static _with_session(mqtt_session) {
1346
+ this.prototype._mqtt_session = mqtt_session;}
1347
+
1348
+ constructor(opt={}) {
1349
+ super(opt);
1350
+ this.with_live(opt.on_live);
1351
+ this.with_reconnect(opt.on_reconnect);}
1352
+
1353
+
1354
+ // on_live(client) ::
1355
+ with_live(on_live) {
1356
+ if (on_live) {
1357
+ this.on_live = on_live;}
1358
+
1359
+ return this}
1360
+
1361
+ // on_reconnect(client) ::
1362
+ with_reconnect(on_reconnect) {
1363
+ if (on_reconnect) {
1364
+ this.on_reconnect = on_reconnect;
1365
+
1366
+ if (! this._conn_.is_set) {
1367
+ on_reconnect(this);} }
1368
+
1369
+ return this}
1370
+
1371
+
1372
+ with_async_iter(async_iter, write_u8_pkt) {
1373
+ let on_mqtt_chunk = this._conn_.set(
1374
+ this._mqtt_session(),
1375
+ write_u8_pkt);
1376
+
1377
+ this._msg_loop = ((async () => {
1378
+ async_iter = await async_iter;
1379
+ for await (let chunk of async_iter)
1380
+ on_mqtt_chunk(chunk);
1381
+
1382
+ this._conn_.reset();})());
1383
+
1384
+ return this}
1385
+
1386
+
1387
+
1388
+
1389
+
1390
+
1391
+
1392
+
1393
+
1394
+
1395
+
1396
+
1397
+
1398
+
1399
+
1400
+
1401
+
1402
+
1403
+ with_tcp(port, hostname) {
1404
+ if (!Number.isFinite(port)) {
1405
+ ({port, hostname} = new URL(port));}
1406
+
1407
+ let sock = connect(port, hostname);
1408
+ return this.with_stream(sock)}
1409
+
1410
+
1411
+
1412
+ with_stream(read_stream, write_stream) {
1413
+ if (undefined === write_stream) {
1414
+ write_stream = read_stream;}
1415
+
1416
+ return this.with_async_iter(read_stream,
1417
+ u8_pkt => write_stream.write(u8_pkt)) }
1418
+
1419
+
1420
+
1421
+
1422
+ with_websock(websock) {
1423
+ if (null == websock) {
1424
+ websock = 'ws://127.0.0.1:9001';}
1425
+
1426
+ if (websock.origin || 'string' === typeof websock) {
1427
+ websock = new WebSocket(new URL(websock), ['mqtt']);}
1428
+
1429
+ websock.binaryType = 'arraybuffer';
1430
+
1431
+ let ready, {readyState} = websock;
1432
+ if (1 !== readyState) {
1433
+ if (0 !== readyState) {
1434
+ throw new Error('Invalid WebSocket readyState') }
1435
+
1436
+ ready = new Promise( y =>
1437
+ websock.addEventListener('open', y, {once: true}));}
1438
+
1439
+
1440
+ let {_conn_} = this;
1441
+ let on_mqtt_chunk = _conn_.set(
1442
+ this._mqtt_session(),
1443
+ async u8_pkt =>(
1444
+ await ready
1445
+ , websock.send(u8_pkt)) );
1446
+
1447
+ websock.addEventListener('close',
1448
+ (() => {
1449
+ delete websock.onmessage;
1450
+ _conn_.reset();})
1451
+
1452
+ , {once: true});
1453
+
1454
+ websock.onmessage = evt =>
1455
+ on_mqtt_chunk(
1456
+ new Uint8Array(evt.data));
1457
+
1458
+ return this} }
1459
+
1460
+ class MQTTClient_v5 extends MQTTCoreClient {
1461
+ _mqtt_session() { return mqtt_session_ctx(5)() }
1462
+ }
1463
+
1464
+ const mqtt_v5 = opt => new MQTTClient_v5(opt);
1465
+
1466
+ export { MQTTClient_v5 as MQTTClient, MQTTClient_v5, mqtt_v5 as default, mqtt_v5 as mqtt, mqtt_v5 };
1467
+ //# sourceMappingURL=v5.js.map