-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqliteWorker.ts
More file actions
275 lines (230 loc) · 7.28 KB
/
sqliteWorker.ts
File metadata and controls
275 lines (230 loc) · 7.28 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
import type { WorkerMessage, WorkerResponse, SQLiteWASMConfig } from '../types/index';
let sqlite3InitModule: any = null;
let sqlite3: any = null;
let PoolUtil: any = null;
let db: any = null;
let currentConfig: SQLiteWASMConfig | null = null;
async function initDatabase(config: SQLiteWASMConfig) {
currentConfig = config;
// Import SQLite WASM
sqlite3InitModule = await import('@sqlite.org/sqlite-wasm');
// Setup console filtering if enabled
if (config.logging?.filterSqlTrace) {
const originalLog = console.log; // eslint-disable-line no-console
const originalError = console.error;
// eslint-disable-next-line no-console
console.log = (...args) => {
const message = args.join(' ');
if (!message.includes('SQL TRACE') && !message.includes('TRACE #')) {
originalLog(...args);
}
};
console.error = (...args) => {
const message = args.join(' ');
if (!message.includes('SQL TRACE') && !message.includes('TRACE #')) {
originalError(...args);
}
};
}
// Initialize SQLite3 (use defaults for print/printErr since functions can't be sent via postMessage)
sqlite3 = await sqlite3InitModule.default({
// eslint-disable-next-line no-console
print: console.log,
printErr: console.error,
});
// Setup VFS based on configuration
const vfsType = config.vfs?.type || 'opfs';
if (vfsType === 'opfs-sahpool') {
PoolUtil = await sqlite3.installOpfsSAHPoolVfs({
initialCapacity: config.vfs?.poolConfig?.initialCapacity || 3,
clearOnInit: config.vfs?.poolConfig?.clearOnInit || false,
name: config.vfs?.poolConfig?.name || 'sqlite-wasm-pool',
});
} else if (vfsType === 'opfs') {
// Direct OPFS VFS (if available)
PoolUtil = sqlite3.opfs || null;
} else if (vfsType === 'memory') {
// In-memory database
PoolUtil = null;
}
}
async function openDatabase(filename: string) {
if (db) throw new Error('Database already opened');
const vfsType = currentConfig?.vfs?.type || 'opfs';
if (vfsType === 'opfs-sahpool' && PoolUtil) {
db = new PoolUtil.OpfsSAHPoolDb({
filename,
flags: 'create',
vfs: 'opfs-sahpool',
});
} else if (vfsType === 'memory') {
db = new sqlite3.oo1.DB(':memory:', 'c');
} else {
// Check if OpfsDb is available (requires COOP/COEP headers)
if (typeof sqlite3.oo1.OpfsDb !== 'function') {
throw new Error(
'OPFS VFS is not available. This usually means your server is not configured with ' +
'the required COOP/COEP headers (Cross-Origin-Opener-Policy: same-origin, ' +
"Cross-Origin-Embedder-Policy: require-corp). Consider using 'opfs-sahpool' instead, " +
'which does not require these headers.'
);
}
db = new sqlite3.oo1.OpfsDb(filename, 'c');
}
// Apply PRAGMA settings
if (currentConfig?.pragma) {
for (const [key, value] of Object.entries(currentConfig.pragma)) {
if (value !== undefined) {
await execSQL(`PRAGMA ${key} = ${value}`);
}
}
}
}
async function closeDatabase() {
if (!db) throw new Error('Database not opened');
await db.close();
db = null;
PoolUtil = null;
sqlite3 = null;
sqlite3InitModule = null;
currentConfig = null;
}
async function execSQL(sql: string, params: unknown[] = []): Promise<void> {
if (!db) throw new Error('Database not opened');
await db.exec({
sql,
bind: params,
rowMode: 'array',
});
}
async function querySQL<T = any>(sql: string, params: unknown[] = []): Promise<T[]> {
if (!db) throw new Error('Database not opened');
const results: T[] = [];
await db.exec({
sql,
bind: params,
rowMode: 'array',
callback: (row: any[], statement: any) => {
const columns = statement.getColumnNames();
const rowObject: any = {};
for (let i = 0; i < columns.length; i++) {
const value = row[i];
// Auto-parse JSON if it looks like JSON
if (typeof value === 'string' && (value.startsWith('{') || value.startsWith('['))) {
try {
rowObject[columns[i]] = JSON.parse(value);
} catch {
rowObject[columns[i]] = value;
}
} else {
rowObject[columns[i]] = value;
}
}
results.push(rowObject);
},
});
return results;
}
async function runSQL(
sql: string,
params: unknown[] = []
): Promise<{ lastInsertRowId?: number; changes?: number }> {
if (!db) throw new Error('Database not opened');
await db.exec({
sql,
bind: params,
rowMode: 'array',
});
// Get last insert row ID and changes - explicitly convert to primitives
// to ensure serializability via postMessage
const changesCount = Number(db.changes) || 0;
const lastInsertRowId =
changesCount > 0 ? Number(db.selectValue('SELECT last_insert_rowid()')) : undefined;
return { lastInsertRowId, changes: changesCount };
}
async function exportDatabase(): Promise<Uint8Array> {
if (!PoolUtil?.exportFile) {
throw new Error('Export not supported for this VFS type');
}
const data = await PoolUtil.exportFile(`/${currentConfig?.filename}`);
return data;
}
async function importDatabase(filename: string, data: Uint8Array): Promise<void> {
if (!PoolUtil?.importDb) {
throw new Error('Import not supported for this VFS type');
}
await PoolUtil.importDb(filename, data);
}
async function deleteDatabase(): Promise<void> {
if (db) {
await db.close();
db = null;
}
if (PoolUtil?.wipeFiles) {
await PoolUtil.wipeFiles();
}
}
async function handleDatabaseOperation(message: WorkerMessage): Promise<WorkerResponse> {
const { id, operation, sql, params, config, filename, data } = message;
try {
switch (operation) {
case 'init': {
if (!config) throw new Error('Configuration required for init');
await initDatabase(config);
return { id, status: 'ready' };
}
case 'open': {
if (!filename) throw new Error('Filename required for open');
await openDatabase(filename);
return { id, status: 'success' };
}
case 'close': {
await closeDatabase();
return { id, status: 'success' };
}
case 'exec': {
if (!sql) throw new Error('SQL required for exec');
await execSQL(sql, params);
return { id, status: 'success' };
}
case 'query': {
if (!sql) throw new Error('SQL required for query');
const results = await querySQL(sql, params);
return { id, status: 'success', results };
}
case 'run': {
if (!sql) throw new Error('SQL required for run');
const results = await runSQL(sql, params);
return { id, status: 'success', results };
}
case 'export': {
const results = await exportDatabase();
return { id, status: 'success', results };
}
case 'import': {
if (!filename || !data) throw new Error('Filename and data required for import');
await importDatabase(filename, data);
return { id, status: 'success' };
}
case 'delete': {
await deleteDatabase();
return { id, status: 'success' };
}
default: {
throw new Error(`Unknown operation: ${operation}`);
}
}
} catch (error: unknown) {
return { id, status: 'error', error: error instanceof Error ? error.message : 'Unknown error' };
}
}
// Worker message handler
self.addEventListener('message', async (event: MessageEvent<WorkerMessage>) => {
const message = event.data;
// Security: Check message origin
if (self.origin && event.origin && event.origin !== self.origin) {
return self.postMessage({ id: message.id, status: 'error', error: 'Invalid message origin' });
}
const response = await handleDatabaseOperation(message);
self.postMessage(response);
});