1616 * See the License for the specific language governing permissions and
1717 * limitations under the License.
1818 */
19- import net from " net" ;
20- import tls from " tls" ;
21- import fs from "fs" ;
22- import path from " path" ;
23- import { EOL } from "os" ;
24- import { NodeBuffer } from " ./buf" ;
25- import { ENCRYPTION_OFF , isEmptyObjectOrNull } from " ./util" ;
26- import { newError , SESSION_EXPIRED } from " ./../error" ;
19+ import net from ' net' ;
20+ import tls from ' tls' ;
21+ import fs from 'fs' ;
22+ import path from ' path' ;
23+ import { EOL } from 'os' ;
24+ import { NodeBuffer } from ' ./buf' ;
25+ import { ENCRYPTION_OFF , isEmptyObjectOrNull } from ' ./util' ;
26+ import { newError } from ' ./../error' ;
2727
2828let _CONNECTION_IDGEN = 0 ;
2929
@@ -107,13 +107,13 @@ const TrustStrategy = {
107107 /**
108108 * @deprecated Since version 1.0. Will be deleted in a future version. {@link #TRUST_CUSTOM_CA_SIGNED_CERTIFICATES}.
109109 */
110- TRUST_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
110+ TRUST_SIGNED_CERTIFICATES : function ( config , onSuccess , onFailure ) {
111111 console . log ( "`TRUST_SIGNED_CERTIFICATES` has been deprecated as option and will be removed in a future version of " +
112112 "the driver. Please use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." ) ;
113- return TrustStrategy . TRUST_CUSTOM_CA_SIGNED_CERTIFICATES ( opts , onSuccess , onFailure ) ;
113+ return TrustStrategy . TRUST_CUSTOM_CA_SIGNED_CERTIFICATES ( config , onSuccess , onFailure ) ;
114114 } ,
115- TRUST_CUSTOM_CA_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
116- if ( ! opts . trustedCertificates || opts . trustedCertificates . length == 0 ) {
115+ TRUST_CUSTOM_CA_SIGNED_CERTIFICATES : function ( config , onSuccess , onFailure ) {
116+ if ( ! config . trustedCertificates || config . trustedCertificates . length = == 0 ) {
117117 onFailure ( newError ( "You are using TRUST_CUSTOM_CA_SIGNED_CERTIFICATES as the method " +
118118 "to verify trust for encrypted connections, but have not configured any " +
119119 "trustedCertificates. You must specify the path to at least one trusted " +
@@ -124,13 +124,13 @@ const TrustStrategy = {
124124 }
125125
126126 let tlsOpts = {
127- ca : opts . trustedCertificates . map ( ( f ) => fs . readFileSync ( f ) ) ,
127+ ca : config . trustedCertificates . map ( ( f ) => fs . readFileSync ( f ) ) ,
128128 // Because we manually check for this in the connect callback, to give
129129 // a more helpful error to the user
130130 rejectUnauthorized : false
131131 } ;
132132
133- let socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
133+ let socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
134134 if ( ! socket . authorized ) {
135135 onFailure ( newError ( "Server certificate is not trusted. If you trust the database you are connecting to, add" +
136136 " the signing certificate, or the server certificate, to the list of certificates trusted by this driver" +
@@ -145,14 +145,14 @@ const TrustStrategy = {
145145 socket . on ( 'error' , onFailure ) ;
146146 return socket ;
147147 } ,
148- TRUST_SYSTEM_CA_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
148+ TRUST_SYSTEM_CA_SIGNED_CERTIFICATES : function ( config , onSuccess , onFailure ) {
149149
150150 let tlsOpts = {
151151 // Because we manually check for this in the connect callback, to give
152152 // a more helpful error to the user
153153 rejectUnauthorized : false
154154 } ;
155- let socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
155+ let socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
156156 if ( ! socket . authorized ) {
157157 onFailure ( newError ( "Server certificate is not trusted. If you trust the database you are connecting to, use " +
158158 "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES and add" +
@@ -171,7 +171,7 @@ const TrustStrategy = {
171171 /**
172172 * @deprecated in 1.1 in favour of {@link #TRUST_ALL_CERTIFICATES}. Will be deleted in a future version.
173173 */
174- TRUST_ON_FIRST_USE : function ( opts , onSuccess , onFailure ) {
174+ TRUST_ON_FIRST_USE : function ( config , onSuccess , onFailure ) {
175175 console . log ( "`TRUST_ON_FIRST_USE` has been deprecated as option and will be removed in a future version of " +
176176 "the driver. Please use `TRUST_ALL_CERTIFICATES` instead." ) ;
177177
@@ -180,7 +180,7 @@ const TrustStrategy = {
180180 rejectUnauthorized : false
181181 } ;
182182
183- let socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
183+ let socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
184184 var serverCert = socket . getPeerCertificate ( /*raw=*/ true ) ;
185185
186186 if ( ! serverCert . raw ) {
@@ -195,9 +195,9 @@ const TrustStrategy = {
195195 return ;
196196 }
197197
198- var serverFingerprint = require ( 'crypto' ) . createHash ( 'sha512' ) . update ( serverCert . raw ) . digest ( "hex" ) ;
199- let knownHostsPath = opts . knownHosts || path . join ( userHome ( ) , ".neo4j" , "known_hosts" ) ;
200- let serverId = opts . host + ":" + opts . port ;
198+ const serverFingerprint = require ( 'crypto' ) . createHash ( 'sha512' ) . update ( serverCert . raw ) . digest ( "hex" ) ;
199+ const knownHostsPath = config . knownHostsPath || path . join ( userHome ( ) , ".neo4j" , "known_hosts" ) ;
200+ const serverId = config . host + ":" + config . port ;
201201
202202 loadFingerprint ( serverId , knownHostsPath , ( knownFingerprint ) => {
203203 if ( knownFingerprint === serverFingerprint ) {
@@ -228,11 +228,11 @@ const TrustStrategy = {
228228 return socket ;
229229 } ,
230230
231- TRUST_ALL_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
231+ TRUST_ALL_CERTIFICATES : function ( config , onSuccess , onFailure ) {
232232 const tlsOpts = {
233233 rejectUnauthorized : false
234234 } ;
235- const socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
235+ const socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
236236 const certificate = socket . getPeerCertificate ( ) ;
237237 if ( isEmptyObjectOrNull ( certificate ) ) {
238238 onFailure ( newError ( "Secure connection was successful but server did not return any valid " +
@@ -249,16 +249,23 @@ const TrustStrategy = {
249249 }
250250} ;
251251
252- function connect ( opts , onSuccess , onFailure = ( ( ) => null ) ) {
252+ /**
253+ * Connect using node socket.
254+ * @param {ChannelConfig } config - configuration of this channel.
255+ * @param {function } onSuccess - callback to execute on connection success.
256+ * @param {function } onFailure - callback to execute on connection failure.
257+ * @return {* } socket connection.
258+ */
259+ function connect ( config , onSuccess , onFailure = ( ( ) => null ) ) {
253260 //still allow boolean for backwards compatibility
254- if ( opts . encrypted === false || opts . encrypted === ENCRYPTION_OFF ) {
255- var conn = net . connect ( opts . port , opts . host , onSuccess ) ;
261+ if ( config . encrypted === false || config . encrypted === ENCRYPTION_OFF ) {
262+ var conn = net . connect ( config . port , config . host , onSuccess ) ;
256263 conn . on ( 'error' , onFailure ) ;
257264 return conn ;
258- } else if ( TrustStrategy [ opts . trust ] ) {
259- return TrustStrategy [ opts . trust ] ( opts , onSuccess , onFailure ) ;
265+ } else if ( TrustStrategy [ config . trust ] ) {
266+ return TrustStrategy [ config . trust ] ( config , onSuccess , onFailure ) ;
260267 } else {
261- onFailure ( newError ( "Unknown trust strategy: " + opts . trust + ". Please use either " +
268+ onFailure ( newError ( "Unknown trust strategy: " + config . trust + ". Please use either " +
262269 "trust:'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES' or trust:'TRUST_ALL_CERTIFICATES' in your driver " +
263270 "configuration. Alternatively, you can disable encryption by setting " +
264271 "`encrypted:\"" + ENCRYPTION_OFF + "\"`. There is no mechanism to use encryption without trust verification, " +
@@ -277,11 +284,9 @@ class NodeChannel {
277284
278285 /**
279286 * Create new instance
280- * @param {Object } opts - Options object
281- * @param {string } opts.host - The host, including protocol to connect to.
282- * @param {Integer } opts.port - The port to use.
287+ * @param {ChannelConfig } config - configuration for this channel.
283288 */
284- constructor ( opts ) {
289+ constructor ( config ) {
285290 let self = this ;
286291
287292 this . id = _CONNECTION_IDGEN ++ ;
@@ -291,9 +296,10 @@ class NodeChannel {
291296 this . _error = null ;
292297 this . _handleConnectionError = this . _handleConnectionError . bind ( this ) ;
293298 this . _handleConnectionTerminated = this . _handleConnectionTerminated . bind ( this ) ;
299+ this . _connectionErrorCode = config . connectionErrorCode ;
294300
295- this . _encrypted = opts . encrypted ;
296- this . _conn = connect ( opts , ( ) => {
301+ this . _encrypted = config . encrypted ;
302+ this . _conn = connect ( config , ( ) => {
297303 if ( ! self . _open ) {
298304 return ;
299305 }
@@ -318,14 +324,14 @@ class NodeChannel {
318324
319325 _handleConnectionError ( err ) {
320326 let msg = err . message || 'Failed to connect to server' ;
321- this . _error = newError ( msg , SESSION_EXPIRED ) ;
327+ this . _error = newError ( msg , this . _connectionErrorCode ) ;
322328 if ( this . onerror ) {
323329 this . onerror ( this . _error ) ;
324330 }
325331 }
326332
327333 _handleConnectionTerminated ( ) {
328- this . _error = newError ( 'Connection was closed by server' , SESSION_EXPIRED ) ;
334+ this . _error = newError ( 'Connection was closed by server' , this . _connectionErrorCode ) ;
329335 if ( this . onerror ) {
330336 this . onerror ( this . _error ) ;
331337 }
0 commit comments