Repository navigation
Expand file tree
/
Copy pathsqlite3-worker1.js
More file actions
344 lines (294 loc) · 10.4 KB
/
Copy pathsqlite3-worker1.js
File metadata and controls
344 lines (294 loc) · 10.4 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
2022-05-23
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 is a JS Worker file for the main sqlite3 api. It loads
sqlite3.js, initializes the module, and postMessage()'s a message
after the module is initialized:
{type: 'sqlite3-api', result: 'worker1-ready'}
This seemingly superfluous level of indirection is necessary when
loading sqlite3.js via a Worker. Instantiating a worker with new
Worker("sqlite.js") will not (cannot) call sqlite3InitModule() to
initialize the module due to a timing/order-of-operations conflict
(and that symbol is not exported in a way that a Worker loading it
that way can see it). Thus JS code wanting to load the sqlite3
Worker-specific API needs to pass _this_ file (or equivalent) to the
Worker constructor and then listen for an event in the form shown
above in order to know when the module has completed initialization.
*/
"use strict";
importScripts('sqlite3.js');
sqlite3InitModule().then(sqlite3 => sqlite3.installOpfsSAHPoolVfs({
vfsName: 'opfs-sahpool',
initialCapacity: 6,
clearOnInit: false,
directory: 'sqlite-sahpool-dir'
}).then(poolutil => {
postMessage({type: 'worker-init-success'});
sqlite3.initWorker1API(
function apiExt({wMsgHandler, getMsgDb}) {
const ext = Object.create(null);
const origOpen = wMsgHandler.open;
const origClose = wMsgHandler.close;
let db;
if (poolutil) {
wMsgHandler.open = function(ev) {
const fn = poolutil.getFileNames();
for (let i = fn.length; i--;) {
if (fn[i] !== ev.args.filename.replace('file:', '/')) {
poolutil.unlink(fn[i]);
}
}
db = origOpen(ev);
return db;
};
}
wMsgHandler.close = function(ev) {
if (db && db.pointer) {
sqlite3.capi.sqlite3_interrupt(db.pointer);
}
return origClose(ev);
};
self.addEventListener('close', wMsgHandler.close);
let dbConfig = null;
let isReadonly = false;
let sessionPragmas = '';
let releaser = null;
let activeDbId = null;
let eHold = false;
let inFlight = 0;
const origExec = wMsgHandler.exec;
if (poolutil) {
wMsgHandler.pauseVfs = function(ev) {
const args = ev && ev.args || {};
if (args.config) {
dbConfig = {filename: args.config.filename, vfs: args.config.vfs};
}
if (typeof args.pragmas === 'string') {
sessionPragmas = args.pragmas;
}
isReadonly = !!args.readonly;
poolutil.pauseVfs();
return {paused: true};
};
}
wMsgHandler.setRole = async function(ev) {
isReadonly = !!(ev.args && ev.args.readonly);
if (activeDbId) {
await origExec({dbId: activeDbId, args: {sql: `PRAGMA query_only=${isReadonly ? 'ON' : 'OFF'}`}});
}
return {readonly: isReadonly};
};
const acquireWebLock = () => new Promise((resolve, reject) => {
navigator.locks.request('fmdb-sqlite-active', {mode: 'exclusive'}, () => new Promise(release => {
releaser = release;
resolve();
})).catch(reject);
});
const openDatabase = async () => {
if (!dbConfig) {
throw new Error('fmdb-sqlite worker: dbConfig not initialized');
}
await poolutil.unpauseVfs();
const openRes = await wMsgHandler.open({args: {filename: dbConfig.filename, vfs: dbConfig.vfs}});
activeDbId = openRes.dbId || openRes.result && openRes.result.dbId;
if (sessionPragmas) {
await origExec({dbId: activeDbId, args: {sql: sessionPragmas}});
}
if (isReadonly) {
await origExec({dbId: activeDbId, args: {sql: 'PRAGMA query_only=ON'}});
}
};
let acquireChain = Promise.resolve();
const acquire = () => {
const next = acquireChain.then(async () => {
if (!releaser) {
await acquireWebLock();
}
if (!activeDbId) {
await openDatabase();
}
});
acquireChain = next.catch(() => {});
return next;
};
const release = () => {
if (eHold || inFlight > 0 || !releaser) {
return;
}
if (activeDbId) {
wMsgHandler.close({dbId: activeDbId, args: {dbId: activeDbId}});
activeDbId = null;
}
poolutil.pauseVfs();
const r = releaser;
releaser = null;
r();
};
wMsgHandler.exec = async function(ev) {
ev.args = ev.args || {};
const sql = ev.args.sql || '';
inFlight++;
try {
await acquire();
ev.dbId = activeDbId;
ev.args.dbId = activeDbId;
const res = await origExec(ev);
if (/^\s*(BEGIN|SAVEPOINT)\b/i.test(sql)) {
eHold = true;
}
return res;
}
finally {
inFlight--;
if (/^\s*(COMMIT|END|ROLLBACK|RELEASE)\b/i.test(sql)) {
eHold = false;
}
release();
}
};
const escapeId = (s) => String(s).replace(/[^A-Za-z0-9_]/g, '_');
const maxVariables = 30000;
const _runBulkStatement = (db, skipTx, total, chunkSize, getSql, bindChunk) => {
let stmt;
let stmtSize = 0;
if (!skipTx) {
db.exec('BEGIN IMMEDIATE');
}
for (let offset = 0; offset < total; offset += chunkSize) {
const size = Math.min(chunkSize, total - offset);
if (!stmt || stmtSize !== size) {
if (stmt) {
stmt.finalize();
}
stmt = db.prepare(getSql(size));
stmtSize = size;
}
bindChunk(stmt, offset, size);
stmt.step();
stmt.reset(true);
}
if (stmt) {
stmt.finalize();
}
if (!skipTx) {
db.exec('COMMIT');
}
};
wMsgHandler.bulkput = async function(ev) {
const args = ev.args || Object.create(null);
const table = args.table;
const columns = Array.isArray(args.columns) ? args.columns : [];
const binds = Array.isArray(args.binds) ? args.binds : [];
const skipTx = args.skipTx !== false;
if (!table || !columns.length) {
throw new Error("bulkput requires table and columns");
}
if (!binds.length) {
return {ok: true, execCount: 0};
}
const perRowBind = columns.length;
if (binds.length % perRowBind !== 0) {
throw new Error('bulkput binds length mismatch');
}
const tableName = escapeId(table);
const colSql = columns.map(c => escapeId(c)).join(',');
const rowPlace = '(' + new Array(perRowBind).fill('?').join(',') + ')';
const totalRows = Math.floor(binds.length / perRowBind);
const maxRowsPerStmt = Math.max(1, Math.floor(maxVariables / perRowBind));
const acquired = !activeDbId;
if (acquired) {
inFlight++;
}
try {
if (acquired) {
await acquire();
}
ev.dbId = activeDbId;
ev.args.dbId = activeDbId;
const db = getMsgDb(ev);
_runBulkStatement(
db,
skipTx,
totalRows,
maxRowsPerStmt,
rowsInStmt => {
const valuesSql = new Array(rowsInStmt).fill(rowPlace).join(',');
return `INSERT OR REPLACE INTO ${tableName} (${colSql}) VALUES ${valuesSql}`;
},
(stmt, rowIdx, rowsInStmt) => {
const start = rowIdx * perRowBind;
const bindCount = rowsInStmt * perRowBind;
for (let i = 0; i < bindCount; i++) {
stmt.bind(i + 1, binds[start + i]);
}
}
);
}
finally {
if (acquired) {
inFlight--;
release();
}
}
return {ok: true, execCount: totalRows};
};
wMsgHandler.bulkdelete = async function(ev) {
const args = ev.args || Object.create(null);
const table = args.table;
const pk = args.pk;
const keys = Array.isArray(args.keys) ? args.keys : [];
const skipTx = args.skipTx !== false;
if (!table || !pk) {
throw new Error("bulkdelete requires table and pk");
}
if (!keys.length) {
return {ok: true, execCount: 0};
}
const tableName = escapeId(table);
const pkName = escapeId(pk);
const acquired = !activeDbId;
if (acquired) {
inFlight++;
}
try {
if (acquired) {
await acquire();
}
ev.dbId = activeDbId;
ev.args.dbId = activeDbId;
const db = getMsgDb(ev);
_runBulkStatement(
db,
skipTx,
keys.length,
maxVariables,
keysInStmt => {
const placeholders = new Array(keysInStmt).fill('?').join(',');
return `DELETE FROM ${tableName} WHERE ${pkName} IN (${placeholders})`;
},
(stmt, offset, keysInStmt) => {
for (let i = 0; i < keysInStmt; i++) {
stmt.bind(i + 1, keys[offset + i]);
}
}
);
}
finally {
if (acquired) {
inFlight--;
release();
}
}
return {ok: true, execCount: keys.length};
};
return ext;
});
})).catch(ex => {
postMessage({type: 'worker-init-failed', error: ex && ex.message || String(ex)});
}
);