u8-mqtt 0.1.0 → 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 (47) hide show
  1. package/cjs/index.cjs +56 -30
  2. package/cjs/index.cjs.map +1 -1
  3. package/cjs/v4.cjs +56 -30
  4. package/cjs/v4.cjs.map +1 -1
  5. package/cjs/v5.cjs +56 -30
  6. package/cjs/v5.cjs.map +1 -1
  7. package/code/base.jsy +55 -29
  8. package/esm/deno/index.js +56 -30
  9. package/esm/deno/index.js.map +1 -1
  10. package/esm/deno/index.mjs +56 -30
  11. package/esm/deno/index.mjs.map +1 -1
  12. package/esm/deno/v4.js +56 -30
  13. package/esm/deno/v4.js.map +1 -1
  14. package/esm/deno/v4.mjs +56 -30
  15. package/esm/deno/v4.mjs.map +1 -1
  16. package/esm/deno/v5.js +56 -30
  17. package/esm/deno/v5.js.map +1 -1
  18. package/esm/deno/v5.mjs +56 -30
  19. package/esm/deno/v5.mjs.map +1 -1
  20. package/esm/node/index.js +56 -30
  21. package/esm/node/index.js.map +1 -1
  22. package/esm/node/index.mjs +56 -30
  23. package/esm/node/index.mjs.map +1 -1
  24. package/esm/node/v4.js +56 -30
  25. package/esm/node/v4.js.map +1 -1
  26. package/esm/node/v4.mjs +56 -30
  27. package/esm/node/v4.mjs.map +1 -1
  28. package/esm/node/v5.js +56 -30
  29. package/esm/node/v5.js.map +1 -1
  30. package/esm/node/v5.mjs +56 -30
  31. package/esm/node/v5.mjs.map +1 -1
  32. package/esm/web/index.js +56 -30
  33. package/esm/web/index.js.map +1 -1
  34. package/esm/web/index.min.mjs +1 -1
  35. package/esm/web/index.mjs +56 -30
  36. package/esm/web/index.mjs.map +1 -1
  37. package/esm/web/v4.js +56 -30
  38. package/esm/web/v4.js.map +1 -1
  39. package/esm/web/v4.min.mjs +1 -1
  40. package/esm/web/v4.mjs +56 -30
  41. package/esm/web/v4.mjs.map +1 -1
  42. package/esm/web/v5.js +56 -30
  43. package/esm/web/v5.js.map +1 -1
  44. package/esm/web/v5.min.mjs +1 -1
  45. package/esm/web/v5.mjs +56 -30
  46. package/esm/web/v5.mjs.map +1 -1
  47. package/package.json +3 -3
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
- async 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
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/esm/deno/index.js CHANGED
@@ -1201,23 +1201,36 @@ class MQTTBaseClient {
1201
1201
  this.router.remove(topic_route, true);
1202
1202
  return this.unsubscribe([[ topic ]]) }
1203
1203
 
1204
+ // alias: shared_sub
1205
+ shared_subscribe(group, topic_route, ...args) {
1206
+ let topic = `$share/${group||''}/${this.topic_for(topic_route)}`;
1207
+ this.router.add(topic_route, true, args.pop() );// handler
1208
+ this.subscribe([[ topic ]], args.pop() );// ex
1209
+ return this}
1210
+
1211
+ // alias: shared_unsub
1212
+ shared_unsubscribe(group, topic_route) {
1213
+ let topic = `$share/${group||''}/${this.topic_for(topic_route)}`;
1214
+ this.router.remove(topic_route, true);
1215
+ return this.unsubscribe([[ topic ]]) }
1216
+
1204
1217
  topic_for(topic_route) {
1205
1218
  return topic_route.replace(/[:*].*$/, '#')}
1206
1219
 
1207
1220
 
1208
1221
  // alias: pub
1209
- publish(pkt, fn_encode) {return _pub(this, pkt, fn_encode)}
1210
- post(topic, payload) {return _pub.m(this, topic, payload)}
1211
- send(topic, payload) {return _pub.mq(this, topic, payload)}
1212
- store(topic, payload) {return _pub.mqr(this, topic, payload)}
1222
+ publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
1223
+ post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
1224
+ send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
1225
+ store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
1213
1226
 
1214
- json_post(topic, msg) {return _pub.o(this, topic, msg)}
1215
- json_send(topic, msg) {return _pub.oq(this, topic, msg)}
1216
- json_store(topic, msg) {return _pub.oqr(this, topic, msg)}
1227
+ json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1228
+ json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1229
+ json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1217
1230
 
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)}
1231
+ obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1232
+ obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1233
+ obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1221
1234
 
1222
1235
 
1223
1236
 
@@ -1276,7 +1289,9 @@ class MQTTBaseClient {
1276
1289
  , sub: p.subscribe
1277
1290
  , unsub: p.unsubscribe
1278
1291
  , sub_topic: p.subscribe_topic
1279
- , unsub_topic: p.unsubscribe_topic} );
1292
+ , unsub_topic: p.unsubscribe_topic
1293
+ , shared_sub: p.shared_subscribe
1294
+ , shared_unsub: p.shared_unsubscribe} );
1280
1295
 
1281
1296
  /*
1282
1297
  p.on_mqtt_type = {
@@ -1303,41 +1318,52 @@ function _as_topics(pkt, ex) {
1303
1318
  return ex ? {...pkt, ...ex} : pkt}
1304
1319
 
1305
1320
 
1306
- async function _pub(self, pkt, fn_encode) {
1321
+ async function _pub(self, pkt, pub_opt) {
1307
1322
  if (undefined === pkt.payload) {
1323
+ if ('function' === typeof pub_opt) {
1324
+ pub_opt = {fn_encode: pub_opt};}
1325
+
1308
1326
  let {msg} = pkt;
1309
1327
  switch (typeof msg) {
1310
1328
  case 'function':
1311
- fn_encode = msg;
1312
- msg = undefined;
1313
-
1329
+ pub_opt = {...pub_opt, fn_encode: msg};
1330
+ // flow into 'undefined' case
1314
1331
  case 'undefined':
1315
- let arg = pkt.arg || 'payload';
1316
- return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
1332
+ // return a single-value closure to publish packets
1333
+ return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
1317
1334
 
1318
1335
  default:
1336
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1337
+ let fn_encode = pub_opt || {};
1319
1338
  pkt.payload = fn_encode
1320
1339
  ? await fn_encode(msg)
1321
1340
  : JSON.stringify(msg);} }
1322
1341
 
1342
+ if (pub_opt) {
1343
+ let {props, xform} = pub_opt;
1344
+ if (props) {
1345
+ pkt.props = props;}
1346
+ if (xform) {
1347
+ pkt = xform(pkt) || pkt;} }
1348
+
1323
1349
  return self._send('publish', pkt,
1324
1350
  pkt.qos ? pkt : void 0 ) }// key
1325
1351
 
1326
1352
  {
1327
1353
  Object.assign(_pub,{
1328
- m: (self, topic, payload) =>
1329
- _pub(self, {topic, payload, qos:0})
1330
- , mq: (self, topic, payload) =>
1331
- _pub(self, {topic, payload, qos:1})
1332
- , mqr: (self, topic, payload) =>
1333
- _pub(self, {topic, payload, qos:1, retain: 1})
1334
-
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)} ); }
1354
+ m: (self, topic, payload, pub_opt) =>
1355
+ _pub(self, {topic, payload, qos:0}, pub_opt)
1356
+ , mq: (self, topic, payload, pub_opt) =>
1357
+ _pub(self, {topic, payload, qos:1}, pub_opt)
1358
+ , mqr: (self, topic, payload, pub_opt) =>
1359
+ _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1360
+
1361
+ , o: (self, topic, msg, pub_opt) =>
1362
+ _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1363
+ , oq: (self, topic, msg, pub_opt) =>
1364
+ _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1365
+ , oqr: (self, topic, msg, pub_opt) =>
1366
+ _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
1341
1367
 
1342
1368
  class MQTTCoreClient extends MQTTBaseClient {
1343
1369
  static _with_session(mqtt_session) {