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};
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,234 +201,6 @@ class mqtt_writer_v4 {
194
201
  }
195
202
  }
196
203
 
197
- function mqtt_decode_connect(ns, mqtt_reader) {
198
- class _connect_flags_ extends Number {
199
- get reserved() { return this & 0x01 !== 0 }
200
- get clean_start() { return this & 0x02 !== 0 }
201
- get will_flag() { return this & 0x04 !== 0 }
202
- get will_qos() { return (this >>> 3) & 0x3 }
203
- get will_retain() { return this & 0x20 !== 0 }
204
- get password() { return this & 0x40 !== 0 }
205
- get username() { return this & 0x80 !== 0 }
206
- }
207
-
208
- return ns[0x1] = (pkt, u8_body) => {
209
- let rdr = mqtt_reader.of(u8_body);
210
- if ('MQTT' !== rdr.utf8())
211
- throw new Error('Invalid mqtt_connect packet')
212
-
213
- pkt._base_.mqtt_level = pkt.mqtt_level = rdr.u8();
214
-
215
- let flags = pkt.flags =
216
- rdr.flags(_connect_flags_);
217
-
218
- pkt.keep_alive = rdr.u16();
219
-
220
- if (5 <= pkt.mqtt_level)
221
- pkt.props = rdr.props();
222
-
223
-
224
-
225
- pkt.client_id = rdr.utf8();
226
- if (flags.will_flag) {
227
- let will = pkt.will = {};
228
- if (5 <= pkt.mqtt_level)
229
- will.props = rdr.props();
230
-
231
- will.topic = rdr.utf8();
232
- will.payload = rdr.bin();
233
- will.qos = flags.will_qos;
234
- will.retain = flags.will_retain;
235
- }
236
-
237
- if (flags.username)
238
- pkt.username = rdr.utf8();
239
-
240
- if (flags.password)
241
- pkt.password = rdr.bin();
242
- return pkt }
243
- }
244
-
245
- function mqtt_decode_connack(ns, mqtt_reader) {
246
- class _connack_flags_ extends Number {
247
- get session_present() { return this & 0x01 !== 0 }
248
- }
249
-
250
- return ns[0x2] = (pkt, u8_body) => {
251
- let rdr = mqtt_reader.of(u8_body);
252
-
253
- pkt.flags =
254
- rdr.flags(_connack_flags_);
255
-
256
- pkt.reason = rdr.reason(pkt.type);
257
- if (5 <= pkt.mqtt_level)
258
- pkt.props = rdr.props();
259
- return pkt }
260
- }
261
-
262
-
263
- function _connack_v4(mqtt_reader) {
264
- mqtt_reader.reasons('connack',
265
- // MQTT 3.1.1
266
- [ 0x00, 'Success'],
267
- [ 0x01, 'Connection refused, unacceptable protocol version'],
268
- [ 0x02, 'Connection refused, identifier rejected'],
269
- [ 0x03, 'Connection refused, server unavailable'],
270
- [ 0x04, 'Connection refused, bad user name or password'],
271
- [ 0x05, 'Connection refused, not authorized'],
272
- );
273
- }
274
-
275
- function mqtt_decode_publish(ns, mqtt_reader) {
276
- return ns[0x3] = (pkt, u8_body) => {
277
- let {hdr} = pkt;
278
- pkt.dup = Boolean(hdr & 0x8);
279
- pkt.retain = Boolean(hdr & 0x1);
280
- let qos = pkt.qos = (hdr>>1) & 0x3;
281
-
282
- let rdr = mqtt_reader.of(u8_body);
283
- pkt.topic = rdr.utf8();
284
- if (0 !== qos)
285
- pkt.pkt_id = rdr.u16();
286
-
287
- if (5 <= pkt.mqtt_level)
288
- pkt.props = rdr.props();
289
-
290
- pkt.payload = rdr.flush();
291
- return pkt }
292
- }
293
-
294
- function mqtt_decode_puback(ns, mqtt_reader) {
295
- return ns[0x4] = (pkt, u8_body) => {
296
- let rdr = mqtt_reader.of(u8_body);
297
-
298
- pkt.pkt_id = rdr.u16();
299
- if (5 <= pkt.mqtt_level) {
300
- pkt.reason = rdr.reason(pkt.type);
301
- pkt.props = rdr.props();
302
- }
303
-
304
- return pkt }
305
- }
306
-
307
- function mqtt_decode_pubxxx(ns, mqtt_reader) {
308
- return ns[0x5] = ns[0x6] = ns[0x7] = (pkt, u8_body) => {
309
- let rdr = mqtt_reader.of(u8_body);
310
-
311
- pkt.pkt_id = rdr.u16();
312
- pkt.reason = rdr.reason('pubxxx', mqtt_reader);
313
- if (5 <= pkt.mqtt_level)
314
- pkt.props = rdr.props();
315
- return pkt }
316
- }
317
-
318
- function _pubxxx_v4(mqtt_reader) {
319
- mqtt_reader.reasons('pubxxx',
320
- // MQTT 3.1.1
321
- [ 0x00, 'Success' ],
322
- [ 0x92, 'Packet Identifier not found' ],
323
- );
324
- }
325
-
326
- function mqtt_decode_subscribe(ns, mqtt_reader) {
327
- class _subscription_options_ extends Number {
328
- get qos() { return this & 0x3 }
329
- get retain() { return this & 0x4 !== 0 }
330
- get retain_handling() { return (this >> 2) & 0x3 }
331
- }
332
-
333
- return ns[0x8] = (pkt, u8_body) => {
334
- let rdr = mqtt_reader.of(u8_body);
335
-
336
- pkt.pkt_id = rdr.u16();
337
- if (5 <= pkt.mqtt_level)
338
- pkt.props = rdr.props();
339
-
340
- let topic, opts, topic_list = pkt.topics = [];
341
- while (rdr.has_more()) {
342
- topic = rdr.utf8();
343
- opts = rdr.flags(_subscription_options_);
344
- topic_list.push({topic, opts});
345
- }
346
-
347
- return pkt }
348
- }
349
-
350
- function _mqtt_decode_suback(mqtt_reader) {
351
- return (pkt, u8_body) => {
352
- let rdr = mqtt_reader.of(u8_body);
353
-
354
- pkt.pkt_id = rdr.u16();
355
- if (5 <= pkt.mqtt_level)
356
- pkt.props = rdr.props();
357
-
358
- let answers = pkt.answers = [];
359
- while (rdr.has_more())
360
- answers.push(
361
- rdr.reason(pkt.type) );
362
-
363
- return pkt }
364
- }
365
-
366
- function mqtt_decode_suback(ns, mqtt_reader) {
367
- return ns[0x9] = _mqtt_decode_suback(mqtt_reader)
368
- }
369
-
370
- function _suback_v4(mqtt_reader) {
371
- mqtt_reader.reasons('suback',
372
- // MQTT 3.1.1
373
- [ 0x00, 'Granted QoS 0'],
374
- [ 0x01, 'Granted QoS 1'],
375
- [ 0x02, 'Granted QoS 2'],
376
- );
377
- }
378
-
379
- function mqtt_decode_unsubscribe(ns, mqtt_reader) {
380
- return ns[0xa] = (pkt, u8_body) => {
381
- let rdr = mqtt_reader.of(u8_body);
382
-
383
- pkt.pkt_id = rdr.u16();
384
- if (5 <= pkt.mqtt_level)
385
- pkt.props = rdr.props();
386
-
387
- let topic_list = pkt.topics = [];
388
- while (rdr.has_more())
389
- topic_list.push(rdr.utf8());
390
-
391
- return pkt }
392
- }
393
-
394
- function mqtt_decode_unsuback(ns, mqtt_reader) {
395
- return ns[0xb] = _mqtt_decode_suback(mqtt_reader)
396
- }
397
-
398
- function _unsuback_v4(mqtt_reader) {
399
- mqtt_reader.reasons('unsuback',
400
- // MQTT 3.1.1
401
- [ 0x00, 'Success'],
402
- [ 0x11, 'No subscription existed'],
403
- [ 0x80, 'Unspecified error'],
404
- [ 0x83, 'Implementation specific error'],
405
- [ 0x87, 'Not authorized'],
406
- [ 0x8F, 'Topic Filter invalid'],
407
- [ 0x91, 'Packet Identifier in use'],
408
- );
409
- }
410
-
411
- function mqtt_decode_pingxxx(ns) {
412
- return ns[0xc] = ns[0xd] = pkt => pkt
413
- }
414
-
415
- function mqtt_decode_disconnect(ns, mqtt_reader) {
416
- return ns[0xe] = (pkt, u8_body) => {
417
- if (u8_body && 5 <= pkt.mqtt_level) {
418
- let rdr = mqtt_reader.of(u8_body);
419
- pkt.reason = rdr.reason(pkt.type);
420
- pkt.props = rdr.props();
421
- }
422
- return pkt }
423
- }
424
-
425
204
  function mqtt_encode_connect(ns, mqtt_writer) {
426
205
  const _c_mqtt_proto = new Uint8Array([
427
206
  0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
@@ -440,7 +219,7 @@ function mqtt_encode_connect(ns, mqtt_writer) {
440
219
  | ( will.retain ? 0x20 : 0 );
441
220
 
442
221
  return ns.connect = ( mqtt_level, pkt ) => {
443
- let wrt = mqtt_writer.of(pkt);
222
+ let wrt = mqtt_writer.for(pkt);
444
223
 
445
224
  wrt.push(_c_mqtt_proto);
446
225
  wrt.u8( mqtt_level );
@@ -478,12 +257,61 @@ function mqtt_encode_connect(ns, mqtt_writer) {
478
257
  }
479
258
  }
480
259
 
260
+ function mqtt_decode_connect(ns, mqtt_reader) {
261
+ class _connect_flags_ extends Number {
262
+ get reserved() { return this & 0x01 !== 0 }
263
+ get clean_start() { return this & 0x02 !== 0 }
264
+ get will_flag() { return this & 0x04 !== 0 }
265
+ get will_qos() { return (this >>> 3) & 0x3 }
266
+ get will_retain() { return this & 0x20 !== 0 }
267
+ get password() { return this & 0x40 !== 0 }
268
+ get username() { return this & 0x80 !== 0 }
269
+ }
270
+
271
+ return ns[0x1] = (pkt, u8_body) => {
272
+ let rdr = mqtt_reader.for(pkt, u8_body);
273
+ if ('MQTT' !== rdr.utf8())
274
+ throw new Error('Invalid mqtt_connect packet')
275
+
276
+ pkt.__proto__.mqtt_level =
277
+ pkt.mqtt_level = rdr.u8();
278
+
279
+ let flags = pkt.flags =
280
+ rdr.flags(_connect_flags_);
281
+
282
+ pkt.keep_alive = rdr.u16();
283
+
284
+ if (5 <= pkt.mqtt_level)
285
+ pkt.props = rdr.props();
286
+
287
+
288
+
289
+ pkt.client_id = rdr.utf8();
290
+ if (flags.will_flag) {
291
+ let will = pkt.will = {};
292
+ if (5 <= pkt.mqtt_level)
293
+ will.props = rdr.props();
294
+
295
+ will.topic = rdr.utf8();
296
+ will.payload = rdr.bin();
297
+ will.qos = flags.will_qos;
298
+ will.retain = flags.will_retain;
299
+ }
300
+
301
+ if (flags.username)
302
+ pkt.username = rdr.utf8();
303
+
304
+ if (flags.password)
305
+ pkt.password = rdr.bin();
306
+ return pkt }
307
+ }
308
+
481
309
  function mqtt_encode_connack(ns, mqtt_writer) {
482
310
  const _enc_flags_connack = flags =>
483
311
  flags.session_present ? 1 : 0;
484
312
 
485
313
  return ns.connack = (mqtt_level, pkt) => {
486
- let wrt = mqtt_writer.of(pkt);
314
+ let wrt = mqtt_writer.for(pkt);
487
315
 
488
316
  wrt.flags( pkt.flags, _enc_flags_connack );
489
317
 
@@ -499,10 +327,28 @@ function mqtt_encode_connack(ns, mqtt_writer) {
499
327
  }
500
328
  }
501
329
 
330
+
331
+ function mqtt_decode_connack(ns, mqtt_reader) {
332
+ class _connack_flags_ extends Number {
333
+ get session_present() { return this & 0x01 !== 0 }
334
+ }
335
+
336
+ return ns[0x2] = (pkt, u8_body) => {
337
+ let rdr = mqtt_reader.for(pkt, u8_body);
338
+
339
+ pkt.flags =
340
+ rdr.flags(_connack_flags_);
341
+
342
+ pkt.reason = rdr.reason(pkt.type);
343
+ if (5 <= pkt.mqtt_level)
344
+ pkt.props = rdr.props();
345
+ return pkt }
346
+ }
347
+
502
348
  function mqtt_encode_publish(ns, mqtt_writer) {
503
349
  return ns.publish = ( mqtt_level, pkt ) => {
504
350
  let qos = (pkt.qos & 0x3) << 1;
505
- let wrt = mqtt_writer.of(pkt);
351
+ let wrt = mqtt_writer.for(pkt);
506
352
 
507
353
  wrt.utf8(pkt.topic);
508
354
  if (0 !== qos)
@@ -520,9 +366,28 @@ function mqtt_encode_publish(ns, mqtt_writer) {
520
366
  }
521
367
  }
522
368
 
369
+ function mqtt_decode_publish(ns, mqtt_reader) {
370
+ return ns[0x3] = (pkt, u8_body) => {
371
+ let {hdr} = pkt;
372
+ pkt.dup = Boolean(hdr & 0x8);
373
+ pkt.retain = Boolean(hdr & 0x1);
374
+ let qos = pkt.qos = (hdr>>1) & 0x3;
375
+
376
+ let rdr = mqtt_reader.for(pkt, u8_body);
377
+ pkt.topic = rdr.utf8();
378
+ if (0 !== qos)
379
+ pkt.pkt_id = rdr.u16();
380
+
381
+ if (5 <= pkt.mqtt_level)
382
+ pkt.props = rdr.props();
383
+
384
+ pkt.payload = rdr.flush();
385
+ return pkt }
386
+ }
387
+
523
388
  function mqtt_encode_puback(ns, mqtt_writer) {
524
389
  return ns.puback = ( mqtt_level, pkt ) => {
525
- let wrt = mqtt_writer.of(pkt);
390
+ let wrt = mqtt_writer.for(pkt);
526
391
 
527
392
  wrt.u16(pkt.pkt_id);
528
393
  if (5 <= mqtt_level) {
@@ -534,6 +399,20 @@ function mqtt_encode_puback(ns, mqtt_writer) {
534
399
  }
535
400
  }
536
401
 
402
+
403
+ function mqtt_decode_puback(ns, mqtt_reader) {
404
+ return ns[0x4] = (pkt, u8_body) => {
405
+ let rdr = mqtt_reader.for(pkt, u8_body);
406
+
407
+ pkt.pkt_id = rdr.u16();
408
+ if (5 <= pkt.mqtt_level) {
409
+ pkt.reason = rdr.reason(pkt.type);
410
+ pkt.props = rdr.props();
411
+ }
412
+
413
+ return pkt }
414
+ }
415
+
537
416
  function mqtt_encode_pubxxx(ns, mqtt_writer) {
538
417
  ns.pubrec = _enc_pubxxx(0x50);
539
418
  ns.pubrel = _enc_pubxxx(0x62);
@@ -542,7 +421,7 @@ function mqtt_encode_pubxxx(ns, mqtt_writer) {
542
421
 
543
422
  function _enc_pubxxx(hdr) {
544
423
  return ( mqtt_level, pkt ) => {
545
- let wrt = mqtt_writer.of(pkt);
424
+ let wrt = mqtt_writer.for(pkt);
546
425
 
547
426
  wrt.u16(pkt.pkt_id);
548
427
  if (5 <= mqtt_level) {
@@ -558,6 +437,17 @@ function mqtt_encode_pubxxx(ns, mqtt_writer) {
558
437
  }
559
438
  }
560
439
 
440
+ function mqtt_decode_pubxxx(ns, mqtt_reader) {
441
+ return ns[0x5] = ns[0x6] = ns[0x7] = (pkt, u8_body) => {
442
+ let rdr = mqtt_reader.for(pkt, u8_body);
443
+
444
+ pkt.pkt_id = rdr.u16();
445
+ pkt.reason = rdr.reason('pubxxx', mqtt_reader);
446
+ if (5 <= pkt.mqtt_level)
447
+ pkt.props = rdr.props();
448
+ return pkt }
449
+ }
450
+
561
451
  function mqtt_encode_subscribe(ns, mqtt_writer) {
562
452
  const _enc_subscribe_flags = opts => 0
563
453
  | ( opts.qos & 0x3 )
@@ -565,7 +455,7 @@ function mqtt_encode_subscribe(ns, mqtt_writer) {
565
455
  | ( (opts.retain_handling & 0x3) << 2 );
566
456
 
567
457
  return ns.subscribe = ( mqtt_level, pkt ) => {
568
- let wrt = mqtt_writer.of(pkt);
458
+ let wrt = mqtt_writer.for(pkt);
569
459
 
570
460
  wrt.u16(pkt.pkt_id);
571
461
  if (5 <= mqtt_level)
@@ -591,6 +481,30 @@ function mqtt_encode_subscribe(ns, mqtt_writer) {
591
481
  }
592
482
  }
593
483
 
484
+ function mqtt_decode_subscribe(ns, mqtt_reader) {
485
+ class _subscription_options_ extends Number {
486
+ get qos() { return this & 0x3 }
487
+ get retain() { return this & 0x4 !== 0 }
488
+ get retain_handling() { return (this >> 2) & 0x3 }
489
+ }
490
+
491
+ return ns[0x8] = (pkt, u8_body) => {
492
+ let rdr = mqtt_reader.for(pkt, u8_body);
493
+
494
+ pkt.pkt_id = rdr.u16();
495
+ if (5 <= pkt.mqtt_level)
496
+ pkt.props = rdr.props();
497
+
498
+ let topic, opts, topic_list = pkt.topics = [];
499
+ while (rdr.has_more()) {
500
+ topic = rdr.utf8();
501
+ opts = rdr.flags(_subscription_options_);
502
+ topic_list.push({topic, opts});
503
+ }
504
+
505
+ return pkt }
506
+ }
507
+
594
508
  function mqtt_encode_xxsuback(ns, mqtt_writer) {
595
509
  ns.suback = _enc_xxsuback(0x90);
596
510
  ns.unsuback = _enc_xxsuback(0xb0);
@@ -598,7 +512,7 @@ function mqtt_encode_xxsuback(ns, mqtt_writer) {
598
512
 
599
513
  function _enc_xxsuback(hdr) {
600
514
  return ( mqtt_level, pkt ) => {
601
- let wrt = mqtt_writer.of(pkt);
515
+ let wrt = mqtt_writer.for(pkt);
602
516
 
603
517
  wrt.u16(pkt.pkt_id);
604
518
  if (5 <= mqtt_level)
@@ -612,9 +526,26 @@ function mqtt_encode_xxsuback(ns, mqtt_writer) {
612
526
  }
613
527
  }
614
528
 
529
+
530
+ function mqtt_decode_xxsuback(ns, mqtt_reader) {
531
+ return ns[0x9] = ns[0xb] = (pkt, u8_body) => {
532
+ let rdr = mqtt_reader.for(pkt, u8_body);
533
+
534
+ pkt.pkt_id = rdr.u16();
535
+ if (5 <= pkt.mqtt_level)
536
+ pkt.props = rdr.props();
537
+
538
+ let answers = pkt.answers = [];
539
+ while (rdr.has_more())
540
+ answers.push(
541
+ rdr.reason(pkt.type) );
542
+
543
+ return pkt }
544
+ }
545
+
615
546
  function mqtt_encode_unsubscribe(ns, mqtt_writer) {
616
547
  return ns.unsubscribe = ( mqtt_level, pkt ) => {
617
- let wrt = mqtt_writer.of(pkt);
548
+ let wrt = mqtt_writer.for(pkt);
618
549
 
619
550
  wrt.u16(pkt.pkt_id);
620
551
  if (5 <= mqtt_level)
@@ -627,14 +558,34 @@ function mqtt_encode_unsubscribe(ns, mqtt_writer) {
627
558
  }
628
559
  }
629
560
 
561
+ function mqtt_decode_unsubscribe(ns, mqtt_reader) {
562
+ return ns[0xa] = (pkt, u8_body) => {
563
+ let rdr = mqtt_reader.for(pkt, u8_body);
564
+
565
+ pkt.pkt_id = rdr.u16();
566
+ if (5 <= pkt.mqtt_level)
567
+ pkt.props = rdr.props();
568
+
569
+ let topic_list = pkt.topics = [];
570
+ while (rdr.has_more())
571
+ topic_list.push(rdr.utf8());
572
+
573
+ return pkt }
574
+ }
575
+
630
576
  function mqtt_encode_pingxxx(ns) {
631
577
  ns.pingreq = () => new Uint8Array([ 0xc0, 0 ]);
632
578
  ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
633
579
  }
634
580
 
581
+
582
+ function mqtt_decode_pingxxx(ns) {
583
+ return ns[0xc] = ns[0xd] = pkt => pkt
584
+ }
585
+
635
586
  function mqtt_encode_disconnect(ns, mqtt_writer) {
636
587
  return ns.disconnect = ( mqtt_level, pkt ) => {
637
- let wrt = mqtt_writer.of(pkt);
588
+ let wrt = mqtt_writer.for(pkt);
638
589
 
639
590
  if (pkt && 5 <= mqtt_level) {
640
591
  if (pkt.reason || pkt.props) {
@@ -647,17 +598,19 @@ function mqtt_encode_disconnect(ns, mqtt_writer) {
647
598
  }
648
599
  }
649
600
 
650
- // not a v4 packet: import { mqtt_encode_auth } from './encode/auth.js'
651
601
 
602
+ function mqtt_decode_disconnect(ns, mqtt_reader) {
603
+ return ns[0xe] = (pkt, u8_body) => {
604
+ if (u8_body && 5 <= pkt.mqtt_level) {
605
+ let rdr = mqtt_reader.for(pkt, u8_body);
606
+ pkt.reason = rdr.reason(pkt.type);
607
+ pkt.props = rdr.props();
608
+ }
609
+ return pkt }
610
+ }
611
+
612
+ // not a v4 packet: import { mqtt_encode_auth, mqtt_decode_auth } from './pkt/auth.js'
652
613
 
653
- const mqtt_reader_v4 = /* #__PURE__ */
654
- mqtt_reader_info(
655
- mqtt_reader_v4$1,
656
- _connack_v4,
657
- _pubxxx_v4,
658
- _suback_v4,
659
- _unsuback_v4,
660
- );
661
614
 
662
615
  const mqtt_decode_v4 = [
663
616
  mqtt_decode_connect,
@@ -666,9 +619,8 @@ const mqtt_decode_v4 = [
666
619
  mqtt_decode_puback,
667
620
  mqtt_decode_pubxxx,
668
621
  mqtt_decode_subscribe,
669
- mqtt_decode_suback,
622
+ mqtt_decode_xxsuback,
670
623
  mqtt_decode_unsubscribe,
671
- mqtt_decode_unsuback,
672
624
  mqtt_decode_pingxxx,
673
625
  mqtt_decode_disconnect,
674
626
  ];
@@ -892,47 +844,38 @@ function _route_remove(all_route_lists, query) {
892
844
  return true} }
893
845
  return false}
894
846
 
895
- /*
896
- export function decode_varint_loop(u8, i=0) {
897
- let i0 = i
898
- let shift = 0, n = (u8[i] & 0x7f)
899
- while ( 0x80 & u8[i++] )
900
- n |= (u8[i] & 0x7f) << (shift += 7)
847
+ function decode_varint(u8, i0=0, invalid) {
848
+ let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
849
+ for(; b & 0x80;)
850
+ n |= ((b=u8[i++]) & 0x7f) << (shift += 7);
901
851
 
902
- return [n, i, i0]
903
- }
904
- */
905
-
906
-
907
- function decode_varint(u8, i=0) {
908
- let i0 = i;
909
- // unrolled for a max of 4 chains
910
- let n = (u8[i] & 0x7f) << 0;
911
- if ( 0x80 & u8[i++] ) {
912
- n |= (u8[i] & 0x7f) << 7;
913
- if ( 0x80 & u8[i++] ) {
914
- n |= (u8[i] & 0x7f) << 14;
915
- if ( 0x80 & u8[i++] ) {
916
- n |= (u8[i] & 0x7f) << 21;
917
- }
918
- }
919
- }
920
- return [n, i, i0]
852
+ return (u8.length < i)
853
+ ? [invalid, i0, i0] // fail: insuffecient u8 bytes to decode
854
+ : [n, i, i0] // successful value
921
855
  }
922
856
 
923
857
  function mqtt_raw_dispatch(opt) {
924
- let u8 = new Uint8Array(0);
858
+ let u8 = new Uint8Array(0), len_tip=0;
925
859
  return u8_buf => {
926
860
  u8 = 0 === u8.byteLength
927
861
  ? u8_buf : _u8_join(u8, u8_buf);
928
862
 
929
863
  let res = [];
930
- while (1) {
931
- let [len_body, len_vh] = decode_varint(u8, 1);
932
- let len_pkt = len_body + len_vh;
933
864
 
934
- if ( u8.byteLength < len_pkt )
935
- return res
865
+ // wait for at least len_tip bytes for next (tip) message
866
+ while ( u8.byteLength >= len_tip ) {
867
+
868
+ // if varint is incomplete, return len_body=NaN
869
+ let [len_body, len_vh] = decode_varint(u8, 1, NaN);
870
+ let len_pkt = len_body + len_vh; // may be NaN
871
+
872
+ if (!( len_pkt <= u8.byteLength )) {
873
+ // incomplete packet cases:
874
+ // - len_pkt is less than available bytes
875
+ // - len_pkt is NaN from decode_varint() due to lack of data
876
+ len_tip = len_pkt || 0; // 0 when NaN
877
+ break
878
+ }
936
879
 
937
880
  let b0 = u8[0];
938
881
  let u8_body = 0 === len_body ? null
@@ -944,6 +887,8 @@ function mqtt_raw_dispatch(opt) {
944
887
  if (null != pkt)
945
888
  res.push( pkt );
946
889
  }
890
+
891
+ return res
947
892
  }
948
893
  }
949
894
 
@@ -954,16 +899,17 @@ function _u8_join(a, b) {
954
899
  return r
955
900
  }
956
901
 
957
- const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
902
+ const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
903
+
904
+
905
+ function mqtt_pkt_ctx(mqtt_level, opts, pkt_api=opts.pkt_ctx) {
906
+ let _as_pkt_ctx = pkt_api => ({
907
+ __proto__: pkt_api,
908
+ get hdr() { return this.b0 & 0xf },
909
+ get id() { return this.b0 >>> 4 },
910
+ get type() { return _pkt_types[this.b0 >>> 4] },
911
+ mqtt_level });
958
912
 
959
- function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
960
- pkt_ctx = {
961
- __proto__: pkt_ctx || opts.pkt_ctx,
962
- mqtt_level,
963
- get hdr() { return this.b0 & 0xf },
964
- get id() { return this.b0 >>> 4 },
965
- get type() { return _pkt_types[this.b0 >>> 4] },
966
- };
967
913
 
968
914
  let op, _decode_by_id=[], _encode_by_type={};
969
915
  for (op of opts.encode_fns)
@@ -972,10 +918,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
972
918
  op(_decode_by_id, opts.mqtt_reader);
973
919
 
974
920
  return {
975
- pkt_ctx,
921
+ pkt_api, pkt_ctx: _as_pkt_ctx(pkt_api),
976
922
 
977
- encode_pkt(type, pkt) {
978
- return _encode_by_type[type]( mqtt_level, pkt ) },
923
+ encode_pkt: (type, pkt) =>
924
+ _encode_by_type[type]( mqtt_level, pkt ),
979
925
 
980
926
  decode_pkt(b0, u8_body) {
981
927
  if (b0.map) // Uint8Array in first arg
@@ -984,12 +930,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
984
930
  let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
985
931
  return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
986
932
 
987
- mqtt_stream() {
988
- let self = { __proto__: this, pkt_ctx: { __proto__: pkt_ctx } };
989
- self.pkt_ctx._base_ = self.pkt_ctx;
933
+ mqtt_stream(pkt_api=this.pkt_api) {
934
+ let self = { __proto__: this, pkt_ctx: _as_pkt_ctx(pkt_api) };
990
935
  self.decode = mqtt_raw_dispatch(self);
991
- return self
992
- },
936
+ return self },
993
937
  }
994
938
  }
995
939
 
@@ -1513,7 +1457,7 @@ class MQTTCore extends MQTTBase {
1513
1457
 
1514
1458
  return this} }
1515
1459
 
1516
- const version = '0.6.2-web';
1460
+ const version = '0.6.3-web';
1517
1461
 
1518
1462
  const MQTTClient_v4 = /* #__PURE__ */
1519
1463
  with_topic_path_router(