Skip to content

Commit 0db7658

Browse files
committed
fix: Bun worker onerror, batch CSS imports, sqlite wrapper improvements
- Add missing onerror handler on Bun Worker path (audit #3) — prevents promise hang when a parse worker crashes - Batch CSS imports instead of inserting one-at-a-time (audit S1) — both full-rebuild and incremental paths now pass the full array to insertImports, letting batchInsert chunk properly - Hoist inner.query() in wrap() to avoid redundant rawDb.prepare() on Node when .get()/.all() is called multiple times (audit S2) - Fix SCHEMA_VERSION comment to match actual value of 1 (audit #4) - Skip PRAGMA optimize on closeDb for read-only query paths (audit S4)
1 parent 24ab43f commit 0db7658

4 files changed

Lines changed: 33 additions & 26 deletions

File tree

src/application/index-engine.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,18 @@ function insertParsedResults(
186186
}
187187
if (parsed.markers?.length) insertMarkers(db, parsed.markers);
188188

189-
if (parsed.cssImportSources) {
190-
for (const importSource of parsed.cssImportSources) {
191-
insertImports(db, [
192-
{
193-
file_path: parsed.relPath,
194-
source: importSource,
195-
resolved_path: null,
196-
specifiers: "[]",
197-
is_type_only: 0,
198-
line_number: 0,
199-
},
200-
]);
201-
}
189+
if (parsed.cssImportSources?.length) {
190+
insertImports(
191+
db,
192+
parsed.cssImportSources.map((importSource) => ({
193+
file_path: parsed.relPath,
194+
source: importSource,
195+
resolved_path: null,
196+
specifiers: "[]",
197+
is_type_only: 0,
198+
line_number: 0,
199+
})),
200+
);
202201
}
203202
} else {
204203
if (parsed.symbols?.length) insertSymbols(db, parsed.symbols);
@@ -333,17 +332,18 @@ export async function indexFiles(
333332
insertCssKeyframes(db, cssData.keyframes);
334333
}
335334
if (cssData.markers.length) insertMarkers(db, cssData.markers);
336-
for (const importSource of cssData.importSources) {
337-
insertImports(db, [
338-
{
335+
if (cssData.importSources.length) {
336+
insertImports(
337+
db,
338+
cssData.importSources.map((importSource) => ({
339339
file_path: relPath,
340340
source: importSource,
341341
resolved_path: null,
342342
specifiers: "[]",
343343
is_type_only: 0,
344344
line_number: 0,
345-
},
346-
]);
345+
})),
346+
);
347347
}
348348
} else {
349349
const data = extractFileData(absPath, source, relPath);
@@ -475,7 +475,7 @@ export function printQueryResult(
475475
}
476476
return 1;
477477
} finally {
478-
if (db !== undefined) closeDb(db);
478+
if (db !== undefined) closeDb(db, { readonly: true });
479479
}
480480
}
481481

@@ -488,6 +488,6 @@ export function queryRows(sql: string): unknown[] {
488488
try {
489489
return db.query(sql).all();
490490
} finally {
491-
closeDb(db);
491+
closeDb(db, { readonly: true });
492492
}
493493
}

src/db.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from "./sqlite-db";
66

77
/**
8-
* Pre-release: keep at **2** until the first npm release — do not bump for DDL
8+
* Pre-release: keep at **1** until the first npm release — do not bump for DDL
99
* tweaks; run `--full` locally after pulling. After v1.0, bump in lockstep with
1010
* `createTables` / `createIndexes` when the on-disk schema changes.
1111
*/
@@ -17,9 +17,11 @@ export function openDb(): CodemapDatabase {
1717
return openCodemapDatabase();
1818
}
1919

20-
export function closeDb(db: CodemapDatabase) {
21-
db.run("PRAGMA analysis_limit = 400");
22-
db.run("PRAGMA optimize");
20+
export function closeDb(db: CodemapDatabase, opts?: { readonly?: boolean }) {
21+
if (!opts?.readonly) {
22+
db.run("PRAGMA analysis_limit = 400");
23+
db.run("PRAGMA optimize");
24+
}
2325
db.close();
2426
}
2527

src/sqlite-db.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,13 @@ function wrap(inner: SqliteInner): CodemapDatabase {
107107
runSql(inner, sql, params);
108108
},
109109
query<T>(sql: string) {
110+
const stmt = inner.query(sql);
110111
return {
111112
get(...params: unknown[]) {
112-
return inner.query(sql).get(...params) as T | undefined;
113+
return stmt.get(...params) as T | undefined;
113114
},
114115
all(...params: unknown[]) {
115-
return inner.query(sql).all(...params) as T[];
116+
return stmt.all(...params) as T[];
116117
},
117118
};
118119
},

src/worker-pool.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export function parseFilesParallel(filePaths: string[]): Promise<ParsedFile[]> {
4444
resolve(event.data.results);
4545
worker.terminate();
4646
};
47+
worker.onerror = (event: ErrorEvent) => {
48+
reject(new Error(event.message));
49+
worker.terminate();
50+
};
4751
worker.postMessage(input);
4852
return;
4953
}

0 commit comments

Comments
 (0)