aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/tests/opfs/sahpool/digest-worker.js
blob: 28b3c1673f4fe4ead6fba1a90eb3df201013e2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
  2025-01-31

  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.

  ***********************************************************************

  This file is part of testing the OPFS SAHPool VFS's computeDigest()
  fix.  See ./digest.html for the details.
*/
const clog = console.log.bind(console);
const wPost = (type,...args)=>postMessage({type, payload:args});
const log = (...args)=>{
  clog("Worker:",...args);
  wPost('log',...args);
}

const hasOpfs = ()=>{
  return globalThis.FileSystemHandle
    && globalThis.FileSystemDirectoryHandle
    && globalThis.FileSystemFileHandle
    && globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle
    && navigator?.storage?.getDirectory;
};
if( !hasOpfs() ){
  wPost('error',"OPFS not detected");
  throw new Error("OPFS not detected");
}

clog("Importing sqlite3...");
const searchParams = new URL(self.location.href).searchParams;
importScripts(searchParams.get('sqlite3.dir') + '/sqlite3.js');

const runTests = function(sqlite3, poolUtil){
  const fname = '/my.db';
  let db = new poolUtil.OpfsSAHPoolDb(fname);
  let n = (new Date()).valueOf();
  try {
    db.exec([
      "create table if not exists t(a);"
    ]);
    db.exec({
      sql: "insert into t(a) values(?)",
      bind: n++
    });
    log(fname,"record count: ",db.selectValue("select count(*) from t"));
  }finally{
    db.close();
  }

  db = new poolUtil.OpfsSAHPoolDb(fname);
  try {
    db.exec({
      sql: "insert into t(a) values(?)",
      bind: n++
    });
    log(fname,"record count: ",db.selectValue("select count(*) from t"));
  }finally{
    db.close();
  }

  const fname2 = '/my2.db';
  db = new poolUtil.OpfsSAHPoolDb(fname2);
  try {
    db.exec([
      "create table if not exists t(a);"
    ]);
    db.exec({
      sql: "insert into t(a) values(?)",
      bind: n++
    });
    log(fname2,"record count: ",db.selectValue("select count(*) from t"));
  }finally{
    db.close();
  }
};

globalThis.sqlite3InitModule().then(async function(sqlite3){
  log("sqlite3 version:",sqlite3.version);
  const sahPoolConfig = {
    name: 'opfs-sahpool-digest',
    clearOnInit: false,
    initialCapacity: 6
  };
  return sqlite3.installOpfsSAHPoolVfs(sahPoolConfig).then(poolUtil=>{
    log('vfs acquired');
    runTests(sqlite3, poolUtil);
  });
});