u8-mqtt 0.0.24 → 0.0.28

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/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
@@ -773,13 +817,15 @@ function mqtt_session_ctx(mqtt_level) {
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 ) || null ) },
820
+ json(u8) { return JSON.parse( this.utf8(u8) || null ) },
821
+ text(u8) { return this.utf8(u8) },
777
822
  };
778
823
 
779
824
  mqtt_session_ctx.ctx = ctx =
780
825
  _bind_mqtt_session_ctx(
781
826
  [ // lst_decode_ops
782
827
  mqtt_decode_connack,
828
+ mqtt_decode_disconnect,
783
829
  mqtt_decode_publish,
784
830
  mqtt_decode_puback,
785
831
  mqtt_decode_pubxxx,
@@ -937,10 +983,21 @@ function _mqtt_topic_router() {
937
983
  let rte = parse(
938
984
  topic_route.replace(/[+#]$/, '*'));
939
985
 
986
+ rte.key = topic_route;
940
987
  rte.tgt = fn;
941
988
  pri_lsts[priority ? 0 : 1].push(rte);
942
989
  return this}
943
990
 
991
+ , remove(topic_route, priority) {
992
+ let lst = pri_lsts[priority ? 0 : 1];
993
+ lst = lst.filter(e => e.key !== topic_route);
994
+ pri_lsts[priority ? 0 : 1] = lst;}
995
+
996
+ , clear(priority) {
997
+ pri_lsts[priority ? 0 : 1] = [];
998
+ if (null == priority) {
999
+ pri_lsts[1] = [];} }
1000
+
944
1001
  , async invoke(pkt, ctx) {
945
1002
  ctx.idx = 0;
946
1003
 
@@ -965,7 +1022,10 @@ function * _mqtt_routes_iter(all_route_lists, topic) {
965
1022
 
966
1023
 
967
1024
  function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
968
- let match = pattern.exec('/'+topic);
1025
+ let match = '/' !== topic[0]
1026
+ ? pattern.exec('/'+topic)
1027
+ : pattern.exec(topic);
1028
+
969
1029
  if (null === match) {
970
1030
  return}
971
1031
 
@@ -1126,16 +1186,27 @@ class MQTTBaseClient {
1126
1186
  pkt = _as_topics(pkt, ex);
1127
1187
  return this._send('unsubscribe', pkt, pkt)}
1128
1188
 
1189
+ get on_topic() {return this.router.add}
1190
+
1129
1191
  // alias: sub_topic
1130
1192
  subscribe_topic(topic_route, ...args) {
1131
- let topic = topic_route.replace(/[:*].*$/, '#');
1132
- this.on_topic(topic_route, true, args.pop() );// handler
1193
+ let topic = this.topic_for(topic_route);
1194
+ this.router.add(topic_route, true, args.pop() );// handler
1133
1195
  this.subscribe([[ topic ]], args.pop() );// ex
1134
1196
  return this}
1135
1197
 
1198
+ // alias: unsub_topic
1199
+ unsubscribe_topic(topic_route) {
1200
+ let topic = this.topic_for(topic_route);
1201
+ this.router.remove(topic_route, true);
1202
+ return this.unsubscribe([[ topic ]]) }
1203
+
1204
+ topic_for(topic_route) {
1205
+ return topic_route.replace(/[:*].*$/, '#')}
1206
+
1136
1207
 
1137
1208
  // alias: pub
1138
- publish(pkt, encode) {return _pub(this, pkt, encode)}
1209
+ publish(pkt, fn_encode) {return _pub(this, pkt, fn_encode)}
1139
1210
  post(topic, payload) {return _pub.m(this, topic, payload)}
1140
1211
  send(topic, payload) {return _pub.mq(this, topic, payload)}
1141
1212
  store(topic, payload) {return _pub.mqr(this, topic, payload)}
@@ -1144,9 +1215,9 @@ class MQTTBaseClient {
1144
1215
  json_send(topic, msg) {return _pub.oq(this, topic, msg)}
1145
1216
  json_store(topic, msg) {return _pub.oqr(this, topic, msg)}
1146
1217
 
1147
- obj_post(topic, msg, encode) {return _pub.o(this, topic, msg, encode)}
1148
- obj_send(topic, msg, encode) {return _pub.oq(this, topic, msg, encode)}
1149
- obj_store(topic, msg, encode) {return _pub.oqr(this, topic, msg, encode)}
1218
+ obj_post(topic, msg, fn_encode) {return _pub.o(this, topic, msg, fn_encode)}
1219
+ obj_send(topic, msg, fn_encode) {return _pub.oq(this, topic, msg, fn_encode)}
1220
+ obj_store(topic, msg, fn_encode) {return _pub.oqr(this, topic, msg, fn_encode)}
1150
1221
 
1151
1222
 
1152
1223
 
@@ -1183,18 +1254,19 @@ class MQTTBaseClient {
1183
1254
  /* async _send(type, pkt) -- provided by _conn_ and transport */
1184
1255
 
1185
1256
  _init_router(opt) {
1186
- let router = _mqtt_topic_router();
1187
- this.on_topic = router.add;
1188
- return this.router = router}
1257
+ return this.router = _mqtt_topic_router()}
1189
1258
 
1190
1259
  _init_dispatch(opt) {
1191
1260
  let router = this._init_router(opt, this);
1192
1261
 
1193
1262
  let tgt ={
1194
1263
  __proto__: opt.on_mqtt_type || {}
1195
- , mqtt_publish: router.invoke};
1264
+ , router};
1265
+
1266
+ if (! tgt.mqtt_publish) {
1267
+ tgt.mqtt_publish = router.invoke;}
1196
1268
 
1197
- return _mqtt_dispatch(this, tgt) } }
1269
+ return _mqtt_dispatch(this, tgt)} }
1198
1270
 
1199
1271
 
1200
1272
  {
@@ -1203,7 +1275,8 @@ class MQTTBaseClient {
1203
1275
  pub: p.publish
1204
1276
  , sub: p.subscribe
1205
1277
  , unsub: p.unsubscribe
1206
- , sub_topic: p.subscribe_topic} );
1278
+ , sub_topic: p.subscribe_topic
1279
+ , unsub_topic: p.unsubscribe_topic} );
1207
1280
 
1208
1281
  /*
1209
1282
  p.on_mqtt_type = {
@@ -1212,6 +1285,7 @@ class MQTTBaseClient {
1212
1285
  mqtt_connack(pkt, ctx) ::
1213
1286
  mqtt_disconnect(pkt, ctx) ::
1214
1287
 
1288
+ mqtt_publish(pkt, ctx)
1215
1289
  mqtt_subscribe(pkt, ctx) ::
1216
1290
  mqtt_unsubscribe(pkt, ctx) ::
1217
1291
 
@@ -1229,16 +1303,22 @@ function _as_topics(pkt, ex) {
1229
1303
  return ex ? {...pkt, ...ex} : pkt}
1230
1304
 
1231
1305
 
1232
- function _pub(self, pkt, encode) {
1306
+ function _pub(self, pkt, fn_encode) {
1233
1307
  if (undefined === pkt.payload) {
1234
1308
  let {msg} = pkt;
1235
- if (undefined === msg) {
1236
- let arg = pkt.arg || 'payload';
1237
- return v => _pub(self, {...pkt, [arg]: v}, encode)}
1309
+ switch (typeof msg) {
1310
+ case 'function':
1311
+ fn_encode = msg;
1312
+ msg = undefined;
1238
1313
 
1239
- pkt.payload = encode
1240
- ? encode(msg)
1241
- : JSON.stringify(msg);}
1314
+ case 'undefined':
1315
+ let arg = pkt.arg || 'payload';
1316
+ return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
1317
+
1318
+ default:
1319
+ pkt.payload = fn_encode
1320
+ ? fn_encode(msg)
1321
+ : JSON.stringify(msg);} }
1242
1322
 
1243
1323
  return self._send('publish', pkt,
1244
1324
  pkt.qos ? pkt : void 0 ) }// key
@@ -1252,12 +1332,12 @@ function _pub(self, pkt, encode) {
1252
1332
  , mqr: (self, topic, payload) =>
1253
1333
  _pub(self, {topic, payload, qos:1, retain: 1})
1254
1334
 
1255
- , o: (self, topic, msg, encode) =>
1256
- _pub(self, {topic, msg, arg: 'msg', qos:0}, encode)
1257
- , oq: (self, topic, msg, encode) =>
1258
- _pub(self, {topic, msg, arg: 'msg', qos:1}, encode)
1259
- , oqr: (self, topic, msg, encode) =>
1260
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, encode)} ); }
1335
+ , o: (self, topic, msg, fn_encode) =>
1336
+ _pub(self, {topic, msg, arg: 'msg', qos:0}, fn_encode)
1337
+ , oq: (self, topic, msg, fn_encode) =>
1338
+ _pub(self, {topic, msg, arg: 'msg', qos:1}, fn_encode)
1339
+ , oqr: (self, topic, msg, fn_encode) =>
1340
+ _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, fn_encode)} ); }
1261
1341
 
1262
1342
  class MQTTCoreClient extends MQTTBaseClient {
1263
1343
  static _with_session(mqtt_session) {
@@ -1335,7 +1415,6 @@ class MQTTCoreClient extends MQTTBaseClient {
1335
1415
 
1336
1416
 
1337
1417
 
1338
-
1339
1418
 
1340
1419
 
1341
1420
  with_websock(websock) {