@@ -5,6 +5,7 @@ import { type ConnectionOptions } from '@slonik/types';
55import { readFileSync } from 'node:fs' ;
66import { z } from 'zod' ;
77
8+ // eslint-disable-next-line complexity
89export const parseDsn = ( dsn : string ) : ConnectionOptions => {
910 if ( dsn . trim ( ) === '' ) {
1011 return { } ;
@@ -56,7 +57,17 @@ export const parseDsn = (dsn: string): ConnectionOptions => {
5657 . describe (
5758 'Specifies the location for the secret key used for the client certificate.' ,
5859 ) ,
59- sslmode : z . enum ( [ 'disable' , 'no-verify' , 'require' ] ) . optional ( ) ,
60+ sslmode : z
61+ . enum ( [
62+ 'allow' ,
63+ 'disable' ,
64+ 'no-verify' ,
65+ 'prefer' ,
66+ 'require' ,
67+ 'verify-ca' ,
68+ 'verify-full' ,
69+ ] )
70+ . optional ( ) ,
6071 sslrootcert : z
6172 . string ( )
6273 . optional ( )
@@ -78,49 +89,81 @@ export const parseDsn = (dsn: string): ConnectionOptions => {
7889 connectionOptions . sslMode = searchParameters . sslmode ;
7990 }
8091
81- let sslCert : string | undefined ;
82- let sslKey : string | undefined ;
83- let sslRootCert : string | undefined ;
92+ /**
93+ * Refer to https://github.com/brianc/node-postgres/pull/2709
94+ */
95+ if (
96+ searchParameters . sslcert ||
97+ searchParameters . sslkey ||
98+ searchParameters . sslrootcert ||
99+ searchParameters . sslmode
100+ ) {
101+ let sslCert : string | undefined ;
102+ let sslKey : string | undefined ;
103+ let sslRootCert : string | undefined ;
104+
105+ if ( searchParameters . sslcert ) {
106+ try {
107+ sslCert = readFileSync ( searchParameters . sslcert , 'utf8' ) ;
108+ } catch {
109+ throw new UnexpectedStateError ( 'Failed to read SSL certificate file.' ) ;
110+ }
111+ }
84112
85- if ( searchParameters . sslcert ) {
86- try {
87- sslCert = readFileSync ( searchParameters . sslcert , 'utf8' ) ;
88- } catch {
89- throw new UnexpectedStateError ( 'Failed to read SSL certificate file.' ) ;
113+ if ( searchParameters . sslkey ) {
114+ try {
115+ sslKey = readFileSync ( searchParameters . sslkey , 'utf8' ) ;
116+ } catch {
117+ throw new UnexpectedStateError ( 'Failed to read SSL key file.' ) ;
118+ }
90119 }
91- }
92120
93- if ( searchParameters . sslkey ) {
94- try {
95- sslKey = readFileSync ( searchParameters . sslkey , 'utf8' ) ;
96- } catch {
97- throw new UnexpectedStateError ( 'Failed to read SSL key file.' ) ;
121+ if ( searchParameters . sslrootcert ) {
122+ try {
123+ sslRootCert = readFileSync ( searchParameters . sslrootcert , 'utf8' ) ;
124+ } catch {
125+ throw new UnexpectedStateError (
126+ 'Failed to read SSL root certificate file.' ,
127+ ) ;
128+ }
98129 }
99- }
100130
101- if ( searchParameters . sslrootcert ) {
102- try {
103- sslRootCert = readFileSync ( searchParameters . sslrootcert , 'utf8' ) ;
104- } catch {
105- throw new UnexpectedStateError (
106- 'Failed to read SSL root certificate file.' ,
107- ) ;
131+ if ( sslCert || sslKey || sslRootCert ) {
132+ if ( ( sslCert && ! sslKey ) || ( ! sslCert && sslKey ) ) {
133+ throw new UnexpectedStateError (
134+ 'Both sslcert and sslkey must be provided together.' ,
135+ ) ;
136+ }
137+
138+ connectionOptions . ssl = {
139+ ca : sslRootCert ,
140+ cert : sslCert ,
141+ key : sslKey ,
142+ rejectUnauthorized : searchParameters . sslmode !== 'no-verify' ,
143+ } ;
108144 }
109145 }
110146
111- if ( sslCert || sslKey || sslRootCert ) {
112- if ( ( sslCert && ! sslKey ) || ( ! sslCert && sslKey ) ) {
113- throw new UnexpectedStateError (
114- 'Both sslcert and sslkey must be provided together.' ,
115- ) ;
147+ switch ( connectionOptions . sslMode ) {
148+ case 'disable' : {
149+ connectionOptions . ssl = false ;
150+ break ;
116151 }
117152
118- connectionOptions . ssl = {
119- ca : sslRootCert ,
120- cert : sslCert ,
121- key : sslKey ,
122- rejectUnauthorized : searchParameters . sslmode !== 'no-verify' ,
123- } ;
153+ case 'no-verify' : {
154+ connectionOptions . ssl = {
155+ ...connectionOptions . ssl ,
156+ rejectUnauthorized : false ,
157+ } ;
158+ break ;
159+ }
160+
161+ case 'prefer' :
162+ case 'require' :
163+ case 'verify-ca' :
164+ case 'verify-full' : {
165+ break ;
166+ }
124167 }
125168
126169 return connectionOptions ;
0 commit comments