Skip to content

Commit

Permalink
Add file import verifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
shoestringresearch committed Oct 31, 2023
1 parent 1fe6d78 commit d69169d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
5 changes: 1 addition & 4 deletions demo/file/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
<meta charset="utf-8">
<title>Local file transfer</title>
<style>
button { margin-top: 1em; }
pre { margin: 0 }
</style>
<script type="module" defer src="./index.js"></script>
</head>
<body>
<h1>Sample database importer</h1>
<div>
<input type="file" id="file-import">
</div>
<div>
<button id="file-export" disabled>Download</button>
</div>
<hr>
</body>
</html>
26 changes: 23 additions & 3 deletions demo/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,27 @@ const DBFILE_MAGIC = 'SQLite format 3\x00';
document.getElementById('file-import').addEventListener('change', async event => {
let vfs;
try {
log('Importing database...');
log(`Importing to IndexedDB ${IDB_NAME}, path ${DB_NAME}`);
vfs = new IDBBatchAtomicVFS(IDB_NAME);
// @ts-ignore
await importDatabase(vfs, DB_NAME, event.target.files[0].stream());
log('Import complete');

log('Verifying database integrity');
const url = new URL('./verifier.js', location.href);
url.searchParams.set('idb', IDB_NAME);
url.searchParams.set('db', DB_NAME);
const worker = new Worker(url, { type: 'module' });
await new Promise(resolve => {
worker.addEventListener('message', ({data}) => {
resolve();
for (const row of data) {
log(`integrity result: ${row}`);
}
worker.terminate();
});
});
log('Verification complete');
} catch (e) {
log(e.toString());
throw e;
Expand All @@ -34,6 +50,7 @@ async function importDatabase(vfs, path, stream) {
const reader = stream.getReader();

// Read at least the file header fields we need.
log('Reading file header...');
while (chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0) < 32) {
const { done, value } = await reader.read();
if (done) throw new Error('Unexpected end of file');
Expand All @@ -55,7 +72,9 @@ async function importDatabase(vfs, path, stream) {
// Extract page parameters.
const pageSize = (field => field === 1 ? 65536 : field)(header.getUint16(16));
const pageCount = header.getUint32(28);
log(`${pageCount} pages, ${pageSize} bytes each, ${pageCount * pageSize} bytes total`);

log('Copying pages...');
for (let i = 0; i < pageCount; ++i) {
// Read enough chunks to produce the next page.
while (chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0) < pageSize) {
Expand Down Expand Up @@ -92,10 +111,11 @@ async function importDatabase(vfs, path, stream) {

const onFinally = [];
try {
// Delete any existing file.
await vfs.xDelete(path, 0);
log(`Deleting ${path}...`);
await vfs.xDelete(path, 1);

// Create the file.
log(`Creating ${path}...`);
const fileId = 1234;
const flags = VFS.SQLITE_OPEN_MAIN_DB | VFS.SQLITE_OPEN_CREATE | VFS.SQLITE_OPEN_READWRITE;
await check(vfs.xOpen(path, fileId, flags, new DataView(new ArrayBuffer(4))));
Expand Down
25 changes: 25 additions & 0 deletions demo/file/verifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import SQLiteESMFactory from '../../dist/wa-sqlite-async.mjs';
import * as SQLite from '../../src/sqlite-api.js';
import { IDBBatchAtomicVFS } from '../../src/examples/IDBBatchAtomicVFS.js';

const SEARCH_PARAMS = new URLSearchParams(location.search);
const IDB_NAME = SEARCH_PARAMS.get('idb') ?? 'sqlite-vfs';
const DB_NAME = SEARCH_PARAMS.get('db') ?? 'sqlite.db';

(async function() {
const module = await SQLiteESMFactory();
const sqlite3 = SQLite.Factory(module);

const vfs = new IDBBatchAtomicVFS(IDB_NAME);
sqlite3.vfs_register(vfs, true);

const db = await sqlite3.open_v2(DB_NAME, SQLite.SQLITE_OPEN_READWRITE, IDB_NAME);

const results = []
await sqlite3.exec(db, 'PRAGMA integrity_check;', (row, columns) => {
results.push(row[0]);
});
await sqlite3.close(db);

postMessage(results);
})();

0 comments on commit d69169d

Please sign in to comment.