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