Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ indent_size = 2

[*.toml]
indent_size = 2

[Makefile]
indent_style = tab
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "daily"
open-pull-requests-limit: 10
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "rust-toolchain"
schedule:
interval: "daily"
6 changes: 2 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ jobs:
matrix:
command:
- cargo fmt --all -- --check
- cargo check --all-targets
- cargo check --target wasm32-unknown-unknown
- cargo clippy --all-targets
- cargo clippy --target wasm32-unknown-unknown
- cargo check --target wasm32-unknown-unknown --all-targets
- cargo clippy --target wasm32-unknown-unknown --all-targets
steps:
- uses: actions/checkout@v5
- uses: wireapp/core-crypto/.github/actions/setup-and-cache-rust@main
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
}
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ version = "0.1.0"
edition = "2024"
description = "Implements a `StorageBackend` which delegates to OPFS"
license = "GPL-3.0-only"
repository = "https://github.com/wireapp/redb-opfs"

[lib]
crate-type = ["lib", "cdylib"]

[dependencies]
async-lock = "3.4.1"
derive_more = { version = "2.0.1", features = ["display", "error"] }
futures-lite = "2.6"
# Temporary! Should be next released version after https://github.com/cberner/redb/pull/1065 is merged
redb = { git = "https://github.com/coriolinus/redb", branch = "prgn/feat/wasm32-unknown-unknown-support", version = "3.0" }
tokio-fs-ext = "0.6.3"
wasm-bindgen = "0.2.101"
wasm-bindgen-futures = "0.4.51"

[profile.release]
lto = true
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
RUST_RS_FILES := $(shell find . -type f -name '*.rs' 2>/dev/null | LC_ALL=C sort)
RUST_SOURCES := Cargo.toml Cargo.lock $(RUST_RS_FILES)
WASM_OUT := ts/gen/redb-opfs_bg.wasm
DTS_OUT := ts/gen/redb-opfs.d.ts
JS_OUT := ts/gen/redb-opfs.js

# If RELEASE is nonempty, build in release mode
# Otherwise build in dev mode, which is much faster
WASM_BUILD_ARGS := $(if $(RELEASE),,--dev)

Cargo.lock: Cargo.toml
cargo check

$(JS_OUT) $(DTS_OUT) $(WASM_OUT) &: $(RUST_SOURCES)
wasm-pack build \
--locked \
--no-pack \
--out-dir ts/gen \
--out-name redb-opfs \
--mode normal \
--target web \
$(WASM_BUILD_ARGS)

# human name for building wasm
.PHONY: wasm-build
wasm-build: $(WASM_OUT)

.PHONY: web-worker-example
web-worker-example: $(WASM_OUT)
cd examples/web-worker && \
timeout 1s bun run src/index.ts
69 changes: 57 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `redb-opfs`: Implements a `StorageBackend` which delegates to OPFS

This allows compilation and deployment on `wasm32-unknown-unknown`.
This allows deployment on `wasm32-unknown-unknown`.

> [!WARNING]
> The contents of this README are a statement of intent, not an accurate reflection of the current state of the project.
Expand All @@ -9,22 +9,67 @@ This allows compilation and deployment on `wasm32-unknown-unknown`.

## Usage

- Add this dependency to your project:
It is important to understand that Redb's [`StorageBackend` interface](https://docs.rs/redb/latest/redb/trait.StorageBackend.html) is fundamentally synchronous,
and OPFS is fundamentally asynchronous.
There's a simple way to tie these two things together--[`block_on`](https://docs.rs/futures-lite/latest/futures_lite/future/fn.block_on.html)--but that method
is illegal on the main thread, in order not to block the UI.

```sh
cargo add redb-opfs
```
> [!IMPORTANT]
> The `OpfsBackend` instance **must** run on a web worker.

- Explicitly choose this backend when initializing your `Database`:
This gives rise to two use cases.

```rust
use redb_opfs::OpfsStorageBackend;
### Your Rust code is already running in a web worker

let database = redb::Builder::new()
.create_with_backend(OpfsStorageBackend::new("my-db"))?;
```
This case is nice and simple; everything stays within Rust. Just explicitly choose this backend when initializing your `Database`:

```rust
use redb_opfs::OpfsBackend;

let database = redb::Builder::new()
.create_with_backend(OpfsBackend::new("my-db")?)?;
```

### Your Rust code is running in the main thread

> [!NOTE]
> Running in this configuration introduces unavoidable performance penalties;
> when possible, you should prefer to run all your Rust code within a web worker to avoid these.

In this case we need to instantiate the `OpfsBackend` on a web worker and then instantiate the handle on the main thread.

You'll want to use the `worker-shim.js` worker file to initialize the worker, and then hand that worker to the `OpfsBackendHandle`

```js
import { WorkerHandle } from "./redb-opfs";

const redbOpfsWorker = new Worker("worker-shim.js");
const workerHandle = WorkerHandle(redbOpfsWorker);

// now pass that handle to your rust code, using a mechanism of your choice.
```

As you're writing your own Rust anyway, you have your own means of getting the handle into your code from there.
To keep life simple, there exists `impl TryFrom<JsValue> for WorkerHandle`.

Once you have that, usage is fairly simple:

```rust
use redb_opfs::WorkerHandle;

let worker_handle = WorkerHandle::try_from(my_js_value)?;
let database = redb::Builder::new()
.create_with_backend(worker_hanndle)?;
```

## Building

### Prerequisites for WASM

- [`wasm-bindgen` cli](https://github.com/wasm-bindgen/wasm-bindgen?tab=readme-ov-file#install-wasm-bindgen-cli)
- [wasm-pack](https://github.com/drager/wasm-pack)
- [GNU Make](https://www.gnu.org/software/make/)

- Go nuts!

## License

Expand Down
175 changes: 175 additions & 0 deletions examples/web-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
19 changes: 19 additions & 0 deletions examples/web-worker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# run-as-web-worker

Demonstrate simple usage of the OPFS functionality within a web worker.

## Setup

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.2.1. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Loading
Loading