u8-mqtt 0.0.27 → 0.1.1

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 (50) hide show
  1. package/cjs/index.cjs +64 -34
  2. package/cjs/index.cjs.map +1 -1
  3. package/cjs/v4.cjs +64 -34
  4. package/cjs/v4.cjs.map +1 -1
  5. package/cjs/v5.cjs +64 -34
  6. package/cjs/v5.cjs.map +1 -1
  7. package/code/_router.jsy +4 -1
  8. package/code/base.jsy +56 -30
  9. package/code/core.jsy +3 -3
  10. package/code/session.mjs +2 -1
  11. package/esm/deno/index.js +1497 -0
  12. package/esm/deno/index.js.map +1 -0
  13. package/esm/deno/index.mjs +65 -35
  14. package/esm/deno/index.mjs.map +1 -1
  15. package/esm/deno/v4.js +1491 -0
  16. package/esm/deno/v4.js.map +1 -0
  17. package/esm/deno/v4.mjs +65 -35
  18. package/esm/deno/v4.mjs.map +1 -1
  19. package/esm/deno/v5.js +1491 -0
  20. package/esm/deno/v5.js.map +1 -0
  21. package/esm/deno/v5.mjs +65 -35
  22. package/esm/deno/v5.mjs.map +1 -1
  23. package/esm/node/index.js +1499 -0
  24. package/esm/node/index.js.map +1 -0
  25. package/esm/node/index.mjs +64 -34
  26. package/esm/node/index.mjs.map +1 -1
  27. package/esm/node/v4.js +1493 -0
  28. package/esm/node/v4.js.map +1 -0
  29. package/esm/node/v4.mjs +64 -34
  30. package/esm/node/v4.mjs.map +1 -1
  31. package/esm/node/v5.js +1493 -0
  32. package/esm/node/v5.js.map +1 -0
  33. package/esm/node/v5.mjs +64 -34
  34. package/esm/node/v5.mjs.map +1 -1
  35. package/esm/web/index.js +1497 -0
  36. package/esm/web/index.js.map +1 -0
  37. package/esm/web/index.min.mjs +1 -1
  38. package/esm/web/index.mjs +63 -33
  39. package/esm/web/index.mjs.map +1 -1
  40. package/esm/web/v4.js +1491 -0
  41. package/esm/web/v4.js.map +1 -0
  42. package/esm/web/v4.min.mjs +1 -1
  43. package/esm/web/v4.mjs +63 -33
  44. package/esm/web/v4.mjs.map +1 -1
  45. package/esm/web/v5.js +1491 -0
  46. package/esm/web/v5.js.map +1 -0
  47. package/esm/web/v5.min.mjs +1 -1
  48. package/esm/web/v5.mjs +63 -33
  49. package/esm/web/v5.mjs.map +1 -1
  50. package/package.json +10 -10
package/code/base.jsy CHANGED
@@ -63,23 +63,36 @@ export class MQTTBaseClient ::
63
63
  this.router.remove @ topic_route, true
64
64
  return this.unsubscribe @ [[ topic ]]
65
65
 
66
+ // alias: shared_sub
67
+ shared_subscribe(group, topic_route, ...args) ::
68
+ let topic = `$share/${group||''}/${this.topic_for(topic_route)}`
69
+ this.router.add @ topic_route, true, args.pop() // handler
70
+ this.subscribe @ [[ topic ]], args.pop() // ex
71
+ return this
72
+
73
+ // alias: shared_unsub
74
+ shared_unsubscribe(group, topic_route) ::
75
+ let topic = `$share/${group||''}/${this.topic_for(topic_route)}`
76
+ this.router.remove @ topic_route, true
77
+ return this.unsubscribe @ [[ topic ]]
78
+
66
79
  topic_for(topic_route) ::
67
80
  return topic_route.replace(/[:*].*$/, '#')
68
81
 
69
82
 
70
83
  // alias: pub
71
- publish(pkt, fn_encode) :: return _pub(this, pkt, fn_encode)
72
- post(topic, payload) :: return _pub.m(this, topic, payload)
73
- send(topic, payload) :: return _pub.mq(this, topic, payload)
74
- store(topic, payload) :: return _pub.mqr(this, topic, payload)
84
+ publish(pkt, pub_opt) :: return _pub(this, pkt, pub_opt)
85
+ post(topic, payload, pub_opt) :: return _pub.m(this, topic, payload, pub_opt)
86
+ send(topic, payload, pub_opt) :: return _pub.mq(this, topic, payload, pub_opt)
87
+ store(topic, payload, pub_opt) :: return _pub.mqr(this, topic, payload, pub_opt)
75
88
 
76
- json_post(topic, msg) :: return _pub.o(this, topic, msg)
77
- json_send(topic, msg) :: return _pub.oq(this, topic, msg)
78
- json_store(topic, msg) :: return _pub.oqr(this, topic, msg)
89
+ json_post(topic, msg, pub_opt) :: return _pub.o(this, topic, msg, pub_opt)
90
+ json_send(topic, msg, pub_opt) :: return _pub.oq(this, topic, msg, pub_opt)
91
+ json_store(topic, msg, pub_opt) :: return _pub.oqr(this, topic, msg, pub_opt)
79
92
 
80
- obj_post(topic, msg, fn_encode) :: return _pub.o(this, topic, msg, fn_encode)
81
- obj_send(topic, msg, fn_encode) :: return _pub.oq(this, topic, msg, fn_encode)
82
- obj_store(topic, msg, fn_encode) :: return _pub.oqr(this, topic, msg, fn_encode)
93
+ obj_post(topic, msg, pub_opt) :: return _pub.o(this, topic, msg, pub_opt)
94
+ obj_send(topic, msg, pub_opt) :: return _pub.oq(this, topic, msg, pub_opt)
95
+ obj_store(topic, msg, pub_opt) :: return _pub.oqr(this, topic, msg, pub_opt)
83
96
 
84
97
 
85
98
 
@@ -139,6 +152,8 @@ export class MQTTBaseClient ::
139
152
  unsub: p.unsubscribe
140
153
  sub_topic: p.subscribe_topic
141
154
  unsub_topic: p.unsubscribe_topic
155
+ shared_sub: p.shared_subscribe
156
+ shared_unsub: p.shared_unsubscribe
142
157
 
143
158
  /*
144
159
  p.on_mqtt_type = {
@@ -165,39 +180,50 @@ function _as_topics(pkt, ex) ::
165
180
  return ex ? {...pkt, ...ex} : pkt
166
181
 
167
182
 
168
- function _pub(self, pkt, fn_encode) ::
183
+ async function _pub(self, pkt, pub_opt) ::
169
184
  if undefined === pkt.payload ::
185
+ if 'function' === typeof pub_opt ::
186
+ pub_opt = {fn_encode: pub_opt}
187
+
170
188
  let {msg} = pkt
171
189
  switch typeof msg ::
172
190
  case 'function':
173
- fn_encode = msg
174
- msg = undefined
175
-
191
+ pub_opt = {...pub_opt, fn_encode: msg}
192
+ // flow into 'undefined' case
176
193
  case 'undefined':
177
- let arg = pkt.arg || 'payload'
178
- return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
194
+ // return a single-value closure to publish packets
195
+ return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
179
196
 
180
197
  default:
198
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
199
+ let fn_encode = pub_opt || {}
181
200
  pkt.payload = fn_encode
182
- ? fn_encode(msg)
201
+ ? await fn_encode(msg)
183
202
  : JSON.stringify(msg)
184
203
 
204
+ if pub_opt ::
205
+ let {props, xform} = pub_opt
206
+ if props ::
207
+ pkt.props = props
208
+ if xform ::
209
+ pkt = xform(pkt) || pkt
210
+
185
211
  return self._send @ 'publish', pkt,
186
212
  pkt.qos ? pkt : void 0 // key
187
213
 
188
214
  ::
189
215
  Object.assign @ _pub, @{}
190
- m: (self, topic, payload) =>
191
- _pub(self, {topic, payload, qos:0})
192
- mq: (self, topic, payload) =>
193
- _pub(self, {topic, payload, qos:1})
194
- mqr: (self, topic, payload) =>
195
- _pub(self, {topic, payload, qos:1, retain: 1})
196
-
197
- o: (self, topic, msg, fn_encode) =>
198
- _pub(self, {topic, msg, arg: 'msg', qos:0}, fn_encode)
199
- oq: (self, topic, msg, fn_encode) =>
200
- _pub(self, {topic, msg, arg: 'msg', qos:1}, fn_encode)
201
- oqr: (self, topic, msg, fn_encode) =>
202
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, fn_encode)
216
+ m: (self, topic, payload, pub_opt) =>
217
+ _pub(self, {topic, payload, qos:0}, pub_opt)
218
+ mq: (self, topic, payload, pub_opt) =>
219
+ _pub(self, {topic, payload, qos:1}, pub_opt)
220
+ mqr: (self, topic, payload, pub_opt) =>
221
+ _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
222
+
223
+ o: (self, topic, msg, pub_opt) =>
224
+ _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
225
+ oq: (self, topic, msg, pub_opt) =>
226
+ _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
227
+ oqr: (self, topic, msg, pub_opt) =>
228
+ _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)
203
229
 
package/code/core.jsy CHANGED
@@ -50,10 +50,10 @@ export class MQTTCoreClient extends MQTTBaseClient ::
50
50
  #IF PLAT_DENO
51
51
  with_tcp(port, hostname) ::
52
52
  if !Number.isFinite(port) ::
53
- ({port, host: hostname} = port)
53
+ ({port, hostname} = new URL(port))
54
54
 
55
55
  Deno.connect @:
56
- port: port || 1883, hostname, transport: 'tcp'
56
+ port, hostname, transport: 'tcp'
57
57
  .then @\ conn =>
58
58
  this.with_async_iter @
59
59
  Deno.iter(conn),
@@ -65,7 +65,7 @@ export class MQTTCoreClient extends MQTTBaseClient ::
65
65
  #IF PLAT_NODEJS
66
66
  with_tcp(port, hostname) ::
67
67
  if !Number.isFinite(port) ::
68
- ({port, host: hostname} = port)
68
+ ({port, hostname} = new URL(port))
69
69
 
70
70
  let sock = tcp_connect(port, hostname)
71
71
  return this.with_stream(sock)
package/code/session.mjs CHANGED
@@ -30,7 +30,8 @@ export function mqtt_session_ctx(mqtt_level) {
30
30
 
31
31
  let std_pkt_api = {
32
32
  utf8(u8) { return as_utf8( u8 || this.payload ) },
33
- json(u8) { return JSON.parse( as_utf8( u8 || this.payload ) || null ) },
33
+ json(u8) { return JSON.parse( this.utf8(u8) || null ) },
34
+ text(u8) { return this.utf8(u8) },
34
35
  }
35
36
 
36
37
  mqtt_session_ctx.ctx = ctx =