u8-mqtt 0.4.1 → 0.5.1

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 +14 -9
package/cjs/index.cjs CHANGED
@@ -154,7 +154,7 @@ class mqtt_reader_v4 {
154
154
 
155
155
  }
156
156
 
157
- class mqtt_reader_v5$1 extends mqtt_reader_v4 {
157
+ let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
158
158
  props() {
159
159
  let {buf, step} = this;
160
160
  let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
@@ -189,7 +189,7 @@ class mqtt_reader_v5$1 extends mqtt_reader_v4 {
189
189
  : buf.subarray(vi, step.k|0)
190
190
  }
191
191
  */
192
- }
192
+ };
193
193
 
194
194
  function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
195
195
  mqtt_reader = class extends mqtt_reader {
@@ -819,31 +819,39 @@ const with_topic_router = mqtt_topic_router =>
819
819
  return super._aliases() +
820
820
  ' sub_topic:subscribe_topic unsub_topic:unsubscribe_topic'}
821
821
 
822
- _init_router(opt, client, target) {
822
+ _init_router(opt, self, target) {
823
+ this._subs = [];
823
824
  let router = this.router = target.router =
824
- mqtt_topic_router(opt, this);
825
+ mqtt_topic_router(opt, this, target);
825
826
  return router?.invoke}
826
- get on_topic() {return this.router.add}
827
827
 
828
- _sub_chain(topic, ex, topic_prefix) {
829
- let res = this.subscribe([[ topic ]], ex, topic_prefix);
830
- let subs = this.subs ||(this.subs = new Map());
831
- subs.set((res.topic = topic), (subs.last = res));
832
- return this }// fluent api -- return this and track side effects
828
+ on_sub(suback, pkt) {
829
+ suback.pkt = pkt;
830
+ this._subs.push(suback);
831
+ return suback}
832
+ subs_settled() {
833
+ return Promise.allSettled(
834
+ this._subs.splice(0,Infinity)) }
833
835
 
834
836
  // alias: sub_topic
835
837
  subscribe_topic(topic_route, ...args) {
836
838
  let router = this.router;
837
839
  router.add(topic_route, true, args.pop() );// handler
838
840
  let topic = router.mqtt_topic(topic_route);
839
- return this._sub_chain(topic, ...args ) }// ex, topic_prefix
841
+ this.subscribe(topic, ...args );// ex, topic_prefix
842
+ return this }// fluent api -- return this and track side effects
840
843
 
841
844
  // alias: unsub_topic
842
845
  unsubscribe_topic(topic_route, ...args) {
843
846
  let router = this.router;
844
847
  router.remove(topic_route, true);
845
848
  let topic = router.mqtt_topic(topic_route);
846
- return this.unsubscribe([[ topic ]], ...args ) } };// topic_prefix
849
+ return this.unsubscribe(topic, ...args ) }// topic_prefix
850
+
851
+ // add topic handlers without corresponding subscribe packet
852
+ on_topic(...args) {
853
+ this.router.add(...args);
854
+ return this} };
847
855
 
848
856
  // Use [regexparam][] for url-like topic parsing
849
857
  // [regexparam]: https://github.com/lukeed/regexparam
@@ -856,7 +864,7 @@ const with_topic_path_router = /* #__PURE__ */
856
864
  const mqtt_topic = topic_route =>
857
865
  topic_route
858
866
  .replace(/[*].*$/, '#')
859
- .replace(/:\w+\??/g, '+');
867
+ .replace(/:\w[^\/]*/g, '+');
860
868
 
861
869
  /* From the [MQTT v5 Spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Topic_Names_and)
862
870
  4.7.1.2 Multi-level wildcard -- (‘#’ U+0023)
@@ -886,9 +894,8 @@ function mqtt_topic_path_router() {
886
894
  let priority = args.pop();
887
895
 
888
896
  if ('function' !== typeof fn) {
889
- if (false === fn) {
890
- fn = _ignore;}
891
- else throw new TypeError()}
897
+ if (fn) {throw new TypeError()}
898
+ fn = _ignore;}
892
899
 
893
900
  let rte = parse(as_topic_path(topic_route));
894
901
 
@@ -904,7 +911,7 @@ function mqtt_topic_path_router() {
904
911
  , clear(priority) {
905
912
  pri_lsts[priority ? 0 : 1] = [];
906
913
  if (null == priority) {
907
- pri_lsts[1] = [];} }
914
+ pri_lsts[1] = []; } }// null clears both lists
908
915
 
909
916
  , async invoke(pkt, ctx) {
910
917
  ctx.idx = 0;
@@ -920,52 +927,34 @@ function mqtt_topic_path_router() {
920
927
  break}
921
928
  else ctx.idx++;}
922
929
 
923
- let {pkt_id, qos} = pkt;
924
- if (1 === qos) {
925
- await ctx.mqtt._send('puback', {pkt_id});} } } }
930
+ if (1 === pkt.qos) {
931
+ await ctx.mqtt.puback(pkt);} } } }
926
932
 
927
933
 
928
934
  function * _routes_iter(all_route_lists, topic) {
935
+ topic = topic.replace(/^[\/]*/, '/'); // ensure '/' prefix for regexparam library
929
936
  for (let route_list of all_route_lists) {
930
- for (let route of route_list) {
931
- let res = _route_match_one(topic, route);
932
- if (undefined !== res) {
933
- yield res;} } } }
934
-
935
-
936
- function _route_match_one(topic, {keys, pattern, tgt}) {
937
- let match = '/' !== topic[0]
938
- ? pattern.exec('/'+topic)
939
- : pattern.exec(topic);
940
-
941
- if (null === match) {
942
- return}
943
-
944
- if (false === keys) {
945
- let {groups} = match;
946
- if (! groups) {
947
- return [tgt]}
948
-
949
- let params = {};
950
- for (let k in groups) {
951
- params[k] = groups[k];}
952
-
953
- return [tgt, params]}
954
-
955
- if (0 === keys.length) {
956
- return [tgt]}
957
-
958
- let params = {};
959
- for (let i=0; i<keys.length; i++) {
960
- params[ keys[i] ] = match[1+i];}
961
- return [tgt, params]}
937
+ for (let {keys, pattern, tgt} of route_list) {
938
+ let match = pattern.exec(topic);
939
+ if (match) {
940
+ let params = keys
941
+ ? keys.reduce(
942
+ (o, k, i) => (o[k] = match[1+i], o)
943
+ , {})
944
+ : match.groups ?? match;
945
+ yield [tgt, params];} } } }
962
946
 
963
947
 
964
948
  function _route_remove(all_route_lists, query) {
965
- let match = route => route===query || route.tgt===query || route.key===query;
949
+ let fn_match = route =>(
950
+ route===query
951
+ || route.tgt===query
952
+ || route.key===query);
966
953
  for (let lst of all_route_lists) {
967
- let i = lst.findIndex(match);
968
- if (0 <= i) {return !! lst.splice(i,1)} }
954
+ let i = lst.findIndex(fn_match);
955
+ if (0 <= i) {
956
+ lst.splice(i,1);
957
+ return true} }
969
958
  return false}
970
959
 
971
960
  /*
@@ -1226,6 +1215,22 @@ const _mqtt_cmdid_dispatch ={
1226
1215
 
1227
1216
  await fn?.call(target, pkt, ctx);} })()) };
1228
1217
 
1218
+ /*
1219
+ on_mqtt_type = {
1220
+ mqtt_auth(pkt, ctx) ::
1221
+ mqtt_connect(pkt, ctx) ::
1222
+ mqtt_connack(pkt, ctx) ::
1223
+ mqtt_disconnect(pkt, ctx) ::
1224
+
1225
+ mqtt_publish(pkt, ctx)
1226
+ mqtt_subscribe(pkt, ctx) ::
1227
+ mqtt_unsubscribe(pkt, ctx) ::
1228
+
1229
+ mqtt_pingreq(pkt, ctx) ::
1230
+ mqtt_pingresp(pkt, ctx) ::
1231
+ }
1232
+ */
1233
+
1229
1234
  function _mqtt_dispatch(opt, target) {
1230
1235
  let _disp_ = _mqtt_cmdid_dispatch.create(target);
1231
1236
  let { cmdids } = _disp_;
@@ -1255,28 +1260,34 @@ class MQTTError extends Error {
1255
1260
 
1256
1261
  class MQTTBase {
1257
1262
  constructor(opt={}) {
1263
+ this.with(opt);
1258
1264
  this._conn_ = _mqtt_conn(this,
1259
1265
  this._init_dispatch(opt, this)); }
1260
1266
 
1267
+ with(fns_ns) {
1268
+ for (let [k,v] of Object.entries(fns_ns)) {
1269
+ if ('function' === typeof v) {this[k] = v;} }
1270
+ return this}
1271
+
1261
1272
  async conn_emit(evt, arg, err_arg) {
1262
1273
  this.log_conn?.(evt, arg, err_arg);
1263
1274
  try {
1264
- let fn_evt = this[await evt]; // microtask break
1275
+ let fn_evt = this[await evt]; // microtask break using `await evt`
1265
1276
  if (fn_evt) {
1266
1277
  await fn_evt.call(this, this, arg, err_arg);}
1267
- else if (err_arg) {
1268
- await this.on_error(err_arg, evt);} }
1278
+ else if (err_arg) {throw err_arg} }
1269
1279
  catch (err) {
1270
1280
  this.on_error(err, evt);} }
1271
1281
 
1272
- on_error(err, err_path) {
1273
- console.warn('[[u8-mqtt error: %s]]', err_path, err); }
1282
+ on_error(err, evt) {
1283
+ console.warn('[[u8-mqtt error: %s]]', evt, err); }
1274
1284
 
1275
1285
  // Handshaking Packets
1276
1286
 
1277
1287
  async connect(pkt={}) {
1278
- let cid = pkt.client_id || ['u8-mqtt--', ''];
1279
- if (Array.isArray(cid)) {
1288
+ let cid = pkt.client_id || this.client_id;
1289
+ if ('string' !== typeof cid) {
1290
+ // see init_client_id implementation in core.jsy
1280
1291
  pkt.client_id = cid = this.init_client_id(cid);}
1281
1292
  this.client_id = cid;
1282
1293
 
@@ -1302,12 +1313,13 @@ class MQTTBase {
1302
1313
  return this._send('auth', pkt, 'auth')}
1303
1314
 
1304
1315
  ping() {return this._send('pingreq', null, 'pingresp')}
1305
-
1316
+ puback({pkt_id}) {return this._send('puback', {pkt_id})}
1306
1317
 
1307
1318
  // alias: sub
1308
1319
  subscribe(pkt, ex, topic_prefix) {
1309
1320
  pkt = _as_topics(pkt, ex, topic_prefix);
1310
- return this._send('subscribe', pkt, pkt)}
1321
+ let suback = this._send('subscribe', pkt, pkt);
1322
+ return this.on_sub?.(suback, pkt) ?? suback}
1311
1323
 
1312
1324
  // alias: unsub
1313
1325
  unsubscribe(pkt, ex, topic_prefix) {
@@ -1315,48 +1327,52 @@ class MQTTBase {
1315
1327
  return this._send('unsubscribe', pkt, pkt)}
1316
1328
 
1317
1329
 
1318
- // alias: pub
1319
- publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
1320
- post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
1321
- send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
1322
- store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
1323
-
1324
- json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1325
- json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1326
- json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1327
-
1328
- obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1329
- obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1330
- obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1331
-
1332
-
1333
-
1334
- // Utility Methods
1335
-
1336
- init_client_id(parts) {
1337
- let cid = this.client_id;
1338
-
1339
- if (undefined === cid) {
1340
- this.client_id = cid = (
1341
-
1342
-
1343
-
1344
- this.new_client_id(parts)
1345
- );}
1346
-
1347
- return cid}
1348
-
1349
- new_client_id(parts) {
1350
- return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
1351
-
1352
-
1353
-
1354
-
1355
-
1356
-
1357
-
1358
-
1359
-
1330
+ post(topic, payload, pub_opt) {// qos:0
1331
+ return this.pub({topic, payload, qos:0}, pub_opt)}
1332
+ send(topic, payload, pub_opt) {// qos:1
1333
+ return this.pub({topic, payload, qos:1}, pub_opt)}
1334
+ store(topic, payload, pub_opt) {// qos:1, retain: 1
1335
+ return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
1336
+
1337
+ // alias: json_post
1338
+ obj_post(topic, msg, pub_opt) {// qos:0
1339
+ return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
1340
+ // alias: json_send
1341
+ obj_send(topic, msg, pub_opt) {// qos:1
1342
+ return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
1343
+ // alias: json_store
1344
+ obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
1345
+ return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
1346
+
1347
+ // alias: publish -- because 'pub' is shorter for semantic aliases above
1348
+ async pub(pkt, pub_opt) {
1349
+ if (undefined === pkt.payload) {
1350
+ if ('function' === typeof pub_opt) {
1351
+ pub_opt = {fn_encode: pub_opt};}
1352
+
1353
+ let {msg} = pkt;
1354
+ switch (typeof msg) {
1355
+ case 'function':
1356
+ pub_opt = {...pub_opt, fn_encode: msg};
1357
+ // flow into 'undefined' case
1358
+ case 'undefined':
1359
+ // return a single-value closure to publish packets
1360
+ return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
1361
+
1362
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1363
+ let {fn_encode} = pub_opt || {};
1364
+ pkt.payload = fn_encode
1365
+ ? await fn_encode(msg)
1366
+ : JSON.stringify(msg);}
1367
+
1368
+ if (pub_opt) {
1369
+ if (pub_opt.props) {
1370
+ pkt.props = pub_opt.props;}
1371
+ if (pub_opt.xform) {
1372
+ pkt = pub_opt.xform(pkt) || pkt;} }
1373
+
1374
+ return this._send('publish', pkt,
1375
+ pkt.qos ? pkt : void 0 ) }// key
1360
1376
 
1361
1377
 
1362
1378
  // Internal API
@@ -1371,41 +1387,18 @@ class MQTTBase {
1371
1387
  return _mqtt_dispatch(this, target)}
1372
1388
 
1373
1389
  static _aliases() {
1374
- return ' pub:publish sub:subscribe unsub:unsubscribe '}
1390
+ return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
1375
1391
 
1376
1392
  static _once_(self=this) {
1377
1393
  self._once_ = _=>0;
1378
- self.MQTTError = MQTTError;
1379
1394
  let p = self.prototype;
1395
+ p.MQTTError = MQTTError;
1380
1396
  for (let alias of self._aliases().split(/\s+/)) {
1381
1397
  alias = alias.split(':');
1382
1398
  let fn = alias[1] && p[alias[1]];
1383
1399
  if (fn) {p[alias[0]] = fn;} } } }
1384
1400
 
1385
1401
 
1386
- /*
1387
- on_mqtt_type = {
1388
- mqtt_auth(pkt, ctx) ::
1389
- mqtt_connect(pkt, ctx) ::
1390
- mqtt_connack(pkt, ctx) ::
1391
- mqtt_disconnect(pkt, ctx) ::
1392
-
1393
- mqtt_publish(pkt, ctx)
1394
- mqtt_subscribe(pkt, ctx) ::
1395
- mqtt_unsubscribe(pkt, ctx) ::
1396
-
1397
- mqtt_pingreq(pkt, ctx) ::
1398
- mqtt_pingresp(pkt, ctx) ::
1399
- }
1400
- */
1401
-
1402
-
1403
- const _prefix_topics = (topic_prefix, iterable) =>
1404
- Array.from(iterable, value =>(
1405
- value.trim // string
1406
- ? _prefix_topics(topic_prefix, value)
1407
- : topic_prefix + value) );
1408
-
1409
1402
  function _as_topics(pkt, ex, topic_prefix) {
1410
1403
  if (ex?.trim) {// string
1411
1404
  topic_prefix = ex;
@@ -1422,55 +1415,11 @@ function _as_topics(pkt, ex, topic_prefix) {
1422
1415
  if (topic_prefix) {
1423
1416
  // particularly useful with shared queues, e.g.
1424
1417
  // topic_prefix = '$share/some-queue-name/'
1425
- pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
1426
- return pkt}
1418
+ let _prefix_topics = v =>
1419
+ v.trim ? topic_prefix+v : v.map(_prefix_topics);
1427
1420
 
1428
-
1429
- async function _pub(self, pkt, pub_opt) {
1430
- if (undefined === pkt.payload) {
1431
- if ('function' === typeof pub_opt) {
1432
- pub_opt = {fn_encode: pub_opt};}
1433
-
1434
- let {msg} = pkt;
1435
- switch (typeof msg) {
1436
- case 'function':
1437
- pub_opt = {...pub_opt, fn_encode: msg};
1438
- // flow into 'undefined' case
1439
- case 'undefined':
1440
- // return a single-value closure to publish packets
1441
- return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
1442
-
1443
- default:
1444
- // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1445
- let {fn_encode} = pub_opt || {};
1446
- pkt.payload = fn_encode
1447
- ? await fn_encode(msg)
1448
- : JSON.stringify(msg);} }
1449
-
1450
- if (pub_opt) {
1451
- if (pub_opt.props) {
1452
- pkt.props = pub_opt.props;}
1453
- if (pub_opt.xform) {
1454
- pkt = pub_opt.xform(pkt) || pkt;} }
1455
-
1456
- return self._send('publish', pkt,
1457
- pkt.qos ? pkt : void 0 ) }// key
1458
-
1459
- {
1460
- Object.assign(_pub,{
1461
- m: (self, topic, payload, pub_opt) =>
1462
- _pub(self, {topic, payload, qos:0}, pub_opt)
1463
- , mq: (self, topic, payload, pub_opt) =>
1464
- _pub(self, {topic, payload, qos:1}, pub_opt)
1465
- , mqr: (self, topic, payload, pub_opt) =>
1466
- _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1467
-
1468
- , o: (self, topic, msg, pub_opt) =>
1469
- _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1470
- , oq: (self, topic, msg, pub_opt) =>
1471
- _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1472
- , oqr: (self, topic, msg, pub_opt) =>
1473
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
1421
+ pkt.topics = pkt.topics.map(_prefix_topics);}
1422
+ return pkt}
1474
1423
 
1475
1424
  const pkt_api = {
1476
1425
  utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
@@ -1479,20 +1428,27 @@ const pkt_api = {
1479
1428
  };
1480
1429
 
1481
1430
  class MQTTCore extends MQTTBase {
1482
- constructor(opt={}) {
1483
- super(opt);
1484
- this.with(opt);}
1485
-
1486
1431
  static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
1487
1432
  let self = class extends this {};
1488
1433
  self.prototype.mqtt_ctx =
1489
1434
  mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
1490
1435
  return self}
1491
1436
 
1492
- with(fns_ns) {
1493
- for (let [k,v] of Object.entries(fns_ns)) {
1494
- if ('function' === typeof v) {this[k] = v;} }
1495
- return this}
1437
+
1438
+ // automatic Client Id for connect()
1439
+ init_client_id(parts=['u8-mqtt--','']) {
1440
+ let sess_stg=this.sess_stg;
1441
+ let key, cid = sess_stg?.getItem(key=parts.join(' '));
1442
+ if (! cid) {
1443
+ cid = parts.join(Math.random().toString(36).slice(2));
1444
+ sess_stg?.setItem(key, cid);}
1445
+ return cid}
1446
+
1447
+ get sess_stg() {return globalThis.sessionStorage}
1448
+
1449
+
1450
+ //on_error(err, evt) ::
1451
+ // console.warn @ '[[u8-mqtt error: %s]]', evt, err
1496
1452
 
1497
1453
  //log_conn(evt, arg, err_arg) ::
1498
1454
  // console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
@@ -1569,7 +1525,6 @@ class MQTTCore extends MQTTBase {
1569
1525
 
1570
1526
  with_tcp(...opt) {
1571
1527
  opt = this._conn_opt(opt);
1572
- console.log({opt});
1573
1528
  return this._use_conn (() =>
1574
1529
  this.with_stream(
1575
1530
  node_net.connect(opt)) ) }
@@ -1631,7 +1586,7 @@ class MQTTCore extends MQTTBase {
1631
1586
 
1632
1587
  return this} }
1633
1588
 
1634
- const version = '0.4.1';
1589
+ const version = '0.5.1-node';
1635
1590
 
1636
1591
  const MQTTClient_v4 = /* #__PURE__ */
1637
1592
  with_topic_path_router(