|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// The standalone host refuses an OPAQUE scheme, and a `*.wasm.db` file name no |
| 4 | +// longer rescues a scheme nothing recognises (#23765). |
| 5 | +// |
| 6 | +// The module header's rule is "Unknown URL schemes throw — we never silently |
| 7 | +// fall back to sqlite". Two arms of `detectDriverFromUrl` broke it: |
| 8 | +// |
| 9 | +// - the scheme-less arm read "no `<scheme>://`" as "a file path", so an |
| 10 | +// opaque scheme — two or more characters and a `:` with no `//` |
| 11 | +// (`jdbc:postgresql://…`, `redis:h`) — opened a SQLite file at a path made |
| 12 | +// of the URL: `OS_DATABASE_URL=jdbc:postgresql://127.0.0.1:5999/db` booted |
| 13 | +// "ready" on `./jdbc:postgresql:/127.0.0.1:5999/db`; |
| 14 | +// - the `.wasm.db` test ran ahead of every scheme check, so |
| 15 | +// `redis://h/x.wasm.db` opened a WASM SQLite file under `./redis:/h/`. |
| 16 | +// |
| 17 | +// The cases hold three things: |
| 18 | +// |
| 19 | +// 1. both shapes meet the refusal an unrecognised `<scheme>://` already met, |
| 20 | +// unchanged — same error class, same own properties, same words with only |
| 21 | +// the scheme differing — through every door the URL arrives by, and its |
| 22 | +// first sentence names the scheme; |
| 23 | +// 2. the refusal names the scheme only: no own property of the thrown error |
| 24 | +// carries the placeholder username, password or token in the URL; |
| 25 | +// 3. CONTROL: a single letter and a `:` is a Windows drive and stays a path, |
| 26 | +// and every file spelling resolves to the kind AND the file it did. |
| 27 | + |
| 28 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 29 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 30 | +import { tmpdir } from 'node:os'; |
| 31 | +import { join } from 'node:path'; |
| 32 | +import { resolveStandaloneDatabase } from './standalone-stack.js'; |
| 33 | + |
| 34 | +// Placeholders only: nothing here is, or looks like, a real secret. |
| 35 | +const USER = 'PLACEHOLDER-USER'; |
| 36 | +const SECRET = 'PLACEHOLDER-SECRET'; |
| 37 | +const TOKEN = 'PLACEHOLDER-TOKEN'; |
| 38 | +/** A userinfo with a password, a host, a path and a token query. */ |
| 39 | +const CREDENTIALED_TAIL = `${USER}:${SECRET}@127.0.0.1:6399/0?authToken=${TOKEN}`; |
| 40 | + |
| 41 | +/** Env keys these tests write; restored after every case. */ |
| 42 | +const ENV_KEYS = [ |
| 43 | + 'OS_DATABASE_URL', |
| 44 | + 'DATABASE_URL', |
| 45 | + 'TURSO_DATABASE_URL', |
| 46 | + 'OS_DATABASE_DRIVER', |
| 47 | + 'OS_HOME', |
| 48 | + 'OS_ARTIFACT_PATH', |
| 49 | +] as const; |
| 50 | +const ORIGINAL_ENV: Record<string, string | undefined> = Object.fromEntries( |
| 51 | + ENV_KEYS.map((k) => [k, process.env[k]]), |
| 52 | +); |
| 53 | + |
| 54 | +let root: string; |
| 55 | +beforeEach(() => { |
| 56 | + for (const key of ENV_KEYS) delete process.env[key]; |
| 57 | + root = mkdtempSync(join(tmpdir(), 'os-opaque-scheme-refusal-')); |
| 58 | + // Every rung that could answer before the one under test points at this |
| 59 | + // empty directory, so no developer state leaks into a case. |
| 60 | + process.env.OS_HOME = root; |
| 61 | +}); |
| 62 | +afterEach(() => { |
| 63 | + try { rmSync(root, { recursive: true, force: true }); } catch { /* noop */ } |
| 64 | + for (const key of ENV_KEYS) { |
| 65 | + const original = ORIGINAL_ENV[key]; |
| 66 | + if (original === undefined) delete process.env[key]; |
| 67 | + else process.env[key] = original; |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +/** The error `run` throws; fails the case when it throws nothing. */ |
| 72 | +function refusalOf(run: () => unknown): Error { |
| 73 | + try { |
| 74 | + run(); |
| 75 | + } catch (err) { |
| 76 | + return err as Error; |
| 77 | + } |
| 78 | + throw new Error('expected a refusal, but the URL resolved'); |
| 79 | +} |
| 80 | + |
| 81 | +/** A compiled artifact whose declared default datasource carries `url`. */ |
| 82 | +function artifactDeclaring(url: string): string { |
| 83 | + const artifactPath = join(root, 'objectstack.json'); |
| 84 | + writeFileSync(artifactPath, JSON.stringify({ |
| 85 | + datasources: [{ name: 'primary', driver: 'postgres', config: { url } }], |
| 86 | + datasourceMapping: [{ default: true, datasource: 'primary' }], |
| 87 | + })); |
| 88 | + return artifactPath; |
| 89 | +} |
| 90 | + |
| 91 | +/** The three doors a database URL reaches this host by. */ |
| 92 | +const DOORS: ReadonlyArray<readonly [door: string, run: (url: string) => unknown]> = [ |
| 93 | + ['the databaseUrl option (--database)', (url) => resolveStandaloneDatabase({ databaseUrl: url })], |
| 94 | + ['OS_DATABASE_URL', (url) => { |
| 95 | + process.env.OS_DATABASE_URL = url; |
| 96 | + return resolveStandaloneDatabase({ projectRoot: root }); |
| 97 | + }], |
| 98 | + ['a declared default datasource', (url) => |
| 99 | + resolveStandaloneDatabase({ projectRoot: root, artifactPath: artifactDeclaring(url) })], |
| 100 | +]; |
| 101 | + |
| 102 | +/** |
| 103 | + * The refusal an unrecognised `<scheme>://` has always met, as the reference |
| 104 | + * the widened refusal is compared against — so the comparison pins sameness, |
| 105 | + * never the prose. |
| 106 | + */ |
| 107 | +const REFERENCE_SCHEME = 'wat://'; |
| 108 | +function referenceRefusal(): Error { |
| 109 | + return refusalOf(() => resolveStandaloneDatabase({ databaseUrl: `${REFERENCE_SCHEME}nope` })); |
| 110 | +} |
| 111 | + |
| 112 | +/** The unsupported-scheme refusal, unchanged, naming `scheme` and nothing of the URL. */ |
| 113 | +function expectUnsupportedSchemeRefusal(err: Error, scheme: string): void { |
| 114 | + const reference = referenceRefusal(); |
| 115 | + // 1. The same refusal: class, own properties (a `code`, were one ever |
| 116 | + // added, included) and words, with only the scheme differing. |
| 117 | + expect(err.constructor).toBe(Error); |
| 118 | + expect(err.constructor).toBe(reference.constructor); |
| 119 | + expect(Object.getOwnPropertyNames(err).sort()).toEqual(Object.getOwnPropertyNames(reference).sort()); |
| 120 | + expect((err as { code?: unknown }).code).toBe((reference as { code?: unknown }).code); |
| 121 | + expect(err.cause).toBeUndefined(); |
| 122 | + expect(err.message.startsWith(`[StandaloneStack] Unsupported database URL scheme "${scheme}". `), |
| 123 | + `the first sentence does not name "${scheme}": ${err.message}`).toBe(true); |
| 124 | + expect(err.message.replace(`"${scheme}"`, '"SCHEME"')) |
| 125 | + .toBe(reference.message.replace(`"${REFERENCE_SCHEME}"`, '"SCHEME"')); |
| 126 | + // 2. The scheme only. Positive control first: the serialisation really |
| 127 | + // holds the message, so the negative reads below read something. |
| 128 | + const everything = JSON.stringify(err, Object.getOwnPropertyNames(err)); |
| 129 | + expect(everything).toContain(JSON.stringify(err.message)); |
| 130 | + for (const placeholder of [USER, SECRET, TOKEN]) { |
| 131 | + expect(everything, `the refusal carries ${placeholder}`).not.toContain(placeholder); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +describe('an OPAQUE scheme is refused, naming the scheme, never the URL', () => { |
| 136 | + // Two or more characters and a `:` with no `//`. The scheme ends at the |
| 137 | + // FIRST `:`, so it cannot hold a userinfo, a host or a path. |
| 138 | + const OPAQUE: ReadonlyArray<readonly [url: string, scheme: string]> = [ |
| 139 | + // The card's reproduction, and the same shape carrying credentials. |
| 140 | + ['jdbc:postgresql://127.0.0.1:5999/db', 'jdbc:'], |
| 141 | + [`jdbc:postgresql://${CREDENTIALED_TAIL}`, 'jdbc:'], |
| 142 | + ['redis:h', 'redis:'], |
| 143 | + [`redis:${CREDENTIALED_TAIL}`, 'redis:'], |
| 144 | + // A scheme this host recognises is opaque too when its `//` is missing: |
| 145 | + // it is not a file path either. |
| 146 | + ['libsql:db.example.turso.io', 'libsql:'], |
| 147 | + // A file name whose FIRST segment holds a colon reads as a scheme, as on |
| 148 | + // the CLI host; `./backup:2026.db` and `file:backup:2026.db` name that |
| 149 | + // file (CONTROL below). |
| 150 | + ['backup:2026.db', 'backup:'], |
| 151 | + ]; |
| 152 | + |
| 153 | + for (const [door, run] of DOORS) { |
| 154 | + it.each(OPAQUE)(`%s via ${door} → refused as %s`, (url, scheme) => { |
| 155 | + expectUnsupportedSchemeRefusal(refusalOf(() => run(url)), scheme); |
| 156 | + }); |
| 157 | + } |
| 158 | +}); |
| 159 | + |
| 160 | +describe('a `*.wasm.db` file name no longer rescues a scheme nothing recognises', () => { |
| 161 | + // It selects the WASM engine for a FILE only: a `file:` URL or a value with |
| 162 | + // no scheme (the CONTROL below). |
| 163 | + const RESCUED: ReadonlyArray<readonly [url: string, scheme: string]> = [ |
| 164 | + ['redis://h/x.wasm.db', 'redis://'], |
| 165 | + // The URL must END in `.wasm.db` for the old test to have rescued it, so |
| 166 | + // the token rides in the path here, not in a trailing query. |
| 167 | + [`redis://${USER}:${SECRET}@127.0.0.1:6399/${TOKEN}/x.wasm.db`, 'redis://'], |
| 168 | + ['https://example.com/app.wasm.db', 'https://'], |
| 169 | + ['jdbc:x/app.wasm.db', 'jdbc:'], |
| 170 | + ]; |
| 171 | + |
| 172 | + for (const [door, run] of DOORS) { |
| 173 | + it.each(RESCUED)(`%s via ${door} → refused as %s`, (url, scheme) => { |
| 174 | + expectUnsupportedSchemeRefusal(refusalOf(() => run(url)), scheme); |
| 175 | + }); |
| 176 | + } |
| 177 | +}); |
| 178 | + |
| 179 | +describe('CONTROL: a drive letter, a file: URL and a plain path open the file they did', () => { |
| 180 | + it.each([ |
| 181 | + // A single letter and a `:` is a Windows drive, not a scheme. |
| 182 | + ['C:\\data\\x.db', 'sqlite', 'C:\\data\\x.db'], |
| 183 | + ['C:/data/x.db', 'sqlite', 'C:/data/x.db'], |
| 184 | + ['c:\\data\\x.wasm.db', 'sqlite-wasm', 'c:\\data\\x.wasm.db'], |
| 185 | + ['file:./x.db', 'sqlite', './x.db'], |
| 186 | + ['sqlite:./x.db', 'sqlite', './x.db'], |
| 187 | + ['./x.db', 'sqlite', './x.db'], |
| 188 | + ['./x.wasm.db', 'sqlite-wasm', './x.wasm.db'], |
| 189 | + ['file:./x.wasm.db', 'sqlite-wasm', './x.wasm.db'], |
| 190 | + ['wasm-sqlite:///tmp/x.db', 'sqlite-wasm', '/tmp/x.db'], |
| 191 | + ['data/app', 'sqlite', 'data/app'], |
| 192 | + // A `:` past the first path segment is not a scheme either, and `file:` |
| 193 | + // names a file whose own name holds one. |
| 194 | + ['./data/app:v2.db', 'sqlite', './data/app:v2.db'], |
| 195 | + ['./backup:2026.db', 'sqlite', './backup:2026.db'], |
| 196 | + ['file:backup:2026.db', 'sqlite', 'backup:2026.db'], |
| 197 | + [':memory:', 'sqlite', null], |
| 198 | + ])('%s → %s, file %s', (url, driver, file) => { |
| 199 | + const resolved = resolveStandaloneDatabase({ databaseUrl: url }); |
| 200 | + expect(resolved.driver).toBe(driver); |
| 201 | + expect(resolved.sqliteFile).toBe(file); |
| 202 | + }); |
| 203 | + |
| 204 | + it.each(['postgres', 'mysql', 'mongodb'])( |
| 205 | + 'a credential-bearing %s:// URL still resolves, credential intact', |
| 206 | + (scheme) => { |
| 207 | + const url = `${scheme}://${CREDENTIALED_TAIL}`; |
| 208 | + expect(resolveStandaloneDatabase({ databaseUrl: url }).url).toBe(url); |
| 209 | + }, |
| 210 | + ); |
| 211 | +}); |
0 commit comments