u8-mqtt 0.5.2 → 0.6.0
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/README.md +23 -6
- package/cjs/basic-v4.cjs +293 -284
- package/cjs/basic-v4.cjs.map +1 -1
- package/cjs/basic-v5.cjs +307 -288
- package/cjs/basic-v5.cjs.map +1 -1
- package/cjs/full-v4.cjs +1538 -0
- package/cjs/full-v4.cjs.map +1 -0
- package/cjs/full-v5.cjs +1812 -0
- package/cjs/full-v5.cjs.map +1 -0
- package/cjs/index.cjs +320 -302
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +305 -296
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +319 -300
- package/cjs/v5.cjs.map +1 -1
- package/code/_cmdid_dispatch.jsy +45 -69
- package/code/_conn.jsy +96 -72
- package/code/_dispatch.jsy +36 -28
- package/code/_utils.jsy +17 -0
- package/code/base.jsy +35 -63
- package/code/core.jsy +78 -56
- package/code/full-v4.js +20 -0
- package/code/full-v5.js +29 -0
- package/code/router_path.jsy +2 -1
- package/esm/basic-v4.js +293 -284
- package/esm/basic-v4.js.map +1 -1
- package/esm/basic-v5.js +307 -288
- package/esm/basic-v5.js.map +1 -1
- package/esm/deno/basic-v4.js +297 -288
- package/esm/deno/basic-v4.js.map +1 -1
- package/esm/deno/basic-v5.js +311 -292
- package/esm/deno/basic-v5.js.map +1 -1
- package/esm/deno/full-v4.js +1526 -0
- package/esm/deno/full-v4.js.map +1 -0
- package/esm/deno/full-v5.js +1798 -0
- package/esm/deno/full-v5.js.map +1 -0
- package/esm/deno/index.js +324 -305
- package/esm/deno/index.js.map +1 -1
- package/esm/deno/v4.js +309 -300
- package/esm/deno/v4.js.map +1 -1
- package/esm/deno/v5.js +323 -304
- package/esm/deno/v5.js.map +1 -1
- package/esm/full-v4.js +1526 -0
- package/esm/full-v4.js.map +1 -0
- package/esm/full-v5.js +1798 -0
- package/esm/full-v5.js.map +1 -0
- package/esm/index.js +320 -301
- package/esm/index.js.map +1 -1
- package/esm/node/basic-v4.js +293 -284
- package/esm/node/basic-v4.js.map +1 -1
- package/esm/node/basic-v4.mjs +293 -284
- package/esm/node/basic-v4.mjs.map +1 -1
- package/esm/node/basic-v5.js +307 -288
- package/esm/node/basic-v5.js.map +1 -1
- package/esm/node/basic-v5.mjs +307 -288
- package/esm/node/basic-v5.mjs.map +1 -1
- package/esm/node/full-v4.js +1529 -0
- package/esm/node/full-v4.js.map +1 -0
- package/esm/node/full-v4.mjs +1529 -0
- package/esm/node/full-v4.mjs.map +1 -0
- package/esm/node/full-v5.js +1801 -0
- package/esm/node/full-v5.js.map +1 -0
- package/esm/node/full-v5.mjs +1801 -0
- package/esm/node/full-v5.mjs.map +1 -0
- package/esm/node/index.js +320 -301
- package/esm/node/index.js.map +1 -1
- package/esm/node/index.mjs +320 -301
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.js +305 -296
- package/esm/node/v4.js.map +1 -1
- package/esm/node/v4.mjs +305 -296
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.js +319 -300
- package/esm/node/v5.js.map +1 -1
- package/esm/node/v5.mjs +319 -300
- package/esm/node/v5.mjs.map +1 -1
- package/esm/v4.js +305 -296
- package/esm/v4.js.map +1 -1
- package/esm/v5.js +319 -300
- package/esm/v5.js.map +1 -1
- package/esm/web/basic-v4.js +293 -284
- 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 +307 -288
- 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 +1526 -0
- package/esm/web/full-v4.js.map +1 -0
- package/esm/web/full-v4.min.js +1 -0
- 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 +1798 -0
- package/esm/web/full-v5.js.map +1 -0
- package/esm/web/full-v5.min.js +1 -0
- 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 +320 -301
- 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 +305 -296
- 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 +319 -300
- 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 +7 -8
|
@@ -0,0 +1,1798 @@
|
|
|
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)
|
|
7
|
+
return a
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
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]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const mqtt_props = /* #__PURE__ */
|
|
40
|
+
init_mqtt_props();
|
|
41
|
+
|
|
42
|
+
function init_mqtt_props() {
|
|
43
|
+
let mqtt_props = new Map();
|
|
44
|
+
|
|
45
|
+
let entries = [
|
|
46
|
+
[ 0x01, 'u8', 'payload_format_indicator'],
|
|
47
|
+
[ 0x02, 'u32', 'message_expiry_interval'],
|
|
48
|
+
[ 0x03, 'utf8', 'content_type'],
|
|
49
|
+
[ 0x08, 'utf8', 'response_topic'],
|
|
50
|
+
[ 0x09, 'bin', 'correlation_data'],
|
|
51
|
+
[ 0x0B, 'vint', 'subscription_identifier'],
|
|
52
|
+
[ 0x11, 'u32', 'session_expiry_interval'],
|
|
53
|
+
[ 0x12, 'utf8', 'assigned_client_identifier'],
|
|
54
|
+
[ 0x13, 'u16', 'server_keep_alive'],
|
|
55
|
+
[ 0x15, 'utf8', 'authentication_method'],
|
|
56
|
+
[ 0x16, 'bin', 'authentication_data'],
|
|
57
|
+
[ 0x17, 'u8', 'request_problem_information'],
|
|
58
|
+
[ 0x18, 'u32', 'will_delay_interval'],
|
|
59
|
+
[ 0x19, 'u8', 'request_response_information'],
|
|
60
|
+
[ 0x1A, 'utf8', 'response_information'],
|
|
61
|
+
[ 0x1C, 'utf8', 'server_reference'],
|
|
62
|
+
[ 0x1F, 'utf8', 'reason_string'],
|
|
63
|
+
[ 0x21, 'u16', 'receive_maximum'],
|
|
64
|
+
[ 0x22, 'u16', 'topic_alias_maximum'],
|
|
65
|
+
[ 0x23, 'u16', 'topic_alias'],
|
|
66
|
+
[ 0x24, 'u8', 'maximum_qos'],
|
|
67
|
+
[ 0x25, 'u8', 'retain_available'],
|
|
68
|
+
[ 0x26, 'pair', 'user_properties', {op: 'kv_obj'}],
|
|
69
|
+
[ 0x27, 'u32', 'maximum_packet_size'],
|
|
70
|
+
[ 0x28, 'u8', 'wildcard_subscription_available'],
|
|
71
|
+
[ 0x29, 'u8', 'subscription_identifiers_available', {op: 'u8_vec'}],
|
|
72
|
+
[ 0x2A, 'u8', 'shared_subscription_available'],
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
for (let [id, type, name, extra] of entries) {
|
|
76
|
+
let prop_obj = {id, type, name, ...extra};
|
|
77
|
+
mqtt_props.set(prop_obj.id, prop_obj);
|
|
78
|
+
mqtt_props.set(prop_obj.name, prop_obj);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return mqtt_props
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
class U8_Reason extends Number {
|
|
85
|
+
static of(v, pkt_kind, by_kind) {
|
|
86
|
+
let self = new this(v);
|
|
87
|
+
self.reason = by_kind?.[pkt_kind]?.get(v) || pkt_kind;
|
|
88
|
+
return self
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
class mqtt_reader_v4 {
|
|
93
|
+
static of(buf) { return this.prototype.of(buf) }
|
|
94
|
+
of(buf) {
|
|
95
|
+
let step = (width, k) => (k=0|step.k, step.k=k+width, k);
|
|
96
|
+
return {__proto__: this, buf, step}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
has_more() {
|
|
100
|
+
return this.buf.byteLength > (this.step.k|0)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
u8() {
|
|
104
|
+
return this.buf[this.step(1)]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
u16() {
|
|
108
|
+
let {buf, step} = this, i = step(2);
|
|
109
|
+
return (buf[i]<<8) | buf[i+1]
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
u32() {
|
|
113
|
+
let {buf, step} = this, i = step(4);
|
|
114
|
+
return (buf[i]<<24) | (buf[i+1]<<16) | (buf[i+2]<<8) | buf[i+3]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
vint() {
|
|
118
|
+
let {buf, step} = this;
|
|
119
|
+
let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
|
|
120
|
+
step(vi - vi0);
|
|
121
|
+
return n
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
bin() {
|
|
125
|
+
let {buf, step} = this, i = step(2);
|
|
126
|
+
let len = (buf[i]<<8) | buf[i+1];
|
|
127
|
+
i = step(len);
|
|
128
|
+
return buf.subarray(i, i+len)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
utf8() { return new TextDecoder('utf-8').decode(this.bin()) }
|
|
132
|
+
pair() { return [ this.utf8(), this.utf8() ] }
|
|
133
|
+
|
|
134
|
+
flags(FlagsType) { return new FlagsType(this.buf[this.step(1)]) }
|
|
135
|
+
|
|
136
|
+
reason(pkt_kind) {
|
|
137
|
+
let v = this.buf[this.step(1)];
|
|
138
|
+
if (null != v)
|
|
139
|
+
return U8_Reason.of(v, pkt_kind, this._reasons_by)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
flush() {
|
|
143
|
+
let {buf, step} = this;
|
|
144
|
+
this.step = this.buf = null;
|
|
145
|
+
return buf.subarray(step.k|0)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let mqtt_reader_v5$1 = class mqtt_reader_v5 extends mqtt_reader_v4 {
|
|
151
|
+
props() {
|
|
152
|
+
let {buf, step} = this;
|
|
153
|
+
let [n, vi, vi0] = decode_varint$1(buf, step.k|0);
|
|
154
|
+
step(n + vi - vi0);
|
|
155
|
+
if (0 === n) return null
|
|
156
|
+
|
|
157
|
+
let res={}, fork = this.of(buf.subarray(vi, step.k|0));
|
|
158
|
+
while (fork.has_more()) {
|
|
159
|
+
let v, pk = fork.u8(), pt = mqtt_props.get( pk );
|
|
160
|
+
|
|
161
|
+
if (!pt) {
|
|
162
|
+
res.error = `Unknown mqtt_prop enum ${pk}`;
|
|
163
|
+
return res
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
v = fork[pt.type]();
|
|
167
|
+
if (pt.op) // accumulate operation
|
|
168
|
+
v = fork[pt.op](res[pt.name], v);
|
|
169
|
+
|
|
170
|
+
res[pt.name] = v;
|
|
171
|
+
}
|
|
172
|
+
return res
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
kv_obj(obj=Object.create(null), [k,v]) {
|
|
176
|
+
obj[k] = v;
|
|
177
|
+
return obj
|
|
178
|
+
}
|
|
179
|
+
u8_vec(vec=[], u8) {
|
|
180
|
+
vec.push(u8);
|
|
181
|
+
return vec
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/*
|
|
185
|
+
vbuf() {
|
|
186
|
+
let {buf, step} = this
|
|
187
|
+
let [n, vi, vi0] = decode_varint(buf, step.k|0)
|
|
188
|
+
step(n + vi - vi0)
|
|
189
|
+
return 0 === n ? null
|
|
190
|
+
: buf.subarray(vi, step.k|0)
|
|
191
|
+
}
|
|
192
|
+
*/
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
|
|
196
|
+
mqtt_reader = class extends mqtt_reader {
|
|
197
|
+
static reasons(pkt_type, ...reason_entries) {
|
|
198
|
+
let proto = this.prototype;
|
|
199
|
+
proto._reasons_by = {... proto._reasons_by};
|
|
200
|
+
|
|
201
|
+
let lut = (proto._reasons_by[pkt_type] ||= new Map());
|
|
202
|
+
for (let [u8, reason] of reason_entries)
|
|
203
|
+
lut.set( u8, reason );
|
|
204
|
+
|
|
205
|
+
return this
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
for (let fn_info of info_fn_list)
|
|
210
|
+
fn_info(mqtt_reader);
|
|
211
|
+
|
|
212
|
+
return mqtt_reader
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
class mqtt_writer_v4 {
|
|
216
|
+
static of() { return this.prototype.of() }
|
|
217
|
+
of() { return {__proto__: this, $:[]} }
|
|
218
|
+
|
|
219
|
+
static init() { return this }
|
|
220
|
+
|
|
221
|
+
as_pkt(pkt_id) { return this.pack([pkt_id]) }
|
|
222
|
+
|
|
223
|
+
push(...z) { this.$.push(...z); }
|
|
224
|
+
pack(hdr) {
|
|
225
|
+
let z, i, n=0, parts = this.$;
|
|
226
|
+
this.$ = false;
|
|
227
|
+
for (z of parts) n += z.length;
|
|
228
|
+
|
|
229
|
+
hdr = encode_varint(n, hdr);
|
|
230
|
+
i = hdr.length;
|
|
231
|
+
|
|
232
|
+
let pkt = new Uint8Array(i + n);
|
|
233
|
+
pkt.set(hdr, 0);
|
|
234
|
+
for (z of parts) {
|
|
235
|
+
pkt.set(z, i);
|
|
236
|
+
i += z.length;
|
|
237
|
+
}
|
|
238
|
+
return pkt
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
u8(v) { this.push([ v & 0xff ]); }
|
|
242
|
+
u16(v) { this.push([ (v>>>8) & 0xff, v & 0xff ]); }
|
|
243
|
+
u32(v) { this.push([ (v>>>24) & 0xff, (v>>>16) & 0xff, (v>>>8) & 0xff, v & 0xff ]); }
|
|
244
|
+
vint(v) { this.push( encode_varint(v) );}
|
|
245
|
+
|
|
246
|
+
bin(u8_buf) {
|
|
247
|
+
if (! u8_buf) return this.u16(0)
|
|
248
|
+
if ('string' === typeof u8_buf)
|
|
249
|
+
return this.utf8(u8_buf)
|
|
250
|
+
|
|
251
|
+
if (u8_buf.length !== u8_buf.byteLength)
|
|
252
|
+
u8_buf = new Uint8Array(u8_buf);
|
|
253
|
+
|
|
254
|
+
this.u16(u8_buf.byteLength);
|
|
255
|
+
this.push(u8_buf);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
utf8(v) {
|
|
259
|
+
let u8_buf = new TextEncoder('utf-8').encode(v);
|
|
260
|
+
this.u16(u8_buf.byteLength);
|
|
261
|
+
this.push(u8_buf);
|
|
262
|
+
}
|
|
263
|
+
pair(k,v) { this.utf8(k); this.utf8(v); }
|
|
264
|
+
|
|
265
|
+
flags(v, enc_flags, b0=0) {
|
|
266
|
+
if (undefined !== v && isNaN(+v))
|
|
267
|
+
v = enc_flags(v, 0);
|
|
268
|
+
|
|
269
|
+
v |= b0;
|
|
270
|
+
this.push([v]);
|
|
271
|
+
return v
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
reason(v) { this.push([v | 0]); }
|
|
275
|
+
|
|
276
|
+
flush(buf) {
|
|
277
|
+
if (null != buf)
|
|
278
|
+
this.push(
|
|
279
|
+
'string' === typeof buf
|
|
280
|
+
? new TextEncoder('utf-8').encode(buf)
|
|
281
|
+
: buf );
|
|
282
|
+
|
|
283
|
+
this.push = false;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class mqtt_writer_v5 extends mqtt_writer_v4 {
|
|
289
|
+
props(props) {
|
|
290
|
+
if (! props)
|
|
291
|
+
return this.u8(0)
|
|
292
|
+
|
|
293
|
+
if (! Array.isArray(props))
|
|
294
|
+
props = props.entries
|
|
295
|
+
? Array.from(props.entries())
|
|
296
|
+
: Object.entries(props);
|
|
297
|
+
|
|
298
|
+
if (0 === props.length)
|
|
299
|
+
return this.u8(0)
|
|
300
|
+
|
|
301
|
+
let fork = this.of();
|
|
302
|
+
for (let [name, value] of props) {
|
|
303
|
+
let pt = mqtt_props.get(name);
|
|
304
|
+
if (!pt)
|
|
305
|
+
throw new Error(`Unknown mqtt_prop "${name}"`)
|
|
306
|
+
fork[pt.op || 'one'](value, pt);
|
|
307
|
+
}
|
|
308
|
+
this.push(fork.pack());
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
one(value, pt) {
|
|
312
|
+
this.u8(pt.id);
|
|
313
|
+
this[pt.type](value);
|
|
314
|
+
}
|
|
315
|
+
kv_obj(obj, pt) {
|
|
316
|
+
for (let kv of Object.entries(obj)) {
|
|
317
|
+
this.u8(pt.id);
|
|
318
|
+
this.pair(kv);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
u8_vec(vec, pt) {
|
|
322
|
+
for (let v of vec) {
|
|
323
|
+
this.u8(pt.id);
|
|
324
|
+
this.u8(v);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function mqtt_decode_connect(ns, mqtt_reader) {
|
|
330
|
+
class _connect_flags_ extends Number {
|
|
331
|
+
get reserved() { return this & 0x01 !== 0 }
|
|
332
|
+
get clean_start() { return this & 0x02 !== 0 }
|
|
333
|
+
get will_flag() { return this & 0x04 !== 0 }
|
|
334
|
+
get will_qos() { return (this >>> 3) & 0x3 }
|
|
335
|
+
get will_retain() { return this & 0x20 !== 0 }
|
|
336
|
+
get password() { return this & 0x40 !== 0 }
|
|
337
|
+
get username() { return this & 0x80 !== 0 }
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return ns[0x1] = (pkt, u8_body) => {
|
|
341
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
342
|
+
if ('MQTT' !== rdr.utf8())
|
|
343
|
+
throw new Error('Invalid mqtt_connect packet')
|
|
344
|
+
|
|
345
|
+
pkt._base_.mqtt_level = pkt.mqtt_level = rdr.u8();
|
|
346
|
+
|
|
347
|
+
let flags = pkt.flags =
|
|
348
|
+
rdr.flags(_connect_flags_);
|
|
349
|
+
|
|
350
|
+
pkt.keep_alive = rdr.u16();
|
|
351
|
+
|
|
352
|
+
if (5 <= pkt.mqtt_level)
|
|
353
|
+
pkt.props = rdr.props();
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
pkt.client_id = rdr.utf8();
|
|
358
|
+
if (flags.will_flag) {
|
|
359
|
+
let will = pkt.will = {};
|
|
360
|
+
if (5 <= pkt.mqtt_level)
|
|
361
|
+
will.props = rdr.props();
|
|
362
|
+
|
|
363
|
+
will.topic = rdr.utf8();
|
|
364
|
+
will.payload = rdr.bin();
|
|
365
|
+
will.qos = flags.will_qos;
|
|
366
|
+
will.retain = flags.will_retain;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (flags.username)
|
|
370
|
+
pkt.username = rdr.utf8();
|
|
371
|
+
|
|
372
|
+
if (flags.password)
|
|
373
|
+
pkt.password = rdr.bin();
|
|
374
|
+
return pkt }
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function mqtt_decode_connack(ns, mqtt_reader) {
|
|
378
|
+
class _connack_flags_ extends Number {
|
|
379
|
+
get session_present() { return this & 0x01 !== 0 }
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
return ns[0x2] = (pkt, u8_body) => {
|
|
383
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
384
|
+
|
|
385
|
+
pkt.flags =
|
|
386
|
+
rdr.flags(_connack_flags_);
|
|
387
|
+
|
|
388
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
389
|
+
if (5 <= pkt.mqtt_level)
|
|
390
|
+
pkt.props = rdr.props();
|
|
391
|
+
return pkt }
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
function _connack_v4(mqtt_reader) {
|
|
396
|
+
mqtt_reader.reasons('connack',
|
|
397
|
+
// MQTT 3.1.1
|
|
398
|
+
[ 0x00, 'Success'],
|
|
399
|
+
[ 0x01, 'Connection refused, unacceptable protocol version'],
|
|
400
|
+
[ 0x02, 'Connection refused, identifier rejected'],
|
|
401
|
+
[ 0x03, 'Connection refused, server unavailable'],
|
|
402
|
+
[ 0x04, 'Connection refused, bad user name or password'],
|
|
403
|
+
[ 0x05, 'Connection refused, not authorized'],
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function _connack_v5(mqtt_reader) {
|
|
408
|
+
_connack_v4(mqtt_reader);
|
|
409
|
+
|
|
410
|
+
mqtt_reader.reasons('connack',
|
|
411
|
+
// MQTT 5.0
|
|
412
|
+
[ 0x81, 'Malformed Packet'],
|
|
413
|
+
[ 0x82, 'Protocol Error'],
|
|
414
|
+
[ 0x83, 'Implementation specific error'],
|
|
415
|
+
[ 0x84, 'Unsupported Protocol Version'],
|
|
416
|
+
[ 0x85, 'Client Identifier not valid'],
|
|
417
|
+
[ 0x86, 'Bad User Name or Password'],
|
|
418
|
+
[ 0x87, 'Not authorized'],
|
|
419
|
+
[ 0x88, 'Server unavailable'],
|
|
420
|
+
[ 0x89, 'Server busy'],
|
|
421
|
+
[ 0x8A, 'Banned'],
|
|
422
|
+
[ 0x8C, 'Bad authentication method'],
|
|
423
|
+
[ 0x90, 'Topic Name invalid'],
|
|
424
|
+
[ 0x95, 'Packet too large'],
|
|
425
|
+
[ 0x97, 'Quota exceeded'],
|
|
426
|
+
[ 0x99, 'Payload format invalid'],
|
|
427
|
+
[ 0x9A, 'Retain not supported'],
|
|
428
|
+
[ 0x9B, 'QoS not supported'],
|
|
429
|
+
[ 0x9C, 'Use another server'],
|
|
430
|
+
[ 0x9D, 'Server moved'],
|
|
431
|
+
[ 0x9F, 'Connection rate exceeded'],
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function mqtt_decode_publish(ns, mqtt_reader) {
|
|
436
|
+
return ns[0x3] = (pkt, u8_body) => {
|
|
437
|
+
let {hdr} = pkt;
|
|
438
|
+
pkt.dup = Boolean(hdr & 0x8);
|
|
439
|
+
pkt.retain = Boolean(hdr & 0x1);
|
|
440
|
+
let qos = pkt.qos = (hdr>>1) & 0x3;
|
|
441
|
+
|
|
442
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
443
|
+
pkt.topic = rdr.utf8();
|
|
444
|
+
if (0 !== qos)
|
|
445
|
+
pkt.pkt_id = rdr.u16();
|
|
446
|
+
|
|
447
|
+
if (5 <= pkt.mqtt_level)
|
|
448
|
+
pkt.props = rdr.props();
|
|
449
|
+
|
|
450
|
+
pkt.payload = rdr.flush();
|
|
451
|
+
return pkt }
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function mqtt_decode_puback(ns, mqtt_reader) {
|
|
455
|
+
return ns[0x4] = (pkt, u8_body) => {
|
|
456
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
457
|
+
|
|
458
|
+
pkt.pkt_id = rdr.u16();
|
|
459
|
+
if (5 <= pkt.mqtt_level) {
|
|
460
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
461
|
+
pkt.props = rdr.props();
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return pkt }
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
function _puback_v5(mqtt_reader) {
|
|
469
|
+
mqtt_reader.reasons('puback',
|
|
470
|
+
// MQTT 5.0
|
|
471
|
+
[ 0x00, 'Success'],
|
|
472
|
+
[ 0x10, 'No matching subscribers'],
|
|
473
|
+
[ 0x80, 'Unspecified error'],
|
|
474
|
+
[ 0x83, 'Implementation specific error'],
|
|
475
|
+
[ 0x87, 'Not authorized'],
|
|
476
|
+
[ 0x90, 'Topic Name invalid'],
|
|
477
|
+
[ 0x91, 'Packet identifier in use'],
|
|
478
|
+
[ 0x97, 'Quota exceeded'],
|
|
479
|
+
[ 0x99, 'Payload format invalid'],
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function mqtt_decode_pubxxx(ns, mqtt_reader) {
|
|
484
|
+
return ns[0x5] = ns[0x6] = ns[0x7] = (pkt, u8_body) => {
|
|
485
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
486
|
+
|
|
487
|
+
pkt.pkt_id = rdr.u16();
|
|
488
|
+
pkt.reason = rdr.reason('pubxxx', mqtt_reader);
|
|
489
|
+
if (5 <= pkt.mqtt_level)
|
|
490
|
+
pkt.props = rdr.props();
|
|
491
|
+
return pkt }
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function _pubxxx_v4(mqtt_reader) {
|
|
495
|
+
mqtt_reader.reasons('pubxxx',
|
|
496
|
+
// MQTT 3.1.1
|
|
497
|
+
[ 0x00, 'Success' ],
|
|
498
|
+
[ 0x92, 'Packet Identifier not found' ],
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function mqtt_decode_subscribe(ns, mqtt_reader) {
|
|
503
|
+
class _subscription_options_ extends Number {
|
|
504
|
+
get qos() { return this & 0x3 }
|
|
505
|
+
get retain() { return this & 0x4 !== 0 }
|
|
506
|
+
get retain_handling() { return (this >> 2) & 0x3 }
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return ns[0x8] = (pkt, u8_body) => {
|
|
510
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
511
|
+
|
|
512
|
+
pkt.pkt_id = rdr.u16();
|
|
513
|
+
if (5 <= pkt.mqtt_level)
|
|
514
|
+
pkt.props = rdr.props();
|
|
515
|
+
|
|
516
|
+
let topic, opts, topic_list = pkt.topics = [];
|
|
517
|
+
while (rdr.has_more()) {
|
|
518
|
+
topic = rdr.utf8();
|
|
519
|
+
opts = rdr.flags(_subscription_options_);
|
|
520
|
+
topic_list.push({topic, opts});
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return pkt }
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function _mqtt_decode_suback(mqtt_reader) {
|
|
527
|
+
return (pkt, u8_body) => {
|
|
528
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
529
|
+
|
|
530
|
+
pkt.pkt_id = rdr.u16();
|
|
531
|
+
if (5 <= pkt.mqtt_level)
|
|
532
|
+
pkt.props = rdr.props();
|
|
533
|
+
|
|
534
|
+
let answers = pkt.answers = [];
|
|
535
|
+
while (rdr.has_more())
|
|
536
|
+
answers.push(
|
|
537
|
+
rdr.reason(pkt.type) );
|
|
538
|
+
|
|
539
|
+
return pkt }
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function mqtt_decode_suback(ns, mqtt_reader) {
|
|
543
|
+
return ns[0x9] = _mqtt_decode_suback(mqtt_reader)
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function _suback_v4(mqtt_reader) {
|
|
547
|
+
mqtt_reader.reasons('suback',
|
|
548
|
+
// MQTT 3.1.1
|
|
549
|
+
[ 0x00, 'Granted QoS 0'],
|
|
550
|
+
[ 0x01, 'Granted QoS 1'],
|
|
551
|
+
[ 0x02, 'Granted QoS 2'],
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function _suback_v5(mqtt_reader) {
|
|
556
|
+
_suback_v4(mqtt_reader);
|
|
557
|
+
|
|
558
|
+
mqtt_reader.reasons('suback',
|
|
559
|
+
// MQTT 5.0
|
|
560
|
+
[ 0x80, 'Unspecified error'],
|
|
561
|
+
[ 0x83, 'Implementation specific error'],
|
|
562
|
+
[ 0x87, 'Not authorized'],
|
|
563
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
564
|
+
[ 0x91, 'Packet Identifier in use'],
|
|
565
|
+
[ 0x97, 'Quota exceeded'],
|
|
566
|
+
[ 0x9E, 'Shared Subscriptions not supported'],
|
|
567
|
+
[ 0xA1, 'Subscription Identifiers not supported'],
|
|
568
|
+
[ 0xA2, 'Wildcard Subscriptions not supported'],
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function mqtt_decode_unsubscribe(ns, mqtt_reader) {
|
|
573
|
+
return ns[0xa] = (pkt, u8_body) => {
|
|
574
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
575
|
+
|
|
576
|
+
pkt.pkt_id = rdr.u16();
|
|
577
|
+
if (5 <= pkt.mqtt_level)
|
|
578
|
+
pkt.props = rdr.props();
|
|
579
|
+
|
|
580
|
+
let topic_list = pkt.topics = [];
|
|
581
|
+
while (rdr.has_more())
|
|
582
|
+
topic_list.push(rdr.utf8());
|
|
583
|
+
|
|
584
|
+
return pkt }
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function mqtt_decode_unsuback(ns, mqtt_reader) {
|
|
588
|
+
return ns[0xb] = _mqtt_decode_suback(mqtt_reader)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function _unsuback_v4(mqtt_reader) {
|
|
592
|
+
mqtt_reader.reasons('unsuback',
|
|
593
|
+
// MQTT 3.1.1
|
|
594
|
+
[ 0x00, 'Success'],
|
|
595
|
+
[ 0x11, 'No subscription existed'],
|
|
596
|
+
[ 0x80, 'Unspecified error'],
|
|
597
|
+
[ 0x83, 'Implementation specific error'],
|
|
598
|
+
[ 0x87, 'Not authorized'],
|
|
599
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
600
|
+
[ 0x91, 'Packet Identifier in use'],
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function mqtt_decode_pingxxx(ns) {
|
|
605
|
+
return ns[0xc] = ns[0xd] = pkt => pkt
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function mqtt_decode_disconnect(ns, mqtt_reader) {
|
|
609
|
+
return ns[0xe] = (pkt, u8_body) => {
|
|
610
|
+
if (u8_body && 5 <= pkt.mqtt_level) {
|
|
611
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
612
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
613
|
+
pkt.props = rdr.props();
|
|
614
|
+
}
|
|
615
|
+
return pkt }
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
function _disconnect_v5(mqtt_reader) {
|
|
620
|
+
mqtt_reader.reasons('disconnect',
|
|
621
|
+
// MQTT 5.0
|
|
622
|
+
[ 0x00, 'Normal disconnection'],
|
|
623
|
+
[ 0x04, 'Disconnect with Will Message'],
|
|
624
|
+
[ 0x80, 'Unspecified error'],
|
|
625
|
+
[ 0x81, 'Malformed Packet'],
|
|
626
|
+
[ 0x82, 'Protocol Error'],
|
|
627
|
+
[ 0x83, 'Implementation specific error'],
|
|
628
|
+
[ 0x87, 'Not authorized'],
|
|
629
|
+
[ 0x89, 'Server busy'],
|
|
630
|
+
[ 0x8B, 'Server shutting down'],
|
|
631
|
+
[ 0x8D, 'Keep Alive timeout'],
|
|
632
|
+
[ 0x8E, 'Session taken over'],
|
|
633
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
634
|
+
[ 0x90, 'Topic Name invalid'],
|
|
635
|
+
[ 0x93, 'Receive Maximum exceeded'],
|
|
636
|
+
[ 0x94, 'Topic Alias invalid'],
|
|
637
|
+
[ 0x95, 'Packet too large'],
|
|
638
|
+
[ 0x96, 'Message rate too high'],
|
|
639
|
+
[ 0x97, 'Quota exceeded'],
|
|
640
|
+
[ 0x98, 'Administrative action'],
|
|
641
|
+
[ 0x99, 'Payload format invalid'],
|
|
642
|
+
[ 0x9A, 'Retain not supported'],
|
|
643
|
+
[ 0x9B, 'QoS not supported'],
|
|
644
|
+
[ 0x9C, 'Use another server'],
|
|
645
|
+
[ 0x9D, 'Server moved'],
|
|
646
|
+
[ 0x9E, 'Shared Subscriptions not supported'],
|
|
647
|
+
[ 0x9F, 'Connection rate exceeded'],
|
|
648
|
+
[ 0xA0, 'Maximum connect time'],
|
|
649
|
+
[ 0xA1, 'Subscription Identifiers not supported'],
|
|
650
|
+
[ 0xA2, 'Wildcard Subscriptions not supported'],
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
function mqtt_decode_auth(ns, mqtt_reader) {
|
|
655
|
+
return ns[0xf] = (pkt, u8_body) => {
|
|
656
|
+
if ( 5 <= pkt.mqtt_level ) {
|
|
657
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
658
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
659
|
+
pkt.props = rdr.props();
|
|
660
|
+
}
|
|
661
|
+
return pkt }
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
function _auth_v5(mqtt_reader) {
|
|
666
|
+
mqtt_reader.reasons('auth',
|
|
667
|
+
// MQTT 5.0
|
|
668
|
+
[ 0x00, 'Success' ],
|
|
669
|
+
[ 0x18, 'Continue authentication' ],
|
|
670
|
+
[ 0x19, 'Re-authenticate' ],
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
function mqtt_encode_connect(ns, mqtt_writer) {
|
|
675
|
+
const _c_mqtt_proto = new Uint8Array([
|
|
676
|
+
0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
|
|
677
|
+
|
|
678
|
+
const _enc_flags_connect = flags => 0
|
|
679
|
+
| ( flags.reserved ? 0x01 : 0 )
|
|
680
|
+
| ( (flags.will_qos & 0x3) << 3 )
|
|
681
|
+
| ( flags.clean_start ? 0x02 : 0 )
|
|
682
|
+
| ( flags.will_flag ? 0x04 : 0 )
|
|
683
|
+
| ( flags.will_retain ? 0x20 : 0 )
|
|
684
|
+
| ( flags.password ? 0x40 : 0 )
|
|
685
|
+
| ( flags.username ? 0x80 : 0 );
|
|
686
|
+
|
|
687
|
+
const _enc_flags_will = will => 0x4
|
|
688
|
+
| ( (will.qos & 0x3) << 3 )
|
|
689
|
+
| ( will.retain ? 0x20 : 0 );
|
|
690
|
+
|
|
691
|
+
return ns.connect = ( mqtt_level, pkt ) => {
|
|
692
|
+
let wrt = mqtt_writer.of(pkt);
|
|
693
|
+
|
|
694
|
+
wrt.push(_c_mqtt_proto);
|
|
695
|
+
wrt.u8( mqtt_level );
|
|
696
|
+
|
|
697
|
+
let {will, username, password} = pkt;
|
|
698
|
+
let flags = wrt.flags(
|
|
699
|
+
pkt.flags,
|
|
700
|
+
_enc_flags_connect,
|
|
701
|
+
0 | (username ? 0x80 : 0)
|
|
702
|
+
| (password ? 0x40 : 0)
|
|
703
|
+
| (will ? _enc_flags_will(will) : 0) );
|
|
704
|
+
|
|
705
|
+
wrt.u16(pkt.keep_alive);
|
|
706
|
+
|
|
707
|
+
if (5 <= mqtt_level)
|
|
708
|
+
wrt.props(pkt.props);
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
wrt.utf8(pkt.client_id);
|
|
712
|
+
if (flags & 0x04) { // .will_flag
|
|
713
|
+
if (5 <= mqtt_level)
|
|
714
|
+
wrt.props(will.props);
|
|
715
|
+
|
|
716
|
+
wrt.utf8(will.topic);
|
|
717
|
+
wrt.bin(will.payload);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
if (flags & 0x80) // .username
|
|
721
|
+
wrt.utf8(username);
|
|
722
|
+
|
|
723
|
+
if (flags & 0x40) // .password
|
|
724
|
+
wrt.bin(password);
|
|
725
|
+
|
|
726
|
+
return wrt.as_pkt(0x10)
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function mqtt_encode_connack(ns, mqtt_writer) {
|
|
731
|
+
const _enc_flags_connack = flags =>
|
|
732
|
+
flags.session_present ? 1 : 0;
|
|
733
|
+
|
|
734
|
+
return ns.connack = (mqtt_level, pkt) => {
|
|
735
|
+
let wrt = mqtt_writer.of(pkt);
|
|
736
|
+
|
|
737
|
+
wrt.flags( pkt.flags, _enc_flags_connack );
|
|
738
|
+
|
|
739
|
+
if (5 <= mqtt_level) {
|
|
740
|
+
wrt.reason( pkt.reason );
|
|
741
|
+
wrt.props( pkt.props );
|
|
742
|
+
|
|
743
|
+
} else {
|
|
744
|
+
wrt.reason( pkt.return_code || pkt.reason );
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
return wrt.as_pkt(0x20)
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
function mqtt_encode_publish(ns, mqtt_writer) {
|
|
752
|
+
return ns.publish = ( mqtt_level, pkt ) => {
|
|
753
|
+
let qos = (pkt.qos & 0x3) << 1;
|
|
754
|
+
let wrt = mqtt_writer.of(pkt);
|
|
755
|
+
|
|
756
|
+
wrt.utf8(pkt.topic);
|
|
757
|
+
if (0 !== qos)
|
|
758
|
+
wrt.u16(pkt.pkt_id);
|
|
759
|
+
|
|
760
|
+
if ( 5 <= mqtt_level) {
|
|
761
|
+
wrt.props(pkt.props);
|
|
762
|
+
wrt.flush(pkt.payload);
|
|
763
|
+
} else {
|
|
764
|
+
wrt.flush(pkt.payload);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
return wrt.as_pkt(
|
|
768
|
+
0x30 | qos | (pkt.dup ? 0x8 : 0) | (pkt.retain ? 0x1 : 0) )
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function mqtt_encode_puback(ns, mqtt_writer) {
|
|
773
|
+
return ns.puback = ( mqtt_level, pkt ) => {
|
|
774
|
+
let wrt = mqtt_writer.of(pkt);
|
|
775
|
+
|
|
776
|
+
wrt.u16(pkt.pkt_id);
|
|
777
|
+
if (5 <= mqtt_level) {
|
|
778
|
+
wrt.reason(pkt.reason);
|
|
779
|
+
wrt.props(pkt.props);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
return wrt.as_pkt(0x40)
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function mqtt_encode_pubxxx(ns, mqtt_writer) {
|
|
787
|
+
ns.pubrec = _enc_pubxxx(0x50);
|
|
788
|
+
ns.pubrel = _enc_pubxxx(0x62);
|
|
789
|
+
ns.pubcomp = _enc_pubxxx(0x70);
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
function _enc_pubxxx(hdr) {
|
|
793
|
+
return ( mqtt_level, pkt ) => {
|
|
794
|
+
let wrt = mqtt_writer.of(pkt);
|
|
795
|
+
|
|
796
|
+
wrt.u16(pkt.pkt_id);
|
|
797
|
+
if (5 <= mqtt_level) {
|
|
798
|
+
wrt.props(pkt.props);
|
|
799
|
+
wrt.reason(pkt.reason);
|
|
800
|
+
|
|
801
|
+
} else {
|
|
802
|
+
wrt.reason( pkt.return_code || pkt.reason );
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
return wrt.as_pkt(hdr)
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
function mqtt_encode_subscribe(ns, mqtt_writer) {
|
|
811
|
+
const _enc_subscribe_flags = opts => 0
|
|
812
|
+
| ( opts.qos & 0x3 )
|
|
813
|
+
| ( opts.retain ? 0x4 : 0 )
|
|
814
|
+
| ( (opts.retain_handling & 0x3) << 2 );
|
|
815
|
+
|
|
816
|
+
return ns.subscribe = ( mqtt_level, pkt ) => {
|
|
817
|
+
let wrt = mqtt_writer.of(pkt);
|
|
818
|
+
|
|
819
|
+
wrt.u16(pkt.pkt_id);
|
|
820
|
+
if (5 <= mqtt_level)
|
|
821
|
+
wrt.props(pkt.props);
|
|
822
|
+
|
|
823
|
+
let f0 = _enc_subscribe_flags(pkt);
|
|
824
|
+
for (let each of pkt.topics) {
|
|
825
|
+
if ('string' === typeof each) {
|
|
826
|
+
wrt.utf8(each);
|
|
827
|
+
wrt.u8(f0);
|
|
828
|
+
} else {
|
|
829
|
+
let [topic, opts] =
|
|
830
|
+
Array.isArray(each) ? each
|
|
831
|
+
: [each.topic, each.opts];
|
|
832
|
+
|
|
833
|
+
wrt.utf8(topic);
|
|
834
|
+
if (undefined === opts) wrt.u8(f0);
|
|
835
|
+
else wrt.flags(opts, _enc_subscribe_flags);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
return wrt.as_pkt(0x82)
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function mqtt_encode_xxsuback(ns, mqtt_writer) {
|
|
844
|
+
ns.suback = _enc_xxsuback(0x90);
|
|
845
|
+
ns.unsuback = _enc_xxsuback(0xb0);
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
function _enc_xxsuback(hdr) {
|
|
849
|
+
return ( mqtt_level, pkt ) => {
|
|
850
|
+
let wrt = mqtt_writer.of(pkt);
|
|
851
|
+
|
|
852
|
+
wrt.u16(pkt.pkt_id);
|
|
853
|
+
if (5 <= mqtt_level)
|
|
854
|
+
wrt.props(pkt.props);
|
|
855
|
+
|
|
856
|
+
for (let ans of pkt.answers)
|
|
857
|
+
wrt.reason(ans);
|
|
858
|
+
|
|
859
|
+
return wrt.as_pkt(hdr)
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
function mqtt_encode_unsubscribe(ns, mqtt_writer) {
|
|
865
|
+
return ns.unsubscribe = ( mqtt_level, pkt ) => {
|
|
866
|
+
let wrt = mqtt_writer.of(pkt);
|
|
867
|
+
|
|
868
|
+
wrt.u16(pkt.pkt_id);
|
|
869
|
+
if (5 <= mqtt_level)
|
|
870
|
+
wrt.props(pkt.props);
|
|
871
|
+
|
|
872
|
+
for (let topic of pkt.topics)
|
|
873
|
+
wrt.utf8(topic);
|
|
874
|
+
|
|
875
|
+
return wrt.as_pkt(0xa2)
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function mqtt_encode_pingxxx(ns) {
|
|
880
|
+
ns.pingreq = () => new Uint8Array([ 0xc0, 0 ]);
|
|
881
|
+
ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
function mqtt_encode_disconnect(ns, mqtt_writer) {
|
|
885
|
+
return ns.disconnect = ( mqtt_level, pkt ) => {
|
|
886
|
+
let wrt = mqtt_writer.of(pkt);
|
|
887
|
+
|
|
888
|
+
if (pkt && 5 <= mqtt_level) {
|
|
889
|
+
if (pkt.reason || pkt.props) {
|
|
890
|
+
wrt.reason(pkt.reason);
|
|
891
|
+
wrt.props(pkt.props);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
return wrt.as_pkt(0xe0)
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
function mqtt_encode_auth(ns, mqtt_writer) {
|
|
900
|
+
return ns.auth = ( mqtt_level, pkt ) => {
|
|
901
|
+
if (5 > mqtt_level)
|
|
902
|
+
throw new Error('Auth packets are only available after MQTT 5.x')
|
|
903
|
+
|
|
904
|
+
let wrt = mqtt_writer.of(pkt);
|
|
905
|
+
|
|
906
|
+
wrt.reason(pkt.reason);
|
|
907
|
+
wrt.props(pkt.props);
|
|
908
|
+
|
|
909
|
+
return wrt.as_pkt(0xf0)
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
const mqtt_reader_v5 = /* #__PURE__ */
|
|
914
|
+
mqtt_reader_info(
|
|
915
|
+
mqtt_reader_v5$1,
|
|
916
|
+
_connack_v5,
|
|
917
|
+
_puback_v5,
|
|
918
|
+
_pubxxx_v4,
|
|
919
|
+
_suback_v5,
|
|
920
|
+
_unsuback_v4,
|
|
921
|
+
_disconnect_v5,
|
|
922
|
+
_auth_v5,
|
|
923
|
+
);
|
|
924
|
+
|
|
925
|
+
const mqtt_decode_v5 = [
|
|
926
|
+
mqtt_decode_connect,
|
|
927
|
+
mqtt_decode_connack,
|
|
928
|
+
mqtt_decode_publish,
|
|
929
|
+
mqtt_decode_puback,
|
|
930
|
+
mqtt_decode_pubxxx,
|
|
931
|
+
mqtt_decode_subscribe,
|
|
932
|
+
mqtt_decode_suback,
|
|
933
|
+
mqtt_decode_unsubscribe,
|
|
934
|
+
mqtt_decode_unsuback,
|
|
935
|
+
mqtt_decode_pingxxx,
|
|
936
|
+
mqtt_decode_disconnect,
|
|
937
|
+
mqtt_decode_auth,
|
|
938
|
+
];
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
const mqtt_encode_v5 = [
|
|
942
|
+
mqtt_encode_connect,
|
|
943
|
+
mqtt_encode_connack,
|
|
944
|
+
mqtt_encode_publish,
|
|
945
|
+
mqtt_encode_puback,
|
|
946
|
+
mqtt_encode_pubxxx,
|
|
947
|
+
mqtt_encode_subscribe,
|
|
948
|
+
mqtt_encode_xxsuback,
|
|
949
|
+
mqtt_encode_unsubscribe,
|
|
950
|
+
mqtt_encode_pingxxx,
|
|
951
|
+
mqtt_encode_disconnect,
|
|
952
|
+
mqtt_encode_auth,
|
|
953
|
+
];
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
const mqtt_opts_v5 =
|
|
957
|
+
{ decode_fns: mqtt_decode_v5,
|
|
958
|
+
mqtt_reader: mqtt_reader_v5,
|
|
959
|
+
encode_fns: mqtt_encode_v5,
|
|
960
|
+
mqtt_writer: mqtt_writer_v5, };
|
|
961
|
+
|
|
962
|
+
function parse(str, loose) {
|
|
963
|
+
if (str instanceof RegExp) return { keys:false, pattern:str };
|
|
964
|
+
var c, o, tmp, ext, keys=[], pattern='', arr = str.split('/');
|
|
965
|
+
arr[0] || arr.shift();
|
|
966
|
+
|
|
967
|
+
while (tmp = arr.shift()) {
|
|
968
|
+
c = tmp[0];
|
|
969
|
+
if (c === '*') {
|
|
970
|
+
keys.push('wild');
|
|
971
|
+
pattern += '/(.*)';
|
|
972
|
+
} else if (c === ':') {
|
|
973
|
+
o = tmp.indexOf('?', 1);
|
|
974
|
+
ext = tmp.indexOf('.', 1);
|
|
975
|
+
keys.push( tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length) );
|
|
976
|
+
pattern += !!~o && !~ext ? '(?:/([^/]+?))?' : '/([^/]+?)';
|
|
977
|
+
if (!!~ext) pattern += (!!~o ? '?' : '') + '\\' + tmp.substring(ext);
|
|
978
|
+
} else {
|
|
979
|
+
pattern += '/' + tmp;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
return {
|
|
984
|
+
keys: keys,
|
|
985
|
+
pattern: new RegExp('^' + pattern + (loose ? '(?=$|\/)' : '\/?$'), 'i')
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
const _isfn = v => typeof v === 'function';
|
|
990
|
+
const _isstr = v => typeof v === 'string';
|
|
991
|
+
|
|
992
|
+
function _interval(fn_callback) {
|
|
993
|
+
let tid;
|
|
994
|
+
return (( td ) => {
|
|
995
|
+
tid = clearInterval(tid);
|
|
996
|
+
if (td) {
|
|
997
|
+
tid = setInterval(fn_callback, 1000 * td);
|
|
998
|
+
|
|
999
|
+
// ensure the interval allows the Deno event loop to exit
|
|
1000
|
+
Deno.unrefTimer(tid);
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
return true} }) }
|
|
1005
|
+
|
|
1006
|
+
/*
|
|
1007
|
+
class AbstractTopicRouter ::
|
|
1008
|
+
async invoke(pkt, ctx) ::
|
|
1009
|
+
add(topic_route, ...args) ::
|
|
1010
|
+
remove(topic_route, priority) ::
|
|
1011
|
+
clear(priority) ::
|
|
1012
|
+
find(topic) :: // optional
|
|
1013
|
+
mqtt_topic(topic_route)
|
|
1014
|
+
*/
|
|
1015
|
+
|
|
1016
|
+
const with_topic_router = mqtt_topic_router =>
|
|
1017
|
+
MQTTKlass => class extends MQTTKlass {
|
|
1018
|
+
static _aliases() {
|
|
1019
|
+
return super._aliases() +
|
|
1020
|
+
' sub_topic:subscribe_topic unsub_topic:unsubscribe_topic'}
|
|
1021
|
+
|
|
1022
|
+
_init_router(opt, self, target) {
|
|
1023
|
+
this._subs = [];
|
|
1024
|
+
let router = this.router = target.router =
|
|
1025
|
+
mqtt_topic_router(opt, this, target);
|
|
1026
|
+
return router?.invoke}
|
|
1027
|
+
|
|
1028
|
+
on_sub(suback, pkt) {
|
|
1029
|
+
suback.pkt = pkt;
|
|
1030
|
+
this._subs.push(suback);
|
|
1031
|
+
return suback}
|
|
1032
|
+
subs_settled() {
|
|
1033
|
+
return Promise.allSettled(
|
|
1034
|
+
this._subs.splice(0,Infinity)) }
|
|
1035
|
+
|
|
1036
|
+
// alias: sub_topic
|
|
1037
|
+
subscribe_topic(topic_route, ...args) {
|
|
1038
|
+
let router = this.router;
|
|
1039
|
+
router.add(topic_route, true, args.pop() );// handler
|
|
1040
|
+
let topic = router.mqtt_topic(topic_route);
|
|
1041
|
+
this.subscribe(topic, ...args );// ex, topic_prefix
|
|
1042
|
+
return this }// fluent api -- return this and track side effects
|
|
1043
|
+
|
|
1044
|
+
// alias: unsub_topic
|
|
1045
|
+
unsubscribe_topic(topic_route, ...args) {
|
|
1046
|
+
let router = this.router;
|
|
1047
|
+
router.remove(topic_route, true);
|
|
1048
|
+
let topic = router.mqtt_topic(topic_route);
|
|
1049
|
+
return this.unsubscribe(topic, ...args ) }// topic_prefix
|
|
1050
|
+
|
|
1051
|
+
// add topic handlers without corresponding subscribe packet
|
|
1052
|
+
on_topic(...args) {
|
|
1053
|
+
this.router.add(...args);
|
|
1054
|
+
return this} };
|
|
1055
|
+
|
|
1056
|
+
// Use [regexparam][] for url-like topic parsing
|
|
1057
|
+
// [regexparam]: https://github.com/lukeed/regexparam
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
const with_topic_path_router = /* #__PURE__ */
|
|
1061
|
+
with_topic_router(mqtt_topic_path_router);
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
const mqtt_topic = topic_route =>
|
|
1065
|
+
topic_route
|
|
1066
|
+
.replace(/[*].*$/, '#')
|
|
1067
|
+
.replace(/:\w[^\/]*/g, '+');
|
|
1068
|
+
|
|
1069
|
+
/* From the [MQTT v5 Spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Topic_Names_and)
|
|
1070
|
+
4.7.1.2 Multi-level wildcard -- (‘#’ U+0023)
|
|
1071
|
+
... MUST be specified either on its own or following a topic level separator.
|
|
1072
|
+
In either case it MUST be the last character specified in the Topic Filter
|
|
1073
|
+
|
|
1074
|
+
4.7.1.3 Single-level wildcard -- (‘+’ U+002B)
|
|
1075
|
+
...it MUST occupy an entire level of the filter.
|
|
1076
|
+
*/
|
|
1077
|
+
|
|
1078
|
+
const as_topic_path = (topic_route, id) =>(
|
|
1079
|
+
id=1,
|
|
1080
|
+
topic_route
|
|
1081
|
+
.replace(/#$/, '*' )// replace MQTT '#' multi-level wildcard at end
|
|
1082
|
+
.replace(/\+/g, () => `:$${id++}` ) );// replace MQTT '+' single-level wildcards
|
|
1083
|
+
|
|
1084
|
+
function _ignore(pkt, params, ctx) {ctx.done = true;}
|
|
1085
|
+
|
|
1086
|
+
function mqtt_topic_path_router() {
|
|
1087
|
+
let pri_lsts = [[],[]], rm = Symbol();
|
|
1088
|
+
let find = topic => _routes_iter(pri_lsts, topic);
|
|
1089
|
+
|
|
1090
|
+
// return duck-type compatible with AbstractTopicRouter in ./with_topic_router
|
|
1091
|
+
return {find, mqtt_topic,
|
|
1092
|
+
add(topic_route, ...args) {
|
|
1093
|
+
let fn = args.pop();
|
|
1094
|
+
let priority = args.pop();
|
|
1095
|
+
|
|
1096
|
+
if (! _isfn(fn)) {
|
|
1097
|
+
if (fn) {throw new TypeError()}
|
|
1098
|
+
fn = _ignore;}
|
|
1099
|
+
|
|
1100
|
+
let rte = parse(as_topic_path(topic_route));
|
|
1101
|
+
|
|
1102
|
+
rte.key = topic_route;
|
|
1103
|
+
rte.tgt = fn;
|
|
1104
|
+
pri_lsts[priority ? 0 : 1].push(rte);
|
|
1105
|
+
return this}
|
|
1106
|
+
|
|
1107
|
+
, remove(topic_route, priority) {
|
|
1108
|
+
let lst = pri_lsts[priority ? 0 : 1];
|
|
1109
|
+
return _route_remove([lst], topic_route)}
|
|
1110
|
+
|
|
1111
|
+
, clear(priority) {
|
|
1112
|
+
pri_lsts[priority ? 0 : 1] = [];
|
|
1113
|
+
if (null == priority) {
|
|
1114
|
+
pri_lsts[1] = []; } }// null clears both lists
|
|
1115
|
+
|
|
1116
|
+
, async invoke(pkt, ctx) {
|
|
1117
|
+
ctx.idx = 0;
|
|
1118
|
+
ctx.rm = rm;
|
|
1119
|
+
|
|
1120
|
+
for (let [fn, params] of find(pkt.topic)) {
|
|
1121
|
+
let res = await fn(pkt, params, ctx);
|
|
1122
|
+
|
|
1123
|
+
if (rm === res) {
|
|
1124
|
+
_route_remove(pri_lsts, fn);}
|
|
1125
|
+
|
|
1126
|
+
if (ctx.done) {
|
|
1127
|
+
break}
|
|
1128
|
+
else ctx.idx++;}
|
|
1129
|
+
|
|
1130
|
+
if (1 === pkt.qos) {
|
|
1131
|
+
await ctx.mqtt.puback(pkt);} } } }
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
function * _routes_iter(all_route_lists, topic) {
|
|
1135
|
+
topic = topic.replace(/^[\/]*/, '/'); // ensure '/' prefix for regexparam library
|
|
1136
|
+
for (let route_list of all_route_lists) {
|
|
1137
|
+
for (let {keys, pattern, tgt} of route_list) {
|
|
1138
|
+
let match = pattern.exec(topic);
|
|
1139
|
+
if (match) {
|
|
1140
|
+
let params = keys
|
|
1141
|
+
? keys.reduce(
|
|
1142
|
+
(o, k, i) => (o[k] = match[1+i], o)
|
|
1143
|
+
, {})
|
|
1144
|
+
: match.groups ?? match;
|
|
1145
|
+
yield [tgt, params];} } } }
|
|
1146
|
+
|
|
1147
|
+
|
|
1148
|
+
function _route_remove(all_route_lists, query) {
|
|
1149
|
+
let fn_match = route =>(
|
|
1150
|
+
route===query
|
|
1151
|
+
|| route.tgt===query
|
|
1152
|
+
|| route.key===query);
|
|
1153
|
+
for (let lst of all_route_lists) {
|
|
1154
|
+
let i = lst.findIndex(fn_match);
|
|
1155
|
+
if (0 <= i) {
|
|
1156
|
+
lst.splice(i,1);
|
|
1157
|
+
return true} }
|
|
1158
|
+
return false}
|
|
1159
|
+
|
|
1160
|
+
/*
|
|
1161
|
+
export function decode_varint_loop(u8, i=0) {
|
|
1162
|
+
let i0 = i
|
|
1163
|
+
let shift = 0, n = (u8[i] & 0x7f)
|
|
1164
|
+
while ( 0x80 & u8[i++] )
|
|
1165
|
+
n |= (u8[i] & 0x7f) << (shift += 7)
|
|
1166
|
+
|
|
1167
|
+
return [n, i, i0]
|
|
1168
|
+
}
|
|
1169
|
+
*/
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
function decode_varint(u8, i=0) {
|
|
1173
|
+
let i0 = i;
|
|
1174
|
+
// unrolled for a max of 4 chains
|
|
1175
|
+
let n = (u8[i] & 0x7f) << 0;
|
|
1176
|
+
if ( 0x80 & u8[i++] ) {
|
|
1177
|
+
n |= (u8[i] & 0x7f) << 7;
|
|
1178
|
+
if ( 0x80 & u8[i++] ) {
|
|
1179
|
+
n |= (u8[i] & 0x7f) << 14;
|
|
1180
|
+
if ( 0x80 & u8[i++] ) {
|
|
1181
|
+
n |= (u8[i] & 0x7f) << 21;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
return [n, i, i0]
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
function mqtt_raw_dispatch(opt) {
|
|
1189
|
+
let u8 = new Uint8Array(0);
|
|
1190
|
+
return u8_buf => {
|
|
1191
|
+
u8 = 0 === u8.byteLength
|
|
1192
|
+
? u8_buf : _u8_join(u8, u8_buf);
|
|
1193
|
+
|
|
1194
|
+
let res = [];
|
|
1195
|
+
while (1) {
|
|
1196
|
+
let [len_body, len_vh] = decode_varint(u8, 1);
|
|
1197
|
+
let len_pkt = len_body + len_vh;
|
|
1198
|
+
|
|
1199
|
+
if ( u8.byteLength < len_pkt )
|
|
1200
|
+
return res
|
|
1201
|
+
|
|
1202
|
+
let b0 = u8[0];
|
|
1203
|
+
let u8_body = 0 === len_body ? null
|
|
1204
|
+
: u8.subarray(len_vh, len_pkt);
|
|
1205
|
+
|
|
1206
|
+
u8 = u8.subarray(len_pkt);
|
|
1207
|
+
|
|
1208
|
+
let pkt = opt.decode_pkt(b0, u8_body, opt);
|
|
1209
|
+
if (null != pkt)
|
|
1210
|
+
res.push( pkt );
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
function _u8_join(a, b) {
|
|
1216
|
+
let alen = a.byteLength, r = new Uint8Array(alen + b.byteLength);
|
|
1217
|
+
r.set(a, 0);
|
|
1218
|
+
r.set(b, alen);
|
|
1219
|
+
return r
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
|
|
1223
|
+
|
|
1224
|
+
function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
|
|
1225
|
+
pkt_ctx = {
|
|
1226
|
+
__proto__: pkt_ctx || opts.pkt_ctx,
|
|
1227
|
+
mqtt_level,
|
|
1228
|
+
get hdr() { return this.b0 & 0xf },
|
|
1229
|
+
get id() { return this.b0 >>> 4 },
|
|
1230
|
+
get type() { return _pkt_types[this.b0 >>> 4] },
|
|
1231
|
+
};
|
|
1232
|
+
|
|
1233
|
+
let op, _decode_by_id=[], _encode_by_type={};
|
|
1234
|
+
for (op of opts.encode_fns)
|
|
1235
|
+
op(_encode_by_type, opts.mqtt_writer);
|
|
1236
|
+
for (op of opts.decode_fns)
|
|
1237
|
+
op(_decode_by_id, opts.mqtt_reader);
|
|
1238
|
+
|
|
1239
|
+
return {
|
|
1240
|
+
pkt_ctx,
|
|
1241
|
+
|
|
1242
|
+
encode_pkt(type, pkt) {
|
|
1243
|
+
return _encode_by_type[type]( mqtt_level, pkt ) },
|
|
1244
|
+
|
|
1245
|
+
decode_pkt(b0, u8_body) {
|
|
1246
|
+
if (b0.map) // Uint8Array in first arg
|
|
1247
|
+
return mqtt_raw_dispatch(this)(b0)[0]
|
|
1248
|
+
|
|
1249
|
+
let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
|
|
1250
|
+
return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
|
|
1251
|
+
|
|
1252
|
+
mqtt_stream() {
|
|
1253
|
+
let self = { __proto__: this, pkt_ctx: { __proto__: pkt_ctx } };
|
|
1254
|
+
self.pkt_ctx._base_ = self.pkt_ctx;
|
|
1255
|
+
self.decode = mqtt_raw_dispatch(self);
|
|
1256
|
+
return self
|
|
1257
|
+
},
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
async function _mqtt_cmd_evt(target, answer, pkt, ctx) {
|
|
1262
|
+
/* target : on_mqtt_type = {
|
|
1263
|
+
mqtt_pkt(pkt, ctx) {}, // generic
|
|
1264
|
+
|
|
1265
|
+
mqtt_auth(pkt, ctx) {},
|
|
1266
|
+
mqtt_connect(pkt, ctx) {},
|
|
1267
|
+
mqtt_connack(pkt, ctx) {},
|
|
1268
|
+
mqtt_disconnect(pkt, ctx) {},
|
|
1269
|
+
|
|
1270
|
+
mqtt_publish(pkt, ctx) {},
|
|
1271
|
+
mqtt_subscribe(pkt, ctx) {},
|
|
1272
|
+
mqtt_unsubscribe(pkt, ctx) {},
|
|
1273
|
+
|
|
1274
|
+
mqtt_pingreq(pkt, ctx) {},
|
|
1275
|
+
mqtt_pingresp(pkt, ctx) {},
|
|
1276
|
+
} */
|
|
1277
|
+
|
|
1278
|
+
let pkt_fn = target[`mqtt_${pkt.type}`] || target.mqtt_pkt;
|
|
1279
|
+
await pkt_fn?.call(target, pkt, ctx);}
|
|
1280
|
+
|
|
1281
|
+
function _mqtt_cmd_type(target, answer, pkt, ctx) {
|
|
1282
|
+
answer(pkt.type, pkt);
|
|
1283
|
+
_mqtt_cmd_evt(target, answer, pkt, ctx);}
|
|
1284
|
+
|
|
1285
|
+
function _mqtt_cmd_id(target, answer, pkt) {
|
|
1286
|
+
answer(pkt.pkt_id, pkt);}
|
|
1287
|
+
|
|
1288
|
+
|
|
1289
|
+
const _mqtt_cmdids =[
|
|
1290
|
+
_ => {} // 0x0 reserved
|
|
1291
|
+
, _mqtt_cmd_evt // 0x1 connect
|
|
1292
|
+
, _mqtt_cmd_type // 0x2 connack
|
|
1293
|
+
, _mqtt_cmd_evt // 0x3 publish
|
|
1294
|
+
, _mqtt_cmd_id // 0x4 puback
|
|
1295
|
+
, _mqtt_cmd_id // 0x5 pubrec
|
|
1296
|
+
, _mqtt_cmd_id // 0x6 pubrel
|
|
1297
|
+
, _mqtt_cmd_id // 0x7 pubcomp
|
|
1298
|
+
, _mqtt_cmd_evt // 0x8 subscribe
|
|
1299
|
+
, _mqtt_cmd_id // 0x9 suback
|
|
1300
|
+
, _mqtt_cmd_evt // 0xa unsubscribe
|
|
1301
|
+
, _mqtt_cmd_id // 0xb unsuback
|
|
1302
|
+
, _mqtt_cmd_type // 0xc pingreq
|
|
1303
|
+
, _mqtt_cmd_type // 0xd pingresp
|
|
1304
|
+
, _mqtt_cmd_evt // 0xe disconnect
|
|
1305
|
+
, _mqtt_cmd_type ];// 0xf auth
|
|
1306
|
+
|
|
1307
|
+
function _mqtt_dispatch(opt, target) {
|
|
1308
|
+
let hashbelt=[], rotate_ts=0;
|
|
1309
|
+
// default rotate at 1s across 5 buckets
|
|
1310
|
+
let { td: rotate_td=1000, n: rotate_n=5 } = opt?.rotate || {};
|
|
1311
|
+
|
|
1312
|
+
// Promise / future scaffolding
|
|
1313
|
+
let _pkt_id=100, _ftr_key; // use _ftr_key to reuse _by_key closure
|
|
1314
|
+
let _ftr_by_key = fn_answer => hashbelt[0].set(_ftr_key, fn_answer);
|
|
1315
|
+
|
|
1316
|
+
on_mqtt([]); // init hashbelt and rotate_ts
|
|
1317
|
+
return [on_mqtt, pkt_future]
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
function pkt_future(pkt_or_key) {
|
|
1321
|
+
if (! _isstr(pkt_or_key)) {
|
|
1322
|
+
_pkt_id = (_pkt_id + 1) & 0xffff; // 16-bit unsigned short
|
|
1323
|
+
_ftr_key = pkt_or_key.pkt_id = _pkt_id;}
|
|
1324
|
+
else _ftr_key = pkt_or_key;
|
|
1325
|
+
|
|
1326
|
+
return new Promise(_ftr_by_key)}
|
|
1327
|
+
|
|
1328
|
+
function answer(key, pkt) {
|
|
1329
|
+
for (let map of hashbelt) {
|
|
1330
|
+
let fn_answer = map.get(key);
|
|
1331
|
+
if (fn_answer) {
|
|
1332
|
+
map.delete(key);
|
|
1333
|
+
|
|
1334
|
+
fn_answer([pkt, /*err*/]); // option/maybe monad
|
|
1335
|
+
return true} }
|
|
1336
|
+
return false}
|
|
1337
|
+
|
|
1338
|
+
function on_mqtt(pkt_list, ctx) {
|
|
1339
|
+
for (let pkt of pkt_list) {
|
|
1340
|
+
_mqtt_cmdids[pkt.id](target, answer, pkt, ctx);}
|
|
1341
|
+
|
|
1342
|
+
// rotate after rotate_ts
|
|
1343
|
+
let now = Date.now();
|
|
1344
|
+
if (now > rotate_ts) {
|
|
1345
|
+
rotate_ts = rotate_td + now;
|
|
1346
|
+
hashbelt.unshift(new Map());
|
|
1347
|
+
while (hashbelt.length > rotate_n) {
|
|
1348
|
+
for (let fn_answer of hashbelt.pop().values()) {
|
|
1349
|
+
fn_answer([/*pkt*/, 'expired']); } } } } }// option/maybe monad
|
|
1350
|
+
|
|
1351
|
+
class MQTTError extends Error {
|
|
1352
|
+
constructor(mqtt_pkt, reason=mqtt_pkt.reason) {
|
|
1353
|
+
// use hex-encoded reasons to match MQTT spec documentation
|
|
1354
|
+
super(`[0x${reason.toString(16)}] ${reason.reason}`);
|
|
1355
|
+
this.mqtt_pkt = mqtt_pkt;
|
|
1356
|
+
this.reason = reason;} }
|
|
1357
|
+
|
|
1358
|
+
class MQTTBase {
|
|
1359
|
+
// Handshaking Packets
|
|
1360
|
+
async connect(pkt={}) {
|
|
1361
|
+
let cid = pkt.client_id;
|
|
1362
|
+
if (! _isstr(cid)) {
|
|
1363
|
+
// see init_client_id implementation in core.jsy
|
|
1364
|
+
pkt.client_id = cid = this.client_id || this.init_client_id(cid);}
|
|
1365
|
+
this.client_id = cid;
|
|
1366
|
+
|
|
1367
|
+
if (null == pkt.keep_alive) {
|
|
1368
|
+
pkt.keep_alive = 60;}
|
|
1369
|
+
|
|
1370
|
+
let response = await this._send0('connect', pkt, 'connack');
|
|
1371
|
+
if (0 != response[0].reason) {// compare to 0 to coerce to number
|
|
1372
|
+
throw new this.MQTTError(response[0])}
|
|
1373
|
+
return this.conn.on_conn(pkt, response)}
|
|
1374
|
+
|
|
1375
|
+
async disconnect(pkt={}) {
|
|
1376
|
+
let response = await this._send0('disconnect', pkt);
|
|
1377
|
+
return this.conn.on_dis(pkt, response)}
|
|
1378
|
+
|
|
1379
|
+
async auth(pkt={}) {
|
|
1380
|
+
let response = await this._send0('auth', pkt, 'auth');
|
|
1381
|
+
if (response[0].reason) {
|
|
1382
|
+
throw new this.MQTTError(response[0])}
|
|
1383
|
+
return this.conn.on_auth(pkt, response)}
|
|
1384
|
+
|
|
1385
|
+
ping() {return this._send('pingreq', null, 'pingresp')}
|
|
1386
|
+
puback({pkt_id}) {return this._send('puback', {pkt_id})}
|
|
1387
|
+
|
|
1388
|
+
// alias: sub
|
|
1389
|
+
subscribe(pkt, ex, topic_prefix) {
|
|
1390
|
+
pkt = _as_topics(pkt, ex, topic_prefix);
|
|
1391
|
+
let suback = this._send('subscribe', pkt, pkt);
|
|
1392
|
+
return this.on_sub?.(suback, pkt) ?? suback}
|
|
1393
|
+
|
|
1394
|
+
// alias: unsub
|
|
1395
|
+
unsubscribe(pkt, ex, topic_prefix) {
|
|
1396
|
+
pkt = _as_topics(pkt, ex, topic_prefix);
|
|
1397
|
+
return this._send('unsubscribe', pkt, pkt)}
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
post(topic, payload, pub_opt) {// qos:0
|
|
1401
|
+
return this.pub({topic, payload, qos:0}, pub_opt)}
|
|
1402
|
+
send(topic, payload, pub_opt) {// qos:1
|
|
1403
|
+
return this.pub({topic, payload, qos:1}, pub_opt)}
|
|
1404
|
+
store(topic, payload, pub_opt) {// qos:1, retain: 1
|
|
1405
|
+
return this.pub({topic, payload, qos:1, retain: 1}, pub_opt)}
|
|
1406
|
+
|
|
1407
|
+
// alias: json_post
|
|
1408
|
+
obj_post(topic, msg, pub_opt) {// qos:0
|
|
1409
|
+
return this.pub({topic, msg, arg: 'msg', qos:0}, pub_opt)}
|
|
1410
|
+
// alias: json_send
|
|
1411
|
+
obj_send(topic, msg, pub_opt) {// qos:1
|
|
1412
|
+
return this.pub({topic, msg, arg: 'msg', qos:1}, pub_opt)}
|
|
1413
|
+
// alias: json_store
|
|
1414
|
+
obj_store(topic, msg, pub_opt) {// qos:1, retain: 1
|
|
1415
|
+
return this.pub({topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)}
|
|
1416
|
+
|
|
1417
|
+
// alias: publish -- because 'pub' is shorter for semantic aliases above
|
|
1418
|
+
async pub(pkt, pub_opt) {
|
|
1419
|
+
if (undefined === pkt.payload) {
|
|
1420
|
+
if (_isfn(pub_opt)) {
|
|
1421
|
+
// pub_opt as a function is fn_encode value
|
|
1422
|
+
pub_opt = {fn_encode: pub_opt};}
|
|
1423
|
+
|
|
1424
|
+
let msg = pkt.msg, fn_encode = pub_opt?.fn_encode;
|
|
1425
|
+
if (null == msg || _isfn(msg)) {
|
|
1426
|
+
// when msg is a function, return closure using fn_encode
|
|
1427
|
+
if (msg) {pub_opt = {...pub_opt, fn_encode: msg};}
|
|
1428
|
+
// return a single-value closure to publish packets
|
|
1429
|
+
return v => this.pub({...pkt, [pkt.arg || 'payload']: v}, pub_opt)}
|
|
1430
|
+
|
|
1431
|
+
// Encode payload from msg; fn_encode allows alternative to JSON.stringify
|
|
1432
|
+
pkt.payload = fn_encode
|
|
1433
|
+
? await fn_encode(msg)
|
|
1434
|
+
: JSON.stringify(msg);}
|
|
1435
|
+
|
|
1436
|
+
if (pub_opt) {
|
|
1437
|
+
if (pub_opt.props) {
|
|
1438
|
+
pkt.props = pub_opt.props;}
|
|
1439
|
+
if (pub_opt.xform) {
|
|
1440
|
+
pkt = pub_opt.xform(pkt) || pkt;} }
|
|
1441
|
+
|
|
1442
|
+
return this._send('publish', pkt,
|
|
1443
|
+
pkt.qos ? pkt : null ) }// key
|
|
1444
|
+
|
|
1445
|
+
|
|
1446
|
+
// Internal API
|
|
1447
|
+
|
|
1448
|
+
/* async _send0(type, pkt) -- provided by conn and transport */
|
|
1449
|
+
/* async _send(type, pkt) -- provided by conn and transport */
|
|
1450
|
+
|
|
1451
|
+
_init_dispatch(opt) {
|
|
1452
|
+
this.constructor?._once_();
|
|
1453
|
+
let target ={__proto__: opt.on_mqtt_type};
|
|
1454
|
+
target.mqtt_publish ||=
|
|
1455
|
+
this._init_router?.(opt, this, target);
|
|
1456
|
+
return _mqtt_dispatch(opt, target)}
|
|
1457
|
+
|
|
1458
|
+
static _aliases() {
|
|
1459
|
+
return ' publish:pub sub:subscribe unsub:unsubscribe json_post:obj_post json_send:obj_send json_store:obj_store'}
|
|
1460
|
+
|
|
1461
|
+
static _once_(klass=this) {
|
|
1462
|
+
klass._once_ = _=>0;
|
|
1463
|
+
var alias, name, p = klass.prototype;
|
|
1464
|
+
p.MQTTError = MQTTError;
|
|
1465
|
+
for (alias of klass._aliases().split(/\s+/)) {
|
|
1466
|
+
[alias, name] = alias.split(':');
|
|
1467
|
+
p[alias] = p[name];} } }
|
|
1468
|
+
|
|
1469
|
+
|
|
1470
|
+
function _as_topics(pkt, ex, topic_prefix) {
|
|
1471
|
+
if (ex?.trim) {// string
|
|
1472
|
+
topic_prefix = ex;
|
|
1473
|
+
ex = null;}
|
|
1474
|
+
|
|
1475
|
+
pkt =(
|
|
1476
|
+
pkt.trim // string
|
|
1477
|
+
? {topics:[pkt], ... ex}
|
|
1478
|
+
: pkt[Symbol.iterator]
|
|
1479
|
+
? {topics:[... pkt], ... ex}
|
|
1480
|
+
: ex ? {...pkt, ...ex}
|
|
1481
|
+
: pkt);
|
|
1482
|
+
|
|
1483
|
+
if (topic_prefix) {
|
|
1484
|
+
// particularly useful with shared queues, e.g.
|
|
1485
|
+
// topic_prefix = '$share/some-queue-name/'
|
|
1486
|
+
let _prefix_topics = v =>
|
|
1487
|
+
v.trim ? topic_prefix+v : v.map(_prefix_topics);
|
|
1488
|
+
|
|
1489
|
+
pkt.topics = pkt.topics.map(_prefix_topics);}
|
|
1490
|
+
return pkt}
|
|
1491
|
+
|
|
1492
|
+
const _defer_obj = o =>(
|
|
1493
|
+
o.p = new Promise((a,e) => { o.a=a; o.e=e; })
|
|
1494
|
+
, o);
|
|
1495
|
+
|
|
1496
|
+
function _dfn_reset(client, attr, fn_after) {
|
|
1497
|
+
// a resetable deferred for a function
|
|
1498
|
+
let self = {set}, afn = async (...args) => (await self.p)(...args);
|
|
1499
|
+
return set()
|
|
1500
|
+
|
|
1501
|
+
function set() {
|
|
1502
|
+
if (afn !== client[attr]) {
|
|
1503
|
+
_defer_obj(self).p.then(fn_after, _=>0);
|
|
1504
|
+
client[attr] = afn;}
|
|
1505
|
+
return self} }
|
|
1506
|
+
|
|
1507
|
+
function _mqtt_conn(opt, client, [on_mqtt, pkt_future]) {
|
|
1508
|
+
let _abort;
|
|
1509
|
+
let _dfn_send0 = _dfn_reset(client, '_send0', // client._send0 getter/setter
|
|
1510
|
+
_=> client.conn_emit('on_live', conn.has_connected));
|
|
1511
|
+
let _dfn_ready = _dfn_reset(client, '_send', // client._send getter/setter
|
|
1512
|
+
_=> client.conn_emit('on_ready'));
|
|
1513
|
+
let _keep_alive_ival = _interval (() =>client._send0('pingreq') );// resettable interval for keep_alive ping
|
|
1514
|
+
|
|
1515
|
+
let conn = Object.create({
|
|
1516
|
+
ping: (td=conn.keep_alive) => _keep_alive_ival(td)
|
|
1517
|
+
|
|
1518
|
+
, on_conn(pkt, response) {
|
|
1519
|
+
conn.has_connected = true;
|
|
1520
|
+
conn.keep_alive = opt.keep_alive || response[0].props?.server_keep_alive || pkt.keep_alive;
|
|
1521
|
+
client.conn_emit('on_conn');
|
|
1522
|
+
return opt.use_auth
|
|
1523
|
+
? response // wait on enhanced authentication step
|
|
1524
|
+
: conn.on_auth(null, response) }// otherwise, connect is also auth
|
|
1525
|
+
|
|
1526
|
+
, on_auth(pkt, response) {
|
|
1527
|
+
_dfn_ready.a(_dfn_send0.p);
|
|
1528
|
+
if (0 != opt.keep_alive) {
|
|
1529
|
+
conn.ping();}
|
|
1530
|
+
client.conn_emit('on_auth', !pkt);
|
|
1531
|
+
return response}
|
|
1532
|
+
|
|
1533
|
+
, on_dis(pkt, response) {
|
|
1534
|
+
conn.reset(false);
|
|
1535
|
+
return response}
|
|
1536
|
+
|
|
1537
|
+
, reset(err) {
|
|
1538
|
+
if (err) {
|
|
1539
|
+
_dfn_send0.e(err); }// send error to uses of _send0 (connect, auth)
|
|
1540
|
+
_abort.e(err); // abort in-progress connections
|
|
1541
|
+
|
|
1542
|
+
delete conn.is_set;
|
|
1543
|
+
conn.ready = handshake();
|
|
1544
|
+
client.conn_emit('on_disconnect', false===err, err);}
|
|
1545
|
+
|
|
1546
|
+
, abort() {
|
|
1547
|
+
_dfn_ready.e(err); // abort all messages awaiting ready state
|
|
1548
|
+
return conn.reset(err)}
|
|
1549
|
+
|
|
1550
|
+
, async setup(gate, send_u8_pkt, init_msg_loop) {
|
|
1551
|
+
if (conn.is_set) {
|
|
1552
|
+
throw new Error() }// already in-progress
|
|
1553
|
+
|
|
1554
|
+
conn.is_set = true;
|
|
1555
|
+
await gate;
|
|
1556
|
+
|
|
1557
|
+
// setup send/recv MQTT parsing context
|
|
1558
|
+
let mqtt_ctx = client.mqtt_ctx.mqtt_stream();
|
|
1559
|
+
|
|
1560
|
+
{// setup inbound message loop
|
|
1561
|
+
let sess_ctx = {mqtt: client}; // mutable session context
|
|
1562
|
+
let on_mqtt_chunk = u8 => on_mqtt(mqtt_ctx.decode(u8), sess_ctx);
|
|
1563
|
+
init_msg_loop(on_mqtt_chunk, conn);}
|
|
1564
|
+
|
|
1565
|
+
// setup outbound message path and transport connection
|
|
1566
|
+
send_u8_pkt = await send_u8_pkt;
|
|
1567
|
+
_dfn_send0.a(
|
|
1568
|
+
async (type, pkt, key) => {
|
|
1569
|
+
let res = undefined !== key
|
|
1570
|
+
? pkt_future(key) : true;
|
|
1571
|
+
|
|
1572
|
+
await send_u8_pkt(
|
|
1573
|
+
mqtt_ctx.encode_pkt(type, pkt));
|
|
1574
|
+
return res} ); } });
|
|
1575
|
+
|
|
1576
|
+
conn.ready = handshake();
|
|
1577
|
+
return conn
|
|
1578
|
+
|
|
1579
|
+
async function handshake() {
|
|
1580
|
+
_abort = _defer_obj({});
|
|
1581
|
+
|
|
1582
|
+
_keep_alive_ival(0); // clearInterval on keep alive ping
|
|
1583
|
+
_dfn_send0.set(); // reset client._send0 if necessary
|
|
1584
|
+
_dfn_ready.set(); // reset client._send if necessary
|
|
1585
|
+
|
|
1586
|
+
try {
|
|
1587
|
+
// set client._send0 as passtrhough after transport connection
|
|
1588
|
+
client._send0 = await Promise.race([_dfn_send0.p, _abort.p]);
|
|
1589
|
+
|
|
1590
|
+
// set client._send as passtrhough after ready
|
|
1591
|
+
client._send = await Promise.race([_dfn_ready.p, _abort.p]);
|
|
1592
|
+
return true}
|
|
1593
|
+
catch (err) {
|
|
1594
|
+
return false} } }
|
|
1595
|
+
|
|
1596
|
+
const pkt_api ={
|
|
1597
|
+
utf8(u8) {return new TextDecoder('utf-8').decode(u8 || this.payload )}
|
|
1598
|
+
, json(u8) {return JSON.parse( this.utf8(u8) || null )}
|
|
1599
|
+
, text(u8) {return this.utf8(u8)} };
|
|
1600
|
+
|
|
1601
|
+
const opt_default ={
|
|
1602
|
+
sess_stg: globalThis.sessionStorage};
|
|
1603
|
+
|
|
1604
|
+
class MQTTCore extends MQTTBase {
|
|
1605
|
+
constructor(opt) {
|
|
1606
|
+
super();
|
|
1607
|
+
this.with(opt);
|
|
1608
|
+
opt ={...opt_default, ...opt};
|
|
1609
|
+
// settings for MQTTCore
|
|
1610
|
+
this.sess_stg = opt.sess_stg;
|
|
1611
|
+
// setup connection and dispatch
|
|
1612
|
+
this.conn = _mqtt_conn(opt, this,
|
|
1613
|
+
this._init_dispatch(opt)); }
|
|
1614
|
+
|
|
1615
|
+
with(fns_ns) {
|
|
1616
|
+
for (let [k,v] of Object.entries(fns_ns)) {
|
|
1617
|
+
if (_isfn(v)) {this[k] = v;} }
|
|
1618
|
+
return this}
|
|
1619
|
+
|
|
1620
|
+
|
|
1621
|
+
static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
|
|
1622
|
+
let klass = class extends this {};
|
|
1623
|
+
klass.prototype.mqtt_ctx =
|
|
1624
|
+
mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
|
|
1625
|
+
return klass}
|
|
1626
|
+
|
|
1627
|
+
|
|
1628
|
+
async conn_emit(evt, arg, err_arg) {
|
|
1629
|
+
this.log_conn?.(evt, arg, err_arg);
|
|
1630
|
+
try {
|
|
1631
|
+
let fn_evt = this[await evt]; // microtask break using `await evt`
|
|
1632
|
+
if (fn_evt) {
|
|
1633
|
+
await fn_evt.call(this, this, arg, err_arg);}
|
|
1634
|
+
else if (err_arg) {throw err_arg} }
|
|
1635
|
+
catch (err) {
|
|
1636
|
+
this.on_error(err, evt);} }
|
|
1637
|
+
|
|
1638
|
+
on_error(err, evt) {
|
|
1639
|
+
console.warn('[[u8-mqtt error: %s]]', evt, err); }
|
|
1640
|
+
log_conn(evt, arg, err_arg) {
|
|
1641
|
+
console.info('[[u8-mqtt conn: %s]]', evt, arg, err_arg); }
|
|
1642
|
+
|
|
1643
|
+
|
|
1644
|
+
// automatic Client Id for connect()
|
|
1645
|
+
init_client_id(parts=['u8-mqtt--','']) {
|
|
1646
|
+
let sess_stg = this.sess_stg;
|
|
1647
|
+
let key, cid = sess_stg?.getItem(key=parts.join(' '));
|
|
1648
|
+
if (! cid) {
|
|
1649
|
+
cid = parts.join(Math.random().toString(36).slice(2));
|
|
1650
|
+
sess_stg?.setItem(key, cid);}
|
|
1651
|
+
return cid}
|
|
1652
|
+
|
|
1653
|
+
|
|
1654
|
+
on_live(client, is_reconnect) {
|
|
1655
|
+
if (is_reconnect) {
|
|
1656
|
+
return client.connect()} }
|
|
1657
|
+
|
|
1658
|
+
// on_ready(client) ::
|
|
1659
|
+
// on_reconnect(client) ::
|
|
1660
|
+
on_disconnect(client, intentional) {
|
|
1661
|
+
if (! intentional) {
|
|
1662
|
+
return client.on_reconnect?.()} }
|
|
1663
|
+
|
|
1664
|
+
_use_conn(fn_reconnect) {
|
|
1665
|
+
return (this.reconnect = fn_reconnect)?.()}
|
|
1666
|
+
with_autoreconnect(opt=2000) {
|
|
1667
|
+
if (opt.toFixed) {opt ={delay: opt};}
|
|
1668
|
+
return this.with({
|
|
1669
|
+
on_reconnect() {
|
|
1670
|
+
this.delay(opt.delay || 2000)
|
|
1671
|
+
.then(this.reconnect)
|
|
1672
|
+
.then(opt.reconnect, opt.error);} }) }
|
|
1673
|
+
|
|
1674
|
+
delay(ms) {
|
|
1675
|
+
return new Promise(done => setTimeout(done, ms)) }
|
|
1676
|
+
|
|
1677
|
+
with_async_iter(async_iter, write_u8_pkt) {
|
|
1678
|
+
this.conn.setup(async_iter,
|
|
1679
|
+
write_u8_pkt,
|
|
1680
|
+
async (on_mqtt_chunk, conn) => {
|
|
1681
|
+
try {
|
|
1682
|
+
async_iter = await async_iter;
|
|
1683
|
+
for await (let chunk of async_iter)
|
|
1684
|
+
on_mqtt_chunk(chunk);
|
|
1685
|
+
conn.reset();}
|
|
1686
|
+
catch (err) {
|
|
1687
|
+
conn.reset(err);} } );
|
|
1688
|
+
return this}
|
|
1689
|
+
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
with_tcp(...opt) {
|
|
1694
|
+
opt = this._conn_opt(opt);
|
|
1695
|
+
opt.transport = 'tcp';
|
|
1696
|
+
return this._use_conn (() =>
|
|
1697
|
+
this.with_deno_iter(
|
|
1698
|
+
Deno.connect(opt)) ) }
|
|
1699
|
+
|
|
1700
|
+
with_tls(...opt) {
|
|
1701
|
+
opt = this._conn_opt(opt);
|
|
1702
|
+
return this._use_conn (() =>
|
|
1703
|
+
this.with_deno_iter(
|
|
1704
|
+
Deno.connectTls(opt)) ) }
|
|
1705
|
+
|
|
1706
|
+
with_deno_iter(transport) {
|
|
1707
|
+
return this.with_async_iter(
|
|
1708
|
+
transport.then(Deno.iter),
|
|
1709
|
+
transport.then(write_stream =>
|
|
1710
|
+
u8_pkt => write_stream.write(u8_pkt)) ) }
|
|
1711
|
+
|
|
1712
|
+
_conn_opt([a0, a1, a2]) {
|
|
1713
|
+
// (port, hostname, options) or (url, options)
|
|
1714
|
+
if (Number.isFinite(a0)) {
|
|
1715
|
+
return {...a2, port: a0, hostname: a1}}
|
|
1716
|
+
|
|
1717
|
+
a0 = new URL(a0);
|
|
1718
|
+
return {...a1, port: a0.port, hostname: a0.hostname}}
|
|
1719
|
+
|
|
1720
|
+
|
|
1721
|
+
|
|
1722
|
+
|
|
1723
|
+
|
|
1724
|
+
|
|
1725
|
+
|
|
1726
|
+
|
|
1727
|
+
|
|
1728
|
+
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
|
|
1732
|
+
|
|
1733
|
+
|
|
1734
|
+
|
|
1735
|
+
|
|
1736
|
+
|
|
1737
|
+
|
|
1738
|
+
|
|
1739
|
+
|
|
1740
|
+
|
|
1741
|
+
with_stream(read_stream, write_stream) {
|
|
1742
|
+
if (undefined === write_stream) {
|
|
1743
|
+
write_stream = read_stream;}
|
|
1744
|
+
|
|
1745
|
+
return this.with_async_iter(read_stream,
|
|
1746
|
+
u8_pkt => write_stream.write(u8_pkt)) }
|
|
1747
|
+
|
|
1748
|
+
|
|
1749
|
+
with_websock(websock) {
|
|
1750
|
+
if (! websock?.send) {
|
|
1751
|
+
websock = new URL(websock || 'ws://127.0.0.1:9001');
|
|
1752
|
+
return this._use_conn (() =>
|
|
1753
|
+
this.with_websock(
|
|
1754
|
+
new WebSocket(websock, ['mqtt'])) ) }
|
|
1755
|
+
|
|
1756
|
+
websock.binaryType = 'arraybuffer';
|
|
1757
|
+
|
|
1758
|
+
let ws_ready, readyState = websock.readyState;
|
|
1759
|
+
if (1 !== readyState) {
|
|
1760
|
+
if (0 !== readyState) {
|
|
1761
|
+
throw new Error('WS readyState') }
|
|
1762
|
+
|
|
1763
|
+
ws_ready = new Promise(ready => websock.onopen = ready); }
|
|
1764
|
+
|
|
1765
|
+
this.conn.setup(ws_ready,
|
|
1766
|
+
u8_pkt => websock.send(u8_pkt),
|
|
1767
|
+
(on_mqtt_chunk, conn) => {
|
|
1768
|
+
websock.onmessage = evt =>(
|
|
1769
|
+
on_mqtt_chunk(new Uint8Array(evt.data)) );
|
|
1770
|
+
|
|
1771
|
+
websock.onclose = evt => {
|
|
1772
|
+
if (! evt.wasClean) {
|
|
1773
|
+
var err = new Error('websocket close');
|
|
1774
|
+
err.code = evt.code;
|
|
1775
|
+
err.reason = evt.reason;}
|
|
1776
|
+
|
|
1777
|
+
conn.reset(err);}; } );
|
|
1778
|
+
|
|
1779
|
+
return this} }
|
|
1780
|
+
|
|
1781
|
+
const version = '0.6.0-deno';
|
|
1782
|
+
|
|
1783
|
+
const MQTTClient_v4 = /* #__PURE__ */
|
|
1784
|
+
with_topic_path_router(
|
|
1785
|
+
MQTTCore.mqtt_ctx(4, mqtt_opts_v5) );
|
|
1786
|
+
|
|
1787
|
+
const MQTTClient_v5 = /* #__PURE__ */
|
|
1788
|
+
with_topic_path_router(
|
|
1789
|
+
MQTTCore.mqtt_ctx(5, mqtt_opts_v5) );
|
|
1790
|
+
|
|
1791
|
+
const mqtt_v4 = opt =>
|
|
1792
|
+
new MQTTClient_v4(opt);
|
|
1793
|
+
|
|
1794
|
+
const mqtt_v5 = opt =>
|
|
1795
|
+
new MQTTClient_v5(opt);
|
|
1796
|
+
|
|
1797
|
+
export { MQTTClient_v5 as MQTTClient, MQTTClient_v4, MQTTClient_v5, mqtt_v5 as default, mqtt_v5 as mqtt, mqtt_v4, mqtt_v5, version };
|
|
1798
|
+
//# sourceMappingURL=full-v5.js.map
|