|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A file wasm SQLite cannot read is refused at connect, naming the file, with |
| 5 | + * the code the native driver answers for the same file, and is left exactly as |
| 6 | + * it was found (#25019): nothing renamed, nothing written beside it, no empty |
| 7 | + * database in its place. |
| 8 | + * |
| 9 | + * Before, `WasmSqliteConnection.open()` moved such a file to |
| 10 | + * `<file>.corrupt-<timestamp>` and started an empty database, so a declared |
| 11 | + * `sqlite-wasm` datasource over an operator's file read as connected, in |
| 12 | + * development and production alike. |
| 13 | + * |
| 14 | + * Real sql.js and real files in a scratch directory. |
| 15 | + */ |
| 16 | + |
| 17 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 18 | +import { mkdtempSync, readdirSync, readFileSync, rmSync, truncateSync, writeFileSync, statSync, existsSync } from 'node:fs'; |
| 19 | +import { tmpdir } from 'node:os'; |
| 20 | +import { join } from 'node:path'; |
| 21 | + |
| 22 | +import { SqliteWasmDriver, WasmSqliteConnection } from './index.js'; |
| 23 | + |
| 24 | +let dir: string; |
| 25 | +const drivers: SqliteWasmDriver[] = []; |
| 26 | + |
| 27 | +beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'wasm-25019-')); }); |
| 28 | + |
| 29 | +afterEach(async () => { |
| 30 | + await Promise.all(drivers.splice(0).map((d) => d.disconnect().catch(() => {}))); |
| 31 | + rmSync(dir, { recursive: true, force: true }); |
| 32 | +}); |
| 33 | + |
| 34 | +function driverFor(filename: string): SqliteWasmDriver { |
| 35 | + const driver = new SqliteWasmDriver({ filename, persist: 'on-write', logger: { warn: () => {} } }); |
| 36 | + drivers.push(driver); |
| 37 | + return driver; |
| 38 | +} |
| 39 | + |
| 40 | +/** A real database written by this driver, holding one row in `t`. Its size in bytes. */ |
| 41 | +async function seedDatabase(filename: string): Promise<number> { |
| 42 | + const seed = new SqliteWasmDriver({ filename, persist: 'on-disconnect' }); |
| 43 | + await seed.connect(); |
| 44 | + await seed.execute('CREATE TABLE t (a INTEGER)'); |
| 45 | + await seed.execute('INSERT INTO t (a) VALUES (7)'); |
| 46 | + await seed.disconnect(); |
| 47 | + return statSync(filename).size; |
| 48 | +} |
| 49 | + |
| 50 | +/** `connect()`'s rejection; a failure when it resolves instead. */ |
| 51 | +async function connectRefusal(driver: SqliteWasmDriver): Promise<any> { |
| 52 | + try { |
| 53 | + await driver.connect(); |
| 54 | + } catch (error) { |
| 55 | + return error; |
| 56 | + } |
| 57 | + throw new Error('connect() resolved over a file wasm SQLite cannot read'); |
| 58 | +} |
| 59 | + |
| 60 | +/** The driver's refusal: the native driver's class and code, the file named, and how to move it aside. */ |
| 61 | +function expectFileRefusal(error: any, filename: string, code: 'SQLITE_NOTADB' | 'SQLITE_CORRUPT'): void { |
| 62 | + expect(error?.name).toBe('SqliteDatabaseFileError'); |
| 63 | + expect(error?.code).toBe(code); |
| 64 | + expect(error?.filename).toBe(filename); |
| 65 | + expect(String(error?.message)).toContain(`SQLite database file '${filename}'`); |
| 66 | + expect(String(error?.message)).toContain( |
| 67 | + `To start from an empty database, move the file aside yourself (mv '${filename}' '${filename}.unreadable') and start again; to keep its data, restore a copy that opens.`, |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +describe('a file wasm SQLite cannot read is refused at connect and left as it was found (#25019)', () => { |
| 72 | + it('plain text: SQLITE_NOTADB, naming the file', async () => { |
| 73 | + const filename = join(dir, 'archive.db'); |
| 74 | + const bytes = Buffer.from('id,name\n1,not a database\n'.repeat(20)); |
| 75 | + writeFileSync(filename, bytes); |
| 76 | + |
| 77 | + const refusal = await connectRefusal(driverFor(filename)); |
| 78 | + expectFileRefusal(refusal, filename, 'SQLITE_NOTADB'); |
| 79 | + expect(String(refusal.message)).toContain('was left as it is'); |
| 80 | + |
| 81 | + expect(readdirSync(dir)).toEqual(['archive.db']); |
| 82 | + expect(readFileSync(filename).equals(bytes)).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it('a database truncated inside its 100-byte header: SQLITE_CORRUPT, naming the file', async () => { |
| 86 | + const filename = join(dir, 'archive.db'); |
| 87 | + await seedDatabase(filename); |
| 88 | + truncateSync(filename, 50); |
| 89 | + const bytes = readFileSync(filename); |
| 90 | + |
| 91 | + expectFileRefusal(await connectRefusal(driverFor(filename)), filename, 'SQLITE_CORRUPT'); |
| 92 | + |
| 93 | + expect(readdirSync(dir)).toEqual(['archive.db']); |
| 94 | + expect(readFileSync(filename).equals(bytes)).toBe(true); |
| 95 | + }); |
| 96 | + |
| 97 | + it('a database short of the pages its schema names: SQLITE_CORRUPT, naming the file', async () => { |
| 98 | + const filename = join(dir, 'archive.db'); |
| 99 | + const size = await seedDatabase(filename); |
| 100 | + truncateSync(filename, Math.floor(size / 2)); |
| 101 | + const bytes = readFileSync(filename); |
| 102 | + |
| 103 | + expectFileRefusal(await connectRefusal(driverFor(filename)), filename, 'SQLITE_CORRUPT'); |
| 104 | + |
| 105 | + expect(readdirSync(dir)).toEqual(['archive.db']); |
| 106 | + expect(readFileSync(filename).equals(bytes)).toBe(true); |
| 107 | + }); |
| 108 | + |
| 109 | + it('a refused file keeps the write-ahead log beside it: nothing is parked', async () => { |
| 110 | + const filename = join(dir, 'archive.db'); |
| 111 | + writeFileSync(filename, 'not a database'); |
| 112 | + const wal = Buffer.from('a stale write-ahead log'); |
| 113 | + writeFileSync(`${filename}-wal`, wal); |
| 114 | + |
| 115 | + expectFileRefusal(await connectRefusal(driverFor(filename)), filename, 'SQLITE_NOTADB'); |
| 116 | + |
| 117 | + expect(readdirSync(dir).sort()).toEqual(['archive.db', 'archive.db-wal']); |
| 118 | + expect(readFileSync(`${filename}-wal`).equals(wal)).toBe(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it('WasmSqliteConnection.open() itself rejects with the code and the file, and writes nothing', async () => { |
| 122 | + const filename = join(dir, 'archive.db'); |
| 123 | + writeFileSync(filename, 'not a database'); |
| 124 | + |
| 125 | + const conn = new WasmSqliteConnection({ filename, logger: { warn: () => {} } }); |
| 126 | + let refusal: any; |
| 127 | + await conn.open().catch((e: unknown) => { refusal = e; }); |
| 128 | + expect(refusal?.code).toBe('SQLITE_NOTADB'); |
| 129 | + expect(refusal?.filename).toBe(filename); |
| 130 | + expect(String(refusal?.message)).toContain(`mv '${filename}' '${filename}.unreadable'`); |
| 131 | + expect(String((refusal?.cause as Error)?.message)).toBe('file is not a database'); |
| 132 | + |
| 133 | + expect(readdirSync(dir)).toEqual(['archive.db']); |
| 134 | + expect(readFileSync(filename, 'utf8')).toBe('not a database'); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('CONTROLS: what keeps today\'s behaviour (#25019)', () => { |
| 139 | + it('a real database opens and answers', async () => { |
| 140 | + const filename = join(dir, 'archive.db'); |
| 141 | + await seedDatabase(filename); |
| 142 | + const driver = driverFor(filename); |
| 143 | + await driver.connect(); |
| 144 | + expect(await driver.execute('SELECT a FROM t')).toEqual([{ a: 7 }]); |
| 145 | + }); |
| 146 | + |
| 147 | + it('an absent file is created', async () => { |
| 148 | + const filename = join(dir, 'new', 'archive.db'); |
| 149 | + const driver = driverFor(filename); |
| 150 | + await driver.connect(); |
| 151 | + await driver.execute('CREATE TABLE t (a INTEGER)'); |
| 152 | + await driver.flush(); |
| 153 | + expect(existsSync(filename)).toBe(true); |
| 154 | + }); |
| 155 | + |
| 156 | + it('an empty file opens as an empty database', async () => { |
| 157 | + const filename = join(dir, 'archive.db'); |
| 158 | + writeFileSync(filename, ''); |
| 159 | + const driver = driverFor(filename); |
| 160 | + await driver.connect(); |
| 161 | + expect(await driver.execute('SELECT 1 AS one')).toEqual([{ one: 1 }]); |
| 162 | + expect(readdirSync(dir).filter((name) => name.includes('.corrupt-'))).toEqual([]); |
| 163 | + }); |
| 164 | +}); |
0 commit comments