2022-09-17 23:50:12 +03:00
|
|
|
/*
|
|
|
|
2022-09-17
|
|
|
|
|
|
|
|
The author disclaims copyright to this source code. In place of a
|
|
|
|
legal notice, here is a blessing:
|
|
|
|
|
|
|
|
* May you do good and not evil.
|
|
|
|
* May you find forgiveness for yourself and forgive others.
|
|
|
|
* May you share freely, never taking more than you give.
|
|
|
|
|
|
|
|
***********************************************************************
|
|
|
|
|
2022-09-18 05:35:30 +03:00
|
|
|
A testing ground for the OPFS VFS.
|
2022-09-17 23:50:12 +03:00
|
|
|
*/
|
2022-09-17 18:08:22 +03:00
|
|
|
'use strict';
|
2022-10-03 14:33:35 +03:00
|
|
|
const tryOpfsVfs = async function(sqlite3){
|
2022-09-17 23:50:12 +03:00
|
|
|
const toss = function(...args){throw new Error(args.join(' '))};
|
2022-09-18 05:35:30 +03:00
|
|
|
const logPrefix = "OPFS tester:";
|
2022-09-18 06:05:55 +03:00
|
|
|
const log = (...args)=>console.log(logPrefix,...args);
|
|
|
|
const warn = (...args)=>console.warn(logPrefix,...args);
|
|
|
|
const error = (...args)=>console.error(logPrefix,...args);
|
2022-09-29 16:17:50 +03:00
|
|
|
const opfs = sqlite3.opfs;
|
2022-09-18 05:35:30 +03:00
|
|
|
log("tryOpfsVfs()");
|
2022-09-29 16:17:50 +03:00
|
|
|
if(!sqlite3.opfs){
|
2023-11-15 14:31:49 +03:00
|
|
|
const e = new Error("OPFS is not available.");
|
2022-09-29 16:17:50 +03:00
|
|
|
error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
2022-09-17 23:50:12 +03:00
|
|
|
const capi = sqlite3.capi;
|
2022-09-27 16:40:12 +03:00
|
|
|
const pVfs = capi.sqlite3_vfs_find("opfs") || toss("Missing 'opfs' VFS.");
|
2022-12-19 17:43:17 +03:00
|
|
|
const oVfs = new capi.sqlite3_vfs(pVfs);
|
2022-09-18 05:35:30 +03:00
|
|
|
log("OPFS VFS:",pVfs, oVfs);
|
2022-09-18 06:05:55 +03:00
|
|
|
|
2022-10-03 14:33:35 +03:00
|
|
|
const wait = async (ms)=>{
|
|
|
|
return new Promise((resolve)=>setTimeout(resolve, ms));
|
|
|
|
};
|
|
|
|
|
2022-09-18 20:32:35 +03:00
|
|
|
const urlArgs = new URL(self.location.href).searchParams;
|
2022-09-18 06:05:55 +03:00
|
|
|
const dbFile = "my-persistent.db";
|
2022-11-01 10:49:49 +03:00
|
|
|
if(urlArgs.has('delete')) sqlite3.opfs.unlink(dbFile);
|
2022-09-18 20:32:35 +03:00
|
|
|
|
2022-12-19 17:43:17 +03:00
|
|
|
const db = new sqlite3.oo1.OpfsDb(dbFile,'ct');
|
2022-09-18 06:05:55 +03:00
|
|
|
log("db file:",db.filename);
|
|
|
|
try{
|
2022-09-18 20:32:35 +03:00
|
|
|
if(opfs.entryExists(dbFile)){
|
|
|
|
let n = db.selectValue("select count(*) from sqlite_schema");
|
2022-09-18 06:05:55 +03:00
|
|
|
log("Persistent data found. sqlite_schema entry count =",n);
|
|
|
|
}
|
|
|
|
db.transaction((db)=>{
|
|
|
|
db.exec({
|
|
|
|
sql:[
|
|
|
|
"create table if not exists t(a);",
|
|
|
|
"insert into t(a) values(?),(?),(?);",
|
|
|
|
],
|
|
|
|
bind: [performance.now() | 0,
|
|
|
|
(performance.now() |0) / 2,
|
|
|
|
(performance.now() |0) / 4]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
log("count(*) from t =",db.selectValue("select count(*) from t"));
|
2022-09-18 20:32:35 +03:00
|
|
|
|
|
|
|
// Some sanity checks of the opfs utility functions...
|
|
|
|
const testDir = '/sqlite3-opfs-'+opfs.randomFilename(12);
|
|
|
|
const aDir = testDir+'/test/dir';
|
2022-11-01 10:49:49 +03:00
|
|
|
await opfs.mkdir(aDir) || toss("mkdir failed");
|
|
|
|
await opfs.mkdir(aDir) || toss("mkdir must pass if the dir exists");
|
|
|
|
await opfs.unlink(testDir+'/test') && toss("delete 1 should have failed (dir not empty)");
|
|
|
|
//await opfs.entryExists(testDir)
|
|
|
|
await opfs.unlink(testDir+'/test/dir') || toss("delete 2 failed");
|
|
|
|
await opfs.unlink(testDir+'/test/dir') && toss("delete 2b should have failed (dir already deleted)");
|
|
|
|
await opfs.unlink(testDir, true) || toss("delete 3 failed");
|
|
|
|
await opfs.entryExists(testDir) && toss("entryExists(",testDir,") should have failed");
|
2022-09-18 06:05:55 +03:00
|
|
|
}finally{
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
2022-09-18 05:35:30 +03:00
|
|
|
log("Done!");
|
|
|
|
}/*tryOpfsVfs()*/;
|
2022-09-17 18:08:22 +03:00
|
|
|
|
2022-10-19 07:44:58 +03:00
|
|
|
importScripts('jswasm/sqlite3.js');
|
2022-09-18 20:32:35 +03:00
|
|
|
self.sqlite3InitModule()
|
|
|
|
.then((sqlite3)=>tryOpfsVfs(sqlite3))
|
|
|
|
.catch((e)=>{
|
|
|
|
console.error("Error initializing module:",e);
|
|
|
|
});
|