@@ -21,24 +21,35 @@ import Session from './session';
2121import { Pool } from './internal/pool' ;
2222import { connect } from "./internal/connector" ;
2323import StreamObserver from './internal/stream-observer' ;
24+ import { VERSION } from '../version' ;
2425
2526/**
26- * A Driver instance is used for mananging {@link Session}s.
27- * @access public
28- */
27+ * A driver maintains one or more {@link Session sessions} with a remote
28+ * Neo4j instance. Through the {@link Session sessions} you can send statements
29+ * and retrieve results from the database.
30+ *
31+ * Drivers are reasonably expensive to create - you should strive to keep one
32+ * driver instance around per Neo4j Instance you connect to.
33+ *
34+ * @access public
35+ */
2936class Driver {
3037 /**
38+ * You should not be calling this directly, instead use {@link driver}.
3139 * @constructor
3240 * @param {string } url
3341 * @param {string } userAgent
3442 * @param {Object } token
43+ * @param {Object } config
44+ * @access private
3545 */
36- constructor ( url , userAgent , token ) {
46+ constructor ( url , userAgent = 'neo4j-javascript/0.0' , token = { } , config = { } ) {
3747 this . _url = url ;
38- this . _userAgent = userAgent || 'neo4j-javascript/0.0' ;
48+ this . _userAgent = userAgent ;
3949 this . _openSessions = { } ;
4050 this . _sessionIdGenerator = 0 ;
41- this . _token = token || { } ;
51+ this . _token = token ;
52+ this . _config = config ;
4253 this . _pool = new Pool (
4354 this . _createConnection . bind ( this ) ,
4455 this . _destroyConnection . bind ( this ) ,
@@ -54,7 +65,7 @@ class Driver {
5465 _createConnection ( release ) {
5566 let sessionId = this . _sessionIdGenerator ++ ;
5667 let streamObserver = new _ConnectionStreamObserver ( this ) ;
57- let conn = connect ( this . _url ) ;
68+ let conn = connect ( this . _url , this . _config ) ;
5869 conn . initialize ( this . _userAgent , this . _token , streamObserver ) ;
5970 conn . _id = sessionId ;
6071 conn . _release = ( ) => release ( conn ) ;
@@ -83,7 +94,16 @@ class Driver {
8394 }
8495
8596 /**
86- * Create and return new session
97+ * Acquire a session to communicate with the database. The driver maintains
98+ * a pool of sessions, so calling this method is normally cheap because you
99+ * will be pulling a session out of the common pool.
100+ *
101+ * This comes with some responsibility - make sure you always call
102+ * {@link Session#close()} when you are done using a session, and likewise,
103+ * make sure you don't close your session before you are done using it. Once
104+ * it is returned to the pool, the session will be reset to a clean state and
105+ * made available for others to use.
106+ *
87107 * @return {Session } new session.
88108 */
89109 session ( ) {
@@ -111,8 +131,9 @@ class Driver {
111131 }
112132
113133 /**
114- * Close sessions connections
115- * @return
134+ * Close all open sessions and other associated resources. You should
135+ * make sure to use this when you are done with this driver instance.
136+ * @return undefined
116137 */
117138 close ( ) {
118139 for ( let sessionId in this . _openSessions ) {
@@ -141,4 +162,63 @@ class _ConnectionStreamObserver extends StreamObserver {
141162 }
142163}
143164
144- export default Driver
165+ let USER_AGENT = "neo4j-javascript/" + VERSION ;
166+
167+ /**
168+ * Construct a new Neo4j Driver. This is your main entry point for this
169+ * library.
170+ *
171+ * ## Configuration
172+ *
173+ * This function optionally takes a configuration argument. Available configuration
174+ * options are as follows:
175+ *
176+ * {
177+ * // Enable TLS encryption. This is on by default in modern NodeJS installs,
178+ * // but off by default in the Web Bundle and old (<=1.0.0) NodeJS installs
179+ * // due to technical limitations on those platforms.
180+ * encrypted: true|false,
181+ *
182+ * // Trust strategy to use if encryption is enabled. There is no mode to disable
183+ * // trust other than disabling encryption altogether. The reason for
184+ * // this is that if you don't know who you are talking to, it is easy for an
185+ * // attacker to hijack your encrypted connection, rendering encryption pointless.
186+ * //
187+ * // TRUST_ON_FIRST_USE is the default for modern NodeJS deployments, and works
188+ * // similarly to how `ssl` works - the first time we connect to a new host,
189+ * // we remember the certificate they use. If the certificate ever changes, we
190+ * // assume it is an attempt to hijack the connection and require manual intervention.
191+ * // This means that by default, connections "just work" while still giving you
192+ * // good encrypted protection.
193+ * //
194+ * // TRUST_SIGNED_CERTIFICATES is the classic approach to trust verification -
195+ * // whenever we establish an encrypted connection, we ensure the host is using
196+ * // an encryption certificate that is in, or is signed by, a certificate listed
197+ * // as trusted. In the web bundle, this list of trusted certificates is maintained
198+ * // by the web browser. In NodeJS, you configure the list with the next config option.
199+ * trust: "TRUST_ON_FIRST_USE" | "TRUST_SIGNED_CERTIFICATES",
200+ *
201+ * // List of one or more paths to trusted encryption certificates. This only
202+ * // works in the NodeJS bundle, and only matters if you use "TRUST_SIGNED_CERTIFICATES".
203+ * // The certificate files should be in regular X.509 PEM format.
204+ * // For instance, ['./trusted.pem']
205+ * trustedCertificates: [],
206+ *
207+ * // Path to a file where the driver saves hosts it has seen in the past, this is
208+ * // very similar to the ssl tool's known_hosts file. Each time we connect to a
209+ * // new host, a hash of their certificate is stored along with the domain name and
210+ * // port, and this is then used to verify the host certificate does not change.
211+ * // This setting has no effect unless TRUST_ON_FIRST_USE is enabled.
212+ * knownHosts:"~/.neo4j/known_hosts",
213+ * }
214+ *
215+ * @param {string } url The URL for the Neo4j database, for instance "bolt://localhost"
216+ * @param {Map<String,String> } authToken Authentication credentials. See {@link auth} for helpers.
217+ * @param {Object } config Configuration object. See the configuration section above for details.
218+ * @returns {Driver }
219+ */
220+ function driver ( url , authToken , config = { } ) {
221+ return new Driver ( url , USER_AGENT , authToken , config ) ;
222+ }
223+
224+ export { Driver , driver }
0 commit comments