stem-mqtt 0.0.0-stage → 0.3.5

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.
@@ -0,0 +1,432 @@
1
+ import { UniffiObjectBase } from "../runtime/objects.js";
2
+
3
+
4
+
5
+ export interface ComponentMetadata {
6
+ namespace: string;
7
+ packageName: string;
8
+ cdylibName: string;
9
+ nodeEngine: string;
10
+ bundledPrebuilds: boolean;
11
+ manualLoad: boolean;
12
+ }
13
+
14
+ export declare const componentMetadata: Readonly<ComponentMetadata>;
15
+
16
+ export { ffiMetadata } from "./mqtt_client-ffi.js";
17
+
18
+
19
+ /**
20
+ * Everything needed to establish an MQTT connection.
21
+ */
22
+ export interface ConnectOptions {
23
+ "host": string;
24
+ "port": number;
25
+ "client_id": string;
26
+ /**
27
+ * Which protocol version to speak. The broker never up/downgrades a
28
+ * connection once CONNECT is sent.
29
+ */
30
+ "version": MqttVersion;
31
+ /**
32
+ * MQTT 3.1.1 "Clean Session" / MQTT 5.0 "Clean Start".
33
+ */
34
+ "clean_start": boolean;
35
+ /**
36
+ * Keep-alive interval in seconds. `0` disables keep-alive pings.
37
+ */
38
+ "keep_alive_secs": number;
39
+ "username": string | undefined;
40
+ "password": Uint8Array | undefined;
41
+ "will": WillOptions | undefined;
42
+ /**
43
+ * Seconds to wait for the TCP connection + CONNACK before failing.
44
+ */
45
+ "connect_timeout_secs": number;
46
+ /**
47
+ * Seconds to wait for a QoS 1/2 handshake step *per attempt* before
48
+ * retrying (with DUP=1) — `MqttClient::publish` retries an unacked
49
+ * QoS 1/2 PUBLISH (or QoS 2's PUBREL) up to 3 times before giving up,
50
+ * so the worst-case total wait for a single `publish()` call is up to
51
+ * 4x this value. `0` selects the built-in default (15s).
52
+ */
53
+ "operation_timeout_secs": number;
54
+ /**
55
+ * If true, [`crate::client::MqttClient`] automatically reconnects
56
+ * (with exponential backoff, see [`Self::reconnect_backoff_secs`] /
57
+ * [`Self::reconnect_max_backoff_secs`]) after a connection loss that
58
+ * wasn't caused by calling `disconnect()`, replaying every topic this
59
+ * client is currently subscribed to (per its own bookkeeping) once
60
+ * reconnected. Off by default — the caller must opt in.
61
+ */
62
+ "auto_reconnect": boolean;
63
+ /**
64
+ * Initial delay before the first reconnect attempt, doubling after
65
+ * each failed attempt up to [`Self::reconnect_max_backoff_secs`].
66
+ * `0` selects the built-in default (1s). Ignored unless
67
+ * [`Self::auto_reconnect`] is set.
68
+ */
69
+ "reconnect_backoff_secs": number;
70
+ /**
71
+ * Cap on the exponential reconnect backoff delay. `0` selects the
72
+ * built-in default (30s). Ignored unless [`Self::auto_reconnect`] is
73
+ * set.
74
+ */
75
+ "reconnect_max_backoff_secs": number;
76
+ /**
77
+ * If set, connect over TLS (`mqtts`) instead of plain TCP. `None`
78
+ * (the default) is plain TCP, unchanged from before TLS support
79
+ * existed.
80
+ */
81
+ "tls": TlsOptions | undefined;
82
+ /**
83
+ * Largest packet (fixed header + body, in bytes) this client will
84
+ * accept from the broker; a larger one is treated as a protocol error
85
+ * and drops the connection instead of being buffered. `0` selects the
86
+ * built-in default (the MQTT protocol maximum, ~256 MiB).
87
+ */
88
+ "max_packet_size": number;
89
+ /**
90
+ * MQTT 5.0 enhanced authentication (SCRAM, Kerberos, OAuth, …): the
91
+ * `Authentication Method` to announce in CONNECT. The broker may then
92
+ * send challenges, which are answered by the
93
+ * [`MqttAuthHandler`] registered with `MqttClient::set_auth_handler`.
94
+ * Requires [`MqttVersion::V5`].
95
+ */
96
+ "auth_method": string | undefined;
97
+ /**
98
+ * Initial `Authentication Data` sent with CONNECT (e.g. a SCRAM
99
+ * client-first message). Requires [`Self::auth_method`].
100
+ */
101
+ "auth_data": Uint8Array | undefined;
102
+ }
103
+
104
+ /**
105
+ * Outcome of a successful [`crate::client::MqttClient::connect`].
106
+ */
107
+ export interface ConnectResult {
108
+ "session_present": boolean;
109
+ /**
110
+ * `0x00` on success. MQTT 5.0 reason code (MQTT 3.1.1 codes are mapped
111
+ * onto the equivalent 5.0 code by
112
+ * [`ConnectReasonCode`](crate::protocol::connect::ConnectReasonCode)).
113
+ */
114
+ "reason_code": number;
115
+ }
116
+
117
+ /**
118
+ * A message delivered to a subscriber.
119
+ */
120
+ export interface MqttMessage {
121
+ "topic": string;
122
+ "payload": Uint8Array;
123
+ "qos": QoS;
124
+ "retain": boolean;
125
+ }
126
+
127
+ /**
128
+ * Result of a subscribe request: the reason/return code the broker
129
+ * granted for the requested filter (`< 0x80` is success; MQTT 3.1.1 codes
130
+ * 0/1/2 map onto the granted QoS).
131
+ */
132
+ export interface SubscribeResult {
133
+ "reason_code": number;
134
+ }
135
+
136
+ /**
137
+ * TLS configuration for [`ConnectOptions::tls`]. All certificate/key
138
+ * material is PEM-encoded bytes (not file paths) so this works
139
+ * identically across every language binding without assuming a
140
+ * filesystem layout.
141
+ */
142
+ export interface TlsOptions {
143
+ /**
144
+ * Additional PEM-encoded root CA certificate(s) to trust, on top of
145
+ * the bundled Mozilla root store (`webpki-roots`). Needed to connect
146
+ * to a broker with a self-signed or private-CA certificate.
147
+ */
148
+ "ca_cert_pem": Uint8Array | undefined;
149
+ /**
150
+ * PEM-encoded client certificate, for mutual TLS (mTLS). Requires
151
+ * [`Self::client_key_pem`] too.
152
+ */
153
+ "client_cert_pem": Uint8Array | undefined;
154
+ /**
155
+ * PEM-encoded client private key, for mutual TLS. Requires
156
+ * [`Self::client_cert_pem`] too.
157
+ */
158
+ "client_key_pem": Uint8Array | undefined;
159
+ /**
160
+ * Skip server certificate verification entirely. **Dangerous** — only
161
+ * for local development against a broker with a self-signed
162
+ * certificate you can't otherwise easily trust (e.g. one generated
163
+ * on the fly for a demo). Never use this in production: it makes the
164
+ * connection trivially interceptable.
165
+ */
166
+ "insecure_skip_certificate_verification": boolean;
167
+ }
168
+
169
+ /**
170
+ * Last-Will-and-Testament configuration for a [`ConnectOptions`].
171
+ */
172
+ export interface WillOptions {
173
+ "topic": string;
174
+ "payload": Uint8Array;
175
+ "qos": QoS;
176
+ "retain": boolean;
177
+ }
178
+
179
+ /**
180
+ * The MQTT protocol version negotiated for a connection.
181
+ */
182
+ export declare const MqttVersion: Readonly<{
183
+ /**
184
+ * MQTT 3.1.1 (protocol level 4).
185
+ */
186
+ "V311": "V311";
187
+ /**
188
+ * MQTT 5.0 (protocol level 5).
189
+ */
190
+ "V5": "V5";
191
+ }>;
192
+ /**
193
+ * The MQTT protocol version negotiated for a connection.
194
+ */
195
+ export type MqttVersion = (typeof MqttVersion)[keyof typeof MqttVersion];
196
+
197
+ /**
198
+ * MQTT Quality of Service level.
199
+ */
200
+ export declare const QoS: Readonly<{
201
+ /**
202
+ * QoS 0 — fire and forget, no acknowledgement.
203
+ */
204
+ "AtMostOnce": "AtMostOnce";
205
+ /**
206
+ * QoS 1 — acknowledged delivery, may be duplicated.
207
+ */
208
+ "AtLeastOnce": "AtLeastOnce";
209
+ /**
210
+ * QoS 2 — exactly-once delivery via a four-part handshake.
211
+ */
212
+ "ExactlyOnce": "ExactlyOnce";
213
+ }>;
214
+ /**
215
+ * MQTT Quality of Service level.
216
+ */
217
+ export type QoS = (typeof QoS)[keyof typeof QoS];
218
+
219
+ /**
220
+ * All errors that can be produced while encoding/decoding MQTT packets or
221
+ * operating an [`crate::client::MqttClient`].
222
+ *
223
+ * Exposed to foreign languages through UniFFI as a plain error enum.
224
+ */
225
+ export declare class MqttError extends globalThis.Error {
226
+ readonly tag: string;
227
+ protected constructor(tag: string, message?: string);
228
+ }
229
+
230
+ /**
231
+ * The peer sent a packet that violates the MQTT specification.
232
+ */
233
+ export declare class MqttErrorMalformedPacket extends MqttError {
234
+ readonly tag: "MalformedPacket";
235
+ constructor(message?: string);
236
+ }
237
+
238
+ /**
239
+ * A value could not be encoded/decoded per the wire protocol.
240
+ */
241
+ export declare class MqttErrorProtocol extends MqttError {
242
+ readonly tag: "Protocol";
243
+ constructor(message?: string);
244
+ }
245
+
246
+ /**
247
+ * Underlying I/O failure (connection reset, DNS failure, etc.).
248
+ */
249
+ export declare class MqttErrorIo extends MqttError {
250
+ readonly tag: "Io";
251
+ constructor(message?: string);
252
+ }
253
+
254
+ /**
255
+ * The broker rejected the connection attempt.
256
+ */
257
+ export declare class MqttErrorConnectionRefused extends MqttError {
258
+ readonly tag: "ConnectionRefused";
259
+ constructor(message?: string);
260
+ }
261
+
262
+ /**
263
+ * An operation was attempted while not connected to a broker.
264
+ */
265
+ export declare class MqttErrorNotConnected extends MqttError {
266
+ readonly tag: "NotConnected";
267
+ constructor(message?: string);
268
+ }
269
+
270
+ /**
271
+ * A packet's declared size exceeds the configured maximum packet size.
272
+ */
273
+ export declare class MqttErrorPacketTooLarge extends MqttError {
274
+ readonly tag: "PacketTooLarge";
275
+ constructor(message?: string);
276
+ }
277
+
278
+ /**
279
+ * The client is already connected.
280
+ */
281
+ export declare class MqttErrorAlreadyConnected extends MqttError {
282
+ readonly tag: "AlreadyConnected";
283
+ constructor(message?: string);
284
+ }
285
+
286
+ /**
287
+ * The peer did not acknowledge a QoS 1/2 packet before the timeout.
288
+ */
289
+ export declare class MqttErrorTimeout extends MqttError {
290
+ readonly tag: "Timeout";
291
+ constructor(message?: string);
292
+ }
293
+
294
+ /**
295
+ * Generic keep-alive / session error.
296
+ */
297
+ export declare class MqttErrorSession extends MqttError {
298
+ readonly tag: "Session";
299
+ constructor(message?: string);
300
+ }
301
+
302
+ /**
303
+ * Answers the broker's authentication challenges during an MQTT 5.0
304
+ * enhanced-authentication handshake (see [`ConnectOptions::auth_method`]).
305
+ */
306
+ export interface MqttAuthHandler {
307
+ /**
308
+ * The broker sent `challenge` for `method`; return the response, or
309
+ * `None` to abort the connection attempt.
310
+ */
311
+ respond(method: string, challenge: Uint8Array): Uint8Array | undefined;
312
+ }
313
+
314
+ /**
315
+ * Callback interface implemented by foreign code to receive events from an
316
+ * [`crate::client::MqttClient`] without polling.
317
+ */
318
+ export interface MqttMessageListener {
319
+ /**
320
+ * Called for every PUBLISH delivered to a subscription this client holds.
321
+ */
322
+ on_message(message: MqttMessage): void;
323
+ /**
324
+ * Called when the connection to the broker is lost or closed, whether
325
+ * by the client, the broker, or the network.
326
+ */
327
+ on_disconnected(reason: string): void;
328
+ }
329
+
330
+ /**
331
+ * A connected (or not-yet-connected) MQTT client instance.
332
+ *
333
+ * One `MqttClient` corresponds to one broker connection / MQTT session.
334
+ * Create it with [`MqttClient::new`], call [`MqttClient::connect`], then
335
+ * use [`MqttClient::publish`] / [`MqttClient::subscribe`] /
336
+ * [`MqttClient::unsubscribe`]. Call [`MqttClient::disconnect`] (or simply
337
+ * drop it) to close the connection.
338
+ */
339
+ export declare class MqttClient extends UniffiObjectBase {
340
+ /**
341
+ * Create a new client from `options`. Does not connect yet.
342
+ */
343
+ constructor(options: ConnectOptions);
344
+ /**
345
+ * Open the TCP connection and complete the CONNECT/CONNACK handshake.
346
+ * Safe to call again after a connection loss (including one this
347
+ * client is still in the middle of auto-reconnecting from).
348
+ *
349
+ * If [`ConnectOptions::auto_reconnect`] is set, a background
350
+ * supervisor is started (once) that automatically re-runs this same
351
+ * handshake — with exponential backoff — after any *unexpected*
352
+ * connection loss (i.e. not caused by [`Self::disconnect`]), then
353
+ * replays every topic this client is currently subscribed to.
354
+ */
355
+ connect(): Promise<ConnectResult>;
356
+ /**
357
+ * Gracefully disconnect: sends a DISCONNECT packet, stops background
358
+ * tasks, and closes the socket. Safe to call multiple times. Also
359
+ * stops the [`ConnectOptions::auto_reconnect`] supervisor, if one is
360
+ * running — this is a deliberate disconnect, not a connection loss to
361
+ * recover from.
362
+ */
363
+ disconnect(): Promise<void>;
364
+ /**
365
+ * Pull-style alternative to [`set_message_listener`](Self::set_message_listener)
366
+ * for runtimes that can't receive callbacks from Rust threads (Dart,
367
+ * Haskell): keep up to `capacity` incoming messages for
368
+ * [`next_message`](Self::next_message). `0` turns the queue off. When it
369
+ * fills up the oldest message is dropped. May be combined with a listener.
370
+ */
371
+ enable_message_queue(capacity: number): void;
372
+ /**
373
+ * `true` once [`connect`](Self::connect) has succeeded and the
374
+ * connection has not since been lost or closed.
375
+ */
376
+ is_connected(): boolean;
377
+ /**
378
+ * Why the most recent connection was lost (`None` if it never was) —
379
+ * the pull-style twin of `MqttMessageListener::on_disconnected`.
380
+ */
381
+ last_disconnect_reason(): string | undefined;
382
+ /**
383
+ * The next queued incoming message, waiting up to `timeout_ms`
384
+ * milliseconds for one; `None` if none arrived. Requires
385
+ * [`enable_message_queue`](Self::enable_message_queue).
386
+ */
387
+ next_message(timeout_ms: number): Promise<MqttMessage | undefined>;
388
+ /**
389
+ * Publish `payload` to `topic`. Resolves once the broker has
390
+ * acknowledged the message (QoS 1: PUBACK; QoS 2: full four-part
391
+ * handshake). Resolves immediately for QoS 0.
392
+ *
393
+ * For QoS 1/2, an unacked step is retried (with DUP=1) up to
394
+ * `MAX_PUBLISH_RETRIES` (3) times before giving up with
395
+ * [`MqttError::Timeout`] — the broker never resends on our behalf, so
396
+ * enforcing "at least once"/"exactly once" delivery is the
397
+ * publisher's job.
398
+ *
399
+ * A topic containing wildcards (`+`, `#`) or that is empty is
400
+ * rejected locally with [`MqttError::Protocol`] (a broker would drop
401
+ * the whole connection for it). An MQTT 5.0 broker that answers with a
402
+ * failure reason code (e.g. "not authorized") surfaces as
403
+ * [`MqttError::Session`] instead of being reported as success.
404
+ */
405
+ publish(topic: string, payload: Uint8Array, qos: QoS, retain: boolean): Promise<void>;
406
+ /**
407
+ * Register the handler that answers the broker's authentication
408
+ * challenges when [`ConnectOptions::auth_method`] is set (MQTT 5.0
409
+ * enhanced authentication). Must be set before `connect()`.
410
+ */
411
+ set_auth_handler(handler: MqttAuthHandler): void;
412
+ /**
413
+ * Register (or replace) the listener that receives incoming messages
414
+ * and disconnect notifications. Safe to call before or after connect.
415
+ */
416
+ set_message_listener(listener: MqttMessageListener): void;
417
+ /**
418
+ * Subscribe to `topic_filter` requesting at most `qos`. Returns the
419
+ * reason/return code the broker granted.
420
+ *
421
+ * Recorded in this client's own subscription bookkeeping so
422
+ * [`ConnectOptions::auto_reconnect`] can replay it after a reconnect
423
+ * — the broker has no way to tell a resumed client what it was
424
+ * subscribed to beyond what a persistent (`clean_start = false`)
425
+ * session already covers.
426
+ */
427
+ subscribe(topic_filter: string, qos: QoS): Promise<SubscribeResult>;
428
+ /**
429
+ * Unsubscribe from `topic_filter`.
430
+ */
431
+ unsubscribe(topic_filter: string): Promise<void>;
432
+ }