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/cjs/v4.cjs
CHANGED
|
@@ -398,6 +398,50 @@ function mqtt_decode_pingxxx(ns) {
|
|
|
398
398
|
return ns[0xc] = ns[0xd] = pkt => pkt
|
|
399
399
|
}
|
|
400
400
|
|
|
401
|
+
function mqtt_decode_disconnect(ns) {
|
|
402
|
+
const _disconnect_reason_ = bind_reason_lookup([
|
|
403
|
+
// MQTT 5.0
|
|
404
|
+
[ 0x00, 'Normal disconnection'],
|
|
405
|
+
[ 0x04, 'Disconnect with Will Message'],
|
|
406
|
+
[ 0x80, 'Unspecified error'],
|
|
407
|
+
[ 0x81, 'Malformed Packet'],
|
|
408
|
+
[ 0x82, 'Protocol Error'],
|
|
409
|
+
[ 0x83, 'Implementation specific error'],
|
|
410
|
+
[ 0x87, 'Not authorized'],
|
|
411
|
+
[ 0x89, 'Server busy'],
|
|
412
|
+
[ 0x8B, 'Server shutting down'],
|
|
413
|
+
[ 0x8D, 'Keep Alive timeout'],
|
|
414
|
+
[ 0x8E, 'Session taken over'],
|
|
415
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
416
|
+
[ 0x90, 'Topic Name invalid'],
|
|
417
|
+
[ 0x93, 'Receive Maximum exceeded'],
|
|
418
|
+
[ 0x94, 'Topic Alias invalid'],
|
|
419
|
+
[ 0x95, 'Packet too large'],
|
|
420
|
+
[ 0x96, 'Message rate too high'],
|
|
421
|
+
[ 0x97, 'Quota exceeded'],
|
|
422
|
+
[ 0x98, 'Administrative action'],
|
|
423
|
+
[ 0x99, 'Payload format invalid'],
|
|
424
|
+
[ 0x9A, 'Retain not supported'],
|
|
425
|
+
[ 0x9B, 'QoS not supported'],
|
|
426
|
+
[ 0x9C, 'Use another server'],
|
|
427
|
+
[ 0x9D, 'Server moved'],
|
|
428
|
+
[ 0x9E, 'Shared Subscriptions not supported'],
|
|
429
|
+
[ 0x9F, 'Connection rate exceeded'],
|
|
430
|
+
[ 0xA0, 'Maximum connect time'],
|
|
431
|
+
[ 0xA1, 'Subscription Identifiers not supported'],
|
|
432
|
+
[ 0xA2, 'Wildcard Subscriptions not supported'],
|
|
433
|
+
]);
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
return ns[0xe] = (pkt, u8_body) => {
|
|
437
|
+
if (u8_body && 5 <= pkt.mqtt_level) {
|
|
438
|
+
const rdr = new mqtt_type_reader(u8_body, 0);
|
|
439
|
+
pkt.reason = rdr.u8_reason(_disconnect_reason_);
|
|
440
|
+
pkt.props = rdr.props();
|
|
441
|
+
}
|
|
442
|
+
return pkt }
|
|
443
|
+
}
|
|
444
|
+
|
|
401
445
|
function mqtt_decode_auth(ns) {
|
|
402
446
|
const _auth_reason_ = bind_reason_lookup([
|
|
403
447
|
// MQTT 5.0
|
|
@@ -774,18 +818,19 @@ function _bind_mqtt_session_ctx(sess_decode, sess_encode, _pkt_ctx_) {
|
|
|
774
818
|
function mqtt_session_ctx(mqtt_level) {
|
|
775
819
|
let {ctx} = mqtt_session_ctx;
|
|
776
820
|
if (undefined === ctx ) {
|
|
777
|
-
|
|
821
|
+
let as_utf8 = u8 =>
|
|
778
822
|
new TextDecoder('utf-8').decode(u8);
|
|
779
823
|
|
|
780
|
-
|
|
824
|
+
let std_pkt_api = {
|
|
781
825
|
utf8(u8) { return as_utf8( u8 || this.payload ) },
|
|
782
|
-
json(u8) { return JSON.parse( as_utf8( u8 || this.payload )) },
|
|
826
|
+
json(u8) { return JSON.parse( as_utf8( u8 || this.payload ) || null ) },
|
|
783
827
|
};
|
|
784
828
|
|
|
785
829
|
mqtt_session_ctx.ctx = ctx =
|
|
786
830
|
_bind_mqtt_session_ctx(
|
|
787
831
|
[ // lst_decode_ops
|
|
788
832
|
mqtt_decode_connack,
|
|
833
|
+
mqtt_decode_disconnect,
|
|
789
834
|
mqtt_decode_publish,
|
|
790
835
|
mqtt_decode_puback,
|
|
791
836
|
mqtt_decode_pubxxx,
|
|
@@ -811,14 +856,14 @@ function mqtt_session_ctx(mqtt_level) {
|
|
|
811
856
|
}
|
|
812
857
|
|
|
813
858
|
function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
814
|
-
|
|
815
|
-
|
|
859
|
+
let q0 = _tiny_deferred_queue();
|
|
860
|
+
let q = _tiny_deferred_queue();
|
|
816
861
|
|
|
817
|
-
|
|
862
|
+
let _asy_send = async (...args) =>
|
|
818
863
|
(await q)(...args);
|
|
819
864
|
let _send = client._send = _asy_send;
|
|
820
865
|
|
|
821
|
-
|
|
866
|
+
let _ping = () => client._send('pingreq');
|
|
822
867
|
let tid_ping, _is_set = false;
|
|
823
868
|
|
|
824
869
|
return {
|
|
@@ -845,7 +890,7 @@ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
|
845
890
|
_send = await q0;}
|
|
846
891
|
|
|
847
892
|
// await connack response
|
|
848
|
-
|
|
893
|
+
let res = await _send(...args);
|
|
849
894
|
|
|
850
895
|
client._send = _send;
|
|
851
896
|
q.notify(_send);
|
|
@@ -854,15 +899,15 @@ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
|
854
899
|
, set(mqtt_session, send_u8_pkt) {
|
|
855
900
|
_is_set = true;
|
|
856
901
|
|
|
857
|
-
|
|
902
|
+
let [mqtt_decode, mqtt_encode] = mqtt_session;
|
|
858
903
|
|
|
859
|
-
|
|
904
|
+
let on_mqtt_chunk = u8_buf =>
|
|
860
905
|
on_mqtt(
|
|
861
906
|
mqtt_decode(u8_buf),
|
|
862
907
|
{mqtt: client});
|
|
863
908
|
|
|
864
909
|
_send = async (type, pkt, key) => {
|
|
865
|
-
|
|
910
|
+
let res = undefined !== key
|
|
866
911
|
? pkt_future(key) : true;
|
|
867
912
|
|
|
868
913
|
await send_u8_pkt(
|
|
@@ -881,13 +926,16 @@ function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
|
881
926
|
|
|
882
927
|
async function _async_evt(obj, evt) {
|
|
883
928
|
// microtask break lookup
|
|
884
|
-
if (undefined !== evt)
|
|
929
|
+
if (undefined !== evt) {
|
|
885
930
|
await evt.call(obj, await obj);
|
|
931
|
+
}
|
|
886
932
|
}
|
|
887
933
|
function _tiny_deferred_queue() {
|
|
888
|
-
|
|
934
|
+
let q = []; // tiny resetting deferred queue
|
|
889
935
|
q.then = y => { q.push(y); };
|
|
890
|
-
q.notify = v => {
|
|
936
|
+
q.notify = v => {
|
|
937
|
+
for (let fn of q.splice(0,q.length))
|
|
938
|
+
fn(v); };
|
|
891
939
|
return q
|
|
892
940
|
}
|
|
893
941
|
|
|
@@ -923,14 +971,14 @@ function parse(str, loose) {
|
|
|
923
971
|
function _ignore(pkt, params, ctx) {ctx.done = true;}
|
|
924
972
|
|
|
925
973
|
function _mqtt_topic_router() {
|
|
926
|
-
|
|
927
|
-
|
|
974
|
+
let pri_lsts = [[],[]];
|
|
975
|
+
let find = topic => _mqtt_routes_iter(pri_lsts, topic);
|
|
928
976
|
|
|
929
977
|
return {find,
|
|
930
978
|
|
|
931
979
|
add(topic_route, ...args) {
|
|
932
980
|
let fn = args.pop();
|
|
933
|
-
|
|
981
|
+
let priority = args.pop();
|
|
934
982
|
|
|
935
983
|
if ('function' !== typeof fn) {
|
|
936
984
|
if (false === fn) {
|
|
@@ -940,44 +988,55 @@ function _mqtt_topic_router() {
|
|
|
940
988
|
let rte = parse(
|
|
941
989
|
topic_route.replace(/[+#]$/, '*'));
|
|
942
990
|
|
|
991
|
+
rte.key = topic_route;
|
|
943
992
|
rte.tgt = fn;
|
|
944
993
|
pri_lsts[priority ? 0 : 1].push(rte);
|
|
945
994
|
return this}
|
|
946
995
|
|
|
996
|
+
, remove(topic_route, priority) {
|
|
997
|
+
let lst = pri_lsts[priority ? 0 : 1];
|
|
998
|
+
lst = lst.filter(e => e.key !== topic_route);
|
|
999
|
+
pri_lsts[priority ? 0 : 1] = lst;}
|
|
1000
|
+
|
|
1001
|
+
, clear(priority) {
|
|
1002
|
+
pri_lsts[priority ? 0 : 1] = [];
|
|
1003
|
+
if (null == priority) {
|
|
1004
|
+
pri_lsts[1] = [];} }
|
|
1005
|
+
|
|
947
1006
|
, async invoke(pkt, ctx) {
|
|
948
1007
|
ctx.idx = 0;
|
|
949
1008
|
|
|
950
|
-
for (
|
|
1009
|
+
for (let [fn, params] of find(pkt.topic)) {
|
|
951
1010
|
await fn(pkt, params, ctx);
|
|
952
1011
|
|
|
953
1012
|
if (ctx.done) {
|
|
954
1013
|
break}
|
|
955
1014
|
else ctx.idx++;}
|
|
956
1015
|
|
|
957
|
-
|
|
1016
|
+
let {pkt_id, qos} = pkt;
|
|
958
1017
|
if (1 === qos) {
|
|
959
1018
|
await ctx.mqtt._send('puback', {pkt_id});} } } }
|
|
960
1019
|
|
|
961
1020
|
|
|
962
1021
|
function * _mqtt_routes_iter(all_route_lists, topic) {
|
|
963
|
-
for (
|
|
964
|
-
for (
|
|
965
|
-
|
|
1022
|
+
for (let route_list of all_route_lists) {
|
|
1023
|
+
for (let route of route_list) {
|
|
1024
|
+
let res = _mqtt_route_match_one(topic, route);
|
|
966
1025
|
if (undefined !== res) {
|
|
967
1026
|
yield res;} } } }
|
|
968
1027
|
|
|
969
1028
|
|
|
970
1029
|
function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
|
|
971
|
-
|
|
1030
|
+
let match = pattern.exec('/'+topic);
|
|
972
1031
|
if (null === match) {
|
|
973
1032
|
return}
|
|
974
1033
|
|
|
975
1034
|
if (false === keys) {
|
|
976
|
-
|
|
1035
|
+
let {groups} = match;
|
|
977
1036
|
if (! groups) {
|
|
978
1037
|
return [tgt]}
|
|
979
1038
|
|
|
980
|
-
|
|
1039
|
+
let params = {};
|
|
981
1040
|
for (let k in groups) {
|
|
982
1041
|
params[k] = groups[k];}
|
|
983
1042
|
|
|
@@ -986,7 +1045,7 @@ function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
|
|
|
986
1045
|
if (0 === keys.length) {
|
|
987
1046
|
return [tgt]}
|
|
988
1047
|
|
|
989
|
-
|
|
1048
|
+
let params = {};
|
|
990
1049
|
for (let i=0; i<keys.length; i++) {
|
|
991
1050
|
params[ keys[i] ] = match[1+i];}
|
|
992
1051
|
return [tgt, params]}
|
|
@@ -996,10 +1055,10 @@ const _mqtt_cmdid_dispatch ={
|
|
|
996
1055
|
return {__proto__: this, target, hashbelt: [new Map()]} }
|
|
997
1056
|
|
|
998
1057
|
, bind_pkt_future(_pkt_id=100) {
|
|
999
|
-
|
|
1058
|
+
let {hashbelt} = this;
|
|
1000
1059
|
|
|
1001
1060
|
let _tmp_; // use _tmp_ to reuse _by_key closure
|
|
1002
|
-
|
|
1061
|
+
let _by_key = answer_monad =>
|
|
1003
1062
|
hashbelt[0].set(_tmp_, answer_monad);
|
|
1004
1063
|
|
|
1005
1064
|
return (( pkt_or_key ) => {
|
|
@@ -1012,8 +1071,8 @@ const _mqtt_cmdid_dispatch ={
|
|
|
1012
1071
|
return new Promise(_by_key)}) }
|
|
1013
1072
|
|
|
1014
1073
|
, answer(key, pkt) {
|
|
1015
|
-
for (
|
|
1016
|
-
|
|
1074
|
+
for (let map of this.hashbelt) {
|
|
1075
|
+
let answer_monad = map.get(key);
|
|
1017
1076
|
if (undefined !== answer_monad) {
|
|
1018
1077
|
map.delete(key);
|
|
1019
1078
|
|
|
@@ -1022,10 +1081,10 @@ const _mqtt_cmdid_dispatch ={
|
|
|
1022
1081
|
return false}
|
|
1023
1082
|
|
|
1024
1083
|
, rotate_belt(n) {
|
|
1025
|
-
|
|
1084
|
+
let {hashbelt} = this;
|
|
1026
1085
|
hashbelt.unshift(new Map());
|
|
1027
|
-
for (
|
|
1028
|
-
for (
|
|
1086
|
+
for (let old of hashbelt.splice(n || 5)) {
|
|
1087
|
+
for (let answer_monad of old.values()) {
|
|
1029
1088
|
answer_monad([/*pkt*/, 'expired']); } } }// option/maybe monad
|
|
1030
1089
|
|
|
1031
1090
|
, cmdids: ((() => {
|
|
@@ -1056,18 +1115,18 @@ const _mqtt_cmdid_dispatch ={
|
|
|
1056
1115
|
by_evt(disp, pkt, ctx);}
|
|
1057
1116
|
|
|
1058
1117
|
async function by_evt({target}, pkt, ctx) {
|
|
1059
|
-
|
|
1118
|
+
let fn = target[`mqtt_${pkt.type}`]
|
|
1060
1119
|
|| target.mqtt_pkt;
|
|
1061
1120
|
|
|
1062
1121
|
if (undefined !== fn) {
|
|
1063
1122
|
await fn.call(target, pkt, ctx);} } })()) };
|
|
1064
1123
|
|
|
1065
1124
|
function _mqtt_dispatch(opt, target) {
|
|
1066
|
-
|
|
1067
|
-
|
|
1125
|
+
let _disp_ = _mqtt_cmdid_dispatch.create(target);
|
|
1126
|
+
let { cmdids } = _disp_;
|
|
1068
1127
|
|
|
1069
1128
|
// default rotate at 1s across 5 buckets
|
|
1070
|
-
|
|
1129
|
+
let { td: rotate_td=1000, n: rotate_n=5 } =
|
|
1071
1130
|
opt && opt.rotate || {};
|
|
1072
1131
|
|
|
1073
1132
|
let rotate_ts = rotate_td + Date.now();
|
|
@@ -1076,7 +1135,7 @@ function _mqtt_dispatch(opt, target) {
|
|
|
1076
1135
|
_disp_.bind_pkt_future()]
|
|
1077
1136
|
|
|
1078
1137
|
function on_mqtt(pkt_list, ctx) {
|
|
1079
|
-
for (
|
|
1138
|
+
for (let pkt of pkt_list) {
|
|
1080
1139
|
cmdids[pkt.id](_disp_, pkt, ctx); }
|
|
1081
1140
|
|
|
1082
1141
|
if (Date.now() > rotate_ts) {
|
|
@@ -1101,7 +1160,7 @@ class MQTTBaseClient {
|
|
|
1101
1160
|
if (null == pkt.keep_alive) {
|
|
1102
1161
|
pkt.keep_alive = 60;}
|
|
1103
1162
|
|
|
1104
|
-
|
|
1163
|
+
let res = await this._conn_
|
|
1105
1164
|
.send_connect('connect', pkt, 'connack');
|
|
1106
1165
|
|
|
1107
1166
|
// TODO: merge with server's keep_alive frequency
|
|
@@ -1109,7 +1168,7 @@ class MQTTBaseClient {
|
|
|
1109
1168
|
return res}
|
|
1110
1169
|
|
|
1111
1170
|
async disconnect(pkt={}) {
|
|
1112
|
-
|
|
1171
|
+
let res = await this._send('disconnect', pkt);
|
|
1113
1172
|
this._conn_.reset();
|
|
1114
1173
|
return res}
|
|
1115
1174
|
|
|
@@ -1129,20 +1188,38 @@ class MQTTBaseClient {
|
|
|
1129
1188
|
pkt = _as_topics(pkt, ex);
|
|
1130
1189
|
return this._send('unsubscribe', pkt, pkt)}
|
|
1131
1190
|
|
|
1132
|
-
|
|
1191
|
+
get on_topic() {return this.router.add}
|
|
1192
|
+
|
|
1193
|
+
// alias: sub_topic
|
|
1133
1194
|
subscribe_topic(topic_route, ...args) {
|
|
1134
|
-
let topic =
|
|
1135
|
-
this.
|
|
1195
|
+
let topic = this.topic_for(topic_route);
|
|
1196
|
+
this.router.add(topic_route, true, args.pop() );// handler
|
|
1136
1197
|
this.subscribe([[ topic ]], args.pop() );// ex
|
|
1137
1198
|
return this}
|
|
1138
1199
|
|
|
1200
|
+
// alias: unsub_topic
|
|
1201
|
+
unsubscribe_topic(topic_route) {
|
|
1202
|
+
let topic = this.topic_for(topic_route);
|
|
1203
|
+
this.router.remove(topic_route, true);
|
|
1204
|
+
return this.unsubscribe([[ topic ]]) }
|
|
1205
|
+
|
|
1206
|
+
topic_for(topic_route) {
|
|
1207
|
+
return topic_route.replace(/[:*].*$/, '#')}
|
|
1208
|
+
|
|
1139
1209
|
|
|
1140
1210
|
// alias: pub
|
|
1141
|
-
publish(pkt) {return _pub(this, pkt)}
|
|
1142
|
-
post(topic, payload) {return _pub(this,
|
|
1143
|
-
send(topic, payload) {return _pub(this,
|
|
1144
|
-
|
|
1145
|
-
|
|
1211
|
+
publish(pkt, fn_encode) {return _pub(this, pkt, fn_encode)}
|
|
1212
|
+
post(topic, payload) {return _pub.m(this, topic, payload)}
|
|
1213
|
+
send(topic, payload) {return _pub.mq(this, topic, payload)}
|
|
1214
|
+
store(topic, payload) {return _pub.mqr(this, topic, payload)}
|
|
1215
|
+
|
|
1216
|
+
json_post(topic, msg) {return _pub.o(this, topic, msg)}
|
|
1217
|
+
json_send(topic, msg) {return _pub.oq(this, topic, msg)}
|
|
1218
|
+
json_store(topic, msg) {return _pub.oqr(this, topic, msg)}
|
|
1219
|
+
|
|
1220
|
+
obj_post(topic, msg, fn_encode) {return _pub.o(this, topic, msg, fn_encode)}
|
|
1221
|
+
obj_send(topic, msg, fn_encode) {return _pub.oq(this, topic, msg, fn_encode)}
|
|
1222
|
+
obj_store(topic, msg, fn_encode) {return _pub.oqr(this, topic, msg, fn_encode)}
|
|
1146
1223
|
|
|
1147
1224
|
|
|
1148
1225
|
|
|
@@ -1179,27 +1256,29 @@ class MQTTBaseClient {
|
|
|
1179
1256
|
/* async _send(type, pkt) -- provided by _conn_ and transport */
|
|
1180
1257
|
|
|
1181
1258
|
_init_router(opt) {
|
|
1182
|
-
|
|
1183
|
-
this.on_topic = router.add;
|
|
1184
|
-
return this.router = router}
|
|
1259
|
+
return this.router = _mqtt_topic_router()}
|
|
1185
1260
|
|
|
1186
1261
|
_init_dispatch(opt) {
|
|
1187
|
-
|
|
1262
|
+
let router = this._init_router(opt, this);
|
|
1188
1263
|
|
|
1189
|
-
|
|
1264
|
+
let tgt ={
|
|
1190
1265
|
__proto__: opt.on_mqtt_type || {}
|
|
1191
|
-
,
|
|
1266
|
+
, router};
|
|
1267
|
+
|
|
1268
|
+
if (! tgt.mqtt_publish) {
|
|
1269
|
+
tgt.mqtt_publish = router.invoke;}
|
|
1192
1270
|
|
|
1193
|
-
return _mqtt_dispatch(this, tgt)
|
|
1271
|
+
return _mqtt_dispatch(this, tgt)} }
|
|
1194
1272
|
|
|
1195
1273
|
|
|
1196
1274
|
{
|
|
1197
|
-
|
|
1275
|
+
let p = MQTTBaseClient.prototype;
|
|
1198
1276
|
Object.assign(p,{
|
|
1199
1277
|
pub: p.publish
|
|
1200
1278
|
, sub: p.subscribe
|
|
1201
1279
|
, unsub: p.unsubscribe
|
|
1202
|
-
, sub_topic: p.subscribe_topic
|
|
1280
|
+
, sub_topic: p.subscribe_topic
|
|
1281
|
+
, unsub_topic: p.unsubscribe_topic} );
|
|
1203
1282
|
|
|
1204
1283
|
/*
|
|
1205
1284
|
p.on_mqtt_type = {
|
|
@@ -1208,6 +1287,7 @@ class MQTTBaseClient {
|
|
|
1208
1287
|
mqtt_connack(pkt, ctx) ::
|
|
1209
1288
|
mqtt_disconnect(pkt, ctx) ::
|
|
1210
1289
|
|
|
1290
|
+
mqtt_publish(pkt, ctx)
|
|
1211
1291
|
mqtt_subscribe(pkt, ctx) ::
|
|
1212
1292
|
mqtt_unsubscribe(pkt, ctx) ::
|
|
1213
1293
|
|
|
@@ -1217,18 +1297,6 @@ class MQTTBaseClient {
|
|
|
1217
1297
|
*/}
|
|
1218
1298
|
|
|
1219
1299
|
|
|
1220
|
-
function _pub(self, pkt) {
|
|
1221
|
-
let key, {qos, msg, payload} = pkt;
|
|
1222
|
-
if (undefined === payload) {
|
|
1223
|
-
if (undefined === msg) {
|
|
1224
|
-
const arg = pkt.arg || 'payload';
|
|
1225
|
-
return v => _pub(self, {...pkt, [arg]: v}) }
|
|
1226
|
-
|
|
1227
|
-
pkt.payload = JSON.stringify(msg);}
|
|
1228
|
-
|
|
1229
|
-
if (1 === qos) key = pkt;
|
|
1230
|
-
return self._send('publish', pkt, key)}
|
|
1231
|
-
|
|
1232
1300
|
function _as_topics(pkt, ex) {
|
|
1233
1301
|
if ('string' === typeof pkt) {
|
|
1234
1302
|
return {topics:[pkt], ... ex}}
|
|
@@ -1236,6 +1304,43 @@ function _as_topics(pkt, ex) {
|
|
|
1236
1304
|
return {topics:[... pkt], ... ex}}
|
|
1237
1305
|
return ex ? {...pkt, ...ex} : pkt}
|
|
1238
1306
|
|
|
1307
|
+
|
|
1308
|
+
function _pub(self, pkt, fn_encode) {
|
|
1309
|
+
if (undefined === pkt.payload) {
|
|
1310
|
+
let {msg} = pkt;
|
|
1311
|
+
switch (typeof msg) {
|
|
1312
|
+
case 'function':
|
|
1313
|
+
fn_encode = msg;
|
|
1314
|
+
msg = undefined;
|
|
1315
|
+
|
|
1316
|
+
case 'undefined':
|
|
1317
|
+
let arg = pkt.arg || 'payload';
|
|
1318
|
+
return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
|
|
1319
|
+
|
|
1320
|
+
default:
|
|
1321
|
+
pkt.payload = fn_encode
|
|
1322
|
+
? fn_encode(msg)
|
|
1323
|
+
: JSON.stringify(msg);} }
|
|
1324
|
+
|
|
1325
|
+
return self._send('publish', pkt,
|
|
1326
|
+
pkt.qos ? pkt : void 0 ) }// key
|
|
1327
|
+
|
|
1328
|
+
{
|
|
1329
|
+
Object.assign(_pub,{
|
|
1330
|
+
m: (self, topic, payload) =>
|
|
1331
|
+
_pub(self, {topic, payload, qos:0})
|
|
1332
|
+
, mq: (self, topic, payload) =>
|
|
1333
|
+
_pub(self, {topic, payload, qos:1})
|
|
1334
|
+
, mqr: (self, topic, payload) =>
|
|
1335
|
+
_pub(self, {topic, payload, qos:1, retain: 1})
|
|
1336
|
+
|
|
1337
|
+
, o: (self, topic, msg, fn_encode) =>
|
|
1338
|
+
_pub(self, {topic, msg, arg: 'msg', qos:0}, fn_encode)
|
|
1339
|
+
, oq: (self, topic, msg, fn_encode) =>
|
|
1340
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1}, fn_encode)
|
|
1341
|
+
, oqr: (self, topic, msg, fn_encode) =>
|
|
1342
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, fn_encode)} ); }
|
|
1343
|
+
|
|
1239
1344
|
class MQTTCoreClient extends MQTTBaseClient {
|
|
1240
1345
|
static _with_session(mqtt_session) {
|
|
1241
1346
|
this.prototype._mqtt_session = mqtt_session;}
|
|
@@ -1243,7 +1348,7 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1243
1348
|
constructor(opt={}) {
|
|
1244
1349
|
super(opt);
|
|
1245
1350
|
this.with_live(opt.on_live);
|
|
1246
|
-
this.
|
|
1351
|
+
this.with_reconnect(opt.on_reconnect);}
|
|
1247
1352
|
|
|
1248
1353
|
|
|
1249
1354
|
// on_live(client) ::
|
|
@@ -1254,24 +1359,24 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1254
1359
|
return this}
|
|
1255
1360
|
|
|
1256
1361
|
// on_reconnect(client) ::
|
|
1257
|
-
|
|
1258
|
-
if (
|
|
1259
|
-
this.
|
|
1362
|
+
with_reconnect(on_reconnect) {
|
|
1363
|
+
if (on_reconnect) {
|
|
1364
|
+
this.on_reconnect = on_reconnect;
|
|
1260
1365
|
|
|
1261
1366
|
if (! this._conn_.is_set) {
|
|
1262
|
-
|
|
1367
|
+
on_reconnect(this);} }
|
|
1263
1368
|
|
|
1264
1369
|
return this}
|
|
1265
1370
|
|
|
1266
1371
|
|
|
1267
1372
|
with_async_iter(async_iter, write_u8_pkt) {
|
|
1268
|
-
|
|
1373
|
+
let on_mqtt_chunk = this._conn_.set(
|
|
1269
1374
|
this._mqtt_session(),
|
|
1270
1375
|
write_u8_pkt);
|
|
1271
1376
|
|
|
1272
1377
|
this._msg_loop = ((async () => {
|
|
1273
1378
|
async_iter = await async_iter;
|
|
1274
|
-
for await (
|
|
1379
|
+
for await (let chunk of async_iter)
|
|
1275
1380
|
on_mqtt_chunk(chunk);
|
|
1276
1381
|
|
|
1277
1382
|
this._conn_.reset();})());
|
|
@@ -1299,7 +1404,7 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1299
1404
|
if (!Number.isFinite(port)) {
|
|
1300
1405
|
({port, host: hostname} = port);}
|
|
1301
1406
|
|
|
1302
|
-
|
|
1407
|
+
let sock = net.connect(port, hostname);
|
|
1303
1408
|
return this.with_stream(sock)}
|
|
1304
1409
|
|
|
1305
1410
|
|
|
@@ -1308,7 +1413,6 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1308
1413
|
if (undefined === write_stream) {
|
|
1309
1414
|
write_stream = read_stream;}
|
|
1310
1415
|
|
|
1311
|
-
read_stream.once('end', this._conn_.reset);
|
|
1312
1416
|
return this.with_async_iter(read_stream,
|
|
1313
1417
|
u8_pkt => write_stream.write(u8_pkt)) }
|
|
1314
1418
|
|
|
@@ -1333,8 +1437,8 @@ class MQTTCoreClient extends MQTTBaseClient {
|
|
|
1333
1437
|
websock.addEventListener('open', y, {once: true}));}
|
|
1334
1438
|
|
|
1335
1439
|
|
|
1336
|
-
|
|
1337
|
-
|
|
1440
|
+
let {_conn_} = this;
|
|
1441
|
+
let on_mqtt_chunk = _conn_.set(
|
|
1338
1442
|
this._mqtt_session(),
|
|
1339
1443
|
async u8_pkt =>(
|
|
1340
1444
|
await ready
|
|
@@ -1361,7 +1465,7 @@ const mqtt_v4 = opt => new MQTTClient_v4(opt);
|
|
|
1361
1465
|
|
|
1362
1466
|
exports.MQTTClient = MQTTClient_v4;
|
|
1363
1467
|
exports.MQTTClient_v4 = MQTTClient_v4;
|
|
1364
|
-
exports
|
|
1468
|
+
exports["default"] = mqtt_v4;
|
|
1365
1469
|
exports.mqtt = mqtt_v4;
|
|
1366
1470
|
exports.mqtt_v4 = mqtt_v4;
|
|
1367
1471
|
//# sourceMappingURL=v4.cjs.map
|