@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,274 @@
1
+ function assignErrorMetadata(error, options = {}) {
2
+ if (options.cause !== undefined) {
3
+ error.cause = options.cause;
4
+ }
5
+ if (options.code !== undefined) {
6
+ error.code = options.code;
7
+ }
8
+ if (options.details !== undefined) {
9
+ error.details = options.details;
10
+ }
11
+ return error;
12
+ }
13
+
14
+ function stagedBinaryGuidance(details = {}) {
15
+ const packageRelativePath =
16
+ details != null && typeof details === "object"
17
+ ? details.packageRelativePath
18
+ : undefined;
19
+ if (packageRelativePath == null) {
20
+ return "Copy the intended native library into the generated package or call load(path).";
21
+ }
22
+ return `Copy the intended native library into the generated package at ${JSON.stringify(packageRelativePath)} or call load(path).`;
23
+ }
24
+
25
+ export class UniffiError extends Error {
26
+ constructor(message = "UniFFI error", options = {}) {
27
+ const captureStack = options.captureStack !== false;
28
+ if (!captureStack && typeof Error.stackTraceLimit === "number") {
29
+ const previousStackTraceLimit = Error.stackTraceLimit;
30
+ Error.stackTraceLimit = 0;
31
+ try {
32
+ super(message);
33
+ } finally {
34
+ Error.stackTraceLimit = previousStackTraceLimit;
35
+ }
36
+ } else {
37
+ super(message);
38
+ }
39
+ this.name = new.target.name;
40
+ assignErrorMetadata(this, options);
41
+ }
42
+ }
43
+
44
+ export class UniffiGeneratedError extends UniffiError {}
45
+
46
+ export class UniffiRuntimeError extends UniffiError {}
47
+
48
+ export class RustPanic extends UniffiRuntimeError {
49
+ constructor(message = "Rust panic", options = {}) {
50
+ super(message, options);
51
+ }
52
+ }
53
+
54
+ export class AbortError extends UniffiRuntimeError {
55
+ constructor(message = "The Rust call was cancelled.", options = {}) {
56
+ super(message, options);
57
+ }
58
+ }
59
+
60
+ export class LibraryNotLoadedError extends UniffiRuntimeError {
61
+ constructor(message = "The native library is not loaded.", options = {}) {
62
+ super(message, options);
63
+ }
64
+ }
65
+
66
+ export class ContractVersionMismatchError extends UniffiRuntimeError {
67
+ constructor(expected, actual, options = {}) {
68
+ const details =
69
+ options.details != null && typeof options.details === "object"
70
+ ? options.details
71
+ : {};
72
+ const libraryPath = details.libraryPath;
73
+ const symbolName = details.symbolName;
74
+ super(
75
+ `UniFFI contract version mismatch${
76
+ symbolName == null ? "" : ` for ${String(symbolName)}`
77
+ }${
78
+ libraryPath == null ? "" : ` in ${JSON.stringify(libraryPath)}`
79
+ }: generated package expects ${String(expected)}, loaded library reported ${String(actual)}. ${stagedBinaryGuidance(details)}`,
80
+ {
81
+ ...options,
82
+ details: {
83
+ ...details,
84
+ expected,
85
+ actual,
86
+ },
87
+ },
88
+ );
89
+ }
90
+ }
91
+
92
+ export class ChecksumMismatchError extends UniffiRuntimeError {
93
+ constructor(kind, expected, actual, options = {}) {
94
+ const libraryPath =
95
+ options.details != null && typeof options.details === "object"
96
+ ? options.details.libraryPath
97
+ : undefined;
98
+ super(
99
+ `UniFFI checksum mismatch for ${String(kind)}${
100
+ libraryPath == null ? "" : ` in ${JSON.stringify(libraryPath)}`
101
+ }: generated package expects ${String(expected)}, loaded library reported ${String(actual)}. ${stagedBinaryGuidance(
102
+ options.details,
103
+ )}`,
104
+ {
105
+ ...options,
106
+ details: {
107
+ ...(options.details != null && typeof options.details === "object"
108
+ ? options.details
109
+ : {}),
110
+ kind,
111
+ expected,
112
+ actual,
113
+ },
114
+ },
115
+ );
116
+ }
117
+ }
118
+
119
+ export class UnexpectedRustCallError extends UniffiRuntimeError {
120
+ constructor(message = "Rust returned an expected error without a decoder.", options = {}) {
121
+ super(message, options);
122
+ }
123
+ }
124
+
125
+ export class UnexpectedRustCallStatusCode extends UniffiRuntimeError {
126
+ constructor(code, options = {}) {
127
+ super(`Unexpected RustCallStatus code: ${String(code)}.`, {
128
+ ...options,
129
+ code,
130
+ });
131
+ }
132
+ }
133
+
134
+ export class UnexpectedEnumCase extends UniffiRuntimeError {
135
+ constructor(message = "Encountered an unknown enum case.", options = {}) {
136
+ super(message, options);
137
+ }
138
+ }
139
+
140
+ export class UnexpectedNullPointer extends UniffiRuntimeError {
141
+ constructor(message = "Received a null pointer where a value was required.", options = {}) {
142
+ super(message, options);
143
+ }
144
+ }
145
+
146
+ export class UnexpectedOptionTag extends UniffiRuntimeError {
147
+ constructor(tag, options = {}) {
148
+ super(`Unexpected option tag byte: ${String(tag)}.`, {
149
+ ...options,
150
+ code: tag,
151
+ });
152
+ }
153
+ }
154
+
155
+ export class ConverterRangeError extends UniffiRuntimeError {
156
+ constructor(message = "Value cannot be represented by the requested UniFFI type.", options = {}) {
157
+ super(message, options);
158
+ }
159
+ }
160
+
161
+ export class BufferOverflowError extends UniffiRuntimeError {
162
+ constructor(message = "Buffer does not contain enough bytes.", options = {}) {
163
+ super(message, options);
164
+ }
165
+ }
166
+
167
+ export class BufferSizeMismatchError extends UniffiRuntimeError {
168
+ constructor(expected, actual, options = {}) {
169
+ super(
170
+ `Serialized buffer size mismatch: expected ${String(expected)} bytes, wrote ${String(actual)}.`,
171
+ {
172
+ ...options,
173
+ details: { expected, actual },
174
+ },
175
+ );
176
+ }
177
+ }
178
+
179
+ export class TrailingBytesError extends UniffiRuntimeError {
180
+ constructor(remaining, options = {}) {
181
+ super(`Serialized buffer has ${String(remaining)} unread trailing byte(s).`, {
182
+ ...options,
183
+ details: { remaining },
184
+ });
185
+ }
186
+ }
187
+
188
+ export class DuplicateHandleError extends UniffiRuntimeError {
189
+ constructor(handle, options = {}) {
190
+ super(`Handle ${String(handle)} is already registered.`, {
191
+ ...options,
192
+ details: { handle },
193
+ });
194
+ }
195
+ }
196
+
197
+ export class StaleHandleError extends UniffiRuntimeError {
198
+ constructor(handle, options = {}) {
199
+ super(`Handle ${String(handle)} is stale or unknown.`, {
200
+ ...options,
201
+ details: { handle },
202
+ });
203
+ }
204
+ }
205
+
206
+ export class UnsupportedConverterError extends UniffiRuntimeError {
207
+ constructor(message = "The requested UniFFI converter is not available.", options = {}) {
208
+ super(message, options);
209
+ }
210
+ }
211
+
212
+ export function attachCause(error, cause) {
213
+ if (cause !== undefined) {
214
+ error.cause = cause;
215
+ }
216
+ return error;
217
+ }
218
+
219
+ export function getErrorMessage(value, fallbackMessage = "Unknown error") {
220
+ if (value instanceof Error) {
221
+ return value.message || fallbackMessage;
222
+ }
223
+ if (typeof value === "string") {
224
+ return value;
225
+ }
226
+ if (value == null) {
227
+ return fallbackMessage;
228
+ }
229
+ if (typeof value === "object" && typeof value.message === "string") {
230
+ return value.message;
231
+ }
232
+ return String(value);
233
+ }
234
+
235
+ export function ensureError(value, fallbackMessage = "Unknown error") {
236
+ if (value instanceof Error) {
237
+ return value;
238
+ }
239
+ return new UniffiRuntimeError(getErrorMessage(value, fallbackMessage), {
240
+ cause: value,
241
+ });
242
+ }
243
+
244
+ export function createUniffiErrorClass(
245
+ name,
246
+ { baseClass = UniffiGeneratedError, defaultMessage = name } = {},
247
+ ) {
248
+ return class extends baseClass {
249
+ constructor(message = defaultMessage, options = {}) {
250
+ super(message, options);
251
+ this.name = name;
252
+ }
253
+ };
254
+ }
255
+
256
+ export const UniffiInternalError = Object.freeze({
257
+ AbortError,
258
+ BufferOverflowError,
259
+ BufferSizeMismatchError,
260
+ ChecksumMismatchError,
261
+ ContractVersionMismatchError,
262
+ ConverterRangeError,
263
+ DuplicateHandleError,
264
+ LibraryNotLoadedError,
265
+ RustPanic,
266
+ StaleHandleError,
267
+ TrailingBytesError,
268
+ UnexpectedEnumCase,
269
+ UnexpectedNullPointer,
270
+ UnexpectedOptionTag,
271
+ UnexpectedRustCallError,
272
+ UnexpectedRustCallStatusCode,
273
+ UnsupportedConverterError,
274
+ });
@@ -0,0 +1,100 @@
1
+ export type UniffiTimestamp = Date;
2
+ export type UniffiDuration = number;
3
+
4
+ export interface FfiConverter<T, FfiType = T> {
5
+ allocationSize(value: T): number;
6
+ lower(value: T): FfiType;
7
+ lift(value: FfiType): T;
8
+ write(value: T, writer: ByteWriter): void;
9
+ read(reader: ByteReader): T;
10
+ }
11
+
12
+ export declare class ByteWriter {
13
+ constructor(length: number);
14
+ remaining(): number;
15
+ writeInt8(value: number): void;
16
+ writeUInt8(value: number): void;
17
+ writeInt16(value: number): void;
18
+ writeUInt16(value: number): void;
19
+ writeInt32(value: number): void;
20
+ writeUInt32(value: number): void;
21
+ writeInt64(value: bigint | number): void;
22
+ writeUInt64(value: bigint | number): void;
23
+ writeFloat32(value: number): void;
24
+ writeFloat64(value: number): void;
25
+ writeBytes(value: ArrayBuffer | ArrayBufferView | number[]): void;
26
+ finish(): Uint8Array;
27
+ }
28
+
29
+ export declare class ByteReader {
30
+ constructor(bytes: ArrayBuffer | ArrayBufferView);
31
+ remaining(): number;
32
+ readInt8(): number;
33
+ readUInt8(): number;
34
+ readInt16(): number;
35
+ readUInt16(): number;
36
+ readInt32(): number;
37
+ readUInt32(): number;
38
+ readInt64(): bigint;
39
+ readUInt64(): bigint;
40
+ readFloat32(): number;
41
+ readFloat64(): number;
42
+ readBytes(length: number): Uint8Array;
43
+ readCount(label: string): number;
44
+ finish(): void;
45
+ }
46
+
47
+ export declare abstract class AbstractFfiConverterByteArray<T>
48
+ implements FfiConverter<T, Uint8Array>
49
+ {
50
+ abstract allocationSize(value: T): number;
51
+ lower(value: T): Uint8Array;
52
+ lift(value: ArrayBuffer | ArrayBufferView): T;
53
+ abstract write(value: T, writer: ByteWriter): void;
54
+ abstract read(reader: ByteReader): T;
55
+ }
56
+
57
+ export declare const FfiConverterInt8: Readonly<FfiConverter<number>>;
58
+ export declare const FfiConverterUInt8: Readonly<FfiConverter<number>>;
59
+ export declare const FfiConverterInt16: Readonly<FfiConverter<number>>;
60
+ export declare const FfiConverterUInt16: Readonly<FfiConverter<number>>;
61
+ export declare const FfiConverterInt32: Readonly<FfiConverter<number>>;
62
+ export declare const FfiConverterUInt32: Readonly<FfiConverter<number>>;
63
+ export declare const FfiConverterInt64: Readonly<FfiConverter<bigint, bigint>>;
64
+ export declare const FfiConverterUInt64: Readonly<FfiConverter<bigint, bigint>>;
65
+ export declare const FfiConverterFloat32: Readonly<FfiConverter<number>>;
66
+ export declare const FfiConverterFloat64: Readonly<FfiConverter<number>>;
67
+ export declare const FfiConverterBool: Readonly<FfiConverter<boolean, number>>;
68
+ export declare const FfiConverterString: Readonly<FfiConverter<string, Uint8Array>>;
69
+ export declare const FfiConverterBytes: Readonly<FfiConverter<Uint8Array, Uint8Array>>;
70
+ export declare const FfiConverterByteArray: typeof FfiConverterBytes;
71
+ export declare const FfiConverterArrayBuffer: typeof FfiConverterBytes;
72
+ export declare const FfiConverterTimestamp: Readonly<
73
+ FfiConverter<UniffiTimestamp, Uint8Array>
74
+ >;
75
+ export declare const FfiConverterDuration: Readonly<
76
+ FfiConverter<UniffiDuration, Uint8Array>
77
+ >;
78
+
79
+ export declare class FfiConverterOptional<T> extends AbstractFfiConverterByteArray<T | undefined> {
80
+ constructor(innerConverter: FfiConverter<T, any>);
81
+ allocationSize(value: T | null | undefined): number;
82
+ write(value: T | null | undefined, writer: ByteWriter): void;
83
+ read(reader: ByteReader): T | undefined;
84
+ }
85
+
86
+ export declare class FfiConverterArray<T> extends AbstractFfiConverterByteArray<T[]> {
87
+ constructor(innerConverter: FfiConverter<T, any>);
88
+ allocationSize(value: Iterable<T> | T[]): number;
89
+ write(value: Iterable<T> | T[], writer: ByteWriter): void;
90
+ read(reader: ByteReader): T[];
91
+ }
92
+
93
+ export declare const FfiConverterSequence: typeof FfiConverterArray;
94
+
95
+ export declare class FfiConverterMap<K, V> extends AbstractFfiConverterByteArray<Map<K, V>> {
96
+ constructor(keyConverter: FfiConverter<K, any>, valueConverter: FfiConverter<V, any>);
97
+ allocationSize(value: Map<K, V> | Iterable<[K, V]>): number;
98
+ write(value: Map<K, V> | Iterable<[K, V]>, writer: ByteWriter): void;
99
+ read(reader: ByteReader): Map<K, V>;
100
+ }