Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/storage/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ export function resolveStorageConfig(env: Record<string, string | undefined>): 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 };
}

Expand Down
9 changes: 9 additions & 0 deletions packages/storage/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
Expand Down
Loading