Skip to content

Commit 4298504

Browse files
committed
feat: extract common and simplify export opfs
1 parent 86b933b commit 4298504

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

playground/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ document.querySelector('.download')?.addEventListener('click', async () => {
5959
}
6060
})
6161

62+
document.querySelector('.downloadW')?.addEventListener('click', async () => {
63+
const worker = new OpfsWorker()
64+
worker.postMessage('dl')
65+
worker.onmessage = (buf: any) => {
66+
download(buf)
67+
worker.terminate()
68+
}
69+
})
70+
6271
const ee = mitt<{
6372
data: [any]
6473
done: []

playground/src/worker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ onmessage = async ({ data }) => {
1010
}
1111
const db = await initSQLite(useOpfsStorage(
1212
'test.db',
13-
data ? withExistDB(data, { url }) : { url },
13+
(data && data !== 'dl') ? withExistDB(data, { url }) : { url },
1414
// { url },
1515
))
16+
if (data === 'dl') {
17+
postMessage(await db.dump())
18+
return
19+
}
1620
// if (data) {
1721
// await db.sync(data)
1822
// }

src/io/common.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Promisable } from '../types'
1+
import type { FacadeVFS, Promisable } from '../types'
22
import { SQLITE_OK } from '../constant'
33

44
export async function check(code: Promisable<number>): Promise<void> {
@@ -10,3 +10,12 @@ export async function check(code: Promisable<number>): Promise<void> {
1010
export function ignoredDataView(): DataView {
1111
return new DataView(new ArrayBuffer(4))
1212
}
13+
export async function getHandleFromPath(path: string): Promise<FileSystemFileHandle> {
14+
const root = await navigator.storage.getDirectory()
15+
const handle = await root.getFileHandle(path, { create: true })
16+
return handle
17+
}
18+
19+
export function isOpfsVFS(vfs: FacadeVFS): boolean {
20+
return 'releaser' in vfs
21+
}

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, ignoredDataView } from './common'
9+
import { check, getHandleFromPath, ignoredDataView, isOpfsVFS } from './common'
1010

1111
export function dumpReadableStream(vfs: FacadeVFS, path: string): ReadableStream {
1212
const source = getExistDataSource(vfs, path)
@@ -111,5 +111,10 @@ export async function streamToUint8Array(stream: ReadableStream): Promise<Uint8A
111111
* @param path database path
112112
*/
113113
export async function exportDatabase(vfs: FacadeVFS, path: string): Promise<Uint8Array> {
114-
return await streamToUint8Array(dumpReadableStream(vfs, path))
114+
return isOpfsVFS(vfs)
115+
? await getHandleFromPath(path)
116+
.then(handle => handle.getFile())
117+
.then(file => file.arrayBuffer())
118+
.then(buf => new Uint8Array(buf))
119+
: await streamToUint8Array(dumpReadableStream(vfs, path))
115120
}

src/io/import.ts

Lines changed: 3 additions & 4 deletions
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, ignoredDataView } from './common'
17+
import { check, getHandleFromPath, ignoredDataView, isOpfsVFS } from './common'
1818

1919
const SQLITE_BINARY_HEADER = new Uint8Array([
2020
0x53, 0x51, 0x4C, 0x69, 0x74, 0x65, 0x20, 0x66, // SQLite f
@@ -124,8 +124,7 @@ async function importDatabaseToOpfs(
124124
path: string,
125125
source: ReadableStream<Uint8Array>,
126126
): Promise<void> {
127-
const root = await navigator.storage.getDirectory()
128-
const handle = await root.getFileHandle(path, { create: true })
127+
const handle = await getHandleFromPath(path)
129128
const [streamForVerify, streamData] = source.tee()
130129

131130
await parseHeaderAndVerify(streamForVerify.getReader())
@@ -148,7 +147,7 @@ export async function importDatabase(
148147
): Promise<void> {
149148
const stream = data instanceof globalThis.File ? data.stream() : data
150149
// is `OPFSCoopSyncVFS`
151-
if ('releaser' in vfs) {
150+
if (isOpfsVFS(vfs)) {
152151
await importDatabaseToOpfs(path, stream)
153152
} else {
154153
await importDatabaseToIdb(vfs, path, stream)

0 commit comments

Comments
 (0)