u8-mqtt 0.3.2-0 → 0.4.1
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 +5 -5
- package/cjs/basic-v4.cjs +1200 -0
- package/cjs/basic-v4.cjs.map +1 -0
- package/cjs/basic-v5.cjs +1464 -0
- package/cjs/basic-v5.cjs.map +1 -0
- package/cjs/index.cjs +312 -269
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +307 -264
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +309 -265
- package/cjs/v5.cjs.map +1 -1
- package/code/_cmdid_dispatch.jsy +1 -3
- package/code/base.jsy +64 -89
- package/code/basic-v4.js +18 -0
- package/code/basic-v5.js +26 -0
- package/code/index.js +2 -1
- package/code/{_router.jsy → router_path.jsy} +36 -12
- package/code/v4.js +3 -1
- package/code/v5.js +5 -2
- package/code/with_topic_router.jsy +42 -0
- package/esm/deno/basic-v4.js +1188 -0
- package/esm/deno/basic-v4.js.map +1 -0
- package/esm/deno/basic-v5.js +1450 -0
- package/esm/deno/basic-v5.js.map +1 -0
- package/esm/deno/index.js +310 -266
- package/esm/deno/index.js.map +1 -1
- package/esm/deno/v4.js +307 -264
- package/esm/deno/v4.js.map +1 -1
- package/esm/deno/v5.js +309 -265
- package/esm/deno/v5.js.map +1 -1
- package/esm/node/basic-v4.js +1191 -0
- package/esm/node/basic-v4.js.map +1 -0
- package/esm/node/basic-v4.mjs +1191 -0
- package/esm/node/basic-v4.mjs.map +1 -0
- package/esm/node/basic-v5.js +1453 -0
- package/esm/node/basic-v5.js.map +1 -0
- package/esm/node/basic-v5.mjs +1453 -0
- package/esm/node/basic-v5.mjs.map +1 -0
- package/esm/node/index.js +310 -266
- package/esm/node/index.js.map +1 -1
- package/esm/node/index.mjs +310 -266
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.js +307 -264
- package/esm/node/v4.js.map +1 -1
- package/esm/node/v4.mjs +307 -264
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.js +309 -265
- package/esm/node/v5.js.map +1 -1
- package/esm/node/v5.mjs +309 -265
- package/esm/node/v5.mjs.map +1 -1
- package/esm/web/basic-v4.js +1188 -0
- package/esm/web/basic-v4.js.map +1 -0
- package/esm/web/basic-v4.min.js +1 -0
- 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 +1450 -0
- package/esm/web/basic-v5.js.map +1 -0
- package/esm/web/basic-v5.min.js +1 -0
- package/esm/web/basic-v5.min.js.br +0 -0
- package/esm/web/basic-v5.min.js.gz +0 -0
- package/esm/web/index.js +309 -265
- 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 +307 -264
- 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 +308 -264
- 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
|
@@ -0,0 +1,1450 @@
|
|
|
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
|
+
class mqtt_reader_v5$1 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 pt = mqtt_props.get( fork.u8() )
|
|
160
|
+
, value = fork[pt.type]();
|
|
161
|
+
res[pt.name] = ! pt.op ? value
|
|
162
|
+
: fork[pt.op](res[pt.name], value);
|
|
163
|
+
}
|
|
164
|
+
return res
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
kv_obj(obj=Object.create(null), [k,v]) {
|
|
168
|
+
obj[k] = v;
|
|
169
|
+
return obj
|
|
170
|
+
}
|
|
171
|
+
u8_vec(vec=[], u8) {
|
|
172
|
+
vec.push(u8);
|
|
173
|
+
return vec
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/*
|
|
177
|
+
vbuf() {
|
|
178
|
+
let {buf, step} = this
|
|
179
|
+
let [n, vi, vi0] = decode_varint(buf, step.k|0)
|
|
180
|
+
step(n + vi - vi0)
|
|
181
|
+
return 0 === n ? null
|
|
182
|
+
: buf.subarray(vi, step.k|0)
|
|
183
|
+
}
|
|
184
|
+
*/
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function mqtt_reader_info(mqtt_reader, ... info_fn_list) {
|
|
188
|
+
mqtt_reader = class extends mqtt_reader {
|
|
189
|
+
static reasons(pkt_type, ...reason_entries) {
|
|
190
|
+
let proto = this.prototype;
|
|
191
|
+
proto._reasons_by = {... proto._reasons_by};
|
|
192
|
+
|
|
193
|
+
let lut = (proto._reasons_by[pkt_type] ||= new Map());
|
|
194
|
+
for (let [u8, reason] of reason_entries)
|
|
195
|
+
lut.set( u8, reason );
|
|
196
|
+
|
|
197
|
+
return this
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
for (let fn_info of info_fn_list)
|
|
202
|
+
fn_info(mqtt_reader);
|
|
203
|
+
|
|
204
|
+
return mqtt_reader
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
class mqtt_writer_v4 {
|
|
208
|
+
static of() { return this.prototype.of() }
|
|
209
|
+
of() { return {__proto__: this, $:[]} }
|
|
210
|
+
|
|
211
|
+
static init() { return this }
|
|
212
|
+
|
|
213
|
+
as_pkt(pkt_id) { return this.pack([pkt_id]) }
|
|
214
|
+
|
|
215
|
+
push(...z) { this.$.push(...z); }
|
|
216
|
+
pack(hdr) {
|
|
217
|
+
let z, i, n=0, parts = this.$;
|
|
218
|
+
this.$ = false;
|
|
219
|
+
for (z of parts) n += z.length;
|
|
220
|
+
|
|
221
|
+
hdr = encode_varint(n, hdr);
|
|
222
|
+
i = hdr.length;
|
|
223
|
+
|
|
224
|
+
let pkt = new Uint8Array(i + n);
|
|
225
|
+
pkt.set(hdr, 0);
|
|
226
|
+
for (z of parts) {
|
|
227
|
+
pkt.set(z, i);
|
|
228
|
+
i += z.length;
|
|
229
|
+
}
|
|
230
|
+
return pkt
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
u8(v) { this.push([ v & 0xff ]); }
|
|
234
|
+
u16(v) { this.push([ (v>>>8) & 0xff, v & 0xff ]); }
|
|
235
|
+
u32(v) { this.push([ (v>>>24) & 0xff, (v>>>16) & 0xff, (v>>>8) & 0xff, v & 0xff ]); }
|
|
236
|
+
vint(v) { this.push( encode_varint(v) );}
|
|
237
|
+
|
|
238
|
+
bin(u8_buf) {
|
|
239
|
+
if (! u8_buf) return this.u16(0)
|
|
240
|
+
if ('string' === typeof u8_buf)
|
|
241
|
+
return this.utf8(u8_buf)
|
|
242
|
+
|
|
243
|
+
if (u8_buf.length !== u8_buf.byteLength)
|
|
244
|
+
u8_buf = new Uint8Array(u8_buf);
|
|
245
|
+
|
|
246
|
+
this.u16(u8_buf.byteLength);
|
|
247
|
+
this.push(u8_buf);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
utf8(v) {
|
|
251
|
+
let u8_buf = new TextEncoder('utf-8').encode(v);
|
|
252
|
+
this.u16(u8_buf.byteLength);
|
|
253
|
+
this.push(u8_buf);
|
|
254
|
+
}
|
|
255
|
+
pair(k,v) { this.utf8(k); this.utf8(v); }
|
|
256
|
+
|
|
257
|
+
flags(v, enc_flags, b0=0) {
|
|
258
|
+
if (undefined !== v && isNaN(+v))
|
|
259
|
+
v = enc_flags(v, 0);
|
|
260
|
+
|
|
261
|
+
v |= b0;
|
|
262
|
+
this.push([v]);
|
|
263
|
+
return v
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
reason(v) { this.push([v | 0]); }
|
|
267
|
+
|
|
268
|
+
flush(buf) {
|
|
269
|
+
if (null != buf)
|
|
270
|
+
this.push(
|
|
271
|
+
'string' === typeof buf
|
|
272
|
+
? new TextEncoder('utf-8').encode(buf)
|
|
273
|
+
: buf );
|
|
274
|
+
|
|
275
|
+
this.push = false;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
class mqtt_writer_v5 extends mqtt_writer_v4 {
|
|
281
|
+
props(props) {
|
|
282
|
+
if (! props)
|
|
283
|
+
return this.u8(0)
|
|
284
|
+
|
|
285
|
+
if (! Array.isArray(props))
|
|
286
|
+
props = props.entries
|
|
287
|
+
? Array.from(props.entries())
|
|
288
|
+
: Object.entries(props);
|
|
289
|
+
|
|
290
|
+
if (0 === props.length)
|
|
291
|
+
return this.u8(0)
|
|
292
|
+
|
|
293
|
+
let fork = this.of();
|
|
294
|
+
for (let [name, value] of props) {
|
|
295
|
+
let pt = mqtt_props.get(name);
|
|
296
|
+
fork[pt.op || 'one'](value, pt);
|
|
297
|
+
}
|
|
298
|
+
this.push(fork.pack());
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
one(value, pt) {
|
|
302
|
+
this.u8(pt.id);
|
|
303
|
+
this[pt.type](value);
|
|
304
|
+
}
|
|
305
|
+
kv_obj(obj, pt) {
|
|
306
|
+
for (let kv of Object.entries(obj)) {
|
|
307
|
+
this.u8(pt.id);
|
|
308
|
+
this.pair(kv);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
u8_vec(vec, pt) {
|
|
312
|
+
for (let v of vec) {
|
|
313
|
+
this.u8(pt.id);
|
|
314
|
+
this.u8(v);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function mqtt_decode_connack(ns, mqtt_reader) {
|
|
320
|
+
class _connack_flags_ extends Number {
|
|
321
|
+
get session_present() { return this & 0x01 !== 0 }
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return ns[0x2] = (pkt, u8_body) => {
|
|
325
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
326
|
+
|
|
327
|
+
pkt.flags =
|
|
328
|
+
rdr.flags(_connack_flags_);
|
|
329
|
+
|
|
330
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
331
|
+
if (5 <= pkt.mqtt_level)
|
|
332
|
+
pkt.props = rdr.props();
|
|
333
|
+
return pkt }
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
function _connack_v4(mqtt_reader) {
|
|
338
|
+
mqtt_reader.reasons('connack',
|
|
339
|
+
// MQTT 3.1.1
|
|
340
|
+
[ 0x00, 'Success'],
|
|
341
|
+
[ 0x01, 'Connection refused, unacceptable protocol version'],
|
|
342
|
+
[ 0x02, 'Connection refused, identifier rejected'],
|
|
343
|
+
[ 0x03, 'Connection refused, server unavailable'],
|
|
344
|
+
[ 0x04, 'Connection refused, bad user name or password'],
|
|
345
|
+
[ 0x05, 'Connection refused, not authorized'],
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function _connack_v5(mqtt_reader) {
|
|
350
|
+
_connack_v4(mqtt_reader);
|
|
351
|
+
|
|
352
|
+
mqtt_reader.reasons('connack',
|
|
353
|
+
// MQTT 5.0
|
|
354
|
+
[ 0x81, 'Malformed Packet'],
|
|
355
|
+
[ 0x82, 'Protocol Error'],
|
|
356
|
+
[ 0x83, 'Implementation specific error'],
|
|
357
|
+
[ 0x84, 'Unsupported Protocol Version'],
|
|
358
|
+
[ 0x85, 'Client Identifier not valid'],
|
|
359
|
+
[ 0x86, 'Bad User Name or Password'],
|
|
360
|
+
[ 0x87, 'Not authorized'],
|
|
361
|
+
[ 0x88, 'Server unavailable'],
|
|
362
|
+
[ 0x89, 'Server busy'],
|
|
363
|
+
[ 0x8A, 'Banned'],
|
|
364
|
+
[ 0x8C, 'Bad authentication method'],
|
|
365
|
+
[ 0x90, 'Topic Name invalid'],
|
|
366
|
+
[ 0x95, 'Packet too large'],
|
|
367
|
+
[ 0x97, 'Quota exceeded'],
|
|
368
|
+
[ 0x99, 'Payload format invalid'],
|
|
369
|
+
[ 0x9A, 'Retain not supported'],
|
|
370
|
+
[ 0x9B, 'QoS not supported'],
|
|
371
|
+
[ 0x9C, 'Use another server'],
|
|
372
|
+
[ 0x9D, 'Server moved'],
|
|
373
|
+
[ 0x9F, 'Connection rate exceeded'],
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function mqtt_decode_publish(ns, mqtt_reader) {
|
|
378
|
+
return ns[0x3] = (pkt, u8_body) => {
|
|
379
|
+
let {hdr} = pkt;
|
|
380
|
+
pkt.dup = Boolean(hdr & 0x8);
|
|
381
|
+
pkt.retain = Boolean(hdr & 0x1);
|
|
382
|
+
let qos = pkt.qos = (hdr>>1) & 0x3;
|
|
383
|
+
|
|
384
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
385
|
+
pkt.topic = rdr.utf8();
|
|
386
|
+
if (0 !== qos)
|
|
387
|
+
pkt.pkt_id = rdr.u16();
|
|
388
|
+
|
|
389
|
+
if (5 <= pkt.mqtt_level)
|
|
390
|
+
pkt.props = rdr.props();
|
|
391
|
+
|
|
392
|
+
pkt.payload = rdr.flush();
|
|
393
|
+
return pkt }
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function mqtt_decode_puback(ns, mqtt_reader) {
|
|
397
|
+
return ns[0x4] = (pkt, u8_body) => {
|
|
398
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
399
|
+
|
|
400
|
+
pkt.pkt_id = rdr.u16();
|
|
401
|
+
if (5 <= pkt.mqtt_level) {
|
|
402
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
403
|
+
pkt.props = rdr.props();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return pkt }
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
function _puback_v5(mqtt_reader) {
|
|
411
|
+
mqtt_reader.reasons('puback',
|
|
412
|
+
// MQTT 5.0
|
|
413
|
+
[ 0x00, 'Success'],
|
|
414
|
+
[ 0x10, 'No matching subscribers'],
|
|
415
|
+
[ 0x80, 'Unspecified error'],
|
|
416
|
+
[ 0x83, 'Implementation specific error'],
|
|
417
|
+
[ 0x87, 'Not authorized'],
|
|
418
|
+
[ 0x90, 'Topic Name invalid'],
|
|
419
|
+
[ 0x91, 'Packet identifier in use'],
|
|
420
|
+
[ 0x97, 'Quota exceeded'],
|
|
421
|
+
[ 0x99, 'Payload format invalid'],
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function _mqtt_decode_suback(mqtt_reader) {
|
|
426
|
+
return (pkt, u8_body) => {
|
|
427
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
428
|
+
|
|
429
|
+
pkt.pkt_id = rdr.u16();
|
|
430
|
+
if (5 <= pkt.mqtt_level)
|
|
431
|
+
pkt.props = rdr.props();
|
|
432
|
+
|
|
433
|
+
let answers = pkt.answers = [];
|
|
434
|
+
while (rdr.has_more())
|
|
435
|
+
answers.push(
|
|
436
|
+
rdr.reason(pkt.type) );
|
|
437
|
+
|
|
438
|
+
return pkt }
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function mqtt_decode_suback(ns, mqtt_reader) {
|
|
442
|
+
return ns[0x9] = _mqtt_decode_suback(mqtt_reader)
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function _suback_v4(mqtt_reader) {
|
|
446
|
+
mqtt_reader.reasons('suback',
|
|
447
|
+
// MQTT 3.1.1
|
|
448
|
+
[ 0x00, 'Granted QoS 0'],
|
|
449
|
+
[ 0x01, 'Granted QoS 1'],
|
|
450
|
+
[ 0x02, 'Granted QoS 2'],
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function _suback_v5(mqtt_reader) {
|
|
455
|
+
_suback_v4(mqtt_reader);
|
|
456
|
+
|
|
457
|
+
mqtt_reader.reasons('suback',
|
|
458
|
+
// MQTT 5.0
|
|
459
|
+
[ 0x80, 'Unspecified error'],
|
|
460
|
+
[ 0x83, 'Implementation specific error'],
|
|
461
|
+
[ 0x87, 'Not authorized'],
|
|
462
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
463
|
+
[ 0x91, 'Packet Identifier in use'],
|
|
464
|
+
[ 0x97, 'Quota exceeded'],
|
|
465
|
+
[ 0x9E, 'Shared Subscriptions not supported'],
|
|
466
|
+
[ 0xA1, 'Subscription Identifiers not supported'],
|
|
467
|
+
[ 0xA2, 'Wildcard Subscriptions not supported'],
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function mqtt_decode_unsuback(ns, mqtt_reader) {
|
|
472
|
+
return ns[0xb] = _mqtt_decode_suback(mqtt_reader)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function _unsuback_v4(mqtt_reader) {
|
|
476
|
+
mqtt_reader.reasons('unsuback',
|
|
477
|
+
// MQTT 3.1.1
|
|
478
|
+
[ 0x00, 'Success'],
|
|
479
|
+
[ 0x11, 'No subscription existed'],
|
|
480
|
+
[ 0x80, 'Unspecified error'],
|
|
481
|
+
[ 0x83, 'Implementation specific error'],
|
|
482
|
+
[ 0x87, 'Not authorized'],
|
|
483
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
484
|
+
[ 0x91, 'Packet Identifier in use'],
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function mqtt_decode_pingxxx(ns) {
|
|
489
|
+
return ns[0xc] = ns[0xd] = pkt => pkt
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function mqtt_decode_disconnect(ns, mqtt_reader) {
|
|
493
|
+
return ns[0xe] = (pkt, u8_body) => {
|
|
494
|
+
if (u8_body && 5 <= pkt.mqtt_level) {
|
|
495
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
496
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
497
|
+
pkt.props = rdr.props();
|
|
498
|
+
}
|
|
499
|
+
return pkt }
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
function _disconnect_v5(mqtt_reader) {
|
|
504
|
+
mqtt_reader.reasons('disconnect',
|
|
505
|
+
// MQTT 5.0
|
|
506
|
+
[ 0x00, 'Normal disconnection'],
|
|
507
|
+
[ 0x04, 'Disconnect with Will Message'],
|
|
508
|
+
[ 0x80, 'Unspecified error'],
|
|
509
|
+
[ 0x81, 'Malformed Packet'],
|
|
510
|
+
[ 0x82, 'Protocol Error'],
|
|
511
|
+
[ 0x83, 'Implementation specific error'],
|
|
512
|
+
[ 0x87, 'Not authorized'],
|
|
513
|
+
[ 0x89, 'Server busy'],
|
|
514
|
+
[ 0x8B, 'Server shutting down'],
|
|
515
|
+
[ 0x8D, 'Keep Alive timeout'],
|
|
516
|
+
[ 0x8E, 'Session taken over'],
|
|
517
|
+
[ 0x8F, 'Topic Filter invalid'],
|
|
518
|
+
[ 0x90, 'Topic Name invalid'],
|
|
519
|
+
[ 0x93, 'Receive Maximum exceeded'],
|
|
520
|
+
[ 0x94, 'Topic Alias invalid'],
|
|
521
|
+
[ 0x95, 'Packet too large'],
|
|
522
|
+
[ 0x96, 'Message rate too high'],
|
|
523
|
+
[ 0x97, 'Quota exceeded'],
|
|
524
|
+
[ 0x98, 'Administrative action'],
|
|
525
|
+
[ 0x99, 'Payload format invalid'],
|
|
526
|
+
[ 0x9A, 'Retain not supported'],
|
|
527
|
+
[ 0x9B, 'QoS not supported'],
|
|
528
|
+
[ 0x9C, 'Use another server'],
|
|
529
|
+
[ 0x9D, 'Server moved'],
|
|
530
|
+
[ 0x9E, 'Shared Subscriptions not supported'],
|
|
531
|
+
[ 0x9F, 'Connection rate exceeded'],
|
|
532
|
+
[ 0xA0, 'Maximum connect time'],
|
|
533
|
+
[ 0xA1, 'Subscription Identifiers not supported'],
|
|
534
|
+
[ 0xA2, 'Wildcard Subscriptions not supported'],
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function mqtt_decode_auth(ns, mqtt_reader) {
|
|
539
|
+
return ns[0xf] = (pkt, u8_body) => {
|
|
540
|
+
if ( 5 <= pkt.mqtt_level ) {
|
|
541
|
+
let rdr = mqtt_reader.of(u8_body);
|
|
542
|
+
pkt.reason = rdr.reason(pkt.type);
|
|
543
|
+
pkt.props = rdr.props();
|
|
544
|
+
}
|
|
545
|
+
return pkt }
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
function _auth_v5(mqtt_reader) {
|
|
550
|
+
mqtt_reader.reasons('auth',
|
|
551
|
+
// MQTT 5.0
|
|
552
|
+
[ 0x00, 'Success' ],
|
|
553
|
+
[ 0x18, 'Continue authentication' ],
|
|
554
|
+
[ 0x19, 'Re-authenticate' ],
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function mqtt_encode_connect(ns, mqtt_writer) {
|
|
559
|
+
const _c_mqtt_proto = new Uint8Array([
|
|
560
|
+
0, 4, 0x4d, 0x51, 0x54, 0x54 ]);
|
|
561
|
+
|
|
562
|
+
const _enc_flags_connect = flags => 0
|
|
563
|
+
| ( flags.reserved ? 0x01 : 0 )
|
|
564
|
+
| ( (flags.will_qos & 0x3) << 3 )
|
|
565
|
+
| ( flags.clean_start ? 0x02 : 0 )
|
|
566
|
+
| ( flags.will_flag ? 0x04 : 0 )
|
|
567
|
+
| ( flags.will_retain ? 0x20 : 0 )
|
|
568
|
+
| ( flags.password ? 0x40 : 0 )
|
|
569
|
+
| ( flags.username ? 0x80 : 0 );
|
|
570
|
+
|
|
571
|
+
const _enc_flags_will = will => 0x4
|
|
572
|
+
| ( (will.qos & 0x3) << 3 )
|
|
573
|
+
| ( will.retain ? 0x20 : 0 );
|
|
574
|
+
|
|
575
|
+
return ns.connect = ( mqtt_level, pkt ) => {
|
|
576
|
+
let wrt = mqtt_writer.of(pkt);
|
|
577
|
+
|
|
578
|
+
wrt.push(_c_mqtt_proto);
|
|
579
|
+
wrt.u8( mqtt_level );
|
|
580
|
+
|
|
581
|
+
let {will, username, password} = pkt;
|
|
582
|
+
let flags = wrt.flags(
|
|
583
|
+
pkt.flags,
|
|
584
|
+
_enc_flags_connect,
|
|
585
|
+
0 | (username ? 0x80 : 0)
|
|
586
|
+
| (password ? 0x40 : 0)
|
|
587
|
+
| (will ? _enc_flags_will(will) : 0) );
|
|
588
|
+
|
|
589
|
+
wrt.u16(pkt.keep_alive);
|
|
590
|
+
|
|
591
|
+
if (5 <= mqtt_level)
|
|
592
|
+
wrt.props(pkt.props);
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
wrt.utf8(pkt.client_id);
|
|
596
|
+
if (flags & 0x04) { // .will_flag
|
|
597
|
+
if (5 <= mqtt_level)
|
|
598
|
+
wrt.props(will.props);
|
|
599
|
+
|
|
600
|
+
wrt.utf8(will.topic);
|
|
601
|
+
wrt.bin(will.payload);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (flags & 0x80) // .username
|
|
605
|
+
wrt.utf8(username);
|
|
606
|
+
|
|
607
|
+
if (flags & 0x40) // .password
|
|
608
|
+
wrt.bin(password);
|
|
609
|
+
|
|
610
|
+
return wrt.as_pkt(0x10)
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function mqtt_encode_publish(ns, mqtt_writer) {
|
|
615
|
+
return ns.publish = ( mqtt_level, pkt ) => {
|
|
616
|
+
let qos = (pkt.qos & 0x3) << 1;
|
|
617
|
+
let wrt = mqtt_writer.of(pkt);
|
|
618
|
+
|
|
619
|
+
wrt.utf8(pkt.topic);
|
|
620
|
+
if (0 !== qos)
|
|
621
|
+
wrt.u16(pkt.pkt_id);
|
|
622
|
+
|
|
623
|
+
if ( 5 <= mqtt_level) {
|
|
624
|
+
wrt.props(pkt.props);
|
|
625
|
+
wrt.flush(pkt.payload);
|
|
626
|
+
} else {
|
|
627
|
+
wrt.flush(pkt.payload);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
return wrt.as_pkt(
|
|
631
|
+
0x30 | qos | (pkt.dup ? 0x8 : 0) | (pkt.retain ? 0x1 : 0) )
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
function mqtt_encode_puback(ns, mqtt_writer) {
|
|
636
|
+
return ns.puback = ( mqtt_level, pkt ) => {
|
|
637
|
+
let wrt = mqtt_writer.of(pkt);
|
|
638
|
+
|
|
639
|
+
wrt.u16(pkt.pkt_id);
|
|
640
|
+
if (5 <= mqtt_level) {
|
|
641
|
+
wrt.reason(pkt.reason);
|
|
642
|
+
wrt.props(pkt.props);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
return wrt.as_pkt(0x40)
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function mqtt_encode_subscribe(ns, mqtt_writer) {
|
|
650
|
+
const _enc_subscribe_flags = opts => 0
|
|
651
|
+
| ( opts.qos & 0x3 )
|
|
652
|
+
| ( opts.retain ? 0x4 : 0 )
|
|
653
|
+
| ( (opts.retain_handling & 0x3) << 2 );
|
|
654
|
+
|
|
655
|
+
return ns.subscribe = ( mqtt_level, pkt ) => {
|
|
656
|
+
let wrt = mqtt_writer.of(pkt);
|
|
657
|
+
|
|
658
|
+
wrt.u16(pkt.pkt_id);
|
|
659
|
+
if (5 <= mqtt_level)
|
|
660
|
+
wrt.props(pkt.props);
|
|
661
|
+
|
|
662
|
+
let f0 = _enc_subscribe_flags(pkt);
|
|
663
|
+
for (let each of pkt.topics) {
|
|
664
|
+
if ('string' === typeof each) {
|
|
665
|
+
wrt.utf8(each);
|
|
666
|
+
wrt.u8(f0);
|
|
667
|
+
} else {
|
|
668
|
+
let [topic, opts] =
|
|
669
|
+
Array.isArray(each) ? each
|
|
670
|
+
: [each.topic, each.opts];
|
|
671
|
+
|
|
672
|
+
wrt.utf8(topic);
|
|
673
|
+
if (undefined === opts) wrt.u8(f0);
|
|
674
|
+
else wrt.flags(opts, _enc_subscribe_flags);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
return wrt.as_pkt(0x82)
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function mqtt_encode_unsubscribe(ns, mqtt_writer) {
|
|
683
|
+
return ns.unsubscribe = ( mqtt_level, pkt ) => {
|
|
684
|
+
let wrt = mqtt_writer.of(pkt);
|
|
685
|
+
|
|
686
|
+
wrt.u16(pkt.pkt_id);
|
|
687
|
+
if (5 <= mqtt_level)
|
|
688
|
+
wrt.props(pkt.props);
|
|
689
|
+
|
|
690
|
+
for (let topic of pkt.topics)
|
|
691
|
+
wrt.utf8(topic);
|
|
692
|
+
|
|
693
|
+
return wrt.as_pkt(0xa2)
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
function mqtt_encode_pingxxx(ns) {
|
|
698
|
+
ns.pingreq = () => new Uint8Array([ 0xc0, 0 ]);
|
|
699
|
+
ns.pingresp = () => new Uint8Array([ 0xd0, 0 ]);
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function mqtt_encode_disconnect(ns, mqtt_writer) {
|
|
703
|
+
return ns.disconnect = ( mqtt_level, pkt ) => {
|
|
704
|
+
let wrt = mqtt_writer.of(pkt);
|
|
705
|
+
|
|
706
|
+
if (pkt && 5 <= mqtt_level) {
|
|
707
|
+
if (pkt.reason || pkt.props) {
|
|
708
|
+
wrt.reason(pkt.reason);
|
|
709
|
+
wrt.props(pkt.props);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
return wrt.as_pkt(0xe0)
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function mqtt_encode_auth(ns, mqtt_writer) {
|
|
718
|
+
return ns.auth = ( mqtt_level, pkt ) => {
|
|
719
|
+
if (5 > mqtt_level)
|
|
720
|
+
throw new Error('Auth packets are only available after MQTT 5.x')
|
|
721
|
+
|
|
722
|
+
let wrt = mqtt_writer.of(pkt);
|
|
723
|
+
|
|
724
|
+
wrt.reason(pkt.reason);
|
|
725
|
+
wrt.props(pkt.props);
|
|
726
|
+
|
|
727
|
+
return wrt.as_pkt(0xf0)
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const mqtt_decode_v5 = [
|
|
732
|
+
mqtt_decode_connack,
|
|
733
|
+
mqtt_decode_publish,
|
|
734
|
+
mqtt_decode_puback,
|
|
735
|
+
mqtt_decode_suback,
|
|
736
|
+
mqtt_decode_unsuback,
|
|
737
|
+
mqtt_decode_pingxxx,
|
|
738
|
+
mqtt_decode_disconnect,
|
|
739
|
+
mqtt_decode_auth,
|
|
740
|
+
];
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
const mqtt_encode_v5 = [
|
|
744
|
+
mqtt_encode_connect,
|
|
745
|
+
mqtt_encode_puback,
|
|
746
|
+
mqtt_encode_publish,
|
|
747
|
+
mqtt_encode_subscribe,
|
|
748
|
+
mqtt_encode_unsubscribe,
|
|
749
|
+
mqtt_encode_pingxxx,
|
|
750
|
+
mqtt_encode_disconnect,
|
|
751
|
+
mqtt_encode_auth,
|
|
752
|
+
];
|
|
753
|
+
|
|
754
|
+
const mqtt_reader_v5 = /* #__PURE__ */
|
|
755
|
+
mqtt_reader_info(
|
|
756
|
+
mqtt_reader_v5$1,
|
|
757
|
+
_connack_v5,
|
|
758
|
+
_puback_v5,
|
|
759
|
+
_suback_v5,
|
|
760
|
+
_unsuback_v4,
|
|
761
|
+
_disconnect_v5,
|
|
762
|
+
_auth_v5,
|
|
763
|
+
);
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
const mqtt_opts_v5 =
|
|
767
|
+
{ decode_fns: mqtt_decode_v5,
|
|
768
|
+
mqtt_reader: mqtt_reader_v5,
|
|
769
|
+
encode_fns: mqtt_encode_v5,
|
|
770
|
+
mqtt_writer: mqtt_writer_v5, };
|
|
771
|
+
|
|
772
|
+
/*
|
|
773
|
+
export function decode_varint_loop(u8, i=0) {
|
|
774
|
+
let i0 = i
|
|
775
|
+
let shift = 0, n = (u8[i] & 0x7f)
|
|
776
|
+
while ( 0x80 & u8[i++] )
|
|
777
|
+
n |= (u8[i] & 0x7f) << (shift += 7)
|
|
778
|
+
|
|
779
|
+
return [n, i, i0]
|
|
780
|
+
}
|
|
781
|
+
*/
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
function decode_varint(u8, i=0) {
|
|
785
|
+
let i0 = i;
|
|
786
|
+
// unrolled for a max of 4 chains
|
|
787
|
+
let n = (u8[i] & 0x7f) << 0;
|
|
788
|
+
if ( 0x80 & u8[i++] ) {
|
|
789
|
+
n |= (u8[i] & 0x7f) << 7;
|
|
790
|
+
if ( 0x80 & u8[i++] ) {
|
|
791
|
+
n |= (u8[i] & 0x7f) << 14;
|
|
792
|
+
if ( 0x80 & u8[i++] ) {
|
|
793
|
+
n |= (u8[i] & 0x7f) << 21;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return [n, i, i0]
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
function mqtt_raw_dispatch(opt) {
|
|
801
|
+
let u8 = new Uint8Array(0);
|
|
802
|
+
return u8_buf => {
|
|
803
|
+
u8 = 0 === u8.byteLength
|
|
804
|
+
? u8_buf : _u8_join(u8, u8_buf);
|
|
805
|
+
|
|
806
|
+
let res = [];
|
|
807
|
+
while (1) {
|
|
808
|
+
let [len_body, len_vh] = decode_varint(u8, 1);
|
|
809
|
+
let len_pkt = len_body + len_vh;
|
|
810
|
+
|
|
811
|
+
if ( u8.byteLength < len_pkt )
|
|
812
|
+
return res
|
|
813
|
+
|
|
814
|
+
let b0 = u8[0];
|
|
815
|
+
let u8_body = 0 === len_body ? null
|
|
816
|
+
: u8.subarray(len_vh, len_pkt);
|
|
817
|
+
|
|
818
|
+
u8 = u8.subarray(len_pkt);
|
|
819
|
+
|
|
820
|
+
let pkt = opt.decode_pkt(b0, u8_body, opt);
|
|
821
|
+
if (null != pkt)
|
|
822
|
+
res.push( pkt );
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
function _u8_join(a, b) {
|
|
828
|
+
let alen = a.byteLength, r = new Uint8Array(alen + b.byteLength);
|
|
829
|
+
r.set(a, 0);
|
|
830
|
+
r.set(b, alen);
|
|
831
|
+
return r
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
const _pkt_types = ['~', 'connect', 'connack', 'publish', 'puback', 'pubrec', 'pubrel', 'pubcomp', 'subscribe', 'suback', 'unsubscribe', 'unsuback', 'pingreq', 'pingresp', 'disconnect', 'auth'];
|
|
835
|
+
|
|
836
|
+
function mqtt_pkt_ctx(mqtt_level, opts, pkt_ctx) {
|
|
837
|
+
pkt_ctx = {
|
|
838
|
+
__proto__: pkt_ctx || opts.pkt_ctx,
|
|
839
|
+
mqtt_level,
|
|
840
|
+
get hdr() { return this.b0 & 0xf },
|
|
841
|
+
get id() { return this.b0 >>> 4 },
|
|
842
|
+
get type() { return _pkt_types[this.b0 >>> 4] },
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
let op, _decode_by_id=[], _encode_by_type={};
|
|
846
|
+
for (op of opts.encode_fns)
|
|
847
|
+
op(_encode_by_type, opts.mqtt_writer);
|
|
848
|
+
for (op of opts.decode_fns)
|
|
849
|
+
op(_decode_by_id, opts.mqtt_reader);
|
|
850
|
+
|
|
851
|
+
return {
|
|
852
|
+
pkt_ctx,
|
|
853
|
+
|
|
854
|
+
encode_pkt(type, pkt) {
|
|
855
|
+
return _encode_by_type[type]( mqtt_level, pkt ) },
|
|
856
|
+
|
|
857
|
+
decode_pkt(b0, u8_body) {
|
|
858
|
+
let fn_decode = _decode_by_id[b0>>>4] || _decode_by_id[0];
|
|
859
|
+
return fn_decode?.({__proto__: this.pkt_ctx, b0}, u8_body) },
|
|
860
|
+
|
|
861
|
+
mqtt_stream() {
|
|
862
|
+
let self = { __proto__: this, pkt_ctx: { __proto__: pkt_ctx } };
|
|
863
|
+
self.pkt_ctx._base_ = self.pkt_ctx;
|
|
864
|
+
self.decode = mqtt_raw_dispatch(self);
|
|
865
|
+
return self
|
|
866
|
+
},
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function ao_defer_ctx(as_res = (...args) => args) {
|
|
871
|
+
let y,n,_pset = (a,b) => { y=a, n=b; };
|
|
872
|
+
return p =>(
|
|
873
|
+
p = new Promise(_pset)
|
|
874
|
+
, as_res(p, y, n)) }
|
|
875
|
+
|
|
876
|
+
const ao_defer_v = /* #__PURE__ */
|
|
877
|
+
ao_defer_ctx();
|
|
878
|
+
|
|
879
|
+
Promise.resolve({type:'init'});
|
|
880
|
+
|
|
881
|
+
function _mqtt_conn(client, [on_mqtt, pkt_future]) {
|
|
882
|
+
let _q_init = ao_defer_v(), _q_ready = ao_defer_v();
|
|
883
|
+
let _send_ready = async (...args) => (await _q_ready[0])(...args);
|
|
884
|
+
let _send_mqtt_pkt, _has_connected;
|
|
885
|
+
client._send = _send_ready;
|
|
886
|
+
|
|
887
|
+
return {
|
|
888
|
+
async when_ready() {await _q_ready[0];}
|
|
889
|
+
|
|
890
|
+
, ping: _ping_interval (() =>_send_mqtt_pkt?.('pingreq'))
|
|
891
|
+
|
|
892
|
+
, reset(err) {
|
|
893
|
+
if (! _send_mqtt_pkt) {return}
|
|
894
|
+
|
|
895
|
+
if (err) {
|
|
896
|
+
_q_init[2](err);}
|
|
897
|
+
|
|
898
|
+
_send_mqtt_pkt = null;
|
|
899
|
+
_q_init = ao_defer_v();
|
|
900
|
+
client._send = _send_ready;
|
|
901
|
+
|
|
902
|
+
// call client.on_conn_reset in next promise microtask
|
|
903
|
+
client.conn_emit('on_disconnect', false===err, err);}
|
|
904
|
+
|
|
905
|
+
, async send_connect(... args) {
|
|
906
|
+
if (! _send_mqtt_pkt) {
|
|
907
|
+
await _q_init[0]; }// _send_mqtt_pkt is set before fulfilled
|
|
908
|
+
|
|
909
|
+
// await connack response
|
|
910
|
+
let res = await _send_mqtt_pkt(...args);
|
|
911
|
+
if (0 == res[0].reason) {
|
|
912
|
+
_has_connected = true;
|
|
913
|
+
// resolve _q_ready[0] with _send_mqtt_pkt closure
|
|
914
|
+
_q_ready[1](client._send = _send_mqtt_pkt);
|
|
915
|
+
_q_ready = ao_defer_v();
|
|
916
|
+
client.conn_emit('on_ready');}
|
|
917
|
+
|
|
918
|
+
return res}
|
|
919
|
+
|
|
920
|
+
, is_set: (() =>!! _send_mqtt_pkt)
|
|
921
|
+
, set(mqtt_ctx, send_u8_pkt) {
|
|
922
|
+
if (_send_mqtt_pkt) {
|
|
923
|
+
throw new Error('Already connected')}
|
|
924
|
+
|
|
925
|
+
mqtt_ctx = mqtt_ctx.mqtt_stream();
|
|
926
|
+
let sess_ctx = {mqtt: client};
|
|
927
|
+
let on_mqtt_chunk = u8_buf =>
|
|
928
|
+
on_mqtt(mqtt_ctx.decode(u8_buf), sess_ctx);
|
|
929
|
+
|
|
930
|
+
_send_mqtt_pkt = async (type, pkt, key) => {
|
|
931
|
+
let res = undefined !== key
|
|
932
|
+
? pkt_future(key) : true;
|
|
933
|
+
|
|
934
|
+
await send_u8_pkt(
|
|
935
|
+
mqtt_ctx.encode_pkt(type, pkt));
|
|
936
|
+
|
|
937
|
+
return res};
|
|
938
|
+
|
|
939
|
+
_q_init[1](_send_mqtt_pkt); // resolve _q_init with _send_mqtt_pkt closure
|
|
940
|
+
|
|
941
|
+
// call client.on_live in next promise microtask
|
|
942
|
+
client.conn_emit('on_live', _has_connected);
|
|
943
|
+
return on_mqtt_chunk} } }
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
function _ping_interval(send_ping) {
|
|
947
|
+
let tid;
|
|
948
|
+
return (( td ) => {
|
|
949
|
+
tid = clearInterval(tid);
|
|
950
|
+
if (td) {
|
|
951
|
+
tid = setInterval(send_ping, 1000 * td);
|
|
952
|
+
|
|
953
|
+
// ensure the interval allows the Deno event loop to exit
|
|
954
|
+
Deno.unrefTimer(tid);
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
return true} }) }
|
|
959
|
+
|
|
960
|
+
const _mqtt_cmdid_dispatch ={
|
|
961
|
+
create(target) {
|
|
962
|
+
return {__proto__: this, target, hashbelt: [new Map()]} }
|
|
963
|
+
|
|
964
|
+
, bind_pkt_future(_pkt_id=100) {
|
|
965
|
+
let {hashbelt} = this;
|
|
966
|
+
|
|
967
|
+
let _tmp_; // use _tmp_ to reuse _by_key closure
|
|
968
|
+
let _by_key = answer_monad =>
|
|
969
|
+
hashbelt[0].set(_tmp_, answer_monad);
|
|
970
|
+
|
|
971
|
+
return (( pkt_or_key ) => {
|
|
972
|
+
if ('string' === typeof pkt_or_key) {
|
|
973
|
+
_tmp_ = pkt_or_key;}
|
|
974
|
+
else {
|
|
975
|
+
_pkt_id = (_pkt_id + 1) & 0xffff;
|
|
976
|
+
_tmp_ = pkt_or_key.pkt_id = _pkt_id;}
|
|
977
|
+
|
|
978
|
+
return new Promise(_by_key)}) }
|
|
979
|
+
|
|
980
|
+
, answer(key, pkt) {
|
|
981
|
+
for (let map of this.hashbelt) {
|
|
982
|
+
let answer_monad = map.get(key);
|
|
983
|
+
if (undefined !== answer_monad) {
|
|
984
|
+
map.delete(key);
|
|
985
|
+
|
|
986
|
+
answer_monad([pkt, /*err*/]); // option/maybe monad
|
|
987
|
+
return true} }
|
|
988
|
+
return false}
|
|
989
|
+
|
|
990
|
+
, rotate_belt(n) {
|
|
991
|
+
let {hashbelt} = this;
|
|
992
|
+
hashbelt.unshift(new Map());
|
|
993
|
+
for (let old of hashbelt.splice(n || 5)) {
|
|
994
|
+
for (let answer_monad of old.values()) {
|
|
995
|
+
answer_monad([/*pkt*/, 'expired']); } } }// option/maybe monad
|
|
996
|
+
|
|
997
|
+
, cmdids: ((() => {
|
|
998
|
+
return [
|
|
999
|
+
(() =>{} )// 0x0 reserved
|
|
1000
|
+
, by_evt // 0x1 connect
|
|
1001
|
+
, by_type // 0x2 connack
|
|
1002
|
+
, by_evt // 0x3 publish
|
|
1003
|
+
, by_id // 0x4 puback
|
|
1004
|
+
, by_id // 0x5 pubrec
|
|
1005
|
+
, by_id // 0x6 pubrel
|
|
1006
|
+
, by_id // 0x7 pubcomp
|
|
1007
|
+
, by_evt // 0x8 subscribe
|
|
1008
|
+
, by_id // 0x9 suback
|
|
1009
|
+
, by_evt // 0xa unsubscribe
|
|
1010
|
+
, by_id // 0xb unsuback
|
|
1011
|
+
, by_type // 0xc pingreq
|
|
1012
|
+
, by_type // 0xd pingresp
|
|
1013
|
+
, by_evt // 0xe disconnect
|
|
1014
|
+
, by_type ]// 0xf auth
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
function by_id(disp, pkt) {
|
|
1018
|
+
disp.answer(pkt.pkt_id, pkt); }
|
|
1019
|
+
|
|
1020
|
+
function by_type(disp, pkt, ctx) {
|
|
1021
|
+
disp.answer(pkt.type, pkt);
|
|
1022
|
+
by_evt(disp, pkt, ctx);}
|
|
1023
|
+
|
|
1024
|
+
async function by_evt({target}, pkt, ctx) {
|
|
1025
|
+
let fn = target[`mqtt_${pkt.type}`]
|
|
1026
|
+
|| target.mqtt_pkt;
|
|
1027
|
+
|
|
1028
|
+
await fn?.call(target, pkt, ctx);} })()) };
|
|
1029
|
+
|
|
1030
|
+
function _mqtt_dispatch(opt, target) {
|
|
1031
|
+
let _disp_ = _mqtt_cmdid_dispatch.create(target);
|
|
1032
|
+
let { cmdids } = _disp_;
|
|
1033
|
+
|
|
1034
|
+
// default rotate at 1s across 5 buckets
|
|
1035
|
+
let { td: rotate_td=1000, n: rotate_n=5 } =
|
|
1036
|
+
opt && opt.rotate || {};
|
|
1037
|
+
|
|
1038
|
+
let rotate_ts = rotate_td + Date.now();
|
|
1039
|
+
|
|
1040
|
+
return [on_mqtt,
|
|
1041
|
+
_disp_.bind_pkt_future()]
|
|
1042
|
+
|
|
1043
|
+
function on_mqtt(pkt_list, ctx) {
|
|
1044
|
+
for (let pkt of pkt_list) {
|
|
1045
|
+
cmdids[pkt.id](_disp_, pkt, ctx); }
|
|
1046
|
+
|
|
1047
|
+
if (Date.now() > rotate_ts) {
|
|
1048
|
+
_disp_.rotate_belt(rotate_n);
|
|
1049
|
+
rotate_ts = rotate_td + Date.now();} } }
|
|
1050
|
+
|
|
1051
|
+
class MQTTError extends Error {
|
|
1052
|
+
constructor(mqtt_pkt, reason=mqtt_pkt.reason) {
|
|
1053
|
+
super(`[0x${reason.toString(16)}] ${reason.reason}`);
|
|
1054
|
+
this.mqtt_pkt = mqtt_pkt;
|
|
1055
|
+
this.reason = reason;} }
|
|
1056
|
+
|
|
1057
|
+
class MQTTBase {
|
|
1058
|
+
constructor(opt={}) {
|
|
1059
|
+
this._conn_ = _mqtt_conn(this,
|
|
1060
|
+
this._init_dispatch(opt, this)); }
|
|
1061
|
+
|
|
1062
|
+
async conn_emit(evt, arg, err_arg) {
|
|
1063
|
+
this.log_conn?.(evt, arg, err_arg);
|
|
1064
|
+
try {
|
|
1065
|
+
let fn_evt = this[await evt]; // microtask break
|
|
1066
|
+
if (fn_evt) {
|
|
1067
|
+
await fn_evt.call(this, this, arg, err_arg);}
|
|
1068
|
+
else if (err_arg) {
|
|
1069
|
+
await this.on_error(err_arg, evt);} }
|
|
1070
|
+
catch (err) {
|
|
1071
|
+
this.on_error(err, evt);} }
|
|
1072
|
+
|
|
1073
|
+
on_error(err, err_path) {
|
|
1074
|
+
console.warn('[[u8-mqtt error: %s]]', err_path, err); }
|
|
1075
|
+
|
|
1076
|
+
// Handshaking Packets
|
|
1077
|
+
|
|
1078
|
+
async connect(pkt={}) {
|
|
1079
|
+
let cid = pkt.client_id || ['u8-mqtt--', ''];
|
|
1080
|
+
if (Array.isArray(cid)) {
|
|
1081
|
+
pkt.client_id = cid = this.init_client_id(cid);}
|
|
1082
|
+
this.client_id = cid;
|
|
1083
|
+
|
|
1084
|
+
if (null == pkt.keep_alive) {
|
|
1085
|
+
pkt.keep_alive = 60;}
|
|
1086
|
+
|
|
1087
|
+
let res = await this._conn_
|
|
1088
|
+
.send_connect('connect', pkt, 'connack');
|
|
1089
|
+
|
|
1090
|
+
if (0 != res[0].reason) {
|
|
1091
|
+
throw new this.MQTTError(res[0])}
|
|
1092
|
+
|
|
1093
|
+
// TODO: merge with server's keep_alive frequency
|
|
1094
|
+
this._conn_.ping(pkt.keep_alive);
|
|
1095
|
+
return res}
|
|
1096
|
+
|
|
1097
|
+
async disconnect(pkt={}) {
|
|
1098
|
+
let res = await this._send('disconnect', pkt);
|
|
1099
|
+
this._conn_.reset(false);
|
|
1100
|
+
return res}
|
|
1101
|
+
|
|
1102
|
+
auth(pkt={}) {
|
|
1103
|
+
return this._send('auth', pkt, 'auth')}
|
|
1104
|
+
|
|
1105
|
+
ping() {return this._send('pingreq', null, 'pingresp')}
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
// alias: sub
|
|
1109
|
+
subscribe(pkt, ex, topic_prefix) {
|
|
1110
|
+
pkt = _as_topics(pkt, ex, topic_prefix);
|
|
1111
|
+
return this._send('subscribe', pkt, pkt)}
|
|
1112
|
+
|
|
1113
|
+
// alias: unsub
|
|
1114
|
+
unsubscribe(pkt, ex, topic_prefix) {
|
|
1115
|
+
pkt = _as_topics(pkt, ex, topic_prefix);
|
|
1116
|
+
return this._send('unsubscribe', pkt, pkt)}
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
// alias: pub
|
|
1120
|
+
publish(pkt, pub_opt) {return _pub(this, pkt, pub_opt)}
|
|
1121
|
+
post(topic, payload, pub_opt) {return _pub.m(this, topic, payload, pub_opt)}
|
|
1122
|
+
send(topic, payload, pub_opt) {return _pub.mq(this, topic, payload, pub_opt)}
|
|
1123
|
+
store(topic, payload, pub_opt) {return _pub.mqr(this, topic, payload, pub_opt)}
|
|
1124
|
+
|
|
1125
|
+
json_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
|
|
1126
|
+
json_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
|
|
1127
|
+
json_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
|
|
1128
|
+
|
|
1129
|
+
obj_post(topic, msg, pub_opt) {return _pub.o(this, topic, msg, pub_opt)}
|
|
1130
|
+
obj_send(topic, msg, pub_opt) {return _pub.oq(this, topic, msg, pub_opt)}
|
|
1131
|
+
obj_store(topic, msg, pub_opt) {return _pub.oqr(this, topic, msg, pub_opt)}
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
// Utility Methods
|
|
1136
|
+
|
|
1137
|
+
init_client_id(parts) {
|
|
1138
|
+
let cid = this.client_id;
|
|
1139
|
+
|
|
1140
|
+
if (undefined === cid) {
|
|
1141
|
+
this.client_id = cid = (
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
this.new_client_id(parts)
|
|
1146
|
+
);}
|
|
1147
|
+
|
|
1148
|
+
return cid}
|
|
1149
|
+
|
|
1150
|
+
new_client_id(parts) {
|
|
1151
|
+
return [parts[0], Math.random().toString(36).slice(2), parts[1]].join('')}
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
// Internal API
|
|
1164
|
+
|
|
1165
|
+
/* async _send(type, pkt) -- provided by _conn_ and transport */
|
|
1166
|
+
|
|
1167
|
+
_init_dispatch(opt) {
|
|
1168
|
+
this.constructor?._once_();
|
|
1169
|
+
let target ={__proto__: opt.on_mqtt_type};
|
|
1170
|
+
target.mqtt_publish ||=
|
|
1171
|
+
this._init_router?.(opt, this, target);
|
|
1172
|
+
return _mqtt_dispatch(this, target)}
|
|
1173
|
+
|
|
1174
|
+
static _aliases() {
|
|
1175
|
+
return ' pub:publish sub:subscribe unsub:unsubscribe '}
|
|
1176
|
+
|
|
1177
|
+
static _once_(self=this) {
|
|
1178
|
+
self._once_ = _=>0;
|
|
1179
|
+
self.MQTTError = MQTTError;
|
|
1180
|
+
let p = self.prototype;
|
|
1181
|
+
for (let alias of self._aliases().split(/\s+/)) {
|
|
1182
|
+
alias = alias.split(':');
|
|
1183
|
+
let fn = alias[1] && p[alias[1]];
|
|
1184
|
+
if (fn) {p[alias[0]] = fn;} } } }
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
/*
|
|
1188
|
+
on_mqtt_type = {
|
|
1189
|
+
mqtt_auth(pkt, ctx) ::
|
|
1190
|
+
mqtt_connect(pkt, ctx) ::
|
|
1191
|
+
mqtt_connack(pkt, ctx) ::
|
|
1192
|
+
mqtt_disconnect(pkt, ctx) ::
|
|
1193
|
+
|
|
1194
|
+
mqtt_publish(pkt, ctx)
|
|
1195
|
+
mqtt_subscribe(pkt, ctx) ::
|
|
1196
|
+
mqtt_unsubscribe(pkt, ctx) ::
|
|
1197
|
+
|
|
1198
|
+
mqtt_pingreq(pkt, ctx) ::
|
|
1199
|
+
mqtt_pingresp(pkt, ctx) ::
|
|
1200
|
+
}
|
|
1201
|
+
*/
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
const _prefix_topics = (topic_prefix, iterable) =>
|
|
1205
|
+
Array.from(iterable, value =>(
|
|
1206
|
+
value.trim // string
|
|
1207
|
+
? _prefix_topics(topic_prefix, value)
|
|
1208
|
+
: topic_prefix + value) );
|
|
1209
|
+
|
|
1210
|
+
function _as_topics(pkt, ex, topic_prefix) {
|
|
1211
|
+
if (ex?.trim) {// string
|
|
1212
|
+
topic_prefix = ex;
|
|
1213
|
+
ex = null;}
|
|
1214
|
+
|
|
1215
|
+
pkt =(
|
|
1216
|
+
pkt.trim // string
|
|
1217
|
+
? {topics:[pkt], ... ex}
|
|
1218
|
+
: pkt[Symbol.iterator]
|
|
1219
|
+
? {topics:[... pkt], ... ex}
|
|
1220
|
+
: ex ? {...pkt, ...ex}
|
|
1221
|
+
: pkt);
|
|
1222
|
+
|
|
1223
|
+
if (topic_prefix) {
|
|
1224
|
+
// particularly useful with shared queues, e.g.
|
|
1225
|
+
// topic_prefix = '$share/some-queue-name/'
|
|
1226
|
+
pkt.topics = _prefix_topics(topic_prefix, pkt.topics);}
|
|
1227
|
+
return pkt}
|
|
1228
|
+
|
|
1229
|
+
|
|
1230
|
+
async function _pub(self, pkt, pub_opt) {
|
|
1231
|
+
if (undefined === pkt.payload) {
|
|
1232
|
+
if ('function' === typeof pub_opt) {
|
|
1233
|
+
pub_opt = {fn_encode: pub_opt};}
|
|
1234
|
+
|
|
1235
|
+
let {msg} = pkt;
|
|
1236
|
+
switch (typeof msg) {
|
|
1237
|
+
case 'function':
|
|
1238
|
+
pub_opt = {...pub_opt, fn_encode: msg};
|
|
1239
|
+
// flow into 'undefined' case
|
|
1240
|
+
case 'undefined':
|
|
1241
|
+
// return a single-value closure to publish packets
|
|
1242
|
+
return v => _pub(self, {...pkt, [pkt.arg || 'payload']: v}, pub_opt)
|
|
1243
|
+
|
|
1244
|
+
default:
|
|
1245
|
+
// Encode payload from msg; fn_encode allows alternative to JSON.stringify
|
|
1246
|
+
let {fn_encode} = pub_opt || {};
|
|
1247
|
+
pkt.payload = fn_encode
|
|
1248
|
+
? await fn_encode(msg)
|
|
1249
|
+
: JSON.stringify(msg);} }
|
|
1250
|
+
|
|
1251
|
+
if (pub_opt) {
|
|
1252
|
+
if (pub_opt.props) {
|
|
1253
|
+
pkt.props = pub_opt.props;}
|
|
1254
|
+
if (pub_opt.xform) {
|
|
1255
|
+
pkt = pub_opt.xform(pkt) || pkt;} }
|
|
1256
|
+
|
|
1257
|
+
return self._send('publish', pkt,
|
|
1258
|
+
pkt.qos ? pkt : void 0 ) }// key
|
|
1259
|
+
|
|
1260
|
+
{
|
|
1261
|
+
Object.assign(_pub,{
|
|
1262
|
+
m: (self, topic, payload, pub_opt) =>
|
|
1263
|
+
_pub(self, {topic, payload, qos:0}, pub_opt)
|
|
1264
|
+
, mq: (self, topic, payload, pub_opt) =>
|
|
1265
|
+
_pub(self, {topic, payload, qos:1}, pub_opt)
|
|
1266
|
+
, mqr: (self, topic, payload, pub_opt) =>
|
|
1267
|
+
_pub(self, {topic, payload, qos:1, retain: 1}, pub_opt)
|
|
1268
|
+
|
|
1269
|
+
, o: (self, topic, msg, pub_opt) =>
|
|
1270
|
+
_pub(self, {topic, msg, arg: 'msg', qos:0}, pub_opt)
|
|
1271
|
+
, oq: (self, topic, msg, pub_opt) =>
|
|
1272
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1}, pub_opt)
|
|
1273
|
+
, oqr: (self, topic, msg, pub_opt) =>
|
|
1274
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, pub_opt)} ); }
|
|
1275
|
+
|
|
1276
|
+
const pkt_api = {
|
|
1277
|
+
utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
|
|
1278
|
+
json(u8) { return JSON.parse( this.utf8(u8) || null ) },
|
|
1279
|
+
text(u8) { return this.utf8(u8) },
|
|
1280
|
+
};
|
|
1281
|
+
|
|
1282
|
+
class MQTTCore extends MQTTBase {
|
|
1283
|
+
constructor(opt={}) {
|
|
1284
|
+
super(opt);
|
|
1285
|
+
this.with(opt);}
|
|
1286
|
+
|
|
1287
|
+
static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) {
|
|
1288
|
+
let self = class extends this {};
|
|
1289
|
+
self.prototype.mqtt_ctx =
|
|
1290
|
+
mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx);
|
|
1291
|
+
return self}
|
|
1292
|
+
|
|
1293
|
+
with(fns_ns) {
|
|
1294
|
+
for (let [k,v] of Object.entries(fns_ns)) {
|
|
1295
|
+
if ('function' === typeof v) {this[k] = v;} }
|
|
1296
|
+
return this}
|
|
1297
|
+
|
|
1298
|
+
//log_conn(evt, arg, err_arg) ::
|
|
1299
|
+
// console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
|
|
1300
|
+
|
|
1301
|
+
on_live(client, is_reconnect) {
|
|
1302
|
+
if (is_reconnect) {
|
|
1303
|
+
return client.connect()} }
|
|
1304
|
+
|
|
1305
|
+
//on_reconnect(client) ::
|
|
1306
|
+
|
|
1307
|
+
_use_conn(fn_reconnect) {
|
|
1308
|
+
return (this.reconnect = fn_reconnect)?.()}
|
|
1309
|
+
with_autoreconnect(opt=2000) {
|
|
1310
|
+
if (opt.toFixed) {opt ={delay: opt};}
|
|
1311
|
+
return this.with({
|
|
1312
|
+
on_reconnect() {
|
|
1313
|
+
this.delay(opt.delay || 2000)
|
|
1314
|
+
.then(this.reconnect)
|
|
1315
|
+
.then(opt.reconnect, opt.error);} }) }
|
|
1316
|
+
|
|
1317
|
+
on_disconnect(client, intentional) {
|
|
1318
|
+
if (! intentional) {
|
|
1319
|
+
return client.on_reconnect?.()} }
|
|
1320
|
+
|
|
1321
|
+
delay(ms) {
|
|
1322
|
+
return new Promise(done => setTimeout(done, ms)) }
|
|
1323
|
+
|
|
1324
|
+
with_async_iter(async_iter, write_u8_pkt) {
|
|
1325
|
+
let on_mqtt_chunk = this._conn_.set(
|
|
1326
|
+
this.mqtt_ctx,
|
|
1327
|
+
write_u8_pkt);
|
|
1328
|
+
|
|
1329
|
+
this._msg_loop = ((async () => {
|
|
1330
|
+
try {
|
|
1331
|
+
async_iter = await async_iter;
|
|
1332
|
+
for await (let chunk of async_iter)
|
|
1333
|
+
on_mqtt_chunk(chunk);
|
|
1334
|
+
this._conn_.reset();}
|
|
1335
|
+
catch (err) {
|
|
1336
|
+
this._conn_.reset(err);} })());
|
|
1337
|
+
|
|
1338
|
+
return this}
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
with_tcp(...opt) {
|
|
1344
|
+
opt = this._conn_opt(opt);
|
|
1345
|
+
opt.transport = 'tcp';
|
|
1346
|
+
return this._use_conn (() =>
|
|
1347
|
+
this.with_deno_iter(
|
|
1348
|
+
Deno.connect(opt)) ) }
|
|
1349
|
+
|
|
1350
|
+
with_tls(...opt) {
|
|
1351
|
+
opt = this._conn_opt(opt);
|
|
1352
|
+
return this._use_conn (() =>
|
|
1353
|
+
this.with_deno_iter(
|
|
1354
|
+
Deno.connectTls(opt)) ) }
|
|
1355
|
+
|
|
1356
|
+
with_deno_iter(conn) {
|
|
1357
|
+
return this.with_async_iter(
|
|
1358
|
+
conn.then(Deno.iter),
|
|
1359
|
+
async u8_pkt =>
|
|
1360
|
+
(await conn).write(u8_pkt)) }
|
|
1361
|
+
|
|
1362
|
+
_conn_opt([a0, a1, a2]) {
|
|
1363
|
+
// (port, hostname, options) or (url, options)
|
|
1364
|
+
if (Number.isFinite(a0)) {
|
|
1365
|
+
return {...a2, port: a0, hostname: a1}}
|
|
1366
|
+
|
|
1367
|
+
a0 = new URL(a0);
|
|
1368
|
+
return {...a1, port: a0.port, hostname: a0.hostname}}
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
1385
|
+
|
|
1386
|
+
|
|
1387
|
+
|
|
1388
|
+
|
|
1389
|
+
|
|
1390
|
+
|
|
1391
|
+
|
|
1392
|
+
with_stream(read_stream, write_stream) {
|
|
1393
|
+
if (undefined === write_stream) {
|
|
1394
|
+
write_stream = read_stream;}
|
|
1395
|
+
|
|
1396
|
+
return this.with_async_iter(read_stream,
|
|
1397
|
+
u8_pkt => write_stream.write(u8_pkt)) }
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
with_websock(websock) {
|
|
1401
|
+
if (! websock?.send) {
|
|
1402
|
+
websock = new URL(websock || 'ws://127.0.0.1:9001');
|
|
1403
|
+
return this._use_conn (() =>
|
|
1404
|
+
this.with_websock(
|
|
1405
|
+
new WebSocket(websock, ['mqtt'])) ) }
|
|
1406
|
+
|
|
1407
|
+
websock.binaryType = 'arraybuffer';
|
|
1408
|
+
|
|
1409
|
+
let ready, {readyState} = websock;
|
|
1410
|
+
if (1 !== readyState) {
|
|
1411
|
+
if (0 !== readyState) {
|
|
1412
|
+
throw new Error('Invalid WebSocket readyState') }
|
|
1413
|
+
|
|
1414
|
+
ready = new Promise(fn => websock.onopen = fn); }
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
let {_conn_} = this;
|
|
1418
|
+
let on_mqtt_chunk = _conn_.set(
|
|
1419
|
+
this.mqtt_ctx,
|
|
1420
|
+
async u8_pkt =>(
|
|
1421
|
+
await ready
|
|
1422
|
+
, websock.send(u8_pkt)) );
|
|
1423
|
+
|
|
1424
|
+
websock.onmessage = evt =>(on_mqtt_chunk(new Uint8Array(evt.data)));
|
|
1425
|
+
websock.onclose = evt => {
|
|
1426
|
+
if (! evt.wasClean) {
|
|
1427
|
+
var err = new Error('websocket connection close');
|
|
1428
|
+
err.code = evt.code;
|
|
1429
|
+
err.reason = evt.reason;}
|
|
1430
|
+
|
|
1431
|
+
_conn_.reset(err);};
|
|
1432
|
+
|
|
1433
|
+
return this} }
|
|
1434
|
+
|
|
1435
|
+
const version = '0.4.1';
|
|
1436
|
+
|
|
1437
|
+
const MQTTClient_v4 = /* #__PURE__ */
|
|
1438
|
+
MQTTCore.mqtt_ctx(4, mqtt_opts_v5);
|
|
1439
|
+
|
|
1440
|
+
const MQTTClient_v5 = /* #__PURE__ */
|
|
1441
|
+
MQTTCore.mqtt_ctx(5, mqtt_opts_v5);
|
|
1442
|
+
|
|
1443
|
+
const mqtt_v4 = opt =>
|
|
1444
|
+
new MQTTClient_v4(opt);
|
|
1445
|
+
|
|
1446
|
+
const mqtt_v5 = opt =>
|
|
1447
|
+
new MQTTClient_v5(opt);
|
|
1448
|
+
|
|
1449
|
+
export { MQTTClient_v5 as MQTTClient, MQTTClient_v4, MQTTClient_v5, mqtt_v5 as default, mqtt_v5 as mqtt, mqtt_v4, mqtt_v5, version };
|
|
1450
|
+
//# sourceMappingURL=basic-v5.js.map
|