weida-mqtt 0.0.0-stage → 0.1.0-alpha.19
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 +70 -2
- package/lib/errors.js +29 -0
- package/lib/index.d.ts +437 -0
- package/lib/index.js +1298 -0
- package/lib/native.js +12 -0
- package/lib/shared/convert.js +208 -0
- package/lib/shared/errors.js +28 -0
- package/lib/shared/handle.js +55 -0
- package/lib/shared/load.js +74 -0
- package/lib/shared/wait.js +90 -0
- package/package.json +43 -4
- package/prebuilds/darwin-arm64/libweida_mqtt.dylib +0 -0
- package/prebuilds/darwin-arm64/weida_mqtt.node +0 -0
- package/prebuilds/linux-arm64/libweida_mqtt.so +0 -0
- package/prebuilds/linux-arm64/weida_mqtt.node +0 -0
- package/prebuilds/linux-x64/libweida_mqtt.so +0 -0
- package/prebuilds/linux-x64/weida_mqtt.node +0 -0
- package/prebuilds/win32-x64/weida_mqtt.dll +0 -0
- package/prebuilds/win32-x64/weida_mqtt.node +0 -0
package/README.md
CHANGED
|
@@ -1,3 +1,71 @@
|
|
|
1
|
-
#
|
|
1
|
+
# weida-mqtt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An MQTT 5.0 client for Node, Bun and Deno: one Node-API addon over `libweida_mqtt`, the C ABI of
|
|
4
|
+
the Rust [weida](https://weida.doodleshnookie.net/) MQTT client, with an ES module and TypeScript
|
|
5
|
+
types on top. It connects over TCP or TLS, publishes at QoS 0, 1 and 2 with a completion that
|
|
6
|
+
says what the hop certified, subscribes with all four subscription options and Subscription
|
|
7
|
+
Identifiers, keeps a session across reconnects, answers enhanced authentication (AUTH) rounds,
|
|
8
|
+
and reports every MQTT reason code by name.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install weida-mqtt
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The package ships prebuilt binaries, so installing it needs no Rust toolchain and no compiler.
|
|
17
|
+
|
|
18
|
+
| Platform | Prebuild |
|
|
19
|
+
| --- | --- |
|
|
20
|
+
| Linux x64, Linux arm64 (glibc) | `linux-x64`, `linux-arm64` |
|
|
21
|
+
| macOS on Apple silicon | `darwin-arm64` |
|
|
22
|
+
| Windows x64 | `win32-x64` |
|
|
23
|
+
|
|
24
|
+
It runs on Node 20 and later, on Bun, and on Deno with `--allow-read --allow-env --allow-ffi`.
|
|
25
|
+
With `weida-native-full` installed at the same version, it and `weida` share one native library
|
|
26
|
+
(`weida/mqtt` is this package).
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import * as mqtt from "weida-mqtt";
|
|
32
|
+
|
|
33
|
+
const context = new mqtt.Context();
|
|
34
|
+
const options = new mqtt.ConnectOptions({ clientId: "sensor-1" });
|
|
35
|
+
const { client, events } = await context.connect("127.0.0.1:1883", options);
|
|
36
|
+
|
|
37
|
+
const granted = await client.subscribe([new mqtt.Subscription("room/+", { qos: 2 })]);
|
|
38
|
+
// granted: one code per filter, [2]
|
|
39
|
+
|
|
40
|
+
const done = await client.publish(
|
|
41
|
+
new mqtt.Message("room/12", new TextEncoder().encode("21.5"), { qos: 2 }),
|
|
42
|
+
);
|
|
43
|
+
// done.kind === "complete": the PUBCOMP arrived
|
|
44
|
+
|
|
45
|
+
for await (const delivery of events) {
|
|
46
|
+
console.log(delivery.topic, new TextDecoder().decode(delivery.payload), delivery.qos);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await client.disconnect();
|
|
51
|
+
context.drop();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The JavaScript thread never blocks: every wait is a promise, and every waiting method takes
|
|
55
|
+
`{ signal }`, so `AbortSignal.timeout(ms)` is the timeout. Durations are milliseconds. Failures
|
|
56
|
+
are one class, `MqttError`, to branch on by `error.kind` (`"NotAuthorized"`, `"Timeout"`, ...)
|
|
57
|
+
or `error.code` (`"WEIDA_MQTT_ECONNECTION_REFUSED"`), with the reason code byte in
|
|
58
|
+
`error.reasonCode`.
|
|
59
|
+
|
|
60
|
+
## Learn more
|
|
61
|
+
|
|
62
|
+
- [Website](https://weida.doodleshnookie.net/) and this package's
|
|
63
|
+
[feature sheet](https://weida.doodleshnookie.net/libraries/mqtt-js/) with every class and
|
|
64
|
+
method
|
|
65
|
+
- [Repository](https://github.com/tuco86/weida), package in `crates/js/weida-mqtt`
|
|
66
|
+
|
|
67
|
+
Status: alpha (`0.1.0-alpha.N`); breaking changes are permitted within `0.x`.
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT OR Apache-2.0, at your option.
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The one error class every failure of libweida_mqtt throws (docs/c/README.md
|
|
2
|
+
// R9, docs/libraries/mqtt-js.md section 3): the shared base's `code` and
|
|
3
|
+
// `errno`, the kind of failure it is, and libweida_mqtt's thread-local
|
|
4
|
+
// record, which the addon sets from the call that failed (or from the event
|
|
5
|
+
// that ended the connection).
|
|
6
|
+
import native from "./native.js";
|
|
7
|
+
import { WeidaError } from "./shared/errors.js";
|
|
8
|
+
|
|
9
|
+
export class MqttError extends WeidaError {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "MqttError";
|
|
13
|
+
/** What failed, as the Python package names its exception class:
|
|
14
|
+
* "NotAuthorized", "Timeout", "InvalidTopic", ...; "MqttError" for a
|
|
15
|
+
* code this package does not know. */
|
|
16
|
+
this.kind = "MqttError";
|
|
17
|
+
/** The MQTT reason code byte of `kind` (0x87 for "NotAuthorized"), null
|
|
18
|
+
* for a failure MQTT gives no byte. */
|
|
19
|
+
this.reasonCode = null;
|
|
20
|
+
/** The rule or wait that failed, when the library names one: the broken
|
|
21
|
+
* rule of WEIDA_MQTT_EINVALID_TOPIC, the wait of WEIDA_MQTT_ETIMEOUT,
|
|
22
|
+
* the feature of WEIDA_MQTT_EUNAVAILABLE. */
|
|
23
|
+
this.condition = null;
|
|
24
|
+
/** The raw OS error of WEIDA_MQTT_EIO, when it carried one. */
|
|
25
|
+
this.osCode = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
native.setErrorClass(MqttError);
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
// Types of weida-mqtt (lib/index.js; docs/libraries/mqtt-js.md).
|
|
2
|
+
import type { KeyObject, X509Certificate } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
/** Bytes: a Uint8Array (a Buffer is one), another ArrayBuffer view or an
|
|
5
|
+
* ArrayBuffer. A string is refused. */
|
|
6
|
+
export type Bytes = Uint8Array | ArrayBufferView | ArrayBuffer;
|
|
7
|
+
|
|
8
|
+
/** An integer a C `uint64_t` holds: a number up to Number.MAX_SAFE_INTEGER,
|
|
9
|
+
* or a BigInt. */
|
|
10
|
+
export type Count = number | bigint;
|
|
11
|
+
|
|
12
|
+
/** A User Property list: `[name, value]` pairs, repeats and order kept. */
|
|
13
|
+
export type UserProperties = ReadonlyArray<readonly [string, string]>;
|
|
14
|
+
|
|
15
|
+
export interface Signal {
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** What failed, as the Python package names its exception class. */
|
|
20
|
+
export type MqttErrorKind =
|
|
21
|
+
| "AdministrativeAction"
|
|
22
|
+
| "BadAuthenticationMethod"
|
|
23
|
+
| "BadUserNameOrPassword"
|
|
24
|
+
| "Banned"
|
|
25
|
+
| "ClientIdentifierNotValid"
|
|
26
|
+
| "ConnectionRateExceeded"
|
|
27
|
+
| "DisconnectWithWillMessage"
|
|
28
|
+
| "ImplementationSpecificError"
|
|
29
|
+
| "KeepAliveTimeout"
|
|
30
|
+
| "MalformedPacket"
|
|
31
|
+
| "MaximumConnectTime"
|
|
32
|
+
| "MessageRateTooHigh"
|
|
33
|
+
| "NormalDisconnection"
|
|
34
|
+
| "NotAuthorized"
|
|
35
|
+
| "PacketTooLarge"
|
|
36
|
+
| "PayloadFormatInvalid"
|
|
37
|
+
| "ProtocolError"
|
|
38
|
+
| "QosNotSupported"
|
|
39
|
+
| "QuotaExceeded"
|
|
40
|
+
| "ReceiveMaximumExceeded"
|
|
41
|
+
| "RetainNotSupported"
|
|
42
|
+
| "ServerBusy"
|
|
43
|
+
| "ServerMoved"
|
|
44
|
+
| "ServerShuttingDown"
|
|
45
|
+
| "ServerUnavailable"
|
|
46
|
+
| "SessionTakenOver"
|
|
47
|
+
| "SharedSubscriptionsNotSupported"
|
|
48
|
+
| "SubscriptionIdentifiersNotSupported"
|
|
49
|
+
| "Success"
|
|
50
|
+
| "TopicAliasInvalid"
|
|
51
|
+
| "TopicFilterInvalid"
|
|
52
|
+
| "TopicNameInvalid"
|
|
53
|
+
| "UnspecifiedError"
|
|
54
|
+
| "UnsupportedProtocolVersion"
|
|
55
|
+
| "UseAnotherServer"
|
|
56
|
+
| "WildcardSubscriptionsNotSupported"
|
|
57
|
+
| "ConnectionClosed"
|
|
58
|
+
| "NotConnected"
|
|
59
|
+
| "Timeout"
|
|
60
|
+
| "Configuration"
|
|
61
|
+
| "InvalidTopic"
|
|
62
|
+
| "UnexpectedPacket"
|
|
63
|
+
| "AuthenticationMethodMismatch"
|
|
64
|
+
| "AuthenticationFailed"
|
|
65
|
+
| "SessionPresentWithoutState"
|
|
66
|
+
| "QuotaExhausted"
|
|
67
|
+
| "AcknowledgementLengthMismatch"
|
|
68
|
+
| "EncodeError"
|
|
69
|
+
| "Io"
|
|
70
|
+
| "Runtime"
|
|
71
|
+
| "InvalidArgument"
|
|
72
|
+
| "Closed"
|
|
73
|
+
| "Panic"
|
|
74
|
+
| "Interrupted"
|
|
75
|
+
| "MqttError";
|
|
76
|
+
|
|
77
|
+
/** Every failure of libweida_mqtt. */
|
|
78
|
+
export class MqttError extends Error {
|
|
79
|
+
readonly name: "MqttError";
|
|
80
|
+
/** The R9 constant name, e.g. "WEIDA_MQTT_ECONNECTION_REFUSED"; null for
|
|
81
|
+
* a code this package does not know. */
|
|
82
|
+
code: string | null;
|
|
83
|
+
/** The positive status code. */
|
|
84
|
+
errno: number;
|
|
85
|
+
/** What failed: the name of the Python package's exception class. */
|
|
86
|
+
kind: MqttErrorKind;
|
|
87
|
+
/** The MQTT reason code byte of `kind`, null where MQTT gives none. */
|
|
88
|
+
reasonCode: number | null;
|
|
89
|
+
/** The rule or wait that failed, when the library names one. */
|
|
90
|
+
condition: string | null;
|
|
91
|
+
/** The raw OS error of an Io failure, when it carried one. */
|
|
92
|
+
osCode: number | null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Every constant of weida_mqtt.h by name. */
|
|
96
|
+
export const CONSTANTS: Readonly<Record<string, number>>;
|
|
97
|
+
export const NORMAL_DISCONNECTION: number;
|
|
98
|
+
export const DISCONNECT_WITH_WILL_MESSAGE: number;
|
|
99
|
+
export const DEFAULT_RECEIVE_MAXIMUM: number;
|
|
100
|
+
export const DEFAULT_TOPIC_ALIAS_MAXIMUM: number;
|
|
101
|
+
export const MAX_TOPIC_BYTES: number;
|
|
102
|
+
export const MAX_PACKET_SIZE: number;
|
|
103
|
+
export const MAX_KEEP_ALIVE_MS: number;
|
|
104
|
+
export const MAX_INTERVAL_MS: number;
|
|
105
|
+
export const MAX_TIMEOUT_MS: number;
|
|
106
|
+
export const SHARE_PREFIX: "$share/";
|
|
107
|
+
|
|
108
|
+
export function version(): string;
|
|
109
|
+
export function libraryPath(): string;
|
|
110
|
+
export function strerror(code: number): string;
|
|
111
|
+
|
|
112
|
+
export function matches(filter: string, topic: string): boolean;
|
|
113
|
+
export function checkTopicFilter(filter: string): void;
|
|
114
|
+
export function checkTopicName(topic: string, options?: { allowEmpty?: boolean }): void;
|
|
115
|
+
export function splitShared(filter: string): Readonly<{ name: string; filter: string }> | null;
|
|
116
|
+
|
|
117
|
+
export type QoS = 0 | 1 | 2;
|
|
118
|
+
|
|
119
|
+
export interface MessageOptions {
|
|
120
|
+
qos?: QoS;
|
|
121
|
+
retain?: boolean;
|
|
122
|
+
contentType?: string | null;
|
|
123
|
+
responseTopic?: string | null;
|
|
124
|
+
correlationData?: Bytes | null;
|
|
125
|
+
messageExpiryMs?: number | null;
|
|
126
|
+
payloadFormatUtf8?: boolean;
|
|
127
|
+
userProperties?: Iterable<readonly [string, string]> | null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** One Application Message to publish, frozen. */
|
|
131
|
+
export class Message {
|
|
132
|
+
constructor(topic: string, payload?: Bytes | null, options?: MessageOptions);
|
|
133
|
+
static deleteRetained(topic: string, options?: { qos?: QoS }): Message;
|
|
134
|
+
readonly topic: string;
|
|
135
|
+
readonly payload: Uint8Array;
|
|
136
|
+
readonly qos: QoS;
|
|
137
|
+
readonly retain: boolean;
|
|
138
|
+
readonly contentType: string | null;
|
|
139
|
+
readonly responseTopic: string | null;
|
|
140
|
+
readonly correlationData: Uint8Array | null;
|
|
141
|
+
readonly messageExpiryMs: number | null;
|
|
142
|
+
readonly payloadFormatUtf8: boolean;
|
|
143
|
+
readonly userProperties: UserProperties;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface SubscriptionOptions {
|
|
147
|
+
qos?: QoS;
|
|
148
|
+
noLocal?: boolean;
|
|
149
|
+
retainAsPublished?: boolean;
|
|
150
|
+
retainHandling?: 0 | 1 | 2;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** One Topic Filter and its options, frozen. */
|
|
154
|
+
export class Subscription {
|
|
155
|
+
constructor(filter: string, options?: SubscriptionOptions);
|
|
156
|
+
readonly filter: string;
|
|
157
|
+
readonly qos: QoS;
|
|
158
|
+
readonly noLocal: boolean;
|
|
159
|
+
readonly retainAsPublished: boolean;
|
|
160
|
+
readonly retainHandling: 0 | 1 | 2;
|
|
161
|
+
readonly shared: boolean;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface WillOptions {
|
|
165
|
+
qos?: QoS;
|
|
166
|
+
retain?: boolean;
|
|
167
|
+
delayMs?: number | null;
|
|
168
|
+
messageExpiryMs?: number | null;
|
|
169
|
+
contentType?: string | null;
|
|
170
|
+
responseTopic?: string | null;
|
|
171
|
+
correlationData?: Bytes | null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The message the server publishes when a connection ends abnormally. */
|
|
175
|
+
export class Will {
|
|
176
|
+
constructor(topic: string, payload?: Bytes | null, options?: WillOptions);
|
|
177
|
+
readonly topic: string;
|
|
178
|
+
readonly payload: Uint8Array;
|
|
179
|
+
readonly qos: QoS;
|
|
180
|
+
readonly retain: boolean;
|
|
181
|
+
readonly delayMs: number | null;
|
|
182
|
+
readonly messageExpiryMs: number | null;
|
|
183
|
+
readonly contentType: string | null;
|
|
184
|
+
readonly responseTopic: string | null;
|
|
185
|
+
readonly correlationData: Uint8Array | null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** A trust anchor: PEM bytes, an X509Certificate, or the path of a PEM
|
|
189
|
+
* file (a string or a file: URL). */
|
|
190
|
+
export type Anchor = Uint8Array | string | URL | X509Certificate;
|
|
191
|
+
|
|
192
|
+
export interface TlsIdentity {
|
|
193
|
+
cert: Uint8Array | X509Certificate | X509Certificate[];
|
|
194
|
+
key: Uint8Array | KeyObject;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export interface TlsOptions {
|
|
198
|
+
ca?: Anchor | Anchor[] | null;
|
|
199
|
+
identity?: TlsIdentity | null;
|
|
200
|
+
serverName?: string | null;
|
|
201
|
+
verify?: boolean;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The client side of TLS for a connection (`mqtts`), frozen. */
|
|
205
|
+
export class Tls {
|
|
206
|
+
constructor(options?: TlsOptions);
|
|
207
|
+
readonly ca: readonly Anchor[];
|
|
208
|
+
readonly identity: Readonly<TlsIdentity> | null;
|
|
209
|
+
readonly serverName: string | null;
|
|
210
|
+
readonly verify: boolean;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface ConnectOptionsInit {
|
|
214
|
+
clientId?: string;
|
|
215
|
+
cleanStart?: boolean;
|
|
216
|
+
keepAliveMs?: number;
|
|
217
|
+
sessionExpiryMs?: number | null;
|
|
218
|
+
userName?: string | null;
|
|
219
|
+
password?: Bytes | null;
|
|
220
|
+
will?: Will | null;
|
|
221
|
+
userProperties?: Iterable<readonly [string, string]> | null;
|
|
222
|
+
receiveMaximum?: number | null;
|
|
223
|
+
maximumPacketSize?: number | null;
|
|
224
|
+
topicAliasMaximum?: number | null;
|
|
225
|
+
maxSubscriptionIds?: Count | null;
|
|
226
|
+
maxUserProperties?: Count | null;
|
|
227
|
+
incomingQueue?: Count | null;
|
|
228
|
+
incomingQueueBytes?: Count | null;
|
|
229
|
+
maxInflight?: Count | null;
|
|
230
|
+
maxInflightBytes?: Count | null;
|
|
231
|
+
maxAliasBytes?: Count | null;
|
|
232
|
+
maxAddresses?: Count | null;
|
|
233
|
+
connectTimeoutMs?: number | null;
|
|
234
|
+
pingTimeoutMs?: number | null;
|
|
235
|
+
writeTimeoutMs?: number | null;
|
|
236
|
+
requestResponseInformation?: boolean;
|
|
237
|
+
requestProblemInformation?: boolean;
|
|
238
|
+
authenticationMethod?: string | null;
|
|
239
|
+
authenticationData?: Bytes | null;
|
|
240
|
+
tls?: Tls | null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Everything a CONNECT declares, frozen and validated at construction. */
|
|
244
|
+
export class ConnectOptions {
|
|
245
|
+
constructor(options?: ConnectOptionsInit);
|
|
246
|
+
readonly clientId: string;
|
|
247
|
+
readonly cleanStart: boolean;
|
|
248
|
+
readonly keepAliveMs: number;
|
|
249
|
+
readonly sessionExpiryMs: number | null;
|
|
250
|
+
readonly userName: string | null;
|
|
251
|
+
readonly password: Uint8Array | null;
|
|
252
|
+
readonly will: Will | null;
|
|
253
|
+
readonly userProperties: UserProperties;
|
|
254
|
+
readonly receiveMaximum: number | null;
|
|
255
|
+
readonly maximumPacketSize: number | null;
|
|
256
|
+
readonly topicAliasMaximum: number | null;
|
|
257
|
+
readonly maxSubscriptionIds: Count | null;
|
|
258
|
+
readonly maxUserProperties: Count | null;
|
|
259
|
+
readonly incomingQueue: Count | null;
|
|
260
|
+
readonly incomingQueueBytes: Count | null;
|
|
261
|
+
readonly maxInflight: Count | null;
|
|
262
|
+
readonly maxInflightBytes: Count | null;
|
|
263
|
+
readonly maxAliasBytes: Count | null;
|
|
264
|
+
readonly maxAddresses: Count | null;
|
|
265
|
+
readonly connectTimeoutMs: number | null;
|
|
266
|
+
readonly pingTimeoutMs: number | null;
|
|
267
|
+
readonly writeTimeoutMs: number | null;
|
|
268
|
+
readonly requestResponseInformation: boolean;
|
|
269
|
+
readonly requestProblemInformation: boolean;
|
|
270
|
+
readonly authenticationMethod: string | null;
|
|
271
|
+
readonly authenticationData: Uint8Array | null;
|
|
272
|
+
readonly tls: Tls | null;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export type CompletionKind = "sent" | "acknowledged" | "complete" | "refused";
|
|
276
|
+
|
|
277
|
+
/** What a publish's hop certified, frozen. */
|
|
278
|
+
export class Completion {
|
|
279
|
+
constructor(kind: CompletionKind, reasonCode?: number | null);
|
|
280
|
+
readonly kind: CompletionKind;
|
|
281
|
+
readonly reasonCode: number | null;
|
|
282
|
+
readonly accepted: boolean;
|
|
283
|
+
readonly isError: boolean;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export interface DeliveryProperties {
|
|
287
|
+
readonly payloadFormatUtf8: boolean;
|
|
288
|
+
readonly messageExpiryMs: number | null;
|
|
289
|
+
readonly contentType: string | null;
|
|
290
|
+
readonly responseTopic: string | null;
|
|
291
|
+
readonly correlationData: Uint8Array | null;
|
|
292
|
+
readonly userProperties: UserProperties;
|
|
293
|
+
readonly subscriptionIds: readonly number[];
|
|
294
|
+
readonly truncated: boolean;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** One Application Message the server delivered, frozen. */
|
|
298
|
+
export class Delivery {
|
|
299
|
+
private constructor();
|
|
300
|
+
readonly topic: string;
|
|
301
|
+
readonly payload: Uint8Array;
|
|
302
|
+
readonly qos: QoS;
|
|
303
|
+
readonly retain: boolean;
|
|
304
|
+
readonly dup: boolean;
|
|
305
|
+
readonly packetId: number | null;
|
|
306
|
+
readonly subscriptionIds: readonly number[];
|
|
307
|
+
readonly properties: DeliveryProperties;
|
|
308
|
+
origin(retainAsPublished?: boolean): "retained" | "live" | "unknowable";
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** One entry of a subscriptions mirror. */
|
|
312
|
+
export interface SubscriptionRecord {
|
|
313
|
+
readonly filter: string;
|
|
314
|
+
readonly maximumQos: number;
|
|
315
|
+
readonly noLocal: boolean;
|
|
316
|
+
readonly retainAsPublished: boolean;
|
|
317
|
+
readonly retainHandling: number;
|
|
318
|
+
readonly subscriptionId: number | null;
|
|
319
|
+
readonly granted: number;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Everything the server declared in CONNACK. */
|
|
323
|
+
export interface ServerLimits {
|
|
324
|
+
readonly maximumQos: number;
|
|
325
|
+
readonly retainAvailable: boolean;
|
|
326
|
+
readonly receiveMaximum: number;
|
|
327
|
+
readonly maximumPacketSize: number | null;
|
|
328
|
+
readonly topicAliasMaximum: number;
|
|
329
|
+
readonly wildcardSubscriptionAvailable: boolean;
|
|
330
|
+
readonly subscriptionIdentifiersAvailable: boolean;
|
|
331
|
+
readonly sharedSubscriptionAvailable: boolean;
|
|
332
|
+
readonly serverKeepAliveMs: number | null;
|
|
333
|
+
readonly sessionExpiryIntervalMs: number | null;
|
|
334
|
+
readonly assignedClientIdentifier: string | null;
|
|
335
|
+
readonly responseInformation: string | null;
|
|
336
|
+
readonly serverReference: string | null;
|
|
337
|
+
readonly reasonString: string | null;
|
|
338
|
+
readonly sendPacketCeiling: number;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** The base of the classes that own a native handle. */
|
|
342
|
+
declare class Handle {
|
|
343
|
+
/** Releases the handle now, ending its pending waits with Closed. */
|
|
344
|
+
drop(): void;
|
|
345
|
+
[Symbol.dispose](): void;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** The client half of an MQTT session, held by the caller. */
|
|
349
|
+
export class Session extends Handle {
|
|
350
|
+
constructor(clientId: string, options?: ConnectOptions | null);
|
|
351
|
+
readonly clientId: string;
|
|
352
|
+
readonly inFlight: number;
|
|
353
|
+
readonly awaitingPubrel: number;
|
|
354
|
+
readonly isEmpty: boolean;
|
|
355
|
+
readonly receiveMaximum: number;
|
|
356
|
+
readonly sendQuota: number;
|
|
357
|
+
readonly subscriptions: readonly SubscriptionRecord[];
|
|
358
|
+
matching(topic: string): string[];
|
|
359
|
+
clear(): void;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** One AUTH round the server asked the client to answer. */
|
|
363
|
+
export class AuthRequest extends Handle {
|
|
364
|
+
private constructor();
|
|
365
|
+
readonly kind: "challenge" | "finish";
|
|
366
|
+
readonly data: Uint8Array | null;
|
|
367
|
+
respond(data?: Bytes): void;
|
|
368
|
+
refuse(): void;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Answers an AUTH round: bytes answer it (an empty array accepts a
|
|
372
|
+
* "finish"), anything else refuses, a throw refuses and becomes the
|
|
373
|
+
* failure's `cause`. */
|
|
374
|
+
export type Authenticator = (
|
|
375
|
+
request: AuthRequest,
|
|
376
|
+
) => Bytes | null | undefined | void | Promise<Bytes | null | undefined | void>;
|
|
377
|
+
|
|
378
|
+
export interface ConnectCall extends Signal {
|
|
379
|
+
authenticator?: Authenticator | null;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export interface Connected {
|
|
383
|
+
readonly client: Client;
|
|
384
|
+
readonly events: Events;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** A context owning a reactor. */
|
|
388
|
+
export class Context extends Handle {
|
|
389
|
+
constructor(options?: { workerThreads?: Count | null });
|
|
390
|
+
static owned(workerThreads?: Count | null): Context;
|
|
391
|
+
static current(): never;
|
|
392
|
+
static sharing(other: Context): Context;
|
|
393
|
+
connect(address: string, options?: ConnectOptions | null, call?: ConnectCall): Promise<Connected>;
|
|
394
|
+
connectSession(
|
|
395
|
+
address: string,
|
|
396
|
+
session: Session,
|
|
397
|
+
options?: ConnectOptions | null,
|
|
398
|
+
call?: ConnectCall,
|
|
399
|
+
): Promise<Connected>;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** The application's end of one connection. */
|
|
403
|
+
export class Client extends Handle {
|
|
404
|
+
private constructor();
|
|
405
|
+
publish(message: Message, call?: Signal): Promise<Completion>;
|
|
406
|
+
subscribe(
|
|
407
|
+
subscriptions: Iterable<Subscription>,
|
|
408
|
+
call?: Signal & { subscriptionId?: Count | null },
|
|
409
|
+
): Promise<number[]>;
|
|
410
|
+
unsubscribe(filters: Iterable<string>, call?: Signal): Promise<number[]>;
|
|
411
|
+
disconnect(call?: Signal & { reasonCode?: number; sessionExpiryMs?: number | null }): Promise<void>;
|
|
412
|
+
ping(call?: Signal): Promise<void>;
|
|
413
|
+
reauthenticate(call?: Signal): Promise<void>;
|
|
414
|
+
readonly clientId: string;
|
|
415
|
+
readonly sessionPresent: boolean;
|
|
416
|
+
readonly resumption: "resumed" | "fresh" | "discarded" | null;
|
|
417
|
+
readonly isEncrypted: boolean;
|
|
418
|
+
readonly keepAliveMs: number | null;
|
|
419
|
+
readonly responseInformation: string | null;
|
|
420
|
+
responseTopic(suffix?: string): string | null;
|
|
421
|
+
readonly server: ServerLimits;
|
|
422
|
+
readonly session: Session;
|
|
423
|
+
readonly subscriptions: readonly SubscriptionRecord[];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export type Event =
|
|
427
|
+
| { readonly kind: "delivered"; readonly delivery: Delivery }
|
|
428
|
+
| { readonly kind: "completed"; readonly packetId: number; readonly completion: Completion }
|
|
429
|
+
| { readonly kind: "abandoned"; readonly packetId: number; readonly cause: string };
|
|
430
|
+
|
|
431
|
+
/** The stream of what arrived on one connection. */
|
|
432
|
+
export class Events extends Handle {
|
|
433
|
+
private constructor();
|
|
434
|
+
recv(call?: Signal): Promise<Delivery | null>;
|
|
435
|
+
nextEvent(call?: Signal): Promise<Event | null>;
|
|
436
|
+
[Symbol.asyncIterator](): AsyncGenerator<Delivery, void, undefined>;
|
|
437
|
+
}
|