u8-mqtt 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/cjs/basic-v4.cjs +103 -137
  2. package/cjs/basic-v4.cjs.map +1 -1
  3. package/cjs/basic-v5.cjs +102 -136
  4. package/cjs/basic-v5.cjs.map +1 -1
  5. package/cjs/index.cjs +144 -189
  6. package/cjs/index.cjs.map +1 -1
  7. package/cjs/v4.cjs +145 -190
  8. package/cjs/v4.cjs.map +1 -1
  9. package/cjs/v5.cjs +144 -189
  10. package/cjs/v5.cjs.map +1 -1
  11. package/code/_dispatch.jsy +16 -0
  12. package/code/base.jsy +68 -124
  13. package/code/core.jsy +15 -9
  14. package/code/router_path.jsy +24 -43
  15. package/code/with_topic_router.jsy +18 -10
  16. package/esm/basic-v4.js +1154 -0
  17. package/esm/basic-v4.js.map +1 -0
  18. package/esm/basic-v5.js +1416 -0
  19. package/esm/basic-v5.js.map +1 -0
  20. package/esm/deno/basic-v4.js +103 -137
  21. package/esm/deno/basic-v4.js.map +1 -1
  22. package/esm/deno/basic-v5.js +102 -136
  23. package/esm/deno/basic-v5.js.map +1 -1
  24. package/esm/deno/index.js +144 -189
  25. package/esm/deno/index.js.map +1 -1
  26. package/esm/deno/v4.js +145 -190
  27. package/esm/deno/v4.js.map +1 -1
  28. package/esm/deno/v5.js +144 -189
  29. package/esm/deno/v5.js.map +1 -1
  30. package/esm/index.js +1599 -0
  31. package/esm/index.js.map +1 -0
  32. package/esm/node/basic-v4.js +103 -137
  33. package/esm/node/basic-v4.js.map +1 -1
  34. package/esm/node/basic-v4.mjs +103 -137
  35. package/esm/node/basic-v4.mjs.map +1 -1
  36. package/esm/node/basic-v5.js +102 -136
  37. package/esm/node/basic-v5.js.map +1 -1
  38. package/esm/node/basic-v5.mjs +102 -136
  39. package/esm/node/basic-v5.mjs.map +1 -1
  40. package/esm/node/index.js +144 -189
  41. package/esm/node/index.js.map +1 -1
  42. package/esm/node/index.mjs +144 -189
  43. package/esm/node/index.mjs.map +1 -1
  44. package/esm/node/v4.js +145 -190
  45. package/esm/node/v4.js.map +1 -1
  46. package/esm/node/v4.mjs +145 -190
  47. package/esm/node/v4.mjs.map +1 -1
  48. package/esm/node/v5.js +144 -189
  49. package/esm/node/v5.js.map +1 -1
  50. package/esm/node/v5.mjs +144 -189
  51. package/esm/node/v5.mjs.map +1 -1
  52. package/esm/v4.js +1336 -0
  53. package/esm/v4.js.map +1 -0
  54. package/esm/v5.js +1599 -0
  55. package/esm/v5.js.map +1 -0
  56. package/esm/web/basic-v4.js +103 -137
  57. package/esm/web/basic-v4.js.map +1 -1
  58. package/esm/web/basic-v4.min.js +1 -1
  59. package/esm/web/basic-v4.min.js.br +0 -0
  60. package/esm/web/basic-v4.min.js.gz +0 -0
  61. package/esm/web/basic-v5.js +102 -136
  62. package/esm/web/basic-v5.js.map +1 -1
  63. package/esm/web/basic-v5.min.js +1 -1
  64. package/esm/web/basic-v5.min.js.br +0 -0
  65. package/esm/web/basic-v5.min.js.gz +0 -0
  66. package/esm/web/index.js +144 -189
  67. package/esm/web/index.js.map +1 -1
  68. package/esm/web/index.min.js +1 -1
  69. package/esm/web/index.min.js.br +0 -0
  70. package/esm/web/index.min.js.gz +0 -0
  71. package/esm/web/v4.js +145 -190
  72. package/esm/web/v4.js.map +1 -1
  73. package/esm/web/v4.min.js +1 -1
  74. package/esm/web/v4.min.js.br +0 -0
  75. package/esm/web/v4.min.js.gz +0 -0
  76. package/esm/web/v5.js +144 -189
  77. package/esm/web/v5.js.map +1 -1
  78. package/esm/web/v5.min.js +1 -1
  79. package/esm/web/v5.min.js.br +0 -0
  80. package/esm/web/v5.min.js.gz +0 -0
  81. package/package.json +7 -8
package/esm/deno/v5.js CHANGED
@@ -147,7 +147,7 @@ class mqtt_reader_v4 {
147
147
 
148
148
  }
149
149
 
150
- class mqtt_reader_v5$1 extends mqtt_reader_v4 {
150
+ let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
151
151
  props() {
152
152
  let {buf, step} = this;
153
153
  let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
@@ -182,7 +182,7 @@ class mqtt_reader_v5$1 extends mqtt_reader_v4 {
182
182
  : buf.subarray(vi, step.k|0)
183
183
  }
184
184
  */
185
- }
185
+ };
186
186
 
187
187
  function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
188
188
  mqtt_reader = class extends mqtt_reader {
@@ -812,31 +812,39 @@ const with_topic_router = mqtt_topic_router =>
812
812
  return super._aliases() +
813
813
  ' sub_topic:subscribe_topic unsub_topic:unsubscribe_topic'}
814
814
 
815
- _init_router(opt, client, target) {
815
+ _init_router(opt, self, target) {
816
+ this._subs = [];
816
817
  let router = this.router = target.router =
817
- mqtt_topic_router(opt, this);
818
+ mqtt_topic_router(opt, this, target);
818
819
  return router?.invoke}
819
- get on_topic() {return this.router.add}
820
820
 
821
- _sub_chain(topic, ex, topic_prefix) {
822
- let res = this.subscribe([[ topic ]], ex, topic_prefix);
823
- let subs = this.subs ||(this.subs = new Map());
824
- subs.set((res.topic = topic), (subs.last = res));
825
- return this }// fluent api -- return this and track side effects
821
+ on_sub(suback, pkt) {
822
+ suback.pkt = pkt;
823
+ this._subs.push(suback);
824
+ return suback}
825
+ subs_settled() {
826
+ return Promise.allSettled(
827
+ this._subs.splice(0,Infinity)) }
826
828
 
827
829
  // alias: sub_topic
828
830
  subscribe_topic(topic_route, ...args) {
829
831
  let router = this.router;
830
832
  router.add(topic_route, true, args.pop() );// handler
831
833
  let topic = router.mqtt_topic(topic_route);
832
- return this._sub_chain(topic, ...args ) }// ex, topic_prefix
834
+ this.subscribe(topic, ...args );// ex, topic_prefix
835
+ return this }// fluent api -- return this and track side effects
833
836
 
834
837
  // alias: unsub_topic
835
838
  unsubscribe_topic(topic_route, ...args) {
836
839
  let router = this.router;
837
840
  router.remove(topic_route, true);
838
841
  let topic = router.mqtt_topic(topic_route);
839
- return this.unsubscribe([[ topic ]], ...args ) } };// topic_prefix
842
+ return this.unsubscribe(topic, ...args ) }// topic_prefix
843
+
844
+ // add topic handlers without corresponding subscribe packet
845
+ on_topic(...args) {
846
+ this.router.add(...args);
847
+ return this} };
840
848
 
841
849
  // Use [regexparam][] for url-like topic parsing
842
850
  // [regexparam]: https://github.com/lukeed/regexparam
@@ -849,7 +857,7 @@ const with_topic_path_router = /* #__PURE__ */
849
857
  const mqtt_topic = topic_route =>
850
858
  topic_route
851
859
  .replace(/[*].*$/, '#')
852
- .replace(/:\w+\??/g, '+');
860
+ .replace(/:\w[^\/]*/g, '+');
853
861
 
854
862
  /* From the [MQTT v5 Spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Topic_Names_and)
855
863
  4.7.1.2 Multi-level wildcard -- (‘#’ U+0023)
@@ -879,9 +887,8 @@ function mqtt_topic_path_router() {
879
887
  let priority = args.pop();
880
888
 
881
889
  if ('function' !== typeof fn) {
882
- if (false === fn) {
883
- fn = _ignore;}
884
- else throw new TypeError()}
890
+ if (fn) {throw new TypeError()}
891
+ fn = _ignore;}
885
892
 
886
893
  let rte = parse(as_topic_path(topic_route));
887
894
 
@@ -897,7 +904,7 @@ function mqtt_topic_path_router() {
897
904
  , clear(priority) {
898
905
  pri_lsts[priority ? 0 : 1] = [];
899
906
  if (null == priority) {
900
- pri_lsts[1] = [];} }
907
+ pri_lsts[1] = []; } }// null clears both lists
901
908
 
902
909
  , async invoke(pkt, ctx) {
903
910
  ctx.idx = 0;
@@ -913,52 +920,34 @@ function mqtt_topic_path_router() {
913
920
  break}
914
921
  else ctx.idx++;}
915
922
 
916
- let {pkt_id, qos} = pkt;
917
- if (1 === qos) {
918
- await ctx.mqtt._send('puback', {pkt_id});} } } }
923
+ if (1 === pkt.qos) {
924
+ await ctx.mqtt.puback(pkt);} } } }
919
925
 
920
926
 
921
927
  function * _routes_iter(all_route_lists, topic) {
928
+ topic = topic.replace(/^[\/]*/, '/'); // ensure '/' prefix for regexparam library
922
929
  for (let route_list of all_route_lists) {
923
- for (let route of route_list) {
924
- let res = _route_match_one(topic, route);
925
- if (undefined !== res) {
926
- yield res;} } } }
927
-
928
-
929
- function _route_match_one(topic, {keys, pattern, tgt}) {
930
- let match = '/' !== topic[0]
931
- ? pattern.exec('/'+topic)
932
- : pattern.exec(topic);
933
-
934
- if (null === match) {
935
- return}
936
-
937
- if (false === keys) {
938
- let {groups} = match;
939
- if (! groups) {
940
- return [tgt]}
941
-
942
- let params = {};
943
- for (let k in groups) {
944
- params[k] = groups[k];}
945
-
946
- return [tgt, params]}
947
-
948
- if (0 === keys.length) {
949
- return [tgt]}
950
-
951
- let params = {};
952
- for (let i=0; i<keys.length; i++) {
953
- params[ keys[i] ] = match[1+i];}
954
- return [tgt, params]}
930
+ for (let {keys, pattern, tgt} of route_list) {
931
+ let match = pattern.exec(topic);
932
+ if (match) {
933
+ let params = keys
934
+ ? keys.reduce(
935
+ (o, k, i) => (o[k] = match[1+i], o)
936
+ , {})
937
+ : match.groups ?? match;
938
+ yield [tgt, params];} } } }
955
939
 
956
940
 
957
941
  function _route_remove(all_route_lists, query) {
958
- let match = route => route===query || route.tgt===query || route.key===query;
942
+ let fn_match = route =>(
943
+ route===query
944
+ || route.tgt===query
945
+ || route.key===query);
959
946
  for (let lst of all_route_lists) {
960
- let i = lst.findIndex(match);
961
- if (0 <= i) {return !! lst.splice(i,1)} }
947
+ let i = lst.findIndex(fn_match);
948
+ if (0 <= i) {
949
+ lst.splice(i,1);
950
+ return true} }
962
951
  return false}
963
952
 
964
953
  /*
@@ -1219,6 +1208,22 @@ const _mqtt_cmdid_dispatch ={
1219
1208
 
1220
1209
  await fn?.call(target, pkt, ctx);} })()) };
1221
1210
 
1211
+ /*
1212
+ 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_publish(pkt, ctx)
1219
+ mqtt_subscribe(pkt, ctx) ::
1220
+ mqtt_unsubscribe(pkt, ctx) ::
1221
+
1222
+ mqtt_pingreq(pkt, ctx) ::
1223
+ mqtt_pingresp(pkt, ctx) ::
1224
+ }
1225
+ */
1226
+
1222
1227
  function _mqtt_dispatch(opt, target) {
1223
1228
  let _disp_ = _mqtt_cmdid_dispatch.create(target);
1224
1229
  let { cmdids } = _disp_;
@@ -1248,28 +1253,34 @@ class MQTTError extends Error {
1248
1253
 
1249
1254
  class MQTTBase {
1250
1255
  constructor(opt={}) {
1256
+ this.with(opt);
1251
1257
  this._conn_ = _mqtt_conn(this,
1252
1258
  this._init_dispatch(opt, this)); }
1253
1259
 
1260
+ with(fns_ns) {
1261
+ for (let [k,v] of Object.entries(fns_ns)) {
1262
+ if ('function' === typeof v) {this[k] = v;} }
1263
+ return this}
1264
+
1254
1265
  async conn_emit(evt, arg, err_arg) {
1255
1266
  this.log_conn?.(evt, arg, err_arg);
1256
1267
  try {
1257
- let fn_evt = this[await evt]; // microtask break
1268
+ let fn_evt = this[await evt]; // microtask break using `await evt`
1258
1269
  if (fn_evt) {
1259
1270
  await fn_evt.call(this, this, arg, err_arg);}
1260
- else if (err_arg) {
1261
- await this.on_error(err_arg, evt);} }
1271
+ else if (err_arg) {throw err_arg} }
1262
1272
  catch (err) {
1263
1273
  this.on_error(err, evt);} }
1264
1274
 
1265
- on_error(err, err_path) {
1266
- console.warn('[[u8-mqtt error: %s]]', err_path, err); }
1275
+ on_error(err, evt) {
1276
+ console.warn('[[u8-mqtt error: %s]]', evt, err); }
1267
1277
 
1268
1278
  // Handshaking Packets
1269
1279
 
1270
1280
  async connect(pkt={}) {
1271
- let cid = pkt.client_id || ['u8-mqtt--', ''];
1272
- if (Array.isArray(cid)) {
1281
+ let cid = pkt.client_id || this.client_id;
1282
+ if ('string' !== typeof cid) {
1283
+ // see init_client_id implementation in core.jsy
1273
1284
  pkt.client_id = cid = this.init_client_id(cid);}
1274
1285
  this.client_id = cid;
1275
1286
 
@@ -1295,12 +1306,13 @@ class MQTTBase {
1295
1306
  return this._send('auth', pkt, 'auth')}
1296
1307
 
1297
1308
  ping() {return this._send('pingreq', null, 'pingresp')}
1298
-
1309
+ puback({pkt_id}) {return this._send('puback', {pkt_id})}
1299
1310
 
1300
1311
  // alias: sub
1301
1312
  subscribe(pkt, ex, topic_prefix) {
1302
1313
  pkt = _as_topics(pkt, ex, topic_prefix);
1303
- return this._send('subscribe', pkt, pkt)}
1314
+ let suback = this._send('subscribe', pkt, pkt);
1315
+ return this.on_sub?.(suback, pkt) ?? suback}
1304
1316
 
1305
1317
  // alias: unsub
1306
1318
  unsubscribe(pkt, ex, topic_prefix) {
@@ -1308,48 +1320,52 @@ class MQTTBase {
1308
1320
  return this._send('unsubscribe', pkt, pkt)}
1309
1321
 
1310
1322
 
1311
- // alias: pub
1312
- publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
1313
- post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
1314
- send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
1315
- store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
1316
-
1317
- json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1318
- json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1319
- json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1320
-
1321
- obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1322
- obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1323
- obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1324
-
1325
-
1326
-
1327
- // Utility Methods
1328
-
1329
- init_client_id(parts) {
1330
- let cid = this.client_id;
1331
-
1332
- if (undefined === cid) {
1333
- this.client_id = cid = (
1334
-
1335
-
1336
-
1337
- this.new_client_id(parts)
1338
- );}
1339
-
1340
- return cid}
1341
-
1342
- new_client_id(parts) {
1343
- return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
1344
-
1345
-
1346
-
1347
-
1348
-
1349
-
1350
-
1351
-
1352
-
1323
+ post(topic, payload, pub_opt) {// qos:0
1324
+ return this.pub({topic, payload, qos:0}, pub_opt)}
1325
+ send(topic, payload, pub_opt) {// qos:1
1326
+ return this.pub({topic, payload, qos:1}, pub_opt)}
1327
+ store(topic, payload, pub_opt) {// qos:1, retain: 1
1328
+ return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
1329
+
1330
+ // alias: json_post
1331
+ obj_post(topic, msg, pub_opt) {// qos:0
1332
+ return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
1333
+ // alias: json_send
1334
+ obj_send(topic, msg, pub_opt) {// qos:1
1335
+ return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
1336
+ // alias: json_store
1337
+ obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
1338
+ return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
1339
+
1340
+ // alias: publish -- because 'pub' is shorter for semantic aliases above
1341
+ async pub(pkt, pub_opt) {
1342
+ if (undefined === pkt.payload) {
1343
+ if ('function' === typeof pub_opt) {
1344
+ pub_opt = {fn_encode: pub_opt};}
1345
+
1346
+ let {msg} = pkt;
1347
+ switch (typeof msg) {
1348
+ case 'function':
1349
+ pub_opt = {...pub_opt, fn_encode: msg};
1350
+ // flow into 'undefined' case
1351
+ case 'undefined':
1352
+ // return a single-value closure to publish packets
1353
+ return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
1354
+
1355
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1356
+ let {fn_encode} = pub_opt || {};
1357
+ pkt.payload = fn_encode
1358
+ ? await fn_encode(msg)
1359
+ : JSON.stringify(msg);}
1360
+
1361
+ if (pub_opt) {
1362
+ if (pub_opt.props) {
1363
+ pkt.props = pub_opt.props;}
1364
+ if (pub_opt.xform) {
1365
+ pkt = pub_opt.xform(pkt) || pkt;} }
1366
+
1367
+ return this._send('publish', pkt,
1368
+ pkt.qos ? pkt : void 0 ) }// key
1353
1369
 
1354
1370
 
1355
1371
  // Internal API
@@ -1364,41 +1380,18 @@ class MQTTBase {
1364
1380
  return _mqtt_dispatch(this, target)}
1365
1381
 
1366
1382
  static _aliases() {
1367
- return ' pub:publish sub:subscribe unsub:unsubscribe '}
1383
+ return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
1368
1384
 
1369
1385
  static _once_(self=this) {
1370
1386
  self._once_ = _=>0;
1371
- self.MQTTError = MQTTError;
1372
1387
  let p = self.prototype;
1388
+ p.MQTTError = MQTTError;
1373
1389
  for (let alias of self._aliases().split(/\s+/)) {
1374
1390
  alias = alias.split(':');
1375
1391
  let fn = alias[1] && p[alias[1]];
1376
1392
  if (fn) {p[alias[0]] = fn;} } } }
1377
1393
 
1378
1394
 
1379
- /*
1380
- on_mqtt_type = {
1381
- mqtt_auth(pkt, ctx) ::
1382
- mqtt_connect(pkt, ctx) ::
1383
- mqtt_connack(pkt, ctx) ::
1384
- mqtt_disconnect(pkt, ctx) ::
1385
-
1386
- mqtt_publish(pkt, ctx)
1387
- mqtt_subscribe(pkt, ctx) ::
1388
- mqtt_unsubscribe(pkt, ctx) ::
1389
-
1390
- mqtt_pingreq(pkt, ctx) ::
1391
- mqtt_pingresp(pkt, ctx) ::
1392
- }
1393
- */
1394
-
1395
-
1396
- const _prefix_topics = (topic_prefix, iterable) =>
1397
- Array.from(iterable, value =>(
1398
- value.trim // string
1399
- ? _prefix_topics(topic_prefix, value)
1400
- : topic_prefix + value) );
1401
-
1402
1395
  function _as_topics(pkt, ex, topic_prefix) {
1403
1396
  if (ex?.trim) {// string
1404
1397
  topic_prefix = ex;
@@ -1415,55 +1408,11 @@ function _as_topics(pkt, ex, topic_prefix) {
1415
1408
  if (topic_prefix) {
1416
1409
  // particularly useful with shared queues, e.g.
1417
1410
  // topic_prefix = '$share/some-queue-name/'
1418
- pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
1419
- return pkt}
1420
-
1411
+ let _prefix_topics = v =>
1412
+ v.trim ? topic_prefix+v : v.map(_prefix_topics);
1421
1413
 
1422
- async function _pub(self, pkt, pub_opt) {
1423
- if (undefined === pkt.payload) {
1424
- if ('function' === typeof pub_opt) {
1425
- pub_opt = {fn_encode: pub_opt};}
1426
-
1427
- let {msg} = pkt;
1428
- switch (typeof msg) {
1429
- case 'function':
1430
- pub_opt = {...pub_opt, fn_encode: msg};
1431
- // flow into 'undefined' case
1432
- case 'undefined':
1433
- // return a single-value closure to publish packets
1434
- return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
1435
-
1436
- default:
1437
- // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1438
- let {fn_encode} = pub_opt || {};
1439
- pkt.payload = fn_encode
1440
- ? await fn_encode(msg)
1441
- : JSON.stringify(msg);} }
1442
-
1443
- if (pub_opt) {
1444
- if (pub_opt.props) {
1445
- pkt.props = pub_opt.props;}
1446
- if (pub_opt.xform) {
1447
- pkt = pub_opt.xform(pkt) || pkt;} }
1448
-
1449
- return self._send('publish', pkt,
1450
- pkt.qos ? pkt : void 0 ) }// key
1451
-
1452
- {
1453
- Object.assign(_pub,{
1454
- m: (self, topic, payload, pub_opt) =>
1455
- _pub(self, {topic, payload, qos:0}, pub_opt)
1456
- , mq: (self, topic, payload, pub_opt) =>
1457
- _pub(self, {topic, payload, qos:1}, pub_opt)
1458
- , mqr: (self, topic, payload, pub_opt) =>
1459
- _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1460
-
1461
- , o: (self, topic, msg, pub_opt) =>
1462
- _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1463
- , oq: (self, topic, msg, pub_opt) =>
1464
- _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1465
- , oqr: (self, topic, msg, pub_opt) =>
1466
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
1414
+ pkt.topics = pkt.topics.map(_prefix_topics);}
1415
+ return pkt}
1467
1416
 
1468
1417
  const pkt_api = {
1469
1418
  utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
@@ -1472,20 +1421,27 @@ const pkt_api = {
1472
1421
  };
1473
1422
 
1474
1423
  class MQTTCore extends MQTTBase {
1475
- constructor(opt={}) {
1476
- super(opt);
1477
- this.with(opt);}
1478
-
1479
1424
  static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
1480
1425
  let self = class extends this {};
1481
1426
  self.prototype.mqtt_ctx =
1482
1427
  mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
1483
1428
  return self}
1484
1429
 
1485
- with(fns_ns) {
1486
- for (let [k,v] of Object.entries(fns_ns)) {
1487
- if ('function' === typeof v) {this[k] = v;} }
1488
- return this}
1430
+
1431
+ // automatic Client Id for connect()
1432
+ init_client_id(parts=['u8-mqtt--','']) {
1433
+ let sess_stg=this.sess_stg;
1434
+ let key, cid = sess_stg?.getItem(key=parts.join(' '));
1435
+ if (! cid) {
1436
+ cid = parts.join(Math.random().toString(36).slice(2));
1437
+ sess_stg?.setItem(key, cid);}
1438
+ return cid}
1439
+
1440
+ get sess_stg() {return globalThis.sessionStorage}
1441
+
1442
+
1443
+ //on_error(err, evt) ::
1444
+ // console.warn @ '[[u8-mqtt error: %s]]', evt, err
1489
1445
 
1490
1446
  //log_conn(evt, arg, err_arg) ::
1491
1447
  // console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
@@ -1563,7 +1519,6 @@ class MQTTCore extends MQTTBase {
1563
1519
 
1564
1520
 
1565
1521
 
1566
-
1567
1522
 
1568
1523
 
1569
1524
 
@@ -1624,7 +1579,7 @@ class MQTTCore extends MQTTBase {
1624
1579
 
1625
1580
  return this} }
1626
1581
 
1627
- const version = '0.4.1';
1582
+ const version = '0.5.0';
1628
1583
 
1629
1584
  const MQTTClient_v4 = /* #__PURE__ */
1630
1585
  with_topic_path_router(