Skip to content

Commit 402f3ea

Browse files
committed
Moving creation of providers to the index.js
1 parent 2ce5354 commit 402f3ea

File tree

3 files changed

+82
-128
lines changed

3 files changed

+82
-128
lines changed

src/driver.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
import ConnectionProvider from './internal/connection-provider'
2121
import Bookmark from './internal/bookmark'
22-
import DirectConnectionProvider from './internal/connection-provider-direct'
2322
import ConnectivityVerifier from './internal/connectivity-verifier'
23+
import ConfiguredCustomResolver from './internal/resolver/configured-custom-resolver'
24+
2425
import {
2526
ACCESS_MODE_READ,
2627
ACCESS_MODE_WRITE,
@@ -74,20 +75,23 @@ class Driver {
7475
* You should not be calling this directly, instead use {@link driver}.
7576
* @constructor
7677
* @protected
77-
* @param {ServerAddress} address
78-
* @param {string} userAgent
79-
* @param {Object} authToken
78+
* @param {Object} meta Metainformation about the driver
8079
* @param {Object} config
80+
* @param {function(id: number, config:Object, log:Logger, hostNameResolver: ConfiguredCustomResolver): ConnectionProvider } createConnectonProvider Creates the connection provider
8181
*/
82-
constructor (address, userAgent, authToken = {}, config = {}) {
82+
constructor (
83+
meta,
84+
config = {},
85+
createConnectonProvider = (id, config, log, hostNameResolver) => {}
86+
) {
8387
sanitizeConfig(config)
88+
validateConfig(config)
8489

8590
this._id = idGenerator++
86-
this._address = address
87-
this._userAgent = userAgent
88-
this._authToken = authToken
91+
this._meta = meta
8992
this._config = config
9093
this._log = Logger.create(config)
94+
this._createConnectionProvider = createConnectonProvider
9195

9296
/**
9397
* Reference to the connection provider. Initialized lazily by {@link _getOrCreateConnectionProvider}.
@@ -144,7 +148,7 @@ class Driver {
144148
* @returns {boolean}
145149
*/
146150
_supportsRouting () {
147-
return false
151+
return this._meta.routing
148152
}
149153

150154
/**
@@ -259,24 +263,10 @@ class Driver {
259263
*/
260264
_afterConstruction () {
261265
this._log.info(
262-
`Direct driver ${this._id} created for server address ${this._address}`
266+
`${this._meta.typename} driver ${this._id} created for server address ${this._meta.address}`
263267
)
264268
}
265269

266-
/**
267-
* @protected
268-
*/
269-
_createConnectionProvider (address, userAgent, authToken) {
270-
return new DirectConnectionProvider({
271-
id: this._id,
272-
config: this._config,
273-
log: this._log,
274-
address: address,
275-
userAgent: userAgent,
276-
authToken: authToken
277-
})
278-
}
279-
280270
/**
281271
* @private
282272
*/
@@ -309,16 +299,31 @@ class Driver {
309299
_getOrCreateConnectionProvider () {
310300
if (!this._connectionProvider) {
311301
this._connectionProvider = this._createConnectionProvider(
312-
this._address,
313-
this._userAgent,
314-
this._authToken
302+
this._id,
303+
this._config,
304+
this._log,
305+
createHostNameResolver(this._config)
315306
)
316307
}
317308

318309
return this._connectionProvider
319310
}
320311
}
321312

313+
/**
314+
* @private
315+
* @returns {Object} the given config.
316+
*/
317+
function validateConfig (config) {
318+
const resolver = config.resolver
319+
if (resolver && typeof resolver !== 'function') {
320+
throw new TypeError(
321+
`Configured resolver should be a function. Got: ${resolver}`
322+
)
323+
}
324+
return config
325+
}
326+
322327
/**
323328
* @private
324329
*/
@@ -371,6 +376,15 @@ function validateFetchSizeValue (rawValue, defaultWhenAbsent) {
371376
}
372377
}
373378

379+
/**
380+
* @private
381+
* @returns {ConfiguredCustomResolver} new custom resolver that wraps the passed-in resolver function.
382+
* If resolved function is not specified, it defaults to an identity resolver.
383+
*/
384+
function createHostNameResolver (config) {
385+
return new ConfiguredCustomResolver(config.resolver)
386+
}
387+
374388
export { Driver, READ, WRITE }
375389

376390
export default Driver

src/index.js

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* limitations under the License.
1818
*/
1919
import { Driver, READ, WRITE } from './driver'
20-
import RoutingDriver from './routing-driver'
2120
import VERSION from './version'
2221
import urlUtil from './internal/url-util'
2322
import ServerAddress from './internal/server-address'
@@ -55,6 +54,9 @@ import {
5554
ResultSummary,
5655
Result
5756
} from 'neo4j-driver-core'
57+
import { HostNameResolver } from './internal/node'
58+
import DirectConnectionProvider from './internal/connection-provider-direct'
59+
import RoutingConnectionProvider from './internal/connection-provider-routing'
5860

5961
const {
6062
util: { ENCRYPTION_ON, ENCRYPTION_OFF, assertString, isEmptyObjectOrNull }
@@ -236,27 +238,46 @@ function driver (url, authToken, config = {}) {
236238

237239
// Use default user agent or user agent specified by user.
238240
config.userAgent = config.userAgent || USER_AGENT
241+
const address = ServerAddress.fromUrl(parsedUrl.hostAndPort)
239242

240-
if (routing) {
241-
return new RoutingDriver(
242-
ServerAddress.fromUrl(parsedUrl.hostAndPort),
243-
parsedUrl.query,
244-
config.userAgent,
245-
authToken,
246-
config
247-
)
248-
} else {
249-
if (!isEmptyObjectOrNull(parsedUrl.query)) {
250-
throw new Error(
251-
`Parameters are not supported with none routed scheme. Given URL: '${url}'`
252-
)
243+
const meta = {
244+
address,
245+
typename: routing ? 'Routing' : 'Direct',
246+
routing
247+
}
248+
249+
return new Driver(meta, config, createConnectionProviderFunction())
250+
251+
function createConnectionProviderFunction () {
252+
if (routing) {
253+
return (id, config, log, hostNameResolver) =>
254+
new RoutingConnectionProvider({
255+
id,
256+
config,
257+
log,
258+
hostNameResolver,
259+
authToken,
260+
address,
261+
userAgent: config.userAgent,
262+
routingContext: parsedUrl.query
263+
})
264+
} else {
265+
if (!isEmptyObjectOrNull(parsedUrl.query)) {
266+
throw new Error(
267+
`Parameters are not supported with none routed scheme. Given URL: '${url}'`
268+
)
269+
}
270+
271+
return (id, config, log) =>
272+
new DirectConnectionProvider({
273+
id,
274+
config,
275+
log,
276+
authToken,
277+
address,
278+
userAgent: config.userAgent
279+
})
253280
}
254-
return new Driver(
255-
ServerAddress.fromUrl(parsedUrl.hostAndPort),
256-
config.userAgent,
257-
authToken,
258-
config
259-
)
260281
}
261282
}
262283

src/routing-driver.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)