u8-mqtt 0.4.0 → 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 (82) hide show
  1. package/README.md +3 -3
  2. package/cjs/basic-v4.cjs +107 -146
  3. package/cjs/basic-v4.cjs.map +1 -1
  4. package/cjs/basic-v5.cjs +106 -145
  5. package/cjs/basic-v5.cjs.map +1 -1
  6. package/cjs/index.cjs +163 -206
  7. package/cjs/index.cjs.map +1 -1
  8. package/cjs/v4.cjs +164 -207
  9. package/cjs/v4.cjs.map +1 -1
  10. package/cjs/v5.cjs +163 -206
  11. package/cjs/v5.cjs.map +1 -1
  12. package/code/_dispatch.jsy +16 -0
  13. package/code/base.jsy +72 -133
  14. package/code/core.jsy +15 -9
  15. package/code/router_path.jsy +38 -51
  16. package/code/with_topic_router.jsy +20 -11
  17. package/esm/basic-v4.js +1154 -0
  18. package/esm/basic-v4.js.map +1 -0
  19. package/esm/basic-v5.js +1416 -0
  20. package/esm/basic-v5.js.map +1 -0
  21. package/esm/deno/basic-v4.js +107 -146
  22. package/esm/deno/basic-v4.js.map +1 -1
  23. package/esm/deno/basic-v5.js +106 -145
  24. package/esm/deno/basic-v5.js.map +1 -1
  25. package/esm/deno/index.js +163 -206
  26. package/esm/deno/index.js.map +1 -1
  27. package/esm/deno/v4.js +164 -207
  28. package/esm/deno/v4.js.map +1 -1
  29. package/esm/deno/v5.js +163 -206
  30. package/esm/deno/v5.js.map +1 -1
  31. package/esm/index.js +1599 -0
  32. package/esm/index.js.map +1 -0
  33. package/esm/node/basic-v4.js +107 -146
  34. package/esm/node/basic-v4.js.map +1 -1
  35. package/esm/node/basic-v4.mjs +107 -146
  36. package/esm/node/basic-v4.mjs.map +1 -1
  37. package/esm/node/basic-v5.js +106 -145
  38. package/esm/node/basic-v5.js.map +1 -1
  39. package/esm/node/basic-v5.mjs +106 -145
  40. package/esm/node/basic-v5.mjs.map +1 -1
  41. package/esm/node/index.js +163 -206
  42. package/esm/node/index.js.map +1 -1
  43. package/esm/node/index.mjs +163 -206
  44. package/esm/node/index.mjs.map +1 -1
  45. package/esm/node/v4.js +164 -207
  46. package/esm/node/v4.js.map +1 -1
  47. package/esm/node/v4.mjs +164 -207
  48. package/esm/node/v4.mjs.map +1 -1
  49. package/esm/node/v5.js +163 -206
  50. package/esm/node/v5.js.map +1 -1
  51. package/esm/node/v5.mjs +163 -206
  52. package/esm/node/v5.mjs.map +1 -1
  53. package/esm/v4.js +1336 -0
  54. package/esm/v4.js.map +1 -0
  55. package/esm/v5.js +1599 -0
  56. package/esm/v5.js.map +1 -0
  57. package/esm/web/basic-v4.js +107 -146
  58. package/esm/web/basic-v4.js.map +1 -1
  59. package/esm/web/basic-v4.min.js +1 -1
  60. package/esm/web/basic-v4.min.js.br +0 -0
  61. package/esm/web/basic-v4.min.js.gz +0 -0
  62. package/esm/web/basic-v5.js +106 -145
  63. package/esm/web/basic-v5.js.map +1 -1
  64. package/esm/web/basic-v5.min.js +1 -1
  65. package/esm/web/basic-v5.min.js.br +0 -0
  66. package/esm/web/basic-v5.min.js.gz +0 -0
  67. package/esm/web/index.js +163 -206
  68. package/esm/web/index.js.map +1 -1
  69. package/esm/web/index.min.js +1 -1
  70. package/esm/web/index.min.js.br +0 -0
  71. package/esm/web/index.min.js.gz +0 -0
  72. package/esm/web/v4.js +164 -207
  73. package/esm/web/v4.js.map +1 -1
  74. package/esm/web/v4.min.js +1 -1
  75. package/esm/web/v4.min.js.br +0 -0
  76. package/esm/web/v4.min.js.gz +0 -0
  77. package/esm/web/v5.js +163 -206
  78. package/esm/web/v5.js.map +1 -1
  79. package/esm/web/v5.min.js +1 -1
  80. package/esm/web/v5.min.js.br +0 -0
  81. package/esm/web/v5.min.js.gz +0 -0
  82. package/package.json +7 -8
@@ -47,7 +47,7 @@ class U8_Reason extends Number {
47
47
  }
48
48
  }
49
49
 
50
- class mqtt_reader_v4$1 {
50
+ let mqtt_reader_v4$1 = class mqtt_reader_v4 {
51
51
  static of(buf) { return this.prototype.of(buf) }
52
52
  of(buf) {
53
53
  let step = (width, k) => (k=0|step.k, step.k=k+width, k);
@@ -103,7 +103,7 @@ class mqtt_reader_v4$1 {
103
103
  return buf.subarray(step.k|0)
104
104
  }
105
105
 
106
- }
106
+ };
107
107
 
108
108
  function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
109
109
  mqtt_reader = class extends mqtt_reader {
@@ -478,7 +478,7 @@ function mqtt_encode_disconnect(ns, mqtt_writer) {
478
478
  }
479
479
  }
480
480
 
481
- // not a v4 packet: import { mqtt_encode_auth } from './encode/auth.mjs'
481
+ // not a v4 packet: import { mqtt_encode_auth } from './encode/auth.js'
482
482
 
483
483
 
484
484
  const mqtt_decode_v4 = [
@@ -774,6 +774,22 @@ const _mqtt_cmdid_dispatch ={
774
774
 
775
775
  await fn?.call(target, pkt, ctx);} })()) };
776
776
 
777
+ /*
778
+ on_mqtt_type = {
779
+ mqtt_auth(pkt, ctx) ::
780
+ mqtt_connect(pkt, ctx) ::
781
+ mqtt_connack(pkt, ctx) ::
782
+ mqtt_disconnect(pkt, ctx) ::
783
+
784
+ mqtt_publish(pkt, ctx)
785
+ mqtt_subscribe(pkt, ctx) ::
786
+ mqtt_unsubscribe(pkt, ctx) ::
787
+
788
+ mqtt_pingreq(pkt, ctx) ::
789
+ mqtt_pingresp(pkt, ctx) ::
790
+ }
791
+ */
792
+
777
793
  function _mqtt_dispatch(opt, target) {
778
794
  let _disp_ = _mqtt_cmdid_dispatch.create(target);
779
795
  let { cmdids } = _disp_;
@@ -803,28 +819,34 @@ class MQTTError extends Error {
803
819
 
804
820
  class MQTTBase {
805
821
  constructor(opt={}) {
822
+ this.with(opt);
806
823
  this._conn_ = _mqtt_conn(this,
807
824
  this._init_dispatch(opt, this)); }
808
825
 
826
+ with(fns_ns) {
827
+ for (let [k,v] of Object.entries(fns_ns)) {
828
+ if ('function' === typeof v) {this[k] = v;} }
829
+ return this}
830
+
809
831
  async conn_emit(evt, arg, err_arg) {
810
832
  this.log_conn?.(evt, arg, err_arg);
811
833
  try {
812
- let fn_evt = this[await evt]; // microtask break
834
+ let fn_evt = this[await evt]; // microtask break using `await evt`
813
835
  if (fn_evt) {
814
836
  await fn_evt.call(this, this, arg, err_arg);}
815
- else if (err_arg) {
816
- await this.on_error(err_arg, evt);} }
837
+ else if (err_arg) {throw err_arg} }
817
838
  catch (err) {
818
839
  this.on_error(err, evt);} }
819
840
 
820
- on_error(err, err_path) {
821
- console.warn('[[u8-mqtt error: %s]]', err_path, err); }
841
+ on_error(err, evt) {
842
+ console.warn('[[u8-mqtt error: %s]]', evt, err); }
822
843
 
823
844
  // Handshaking Packets
824
845
 
825
846
  async connect(pkt={}) {
826
- let cid = pkt.client_id || ['u8-mqtt--', ''];
827
- if (Array.isArray(cid)) {
847
+ let cid = pkt.client_id || this.client_id;
848
+ if ('string' !== typeof cid) {
849
+ // see init_client_id implementation in core.jsy
828
850
  pkt.client_id = cid = this.init_client_id(cid);}
829
851
  this.client_id = cid;
830
852
 
@@ -850,12 +872,13 @@ class MQTTBase {
850
872
  return this._send('auth', pkt, 'auth')}
851
873
 
852
874
  ping() {return this._send('pingreq', null, 'pingresp')}
853
-
875
+ puback({pkt_id}) {return this._send('puback', {pkt_id})}
854
876
 
855
877
  // alias: sub
856
878
  subscribe(pkt, ex, topic_prefix) {
857
879
  pkt = _as_topics(pkt, ex, topic_prefix);
858
- return this._send('subscribe', pkt, pkt)}
880
+ let suback = this._send('subscribe', pkt, pkt);
881
+ return this.on_sub?.(suback, pkt) ?? suback}
859
882
 
860
883
  // alias: unsub
861
884
  unsubscribe(pkt, ex, topic_prefix) {
@@ -863,48 +886,52 @@ class MQTTBase {
863
886
  return this._send('unsubscribe', pkt, pkt)}
864
887
 
865
888
 
866
- // alias: pub
867
- publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
868
- post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
869
- send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
870
- store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
871
-
872
- json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
873
- json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
874
- json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
875
-
876
- obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
877
- obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
878
- obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
879
-
880
-
881
-
882
- // Utility Methods
883
-
884
- init_client_id(parts) {
885
- let cid = this.client_id;
886
-
887
- if (undefined === cid) {
888
- this.client_id = cid = (
889
-
890
-
891
-
892
- this.new_client_id(parts)
893
- );}
894
-
895
- return cid}
896
-
897
- new_client_id(parts) {
898
- return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
899
-
900
-
901
-
902
-
903
-
904
-
905
-
906
-
907
-
889
+ post(topic, payload, pub_opt) {// qos:0
890
+ return this.pub({topic, payload, qos:0}, pub_opt)}
891
+ send(topic, payload, pub_opt) {// qos:1
892
+ return this.pub({topic, payload, qos:1}, pub_opt)}
893
+ store(topic, payload, pub_opt) {// qos:1, retain: 1
894
+ return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
895
+
896
+ // alias: json_post
897
+ obj_post(topic, msg, pub_opt) {// qos:0
898
+ return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
899
+ // alias: json_send
900
+ obj_send(topic, msg, pub_opt) {// qos:1
901
+ return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
902
+ // alias: json_store
903
+ obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
904
+ return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
905
+
906
+ // alias: publish -- because 'pub' is shorter for semantic aliases above
907
+ async pub(pkt, pub_opt) {
908
+ if (undefined === pkt.payload) {
909
+ if ('function' === typeof pub_opt) {
910
+ pub_opt = {fn_encode: pub_opt};}
911
+
912
+ let {msg} = pkt;
913
+ switch (typeof msg) {
914
+ case 'function':
915
+ pub_opt = {...pub_opt, fn_encode: msg};
916
+ // flow into 'undefined' case
917
+ case 'undefined':
918
+ // return a single-value closure to publish packets
919
+ return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
920
+
921
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
922
+ let {fn_encode} = pub_opt || {};
923
+ pkt.payload = fn_encode
924
+ ? await fn_encode(msg)
925
+ : JSON.stringify(msg);}
926
+
927
+ if (pub_opt) {
928
+ if (pub_opt.props) {
929
+ pkt.props = pub_opt.props;}
930
+ if (pub_opt.xform) {
931
+ pkt = pub_opt.xform(pkt) || pkt;} }
932
+
933
+ return this._send('publish', pkt,
934
+ pkt.qos ? pkt : void 0 ) }// key
908
935
 
909
936
 
910
937
  // Internal API
@@ -913,52 +940,24 @@ class MQTTBase {
913
940
 
914
941
  _init_dispatch(opt) {
915
942
  this.constructor?._once_();
916
- let router = this.router =
917
- this._init_router?.(opt, this);
918
-
919
- let tgt ={
920
- __proto__: opt.on_mqtt_type || {}
921
- , router};
922
-
923
- tgt.mqtt_publish ||= router?.invoke;
924
- return _mqtt_dispatch(this, tgt)}
943
+ let target ={__proto__: opt.on_mqtt_type};
944
+ target.mqtt_publish ||=
945
+ this._init_router?.(opt, this, target);
946
+ return _mqtt_dispatch(this, target)}
925
947
 
926
948
  static _aliases() {
927
- return ' pub:publish sub:subscribe unsub:unsubscribe '}
949
+ return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
928
950
 
929
951
  static _once_(self=this) {
930
952
  self._once_ = _=>0;
931
- self.MQTTError = MQTTError;
932
953
  let p = self.prototype;
954
+ p.MQTTError = MQTTError;
933
955
  for (let alias of self._aliases().split(/\s+/)) {
934
956
  alias = alias.split(':');
935
957
  let fn = alias[1] && p[alias[1]];
936
958
  if (fn) {p[alias[0]] = fn;} } } }
937
959
 
938
960
 
939
- /*
940
- on_mqtt_type = {
941
- mqtt_auth(pkt, ctx) ::
942
- mqtt_connect(pkt, ctx) ::
943
- mqtt_connack(pkt, ctx) ::
944
- mqtt_disconnect(pkt, ctx) ::
945
-
946
- mqtt_publish(pkt, ctx)
947
- mqtt_subscribe(pkt, ctx) ::
948
- mqtt_unsubscribe(pkt, ctx) ::
949
-
950
- mqtt_pingreq(pkt, ctx) ::
951
- mqtt_pingresp(pkt, ctx) ::
952
- }
953
- */
954
-
955
-
956
- const _prefix_topics = (topic_prefix, iterable) =>
957
- Array.from(iterable, value =>(
958
- value.trim // string
959
- ? _prefix_topics(topic_prefix, value)
960
- : topic_prefix + value) );
961
-
962
961
  function _as_topics(pkt, ex, topic_prefix) {
963
962
  if (ex?.trim) {// string
964
963
  topic_prefix = ex;
@@ -975,55 +974,11 @@ function _as_topics(pkt, ex, topic_prefix) {
975
974
  if (topic_prefix) {
976
975
  // particularly useful with shared queues, e.g.
977
976
  // topic_prefix = '$share/some-queue-name/'
978
- pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
979
- return pkt}
980
-
977
+ let _prefix_topics = v =>
978
+ v.trim ? topic_prefix+v : v.map(_prefix_topics);
981
979
 
982
- async function _pub(self, pkt, pub_opt) {
983
- if (undefined === pkt.payload) {
984
- if ('function' === typeof pub_opt) {
985
- pub_opt = {fn_encode: pub_opt};}
986
-
987
- let {msg} = pkt;
988
- switch (typeof msg) {
989
- case 'function':
990
- pub_opt = {...pub_opt, fn_encode: msg};
991
- // flow into 'undefined' case
992
- case 'undefined':
993
- // return a single-value closure to publish packets
994
- return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
995
-
996
- default:
997
- // Encode payload from msg; fn_encode allows alternative to JSON.stringify
998
- let {fn_encode} = pub_opt || {};
999
- pkt.payload = fn_encode
1000
- ? await fn_encode(msg)
1001
- : JSON.stringify(msg);} }
1002
-
1003
- if (pub_opt) {
1004
- if (pub_opt.props) {
1005
- pkt.props = pub_opt.props;}
1006
- if (pub_opt.xform) {
1007
- pkt = pub_opt.xform(pkt) || pkt;} }
1008
-
1009
- return self._send('publish', pkt,
1010
- pkt.qos ? pkt : void 0 ) }// key
1011
-
1012
- {
1013
- Object.assign(_pub,{
1014
- m: (self, topic, payload, pub_opt) =>
1015
- _pub(self, {topic, payload, qos:0}, pub_opt)
1016
- , mq: (self, topic, payload, pub_opt) =>
1017
- _pub(self, {topic, payload, qos:1}, pub_opt)
1018
- , mqr: (self, topic, payload, pub_opt) =>
1019
- _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1020
-
1021
- , o: (self, topic, msg, pub_opt) =>
1022
- _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1023
- , oq: (self, topic, msg, pub_opt) =>
1024
- _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1025
- , oqr: (self, topic, msg, pub_opt) =>
1026
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
980
+ pkt.topics = pkt.topics.map(_prefix_topics);}
981
+ return pkt}
1027
982
 
1028
983
  const pkt_api = {
1029
984
  utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
@@ -1032,20 +987,27 @@ const pkt_api = {
1032
987
  };
1033
988
 
1034
989
  class MQTTCore extends MQTTBase {
1035
- constructor(opt={}) {
1036
- super(opt);
1037
- this.with(opt);}
1038
-
1039
990
  static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
1040
991
  let self = class extends this {};
1041
992
  self.prototype.mqtt_ctx =
1042
993
  mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
1043
994
  return self}
1044
995
 
1045
- with(fns_ns) {
1046
- for (let [k,v] of Object.entries(fns_ns)) {
1047
- if ('function' === typeof v) {this[k] = v;} }
1048
- return this}
996
+
997
+ // automatic Client Id for connect()
998
+ init_client_id(parts=['u8-mqtt--','']) {
999
+ let sess_stg=this.sess_stg;
1000
+ let key, cid = sess_stg?.getItem(key=parts.join(' '));
1001
+ if (! cid) {
1002
+ cid = parts.join(Math.random().toString(36).slice(2));
1003
+ sess_stg?.setItem(key, cid);}
1004
+ return cid}
1005
+
1006
+ get sess_stg() {return globalThis.sessionStorage}
1007
+
1008
+
1009
+ //on_error(err, evt) ::
1010
+ // console.warn @ '[[u8-mqtt error: %s]]', evt, err
1049
1011
 
1050
1012
  //log_conn(evt, arg, err_arg) ::
1051
1013
  // console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
@@ -1122,7 +1084,6 @@ class MQTTCore extends MQTTBase {
1122
1084
 
1123
1085
  with_tcp(...opt) {
1124
1086
  opt = this._conn_opt(opt);
1125
- console.log({opt});
1126
1087
  return this._use_conn (() =>
1127
1088
  this.with_stream(
1128
1089
  connect(opt)) ) }
@@ -1184,7 +1145,7 @@ class MQTTCore extends MQTTBase {
1184
1145
 
1185
1146
  return this} }
1186
1147
 
1187
- const version = '0.4.0';
1148
+ const version = '0.5.0';
1188
1149
 
1189
1150
  const MQTTClient_v4 = /* #__PURE__ */
1190
1151
  MQTTCore.mqtt_ctx(4, mqtt_opts_v4);