u8-mqtt 0.6.2 → 0.6.4
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.
- package/cjs/basic-v4.cjs +194 -243
- package/cjs/basic-v4.cjs.map +1 -1
- package/cjs/basic-v5.cjs +247 -368
- package/cjs/basic-v5.cjs.map +1 -1
- package/cjs/full-v4.cjs +342 -384
- package/cjs/full-v4.cjs.map +1 -1
- package/cjs/full-v5.cjs +395 -507
- package/cjs/full-v5.cjs.map +1 -1
- package/cjs/index.cjs +292 -399
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +239 -274
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +292 -399
- package/cjs/v5.cjs.map +1 -1
- package/code/router_path.jsy +39 -28
- package/code/with_topic_router.jsy +4 -1
- package/esm/basic-v4.js +194 -243
- package/esm/basic-v4.js.map +1 -1
- package/esm/basic-v5.js +247 -368
- package/esm/basic-v5.js.map +1 -1
- package/esm/deno/basic-v4.js +194 -243
- package/esm/deno/basic-v4.js.map +1 -1
- package/esm/deno/basic-v5.js +247 -368
- package/esm/deno/basic-v5.js.map +1 -1
- package/esm/deno/full-v4.js +342 -384
- package/esm/deno/full-v4.js.map +1 -1
- package/esm/deno/full-v5.js +395 -507
- package/esm/deno/full-v5.js.map +1 -1
- package/esm/deno/index.js +292 -399
- package/esm/deno/index.js.map +1 -1
- package/esm/deno/v4.js +239 -274
- package/esm/deno/v4.js.map +1 -1
- package/esm/deno/v5.js +292 -399
- package/esm/deno/v5.js.map +1 -1
- package/esm/full-v4.js +342 -384
- package/esm/full-v4.js.map +1 -1
- package/esm/full-v5.js +395 -507
- package/esm/full-v5.js.map +1 -1
- package/esm/index.js +292 -399
- package/esm/index.js.map +1 -1
- package/esm/node/basic-v4.js +194 -243
- package/esm/node/basic-v4.js.map +1 -1
- package/esm/node/basic-v4.mjs +194 -243
- package/esm/node/basic-v4.mjs.map +1 -1
- package/esm/node/basic-v5.js +247 -368
- package/esm/node/basic-v5.js.map +1 -1
- package/esm/node/basic-v5.mjs +247 -368
- package/esm/node/basic-v5.mjs.map +1 -1
- package/esm/node/full-v4.js +342 -384
- package/esm/node/full-v4.js.map +1 -1
- package/esm/node/full-v4.mjs +342 -384
- package/esm/node/full-v4.mjs.map +1 -1
- package/esm/node/full-v5.js +395 -507
- package/esm/node/full-v5.js.map +1 -1
- package/esm/node/full-v5.mjs +395 -507
- package/esm/node/full-v5.mjs.map +1 -1
- package/esm/node/index.js +292 -399
- package/esm/node/index.js.map +1 -1
- package/esm/node/index.mjs +292 -399
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.js +239 -274
- package/esm/node/v4.js.map +1 -1
- package/esm/node/v4.mjs +239 -274
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.js +292 -399
- package/esm/node/v5.js.map +1 -1
- package/esm/node/v5.mjs +292 -399
- package/esm/node/v5.mjs.map +1 -1
- package/esm/v4.js +239 -274
- package/esm/v4.js.map +1 -1
- package/esm/v5.js +292 -399
- package/esm/v5.js.map +1 -1
- package/esm/web/basic-v4.js +194 -243
- package/esm/web/basic-v4.js.map +1 -1
- package/esm/web/basic-v4.min.js +1 -1
- package/esm/web/basic-v4.min.js.br +0 -0
- package/esm/web/basic-v4.min.js.gz +0 -0
- package/esm/web/basic-v5.js +247 -368
- package/esm/web/basic-v5.js.map +1 -1
- package/esm/web/basic-v5.min.js +1 -1
- package/esm/web/basic-v5.min.js.br +0 -0
- package/esm/web/basic-v5.min.js.gz +0 -0
- package/esm/web/full-v4.js +342 -384
- package/esm/web/full-v4.js.map +1 -1
- package/esm/web/full-v4.min.js +1 -1
- package/esm/web/full-v4.min.js.br +0 -0
- package/esm/web/full-v4.min.js.gz +0 -0
- package/esm/web/full-v5.js +395 -507
- package/esm/web/full-v5.js.map +1 -1
- package/esm/web/full-v5.min.js +1 -1
- package/esm/web/full-v5.min.js.br +0 -0
- package/esm/web/full-v5.min.js.gz +0 -0
- package/esm/web/index.js +292 -399
- package/esm/web/index.js.map +1 -1
- package/esm/web/index.min.js +1 -1
- package/esm/web/index.min.js.br +0 -0
- package/esm/web/index.min.js.gz +0 -0
- package/esm/web/v4.js +239 -274
- package/esm/web/v4.js.map +1 -1
- package/esm/web/v4.min.js +1 -1
- package/esm/web/v4.min.js.br +0 -0
- package/esm/web/v4.min.js.gz +0 -0
- package/esm/web/v5.js +292 -399
- package/esm/web/v5.js.map +1 -1
- package/esm/web/v5.min.js +1 -1
- package/esm/web/v5.min.js.br +0 -0
- package/esm/web/v5.min.js.gz +0 -0
- package/package.json +5 -5
package/esm/full-v4.js
CHANGED
|
@@ -1,54 +1,32 @@
|
|
|
1
1
|
function encode_varint(n, a=[]) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
n
|
|
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
|
-
|
|
13
|
-
|
|
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
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
|
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
|
|
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
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
-
|
|
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
|
];
|
|
@@ -779,7 +731,10 @@ const with_topic_router = mqtt_topic_router =>
|
|
|
779
731
|
// alias: unsub_topic
|
|
780
732
|
unsubscribe_topic(topic_route, ...args) {
|
|
781
733
|
let router = this.router;
|
|
782
|
-
|
|
734
|
+
|
|
735
|
+
let fn = args.at(-1)?.call ? args.pop() : null;
|
|
736
|
+
router.remove(topic_route, true, fn);
|
|
737
|
+
|
|
783
738
|
let topic = router.mqtt_topic(topic_route);
|
|
784
739
|
return this.unsubscribe(topic, ...args ) }// topic_prefix
|
|
785
740
|
|
|
@@ -832,34 +787,38 @@ function mqtt_topic_path_router() {
|
|
|
832
787
|
if (fn) {throw new TypeError()}
|
|
833
788
|
fn = _ignore;}
|
|
834
789
|
|
|
835
|
-
let
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
pri_lsts[priority ? 0 : 1].push(rte);
|
|
790
|
+
let route = parse(as_topic_path(topic_route));
|
|
791
|
+
route.topic = topic_route;
|
|
792
|
+
route.tgt = fn;
|
|
793
|
+
pri_lsts[priority ? 0 : 1].push(route);
|
|
840
794
|
return this}
|
|
841
795
|
|
|
842
|
-
, remove(
|
|
843
|
-
let lst = pri_lsts
|
|
844
|
-
|
|
796
|
+
, remove(query, ...args) {
|
|
797
|
+
let lst = pri_lsts;
|
|
798
|
+
if ('boolean' === typeof args[0]) {
|
|
799
|
+
lst = [pri_lsts[args.shift() ? 0 : 1]];}
|
|
800
|
+
|
|
801
|
+
if ('string' === typeof query) {
|
|
802
|
+
query ={topic: query, tgt: args.pop()}; }
|
|
803
|
+
return _route_remove(lst, query)}
|
|
845
804
|
|
|
846
805
|
, clear(priority) {
|
|
847
|
-
pri_lsts[priority ? 0 : 1] = [];
|
|
848
806
|
if (null == priority) {
|
|
849
|
-
pri_lsts
|
|
807
|
+
pri_lsts = [[],[]]; }// null clears both lists
|
|
808
|
+
else {
|
|
809
|
+
pri_lsts[priority ? 0 : 1] = [];} }
|
|
850
810
|
|
|
851
811
|
, async invoke(pkt, ctx) {
|
|
852
812
|
ctx.idx = 0;
|
|
853
813
|
ctx.rm = rm;
|
|
854
814
|
|
|
855
|
-
for (let [fn, params] of find(pkt.topic)) {
|
|
815
|
+
for (let [fn, params, route] of find(pkt.topic)) {
|
|
856
816
|
let res = await fn(pkt, params, ctx);
|
|
857
817
|
|
|
858
818
|
if (rm === res) {
|
|
859
|
-
_route_remove(pri_lsts,
|
|
819
|
+
_route_remove(pri_lsts, route);}
|
|
860
820
|
|
|
861
|
-
if (ctx.done) {
|
|
862
|
-
break}
|
|
821
|
+
if (ctx.done) {break}
|
|
863
822
|
else ctx.idx++;}
|
|
864
823
|
|
|
865
824
|
if (1 === pkt.qos) {
|
|
@@ -869,70 +828,68 @@ function mqtt_topic_path_router() {
|
|
|
869
828
|
function * _routes_iter(all_route_lists, topic) {
|
|
870
829
|
topic = topic.replace(/^[\/]*/, '/'); // ensure '/' prefix for regexparam library
|
|
871
830
|
for (let route_list of all_route_lists) {
|
|
872
|
-
for (let
|
|
873
|
-
let match = pattern.exec(topic);
|
|
831
|
+
for (let route of route_list) {
|
|
832
|
+
let match = route.pattern.exec(topic);
|
|
874
833
|
if (match) {
|
|
875
|
-
let params = keys
|
|
876
|
-
? keys.reduce(
|
|
834
|
+
let params = route.keys
|
|
835
|
+
? route.keys.reduce(
|
|
877
836
|
(o, k, i) => (o[k] = match[1+i], o)
|
|
878
837
|
, {})
|
|
879
838
|
: match.groups ?? match;
|
|
880
|
-
yield [tgt, params];} } } }
|
|
839
|
+
yield [route.tgt, params, route];} } } }
|
|
881
840
|
|
|
882
841
|
|
|
883
842
|
function _route_remove(all_route_lists, query) {
|
|
884
|
-
let
|
|
885
|
-
route===query
|
|
886
|
-
|| route.tgt===query
|
|
887
|
-
|| route.key===query);
|
|
843
|
+
let ans = false;
|
|
888
844
|
for (let lst of all_route_lists) {
|
|
889
|
-
let
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
n
|
|
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]
|
|
845
|
+
let idx_tip = 0;
|
|
846
|
+
for (let route of lst) {
|
|
847
|
+
// skip matching routes to remove from compacted list
|
|
848
|
+
if (route === query) continue
|
|
849
|
+
if (route.topic === query.topic) {
|
|
850
|
+
if (null == query.tgt) continue
|
|
851
|
+
if (route.tgt === query.tgt) continue}
|
|
852
|
+
|
|
853
|
+
lst[idx_tip++] = route;}
|
|
854
|
+
|
|
855
|
+
// truncate remaining list
|
|
856
|
+
if (lst.splice(idx_tip).length)
|
|
857
|
+
ans = true;}
|
|
858
|
+
|
|
859
|
+
return ans}
|
|
860
|
+
|
|
861
|
+
function decode_varint(u8, i0=0, invalid) {
|
|
862
|
+
let shift=0, i=i0, b=u8[i++], n=(b & 0x7f);
|
|
863
|
+
for(; b & 0x80;)
|
|
864
|
+
n |= ((b=u8[i++]) & 0x7f) << (shift += 7);
|
|
865
|
+
|
|
866
|
+
return (u8.length < i)
|
|
867
|
+
? [invalid, i0, i0] // fail: insuffecient u8 bytes to decode
|
|
868
|
+
: [n, i, i0] // successful value
|
|
921
869
|
}
|
|
922
870
|
|
|
923
871
|
function mqtt_raw_dispatch(opt) {
|
|
924
|
-
let u8 = new Uint8Array(0);
|
|
872
|
+
let u8 = new Uint8Array(0), len_tip=0;
|
|
925
873
|
return u8_buf => {
|
|
926
874
|
u8 = 0 === u8.byteLength
|
|
927
875
|
? u8_buf : _u8_join(u8, u8_buf);
|
|
928
876
|
|
|
929
877
|
let res = [];
|
|
930
|
-
while (1) {
|
|
931
|
-
let [len_body, len_vh] = decode_varint(u8, 1);
|
|
932
|
-
let len_pkt = len_body + len_vh;
|
|
933
878
|
|
|
934
|
-
|
|
935
|
-
|
|
879
|
+
// wait for at least len_tip bytes for next (tip) message
|
|
880
|
+
while ( u8.byteLength >= len_tip ) {
|
|
881
|
+
|
|
882
|
+
// if varint is incomplete, return len_body=NaN
|
|
883
|
+
let [len_body, len_vh] = decode_varint(u8, 1, NaN);
|
|
884
|
+
let len_pkt = len_body + len_vh; // may be NaN
|
|
885
|
+
|
|
886
|
+
if (!( len_pkt <= u8.byteLength )) {
|
|
887
|
+
// incomplete packet cases:
|
|
888
|
+
// - len_pkt is less than available bytes
|
|
889
|
+
// - len_pkt is NaN from decode_varint() due to lack of data
|
|
890
|
+
len_tip = len_pkt || 0; // 0 when NaN
|
|
891
|
+
break
|
|
892
|
+
}
|
|
936
893
|
|
|
937
894
|
let b0 = u8[0];
|
|
938
895
|
let u8_body = 0 === len_body ? null
|
|
@@ -944,6 +901,8 @@ function mqtt_raw_dispatch(opt) {
|
|
|
944
901
|
if (null != pkt)
|
|
945
902
|
res.push( pkt );
|
|
946
903
|
}
|
|
904
|
+
|
|
905
|
+
return res
|
|
947
906
|
}
|
|
948
907
|
}
|
|
949
908
|
|
|
@@ -954,16 +913,17 @@ function _u8_join(a, b) {
|
|
|
954
913
|
return r
|
|
955
914
|
}
|
|
956
915
|
|
|
957
|
-
const _pkt_types =
|
|
916
|
+
const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
function mqtt_pkt_ctx(mqtt_level, opts, pkt_api=opts.pkt_ctx) {
|
|
920
|
+
let _as_pkt_ctx = pkt_api => ({
|
|
921
|
+
__proto__: pkt_api,
|
|
922
|
+
get hdr() { return this.b0 & 0xf },
|
|
923
|
+
get id() { return this.b0 >>> 4 },
|
|
924
|
+
get type() { return _pkt_types[this.b0 >>> 4] },
|
|
925
|
+
mqtt_level });
|
|
958
926
|
|
|
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
927
|
|
|
968
928
|
let op, _decode_by_id=[], _encode_by_type={};
|
|
969
929
|
for (op of opts.encode_fns)
|
|
@@ -972,10 +932,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
|
|
|
972
932
|
op(_decode_by_id, opts.mqtt_reader);
|
|
973
933
|
|
|
974
934
|
return {
|
|
975
|
-
pkt_ctx,
|
|
935
|
+
pkt_api, pkt_ctx: _as_pkt_ctx(pkt_api),
|
|
976
936
|
|
|
977
|
-
encode_pkt(type, pkt)
|
|
978
|
-
|
|
937
|
+
encode_pkt: (type, pkt) =>
|
|
938
|
+
_encode_by_type[type]( mqtt_level, pkt ),
|
|
979
939
|
|
|
980
940
|
decode_pkt(b0, u8_body) {
|
|
981
941
|
if (b0.map) // Uint8Array in first arg
|
|
@@ -984,12 +944,10 @@ function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
|
|
|
984
944
|
let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
|
|
985
945
|
return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
|
|
986
946
|
|
|
987
|
-
mqtt_stream() {
|
|
988
|
-
let self = { __proto__: this, pkt_ctx:
|
|
989
|
-
self.pkt_ctx._base_ = self.pkt_ctx;
|
|
947
|
+
mqtt_stream(pkt_api=this.pkt_api) {
|
|
948
|
+
let self = { __proto__: this, pkt_ctx: _as_pkt_ctx(pkt_api) };
|
|
990
949
|
self.decode = mqtt_raw_dispatch(self);
|
|
991
|
-
return self
|
|
992
|
-
},
|
|
950
|
+
return self },
|
|
993
951
|
}
|
|
994
952
|
}
|
|
995
953
|
|
|
@@ -1513,7 +1471,7 @@ class MQTTCore extends MQTTBase {
|
|
|
1513
1471
|
|
|
1514
1472
|
return this} }
|
|
1515
1473
|
|
|
1516
|
-
const version = '0.6.
|
|
1474
|
+
const version = '0.6.4';
|
|
1517
1475
|
|
|
1518
1476
|
const MQTTClient_v4 = /* #__PURE__ */
|
|
1519
1477
|
with_topic_path_router(
|