u8-mqtt 0.0.22 → 0.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.cjs +185 -81
- package/cjs/index.cjs.map +1 -1
- package/cjs/v4.cjs +185 -81
- package/cjs/v4.cjs.map +1 -1
- package/cjs/v5.cjs +185 -81
- package/cjs/v5.cjs.map +1 -1
- package/code/_cmdid_dispatch.jsy +8 -8
- package/code/_conn.jsy +14 -11
- package/code/_dispatch.jsy +4 -4
- package/code/_router.jsy +23 -12
- package/code/base.jsy +77 -32
- package/code/core.jsy +11 -13
- package/code/session.mjs +5 -4
- package/code/v4.mjs +3 -3
- package/code/v5.mjs +3 -3
- package/esm/deno/index.mjs +184 -81
- package/esm/deno/index.mjs.map +1 -1
- package/esm/deno/v4.mjs +184 -81
- package/esm/deno/v4.mjs.map +1 -1
- package/esm/deno/v5.mjs +184 -81
- package/esm/deno/v5.mjs.map +1 -1
- package/esm/node/index.mjs +185 -82
- package/esm/node/index.mjs.map +1 -1
- package/esm/node/v4.mjs +185 -82
- package/esm/node/v4.mjs.map +1 -1
- package/esm/node/v5.mjs +185 -82
- package/esm/node/v5.mjs.map +1 -1
- package/esm/web/index.min.mjs +1 -1
- package/esm/web/index.mjs +184 -81
- package/esm/web/index.mjs.map +1 -1
- package/esm/web/v4.min.mjs +1 -1
- package/esm/web/v4.mjs +184 -81
- package/esm/web/v4.mjs.map +1 -1
- package/esm/web/v5.min.mjs +1 -1
- package/esm/web/v5.mjs +184 -81
- package/esm/web/v5.mjs.map +1 -1
- package/package.json +17 -12
package/code/base.jsy
CHANGED
|
@@ -2,7 +2,6 @@ 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 default MQTTBaseClient
|
|
6
5
|
export class MQTTBaseClient ::
|
|
7
6
|
constructor(opt={}) ::
|
|
8
7
|
this._conn_ = _mqtt_conn @ this,
|
|
@@ -21,15 +20,15 @@ export class MQTTBaseClient ::
|
|
|
21
20
|
if null == pkt.keep_alive ::
|
|
22
21
|
pkt.keep_alive = 60
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
.send_connect
|
|
23
|
+
let res = await this._conn_
|
|
24
|
+
.send_connect('connect', pkt, 'connack')
|
|
26
25
|
|
|
27
26
|
// TODO: merge with server's keep_alive frequency
|
|
28
27
|
this._conn_.ping(pkt.keep_alive)
|
|
29
28
|
return res
|
|
30
29
|
|
|
31
30
|
async disconnect(pkt={}) ::
|
|
32
|
-
|
|
31
|
+
let res = await this._send('disconnect', pkt)
|
|
33
32
|
this._conn_.reset()
|
|
34
33
|
return res
|
|
35
34
|
|
|
@@ -49,20 +48,38 @@ export class MQTTBaseClient ::
|
|
|
49
48
|
pkt = _as_topics(pkt, ex)
|
|
50
49
|
return this._send('unsubscribe', pkt, pkt)
|
|
51
50
|
|
|
52
|
-
|
|
51
|
+
get on_topic() :: return this.router.add
|
|
52
|
+
|
|
53
|
+
// alias: sub_topic
|
|
53
54
|
subscribe_topic(topic_route, ...args) ::
|
|
54
|
-
let topic =
|
|
55
|
-
this.
|
|
55
|
+
let topic = this.topic_for(topic_route)
|
|
56
|
+
this.router.add @ topic_route, true, args.pop() // handler
|
|
56
57
|
this.subscribe @ [[ topic ]], args.pop() // ex
|
|
57
58
|
return this
|
|
58
59
|
|
|
60
|
+
// alias: unsub_topic
|
|
61
|
+
unsubscribe_topic(topic_route) ::
|
|
62
|
+
let topic = this.topic_for(topic_route)
|
|
63
|
+
this.router.remove @ topic_route, true
|
|
64
|
+
return this.unsubscribe @ [[ topic ]]
|
|
65
|
+
|
|
66
|
+
topic_for(topic_route) ::
|
|
67
|
+
return topic_route.replace(/[:*].*$/, '#')
|
|
68
|
+
|
|
59
69
|
|
|
60
70
|
// alias: pub
|
|
61
|
-
publish(pkt) :: return _pub(this, pkt)
|
|
62
|
-
post(topic, payload) :: return _pub(this,
|
|
63
|
-
send(topic, payload) :: return _pub(this,
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
publish(pkt, fn_encode) :: return _pub(this, pkt, fn_encode)
|
|
72
|
+
post(topic, payload) :: return _pub.m(this, topic, payload)
|
|
73
|
+
send(topic, payload) :: return _pub.mq(this, topic, payload)
|
|
74
|
+
store(topic, payload) :: return _pub.mqr(this, topic, payload)
|
|
75
|
+
|
|
76
|
+
json_post(topic, msg) :: return _pub.o(this, topic, msg)
|
|
77
|
+
json_send(topic, msg) :: return _pub.oq(this, topic, msg)
|
|
78
|
+
json_store(topic, msg) :: return _pub.oqr(this, topic, msg)
|
|
79
|
+
|
|
80
|
+
obj_post(topic, msg, fn_encode) :: return _pub.o(this, topic, msg, fn_encode)
|
|
81
|
+
obj_send(topic, msg, fn_encode) :: return _pub.oq(this, topic, msg, fn_encode)
|
|
82
|
+
obj_store(topic, msg, fn_encode) :: return _pub.oqr(this, topic, msg, fn_encode)
|
|
66
83
|
|
|
67
84
|
|
|
68
85
|
|
|
@@ -99,27 +116,29 @@ export class MQTTBaseClient ::
|
|
|
99
116
|
/* async _send(type, pkt) -- provided by _conn_ and transport */
|
|
100
117
|
|
|
101
118
|
_init_router(opt) ::
|
|
102
|
-
|
|
103
|
-
this.on_topic = router.add
|
|
104
|
-
return this.router = router
|
|
119
|
+
return this.router = _mqtt_topic_router(this)
|
|
105
120
|
|
|
106
121
|
_init_dispatch(opt) ::
|
|
107
|
-
|
|
122
|
+
let router = this._init_router(opt, this)
|
|
108
123
|
|
|
109
|
-
|
|
124
|
+
let tgt = @{}
|
|
110
125
|
__proto__: opt.on_mqtt_type || {}
|
|
111
|
-
|
|
126
|
+
router
|
|
127
|
+
|
|
128
|
+
if ! tgt.mqtt_publish ::
|
|
129
|
+
tgt.mqtt_publish = router.invoke
|
|
112
130
|
|
|
113
|
-
return _mqtt_dispatch
|
|
131
|
+
return _mqtt_dispatch(this, tgt)
|
|
114
132
|
|
|
115
133
|
|
|
116
134
|
::
|
|
117
|
-
|
|
135
|
+
let p = MQTTBaseClient.prototype
|
|
118
136
|
Object.assign @ p, @{}
|
|
119
137
|
pub: p.publish
|
|
120
138
|
sub: p.subscribe
|
|
121
139
|
unsub: p.unsubscribe
|
|
122
140
|
sub_topic: p.subscribe_topic
|
|
141
|
+
unsub_topic: p.unsubscribe_topic
|
|
123
142
|
|
|
124
143
|
/*
|
|
125
144
|
p.on_mqtt_type = {
|
|
@@ -128,6 +147,7 @@ export class MQTTBaseClient ::
|
|
|
128
147
|
mqtt_connack(pkt, ctx) ::
|
|
129
148
|
mqtt_disconnect(pkt, ctx) ::
|
|
130
149
|
|
|
150
|
+
mqtt_publish(pkt, ctx)
|
|
131
151
|
mqtt_subscribe(pkt, ctx) ::
|
|
132
152
|
mqtt_unsubscribe(pkt, ctx) ::
|
|
133
153
|
|
|
@@ -137,18 +157,6 @@ export class MQTTBaseClient ::
|
|
|
137
157
|
*/
|
|
138
158
|
|
|
139
159
|
|
|
140
|
-
function _pub(self, pkt) ::
|
|
141
|
-
let key, {qos, msg, payload} = pkt
|
|
142
|
-
if undefined === payload ::
|
|
143
|
-
if undefined === msg ::
|
|
144
|
-
const arg = pkt.arg || 'payload'
|
|
145
|
-
return v => _pub @ self, {...pkt, [arg]: v}
|
|
146
|
-
|
|
147
|
-
pkt.payload = JSON.stringify(msg)
|
|
148
|
-
|
|
149
|
-
if (1 === qos) key = pkt
|
|
150
|
-
return self._send('publish', pkt, key)
|
|
151
|
-
|
|
152
160
|
function _as_topics(pkt, ex) ::
|
|
153
161
|
if 'string' === typeof pkt ::
|
|
154
162
|
return {topics:[pkt], ... ex}
|
|
@@ -156,3 +164,40 @@ function _as_topics(pkt, ex) ::
|
|
|
156
164
|
return {topics:[... pkt], ... ex}
|
|
157
165
|
return ex ? {...pkt, ...ex} : pkt
|
|
158
166
|
|
|
167
|
+
|
|
168
|
+
function _pub(self, pkt, fn_encode) ::
|
|
169
|
+
if undefined === pkt.payload ::
|
|
170
|
+
let {msg} = pkt
|
|
171
|
+
switch typeof msg ::
|
|
172
|
+
case 'function':
|
|
173
|
+
fn_encode = msg
|
|
174
|
+
msg = undefined
|
|
175
|
+
|
|
176
|
+
case 'undefined':
|
|
177
|
+
let arg = pkt.arg || 'payload'
|
|
178
|
+
return v => _pub(self, {...pkt, [arg]: v}, fn_encode)
|
|
179
|
+
|
|
180
|
+
default:
|
|
181
|
+
pkt.payload = fn_encode
|
|
182
|
+
? fn_encode(msg)
|
|
183
|
+
: JSON.stringify(msg)
|
|
184
|
+
|
|
185
|
+
return self._send @ 'publish', pkt,
|
|
186
|
+
pkt.qos ? pkt : void 0 // key
|
|
187
|
+
|
|
188
|
+
::
|
|
189
|
+
Object.assign @ _pub, @{}
|
|
190
|
+
m: (self, topic, payload) =>
|
|
191
|
+
_pub(self, {topic, payload, qos:0})
|
|
192
|
+
mq: (self, topic, payload) =>
|
|
193
|
+
_pub(self, {topic, payload, qos:1})
|
|
194
|
+
mqr: (self, topic, payload) =>
|
|
195
|
+
_pub(self, {topic, payload, qos:1, retain: 1})
|
|
196
|
+
|
|
197
|
+
o: (self, topic, msg, fn_encode) =>
|
|
198
|
+
_pub(self, {topic, msg, arg: 'msg', qos:0}, fn_encode)
|
|
199
|
+
oq: (self, topic, msg, fn_encode) =>
|
|
200
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1}, fn_encode)
|
|
201
|
+
oqr: (self, topic, msg, fn_encode) =>
|
|
202
|
+
_pub(self, {topic, msg, arg: 'msg', qos:1, retain: 1}, fn_encode)
|
|
203
|
+
|
package/code/core.jsy
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import MQTTBaseClient from './base.jsy'
|
|
1
|
+
import {MQTTBaseClient} from './base.jsy'
|
|
2
2
|
|
|
3
3
|
#IF PLAT_NODEJS
|
|
4
4
|
import {connect as tcp_connect} from 'net'
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
export default MQTTCoreClient
|
|
8
7
|
export class MQTTCoreClient extends MQTTBaseClient ::
|
|
9
8
|
static _with_session(mqtt_session) ::
|
|
10
9
|
this.prototype._mqtt_session = mqtt_session
|
|
@@ -12,7 +11,7 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
12
11
|
constructor(opt={}) ::
|
|
13
12
|
super(opt)
|
|
14
13
|
this.with_live(opt.on_live)
|
|
15
|
-
this.
|
|
14
|
+
this.with_reconnect(opt.on_reconnect)
|
|
16
15
|
|
|
17
16
|
|
|
18
17
|
// on_live(client) ::
|
|
@@ -23,24 +22,24 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
23
22
|
return this
|
|
24
23
|
|
|
25
24
|
// on_reconnect(client) ::
|
|
26
|
-
|
|
27
|
-
if
|
|
28
|
-
this.
|
|
25
|
+
with_reconnect(on_reconnect) ::
|
|
26
|
+
if on_reconnect ::
|
|
27
|
+
this.on_reconnect = on_reconnect
|
|
29
28
|
|
|
30
29
|
if ! this._conn_.is_set ::
|
|
31
|
-
|
|
30
|
+
on_reconnect(this)
|
|
32
31
|
|
|
33
32
|
return this
|
|
34
33
|
|
|
35
34
|
|
|
36
35
|
with_async_iter(async_iter, write_u8_pkt) ::
|
|
37
|
-
|
|
36
|
+
let on_mqtt_chunk = this._conn_.set @
|
|
38
37
|
this._mqtt_session(),
|
|
39
38
|
write_u8_pkt
|
|
40
39
|
|
|
41
40
|
this._msg_loop = @!>
|
|
42
41
|
async_iter = await async_iter
|
|
43
|
-
for await (
|
|
42
|
+
for await (let chunk of async_iter)
|
|
44
43
|
on_mqtt_chunk(chunk)
|
|
45
44
|
|
|
46
45
|
this._conn_.reset()
|
|
@@ -68,7 +67,7 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
68
67
|
if !Number.isFinite(port) ::
|
|
69
68
|
({port, host: hostname} = port)
|
|
70
69
|
|
|
71
|
-
|
|
70
|
+
let sock = tcp_connect(port, hostname)
|
|
72
71
|
return this.with_stream(sock)
|
|
73
72
|
|
|
74
73
|
|
|
@@ -77,7 +76,6 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
77
76
|
if undefined === write_stream ::
|
|
78
77
|
write_stream = read_stream
|
|
79
78
|
|
|
80
|
-
read_stream.once @ 'end', this._conn_.reset
|
|
81
79
|
return this.with_async_iter @ read_stream,
|
|
82
80
|
u8_pkt => write_stream.write(u8_pkt)
|
|
83
81
|
|
|
@@ -102,8 +100,8 @@ export class MQTTCoreClient extends MQTTBaseClient ::
|
|
|
102
100
|
websock.addEventListener('open', y, {once: true}))
|
|
103
101
|
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
let {_conn_} = this
|
|
104
|
+
let on_mqtt_chunk = _conn_.set @
|
|
107
105
|
this._mqtt_session(),
|
|
108
106
|
async u8_pkt => @
|
|
109
107
|
await ready
|
package/code/session.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
_bind_mqtt_session_ctx,
|
|
3
3
|
|
|
4
4
|
mqtt_decode_connack,
|
|
5
|
+
mqtt_decode_disconnect,
|
|
5
6
|
mqtt_decode_publish,
|
|
6
7
|
mqtt_decode_puback,
|
|
7
8
|
mqtt_decode_pubxxx,
|
|
@@ -21,22 +22,22 @@ import {
|
|
|
21
22
|
} from 'u8-mqtt-packet'
|
|
22
23
|
|
|
23
24
|
|
|
24
|
-
export default mqtt_session_ctx
|
|
25
25
|
export function mqtt_session_ctx(mqtt_level) {
|
|
26
26
|
let {ctx} = mqtt_session_ctx
|
|
27
27
|
if (undefined === ctx ) {
|
|
28
|
-
|
|
28
|
+
let as_utf8 = u8 =>
|
|
29
29
|
new TextDecoder('utf-8').decode(u8)
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
let std_pkt_api = {
|
|
32
32
|
utf8(u8) { return as_utf8( u8 || this.payload ) },
|
|
33
|
-
json(u8) { return JSON.parse( as_utf8( u8 || this.payload )) },
|
|
33
|
+
json(u8) { return JSON.parse( as_utf8( u8 || this.payload ) || null ) },
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
mqtt_session_ctx.ctx = ctx =
|
|
37
37
|
_bind_mqtt_session_ctx(
|
|
38
38
|
[ // lst_decode_ops
|
|
39
39
|
mqtt_decode_connack,
|
|
40
|
+
mqtt_decode_disconnect,
|
|
40
41
|
mqtt_decode_publish,
|
|
41
42
|
mqtt_decode_puback,
|
|
42
43
|
mqtt_decode_pubxxx,
|
package/code/v4.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import mqtt_session_ctx from './session.mjs'
|
|
2
|
-
import
|
|
1
|
+
import {mqtt_session_ctx} from './session.mjs'
|
|
2
|
+
import {MQTTCoreClient} from './core.jsy'
|
|
3
3
|
|
|
4
|
-
class MQTTClient_v4 extends
|
|
4
|
+
class MQTTClient_v4 extends MQTTCoreClient {
|
|
5
5
|
_mqtt_session() { return mqtt_session_ctx(4)() }
|
|
6
6
|
}
|
|
7
7
|
|
package/code/v5.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import mqtt_session_ctx from './session.mjs'
|
|
2
|
-
import
|
|
1
|
+
import {mqtt_session_ctx} from './session.mjs'
|
|
2
|
+
import {MQTTCoreClient} from './core.jsy'
|
|
3
3
|
|
|
4
|
-
class MQTTClient_v5 extends
|
|
4
|
+
class MQTTClient_v5 extends MQTTCoreClient {
|
|
5
5
|
_mqtt_session() { return mqtt_session_ctx(5)() }
|
|
6
6
|
}
|
|
7
7
|
|