u8-mqtt 0.6.4 → 0.6.6

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.
Files changed (96) hide show
  1. package/cjs/basic-v4.cjs +1 -1
  2. package/cjs/basic-v5.cjs +1 -1
  3. package/cjs/full-v4.cjs +46 -25
  4. package/cjs/full-v4.cjs.map +1 -1
  5. package/cjs/full-v5.cjs +46 -25
  6. package/cjs/full-v5.cjs.map +1 -1
  7. package/cjs/index.cjs +46 -25
  8. package/cjs/index.cjs.map +1 -1
  9. package/cjs/v4.cjs +46 -25
  10. package/cjs/v4.cjs.map +1 -1
  11. package/cjs/v5.cjs +46 -25
  12. package/cjs/v5.cjs.map +1 -1
  13. package/code/router_path.jsy +34 -21
  14. package/code/with_topic_router.jsy +3 -0
  15. package/esm/basic-v4.js +1 -1
  16. package/esm/basic-v5.js +1 -1
  17. package/esm/deno/basic-v4.js +1 -1
  18. package/esm/deno/basic-v5.js +1 -1
  19. package/esm/deno/full-v4.js +46 -25
  20. package/esm/deno/full-v4.js.map +1 -1
  21. package/esm/deno/full-v5.js +46 -25
  22. package/esm/deno/full-v5.js.map +1 -1
  23. package/esm/deno/index.js +46 -25
  24. package/esm/deno/index.js.map +1 -1
  25. package/esm/deno/v4.js +46 -25
  26. package/esm/deno/v4.js.map +1 -1
  27. package/esm/deno/v5.js +46 -25
  28. package/esm/deno/v5.js.map +1 -1
  29. package/esm/full-v4.js +46 -25
  30. package/esm/full-v4.js.map +1 -1
  31. package/esm/full-v5.js +46 -25
  32. package/esm/full-v5.js.map +1 -1
  33. package/esm/index.js +46 -25
  34. package/esm/index.js.map +1 -1
  35. package/esm/node/basic-v4.js +1 -1
  36. package/esm/node/basic-v4.mjs +1 -1
  37. package/esm/node/basic-v5.js +1 -1
  38. package/esm/node/basic-v5.mjs +1 -1
  39. package/esm/node/full-v4.js +46 -25
  40. package/esm/node/full-v4.js.map +1 -1
  41. package/esm/node/full-v4.mjs +46 -25
  42. package/esm/node/full-v4.mjs.map +1 -1
  43. package/esm/node/full-v5.js +46 -25
  44. package/esm/node/full-v5.js.map +1 -1
  45. package/esm/node/full-v5.mjs +46 -25
  46. package/esm/node/full-v5.mjs.map +1 -1
  47. package/esm/node/index.js +46 -25
  48. package/esm/node/index.js.map +1 -1
  49. package/esm/node/index.mjs +46 -25
  50. package/esm/node/index.mjs.map +1 -1
  51. package/esm/node/v4.js +46 -25
  52. package/esm/node/v4.js.map +1 -1
  53. package/esm/node/v4.mjs +46 -25
  54. package/esm/node/v4.mjs.map +1 -1
  55. package/esm/node/v5.js +46 -25
  56. package/esm/node/v5.js.map +1 -1
  57. package/esm/node/v5.mjs +46 -25
  58. package/esm/node/v5.mjs.map +1 -1
  59. package/esm/v4.js +46 -25
  60. package/esm/v4.js.map +1 -1
  61. package/esm/v5.js +46 -25
  62. package/esm/v5.js.map +1 -1
  63. package/esm/web/basic-v4.js +1 -1
  64. package/esm/web/basic-v4.min.js +1 -1
  65. package/esm/web/basic-v4.min.js.br +0 -0
  66. package/esm/web/basic-v4.min.js.gz +0 -0
  67. package/esm/web/basic-v5.js +1 -1
  68. package/esm/web/basic-v5.min.js +1 -1
  69. package/esm/web/basic-v5.min.js.br +0 -0
  70. package/esm/web/basic-v5.min.js.gz +0 -0
  71. package/esm/web/full-v4.js +46 -25
  72. package/esm/web/full-v4.js.map +1 -1
  73. package/esm/web/full-v4.min.js +1 -1
  74. package/esm/web/full-v4.min.js.br +0 -0
  75. package/esm/web/full-v4.min.js.gz +0 -0
  76. package/esm/web/full-v5.js +46 -25
  77. package/esm/web/full-v5.js.map +1 -1
  78. package/esm/web/full-v5.min.js +1 -1
  79. package/esm/web/full-v5.min.js.br +0 -0
  80. package/esm/web/full-v5.min.js.gz +0 -0
  81. package/esm/web/index.js +46 -25
  82. package/esm/web/index.js.map +1 -1
  83. package/esm/web/index.min.js +1 -1
  84. package/esm/web/index.min.js.br +0 -0
  85. package/esm/web/index.min.js.gz +0 -0
  86. package/esm/web/v4.js +46 -25
  87. package/esm/web/v4.js.map +1 -1
  88. package/esm/web/v4.min.js +1 -1
  89. package/esm/web/v4.min.js.br +0 -0
  90. package/esm/web/v4.min.js.gz +0 -0
  91. package/esm/web/v5.js +46 -25
  92. package/esm/web/v5.js.map +1 -1
  93. package/esm/web/v5.min.js +1 -1
  94. package/esm/web/v5.min.js.br +0 -0
  95. package/esm/web/v5.min.js.gz +0 -0
  96. package/package.json +2 -2
@@ -60,6 +60,17 @@ function mqtt_topic_path_router() ::
60
60
  query = @{} topic: query, tgt: args.pop()
61
61
  return _route_remove(lst, query)
62
62
 
63
+ count(topic_route) ::
64
+ if ! topic_route ::
65
+ return pri_lsts.reduce((r,l) => r+l.length, 0)
66
+
67
+ let count = 0
68
+ for let lst of pri_lsts ::
69
+ for let route of lst ::
70
+ if topic_route === route.topic ::
71
+ count++
72
+ return count
73
+
63
74
  clear(priority) ::
64
75
  if null == priority ::
65
76
  pri_lsts = [[],[]] // null clears both lists
@@ -83,36 +94,38 @@ function mqtt_topic_path_router() ::
83
94
  await ctx.mqtt.puback(pkt)
84
95
 
85
96
 
97
+ const _regexparam = @{}
98
+ get wild() :: return this['*']
99
+ set wild(v) :: return this['*']=wild
100
+
86
101
  function * _routes_iter(all_route_lists, topic) ::
87
102
  topic = topic.replace(/^[\/]*/, '/') // ensure '/' prefix for regexparam library
88
- for let route_list of all_route_lists ::
89
- for let route of route_list ::
90
- let match = route.pattern.exec(topic)
91
- if match ::
92
- let params = route.keys
93
- ? route.keys.reduce @
94
- (o, k, i) => (o[k] = match[1+i], o)
95
- {}
96
- : match.groups ?? match
103
+ for let route of all_route_lists.flat() ::
104
+ let match = route?.pattern.exec(topic)
105
+ if match ::
106
+ let keys = route.keys
107
+ if (keys) ::
108
+ let k, i=0, params={__proto__:_regexparam}
109
+ for (k of keys) ::
110
+ params[k] = match[++i]
97
111
  yield [route.tgt, params, route]
112
+ else ::
113
+ yield [route.tgt, match.groups ?? match, route]
98
114
 
99
115
 
100
116
  function _route_remove(all_route_lists, query) ::
101
- let ans = false
117
+ let n_removed = 0
102
118
  for let lst of all_route_lists ::
103
119
  let idx_tip = 0
104
- for (let route of lst) ::
105
- // skip matching routes to remove from compacted list
106
- if (route === query) continue
107
- if (route.topic === query.topic) ::
108
- if (null == query.tgt) continue
109
- if (route.tgt === query.tgt) continue
120
+ for let route of lst ::
121
+ if route.topic === query.topic ::
122
+ if null == query.tgt || route.tgt === query.tgt ::
123
+ // skip matching routes to remove from compacted list
124
+ n_removed++
125
+ continue
110
126
 
111
127
  lst[idx_tip++] = route
112
128
 
113
129
  // truncate remaining list
114
- if (lst.splice(idx_tip).length)
115
- ans = true
116
-
117
- return ans
118
-
130
+ lst.length = idx_tip
131
+ return n_removed
@@ -3,6 +3,7 @@ class AbstractTopicRouter ::
3
3
  async invoke(pkt, ctx) ::
4
4
  add(topic_route, ...args) ::
5
5
  remove(topic_route, priority) ::
6
+ count(topic_route) ::
6
7
  clear(priority) ::
7
8
  find(topic) :: // optional
8
9
  mqtt_topic(topic_route)
@@ -42,6 +43,8 @@ export const with_topic_router = mqtt_topic_router =>
42
43
 
43
44
  let fn = args.at(-1)?.call ? args.pop() : null
44
45
  router.remove @ topic_route, true, fn
46
+ if 0 !== router.count @ topic_route ::
47
+ return null
45
48
 
46
49
  let topic = router.mqtt_topic(topic_route)
47
50
  return this.unsubscribe @ topic, ...args // topic_prefix
package/esm/basic-v4.js CHANGED
@@ -1102,7 +1102,7 @@ class MQTTCore extends MQTTBase {
1102
1102
 
1103
1103
  return this} }
1104
1104
 
1105
- const version = '0.6.4';
1105
+ const version = '0.6.6';
1106
1106
 
1107
1107
  const MQTTClient_v4 = /* #__PURE__ */
1108
1108
  MQTTCore.mqtt_ctx(4, mqtt_opts_v4);
package/esm/basic-v5.js CHANGED
@@ -1296,7 +1296,7 @@ class MQTTCore extends MQTTBase {
1296
1296
 
1297
1297
  return this} }
1298
1298
 
1299
- const version = '0.6.4';
1299
+ const version = '0.6.6';
1300
1300
 
1301
1301
  const MQTTClient_v4 = /* #__PURE__ */
1302
1302
  MQTTCore.mqtt_ctx(4, mqtt_opts_v5);
@@ -1102,7 +1102,7 @@ class MQTTCore extends MQTTBase {
1102
1102
 
1103
1103
  return this} }
1104
1104
 
1105
- const version = '0.6.4-deno';
1105
+ const version = '0.6.6-deno';
1106
1106
 
1107
1107
  const MQTTClient_v4 = /* #__PURE__ */
1108
1108
  MQTTCore.mqtt_ctx(4, mqtt_opts_v4);
@@ -1296,7 +1296,7 @@ class MQTTCore extends MQTTBase {
1296
1296
 
1297
1297
  return this} }
1298
1298
 
1299
- const version = '0.6.4-deno';
1299
+ const version = '0.6.6-deno';
1300
1300
 
1301
1301
  const MQTTClient_v4 = /* #__PURE__ */
1302
1302
  MQTTCore.mqtt_ctx(4, mqtt_opts_v5);
@@ -646,16 +646,20 @@ const mqtt_opts_v4 =
646
646
  encode_fns: mqtt_encode_v4,
647
647
  mqtt_writer: mqtt_writer_v4, };
648
648
 
649
- function parse(str, loose) {
650
- if (str instanceof RegExp) return { keys:false, pattern:str };
651
- var c, o, tmp, ext, keys=[], pattern='', arr = str.split('/');
649
+ /**
650
+ * @param {string|RegExp} input The route pattern
651
+ * @param {boolean} [loose] Allow open-ended matching. Ignored with `RegExp` input.
652
+ */
653
+ function parse(input, loose) {
654
+ if (input instanceof RegExp) return { keys:false, pattern:input };
655
+ var c, o, tmp, ext, keys=[], pattern='', arr = input.split('/');
652
656
  arr[0] || arr.shift();
653
657
 
654
658
  while (tmp = arr.shift()) {
655
659
  c = tmp[0];
656
660
  if (c === '*') {
657
- keys.push('wild');
658
- pattern += '/(.*)';
661
+ keys.push(c);
662
+ pattern += tmp[1] === '?' ? '(?:/(.*))?' : '/(.*)';
659
663
  } else if (c === ':') {
660
664
  o = tmp.indexOf('?', 1);
661
665
  ext = tmp.indexOf('.', 1);
@@ -695,6 +699,7 @@ class AbstractTopicRouter ::
695
699
  async invoke(pkt, ctx) ::
696
700
  add(topic_route, ...args) ::
697
701
  remove(topic_route, priority) ::
702
+ count(topic_route) ::
698
703
  clear(priority) ::
699
704
  find(topic) :: // optional
700
705
  mqtt_topic(topic_route)
@@ -734,6 +739,8 @@ const with_topic_router = mqtt_topic_router =>
734
739
 
735
740
  let fn = args.at(-1)?.call ? args.pop() : null;
736
741
  router.remove(topic_route, true, fn);
742
+ if (0 !== router.count(topic_route) ) {
743
+ return null}
737
744
 
738
745
  let topic = router.mqtt_topic(topic_route);
739
746
  return this.unsubscribe(topic, ...args ) }// topic_prefix
@@ -802,6 +809,17 @@ function mqtt_topic_path_router() {
802
809
  query ={topic: query, tgt: args.pop()}; }
803
810
  return _route_remove(lst, query)}
804
811
 
812
+ , count(topic_route) {
813
+ if (! topic_route) {
814
+ return pri_lsts.reduce((r,l) => r+l.length, 0)}
815
+
816
+ let count = 0;
817
+ for (let lst of pri_lsts) {
818
+ for (let route of lst) {
819
+ if (topic_route === route.topic) {
820
+ count++;} } }
821
+ return count}
822
+
805
823
  , clear(priority) {
806
824
  if (null == priority) {
807
825
  pri_lsts = [[],[]]; }// null clears both lists
@@ -825,38 +843,41 @@ function mqtt_topic_path_router() {
825
843
  await ctx.mqtt.puback(pkt);} } } }
826
844
 
827
845
 
846
+ const _regexparam ={
847
+ get wild() {return this['*']}
848
+ , set wild(v) {return this['*']=wild} };
849
+
828
850
  function * _routes_iter(all_route_lists, topic) {
829
851
  topic = topic.replace(/^[\/]*/, '/'); // ensure '/' prefix for regexparam library
830
- for (let route_list of all_route_lists) {
831
- for (let route of route_list) {
832
- let match = route.pattern.exec(topic);
833
- if (match) {
834
- let params = route.keys
835
- ? route.keys.reduce(
836
- (o, k, i) => (o[k] = match[1+i], o)
837
- , {})
838
- : match.groups ?? match;
839
- yield [route.tgt, params, route];} } } }
852
+ for (let route of all_route_lists.flat()) {
853
+ let match = route?.pattern.exec(topic);
854
+ if (match) {
855
+ let keys = route.keys;
856
+ if (keys) {
857
+ let k, i=0, params={__proto__:_regexparam};
858
+ for (k of keys) {
859
+ params[k] = match[++i];}
860
+ yield [route.tgt, params, route];}
861
+ else {
862
+ yield [route.tgt, match.groups ?? match, route];} } } }
840
863
 
841
864
 
842
865
  function _route_remove(all_route_lists, query) {
843
- let ans = false;
866
+ let n_removed = 0;
844
867
  for (let lst of all_route_lists) {
845
868
  let idx_tip = 0;
846
869
  for (let route of lst) {
847
- // skip matching routes to remove from compacted list
848
- if (route === query) continue
849
870
  if (route.topic === query.topic) {
850
- if (null == query.tgt) continue
851
- if (route.tgt === query.tgt) continue}
871
+ if (null == query.tgt || route.tgt === query.tgt) {
872
+ // skip matching routes to remove from compacted list
873
+ n_removed++;
874
+ continue} }
852
875
 
853
876
  lst[idx_tip++] = route;}
854
877
 
855
878
  // truncate remaining list
856
- if (lst.splice(idx_tip).length)
857
- ans = true;}
858
-
859
- return ans}
879
+ lst.length = idx_tip;}
880
+ return n_removed}
860
881
 
861
882
  function decode_varint(u8, i0=0, invalid) {
862
883
  let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
@@ -1471,7 +1492,7 @@ class MQTTCore extends MQTTBase {
1471
1492
 
1472
1493
  return this} }
1473
1494
 
1474
- const version = '0.6.4-deno';
1495
+ const version = '0.6.6-deno';
1475
1496
 
1476
1497
  const MQTTClient_v4 = /* #__PURE__ */
1477
1498
  with_topic_path_router(