u8-mqtt 0.1.0 → 0.1.3

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 +64 -33
  2. package/cjs/index.cjs.map +1 -1
  3. package/cjs/v4.cjs +64 -33
  4. package/cjs/v4.cjs.map +1 -1
  5. package/cjs/v5.cjs +64 -33
  6. package/cjs/v5.cjs.map +1 -1
  7. package/code/base.jsy +63 -32
  8. package/esm/deno/index.js +64 -33
  9. package/esm/deno/index.js.map +1 -1
  10. package/esm/deno/index.mjs +64 -33
  11. package/esm/deno/index.mjs.map +1 -1
  12. package/esm/deno/v4.js +64 -33
  13. package/esm/deno/v4.js.map +1 -1
  14. package/esm/deno/v4.mjs +64 -33
  15. package/esm/deno/v4.mjs.map +1 -1
  16. package/esm/deno/v5.js +64 -33
  17. package/esm/deno/v5.js.map +1 -1
  18. package/esm/deno/v5.mjs +64 -33
  19. package/esm/deno/v5.mjs.map +1 -1
  20. package/esm/node/index.js +64 -33
  21. package/esm/node/index.js.map +1 -1
  22. package/esm/node/index.mjs +64 -33
  23. package/esm/node/index.mjs.map +1 -1
  24. package/esm/node/v4.js +64 -33
  25. package/esm/node/v4.js.map +1 -1
  26. package/esm/node/v4.mjs +64 -33
  27. package/esm/node/v4.mjs.map +1 -1
  28. package/esm/node/v5.js +64 -33
  29. package/esm/node/v5.js.map +1 -1
  30. package/esm/node/v5.mjs +64 -33
  31. package/esm/node/v5.mjs.map +1 -1
  32. package/esm/web/index.js +64 -33
  33. package/esm/web/index.js.map +1 -1
  34. package/esm/web/index.min.mjs +1 -1
  35. package/esm/web/index.mjs +64 -33
  36. package/esm/web/index.mjs.map +1 -1
  37. package/esm/web/v4.js +64 -33
  38. package/esm/web/v4.js.map +1 -1
  39. package/esm/web/v4.min.mjs +1 -1
  40. package/esm/web/v4.mjs +64 -33
  41. package/esm/web/v4.mjs.map +1 -1
  42. package/esm/web/v5.js +64 -33
  43. package/esm/web/v5.js.map +1 -1
  44. package/esm/web/v5.min.mjs +1 -1
  45. package/esm/web/v5.mjs +64 -33
  46. package/esm/web/v5.mjs.map +1 -1
  47. package/package.json +4 -4
package/code/base.jsy CHANGED
@@ -42,6 +42,10 @@ export class MQTTBaseClient ::
42
42
  subscribe(pkt, ex) ::
43
43
  pkt = _as_topics(pkt, ex)
44
44
  return this._send('subscribe', pkt, pkt)
45
+ _sub_chain(topic, ex) ::
46
+ let subs = this.subs || @ this.subs = new Map()
47
+ subs.set @ topic, this.subscribe @ [[ topic ]], ex
48
+ return this // fluent api -- return this and track side effects
45
49
 
46
50
  // alias: unsub
47
51
  unsubscribe(pkt, ex) ::
@@ -52,15 +56,30 @@ export class MQTTBaseClient ::
52
56
 
53
57
  // alias: sub_topic
54
58
  subscribe_topic(topic_route, ...args) ::
55
- let topic = this.topic_for(topic_route)
56
59
  this.router.add @ topic_route, true, args.pop() // handler
57
- this.subscribe @ [[ topic ]], args.pop() // ex
58
- return this
60
+ let topic = this.topic_for(topic_route)
61
+ return this._sub_chain @ topic, args.pop() // ex
59
62
 
60
63
  // alias: unsub_topic
61
64
  unsubscribe_topic(topic_route) ::
65
+ this.router.remove @ topic_route, true
66
+ let topic = this.topic_for(topic_route)
67
+ return this.unsubscribe @ [[ topic ]]
68
+
69
+ // alias: shared_sub
70
+ shared_subscribe(group, topic_route, ...args) ::
71
+ this.router.add @ topic_route, true, args.pop() // handler
62
72
  let topic = this.topic_for(topic_route)
73
+ if null != group ::
74
+ topic = `$share/${group}/${topic}`
75
+ return this._sub_chain @ topic, args.pop() // ex
76
+
77
+ // alias: shared_unsub
78
+ shared_unsubscribe(group, topic_route) ::
63
79
  this.router.remove @ topic_route, true
80
+ let topic = this.topic_for(topic_route)
81
+ if null != group ::
82
+ topic = `$share/${group}/${topic}`
64
83
  return this.unsubscribe @ [[ topic ]]
65
84
 
66
85
  topic_for(topic_route) ::
@@ -68,18 +87,18 @@ export class MQTTBaseClient ::
68
87
 
69
88
 
70
89
  // 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)
90
+ publish(pkt, pub_opt) :: return _pub(this, pkt, pub_opt)
91
+ post(topic, payload, pub_opt) :: return _pub.m(this, topic, payload, pub_opt)
92
+ send(topic, payload, pub_opt) :: return _pub.mq(this, topic, payload, pub_opt)
93
+ store(topic, payload, pub_opt) :: return _pub.mqr(this, topic, payload, pub_opt)
75
94
 
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)
95
+ json_post(topic, msg, pub_opt) :: return _pub.o(this, topic, msg, pub_opt)
96
+ json_send(topic, msg, pub_opt) :: return _pub.oq(this, topic, msg, pub_opt)
97
+ json_store(topic, msg, pub_opt) :: return _pub.oqr(this, topic, msg, pub_opt)
79
98
 
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)
99
+ obj_post(topic, msg, pub_opt) :: return _pub.o(this, topic, msg, pub_opt)
100
+ obj_send(topic, msg, pub_opt) :: return _pub.oq(this, topic, msg, pub_opt)
101
+ obj_store(topic, msg, pub_opt) :: return _pub.oqr(this, topic, msg, pub_opt)
83
102
 
84
103
 
85
104
 
@@ -139,6 +158,8 @@ export class MQTTBaseClient ::
139
158
  unsub: p.unsubscribe
140
159
  sub_topic: p.subscribe_topic
141
160
  unsub_topic: p.unsubscribe_topic
161
+ shared_sub: p.shared_subscribe
162
+ shared_unsub: p.shared_unsubscribe
142
163
 
143
164
  /*
144
165
  p.on_mqtt_type = {
@@ -165,39 +186,49 @@ function _as_topics(pkt, ex) ::
165
186
  return ex ? {...pkt, ...ex} : pkt
166
187
 
167
188
 
168
- async function _pub(self, pkt, fn_encode) ::
189
+ async function _pub(self, pkt, pub_opt) ::
169
190
  if undefined === pkt.payload ::
191
+ if 'function' === typeof pub_opt ::
192
+ pub_opt = {fn_encode: pub_opt}
193
+
170
194
  let {msg} = pkt
171
195
  switch typeof msg ::
172
196
  case 'function':
173
- fn_encode = msg
174
- msg = undefined
175
-
197
+ pub_opt = {...pub_opt, fn_encode: msg}
198
+ // flow into 'undefined' case
176
199
  case 'undefined':
177
- let arg = pkt.arg || 'payload'
178
- return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
200
+ // return a single-value closure to publish packets
201
+ return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
179
202
 
180
203
  default:
204
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
205
+ let {fn_encode} = pub_opt || {}
181
206
  pkt.payload = fn_encode
182
207
  ? await fn_encode(msg)
183
208
  : JSON.stringify(msg)
184
209
 
210
+ if pub_opt ::
211
+ if pub_opt.props ::
212
+ pkt.props = pub_opt.props
213
+ if pub_opt.xform ::
214
+ pkt = pub_opt.xform(pkt) || pkt
215
+
185
216
  return self._send @ 'publish', pkt,
186
217
  pkt.qos ? pkt : void 0 // key
187
218
 
188
219
  ::
189
220
  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)
221
+ m: (self, topic, payload, pub_opt) =>
222
+ _pub(self, {topic, payload, qos:0}, pub_opt)
223
+ mq: (self, topic, payload, pub_opt) =>
224
+ _pub(self, {topic, payload, qos:1}, pub_opt)
225
+ mqr: (self, topic, payload, pub_opt) =>
226
+ _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
227
+
228
+ o: (self, topic, msg, pub_opt) =>
229
+ _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
230
+ oq: (self, topic, msg, pub_opt) =>
231
+ _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
232
+ oqr: (self, topic, msg, pub_opt) =>
233
+ _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)
203
234
 
package/esm/deno/index.js CHANGED
@@ -1180,6 +1180,10 @@ class MQTTBaseClient {
1180
1180
  subscribe(pkt, ex) {
1181
1181
  pkt = _as_topics(pkt, ex);
1182
1182
  return this._send('subscribe', pkt, pkt)}
1183
+ _sub_chain(topic, ex) {
1184
+ let subs = this.subs ||(this.subs = new Map());
1185
+ subs.set(topic, this.subscribe([[ topic ]], ex));
1186
+ return this }// fluent api -- return this and track side effects
1183
1187
 
1184
1188
  // alias: unsub
1185
1189
  unsubscribe(pkt, ex) {
@@ -1190,15 +1194,30 @@ class MQTTBaseClient {
1190
1194
 
1191
1195
  // alias: sub_topic
1192
1196
  subscribe_topic(topic_route, ...args) {
1193
- let topic = this.topic_for(topic_route);
1194
1197
  this.router.add(topic_route, true, args.pop() );// handler
1195
- this.subscribe([[ topic ]], args.pop() );// ex
1196
- return this}
1198
+ let topic = this.topic_for(topic_route);
1199
+ return this._sub_chain(topic, args.pop() ) }// ex
1197
1200
 
1198
1201
  // alias: unsub_topic
1199
1202
  unsubscribe_topic(topic_route) {
1203
+ this.router.remove(topic_route, true);
1200
1204
  let topic = this.topic_for(topic_route);
1205
+ return this.unsubscribe([[ topic ]]) }
1206
+
1207
+ // alias: shared_sub
1208
+ shared_subscribe(group, topic_route, ...args) {
1209
+ this.router.add(topic_route, true, args.pop() );// handler
1210
+ let topic = this.topic_for(topic_route);
1211
+ if (null != group) {
1212
+ topic = `$share/${group}/${topic}`;}
1213
+ return this._sub_chain(topic, args.pop() ) }// ex
1214
+
1215
+ // alias: shared_unsub
1216
+ shared_unsubscribe(group, topic_route) {
1201
1217
  this.router.remove(topic_route, true);
1218
+ let topic = this.topic_for(topic_route);
1219
+ if (null != group) {
1220
+ topic = `$share/${group}/${topic}`;}
1202
1221
  return this.unsubscribe([[ topic ]]) }
1203
1222
 
1204
1223
  topic_for(topic_route) {
@@ -1206,18 +1225,18 @@ class MQTTBaseClient {
1206
1225
 
1207
1226
 
1208
1227
  // 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)}
1228
+ publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
1229
+ post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
1230
+ send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
1231
+ store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
1213
1232
 
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)}
1233
+ json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1234
+ json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1235
+ json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1217
1236
 
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)}
1237
+ obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1238
+ obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1239
+ obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1221
1240
 
1222
1241
 
1223
1242
 
@@ -1276,7 +1295,9 @@ class MQTTBaseClient {
1276
1295
  , sub: p.subscribe
1277
1296
  , unsub: p.unsubscribe
1278
1297
  , sub_topic: p.subscribe_topic
1279
- , unsub_topic: p.unsubscribe_topic} );
1298
+ , unsub_topic: p.unsubscribe_topic
1299
+ , shared_sub: p.shared_subscribe
1300
+ , shared_unsub: p.shared_unsubscribe} );
1280
1301
 
1281
1302
  /*
1282
1303
  p.on_mqtt_type = {
@@ -1303,41 +1324,51 @@ function _as_topics(pkt, ex) {
1303
1324
  return ex ? {...pkt, ...ex} : pkt}
1304
1325
 
1305
1326
 
1306
- async function _pub(self, pkt, fn_encode) {
1327
+ async function _pub(self, pkt, pub_opt) {
1307
1328
  if (undefined === pkt.payload) {
1329
+ if ('function' === typeof pub_opt) {
1330
+ pub_opt = {fn_encode: pub_opt};}
1331
+
1308
1332
  let {msg} = pkt;
1309
1333
  switch (typeof msg) {
1310
1334
  case 'function':
1311
- fn_encode = msg;
1312
- msg = undefined;
1313
-
1335
+ pub_opt = {...pub_opt, fn_encode: msg};
1336
+ // flow into 'undefined' case
1314
1337
  case 'undefined':
1315
- let arg = pkt.arg || 'payload';
1316
- return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
1338
+ // return a single-value closure to publish packets
1339
+ return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
1317
1340
 
1318
1341
  default:
1342
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1343
+ let {fn_encode} = pub_opt || {};
1319
1344
  pkt.payload = fn_encode
1320
1345
  ? await fn_encode(msg)
1321
1346
  : JSON.stringify(msg);} }
1322
1347
 
1348
+ if (pub_opt) {
1349
+ if (pub_opt.props) {
1350
+ pkt.props = pub_opt.props;}
1351
+ if (pub_opt.xform) {
1352
+ pkt = pub_opt.xform(pkt) || pkt;} }
1353
+
1323
1354
  return self._send('publish', pkt,
1324
1355
  pkt.qos ? pkt : void 0 ) }// key
1325
1356
 
1326
1357
  {
1327
1358
  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)} ); }
1359
+ m: (self, topic, payload, pub_opt) =>
1360
+ _pub(self, {topic, payload, qos:0}, pub_opt)
1361
+ , mq: (self, topic, payload, pub_opt) =>
1362
+ _pub(self, {topic, payload, qos:1}, pub_opt)
1363
+ , mqr: (self, topic, payload, pub_opt) =>
1364
+ _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1365
+
1366
+ , o: (self, topic, msg, pub_opt) =>
1367
+ _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1368
+ , oq: (self, topic, msg, pub_opt) =>
1369
+ _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1370
+ , oqr: (self, topic, msg, pub_opt) =>
1371
+ _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
1341
1372
 
1342
1373
  class MQTTCoreClient extends MQTTBaseClient {
1343
1374
  static _with_session(mqtt_session) {