u8-mqtt 0.4.0 → 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 (82) hide show
  1. package/README.md +3 -3
  2. package/cjs/basic-v4.cjs +107 -146
  3. package/cjs/basic-v4.cjs.map +1 -1
  4. package/cjs/basic-v5.cjs +106 -145
  5. package/cjs/basic-v5.cjs.map +1 -1
  6. package/cjs/index.cjs +163 -206
  7. package/cjs/index.cjs.map +1 -1
  8. package/cjs/v4.cjs +164 -207
  9. package/cjs/v4.cjs.map +1 -1
  10. package/cjs/v5.cjs +163 -206
  11. package/cjs/v5.cjs.map +1 -1
  12. package/code/_dispatch.jsy +16 -0
  13. package/code/base.jsy +72 -133
  14. package/code/core.jsy +15 -9
  15. package/code/router_path.jsy +38 -51
  16. package/code/with_topic_router.jsy +20 -11
  17. package/esm/basic-v4.js +1154 -0
  18. package/esm/basic-v4.js.map +1 -0
  19. package/esm/basic-v5.js +1416 -0
  20. package/esm/basic-v5.js.map +1 -0
  21. package/esm/deno/basic-v4.js +107 -146
  22. package/esm/deno/basic-v4.js.map +1 -1
  23. package/esm/deno/basic-v5.js +106 -145
  24. package/esm/deno/basic-v5.js.map +1 -1
  25. package/esm/deno/index.js +163 -206
  26. package/esm/deno/index.js.map +1 -1
  27. package/esm/deno/v4.js +164 -207
  28. package/esm/deno/v4.js.map +1 -1
  29. package/esm/deno/v5.js +163 -206
  30. package/esm/deno/v5.js.map +1 -1
  31. package/esm/index.js +1599 -0
  32. package/esm/index.js.map +1 -0
  33. package/esm/node/basic-v4.js +107 -146
  34. package/esm/node/basic-v4.js.map +1 -1
  35. package/esm/node/basic-v4.mjs +107 -146
  36. package/esm/node/basic-v4.mjs.map +1 -1
  37. package/esm/node/basic-v5.js +106 -145
  38. package/esm/node/basic-v5.js.map +1 -1
  39. package/esm/node/basic-v5.mjs +106 -145
  40. package/esm/node/basic-v5.mjs.map +1 -1
  41. package/esm/node/index.js +163 -206
  42. package/esm/node/index.js.map +1 -1
  43. package/esm/node/index.mjs +163 -206
  44. package/esm/node/index.mjs.map +1 -1
  45. package/esm/node/v4.js +164 -207
  46. package/esm/node/v4.js.map +1 -1
  47. package/esm/node/v4.mjs +164 -207
  48. package/esm/node/v4.mjs.map +1 -1
  49. package/esm/node/v5.js +163 -206
  50. package/esm/node/v5.js.map +1 -1
  51. package/esm/node/v5.mjs +163 -206
  52. package/esm/node/v5.mjs.map +1 -1
  53. package/esm/v4.js +1336 -0
  54. package/esm/v4.js.map +1 -0
  55. package/esm/v5.js +1599 -0
  56. package/esm/v5.js.map +1 -0
  57. package/esm/web/basic-v4.js +107 -146
  58. package/esm/web/basic-v4.js.map +1 -1
  59. package/esm/web/basic-v4.min.js +1 -1
  60. package/esm/web/basic-v4.min.js.br +0 -0
  61. package/esm/web/basic-v4.min.js.gz +0 -0
  62. package/esm/web/basic-v5.js +106 -145
  63. package/esm/web/basic-v5.js.map +1 -1
  64. package/esm/web/basic-v5.min.js +1 -1
  65. package/esm/web/basic-v5.min.js.br +0 -0
  66. package/esm/web/basic-v5.min.js.gz +0 -0
  67. package/esm/web/index.js +163 -206
  68. package/esm/web/index.js.map +1 -1
  69. package/esm/web/index.min.js +1 -1
  70. package/esm/web/index.min.js.br +0 -0
  71. package/esm/web/index.min.js.gz +0 -0
  72. package/esm/web/v4.js +164 -207
  73. package/esm/web/v4.js.map +1 -1
  74. package/esm/web/v4.min.js +1 -1
  75. package/esm/web/v4.min.js.br +0 -0
  76. package/esm/web/v4.min.js.gz +0 -0
  77. package/esm/web/v5.js +163 -206
  78. package/esm/web/v5.js.map +1 -1
  79. package/esm/web/v5.min.js +1 -1
  80. package/esm/web/v5.min.js.br +0 -0
  81. package/esm/web/v5.min.js.gz +0 -0
  82. package/package.json +7 -8
package/cjs/basic-v5.cjs CHANGED
@@ -154,7 +154,7 @@ class mqtt_reader_v4 {
154
154
 
155
155
  }
156
156
 
157
- class mqtt_reader_v5$1 extends mqtt_reader_v4 {
157
+ let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
158
158
  props() {
159
159
  let {buf, step} = this;
160
160
  let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
@@ -189,7 +189,7 @@ class mqtt_reader_v5$1 extends mqtt_reader_v4 {
189
189
  : buf.subarray(vi, step.k|0)
190
190
  }
191
191
  */
192
- }
192
+ };
193
193
 
194
194
  function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
195
195
  mqtt_reader = class extends mqtt_reader {
@@ -1034,6 +1034,22 @@ const _mqtt_cmdid_dispatch ={
1034
1034
 
1035
1035
  await fn?.call(target, pkt, ctx);} })()) };
1036
1036
 
1037
+ /*
1038
+ on_mqtt_type = {
1039
+ mqtt_auth(pkt, ctx) ::
1040
+ mqtt_connect(pkt, ctx) ::
1041
+ mqtt_connack(pkt, ctx) ::
1042
+ mqtt_disconnect(pkt, ctx) ::
1043
+
1044
+ mqtt_publish(pkt, ctx)
1045
+ mqtt_subscribe(pkt, ctx) ::
1046
+ mqtt_unsubscribe(pkt, ctx) ::
1047
+
1048
+ mqtt_pingreq(pkt, ctx) ::
1049
+ mqtt_pingresp(pkt, ctx) ::
1050
+ }
1051
+ */
1052
+
1037
1053
  function _mqtt_dispatch(opt, target) {
1038
1054
  let _disp_ = _mqtt_cmdid_dispatch.create(target);
1039
1055
  let { cmdids } = _disp_;
@@ -1063,28 +1079,34 @@ class MQTTError extends Error {
1063
1079
 
1064
1080
  class MQTTBase {
1065
1081
  constructor(opt={}) {
1082
+ this.with(opt);
1066
1083
  this._conn_ = _mqtt_conn(this,
1067
1084
  this._init_dispatch(opt, this)); }
1068
1085
 
1086
+ with(fns_ns) {
1087
+ for (let [k,v] of Object.entries(fns_ns)) {
1088
+ if ('function' === typeof v) {this[k] = v;} }
1089
+ return this}
1090
+
1069
1091
  async conn_emit(evt, arg, err_arg) {
1070
1092
  this.log_conn?.(evt, arg, err_arg);
1071
1093
  try {
1072
- let fn_evt = this[await evt]; // microtask break
1094
+ let fn_evt = this[await evt]; // microtask break using `await evt`
1073
1095
  if (fn_evt) {
1074
1096
  await fn_evt.call(this, this, arg, err_arg);}
1075
- else if (err_arg) {
1076
- await this.on_error(err_arg, evt);} }
1097
+ else if (err_arg) {throw err_arg} }
1077
1098
  catch (err) {
1078
1099
  this.on_error(err, evt);} }
1079
1100
 
1080
- on_error(err, err_path) {
1081
- console.warn('[[u8-mqtt error: %s]]', err_path, err); }
1101
+ on_error(err, evt) {
1102
+ console.warn('[[u8-mqtt error: %s]]', evt, err); }
1082
1103
 
1083
1104
  // Handshaking Packets
1084
1105
 
1085
1106
  async connect(pkt={}) {
1086
- let cid = pkt.client_id || ['u8-mqtt--', ''];
1087
- if (Array.isArray(cid)) {
1107
+ let cid = pkt.client_id || this.client_id;
1108
+ if ('string' !== typeof cid) {
1109
+ // see init_client_id implementation in core.jsy
1088
1110
  pkt.client_id = cid = this.init_client_id(cid);}
1089
1111
  this.client_id = cid;
1090
1112
 
@@ -1110,12 +1132,13 @@ class MQTTBase {
1110
1132
  return this._send('auth', pkt, 'auth')}
1111
1133
 
1112
1134
  ping() {return this._send('pingreq', null, 'pingresp')}
1113
-
1135
+ puback({pkt_id}) {return this._send('puback', {pkt_id})}
1114
1136
 
1115
1137
  // alias: sub
1116
1138
  subscribe(pkt, ex, topic_prefix) {
1117
1139
  pkt = _as_topics(pkt, ex, topic_prefix);
1118
- return this._send('subscribe', pkt, pkt)}
1140
+ let suback = this._send('subscribe', pkt, pkt);
1141
+ return this.on_sub?.(suback, pkt) ?? suback}
1119
1142
 
1120
1143
  // alias: unsub
1121
1144
  unsubscribe(pkt, ex, topic_prefix) {
@@ -1123,48 +1146,52 @@ class MQTTBase {
1123
1146
  return this._send('unsubscribe', pkt, pkt)}
1124
1147
 
1125
1148
 
1126
- // alias: pub
1127
- publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
1128
- post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
1129
- send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
1130
- store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
1131
-
1132
- json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1133
- json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1134
- json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1135
-
1136
- obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1137
- obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1138
- obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1139
-
1140
-
1141
-
1142
- // Utility Methods
1143
-
1144
- init_client_id(parts) {
1145
- let cid = this.client_id;
1146
-
1147
- if (undefined === cid) {
1148
- this.client_id = cid = (
1149
-
1150
-
1151
-
1152
- this.new_client_id(parts)
1153
- );}
1154
-
1155
- return cid}
1156
-
1157
- new_client_id(parts) {
1158
- return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
1159
-
1160
-
1161
-
1162
-
1163
-
1164
-
1165
-
1166
-
1167
-
1149
+ post(topic, payload, pub_opt) {// qos:0
1150
+ return this.pub({topic, payload, qos:0}, pub_opt)}
1151
+ send(topic, payload, pub_opt) {// qos:1
1152
+ return this.pub({topic, payload, qos:1}, pub_opt)}
1153
+ store(topic, payload, pub_opt) {// qos:1, retain: 1
1154
+ return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
1155
+
1156
+ // alias: json_post
1157
+ obj_post(topic, msg, pub_opt) {// qos:0
1158
+ return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
1159
+ // alias: json_send
1160
+ obj_send(topic, msg, pub_opt) {// qos:1
1161
+ return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
1162
+ // alias: json_store
1163
+ obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
1164
+ return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
1165
+
1166
+ // alias: publish -- because 'pub' is shorter for semantic aliases above
1167
+ async pub(pkt, pub_opt) {
1168
+ if (undefined === pkt.payload) {
1169
+ if ('function' === typeof pub_opt) {
1170
+ pub_opt = {fn_encode: pub_opt};}
1171
+
1172
+ let {msg} = pkt;
1173
+ switch (typeof msg) {
1174
+ case 'function':
1175
+ pub_opt = {...pub_opt, fn_encode: msg};
1176
+ // flow into 'undefined' case
1177
+ case 'undefined':
1178
+ // return a single-value closure to publish packets
1179
+ return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
1180
+
1181
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1182
+ let {fn_encode} = pub_opt || {};
1183
+ pkt.payload = fn_encode
1184
+ ? await fn_encode(msg)
1185
+ : JSON.stringify(msg);}
1186
+
1187
+ if (pub_opt) {
1188
+ if (pub_opt.props) {
1189
+ pkt.props = pub_opt.props;}
1190
+ if (pub_opt.xform) {
1191
+ pkt = pub_opt.xform(pkt) || pkt;} }
1192
+
1193
+ return this._send('publish', pkt,
1194
+ pkt.qos ? pkt : void 0 ) }// key
1168
1195
 
1169
1196
 
1170
1197
  // Internal API
@@ -1173,52 +1200,24 @@ class MQTTBase {
1173
1200
 
1174
1201
  _init_dispatch(opt) {
1175
1202
  this.constructor?._once_();
1176
- let router = this.router =
1177
- this._init_router?.(opt, this);
1178
-
1179
- let tgt ={
1180
- __proto__: opt.on_mqtt_type || {}
1181
- , router};
1182
-
1183
- tgt.mqtt_publish ||= router?.invoke;
1184
- return _mqtt_dispatch(this, tgt)}
1203
+ let target ={__proto__: opt.on_mqtt_type};
1204
+ target.mqtt_publish ||=
1205
+ this._init_router?.(opt, this, target);
1206
+ return _mqtt_dispatch(this, target)}
1185
1207
 
1186
1208
  static _aliases() {
1187
- return ' pub:publish sub:subscribe unsub:unsubscribe '}
1209
+ return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
1188
1210
 
1189
1211
  static _once_(self=this) {
1190
1212
  self._once_ = _=>0;
1191
- self.MQTTError = MQTTError;
1192
1213
  let p = self.prototype;
1214
+ p.MQTTError = MQTTError;
1193
1215
  for (let alias of self._aliases().split(/\s+/)) {
1194
1216
  alias = alias.split(':');
1195
1217
  let fn = alias[1] && p[alias[1]];
1196
1218
  if (fn) {p[alias[0]] = fn;} } } }
1197
1219
 
1198
1220
 
1199
- /*
1200
- on_mqtt_type = {
1201
- mqtt_auth(pkt, ctx) ::
1202
- mqtt_connect(pkt, ctx) ::
1203
- mqtt_connack(pkt, ctx) ::
1204
- mqtt_disconnect(pkt, ctx) ::
1205
-
1206
- mqtt_publish(pkt, ctx)
1207
- mqtt_subscribe(pkt, ctx) ::
1208
- mqtt_unsubscribe(pkt, ctx) ::
1209
-
1210
- mqtt_pingreq(pkt, ctx) ::
1211
- mqtt_pingresp(pkt, ctx) ::
1212
- }
1213
- */
1214
-
1215
-
1216
- const _prefix_topics = (topic_prefix, iterable) =>
1217
- Array.from(iterable, value =>(
1218
- value.trim // string
1219
- ? _prefix_topics(topic_prefix, value)
1220
- : topic_prefix + value) );
1221
-
1222
1221
  function _as_topics(pkt, ex, topic_prefix) {
1223
1222
  if (ex?.trim) {// string
1224
1223
  topic_prefix = ex;
@@ -1235,55 +1234,11 @@ function _as_topics(pkt, ex, topic_prefix) {
1235
1234
  if (topic_prefix) {
1236
1235
  // particularly useful with shared queues, e.g.
1237
1236
  // topic_prefix = '$share/some-queue-name/'
1238
- pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
1239
- return pkt}
1240
-
1237
+ let _prefix_topics = v =>
1238
+ v.trim ? topic_prefix+v : v.map(_prefix_topics);
1241
1239
 
1242
- async function _pub(self, pkt, pub_opt) {
1243
- if (undefined === pkt.payload) {
1244
- if ('function' === typeof pub_opt) {
1245
- pub_opt = {fn_encode: pub_opt};}
1246
-
1247
- let {msg} = pkt;
1248
- switch (typeof msg) {
1249
- case 'function':
1250
- pub_opt = {...pub_opt, fn_encode: msg};
1251
- // flow into 'undefined' case
1252
- case 'undefined':
1253
- // return a single-value closure to publish packets
1254
- return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
1255
-
1256
- default:
1257
- // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1258
- let {fn_encode} = pub_opt || {};
1259
- pkt.payload = fn_encode
1260
- ? await fn_encode(msg)
1261
- : JSON.stringify(msg);} }
1262
-
1263
- if (pub_opt) {
1264
- if (pub_opt.props) {
1265
- pkt.props = pub_opt.props;}
1266
- if (pub_opt.xform) {
1267
- pkt = pub_opt.xform(pkt) || pkt;} }
1268
-
1269
- return self._send('publish', pkt,
1270
- pkt.qos ? pkt : void 0 ) }// key
1271
-
1272
- {
1273
- Object.assign(_pub,{
1274
- m: (self, topic, payload, pub_opt) =>
1275
- _pub(self, {topic, payload, qos:0}, pub_opt)
1276
- , mq: (self, topic, payload, pub_opt) =>
1277
- _pub(self, {topic, payload, qos:1}, pub_opt)
1278
- , mqr: (self, topic, payload, pub_opt) =>
1279
- _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1280
-
1281
- , o: (self, topic, msg, pub_opt) =>
1282
- _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1283
- , oq: (self, topic, msg, pub_opt) =>
1284
- _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1285
- , oqr: (self, topic, msg, pub_opt) =>
1286
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
1240
+ pkt.topics = pkt.topics.map(_prefix_topics);}
1241
+ return pkt}
1287
1242
 
1288
1243
  const pkt_api = {
1289
1244
  utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
@@ -1292,20 +1247,27 @@ const pkt_api = {
1292
1247
  };
1293
1248
 
1294
1249
  class MQTTCore extends MQTTBase {
1295
- constructor(opt={}) {
1296
- super(opt);
1297
- this.with(opt);}
1298
-
1299
1250
  static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
1300
1251
  let self = class extends this {};
1301
1252
  self.prototype.mqtt_ctx =
1302
1253
  mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
1303
1254
  return self}
1304
1255
 
1305
- with(fns_ns) {
1306
- for (let [k,v] of Object.entries(fns_ns)) {
1307
- if ('function' === typeof v) {this[k] = v;} }
1308
- return this}
1256
+
1257
+ // automatic Client Id for connect()
1258
+ init_client_id(parts=['u8-mqtt--','']) {
1259
+ let sess_stg=this.sess_stg;
1260
+ let key, cid = sess_stg?.getItem(key=parts.join(' '));
1261
+ if (! cid) {
1262
+ cid = parts.join(Math.random().toString(36).slice(2));
1263
+ sess_stg?.setItem(key, cid);}
1264
+ return cid}
1265
+
1266
+ get sess_stg() {return globalThis.sessionStorage}
1267
+
1268
+
1269
+ //on_error(err, evt) ::
1270
+ // console.warn @ '[[u8-mqtt error: %s]]', evt, err
1309
1271
 
1310
1272
  //log_conn(evt, arg, err_arg) ::
1311
1273
  // console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
@@ -1382,7 +1344,6 @@ class MQTTCore extends MQTTBase {
1382
1344
 
1383
1345
  with_tcp(...opt) {
1384
1346
  opt = this._conn_opt(opt);
1385
- console.log({opt});
1386
1347
  return this._use_conn (() =>
1387
1348
  this.with_stream(
1388
1349
  node_net.connect(opt)) ) }
@@ -1444,7 +1405,7 @@ class MQTTCore extends MQTTBase {
1444
1405
 
1445
1406
  return this} }
1446
1407
 
1447
- const version = '0.4.0';
1408
+ const version = '0.5.0';
1448
1409
 
1449
1410
  const MQTTClient_v4 = /* #__PURE__ */
1450
1411
  MQTTCore.mqtt_ctx(4, mqtt_opts_v5);