|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The two doors that reached wasm SQLite's open over a file it cannot read now |
| 5 | + * refuse that file at connect, naming it, and leave it as it was found (#25019): |
| 6 | + * |
| 7 | + * (a) a declared `sqlite` datasource on a development boot whose native addon |
| 8 | + * cannot LOAD. The native rung never reads the file, so a file that is not |
| 9 | + * a database, or a damaged one, reached the wasm rung; |
| 10 | + * (b) a datasource declared `driver: 'sqlite-wasm'`, in development AND |
| 11 | + * production. |
| 12 | + * |
| 13 | + * Before, measured on the card: wasm SQLite's open moved the file to |
| 14 | + * `<file>.corrupt-<timestamp>`, wrote an empty database in its place, and the |
| 15 | + * datasource read `available`. Now the refusal is wasm SQLite's own, with the |
| 16 | + * native driver's class, code and words, and `resolveSqliteDriver` re-throws it |
| 17 | + * instead of trying the in-memory rung. |
| 18 | + * |
| 19 | + * Real `better-sqlite3`, real wasm SQLite and real files in a scratch |
| 20 | + * directory. The load failure is a real `ERR_DLOPEN_FAILED` at the seam |
| 21 | + * `./sqlite-driver-fallback-damaged-file-and-dangling-link.test.ts` uses. |
| 22 | + */ |
| 23 | + |
| 24 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 25 | +import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, truncateSync, writeFileSync } from 'node:fs'; |
| 26 | +import { tmpdir } from 'node:os'; |
| 27 | +import { join } from 'node:path'; |
| 28 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 29 | +import { |
| 30 | + NATIVE_SQLITE_MEMORY_FALLBACK_WARNING, |
| 31 | + NATIVE_SQLITE_WASM_FALLBACK_WARNING, |
| 32 | + resolveSqliteDriver, |
| 33 | + type ResolvedSqliteDriver, |
| 34 | +} from '../sqlite-driver-fallback.js'; |
| 35 | +import { DatasourceConnectionService } from '../datasource-connection-service.js'; |
| 36 | +import { createDefaultDatasourceDriverFactory } from '../default-datasource-driver-factory.js'; |
| 37 | + |
| 38 | +let dir: string; |
| 39 | +const opened: ResolvedSqliteDriver[] = []; |
| 40 | + |
| 41 | +beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'os-25019-wasm-')); }); |
| 42 | + |
| 43 | +afterEach(async () => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | + vi.unstubAllEnvs(); |
| 46 | + await Promise.all(opened.splice(0).map((r) => r.driver?.disconnect?.().catch(() => {}))); |
| 47 | + rmSync(dir, { recursive: true, force: true }); |
| 48 | +}); |
| 49 | + |
| 50 | +/** A real SQLite database, written by the native driver, holding one row in `t`. */ |
| 51 | +async function seedDatabase(filename: string): Promise<void> { |
| 52 | + const seed = new SqlDriver({ client: 'better-sqlite3', connection: { filename }, useNullAsDefault: true } as any); |
| 53 | + await seed.connect(); |
| 54 | + await seed.execute('CREATE TABLE t (a INTEGER)'); |
| 55 | + await seed.execute('INSERT INTO t (a) VALUES (7)'); |
| 56 | + await seed.disconnect(); |
| 57 | +} |
| 58 | + |
| 59 | +/** The two files wasm SQLite cannot read, each with the code both engines answer. */ |
| 60 | +const UNREADABLE = [ |
| 61 | + { |
| 62 | + label: 'a plain-text file', |
| 63 | + code: 'SQLITE_NOTADB', |
| 64 | + write: async (filename: string) => { writeFileSync(filename, 'id,name\n1,not a database\n'.repeat(20)); }, |
| 65 | + }, |
| 66 | + { |
| 67 | + label: 'a database truncated inside its header', |
| 68 | + code: 'SQLITE_CORRUPT', |
| 69 | + write: async (filename: string) => { await seedDatabase(filename); truncateSync(filename, 50); }, |
| 70 | + }, |
| 71 | +] as const; |
| 72 | + |
| 73 | +/** `promise`'s rejection; a failure naming the engine when it resolves instead. */ |
| 74 | +async function rejectionOf(promise: Promise<ResolvedSqliteDriver>): Promise<any> { |
| 75 | + let resolved: ResolvedSqliteDriver; |
| 76 | + try { |
| 77 | + resolved = await promise; |
| 78 | + } catch (error) { |
| 79 | + return error; |
| 80 | + } |
| 81 | + opened.push(resolved); |
| 82 | + throw new Error(`resolved to engine '${resolved.engine}' for a file the datasource cannot use`); |
| 83 | +} |
| 84 | + |
| 85 | +/** The refusal: the driver's class and code, the file named, and how to move it aside. */ |
| 86 | +function expectFileRefusal(error: any, filename: string, code: string): void { |
| 87 | + expect(error?.name).toBe('SqliteDatabaseFileError'); |
| 88 | + expect(error?.code).toBe(code); |
| 89 | + expect(error?.filename).toBe(filename); |
| 90 | + expect(String(error?.message)).toContain(`SQLite database file '${filename}'`); |
| 91 | + expect(String(error?.message)).toContain(`move the file aside yourself (mv '${filename}' '${filename}.unreadable')`); |
| 92 | +} |
| 93 | + |
| 94 | +/** The bytes it was found with, and nothing beside it: no `.corrupt-*` sibling, no empty database. */ |
| 95 | +function expectLeftAsFound(filename: string, bytes: Buffer): void { |
| 96 | + expect(readFileSync(filename).equals(bytes)).toBe(true); |
| 97 | + expect(readdirSync(dir)).toEqual(['archive.db']); |
| 98 | +} |
| 99 | + |
| 100 | +function connectionService(mode: 'development' | 'production') { |
| 101 | + const registered: string[] = []; |
| 102 | + const engine = { |
| 103 | + registerDriver: (driver: { name?: string }) => { registered.push(String(driver?.name)); }, |
| 104 | + getDriverByName: (name: string) => (registered.includes(name) ? {} : undefined), |
| 105 | + }; |
| 106 | + const service = new DatasourceConnectionService({ |
| 107 | + factory: () => createDefaultDatasourceDriverFactory({ dev: mode === 'development' }), |
| 108 | + engine: () => engine as any, |
| 109 | + }); |
| 110 | + return { service, registered }; |
| 111 | +} |
| 112 | + |
| 113 | +/** A real `ERR_DLOPEN_FAILED`: Node asked to load, as an addon, a file that is not one. */ |
| 114 | +function realDlopenFailure(): Error { |
| 115 | + const notAnAddon = join(dir, '..', `${dir.split('/').pop()}-not-an-addon.node`); |
| 116 | + writeFileSync(notAnAddon, 'x'); |
| 117 | + try { |
| 118 | + process.dlopen({ exports: {} }, notAnAddon); |
| 119 | + } catch (error) { |
| 120 | + return error as Error; |
| 121 | + } finally { |
| 122 | + rmSync(notAnAddon, { force: true }); |
| 123 | + } |
| 124 | + throw new Error('process.dlopen loaded a file that is not an addon'); |
| 125 | +} |
| 126 | + |
| 127 | +/** Make the native addon fail to load with `error`, where `better-sqlite3` loads it (the #24950 seam). */ |
| 128 | +async function failNativeAddonLoad(error: Error): Promise<void> { |
| 129 | + const probe = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); |
| 130 | + const clientProto = Object.getPrototypeOf((probe as any).knex.client); |
| 131 | + await probe.disconnect(); |
| 132 | + expect(Object.prototype.hasOwnProperty.call(clientProto, 'acquireRawConnection')).toBe(true); |
| 133 | + vi.spyOn(clientProto, 'acquireRawConnection').mockRejectedValue(error); |
| 134 | +} |
| 135 | + |
| 136 | +describe('(a) a declared sqlite datasource whose native addon cannot load, over a file wasm SQLite cannot read (#25019)', () => { |
| 137 | + for (const file of UNREADABLE) { |
| 138 | + it(`${file.label}: resolveSqliteDriver rejects with wasm SQLite's ${file.code} refusal, never the in-memory rung, and leaves the file as it was`, async () => { |
| 139 | + const filename = join(dir, 'archive.db'); |
| 140 | + await file.write(filename); |
| 141 | + const bytes = readFileSync(filename); |
| 142 | + await failNativeAddonLoad(realDlopenFailure()); |
| 143 | + const warn = vi.fn(); |
| 144 | + |
| 145 | + expectFileRefusal(await rejectionOf(resolveSqliteDriver({ filename, dev: true, warn })), filename, file.code); |
| 146 | + |
| 147 | + expect(warn).not.toHaveBeenCalledWith(NATIVE_SQLITE_WASM_FALLBACK_WARNING); |
| 148 | + expect(warn).not.toHaveBeenCalledWith(NATIVE_SQLITE_MEMORY_FALLBACK_WARNING); |
| 149 | + expectLeftAsFound(filename, bytes); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + it('DatasourceConnectionService, development: reports the datasource failed naming the file, registers no driver, and moves nothing aside', async () => { |
| 154 | + vi.stubEnv('NODE_ENV', 'development'); |
| 155 | + const filename = join(dir, 'archive.db'); |
| 156 | + writeFileSync(filename, 'not a database'); |
| 157 | + const bytes = readFileSync(filename); |
| 158 | + await failNativeAddonLoad(realDlopenFailure()); |
| 159 | + const { service, registered } = connectionService('development'); |
| 160 | + |
| 161 | + await service.connectDeclared({ |
| 162 | + datasources: [{ name: 'archive', driver: 'sqlite', config: { filename }, autoConnect: true }], |
| 163 | + }); |
| 164 | + |
| 165 | + const state = service.getConnectionState('archive'); |
| 166 | + expect(state?.availability).toBe('failed'); |
| 167 | + expect(state?.reason).toContain(filename); |
| 168 | + expect(registered).toEqual([]); |
| 169 | + expectLeftAsFound(filename, bytes); |
| 170 | + }); |
| 171 | +}); |
| 172 | + |
| 173 | +describe('(b) a datasource declared driver: sqlite-wasm, over a file wasm SQLite cannot read (#25019)', () => { |
| 174 | + for (const mode of ['development', 'production'] as const) { |
| 175 | + for (const file of UNREADABLE) { |
| 176 | + it(`${mode}, ${file.label}: reports the datasource failed with ${file.code} naming the file, not available, and moves nothing aside`, async () => { |
| 177 | + vi.stubEnv('NODE_ENV', mode); |
| 178 | + const filename = join(dir, 'archive.db'); |
| 179 | + await file.write(filename); |
| 180 | + const bytes = readFileSync(filename); |
| 181 | + const { service, registered } = connectionService(mode); |
| 182 | + |
| 183 | + await service.connectDeclared({ |
| 184 | + datasources: [{ name: 'archive', driver: 'sqlite-wasm', config: { filename }, autoConnect: true }], |
| 185 | + }); |
| 186 | + |
| 187 | + const state = service.getConnectionState('archive'); |
| 188 | + expect(state?.availability).toBe('failed'); |
| 189 | + expect(state?.reason).toContain(`SQLite database file '${filename}'`); |
| 190 | + expect(state?.reason).toContain(file.code === 'SQLITE_NOTADB' ? 'is not a SQLite database' : 'is damaged'); |
| 191 | + expect(registered).toEqual([]); |
| 192 | + expectLeftAsFound(filename, bytes); |
| 193 | + }); |
| 194 | + } |
| 195 | + } |
| 196 | +}); |
| 197 | + |
| 198 | +describe('CONTROLS: what keeps today\'s behaviour (#25019)', () => { |
| 199 | + it('a native addon that cannot load, over an absent file, still steps down to wasm SQLite, which creates it', async () => { |
| 200 | + const filename = join(dir, 'new', 'archive.db'); |
| 201 | + await failNativeAddonLoad(realDlopenFailure()); |
| 202 | + const warn = vi.fn(); |
| 203 | + const resolved = await resolveSqliteDriver({ filename, dev: true, warn }); |
| 204 | + opened.push(resolved); |
| 205 | + expect(resolved.engine).toBe('sqlite-wasm'); |
| 206 | + expect(warn).toHaveBeenCalledWith(NATIVE_SQLITE_WASM_FALLBACK_WARNING); |
| 207 | + await resolved.driver.execute('CREATE TABLE t (a INTEGER)'); |
| 208 | + await resolved.driver.flush(); |
| 209 | + expect(existsSync(filename)).toBe(true); |
| 210 | + }); |
| 211 | + |
| 212 | + for (const mode of ['development', 'production'] as const) { |
| 213 | + it(`${mode}: a datasource declared driver: sqlite-wasm over a real database is available and reads its rows`, async () => { |
| 214 | + vi.stubEnv('NODE_ENV', mode); |
| 215 | + const filename = join(dir, 'archive.db'); |
| 216 | + await seedDatabase(filename); |
| 217 | + const { service, registered } = connectionService(mode); |
| 218 | + |
| 219 | + await service.connectDeclared({ |
| 220 | + datasources: [{ name: 'archive', driver: 'sqlite-wasm', config: { filename }, autoConnect: true }], |
| 221 | + }); |
| 222 | + |
| 223 | + expect(service.getConnectionState('archive')?.availability).toBe('available'); |
| 224 | + expect(registered.length).toBe(1); |
| 225 | + expect(readdirSync(dir).filter((name) => name.includes('.corrupt-'))).toEqual([]); |
| 226 | + }); |
| 227 | + } |
| 228 | +}); |
0 commit comments