feat: enabling OPFS for client-wasm #300
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is not a finished PR, it's to start a discussion so I can better understand how codeowners would want this work done, if at all
What and why I've done this
How this works
browser.runtime.getURL('assets/sqlite3.wasm)
to get the location of the sqlite WASMinitSqlite3
function is within the context of the client and passes the path as thelocateFile
arg into thesqlite3InitModule
that already exists within the original clientcreateClient
as we would normally. However, under the hood, we've changed two lines within the custom client, basically using thissqlite3.oo1.OpfsDb
instead of thissqlite3.oo1.DB
Problems and reasons for this approach
assets/sqlite3.wasm
path (yes, it's possible to bundle the WASM file as a string, even if it is very inefficient, but I believe I ended up with CSP restrictions set by the browser that cannot be overridden)@libsql/client-wasm
needs the declared and initialisedsqlite3
WASM file within thewasm.js
file that declares the client and then directly referencessqlite3
and uses it after it's been initialisedlocateFile
in it's params along with the url to import the location of the wasm file (even though this is not directly within the wasm client and is in thelibsql-wasm-experimental
dependencies files)browser.runtime.getURL
opfs-proxy
one, no matter what I triedlibsql-wasm-experimental
module directly into my code, as this means the web workers are technically my bundled code and the browser will let them run without issue when the module, that's also now within my code, tries to call themopfsDb
rather than justDB
DB
DB
toopfsDb
libsql-wasm-experimental
shows, this is just regularsqlite-wasm
, OPFS works like a charm, the only issue I could really see was that obviously none of the turso functionality was actually available, e.g.sync
being disabledWhy this is important for the broader LibSQL project
Next Steps