@linxiraos/pi-utils 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,21 +1,27 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.3.1] - 2026-10-19
4
+
5
+ ### Added
6
+
7
+ - Added `readSalvageableRows()` to read the rows of a damaged SQLite table that are still readable, skipping only the damaged pages ([#15751](https://github.com/can1357/oh-my-pi/pull/15751) by [@Shadorain](https://github.com/Shadorain))
8
+
3
9
  ## [1.3.0] - 2026-10-18
4
10
 
5
11
  ### Added
6
12
 
7
- - Added `getMnemopiEmbedRuntimeDir()` for the shared mnemopi embedding server endpoints and logs.
13
+ - Added `sanitizeTextDeep()`, which applies `sanitizeText()` to every string in a JSON-shaped value without mutating it ([#15963](https://github.com/can1357/oh-my-pi/issues/15963))
8
14
  - Exported `sqliteFileIdentity()`, so a caller holding a long-lived SQLite connection can tell when its path now names a different file (e.g. after a corrupt store was quarantined and replaced) ([#14361](https://github.com/can1357/oh-my-pi/pull/14361) by [@F0Rextasy](https://github.com/F0Rextasy))
9
15
  - Added `slugify()`, the shared lowercase ASCII kebab-case helper with an optional `maxLength` ([#14283](https://github.com/can1357/oh-my-pi/pull/14283) by [@jaredlyon](https://github.com/jaredlyon))
10
- - Added `sanitizeTextDeep()`, which applies `sanitizeText()` to every string in a JSON-shaped value without mutating it ([#15963](https://github.com/can1357/oh-my-pi/issues/15963))
16
+ - Added `getMnemopiEmbedRuntimeDir()` for the shared mnemopi embedding server endpoints and logs.
11
17
 
12
18
  ### Fixed
13
19
 
14
- - Fixed `ChildProcess.kill()` silently leaving the child running when the native process handle could not be resolved or used; it now signals the child directly and escalates to SIGKILL after the grace period.
15
- - Fixed an unhandled rejection when a subprocess is spawned with an already-aborted signal ([#15750](https://github.com/can1357/oh-my-pi/pull/15750) by [@harshaygadekar](https://github.com/harshaygadekar))
20
+ - Fixed Windows archive extraction overwriting existing files or following existing symlinks when copying a symlink entry without symlink privileges ([#15931](https://github.com/can1357/oh-my-pi/pull/15931) by [@jchanghong023](https://github.com/jchanghong023)).
16
21
  - Fixed Markdown fence info-string parsing to keep backtick-containing backtick openers as paragraph text while accepting backticks in tilde info strings and Unicode line separators in either fence's info string ([#15928](https://github.com/can1357/oh-my-pi/pull/15928) by [@jchanghong023](https://github.com/jchanghong023)).
17
22
  - Fixed `parseHTML` treating `<link>` in RSS/Atom documents as an HTML void element, which dropped the URL text of RSS `<link>…</link>` ([#15954](https://github.com/can1357/oh-my-pi/issues/15954))
18
- - Fixed Windows archive extraction overwriting existing files or following existing symlinks when copying a symlink entry without symlink privileges ([#15931](https://github.com/can1357/oh-my-pi/pull/15931) by [@jchanghong023](https://github.com/jchanghong023)).
23
+ - Fixed an unhandled rejection when a subprocess is spawned with an already-aborted signal ([#15750](https://github.com/can1357/oh-my-pi/pull/15750) by [@harshaygadekar](https://github.com/harshaygadekar))
24
+ - Fixed `ChildProcess.kill()` silently leaving the child running when the native process handle could not be resolved or used; it now signals the child directly and escalates to SIGKILL after the grace period.
19
25
 
20
26
  ## [1.2.0] - 2026-10-12
21
27
 
@@ -42,6 +42,17 @@ export declare function openSqliteDatabaseSync<T>(dbPath: string, initialize: (d
42
42
  export declare function annotateSqliteError(error: unknown, dbPath: string): Error;
43
43
  /** Checkpoints committed WAL frames without waiting for concurrent readers. */
44
44
  export declare function checkpointWal(db: Database): void;
45
+ /**
46
+ * Reads every row of a rowid table that SQLite can still decode, to salvage a
47
+ * damaged store. A full scan aborts at the first unreadable page, so this walks
48
+ * rowids with indexed seeks instead: when a seek hits damage it doubles its
49
+ * stride past it, then bisects back to the first rowid from which seeking
50
+ * works again, losing only rows on the damaged pages. Cross-linked pages can
51
+ * still decode rows with foreign column types, so callers validate types;
52
+ * rows returned out of rowid order count as damage. Only corruption errors are
53
+ * treated as damage; anything else is thrown.
54
+ */
55
+ export declare function readSalvageableRows<T extends object>(db: Database, table: string, columns: string): T[];
45
56
  /**
46
57
  * SQLite's busy result-code family — base `SQLITE_BUSY` plus the extended
47
58
  * variants `SQLITE_BUSY_RECOVERY` (concurrent WAL recovery), `SQLITE_BUSY_SNAPSHOT`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linxiraos/pi-utils",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Shared utilities for pi packages",
5
5
  "keywords": [
6
6
  "utilities",
@@ -54,7 +54,7 @@
54
54
  "fmt": "oxfmt --no-error-on-unmatched-pattern 'src/**/*.{ts,tsx}' '{test,bench,examples,scripts}/**/*.ts' '*.ts'"
55
55
  },
56
56
  "dependencies": {
57
- "@linxiraos/pi-natives": "1.3.0"
57
+ "@linxiraos/pi-natives": "1.3.1"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@types/bun": "1.4.3"
package/src/sqlite.ts CHANGED
@@ -343,6 +343,75 @@ export function checkpointWal(db: Database): void {
343
343
  db.run("PRAGMA wal_checkpoint(PASSIVE)");
344
344
  }
345
345
 
346
+ /** Damaged regions one {@link readSalvageableRows} scan skips before abandoning the rest of the table. */
347
+ const SALVAGE_MAX_DAMAGED_REGIONS = 1000;
348
+
349
+ /**
350
+ * Reads every row of a rowid table that SQLite can still decode, to salvage a
351
+ * damaged store. A full scan aborts at the first unreadable page, so this walks
352
+ * rowids with indexed seeks instead: when a seek hits damage it doubles its
353
+ * stride past it, then bisects back to the first rowid from which seeking
354
+ * works again, losing only rows on the damaged pages. Cross-linked pages can
355
+ * still decode rows with foreign column types, so callers validate types;
356
+ * rows returned out of rowid order count as damage. Only corruption errors are
357
+ * treated as damage; anything else is thrown.
358
+ */
359
+ export function readSalvageableRows<T extends object>(db: Database, table: string, columns: string): T[] {
360
+ type Row = T & { salvage_rowid: number };
361
+ using seek = db.prepare<Row, [number]>(
362
+ `SELECT rowid AS salvage_rowid, ${columns} FROM ${table} WHERE rowid > ? ORDER BY rowid LIMIT 1`,
363
+ );
364
+ using exact = db.prepare<Row, [number]>(`SELECT rowid AS salvage_rowid, ${columns} FROM ${table} WHERE rowid = ?`);
365
+ // The row found, null when there is none, or undefined when the read hit damage.
366
+ const probe = (after: number, at?: number): Row | null | undefined => {
367
+ try {
368
+ const row = at === undefined ? seek.get(after) : exact.get(at);
369
+ if (!row) return null;
370
+ const valid = Number.isSafeInteger(row.salvage_rowid) && row.salvage_rowid === (at ?? row.salvage_rowid);
371
+ return valid && row.salvage_rowid > after ? row : undefined;
372
+ } catch (error) {
373
+ if (isSqliteCorruptionError(error)) return undefined;
374
+ throw error;
375
+ }
376
+ };
377
+
378
+ const rows: T[] = [];
379
+ const keep = (row: Row): void => {
380
+ const { salvage_rowid: _, ...values } = row;
381
+ rows.push(values as unknown as T);
382
+ };
383
+ let after = Number.MIN_SAFE_INTEGER;
384
+ let damaged = 0;
385
+ while (after < Number.MAX_SAFE_INTEGER) {
386
+ const row = probe(after);
387
+ if (row === null) break;
388
+ if (row) {
389
+ keep(row);
390
+ after = row.salvage_rowid;
391
+ continue;
392
+ }
393
+ if (++damaged > SALVAGE_MAX_DAMAGED_REGIONS) break;
394
+ // An end-of-table result counts as clear: the gap below it may still hold readable rows.
395
+ let bad = after;
396
+ let clear = Number.MAX_SAFE_INTEGER;
397
+ for (let step = 1; after + step < clear; step *= 2) {
398
+ if (probe(after + step) !== undefined) clear = after + step;
399
+ else bad = after + step;
400
+ }
401
+ while (clear - bad > 1) {
402
+ const mid = bad + Math.floor((clear - bad) / 2);
403
+ if (probe(mid) === undefined) bad = mid;
404
+ else clear = mid;
405
+ }
406
+ // An interior key bounds its left subtree inclusively, so seeking past the last
407
+ // damaged rowid descends into the damage again and `clear` itself is skipped.
408
+ const boundary = probe(bad, clear);
409
+ if (boundary) keep(boundary);
410
+ after = clear;
411
+ }
412
+ return rows;
413
+ }
414
+
346
415
  /**
347
416
  * SQLite's busy result-code family — base `SQLITE_BUSY` plus the extended
348
417
  * variants `SQLITE_BUSY_RECOVERY` (concurrent WAL recovery), `SQLITE_BUSY_SNAPSHOT`,