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: respect access mode and file open flags #1962

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

Conversation

sumimakito
Copy link

@sumimakito sumimakito commented Feb 22, 2025

Previous PR: #1960 (it can be closed if this PR looks better as this PR addresses more issues for both OPFS and Node FS)

Summary

  • accessMode in db.open(...) will be respected and empty database files (and directories in OPFS) will no longer be created in the read-only mode
  • Fix file open flag used in Node FS and
  • Fix the return value of openFile in the Node runtime as it should return nullptr when the file does not exist and the FILE_FLAGS_NULL_IF_NOT_EXISTS is set
  • Add missing flags in FileFlags to the JavaScript bindings
  • Add tests for Node FS and OPFS
  • Update patch for src/storage/storage_manager.cpp in the duckdb submodule

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

Copy link
Contributor

@e1arikawa e1arikawa Feb 28, 2025

Choose a reason for hiding this comment

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

@amiller-gh
I believe it's fine for this PR to focus only on a single window.
I think we should first handle single-window support before addressing multiple windows.

Because the mode argument of the createSyncAccessHandle method was introduced to specify the file access mode. However, this argument has not yet been enabled in lib.webworker.d.ts (the Web Worker type definition file). As a result, using this argument in TypeScript may cause a type error. To resolve this issue, you need to either wait for the type definition file to be updated or extend the type definitions within your project.

And, I plan to create a PR for handling multiple windows, so this PR needs to be merged for that.

@e1arikawa
Copy link
Contributor

e1arikawa commented Feb 28, 2025

@sumimakito
I believe it's fine for this PR to focus only on a single window.
I think we should first handle single-window support before addressing multiple windows.

Because the mode argument of the createSyncAccessHandle method was introduced to specify the file access mode. However, this argument has not yet been enabled in lib.webworker.d.ts (the Web Worker type definition file). As a result, using this argument in TypeScript may cause a type error. To resolve this issue, you need to either wait for the type definition file to be updated or extend the type definitions within your project.

And, I plan to create a PR for handling multiple windows, so this PR needs to be merged for that.

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.

3 participants