Skip to content
Open
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
4 changes: 4 additions & 0 deletions core/edge/opfs_wasm32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ version.workspace = true
[lib]
crate-type = ["cdylib"]

[features]
# Run the tests in a dedicated web worker instead of the main browser thread.
worker = []

[dependencies]
futures = { workspace = true, features = ["std", "async-await"] }
opendal = { path = "../..", default-features = false, features = [
Expand Down
11 changes: 11 additions & 0 deletions core/edge/opfs_wasm32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ NOTE:
- OPFS requires a browser context (no Node.js support).
- Headless Chrome may not work for OPFS tests.

Some code execution path differ whether it's running in the main thread or in a web worker, use the 2 following commands to test both cases:

### Main thread
```shell
wasm-pack test --chrome
```

### Web worker

To run the same tests in a dedicated web worker:

```shell
wasm-pack test --chrome -- --features worker
```
7 changes: 4 additions & 3 deletions core/edge/opfs_wasm32/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ mod tests {
};
}

#[cfg(not(feature = "worker"))]
wasm_bindgen_test_configure!(run_in_browser);
#[cfg(feature = "worker")]
wasm_bindgen_test_configure!(run_in_dedicated_worker);

fn new_operator() -> Operator {
Operator::from_config(OpfsConfig::default())
.expect("failed to create opfs operator")
.finish()
Operator::from_config(OpfsConfig::default()).expect("failed to create opfs operator")
}

#[wasm_bindgen_test]
Expand Down
2 changes: 2 additions & 0 deletions core/services/opfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ web-sys = { version = "0.3.77", features = [
"WriteParams",
"StorageManager",
"Window",
"WorkerGlobalScope",
"WorkerNavigator",
] }
26 changes: 23 additions & 3 deletions core/services/opfs/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,34 @@ mod utils {
use web_sys::FileSystemFileHandle;
use web_sys::FileSystemGetDirectoryOptions;
use web_sys::FileSystemGetFileOptions;
use web_sys::window;
use web_sys::StorageManager;
use web_sys::Window;
use web_sys::WorkerGlobalScope;

use crate::core::*;

/// Get the storage manager from the current global scope.
///
/// OPFS is available both on the main thread (`Window`) and in workers
/// (`WorkerGlobalScope`), so `web_sys::window()` cannot be used: it is
/// `None` inside a worker.
fn storage_manager() -> Result<StorageManager> {
let global = js_sys::global();
if let Some(window) = global.dyn_ref::<Window>() {
Ok(window.navigator().storage())
} else if let Some(worker) = global.dyn_ref::<WorkerGlobalScope>() {
Ok(worker.navigator().storage())
} else {
Err(Error::new(
ErrorKind::Unsupported,
"OPFS requires a window or worker global scope",
))
}
}

/// Get the OPFS root directory handle.
pub(crate) async fn get_root_directory_handle() -> Result<FileSystemDirectoryHandle> {
let navigator = window().unwrap().navigator();
let storage_manager = navigator.storage();
let storage_manager = storage_manager()?;
// This may fail if not secure (not: HTTPS or localhost)
JsFuture::from(storage_manager.get_directory())
.await
Expand Down
Loading