u8-mqtt 0.0.22 → 0.0.27
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/index.cjs +185 -81
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +185 -81
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +185 -81
- package/cjs/v5.cjs.map +1 -1
- package/code/_cmdid_dispatch.jsy +8 -8
- package/code/_conn.jsy +14 -11
- package/code/_dispatch.jsy +4 -4
- package/code/_router.jsy +23 -12
- package/code/base.jsy +77 -32
- package/code/core.jsy +11 -13
- package/code/session.mjs +5 -4
- package/code/v4.mjs +3 -3
- package/code/v5.mjs +3 -3
- package/esm/deno/index.mjs +184 -81
- package/esm/deno/index.mjs.map +1 -1
- package/esm/deno/v4.mjs +184 -81
- package/esm/deno/v4.mjs.map +1 -1
- package/esm/deno/v5.mjs +184 -81
- package/esm/deno/v5.mjs.map +1 -1
- package/esm/node/index.mjs +185 -82
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.mjs +185 -82
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.mjs +185 -82
- package/esm/node/v5.mjs.map +1 -1
- package/esm/web/index.min.mjs +1 -1
- package/esm/web/index.mjs +184 -81
- package/esm/web/index.mjs.map +1 -1
- package/esm/web/v4.min.mjs +1 -1
- package/esm/web/v4.mjs +184 -81
- package/esm/web/v4.mjs.map +1 -1
- package/esm/web/v5.min.mjs +1 -1
- package/esm/web/v5.mjs +184 -81
- package/esm/web/v5.mjs.map +1 -1
- package/package.json +17 -12
package/esm/web/index.mjs
CHANGED
|
@@ -392,6 +392,50 @@ function mqtt_decode_pingxxx(ns) {
|
|
|
392
392
|
return ns[0xc] = ns[0xd] = pkt => pkt
|
|
393
393
|
}
|
|
394
394
|
|
|
395
|
+
function mqtt_decode_disconnect(ns) {
|
|
396
|
+
const _disconnect_reason_ = bind_reason_lookup([
|
|
397
|
+
// MQTT 5.0
|
|
398
|
+
[ 0x00, 'Normal disconnection'],
|
|
399
|
+
[ 0x04, 'Disconnect with Will Message'],
|
|
400
|
+
[ 0x80, 'Unspecified error'],
|
|
401
|
+
[ 0x81, 'Malformed Packet'],
|
|
402
|
+
[ 0x82, 'Protocol Error'],
|
|
403
|
+
[ 0x83, 'Implementation specific error'],
|
|
404
|
+
[ 0x87, 'Not authorized'],
|
|
405
|
+
[ 0x89, 'Server busy'],
|
|
406
|
+
[ 0x8B, 'Server shutting down'],
|
|
407
|
+
[ 0x8D, 'Keep Alive timeout'],
|
|
408
|
+
[ 0x8E, 'Session taken over'],
|
|
409
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
410
|
+
[ 0x90, 'Topic Name invalid'],
|
|
411
|
+
[ 0x93, 'Receive Maximum exceeded'],
|
|
412
|
+
[ 0x94, 'Topic Alias invalid'],
|
|
413
|
+
[ 0x95, 'Packet too large'],
|
|
414
|
+
[ 0x96, 'Message rate too high'],
|
|
415
|
+
[ 0x97, 'Quota exceeded'],
|
|
416
|
+
[ 0x98, 'Administrative action'],
|
|
417
|
+
[ 0x99, 'Payload format invalid'],
|
|
418
|
+
[ 0x9A, 'Retain not supported'],
|
|
419
|
+
[ 0x9B, 'QoS not supported'],
|
|
420
|
+
[ 0x9C, 'Use another server'],
|
|
421
|
+
[ 0x9D, 'Server moved'],
|
|
422
|
+
[ 0x9E, 'Shared Subscriptions not supported'],
|
|
423
|
+
[ 0x9F, 'Connection rate exceeded'],
|
|
424
|
+
[ 0xA0, 'Maximum connect time'],
|
|
425
|
+
[ 0xA1, 'Subscription Identifiers not supported'],
|
|
426
|
+
[ 0xA2, 'Wildcard Subscriptions not supported'],
|
|
427
|
+
]);
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
return ns[0xe] = (pkt, u8_body) => {
|
|
431
|
+
if (u8_body && 5 <= pkt.mqtt_level) {
|
|
432
|
+
const rdr = new mqtt_type_reader(u8_body, 0);
|
|
433
|
+
pkt.reason = rdr.u8_reason(_disconnect_reason_);
|
|
434
|
+
pkt.props = rdr.props();
|
|
435
|
+
}
|
|
436
|
+
return pkt }
|
|
437
|
+
}
|
|
438
|
+
|
|
395
439
|
function mqtt_decode_auth(ns) {
|
|
396
440
|
const _auth_reason_ = bind_reason_lookup([
|
|
397
441
|
// MQTT 5.0
|
|
@@ -768,18 +812,19 @@ function _bind_mqtt_session_ctx(sess_decode, sess_encode, _pkt_ctx_) {
|
|
|
768
812
|
function mqtt_session_ctx(mqtt_level) {
|
|
769
813
|
let {ctx} = mqtt_session_ctx;
|
|
770
814
|
if (undefined === ctx ) {
|
|
771
|
-
|
|
815
|
+
let as_utf8 = u8 =>
|
|
772
816
|
new TextDecoder('utf-8').decode(u8);
|
|
773
817
|
|
|
774
|
-
|
|
818
|
+
let std_pkt_api = {
|
|
775
819
|
utf8(u8) { return as_utf8( u8 || this.payload ) },
|
|
776
|
-
json(u8) { return JSON.parse( as_utf8( u8 || this.payload )) },
|
|
820
|
+
json(u8) { return JSON.parse( as_utf8( u8 || this.payload ) || null ) },
|
|
777
821
|
};
|
|
778
822
|
|
|
779
823
|
mqtt_session_ctx.ctx = ctx =
|
|
780
824
|
_bind_mqtt_session_ctx(
|
|
781
825
|
[ // lst_decode_ops
|
|
782
826
|
mqtt_decode_connack,
|
|
827
|
+
mqtt_decode_disconnect,
|
|
783
828
|
mqtt_decode_publish,
|
|
784
829
|
mqtt_decode_puback,
|
|
785
830
|
mqtt_decode_pubxxx,
|
|
@@ -805,14 +850,14 @@ function mqtt_session_ctx(mqtt_level) {
|
|
|
805
850
|
}
|
|
806
851
|
|
|
807
852
|
function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
808
|
-
|
|
809
|
-
|
|
853
|
+
let q0 = _tiny_deferred_queue();
|
|
854
|
+
let q = _tiny_deferred_queue();
|
|
810
855
|
|
|
811
|
-
|
|
856
|
+
let _asy_send = async (...args) =>
|
|
812
857
|
(await q)(...args);
|
|
813
858
|
let _send = client._send = _asy_send;
|
|
814
859
|
|
|
815
|
-
|
|
860
|
+
let _ping = () => client._send('pingreq');
|
|
816
861
|
let tid_ping, _is_set = false;
|
|
817
862
|
|
|
818
863
|
return {
|
|
@@ -839,7 +884,7 @@ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
|
839
884
|
_send = await q0;}
|
|
840
885
|
|
|
841
886
|
// await connack response
|
|
842
|
-
|
|
887
|
+
let res = await _send(...args);
|
|
843
888
|
|
|
844
889
|
client._send = _send;
|
|
845
890
|
q.notify(_send);
|
|
@@ -848,15 +893,15 @@ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
|
848
893
|
, set(mqtt_session, send_u8_pkt) {
|
|
849
894
|
_is_set = true;
|
|
850
895
|
|
|
851
|
-
|
|
896
|
+
let [mqtt_decode, mqtt_encode] = mqtt_session;
|
|
852
897
|
|
|
853
|
-
|
|
898
|
+
let on_mqtt_chunk = u8_buf =>
|
|
854
899
|
on_mqtt(
|
|
855
900
|
mqtt_decode(u8_buf),
|
|
856
901
|
{mqtt: client});
|
|
857
902
|
|
|
858
903
|
_send = async (type, pkt, key) => {
|
|
859
|
-
|
|
904
|
+
let res = undefined !== key
|
|
860
905
|
? pkt_future(key) : true;
|
|
861
906
|
|
|
862
907
|
await send_u8_pkt(
|
|
@@ -875,13 +920,16 @@ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
|
875
920
|
|
|
876
921
|
async function _async_evt(obj, evt) {
|
|
877
922
|
// microtask break lookup
|
|
878
|
-
if (undefined !== evt)
|
|
923
|
+
if (undefined !== evt) {
|
|
879
924
|
await evt.call(obj, await obj);
|
|
925
|
+
}
|
|
880
926
|
}
|
|
881
927
|
function _tiny_deferred_queue() {
|
|
882
|
-
|
|
928
|
+
let q = []; // tiny resetting deferred queue
|
|
883
929
|
q.then = y => { q.push(y); };
|
|
884
|
-
q.notify = v => {
|
|
930
|
+
q.notify = v => {
|
|
931
|
+
for (let fn of q.splice(0,q.length))
|
|
932
|
+
fn(v); };
|
|
885
933
|
return q
|
|
886
934
|
}
|
|
887
935
|
|
|
@@ -917,14 +965,14 @@ function parse(str, loose) {
|
|
|
917
965
|
function _ignore(pkt, params, ctx) {ctx.done = true;}
|
|
918
966
|
|
|
919
967
|
function _mqtt_topic_router() {
|
|
920
|
-
|
|
921
|
-
|
|
968
|
+
let pri_lsts = [[],[]];
|
|
969
|
+
let find = topic => _mqtt_routes_iter(pri_lsts, topic);
|
|
922
970
|
|
|
923
971
|
return {find,
|
|
924
972
|
|
|
925
973
|
add(topic_route, ...args) {
|
|
926
974
|
let fn = args.pop();
|
|
927
|
-
|
|
975
|
+
let priority = args.pop();
|
|
928
976
|
|
|
929
977
|
if ('function' !== typeof fn) {
|
|
930
978
|
if (false === fn) {
|
|
@@ -934,44 +982,55 @@ function _mqtt_topic_router() {
|
|
|
934
982
|
let rte = parse(
|
|
935
983
|
topic_route.replace(/[+#]$/, '*'));
|
|
936
984
|
|
|
985
|
+
rte.key = topic_route;
|
|
937
986
|
rte.tgt = fn;
|
|
938
987
|
pri_lsts[priority ? 0 : 1].push(rte);
|
|
939
988
|
return this}
|
|
940
989
|
|
|
990
|
+
, remove(topic_route, priority) {
|
|
991
|
+
let lst = pri_lsts[priority ? 0 : 1];
|
|
992
|
+
lst = lst.filter(e => e.key !== topic_route);
|
|
993
|
+
pri_lsts[priority ? 0 : 1] = lst;}
|
|
994
|
+
|
|
995
|
+
, clear(priority) {
|
|
996
|
+
pri_lsts[priority ? 0 : 1] = [];
|
|
997
|
+
if (null == priority) {
|
|
998
|
+
pri_lsts[1] = [];} }
|
|
999
|
+
|
|
941
1000
|
, async invoke(pkt, ctx) {
|
|
942
1001
|
ctx.idx = 0;
|
|
943
1002
|
|
|
944
|
-
for (
|
|
1003
|
+
for (let [fn, params] of find(pkt.topic)) {
|
|
945
1004
|
await fn(pkt, params, ctx);
|
|
946
1005
|
|
|
947
1006
|
if (ctx.done) {
|
|
948
1007
|
break}
|
|
949
1008
|
else ctx.idx++;}
|
|
950
1009
|
|
|
951
|
-
|
|
1010
|
+
let {pkt_id, qos} = pkt;
|
|
952
1011
|
if (1 === qos) {
|
|
953
1012
|
await ctx.mqtt._send('puback', {pkt_id});} } } }
|
|
954
1013
|
|
|
955
1014
|
|
|
956
1015
|
function * _mqtt_routes_iter(all_route_lists, topic) {
|
|
957
|
-
for (
|
|
958
|
-
for (
|
|
959
|
-
|
|
1016
|
+
for (let route_list of all_route_lists) {
|
|
1017
|
+
for (let route of route_list) {
|
|
1018
|
+
let res = _mqtt_route_match_one(topic, route);
|
|
960
1019
|
if (undefined !== res) {
|
|
961
1020
|
yield res;} } } }
|
|
962
1021
|
|
|
963
1022
|
|
|
964
1023
|
function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
|
|
965
|
-
|
|
1024
|
+
let match = pattern.exec('/'+topic);
|
|
966
1025
|
if (null === match) {
|
|
967
1026
|
return}
|
|
968
1027
|
|
|
969
1028
|
if (false === keys) {
|
|
970
|
-
|
|
1029
|
+
let {groups} = match;
|
|
971
1030
|
if (! groups) {
|
|
972
1031
|
return [tgt]}
|
|
973
1032
|
|
|
974
|
-
|
|
1033
|
+
let params = {};
|
|
975
1034
|
for (let k in groups) {
|
|
976
1035
|
params[k] = groups[k];}
|
|
977
1036
|
|
|
@@ -980,7 +1039,7 @@ function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
|
|
|
980
1039
|
if (0 === keys.length) {
|
|
981
1040
|
return [tgt]}
|
|
982
1041
|
|
|
983
|
-
|
|
1042
|
+
let params = {};
|
|
984
1043
|
for (let i=0; i<keys.length; i++) {
|
|
985
1044
|
params[ keys[i] ] = match[1+i];}
|
|
986
1045
|
return [tgt, params]}
|
|
@@ -990,10 +1049,10 @@ const _mqtt_cmdid_dispatch ={
|
|
|
990
1049
|
return {__proto__: this, target, hashbelt: [new Map()]} }
|
|
991
1050
|
|
|
992
1051
|
, bind_pkt_future(_pkt_id=100) {
|
|
993
|
-
|
|
1052
|
+
let {hashbelt} = this;
|
|
994
1053
|
|
|
995
1054
|
let _tmp_; // use _tmp_ to reuse _by_key closure
|
|
996
|
-
|
|
1055
|
+
let _by_key = answer_monad =>
|
|
997
1056
|
hashbelt[0].set(_tmp_, answer_monad);
|
|
998
1057
|
|
|
999
1058
|
return (( pkt_or_key ) => {
|
|
@@ -1006,8 +1065,8 @@ const _mqtt_cmdid_dispatch ={
|
|
|
1006
1065
|
return new Promise(_by_key)}) }
|
|
1007
1066
|
|
|
1008
1067
|
, answer(key, pkt) {
|
|
1009
|
-
for (
|
|
1010
|
-
|
|
1068
|
+
for (let map of this.hashbelt) {
|
|
1069
|
+
let answer_monad = map.get(key);
|
|
1011
1070
|
if (undefined !== answer_monad) {
|
|
1012
1071
|
map.delete(key);
|
|
1013
1072
|
|
|
@@ -1016,10 +1075,10 @@ const _mqtt_cmdid_dispatch ={
|
|
|
1016
1075
|
return false}
|
|
1017
1076
|
|
|
1018
1077
|
, rotate_belt(n) {
|
|
1019
|
-
|
|
1078
|
+
let {hashbelt} = this;
|
|
1020
1079
|
hashbelt.unshift(new Map());
|
|
1021
|
-
for (
|
|
1022
|
-
for (
|
|
1080
|
+
for (let old of hashbelt.splice(n || 5)) {
|
|
1081
|
+
for (let answer_monad of old.values()) {
|
|
1023
1082
|
answer_monad([/*pkt*/, 'expired']); } } }// option/maybe monad
|
|
1024
1083
|
|
|
1025
1084
|
, cmdids: ((() => {
|
|
@@ -1050,18 +1109,18 @@ const _mqtt_cmdid_dispatch ={
|
|
|
1050
1109
|
by_evt(disp, pkt, ctx);}
|
|
1051
1110
|
|
|
1052
1111
|
async function by_evt({target}, pkt, ctx) {
|
|
1053
|
-
|
|
1112
|
+
let fn = target[`mqtt_${pkt.type}`]
|
|
1054
1113
|
|| target.mqtt_pkt;
|
|
1055
1114
|
|
|
1056
1115
|
if (undefined !== fn) {
|
|
1057
1116
|
await fn.call(target, pkt, ctx);} } })()) };
|
|
1058
1117
|
|
|
1059
1118
|
function _mqtt_dispatch(opt, target) {
|
|
1060
|
-
|
|
1061
|
-
|
|
1119
|
+
let _disp_ = _mqtt_cmdid_dispatch.create(target);
|
|
1120
|
+
let { cmdids } = _disp_;
|
|
1062
1121
|
|
|
1063
1122
|
// default rotate at 1s across 5 buckets
|
|
1064
|
-
|
|
1123
|
+
let { td: rotate_td=1000, n: rotate_n=5 } =
|
|
1065
1124
|
opt && opt.rotate || {};
|
|
1066
1125
|
|
|
1067
1126
|
let rotate_ts = rotate_td + Date.now();
|
|
@@ -1070,7 +1129,7 @@ function _mqtt_dispatch(opt, target) {
|
|
|
1070
1129
|
_disp_.bind_pkt_future()]
|
|
1071
1130
|
|
|
1072
1131
|
function on_mqtt(pkt_list, ctx) {
|
|
1073
|
-
for (
|
|
1132
|
+
for (let pkt of pkt_list) {
|
|
1074
1133
|
cmdids[pkt.id](_disp_, pkt, ctx); }
|
|
1075
1134
|
|
|
1076
1135
|
if (Date.now() > rotate_ts) {
|
|
@@ -1095,7 +1154,7 @@ class MQTTBaseClient {
|
|
|
1095
1154
|
if (null == pkt.keep_alive) {
|
|
1096
1155
|
pkt.keep_alive = 60;}
|
|
1097
1156
|
|
|
1098
|
-
|
|
1157
|
+
let res = await this._conn_
|
|
1099
1158
|
.send_connect('connect', pkt, 'connack');
|
|
1100
1159
|
|
|
1101
1160
|
// TODO: merge with server's keep_alive frequency
|
|
@@ -1103,7 +1162,7 @@ class MQTTBaseClient {
|
|
|
1103
1162
|
return res}
|
|
1104
1163
|
|
|
1105
1164
|
async disconnect(pkt={}) {
|
|
1106
|
-
|
|
1165
|
+
let res = await this._send('disconnect', pkt);
|
|
1107
1166
|
this._conn_.reset();
|
|
1108
1167
|
return res}
|
|
1109
1168
|
|
|
@@ -1123,20 +1182,38 @@ class MQTTBaseClient {
|
|
|
1123
1182
|
pkt = _as_topics(pkt, ex);
|
|
1124
1183
|
return this._send('unsubscribe', pkt, pkt)}
|
|
1125
1184
|
|
|
1126
|
-
|
|
1185
|
+
get on_topic() {return this.router.add}
|
|
1186
|
+
|
|
1187
|
+
// alias: sub_topic
|
|
1127
1188
|
subscribe_topic(topic_route, ...args) {
|
|
1128
|
-
let topic =
|
|
1129
|
-
this.
|
|
1189
|
+
let topic = this.topic_for(topic_route);
|
|
1190
|
+
this.router.add(topic_route, true, args.pop() );// handler
|
|
1130
1191
|
this.subscribe([[ topic ]], args.pop() );// ex
|
|
1131
1192
|
return this}
|
|
1132
1193
|
|
|
1194
|
+
// alias: unsub_topic
|
|
1195
|
+
unsubscribe_topic(topic_route) {
|
|
1196
|
+
let topic = this.topic_for(topic_route);
|
|
1197
|
+
this.router.remove(topic_route, true);
|
|
1198
|
+
return this.unsubscribe([[ topic ]]) }
|
|
1199
|
+
|
|
1200
|
+
topic_for(topic_route) {
|
|
1201
|
+
return topic_route.replace(/[:*].*$/, '#')}
|
|
1202
|
+
|
|
1133
1203
|
|
|
1134
1204
|
// alias: pub
|
|
1135
|
-
publish(pkt) {return _pub(this, pkt)}
|
|
1136
|
-
post(topic, payload) {return _pub(this,
|
|
1137
|
-
send(topic, payload) {return _pub(this,
|
|
1138
|
-
|
|
1139
|
-
|
|
1205
|
+
publish(pkt, fn_encode) {return _pub(this, pkt, fn_encode)}
|
|
1206
|
+
post(topic, payload) {return _pub.m(this, topic, payload)}
|
|
1207
|
+
send(topic, payload) {return _pub.mq(this, topic, payload)}
|
|
1208
|
+
store(topic, payload) {return _pub.mqr(this, topic, payload)}
|
|
1209
|
+
|
|
1210
|
+
json_post(topic, msg) {return _pub.o(this, topic, msg)}
|
|
1211
|
+
json_send(topic, msg) {return _pub.oq(this, topic, msg)}
|
|
1212
|
+
json_store(topic, msg) {return _pub.oqr(this, topic, msg)}
|
|
1213
|
+
|
|
1214
|
+
obj_post(topic, msg, fn_encode) {return _pub.o(this, topic, msg, fn_encode)}
|
|
1215
|
+
obj_send(topic, msg, fn_encode) {return _pub.oq(this, topic, msg, fn_encode)}
|
|
1216
|
+
obj_store(topic, msg, fn_encode) {return _pub.oqr(this, topic, msg, fn_encode)}
|
|
1140
1217
|
|
|
1141
1218
|
|
|
1142
1219
|
|
|
@@ -1173,27 +1250,29 @@ class MQTTBaseClient {
|
|
|
1173
1250
|
/* async _send(type, pkt) -- provided by _conn_ and transport */
|
|
1174
1251
|
|
|
1175
1252
|
_init_router(opt) {
|
|
1176
|
-
|
|
1177
|
-
this.on_topic = router.add;
|
|
1178
|
-
return this.router = router}
|
|
1253
|
+
return this.router = _mqtt_topic_router()}
|
|
1179
1254
|
|
|
1180
1255
|
_init_dispatch(opt) {
|
|
1181
|
-
|
|
1256
|
+
let router = this._init_router(opt, this);
|
|
1182
1257
|
|
|
1183
|
-
|
|
1258
|
+
let tgt ={
|
|
1184
1259
|
__proto__: opt.on_mqtt_type || {}
|
|
1185
|
-
,
|
|
1260
|
+
, router};
|
|
1186
1261
|
|
|
1187
|
-
|
|
1262
|
+
if (! tgt.mqtt_publish) {
|
|
1263
|
+
tgt.mqtt_publish = router.invoke;}
|
|
1264
|
+
|
|
1265
|
+
return _mqtt_dispatch(this, tgt)} }
|
|
1188
1266
|
|
|
1189
1267
|
|
|
1190
1268
|
{
|
|
1191
|
-
|
|
1269
|
+
let p = MQTTBaseClient.prototype;
|
|
1192
1270
|
Object.assign(p,{
|
|
1193
1271
|
pub: p.publish
|
|
1194
1272
|
, sub: p.subscribe
|
|
1195
1273
|
, unsub: p.unsubscribe
|
|
1196
|
-
, sub_topic: p.subscribe_topic
|
|
1274
|
+
, sub_topic: p.subscribe_topic
|
|
1275
|
+
, unsub_topic: p.unsubscribe_topic} );
|
|
1197
1276
|
|
|
1198
1277
|
/*
|
|
1199
1278
|
p.on_mqtt_type = {
|
|
@@ -1202,6 +1281,7 @@ class MQTTBaseClient {
|
|
|
1202
1281
|
mqtt_connack(pkt, ctx) ::
|
|
1203
1282
|
mqtt_disconnect(pkt, ctx) ::
|
|
1204
1283
|
|
|
1284
|
+
mqtt_publish(pkt, ctx)
|
|
1205
1285
|
mqtt_subscribe(pkt, ctx) ::
|
|
1206
1286
|
mqtt_unsubscribe(pkt, ctx) ::
|
|
1207
1287
|
|
|
@@ -1211,18 +1291,6 @@ class MQTTBaseClient {
|
|
|
1211
1291
|
*/}
|
|
1212
1292
|
|
|
1213
1293
|
|
|
1214
|
-
function _pub(self, pkt) {
|
|
1215
|
-
let key, {qos, msg, payload} = pkt;
|
|
1216
|
-
if (undefined === payload) {
|
|
1217
|
-
if (undefined === msg) {
|
|
1218
|
-
const arg = pkt.arg || 'payload';
|
|
1219
|
-
return v => _pub(self, {...pkt, [arg]: v}) }
|
|
1220
|
-
|
|
1221
|
-
pkt.payload = JSON.stringify(msg);}
|
|
1222
|
-
|
|
1223
|
-
if (1 === qos) key = pkt;
|
|
1224
|
-
return self._send('publish', pkt, key)}
|
|
1225
|
-
|
|
1226
1294
|
function _as_topics(pkt, ex) {
|
|
1227
1295
|
if ('string' === typeof pkt) {
|
|
1228
1296
|
return {topics:[pkt], ... ex}}
|
|
@@ -1230,6 +1298,43 @@ function _as_topics(pkt, ex) {
|
|
|
1230
1298
|
return {topics:[... pkt], ... ex}}
|
|
1231
1299
|
return ex ? {...pkt, ...ex} : pkt}
|
|
1232
1300
|
|
|
1301
|
+
|
|
1302
|
+
function _pub(self, pkt, fn_encode) {
|
|
1303
|
+
if (undefined === pkt.payload) {
|
|
1304
|
+
let {msg} = pkt;
|
|
1305
|
+
switch (typeof msg) {
|
|
1306
|
+
case 'function':
|
|
1307
|
+
fn_encode = msg;
|
|
1308
|
+
msg = undefined;
|
|
1309
|
+
|
|
1310
|
+
case 'undefined':
|
|
1311
|
+
let arg = pkt.arg || 'payload';
|
|
1312
|
+
return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
|
|
1313
|
+
|
|
1314
|
+
default:
|
|
1315
|
+
pkt.payload = fn_encode
|
|
1316
|
+
? fn_encode(msg)
|
|
1317
|
+
: JSON.stringify(msg);} }
|
|
1318
|
+
|
|
1319
|
+
return self._send('publish', pkt,
|
|
1320
|
+
pkt.qos ? pkt : void 0 ) }// key
|
|
1321
|
+
|
|
1322
|
+
{
|
|
1323
|
+
Object.assign(_pub,{
|
|
1324
|
+
m: (self, topic, payload) =>
|
|
1325
|
+
_pub(self, {topic, payload, qos:0})
|
|
1326
|
+
, mq: (self, topic, payload) =>
|
|
1327
|
+
_pub(self, {topic, payload, qos:1})
|
|
1328
|
+
, mqr: (self, topic, payload) =>
|
|
1329
|
+
_pub(self, {topic, payload, qos:1, retain: 1})
|
|
1330
|
+
|
|
1331
|
+
, o: (self, topic, msg, fn_encode) =>
|
|
1332
|
+
_pub(self, {topic, msg, arg: 'msg', qos:0}, fn_encode)
|
|
1333
|
+
, oq: (self, topic, msg, fn_encode) =>
|
|
1334
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1}, fn_encode)
|
|
1335
|
+
, oqr: (self, topic, msg, fn_encode) =>
|
|
1336
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, fn_encode)} ); }
|
|
1337
|
+
|
|
1233
1338
|
class MQTTCoreClient extends MQTTBaseClient {
|
|
1234
1339
|
static _with_session(mqtt_session) {
|
|
1235
1340
|
this.prototype._mqtt_session = mqtt_session;}
|
|
@@ -1237,7 +1342,7 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1237
1342
|
constructor(opt={}) {
|
|
1238
1343
|
super(opt);
|
|
1239
1344
|
this.with_live(opt.on_live);
|
|
1240
|
-
this.
|
|
1345
|
+
this.with_reconnect(opt.on_reconnect);}
|
|
1241
1346
|
|
|
1242
1347
|
|
|
1243
1348
|
// on_live(client) ::
|
|
@@ -1248,24 +1353,24 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1248
1353
|
return this}
|
|
1249
1354
|
|
|
1250
1355
|
// on_reconnect(client) ::
|
|
1251
|
-
|
|
1252
|
-
if (
|
|
1253
|
-
this.
|
|
1356
|
+
with_reconnect(on_reconnect) {
|
|
1357
|
+
if (on_reconnect) {
|
|
1358
|
+
this.on_reconnect = on_reconnect;
|
|
1254
1359
|
|
|
1255
1360
|
if (! this._conn_.is_set) {
|
|
1256
|
-
|
|
1361
|
+
on_reconnect(this);} }
|
|
1257
1362
|
|
|
1258
1363
|
return this}
|
|
1259
1364
|
|
|
1260
1365
|
|
|
1261
1366
|
with_async_iter(async_iter, write_u8_pkt) {
|
|
1262
|
-
|
|
1367
|
+
let on_mqtt_chunk = this._conn_.set(
|
|
1263
1368
|
this._mqtt_session(),
|
|
1264
1369
|
write_u8_pkt);
|
|
1265
1370
|
|
|
1266
1371
|
this._msg_loop = ((async () => {
|
|
1267
1372
|
async_iter = await async_iter;
|
|
1268
|
-
for await (
|
|
1373
|
+
for await (let chunk of async_iter)
|
|
1269
1374
|
on_mqtt_chunk(chunk);
|
|
1270
1375
|
|
|
1271
1376
|
this._conn_.reset();})());
|
|
@@ -1306,7 +1411,6 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1306
1411
|
|
|
1307
1412
|
|
|
1308
1413
|
|
|
1309
|
-
|
|
1310
1414
|
|
|
1311
1415
|
|
|
1312
1416
|
with_websock(websock) {
|
|
@@ -1327,8 +1431,8 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1327
1431
|
websock.addEventListener('open', y, {once: true}));}
|
|
1328
1432
|
|
|
1329
1433
|
|
|
1330
|
-
|
|
1331
|
-
|
|
1434
|
+
let {_conn_} = this;
|
|
1435
|
+
let on_mqtt_chunk = _conn_.set(
|
|
1332
1436
|
this._mqtt_session(),
|
|
1333
1437
|
async u8_pkt =>(
|
|
1334
1438
|
await ready
|
|
@@ -1359,6 +1463,5 @@ class MQTTClient_v5 extends MQTTCoreClient {
|
|
|
1359
1463
|
|
|
1360
1464
|
const mqtt_v5 = opt => new MQTTClient_v5(opt);
|
|
1361
1465
|
|
|
1362
|
-
export default mqtt_v4;
|
|
1363
|
-
export { MQTTBaseClient, MQTTClient_v4, MQTTClient_v5, MQTTCoreClient, _mqtt_cmdid_dispatch, _mqtt_conn, _mqtt_dispatch, _mqtt_route_match_one, _mqtt_routes_iter, _mqtt_topic_router, mqtt_session_ctx, mqtt_v4, mqtt_v5 };
|
|
1466
|
+
export { MQTTBaseClient, MQTTClient_v4, MQTTClient_v5, MQTTCoreClient, _mqtt_cmdid_dispatch, _mqtt_conn, _mqtt_dispatch, _mqtt_route_match_one, _mqtt_routes_iter, _mqtt_topic_router, mqtt_v4 as default, mqtt_session_ctx, mqtt_v4, mqtt_v5 };
|
|
1364
1467
|
//# sourceMappingURL=index.mjs.map
|