@tursodatabase/sync-react-native 0.5.0-pre.4

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.
Files changed (72) hide show
  1. package/README.md +117 -0
  2. package/android/CMakeLists.txt +53 -0
  3. package/android/build.gradle +84 -0
  4. package/android/cpp-adapter.cpp +49 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/com/turso/sync/reactnative/TursoBridge.java +44 -0
  7. package/android/src/main/java/com/turso/sync/reactnative/TursoModule.java +82 -0
  8. package/android/src/main/java/com/turso/sync/reactnative/TursoPackage.java +29 -0
  9. package/cpp/TursoConnectionHostObject.cpp +179 -0
  10. package/cpp/TursoConnectionHostObject.h +52 -0
  11. package/cpp/TursoDatabaseHostObject.cpp +98 -0
  12. package/cpp/TursoDatabaseHostObject.h +49 -0
  13. package/cpp/TursoHostObject.cpp +561 -0
  14. package/cpp/TursoHostObject.h +24 -0
  15. package/cpp/TursoStatementHostObject.cpp +414 -0
  16. package/cpp/TursoStatementHostObject.h +65 -0
  17. package/cpp/TursoSyncChangesHostObject.cpp +41 -0
  18. package/cpp/TursoSyncChangesHostObject.h +52 -0
  19. package/cpp/TursoSyncDatabaseHostObject.cpp +328 -0
  20. package/cpp/TursoSyncDatabaseHostObject.h +61 -0
  21. package/cpp/TursoSyncIoItemHostObject.cpp +304 -0
  22. package/cpp/TursoSyncIoItemHostObject.h +52 -0
  23. package/cpp/TursoSyncOperationHostObject.cpp +168 -0
  24. package/cpp/TursoSyncOperationHostObject.h +53 -0
  25. package/ios/TursoModule.h +8 -0
  26. package/ios/TursoModule.mm +95 -0
  27. package/lib/commonjs/Database.js +445 -0
  28. package/lib/commonjs/Database.js.map +1 -0
  29. package/lib/commonjs/Statement.js +339 -0
  30. package/lib/commonjs/Statement.js.map +1 -0
  31. package/lib/commonjs/index.js +229 -0
  32. package/lib/commonjs/index.js.map +1 -0
  33. package/lib/commonjs/internal/asyncOperation.js +124 -0
  34. package/lib/commonjs/internal/asyncOperation.js.map +1 -0
  35. package/lib/commonjs/internal/ioProcessor.js +315 -0
  36. package/lib/commonjs/internal/ioProcessor.js.map +1 -0
  37. package/lib/commonjs/package.json +1 -0
  38. package/lib/commonjs/types.js +133 -0
  39. package/lib/commonjs/types.js.map +1 -0
  40. package/lib/module/Database.js +441 -0
  41. package/lib/module/Database.js.map +1 -0
  42. package/lib/module/Statement.js +335 -0
  43. package/lib/module/Statement.js.map +1 -0
  44. package/lib/module/index.js +205 -0
  45. package/lib/module/index.js.map +1 -0
  46. package/lib/module/internal/asyncOperation.js +116 -0
  47. package/lib/module/internal/asyncOperation.js.map +1 -0
  48. package/lib/module/internal/ioProcessor.js +309 -0
  49. package/lib/module/internal/ioProcessor.js.map +1 -0
  50. package/lib/module/package.json +1 -0
  51. package/lib/module/types.js +163 -0
  52. package/lib/module/types.js.map +1 -0
  53. package/lib/typescript/Database.d.ts +140 -0
  54. package/lib/typescript/Database.d.ts.map +1 -0
  55. package/lib/typescript/Statement.d.ts +105 -0
  56. package/lib/typescript/Statement.d.ts.map +1 -0
  57. package/lib/typescript/index.d.ts +175 -0
  58. package/lib/typescript/index.d.ts.map +1 -0
  59. package/lib/typescript/internal/asyncOperation.d.ts +39 -0
  60. package/lib/typescript/internal/asyncOperation.d.ts.map +1 -0
  61. package/lib/typescript/internal/ioProcessor.d.ts +48 -0
  62. package/lib/typescript/internal/ioProcessor.d.ts.map +1 -0
  63. package/lib/typescript/types.d.ts +316 -0
  64. package/lib/typescript/types.d.ts.map +1 -0
  65. package/package.json +97 -0
  66. package/src/Database.ts +480 -0
  67. package/src/Statement.ts +372 -0
  68. package/src/index.ts +240 -0
  69. package/src/internal/asyncOperation.ts +147 -0
  70. package/src/internal/ioProcessor.ts +328 -0
  71. package/src/types.ts +391 -0
  72. package/turso-sync-react-native.podspec +56 -0
package/src/types.ts ADDED
@@ -0,0 +1,391 @@
1
+ /**
2
+ * Turso React Native SDK-KIT Types
3
+ *
4
+ * Clean TypeScript types matching the SDK-KIT C API patterns.
5
+ * All logic lives in TypeScript or Rust - the C++ layer is just a thin bridge.
6
+ */
7
+
8
+ // ============================================================================
9
+ // Core SDK-KIT Types (Local Database)
10
+ // ============================================================================
11
+
12
+ /**
13
+ * Native database interface (local-only)
14
+ * Thin wrapper around TursoDatabaseHostObject
15
+ */
16
+ export interface NativeDatabase {
17
+ open(): void;
18
+ connect(): NativeConnection;
19
+ close(): void;
20
+ }
21
+
22
+ /**
23
+ * Native connection interface
24
+ * Thin wrapper around TursoConnectionHostObject
25
+ */
26
+ export interface NativeConnection {
27
+ prepareSingle(sql: string): NativeStatement;
28
+ prepareFirst(sql: string): { statement: NativeStatement | null; tailIdx: number };
29
+ lastInsertRowid(): number;
30
+ getAutocommit(): boolean;
31
+ setBusyTimeout(timeoutMs: number): void;
32
+ close(): void;
33
+ }
34
+
35
+ /**
36
+ * Native statement interface
37
+ * Thin wrapper around TursoStatementHostObject
38
+ */
39
+ export interface NativeStatement {
40
+ // Bind methods
41
+ bindPositionalNull(position: number): number;
42
+ bindPositionalInt(position: number, value: number): number;
43
+ bindPositionalDouble(position: number, value: number): number;
44
+ bindPositionalBlob(position: number, value: ArrayBuffer): number;
45
+ bindPositionalText(position: number, value: string): number;
46
+
47
+ // Execution methods
48
+ execute(): { status: number; rowsChanged: number };
49
+ step(): number; // Returns status code
50
+ runIo(): number;
51
+ reset(): void;
52
+ finalize(): number;
53
+
54
+ // Query methods
55
+ nChange(): number;
56
+ columnCount(): number;
57
+ columnName(index: number): string | null;
58
+ rowValueKind(index: number): number; // TursoType enum
59
+ rowValueBytesCount(index: number): number;
60
+ rowValueBytesPtr(index: number): ArrayBuffer | null;
61
+ rowValueText(index: number): string;
62
+ rowValueInt(index: number): number;
63
+ rowValueDouble(index: number): number;
64
+
65
+ // Parameter methods
66
+ namedPosition(name: string): number;
67
+ parametersCount(): number;
68
+ }
69
+
70
+ // ============================================================================
71
+ // Sync SDK-KIT Types (Embedded Replica)
72
+ // ============================================================================
73
+
74
+ /**
75
+ * Native sync database interface (embedded replica)
76
+ * Thin wrapper around TursoSyncDatabaseHostObject
77
+ */
78
+ export interface NativeSyncDatabase {
79
+ // Async operations - return NativeSyncOperation
80
+ open(): NativeSyncOperation;
81
+ create(): NativeSyncOperation;
82
+ connect(): NativeSyncOperation;
83
+ stats(): NativeSyncOperation;
84
+ checkpoint(): NativeSyncOperation;
85
+ pushChanges(): NativeSyncOperation;
86
+ waitChanges(): NativeSyncOperation;
87
+ applyChanges(changes: NativeSyncChanges): NativeSyncOperation;
88
+
89
+ // IO queue management
90
+ ioTakeItem(): NativeSyncIoItem | null;
91
+ ioStepCallbacks(): void;
92
+
93
+ close(): void;
94
+ }
95
+
96
+ /**
97
+ * Native sync operation interface
98
+ * Thin wrapper around TursoSyncOperationHostObject
99
+ * Represents an async operation that must be driven by calling resume()
100
+ */
101
+ export interface NativeSyncOperation {
102
+ resume(): number; // Returns status code (TURSO_DONE, TURSO_IO, etc.)
103
+ resultKind(): number; // Returns result type enum
104
+ extractConnection(): NativeConnection;
105
+ extractChanges(): NativeSyncChanges | null;
106
+ extractStats(): SyncStats;
107
+ }
108
+
109
+ /**
110
+ * Native sync IO item interface
111
+ * Thin wrapper around TursoSyncIoItemHostObject
112
+ * Represents an IO request that JavaScript must process using fetch() or fs
113
+ */
114
+ export interface NativeSyncIoItem {
115
+ getKind(): 'HTTP' | 'FULL_READ' | 'FULL_WRITE' | 'NONE';
116
+ getHttpRequest(): HttpRequest;
117
+ getFullReadPath(): string;
118
+ getFullWriteRequest(): FullWriteRequest;
119
+
120
+ // Completion methods
121
+ poison(error: string): void;
122
+ setStatus(statusCode: number): void;
123
+ pushBuffer(data: ArrayBuffer): void;
124
+ done(): void;
125
+ }
126
+
127
+ /**
128
+ * Native sync changes interface
129
+ * Thin wrapper around TursoSyncChangesHostObject
130
+ * Represents changes fetched from remote (opaque, passed to applyChanges)
131
+ */
132
+ export interface NativeSyncChanges {
133
+ // Mostly opaque - just passed to applyChanges()
134
+ }
135
+
136
+ // ============================================================================
137
+ // Turso Status Codes
138
+ // ============================================================================
139
+
140
+ export enum TursoStatus {
141
+ OK = 0,
142
+ DONE = 1,
143
+ ROW = 2,
144
+ IO = 3,
145
+ BUSY = 4,
146
+ INTERRUPT = 5,
147
+ BUSY_SNAPSHOT = 6,
148
+ ERROR = 127,
149
+ MISUSE = 128,
150
+ CONSTRAINT = 129,
151
+ READONLY = 130,
152
+ DATABASE_FULL = 131,
153
+ NOTADB = 132,
154
+ CORRUPT = 133,
155
+ IOERR = 134,
156
+ }
157
+
158
+ // ============================================================================
159
+ // Turso Value Types
160
+ // ============================================================================
161
+
162
+ export enum TursoType {
163
+ UNKNOWN = 0,
164
+ INTEGER = 1,
165
+ REAL = 2,
166
+ TEXT = 3,
167
+ BLOB = 4,
168
+ NULL = 5,
169
+ }
170
+
171
+ // ============================================================================
172
+ // Sync Operation Result Types
173
+ // ============================================================================
174
+
175
+ export enum SyncOperationResultType {
176
+ NONE = 0,
177
+ CONNECTION = 1,
178
+ CHANGES = 2,
179
+ STATS = 3,
180
+ }
181
+
182
+ // ============================================================================
183
+ // Public API Types (High-level TypeScript)
184
+ // ============================================================================
185
+
186
+ /**
187
+ * Supported SQLite value types for the public API
188
+ */
189
+ export type SQLiteValue = null | number | string | ArrayBuffer;
190
+
191
+ /**
192
+ * Parameters that can be bound to SQL statements
193
+ */
194
+ export type BindParams =
195
+ | SQLiteValue[]
196
+ | Record<string, SQLiteValue>
197
+ | SQLiteValue;
198
+
199
+ /**
200
+ * Result of a run() or exec() operation
201
+ */
202
+ export interface RunResult {
203
+ /** Number of rows changed by the statement */
204
+ changes: number;
205
+ /** Last inserted row ID */
206
+ lastInsertRowid: number;
207
+ }
208
+
209
+ /**
210
+ * A row returned from a query
211
+ */
212
+ export type Row = Record<string, SQLiteValue>;
213
+
214
+ /**
215
+ * Encryption options (matches JavaScript bindings)
216
+ */
217
+ export interface EncryptionOpts {
218
+ /** base64 encoded encryption key (must be either 16 or 32 bytes depending on cipher) */
219
+ key: string;
220
+ /**
221
+ * encryption cipher algorithm
222
+ * - aes256gcm, aes128gcm, chacha20poly1305: 28 reserved bytes
223
+ * - aegis128l, aegis128x2, aegis128x4: 32 reserved bytes
224
+ * - aegis256, aegis256x2, aegis256x4: 48 reserved bytes
225
+ */
226
+ cipher:
227
+ | 'aes256gcm'
228
+ | 'aes128gcm'
229
+ | 'chacha20poly1305'
230
+ | 'aegis128l'
231
+ | 'aegis128x2'
232
+ | 'aegis128x4'
233
+ | 'aegis256'
234
+ | 'aegis256x2'
235
+ | 'aegis256x4';
236
+ }
237
+
238
+ /**
239
+ * Database options (matches JavaScript bindings)
240
+ * Single unified config for both local and sync databases
241
+ */
242
+ export interface DatabaseOpts {
243
+ /**
244
+ * Local path where to store database file (e.g. local.db)
245
+ * Sync database will write several files with that prefix
246
+ * (e.g. local.db-info, local.db-wal, etc)
247
+ */
248
+ path: string;
249
+
250
+ /**
251
+ * Optional URL of the remote database (e.g. libsql://db-org.turso.io)
252
+ * If omitted - local-only database will be created
253
+ *
254
+ * You can also provide function which will return URL or null
255
+ * In this case local database will be created and sync will be "switched-on"
256
+ * whenever the url returns non-empty value
257
+ */
258
+ url?: string | (() => string | null);
259
+
260
+ /**
261
+ * Auth token for the remote database
262
+ * (can be either static string or function which will provide short-lived credentials)
263
+ */
264
+ authToken?: string | (() => Promise<string>);
265
+
266
+ /**
267
+ * Arbitrary client name which can be used to distinguish clients internally
268
+ * The library will guarantee uniqueness of the clientId by appending unique suffix
269
+ */
270
+ clientName?: string;
271
+
272
+ /**
273
+ * Optional remote encryption parameters if cloud database was encrypted
274
+ */
275
+ remoteEncryption?: EncryptionOpts;
276
+
277
+ /**
278
+ * Optional long-polling timeout for pull operation
279
+ * If not set - no timeout is applied
280
+ */
281
+ longPollTimeoutMs?: number;
282
+
283
+ /**
284
+ * Optional parameter to enable internal logging for the database
285
+ */
286
+ tracing?: 'error' | 'warn' | 'info' | 'debug' | 'trace';
287
+
288
+ /**
289
+ * Bootstrap database if empty; if set - client will be able to connect
290
+ * to fresh db only when network is online
291
+ */
292
+ bootstrapIfEmpty?: boolean;
293
+
294
+ /**
295
+ * Optional parameter to enable partial sync for the database
296
+ * WARNING: This feature is EXPERIMENTAL
297
+ */
298
+ partialSyncExperimental?: {
299
+ /**
300
+ * Bootstrap strategy configuration
301
+ * - prefix strategy loads first N bytes locally at startup
302
+ * - query strategy loads pages touched by the provided SQL statement
303
+ */
304
+ bootstrapStrategy:
305
+ | { kind: 'prefix'; length: number }
306
+ | { kind: 'query'; query: string };
307
+ /**
308
+ * Optional segment size which makes sync engine load pages in batches
309
+ * (so, if loading page 1 with segment_size=128kb then 32 pages [1..32] will be loaded)
310
+ */
311
+ segmentSize?: number;
312
+ /**
313
+ * Optional parameter which makes sync engine prefetch pages which probably
314
+ * will be accessed soon
315
+ */
316
+ prefetch?: boolean;
317
+ };
318
+ }
319
+
320
+
321
+ /**
322
+ * Sync stats returned by stats() operation
323
+ */
324
+ export interface SyncStats {
325
+ cdcOperations: number;
326
+ mainWalSize: number;
327
+ revertWalSize: number;
328
+ lastPullUnixTime: number;
329
+ lastPushUnixTime: number;
330
+ networkSentBytes: number;
331
+ networkReceivedBytes: number;
332
+ revision: string | null;
333
+ }
334
+
335
+ /**
336
+ * HTTP request from sync engine
337
+ */
338
+ export interface HttpRequest {
339
+ url: string | null;
340
+ method: string;
341
+ path: string;
342
+ headers: Record<string, string>;
343
+ body: ArrayBuffer | null;
344
+ }
345
+
346
+ /**
347
+ * Full write request from sync engine
348
+ */
349
+ export interface FullWriteRequest {
350
+ path: string;
351
+ content: ArrayBuffer | null;
352
+ }
353
+
354
+ // ============================================================================
355
+ // Global Turso Proxy Interface
356
+ // ============================================================================
357
+
358
+ /**
359
+ * Native proxy interface exposed via JSI
360
+ */
361
+ export interface TursoProxy {
362
+ newDatabase(path: string, config?: any): NativeDatabase;
363
+ newSyncDatabase(dbConfig: any, syncConfig: any): NativeSyncDatabase;
364
+ version(): string;
365
+ setup(options: { logLevel?: string }): void;
366
+ fsReadFile(path: string): ArrayBuffer | null;
367
+ fsWriteFile(path: string, data: ArrayBuffer): void;
368
+ }
369
+
370
+ /**
371
+ * Native module interface (React Native bridge)
372
+ */
373
+ export interface TursoNativeModule {
374
+ install(): boolean;
375
+ // Constants exposed by native module
376
+ ANDROID_DATABASE_PATH?: string;
377
+ ANDROID_FILES_PATH?: string;
378
+ ANDROID_EXTERNAL_FILES_PATH?: string;
379
+ IOS_DOCUMENT_PATH?: string;
380
+ IOS_LIBRARY_PATH?: string;
381
+ }
382
+
383
+ /**
384
+ * Global __TursoProxy object injected by native code
385
+ */
386
+ declare global {
387
+ // eslint-disable-next-line no-var
388
+ var __TursoProxy: TursoProxy;
389
+ }
390
+
391
+ export {};
@@ -0,0 +1,56 @@
1
+ require "json"
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "turso-sync-react-native"
7
+ s.version = package["version"]
8
+ s.summary = package["description"]
9
+ s.homepage = package["homepage"]
10
+ s.license = package["license"]
11
+ s.authors = package["author"]
12
+
13
+ s.platforms = { :ios => "13.0" }
14
+ s.source = { :git => "https://github.com/tursodatabase/turso.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = [
17
+ "ios/**/*.{h,m,mm}",
18
+ "cpp/**/*.{h,hpp,cpp}"
19
+ ]
20
+
21
+ # Vendored Rust XCFramework (handles device + simulator automatically)
22
+ s.vendored_frameworks = "libs/ios/turso-sync-sdk-kit.xcframework"
23
+
24
+ # Explicitly export C headers from the vendored xcframework
25
+ s.static_framework = true
26
+ s.public_header_files = "libs/ios/turso-sync-sdk-kit.xcframework/**/Headers/*.h"
27
+ s.header_mappings_dir = "libs/ios/turso-sync-sdk-kit.xcframework"
28
+
29
+ # Header search paths
30
+ s.pod_target_xcconfig = {
31
+ "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
32
+ "HEADER_SEARCH_PATHS" => [
33
+ "$(PODS_TARGET_SRCROOT)/cpp",
34
+ "$(PODS_TARGET_SRCROOT)/libs/ios"
35
+ ].join(" "),
36
+ "OTHER_LDFLAGS" => "-lc++",
37
+ "DEFINES_MODULE" => "YES",
38
+ "GCC_PRECOMPILE_PREFIX_HEADER" => "NO"
39
+ }
40
+
41
+ # User header search paths for consumers
42
+ s.user_target_xcconfig = {
43
+ "HEADER_SEARCH_PATHS" => "$(PODS_ROOT)/turso-sync-react-native/cpp $(PODS_ROOT)/turso-sync-react-native/libs/ios"
44
+ }
45
+
46
+ # Build script to compile Rust
47
+ s.script_phase = {
48
+ :name => "Build Turso Rust Library",
49
+ :script => 'set -e; cd "${PODS_TARGET_SRCROOT}"; if [ ! -d "libs/ios/turso-sync-sdk-kit.xcframework" ]; then echo "Building Rust library for iOS..."; make ios; fi',
50
+ :execution_position => :before_compile,
51
+ :shell_path => "/bin/bash"
52
+ }
53
+
54
+ # Install React Native module dependencies (includes React-Core, turbomodule, etc.)
55
+ install_modules_dependencies(s)
56
+ end