u8-mqtt 0.1.3 → 0.3.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 +42 -59
- package/cjs/index.cjs +635 -557
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +482 -662
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +634 -550
- package/cjs/v5.cjs.map +1 -1
- package/code/_conn.jsy +53 -54
- package/code/_router.jsy +15 -4
- package/code/base.jsy +35 -16
- package/code/core.jsy +78 -59
- package/code/index.mjs +5 -4
- package/code/v4.mjs +7 -6
- package/code/v5.mjs +15 -6
- package/esm/deno/index.js +637 -562
- package/esm/deno/index.js.map +1 -1
- package/esm/deno/v4.js +489 -670
- package/esm/deno/v4.js.map +1 -1
- package/esm/deno/v5.js +639 -558
- package/esm/deno/v5.js.map +1 -1
- package/esm/node/index.js +630 -555
- package/esm/node/index.js.map +1 -1
- package/esm/node/index.mjs +630 -555
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.js +482 -663
- package/esm/node/v4.js.map +1 -1
- package/esm/node/v4.mjs +482 -663
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.js +632 -551
- package/esm/node/v5.js.map +1 -1
- package/esm/node/v5.mjs +632 -551
- package/esm/node/v5.mjs.map +1 -1
- package/esm/web/index.js +632 -557
- package/esm/web/index.js.map +1 -1
- package/esm/web/index.min.js +1 -0
- package/esm/web/index.min.js.br +0 -0
- package/esm/web/index.min.js.gz +0 -0
- package/esm/web/v4.js +484 -665
- package/esm/web/v4.js.map +1 -1
- package/esm/web/v4.min.js +1 -0
- package/esm/web/v4.min.js.br +0 -0
- package/esm/web/v4.min.js.gz +0 -0
- package/esm/web/v5.js +634 -553
- package/esm/web/v5.js.map +1 -1
- package/esm/web/v5.min.js +1 -0
- package/esm/web/v5.min.js.br +0 -0
- package/esm/web/v5.min.js.gz +0 -0
- package/package.json +24 -12
- package/code/session.mjs +0 -65
- package/esm/deno/index.mjs +0 -1502
- package/esm/deno/index.mjs.map +0 -1
- package/esm/deno/v4.mjs +0 -1496
- package/esm/deno/v4.mjs.map +0 -1
- package/esm/deno/v5.mjs +0 -1496
- package/esm/deno/v5.mjs.map +0 -1
- package/esm/web/index.min.mjs +0 -1
- package/esm/web/index.mjs +0 -1502
- package/esm/web/index.mjs.map +0 -1
- package/esm/web/v4.min.mjs +0 -1
- package/esm/web/v4.mjs +0 -1496
- package/esm/web/v4.mjs.map +0 -1
- package/esm/web/v5.min.mjs +0 -1
- package/esm/web/v5.mjs +0 -1496
- package/esm/web/v5.mjs.map +0 -1
package/code/_conn.jsy
CHANGED
|
@@ -1,83 +1,82 @@
|
|
|
1
|
+
import { ao_defer_v } from 'roap'
|
|
2
|
+
export { ao_defer_v }
|
|
3
|
+
|
|
1
4
|
export function _mqtt_conn(client, [on_mqtt, pkt_future]) ::
|
|
2
|
-
let
|
|
3
|
-
let
|
|
5
|
+
let _q_init = ao_defer_v(), _q_ready = ao_defer_v()
|
|
6
|
+
let _send_ready = async (...args) => (await _q_ready[0])(...args)
|
|
7
|
+
let _send_mqtt_pkt, _has_connected
|
|
8
|
+
client._send = _send_ready
|
|
4
9
|
|
|
5
|
-
|
|
6
|
-
(await
|
|
7
|
-
let _send = client._send = _asy_send
|
|
10
|
+
return @{}
|
|
11
|
+
async when_ready() :: await _q_ready[0]
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
let tid_ping, _is_set = false
|
|
13
|
+
ping: _ping_interval @=> _send_mqtt_pkt?.('pingreq')
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
is_set: @=> _is_set
|
|
15
|
+
reset(err) ::
|
|
16
|
+
if ! _send_mqtt_pkt :: return
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
client._send = _send = _asy_send
|
|
19
|
-
_is_set = false
|
|
18
|
+
if err ::
|
|
19
|
+
_q_init[2](err)
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
_send_mqtt_pkt = null
|
|
22
|
+
_q_init = ao_defer_v()
|
|
23
|
+
client._send = _send_ready
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if td ::
|
|
27
|
-
tid_ping = setInterval(_ping, 1000 * td)
|
|
28
|
-
if tid_ping.unref ::
|
|
29
|
-
tid_ping.unref()
|
|
25
|
+
// call client.on_conn_reset in next promise microtask
|
|
26
|
+
client.conn_emit('on_disconnect', false===err, err)
|
|
30
27
|
|
|
31
28
|
async send_connect(... args) ::
|
|
32
|
-
if
|
|
33
|
-
|
|
29
|
+
if ! _send_mqtt_pkt ::
|
|
30
|
+
await _q_init[0] // _send_mqtt_pkt is set before fulfilled
|
|
34
31
|
|
|
35
32
|
// await connack response
|
|
36
|
-
let res = await
|
|
33
|
+
let res = await _send_mqtt_pkt(...args)
|
|
34
|
+
if 0 == res[0].reason ::
|
|
35
|
+
_has_connected = true
|
|
36
|
+
// resolve _q_ready[0] with _send_mqtt_pkt closure
|
|
37
|
+
_q_ready[1](client._send = _send_mqtt_pkt)
|
|
38
|
+
_q_ready = ao_defer_v()
|
|
39
|
+
client.conn_emit('on_ready')
|
|
37
40
|
|
|
38
|
-
client._send = _send
|
|
39
|
-
q.notify(_send)
|
|
40
41
|
return res
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
is_set: @=> !! _send_mqtt_pkt
|
|
44
|
+
set(mqtt_ctx, send_u8_pkt) ::
|
|
45
|
+
if _send_mqtt_pkt ::
|
|
46
|
+
throw new Error('Already connected')
|
|
46
47
|
|
|
48
|
+
mqtt_ctx = mqtt_ctx.mqtt_stream()
|
|
49
|
+
let sess_ctx = {mqtt: client}
|
|
47
50
|
let on_mqtt_chunk = u8_buf =>
|
|
48
|
-
on_mqtt(
|
|
49
|
-
mqtt_decode(u8_buf),
|
|
50
|
-
{mqtt: client})
|
|
51
|
+
on_mqtt(mqtt_ctx.decode(u8_buf), sess_ctx)
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
_send_mqtt_pkt = async (type, pkt, key) => ::
|
|
53
54
|
let res = undefined !== key
|
|
54
55
|
? pkt_future(key) : true
|
|
55
56
|
|
|
56
57
|
await send_u8_pkt @
|
|
57
|
-
|
|
58
|
+
mqtt_ctx.encode_pkt(type, pkt)
|
|
58
59
|
|
|
59
60
|
return res
|
|
60
61
|
|
|
61
|
-
|
|
62
|
-
q0.notify(_send)
|
|
62
|
+
_q_init[1](_send_mqtt_pkt) // resolve _q_init with _send_mqtt_pkt closure
|
|
63
63
|
|
|
64
64
|
// call client.on_live in next promise microtask
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
client.conn_emit('on_live', _has_connected)
|
|
67
66
|
return on_mqtt_chunk
|
|
68
67
|
|
|
69
68
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
69
|
+
function _ping_interval(send_ping) ::
|
|
70
|
+
let tid
|
|
71
|
+
return @\ td ::
|
|
72
|
+
tid = clearInterval(tid)
|
|
73
|
+
if td ::
|
|
74
|
+
tid = setInterval(send_ping, 1000 * td)
|
|
75
|
+
#IF PLAT_DENO
|
|
76
|
+
// ensure the interval allows the Deno event loop to exit
|
|
77
|
+
Deno.unrefTimer(tid)
|
|
78
|
+
#ELSE
|
|
79
|
+
// ensure the interval allows the NodeJS event loop to exit
|
|
80
|
+
tid.unref?.()
|
|
81
|
+
return true
|
|
82
|
+
|
package/code/_router.jsy
CHANGED
|
@@ -6,7 +6,7 @@ import {parse as _rxp_parse} from 'regexparam'
|
|
|
6
6
|
function _ignore(pkt, params, ctx) :: ctx.done = true
|
|
7
7
|
|
|
8
8
|
export function _mqtt_topic_router() ::
|
|
9
|
-
let pri_lsts = [[],[]]
|
|
9
|
+
let pri_lsts = [[],[]], rm = Symbol()
|
|
10
10
|
let find = topic => _mqtt_routes_iter(pri_lsts, topic)
|
|
11
11
|
|
|
12
12
|
return @{} find,
|
|
@@ -30,8 +30,7 @@ export function _mqtt_topic_router() ::
|
|
|
30
30
|
|
|
31
31
|
remove(topic_route, priority) ::
|
|
32
32
|
let lst = pri_lsts[priority ? 0 : 1]
|
|
33
|
-
|
|
34
|
-
pri_lsts[priority ? 0 : 1] = lst
|
|
33
|
+
return _mqtt_route_remove([lst], topic_route)
|
|
35
34
|
|
|
36
35
|
clear(priority) ::
|
|
37
36
|
pri_lsts[priority ? 0 : 1] = []
|
|
@@ -40,9 +39,13 @@ export function _mqtt_topic_router() ::
|
|
|
40
39
|
|
|
41
40
|
async invoke(pkt, ctx) ::
|
|
42
41
|
ctx.idx = 0
|
|
42
|
+
ctx.rm = rm
|
|
43
43
|
|
|
44
44
|
for let [fn, params] of find(pkt.topic) ::
|
|
45
|
-
await fn(pkt, params, ctx)
|
|
45
|
+
let res = await fn(pkt, params, ctx)
|
|
46
|
+
|
|
47
|
+
if rm === res ::
|
|
48
|
+
_mqtt_route_remove(pri_lsts, fn)
|
|
46
49
|
|
|
47
50
|
if ctx.done ::
|
|
48
51
|
break
|
|
@@ -88,3 +91,11 @@ export function _mqtt_route_match_one(topic, {keys, pattern, tgt}) ::
|
|
|
88
91
|
params[ keys[i] ] = match[1+i]
|
|
89
92
|
return [tgt, params]
|
|
90
93
|
|
|
94
|
+
|
|
95
|
+
export function _mqtt_route_remove(all_route_lists, query) ::
|
|
96
|
+
let match = route => route===query || route.tgt===query || route.key===query
|
|
97
|
+
for let lst of all_route_lists ::
|
|
98
|
+
let i = lst.findIndex(match)
|
|
99
|
+
if 0 <= i :: return !! lst.splice(i,1)
|
|
100
|
+
return false
|
|
101
|
+
|
package/code/base.jsy
CHANGED
|
@@ -2,20 +2,38 @@ import {_mqtt_conn} from './_conn.jsy'
|
|
|
2
2
|
import {_mqtt_topic_router} from './_router.jsy'
|
|
3
3
|
import {_mqtt_dispatch} from './_dispatch.jsy'
|
|
4
4
|
|
|
5
|
-
export class
|
|
5
|
+
export class MQTTError extends Error ::
|
|
6
|
+
constructor(mqtt_pkt, reason=mqtt_pkt.reason) ::
|
|
7
|
+
super(`[0x${reason.toString(16)}] ${reason.reason}`)
|
|
8
|
+
this.mqtt_pkt = mqtt_pkt
|
|
9
|
+
this.reason = reason
|
|
10
|
+
|
|
11
|
+
export class MQTTBase ::
|
|
6
12
|
constructor(opt={}) ::
|
|
7
13
|
this._conn_ = _mqtt_conn @ this,
|
|
8
14
|
this._init_dispatch(opt, this)
|
|
9
15
|
|
|
16
|
+
async conn_emit(evt, arg, err_arg) ::
|
|
17
|
+
this.log_conn?.(evt, arg, err_arg)
|
|
18
|
+
try ::
|
|
19
|
+
let fn_evt = this[await evt] // microtask break
|
|
20
|
+
if fn_evt ::
|
|
21
|
+
await fn_evt.call(this, this, arg, err_arg)
|
|
22
|
+
else if err_arg ::
|
|
23
|
+
await this.on_error(err_arg, evt)
|
|
24
|
+
catch err ::
|
|
25
|
+
this.on_error(err, evt)
|
|
26
|
+
|
|
27
|
+
on_error(err, err_path) ::
|
|
28
|
+
console.warn @ '[[u8-mqtt error: %s]]', err_path, err
|
|
29
|
+
|
|
10
30
|
// Handshaking Packets
|
|
11
31
|
|
|
12
32
|
async connect(pkt={}) ::
|
|
13
|
-
let
|
|
14
|
-
if
|
|
15
|
-
pkt.client_id = cid = this.init_client_id(['u8-mqtt--', ''])
|
|
16
|
-
else if Array.isArray(cid) ::
|
|
33
|
+
let cid = pkt.client_id || ['u8-mqtt--', '']
|
|
34
|
+
if Array.isArray(cid) ::
|
|
17
35
|
pkt.client_id = cid = this.init_client_id(cid)
|
|
18
|
-
|
|
36
|
+
this.client_id = cid
|
|
19
37
|
|
|
20
38
|
if null == pkt.keep_alive ::
|
|
21
39
|
pkt.keep_alive = 60
|
|
@@ -23,13 +41,16 @@ export class MQTTBaseClient ::
|
|
|
23
41
|
let res = await this._conn_
|
|
24
42
|
.send_connect('connect', pkt, 'connack')
|
|
25
43
|
|
|
44
|
+
if 0 != res[0].reason ::
|
|
45
|
+
throw new this.MQTTError(res[0])
|
|
46
|
+
|
|
26
47
|
// TODO: merge with server's keep_alive frequency
|
|
27
48
|
this._conn_.ping(pkt.keep_alive)
|
|
28
49
|
return res
|
|
29
50
|
|
|
30
51
|
async disconnect(pkt={}) ::
|
|
31
52
|
let res = await this._send('disconnect', pkt)
|
|
32
|
-
this._conn_.reset()
|
|
53
|
+
this._conn_.reset(false)
|
|
33
54
|
return res
|
|
34
55
|
|
|
35
56
|
auth(pkt={}) ::
|
|
@@ -43,8 +64,9 @@ export class MQTTBaseClient ::
|
|
|
43
64
|
pkt = _as_topics(pkt, ex)
|
|
44
65
|
return this._send('subscribe', pkt, pkt)
|
|
45
66
|
_sub_chain(topic, ex) ::
|
|
67
|
+
let res = this.subscribe @ [[ topic ]], ex
|
|
46
68
|
let subs = this.subs || @ this.subs = new Map()
|
|
47
|
-
subs.set @ topic,
|
|
69
|
+
subs.set @ (res.topic = topic), (subs.last = res)
|
|
48
70
|
return this // fluent api -- return this and track side effects
|
|
49
71
|
|
|
50
72
|
// alias: unsub
|
|
@@ -122,7 +144,7 @@ export class MQTTBaseClient ::
|
|
|
122
144
|
|
|
123
145
|
#IF PLAT_WEB
|
|
124
146
|
sess_client_id(parts) ::
|
|
125
|
-
let key =
|
|
147
|
+
let key = parts.join('\x20')
|
|
126
148
|
let cid = sessionStorage.getItem(key)
|
|
127
149
|
if null == cid ::
|
|
128
150
|
cid = this.new_client_id(parts)
|
|
@@ -138,21 +160,18 @@ export class MQTTBaseClient ::
|
|
|
138
160
|
return this.router = _mqtt_topic_router(this)
|
|
139
161
|
|
|
140
162
|
_init_dispatch(opt) ::
|
|
141
|
-
let router = this._init_router(opt, this)
|
|
142
|
-
|
|
143
163
|
let tgt = @{}
|
|
144
164
|
__proto__: opt.on_mqtt_type || {}
|
|
145
|
-
router
|
|
146
|
-
|
|
147
|
-
if ! tgt.mqtt_publish ::
|
|
148
|
-
tgt.mqtt_publish = router.invoke
|
|
165
|
+
router: this._init_router(opt, this)
|
|
149
166
|
|
|
167
|
+
tgt.mqtt_publish ||= tgt.router.invoke
|
|
150
168
|
return _mqtt_dispatch(this, tgt)
|
|
151
169
|
|
|
152
170
|
|
|
153
171
|
::
|
|
154
|
-
let p =
|
|
172
|
+
let p = MQTTBase.prototype
|
|
155
173
|
Object.assign @ p, @{}
|
|
174
|
+
MQTTError
|
|
156
175
|
pub: p.publish
|
|
157
176
|
sub: p.subscribe
|
|
158
177
|
unsub: p.unsubscribe
|
package/code/core.jsy
CHANGED
|
@@ -1,48 +1,70 @@
|
|
|
1
|
-
import
|
|
1
|
+
import mqtt_pkt_ctx from 'u8-mqtt-packet/esm/codec_bind.js'
|
|
2
|
+
import { MQTTBase } from './base.jsy'
|
|
2
3
|
|
|
3
4
|
#IF PLAT_NODEJS
|
|
4
|
-
import {connect as tcp_connect} from 'net'
|
|
5
|
+
import {connect as tcp_connect} from 'node:net'
|
|
5
6
|
|
|
7
|
+
const pkt_api = {
|
|
8
|
+
utf8(u8) { return new TextDecoder('utf-8').decode(u8 || this.payload ) },
|
|
9
|
+
json(u8) { return JSON.parse( this.utf8(u8) || null ) },
|
|
10
|
+
text(u8) { return this.utf8(u8) },
|
|
11
|
+
}
|
|
6
12
|
|
|
7
|
-
export class
|
|
8
|
-
static _with_session(mqtt_session) ::
|
|
9
|
-
this.prototype._mqtt_session = mqtt_session
|
|
10
|
-
|
|
13
|
+
export class MQTTCore extends MQTTBase ::
|
|
11
14
|
constructor(opt={}) ::
|
|
12
15
|
super(opt)
|
|
13
|
-
this.
|
|
14
|
-
this.with_reconnect(opt.on_reconnect)
|
|
15
|
-
|
|
16
|
+
this.with(opt)
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
static mqtt_ctx(mqtt_level, mqtt_opts, pkt_ctx=pkt_api) ::
|
|
19
|
+
let self = class extends this {}
|
|
20
|
+
self.prototype.mqtt_ctx =
|
|
21
|
+
mqtt_pkt_ctx(mqtt_level, mqtt_opts, pkt_ctx)
|
|
22
|
+
return self
|
|
21
23
|
|
|
24
|
+
with(fns_ns) ::
|
|
25
|
+
for let [k,v] of Object.entries(fns_ns) ::
|
|
26
|
+
if 'function' === typeof v :: this[k] = v
|
|
22
27
|
return this
|
|
23
28
|
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
if on_reconnect ::
|
|
27
|
-
this.on_reconnect = on_reconnect
|
|
29
|
+
//log_conn(evt, arg, err_arg) ::
|
|
30
|
+
//console.info @ '[[u8-mqtt log: %s]]', evt, arg, err_arg
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
on_live(client, is_reconnect) ::
|
|
33
|
+
if is_reconnect ::
|
|
34
|
+
return client.connect()
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
//on_reconnect(client) ::
|
|
33
37
|
|
|
38
|
+
_use_conn(fn_reconnect) ::
|
|
39
|
+
return (this.reconnect = fn_reconnect)?.()
|
|
40
|
+
with_autoreconnect(opt=2000) ::
|
|
41
|
+
if opt.toFixed :: opt = @{} delay: opt
|
|
42
|
+
return this.with @:
|
|
43
|
+
on_reconnect() ::
|
|
44
|
+
this.delay(opt.delay || 2000)
|
|
45
|
+
.then(this.reconnect)
|
|
46
|
+
.then(opt.reconnect, opt.error)
|
|
47
|
+
|
|
48
|
+
on_disconnect(client, intentional) ::
|
|
49
|
+
if ! intentional ::
|
|
50
|
+
return client.on_reconnect?.()
|
|
51
|
+
|
|
52
|
+
delay(ms) ::
|
|
53
|
+
return new Promise @ done => setTimeout(done, ms)
|
|
34
54
|
|
|
35
55
|
with_async_iter(async_iter, write_u8_pkt) ::
|
|
36
56
|
let on_mqtt_chunk = this._conn_.set @
|
|
37
|
-
this.
|
|
57
|
+
this.mqtt_ctx,
|
|
38
58
|
write_u8_pkt
|
|
39
59
|
|
|
40
60
|
this._msg_loop = @!>
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
61
|
+
try ::
|
|
62
|
+
async_iter = await async_iter
|
|
63
|
+
for await (let chunk of async_iter)
|
|
64
|
+
on_mqtt_chunk(chunk)
|
|
65
|
+
this._conn_.reset()
|
|
66
|
+
catch err ::
|
|
67
|
+
this._conn_.reset(err)
|
|
46
68
|
|
|
47
69
|
return this
|
|
48
70
|
|
|
@@ -52,14 +74,15 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
52
74
|
if !Number.isFinite(port) ::
|
|
53
75
|
({port, hostname} = new URL(port))
|
|
54
76
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
77
|
+
return this._use_conn @::
|
|
78
|
+
let conn = Deno.connect @:
|
|
79
|
+
port, hostname, transport: 'tcp'
|
|
80
|
+
|
|
58
81
|
this.with_async_iter @
|
|
59
|
-
Deno.iter
|
|
60
|
-
u8_pkt => conn.write(u8_pkt)
|
|
82
|
+
conn.then @ Deno.iter,
|
|
83
|
+
async u8_pkt => (await conn).write(u8_pkt)
|
|
61
84
|
|
|
62
|
-
|
|
85
|
+
return this
|
|
63
86
|
|
|
64
87
|
|
|
65
88
|
#IF PLAT_NODEJS
|
|
@@ -67,27 +90,25 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
67
90
|
if !Number.isFinite(port) ::
|
|
68
91
|
({port, hostname} = new URL(port))
|
|
69
92
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
#IF HAS_STREAM
|
|
75
|
-
with_stream(read_stream, write_stream) ::
|
|
76
|
-
if undefined === write_stream ::
|
|
77
|
-
write_stream = read_stream
|
|
93
|
+
return this._use_conn @=>
|
|
94
|
+
this.with_stream @
|
|
95
|
+
tcp_connect(port, hostname)
|
|
78
96
|
|
|
79
|
-
return this.with_async_iter @ read_stream,
|
|
80
|
-
u8_pkt => write_stream.write(u8_pkt)
|
|
81
97
|
|
|
98
|
+
with_stream(read_stream, write_stream) ::
|
|
99
|
+
if undefined === write_stream ::
|
|
100
|
+
write_stream = read_stream
|
|
82
101
|
|
|
102
|
+
return this.with_async_iter @ read_stream,
|
|
103
|
+
u8_pkt => write_stream.write(u8_pkt)
|
|
83
104
|
|
|
84
105
|
|
|
85
106
|
with_websock(websock) ::
|
|
86
|
-
if
|
|
87
|
-
websock = 'ws://127.0.0.1:9001'
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
107
|
+
if ! websock?.send ::
|
|
108
|
+
websock = new URL(websock || 'ws://127.0.0.1:9001')
|
|
109
|
+
return this._use_conn @=>
|
|
110
|
+
this.with_websock @
|
|
111
|
+
new WebSocket(websock, ['mqtt'])
|
|
91
112
|
|
|
92
113
|
websock.binaryType = 'arraybuffer'
|
|
93
114
|
|
|
@@ -96,27 +117,25 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
96
117
|
if 0 !== readyState ::
|
|
97
118
|
throw new Error @ 'Invalid WebSocket readyState'
|
|
98
119
|
|
|
99
|
-
ready = new Promise
|
|
100
|
-
websock.addEventListener('open', y, {once: true}))
|
|
120
|
+
ready = new Promise @ fn => websock.onopen = fn
|
|
101
121
|
|
|
102
122
|
|
|
103
123
|
let {_conn_} = this
|
|
104
124
|
let on_mqtt_chunk = _conn_.set @
|
|
105
|
-
this.
|
|
125
|
+
this.mqtt_ctx,
|
|
106
126
|
async u8_pkt => @
|
|
107
127
|
await ready
|
|
108
128
|
websock.send(u8_pkt)
|
|
109
129
|
|
|
110
|
-
websock.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
130
|
+
websock.onmessage = evt => @ on_mqtt_chunk @ new Uint8Array(evt.data)
|
|
131
|
+
websock.onclose = evt => ::
|
|
132
|
+
if ! evt.wasClean ::
|
|
133
|
+
var err = new Error('websocket connection close')
|
|
134
|
+
err.code = evt.code
|
|
135
|
+
err.reason = evt.reason
|
|
116
136
|
|
|
117
|
-
|
|
118
|
-
on_mqtt_chunk @
|
|
119
|
-
new Uint8Array(evt.data)
|
|
137
|
+
_conn_.reset(err)
|
|
120
138
|
|
|
121
139
|
return this
|
|
122
140
|
|
|
141
|
+
export { MQTTCore as default }
|
package/code/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export {
|
|
2
|
+
version,
|
|
3
|
+
MQTTClient_v5, mqtt_v5,
|
|
4
|
+
MQTTClient_v4, mqtt_v4, mqtt_v4 as default, // u8-mqtt-packet version 5 can also parse version 4 (3.1.1) packets
|
|
5
|
+
} from './v5.mjs'
|
|
5
6
|
|
|
6
7
|
export * from './core.jsy'
|
|
7
8
|
export * from './base.jsy'
|
package/code/v4.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import mqtt_opts_v4 from 'u8-mqtt-packet/esm/codec_v4_client.js'
|
|
2
|
+
import MQTTCore from './core.jsy'
|
|
3
|
+
export { version } from '../package.json'
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
5
|
+
const MQTTClient_v4 = /* #__PURE__ */
|
|
6
|
+
MQTTCore.mqtt_ctx(4, mqtt_opts_v4)
|
|
7
7
|
|
|
8
|
-
const mqtt_v4 = opt =>
|
|
8
|
+
const mqtt_v4 = opt =>
|
|
9
|
+
new MQTTClient_v4(opt)
|
|
9
10
|
|
|
10
11
|
export {
|
|
11
12
|
MQTTClient_v4,
|
package/code/v5.mjs
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import mqtt_opts_v5 from 'u8-mqtt-packet/esm/codec_v5_client.js'
|
|
2
|
+
import MQTTCore from './core.jsy'
|
|
3
|
+
export { version } from '../package.json'
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const MQTTClient_v4 = /* #__PURE__ */
|
|
6
|
+
MQTTCore.mqtt_ctx(4, mqtt_opts_v5)
|
|
7
|
+
|
|
8
|
+
const MQTTClient_v5 = /* #__PURE__ */
|
|
9
|
+
MQTTCore.mqtt_ctx(5, mqtt_opts_v5)
|
|
10
|
+
|
|
11
|
+
const mqtt_v4 = opt =>
|
|
12
|
+
new MQTTClient_v4(opt)
|
|
7
13
|
|
|
8
|
-
const mqtt_v5 = opt =>
|
|
14
|
+
const mqtt_v5 = opt =>
|
|
15
|
+
new MQTTClient_v5(opt)
|
|
9
16
|
|
|
10
17
|
export {
|
|
18
|
+
MQTTClient_v4,
|
|
11
19
|
MQTTClient_v5,
|
|
12
20
|
MQTTClient_v5 as MQTTClient,
|
|
13
21
|
|
|
22
|
+
mqtt_v4,
|
|
14
23
|
mqtt_v5,
|
|
15
24
|
mqtt_v5 as mqtt,
|
|
16
25
|
mqtt_v5 as default,
|