Add BLite.Wasm: OPFS & IndexedDB storage backends for browser WASM (Issues 1-4)#63
Merged
mrdevrobot merged 2 commits intomainfrom Apr 17, 2026
Merged
Conversation
…ry API Implements Issues 1-4 from WASM_SUPPORT.md: - OpfsPageStorage: OPFS SyncAccessHandle-based page I/O via JSImport - IndexedDbPageStorage: IndexedDB async page I/O with base64 marshalling - OpfsWriteAheadLog: OPFS-backed WAL for crash recovery - IndexedDbWriteAheadLog: IndexedDB-backed WAL for crash recovery - BLiteWasm factory: auto-selects OPFS → IndexedDB → InMemory - AddBLiteWasm Blazor DI extension - BLiteEngine.CreateFromStorage public factory method - JavaScript interop modules (blite-opfs.mjs, blite-indexeddb.mjs) - Test for CreateFromStorage in InMemoryStorageTests Agent-Logs-Url: https://github.com/EntglDb/BLite/sessions/d18510cd-be4e-47fe-a1ef-788758aafe5a Co-authored-by: mrdevrobot <12503462+mrdevrobot@users.noreply.github.com>
Agent-Logs-Url: https://github.com/EntglDb/BLite/sessions/d18510cd-be4e-47fe-a1ef-788758aafe5a Co-authored-by: mrdevrobot <12503462+mrdevrobot@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
mrdevrobot
April 16, 2026 23:22
View session
mrdevrobot
approved these changes
Apr 17, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new BLite.Wasm package intended to enable BLite persistence in browser WASM via OPFS and IndexedDB backends, and exposes a core factory (BLiteEngine.CreateFromStorage) to allow external storage engines to plug into BLite.
Changes:
- Introduces
BLite.Wasmproject (net10.0-browser) with OPFS/IndexedDBIPageStorage+IWriteAheadLogimplementations and aBLiteWasm.CreateAsyncfactory. - Adds JS interop modules (
blite-opfs.mjs,blite-indexeddb.mjs) and corresponding[JSImport]bridges. - Adds
BLiteEngine.CreateFromStorage(...)plus a small integration test.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 15 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/BLite.Tests/InMemoryStorageTests.cs | Adds integration test for BLiteEngine.CreateFromStorage. |
| src/BLite.Wasm/wwwroot/blite-opfs.mjs | OPFS JS module: open/read/write/flush/truncate/close + availability probe. |
| src/BLite.Wasm/wwwroot/blite-indexeddb.mjs | IndexedDB JS module: page storage, meta, and WAL support. |
| src/BLite.Wasm/Transactions/OpfsWriteAheadLog.cs | OPFS-backed WAL implementation. |
| src/BLite.Wasm/Transactions/IndexedDbWriteAheadLog.cs | IndexedDB-backed WAL implementation. |
| src/BLite.Wasm/Storage/OpfsPageStorage.cs | OPFS page storage implementation. |
| src/BLite.Wasm/Storage/IndexedDbPageStorage.cs | IndexedDB page storage implementation. |
| src/BLite.Wasm/Interop/OpfsInterop.cs | [JSImport] bridge for OPFS module. |
| src/BLite.Wasm/Interop/IndexedDbInterop.cs | [JSImport] bridge for IndexedDB module. |
| src/BLite.Wasm/BLiteWasmServiceExtensions.cs | Blazor DI registration helper (AddBLiteWasm). |
| src/BLite.Wasm/BLiteWasm.cs | WASM factory (CreateAsync) + backend selection logic. |
| src/BLite.Wasm/BLite.Wasm.csproj | New package project definition. |
| src/BLite.Core/BLiteEngine.cs | Adds CreateFromStorage(StorageEngine, BLiteKvOptions?) factory method. |
| WASM_SUPPORT.md | Updates roadmap to mark Issues 1–4 as implemented and documents new APIs. |
| BLite.slnx | Adds BLite.Wasm to the solution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+38
to
+46
| /// <param name="dbName">Logical database name. The WAL file will be <c>{dbName}.wal</c>.</param> | ||
| /// <param name="writeTimeoutMs">Timeout in milliseconds for acquiring the internal lock.</param> | ||
| public OpfsWriteAheadLog(string dbName, int writeTimeoutMs = 5_000) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(dbName)) | ||
| throw new ArgumentException("Database name must not be null or empty.", nameof(dbName)); | ||
|
|
||
| _dbName = dbName + ".wal"; | ||
| _writeTimeoutMs = writeTimeoutMs; |
| public uint NextPageId => _nextPageId; | ||
|
|
||
| /// <summary>Returns <c>true</c> if IndexedDB is available in the current browser context.</summary> | ||
| public static bool IsAvailable() => IndexedDbInterop.IsAvailable(); |
Comment on lines
+56
to
+58
| // Open with pageSize=1 — we manage our own offsets. | ||
| await OpfsInterop.OpenAsync(_dbName, 1); | ||
| _opened = true; |
Comment on lines
+15
to
+26
| internal static partial class IndexedDbInterop | ||
| { | ||
| private const string ModuleName = "./blite-indexeddb.mjs"; | ||
|
|
||
| // ─── Page storage ──────────────────────────────────────────────────────── | ||
|
|
||
| [JSImport("idbOpen", ModuleName)] | ||
| internal static partial Task<double> OpenAsync(string dbName); | ||
|
|
||
| [JSImport("idbReadPage", ModuleName)] | ||
| internal static partial Task<string> ReadPageAsync(string dbName, int pageId, int pageSize); | ||
|
|
Comment on lines
+170
to
+194
| public uint AllocatePage() | ||
| { | ||
| ThrowIfDisposed(); | ||
| ThrowIfNotOpened(); | ||
|
|
||
| lock (_allocationLock) | ||
| { | ||
| if (_freeList.Count > 0) | ||
| return _freeList.Pop(); | ||
|
|
||
| return _nextPageId++; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void FreePage(uint pageId) | ||
| { | ||
| ThrowIfDisposed(); | ||
| if (pageId == 0) | ||
| throw new InvalidOperationException("Cannot free the header page (page 0)."); | ||
|
|
||
| lock (_allocationLock) | ||
| { | ||
| _freeList.Push(pageId); | ||
| } |
| public uint NextPageId => _nextPageId; | ||
|
|
||
| /// <summary>Returns <c>true</c> if the OPFS SyncAccessHandle API is available in the current browser context.</summary> | ||
| public static bool IsAvailable() => OpfsInterop.IsAvailable(); |
Comment on lines
+232
to
+233
| var buffer = new byte[fileSize]; | ||
| OpfsInterop.ReadPage(_dbName, 0, buffer.AsSpan()); |
Comment on lines
+103
to
+108
| * @returns {boolean} | ||
| */ | ||
| export function opfsIsAvailable() { | ||
| return typeof navigator !== "undefined" | ||
| && typeof navigator.storage !== "undefined" | ||
| && typeof navigator.storage.getDirectory === "function"; |
Comment on lines
+1
to
+21
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0-browser</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| <TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
|
|
||
| <PackageId>BLite.Wasm</PackageId> | ||
| <Version>4.3.1</Version> | ||
| <Authors>BLite Team</Authors> | ||
| <Description>BLite browser storage backends for .NET WASM — OPFS and IndexedDB page storage and WAL implementations</Description> | ||
| <PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
| <PackageReadmeFile>README.md</PackageReadmeFile> | ||
| <PackageIcon>icon.png</PackageIcon> | ||
| <RepositoryUrl>https://github.com/EntglDb/BLite</RepositoryUrl> | ||
| <PackageTags>database;embedded;bson;nosql;wasm;blazor;browser;opfs;indexeddb</PackageTags> | ||
| <GeneratePackageOnBuild>True</GeneratePackageOnBuild> | ||
| </PropertyGroup> |
Comment on lines
+159
to
+187
| public uint AllocatePage() | ||
| { | ||
| ThrowIfDisposed(); | ||
| ThrowIfNotOpened(); | ||
|
|
||
| lock (_allocationLock) | ||
| { | ||
| if (_freeList.Count > 0) | ||
| return _freeList.Pop(); | ||
|
|
||
| var id = _nextPageId++; | ||
| // Persist the counter so it survives browser restarts. | ||
| IndexedDbInterop.SaveNextPageIdAsync(_dbName, (int)_nextPageId).GetAwaiter().GetResult(); | ||
| return id; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void FreePage(uint pageId) | ||
| { | ||
| ThrowIfDisposed(); | ||
| if (pageId == 0) | ||
| throw new InvalidOperationException("Cannot free the header page (page 0)."); | ||
|
|
||
| lock (_allocationLock) | ||
| { | ||
| _freeList.Push(pageId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Implements Issues 1-4 from
WASM_SUPPORT.md, delivering a completeBLite.WasmNuGet package that enables BLite to run with persistent storage in browser WASM environments.New
BLite.WasmPackageProject:
src/BLite.Wasm/BLite.Wasm.csproj— targetsnet10.0-browserStorage Backends
OpfsPageStorageIndexedDbPageStorageMemoryPageStorage(existing)WAL Implementations
OpfsWriteAheadLog.walOPFS fileIndexedDbWriteAheadLogFactory API
Changes to BLite.Core
BLiteEngine.CreateFromStorage(StorageEngine, BLiteKvOptions?)public factory method for external projects to create engines from custom storage backends.Architecture
[JSImport]with synchronousSpan<byte>marshalling for high-perf page I/O viaFileSystemSyncAccessHandle[JSImport]with base64 string encoding for async JS interop (required because[JSImport]does not supportbyte[]onTask-returning methods)IPageStorage/IWriteAheadLoginterfaces, plugging directly into theStorageEnginewith zero changes to the core engineFiles Added
src/BLite.Wasm/BLite.Wasm.csprojsrc/BLite.Wasm/BLiteWasm.cssrc/BLite.Wasm/BLiteWasmServiceExtensions.cssrc/BLite.Wasm/Storage/OpfsPageStorage.cssrc/BLite.Wasm/Storage/IndexedDbPageStorage.cssrc/BLite.Wasm/Transactions/OpfsWriteAheadLog.cssrc/BLite.Wasm/Transactions/IndexedDbWriteAheadLog.cssrc/BLite.Wasm/Interop/OpfsInterop.cssrc/BLite.Wasm/Interop/IndexedDbInterop.cssrc/BLite.Wasm/wwwroot/blite-opfs.mjssrc/BLite.Wasm/wwwroot/blite-indexeddb.mjsTesting
CreateFromStorage_InsertAndFind_Workstest for the new factory methodRemaining Work
Issue 5 (Blazor WASM sample + docs) is the only remaining item from
WASM_SUPPORT.md.