u8-mqtt 0.6.1 → 0.6.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 (106) hide show
  1. package/cjs/basic-v4.cjs +194 -243
  2. package/cjs/basic-v4.cjs.map +1 -1
  3. package/cjs/basic-v5.cjs +264 -385
  4. package/cjs/basic-v5.cjs.map +1 -1
  5. package/cjs/full-v4.cjs +297 -353
  6. package/cjs/full-v4.cjs.map +1 -1
  7. package/cjs/full-v5.cjs +366 -492
  8. package/cjs/full-v5.cjs.map +1 -1
  9. package/cjs/index.cjs +263 -384
  10. package/cjs/index.cjs.map +1 -1
  11. package/cjs/v4.cjs +194 -243
  12. package/cjs/v4.cjs.map +1 -1
  13. package/cjs/v5.cjs +263 -384
  14. package/cjs/v5.cjs.map +1 -1
  15. package/esm/basic-v4.js +194 -243
  16. package/esm/basic-v4.js.map +1 -1
  17. package/esm/basic-v5.js +264 -385
  18. package/esm/basic-v5.js.map +1 -1
  19. package/esm/deno/basic-v4.js +194 -243
  20. package/esm/deno/basic-v4.js.map +1 -1
  21. package/esm/deno/basic-v5.js +264 -385
  22. package/esm/deno/basic-v5.js.map +1 -1
  23. package/esm/deno/full-v4.js +297 -353
  24. package/esm/deno/full-v4.js.map +1 -1
  25. package/esm/deno/full-v5.js +366 -492
  26. package/esm/deno/full-v5.js.map +1 -1
  27. package/esm/deno/index.js +263 -384
  28. package/esm/deno/index.js.map +1 -1
  29. package/esm/deno/v4.js +194 -243
  30. package/esm/deno/v4.js.map +1 -1
  31. package/esm/deno/v5.js +263 -384
  32. package/esm/deno/v5.js.map +1 -1
  33. package/esm/full-v4.js +297 -353
  34. package/esm/full-v4.js.map +1 -1
  35. package/esm/full-v5.js +366 -492
  36. package/esm/full-v5.js.map +1 -1
  37. package/esm/index.js +263 -384
  38. package/esm/index.js.map +1 -1
  39. package/esm/node/basic-v4.js +194 -243
  40. package/esm/node/basic-v4.js.map +1 -1
  41. package/esm/node/basic-v4.mjs +194 -243
  42. package/esm/node/basic-v4.mjs.map +1 -1
  43. package/esm/node/basic-v5.js +264 -385
  44. package/esm/node/basic-v5.js.map +1 -1
  45. package/esm/node/basic-v5.mjs +264 -385
  46. package/esm/node/basic-v5.mjs.map +1 -1
  47. package/esm/node/full-v4.js +297 -353
  48. package/esm/node/full-v4.js.map +1 -1
  49. package/esm/node/full-v4.mjs +297 -353
  50. package/esm/node/full-v4.mjs.map +1 -1
  51. package/esm/node/full-v5.js +366 -492
  52. package/esm/node/full-v5.js.map +1 -1
  53. package/esm/node/full-v5.mjs +366 -492
  54. package/esm/node/full-v5.mjs.map +1 -1
  55. package/esm/node/index.js +263 -384
  56. package/esm/node/index.js.map +1 -1
  57. package/esm/node/index.mjs +263 -384
  58. package/esm/node/index.mjs.map +1 -1
  59. package/esm/node/v4.js +194 -243
  60. package/esm/node/v4.js.map +1 -1
  61. package/esm/node/v4.mjs +194 -243
  62. package/esm/node/v4.mjs.map +1 -1
  63. package/esm/node/v5.js +263 -384
  64. package/esm/node/v5.js.map +1 -1
  65. package/esm/node/v5.mjs +263 -384
  66. package/esm/node/v5.mjs.map +1 -1
  67. package/esm/v4.js +194 -243
  68. package/esm/v4.js.map +1 -1
  69. package/esm/v5.js +263 -384
  70. package/esm/v5.js.map +1 -1
  71. package/esm/web/basic-v4.js +194 -243
  72. package/esm/web/basic-v4.js.map +1 -1
  73. package/esm/web/basic-v4.min.js +1 -1
  74. package/esm/web/basic-v4.min.js.br +0 -0
  75. package/esm/web/basic-v4.min.js.gz +0 -0
  76. package/esm/web/basic-v5.js +264 -385
  77. package/esm/web/basic-v5.js.map +1 -1
  78. package/esm/web/basic-v5.min.js +1 -1
  79. package/esm/web/basic-v5.min.js.br +0 -0
  80. package/esm/web/basic-v5.min.js.gz +0 -0
  81. package/esm/web/full-v4.js +297 -353
  82. package/esm/web/full-v4.js.map +1 -1
  83. package/esm/web/full-v4.min.js +1 -1
  84. package/esm/web/full-v4.min.js.br +0 -0
  85. package/esm/web/full-v4.min.js.gz +0 -0
  86. package/esm/web/full-v5.js +366 -492
  87. package/esm/web/full-v5.js.map +1 -1
  88. package/esm/web/full-v5.min.js +1 -1
  89. package/esm/web/full-v5.min.js.br +0 -0
  90. package/esm/web/full-v5.min.js.gz +0 -0
  91. package/esm/web/index.js +263 -384
  92. package/esm/web/index.js.map +1 -1
  93. package/esm/web/index.min.js +1 -1
  94. package/esm/web/index.min.js.br +0 -0
  95. package/esm/web/index.min.js.gz +0 -0
  96. package/esm/web/v4.js +194 -243
  97. package/esm/web/v4.js.map +1 -1
  98. package/esm/web/v4.min.js +1 -1
  99. package/esm/web/v4.min.js.br +0 -0
  100. package/esm/web/v4.min.js.gz +0 -0
  101. package/esm/web/v5.js +263 -384
  102. package/esm/web/v5.js.map +1 -1
  103. package/esm/web/v5.min.js +1 -1
  104. package/esm/web/v5.min.js.br +0 -0
  105. package/esm/web/v5.min.js.gz +0 -0
  106. package/package.json +2 -2
package/esm/web/v4.js CHANGED
@@ -1,54 +1,32 @@
1
1
  function encode_varint(n, a=[]) {
2
- do {
3
- const ni = n & 0x7f;
4
- n >>>= 7;
5
- a.push( ni | (0===n ? 0 : 0x80) );
6
- } while (n > 0)
2
+ a.push((n<0x80 ? 0 : 0x80) | (n & 0x7f));
3
+ for(; ( n>>>=7 ) > 0 ;)
4
+ a.push((n<0x80 ? 0 : 0x80) | (n & 0x7f));
7
5
  return a
8
6
  }
9
7
 
8
+ function decode_varint$1(u8, i0=0, invalid) {
9
+ let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
10
+ for(; b & 0x80;)
11
+ n |= ((b=u8[i++]) & 0x7f) << (shift += 7);
10
12
 
11
- /*
12
- export function decode_varint_loop(u8, i=0) {
13
- let i0 = i
14
- let shift = 0, n = (u8[i] & 0x7f)
15
- while ( 0x80 & u8[i++] )
16
- n |= (u8[i] & 0x7f) << (shift += 7)
17
-
18
- return [n, i, i0]
19
- }
20
- */
21
-
22
-
23
- function decode_varint$1(u8, i=0) {
24
- let i0 = i;
25
- // unrolled for a max of 4 chains
26
- let n = (u8[i] & 0x7f) << 0;
27
- if ( 0x80 & u8[i++] ) {
28
- n |= (u8[i] & 0x7f) << 7;
29
- if ( 0x80 & u8[i++] ) {
30
- n |= (u8[i] & 0x7f) << 14;
31
- if ( 0x80 & u8[i++] ) {
32
- n |= (u8[i] & 0x7f) << 21;
33
- }
34
- }
35
- }
36
- return [n, i, i0]
13
+ return (u8.length < i)
14
+ ? [invalid, i0, i0] // fail: insuffecient u8 bytes to decode
15
+ : [n, i, i0] // successful value
37
16
  }
38
17
 
39
- class U8_Reason extends Number {
40
- static of(v, pkt_kind, by_kind) {
41
- let self = new this(v);
42
- self.reason = by_kind?.[pkt_kind]?.get(v) || pkt_kind;
43
- return self
18
+ class mqtt_reason extends Number {
19
+ constructor(v, reason) {
20
+ super(v);
21
+ this.reason = `:${(this.ok = v<0x80) ? 'ok' : 'fail'}:${reason}`;
44
22
  }
45
23
  }
46
24
 
47
25
  let mqtt_reader_v4$1 = class mqtt_reader_v4 {
48
- static of(buf) { return this.prototype.of(buf) }
49
- of(buf) {
26
+ static for(pkt, u8_body) { return new this().of(u8_body, {pkt}) }
27
+ of(buf, opt) {
50
28
  let step = (width, k) => (k=0|step.k, step.k=k+width, k);
51
- return {__proto__: this, buf, step}
29
+ return {__proto__: this, buf, step, ...opt}
52
30
  }
53
31
 
54
32
  has_more() {
@@ -91,7 +69,7 @@ let mqtt_reader_v4$1 = class mqtt_reader_v4 {
91
69
  reason(pkt_kind) {
92
70
  let v = this.buf[this.step(1)];
93
71
  if (null != v)
94
- return U8_Reason.of(v, pkt_kind, this._reasons_by)
72
+ return new mqtt_reason(v, this._reason_for?.(v, pkt_kind))
95
73
  }
96
74
 
97
75
  flush() {
@@ -102,28 +80,57 @@ let mqtt_reader_v4$1 = class mqtt_reader_v4 {
102
80
 
103
81
  };
104
82
 
105
- function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
106
- mqtt_reader = class extends mqtt_reader {
107
- static reasons(pkt_type, ...reason_entries) {
108
- let proto = this.prototype;
109
- proto._reasons_by = {... proto._reasons_by};
83
+ function with_reasons(mqtt_reader, by_kind) {
84
+ for (let [k,lut] of Object.entries(by_kind))
85
+ by_kind[k] = new Map(lut);
110
86
 
111
- let lut = (proto._reasons_by[pkt_type] ||= new Map());
112
- for (let [u8, reason] of reason_entries)
113
- lut.set( u8, reason );
114
-
115
- return this
87
+ return class extends mqtt_reader {
88
+ _reason_for(v, pkt_kind) {
89
+ return by_kind[pkt_kind]?.get(v) || by_kind.all.get(v)
90
+ }
91
+ warn(msg) {
92
+ let pkt = this.pkt;
93
+ pkt.warn ? pkt.warn(msg, pkt)
94
+ : console.warn('[u8-mqtt-packet] '+msg);
116
95
  }
117
- };
96
+ }
97
+ }
118
98
 
119
- for (let fn_info of info_fn_list)
120
- fn_info(mqtt_reader);
121
99
 
122
- return mqtt_reader
123
- }
100
+ const reasons_v4 = {
101
+ connack: [
102
+ [ 0x01, 'conn refused: unacceptable protocol version'],
103
+ [ 0x02, 'conn refused: identifier rejected'],
104
+ [ 0x03, 'conn refused: server unavailable'],
105
+ [ 0x04, 'conn refused: bad user name or password'],
106
+ [ 0x05, 'conn refused: not authorized'],
107
+ ],
108
+ suback: [
109
+ [ 0x00, 'qos=0'],
110
+ [ 0x01, 'qos=1'],
111
+ [ 0x02, 'qos=2'],
112
+ ],
113
+ unsuback: [
114
+ [ 0x11, 'no subscription existed'],
115
+ ],
116
+ puback: [
117
+ [ 0x10, 'no matching subscribers'],
118
+ ],
119
+ all: [
120
+ [ 0, ''], // Success
121
+ [ 0x80, 'unspecified error'], // disconnect puback suback unsuback
122
+ [ 0x83, 'implementation specific error'], // connack disconnect puback suback unsuback
123
+ [ 0x87, 'not authorized'], // connack disconnect puback suback unsuback
124
+ [ 0x8F, 'topic filter invalid'], // disconnect suback unsuback
125
+ [ 0x91, 'packet identifier in use'], // puback suback unsuback
126
+ [ 0x92, 'packet identifier not found' ], // pubxxx
127
+ ]};
128
+
129
+ const mqtt_reader_v4 = /* #__PURE__ */
130
+ with_reasons(mqtt_reader_v4$1, reasons_v4);
124
131
 
125
132
  class mqtt_writer_v4 {
126
- static of() { return this.prototype.of() }
133
+ static for(pkt) { return new this().of() }
127
134
  of() { return {__proto__: this, $:[]} }
128
135
 
129
136
  static init() { return this }
@@ -194,128 +201,6 @@ class mqtt_writer_v4 {
194
201
  }
195
202
  }
196
203
 
197
- function mqtt_decode_connack(ns, mqtt_reader) {
198
- class _connack_flags_ extends Number {
199
- get session_present() { return this & 0x01 !== 0 }
200
- }
201
-
202
- return ns[0x2] = (pkt, u8_body) => {
203
- let rdr = mqtt_reader.of(u8_body);
204
-
205
- pkt.flags =
206
- rdr.flags(_connack_flags_);
207
-
208
- pkt.reason = rdr.reason(pkt.type);
209
- if (5 <= pkt.mqtt_level)
210
- pkt.props = rdr.props();
211
- return pkt }
212
- }
213
-
214
-
215
- function _connack_v4(mqtt_reader) {
216
- mqtt_reader.reasons('connack',
217
- // MQTT 3.1.1
218
- [ 0x00, 'Success'],
219
- [ 0x01, 'Connection refused, unacceptable protocol version'],
220
- [ 0x02, 'Connection refused, identifier rejected'],
221
- [ 0x03, 'Connection refused, server unavailable'],
222
- [ 0x04, 'Connection refused, bad user name or password'],
223
- [ 0x05, 'Connection refused, not authorized'],
224
- );
225
- }
226
-
227
- function mqtt_decode_publish(ns, mqtt_reader) {
228
- return ns[0x3] = (pkt, u8_body) => {
229
- let {hdr} = pkt;
230
- pkt.dup = Boolean(hdr & 0x8);
231
- pkt.retain = Boolean(hdr & 0x1);
232
- let qos = pkt.qos = (hdr>>1) & 0x3;
233
-
234
- let rdr = mqtt_reader.of(u8_body);
235
- pkt.topic = rdr.utf8();
236
- if (0 !== qos)
237
- pkt.pkt_id = rdr.u16();
238
-
239
- if (5 <= pkt.mqtt_level)
240
- pkt.props = rdr.props();
241
-
242
- pkt.payload = rdr.flush();
243
- return pkt }
244
- }
245
-
246
- function mqtt_decode_puback(ns, mqtt_reader) {
247
- return ns[0x4] = (pkt, u8_body) => {
248
- let rdr = mqtt_reader.of(u8_body);
249
-
250
- pkt.pkt_id = rdr.u16();
251
- if (5 <= pkt.mqtt_level) {
252
- pkt.reason = rdr.reason(pkt.type);
253
- pkt.props = rdr.props();
254
- }
255
-
256
- return pkt }
257
- }
258
-
259
- function _mqtt_decode_suback(mqtt_reader) {
260
- return (pkt, u8_body) => {
261
- let rdr = mqtt_reader.of(u8_body);
262
-
263
- pkt.pkt_id = rdr.u16();
264
- if (5 <= pkt.mqtt_level)
265
- pkt.props = rdr.props();
266
-
267
- let answers = pkt.answers = [];
268
- while (rdr.has_more())
269
- answers.push(
270
- rdr.reason(pkt.type) );
271
-
272
- return pkt }
273
- }
274
-
275
- function mqtt_decode_suback(ns, mqtt_reader) {
276
- return ns[0x9] = _mqtt_decode_suback(mqtt_reader)
277
- }
278
-
279
- function _suback_v4(mqtt_reader) {
280
- mqtt_reader.reasons('suback',
281
- // MQTT 3.1.1
282
- [ 0x00, 'Granted QoS 0'],
283
- [ 0x01, 'Granted QoS 1'],
284
- [ 0x02, 'Granted QoS 2'],
285
- );
286
- }
287
-
288
- function mqtt_decode_unsuback(ns, mqtt_reader) {
289
- return ns[0xb] = _mqtt_decode_suback(mqtt_reader)
290
- }
291
-
292
- function _unsuback_v4(mqtt_reader) {
293
- mqtt_reader.reasons('unsuback',
294
- // MQTT 3.1.1
295
- [ 0x00, 'Success'],
296
- [ 0x11, 'No subscription existed'],
297
- [ 0x80, 'Unspecified error'],
298
- [ 0x83, 'Implementation specific error'],
299
- [ 0x87, 'Not authorized'],
300
- [ 0x8F, 'Topic Filter invalid'],
301
- [ 0x91, 'Packet Identifier in use'],
302
- );
303
- }
304
-
305
- function mqtt_decode_pingxxx(ns) {
306
- return ns[0xc] = ns[0xd] = pkt => pkt
307
- }
308
-
309
- function mqtt_decode_disconnect(ns, mqtt_reader) {
310
- return ns[0xe] = (pkt, u8_body) => {
311
- if (u8_body && 5 <= pkt.mqtt_level) {
312
- let rdr = mqtt_reader.of(u8_body);
313
- pkt.reason = rdr.reason(pkt.type);
314
- pkt.props = rdr.props();
315
- }
316
- return pkt }
317
- }
318
-
319
204
  function mqtt_encode_connect(ns, mqtt_writer) {
320
205
  const _c_mqtt_proto = new Uint8Array([
321
206
  0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
@@ -334,7 +219,7 @@ function mqtt_encode_connect(ns, mqtt_writer) {
334
219
  | ( will.retain ? 0x20 : 0 );
335
220
 
336
221
  return ns.connect = ( mqtt_level, pkt ) => {
337
- let wrt = mqtt_writer.of(pkt);
222
+ let wrt = mqtt_writer.for(pkt);
338
223
 
339
224
  wrt.push(_c_mqtt_proto);
340
225
  wrt.u8( mqtt_level );
@@ -372,10 +257,27 @@ function mqtt_encode_connect(ns, mqtt_writer) {
372
257
  }
373
258
  }
374
259
 
260
+ function mqtt_decode_connack(ns, mqtt_reader) {
261
+ class _connack_flags_ extends Number {
262
+ get session_present() { return this & 0x01 !== 0 }
263
+ }
264
+
265
+ return ns[0x2] = (pkt, u8_body) => {
266
+ let rdr = mqtt_reader.for(pkt, u8_body);
267
+
268
+ pkt.flags =
269
+ rdr.flags(_connack_flags_);
270
+
271
+ pkt.reason = rdr.reason(pkt.type);
272
+ if (5 <= pkt.mqtt_level)
273
+ pkt.props = rdr.props();
274
+ return pkt }
275
+ }
276
+
375
277
  function mqtt_encode_publish(ns, mqtt_writer) {
376
278
  return ns.publish = ( mqtt_level, pkt ) => {
377
279
  let qos = (pkt.qos & 0x3) << 1;
378
- let wrt = mqtt_writer.of(pkt);
280
+ let wrt = mqtt_writer.for(pkt);
379
281
 
380
282
  wrt.utf8(pkt.topic);
381
283
  if (0 !== qos)
@@ -393,9 +295,28 @@ function mqtt_encode_publish(ns, mqtt_writer) {
393
295
  }
394
296
  }
395
297
 
298
+ function mqtt_decode_publish(ns, mqtt_reader) {
299
+ return ns[0x3] = (pkt, u8_body) => {
300
+ let {hdr} = pkt;
301
+ pkt.dup = Boolean(hdr & 0x8);
302
+ pkt.retain = Boolean(hdr & 0x1);
303
+ let qos = pkt.qos = (hdr>>1) & 0x3;
304
+
305
+ let rdr = mqtt_reader.for(pkt, u8_body);
306
+ pkt.topic = rdr.utf8();
307
+ if (0 !== qos)
308
+ pkt.pkt_id = rdr.u16();
309
+
310
+ if (5 <= pkt.mqtt_level)
311
+ pkt.props = rdr.props();
312
+
313
+ pkt.payload = rdr.flush();
314
+ return pkt }
315
+ }
316
+
396
317
  function mqtt_encode_puback(ns, mqtt_writer) {
397
318
  return ns.puback = ( mqtt_level, pkt ) => {
398
- let wrt = mqtt_writer.of(pkt);
319
+ let wrt = mqtt_writer.for(pkt);
399
320
 
400
321
  wrt.u16(pkt.pkt_id);
401
322
  if (5 <= mqtt_level) {
@@ -407,6 +328,20 @@ function mqtt_encode_puback(ns, mqtt_writer) {
407
328
  }
408
329
  }
409
330
 
331
+
332
+ function mqtt_decode_puback(ns, mqtt_reader) {
333
+ return ns[0x4] = (pkt, u8_body) => {
334
+ let rdr = mqtt_reader.for(pkt, u8_body);
335
+
336
+ pkt.pkt_id = rdr.u16();
337
+ if (5 <= pkt.mqtt_level) {
338
+ pkt.reason = rdr.reason(pkt.type);
339
+ pkt.props = rdr.props();
340
+ }
341
+
342
+ return pkt }
343
+ }
344
+
410
345
  function mqtt_encode_subscribe(ns, mqtt_writer) {
411
346
  const _enc_subscribe_flags = opts => 0
412
347
  | ( opts.qos & 0x3 )
@@ -414,7 +349,7 @@ function mqtt_encode_subscribe(ns, mqtt_writer) {
414
349
  | ( (opts.retain_handling & 0x3) << 2 );
415
350
 
416
351
  return ns.subscribe = ( mqtt_level, pkt ) => {
417
- let wrt = mqtt_writer.of(pkt);
352
+ let wrt = mqtt_writer.for(pkt);
418
353
 
419
354
  wrt.u16(pkt.pkt_id);
420
355
  if (5 <= mqtt_level)
@@ -440,9 +375,25 @@ function mqtt_encode_subscribe(ns, mqtt_writer) {
440
375
  }
441
376
  }
442
377
 
378
+ function mqtt_decode_xxsuback(ns, mqtt_reader) {
379
+ return ns[0x9] = ns[0xb] = (pkt, u8_body) => {
380
+ let rdr = mqtt_reader.for(pkt, u8_body);
381
+
382
+ pkt.pkt_id = rdr.u16();
383
+ if (5 <= pkt.mqtt_level)
384
+ pkt.props = rdr.props();
385
+
386
+ let answers = pkt.answers = [];
387
+ while (rdr.has_more())
388
+ answers.push(
389
+ rdr.reason(pkt.type) );
390
+
391
+ return pkt }
392
+ }
393
+
443
394
  function mqtt_encode_unsubscribe(ns, mqtt_writer) {
444
395
  return ns.unsubscribe = ( mqtt_level, pkt ) => {
445
- let wrt = mqtt_writer.of(pkt);
396
+ let wrt = mqtt_writer.for(pkt);
446
397
 
447
398
  wrt.u16(pkt.pkt_id);
448
399
  if (5 <= mqtt_level)
@@ -460,9 +411,14 @@ function mqtt_encode_pingxxx(ns) {
460
411
  ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
461
412
  }
462
413
 
414
+
415
+ function mqtt_decode_pingxxx(ns) {
416
+ return ns[0xc] = ns[0xd] = pkt => pkt
417
+ }
418
+
463
419
  function mqtt_encode_disconnect(ns, mqtt_writer) {
464
420
  return ns.disconnect = ( mqtt_level, pkt ) => {
465
- let wrt = mqtt_writer.of(pkt);
421
+ let wrt = mqtt_writer.for(pkt);
466
422
 
467
423
  if (pkt && 5 <= mqtt_level) {
468
424
  if (pkt.reason || pkt.props) {
@@ -475,15 +431,25 @@ function mqtt_encode_disconnect(ns, mqtt_writer) {
475
431
  }
476
432
  }
477
433
 
478
- // not a v4 packet: import { mqtt_encode_auth } from './encode/auth.js'
434
+
435
+ function mqtt_decode_disconnect(ns, mqtt_reader) {
436
+ return ns[0xe] = (pkt, u8_body) => {
437
+ if (u8_body && 5 <= pkt.mqtt_level) {
438
+ let rdr = mqtt_reader.for(pkt, u8_body);
439
+ pkt.reason = rdr.reason(pkt.type);
440
+ pkt.props = rdr.props();
441
+ }
442
+ return pkt }
443
+ }
444
+
445
+ // not a v4 packet: import { mqtt_encode_auth, mqtt_decode_auth } from './pkt/auth.js'
479
446
 
480
447
 
481
448
  const mqtt_decode_v4 = [
482
449
  mqtt_decode_connack,
483
450
  mqtt_decode_publish,
484
451
  mqtt_decode_puback,
485
- mqtt_decode_suback,
486
- mqtt_decode_unsuback,
452
+ mqtt_decode_xxsuback,
487
453
  mqtt_decode_pingxxx,
488
454
  mqtt_decode_disconnect,
489
455
  ];
@@ -499,13 +465,6 @@ const mqtt_encode_v4 = [
499
465
  mqtt_encode_disconnect,
500
466
  ];
501
467
 
502
- const mqtt_reader_v4 = /* #__PURE__ */
503
- mqtt_reader_info(
504
- mqtt_reader_v4$1,
505
- _connack_v4,
506
- _suback_v4,
507
- _unsuback_v4,
508
- );
509
468
 
510
469
  const mqtt_opts_v4 =
511
470
  { decode_fns: mqtt_decode_v4,
@@ -711,47 +670,38 @@ function _route_remove(all_route_lists, query) {
711
670
  return true} }
712
671
  return false}
713
672
 
714
- /*
715
- export function decode_varint_loop(u8, i=0) {
716
- let i0 = i
717
- let shift = 0, n = (u8[i] & 0x7f)
718
- while ( 0x80 & u8[i++] )
719
- n |= (u8[i] & 0x7f) << (shift += 7)
673
+ function decode_varint(u8, i0=0, invalid) {
674
+ let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
675
+ for(; b & 0x80;)
676
+ n |= ((b=u8[i++]) & 0x7f) << (shift += 7);
720
677
 
721
- return [n, i, i0]
722
- }
723
- */
724
-
725
-
726
- function decode_varint(u8, i=0) {
727
- let i0 = i;
728
- // unrolled for a max of 4 chains
729
- let n = (u8[i] & 0x7f) << 0;
730
- if ( 0x80 & u8[i++] ) {
731
- n |= (u8[i] & 0x7f) << 7;
732
- if ( 0x80 & u8[i++] ) {
733
- n |= (u8[i] & 0x7f) << 14;
734
- if ( 0x80 & u8[i++] ) {
735
- n |= (u8[i] & 0x7f) << 21;
736
- }
737
- }
738
- }
739
- return [n, i, i0]
678
+ return (u8.length < i)
679
+ ? [invalid, i0, i0] // fail: insuffecient u8 bytes to decode
680
+ : [n, i, i0] // successful value
740
681
  }
741
682
 
742
683
  function mqtt_raw_dispatch(opt) {
743
- let u8 = new Uint8Array(0);
684
+ let u8 = new Uint8Array(0), len_tip=0;
744
685
  return u8_buf => {
745
686
  u8 = 0 === u8.byteLength
746
687
  ? u8_buf : _u8_join(u8, u8_buf);
747
688
 
748
689
  let res = [];
749
- while (1) {
750
- let [len_body, len_vh] = decode_varint(u8, 1);
751
- let len_pkt = len_body + len_vh;
752
690
 
753
- if ( u8.byteLength < len_pkt )
754
- return res
691
+ // wait for at least len_tip bytes for next (tip) message
692
+ while ( u8.byteLength >= len_tip ) {
693
+
694
+ // if varint is incomplete, return len_body=NaN
695
+ let [len_body, len_vh] = decode_varint(u8, 1, NaN);
696
+ let len_pkt = len_body + len_vh; // may be NaN
697
+
698
+ if (!( len_pkt <= u8.byteLength )) {
699
+ // incomplete packet cases:
700
+ // - len_pkt is less than available bytes
701
+ // - len_pkt is NaN from decode_varint() due to lack of data
702
+ len_tip = len_pkt || 0; // 0 when NaN
703
+ break
704
+ }
755
705
 
756
706
  let b0 = u8[0];
757
707
  let u8_body = 0 === len_body ? null
@@ -763,6 +713,8 @@ function mqtt_raw_dispatch(opt) {
763
713
  if (null != pkt)
764
714
  res.push( pkt );
765
715
  }
716
+
717
+ return res
766
718
  }
767
719
  }
768
720
 
@@ -773,16 +725,17 @@ function _u8_join(a, b) {
773
725
  return r
774
726
  }
775
727
 
776
- const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
728
+ const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
729
+
730
+
731
+ function mqtt_pkt_ctx(mqtt_level, opts, pkt_api=opts.pkt_ctx) {
732
+ let _as_pkt_ctx = pkt_api => ({
733
+ __proto__: pkt_api,
734
+ get hdr() { return this.b0 & 0xf },
735
+ get id() { return this.b0 >>> 4 },
736
+ get type() { return _pkt_types[this.b0 >>> 4] },
737
+ mqtt_level });
777
738
 
778
- function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
779
- pkt_ctx = {
780
- __proto__: pkt_ctx || opts.pkt_ctx,
781
- mqtt_level,
782
- get hdr() { return this.b0 & 0xf },
783
- get id() { return this.b0 >>> 4 },
784
- get type() { return _pkt_types[this.b0 >>> 4] },
785
- };
786
739
 
787
740
  let op, _decode_by_id=[], _encode_by_type={};
788
741
  for (op of opts.encode_fns)
@@ -791,10 +744,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
791
744
  op(_decode_by_id, opts.mqtt_reader);
792
745
 
793
746
  return {
794
- pkt_ctx,
747
+ pkt_api, pkt_ctx: _as_pkt_ctx(pkt_api),
795
748
 
796
- encode_pkt(type, pkt) {
797
- return _encode_by_type[type]( mqtt_level, pkt ) },
749
+ encode_pkt: (type, pkt) =>
750
+ _encode_by_type[type]( mqtt_level, pkt ),
798
751
 
799
752
  decode_pkt(b0, u8_body) {
800
753
  if (b0.map) // Uint8Array in first arg
@@ -803,12 +756,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
803
756
  let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
804
757
  return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
805
758
 
806
- mqtt_stream() {
807
- let self = { __proto__: this, pkt_ctx: { __proto__: pkt_ctx } };
808
- self.pkt_ctx._base_ = self.pkt_ctx;
759
+ mqtt_stream(pkt_api=this.pkt_api) {
760
+ let self = { __proto__: this, pkt_ctx: _as_pkt_ctx(pkt_api) };
809
761
  self.decode = mqtt_raw_dispatch(self);
810
- return self
811
- },
762
+ return self },
812
763
  }
813
764
  }
814
765
 
@@ -1332,7 +1283,7 @@ class MQTTCore extends MQTTBase {
1332
1283
 
1333
1284
  return this} }
1334
1285
 
1335
- const version = '0.6.1-web';
1286
+ const version = '0.6.3-web';
1336
1287
 
1337
1288
  const MQTTClient_v4 = /* #__PURE__ */
1338
1289
  with_topic_path_router(