Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/wa-sqlite-opfs-path-capacity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@treecrdt/wa-sqlite': patch
---

Allow OPFS database paths longer than wa-sqlite's 64-byte VFS default.
4 changes: 3 additions & 1 deletion packages/treecrdt-wa-sqlite/e2e/tests/lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ test.describe('browser OPFS lifecycle', () => {
page.on('console', (msg) => console.log(`[page][${msg.type()}] ${msg.text()}`));

const suffix = `${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 8)}`;
const filename = `/lifecycle-${'p'.repeat(48)}-${scenario.runtime}-${suffix}.db`;
expect(new TextEncoder().encode(`${filename}-journal`).byteLength).toBeGreaterThan(64);
const opts = {
docId: `lifecycle-${scenario.runtime}-${suffix}`,
filename: `/lifecycle-${scenario.runtime}-${suffix}.db`,
filename,
runtime: scenario.runtime,
};

Expand Down
14 changes: 12 additions & 2 deletions packages/treecrdt-wa-sqlite/src/opfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ export type OpfsVfsOptions = {
kind?: OpfsVfsKind;
};

const OPFS_MAX_PATHNAME = 512;

function withOpfsPathCapacity(vfs: any): any {
// SQLite appends sidecar suffixes such as "-journal" after checking this capacity.
vfs.mxPathname = Math.max(vfs.mxPathname ?? 0, OPFS_MAX_PATHNAME);
return vfs;
}

/**
* Create an OPFS VFS bound to the provided wa-sqlite Module.
* Uses local copies of wa-sqlite's example VFS implementations to avoid reaching into vendor paths.
Expand All @@ -133,12 +141,14 @@ export async function createOpfsVfs(module: any, opts: OpfsVfsOptions = {}): Pro
if (opts.kind === 'any-context') {
// @ts-ignore vendored module lacks type declarations
const { OPFSAnyContextVFS } = await import('./vendor/OPFSAnyContextVFS.js');
return OPFSAnyContextVFS.create(name, module, { lockPolicy: 'exclusive' });
return withOpfsPathCapacity(
await OPFSAnyContextVFS.create(name, module, { lockPolicy: 'exclusive' }),
);
}

// @ts-ignore vendored module lacks type declarations
const { OPFSCoopSyncVFS } = await import('./vendor/OPFSCoopSyncVFS.js');
return OPFSCoopSyncVFS.create(name, module);
return withOpfsPathCapacity(await OPFSCoopSyncVFS.create(name, module));
}

export type OpenOptions = {
Expand Down
Loading