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/esm/node/basic-v4.mjs
CHANGED
|
@@ -47,7 +47,7 @@ class U8_Reason extends Number {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
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.
|
|
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,
|
|
821
|
-
console.warn('[[u8-mqtt error: %s]]',
|
|
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 ||
|
|
827
|
-
if (
|
|
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
|
-
|
|
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
|
-
//
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
store(topic, payload, pub_opt) {
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
obj_send(topic, msg, pub_opt) {
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
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
|
-
|
|
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
|
|
@@ -919,41 +946,18 @@ class MQTTBase {
|
|
|
919
946
|
return _mqtt_dispatch(this, target)}
|
|
920
947
|
|
|
921
948
|
static _aliases() {
|
|
922
|
-
return ' pub
|
|
949
|
+
return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
|
|
923
950
|
|
|
924
951
|
static _once_(self=this) {
|
|
925
952
|
self._once_ = _=>0;
|
|
926
|
-
self.MQTTError = MQTTError;
|
|
927
953
|
let p = self.prototype;
|
|
954
|
+
p.MQTTError = MQTTError;
|
|
928
955
|
for (let alias of self._aliases().split(/\s+/)) {
|
|
929
956
|
alias = alias.split(':');
|
|
930
957
|
let fn = alias[1] && p[alias[1]];
|
|
931
958
|
if (fn) {p[alias[0]] = fn;} } } }
|
|
932
959
|
|
|
933
960
|
|
|
934
|
-
/*
|
|
935
|
-
on_mqtt_type = {
|
|
936
|
-
mqtt_auth(pkt, ctx) ::
|
|
937
|
-
mqtt_connect(pkt, ctx) ::
|
|
938
|
-
mqtt_connack(pkt, ctx) ::
|
|
939
|
-
mqtt_disconnect(pkt, ctx) ::
|
|
940
|
-
|
|
941
|
-
mqtt_publish(pkt, ctx)
|
|
942
|
-
mqtt_subscribe(pkt, ctx) ::
|
|
943
|
-
mqtt_unsubscribe(pkt, ctx) ::
|
|
944
|
-
|
|
945
|
-
mqtt_pingreq(pkt, ctx) ::
|
|
946
|
-
mqtt_pingresp(pkt, ctx) ::
|
|
947
|
-
}
|
|
948
|
-
*/
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
const _prefix_topics = (topic_prefix, iterable) =>
|
|
952
|
-
Array.from(iterable, value =>(
|
|
953
|
-
value.trim // string
|
|
954
|
-
? _prefix_topics(topic_prefix, value)
|
|
955
|
-
: topic_prefix + value) );
|
|
956
|
-
|
|
957
961
|
function _as_topics(pkt, ex, topic_prefix) {
|
|
958
962
|
if (ex?.trim) {// string
|
|
959
963
|
topic_prefix = ex;
|
|
@@ -970,55 +974,11 @@ function _as_topics(pkt, ex, topic_prefix) {
|
|
|
970
974
|
if (topic_prefix) {
|
|
971
975
|
// particularly useful with shared queues, e.g.
|
|
972
976
|
// topic_prefix = '$share/some-queue-name/'
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
977
|
+
let _prefix_topics = v =>
|
|
978
|
+
v.trim ? topic_prefix+v : v.map(_prefix_topics);
|
|
976
979
|
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
if ('function' === typeof pub_opt) {
|
|
980
|
-
pub_opt = {fn_encode: pub_opt};}
|
|
981
|
-
|
|
982
|
-
let {msg} = pkt;
|
|
983
|
-
switch (typeof msg) {
|
|
984
|
-
case 'function':
|
|
985
|
-
pub_opt = {...pub_opt, fn_encode: msg};
|
|
986
|
-
// flow into 'undefined' case
|
|
987
|
-
case 'undefined':
|
|
988
|
-
// return a single-value closure to publish packets
|
|
989
|
-
return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
|
|
990
|
-
|
|
991
|
-
default:
|
|
992
|
-
// Encode payload from msg; fn_encode allows alternative to JSON.stringify
|
|
993
|
-
let {fn_encode} = pub_opt || {};
|
|
994
|
-
pkt.payload = fn_encode
|
|
995
|
-
? await fn_encode(msg)
|
|
996
|
-
: JSON.stringify(msg);} }
|
|
997
|
-
|
|
998
|
-
if (pub_opt) {
|
|
999
|
-
if (pub_opt.props) {
|
|
1000
|
-
pkt.props = pub_opt.props;}
|
|
1001
|
-
if (pub_opt.xform) {
|
|
1002
|
-
pkt = pub_opt.xform(pkt) || pkt;} }
|
|
1003
|
-
|
|
1004
|
-
return self._send('publish', pkt,
|
|
1005
|
-
pkt.qos ? pkt : void 0 ) }// key
|
|
1006
|
-
|
|
1007
|
-
{
|
|
1008
|
-
Object.assign(_pub,{
|
|
1009
|
-
m: (self, topic, payload, pub_opt) =>
|
|
1010
|
-
_pub(self, {topic, payload, qos:0}, pub_opt)
|
|
1011
|
-
, mq: (self, topic, payload, pub_opt) =>
|
|
1012
|
-
_pub(self, {topic, payload, qos:1}, pub_opt)
|
|
1013
|
-
, mqr: (self, topic, payload, pub_opt) =>
|
|
1014
|
-
_pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
|
|
1015
|
-
|
|
1016
|
-
, o: (self, topic, msg, pub_opt) =>
|
|
1017
|
-
_pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
|
|
1018
|
-
, oq: (self, topic, msg, pub_opt) =>
|
|
1019
|
-
_pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
|
|
1020
|
-
, oqr: (self, topic, msg, pub_opt) =>
|
|
1021
|
-
_pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
|
|
980
|
+
pkt.topics = pkt.topics.map(_prefix_topics);}
|
|
981
|
+
return pkt}
|
|
1022
982
|
|
|
1023
983
|
const pkt_api = {
|
|
1024
984
|
utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
|
|
@@ -1027,20 +987,27 @@ const pkt_api = {
|
|
|
1027
987
|
};
|
|
1028
988
|
|
|
1029
989
|
class MQTTCore extends MQTTBase {
|
|
1030
|
-
constructor(opt={}) {
|
|
1031
|
-
super(opt);
|
|
1032
|
-
this.with(opt);}
|
|
1033
|
-
|
|
1034
990
|
static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
|
|
1035
991
|
let self = class extends this {};
|
|
1036
992
|
self.prototype.mqtt_ctx =
|
|
1037
993
|
mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
|
|
1038
994
|
return self}
|
|
1039
995
|
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
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
|
|
1044
1011
|
|
|
1045
1012
|
//log_conn(evt, arg, err_arg) ::
|
|
1046
1013
|
// console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
|
|
@@ -1117,7 +1084,6 @@ class MQTTCore extends MQTTBase {
|
|
|
1117
1084
|
|
|
1118
1085
|
with_tcp(...opt) {
|
|
1119
1086
|
opt = this._conn_opt(opt);
|
|
1120
|
-
console.log({opt});
|
|
1121
1087
|
return this._use_conn (() =>
|
|
1122
1088
|
this.with_stream(
|
|
1123
1089
|
connect(opt)) ) }
|
|
@@ -1179,7 +1145,7 @@ class MQTTCore extends MQTTBase {
|
|
|
1179
1145
|
|
|
1180
1146
|
return this} }
|
|
1181
1147
|
|
|
1182
|
-
const version = '0.
|
|
1148
|
+
const version = '0.5.1-node';
|
|
1183
1149
|
|
|
1184
1150
|
const MQTTClient_v4 = /* #__PURE__ */
|
|
1185
1151
|
MQTTCore.mqtt_ctx(4, mqtt_opts_v4);
|