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,12 @@
1
+ {
2
+ "name": "mqtt_client",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "engines": {
6
+ "node": ">=16"
7
+ },
8
+ "dependencies": {
9
+ "koffi": "^3.0.2"
10
+ },
11
+ "exports": "./index.js"
12
+ }
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "stem-mqtt",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.3.5",
4
+ "description": "MQTT 3.1.1 / 5.0 client and broker (Rust core, UniFFI Node bindings)",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=16"
8
+ },
9
+ "dependencies": {
10
+ "koffi": "^3.0.2"
11
+ },
12
+ "exports": {
13
+ "./client": "./client/index.js",
14
+ "./broker": "./broker/index.js"
15
+ },
16
+ "files": [
17
+ "client",
18
+ "broker",
19
+ "runtime",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "license": "MIT",
24
+ "author": "Sorin Irimies",
25
+ "keywords": [
26
+ "mqtt",
27
+ "mqtt5",
28
+ "broker",
29
+ "client",
30
+ "iot",
31
+ "rust",
32
+ "uniffi"
33
+ ],
34
+ "homepage": "https://github.com/sorinirimies/stem-mqtt#readme",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/sorinirimies/stem-mqtt.git"
38
+ },
39
+ "bugs": {
40
+ "url": "https://github.com/sorinirimies/stem-mqtt/issues"
41
+ },
42
+ "publishConfig": {
43
+ "registry": "https://registry.npmjs.org/",
44
+ "access": "public"
45
+ },
46
+ "os": [
47
+ "linux"
48
+ ],
49
+ "cpu": [
50
+ "x64"
51
+ ]
6
52
  }
@@ -0,0 +1,86 @@
1
+ import type { RustCallStatusStruct } from "./ffi-types.js";
2
+ import type { RustCallOptions, UniffiRustCaller } from "./rust-call.js";
3
+
4
+ export declare const RUST_FUTURE_POLL_READY: 0;
5
+ export declare const RUST_FUTURE_POLL_WAKE: 1;
6
+
7
+ export interface AsyncCallState {
8
+ resolverHandle: bigint | null;
9
+ }
10
+
11
+ export type RustFutureContinuationCallback = (
12
+ handle: bigint | number,
13
+ pollResult: bigint | number,
14
+ ) => boolean;
15
+
16
+ export interface PollRustFutureOptions {
17
+ continuationCallback?: RustFutureContinuationCallback;
18
+ state?: AsyncCallState;
19
+ }
20
+
21
+ export interface CompleteRustFutureOptions<
22
+ Status = RustCallStatusStruct,
23
+ E extends Error = Error,
24
+ > extends RustCallOptions<E> {
25
+ rustCaller?: UniffiRustCaller<Status>;
26
+ }
27
+
28
+ export interface RustCallAsyncOptions<
29
+ Lowered,
30
+ Result = Lowered,
31
+ Status = RustCallStatusStruct,
32
+ E extends Error = Error,
33
+ > extends CompleteRustFutureOptions<Status, E> {
34
+ cancelFunc?: ((rustFuture: bigint) => void) | null;
35
+ completeFunc: (rustFuture: bigint, status: Status) => Lowered;
36
+ continuationCallback?: RustFutureContinuationCallback;
37
+ freeFunc?: ((rustFuture: bigint) => void) | null;
38
+ liftFunc?: (value: Lowered) => Result;
39
+ pollFunc: (
40
+ rustFuture: bigint,
41
+ continuationCallback: RustFutureContinuationCallback,
42
+ continuationHandle: bigint,
43
+ ) => void;
44
+ rustFutureFunc: () => bigint;
45
+ signal?: AbortSignal | null;
46
+ }
47
+
48
+ export declare function decodeRustFuturePoll(value: bigint | number): number;
49
+ export declare function createAsyncCallState(): AsyncCallState;
50
+ export declare function cleanupAsyncCallState(state: AsyncCallState): void;
51
+ export declare const rustFutureContinuationCallback: RustFutureContinuationCallback;
52
+ export declare function pollRustFuture(
53
+ rustFuture: bigint,
54
+ pollFunc: (
55
+ rustFuture: bigint,
56
+ continuationCallback: RustFutureContinuationCallback,
57
+ continuationHandle: bigint,
58
+ ) => void,
59
+ options?: PollRustFutureOptions,
60
+ ): Promise<number>;
61
+ export declare function completeRustFuture<
62
+ Lowered,
63
+ Status = RustCallStatusStruct,
64
+ E extends Error = Error,
65
+ >(
66
+ rustFuture: bigint,
67
+ completeFunc: (rustFuture: bigint, status: Status) => Lowered,
68
+ options?: CompleteRustFutureOptions<Status, E>,
69
+ ): Lowered;
70
+ export declare function cancelRustFuture(
71
+ rustFuture: bigint,
72
+ cancelFunc?: ((rustFuture: bigint) => void) | null,
73
+ ): void;
74
+ export declare function freeRustFuture(
75
+ rustFuture: bigint,
76
+ freeFunc?: ((rustFuture: bigint) => void) | null,
77
+ ): void;
78
+ export declare function rustCallAsync<
79
+ Lowered,
80
+ Result = Lowered,
81
+ Status = RustCallStatusStruct,
82
+ E extends Error = Error,
83
+ >(
84
+ options: RustCallAsyncOptions<Lowered, Result, Status, E>,
85
+ ): Promise<Result>;
86
+ export declare function rustFutureHandleCount(): number;
@@ -0,0 +1,217 @@
1
+ import { AbortError, ConverterRangeError } from "./errors.js";
2
+ import { UniffiHandleMap } from "./handle-map.js";
3
+ import { defaultRustCaller } from "./rust-call.js";
4
+
5
+ export const RUST_FUTURE_POLL_READY = 0;
6
+ export const RUST_FUTURE_POLL_WAKE = 1;
7
+ const RUST_FUTURE_KEEPALIVE_DELAY_MS = 0x7fffffff;
8
+
9
+ const MIN_I8 = -0x80;
10
+ const MAX_I8 = 0x7f;
11
+ const MAX_U8 = 0xff;
12
+ const RUST_FUTURE_RESOLVER_MAP = new UniffiHandleMap();
13
+ const RUST_FUTURE_KEEPALIVE_MAP = new Map();
14
+
15
+ function identity(value) {
16
+ return value;
17
+ }
18
+
19
+ export function decodeRustFuturePoll(value) {
20
+ const numericValue =
21
+ typeof value === "bigint"
22
+ ? Number(value)
23
+ : value;
24
+ if (!Number.isInteger(numericValue) || numericValue < MIN_I8 || numericValue > MAX_U8) {
25
+ throw new ConverterRangeError(
26
+ `RustFuturePoll must be an integer between ${MIN_I8} and ${MAX_U8}.`,
27
+ );
28
+ }
29
+ return numericValue > MAX_I8
30
+ ? numericValue - 0x100
31
+ : numericValue;
32
+ }
33
+
34
+ export function createAsyncCallState() {
35
+ return {
36
+ resolverHandle: null,
37
+ };
38
+ }
39
+
40
+ function clearRustFutureKeepAlive(handle) {
41
+ const keepAlive = RUST_FUTURE_KEEPALIVE_MAP.get(handle);
42
+ if (keepAlive === undefined) {
43
+ return;
44
+ }
45
+
46
+ RUST_FUTURE_KEEPALIVE_MAP.delete(handle);
47
+ clearTimeout(keepAlive);
48
+ }
49
+
50
+ export function cleanupAsyncCallState(state) {
51
+ if (!state || state.resolverHandle == null) {
52
+ return;
53
+ }
54
+
55
+ clearRustFutureKeepAlive(state.resolverHandle);
56
+ RUST_FUTURE_RESOLVER_MAP.remove(state.resolverHandle);
57
+ state.resolverHandle = null;
58
+ }
59
+
60
+ export const rustFutureContinuationCallback = (handle, pollResult) => {
61
+ clearRustFutureKeepAlive(handle);
62
+ const resolve = RUST_FUTURE_RESOLVER_MAP.remove(handle);
63
+ if (resolve === undefined) {
64
+ return false;
65
+ }
66
+
67
+ queueMicrotask(() => {
68
+ resolve(decodeRustFuturePoll(pollResult));
69
+ });
70
+ return true;
71
+ };
72
+
73
+ export function pollRustFuture(
74
+ rustFuture,
75
+ pollFunc,
76
+ {
77
+ continuationCallback = rustFutureContinuationCallback,
78
+ state = createAsyncCallState(),
79
+ } = {},
80
+ ) {
81
+ cleanupAsyncCallState(state);
82
+
83
+ return new Promise((resolve, reject) => {
84
+ // Node 22 can tear down the event loop while this promise is still waiting
85
+ // on a native callback. Keep a ref'ed timer alive until the callback
86
+ // settles so Rust future completions arrive before environment cleanup.
87
+ const keepAlive = setTimeout(() => {}, RUST_FUTURE_KEEPALIVE_DELAY_MS);
88
+ let resolverHandle = null;
89
+ const settle = (callback, value) => {
90
+ clearRustFutureKeepAlive(resolverHandle);
91
+ callback(value);
92
+ };
93
+ resolverHandle = RUST_FUTURE_RESOLVER_MAP.insert((pollCode) => {
94
+ state.resolverHandle = null;
95
+ settle(resolve, pollCode);
96
+ });
97
+ RUST_FUTURE_KEEPALIVE_MAP.set(resolverHandle, keepAlive);
98
+ state.resolverHandle = resolverHandle;
99
+
100
+ try {
101
+ pollFunc(rustFuture, continuationCallback, resolverHandle);
102
+ } catch (error) {
103
+ cleanupAsyncCallState(state);
104
+ settle(reject, error);
105
+ }
106
+ });
107
+ }
108
+
109
+ export function completeRustFuture(
110
+ rustFuture,
111
+ completeFunc,
112
+ {
113
+ errorHandler,
114
+ freeRustBuffer,
115
+ liftString,
116
+ rustCaller = defaultRustCaller,
117
+ } = {},
118
+ ) {
119
+ return rustCaller.makeRustCall(
120
+ (status) => completeFunc(rustFuture, status),
121
+ {
122
+ errorHandler,
123
+ freeRustBuffer,
124
+ liftString,
125
+ },
126
+ );
127
+ }
128
+
129
+ export function cancelRustFuture(rustFuture, cancelFunc = undefined) {
130
+ if (typeof cancelFunc === "function") {
131
+ cancelFunc(rustFuture);
132
+ }
133
+ }
134
+
135
+ export function freeRustFuture(rustFuture, freeFunc = undefined) {
136
+ if (typeof freeFunc === "function") {
137
+ freeFunc(rustFuture);
138
+ }
139
+ }
140
+
141
+ export async function rustCallAsync({
142
+ cancelFunc,
143
+ completeFunc,
144
+ continuationCallback = rustFutureContinuationCallback,
145
+ errorHandler,
146
+ freeFunc,
147
+ freeRustBuffer,
148
+ liftFunc = identity,
149
+ liftString,
150
+ pollFunc,
151
+ rustCaller = defaultRustCaller,
152
+ rustFutureFunc,
153
+ signal = undefined,
154
+ }) {
155
+ if (signal?.aborted) {
156
+ throw new AbortError();
157
+ }
158
+
159
+ const rustFuture = rustFutureFunc();
160
+ const state = createAsyncCallState();
161
+ const abortListener =
162
+ signal == null
163
+ ? null
164
+ : () => {
165
+ try {
166
+ cancelRustFuture(rustFuture, cancelFunc);
167
+ } catch {
168
+ // Preserve the original async failure path.
169
+ }
170
+ };
171
+
172
+ if (signal && abortListener) {
173
+ if (signal.aborted) {
174
+ abortListener();
175
+ } else {
176
+ signal.addEventListener("abort", abortListener, { once: true });
177
+ }
178
+ }
179
+
180
+ try {
181
+ while (true) {
182
+ const pollCode = await pollRustFuture(rustFuture, pollFunc, {
183
+ continuationCallback,
184
+ state,
185
+ });
186
+ if (pollCode === RUST_FUTURE_POLL_READY) {
187
+ break;
188
+ }
189
+ if (pollCode !== RUST_FUTURE_POLL_WAKE) {
190
+ throw new ConverterRangeError(
191
+ `Unexpected RustFuturePoll value ${String(pollCode)}.`,
192
+ );
193
+ }
194
+ }
195
+
196
+ const completed = completeRustFuture(rustFuture, completeFunc, {
197
+ errorHandler,
198
+ freeRustBuffer,
199
+ liftString,
200
+ rustCaller,
201
+ });
202
+ return liftFunc(completed);
203
+ } finally {
204
+ if (signal && abortListener) {
205
+ signal.removeEventListener("abort", abortListener);
206
+ }
207
+ try {
208
+ freeRustFuture(rustFuture, freeFunc);
209
+ } finally {
210
+ cleanupAsyncCallState(state);
211
+ }
212
+ }
213
+ }
214
+
215
+ export function rustFutureHandleCount() {
216
+ return RUST_FUTURE_RESOLVER_MAP.size;
217
+ }
@@ -0,0 +1,122 @@
1
+ import type { HandleMapOptions } from "./handle-map.js";
2
+ import type { RustBufferStruct, RustCallStatusStruct } from "./ffi-types.js";
3
+
4
+ export interface CallbackRegistryOptions<T extends object = Record<string, unknown>>
5
+ extends HandleMapOptions
6
+ {
7
+ interfaceName?: string;
8
+ validate?: ((value: T) => void) | null;
9
+ }
10
+
11
+ export interface CallbackErrorOptions<E extends Error = Error> {
12
+ lowerError?: ((error: E) => RustBufferStruct | null | undefined) | null;
13
+ lowerString?: ((message: string) => RustBufferStruct) | null;
14
+ defaultMessage?: string;
15
+ }
16
+
17
+ export interface PendingForeignFuture<Result = unknown> {
18
+ callbackData: bigint | number | undefined;
19
+ cancelled: boolean;
20
+ complete: ((
21
+ callbackData: bigint | number | undefined,
22
+ result: Result,
23
+ ) => void) | undefined;
24
+ handle: bigint;
25
+ settled: boolean;
26
+ }
27
+
28
+ export interface PendingForeignFutureOptions<Result = unknown> {
29
+ callbackData?: bigint | number;
30
+ complete?: ((
31
+ callbackData: bigint | number | undefined,
32
+ result: Result,
33
+ ) => void) | null;
34
+ }
35
+
36
+ export interface AsyncCallbackCompletion<Lowered = unknown> {
37
+ call_status: RustCallStatusStruct;
38
+ return_value?: Lowered;
39
+ }
40
+
41
+ export interface InvokeCallbackMethodOptions<T = unknown, E extends Error = Error>
42
+ extends CallbackErrorOptions<E>
43
+ {
44
+ args?: readonly unknown[];
45
+ defaultReturnValue?: T;
46
+ interfaceName?: string;
47
+ status?: RustCallStatusStruct | null;
48
+ }
49
+
50
+ export interface InvokeAsyncCallbackMethodOptions<
51
+ Result = unknown,
52
+ Lowered = Result,
53
+ E extends Error = Error,
54
+ > extends CallbackErrorOptions<E>
55
+ {
56
+ args?: readonly unknown[];
57
+ callbackData?: bigint | number;
58
+ complete?: ((
59
+ callbackData: bigint | number | undefined,
60
+ result: AsyncCallbackCompletion<Lowered>,
61
+ ) => void) | null;
62
+ defaultReturnValue?: Lowered;
63
+ interfaceName?: string;
64
+ lowerReturn?: ((value: Result) => Lowered) | null;
65
+ }
66
+
67
+ export declare function resetCallbackCallStatus(
68
+ status?: RustCallStatusStruct | null,
69
+ ): RustCallStatusStruct;
70
+ export declare function writeCallbackError<E extends Error = Error>(
71
+ status: RustCallStatusStruct | null | undefined,
72
+ error: unknown,
73
+ options?: CallbackErrorOptions<E>,
74
+ ): E;
75
+ export declare function createPendingForeignFuture<Result = unknown>(
76
+ options?: PendingForeignFutureOptions<Result>,
77
+ ): PendingForeignFuture<Result>;
78
+ export declare function takePendingForeignFuture<Result = unknown>(
79
+ handle: bigint | number,
80
+ ): PendingForeignFuture<Result> | undefined;
81
+ export declare function freePendingForeignFuture<Result = unknown>(
82
+ handle: bigint | number,
83
+ ): PendingForeignFuture<Result> | undefined;
84
+ export declare function clearPendingForeignFutures(): void;
85
+ export declare function foreignFutureHandleCount(): number;
86
+ export declare function invokeCallbackMethod<T = unknown, E extends Error = Error>(params: {
87
+ registry: Pick<UniffiCallbackRegistry, "get"> & { interfaceName?: string };
88
+ handle: bigint | number;
89
+ methodName: string;
90
+ } & InvokeCallbackMethodOptions<T, E>): T;
91
+ export declare function invokeAsyncCallbackMethod<
92
+ Result = unknown,
93
+ Lowered = Result,
94
+ E extends Error = Error,
95
+ >(params: {
96
+ registry: Pick<UniffiCallbackRegistry, "get"> & { interfaceName?: string };
97
+ handle: bigint | number;
98
+ methodName: string;
99
+ } & InvokeAsyncCallbackMethodOptions<Result, Lowered, E>): bigint;
100
+
101
+ export declare class UniffiCallbackRegistry<T extends object = Record<string, unknown>> {
102
+ readonly interfaceName: string;
103
+ constructor(options?: CallbackRegistryOptions<T>);
104
+ register(value: T): bigint;
105
+ cloneHandle(handle: bigint | number): bigint;
106
+ get(handle: bigint | number): T;
107
+ remove(handle: bigint | number | null | undefined): T | undefined;
108
+ take(handle: bigint | number): T;
109
+ has(handle: bigint | number | null | undefined): boolean;
110
+ clear(): void;
111
+ invoke<R>(handle: bigint | number, methodName: string, args?: readonly unknown[]): R;
112
+ invokeWithRustCallStatus<R, E extends Error = Error>(
113
+ handle: bigint | number,
114
+ methodName: string,
115
+ options?: InvokeCallbackMethodOptions<R, E>,
116
+ ): R;
117
+ readonly size: number;
118
+ }
119
+
120
+ export declare function createCallbackRegistry<T extends object = Record<string, unknown>>(
121
+ options?: CallbackRegistryOptions<T>,
122
+ ): UniffiCallbackRegistry<T>;