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

fix(bindings): handle accessMode in runtime bindings with OPFS #1960

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sumimakito
Copy link

@sumimakito sumimakito commented Feb 19, 2025

This is an amazing project with many great works. Thank you

Context

During my recent usage with duckdb-wasm with the recent OPFS support done by @e1arikawa (Thank you for the cool work!), I encountered an issue where the file/directory handles were always created even when I opened the database with read-only options like:

db.open({
    path: 'opfs://not_yet_existing.db',
    accessMode: DuckDBAccessMode.READ_ONLY, // We should NOT create any files/directories
})

Summary

Going deeper, it seems we didn't handle the accessMode in helper functions like prepareFileHandles. Therefore, in this pull request, I tried to add the missing parameters to these functions as optional ones and added several test cases for open operations as such with different accessMode in OPFS.

console.debug(`File ${path} does not exists yet, creating...`);
return dirHandle.getFileHandle(fileName, { create: true });
}
console.debug(`File ${path} does not exists, aborting as we are in read-only mode`);

Choose a reason for hiding this comment

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

Five lines below this, where we call createSyncAccessHandle, we can pass a new optional mode to OPFS to further enforce the access mode: https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle#mode

If the access mode is passed to createSyncAccessHandle, it would allow read-only connections to be opened simultaneously (for example when using the same app in multiple tabs). A big win for duckdb-wasm + OPFS.

The option is only implemented in Chrome and Edge right now, but issues exist to implement in both Webkit and Firefox:
https://bugs.webkit.org/show_bug.cgi?id=283959
https://bugzilla.mozilla.org/show_bug.cgi?id=1949462

And tests have already been added to the Web Platform Tests dashboard:
https://wpt.fyi/results/fs/FileSystemFileHandle-sync-access-handle-lock-modes.https.tentative.worker.html?label=experimental&label=master&aligned

Choose a reason for hiding this comment

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

Ah, just noticed you started a new PR for this! Will comment over there instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants