diff --git a/packages/storage/src/config.ts b/packages/storage/src/config.ts index 86d6de7..8b8ba25 100644 --- a/packages/storage/src/config.ts +++ b/packages/storage/src/config.ts @@ -85,9 +85,16 @@ export function resolveStorageConfig(env: Record): S if (!authToken) { throw new Error(`Remote DB URL requires ${ENV.authToken}.`); } + let hostname: string; + try { + hostname = new URL(url).hostname; + } catch { + throw new Error(`${ENV.url} must be a valid remote libSQL/Turso URL or a local file path.`); + } + // Managed Turso (*.turso.io) gets the cloud tier (managed backups); any other // remote libSQL the user runs themselves is self-hosted. - const tier: StorageTier = /\.turso\.io$/.test(new URL(url).hostname) ? 'cloud' : 'self-hosted'; + const tier: StorageTier = /\.turso\.io$/.test(hostname) ? 'cloud' : 'self-hosted'; return { driver: 'turso', tier, url, authToken }; } diff --git a/packages/storage/src/index.test.ts b/packages/storage/src/index.test.ts index bca9994..03e501e 100644 --- a/packages/storage/src/index.test.ts +++ b/packages/storage/src/index.test.ts @@ -37,6 +37,15 @@ describe('@tronbrowser/storage config', () => { expect(() => resolveStorageConfig({ [ENV.url]: 'libsql://x.turso.io' })).toThrow(/AUTH_TOKEN/); }); + it('reports invalid remote URLs as storage configuration errors', () => { + expect(() => + resolveStorageConfig({ + [ENV.url]: 'not a valid remote url', + [ENV.authToken]: 'tok', + }), + ).toThrow(/TRONBROWSER_DB_URL must be a valid remote libSQL\/Turso URL/); + }); + it('errors when nothing is configured', () => { expect(() => resolveStorageConfig({})).toThrow(/No storage configured/); });