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,419 @@
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_broker-ffi.js";
17
+
18
+
19
+ /**
20
+ * One CONNECT awaiting an allow/deny decision.
21
+ */
22
+ export interface AuthRequest {
23
+ /**
24
+ * Pass this to `resolve_auth_request`.
25
+ */
26
+ "id": bigint | number;
27
+ "client_id": string;
28
+ "username": string | undefined;
29
+ "password": Uint8Array | undefined;
30
+ }
31
+
32
+ /**
33
+ * TLS configuration for [`MqttBrokerConfig::tls`]. All certificate/key
34
+ * material is PEM-encoded bytes (not file paths), matching
35
+ * `mqtt_client::TlsOptions` so this works identically across every
36
+ * language binding without assuming a filesystem layout.
37
+ */
38
+ export interface BrokerTlsConfig {
39
+ /**
40
+ * Port to accept TLS connections on.
41
+ */
42
+ "port": number;
43
+ /**
44
+ * PEM-encoded server certificate chain (leaf certificate first, then
45
+ * any intermediates).
46
+ */
47
+ "cert_pem": Uint8Array;
48
+ /**
49
+ * PEM-encoded server private key.
50
+ */
51
+ "key_pem": Uint8Array;
52
+ /**
53
+ * PEM-encoded CA certificate(s) to verify *client* certificates
54
+ * against, for mutual TLS (mTLS). If unset (the default), client
55
+ * certificates aren't required or checked — just a normal one-way
56
+ * TLS listener.
57
+ */
58
+ "client_ca_pem": Uint8Array | undefined;
59
+ }
60
+
61
+ /**
62
+ * What the broker should do after one authentication round — a record
63
+ * (outcome + payload) rather than an enum-with-data, because several
64
+ * binding generators mishandle callbacks that return data-carrying enums.
65
+ */
66
+ export interface EnhancedAuthStep {
67
+ "outcome": EnhancedAuthOutcome;
68
+ /**
69
+ * Challenge (for `Continue`) or final data (for `Success`); empty = none.
70
+ */
71
+ "data": Uint8Array;
72
+ }
73
+
74
+ /**
75
+ * Configuration for an [`crate::broker::MqttBroker`].
76
+ */
77
+ export interface MqttBrokerConfig {
78
+ /**
79
+ * Address to bind the listening socket to, e.g. `"0.0.0.0"` or `"::"`.
80
+ */
81
+ "bind_address": string;
82
+ "port": number;
83
+ /**
84
+ * Port to accept MQTT-over-WebSocket connections on (MQTT-5.0 §6 /
85
+ * MQTT-3.1.1 Appendix B), bound alongside the raw-TCP `port` above.
86
+ * `None` disables the WebSocket listener entirely — the default,
87
+ * since most deployments only need raw TCP. `Some(0)` binds an
88
+ * OS-assigned ephemeral WebSocket port (mirroring `port`'s own `0`
89
+ * convention), which is why this is `Option<u16>` rather than reusing
90
+ * `0` itself as the "disabled" sentinel.
91
+ */
92
+ "ws_port": number | undefined;
93
+ /**
94
+ * With no [`MqttAuthProvider`] registered, whether *everyone* may connect
95
+ * (`true`) or *no one* may (`false`) — there is nothing to verify a
96
+ * username against, so supplying one grants nothing. Ignored if an auth
97
+ * provider is set — the provider always makes the final decision.
98
+ */
99
+ "allow_anonymous": boolean;
100
+ /**
101
+ * Maximum simultaneously connected clients. `0` means unlimited.
102
+ */
103
+ "max_clients": number;
104
+ /**
105
+ * Highest QoS the broker will grant on SUBSCRIBE (messages published
106
+ * at a higher QoS are still accepted, but delivered at this ceiling).
107
+ */
108
+ "max_qos": QoS;
109
+ /**
110
+ * Maximum number of retained messages kept in memory. `0` means
111
+ * unlimited.
112
+ */
113
+ "max_retained_messages": number;
114
+ /**
115
+ * Maximum number of messages queued per offline session (for clients
116
+ * that connected with `clean_start = false`). `0` means unlimited.
117
+ */
118
+ "max_queued_per_client": number;
119
+ /**
120
+ * How often the broker resends (with DUP=1) an outgoing QoS 1/2
121
+ * packet that hasn't been acked yet. `0` selects the built-in default
122
+ * (5 seconds).
123
+ */
124
+ "redelivery_interval_secs": number;
125
+ /**
126
+ * If set, also accept TLS (`mqtts`) connections on
127
+ * [`BrokerTlsConfig::port`], alongside the plain-TCP `port` (and
128
+ * optional WebSocket `ws_port`) above. `None` (the default) means no
129
+ * TLS listener at all, unchanged from before TLS support existed.
130
+ */
131
+ "tls": BrokerTlsConfig | undefined;
132
+ /**
133
+ * Largest packet (fixed header + body, in bytes) accepted from any
134
+ * client. A client announcing a bigger one is disconnected as soon as
135
+ * the packet *header* is read, before the body is buffered. `0`
136
+ * selects the built-in default
137
+ * ([`DEFAULT_MAX_PACKET_SIZE`], 1 MiB).
138
+ */
139
+ "max_packet_size": number;
140
+ /**
141
+ * Maximum encoded packets queued for one client's socket. A client
142
+ * that reads slower than messages arrive would otherwise make the
143
+ * broker buffer without bound; once this many are waiting, further
144
+ * deliveries to *that* client are dropped (QoS 1/2 ones are retried
145
+ * by the redelivery sweep). `0` selects the built-in default
146
+ * ([`DEFAULT_MAX_OUTBOUND_QUEUE`], 4096).
147
+ */
148
+ "max_outbound_queue": number;
149
+ /**
150
+ * Seconds a persistent (`clean_start = false`) session may stay offline
151
+ * before the broker discards it, with its subscriptions and queued
152
+ * messages. `0` (the default) keeps sessions forever, as before.
153
+ */
154
+ "session_expiry_secs": number;
155
+ }
156
+
157
+ /**
158
+ * The verdict of one round of an MQTT 5.0 enhanced authentication exchange.
159
+ */
160
+ export declare const EnhancedAuthOutcome: Readonly<{
161
+ /**
162
+ * Send [`EnhancedAuthStep::data`] to the client as a challenge and wait
163
+ * for its response.
164
+ */
165
+ "Continue": "Continue";
166
+ /**
167
+ * Authentication succeeded; [`EnhancedAuthStep::data`] (if non-empty) is
168
+ * returned to the client in the CONNACK (e.g. a SCRAM server-final message).
169
+ */
170
+ "Success": "Success";
171
+ /**
172
+ * Refuse the connection.
173
+ */
174
+ "Failure": "Failure";
175
+ }>;
176
+ /**
177
+ * The verdict of one round of an MQTT 5.0 enhanced authentication exchange.
178
+ */
179
+ export type EnhancedAuthOutcome = (typeof EnhancedAuthOutcome)[keyof typeof EnhancedAuthOutcome];
180
+
181
+ /**
182
+ * MQTT Quality of Service level, as used in the broker's own API
183
+ * ([`MqttBrokerConfig::max_qos`], [`MqttBrokerEventListener`], [`crate::BrokerEvent`]).
184
+ *
185
+ * This is deliberately the **broker's own** enum rather than a re-export of
186
+ * `mqtt_client::QoS`. A type defined in another crate is an *external type*
187
+ * to UniFFI, and several binding generators (Node.js) can't handle those;
188
+ * owning it makes `mqtt_broker`'s bindings fully self-contained in every
189
+ * language — one package, no dependency on the client component's generated
190
+ * code. The cost is a second `QoS` next to the client's (identical, with
191
+ * lossless `From` conversions in Rust).
192
+ */
193
+ export declare const QoS: Readonly<{
194
+ /**
195
+ * QoS 0 — fire and forget.
196
+ */
197
+ "AtMostOnce": "AtMostOnce";
198
+ /**
199
+ * QoS 1 — acknowledged delivery, may be duplicated.
200
+ */
201
+ "AtLeastOnce": "AtLeastOnce";
202
+ /**
203
+ * QoS 2 — exactly-once delivery.
204
+ */
205
+ "ExactlyOnce": "ExactlyOnce";
206
+ }>;
207
+ /**
208
+ * MQTT Quality of Service level, as used in the broker's own API
209
+ * ([`MqttBrokerConfig::max_qos`], [`MqttBrokerEventListener`], [`crate::BrokerEvent`]).
210
+ *
211
+ * This is deliberately the **broker's own** enum rather than a re-export of
212
+ * `mqtt_client::QoS`. A type defined in another crate is an *external type*
213
+ * to UniFFI, and several binding generators (Node.js) can't handle those;
214
+ * owning it makes `mqtt_broker`'s bindings fully self-contained in every
215
+ * language — one package, no dependency on the client component's generated
216
+ * code. The cost is a second `QoS` next to the client's (identical, with
217
+ * lossless `From` conversions in Rust).
218
+ */
219
+ export type QoS = (typeof QoS)[keyof typeof QoS];
220
+
221
+ export interface BrokerEventClientConnected {
222
+ tag: "ClientConnected";
223
+ "client_id": string;
224
+ }
225
+
226
+ export interface BrokerEventClientDisconnected {
227
+ tag: "ClientDisconnected";
228
+ "client_id": string;
229
+ "reason": string;
230
+ }
231
+
232
+ export interface BrokerEventMessagePublished {
233
+ tag: "MessagePublished";
234
+ "client_id": string;
235
+ "topic": string;
236
+ "qos": QoS;
237
+ }
238
+
239
+ /**
240
+ * A broker event, as returned by the pull-style `MqttBroker::next_event`
241
+ * (the polling twin of [`MqttBrokerEventListener`](crate::config::MqttBrokerEventListener)).
242
+ */
243
+ export type BrokerEvent = BrokerEventClientConnected | BrokerEventClientDisconnected | BrokerEventMessagePublished;
244
+ /**
245
+ * A broker event, as returned by the pull-style `MqttBroker::next_event`
246
+ * (the polling twin of [`MqttBrokerEventListener`](crate::config::MqttBrokerEventListener)).
247
+ */
248
+ export declare const BrokerEvent: Readonly<{
249
+ ClientConnected(client_id: string): BrokerEventClientConnected;
250
+ ClientDisconnected(client_id: string, reason: string): BrokerEventClientDisconnected;
251
+ MessagePublished(client_id: string, topic: string, qos: QoS): BrokerEventMessagePublished;
252
+ }>;
253
+
254
+ /**
255
+ * Errors produced while starting or stopping an [`crate::MqttBroker`].
256
+ */
257
+ export declare class MqttBrokerError extends globalThis.Error {
258
+ readonly tag: string;
259
+ protected constructor(tag: string, message?: string);
260
+ }
261
+
262
+ /**
263
+ * `start()` was called while the broker was already running.
264
+ */
265
+ export declare class MqttBrokerErrorAlreadyRunning extends MqttBrokerError {
266
+ readonly tag: "AlreadyRunning";
267
+ constructor(message?: string);
268
+ }
269
+
270
+ /**
271
+ * A listening socket could not be bound or queried.
272
+ */
273
+ export declare class MqttBrokerErrorIo extends MqttBrokerError {
274
+ readonly tag: "Io";
275
+ constructor(message?: string);
276
+ }
277
+
278
+ /**
279
+ * TLS certificate, key, or verifier configuration was invalid.
280
+ */
281
+ export declare class MqttBrokerErrorTls extends MqttBrokerError {
282
+ readonly tag: "Tls";
283
+ constructor(message?: string);
284
+ }
285
+
286
+ /**
287
+ * Pluggable authentication, evaluated once per CONNECT.
288
+ */
289
+ export interface MqttAuthProvider {
290
+ /**
291
+ * Return `true` to accept the connection.
292
+ */
293
+ authenticate(client_id: string, username: string | undefined, password: Uint8Array | undefined): boolean;
294
+ }
295
+
296
+ /**
297
+ * Broker-side view of a delivered/observed event, for monitoring and
298
+ * logging from foreign code.
299
+ */
300
+ export interface MqttBrokerEventListener {
301
+ on_client_connected(client_id: string): void;
302
+ on_client_disconnected(client_id: string, reason: string): void;
303
+ on_message_published(client_id: string, topic: string, qos: QoS): void;
304
+ }
305
+
306
+ /**
307
+ * Pluggable MQTT 5.0 *enhanced* authentication (SCRAM, Kerberos, OAuth, …):
308
+ * a multi-round challenge/response evaluated during CONNECT, before the
309
+ * session is attached. A CONNECT naming an `Authentication Method` is refused
310
+ * unless a provider is registered.
311
+ */
312
+ export interface MqttEnhancedAuthProvider {
313
+ /**
314
+ * One round. `round` is `0` for the CONNECT's own `Authentication Data`
315
+ * (`data`, possibly `None`) and `1, 2, …` for each client response.
316
+ */
317
+ step(client_id: string, method: string, data: Uint8Array | undefined, round: number): EnhancedAuthStep;
318
+ }
319
+
320
+ /**
321
+ * A running (or not-yet-started) MQTT broker instance, speaking both MQTT
322
+ * 3.1.1 and MQTT 5.0 — the protocol version is negotiated independently
323
+ * per connection from each client's CONNECT packet.
324
+ *
325
+ * Dropping a broker stops it (listeners are closed and live connections
326
+ * told to shut down); there is no way to reach a broker that has no
327
+ * remaining handle, so leaving it running would just leak its ports.
328
+ */
329
+ export declare class MqttBroker extends UniffiObjectBase {
330
+ constructor(config: MqttBrokerConfig);
331
+ /**
332
+ * The TCP port actually bound (useful when `config.port == 0` was used
333
+ * to request an ephemeral port, e.g. in tests).
334
+ */
335
+ bound_port(): number | undefined;
336
+ /**
337
+ * The TLS port actually bound, if `config.tls` is set.
338
+ */
339
+ bound_tls_port(): number | undefined;
340
+ /**
341
+ * The WebSocket port actually bound, if `config.ws_port` is set.
342
+ */
343
+ bound_ws_port(): number | undefined;
344
+ /**
345
+ * Number of currently connected clients.
346
+ */
347
+ client_count(): number;
348
+ /**
349
+ * Pull-style authentication for runtimes that can't implement
350
+ * [`MqttAuthProvider`] (Dart, Haskell). Once enabled — and with no
351
+ * provider set — every CONNECT is queued as an [`AuthRequest`](crate::AuthRequest)
352
+ * for [`next_auth_request`](Self::next_auth_request); answer it with
353
+ * [`resolve_auth_request`](Self::resolve_auth_request). A request left
354
+ * unanswered for 10 seconds, or dropped because more than `capacity` are
355
+ * waiting, is **denied**. `0` disables the queue (and denies anything
356
+ * waiting); the broker then falls back to `allow_anonymous`.
357
+ */
358
+ enable_auth_queue(capacity: number): void;
359
+ /**
360
+ * Pull-style alternative to [`set_event_listener`](Self::set_event_listener)
361
+ * for runtimes that can't receive callbacks from Rust threads (Dart,
362
+ * Haskell): keep up to `capacity` events for [`next_event`](Self::next_event).
363
+ * `0` turns the queue off; when it fills up the oldest event is dropped.
364
+ */
365
+ enable_event_queue(capacity: number): void;
366
+ is_running(): boolean;
367
+ /**
368
+ * The next CONNECT awaiting a decision, waiting up to `timeout_ms`
369
+ * milliseconds; `None` if none arrived. Requires
370
+ * [`enable_auth_queue`](Self::enable_auth_queue).
371
+ */
372
+ next_auth_request(timeout_ms: number): Promise<AuthRequest | undefined>;
373
+ /**
374
+ * The next queued [`BrokerEvent`], waiting up to `timeout_ms`
375
+ * milliseconds; `None` if none arrived. Requires
376
+ * [`enable_event_queue`](Self::enable_event_queue).
377
+ */
378
+ next_event(timeout_ms: number): Promise<BrokerEvent | undefined>;
379
+ /**
380
+ * Admit (`allow = true`) or refuse the CONNECT behind request `id`.
381
+ * Returns `false` if the request is unknown or already expired.
382
+ */
383
+ resolve_auth_request(id: bigint | number, allow: boolean): boolean;
384
+ /**
385
+ * Register a pluggable authentication provider. Must be called before
386
+ * [`start`](Self::start) to affect connections made right away, but
387
+ * may be replaced at any time.
388
+ */
389
+ set_auth_provider(provider: MqttAuthProvider): void;
390
+ /**
391
+ * Register the MQTT 5.0 enhanced-authentication provider (multi-round
392
+ * challenge/response during CONNECT). Without one, a CONNECT that names
393
+ * an `Authentication Method` is refused with "Bad authentication method".
394
+ */
395
+ set_enhanced_auth_provider(provider: MqttEnhancedAuthProvider): void;
396
+ /**
397
+ * Register a listener for connect/disconnect/publish events.
398
+ */
399
+ set_event_listener(listener: MqttBrokerEventListener): void;
400
+ /**
401
+ * Bind the listening socket(s) and start accepting connections. Also
402
+ * binds a second, MQTT-over-WebSocket listener on `config.ws_port` and
403
+ * a TLS listener on `config.tls` when those are configured.
404
+ *
405
+ * All-or-nothing: every socket is bound (and the TLS configuration
406
+ * validated) *before* anything starts accepting, so a failure — say
407
+ * the WebSocket port is taken — leaves the broker cleanly stopped
408
+ * instead of half-running with some listeners already live.
409
+ */
410
+ start(): Promise<void>;
411
+ /**
412
+ * Stop the broker: close the listening sockets and ask every
413
+ * connected client's connection to shut down. Sessions that persist
414
+ * across disconnects (`clean_start = false`) are kept in memory, so a
415
+ * later [`start`](Self::start) resumes them. Calling `stop()` on a
416
+ * broker that isn't running is a no-op.
417
+ */
418
+ stop(): Promise<void>;
419
+ }