@slatedb/uniffi 0.12.0

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,77 @@
1
+ import type { RustBufferStruct, RustCallStatusStruct } from "./ffi-types.js";
2
+
3
+ export declare const CALL_SUCCESS: 0;
4
+ export declare const CALL_ERROR: 1;
5
+ export declare const CALL_UNEXPECTED_ERROR: 2;
6
+ export declare const CALL_CANCELLED: 3;
7
+
8
+ export type RustStringLifter = (bytes: Uint8Array) => string;
9
+ export type RustErrorHandler<E extends Error = Error> = (
10
+ bytes: Uint8Array,
11
+ status: RustCallStatusStruct,
12
+ ) => E;
13
+ export type RustCallStatusReader<Status = RustCallStatusStruct> = (
14
+ status: Status,
15
+ ) => RustCallStatusStruct | null | undefined;
16
+ export type RustCallStatusWriter<Status = RustCallStatusStruct> = (
17
+ status: Status,
18
+ value: RustCallStatusStruct,
19
+ ) => Status;
20
+
21
+ export interface RustCallOptions<E extends Error = Error> {
22
+ errorHandler?: RustErrorHandler<E>;
23
+ freeRustBuffer?: ((buffer: RustBufferStruct) => void) | null;
24
+ liftString?: RustStringLifter;
25
+ }
26
+
27
+ export declare function createRustCallStatus(
28
+ code?: number,
29
+ error_buf?: RustBufferStruct | null,
30
+ ): RustCallStatusStruct;
31
+ export declare function createRustErrorStatus(
32
+ code?: number,
33
+ error_buf?: RustBufferStruct | null,
34
+ ): RustCallStatusStruct;
35
+ export declare function checkRustCallStatus<E extends Error = Error>(
36
+ status: RustCallStatusStruct | null | undefined,
37
+ options?: RustCallOptions<E>,
38
+ ): RustCallStatusStruct;
39
+ export declare function rustCall<T, E extends Error = Error>(
40
+ caller: (status: RustCallStatusStruct) => T,
41
+ options?: RustCallOptions<E>,
42
+ ): T;
43
+ export declare function rustCallWithError<T, E extends Error = Error>(
44
+ errorHandler: RustErrorHandler<E>,
45
+ caller: (status: RustCallStatusStruct) => T,
46
+ options?: Omit<RustCallOptions<E>, "errorHandler">,
47
+ ): T;
48
+
49
+ export declare class UniffiRustCaller<
50
+ Status = RustCallStatusStruct,
51
+ > {
52
+ constructor(options?: {
53
+ createStatus?: () => Status;
54
+ disposeStatus?: ((status: Status) => void) | null;
55
+ freeRustBuffer?: ((buffer: RustBufferStruct) => void) | null;
56
+ liftString?: RustStringLifter;
57
+ readStatus?: RustCallStatusReader<Status>;
58
+ writeStatus?: RustCallStatusWriter<Status>;
59
+ });
60
+ createCallStatus(): Status;
61
+ createErrorStatus(code?: number, error_buf?: RustBufferStruct | null): Status;
62
+ makeRustCall<T, E extends Error = Error>(
63
+ caller: (status: Status) => T,
64
+ options?: RustCallOptions<E>,
65
+ ): T;
66
+ rustCall<T, E extends Error = Error>(
67
+ caller: (status: Status) => T,
68
+ options?: RustCallOptions<E>,
69
+ ): T;
70
+ rustCallWithError<T, E extends Error = Error>(
71
+ errorHandler: RustErrorHandler<E>,
72
+ caller: (status: Status) => T,
73
+ options?: Omit<RustCallOptions<E>, "errorHandler">,
74
+ ): T;
75
+ }
76
+
77
+ export declare const defaultRustCaller: UniffiRustCaller;
@@ -0,0 +1,233 @@
1
+ import {
2
+ AbortError,
3
+ RustPanic,
4
+ UnexpectedRustCallError,
5
+ UnexpectedRustCallStatusCode,
6
+ } from "./errors.js";
7
+ import { FfiConverterString } from "./ffi-converters.js";
8
+ import {
9
+ EMPTY_RUST_BUFFER,
10
+ EMPTY_RUST_CALL_STATUS,
11
+ RustBufferValue,
12
+ RustCallStatusCodes,
13
+ normalizeRustBuffer,
14
+ normalizeRustCallStatus,
15
+ } from "./ffi-types.js";
16
+
17
+ export const CALL_SUCCESS = RustCallStatusCodes.SUCCESS;
18
+ export const CALL_ERROR = RustCallStatusCodes.ERROR;
19
+ export const CALL_UNEXPECTED_ERROR = RustCallStatusCodes.UNEXPECTED_ERROR;
20
+ export const CALL_CANCELLED = RustCallStatusCodes.CANCELLED;
21
+
22
+ function defaultLiftString(bytes) {
23
+ return FfiConverterString.lift(bytes);
24
+ }
25
+
26
+ function defaultReadStatus(status) {
27
+ return status;
28
+ }
29
+
30
+ function defaultWriteStatus(status, value) {
31
+ if (status == null || typeof status !== "object") {
32
+ return value;
33
+ }
34
+
35
+ status.code = value.code;
36
+ status.error_buf = createMutableRustBuffer(value.error_buf);
37
+ return status;
38
+ }
39
+
40
+ function createMutableRustBuffer(buffer = EMPTY_RUST_BUFFER) {
41
+ return normalizeRustBuffer(buffer) ?? {
42
+ capacity: 0n,
43
+ len: 0n,
44
+ data: null,
45
+ };
46
+ }
47
+
48
+ function isEmptyRustBuffer(buffer) {
49
+ return (
50
+ buffer == null
51
+ || (
52
+ buffer.data == null
53
+ && buffer.len === 0n
54
+ && buffer.capacity === 0n
55
+ )
56
+ );
57
+ }
58
+
59
+ function consumeRustBuffer(buffer, freeRustBuffer) {
60
+ const normalized = createMutableRustBuffer(buffer);
61
+ if (normalized.len === 0n) {
62
+ return new Uint8Array();
63
+ }
64
+
65
+ const rustBuffer = new RustBufferValue(normalized);
66
+ if (typeof freeRustBuffer === "function") {
67
+ return rustBuffer.consumeIntoUint8Array(freeRustBuffer);
68
+ }
69
+ return rustBuffer.toUint8Array();
70
+ }
71
+
72
+ export function createRustCallStatus(
73
+ code = CALL_SUCCESS,
74
+ error_buf = EMPTY_RUST_BUFFER,
75
+ ) {
76
+ return {
77
+ code,
78
+ error_buf: createMutableRustBuffer(error_buf),
79
+ };
80
+ }
81
+
82
+ export function createRustErrorStatus(
83
+ code = CALL_ERROR,
84
+ error_buf = EMPTY_RUST_BUFFER,
85
+ ) {
86
+ return createRustCallStatus(code, error_buf);
87
+ }
88
+
89
+ function checkRustCallStatusImpl(
90
+ status,
91
+ options,
92
+ freeRustBuffer,
93
+ liftString,
94
+ errorHandlerOverride = undefined,
95
+ ) {
96
+ if (status?.code === CALL_SUCCESS && isEmptyRustBuffer(status.error_buf)) {
97
+ return status;
98
+ }
99
+
100
+ const normalized = normalizeRustCallStatus(status) ?? createRustCallStatus();
101
+ const errorHandler = errorHandlerOverride ?? options?.errorHandler;
102
+ const resolvedFreeRustBuffer = options?.freeRustBuffer ?? freeRustBuffer;
103
+ const resolvedLiftString = options?.liftString ?? liftString;
104
+
105
+ switch (normalized.code) {
106
+ case CALL_SUCCESS:
107
+ return normalized;
108
+
109
+ case CALL_ERROR: {
110
+ const errorBytes = consumeRustBuffer(normalized.error_buf, resolvedFreeRustBuffer);
111
+ if (typeof errorHandler === "function") {
112
+ throw errorHandler(errorBytes, normalized);
113
+ }
114
+ throw new UnexpectedRustCallError();
115
+ }
116
+
117
+ case CALL_UNEXPECTED_ERROR: {
118
+ const errorBytes = consumeRustBuffer(normalized.error_buf, resolvedFreeRustBuffer);
119
+ const message =
120
+ errorBytes.byteLength === 0
121
+ ? "Rust panic"
122
+ : resolvedLiftString(errorBytes);
123
+ throw new RustPanic(message, { captureStack: false });
124
+ }
125
+
126
+ case CALL_CANCELLED:
127
+ throw new AbortError();
128
+
129
+ default:
130
+ throw new UnexpectedRustCallStatusCode(normalized.code);
131
+ }
132
+ }
133
+
134
+ export function checkRustCallStatus(status, options = undefined) {
135
+ return checkRustCallStatusImpl(
136
+ status,
137
+ options,
138
+ undefined,
139
+ defaultLiftString,
140
+ );
141
+ }
142
+
143
+ export function rustCall(caller, options = undefined) {
144
+ const status = createRustCallStatus();
145
+ const result = caller(status);
146
+ checkRustCallStatusImpl(status, options, undefined, defaultLiftString);
147
+ return result;
148
+ }
149
+
150
+ export function rustCallWithError(errorHandler, caller, options = undefined) {
151
+ const status = createRustCallStatus();
152
+ const result = caller(status);
153
+ checkRustCallStatusImpl(
154
+ status,
155
+ options,
156
+ undefined,
157
+ defaultLiftString,
158
+ errorHandler,
159
+ );
160
+ return result;
161
+ }
162
+
163
+ export class UniffiRustCaller {
164
+ constructor({
165
+ createStatus = () => createRustCallStatus(),
166
+ disposeStatus = undefined,
167
+ freeRustBuffer = undefined,
168
+ liftString = defaultLiftString,
169
+ readStatus = defaultReadStatus,
170
+ writeStatus = defaultWriteStatus,
171
+ } = {}) {
172
+ this._createStatus = createStatus;
173
+ this._disposeStatus = disposeStatus;
174
+ this._freeRustBuffer = freeRustBuffer;
175
+ this._liftString = liftString;
176
+ this._readStatus = readStatus;
177
+ this._writeStatus = writeStatus;
178
+ }
179
+
180
+ createCallStatus() {
181
+ return this._writeStatus(this._createStatus(), EMPTY_RUST_CALL_STATUS);
182
+ }
183
+
184
+ createErrorStatus(code = CALL_ERROR, error_buf = EMPTY_RUST_BUFFER) {
185
+ return this._writeStatus(
186
+ this._createStatus(),
187
+ createRustCallStatus(code, error_buf),
188
+ );
189
+ }
190
+
191
+ makeRustCall(caller, options = undefined) {
192
+ const status = this.createCallStatus();
193
+ try {
194
+ const result = caller(status);
195
+ checkRustCallStatusImpl(
196
+ this._readStatus(status),
197
+ options,
198
+ this._freeRustBuffer,
199
+ this._liftString,
200
+ );
201
+ return result;
202
+ } finally {
203
+ if (typeof this._disposeStatus === "function") {
204
+ this._disposeStatus(status);
205
+ }
206
+ }
207
+ }
208
+
209
+ rustCall(caller, options = undefined) {
210
+ return this.makeRustCall(caller, options);
211
+ }
212
+
213
+ rustCallWithError(errorHandler, caller, options = undefined) {
214
+ const status = this.createCallStatus();
215
+ try {
216
+ const result = caller(status);
217
+ checkRustCallStatusImpl(
218
+ this._readStatus(status),
219
+ options,
220
+ this._freeRustBuffer,
221
+ this._liftString,
222
+ errorHandler,
223
+ );
224
+ return result;
225
+ } finally {
226
+ if (typeof this._disposeStatus === "function") {
227
+ this._disposeStatus(status);
228
+ }
229
+ }
230
+ }
231
+ }
232
+
233
+ export const defaultRustCaller = new UniffiRustCaller();