Skip to content

Commit

Permalink
refactor: move port value to cloud sql instance
Browse files Browse the repository at this point in the history
Moves the definition of the `port` value to CloudSQLInstance, allowing
it to be exposed to drivers that might need that value (such as
`tedious`) while also making it so that the `port` value is defined in
the same place as `host`.

Relates to: #66
  • Loading branch information
ruyadorno committed May 17, 2023
1 parent a70d1bf commit 2759fa0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/cloud-sql-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class CloudSQLInstance {
public readonly instanceInfo: InstanceConnectionInfo;
public ephemeralCert?: SslCert;
public host?: string;
public port = 3307;
public privateKey?: string;
public serverCaCert?: SslCert;

Expand Down
12 changes: 10 additions & 2 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,28 @@ export class Connector {

return {
stream() {
const {instanceInfo, ephemeralCert, host, privateKey, serverCaCert} =
instances.getInstance({instanceConnectionName, ipType, authType});
const {
instanceInfo,
ephemeralCert,
host,
port,
privateKey,
serverCaCert,
} = instances.getInstance({instanceConnectionName, ipType, authType});

if (
instanceInfo &&
ephemeralCert &&
host &&
port &&
privateKey &&
serverCaCert
) {
return getSocket({
instanceInfo,
ephemeralCert,
host,
port,
privateKey,
serverCaCert,
});
Expand Down
4 changes: 3 additions & 1 deletion src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const DEFAULT_KEEP_ALIVE_DELAY_MS = 30 * 1000;
interface SocketOptions {
ephemeralCert: SslCert;
host: string;
port: number;
instanceInfo: InstanceConnectionInfo;
privateKey: string;
serverCaCert: SslCert;
Expand Down Expand Up @@ -49,13 +50,14 @@ export function validateCertificate(instanceInfo: InstanceConnectionInfo) {
export function getSocket({
ephemeralCert,
host,
port,
instanceInfo,
privateKey,
serverCaCert,
}: SocketOptions): tls.TLSSocket {
const socketOpts = {
host,
port: 3307,
port,
secureContext: tls.createSecureContext({
ca: serverCaCert.cert,
cert: ephemeralCert.cert,
Expand Down
1 change: 1 addition & 0 deletions test/cloud-sql-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ t.test('CloudSQLInstance', async t => {
t.same(instance.privateKey, CLIENT_KEY, 'should have expected privateKey');

t.same(instance.host, '127.0.0.1', 'should have expected host');
t.same(instance.port, 3307, 'should have expected port');

t.same(
instance.serverCaCert.cert,
Expand Down
1 change: 1 addition & 0 deletions test/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ t.test('getSocket', async t => {
expirationTime: '2033-01-06T10:00:00.232Z',
},
host: '127.0.0.1',
port: 3307,
privateKey: CLIENT_KEY,
serverCaCert: {
cert: CA_CERT,
Expand Down

0 comments on commit 2759fa0

Please sign in to comment.