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.
@@ -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
@@ -775,13 +819,15 @@ function mqtt_session_ctx(mqtt_level) {
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 ) || null ) },
822
+ json(u8) { return JSON.parse( this.utf8(u8) || null ) },
823
+ text(u8) { return this.utf8(u8) },
779
824
  };
780
825
 
781
826
  mqtt_session_ctx.ctx = ctx =
782
827
  _bind_mqtt_session_ctx(
783
828
  [ // lst_decode_ops
784
829
  mqtt_decode_connack,
830
+ mqtt_decode_disconnect,
785
831
  mqtt_decode_publish,
786
832
  mqtt_decode_puback,
787
833
  mqtt_decode_pubxxx,
@@ -939,10 +985,21 @@ function _mqtt_topic_router() {
939
985
  let rte = parse(
940
986
  topic_route.replace(/[+#]$/, '*'));
941
987
 
988
+ rte.key = topic_route;
942
989
  rte.tgt = fn;
943
990
  pri_lsts[priority ? 0 : 1].push(rte);
944
991
  return this}
945
992
 
993
+ , remove(topic_route, priority) {
994
+ let lst = pri_lsts[priority ? 0 : 1];
995
+ lst = lst.filter(e => e.key !== topic_route);
996
+ pri_lsts[priority ? 0 : 1] = lst;}
997
+
998
+ , clear(priority) {
999
+ pri_lsts[priority ? 0 : 1] = [];
1000
+ if (null == priority) {
1001
+ pri_lsts[1] = [];} }
1002
+
946
1003
  , async invoke(pkt, ctx) {
947
1004
  ctx.idx = 0;
948
1005
 
@@ -967,7 +1024,10 @@ function * _mqtt_routes_iter(all_route_lists, topic) {
967
1024
 
968
1025
 
969
1026
  function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
970
- let match = pattern.exec('/'+topic);
1027
+ let match = '/' !== topic[0]
1028
+ ? pattern.exec('/'+topic)
1029
+ : pattern.exec(topic);
1030
+
971
1031
  if (null === match) {
972
1032
  return}
973
1033
 
@@ -1128,16 +1188,27 @@ class MQTTBaseClient {
1128
1188
  pkt = _as_topics(pkt, ex);
1129
1189
  return this._send('unsubscribe', pkt, pkt)}
1130
1190
 
1191
+ get on_topic() {return this.router.add}
1192
+
1131
1193
  // alias: sub_topic
1132
1194
  subscribe_topic(topic_route, ...args) {
1133
- let topic = topic_route.replace(/[:*].*$/, '#');
1134
- this.on_topic(topic_route, true, args.pop() );// handler
1195
+ let topic = this.topic_for(topic_route);
1196
+ this.router.add(topic_route, true, args.pop() );// handler
1135
1197
  this.subscribe([[ topic ]], args.pop() );// ex
1136
1198
  return this}
1137
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
+
1138
1209
 
1139
1210
  // alias: pub
1140
- publish(pkt, encode) {return _pub(this, pkt, encode)}
1211
+ publish(pkt, fn_encode) {return _pub(this, pkt, fn_encode)}
1141
1212
  post(topic, payload) {return _pub.m(this, topic, payload)}
1142
1213
  send(topic, payload) {return _pub.mq(this, topic, payload)}
1143
1214
  store(topic, payload) {return _pub.mqr(this, topic, payload)}
@@ -1146,9 +1217,9 @@ class MQTTBaseClient {
1146
1217
  json_send(topic, msg) {return _pub.oq(this, topic, msg)}
1147
1218
  json_store(topic, msg) {return _pub.oqr(this, topic, msg)}
1148
1219
 
1149
- obj_post(topic, msg, encode) {return _pub.o(this, topic, msg, encode)}
1150
- obj_send(topic, msg, encode) {return _pub.oq(this, topic, msg, encode)}
1151
- obj_store(topic, msg, encode) {return _pub.oqr(this, topic, msg, encode)}
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)}
1152
1223
 
1153
1224
 
1154
1225
 
@@ -1185,18 +1256,19 @@ class MQTTBaseClient {
1185
1256
  /* async _send(type, pkt) -- provided by _conn_ and transport */
1186
1257
 
1187
1258
  _init_router(opt) {
1188
- let router = _mqtt_topic_router();
1189
- this.on_topic = router.add;
1190
- return this.router = router}
1259
+ return this.router = _mqtt_topic_router()}
1191
1260
 
1192
1261
  _init_dispatch(opt) {
1193
1262
  let router = this._init_router(opt, this);
1194
1263
 
1195
1264
  let tgt ={
1196
1265
  __proto__: opt.on_mqtt_type || {}
1197
- , mqtt_publish: router.invoke};
1266
+ , router};
1198
1267
 
1199
- return _mqtt_dispatch(this, tgt) } }
1268
+ if (! tgt.mqtt_publish) {
1269
+ tgt.mqtt_publish = router.invoke;}
1270
+
1271
+ return _mqtt_dispatch(this, tgt)} }
1200
1272
 
1201
1273
 
1202
1274
  {
@@ -1205,7 +1277,8 @@ class MQTTBaseClient {
1205
1277
  pub: p.publish
1206
1278
  , sub: p.subscribe
1207
1279
  , unsub: p.unsubscribe
1208
- , sub_topic: p.subscribe_topic} );
1280
+ , sub_topic: p.subscribe_topic
1281
+ , unsub_topic: p.unsubscribe_topic} );
1209
1282
 
1210
1283
  /*
1211
1284
  p.on_mqtt_type = {
@@ -1214,6 +1287,7 @@ class MQTTBaseClient {
1214
1287
  mqtt_connack(pkt, ctx) ::
1215
1288
  mqtt_disconnect(pkt, ctx) ::
1216
1289
 
1290
+ mqtt_publish(pkt, ctx)
1217
1291
  mqtt_subscribe(pkt, ctx) ::
1218
1292
  mqtt_unsubscribe(pkt, ctx) ::
1219
1293
 
@@ -1231,16 +1305,22 @@ function _as_topics(pkt, ex) {
1231
1305
  return ex ? {...pkt, ...ex} : pkt}
1232
1306
 
1233
1307
 
1234
- function _pub(self, pkt, encode) {
1308
+ function _pub(self, pkt, fn_encode) {
1235
1309
  if (undefined === pkt.payload) {
1236
1310
  let {msg} = pkt;
1237
- if (undefined === msg) {
1238
- let arg = pkt.arg || 'payload';
1239
- return v => _pub(self, {...pkt, [arg]: v}, encode)}
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)
1240
1319
 
1241
- pkt.payload = encode
1242
- ? encode(msg)
1243
- : JSON.stringify(msg);}
1320
+ default:
1321
+ pkt.payload = fn_encode
1322
+ ? fn_encode(msg)
1323
+ : JSON.stringify(msg);} }
1244
1324
 
1245
1325
  return self._send('publish', pkt,
1246
1326
  pkt.qos ? pkt : void 0 ) }// key
@@ -1254,12 +1334,12 @@ function _pub(self, pkt, encode) {
1254
1334
  , mqr: (self, topic, payload) =>
1255
1335
  _pub(self, {topic, payload, qos:1, retain: 1})
1256
1336
 
1257
- , o: (self, topic, msg, encode) =>
1258
- _pub(self, {topic, msg, arg: 'msg', qos:0}, encode)
1259
- , oq: (self, topic, msg, encode) =>
1260
- _pub(self, {topic, msg, arg: 'msg', qos:1}, encode)
1261
- , oqr: (self, topic, msg, encode) =>
1262
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, encode)} ); }
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)} ); }
1263
1343
 
1264
1344
  class MQTTCoreClient extends MQTTBaseClient {
1265
1345
  static _with_session(mqtt_session) {
@@ -1322,7 +1402,7 @@ class MQTTCoreClient extends MQTTBaseClient {
1322
1402
 
1323
1403
  with_tcp(port, hostname) {
1324
1404
  if (!Number.isFinite(port)) {
1325
- ({port, host: hostname} = port);}
1405
+ ({port, hostname} = new URL(port));}
1326
1406
 
1327
1407
  let sock = connect(port, hostname);
1328
1408
  return this.with_stream(sock)}
@@ -1333,7 +1413,6 @@ class MQTTCoreClient extends MQTTBaseClient {
1333
1413
  if (undefined === write_stream) {
1334
1414
  write_stream = read_stream;}
1335
1415
 
1336
- read_stream.once('end', this._conn_.reset);
1337
1416
  return this.with_async_iter(read_stream,
1338
1417
  u8_pkt => write_stream.write(u8_pkt)) }
1339
1418