Skip to content

Commit a226dff

Browse files
committed
feat: opfs vfs checker
1 parent af0d6f8 commit a226dff

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/io/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export async function getHandleFromPath(path: string, create?: boolean): Promise
1616
return await root.getFileHandle(path, { create })
1717
}
1818

19-
export function isOpfsVFS(vfs: FacadeVFS): boolean {
19+
export function defaultIsOpfsVFS(vfs: FacadeVFS): boolean {
2020
return 'releaser' in vfs
2121
}

src/io/export.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
SQLITE_OPEN_MAIN_DB,
77
SQLITE_OPEN_READONLY,
88
} from '../constant'
9-
import { check, getHandleFromPath, ignoredDataView, isOpfsVFS } from './common'
9+
import { check, defaultIsOpfsVFS, getHandleFromPath, ignoredDataView } from './common'
1010

1111
export function dumpReadableStream(vfs: FacadeVFS, path: string): ReadableStream {
1212
const source = getExistDataSource(vfs, path)
@@ -109,8 +109,13 @@ export async function streamToUint8Array(stream: ReadableStream): Promise<Uint8A
109109
* Export database to `Uint8Array`
110110
* @param vfs SQLite VFS
111111
* @param path database path
112+
* @param isOpfsVFS check if vfs is on OPFS, {@link defaultIsOpfsVFS} by default
112113
*/
113-
export async function exportDatabase(vfs: FacadeVFS, path: string): Promise<Uint8Array> {
114+
export async function exportDatabase(
115+
vfs: FacadeVFS,
116+
path: string,
117+
isOpfsVFS = defaultIsOpfsVFS,
118+
): Promise<Uint8Array> {
114119
return isOpfsVFS(vfs)
115120
? await getHandleFromPath(path)
116121
.then(handle => handle.getFile())

src/io/import.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
SQLITE_OPEN_READWRITE,
1515
SQLITE_SYNC_NORMAL,
1616
} from '../constant'
17-
import { check, getHandleFromPath, ignoredDataView, isOpfsVFS } from './common'
17+
import { check, defaultIsOpfsVFS, getHandleFromPath, ignoredDataView } from './common'
1818

1919
const SQLITE_BINARY_HEADER = new Uint8Array([
2020
0x53, 0x51, 0x4C, 0x69, 0x74, 0x65, 0x20, 0x66, // SQLite f
@@ -139,11 +139,13 @@ async function importDatabaseToOpfs(
139139
* @param vfs SQLite VFS
140140
* @param path db path
141141
* @param data existing database
142+
* @param isOpfsVFS check if vfs is on OPFS, {@link defaultIsOpfsVFS} by default
142143
*/
143144
export async function importDatabase(
144145
vfs: FacadeVFS,
145146
path: string,
146147
data: File | ReadableStream<Uint8Array>,
148+
isOpfsVFS = defaultIsOpfsVFS,
147149
): Promise<void> {
148150
const stream = data instanceof globalThis.File ? data.stream() : data
149151
// is `OPFSCoopSyncVFS`

0 commit comments

Comments
 (0)