Skip to content

Commit

Permalink
fix(datasource-sql): do not crash at connection time when using proxy…
Browse files Browse the repository at this point in the history
… without providing port (#705)
  • Loading branch information
romain-gilliotte authored May 30, 2023
1 parent 813e4ff commit c3d7d60
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/datasource-sql/src/connection/reverse-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,18 @@ export default class ReverseProxy {
}

private get destinationPort(): number {
return Number(
this.destination.uri ? new URL(this.destination.uri).port : this.destination.port,
);
// Use port from object or uri if provided
if (this.destination.port) return this.destination.port;
if (this.destination.uri && new URL(this.destination.uri).port)
return Number(new URL(this.destination.uri).port);

// Use default port for known dialects otherwise
if (this.destination.dialect === 'postgres') return 5432;
if (this.destination.dialect === 'mysql' || this.destination.dialect === 'mariadb') return 3306;
if (this.destination.dialect === 'mssql') return 1433;

// Otherwise throw an error
throw new Error('Port is required');
}

private async onConnection(socket: net.Socket): Promise<void> {
Expand Down

0 comments on commit c3d7d60

Please sign in to comment.