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
package/README.md CHANGED
@@ -71,9 +71,9 @@ Built for small footprint with ES Modules (ESM) using embedded [u8-mqtt-packet][
71
71
 
72
72
  | module | brotli | minified |
73
73
  |:------------------------|---------:|---------:|
74
- | `u8-mqtt` | 6608 B | 19988 B |
75
- | `u8-mqtt/esm/v5.min.js` | 6518 B | 19815 B |
76
- | `u8-mqtt/esm/v4.min.js` | 5434 B | 15405 B |
74
+ | `u8-mqtt` | 6587 B | 19962 B |
75
+ | `u8-mqtt/esm/v5.min.js` | 6504 B | 19789 B |
76
+ | `u8-mqtt/esm/v4.min.js` | 5420 B | 15379 B |
77
77
 
78
78
  [automated sizing report](./docs/compressed.md)
79
79
 
package/cjs/basic-v4.cjs CHANGED
@@ -51,7 +51,7 @@ class U8_Reason extends Number {
51
51
  }
52
52
  }
53
53
 
54
- class mqtt_reader_v4$1 {
54
+ let mqtt_reader_v4$1 = class mqtt_reader_v4 {
55
55
  static of(buf) { return this.prototype.of(buf) }
56
56
  of(buf) {
57
57
  let step = (width, k) => (k=0|step.k, step.k=k+width, k);
@@ -107,7 +107,7 @@ class mqtt_reader_v4$1 {
107
107
  return buf.subarray(step.k|0)
108
108
  }
109
109
 
110
- }
110
+ };
111
111
 
112
112
  function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
113
113
  mqtt_reader = class extends mqtt_reader {
@@ -482,7 +482,7 @@ function mqtt_encode_disconnect(ns, mqtt_writer) {
482
482
  }
483
483
  }
484
484
 
485
- // not a v4 packet: import { mqtt_encode_auth } from './encode/auth.mjs'
485
+ // not a v4 packet: import { mqtt_encode_auth } from './encode/auth.js'
486
486
 
487
487
 
488
488
  const mqtt_decode_v4 = [
@@ -778,6 +778,22 @@ const _mqtt_cmdid_dispatch ={
778
778
 
779
779
  await fn?.call(target, pkt, ctx);} })()) };
780
780
 
781
+ /*
782
+ on_mqtt_type = {
783
+ mqtt_auth(pkt, ctx) ::
784
+ mqtt_connect(pkt, ctx) ::
785
+ mqtt_connack(pkt, ctx) ::
786
+ mqtt_disconnect(pkt, ctx) ::
787
+
788
+ mqtt_publish(pkt, ctx)
789
+ mqtt_subscribe(pkt, ctx) ::
790
+ mqtt_unsubscribe(pkt, ctx) ::
791
+
792
+ mqtt_pingreq(pkt, ctx) ::
793
+ mqtt_pingresp(pkt, ctx) ::
794
+ }
795
+ */
796
+
781
797
  function _mqtt_dispatch(opt, target) {
782
798
  let _disp_ = _mqtt_cmdid_dispatch.create(target);
783
799
  let { cmdids } = _disp_;
@@ -807,28 +823,34 @@ class MQTTError extends Error {
807
823
 
808
824
  class MQTTBase {
809
825
  constructor(opt={}) {
826
+ this.with(opt);
810
827
  this._conn_ = _mqtt_conn(this,
811
828
  this._init_dispatch(opt, this)); }
812
829
 
830
+ with(fns_ns) {
831
+ for (let [k,v] of Object.entries(fns_ns)) {
832
+ if ('function' === typeof v) {this[k] = v;} }
833
+ return this}
834
+
813
835
  async conn_emit(evt, arg, err_arg) {
814
836
  this.log_conn?.(evt, arg, err_arg);
815
837
  try {
816
- let fn_evt = this[await evt]; // microtask break
838
+ let fn_evt = this[await evt]; // microtask break using `await evt`
817
839
  if (fn_evt) {
818
840
  await fn_evt.call(this, this, arg, err_arg);}
819
- else if (err_arg) {
820
- await this.on_error(err_arg, evt);} }
841
+ else if (err_arg) {throw err_arg} }
821
842
  catch (err) {
822
843
  this.on_error(err, evt);} }
823
844
 
824
- on_error(err, err_path) {
825
- console.warn('[[u8-mqtt error: %s]]', err_path, err); }
845
+ on_error(err, evt) {
846
+ console.warn('[[u8-mqtt error: %s]]', evt, err); }
826
847
 
827
848
  // Handshaking Packets
828
849
 
829
850
  async connect(pkt={}) {
830
- let cid = pkt.client_id || ['u8-mqtt--', ''];
831
- if (Array.isArray(cid)) {
851
+ let cid = pkt.client_id || this.client_id;
852
+ if ('string' !== typeof cid) {
853
+ // see init_client_id implementation in core.jsy
832
854
  pkt.client_id = cid = this.init_client_id(cid);}
833
855
  this.client_id = cid;
834
856
 
@@ -854,12 +876,13 @@ class MQTTBase {
854
876
  return this._send('auth', pkt, 'auth')}
855
877
 
856
878
  ping() {return this._send('pingreq', null, 'pingresp')}
857
-
879
+ puback({pkt_id}) {return this._send('puback', {pkt_id})}
858
880
 
859
881
  // alias: sub
860
882
  subscribe(pkt, ex, topic_prefix) {
861
883
  pkt = _as_topics(pkt, ex, topic_prefix);
862
- return this._send('subscribe', pkt, pkt)}
884
+ let suback = this._send('subscribe', pkt, pkt);
885
+ return this.on_sub?.(suback, pkt) ?? suback}
863
886
 
864
887
  // alias: unsub
865
888
  unsubscribe(pkt, ex, topic_prefix) {
@@ -867,48 +890,52 @@ class MQTTBase {
867
890
  return this._send('unsubscribe', pkt, pkt)}
868
891
 
869
892
 
870
- // alias: pub
871
- publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
872
- post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
873
- send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
874
- store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
875
-
876
- json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
877
- json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
878
- json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
879
-
880
- obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
881
- obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
882
- obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
883
-
884
-
885
-
886
- // Utility Methods
887
-
888
- init_client_id(parts) {
889
- let cid = this.client_id;
890
-
891
- if (undefined === cid) {
892
- this.client_id = cid = (
893
-
894
-
895
-
896
- this.new_client_id(parts)
897
- );}
898
-
899
- return cid}
900
-
901
- new_client_id(parts) {
902
- return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
903
-
904
-
905
-
906
-
907
-
908
-
909
-
910
-
911
-
893
+ post(topic, payload, pub_opt) {// qos:0
894
+ return this.pub({topic, payload, qos:0}, pub_opt)}
895
+ send(topic, payload, pub_opt) {// qos:1
896
+ return this.pub({topic, payload, qos:1}, pub_opt)}
897
+ store(topic, payload, pub_opt) {// qos:1, retain: 1
898
+ return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
899
+
900
+ // alias: json_post
901
+ obj_post(topic, msg, pub_opt) {// qos:0
902
+ return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
903
+ // alias: json_send
904
+ obj_send(topic, msg, pub_opt) {// qos:1
905
+ return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
906
+ // alias: json_store
907
+ obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
908
+ return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
909
+
910
+ // alias: publish -- because 'pub' is shorter for semantic aliases above
911
+ async pub(pkt, pub_opt) {
912
+ if (undefined === pkt.payload) {
913
+ if ('function' === typeof pub_opt) {
914
+ pub_opt = {fn_encode: pub_opt};}
915
+
916
+ let {msg} = pkt;
917
+ switch (typeof msg) {
918
+ case 'function':
919
+ pub_opt = {...pub_opt, fn_encode: msg};
920
+ // flow into 'undefined' case
921
+ case 'undefined':
922
+ // return a single-value closure to publish packets
923
+ return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
924
+
925
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
926
+ let {fn_encode} = pub_opt || {};
927
+ pkt.payload = fn_encode
928
+ ? await fn_encode(msg)
929
+ : JSON.stringify(msg);}
930
+
931
+ if (pub_opt) {
932
+ if (pub_opt.props) {
933
+ pkt.props = pub_opt.props;}
934
+ if (pub_opt.xform) {
935
+ pkt = pub_opt.xform(pkt) || pkt;} }
936
+
937
+ return this._send('publish', pkt,
938
+ pkt.qos ? pkt : void 0 ) }// key
912
939
 
913
940
 
914
941
  // Internal API
@@ -917,52 +944,24 @@ class MQTTBase {
917
944
 
918
945
  _init_dispatch(opt) {
919
946
  this.constructor?._once_();
920
- let router = this.router =
921
- this._init_router?.(opt, this);
922
-
923
- let tgt ={
924
- __proto__: opt.on_mqtt_type || {}
925
- , router};
926
-
927
- tgt.mqtt_publish ||= router?.invoke;
928
- return _mqtt_dispatch(this, tgt)}
947
+ let target ={__proto__: opt.on_mqtt_type};
948
+ target.mqtt_publish ||=
949
+ this._init_router?.(opt, this, target);
950
+ return _mqtt_dispatch(this, target)}
929
951
 
930
952
  static _aliases() {
931
- return ' pub:publish sub:subscribe unsub:unsubscribe '}
953
+ return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
932
954
 
933
955
  static _once_(self=this) {
934
956
  self._once_ = _=>0;
935
- self.MQTTError = MQTTError;
936
957
  let p = self.prototype;
958
+ p.MQTTError = MQTTError;
937
959
  for (let alias of self._aliases().split(/\s+/)) {
938
960
  alias = alias.split(':');
939
961
  let fn = alias[1] && p[alias[1]];
940
962
  if (fn) {p[alias[0]] = fn;} } } }
941
963
 
942
964
 
943
- /*
944
- on_mqtt_type = {
945
- mqtt_auth(pkt, ctx) ::
946
- mqtt_connect(pkt, ctx) ::
947
- mqtt_connack(pkt, ctx) ::
948
- mqtt_disconnect(pkt, ctx) ::
949
-
950
- mqtt_publish(pkt, ctx)
951
- mqtt_subscribe(pkt, ctx) ::
952
- mqtt_unsubscribe(pkt, ctx) ::
953
-
954
- mqtt_pingreq(pkt, ctx) ::
955
- mqtt_pingresp(pkt, ctx) ::
956
- }
957
- */
958
-
959
-
960
- const _prefix_topics = (topic_prefix, iterable) =>
961
- Array.from(iterable, value =>(
962
- value.trim // string
963
- ? _prefix_topics(topic_prefix, value)
964
- : topic_prefix + value) );
965
-
966
965
  function _as_topics(pkt, ex, topic_prefix) {
967
966
  if (ex?.trim) {// string
968
967
  topic_prefix = ex;
@@ -979,55 +978,11 @@ function _as_topics(pkt, ex, topic_prefix) {
979
978
  if (topic_prefix) {
980
979
  // particularly useful with shared queues, e.g.
981
980
  // topic_prefix = '$share/some-queue-name/'
982
- pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
983
- return pkt}
984
-
981
+ let _prefix_topics = v =>
982
+ v.trim ? topic_prefix+v : v.map(_prefix_topics);
985
983
 
986
- async function _pub(self, pkt, pub_opt) {
987
- if (undefined === pkt.payload) {
988
- if ('function' === typeof pub_opt) {
989
- pub_opt = {fn_encode: pub_opt};}
990
-
991
- let {msg} = pkt;
992
- switch (typeof msg) {
993
- case 'function':
994
- pub_opt = {...pub_opt, fn_encode: msg};
995
- // flow into 'undefined' case
996
- case 'undefined':
997
- // return a single-value closure to publish packets
998
- return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
999
-
1000
- default:
1001
- // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1002
- let {fn_encode} = pub_opt || {};
1003
- pkt.payload = fn_encode
1004
- ? await fn_encode(msg)
1005
- : JSON.stringify(msg);} }
1006
-
1007
- if (pub_opt) {
1008
- if (pub_opt.props) {
1009
- pkt.props = pub_opt.props;}
1010
- if (pub_opt.xform) {
1011
- pkt = pub_opt.xform(pkt) || pkt;} }
1012
-
1013
- return self._send('publish', pkt,
1014
- pkt.qos ? pkt : void 0 ) }// key
1015
-
1016
- {
1017
- Object.assign(_pub,{
1018
- m: (self, topic, payload, pub_opt) =>
1019
- _pub(self, {topic, payload, qos:0}, pub_opt)
1020
- , mq: (self, topic, payload, pub_opt) =>
1021
- _pub(self, {topic, payload, qos:1}, pub_opt)
1022
- , mqr: (self, topic, payload, pub_opt) =>
1023
- _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1024
-
1025
- , o: (self, topic, msg, pub_opt) =>
1026
- _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1027
- , oq: (self, topic, msg, pub_opt) =>
1028
- _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1029
- , oqr: (self, topic, msg, pub_opt) =>
1030
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
984
+ pkt.topics = pkt.topics.map(_prefix_topics);}
985
+ return pkt}
1031
986
 
1032
987
  const pkt_api = {
1033
988
  utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
@@ -1036,20 +991,27 @@ const pkt_api = {
1036
991
  };
1037
992
 
1038
993
  class MQTTCore extends MQTTBase {
1039
- constructor(opt={}) {
1040
- super(opt);
1041
- this.with(opt);}
1042
-
1043
994
  static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
1044
995
  let self = class extends this {};
1045
996
  self.prototype.mqtt_ctx =
1046
997
  mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
1047
998
  return self}
1048
999
 
1049
- with(fns_ns) {
1050
- for (let [k,v] of Object.entries(fns_ns)) {
1051
- if ('function' === typeof v) {this[k] = v;} }
1052
- return this}
1000
+
1001
+ // automatic Client Id for connect()
1002
+ init_client_id(parts=['u8-mqtt--','']) {
1003
+ let sess_stg=this.sess_stg;
1004
+ let key, cid = sess_stg?.getItem(key=parts.join(' '));
1005
+ if (! cid) {
1006
+ cid = parts.join(Math.random().toString(36).slice(2));
1007
+ sess_stg?.setItem(key, cid);}
1008
+ return cid}
1009
+
1010
+ get sess_stg() {return globalThis.sessionStorage}
1011
+
1012
+
1013
+ //on_error(err, evt) ::
1014
+ // console.warn @ '[[u8-mqtt error: %s]]', evt, err
1053
1015
 
1054
1016
  //log_conn(evt, arg, err_arg) ::
1055
1017
  // console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
@@ -1126,7 +1088,6 @@ class MQTTCore extends MQTTBase {
1126
1088
 
1127
1089
  with_tcp(...opt) {
1128
1090
  opt = this._conn_opt(opt);
1129
- console.log({opt});
1130
1091
  return this._use_conn (() =>
1131
1092
  this.with_stream(
1132
1093
  node_net.connect(opt)) ) }
@@ -1188,7 +1149,7 @@ class MQTTCore extends MQTTBase {
1188
1149
 
1189
1150
  return this} }
1190
1151
 
1191
- const version = '0.4.0';
1152
+ const version = '0.5.0';
1192
1153
 
1193
1154
  const MQTTClient_v4 = /* #__PURE__ */
1194
1155
  MQTTCore.mqtt_ctx(4, mqtt_opts_v4);