rosedb 0.0.0-stage → 1.0.2

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Blckrose
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,115 @@
1
- # Temporary Holding Version
1
+ # RoséDB
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ High-performance embedded Key-Value & Document database engine written in **Rust** with native **Bun FFI (`bun:ffi`)** bindings.
4
+
5
+ ## Fitur Utama
6
+
7
+ - **Storage Engine (`rosedb-storage`)**: Binary page `16 KiB` little-endian dengan **Slotted Data Pages** (page packing), active dirty page buffer, overflow blob chaining, free page allocator, dan atomic `manifest.rdb`.
8
+ - **Write-Ahead Log (`rosedb-wal`)**: Append-only WAL (`BEGIN`, `PUT`, `DELETE`, `COMMIT`, `ABORT`, `CHECKPOINT`) dengan checksum **CRC32C** per record, zero-allocation encoding, pemotongan tail tidak lengkap saat crash, dan deteksi korupsi di tengah log.
9
+ - **Dual Index (`rosedb-index`)**: In-memory **Hash Index** untuk lookup `O(1)` + persistent **B+Tree** (`index.rdb`) untuk `scan` range & prefix query.
10
+ - **Sharded LRU Cache (`rosedb-cache`)**: 32-shard bounded LRU cache (`max_memory`, `max_entries`) untuk skalabilitas multi-reader.
11
+ - **MVCC & Snapshot Isolation (`rosedb-core`)**: Transaksi ACID, `db.snapshot()` untuk pembacaan konsisten tanpa memblokir writer, serta automatic garbage collection versi lama saat snapshot ditutup.
12
+ - **Keamanan (`rosedb-security`)**: Verifikasi integritas **CRC32C**, enkripsi opsional **AES-256-GCM** at rest dengan nonce acak per record, key derivation **Argon2id**, validasi batas key/value, proteksi path traversal, dan file permission ketat (`0o700` / `0o600`).
13
+ - **Bun / TypeScript SDK (`rosedb`)**: Native `bun:ffi` binding dengan reusable scratch buffers (`rosedb_get_into`), `mget()`, `snapshot()`, serta API sync dan async.
14
+
15
+ ---
16
+
17
+ ## Instalasi (npm / Bun)
18
+
19
+ ```bash
20
+ bun add rosedb
21
+ ```
22
+
23
+ atau dengan npm:
24
+
25
+ ```bash
26
+ npm install rosedb
27
+ ```
28
+
29
+ ---
30
+
31
+ ## Penggunaan (Bun / TypeScript)
32
+
33
+ ```ts
34
+ import { Database } from "rosedb";
35
+
36
+ const db = new Database("./rose-data", {
37
+ sync_on_commit: true,
38
+ encryption_password: "optional-secret-password",
39
+ });
40
+
41
+ db.put("username", "Blckrose");
42
+ console.log(db.get("username"));
43
+
44
+ db.put("session:1", "active", 60_000);
45
+
46
+ db.batch([
47
+ { op: "put", key: "user:1", value: "Alice" },
48
+ { op: "put", key: "user:2", value: "Bob" },
49
+ ]);
50
+
51
+ console.log(db.mget(["user:1", "user:2", "missing"]));
52
+ console.log(db.scan("user:1", "user:2"));
53
+
54
+ const snap = db.snapshot();
55
+ db.put("username", "NewName");
56
+ console.log("Current:", db.get("username"));
57
+ console.log("Snapshot:", snap.get("username"));
58
+ snap.close();
59
+
60
+ db.checkpoint();
61
+ db.backup("./rose-backup");
62
+ console.log(db.stats());
63
+ console.log(db.integrityCheck());
64
+
65
+ db.close();
66
+ ```
67
+
68
+ ---
69
+
70
+ ## Penggunaan (Rust)
71
+
72
+ ```rust
73
+ use rosedb_core::Database;
74
+
75
+ fn main() -> Result<(), Box<dyn std::error::Error>> {
76
+ let db = Database::open("./rose-data")?;
77
+ db.put("username", b"Blckrose")?;
78
+
79
+ let snap = db.snapshot()?;
80
+ db.transaction(|tx| {
81
+ tx.put("balance", b"50000")?;
82
+ tx.put("username", b"Updated")?;
83
+ Ok(())
84
+ })?;
85
+
86
+ assert_eq!(snap.get("username")?.as_deref(), Some(b"Blckrose".as_slice()));
87
+ assert_eq!(db.get("username")?.as_deref(), Some(b"Updated".as_slice()));
88
+ snap.close();
89
+
90
+ db.close()?;
91
+ Ok(())
92
+ }
93
+ ```
94
+
95
+ ---
96
+
97
+ ## Build dari Source & Publish ke npm
98
+
99
+ ```bash
100
+ cargo build --release
101
+ cargo test --workspace
102
+ cd bindings/bun
103
+ bun test
104
+ npm publish --access public
105
+ ```
106
+
107
+ ---
108
+
109
+ ## Dokumentasi Lanjutan
110
+
111
+ - [Architecture](docs/architecture.md)
112
+ - [Storage Binary Format](docs/storage-format.md)
113
+ - [WAL Binary Format](docs/wal-format.md)
114
+ - [Security Model](docs/security.md)
115
+ - [Benchmarks](docs/benchmarks.md)
package/index.ts ADDED
@@ -0,0 +1,654 @@
1
+ import { dlopen, FFIType, ptr, CString, toArrayBuffer } from "bun:ffi";
2
+ import { existsSync } from "node:fs";
3
+ import { join, dirname } from "node:path";
4
+
5
+ function resolveLibraryPath(): string {
6
+ const envPath = process.env.ROSEDB_LIB_PATH;
7
+ if (envPath && existsSync(envPath)) {
8
+ return envPath;
9
+ }
10
+ const baseDir = dirname(import.meta.path);
11
+ const candidates = [
12
+ join(baseDir, "librosedb_node.so"),
13
+ join(baseDir, "librosedb_node.dylib"),
14
+ join(baseDir, "../../target/release/librosedb_node.so"),
15
+ join(baseDir, "../../target/debug/librosedb_node.so"),
16
+ join(baseDir, "../../target/release/librosedb_node.dylib"),
17
+ join(baseDir, "../../target/debug/librosedb_node.dylib"),
18
+ ];
19
+ for (const candidate of candidates) {
20
+ if (existsSync(candidate)) {
21
+ return candidate;
22
+ }
23
+ }
24
+ throw new Error(
25
+ "RoséDB native library (librosedb_node.so) not found. Run `cargo build --release` first."
26
+ );
27
+ }
28
+
29
+ const lib = dlopen(resolveLibraryPath(), {
30
+ rosedb_open: {
31
+ args: [FFIType.ptr, FFIType.u64, FFIType.ptr, FFIType.u64, FFIType.ptr],
32
+ returns: FFIType.ptr,
33
+ },
34
+ rosedb_close: {
35
+ args: [FFIType.ptr, FFIType.ptr],
36
+ returns: FFIType.i32,
37
+ },
38
+ rosedb_put: {
39
+ args: [
40
+ FFIType.ptr,
41
+ FFIType.ptr,
42
+ FFIType.u64,
43
+ FFIType.ptr,
44
+ FFIType.u64,
45
+ FFIType.i64,
46
+ FFIType.ptr,
47
+ ],
48
+ returns: FFIType.i32,
49
+ },
50
+ rosedb_get: {
51
+ args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr, FFIType.ptr],
52
+ returns: FFIType.ptr,
53
+ },
54
+ rosedb_get_into: {
55
+ args: [
56
+ FFIType.ptr,
57
+ FFIType.ptr,
58
+ FFIType.u64,
59
+ FFIType.ptr,
60
+ FFIType.u64,
61
+ FFIType.ptr,
62
+ FFIType.ptr,
63
+ FFIType.ptr,
64
+ ],
65
+ returns: FFIType.i32,
66
+ },
67
+ rosedb_mget_json: {
68
+ args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr, FFIType.ptr],
69
+ returns: FFIType.ptr,
70
+ },
71
+ rosedb_delete: {
72
+ args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr],
73
+ returns: FFIType.i32,
74
+ },
75
+ rosedb_exists: {
76
+ args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr],
77
+ returns: FFIType.i32,
78
+ },
79
+ rosedb_scan_json: {
80
+ args: [
81
+ FFIType.ptr,
82
+ FFIType.ptr,
83
+ FFIType.u64,
84
+ FFIType.ptr,
85
+ FFIType.u64,
86
+ FFIType.i64,
87
+ FFIType.ptr,
88
+ FFIType.ptr,
89
+ ],
90
+ returns: FFIType.ptr,
91
+ },
92
+ rosedb_batch_json: {
93
+ args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr],
94
+ returns: FFIType.i32,
95
+ },
96
+ rosedb_checkpoint: {
97
+ args: [FFIType.ptr, FFIType.ptr],
98
+ returns: FFIType.i32,
99
+ },
100
+ rosedb_backup: {
101
+ args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr],
102
+ returns: FFIType.i32,
103
+ },
104
+ rosedb_stats_json: {
105
+ args: [FFIType.ptr, FFIType.ptr, FFIType.ptr],
106
+ returns: FFIType.ptr,
107
+ },
108
+ rosedb_integrity_check_json: {
109
+ args: [FFIType.ptr, FFIType.ptr, FFIType.ptr],
110
+ returns: FFIType.ptr,
111
+ },
112
+ rosedb_snapshot_begin: {
113
+ args: [FFIType.ptr, FFIType.ptr],
114
+ returns: FFIType.ptr,
115
+ },
116
+ rosedb_snapshot_version: {
117
+ args: [FFIType.ptr],
118
+ returns: FFIType.u64,
119
+ },
120
+ rosedb_snapshot_get: {
121
+ args: [FFIType.ptr, FFIType.ptr, FFIType.u64, FFIType.ptr, FFIType.ptr],
122
+ returns: FFIType.ptr,
123
+ },
124
+ rosedb_snapshot_scan_json: {
125
+ args: [
126
+ FFIType.ptr,
127
+ FFIType.ptr,
128
+ FFIType.u64,
129
+ FFIType.ptr,
130
+ FFIType.u64,
131
+ FFIType.i64,
132
+ FFIType.ptr,
133
+ FFIType.ptr,
134
+ ],
135
+ returns: FFIType.ptr,
136
+ },
137
+ rosedb_snapshot_close: {
138
+ args: [FFIType.ptr],
139
+ returns: FFIType.void,
140
+ },
141
+ rosedb_free_bytes: {
142
+ args: [FFIType.ptr, FFIType.u64],
143
+ returns: FFIType.void,
144
+ },
145
+ rosedb_free_cstr: {
146
+ args: [FFIType.ptr],
147
+ returns: FFIType.void,
148
+ },
149
+ });
150
+
151
+ const encoder = new TextEncoder();
152
+ const decoder = new TextDecoder();
153
+
154
+ function toBytes(input: string | Uint8Array): Uint8Array {
155
+ return typeof input === "string" ? encoder.encode(input) : input;
156
+ }
157
+
158
+ function takeError(errBuf: BigUint64Array): string | null {
159
+ const errPtr = Number(errBuf[0]);
160
+ if (!errPtr) return null;
161
+ const msg = new CString(errPtr as any).toString();
162
+ lib.symbols.rosedb_free_cstr(errPtr as any);
163
+ errBuf[0] = 0n;
164
+ return msg;
165
+ }
166
+
167
+ export interface DatabaseOptions {
168
+ page_size?: number;
169
+ buffer_pool_pages?: number;
170
+ cache_enabled?: boolean;
171
+ cache_max_memory?: number;
172
+ cache_max_entries?: number;
173
+ sync_on_commit?: boolean;
174
+ encryption_password?: string;
175
+ }
176
+
177
+ export interface BatchOperation {
178
+ op: "put" | "delete";
179
+ key: string;
180
+ value?: string;
181
+ }
182
+
183
+ export interface ScanEntry {
184
+ key: string;
185
+ value: string;
186
+ }
187
+
188
+ export interface DbStats {
189
+ keys: number;
190
+ data_file_bytes: number;
191
+ wal_file_bytes: number;
192
+ page_size: number;
193
+ encrypted: boolean;
194
+ cache: {
195
+ hits: number;
196
+ misses: number;
197
+ evictions: number;
198
+ current_bytes: number;
199
+ current_entries: number;
200
+ };
201
+ last_checkpoint_seq: number;
202
+ mvcc_version: number;
203
+ active_snapshots: number;
204
+ }
205
+
206
+ export class Snapshot {
207
+ private snapHandle: number | null;
208
+ private outLenBuf = new BigUint64Array(1);
209
+ private errBuf = new BigUint64Array(1);
210
+
211
+ constructor(snapHandle: number) {
212
+ this.snapHandle = snapHandle;
213
+ }
214
+
215
+ private ensureOpen(): any {
216
+ if (!this.snapHandle) {
217
+ throw new Error("Snapshot is closed");
218
+ }
219
+ return this.snapHandle;
220
+ }
221
+
222
+ version(): number {
223
+ const h = this.ensureOpen();
224
+ return Number(lib.symbols.rosedb_snapshot_version(h));
225
+ }
226
+
227
+ get(key: string | Uint8Array): string | null {
228
+ const h = this.ensureOpen();
229
+ const k = toBytes(key);
230
+ this.errBuf[0] = 0n;
231
+ const resPtr = lib.symbols.rosedb_snapshot_get(
232
+ h,
233
+ ptr(k),
234
+ k.length,
235
+ ptr(this.outLenBuf),
236
+ ptr(this.errBuf)
237
+ );
238
+ const err = takeError(this.errBuf);
239
+ if (err) {
240
+ throw new Error(err);
241
+ }
242
+ if (!resPtr) {
243
+ return null;
244
+ }
245
+ const len = Number(this.outLenBuf[0]);
246
+ const copy = new Uint8Array(
247
+ toArrayBuffer(resPtr as any, 0, len).slice(0)
248
+ );
249
+ lib.symbols.rosedb_free_bytes(resPtr as any, len);
250
+ return decoder.decode(copy);
251
+ }
252
+
253
+ scan(start?: string, end?: string, limit?: number): ScanEntry[] {
254
+ const h = this.ensureOpen();
255
+ const sBytes = start ? encoder.encode(start) : new Uint8Array(0);
256
+ const eBytes = end ? encoder.encode(end) : new Uint8Array(0);
257
+ this.errBuf[0] = 0n;
258
+ const resPtr = lib.symbols.rosedb_snapshot_scan_json(
259
+ h,
260
+ sBytes.length > 0 ? ptr(sBytes) : null,
261
+ sBytes.length,
262
+ eBytes.length > 0 ? ptr(eBytes) : null,
263
+ eBytes.length,
264
+ limit ? BigInt(limit) : -1n,
265
+ ptr(this.outLenBuf),
266
+ ptr(this.errBuf)
267
+ );
268
+ const err = takeError(this.errBuf);
269
+ if (err) {
270
+ throw new Error(err);
271
+ }
272
+ if (!resPtr) {
273
+ return [];
274
+ }
275
+ const len = Number(this.outLenBuf[0]);
276
+ const jsonBytes = new Uint8Array(
277
+ toArrayBuffer(resPtr as any, 0, len).slice(0)
278
+ );
279
+ lib.symbols.rosedb_free_bytes(resPtr as any, len);
280
+ return JSON.parse(decoder.decode(jsonBytes));
281
+ }
282
+
283
+ snapshot(): Snapshot {
284
+ const h = this.ensureHandle();
285
+ this.errBuf[0] = 0n;
286
+ const snapPtr = lib.symbols.rosedb_snapshot_begin(h, ptr(this.errBuf));
287
+ const err = takeError(this.errBuf);
288
+ if (err || !snapPtr) {
289
+ throw new Error(err || "Failed to begin snapshot");
290
+ }
291
+ return new Snapshot(snapPtr as unknown as number);
292
+ }
293
+
294
+ close(): void {
295
+ if (!this.snapHandle) return;
296
+ lib.symbols.rosedb_snapshot_close(this.snapHandle as any);
297
+ this.snapHandle = null;
298
+ }
299
+ }
300
+
301
+ export interface IntegrityReport {
302
+ valid: boolean;
303
+ pages_verified: number;
304
+ wal_records_verified: number;
305
+ index_entries_verified: number;
306
+ errors: string[];
307
+ }
308
+
309
+ export class Database {
310
+ private handle: number | null;
311
+ private keyBuf = new Uint8Array(64 * 1024);
312
+ private outBuf = new Uint8Array(64 * 1024);
313
+ private outLenBuf = new BigUint64Array(1);
314
+ private overflowPtrBuf = new BigUint64Array(1);
315
+ private errBuf = new BigUint64Array(1);
316
+
317
+ constructor(path: string, options?: DatabaseOptions) {
318
+ const pathBytes = encoder.encode(path);
319
+ const optsBytes = options
320
+ ? encoder.encode(JSON.stringify(options))
321
+ : new Uint8Array(0);
322
+ const errBuf = new BigUint64Array(1);
323
+
324
+ const h = lib.symbols.rosedb_open(
325
+ ptr(pathBytes),
326
+ pathBytes.length,
327
+ optsBytes.length > 0 ? ptr(optsBytes) : null,
328
+ optsBytes.length,
329
+ ptr(errBuf)
330
+ );
331
+
332
+ const err = takeError(errBuf);
333
+ if (err || !h) {
334
+ throw new Error(err || "Failed to open RoséDB database");
335
+ }
336
+ this.handle = h as unknown as number;
337
+ }
338
+
339
+ private ensureHandle(): any {
340
+ if (!this.handle) {
341
+ throw new Error("RoséDB database is closed");
342
+ }
343
+ return this.handle;
344
+ }
345
+
346
+ put(
347
+ key: string | Uint8Array,
348
+ value: string | Uint8Array,
349
+ ttlMs?: number
350
+ ): void {
351
+ const h = this.ensureHandle();
352
+ const k = toBytes(key);
353
+ const v = toBytes(value);
354
+ const errBuf = new BigUint64Array(1);
355
+ const rc = lib.symbols.rosedb_put(
356
+ h,
357
+ ptr(k),
358
+ k.length,
359
+ v.length > 0 ? ptr(v) : null,
360
+ v.length,
361
+ ttlMs ? BigInt(ttlMs) : -1n,
362
+ ptr(errBuf)
363
+ );
364
+ const err = takeError(errBuf);
365
+ if (rc !== 0 || err) {
366
+ throw new Error(err || "put failed");
367
+ }
368
+ }
369
+
370
+ get(key: string | Uint8Array): string | null {
371
+ const h = this.ensureHandle();
372
+ let kPtr: any;
373
+ let kLen: number;
374
+ if (typeof key === "string" && key.length <= 20000) {
375
+ const { written } = encoder.encodeInto(key, this.keyBuf);
376
+ kPtr = ptr(this.keyBuf);
377
+ kLen = written;
378
+ } else {
379
+ const k = toBytes(key);
380
+ kPtr = ptr(k);
381
+ kLen = k.length;
382
+ }
383
+
384
+ this.errBuf[0] = 0n;
385
+ const rc = lib.symbols.rosedb_get_into(
386
+ h,
387
+ kPtr,
388
+ kLen,
389
+ ptr(this.outBuf),
390
+ this.outBuf.length,
391
+ ptr(this.outLenBuf),
392
+ ptr(this.overflowPtrBuf),
393
+ ptr(this.errBuf)
394
+ );
395
+ const err = takeError(this.errBuf);
396
+ if (rc < 0 || err) {
397
+ throw new Error(err || "get failed");
398
+ }
399
+ if (rc === 0) {
400
+ return null;
401
+ }
402
+ const len = Number(this.outLenBuf[0]);
403
+ if (rc === 1) {
404
+ return decoder.decode(this.outBuf.subarray(0, len));
405
+ }
406
+ const ovPtr = Number(this.overflowPtrBuf[0]);
407
+ const copy = new Uint8Array(toArrayBuffer(ovPtr as any, 0, len).slice(0));
408
+ lib.symbols.rosedb_free_bytes(ovPtr as any, len);
409
+ return decoder.decode(copy);
410
+ }
411
+
412
+ mget(keys: string[]): (string | null)[] {
413
+ const h = this.ensureHandle();
414
+ const payload = encoder.encode(JSON.stringify(keys));
415
+ this.errBuf[0] = 0n;
416
+ const resPtr = lib.symbols.rosedb_mget_json(
417
+ h,
418
+ ptr(payload),
419
+ payload.length,
420
+ ptr(this.outLenBuf),
421
+ ptr(this.errBuf)
422
+ );
423
+ const err = takeError(this.errBuf);
424
+ if (err || !resPtr) {
425
+ throw new Error(err || "mget failed");
426
+ }
427
+ const len = Number(this.outLenBuf[0]);
428
+ const jsonBytes = new Uint8Array(
429
+ toArrayBuffer(resPtr as any, 0, len).slice(0)
430
+ );
431
+ lib.symbols.rosedb_free_bytes(resPtr as any, len);
432
+ return JSON.parse(decoder.decode(jsonBytes));
433
+ }
434
+
435
+ getBuffer(key: string | Uint8Array): Uint8Array | null {
436
+ const h = this.ensureHandle();
437
+ const k = toBytes(key);
438
+ const outLen = new BigUint64Array(1);
439
+ const errBuf = new BigUint64Array(1);
440
+ const resPtr = lib.symbols.rosedb_get(
441
+ h,
442
+ ptr(k),
443
+ k.length,
444
+ ptr(outLen),
445
+ ptr(errBuf)
446
+ );
447
+ const err = takeError(errBuf);
448
+ if (err) {
449
+ throw new Error(err);
450
+ }
451
+ if (!resPtr) {
452
+ return null;
453
+ }
454
+ const len = Number(outLen[0]);
455
+ const copy = new Uint8Array(
456
+ toArrayBuffer(resPtr as any, 0, len).slice(0)
457
+ );
458
+ lib.symbols.rosedb_free_bytes(resPtr as any, len);
459
+ return copy;
460
+ }
461
+
462
+ delete(key: string | Uint8Array): void {
463
+ const h = this.ensureHandle();
464
+ const k = toBytes(key);
465
+ const errBuf = new BigUint64Array(1);
466
+ const rc = lib.symbols.rosedb_delete(h, ptr(k), k.length, ptr(errBuf));
467
+ const err = takeError(errBuf);
468
+ if (rc !== 0 || err) {
469
+ throw new Error(err || "delete failed");
470
+ }
471
+ }
472
+
473
+ exists(key: string | Uint8Array): boolean {
474
+ const h = this.ensureHandle();
475
+ const k = toBytes(key);
476
+ const errBuf = new BigUint64Array(1);
477
+ const rc = lib.symbols.rosedb_exists(h, ptr(k), k.length, ptr(errBuf));
478
+ const err = takeError(errBuf);
479
+ if (rc < 0 || err) {
480
+ throw new Error(err || "exists failed");
481
+ }
482
+ return rc === 1;
483
+ }
484
+
485
+ scan(start?: string, end?: string, limit?: number): ScanEntry[] {
486
+ const h = this.ensureHandle();
487
+ const sBytes = start ? encoder.encode(start) : new Uint8Array(0);
488
+ const eBytes = end ? encoder.encode(end) : new Uint8Array(0);
489
+ const outLen = new BigUint64Array(1);
490
+ const errBuf = new BigUint64Array(1);
491
+
492
+ const resPtr = lib.symbols.rosedb_scan_json(
493
+ h,
494
+ sBytes.length > 0 ? ptr(sBytes) : null,
495
+ sBytes.length,
496
+ eBytes.length > 0 ? ptr(eBytes) : null,
497
+ eBytes.length,
498
+ limit ? BigInt(limit) : -1n,
499
+ ptr(outLen),
500
+ ptr(errBuf)
501
+ );
502
+ const err = takeError(errBuf);
503
+ if (err) {
504
+ throw new Error(err);
505
+ }
506
+ if (!resPtr) {
507
+ return [];
508
+ }
509
+ const len = Number(outLen[0]);
510
+ const jsonBytes = new Uint8Array(
511
+ toArrayBuffer(resPtr as any, 0, len).slice(0)
512
+ );
513
+ lib.symbols.rosedb_free_bytes(resPtr as any, len);
514
+ return JSON.parse(decoder.decode(jsonBytes));
515
+ }
516
+
517
+ batch(ops: BatchOperation[]): void {
518
+ const h = this.ensureHandle();
519
+ const payload = encoder.encode(JSON.stringify(ops));
520
+ const errBuf = new BigUint64Array(1);
521
+ const rc = lib.symbols.rosedb_batch_json(
522
+ h,
523
+ ptr(payload),
524
+ payload.length,
525
+ ptr(errBuf)
526
+ );
527
+ const err = takeError(errBuf);
528
+ if (rc !== 0 || err) {
529
+ throw new Error(err || "batch failed");
530
+ }
531
+ }
532
+
533
+ checkpoint(): void {
534
+ const h = this.ensureHandle();
535
+ const errBuf = new BigUint64Array(1);
536
+ const rc = lib.symbols.rosedb_checkpoint(h, ptr(errBuf));
537
+ const err = takeError(errBuf);
538
+ if (rc !== 0 || err) {
539
+ throw new Error(err || "checkpoint failed");
540
+ }
541
+ }
542
+
543
+ backup(destDir: string): void {
544
+ const h = this.ensureHandle();
545
+ const destBytes = encoder.encode(destDir);
546
+ const errBuf = new BigUint64Array(1);
547
+ const rc = lib.symbols.rosedb_backup(
548
+ h,
549
+ ptr(destBytes),
550
+ destBytes.length,
551
+ ptr(errBuf)
552
+ );
553
+ const err = takeError(errBuf);
554
+ if (rc !== 0 || err) {
555
+ throw new Error(err || "backup failed");
556
+ }
557
+ }
558
+
559
+ stats(): DbStats {
560
+ const h = this.ensureHandle();
561
+ const outLen = new BigUint64Array(1);
562
+ const errBuf = new BigUint64Array(1);
563
+ const resPtr = lib.symbols.rosedb_stats_json(h, ptr(outLen), ptr(errBuf));
564
+ const err = takeError(errBuf);
565
+ if (err || !resPtr) {
566
+ throw new Error(err || "stats failed");
567
+ }
568
+ const len = Number(outLen[0]);
569
+ const jsonBytes = new Uint8Array(
570
+ toArrayBuffer(resPtr as any, 0, len).slice(0)
571
+ );
572
+ lib.symbols.rosedb_free_bytes(resPtr as any, len);
573
+ return JSON.parse(decoder.decode(jsonBytes));
574
+ }
575
+
576
+ integrityCheck(): IntegrityReport {
577
+ const h = this.ensureHandle();
578
+ const outLen = new BigUint64Array(1);
579
+ const errBuf = new BigUint64Array(1);
580
+ const resPtr = lib.symbols.rosedb_integrity_check_json(
581
+ h,
582
+ ptr(outLen),
583
+ ptr(errBuf)
584
+ );
585
+ const err = takeError(errBuf);
586
+ if (err || !resPtr) {
587
+ throw new Error(err || "integrityCheck failed");
588
+ }
589
+ const len = Number(outLen[0]);
590
+ const jsonBytes = new Uint8Array(
591
+ toArrayBuffer(resPtr as any, 0, len).slice(0)
592
+ );
593
+ lib.symbols.rosedb_free_bytes(resPtr as any, len);
594
+ return JSON.parse(decoder.decode(jsonBytes));
595
+ }
596
+
597
+ snapshot(): Snapshot {
598
+ const h = this.ensureHandle();
599
+ this.errBuf[0] = 0n;
600
+ const snapPtr = lib.symbols.rosedb_snapshot_begin(h, ptr(this.errBuf));
601
+ const err = takeError(this.errBuf);
602
+ if (err || !snapPtr) {
603
+ throw new Error(err || "Failed to begin snapshot");
604
+ }
605
+ return new Snapshot(snapPtr as unknown as number);
606
+ }
607
+
608
+ close(): void {
609
+ if (!this.handle) return;
610
+ const errBuf = new BigUint64Array(1);
611
+ const rc = lib.symbols.rosedb_close(this.handle as any, ptr(errBuf));
612
+ this.handle = null;
613
+ const err = takeError(errBuf);
614
+ if (rc !== 0 || err) {
615
+ throw new Error(err || "close failed");
616
+ }
617
+ }
618
+
619
+ async putAsync(
620
+ key: string | Uint8Array,
621
+ value: string | Uint8Array,
622
+ ttlMs?: number
623
+ ): Promise<void> {
624
+ return Promise.resolve().then(() => this.put(key, value, ttlMs));
625
+ }
626
+
627
+ async getAsync(key: string | Uint8Array): Promise<string | null> {
628
+ return Promise.resolve().then(() => this.get(key));
629
+ }
630
+
631
+ async deleteAsync(key: string | Uint8Array): Promise<void> {
632
+ return Promise.resolve().then(() => this.delete(key));
633
+ }
634
+
635
+ async scanAsync(
636
+ start?: string,
637
+ end?: string,
638
+ limit?: number
639
+ ): Promise<ScanEntry[]> {
640
+ return Promise.resolve().then(() => this.scan(start, end, limit));
641
+ }
642
+
643
+ async batchAsync(ops: BatchOperation[]): Promise<void> {
644
+ return Promise.resolve().then(() => this.batch(ops));
645
+ }
646
+
647
+ async checkpointAsync(): Promise<void> {
648
+ return Promise.resolve().then(() => this.checkpoint());
649
+ }
650
+
651
+ async backupAsync(destDir: string): Promise<void> {
652
+ return Promise.resolve().then(() => this.backup(destDir));
653
+ }
654
+ }
Binary file
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "rosedb",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.2",
4
+ "description": "High-performance embedded Key-Value & Document database engine written in Rust with Bun FFI bindings (ACID, WAL, B+Tree, MVCC Snapshot Isolation, AES-256-GCM).",
5
+ "type": "module",
6
+ "main": "./index.ts",
7
+ "types": "./index.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./index.ts",
11
+ "types": "./index.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "index.ts",
16
+ "librosedb_node.so",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "keywords": [
21
+ "database",
22
+ "embedded",
23
+ "kv",
24
+ "rust",
25
+ "bun",
26
+ "ffi",
27
+ "wal",
28
+ "mvcc",
29
+ "btree",
30
+ "aes-gcm"
31
+ ],
32
+ "author": "Blckrose",
33
+ "license": "MIT",
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "engines": {
38
+ "bun": ">=1.1.0"
39
+ },
40
+ "scripts": {
41
+ "test": "bun test",
42
+ "prepack": "cp ../../target/release/librosedb_node.so ./librosedb_node.so && cp ../../README.md ./README.md && cp ../../LICENSE ./LICENSE"
43
+ }
44
+ }