u8-mqtt 0.0.23
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/LICENSE +25 -0
- package/README.md +119 -0
- package/cjs/index.cjs +1397 -0
- package/cjs/index.cjs.map +1 -0
- package/cjs/v4.cjs +1382 -0
- package/cjs/v4.cjs.map +1 -0
- package/cjs/v5.cjs +1382 -0
- package/cjs/v5.cjs.map +1 -0
- package/code/_cmdid_dispatch.jsy +73 -0
- package/code/_conn.jsy +83 -0
- package/code/_dispatch.jsy +23 -0
- package/code/_router.jsy +76 -0
- package/code/base.jsy +169 -0
- package/code/core.jsy +123 -0
- package/code/index.mjs +12 -0
- package/code/session.mjs +62 -0
- package/code/v4.mjs +17 -0
- package/code/v5.mjs +17 -0
- package/esm/deno/index.mjs +1378 -0
- package/esm/deno/index.mjs.map +1 -0
- package/esm/deno/v4.mjs +1372 -0
- package/esm/deno/v4.mjs.map +1 -0
- package/esm/deno/v5.mjs +1372 -0
- package/esm/deno/v5.mjs.map +1 -0
- package/esm/node/index.mjs +1380 -0
- package/esm/node/index.mjs.map +1 -0
- package/esm/node/v4.mjs +1374 -0
- package/esm/node/v4.mjs.map +1 -0
- package/esm/node/v5.mjs +1374 -0
- package/esm/node/v5.mjs.map +1 -0
- package/esm/web/index.min.mjs +1 -0
- package/esm/web/index.mjs +1378 -0
- package/esm/web/index.mjs.map +1 -0
- package/esm/web/v4.min.mjs +1 -0
- package/esm/web/v4.mjs +1372 -0
- package/esm/web/v4.mjs.map +1 -0
- package/esm/web/v5.min.mjs +1 -0
- package/esm/web/v5.mjs +1372 -0
- package/esm/web/v5.mjs.map +1 -0
- package/package.json +59 -0
package/code/core.jsy
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {MQTTBaseClient} from './base.jsy'
|
|
2
|
+
|
|
3
|
+
#IF PLAT_NODEJS
|
|
4
|
+
import {connect as tcp_connect} from 'net'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export class MQTTCoreClient extends MQTTBaseClient ::
|
|
8
|
+
static _with_session(mqtt_session) ::
|
|
9
|
+
this.prototype._mqtt_session = mqtt_session
|
|
10
|
+
|
|
11
|
+
constructor(opt={}) ::
|
|
12
|
+
super(opt)
|
|
13
|
+
this.with_live(opt.on_live)
|
|
14
|
+
this.with_reconnect(opt.on_reconnect)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
// on_live(client) ::
|
|
18
|
+
with_live(on_live) ::
|
|
19
|
+
if on_live ::
|
|
20
|
+
this.on_live = on_live
|
|
21
|
+
|
|
22
|
+
return this
|
|
23
|
+
|
|
24
|
+
// on_reconnect(client) ::
|
|
25
|
+
with_reconnect(on_reconnect) ::
|
|
26
|
+
if on_reconnect ::
|
|
27
|
+
this.on_reconnect = on_reconnect
|
|
28
|
+
|
|
29
|
+
if ! this._conn_.is_set ::
|
|
30
|
+
on_reconnect(this)
|
|
31
|
+
|
|
32
|
+
return this
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
with_async_iter(async_iter, write_u8_pkt) ::
|
|
36
|
+
let on_mqtt_chunk = this._conn_.set @
|
|
37
|
+
this._mqtt_session(),
|
|
38
|
+
write_u8_pkt
|
|
39
|
+
|
|
40
|
+
this._msg_loop = @!>
|
|
41
|
+
async_iter = await async_iter
|
|
42
|
+
for await (let chunk of async_iter)
|
|
43
|
+
on_mqtt_chunk(chunk)
|
|
44
|
+
|
|
45
|
+
this._conn_.reset()
|
|
46
|
+
|
|
47
|
+
return this
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
#IF PLAT_DENO
|
|
51
|
+
with_tcp(port, hostname) ::
|
|
52
|
+
if !Number.isFinite(port) ::
|
|
53
|
+
({port, host: hostname} = port)
|
|
54
|
+
|
|
55
|
+
Deno.connect @:
|
|
56
|
+
port: port || 1883, hostname, transport: 'tcp'
|
|
57
|
+
.then @\ conn =>
|
|
58
|
+
this.with_async_iter @
|
|
59
|
+
Deno.iter(conn),
|
|
60
|
+
u8_pkt => conn.write(u8_pkt)
|
|
61
|
+
|
|
62
|
+
return this
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
#IF PLAT_NODEJS
|
|
66
|
+
with_tcp(port, hostname) ::
|
|
67
|
+
if !Number.isFinite(port) ::
|
|
68
|
+
({port, host: hostname} = port)
|
|
69
|
+
|
|
70
|
+
let sock = tcp_connect(port, hostname)
|
|
71
|
+
return this.with_stream(sock)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
#IF HAS_STREAM
|
|
75
|
+
with_stream(read_stream, write_stream) ::
|
|
76
|
+
if undefined === write_stream ::
|
|
77
|
+
write_stream = read_stream
|
|
78
|
+
|
|
79
|
+
read_stream.once @ 'end', this._conn_.reset
|
|
80
|
+
return this.with_async_iter @ read_stream,
|
|
81
|
+
u8_pkt => write_stream.write(u8_pkt)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
with_websock(websock) ::
|
|
87
|
+
if null == websock ::
|
|
88
|
+
websock = 'ws://127.0.0.1:9001'
|
|
89
|
+
|
|
90
|
+
if websock.origin || 'string' === typeof websock ::
|
|
91
|
+
websock = new WebSocket(new URL(websock), ['mqtt'])
|
|
92
|
+
|
|
93
|
+
websock.binaryType = 'arraybuffer'
|
|
94
|
+
|
|
95
|
+
let ready, {readyState} = websock
|
|
96
|
+
if 1 !== readyState ::
|
|
97
|
+
if 0 !== readyState ::
|
|
98
|
+
throw new Error @ 'Invalid WebSocket readyState'
|
|
99
|
+
|
|
100
|
+
ready = new Promise( y =>
|
|
101
|
+
websock.addEventListener('open', y, {once: true}))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
let {_conn_} = this
|
|
105
|
+
let on_mqtt_chunk = _conn_.set @
|
|
106
|
+
this._mqtt_session(),
|
|
107
|
+
async u8_pkt => @
|
|
108
|
+
await ready
|
|
109
|
+
websock.send(u8_pkt)
|
|
110
|
+
|
|
111
|
+
websock.addEventListener @ 'close',
|
|
112
|
+
@::
|
|
113
|
+
delete websock.onmessage
|
|
114
|
+
_conn_.reset()
|
|
115
|
+
|
|
116
|
+
{once: true}
|
|
117
|
+
|
|
118
|
+
websock.onmessage = evt =>
|
|
119
|
+
on_mqtt_chunk @
|
|
120
|
+
new Uint8Array(evt.data)
|
|
121
|
+
|
|
122
|
+
return this
|
|
123
|
+
|
package/code/index.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export {MQTTClient_v4, mqtt_v4, default} from './v4.mjs'
|
|
2
|
+
export {MQTTClient_v5, mqtt_v5} from './v5.mjs'
|
|
3
|
+
|
|
4
|
+
export * from './session.mjs'
|
|
5
|
+
|
|
6
|
+
export * from './core.jsy'
|
|
7
|
+
export * from './base.jsy'
|
|
8
|
+
export * from './_conn.jsy'
|
|
9
|
+
export * from './_router.jsy'
|
|
10
|
+
export * from './_dispatch.jsy'
|
|
11
|
+
export * from './_cmdid_dispatch.jsy'
|
|
12
|
+
|
package/code/session.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
_bind_mqtt_session_ctx,
|
|
3
|
+
|
|
4
|
+
mqtt_decode_connack,
|
|
5
|
+
mqtt_decode_publish,
|
|
6
|
+
mqtt_decode_puback,
|
|
7
|
+
mqtt_decode_pubxxx,
|
|
8
|
+
mqtt_decode_pingxxx,
|
|
9
|
+
mqtt_decode_suback,
|
|
10
|
+
mqtt_decode_unsuback,
|
|
11
|
+
mqtt_decode_auth,
|
|
12
|
+
|
|
13
|
+
mqtt_encode_connect,
|
|
14
|
+
mqtt_encode_disconnect,
|
|
15
|
+
mqtt_encode_publish,
|
|
16
|
+
mqtt_encode_puback,
|
|
17
|
+
mqtt_encode_pingxxx,
|
|
18
|
+
mqtt_encode_subscribe,
|
|
19
|
+
mqtt_encode_unsubscribe,
|
|
20
|
+
mqtt_encode_auth,
|
|
21
|
+
} from 'u8-mqtt-packet'
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export function mqtt_session_ctx(mqtt_level) {
|
|
25
|
+
let {ctx} = mqtt_session_ctx
|
|
26
|
+
if (undefined === ctx ) {
|
|
27
|
+
let as_utf8 = u8 =>
|
|
28
|
+
new TextDecoder('utf-8').decode(u8)
|
|
29
|
+
|
|
30
|
+
let std_pkt_api = {
|
|
31
|
+
utf8(u8) { return as_utf8( u8 || this.payload ) },
|
|
32
|
+
json(u8) { return JSON.parse( as_utf8( u8 || this.payload ) || null ) },
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
mqtt_session_ctx.ctx = ctx =
|
|
36
|
+
_bind_mqtt_session_ctx(
|
|
37
|
+
[ // lst_decode_ops
|
|
38
|
+
mqtt_decode_connack,
|
|
39
|
+
mqtt_decode_publish,
|
|
40
|
+
mqtt_decode_puback,
|
|
41
|
+
mqtt_decode_pubxxx,
|
|
42
|
+
mqtt_decode_pingxxx,
|
|
43
|
+
mqtt_decode_suback,
|
|
44
|
+
mqtt_decode_unsuback,
|
|
45
|
+
mqtt_decode_auth, ],
|
|
46
|
+
|
|
47
|
+
[ // lst_encode_ops
|
|
48
|
+
mqtt_encode_connect,
|
|
49
|
+
mqtt_encode_disconnect,
|
|
50
|
+
mqtt_encode_publish,
|
|
51
|
+
mqtt_encode_puback,
|
|
52
|
+
mqtt_encode_pingxxx,
|
|
53
|
+
mqtt_encode_subscribe,
|
|
54
|
+
mqtt_encode_unsubscribe,
|
|
55
|
+
mqtt_encode_auth, ],
|
|
56
|
+
|
|
57
|
+
std_pkt_api )
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return ctx(mqtt_level)
|
|
61
|
+
}
|
|
62
|
+
|
package/code/v4.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {mqtt_session_ctx} from './session.mjs'
|
|
2
|
+
import {MQTTCoreClient} from './core.jsy'
|
|
3
|
+
|
|
4
|
+
class MQTTClient_v4 extends MQTTCoreClient {
|
|
5
|
+
_mqtt_session() { return mqtt_session_ctx(4)() }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const mqtt_v4 = opt => new MQTTClient_v4(opt)
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
MQTTClient_v4,
|
|
12
|
+
MQTTClient_v4 as MQTTClient,
|
|
13
|
+
|
|
14
|
+
mqtt_v4,
|
|
15
|
+
mqtt_v4 as mqtt,
|
|
16
|
+
mqtt_v4 as default,
|
|
17
|
+
}
|
package/code/v5.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {mqtt_session_ctx} from './session.mjs'
|
|
2
|
+
import {MQTTCoreClient} from './core.jsy'
|
|
3
|
+
|
|
4
|
+
class MQTTClient_v5 extends MQTTCoreClient {
|
|
5
|
+
_mqtt_session() { return mqtt_session_ctx(5)() }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const mqtt_v5 = opt => new MQTTClient_v5(opt)
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
MQTTClient_v5,
|
|
12
|
+
MQTTClient_v5 as MQTTClient,
|
|
13
|
+
|
|
14
|
+
mqtt_v5,
|
|
15
|
+
mqtt_v5 as mqtt,
|
|
16
|
+
mqtt_v5 as default,
|
|
17
|
+
}
|