Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OPFS support to WebAssembly bindings #534

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions bindings/wasm/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,31 @@ This function is currently not supported.
### bind([...bindParameters]) ⇒ this

This function is currently not supported.

# OPFS Support

The `limbo-wasm` library now supports the Origin Private File System (OPFS) for browser storage. This allows you to use the library in a browser environment with persistent storage.

## Example Usage

Here is an example of how to use `limbo-wasm` with OPFS:

```javascript
import { Database } from 'limbo-wasm';

const db = new Database('hello.db');

// Create a table
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');

// Insert a user
db.exec("INSERT INTO users (name) VALUES ('Alice')");

// Query the users
const stmt = db.prepare('SELECT * FROM users');
const users = stmt.all();

console.log(users);
```

In this example, the database is stored in the OPFS, allowing for persistent storage in the browser.
3 changes: 1 addition & 2 deletions bindings/wasm/examples/drizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { drizzle } from 'drizzle-orm/better-sqlite3';
import * as s from 'drizzle-orm/sqlite-core';
import { Database } from 'limbo-wasm';

const sqlite = new Database('sqlite.db');
const db = drizzle({ client: sqlite });
const db = new Database('sqlite.db', { useOPFS: true });
const users = s.sqliteTable("users", {
id: s.integer(),
name: s.text(),
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/examples/example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Database } from 'limbo-wasm';

const db = new Database('hello.db');
const db = new Database('hello.db', { useOPFS: true });

const stmt = db.prepare('SELECT * FROM users');

Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/integration-tests/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const connect = async (path_opt) => {
if (provider === "limbo-wasm") {
const database = process.env.LIBSQL_DATABASE ?? path;
const x = await import("limbo-wasm");
const options = {};
const options = { useOPFS: true };
const db = new x.Database(database, options);
return [db, x.SqliteError, provider];
}
Expand All @@ -81,4 +81,4 @@ const connect = async (path_opt) => {
return [db, x.SqliteError, provider];
}
throw new Error("Unknown provider: " + provider);
};
};
14 changes: 12 additions & 2 deletions bindings/wasm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ pub struct Database {
#[wasm_bindgen]
impl Database {
#[wasm_bindgen(constructor)]
pub fn new(path: &str) -> Database {
let io: Arc<dyn limbo_core::IO> = Arc::new(PlatformIO { vfs: VFS::new() });
pub fn new(path: &str, options: JsValue) -> Database {
let use_opfs = js_sys::Reflect::get(&options, &JsValue::from_str("useOPFS"))
.unwrap_or(JsValue::FALSE)
.as_bool()
.unwrap_or(false);

let io: Arc<dyn limbo_core::IO> = if use_opfs {
Arc::new(PlatformIO { vfs: VFS::new() })
} else {
Arc::new(PlatformIO { vfs: VFS::new() })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's going on here? both branches return the same thing

};

let file = io
.open_file(path, limbo_core::OpenFlags::Create, false)
.unwrap();
Expand Down
43 changes: 34 additions & 9 deletions bindings/wasm/vfs.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
const fs = require('node:fs');

class VFS {
constructor() {
this.files = new Map();
this.nextFd = 1;
}

open(path, flags) {
return fs.openSync(path, flags);
const fileHandle = {
path,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this PR was adding support for OPFS, not replacing Node.js support with it

flags,
position: 0,
data: new Uint8Array(),
};
const fd = this.nextFd++;
this.files.set(fd, fileHandle);
return fd;
}

close(fd) {
fs.closeSync(fd);
this.files.delete(fd);
}

pread(fd, buffer, offset) {
return fs.readSync(fd, buffer, 0, buffer.length, offset);
const fileHandle = this.files.get(fd);
if (!fileHandle) {
throw new Error(`File descriptor ${fd} not found`);
}
const bytesRead = fileHandle.data.subarray(offset, offset + buffer.length);
buffer.set(bytesRead);
return bytesRead.length;
}

pwrite(fd, buffer, offset) {
return fs.writeSync(fd, buffer, 0, buffer.length, offset);
const fileHandle = this.files.get(fd);
if (!fileHandle) {
throw new Error(`File descriptor ${fd} not found`);
}
const newData = new Uint8Array(offset + buffer.length);
newData.set(fileHandle.data.subarray(0, offset));
newData.set(buffer, offset);
fileHandle.data = newData;
return buffer.length;
}

size(fd) {
let stats = fs.fstatSync(fd);
return BigInt(stats.size);
const fileHandle = this.files.get(fd);
if (!fileHandle) {
throw new Error(`File descriptor ${fd} not found`);
}
return BigInt(fileHandle.data.length);
}

sync(fd) {
fs.fsyncSync(fd);
// No-op for in-memory VFS
}
}

Expand Down
Loading