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/cjs/v5.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
@@ -779,13 +823,15 @@ function mqtt_session_ctx(mqtt_level) {
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 ) || null ) },
826
+ json(u8) { return JSON.parse( this.utf8(u8) || null ) },
827
+ text(u8) { return this.utf8(u8) },
783
828
  };
784
829
 
785
830
  mqtt_session_ctx.ctx = ctx =
786
831
  _bind_mqtt_session_ctx(
787
832
  [ // lst_decode_ops
788
833
  mqtt_decode_connack,
834
+ mqtt_decode_disconnect,
789
835
  mqtt_decode_publish,
790
836
  mqtt_decode_puback,
791
837
  mqtt_decode_pubxxx,
@@ -943,10 +989,21 @@ function _mqtt_topic_router() {
943
989
  let rte = parse(
944
990
  topic_route.replace(/[+#]$/, '*'));
945
991
 
992
+ rte.key = topic_route;
946
993
  rte.tgt = fn;
947
994
  pri_lsts[priority ? 0 : 1].push(rte);
948
995
  return this}
949
996
 
997
+ , remove(topic_route, priority) {
998
+ let lst = pri_lsts[priority ? 0 : 1];
999
+ lst = lst.filter(e => e.key !== topic_route);
1000
+ pri_lsts[priority ? 0 : 1] = lst;}
1001
+
1002
+ , clear(priority) {
1003
+ pri_lsts[priority ? 0 : 1] = [];
1004
+ if (null == priority) {
1005
+ pri_lsts[1] = [];} }
1006
+
950
1007
  , async invoke(pkt, ctx) {
951
1008
  ctx.idx = 0;
952
1009
 
@@ -971,7 +1028,10 @@ function * _mqtt_routes_iter(all_route_lists, topic) {
971
1028
 
972
1029
 
973
1030
  function _mqtt_route_match_one(topic, {keys, pattern, tgt}) {
974
- let match = pattern.exec('/'+topic);
1031
+ let match = '/' !== topic[0]
1032
+ ? pattern.exec('/'+topic)
1033
+ : pattern.exec(topic);
1034
+
975
1035
  if (null === match) {
976
1036
  return}
977
1037
 
@@ -1132,16 +1192,27 @@ class MQTTBaseClient {
1132
1192
  pkt = _as_topics(pkt, ex);
1133
1193
  return this._send('unsubscribe', pkt, pkt)}
1134
1194
 
1195
+ get on_topic() {return this.router.add}
1196
+
1135
1197
  // alias: sub_topic
1136
1198
  subscribe_topic(topic_route, ...args) {
1137
- let topic = topic_route.replace(/[:*].*$/, '#');
1138
- this.on_topic(topic_route, true, args.pop() );// handler
1199
+ let topic = this.topic_for(topic_route);
1200
+ this.router.add(topic_route, true, args.pop() );// handler
1139
1201
  this.subscribe([[ topic ]], args.pop() );// ex
1140
1202
  return this}
1141
1203
 
1204
+ // alias: unsub_topic
1205
+ unsubscribe_topic(topic_route) {
1206
+ let topic = this.topic_for(topic_route);
1207
+ this.router.remove(topic_route, true);
1208
+ return this.unsubscribe([[ topic ]]) }
1209
+
1210
+ topic_for(topic_route) {
1211
+ return topic_route.replace(/[:*].*$/, '#')}
1212
+
1142
1213
 
1143
1214
  // alias: pub
1144
- publish(pkt, encode) {return _pub(this, pkt, encode)}
1215
+ publish(pkt, fn_encode) {return _pub(this, pkt, fn_encode)}
1145
1216
  post(topic, payload) {return _pub.m(this, topic, payload)}
1146
1217
  send(topic, payload) {return _pub.mq(this, topic, payload)}
1147
1218
  store(topic, payload) {return _pub.mqr(this, topic, payload)}
@@ -1150,9 +1221,9 @@ class MQTTBaseClient {
1150
1221
  json_send(topic, msg) {return _pub.oq(this, topic, msg)}
1151
1222
  json_store(topic, msg) {return _pub.oqr(this, topic, msg)}
1152
1223
 
1153
- obj_post(topic, msg, encode) {return _pub.o(this, topic, msg, encode)}
1154
- obj_send(topic, msg, encode) {return _pub.oq(this, topic, msg, encode)}
1155
- obj_store(topic, msg, encode) {return _pub.oqr(this, topic, msg, encode)}
1224
+ obj_post(topic, msg, fn_encode) {return _pub.o(this, topic, msg, fn_encode)}
1225
+ obj_send(topic, msg, fn_encode) {return _pub.oq(this, topic, msg, fn_encode)}
1226
+ obj_store(topic, msg, fn_encode) {return _pub.oqr(this, topic, msg, fn_encode)}
1156
1227
 
1157
1228
 
1158
1229
 
@@ -1189,18 +1260,19 @@ class MQTTBaseClient {
1189
1260
  /* async _send(type, pkt) -- provided by _conn_ and transport */
1190
1261
 
1191
1262
  _init_router(opt) {
1192
- let router = _mqtt_topic_router();
1193
- this.on_topic = router.add;
1194
- return this.router = router}
1263
+ return this.router = _mqtt_topic_router()}
1195
1264
 
1196
1265
  _init_dispatch(opt) {
1197
1266
  let router = this._init_router(opt, this);
1198
1267
 
1199
1268
  let tgt ={
1200
1269
  __proto__: opt.on_mqtt_type || {}
1201
- , mqtt_publish: router.invoke};
1270
+ , router};
1202
1271
 
1203
- return _mqtt_dispatch(this, tgt) } }
1272
+ if (! tgt.mqtt_publish) {
1273
+ tgt.mqtt_publish = router.invoke;}
1274
+
1275
+ return _mqtt_dispatch(this, tgt)} }
1204
1276
 
1205
1277
 
1206
1278
  {
@@ -1209,7 +1281,8 @@ class MQTTBaseClient {
1209
1281
  pub: p.publish
1210
1282
  , sub: p.subscribe
1211
1283
  , unsub: p.unsubscribe
1212
- , sub_topic: p.subscribe_topic} );
1284
+ , sub_topic: p.subscribe_topic
1285
+ , unsub_topic: p.unsubscribe_topic} );
1213
1286
 
1214
1287
  /*
1215
1288
  p.on_mqtt_type = {
@@ -1218,6 +1291,7 @@ class MQTTBaseClient {
1218
1291
  mqtt_connack(pkt, ctx) ::
1219
1292
  mqtt_disconnect(pkt, ctx) ::
1220
1293
 
1294
+ mqtt_publish(pkt, ctx)
1221
1295
  mqtt_subscribe(pkt, ctx) ::
1222
1296
  mqtt_unsubscribe(pkt, ctx) ::
1223
1297
 
@@ -1235,16 +1309,22 @@ function _as_topics(pkt, ex) {
1235
1309
  return ex ? {...pkt, ...ex} : pkt}
1236
1310
 
1237
1311
 
1238
- function _pub(self, pkt, encode) {
1312
+ function _pub(self, pkt, fn_encode) {
1239
1313
  if (undefined === pkt.payload) {
1240
1314
  let {msg} = pkt;
1241
- if (undefined === msg) {
1242
- let arg = pkt.arg || 'payload';
1243
- return v => _pub(self, {...pkt, [arg]: v}, encode)}
1315
+ switch (typeof msg) {
1316
+ case 'function':
1317
+ fn_encode = msg;
1318
+ msg = undefined;
1319
+
1320
+ case 'undefined':
1321
+ let arg = pkt.arg || 'payload';
1322
+ return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
1244
1323
 
1245
- pkt.payload = encode
1246
- ? encode(msg)
1247
- : JSON.stringify(msg);}
1324
+ default:
1325
+ pkt.payload = fn_encode
1326
+ ? fn_encode(msg)
1327
+ : JSON.stringify(msg);} }
1248
1328
 
1249
1329
  return self._send('publish', pkt,
1250
1330
  pkt.qos ? pkt : void 0 ) }// key
@@ -1258,12 +1338,12 @@ function _pub(self, pkt, encode) {
1258
1338
  , mqr: (self, topic, payload) =>
1259
1339
  _pub(self, {topic, payload, qos:1, retain: 1})
1260
1340
 
1261
- , o: (self, topic, msg, encode) =>
1262
- _pub(self, {topic, msg, arg: 'msg', qos:0}, encode)
1263
- , oq: (self, topic, msg, encode) =>
1264
- _pub(self, {topic, msg, arg: 'msg', qos:1}, encode)
1265
- , oqr: (self, topic, msg, encode) =>
1266
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, encode)} ); }
1341
+ , o: (self, topic, msg, fn_encode) =>
1342
+ _pub(self, {topic, msg, arg: 'msg', qos:0}, fn_encode)
1343
+ , oq: (self, topic, msg, fn_encode) =>
1344
+ _pub(self, {topic, msg, arg: 'msg', qos:1}, fn_encode)
1345
+ , oqr: (self, topic, msg, fn_encode) =>
1346
+ _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, fn_encode)} ); }
1267
1347
 
1268
1348
  class MQTTCoreClient extends MQTTBaseClient {
1269
1349
  static _with_session(mqtt_session) {
@@ -1326,7 +1406,7 @@ class MQTTCoreClient extends MQTTBaseClient {
1326
1406
 
1327
1407
  with_tcp(port, hostname) {
1328
1408
  if (!Number.isFinite(port)) {
1329
- ({port, host: hostname} = port);}
1409
+ ({port, hostname} = new URL(port));}
1330
1410
 
1331
1411
  let sock = net.connect(port, hostname);
1332
1412
  return this.with_stream(sock)}
@@ -1337,7 +1417,6 @@ class MQTTCoreClient extends MQTTBaseClient {
1337
1417
  if (undefined === write_stream) {
1338
1418
  write_stream = read_stream;}
1339
1419
 
1340
- read_stream.once('end', this._conn_.reset);
1341
1420
  return this.with_async_iter(read_stream,
1342
1421
  u8_pkt => write_stream.write(u8_pkt)) }
1343
1422