absurder-sql 0.1.27

High-performance SQLite for browsers (IndexedDB or Hybrid OPFS), native apps, and mobile, with export/import and multi-tab coordination
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! WASM OPFS block persistence helpers.
//!
//! This module provides a minimal write-through OPFS path for browser worker
//! contexts using `FileSystemSyncAccessHandle`. The current slice mirrors block
//! data into OPFS on sync while keeping IndexedDB restoration as the reload path
//! until full OPFS restore/orchestration lands.

use crate::storage::metadata::{BlockMetadataPersist, ChecksumAlgorithm, ChecksumManager};
use crate::storage::vfs_sync;
use crate::types::DatabaseError;
use crate::utils::normalize_db_name;
use wasm_bindgen::prelude::*;

const OPFS_FILE_SUFFIX: &str = ".absurder.blocks.bin";

#[wasm_bindgen(inline_js = r#"
const absurdHandles = new Map();
let absurdNextHandleId = 1;

function absurdGetRoot() {
  if (!globalThis.navigator || !globalThis.navigator.storage || typeof globalThis.navigator.storage.getDirectory !== 'function') {
    throw new Error('OPFS is not available in this context');
  }

  return globalThis.navigator.storage.getDirectory();
}

export async function absurd_opfs_open(fileName, create = true) {
  const root = await absurdGetRoot();
    const fileHandle = await root.getFileHandle(fileName, { create });
  const accessHandle = await fileHandle.createSyncAccessHandle();
  const id = absurdNextHandleId++;
  absurdHandles.set(id, accessHandle);
  return id;
}

export async function absurd_opfs_exists(fileName) {
    try {
        const root = await absurdGetRoot();
        await root.getFileHandle(fileName, { create: false });
        return true;
    } catch (error) {
        if (error && (error.name === 'NotFoundError' || String(error).includes('NotFound'))) {
            return false;
        }
        throw error;
    }
}

export function absurd_opfs_read(id, offset, len) {
  const handle = absurdHandles.get(id);
  if (!handle) {
    throw new Error('OPFS handle not found: ' + id);
  }

  const buffer = new Uint8Array(len);
  handle.read(buffer, { at: offset });
  return buffer;
}

export function absurd_opfs_write(id, offset, data) {
  const handle = absurdHandles.get(id);
  if (!handle) {
    throw new Error('OPFS handle not found: ' + id);
  }

  handle.write(data, { at: offset });
}

export function absurd_opfs_flush(id) {
  const handle = absurdHandles.get(id);
  if (handle) {
    handle.flush();
  }
}

export function absurd_opfs_size(id) {
  const handle = absurdHandles.get(id);
  if (!handle) {
    return 0;
  }

  return handle.getSize();
}

export function absurd_opfs_truncate(id, size) {
    const handle = absurdHandles.get(id);
    if (!handle) {
        throw new Error('OPFS handle not found: ' + id);
    }

    handle.truncate(size);
}

export async function absurd_opfs_close(id) {
  const handle = absurdHandles.get(id);
  if (!handle) {
    return;
  }

  const result = handle.close();
  absurdHandles.delete(id);
  if (result && typeof result.then === 'function') {
    await result;
  }
}

export async function absurd_opfs_delete(fileName) {
  try {
    const root = await absurdGetRoot();
    await root.removeEntry(fileName);
  } catch (error) {
    if (!String(error).includes('NotFound')) {
      throw error;
    }
  }
}

export function absurd_opfs_available() {
  return !!(globalThis.navigator &&
    globalThis.navigator.storage &&
    typeof globalThis.navigator.storage.getDirectory === 'function');
}
"#)]
extern "C" {
    #[wasm_bindgen(js_name = absurd_opfs_open, catch)]
    async fn js_opfs_open(file_name: &str, create: bool) -> Result<JsValue, JsValue>;

    #[wasm_bindgen(js_name = absurd_opfs_exists, catch)]
    async fn js_opfs_exists(file_name: &str) -> Result<JsValue, JsValue>;

    #[wasm_bindgen(js_name = absurd_opfs_read)]
    fn js_opfs_read(handle_id: u32, offset: u32, len: u32) -> js_sys::Uint8Array;

    #[wasm_bindgen(js_name = absurd_opfs_write)]
    fn js_opfs_write(handle_id: u32, offset: u32, data: &[u8]);

    #[wasm_bindgen(js_name = absurd_opfs_flush)]
    fn js_opfs_flush(handle_id: u32);

    #[wasm_bindgen(js_name = absurd_opfs_size)]
    fn js_opfs_size(handle_id: u32) -> u32;

    #[wasm_bindgen(js_name = absurd_opfs_truncate)]
    fn js_opfs_truncate(handle_id: u32, size: u32);

    #[wasm_bindgen(js_name = absurd_opfs_close, catch)]
    async fn js_opfs_close(handle_id: u32) -> Result<(), JsValue>;

    #[wasm_bindgen(js_name = absurd_opfs_delete, catch)]
    async fn js_opfs_delete(file_name: &str) -> Result<(), JsValue>;

    #[wasm_bindgen(js_name = absurd_opfs_available)]
    fn js_opfs_available() -> bool;
}

fn sanitize_opfs_component(value: &str) -> String {
    value
        .chars()
        .map(|ch| match ch {
            '/' | '\\' | ':' => '_',
            _ => ch,
        })
        .collect()
}

fn opfs_file_name(db_name: &str) -> String {
    let normalized = normalize_db_name(db_name);
    format!(
        "{}{}",
        sanitize_opfs_component(&normalized),
        OPFS_FILE_SUFFIX
    )
}

fn map_js_error(code: &str, error: JsValue) -> DatabaseError {
    DatabaseError::new(code, &format!("{:?}", error))
}

async fn with_opfs_lock<F, Fut, T>(db_name: &str, f: F) -> Result<T, DatabaseError>
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = Result<T, DatabaseError>>,
{
    let lock_name = format!("opfs:{}", opfs_file_name(db_name));
    crate::storage::export_import_lock::with_lock(&lock_name, || async move {
        f().await
            .map_err(|error| JsValue::from_str(&error.to_string()))
    })
    .await
    .map_err(|error| map_js_error("OPFS_LOCK_ERROR", error))
}

async fn open_handle_for_db(db_name: &str, create: bool) -> Result<u32, DatabaseError> {
    let handle_value = js_opfs_open(&opfs_file_name(db_name), create)
        .await
        .map_err(|error| map_js_error("OPFS_OPEN_ERROR", error))?;

    let handle_id = handle_value.as_f64().ok_or_else(|| {
        DatabaseError::new(
            "OPFS_OPEN_ERROR",
            "OPFS bridge did not return a numeric handle",
        )
    })? as u32;

    Ok(handle_id)
}

async fn opfs_file_exists(db_name: &str) -> Result<bool, DatabaseError> {
    let exists = js_opfs_exists(&opfs_file_name(db_name))
        .await
        .map_err(|error| map_js_error("OPFS_EXISTS_ERROR", error))?;

    exists.as_bool().ok_or_else(|| {
        DatabaseError::new(
            "OPFS_EXISTS_ERROR",
            "OPFS exists bridge did not return a boolean",
        )
    })
}

async fn close_handle(handle_id: u32) {
    let _ = js_opfs_close(handle_id).await;
}

pub fn is_opfs_available() -> bool {
    js_opfs_available()
}

pub async fn persist_to_opfs(
    db_name: &str,
    blocks: Vec<(u64, Vec<u8>)>,
) -> Result<(), DatabaseError> {
    let db_name = db_name.to_string();
    let lock_name = db_name.clone();
    with_opfs_lock(&lock_name, move || async move {
        if blocks.is_empty() {
            return Ok(());
        }

        if !is_opfs_available() {
            return Err(DatabaseError::new(
                "OPFS_UNAVAILABLE",
                "Origin Private File System is not available in this context",
            ));
        }

        let handle_id = open_handle_for_db(&db_name, true).await?;

        for (block_id, data) in blocks {
            let offset = block_id
                .checked_mul(crate::storage::BLOCK_SIZE as u64)
                .ok_or_else(|| DatabaseError::new("OPFS_OFFSET_ERROR", "Block offset overflow"))?;
            let offset = u32::try_from(offset).map_err(|_| {
                DatabaseError::new(
                    "OPFS_OFFSET_ERROR",
                    "Block offset exceeds OPFS bridge range",
                )
            })?;

            let mut padded = vec![0u8; crate::storage::BLOCK_SIZE];
            let copy_len = data.len().min(crate::storage::BLOCK_SIZE);
            padded[..copy_len].copy_from_slice(&data[..copy_len]);
            js_opfs_write(handle_id, offset, &padded);
        }

        js_opfs_flush(handle_id);
        close_handle(handle_id).await;
        Ok(())
    })
    .await
}

pub async fn restore_from_opfs(db_name: &str) -> Result<usize, DatabaseError> {
    let db_name = db_name.to_string();
    let lock_name = db_name.clone();
    with_opfs_lock(&lock_name, move || async move {
        if !is_opfs_available() {
            return Ok(0);
        }

        if !opfs_file_exists(&db_name).await? {
            return Ok(0);
        }

        let handle_id = open_handle_for_db(&db_name, false).await?;
        let size = js_opfs_size(handle_id) as u64;
        if size == 0 {
            close_handle(handle_id).await;
            return Ok(0);
        }

        let mut block_ids = vfs_sync::with_global_allocation_map(|allocation_map| {
            allocation_map
                .borrow()
                .get(&db_name)
                .map(|entries| entries.iter().copied().collect::<Vec<_>>())
                .unwrap_or_default()
        });

        if block_ids.is_empty() {
            block_ids = vfs_sync::with_global_metadata(|metadata_map| {
                metadata_map
                    .borrow()
                    .get(&db_name)
                    .map(|entries| entries.keys().copied().collect::<Vec<_>>())
                    .unwrap_or_default()
            });
        }

        if block_ids.is_empty() {
            let total_blocks = size / crate::storage::BLOCK_SIZE as u64;
            block_ids = (0..total_blocks).collect();
        }

        block_ids.sort_unstable();
        block_ids.dedup();

        let mut restored_blocks = Vec::new();
        for block_id in block_ids {
            let offset = block_id
                .checked_mul(crate::storage::BLOCK_SIZE as u64)
                .ok_or_else(|| DatabaseError::new("OPFS_OFFSET_ERROR", "Block offset overflow"))?;

            if offset >= size {
                continue;
            }

            let buffer = js_opfs_read(handle_id, offset as u32, crate::storage::BLOCK_SIZE as u32);
            let mut data = vec![0u8; crate::storage::BLOCK_SIZE];
            buffer.copy_to(&mut data);
            restored_blocks.push((block_id, data));
        }

        close_handle(handle_id).await;

        if restored_blocks.is_empty() {
            return Ok(0);
        }

        let restored_count = restored_blocks.len();
        let restored_commit = vfs_sync::with_global_commit_marker(|commit_map| {
            commit_map.borrow().get(&db_name).copied()
        })
        .unwrap_or(0)
        .max(1);
        let restored_version = u32::try_from(restored_commit).unwrap_or(u32::MAX);
        let restored_at_ms = js_sys::Date::now() as u64;

        vfs_sync::with_global_storage(|storage_map| {
            let mut storage_map = storage_map.borrow_mut();
            let db_storage = storage_map
                .entry(db_name.to_string())
                .or_insert_with(std::collections::HashMap::new);
            for (block_id, data) in &restored_blocks {
                db_storage.insert(*block_id, data.clone());
            }
        });

        vfs_sync::with_global_allocation_map(|allocation_map| {
            let mut allocation_map = allocation_map.borrow_mut();
            let allocations = allocation_map
                .entry(db_name.to_string())
                .or_insert_with(std::collections::HashSet::new);
            for (block_id, _) in &restored_blocks {
                allocations.insert(*block_id);
            }
        });

        vfs_sync::with_global_metadata(|metadata_map| {
            let mut metadata_map = metadata_map.borrow_mut();
            let db_metadata = metadata_map
                .entry(db_name.to_string())
                .or_insert_with(std::collections::HashMap::new);

            for (block_id, data) in &restored_blocks {
                db_metadata.insert(
                    *block_id,
                    BlockMetadataPersist {
                        checksum: ChecksumManager::compute_checksum_with(
                            data,
                            ChecksumAlgorithm::FastHash,
                        ),
                        last_modified_ms: restored_at_ms,
                        version: restored_version,
                        algo: ChecksumAlgorithm::FastHash,
                    },
                );
            }
        });

        vfs_sync::with_global_commit_marker(|commit_map| {
            commit_map
                .borrow_mut()
                .insert(db_name.to_string(), restored_commit);
        });

        Ok(restored_count)
    })
    .await
}

pub async fn reconcile_opfs_with_metadata(
    db_name: &str,
    valid_block_ids: &[u64],
) -> Result<(), DatabaseError> {
    let db_name = db_name.to_string();
    let valid_block_ids = valid_block_ids.to_vec();
    let lock_name = db_name.clone();
    with_opfs_lock(&lock_name, move || async move {
        if !is_opfs_available() || valid_block_ids.is_empty() {
            return Ok(());
        }

        if !opfs_file_exists(&db_name).await? {
            return Ok(());
        }

        let handle_id = open_handle_for_db(&db_name, false).await?;
        let current_size = js_opfs_size(handle_id) as u64;
        if current_size == 0 {
            close_handle(handle_id).await;
            return Ok(());
        }

        let block_size = crate::storage::BLOCK_SIZE as u64;
        let total_blocks = (current_size + block_size - 1) / block_size;
        let valid_blocks = valid_block_ids
            .iter()
            .copied()
            .collect::<std::collections::HashSet<_>>();
        let zero_block = vec![0u8; crate::storage::BLOCK_SIZE];

        for block_id in 0..total_blocks {
            if valid_blocks.contains(&block_id) {
                continue;
            }

            let offset = block_id
                .checked_mul(block_size)
                .ok_or_else(|| DatabaseError::new("OPFS_OFFSET_ERROR", "Block offset overflow"))?;
            let offset = u32::try_from(offset).map_err(|_| {
                DatabaseError::new(
                    "OPFS_OFFSET_ERROR",
                    "Block offset exceeds OPFS bridge range",
                )
            })?;
            js_opfs_write(handle_id, offset, &zero_block);
        }

        let max_block_id = valid_block_ids.iter().copied().max().unwrap_or(0);
        let expected_size = max_block_id
            .checked_add(1)
            .and_then(|block_count| block_count.checked_mul(block_size))
            .ok_or_else(|| DatabaseError::new("OPFS_SIZE_ERROR", "Expected OPFS size overflow"))?;

        if expected_size < current_size {
            let expected_size = u32::try_from(expected_size).map_err(|_| {
                DatabaseError::new(
                    "OPFS_SIZE_ERROR",
                    "Expected OPFS size exceeds OPFS bridge range",
                )
            })?;
            js_opfs_truncate(handle_id, expected_size);
        }

        js_opfs_flush(handle_id);
        close_handle(handle_id).await;
        Ok(())
    })
    .await
}

pub async fn delete_blocks_from_opfs(
    db_name: &str,
    block_ids: &[u64],
) -> Result<(), DatabaseError> {
    let db_name = db_name.to_string();
    let block_ids = block_ids.to_vec();
    let lock_name = db_name.clone();
    with_opfs_lock(&lock_name, move || async move {
        if block_ids.is_empty() || !is_opfs_available() {
            return Ok(());
        }

        if !opfs_file_exists(&db_name).await? {
            return Ok(());
        }

        let handle_id = open_handle_for_db(&db_name, false).await?;
        let zero_block = vec![0u8; crate::storage::BLOCK_SIZE];

        for block_id in block_ids {
            let offset = block_id
                .checked_mul(crate::storage::BLOCK_SIZE as u64)
                .ok_or_else(|| DatabaseError::new("OPFS_OFFSET_ERROR", "Block offset overflow"))?;
            let offset = u32::try_from(offset).map_err(|_| {
                DatabaseError::new(
                    "OPFS_OFFSET_ERROR",
                    "Block offset exceeds OPFS bridge range",
                )
            })?;
            js_opfs_write(handle_id, offset, &zero_block);
        }

        js_opfs_flush(handle_id);
        close_handle(handle_id).await;
        Ok(())
    })
    .await
}

pub async fn delete_all_from_opfs(db_name: &str) -> Result<(), DatabaseError> {
    let db_name = db_name.to_string();
    let lock_name = db_name.clone();
    with_opfs_lock(&lock_name, move || async move {
        if !is_opfs_available() {
            return Ok(());
        }

        js_opfs_delete(&opfs_file_name(&db_name))
            .await
            .map_err(|error| map_js_error("OPFS_DELETE_ERROR", error))
    })
    .await
}