UNPKG

@livestore/sqlite-wasm

Version:

19 lines 999 B
import { Effect } from '@livestore/utils/effect'; import { AccessHandlePoolVFS } from './AccessHandlePoolVFS.js'; const semaphore = Effect.makeSemaphore(1).pipe(Effect.runSync); const opfsVfsMap = new Map(); export const makeOpfsDb = ({ sqlite3, directory, fileName, }) => Effect.gen(function* () { // Replace all special characters with underscores const safePath = directory.replaceAll(/["*/:<>?\\|]/g, '_'); const pathSegment = safePath.length === 0 ? '' : `-${safePath}`; const vfsName = `opfs${pathSegment}`; if (sqlite3.vfs_registered.has(vfsName) === false) { const vfs = yield* Effect.promise(() => AccessHandlePoolVFS.create(vfsName, directory, sqlite3.module)); sqlite3.vfs_register(vfs, false); opfsVfsMap.set(vfsName, vfs); } const dbPointer = sqlite3.open_v2Sync(fileName, undefined, vfsName); const vfs = opfsVfsMap.get(vfsName); return { dbPointer, vfs }; }).pipe(semaphore.withPermits(1)); //# sourceMappingURL=index.js.map