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
@@ -2,50 +2,33 @@ import { connect } from 'node:net';
2
2
  import { connect as connect$1 } from 'node:tls';
3
3
 
4
4
  function encode_varint(n, a=[]) {
5
- do {
6
- const ni = n & 0x7f;
7
- n >>>= 7;
8
- a.push( ni | (0===n ? 0 : 0x80) );
9
- } while (n > 0)
5
+ a.push((n<0x80 ? 0 : 0x80) | (n & 0x7f));
6
+ for(; ( n>>>=7 ) > 0 ;)
7
+ a.push((n<0x80 ? 0 : 0x80) | (n & 0x7f));
10
8
  return a
11
9
  }
12
10
 
11
+ function decode_varint$1(u8, i0=0, invalid) {
12
+ let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
13
+ for(; b & 0x80;)
14
+ n |= ((b=u8[i++]) & 0x7f) << (shift += 7);
13
15
 
14
- /*
15
- export function decode_varint_loop(u8, i=0) {
16
- let i0 = i
17
- let shift = 0, n = (u8[i] & 0x7f)
18
- while ( 0x80 & u8[i++] )
19
- n |= (u8[i] & 0x7f) << (shift += 7)
20
-
21
- return [n, i, i0]
16
+ return (u8.length < i)
17
+ ? [invalid, i0, i0] // fail: insuffecient u8 bytes to decode
18
+ : [n, i, i0] // successful value
22
19
  }
23
- */
24
-
25
-
26
- function decode_varint$1(u8, i=0) {
27
- let i0 = i;
28
- // unrolled for a max of 4 chains
29
- let n = (u8[i] & 0x7f) << 0;
30
- if ( 0x80 & u8[i++] ) {
31
- n |= (u8[i] & 0x7f) << 7;
32
- if ( 0x80 & u8[i++] ) {
33
- n |= (u8[i] & 0x7f) << 14;
34
- if ( 0x80 & u8[i++] ) {
35
- n |= (u8[i] & 0x7f) << 21;
36
- }
37
- }
20
+
21
+ function add_mqtt_props(mqtt_props, entries) {
22
+ for (let [id, type, name, obj] of entries) {
23
+ obj = {id, type, name, ...obj};
24
+ mqtt_props.set(id, obj).set(name, obj);
38
25
  }
39
- return [n, i, i0]
26
+ return mqtt_props
40
27
  }
41
28
 
42
- const mqtt_props = /* #__PURE__ */
43
- init_mqtt_props();
44
-
45
- function init_mqtt_props() {
46
- let mqtt_props = new Map();
47
29
 
48
- let entries = [
30
+ const init_mqtt_props = () =>
31
+ add_mqtt_props(new Map(), [
49
32
  [ 0x01, 'u8', 'payload_format_indicator'],
50
33
  [ 0x02, 'u32', 'message_expiry_interval'],
51
34
  [ 0x03, 'utf8', 'content_type'],
@@ -73,30 +56,24 @@ function init_mqtt_props() {
73
56
  [ 0x28, 'u8', 'wildcard_subscription_available'],
74
57
  [ 0x29, 'u8', 'subscription_identifiers_available', {op: 'u8_vec'}],
75
58
  [ 0x2A, 'u8', 'shared_subscription_available'],
76
- ];
59
+ ]);
77
60
 
78
- for (let [id, type, name, extra] of entries) {
79
- let prop_obj = {id, type, name, ...extra};
80
- mqtt_props.set(prop_obj.id, prop_obj);
81
- mqtt_props.set(prop_obj.name, prop_obj);
82
- }
83
61
 
84
- return mqtt_props
85
- }
62
+ const mqtt_props = /* #__PURE__ */
63
+ init_mqtt_props();
86
64
 
87
- class U8_Reason extends Number {
88
- static of(v, pkt_kind, by_kind) {
89
- let self = new this(v);
90
- self.reason = by_kind?.[pkt_kind]?.get(v) || pkt_kind;
91
- return self
65
+ class mqtt_reason extends Number {
66
+ constructor(v, reason) {
67
+ super(v);
68
+ this.reason = `:${(this.ok = v<0x80) ? 'ok' : 'fail'}:${reason}`;
92
69
  }
93
70
  }
94
71
 
95
72
  class mqtt_reader_v4 {
96
- static of(buf) { return this.prototype.of(buf) }
97
- of(buf) {
73
+ static for(pkt, u8_body) { return new this().of(u8_body, {pkt}) }
74
+ of(buf, opt) {
98
75
  let step = (width, k) => (k=0|step.k, step.k=k+width, k);
99
- return {__proto__: this, buf, step}
76
+ return {__proto__: this, buf, step, ...opt}
100
77
  }
101
78
 
102
79
  has_more() {
@@ -139,7 +116,7 @@ class mqtt_reader_v4 {
139
116
  reason(pkt_kind) {
140
117
  let v = this.buf[this.step(1)];
141
118
  if (null != v)
142
- return U8_Reason.of(v, pkt_kind, this._reasons_by)
119
+ return new mqtt_reason(v, this._reason_for?.(v, pkt_kind))
143
120
  }
144
121
 
145
122
  flush() {
@@ -153,24 +130,22 @@ class mqtt_reader_v4 {
153
130
  let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
154
131
  props() {
155
132
  let {buf, step} = this;
156
- let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
133
+ let [n, vi, vi0] = decode_varint$1(buf, step.k|0, 0);
157
134
  step(n + vi - vi0);
158
135
  if (0 === n) return null
159
136
 
160
137
  let res={}, fork = this.of(buf.subarray(vi, step.k|0));
161
138
  while (fork.has_more()) {
162
139
  let v, pk = fork.u8(), pt = mqtt_props.get( pk );
163
-
164
140
  if (!pt) {
165
- res.error = `Unknown mqtt_prop enum ${pk}`;
166
- return res
141
+ res._unknown_ = pk;
142
+ this.warn?.(`unknown property: ${pk}`);
143
+ break
167
144
  }
168
145
 
169
146
  v = fork[pt.type]();
170
- if (pt.op) // accumulate operation
171
- v = fork[pt.op](res[pt.name], v);
172
-
173
- res[pt.name] = v;
147
+ res[pt.name] = !pt.op ? v
148
+ : fork[pt.op](res[pt.name], v); // accumulate operation
174
149
  }
175
150
  return res
176
151
  }
@@ -195,28 +170,99 @@ let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
195
170
  */
196
171
  };
197
172
 
198
- function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
199
- mqtt_reader = class extends mqtt_reader {
200
- static reasons(pkt_type, ...reason_entries) {
201
- let proto = this.prototype;
202
- proto._reasons_by = {... proto._reasons_by};
203
-
204
- let lut = (proto._reasons_by[pkt_type] ||= new Map());
205
- for (let [u8, reason] of reason_entries)
206
- lut.set( u8, reason );
173
+ function with_reasons(mqtt_reader, by_kind) {
174
+ for (let [k,lut] of Object.entries(by_kind))
175
+ by_kind[k] = new Map(lut);
207
176
 
208
- return this
177
+ return class extends mqtt_reader {
178
+ _reason_for(v, pkt_kind) {
179
+ return by_kind[pkt_kind]?.get(v) || by_kind.all.get(v)
209
180
  }
210
- };
181
+ warn(msg) {
182
+ let pkt = this.pkt;
183
+ pkt.warn ? pkt.warn(msg, pkt)
184
+ : console.warn('[u8-mqtt-packet] '+msg);
185
+ }
186
+ }
187
+ }
211
188
 
212
- for (let fn_info of info_fn_list)
213
- fn_info(mqtt_reader);
214
189
 
215
- return mqtt_reader
216
- }
190
+ const reasons_v4 = {
191
+ connack: [
192
+ [ 0x01, 'conn refused: unacceptable protocol version'],
193
+ [ 0x02, 'conn refused: identifier rejected'],
194
+ [ 0x03, 'conn refused: server unavailable'],
195
+ [ 0x04, 'conn refused: bad user name or password'],
196
+ [ 0x05, 'conn refused: not authorized'],
197
+ ],
198
+ suback: [
199
+ [ 0x00, 'qos=0'],
200
+ [ 0x01, 'qos=1'],
201
+ [ 0x02, 'qos=2'],
202
+ ],
203
+ unsuback: [
204
+ [ 0x11, 'no subscription existed'],
205
+ ],
206
+ puback: [
207
+ [ 0x10, 'no matching subscribers'],
208
+ ],
209
+ all: [
210
+ [ 0, ''], // Success
211
+ [ 0x80, 'unspecified error'], // disconnect puback suback unsuback
212
+ [ 0x83, 'implementation specific error'], // connack disconnect puback suback unsuback
213
+ [ 0x87, 'not authorized'], // connack disconnect puback suback unsuback
214
+ [ 0x8F, 'topic filter invalid'], // disconnect suback unsuback
215
+ [ 0x91, 'packet identifier in use'], // puback suback unsuback
216
+ [ 0x92, 'packet identifier not found' ], // pubxxx
217
+ ]};
218
+
219
+ const reasons_v5 = {
220
+ ... reasons_v4,
221
+ auth: [
222
+ [ 0x18, 'continue authentication' ],
223
+ [ 0x19, 're-authenticate' ],
224
+ ],
225
+ disconnect: [
226
+ [ 0x04, 'disconnect with will message'],
227
+ ],
228
+ all: [
229
+ ... reasons_v4.all,
230
+ [ 0x81, 'malformed packet'], // connack disconnect
231
+ [ 0x82, 'protocol error'], // connack disconnect
232
+ [ 0x84, 'unsupported protocol version'], // connack
233
+ [ 0x85, 'client identifier not valid'], // connack
234
+ [ 0x86, 'bad user name or password'], // connack
235
+ [ 0x88, 'server unavailable'], // connack
236
+ [ 0x89, 'server busy'], // connack disconnect
237
+ [ 0x8A, 'banned'], // connack
238
+ [ 0x8B, 'server shutting down'], // disconnect
239
+ [ 0x8C, 'bad authentication method'], // connack
240
+ [ 0x8D, 'keep alive timeout'], // disconnect
241
+ [ 0x8E, 'session taken over'], // disconnect
242
+ [ 0x90, 'topic name invalid'], // connack disconnect puback
243
+ [ 0x93, 'receive maximum exceeded'], // disconnect
244
+ [ 0x94, 'topic alias invalid'], // disconnect
245
+ [ 0x95, 'packet too large'], // connack disconnect
246
+ [ 0x96, 'message rate too high'], // disconnect
247
+ [ 0x97, 'quota exceeded'], // connack disconnect puback suback
248
+ [ 0x98, 'administrative action'], // disconnect
249
+ [ 0x99, 'payload format invalid'], // connack disconnect puback
250
+ [ 0x9A, 'retain not supported'], // connack disconnect
251
+ [ 0x9B, 'qoS not supported'], // connack disconnect
252
+ [ 0x9C, 'use another server'], // connack disconnect
253
+ [ 0x9D, 'server moved'], // connack disconnect
254
+ [ 0x9E, 'shared subscriptions not supported'], // disconnect suback
255
+ [ 0x9F, 'connection rate exceeded'], // connack disconnect
256
+ [ 0xA0, 'maximum connect time'], // disconnect
257
+ [ 0xA1, 'subscription identifiers not supported'], // disconnect suback
258
+ [ 0xA2, 'wildcard subscriptions not supported'], // disconnect suback
259
+ ]};
260
+
261
+ const mqtt_reader_v5 = /* #__PURE__ */
262
+ with_reasons(mqtt_reader_v5$1, reasons_v5);
217
263
 
218
264
  class mqtt_writer_v4 {
219
- static of() { return this.prototype.of() }
265
+ static for(pkt) { return new this().of() }
220
266
  of() { return {__proto__: this, $:[]} }
221
267
 
222
268
  static init() { return this }
@@ -305,7 +351,8 @@ class mqtt_writer_v5 extends mqtt_writer_v4 {
305
351
  for (let [name, value] of props) {
306
352
  let pt = mqtt_props.get(name);
307
353
  if (!pt)
308
- throw new Error(`Unknown mqtt_prop "${name}"`)
354
+ throw new Error(`MQTT unknown property "${name}"`)
355
+
309
356
  fork[pt.op || 'one'](value, pt);
310
357
  }
311
358
  this.push(fork.pack());
@@ -329,245 +376,6 @@ class mqtt_writer_v5 extends mqtt_writer_v4 {
329
376
  }
330
377
  }
331
378
 
332
- function mqtt_decode_connack(ns, mqtt_reader) {
333
- class _connack_flags_ extends Number {
334
- get session_present() { return this & 0x01 !== 0 }
335
- }
336
-
337
- return ns[0x2] = (pkt, u8_body) => {
338
- let rdr = mqtt_reader.of(u8_body);
339
-
340
- pkt.flags =
341
- rdr.flags(_connack_flags_);
342
-
343
- pkt.reason = rdr.reason(pkt.type);
344
- if (5 <= pkt.mqtt_level)
345
- pkt.props = rdr.props();
346
- return pkt }
347
- }
348
-
349
-
350
- function _connack_v4(mqtt_reader) {
351
- mqtt_reader.reasons('connack',
352
- // MQTT 3.1.1
353
- [ 0x00, 'Success'],
354
- [ 0x01, 'Connection refused, unacceptable protocol version'],
355
- [ 0x02, 'Connection refused, identifier rejected'],
356
- [ 0x03, 'Connection refused, server unavailable'],
357
- [ 0x04, 'Connection refused, bad user name or password'],
358
- [ 0x05, 'Connection refused, not authorized'],
359
- );
360
- }
361
-
362
- function _connack_v5(mqtt_reader) {
363
- _connack_v4(mqtt_reader);
364
-
365
- mqtt_reader.reasons('connack',
366
- // MQTT 5.0
367
- [ 0x81, 'Malformed Packet'],
368
- [ 0x82, 'Protocol Error'],
369
- [ 0x83, 'Implementation specific error'],
370
- [ 0x84, 'Unsupported Protocol Version'],
371
- [ 0x85, 'Client Identifier not valid'],
372
- [ 0x86, 'Bad User Name or Password'],
373
- [ 0x87, 'Not authorized'],
374
- [ 0x88, 'Server unavailable'],
375
- [ 0x89, 'Server busy'],
376
- [ 0x8A, 'Banned'],
377
- [ 0x8C, 'Bad authentication method'],
378
- [ 0x90, 'Topic Name invalid'],
379
- [ 0x95, 'Packet too large'],
380
- [ 0x97, 'Quota exceeded'],
381
- [ 0x99, 'Payload format invalid'],
382
- [ 0x9A, 'Retain not supported'],
383
- [ 0x9B, 'QoS not supported'],
384
- [ 0x9C, 'Use another server'],
385
- [ 0x9D, 'Server moved'],
386
- [ 0x9F, 'Connection rate exceeded'],
387
- );
388
- }
389
-
390
- function mqtt_decode_publish(ns, mqtt_reader) {
391
- return ns[0x3] = (pkt, u8_body) => {
392
- let {hdr} = pkt;
393
- pkt.dup = Boolean(hdr & 0x8);
394
- pkt.retain = Boolean(hdr & 0x1);
395
- let qos = pkt.qos = (hdr>>1) & 0x3;
396
-
397
- let rdr = mqtt_reader.of(u8_body);
398
- pkt.topic = rdr.utf8();
399
- if (0 !== qos)
400
- pkt.pkt_id = rdr.u16();
401
-
402
- if (5 <= pkt.mqtt_level)
403
- pkt.props = rdr.props();
404
-
405
- pkt.payload = rdr.flush();
406
- return pkt }
407
- }
408
-
409
- function mqtt_decode_puback(ns, mqtt_reader) {
410
- return ns[0x4] = (pkt, u8_body) => {
411
- let rdr = mqtt_reader.of(u8_body);
412
-
413
- pkt.pkt_id = rdr.u16();
414
- if (5 <= pkt.mqtt_level) {
415
- pkt.reason = rdr.reason(pkt.type);
416
- pkt.props = rdr.props();
417
- }
418
-
419
- return pkt }
420
- }
421
-
422
-
423
- function _puback_v5(mqtt_reader) {
424
- mqtt_reader.reasons('puback',
425
- // MQTT 5.0
426
- [ 0x00, 'Success'],
427
- [ 0x10, 'No matching subscribers'],
428
- [ 0x80, 'Unspecified error'],
429
- [ 0x83, 'Implementation specific error'],
430
- [ 0x87, 'Not authorized'],
431
- [ 0x90, 'Topic Name invalid'],
432
- [ 0x91, 'Packet identifier in use'],
433
- [ 0x97, 'Quota exceeded'],
434
- [ 0x99, 'Payload format invalid'],
435
- );
436
- }
437
-
438
- function _mqtt_decode_suback(mqtt_reader) {
439
- return (pkt, u8_body) => {
440
- let rdr = mqtt_reader.of(u8_body);
441
-
442
- pkt.pkt_id = rdr.u16();
443
- if (5 <= pkt.mqtt_level)
444
- pkt.props = rdr.props();
445
-
446
- let answers = pkt.answers = [];
447
- while (rdr.has_more())
448
- answers.push(
449
- rdr.reason(pkt.type) );
450
-
451
- return pkt }
452
- }
453
-
454
- function mqtt_decode_suback(ns, mqtt_reader) {
455
- return ns[0x9] = _mqtt_decode_suback(mqtt_reader)
456
- }
457
-
458
- function _suback_v4(mqtt_reader) {
459
- mqtt_reader.reasons('suback',
460
- // MQTT 3.1.1
461
- [ 0x00, 'Granted QoS 0'],
462
- [ 0x01, 'Granted QoS 1'],
463
- [ 0x02, 'Granted QoS 2'],
464
- );
465
- }
466
-
467
- function _suback_v5(mqtt_reader) {
468
- _suback_v4(mqtt_reader);
469
-
470
- mqtt_reader.reasons('suback',
471
- // MQTT 5.0
472
- [ 0x80, 'Unspecified error'],
473
- [ 0x83, 'Implementation specific error'],
474
- [ 0x87, 'Not authorized'],
475
- [ 0x8F, 'Topic Filter invalid'],
476
- [ 0x91, 'Packet Identifier in use'],
477
- [ 0x97, 'Quota exceeded'],
478
- [ 0x9E, 'Shared Subscriptions not supported'],
479
- [ 0xA1, 'Subscription Identifiers not supported'],
480
- [ 0xA2, 'Wildcard Subscriptions not supported'],
481
- );
482
- }
483
-
484
- function mqtt_decode_unsuback(ns, mqtt_reader) {
485
- return ns[0xb] = _mqtt_decode_suback(mqtt_reader)
486
- }
487
-
488
- function _unsuback_v4(mqtt_reader) {
489
- mqtt_reader.reasons('unsuback',
490
- // MQTT 3.1.1
491
- [ 0x00, 'Success'],
492
- [ 0x11, 'No subscription existed'],
493
- [ 0x80, 'Unspecified error'],
494
- [ 0x83, 'Implementation specific error'],
495
- [ 0x87, 'Not authorized'],
496
- [ 0x8F, 'Topic Filter invalid'],
497
- [ 0x91, 'Packet Identifier in use'],
498
- );
499
- }
500
-
501
- function mqtt_decode_pingxxx(ns) {
502
- return ns[0xc] = ns[0xd] = pkt => pkt
503
- }
504
-
505
- function mqtt_decode_disconnect(ns, mqtt_reader) {
506
- return ns[0xe] = (pkt, u8_body) => {
507
- if (u8_body && 5 <= pkt.mqtt_level) {
508
- let rdr = mqtt_reader.of(u8_body);
509
- pkt.reason = rdr.reason(pkt.type);
510
- pkt.props = rdr.props();
511
- }
512
- return pkt }
513
- }
514
-
515
-
516
- function _disconnect_v5(mqtt_reader) {
517
- mqtt_reader.reasons('disconnect',
518
- // MQTT 5.0
519
- [ 0x00, 'Normal disconnection'],
520
- [ 0x04, 'Disconnect with Will Message'],
521
- [ 0x80, 'Unspecified error'],
522
- [ 0x81, 'Malformed Packet'],
523
- [ 0x82, 'Protocol Error'],
524
- [ 0x83, 'Implementation specific error'],
525
- [ 0x87, 'Not authorized'],
526
- [ 0x89, 'Server busy'],
527
- [ 0x8B, 'Server shutting down'],
528
- [ 0x8D, 'Keep Alive timeout'],
529
- [ 0x8E, 'Session taken over'],
530
- [ 0x8F, 'Topic Filter invalid'],
531
- [ 0x90, 'Topic Name invalid'],
532
- [ 0x93, 'Receive Maximum exceeded'],
533
- [ 0x94, 'Topic Alias invalid'],
534
- [ 0x95, 'Packet too large'],
535
- [ 0x96, 'Message rate too high'],
536
- [ 0x97, 'Quota exceeded'],
537
- [ 0x98, 'Administrative action'],
538
- [ 0x99, 'Payload format invalid'],
539
- [ 0x9A, 'Retain not supported'],
540
- [ 0x9B, 'QoS not supported'],
541
- [ 0x9C, 'Use another server'],
542
- [ 0x9D, 'Server moved'],
543
- [ 0x9E, 'Shared Subscriptions not supported'],
544
- [ 0x9F, 'Connection rate exceeded'],
545
- [ 0xA0, 'Maximum connect time'],
546
- [ 0xA1, 'Subscription Identifiers not supported'],
547
- [ 0xA2, 'Wildcard Subscriptions not supported'],
548
- );
549
- }
550
-
551
- function mqtt_decode_auth(ns, mqtt_reader) {
552
- return ns[0xf] = (pkt, u8_body) => {
553
- if ( 5 <= pkt.mqtt_level ) {
554
- let rdr = mqtt_reader.of(u8_body);
555
- pkt.reason = rdr.reason(pkt.type);
556
- pkt.props = rdr.props();
557
- }
558
- return pkt }
559
- }
560
-
561
-
562
- function _auth_v5(mqtt_reader) {
563
- mqtt_reader.reasons('auth',
564
- // MQTT 5.0
565
- [ 0x00, 'Success' ],
566
- [ 0x18, 'Continue authentication' ],
567
- [ 0x19, 'Re-authenticate' ],
568
- );
569
- }
570
-
571
379
  function mqtt_encode_connect(ns, mqtt_writer) {
572
380
  const _c_mqtt_proto = new Uint8Array([
573
381
  0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
@@ -586,7 +394,7 @@ function mqtt_encode_connect(ns, mqtt_writer) {
586
394
  | ( will.retain ? 0x20 : 0 );
587
395
 
588
396
  return ns.connect = ( mqtt_level, pkt ) => {
589
- let wrt = mqtt_writer.of(pkt);
397
+ let wrt = mqtt_writer.for(pkt);
590
398
 
591
399
  wrt.push(_c_mqtt_proto);
592
400
  wrt.u8( mqtt_level );
@@ -624,10 +432,27 @@ function mqtt_encode_connect(ns, mqtt_writer) {
624
432
  }
625
433
  }
626
434
 
435
+ function mqtt_decode_connack(ns, mqtt_reader) {
436
+ class _connack_flags_ extends Number {
437
+ get session_present() { return this & 0x01 !== 0 }
438
+ }
439
+
440
+ return ns[0x2] = (pkt, u8_body) => {
441
+ let rdr = mqtt_reader.for(pkt, u8_body);
442
+
443
+ pkt.flags =
444
+ rdr.flags(_connack_flags_);
445
+
446
+ pkt.reason = rdr.reason(pkt.type);
447
+ if (5 <= pkt.mqtt_level)
448
+ pkt.props = rdr.props();
449
+ return pkt }
450
+ }
451
+
627
452
  function mqtt_encode_publish(ns, mqtt_writer) {
628
453
  return ns.publish = ( mqtt_level, pkt ) => {
629
454
  let qos = (pkt.qos & 0x3) << 1;
630
- let wrt = mqtt_writer.of(pkt);
455
+ let wrt = mqtt_writer.for(pkt);
631
456
 
632
457
  wrt.utf8(pkt.topic);
633
458
  if (0 !== qos)
@@ -645,9 +470,28 @@ function mqtt_encode_publish(ns, mqtt_writer) {
645
470
  }
646
471
  }
647
472
 
473
+ function mqtt_decode_publish(ns, mqtt_reader) {
474
+ return ns[0x3] = (pkt, u8_body) => {
475
+ let {hdr} = pkt;
476
+ pkt.dup = Boolean(hdr & 0x8);
477
+ pkt.retain = Boolean(hdr & 0x1);
478
+ let qos = pkt.qos = (hdr>>1) & 0x3;
479
+
480
+ let rdr = mqtt_reader.for(pkt, u8_body);
481
+ pkt.topic = rdr.utf8();
482
+ if (0 !== qos)
483
+ pkt.pkt_id = rdr.u16();
484
+
485
+ if (5 <= pkt.mqtt_level)
486
+ pkt.props = rdr.props();
487
+
488
+ pkt.payload = rdr.flush();
489
+ return pkt }
490
+ }
491
+
648
492
  function mqtt_encode_puback(ns, mqtt_writer) {
649
493
  return ns.puback = ( mqtt_level, pkt ) => {
650
- let wrt = mqtt_writer.of(pkt);
494
+ let wrt = mqtt_writer.for(pkt);
651
495
 
652
496
  wrt.u16(pkt.pkt_id);
653
497
  if (5 <= mqtt_level) {
@@ -659,6 +503,20 @@ function mqtt_encode_puback(ns, mqtt_writer) {
659
503
  }
660
504
  }
661
505
 
506
+
507
+ function mqtt_decode_puback(ns, mqtt_reader) {
508
+ return ns[0x4] = (pkt, u8_body) => {
509
+ let rdr = mqtt_reader.for(pkt, u8_body);
510
+
511
+ pkt.pkt_id = rdr.u16();
512
+ if (5 <= pkt.mqtt_level) {
513
+ pkt.reason = rdr.reason(pkt.type);
514
+ pkt.props = rdr.props();
515
+ }
516
+
517
+ return pkt }
518
+ }
519
+
662
520
  function mqtt_encode_subscribe(ns, mqtt_writer) {
663
521
  const _enc_subscribe_flags = opts => 0
664
522
  | ( opts.qos & 0x3 )
@@ -666,7 +524,7 @@ function mqtt_encode_subscribe(ns, mqtt_writer) {
666
524
  | ( (opts.retain_handling & 0x3) << 2 );
667
525
 
668
526
  return ns.subscribe = ( mqtt_level, pkt ) => {
669
- let wrt = mqtt_writer.of(pkt);
527
+ let wrt = mqtt_writer.for(pkt);
670
528
 
671
529
  wrt.u16(pkt.pkt_id);
672
530
  if (5 <= mqtt_level)
@@ -692,9 +550,25 @@ function mqtt_encode_subscribe(ns, mqtt_writer) {
692
550
  }
693
551
  }
694
552
 
553
+ function mqtt_decode_xxsuback(ns, mqtt_reader) {
554
+ return ns[0x9] = ns[0xb] = (pkt, u8_body) => {
555
+ let rdr = mqtt_reader.for(pkt, u8_body);
556
+
557
+ pkt.pkt_id = rdr.u16();
558
+ if (5 <= pkt.mqtt_level)
559
+ pkt.props = rdr.props();
560
+
561
+ let answers = pkt.answers = [];
562
+ while (rdr.has_more())
563
+ answers.push(
564
+ rdr.reason(pkt.type) );
565
+
566
+ return pkt }
567
+ }
568
+
695
569
  function mqtt_encode_unsubscribe(ns, mqtt_writer) {
696
570
  return ns.unsubscribe = ( mqtt_level, pkt ) => {
697
- let wrt = mqtt_writer.of(pkt);
571
+ let wrt = mqtt_writer.for(pkt);
698
572
 
699
573
  wrt.u16(pkt.pkt_id);
700
574
  if (5 <= mqtt_level)
@@ -712,9 +586,14 @@ function mqtt_encode_pingxxx(ns) {
712
586
  ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
713
587
  }
714
588
 
589
+
590
+ function mqtt_decode_pingxxx(ns) {
591
+ return ns[0xc] = ns[0xd] = pkt => pkt
592
+ }
593
+
715
594
  function mqtt_encode_disconnect(ns, mqtt_writer) {
716
595
  return ns.disconnect = ( mqtt_level, pkt ) => {
717
- let wrt = mqtt_writer.of(pkt);
596
+ let wrt = mqtt_writer.for(pkt);
718
597
 
719
598
  if (pkt && 5 <= mqtt_level) {
720
599
  if (pkt.reason || pkt.props) {
@@ -727,6 +606,17 @@ function mqtt_encode_disconnect(ns, mqtt_writer) {
727
606
  }
728
607
  }
729
608
 
609
+
610
+ function mqtt_decode_disconnect(ns, mqtt_reader) {
611
+ return ns[0xe] = (pkt, u8_body) => {
612
+ if (u8_body && 5 <= pkt.mqtt_level) {
613
+ let rdr = mqtt_reader.for(pkt, u8_body);
614
+ pkt.reason = rdr.reason(pkt.type);
615
+ pkt.props = rdr.props();
616
+ }
617
+ return pkt }
618
+ }
619
+
730
620
  function mqtt_encode_auth(ns, mqtt_writer) {
731
621
  return ns.auth = ( mqtt_level, pkt ) => {
732
622
  if (5 > mqtt_level)
@@ -741,12 +631,21 @@ function mqtt_encode_auth(ns, mqtt_writer) {
741
631
  }
742
632
  }
743
633
 
634
+ function mqtt_decode_auth(ns, mqtt_reader) {
635
+ return ns[0xf] = (pkt, u8_body) => {
636
+ if ( 5 <= pkt.mqtt_level ) {
637
+ let rdr = mqtt_reader.of(u8_body);
638
+ pkt.reason = rdr.reason(pkt.type);
639
+ pkt.props = rdr.props();
640
+ }
641
+ return pkt }
642
+ }
643
+
744
644
  const mqtt_decode_v5 = [
745
645
  mqtt_decode_connack,
746
646
  mqtt_decode_publish,
747
647
  mqtt_decode_puback,
748
- mqtt_decode_suback,
749
- mqtt_decode_unsuback,
648
+ mqtt_decode_xxsuback,
750
649
  mqtt_decode_pingxxx,
751
650
  mqtt_decode_disconnect,
752
651
  mqtt_decode_auth,
@@ -764,65 +663,44 @@ const mqtt_encode_v5 = [
764
663
  mqtt_encode_auth,
765
664
  ];
766
665
 
767
- const mqtt_reader_v5 = /* #__PURE__ */
768
- mqtt_reader_info(
769
- mqtt_reader_v5$1,
770
- _connack_v5,
771
- _puback_v5,
772
- _suback_v5,
773
- _unsuback_v4,
774
- _disconnect_v5,
775
- _auth_v5,
776
- );
777
-
778
-
779
666
  const mqtt_opts_v5 =
780
667
  { decode_fns: mqtt_decode_v5,
781
668
  mqtt_reader: mqtt_reader_v5,
782
669
  encode_fns: mqtt_encode_v5,
783
670
  mqtt_writer: mqtt_writer_v5, };
784
671
 
785
- /*
786
- export function decode_varint_loop(u8, i=0) {
787
- let i0 = i
788
- let shift = 0, n = (u8[i] & 0x7f)
789
- while ( 0x80 & u8[i++] )
790
- n |= (u8[i] & 0x7f) << (shift += 7)
672
+ function decode_varint(u8, i0=0, invalid) {
673
+ let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
674
+ for(; b & 0x80;)
675
+ n |= ((b=u8[i++]) & 0x7f) << (shift += 7);
791
676
 
792
- return [n, i, i0]
793
- }
794
- */
795
-
796
-
797
- function decode_varint(u8, i=0) {
798
- let i0 = i;
799
- // unrolled for a max of 4 chains
800
- let n = (u8[i] & 0x7f) << 0;
801
- if ( 0x80 & u8[i++] ) {
802
- n |= (u8[i] & 0x7f) << 7;
803
- if ( 0x80 & u8[i++] ) {
804
- n |= (u8[i] & 0x7f) << 14;
805
- if ( 0x80 & u8[i++] ) {
806
- n |= (u8[i] & 0x7f) << 21;
807
- }
808
- }
809
- }
810
- return [n, i, i0]
677
+ return (u8.length < i)
678
+ ? [invalid, i0, i0] // fail: insuffecient u8 bytes to decode
679
+ : [n, i, i0] // successful value
811
680
  }
812
681
 
813
682
  function mqtt_raw_dispatch(opt) {
814
- let u8 = new Uint8Array(0);
683
+ let u8 = new Uint8Array(0), len_tip=0;
815
684
  return u8_buf => {
816
685
  u8 = 0 === u8.byteLength
817
686
  ? u8_buf : _u8_join(u8, u8_buf);
818
687
 
819
688
  let res = [];
820
- while (1) {
821
- let [len_body, len_vh] = decode_varint(u8, 1);
822
- let len_pkt = len_body + len_vh;
823
689
 
824
- if ( u8.byteLength < len_pkt )
825
- return res
690
+ // wait for at least len_tip bytes for next (tip) message
691
+ while ( u8.byteLength >= len_tip ) {
692
+
693
+ // if varint is incomplete, return len_body=NaN
694
+ let [len_body, len_vh] = decode_varint(u8, 1, NaN);
695
+ let len_pkt = len_body + len_vh; // may be NaN
696
+
697
+ if (!( len_pkt <= u8.byteLength )) {
698
+ // incomplete packet cases:
699
+ // - len_pkt is less than available bytes
700
+ // - len_pkt is NaN from decode_varint() due to lack of data
701
+ len_tip = len_pkt || 0; // 0 when NaN
702
+ break
703
+ }
826
704
 
827
705
  let b0 = u8[0];
828
706
  let u8_body = 0 === len_body ? null
@@ -834,6 +712,8 @@ function mqtt_raw_dispatch(opt) {
834
712
  if (null != pkt)
835
713
  res.push( pkt );
836
714
  }
715
+
716
+ return res
837
717
  }
838
718
  }
839
719
 
@@ -844,16 +724,17 @@ function _u8_join(a, b) {
844
724
  return r
845
725
  }
846
726
 
847
- const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
727
+ const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
728
+
729
+
730
+ function mqtt_pkt_ctx(mqtt_level, opts, pkt_api=opts.pkt_ctx) {
731
+ let _as_pkt_ctx = pkt_api => ({
732
+ __proto__: pkt_api,
733
+ get hdr() { return this.b0 & 0xf },
734
+ get id() { return this.b0 >>> 4 },
735
+ get type() { return _pkt_types[this.b0 >>> 4] },
736
+ mqtt_level });
848
737
 
849
- function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
850
- pkt_ctx = {
851
- __proto__: pkt_ctx || opts.pkt_ctx,
852
- mqtt_level,
853
- get hdr() { return this.b0 & 0xf },
854
- get id() { return this.b0 >>> 4 },
855
- get type() { return _pkt_types[this.b0 >>> 4] },
856
- };
857
738
 
858
739
  let op, _decode_by_id=[], _encode_by_type={};
859
740
  for (op of opts.encode_fns)
@@ -862,10 +743,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
862
743
  op(_decode_by_id, opts.mqtt_reader);
863
744
 
864
745
  return {
865
- pkt_ctx,
746
+ pkt_api, pkt_ctx: _as_pkt_ctx(pkt_api),
866
747
 
867
- encode_pkt(type, pkt) {
868
- return _encode_by_type[type]( mqtt_level, pkt ) },
748
+ encode_pkt: (type, pkt) =>
749
+ _encode_by_type[type]( mqtt_level, pkt ),
869
750
 
870
751
  decode_pkt(b0, u8_body) {
871
752
  if (b0.map) // Uint8Array in first arg
@@ -874,12 +755,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
874
755
  let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
875
756
  return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
876
757
 
877
- mqtt_stream() {
878
- let self = { __proto__: this, pkt_ctx: { __proto__: pkt_ctx } };
879
- self.pkt_ctx._base_ = self.pkt_ctx;
758
+ mqtt_stream(pkt_api=this.pkt_api) {
759
+ let self = { __proto__: this, pkt_ctx: _as_pkt_ctx(pkt_api) };
880
760
  self.decode = mqtt_raw_dispatch(self);
881
- return self
882
- },
761
+ return self },
883
762
  }
884
763
  }
885
764
 
@@ -1420,7 +1299,7 @@ class MQTTCore extends MQTTBase {
1420
1299
 
1421
1300
  return this} }
1422
1301
 
1423
- const version = '0.6.1-node';
1302
+ const version = '0.6.3-node';
1424
1303
 
1425
1304
  const MQTTClient_v4 = /* #__PURE__ */
1426
1305
  MQTTCore.mqtt_ctx(4, mqtt_opts_v5);