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
@@ -150,7 +150,7 @@ class mqtt_reader_v4 {
150
150
 
151
151
  }
152
152
 
153
- class mqtt_reader_v5$1 extends mqtt_reader_v4 {
153
+ let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
154
154
  props() {
155
155
  let {buf, step} = this;
156
156
  let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
@@ -185,7 +185,7 @@ class mqtt_reader_v5$1 extends mqtt_reader_v4 {
185
185
  : buf.subarray(vi, step.k|0)
186
186
  }
187
187
  */
188
- }
188
+ };
189
189
 
190
190
  function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
191
191
  mqtt_reader = class extends mqtt_reader {
@@ -1030,6 +1030,22 @@ const _mqtt_cmdid_dispatch ={
1030
1030
 
1031
1031
  await fn?.call(target, pkt, ctx);} })()) };
1032
1032
 
1033
+ /*
1034
+ on_mqtt_type = {
1035
+ mqtt_auth(pkt, ctx) ::
1036
+ mqtt_connect(pkt, ctx) ::
1037
+ mqtt_connack(pkt, ctx) ::
1038
+ mqtt_disconnect(pkt, ctx) ::
1039
+
1040
+ mqtt_publish(pkt, ctx)
1041
+ mqtt_subscribe(pkt, ctx) ::
1042
+ mqtt_unsubscribe(pkt, ctx) ::
1043
+
1044
+ mqtt_pingreq(pkt, ctx) ::
1045
+ mqtt_pingresp(pkt, ctx) ::
1046
+ }
1047
+ */
1048
+
1033
1049
  function _mqtt_dispatch(opt, target) {
1034
1050
  let _disp_ = _mqtt_cmdid_dispatch.create(target);
1035
1051
  let { cmdids } = _disp_;
@@ -1059,28 +1075,34 @@ class MQTTError extends Error {
1059
1075
 
1060
1076
  class MQTTBase {
1061
1077
  constructor(opt={}) {
1078
+ this.with(opt);
1062
1079
  this._conn_ = _mqtt_conn(this,
1063
1080
  this._init_dispatch(opt, this)); }
1064
1081
 
1082
+ with(fns_ns) {
1083
+ for (let [k,v] of Object.entries(fns_ns)) {
1084
+ if ('function' === typeof v) {this[k] = v;} }
1085
+ return this}
1086
+
1065
1087
  async conn_emit(evt, arg, err_arg) {
1066
1088
  this.log_conn?.(evt, arg, err_arg);
1067
1089
  try {
1068
- let fn_evt = this[await evt]; // microtask break
1090
+ let fn_evt = this[await evt]; // microtask break using `await evt`
1069
1091
  if (fn_evt) {
1070
1092
  await fn_evt.call(this, this, arg, err_arg);}
1071
- else if (err_arg) {
1072
- await this.on_error(err_arg, evt);} }
1093
+ else if (err_arg) {throw err_arg} }
1073
1094
  catch (err) {
1074
1095
  this.on_error(err, evt);} }
1075
1096
 
1076
- on_error(err, err_path) {
1077
- console.warn('[[u8-mqtt error: %s]]', err_path, err); }
1097
+ on_error(err, evt) {
1098
+ console.warn('[[u8-mqtt error: %s]]', evt, err); }
1078
1099
 
1079
1100
  // Handshaking Packets
1080
1101
 
1081
1102
  async connect(pkt={}) {
1082
- let cid = pkt.client_id || ['u8-mqtt--', ''];
1083
- if (Array.isArray(cid)) {
1103
+ let cid = pkt.client_id || this.client_id;
1104
+ if ('string' !== typeof cid) {
1105
+ // see init_client_id implementation in core.jsy
1084
1106
  pkt.client_id = cid = this.init_client_id(cid);}
1085
1107
  this.client_id = cid;
1086
1108
 
@@ -1106,12 +1128,13 @@ class MQTTBase {
1106
1128
  return this._send('auth', pkt, 'auth')}
1107
1129
 
1108
1130
  ping() {return this._send('pingreq', null, 'pingresp')}
1109
-
1131
+ puback({pkt_id}) {return this._send('puback', {pkt_id})}
1110
1132
 
1111
1133
  // alias: sub
1112
1134
  subscribe(pkt, ex, topic_prefix) {
1113
1135
  pkt = _as_topics(pkt, ex, topic_prefix);
1114
- return this._send('subscribe', pkt, pkt)}
1136
+ let suback = this._send('subscribe', pkt, pkt);
1137
+ return this.on_sub?.(suback, pkt) ?? suback}
1115
1138
 
1116
1139
  // alias: unsub
1117
1140
  unsubscribe(pkt, ex, topic_prefix) {
@@ -1119,48 +1142,52 @@ class MQTTBase {
1119
1142
  return this._send('unsubscribe', pkt, pkt)}
1120
1143
 
1121
1144
 
1122
- // alias: pub
1123
- publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
1124
- post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
1125
- send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
1126
- store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
1127
-
1128
- json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1129
- json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1130
- json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1131
-
1132
- obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
1133
- obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
1134
- obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
1135
-
1136
-
1137
-
1138
- // Utility Methods
1139
-
1140
- init_client_id(parts) {
1141
- let cid = this.client_id;
1142
-
1143
- if (undefined === cid) {
1144
- this.client_id = cid = (
1145
-
1146
-
1147
-
1148
- this.new_client_id(parts)
1149
- );}
1150
-
1151
- return cid}
1152
-
1153
- new_client_id(parts) {
1154
- return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
1155
-
1156
-
1157
-
1158
-
1159
-
1160
-
1161
-
1162
-
1163
-
1145
+ post(topic, payload, pub_opt) {// qos:0
1146
+ return this.pub({topic, payload, qos:0}, pub_opt)}
1147
+ send(topic, payload, pub_opt) {// qos:1
1148
+ return this.pub({topic, payload, qos:1}, pub_opt)}
1149
+ store(topic, payload, pub_opt) {// qos:1, retain: 1
1150
+ return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
1151
+
1152
+ // alias: json_post
1153
+ obj_post(topic, msg, pub_opt) {// qos:0
1154
+ return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
1155
+ // alias: json_send
1156
+ obj_send(topic, msg, pub_opt) {// qos:1
1157
+ return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
1158
+ // alias: json_store
1159
+ obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
1160
+ return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
1161
+
1162
+ // alias: publish -- because 'pub' is shorter for semantic aliases above
1163
+ async pub(pkt, pub_opt) {
1164
+ if (undefined === pkt.payload) {
1165
+ if ('function' === typeof pub_opt) {
1166
+ pub_opt = {fn_encode: pub_opt};}
1167
+
1168
+ let {msg} = pkt;
1169
+ switch (typeof msg) {
1170
+ case 'function':
1171
+ pub_opt = {...pub_opt, fn_encode: msg};
1172
+ // flow into 'undefined' case
1173
+ case 'undefined':
1174
+ // return a single-value closure to publish packets
1175
+ return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
1176
+
1177
+ // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1178
+ let {fn_encode} = pub_opt || {};
1179
+ pkt.payload = fn_encode
1180
+ ? await fn_encode(msg)
1181
+ : JSON.stringify(msg);}
1182
+
1183
+ if (pub_opt) {
1184
+ if (pub_opt.props) {
1185
+ pkt.props = pub_opt.props;}
1186
+ if (pub_opt.xform) {
1187
+ pkt = pub_opt.xform(pkt) || pkt;} }
1188
+
1189
+ return this._send('publish', pkt,
1190
+ pkt.qos ? pkt : void 0 ) }// key
1164
1191
 
1165
1192
 
1166
1193
  // Internal API
@@ -1169,52 +1196,24 @@ class MQTTBase {
1169
1196
 
1170
1197
  _init_dispatch(opt) {
1171
1198
  this.constructor?._once_();
1172
- let router = this.router =
1173
- this._init_router?.(opt, this);
1174
-
1175
- let tgt ={
1176
- __proto__: opt.on_mqtt_type || {}
1177
- , router};
1178
-
1179
- tgt.mqtt_publish ||= router?.invoke;
1180
- return _mqtt_dispatch(this, tgt)}
1199
+ let target ={__proto__: opt.on_mqtt_type};
1200
+ target.mqtt_publish ||=
1201
+ this._init_router?.(opt, this, target);
1202
+ return _mqtt_dispatch(this, target)}
1181
1203
 
1182
1204
  static _aliases() {
1183
- return ' pub:publish sub:subscribe unsub:unsubscribe '}
1205
+ return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
1184
1206
 
1185
1207
  static _once_(self=this) {
1186
1208
  self._once_ = _=>0;
1187
- self.MQTTError = MQTTError;
1188
1209
  let p = self.prototype;
1210
+ p.MQTTError = MQTTError;
1189
1211
  for (let alias of self._aliases().split(/\s+/)) {
1190
1212
  alias = alias.split(':');
1191
1213
  let fn = alias[1] && p[alias[1]];
1192
1214
  if (fn) {p[alias[0]] = fn;} } } }
1193
1215
 
1194
1216
 
1195
- /*
1196
- on_mqtt_type = {
1197
- mqtt_auth(pkt, ctx) ::
1198
- mqtt_connect(pkt, ctx) ::
1199
- mqtt_connack(pkt, ctx) ::
1200
- mqtt_disconnect(pkt, ctx) ::
1201
-
1202
- mqtt_publish(pkt, ctx)
1203
- mqtt_subscribe(pkt, ctx) ::
1204
- mqtt_unsubscribe(pkt, ctx) ::
1205
-
1206
- mqtt_pingreq(pkt, ctx) ::
1207
- mqtt_pingresp(pkt, ctx) ::
1208
- }
1209
- */
1210
-
1211
-
1212
- const _prefix_topics = (topic_prefix, iterable) =>
1213
- Array.from(iterable, value =>(
1214
- value.trim // string
1215
- ? _prefix_topics(topic_prefix, value)
1216
- : topic_prefix + value) );
1217
-
1218
1217
  function _as_topics(pkt, ex, topic_prefix) {
1219
1218
  if (ex?.trim) {// string
1220
1219
  topic_prefix = ex;
@@ -1231,55 +1230,11 @@ function _as_topics(pkt, ex, topic_prefix) {
1231
1230
  if (topic_prefix) {
1232
1231
  // particularly useful with shared queues, e.g.
1233
1232
  // topic_prefix = '$share/some-queue-name/'
1234
- pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
1235
- return pkt}
1236
-
1233
+ let _prefix_topics = v =>
1234
+ v.trim ? topic_prefix+v : v.map(_prefix_topics);
1237
1235
 
1238
- async function _pub(self, pkt, pub_opt) {
1239
- if (undefined === pkt.payload) {
1240
- if ('function' === typeof pub_opt) {
1241
- pub_opt = {fn_encode: pub_opt};}
1242
-
1243
- let {msg} = pkt;
1244
- switch (typeof msg) {
1245
- case 'function':
1246
- pub_opt = {...pub_opt, fn_encode: msg};
1247
- // flow into 'undefined' case
1248
- case 'undefined':
1249
- // return a single-value closure to publish packets
1250
- return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
1251
-
1252
- default:
1253
- // Encode payload from msg; fn_encode allows alternative to JSON.stringify
1254
- let {fn_encode} = pub_opt || {};
1255
- pkt.payload = fn_encode
1256
- ? await fn_encode(msg)
1257
- : JSON.stringify(msg);} }
1258
-
1259
- if (pub_opt) {
1260
- if (pub_opt.props) {
1261
- pkt.props = pub_opt.props;}
1262
- if (pub_opt.xform) {
1263
- pkt = pub_opt.xform(pkt) || pkt;} }
1264
-
1265
- return self._send('publish', pkt,
1266
- pkt.qos ? pkt : void 0 ) }// key
1267
-
1268
- {
1269
- Object.assign(_pub,{
1270
- m: (self, topic, payload, pub_opt) =>
1271
- _pub(self, {topic, payload, qos:0}, pub_opt)
1272
- , mq: (self, topic, payload, pub_opt) =>
1273
- _pub(self, {topic, payload, qos:1}, pub_opt)
1274
- , mqr: (self, topic, payload, pub_opt) =>
1275
- _pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
1276
-
1277
- , o: (self, topic, msg, pub_opt) =>
1278
- _pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
1279
- , oq: (self, topic, msg, pub_opt) =>
1280
- _pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
1281
- , oqr: (self, topic, msg, pub_opt) =>
1282
- _pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
1236
+ pkt.topics = pkt.topics.map(_prefix_topics);}
1237
+ return pkt}
1283
1238
 
1284
1239
  const pkt_api = {
1285
1240
  utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
@@ -1288,20 +1243,27 @@ const pkt_api = {
1288
1243
  };
1289
1244
 
1290
1245
  class MQTTCore extends MQTTBase {
1291
- constructor(opt={}) {
1292
- super(opt);
1293
- this.with(opt);}
1294
-
1295
1246
  static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
1296
1247
  let self = class extends this {};
1297
1248
  self.prototype.mqtt_ctx =
1298
1249
  mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
1299
1250
  return self}
1300
1251
 
1301
- with(fns_ns) {
1302
- for (let [k,v] of Object.entries(fns_ns)) {
1303
- if ('function' === typeof v) {this[k] = v;} }
1304
- return this}
1252
+
1253
+ // automatic Client Id for connect()
1254
+ init_client_id(parts=['u8-mqtt--','']) {
1255
+ let sess_stg=this.sess_stg;
1256
+ let key, cid = sess_stg?.getItem(key=parts.join(' '));
1257
+ if (! cid) {
1258
+ cid = parts.join(Math.random().toString(36).slice(2));
1259
+ sess_stg?.setItem(key, cid);}
1260
+ return cid}
1261
+
1262
+ get sess_stg() {return globalThis.sessionStorage}
1263
+
1264
+
1265
+ //on_error(err, evt) ::
1266
+ // console.warn @ '[[u8-mqtt error: %s]]', evt, err
1305
1267
 
1306
1268
  //log_conn(evt, arg, err_arg) ::
1307
1269
  // console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
@@ -1378,7 +1340,6 @@ class MQTTCore extends MQTTBase {
1378
1340
 
1379
1341
  with_tcp(...opt) {
1380
1342
  opt = this._conn_opt(opt);
1381
- console.log({opt});
1382
1343
  return this._use_conn (() =>
1383
1344
  this.with_stream(
1384
1345
  connect(opt)) ) }
@@ -1440,7 +1401,7 @@ class MQTTCore extends MQTTBase {
1440
1401
 
1441
1402
  return this} }
1442
1403
 
1443
- const version = '0.4.0';
1404
+ const version = '0.5.0';
1444
1405
 
1445
1406
  const MQTTClient_v4 = /* #__PURE__ */
1446
1407
  MQTTCore.mqtt_ctx(4, mqtt_opts_v5);