u8-mqtt 0.6.2 → 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 +247 -368
  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 +350 -476
  8. package/cjs/full-v5.cjs.map +1 -1
  9. package/cjs/index.cjs +247 -368
  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 +247 -368
  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 +247 -368
  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 +247 -368
  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 +350 -476
  26. package/esm/deno/full-v5.js.map +1 -1
  27. package/esm/deno/index.js +247 -368
  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 +247 -368
  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 +350 -476
  36. package/esm/full-v5.js.map +1 -1
  37. package/esm/index.js +247 -368
  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 +247 -368
  44. package/esm/node/basic-v5.js.map +1 -1
  45. package/esm/node/basic-v5.mjs +247 -368
  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 +350 -476
  52. package/esm/node/full-v5.js.map +1 -1
  53. package/esm/node/full-v5.mjs +350 -476
  54. package/esm/node/full-v5.mjs.map +1 -1
  55. package/esm/node/index.js +247 -368
  56. package/esm/node/index.js.map +1 -1
  57. package/esm/node/index.mjs +247 -368
  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 +247 -368
  64. package/esm/node/v5.js.map +1 -1
  65. package/esm/node/v5.mjs +247 -368
  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 +247 -368
  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 +247 -368
  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 +350 -476
  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 +247 -368
  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 +247 -368
  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
@@ -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};
110
-
111
- let lut = (proto._reasons_by[pkt_type] ||= new Map());
112
- for (let [u8, reason] of reason_entries)
113
- lut.set( u8, reason );
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);
114
86
 
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)
116
90
  }
117
- };
91
+ warn(msg) {
92
+ let pkt = this.pkt;
93
+ pkt.warn ? pkt.warn(msg, pkt)
94
+ : console.warn('[u8-mqtt-packet] '+msg);
95
+ }
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,
@@ -513,47 +472,38 @@ const mqtt_opts_v4 =
513
472
  encode_fns: mqtt_encode_v4,
514
473
  mqtt_writer: mqtt_writer_v4, };
515
474
 
516
- /*
517
- export function decode_varint_loop(u8, i=0) {
518
- let i0 = i
519
- let shift = 0, n = (u8[i] & 0x7f)
520
- while ( 0x80 & u8[i++] )
521
- n |= (u8[i] & 0x7f) << (shift += 7)
475
+ function decode_varint(u8, i0=0, invalid) {
476
+ let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
477
+ for(; b & 0x80;)
478
+ n |= ((b=u8[i++]) & 0x7f) << (shift += 7);
522
479
 
523
- return [n, i, i0]
524
- }
525
- */
526
-
527
-
528
- function decode_varint(u8, i=0) {
529
- let i0 = i;
530
- // unrolled for a max of 4 chains
531
- let n = (u8[i] & 0x7f) << 0;
532
- if ( 0x80 & u8[i++] ) {
533
- n |= (u8[i] & 0x7f) << 7;
534
- if ( 0x80 & u8[i++] ) {
535
- n |= (u8[i] & 0x7f) << 14;
536
- if ( 0x80 & u8[i++] ) {
537
- n |= (u8[i] & 0x7f) << 21;
538
- }
539
- }
540
- }
541
- return [n, i, i0]
480
+ return (u8.length < i)
481
+ ? [invalid, i0, i0] // fail: insuffecient u8 bytes to decode
482
+ : [n, i, i0] // successful value
542
483
  }
543
484
 
544
485
  function mqtt_raw_dispatch(opt) {
545
- let u8 = new Uint8Array(0);
486
+ let u8 = new Uint8Array(0), len_tip=0;
546
487
  return u8_buf => {
547
488
  u8 = 0 === u8.byteLength
548
489
  ? u8_buf : _u8_join(u8, u8_buf);
549
490
 
550
491
  let res = [];
551
- while (1) {
552
- let [len_body, len_vh] = decode_varint(u8, 1);
553
- let len_pkt = len_body + len_vh;
554
492
 
555
- if ( u8.byteLength < len_pkt )
556
- return res
493
+ // wait for at least len_tip bytes for next (tip) message
494
+ while ( u8.byteLength >= len_tip ) {
495
+
496
+ // if varint is incomplete, return len_body=NaN
497
+ let [len_body, len_vh] = decode_varint(u8, 1, NaN);
498
+ let len_pkt = len_body + len_vh; // may be NaN
499
+
500
+ if (!( len_pkt <= u8.byteLength )) {
501
+ // incomplete packet cases:
502
+ // - len_pkt is less than available bytes
503
+ // - len_pkt is NaN from decode_varint() due to lack of data
504
+ len_tip = len_pkt || 0; // 0 when NaN
505
+ break
506
+ }
557
507
 
558
508
  let b0 = u8[0];
559
509
  let u8_body = 0 === len_body ? null
@@ -565,6 +515,8 @@ function mqtt_raw_dispatch(opt) {
565
515
  if (null != pkt)
566
516
  res.push( pkt );
567
517
  }
518
+
519
+ return res
568
520
  }
569
521
  }
570
522
 
@@ -575,16 +527,17 @@ function _u8_join(a, b) {
575
527
  return r
576
528
  }
577
529
 
578
- const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
530
+ const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
531
+
532
+
533
+ function mqtt_pkt_ctx(mqtt_level, opts, pkt_api=opts.pkt_ctx) {
534
+ let _as_pkt_ctx = pkt_api => ({
535
+ __proto__: pkt_api,
536
+ get hdr() { return this.b0 & 0xf },
537
+ get id() { return this.b0 >>> 4 },
538
+ get type() { return _pkt_types[this.b0 >>> 4] },
539
+ mqtt_level });
579
540
 
580
- function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
581
- pkt_ctx = {
582
- __proto__: pkt_ctx || opts.pkt_ctx,
583
- mqtt_level,
584
- get hdr() { return this.b0 & 0xf },
585
- get id() { return this.b0 >>> 4 },
586
- get type() { return _pkt_types[this.b0 >>> 4] },
587
- };
588
541
 
589
542
  let op, _decode_by_id=[], _encode_by_type={};
590
543
  for (op of opts.encode_fns)
@@ -593,10 +546,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
593
546
  op(_decode_by_id, opts.mqtt_reader);
594
547
 
595
548
  return {
596
- pkt_ctx,
549
+ pkt_api, pkt_ctx: _as_pkt_ctx(pkt_api),
597
550
 
598
- encode_pkt(type, pkt) {
599
- return _encode_by_type[type]( mqtt_level, pkt ) },
551
+ encode_pkt: (type, pkt) =>
552
+ _encode_by_type[type]( mqtt_level, pkt ),
600
553
 
601
554
  decode_pkt(b0, u8_body) {
602
555
  if (b0.map) // Uint8Array in first arg
@@ -605,12 +558,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
605
558
  let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
606
559
  return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
607
560
 
608
- mqtt_stream() {
609
- let self = { __proto__: this, pkt_ctx: { __proto__: pkt_ctx } };
610
- self.pkt_ctx._base_ = self.pkt_ctx;
561
+ mqtt_stream(pkt_api=this.pkt_api) {
562
+ let self = { __proto__: this, pkt_ctx: _as_pkt_ctx(pkt_api) };
611
563
  self.decode = mqtt_raw_dispatch(self);
612
- return self
613
- },
564
+ return self },
614
565
  }
615
566
  }
616
567
 
@@ -1151,7 +1102,7 @@ class MQTTCore extends MQTTBase {
1151
1102
 
1152
1103
  return this} }
1153
1104
 
1154
- const version = '0.6.2-web';
1105
+ const version = '0.6.3-web';
1155
1106
 
1156
1107
  const MQTTClient_v4 = /* #__PURE__ */
1157
1108
  MQTTCore.mqtt_ctx(4, mqtt_opts_v4);