u8-mqtt 0.4.1 → 0.5.0

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 (81) hide show
  1. package/cjs/basic-v4.cjs +103 -137
  2. package/cjs/basic-v4.cjs.map +1 -1
  3. package/cjs/basic-v5.cjs +102 -136
  4. package/cjs/basic-v5.cjs.map +1 -1
  5. package/cjs/index.cjs +144 -189
  6. package/cjs/index.cjs.map +1 -1
  7. package/cjs/v4.cjs +145 -190
  8. package/cjs/v4.cjs.map +1 -1
  9. package/cjs/v5.cjs +144 -189
  10. package/cjs/v5.cjs.map +1 -1
  11. package/code/_dispatch.jsy +16 -0
  12. package/code/base.jsy +68 -124
  13. package/code/core.jsy +15 -9
  14. package/code/router_path.jsy +24 -43
  15. package/code/with_topic_router.jsy +18 -10
  16. package/esm/basic-v4.js +1154 -0
  17. package/esm/basic-v4.js.map +1 -0
  18. package/esm/basic-v5.js +1416 -0
  19. package/esm/basic-v5.js.map +1 -0
  20. package/esm/deno/basic-v4.js +103 -137
  21. package/esm/deno/basic-v4.js.map +1 -1
  22. package/esm/deno/basic-v5.js +102 -136
  23. package/esm/deno/basic-v5.js.map +1 -1
  24. package/esm/deno/index.js +144 -189
  25. package/esm/deno/index.js.map +1 -1
  26. package/esm/deno/v4.js +145 -190
  27. package/esm/deno/v4.js.map +1 -1
  28. package/esm/deno/v5.js +144 -189
  29. package/esm/deno/v5.js.map +1 -1
  30. package/esm/index.js +1599 -0
  31. package/esm/index.js.map +1 -0
  32. package/esm/node/basic-v4.js +103 -137
  33. package/esm/node/basic-v4.js.map +1 -1
  34. package/esm/node/basic-v4.mjs +103 -137
  35. package/esm/node/basic-v4.mjs.map +1 -1
  36. package/esm/node/basic-v5.js +102 -136
  37. package/esm/node/basic-v5.js.map +1 -1
  38. package/esm/node/basic-v5.mjs +102 -136
  39. package/esm/node/basic-v5.mjs.map +1 -1
  40. package/esm/node/index.js +144 -189
  41. package/esm/node/index.js.map +1 -1
  42. package/esm/node/index.mjs +144 -189
  43. package/esm/node/index.mjs.map +1 -1
  44. package/esm/node/v4.js +145 -190
  45. package/esm/node/v4.js.map +1 -1
  46. package/esm/node/v4.mjs +145 -190
  47. package/esm/node/v4.mjs.map +1 -1
  48. package/esm/node/v5.js +144 -189
  49. package/esm/node/v5.js.map +1 -1
  50. package/esm/node/v5.mjs +144 -189
  51. package/esm/node/v5.mjs.map +1 -1
  52. package/esm/v4.js +1336 -0
  53. package/esm/v4.js.map +1 -0
  54. package/esm/v5.js +1599 -0
  55. package/esm/v5.js.map +1 -0
  56. package/esm/web/basic-v4.js +103 -137
  57. package/esm/web/basic-v4.js.map +1 -1
  58. package/esm/web/basic-v4.min.js +1 -1
  59. package/esm/web/basic-v4.min.js.br +0 -0
  60. package/esm/web/basic-v4.min.js.gz +0 -0
  61. package/esm/web/basic-v5.js +102 -136
  62. package/esm/web/basic-v5.js.map +1 -1
  63. package/esm/web/basic-v5.min.js +1 -1
  64. package/esm/web/basic-v5.min.js.br +0 -0
  65. package/esm/web/basic-v5.min.js.gz +0 -0
  66. package/esm/web/index.js +144 -189
  67. package/esm/web/index.js.map +1 -1
  68. package/esm/web/index.min.js +1 -1
  69. package/esm/web/index.min.js.br +0 -0
  70. package/esm/web/index.min.js.gz +0 -0
  71. package/esm/web/v4.js +145 -190
  72. package/esm/web/v4.js.map +1 -1
  73. package/esm/web/v4.min.js +1 -1
  74. package/esm/web/v4.min.js.br +0 -0
  75. package/esm/web/v4.min.js.gz +0 -0
  76. package/esm/web/v5.js +144 -189
  77. package/esm/web/v5.js.map +1 -1
  78. package/esm/web/v5.min.js +1 -1
  79. package/esm/web/v5.min.js.br +0 -0
  80. package/esm/web/v5.min.js.gz +0 -0
  81. package/package.json +7 -8
@@ -147,7 +147,7 @@ class mqtt_reader_v4 {
147
147
 
148
148
  }
149
149
 
150
- class mqtt_reader_v5$1 extends mqtt_reader_v4 {
150
+ let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
151
151
  props() {
152
152
  let {buf, step} = this;
153
153
  let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
@@ -182,7 +182,7 @@ class mqtt_reader_v5$1 extends mqtt_reader_v4 {
182
182
  : buf.subarray(vi, step.k|0)
183
183
  }
184
184
  */
185
- }
185
+ };
186
186
 
187
187
  function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
188
188
  mqtt_reader = class extends mqtt_reader {
@@ -1027,6 +1027,22 @@ const _mqtt_cmdid_dispatch ={
1027
1027
 
1028
1028
  await fn?.call(target, pkt, ctx);} })()) };
1029
1029
 
1030
+ /*
1031
+ on_mqtt_type = {
1032
+ mqtt_auth(pkt, ctx) ::
1033
+ mqtt_connect(pkt, ctx) ::
1034
+ mqtt_connack(pkt, ctx) ::
1035
+ mqtt_disconnect(pkt, ctx) ::
1036
+
1037
+ mqtt_publish(pkt, ctx)
1038
+ mqtt_subscribe(pkt, ctx) ::
1039
+ mqtt_unsubscribe(pkt, ctx) ::
1040
+
1041
+ mqtt_pingreq(pkt, ctx) ::
1042
+ mqtt_pingresp(pkt, ctx) ::
1043
+ }
1044
+ */
1045
+
1030
1046
  function _mqtt_dispatch(opt, target) {
1031
1047
  let _disp_ = _mqtt_cmdid_dispatch.create(target);
1032
1048
  let { cmdids } = _disp_;
@@ -1056,28 +1072,34 @@ class MQTTError extends Error {
1056
1072
 
1057
1073
  class MQTTBase {
1058
1074
  constructor(opt={}) {
1075
+ this.with(opt);
1059
1076
  this._conn_ = _mqtt_conn(this,
1060
1077
  this._init_dispatch(opt, this)); }
1061
1078
 
1079
+ with(fns_ns) {
1080
+ for (let [k,v] of Object.entries(fns_ns)) {
1081
+ if ('function' === typeof v) {this[k] = v;} }
1082
+ return this}
1083
+
1062
1084
  async conn_emit(evt, arg, err_arg) {
1063
1085
  this.log_conn?.(evt, arg, err_arg);
1064
1086
  try {
1065
- let fn_evt = this[await evt]; // microtask break
1087
+ let fn_evt = this[await evt]; // microtask break using `await evt`
1066
1088
  if (fn_evt) {
1067
1089
  await fn_evt.call(this, this, arg, err_arg);}
1068
- else if (err_arg) {
1069
- await this.on_error(err_arg, evt);} }
1090
+ else if (err_arg) {throw err_arg} }
1070
1091
  catch (err) {
1071
1092
  this.on_error(err, evt);} }
1072
1093
 
1073
- on_error(err, err_path) {
1074
- console.warn('[[u8-mqtt error: %s]]', err_path, err); }
1094
+ on_error(err, evt) {
1095
+ console.warn('[[u8-mqtt error: %s]]', evt, err); }
1075
1096
 
1076
1097
  // Handshaking Packets
1077
1098
 
1078
1099
  async connect(pkt={}) {
1079
- let cid = pkt.client_id || ['u8-mqtt--', ''];
1080
- if (Array.isArray(cid)) {
1100
+ let cid = pkt.client_id || this.client_id;
1101
+ if ('string' !== typeof cid) {
1102
+ // see init_client_id implementation in core.jsy
1081
1103
  pkt.client_id = cid = this.init_client_id(cid);}
1082
1104
  this.client_id = cid;
1083
1105
 
@@ -1103,12 +1125,13 @@ class MQTTBase {
1103
1125
  return this._send('auth', pkt, 'auth')}
1104
1126
 
1105
1127
  ping() {return this._send('pingreq', null, 'pingresp')}
1106
-
1128
+ puback({pkt_id}) {return this._send('puback', {pkt_id})}
1107
1129
 
1108
1130
  // alias: sub
1109
1131
  subscribe(pkt, ex, topic_prefix) {
1110
1132
  pkt = _as_topics(pkt, ex, topic_prefix);
1111
- return this._send('subscribe', pkt, pkt)}
1133
+ let suback = this._send('subscribe', pkt, pkt);
1134
+ return this.on_sub?.(suback, pkt) ?? suback}
1112
1135
 
1113
1136
  // alias: unsub
1114
1137
  unsubscribe(pkt, ex, topic_prefix) {
@@ -1116,48 +1139,52 @@ class MQTTBase {
1116
1139
  return this._send('unsubscribe', pkt, pkt)}
1117
1140
 
1118
1141
 
1119
- // alias: pub
1120
- publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
1121
- post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
1122
- send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
1123
- store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
1124
-
1125
- json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1126
- json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1127
- json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1128
-
1129
- obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1130
- obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1131
- obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1132
-
1133
-
1134
-
1135
- // Utility Methods
1136
-
1137
- init_client_id(parts) {
1138
- let cid = this.client_id;
1139
-
1140
- if (undefined === cid) {
1141
- this.client_id = cid = (
1142
-
1143
-
1144
-
1145
- this.new_client_id(parts)
1146
- );}
1147
-
1148
- return cid}
1149
-
1150
- new_client_id(parts) {
1151
- return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
1152
-
1153
-
1154
-
1155
-
1156
-
1157
-
1158
-
1159
-
1160
-
1142
+ post(topic, payload, pub_opt) {// qos:0
1143
+ return this.pub({topic, payload, qos:0}, pub_opt)}
1144
+ send(topic, payload, pub_opt) {// qos:1
1145
+ return this.pub({topic, payload, qos:1}, pub_opt)}
1146
+ store(topic, payload, pub_opt) {// qos:1, retain: 1
1147
+ return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
1148
+
1149
+ // alias: json_post
1150
+ obj_post(topic, msg, pub_opt) {// qos:0
1151
+ return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
1152
+ // alias: json_send
1153
+ obj_send(topic, msg, pub_opt) {// qos:1
1154
+ return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
1155
+ // alias: json_store
1156
+ obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
1157
+ return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
1158
+
1159
+ // alias: publish -- because 'pub' is shorter for semantic aliases above
1160
+ async pub(pkt, pub_opt) {
1161
+ if (undefined === pkt.payload) {
1162
+ if ('function' === typeof pub_opt) {
1163
+ pub_opt = {fn_encode: pub_opt};}
1164
+
1165
+ let {msg} = pkt;
1166
+ switch (typeof msg) {
1167
+ case 'function':
1168
+ pub_opt = {...pub_opt, fn_encode: msg};
1169
+ // flow into 'undefined' case
1170
+ case 'undefined':
1171
+ // return a single-value closure to publish packets
1172
+ return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
1173
+
1174
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1175
+ let {fn_encode} = pub_opt || {};
1176
+ pkt.payload = fn_encode
1177
+ ? await fn_encode(msg)
1178
+ : JSON.stringify(msg);}
1179
+
1180
+ if (pub_opt) {
1181
+ if (pub_opt.props) {
1182
+ pkt.props = pub_opt.props;}
1183
+ if (pub_opt.xform) {
1184
+ pkt = pub_opt.xform(pkt) || pkt;} }
1185
+
1186
+ return this._send('publish', pkt,
1187
+ pkt.qos ? pkt : void 0 ) }// key
1161
1188
 
1162
1189
 
1163
1190
  // Internal API
@@ -1172,41 +1199,18 @@ class MQTTBase {
1172
1199
  return _mqtt_dispatch(this, target)}
1173
1200
 
1174
1201
  static _aliases() {
1175
- return ' pub:publish sub:subscribe unsub:unsubscribe '}
1202
+ return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
1176
1203
 
1177
1204
  static _once_(self=this) {
1178
1205
  self._once_ = _=>0;
1179
- self.MQTTError = MQTTError;
1180
1206
  let p = self.prototype;
1207
+ p.MQTTError = MQTTError;
1181
1208
  for (let alias of self._aliases().split(/\s+/)) {
1182
1209
  alias = alias.split(':');
1183
1210
  let fn = alias[1] && p[alias[1]];
1184
1211
  if (fn) {p[alias[0]] = fn;} } } }
1185
1212
 
1186
1213
 
1187
- /*
1188
- on_mqtt_type = {
1189
- mqtt_auth(pkt, ctx) ::
1190
- mqtt_connect(pkt, ctx) ::
1191
- mqtt_connack(pkt, ctx) ::
1192
- mqtt_disconnect(pkt, ctx) ::
1193
-
1194
- mqtt_publish(pkt, ctx)
1195
- mqtt_subscribe(pkt, ctx) ::
1196
- mqtt_unsubscribe(pkt, ctx) ::
1197
-
1198
- mqtt_pingreq(pkt, ctx) ::
1199
- mqtt_pingresp(pkt, ctx) ::
1200
- }
1201
- */
1202
-
1203
-
1204
- const _prefix_topics = (topic_prefix, iterable) =>
1205
- Array.from(iterable, value =>(
1206
- value.trim // string
1207
- ? _prefix_topics(topic_prefix, value)
1208
- : topic_prefix + value) );
1209
-
1210
1214
  function _as_topics(pkt, ex, topic_prefix) {
1211
1215
  if (ex?.trim) {// string
1212
1216
  topic_prefix = ex;
@@ -1223,55 +1227,11 @@ function _as_topics(pkt, ex, topic_prefix) {
1223
1227
  if (topic_prefix) {
1224
1228
  // particularly useful with shared queues, e.g.
1225
1229
  // topic_prefix = '$share/some-queue-name/'
1226
- pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
1227
- return pkt}
1230
+ let _prefix_topics = v =>
1231
+ v.trim ? topic_prefix+v : v.map(_prefix_topics);
1228
1232
 
1229
-
1230
- async function _pub(self, pkt, pub_opt) {
1231
- if (undefined === pkt.payload) {
1232
- if ('function' === typeof pub_opt) {
1233
- pub_opt = {fn_encode: pub_opt};}
1234
-
1235
- let {msg} = pkt;
1236
- switch (typeof msg) {
1237
- case 'function':
1238
- pub_opt = {...pub_opt, fn_encode: msg};
1239
- // flow into 'undefined' case
1240
- case 'undefined':
1241
- // return a single-value closure to publish packets
1242
- return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
1243
-
1244
- default:
1245
- // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1246
- let {fn_encode} = pub_opt || {};
1247
- pkt.payload = fn_encode
1248
- ? await fn_encode(msg)
1249
- : JSON.stringify(msg);} }
1250
-
1251
- if (pub_opt) {
1252
- if (pub_opt.props) {
1253
- pkt.props = pub_opt.props;}
1254
- if (pub_opt.xform) {
1255
- pkt = pub_opt.xform(pkt) || pkt;} }
1256
-
1257
- return self._send('publish', pkt,
1258
- pkt.qos ? pkt : void 0 ) }// key
1259
-
1260
- {
1261
- Object.assign(_pub,{
1262
- m: (self, topic, payload, pub_opt) =>
1263
- _pub(self, {topic, payload, qos:0}, pub_opt)
1264
- , mq: (self, topic, payload, pub_opt) =>
1265
- _pub(self, {topic, payload, qos:1}, pub_opt)
1266
- , mqr: (self, topic, payload, pub_opt) =>
1267
- _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1268
-
1269
- , o: (self, topic, msg, pub_opt) =>
1270
- _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1271
- , oq: (self, topic, msg, pub_opt) =>
1272
- _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1273
- , oqr: (self, topic, msg, pub_opt) =>
1274
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
1233
+ pkt.topics = pkt.topics.map(_prefix_topics);}
1234
+ return pkt}
1275
1235
 
1276
1236
  const pkt_api = {
1277
1237
  utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
@@ -1280,20 +1240,27 @@ const pkt_api = {
1280
1240
  };
1281
1241
 
1282
1242
  class MQTTCore extends MQTTBase {
1283
- constructor(opt={}) {
1284
- super(opt);
1285
- this.with(opt);}
1286
-
1287
1243
  static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
1288
1244
  let self = class extends this {};
1289
1245
  self.prototype.mqtt_ctx =
1290
1246
  mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
1291
1247
  return self}
1292
1248
 
1293
- with(fns_ns) {
1294
- for (let [k,v] of Object.entries(fns_ns)) {
1295
- if ('function' === typeof v) {this[k] = v;} }
1296
- return this}
1249
+
1250
+ // automatic Client Id for connect()
1251
+ init_client_id(parts=['u8-mqtt--','']) {
1252
+ let sess_stg=this.sess_stg;
1253
+ let key, cid = sess_stg?.getItem(key=parts.join(' '));
1254
+ if (! cid) {
1255
+ cid = parts.join(Math.random().toString(36).slice(2));
1256
+ sess_stg?.setItem(key, cid);}
1257
+ return cid}
1258
+
1259
+ get sess_stg() {return globalThis.sessionStorage}
1260
+
1261
+
1262
+ //on_error(err, evt) ::
1263
+ // console.warn @ '[[u8-mqtt error: %s]]', evt, err
1297
1264
 
1298
1265
  //log_conn(evt, arg, err_arg) ::
1299
1266
  // console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
@@ -1371,7 +1338,6 @@ class MQTTCore extends MQTTBase {
1371
1338
 
1372
1339
 
1373
1340
 
1374
-
1375
1341
 
1376
1342
 
1377
1343
 
@@ -1432,7 +1398,7 @@ class MQTTCore extends MQTTBase {
1432
1398
 
1433
1399
  return this} }
1434
1400
 
1435
- const version = '0.4.1';
1401
+ const version = '0.5.0';
1436
1402
 
1437
1403
  const MQTTClient_v4 = /* #__PURE__ */
1438
1404
  MQTTCore.mqtt_ctx(4, mqtt_opts_v5);