18
18
*/
19
19
20
20
import Session from './session' ;
21
- import { Pool } from './internal/pool' ;
21
+ import Pool from './internal/pool' ;
22
+ import Integer from './integer' ;
22
23
import { connect } from "./internal/connector" ;
23
24
import StreamObserver from './internal/stream-observer' ;
24
- import { VERSION } from '../version' ;
25
+ import { newError , SERVICE_UNAVAILABLE } from "./error" ;
26
+ import "babel-polyfill" ;
25
27
28
+ let READ = 'READ' , WRITE = 'WRITE' ;
26
29
/**
27
30
* A driver maintains one or more {@link Session sessions} with a remote
28
31
* Neo4j instance. Through the {@link Session sessions} you can send statements
@@ -53,7 +56,7 @@ class Driver {
53
56
this . _pool = new Pool (
54
57
this . _createConnection . bind ( this ) ,
55
58
this . _destroyConnection . bind ( this ) ,
56
- this . _validateConnection . bind ( this ) ,
59
+ Driver . _validateConnection . bind ( this ) ,
57
60
config . connectionPoolSize
58
61
) ;
59
62
}
@@ -63,13 +66,13 @@ class Driver {
63
66
* @return {Connection } new connector-api session instance, a low level session API.
64
67
* @access private
65
68
*/
66
- _createConnection ( release ) {
69
+ _createConnection ( url , release ) {
67
70
let sessionId = this . _sessionIdGenerator ++ ;
68
71
let streamObserver = new _ConnectionStreamObserver ( this ) ;
69
- let conn = connect ( this . _url , this . _config ) ;
72
+ let conn = connect ( url , this . _config ) ;
70
73
conn . initialize ( this . _userAgent , this . _token , streamObserver ) ;
71
74
conn . _id = sessionId ;
72
- conn . _release = ( ) => release ( conn ) ;
75
+ conn . _release = ( ) => release ( this . _url , conn ) ;
73
76
74
77
this . _openSessions [ sessionId ] = conn ;
75
78
return conn ;
@@ -80,7 +83,7 @@ class Driver {
80
83
* @return {boolean } true if the connection is open
81
84
* @access private
82
85
**/
83
- _validateConnection ( conn ) {
86
+ static _validateConnection ( conn ) {
84
87
return conn . isOpen ( ) ;
85
88
}
86
89
@@ -89,7 +92,7 @@ class Driver {
89
92
* @return {Session } new session.
90
93
* @access private
91
94
*/
92
- _destroyConnection ( conn ) {
95
+ _destroyConnection ( conn ) {
93
96
delete this . _openSessions [ conn . _id ] ;
94
97
conn . close ( ) ;
95
98
}
@@ -105,11 +108,19 @@ class Driver {
105
108
* it is returned to the pool, the session will be reset to a clean state and
106
109
* made available for others to use.
107
110
*
111
+ * @param {String } mode of session - optional
108
112
* @return {Session } new session.
109
113
*/
110
- session ( ) {
111
- let conn = this . _pool . acquire ( ) ;
112
- return new Session ( conn , ( cb ) => {
114
+ session ( mode ) {
115
+ let connectionPromise = this . _acquireConnection ( mode ) ;
116
+ connectionPromise . catch ( ( err ) => {
117
+ if ( this . onError && err . code === SERVICE_UNAVAILABLE ) {
118
+ this . onError ( err ) ;
119
+ } else {
120
+ //we don't need to tell the driver about this error
121
+ }
122
+ } ) ;
123
+ return this . _createSession ( connectionPromise , ( cb ) => {
113
124
// This gets called on Session#close(), and is where we return
114
125
// the pooled 'connection' instance.
115
126
@@ -119,17 +130,32 @@ class Driver {
119
130
120
131
// Queue up a 'reset', to ensure the next user gets a clean
121
132
// session to work with.
122
- conn . reset ( ) ;
123
- conn . sync ( ) ;
124
133
125
- // Return connection to the pool
126
- conn . _release ( ) ;
134
+ connectionPromise . then ( ( conn ) => {
135
+ conn . reset ( ) ;
136
+ conn . sync ( ) ;
137
+
138
+ // Return connection to the pool
139
+ conn . _release ( ) ;
140
+ } ) . catch ( ( ) => { /*ignore errors here*/ } ) ;
127
141
128
142
// Call user callback
129
- if ( cb ) { cb ( ) ; }
143
+ if ( cb ) {
144
+ cb ( ) ;
145
+ }
130
146
} ) ;
131
147
}
132
148
149
+ //Extension point
150
+ _acquireConnection ( mode ) {
151
+ return Promise . resolve ( this . _pool . acquire ( this . _url ) ) ;
152
+ }
153
+
154
+ //Extension point
155
+ _createSession ( connectionPromise , cb ) {
156
+ return new Session ( connectionPromise , cb ) ;
157
+ }
158
+
133
159
/**
134
160
* Close all open sessions and other associated resources. You should
135
161
* make sure to use this when you are done with this driver instance.
@@ -140,6 +166,7 @@ class Driver {
140
166
if ( this . _openSessions . hasOwnProperty ( sessionId ) ) {
141
167
this . _openSessions [ sessionId ] . close ( ) ;
142
168
}
169
+ this . _pool . purgeAll ( ) ;
143
170
}
144
171
}
145
172
}
@@ -151,83 +178,26 @@ class _ConnectionStreamObserver extends StreamObserver {
151
178
this . _driver = driver ;
152
179
this . _hasFailed = false ;
153
180
}
181
+
154
182
onError ( error ) {
155
183
if ( ! this . _hasFailed ) {
156
184
super . onError ( error ) ;
157
- if ( this . _driver . onError ) {
185
+ if ( this . _driver . onError ) {
158
186
this . _driver . onError ( error ) ;
159
187
}
160
188
this . _hasFailed = true ;
161
189
}
162
190
}
191
+
163
192
onCompleted ( message ) {
164
- if ( this . _driver . onCompleted ) {
165
- this . _driver . onCompleted ( message ) ;
193
+ if ( this . _driver . onCompleted ) {
194
+ this . _driver . onCompleted ( message ) ;
166
195
}
167
196
}
168
197
}
169
198
170
- let USER_AGENT = "neo4j-javascript/" + VERSION ;
171
199
172
- /**
173
- * Construct a new Neo4j Driver. This is your main entry point for this
174
- * library.
175
- *
176
- * ## Configuration
177
- *
178
- * This function optionally takes a configuration argument. Available configuration
179
- * options are as follows:
180
- *
181
- * {
182
- * // Encryption level: one of ENCRYPTION_ON, ENCRYPTION_OFF or ENCRYPTION_NON_LOCAL.
183
- * // ENCRYPTION_NON_LOCAL is on by default in modern NodeJS installs,
184
- * // but off by default in the Web Bundle and old (<=1.0.0) NodeJS installs
185
- * // due to technical limitations on those platforms.
186
- * encrypted: ENCRYPTION_ON|ENCRYPTION_OFF|ENCRYPTION_NON_LOCAL
187
- *
188
- * // Trust strategy to use if encryption is enabled. There is no mode to disable
189
- * // trust other than disabling encryption altogether. The reason for
190
- * // this is that if you don't know who you are talking to, it is easy for an
191
- * // attacker to hijack your encrypted connection, rendering encryption pointless.
192
- * //
193
- * // TRUST_ON_FIRST_USE is the default for modern NodeJS deployments, and works
194
- * // similarly to how `ssl` works - the first time we connect to a new host,
195
- * // we remember the certificate they use. If the certificate ever changes, we
196
- * // assume it is an attempt to hijack the connection and require manual intervention.
197
- * // This means that by default, connections "just work" while still giving you
198
- * // good encrypted protection.
199
- * //
200
- * // TRUST_CUSTOM_CA_SIGNED_CERTIFICATES is the classic approach to trust verification -
201
- * // whenever we establish an encrypted connection, we ensure the host is using
202
- * // an encryption certificate that is in, or is signed by, a certificate listed
203
- * // as trusted. In the web bundle, this list of trusted certificates is maintained
204
- * // by the web browser. In NodeJS, you configure the list with the next config option.
205
- * //
206
- * // TRUST_SYSTEM_CA_SIGNED_CERTIFICATES meand that you trust whatever certificates
207
- * // are in the default certificate chain of th
208
- * trust: "TRUST_ON_FIRST_USE" | "TRUST_SIGNED_CERTIFICATES" | TRUST_CUSTOM_CA_SIGNED_CERTIFICATES | TRUST_SYSTEM_CA_SIGNED_CERTIFICATES,
209
- *
210
- * // List of one or more paths to trusted encryption certificates. This only
211
- * // works in the NodeJS bundle, and only matters if you use "TRUST_CUSTOM_CA_SIGNED_CERTIFICATES".
212
- * // The certificate files should be in regular X.509 PEM format.
213
- * // For instance, ['./trusted.pem']
214
- * trustedCertificates: [],
215
- *
216
- * // Path to a file where the driver saves hosts it has seen in the past, this is
217
- * // very similar to the ssl tool's known_hosts file. Each time we connect to a
218
- * // new host, a hash of their certificate is stored along with the domain name and
219
- * // port, and this is then used to verify the host certificate does not change.
220
- * // This setting has no effect unless TRUST_ON_FIRST_USE is enabled.
221
- * knownHosts:"~/.neo4j/known_hosts",
222
- * }
223
- *
224
- * @param {string } url The URL for the Neo4j database, for instance "bolt://localhost"
225
- * @param {Map<String,String> } authToken Authentication credentials. See {@link auth} for helpers.
226
- * @param {Object } config Configuration object. See the configuration section above for details.
227
- * @returns {Driver }
228
- */
229
- function driver ( url , authToken , config = { } ) {
230
- return new Driver ( url , USER_AGENT , authToken , config ) ;
231
- }
232
200
233
- export { Driver , driver }
201
+ export { Driver , READ , WRITE }
202
+
203
+ export default Driver
0 commit comments