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.
- package/cjs/basic-v4.cjs +103 -137
- package/cjs/basic-v4.cjs.map +1 -1
- package/cjs/basic-v5.cjs +102 -136
- package/cjs/basic-v5.cjs.map +1 -1
- package/cjs/index.cjs +144 -189
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +145 -190
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +144 -189
- package/cjs/v5.cjs.map +1 -1
- package/code/_dispatch.jsy +16 -0
- package/code/base.jsy +68 -124
- package/code/core.jsy +15 -9
- package/code/router_path.jsy +24 -43
- package/code/with_topic_router.jsy +18 -10
- package/esm/basic-v4.js +1154 -0
- package/esm/basic-v4.js.map +1 -0
- package/esm/basic-v5.js +1416 -0
- package/esm/basic-v5.js.map +1 -0
- package/esm/deno/basic-v4.js +103 -137
- package/esm/deno/basic-v4.js.map +1 -1
- package/esm/deno/basic-v5.js +102 -136
- package/esm/deno/basic-v5.js.map +1 -1
- package/esm/deno/index.js +144 -189
- package/esm/deno/index.js.map +1 -1
- package/esm/deno/v4.js +145 -190
- package/esm/deno/v4.js.map +1 -1
- package/esm/deno/v5.js +144 -189
- package/esm/deno/v5.js.map +1 -1
- package/esm/index.js +1599 -0
- package/esm/index.js.map +1 -0
- package/esm/node/basic-v4.js +103 -137
- package/esm/node/basic-v4.js.map +1 -1
- package/esm/node/basic-v4.mjs +103 -137
- package/esm/node/basic-v4.mjs.map +1 -1
- package/esm/node/basic-v5.js +102 -136
- package/esm/node/basic-v5.js.map +1 -1
- package/esm/node/basic-v5.mjs +102 -136
- package/esm/node/basic-v5.mjs.map +1 -1
- package/esm/node/index.js +144 -189
- package/esm/node/index.js.map +1 -1
- package/esm/node/index.mjs +144 -189
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.js +145 -190
- package/esm/node/v4.js.map +1 -1
- package/esm/node/v4.mjs +145 -190
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.js +144 -189
- package/esm/node/v5.js.map +1 -1
- package/esm/node/v5.mjs +144 -189
- package/esm/node/v5.mjs.map +1 -1
- package/esm/v4.js +1336 -0
- package/esm/v4.js.map +1 -0
- package/esm/v5.js +1599 -0
- package/esm/v5.js.map +1 -0
- package/esm/web/basic-v4.js +103 -137
- package/esm/web/basic-v4.js.map +1 -1
- package/esm/web/basic-v4.min.js +1 -1
- package/esm/web/basic-v4.min.js.br +0 -0
- package/esm/web/basic-v4.min.js.gz +0 -0
- package/esm/web/basic-v5.js +102 -136
- package/esm/web/basic-v5.js.map +1 -1
- package/esm/web/basic-v5.min.js +1 -1
- package/esm/web/basic-v5.min.js.br +0 -0
- package/esm/web/basic-v5.min.js.gz +0 -0
- package/esm/web/index.js +144 -189
- package/esm/web/index.js.map +1 -1
- package/esm/web/index.min.js +1 -1
- package/esm/web/index.min.js.br +0 -0
- package/esm/web/index.min.js.gz +0 -0
- package/esm/web/v4.js +145 -190
- package/esm/web/v4.js.map +1 -1
- package/esm/web/v4.min.js +1 -1
- package/esm/web/v4.min.js.br +0 -0
- package/esm/web/v4.min.js.gz +0 -0
- package/esm/web/v5.js +144 -189
- package/esm/web/v5.js.map +1 -1
- package/esm/web/v5.min.js +1 -1
- package/esm/web/v5.min.js.br +0 -0
- package/esm/web/v5.min.js.gz +0 -0
- package/package.json +14 -9
package/cjs/basic-v4.cjs
CHANGED
|
@@ -51,7 +51,7 @@ class U8_Reason extends Number {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
|
|
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.
|
|
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,
|
|
825
|
-
console.warn('[[u8-mqtt error: %s]]',
|
|
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 ||
|
|
831
|
-
if (
|
|
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
|
-
|
|
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
|
-
//
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
store(topic, payload, pub_opt) {
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
obj_send(topic, msg, pub_opt) {
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
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
|
|
@@ -923,41 +950,18 @@ class MQTTBase {
|
|
|
923
950
|
return _mqtt_dispatch(this, target)}
|
|
924
951
|
|
|
925
952
|
static _aliases() {
|
|
926
|
-
return ' pub
|
|
953
|
+
return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
|
|
927
954
|
|
|
928
955
|
static _once_(self=this) {
|
|
929
956
|
self._once_ = _=>0;
|
|
930
|
-
self.MQTTError = MQTTError;
|
|
931
957
|
let p = self.prototype;
|
|
958
|
+
p.MQTTError = MQTTError;
|
|
932
959
|
for (let alias of self._aliases().split(/\s+/)) {
|
|
933
960
|
alias = alias.split(':');
|
|
934
961
|
let fn = alias[1] && p[alias[1]];
|
|
935
962
|
if (fn) {p[alias[0]] = fn;} } } }
|
|
936
963
|
|
|
937
964
|
|
|
938
|
-
/*
|
|
939
|
-
on_mqtt_type = {
|
|
940
|
-
mqtt_auth(pkt, ctx) ::
|
|
941
|
-
mqtt_connect(pkt, ctx) ::
|
|
942
|
-
mqtt_connack(pkt, ctx) ::
|
|
943
|
-
mqtt_disconnect(pkt, ctx) ::
|
|
944
|
-
|
|
945
|
-
mqtt_publish(pkt, ctx)
|
|
946
|
-
mqtt_subscribe(pkt, ctx) ::
|
|
947
|
-
mqtt_unsubscribe(pkt, ctx) ::
|
|
948
|
-
|
|
949
|
-
mqtt_pingreq(pkt, ctx) ::
|
|
950
|
-
mqtt_pingresp(pkt, ctx) ::
|
|
951
|
-
}
|
|
952
|
-
*/
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
const _prefix_topics = (topic_prefix, iterable) =>
|
|
956
|
-
Array.from(iterable, value =>(
|
|
957
|
-
value.trim // string
|
|
958
|
-
? _prefix_topics(topic_prefix, value)
|
|
959
|
-
: topic_prefix + value) );
|
|
960
|
-
|
|
961
965
|
function _as_topics(pkt, ex, topic_prefix) {
|
|
962
966
|
if (ex?.trim) {// string
|
|
963
967
|
topic_prefix = ex;
|
|
@@ -974,55 +978,11 @@ function _as_topics(pkt, ex, topic_prefix) {
|
|
|
974
978
|
if (topic_prefix) {
|
|
975
979
|
// particularly useful with shared queues, e.g.
|
|
976
980
|
// topic_prefix = '$share/some-queue-name/'
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
981
|
+
let _prefix_topics = v =>
|
|
982
|
+
v.trim ? topic_prefix+v : v.map(_prefix_topics);
|
|
980
983
|
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
if ('function' === typeof pub_opt) {
|
|
984
|
-
pub_opt = {fn_encode: pub_opt};}
|
|
985
|
-
|
|
986
|
-
let {msg} = pkt;
|
|
987
|
-
switch (typeof msg) {
|
|
988
|
-
case 'function':
|
|
989
|
-
pub_opt = {...pub_opt, fn_encode: msg};
|
|
990
|
-
// flow into 'undefined' case
|
|
991
|
-
case 'undefined':
|
|
992
|
-
// return a single-value closure to publish packets
|
|
993
|
-
return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
|
|
994
|
-
|
|
995
|
-
default:
|
|
996
|
-
// Encode payload from msg; fn_encode allows alternative to JSON.stringify
|
|
997
|
-
let {fn_encode} = pub_opt || {};
|
|
998
|
-
pkt.payload = fn_encode
|
|
999
|
-
? await fn_encode(msg)
|
|
1000
|
-
: JSON.stringify(msg);} }
|
|
1001
|
-
|
|
1002
|
-
if (pub_opt) {
|
|
1003
|
-
if (pub_opt.props) {
|
|
1004
|
-
pkt.props = pub_opt.props;}
|
|
1005
|
-
if (pub_opt.xform) {
|
|
1006
|
-
pkt = pub_opt.xform(pkt) || pkt;} }
|
|
1007
|
-
|
|
1008
|
-
return self._send('publish', pkt,
|
|
1009
|
-
pkt.qos ? pkt : void 0 ) }// key
|
|
1010
|
-
|
|
1011
|
-
{
|
|
1012
|
-
Object.assign(_pub,{
|
|
1013
|
-
m: (self, topic, payload, pub_opt) =>
|
|
1014
|
-
_pub(self, {topic, payload, qos:0}, pub_opt)
|
|
1015
|
-
, mq: (self, topic, payload, pub_opt) =>
|
|
1016
|
-
_pub(self, {topic, payload, qos:1}, pub_opt)
|
|
1017
|
-
, mqr: (self, topic, payload, pub_opt) =>
|
|
1018
|
-
_pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
|
|
1019
|
-
|
|
1020
|
-
, o: (self, topic, msg, pub_opt) =>
|
|
1021
|
-
_pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
|
|
1022
|
-
, oq: (self, topic, msg, pub_opt) =>
|
|
1023
|
-
_pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
|
|
1024
|
-
, oqr: (self, topic, msg, pub_opt) =>
|
|
1025
|
-
_pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
|
|
984
|
+
pkt.topics = pkt.topics.map(_prefix_topics);}
|
|
985
|
+
return pkt}
|
|
1026
986
|
|
|
1027
987
|
const pkt_api = {
|
|
1028
988
|
utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
|
|
@@ -1031,20 +991,27 @@ const pkt_api = {
|
|
|
1031
991
|
};
|
|
1032
992
|
|
|
1033
993
|
class MQTTCore extends MQTTBase {
|
|
1034
|
-
constructor(opt={}) {
|
|
1035
|
-
super(opt);
|
|
1036
|
-
this.with(opt);}
|
|
1037
|
-
|
|
1038
994
|
static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
|
|
1039
995
|
let self = class extends this {};
|
|
1040
996
|
self.prototype.mqtt_ctx =
|
|
1041
997
|
mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
|
|
1042
998
|
return self}
|
|
1043
999
|
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
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
|
|
1048
1015
|
|
|
1049
1016
|
//log_conn(evt, arg, err_arg) ::
|
|
1050
1017
|
// console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
|
|
@@ -1121,7 +1088,6 @@ class MQTTCore extends MQTTBase {
|
|
|
1121
1088
|
|
|
1122
1089
|
with_tcp(...opt) {
|
|
1123
1090
|
opt = this._conn_opt(opt);
|
|
1124
|
-
console.log({opt});
|
|
1125
1091
|
return this._use_conn (() =>
|
|
1126
1092
|
this.with_stream(
|
|
1127
1093
|
node_net.connect(opt)) ) }
|
|
@@ -1183,7 +1149,7 @@ class MQTTCore extends MQTTBase {
|
|
|
1183
1149
|
|
|
1184
1150
|
return this} }
|
|
1185
1151
|
|
|
1186
|
-
const version = '0.
|
|
1152
|
+
const version = '0.5.1-node';
|
|
1187
1153
|
|
|
1188
1154
|
const MQTTClient_v4 = /* #__PURE__ */
|
|
1189
1155
|
MQTTCore.mqtt_ctx(4, mqtt_opts_v4);
|